Mercurial > projects > dynamin
annotate dynamin/gui/list_box.d @ 55:c138461bf845
Add focusing and other changes that are related
like descendantAdded/Removed events, Window.activated event, and updating List.
Window.state was also added, even though focusing does not depend on it.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Sat, 08 Aug 2009 15:42:27 -0500 |
parents | ccc108b25a0a |
children | 651082a9b364 |
rev | line source |
---|---|
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: | |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
10
diff
changeset
|
41 List!(string, true) _items; |
0 | 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 | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
81 /// Override this method in a subclass to handle the selectionChanged event. |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
82 protected void whenSelectionChanged(EventArgs e) { } |
0 | 83 /// This event occurs after the selection has changed. |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
84 Event!(whenSelectionChanged) selectionChanged; |
0 | 85 |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
10
diff
changeset
|
86 void whenListItemsChanged(string, int) { |
0 | 87 super.layout(); |
88 repaint(); | |
89 } | |
90 | |
91 /// | |
92 this() { | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
93 selectionChanged.mainHandler = &whenSelectionChanged; |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
10
diff
changeset
|
94 _items = new List!(string, true)(&whenListItemsChanged, &whenListItemsChanged); |
0 | 95 |
96 super(); | |
97 _focusable = true; | |
98 // TODO: hack | |
99 backColor = WindowsTheme.getColor(5); | |
100 } | |
101 /// | |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
10
diff
changeset
|
102 List!(string, true) items() { |
0 | 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 |