1 /** DGui project file.
2 
3 Copyright: Trogu Antonio Davide 2011-2013
4 
5 License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
6 
7 Authors: Trogu Antonio Davide
8 */
9 module dgui.listbox;
10 
11 import std.utf: toUTFz;
12 public import dgui.core.controls.ownerdrawcontrol;
13 import dgui.core.utils;
14 
15 class ListBox: OwnerDrawControl
16 {
17 	private static class StringItem
18 	{
19 		private string _str;
20 
21 		public this(string s)
22 		{
23 			this._str = s;
24 		}
25 
26 		public override string toString()
27 		{
28 			return this._str;
29 		}
30 	}
31 
32 	public Event!(Control, EventArgs) itemChanged;
33 
34 	private Collection!(Object) _items;
35 	private Object _selectedItem;
36 	private int _selectedIndex;
37 
38 	public final int addItem(string s)
39 	{
40 		return this.addItem(new StringItem(s));
41 	}
42 
43 	public final int addItem(Object obj)
44 	{
45 		if(!this._items)
46 		{
47 			this._items = new Collection!(Object)();
48 		}
49 
50 		this._items.add(obj);
51 
52 		if(this.created)
53 		{
54 			return this.createItem(obj);
55 		}
56 
57 		return this._items.length - 1;
58 	}
59 
60 	public final void removeItem(int idx)
61 	{
62 		if(this.created)
63 		{
64 			this.sendMessage(LB_DELETESTRING, idx, 0);
65 		}
66 
67 		this._items.removeAt(idx);
68 	}
69 
70 	@property public final int selectedIndex()
71 	{
72 		if(this.created)
73 		{
74 			return this.sendMessage(LB_GETCURSEL, 0, 0);
75 		}
76 
77 		return this._selectedIndex;
78 	}
79 
80 	@property public final void selectedIndex(int i)
81 	{
82 		this._selectedIndex = i;
83 
84 		if(this.created)
85 		{
86 			this.sendMessage(LB_SETCURSEL, i, 0);
87 		}
88 	}
89 
90 	@property public final Object selectedItem()
91 	{
92 		int idx = this.selectedIndex;
93 
94 		if(this._items)
95 		{
96 			return this._items[idx];
97 		}
98 
99 		return null;
100 	}
101 
102 	@property public final string selectedString()
103 	{
104 		Object obj = this.selectedItem;
105 		return (obj ? obj.toString() : null);
106 	}
107 
108 	@property public final Object[] items()
109 	{
110 		if(this._items)
111 		{
112 			return this._items.get();
113 		}
114 
115 		return null;
116 	}
117 
118 	private int createItem(Object obj)
119 	{
120 		return this.sendMessage(LB_ADDSTRING, 0, cast(LPARAM)toUTFz!(wchar*)(obj.toString()));
121 	}
122 
123 	protected void onItemChanged(EventArgs e)
124 	{
125 		this.itemChanged(this, e);
126 	}
127 
128 	protected override void createControlParams(ref CreateControlParams ccp)
129 	{
130 		/* LBN_SELCHANGE: This notification code is sent only by a list box that has the LBS_NOTIFY style (by MSDN) */
131 		this.setStyle(LBS_NOINTEGRALHEIGHT | LBS_NOTIFY, true);
132 		this.setExStyle(WS_EX_CLIENTEDGE, true);
133 
134 		ccp.superclassName = WC_LISTBOX;
135 		ccp.className = WC_DLISTBOX;
136 		ccp.defaultBackColor = SystemColors.colorWindow;
137 
138 		switch(this._drawMode)
139 		{
140 			case OwnerDrawMode.fixed:
141 				this.setStyle(LBS_OWNERDRAWFIXED, true);
142 				break;
143 
144 			case OwnerDrawMode.variable:
145 				this.setStyle(LBS_OWNERDRAWVARIABLE, true);
146 				break;
147 
148 			default:
149 				break;
150 		}
151 
152 		super.createControlParams(ccp);
153 	}
154 
155 	protected override void onReflectedMessage(ref Message m)
156 	{
157 		if(m.msg == WM_COMMAND && HIWORD(m.wParam) == LBN_SELCHANGE)
158 		{
159 			this._selectedIndex = this.sendMessage(LB_GETCURSEL, 0, 0);
160 			this.onItemChanged(EventArgs.empty);
161 		}
162 
163 		super.onReflectedMessage(m);
164 	}
165 
166 	protected override void onHandleCreated(EventArgs e)
167 	{
168 		if(this._items)
169 		{
170 			foreach(Object obj; this._items)
171 			{
172 				this.createItem(obj);
173 			}
174 		}
175 
176 		super.onHandleCreated(e);
177 	}
178 }