Mercurial > projects > dynamin
annotate dynamin/gui/list_box.d @ 113:4636a64afabc default tip
Add reverse() function.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Sat, 19 Jan 2013 21:08:52 -0600 |
parents | acdbb30fee7e |
children |
rev | line source |
---|---|
0 | 1 |
2 /* | |
103
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
3 * Copyright Jordan Miner |
0 | 4 * |
103
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
5 * This Source Code Form is subject to the terms of the Mozilla Public |
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
6 * License, v. 2.0. If a copy of the MPL was not distributed with this |
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
0 | 8 * |
9 */ | |
10 | |
11 module dynamin.gui.list_box; | |
12 | |
13 import dynamin.all_painting; | |
14 import dynamin.all_core; | |
15 import dynamin.all_gui; | |
16 | |
17 /** | |
18 * A control that shows a list of items that can be selected. | |
19 * | |
20 * The appearance of a list box with Windows Classic: | |
21 * | |
22 * $(IMAGE ../web/example_list_box.png) | |
23 */ | |
24 class ListBox : Scrollable { | |
25 protected: | |
106 | 26 List!(mstring, true) _items; |
0 | 27 int _selectedIndex = -1; |
28 | |
29 override void whenKeyDown(KeyEventArgs e) { | |
30 if(e.key == Key.Down) { | |
31 if(selectedIndex + 1 < _items.count) | |
32 selectedIndex = selectedIndex + 1; | |
33 } else if(e.key == Key.Up) { | |
34 if(selectedIndex - 1 >= 0) | |
35 selectedIndex = selectedIndex - 1; | |
36 } | |
37 } | |
38 override Size bestSize() { | |
39 return Size(0, 13*_items.count); | |
40 } | |
41 override void whenContentPainting(PaintingEventArgs e) { | |
42 with(e.graphics) { | |
43 source = backColor; | |
44 paint(); | |
45 auto clip = getClipExtents(); | |
46 int start = cast(int)clip.y/13, end = cast(int)clip.bottom/13+1; | |
47 for(int i = start; i < _items.count && i < end; ++i) { | |
48 source = Color.Black; | |
49 if(i == _selectedIndex) { | |
50 // TODO: hack | |
51 //Source = WindowsTheme.getColor(dynamin.c.windows.COLOR_HIGHLIGHT); | |
52 rectangle(0, i*13, width, 13); | |
53 fill(); | |
54 //Source = WindowsTheme.getColor(dynamin.c.windows.COLOR_HIGHLIGHTTEXT); | |
55 } | |
56 drawText(_items[i], 3, i*13); | |
57 } | |
58 } | |
59 } | |
60 override void whenContentMouseDown(MouseEventArgs e) { | |
61 focus(); | |
62 selectedIndex = cast(int)e.y/13; | |
63 } | |
64 public: | |
65 | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
66 /// 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
|
67 protected void whenSelectionChanged(EventArgs e) { } |
0 | 68 /// 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
|
69 Event!(whenSelectionChanged) selectionChanged; |
0 | 70 |
106 | 71 void whenListItemsChanged(ListChangeType, mstring, mstring, uint) { |
0 | 72 super.layout(); |
73 repaint(); | |
74 } | |
75 | |
76 /// | |
77 this() { | |
78
651082a9b364
Add Event.setUp() and use in place of mainHandler and dispatcher.
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
78 selectionChanged.setUp(&whenSelectionChanged); |
106 | 79 _items = new List!(mstring, true)(&whenListItemsChanged); |
0 | 80 |
81 super(); | |
82 _focusable = true; | |
83 // TODO: hack | |
84 backColor = WindowsTheme.getColor(5); | |
85 } | |
86 /// | |
106 | 87 List!(mstring, true) items() { |
0 | 88 return _items; |
89 } | |
90 /// | |
91 int selectedIndex() { return _selectedIndex; } | |
92 /// ditto | |
93 void selectedIndex(int i) { | |
94 if(i == _selectedIndex) | |
95 return; | |
96 _selectedIndex = i; | |
97 repaint(); | |
98 selectionChanged(new EventArgs); | |
99 } | |
100 } | |
101 |