0
|
1 // Written in the D programming language
|
|
2 // www.digitalmars.com/d/
|
|
3
|
|
4 /*
|
|
5 * The contents of this file are subject to the Mozilla Public License Version
|
|
6 * 1.1 (the "License"); you may not use this file except in compliance with
|
|
7 * the License. You may obtain a copy of the License at
|
|
8 * http://www.mozilla.org/MPL/
|
|
9 *
|
|
10 * Software distributed under the License is distributed on an "AS IS" basis,
|
|
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
12 * for the specific language governing rights and limitations under the
|
|
13 * License.
|
|
14 *
|
|
15 * The Original Code is the Dynamin library.
|
|
16 *
|
|
17 * The Initial Developer of the Original Code is Jordan Miner.
|
|
18 * Portions created by the Initial Developer are Copyright (C) 2007-2009
|
|
19 * the Initial Developer. All Rights Reserved.
|
|
20 *
|
|
21 * Contributor(s):
|
|
22 * Jordan Miner <jminer7@gmail.com>
|
|
23 *
|
|
24 */
|
|
25
|
|
26 module dynamin.gui.list_box;
|
|
27
|
|
28 import dynamin.all_painting;
|
|
29 import dynamin.all_core;
|
|
30 import dynamin.all_gui;
|
|
31
|
|
32 /**
|
|
33 * A control that shows a list of items that can be selected.
|
|
34 *
|
|
35 * The appearance of a list box with Windows Classic:
|
|
36 *
|
|
37 * $(IMAGE ../web/example_list_box.png)
|
|
38 */
|
|
39 class ListBox : Scrollable {
|
|
40 protected:
|
|
41 List!(string) _items;
|
|
42 int _selectedIndex = -1;
|
|
43
|
|
44 override void whenKeyDown(KeyEventArgs e) {
|
|
45 if(e.key == Key.Down) {
|
|
46 if(selectedIndex + 1 < _items.count)
|
|
47 selectedIndex = selectedIndex + 1;
|
|
48 } else if(e.key == Key.Up) {
|
|
49 if(selectedIndex - 1 >= 0)
|
|
50 selectedIndex = selectedIndex - 1;
|
|
51 }
|
|
52 }
|
|
53 override Size bestSize() {
|
|
54 return Size(0, 13*_items.count);
|
|
55 }
|
|
56 override void whenContentPainting(PaintingEventArgs e) {
|
|
57 with(e.graphics) {
|
|
58 source = backColor;
|
|
59 paint();
|
|
60 auto clip = getClipExtents();
|
|
61 int start = cast(int)clip.y/13, end = cast(int)clip.bottom/13+1;
|
|
62 for(int i = start; i < _items.count && i < end; ++i) {
|
|
63 source = Color.Black;
|
|
64 if(i == _selectedIndex) {
|
|
65 // TODO: hack
|
|
66 //Source = WindowsTheme.getColor(dynamin.c.windows.COLOR_HIGHLIGHT);
|
|
67 rectangle(0, i*13, width, 13);
|
|
68 fill();
|
|
69 //Source = WindowsTheme.getColor(dynamin.c.windows.COLOR_HIGHLIGHTTEXT);
|
|
70 }
|
|
71 drawText(_items[i], 3, i*13);
|
|
72 }
|
|
73 }
|
|
74 }
|
|
75 override void whenContentMouseDown(MouseEventArgs e) {
|
|
76 focus();
|
|
77 selectedIndex = cast(int)e.y/13;
|
|
78 }
|
|
79 public:
|
|
80
|
|
81 /// This event occurs after the selection has changed.
|
|
82 Event!() selectionChanged;
|
|
83 /// Override this method in a subclass to handle the SelectionChanged event.
|
|
84 protected void whenSelectionChanged(EventArgs e) { }
|
|
85
|
|
86 void listItemsChanged() {
|
|
87 super.layout();
|
|
88 repaint();
|
|
89 }
|
|
90
|
|
91 ///
|
|
92 this() {
|
|
93 selectionChanged = new Event!()(&whenSelectionChanged);
|
|
94 _items = new List!(string)(&listItemsChanged);
|
|
95
|
|
96 super();
|
|
97 _focusable = true;
|
|
98 // TODO: hack
|
|
99 backColor = WindowsTheme.getColor(5);
|
|
100 }
|
|
101 ///
|
|
102 List!(string) items() {
|
|
103 return _items;
|
|
104 }
|
|
105 ///
|
|
106 int selectedIndex() { return _selectedIndex; }
|
|
107 /// ditto
|
|
108 void selectedIndex(int i) {
|
|
109 if(i == _selectedIndex)
|
|
110 return;
|
|
111 _selectedIndex = i;
|
|
112 repaint();
|
|
113 selectionChanged(new EventArgs);
|
|
114 }
|
|
115 }
|
|
116
|