]>
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 | |
c085e333 | 9 | // Licence: wxWindows license |
2bda0e17 KB |
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, | |
c085e333 VZ |
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); | |
fd71308f JS |
59 | SetBackgroundColour(parent->GetBackgroundColour()) ; |
60 | SetForegroundColour(parent->GetForegroundColour()) ; | |
bbcdf8bc | 61 | m_noStrings = n; |
2bda0e17 KB |
62 | |
63 | m_windowStyle = style; | |
64 | ||
65 | if ( id == -1 ) | |
c085e333 | 66 | m_windowId = (int)NewControlId(); |
2bda0e17 | 67 | else |
c085e333 | 68 | m_windowId = id; |
2bda0e17 KB |
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. | |
c085e333 | 85 | if ( want3D || wxStyleHasBorder(m_windowStyle) ) |
2bda0e17 KB |
86 | msStyle |= WS_BORDER; |
87 | ||
c085e333 | 88 | m_hWnd = (WXHWND)::CreateWindowEx(exStyle, "COMBOBOX", NULL, |
2bda0e17 KB |
89 | msStyle, |
90 | 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId, | |
91 | wxGetInstance(), NULL); | |
c085e333 VZ |
92 | |
93 | wxCHECK_MSG( m_hWnd, FALSE, "Failed to create combobox" ); | |
94 | ||
2bda0e17 KB |
95 | /* |
96 | #if CTL3D | |
97 | if (want3D) | |
98 | { | |
99 | m_useCtl3D = TRUE; | |
100 | Ctl3dSubclassCtl(wx_combo); // Does CTL3D affect the combobox? I think not. | |
101 | } | |
102 | #endif | |
103 | */ | |
104 | ||
2bda0e17 | 105 | // Subclass again for purposes of dialog editing mode |
c085e333 | 106 | SubclassWin(m_hWnd); |
2bda0e17 | 107 | |
c0ed460c | 108 | SetFont(parent->GetFont()); |
2bda0e17 KB |
109 | |
110 | int i; | |
111 | for (i = 0; i < n; i++) | |
c085e333 VZ |
112 | { |
113 | Append(choices[i]); | |
114 | } | |
115 | SetSelection(n); | |
2bda0e17 KB |
116 | |
117 | SetSize(x, y, width, height); | |
118 | ||
119 | return TRUE; | |
120 | } | |
121 | ||
122 | void wxChoice::Append(const wxString& item) | |
123 | { | |
124 | SendMessage((HWND) GetHWND(), CB_ADDSTRING, 0, (LONG)(const char *)item); | |
125 | ||
bbcdf8bc | 126 | m_noStrings ++; |
2bda0e17 KB |
127 | } |
128 | ||
debe6624 | 129 | void wxChoice::Delete(int n) |
2bda0e17 | 130 | { |
bbcdf8bc | 131 | m_noStrings = (int)SendMessage((HWND) GetHWND(), CB_DELETESTRING, n, 0); |
2bda0e17 KB |
132 | } |
133 | ||
134 | void wxChoice::Clear(void) | |
135 | { | |
136 | SendMessage((HWND) GetHWND(), CB_RESETCONTENT, 0, 0); | |
137 | ||
bbcdf8bc | 138 | m_noStrings = 0; |
2bda0e17 KB |
139 | } |
140 | ||
141 | ||
142 | int wxChoice::GetSelection(void) const | |
143 | { | |
144 | return (int)SendMessage((HWND) GetHWND(), CB_GETCURSEL, 0, 0); | |
145 | } | |
146 | ||
debe6624 | 147 | void wxChoice::SetSelection(int n) |
2bda0e17 KB |
148 | { |
149 | SendMessage((HWND) GetHWND(), CB_SETCURSEL, n, 0); | |
150 | } | |
151 | ||
152 | int wxChoice::FindString(const wxString& s) const | |
153 | { | |
154 | #if defined(__WATCOMC__) && defined(__WIN386__) | |
155 | // For some reason, Watcom in WIN386 mode crashes in the CB_FINDSTRINGEXACT message. | |
156 | // Do it the long way instead. | |
157 | char buf[512]; | |
158 | for (int i = 0; i < Number(); i++) | |
159 | { | |
160 | int len = (int)SendMessage((HWND) GetHWND(), CB_GETLBTEXT, i, (LPARAM)(LPSTR)buf); | |
161 | buf[len] = 0; | |
162 | if (strcmp(buf, (const char *)s) == 0) | |
163 | return i; | |
164 | } | |
165 | return -1; | |
166 | #else | |
167 | int pos = (int)SendMessage((HWND) GetHWND(), CB_FINDSTRINGEXACT, (WPARAM)-1, (LPARAM)(LPSTR)(const char *)s); | |
168 | if (pos == LB_ERR) | |
169 | return -1; | |
170 | else | |
171 | return pos; | |
172 | #endif | |
173 | } | |
174 | ||
debe6624 | 175 | wxString wxChoice::GetString(int n) const |
2bda0e17 KB |
176 | { |
177 | int len = (int)SendMessage((HWND) GetHWND(), CB_GETLBTEXT, n, (long)wxBuffer); | |
178 | wxBuffer[len] = 0; | |
179 | return wxString(wxBuffer); | |
180 | } | |
181 | ||
debe6624 | 182 | void wxChoice::SetSize(int x, int y, int width, int height, int sizeFlags) |
2bda0e17 KB |
183 | { |
184 | int currentX, currentY; | |
185 | GetPosition(¤tX, ¤tY); | |
186 | ||
187 | int x1 = x; | |
188 | int y1 = y; | |
189 | int w1 = width; | |
190 | int h1 = height; | |
191 | ||
192 | if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
193 | x1 = currentX; | |
194 | if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
195 | y1 = currentY; | |
196 | ||
81d66cf3 JS |
197 | AdjustForParentClientOrigin(x1, y1, sizeFlags); |
198 | ||
2bda0e17 KB |
199 | // If we're prepared to use the existing size, then... |
200 | if (width == -1 && height == -1 && ((sizeFlags & wxSIZE_AUTO) != wxSIZE_AUTO)) | |
201 | { | |
202 | GetSize(&w1, &h1); | |
203 | } | |
204 | ||
205 | int cx; // button font dimensions | |
206 | int cy; | |
c0ed460c | 207 | wxGetCharSize(GetHWND(), &cx, &cy, & GetFont()); |
2bda0e17 | 208 | |
1c4a764c | 209 | int control_width, control_height; |
2bda0e17 KB |
210 | |
211 | // Ignore height parameter because height doesn't | |
212 | // mean 'initially displayed' height, it refers to the | |
213 | // drop-down menu as well. The wxWindows interpretation | |
214 | // is different; also, getting the size returns the | |
215 | // _displayed_ size (NOT the drop down menu size) | |
216 | // so setting-getting-setting size would not work. | |
217 | h1 = -1; | |
218 | ||
219 | // Deal with default size (using -1 values) | |
220 | if (width <= 0) | |
221 | { | |
222 | // Find the longest string | |
bbcdf8bc | 223 | if (m_noStrings == 0) |
1c4a764c VZ |
224 | { |
225 | control_width = 100; | |
226 | } | |
2bda0e17 KB |
227 | else |
228 | { | |
debe6624 | 229 | int len, ht; |
1c4a764c | 230 | int longest = 0; |
2bda0e17 | 231 | int i; |
bbcdf8bc | 232 | for (i = 0; i < m_noStrings; i++) |
2bda0e17 KB |
233 | { |
234 | wxString str(GetString(i)); | |
c0ed460c | 235 | GetTextExtent(str, &len, &ht, NULL, NULL, & GetFont()); |
1c4a764c VZ |
236 | if ( len > longest) |
237 | longest = len; | |
2bda0e17 KB |
238 | } |
239 | ||
1c4a764c | 240 | control_width = longest + cx*5; |
2bda0e17 KB |
241 | } |
242 | } | |
fd3f686c VZ |
243 | else |
244 | { | |
245 | // If non-default width... | |
246 | control_width = w1; | |
247 | } | |
248 | ||
2bda0e17 KB |
249 | |
250 | // Choice drop-down list depends on number of items (limited to 10) | |
251 | if (h1 <= 0) | |
252 | { | |
bbcdf8bc | 253 | if (m_noStrings == 0) |
1c4a764c VZ |
254 | h1 = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*10; |
255 | else | |
256 | h1 = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*(wxMin(10, m_noStrings) + 1); | |
2bda0e17 KB |
257 | } |
258 | ||
1c4a764c | 259 | control_height = h1; |
2bda0e17 KB |
260 | |
261 | // Calculations may have made text size too small | |
262 | if (control_height <= 0) | |
1c4a764c | 263 | control_height = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy); |
2bda0e17 KB |
264 | |
265 | if (control_width <= 0) | |
1c4a764c | 266 | control_width = 100; |
2bda0e17 | 267 | |
1c4a764c VZ |
268 | MoveWindow((HWND)GetHWND(), x1, y1, |
269 | control_width, control_height, TRUE); | |
2bda0e17 KB |
270 | } |
271 | ||
debe6624 | 272 | WXHBRUSH wxChoice::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor, |
c085e333 | 273 | WXUINT message, WXWPARAM wParam, WXLPARAM lParam) |
2bda0e17 | 274 | { |
c085e333 | 275 | return 0; |
2bda0e17 KB |
276 | } |
277 | ||
278 | long wxChoice::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) | |
279 | { | |
280 | switch (nMsg) | |
281 | { | |
282 | /* | |
283 | case WM_GETDLGCODE: | |
284 | { | |
285 | if (GetWindowStyleFlag() & wxPROCESS_ENTER) | |
286 | return DLGC_WANTALLKEYS; | |
287 | break; | |
288 | } | |
289 | */ | |
290 | /* | |
291 | case WM_CHAR: // Always an ASCII character | |
292 | { | |
293 | if (wParam == VK_RETURN) | |
294 | { | |
295 | wxCommandEvent event(wxEVENT_TYPE_TEXT_ENTER_COMMAND); | |
296 | event.commandString = ((wxTextCtrl *)item)->GetValue(); | |
297 | event.eventObject = item; | |
298 | item->ProcessCommand(event); | |
299 | return FALSE; | |
300 | } | |
301 | break; | |
302 | } | |
303 | */ | |
304 | case WM_LBUTTONUP: | |
305 | { | |
306 | int x = (int)LOWORD(lParam); | |
307 | int y = (int)HIWORD(lParam); | |
308 | ||
309 | // Ok, this is truly weird, but if a panel with a wxChoice loses the | |
310 | // focus, then you get a *fake* WM_LBUTTONUP message | |
311 | // with x = 65535 and y = 65535. | |
312 | // Filter out this nonsense. | |
313 | if (x == 65535 && y == 65535) | |
314 | return Default(); | |
315 | break; | |
316 | } | |
317 | } | |
318 | ||
319 | return wxWindow::MSWWindowProc(nMsg, wParam, lParam); | |
320 | } | |
321 | ||
322 | wxString wxChoice::GetStringSelection (void) const | |
323 | { | |
324 | int sel = GetSelection (); | |
325 | if (sel > -1) | |
326 | return wxString(this->GetString (sel)); | |
327 | else | |
328 | return wxString(""); | |
329 | } | |
330 | ||
331 | bool wxChoice::SetStringSelection (const wxString& s) | |
332 | { | |
333 | int sel = FindString (s); | |
334 | if (sel > -1) | |
335 | { | |
336 | SetSelection (sel); | |
337 | return TRUE; | |
338 | } | |
339 | else | |
340 | return FALSE; | |
341 | } | |
342 | ||
343 | void wxChoice::Command(wxCommandEvent & event) | |
344 | { | |
345 | SetSelection (event.GetInt()); | |
346 | ProcessCommand (event); | |
347 | } | |
348 | ||
349 | ||
350 |