]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: choice.cpp | |
3 | // Purpose: wxChoice | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "choice.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/choice.h" | |
25 | #endif | |
26 | ||
27 | #include "wx/msw/private.h" | |
28 | ||
29 | #if !USE_SHARED_LIBRARY | |
30 | IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl) | |
31 | #endif | |
32 | ||
debe6624 | 33 | bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) |
2bda0e17 KB |
34 | { |
35 | if (param == CBN_SELCHANGE) | |
36 | { | |
5de76427 | 37 | wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId); |
2bda0e17 KB |
38 | event.SetInt(GetSelection()); |
39 | event.SetEventObject(this); | |
40 | event.SetString(copystring(GetStringSelection())); | |
41 | ProcessCommand(event); | |
42 | delete[] event.GetString(); | |
43 | return TRUE; | |
44 | } | |
45 | else return FALSE; | |
46 | } | |
47 | ||
debe6624 | 48 | bool wxChoice::Create(wxWindow *parent, wxWindowID id, |
2bda0e17 KB |
49 | const wxPoint& pos, |
50 | const wxSize& size, | |
debe6624 JS |
51 | int n, const wxString choices[], |
52 | long style, | |
2bda0e17 KB |
53 | const wxValidator& validator, |
54 | const wxString& name) | |
55 | { | |
56 | SetName(name); | |
57 | SetValidator(validator); | |
58 | if (parent) parent->AddChild(this); | |
59 | SetBackgroundColour(parent->GetDefaultBackgroundColour()) ; | |
60 | SetForegroundColour(parent->GetDefaultForegroundColour()) ; | |
bbcdf8bc | 61 | m_noStrings = n; |
2bda0e17 KB |
62 | |
63 | m_windowStyle = style; | |
64 | ||
65 | if ( id == -1 ) | |
66 | m_windowId = (int)NewControlId(); | |
67 | else | |
68 | m_windowId = id; | |
69 | ||
70 | int x = pos.x; | |
71 | int y = pos.y; | |
72 | int width = size.x; | |
73 | int height = size.y; | |
74 | ||
75 | long msStyle = WS_CHILD | CBS_DROPDOWNLIST | WS_HSCROLL | WS_VSCROLL | |
76 | | WS_TABSTOP | WS_VISIBLE; | |
77 | if (m_windowStyle & wxCB_SORT) | |
78 | msStyle |= CBS_SORT; | |
79 | ||
80 | bool want3D; | |
81 | WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ; | |
82 | ||
83 | // Even with extended styles, need to combine with WS_BORDER | |
84 | // for them to look right. | |
85 | if (want3D || (m_windowStyle & wxSIMPLE_BORDER) || (m_windowStyle & wxRAISED_BORDER) || | |
86 | (m_windowStyle & wxSUNKEN_BORDER) || (m_windowStyle & wxDOUBLE_BORDER)) | |
87 | msStyle |= WS_BORDER; | |
88 | ||
89 | HWND wx_combo = CreateWindowEx(exStyle, "COMBOBOX", NULL, | |
90 | msStyle, | |
91 | 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId, | |
92 | wxGetInstance(), NULL); | |
93 | /* | |
94 | #if CTL3D | |
95 | if (want3D) | |
96 | { | |
97 | m_useCtl3D = TRUE; | |
98 | Ctl3dSubclassCtl(wx_combo); // Does CTL3D affect the combobox? I think not. | |
99 | } | |
100 | #endif | |
101 | */ | |
102 | ||
103 | m_hWnd = (WXHWND) wx_combo; | |
104 | ||
105 | // Subclass again for purposes of dialog editing mode | |
106 | SubclassWin((WXHWND) wx_combo); | |
107 | ||
108 | SetFont(* parent->GetFont()); | |
109 | ||
110 | int i; | |
111 | for (i = 0; i < n; i++) | |
112 | SendMessage(wx_combo, CB_INSERTSTRING, i, (LONG)(const char *)choices[i]); | |
113 | SendMessage(wx_combo, CB_SETCURSEL, i, 0); | |
114 | ||
115 | SetSize(x, y, width, height); | |
116 | ||
117 | return TRUE; | |
118 | } | |
119 | ||
120 | void wxChoice::Append(const wxString& item) | |
121 | { | |
122 | SendMessage((HWND) GetHWND(), CB_ADDSTRING, 0, (LONG)(const char *)item); | |
123 | ||
bbcdf8bc | 124 | m_noStrings ++; |
2bda0e17 KB |
125 | } |
126 | ||
debe6624 | 127 | void wxChoice::Delete(int n) |
2bda0e17 | 128 | { |
bbcdf8bc | 129 | m_noStrings = (int)SendMessage((HWND) GetHWND(), CB_DELETESTRING, n, 0); |
2bda0e17 KB |
130 | } |
131 | ||
132 | void wxChoice::Clear(void) | |
133 | { | |
134 | SendMessage((HWND) GetHWND(), CB_RESETCONTENT, 0, 0); | |
135 | ||
bbcdf8bc | 136 | m_noStrings = 0; |
2bda0e17 KB |
137 | } |
138 | ||
139 | ||
140 | int wxChoice::GetSelection(void) const | |
141 | { | |
142 | return (int)SendMessage((HWND) GetHWND(), CB_GETCURSEL, 0, 0); | |
143 | } | |
144 | ||
debe6624 | 145 | void wxChoice::SetSelection(int n) |
2bda0e17 KB |
146 | { |
147 | SendMessage((HWND) GetHWND(), CB_SETCURSEL, n, 0); | |
148 | } | |
149 | ||
150 | int wxChoice::FindString(const wxString& s) const | |
151 | { | |
152 | #if defined(__WATCOMC__) && defined(__WIN386__) | |
153 | // For some reason, Watcom in WIN386 mode crashes in the CB_FINDSTRINGEXACT message. | |
154 | // Do it the long way instead. | |
155 | char buf[512]; | |
156 | for (int i = 0; i < Number(); i++) | |
157 | { | |
158 | int len = (int)SendMessage((HWND) GetHWND(), CB_GETLBTEXT, i, (LPARAM)(LPSTR)buf); | |
159 | buf[len] = 0; | |
160 | if (strcmp(buf, (const char *)s) == 0) | |
161 | return i; | |
162 | } | |
163 | return -1; | |
164 | #else | |
165 | int pos = (int)SendMessage((HWND) GetHWND(), CB_FINDSTRINGEXACT, (WPARAM)-1, (LPARAM)(LPSTR)(const char *)s); | |
166 | if (pos == LB_ERR) | |
167 | return -1; | |
168 | else | |
169 | return pos; | |
170 | #endif | |
171 | } | |
172 | ||
debe6624 | 173 | wxString wxChoice::GetString(int n) const |
2bda0e17 KB |
174 | { |
175 | int len = (int)SendMessage((HWND) GetHWND(), CB_GETLBTEXT, n, (long)wxBuffer); | |
176 | wxBuffer[len] = 0; | |
177 | return wxString(wxBuffer); | |
178 | } | |
179 | ||
debe6624 | 180 | void wxChoice::SetSize(int x, int y, int width, int height, int sizeFlags) |
2bda0e17 KB |
181 | { |
182 | int currentX, currentY; | |
183 | GetPosition(¤tX, ¤tY); | |
184 | ||
185 | int x1 = x; | |
186 | int y1 = y; | |
187 | int w1 = width; | |
188 | int h1 = height; | |
189 | ||
190 | if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
191 | x1 = currentX; | |
192 | if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
193 | y1 = currentY; | |
194 | ||
81d66cf3 JS |
195 | AdjustForParentClientOrigin(x1, y1, sizeFlags); |
196 | ||
2bda0e17 KB |
197 | // If we're prepared to use the existing size, then... |
198 | if (width == -1 && height == -1 && ((sizeFlags & wxSIZE_AUTO) != wxSIZE_AUTO)) | |
199 | { | |
200 | GetSize(&w1, &h1); | |
201 | } | |
202 | ||
203 | int cx; // button font dimensions | |
204 | int cy; | |
205 | wxGetCharSize(GetHWND(), &cx, &cy, GetFont()); | |
206 | ||
1c4a764c | 207 | int control_width, control_height; |
2bda0e17 KB |
208 | |
209 | // Ignore height parameter because height doesn't | |
210 | // mean 'initially displayed' height, it refers to the | |
211 | // drop-down menu as well. The wxWindows interpretation | |
212 | // is different; also, getting the size returns the | |
213 | // _displayed_ size (NOT the drop down menu size) | |
214 | // so setting-getting-setting size would not work. | |
215 | h1 = -1; | |
216 | ||
217 | // Deal with default size (using -1 values) | |
218 | if (width <= 0) | |
219 | { | |
220 | // Find the longest string | |
bbcdf8bc | 221 | if (m_noStrings == 0) |
1c4a764c VZ |
222 | { |
223 | control_width = 100; | |
224 | } | |
2bda0e17 KB |
225 | else |
226 | { | |
debe6624 | 227 | int len, ht; |
1c4a764c | 228 | int longest = 0; |
2bda0e17 | 229 | int i; |
bbcdf8bc | 230 | for (i = 0; i < m_noStrings; i++) |
2bda0e17 KB |
231 | { |
232 | wxString str(GetString(i)); | |
233 | GetTextExtent(str, &len, &ht, NULL, NULL,GetFont()); | |
1c4a764c VZ |
234 | if ( len > longest) |
235 | longest = len; | |
2bda0e17 KB |
236 | } |
237 | ||
1c4a764c | 238 | control_width = longest + cx*5; |
2bda0e17 KB |
239 | } |
240 | } | |
241 | ||
242 | // Choice drop-down list depends on number of items (limited to 10) | |
243 | if (h1 <= 0) | |
244 | { | |
bbcdf8bc | 245 | if (m_noStrings == 0) |
1c4a764c VZ |
246 | h1 = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*10; |
247 | else | |
248 | h1 = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*(wxMin(10, m_noStrings) + 1); | |
2bda0e17 KB |
249 | } |
250 | ||
251 | // If non-default width... | |
9c331ded | 252 | if (w1 >= 0) |
1c4a764c | 253 | control_width = w1; |
2bda0e17 | 254 | |
1c4a764c | 255 | control_height = h1; |
2bda0e17 KB |
256 | |
257 | // Calculations may have made text size too small | |
258 | if (control_height <= 0) | |
1c4a764c | 259 | control_height = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy); |
2bda0e17 KB |
260 | |
261 | if (control_width <= 0) | |
1c4a764c | 262 | control_width = 100; |
2bda0e17 | 263 | |
1c4a764c VZ |
264 | MoveWindow((HWND)GetHWND(), x1, y1, |
265 | control_width, control_height, TRUE); | |
2bda0e17 KB |
266 | } |
267 | ||
debe6624 | 268 | WXHBRUSH wxChoice::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor, |
2bda0e17 KB |
269 | WXUINT message, WXWPARAM wParam, WXLPARAM lParam) |
270 | { | |
271 | return 0; | |
272 | } | |
273 | ||
274 | long wxChoice::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) | |
275 | { | |
276 | switch (nMsg) | |
277 | { | |
278 | /* | |
279 | case WM_GETDLGCODE: | |
280 | { | |
281 | if (GetWindowStyleFlag() & wxPROCESS_ENTER) | |
282 | return DLGC_WANTALLKEYS; | |
283 | break; | |
284 | } | |
285 | */ | |
286 | /* | |
287 | case WM_CHAR: // Always an ASCII character | |
288 | { | |
289 | if (wParam == VK_RETURN) | |
290 | { | |
291 | wxCommandEvent event(wxEVENT_TYPE_TEXT_ENTER_COMMAND); | |
292 | event.commandString = ((wxTextCtrl *)item)->GetValue(); | |
293 | event.eventObject = item; | |
294 | item->ProcessCommand(event); | |
295 | return FALSE; | |
296 | } | |
297 | break; | |
298 | } | |
299 | */ | |
300 | case WM_LBUTTONUP: | |
301 | { | |
302 | int x = (int)LOWORD(lParam); | |
303 | int y = (int)HIWORD(lParam); | |
304 | ||
305 | // Ok, this is truly weird, but if a panel with a wxChoice loses the | |
306 | // focus, then you get a *fake* WM_LBUTTONUP message | |
307 | // with x = 65535 and y = 65535. | |
308 | // Filter out this nonsense. | |
309 | if (x == 65535 && y == 65535) | |
310 | return Default(); | |
311 | break; | |
312 | } | |
313 | } | |
314 | ||
315 | return wxWindow::MSWWindowProc(nMsg, wParam, lParam); | |
316 | } | |
317 | ||
318 | wxString wxChoice::GetStringSelection (void) const | |
319 | { | |
320 | int sel = GetSelection (); | |
321 | if (sel > -1) | |
322 | return wxString(this->GetString (sel)); | |
323 | else | |
324 | return wxString(""); | |
325 | } | |
326 | ||
327 | bool wxChoice::SetStringSelection (const wxString& s) | |
328 | { | |
329 | int sel = FindString (s); | |
330 | if (sel > -1) | |
331 | { | |
332 | SetSelection (sel); | |
333 | return TRUE; | |
334 | } | |
335 | else | |
336 | return FALSE; | |
337 | } | |
338 | ||
339 | void wxChoice::Command(wxCommandEvent & event) | |
340 | { | |
341 | SetSelection (event.GetInt()); | |
342 | ProcessCommand (event); | |
343 | } | |
344 | ||
345 | ||
346 |