]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
8e25c198 | 2 | // Name: src/msw/combobox.cpp |
2bda0e17 KB |
3 | // Purpose: wxComboBox class |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 01/02/97 | |
7 | // RCS-ID: $Id$ | |
6c9a19aa | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
f6bcfd97 BP |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
2bda0e17 KB |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
fddfd9e1 | 24 | #pragma hdrstop |
2bda0e17 KB |
25 | #endif |
26 | ||
af79c52d VZ |
27 | #if wxUSE_COMBOBOX |
28 | ||
670f9935 WS |
29 | #include "wx/combobox.h" |
30 | ||
2bda0e17 | 31 | #ifndef WX_PRECOMP |
57bd4c60 | 32 | #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly" |
fddfd9e1 VZ |
33 | #include "wx/settings.h" |
34 | #include "wx/log.h" | |
2b5f62a0 VZ |
35 | // for wxEVT_COMMAND_TEXT_ENTER |
36 | #include "wx/textctrl.h" | |
670f9935 | 37 | #include "wx/app.h" |
c64755ed | 38 | #include "wx/brush.h" |
2bda0e17 KB |
39 | #endif |
40 | ||
2bda0e17 | 41 | #include "wx/clipbrd.h" |
ca96978a | 42 | #include "wx/dynlib.h" |
428f8b06 | 43 | #include "wx/wupdlock.h" |
2bda0e17 KB |
44 | #include "wx/msw/private.h" |
45 | ||
63f7d502 VZ |
46 | #if wxUSE_UXTHEME |
47 | #include "wx/msw/uxtheme.h" | |
48 | #endif | |
49 | ||
f6bcfd97 | 50 | #if wxUSE_TOOLTIPS |
f6bcfd97 BP |
51 | #include "wx/tooltip.h" |
52 | #endif // wxUSE_TOOLTIPS | |
53 | ||
54 | // ---------------------------------------------------------------------------- | |
55 | // wxWin macros | |
56 | // ---------------------------------------------------------------------------- | |
57 | ||
6a89f9ee | 58 | #if wxUSE_EXTENDED_RTTI |
bc9fb572 JS |
59 | WX_DEFINE_FLAGS( wxComboBoxStyle ) |
60 | ||
3ff066a4 | 61 | wxBEGIN_FLAGS( wxComboBoxStyle ) |
bc9fb572 JS |
62 | // new style border flags, we put them first to |
63 | // use them for streaming out | |
3ff066a4 SC |
64 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) |
65 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
66 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
67 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
68 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
69 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
02b7b6b0 | 70 | |
bc9fb572 | 71 | // old style border flags |
3ff066a4 SC |
72 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) |
73 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
74 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
75 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
76 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
cb0afb26 | 77 | wxFLAGS_MEMBER(wxBORDER) |
bc9fb572 JS |
78 | |
79 | // standard window styles | |
3ff066a4 SC |
80 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) |
81 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
82 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
83 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
cb0afb26 | 84 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) |
3ff066a4 SC |
85 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) |
86 | wxFLAGS_MEMBER(wxVSCROLL) | |
87 | wxFLAGS_MEMBER(wxHSCROLL) | |
88 | ||
89 | wxFLAGS_MEMBER(wxCB_SIMPLE) | |
90 | wxFLAGS_MEMBER(wxCB_SORT) | |
91 | wxFLAGS_MEMBER(wxCB_READONLY) | |
92 | wxFLAGS_MEMBER(wxCB_DROPDOWN) | |
93 | ||
94 | wxEND_FLAGS( wxComboBoxStyle ) | |
bc9fb572 | 95 | |
0ad2a19e | 96 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxComboBox, wxChoice,"wx/combobox.h") |
6a89f9ee | 97 | |
3ff066a4 | 98 | wxBEGIN_PROPERTIES_TABLE(wxComboBox) |
02b7b6b0 | 99 | wxEVENT_PROPERTY( Select , wxEVT_COMMAND_COMBOBOX_SELECTED , wxCommandEvent ) |
3ff066a4 | 100 | wxEVENT_PROPERTY( TextEnter , wxEVT_COMMAND_TEXT_ENTER , wxCommandEvent ) |
c5ca409b | 101 | |
6a89f9ee | 102 | // TODO DELEGATES |
02b7b6b0 | 103 | wxPROPERTY( Font , wxFont , SetFont , GetFont , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) |
3ff066a4 | 104 | wxPROPERTY_COLLECTION( Choices , wxArrayString , wxString , AppendString , GetStrings , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) |
02b7b6b0 WS |
105 | wxPROPERTY( Value ,wxString, SetValue, GetValue, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) |
106 | wxPROPERTY( Selection ,int, SetSelection, GetSelection, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
af498247 | 107 | wxPROPERTY_FLAGS( WindowStyle , wxComboBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style |
3ff066a4 | 108 | wxEND_PROPERTIES_TABLE() |
6a89f9ee | 109 | |
3ff066a4 SC |
110 | wxBEGIN_HANDLERS_TABLE(wxComboBox) |
111 | wxEND_HANDLERS_TABLE() | |
2bda0e17 | 112 | |
3ff066a4 | 113 | wxCONSTRUCTOR_5( wxComboBox , wxWindow* , Parent , wxWindowID , Id , wxString , Value , wxPoint , Position , wxSize , Size ) |
bb8d428f | 114 | |
6a89f9ee | 115 | #else |
bb8d428f | 116 | |
0ad2a19e | 117 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxChoice) |
150e31d2 | 118 | |
bb8d428f WS |
119 | #endif |
120 | ||
150e31d2 JS |
121 | BEGIN_EVENT_TABLE(wxComboBox, wxControl) |
122 | EVT_MENU(wxID_CUT, wxComboBox::OnCut) | |
123 | EVT_MENU(wxID_COPY, wxComboBox::OnCopy) | |
124 | EVT_MENU(wxID_PASTE, wxComboBox::OnPaste) | |
125 | EVT_MENU(wxID_UNDO, wxComboBox::OnUndo) | |
126 | EVT_MENU(wxID_REDO, wxComboBox::OnRedo) | |
127 | EVT_MENU(wxID_CLEAR, wxComboBox::OnDelete) | |
128 | EVT_MENU(wxID_SELECTALL, wxComboBox::OnSelectAll) | |
129 | ||
130 | EVT_UPDATE_UI(wxID_CUT, wxComboBox::OnUpdateCut) | |
131 | EVT_UPDATE_UI(wxID_COPY, wxComboBox::OnUpdateCopy) | |
132 | EVT_UPDATE_UI(wxID_PASTE, wxComboBox::OnUpdatePaste) | |
133 | EVT_UPDATE_UI(wxID_UNDO, wxComboBox::OnUpdateUndo) | |
134 | EVT_UPDATE_UI(wxID_REDO, wxComboBox::OnUpdateRedo) | |
135 | EVT_UPDATE_UI(wxID_CLEAR, wxComboBox::OnUpdateDelete) | |
136 | EVT_UPDATE_UI(wxID_SELECTALL, wxComboBox::OnUpdateSelectAll) | |
137 | END_EVENT_TABLE() | |
138 | ||
f6bcfd97 BP |
139 | // ---------------------------------------------------------------------------- |
140 | // function prototypes | |
141 | // ---------------------------------------------------------------------------- | |
142 | ||
143 | LRESULT APIENTRY _EXPORT wxComboEditWndProc(HWND hWnd, | |
144 | UINT message, | |
145 | WPARAM wParam, | |
146 | LPARAM lParam); | |
147 | ||
148 | // --------------------------------------------------------------------------- | |
149 | // global vars | |
150 | // --------------------------------------------------------------------------- | |
151 | ||
152 | // the pointer to standard radio button wnd proc | |
975b6bcf | 153 | static WNDPROC gs_wndprocEdit = (WNDPROC)NULL; |
f6bcfd97 BP |
154 | |
155 | // ============================================================================ | |
156 | // implementation | |
157 | // ============================================================================ | |
158 | ||
159 | // ---------------------------------------------------------------------------- | |
160 | // wnd proc for subclassed edit control | |
161 | // ---------------------------------------------------------------------------- | |
162 | ||
163 | LRESULT APIENTRY _EXPORT wxComboEditWndProc(HWND hWnd, | |
164 | UINT message, | |
165 | WPARAM wParam, | |
166 | LPARAM lParam) | |
167 | { | |
168 | HWND hwndCombo = ::GetParent(hWnd); | |
169 | wxWindow *win = wxFindWinFromHandle((WXHWND)hwndCombo); | |
170 | ||
171 | switch ( message ) | |
172 | { | |
53c3a78b VZ |
173 | // forward some messages to the combobox to generate the appropriate |
174 | // wxEvents from them | |
f6bcfd97 BP |
175 | case WM_KEYUP: |
176 | case WM_KEYDOWN: | |
177 | case WM_CHAR: | |
a1254663 VZ |
178 | case WM_SYSCHAR: |
179 | case WM_SYSKEYDOWN: | |
180 | case WM_SYSKEYUP: | |
53c3a78b VZ |
181 | case WM_SETFOCUS: |
182 | case WM_KILLFOCUS: | |
f6bcfd97 BP |
183 | { |
184 | wxComboBox *combo = wxDynamicCast(win, wxComboBox); | |
64b39766 VZ |
185 | if ( !combo ) |
186 | { | |
187 | // we can get WM_KILLFOCUS while our parent is already half | |
188 | // destroyed and hence doesn't look like a combobx any | |
189 | // longer, check for it to avoid bogus assert failures | |
190 | if ( !win->IsBeingDeleted() ) | |
191 | { | |
9a83f860 | 192 | wxFAIL_MSG( wxT("should have combo as parent") ); |
64b39766 VZ |
193 | } |
194 | } | |
195 | else if ( combo->MSWProcessEditMsg(message, wParam, lParam) ) | |
196 | { | |
197 | // handled by parent | |
f6bcfd97 | 198 | return 0; |
64b39766 | 199 | } |
f6bcfd97 BP |
200 | } |
201 | break; | |
202 | ||
f6bcfd97 BP |
203 | case WM_GETDLGCODE: |
204 | { | |
9a83f860 | 205 | wxCHECK_MSG( win, 0, wxT("should have a parent") ); |
f6bcfd97 | 206 | |
8e13c1ec | 207 | if ( win->GetWindowStyle() & wxTE_PROCESS_ENTER ) |
f6bcfd97 BP |
208 | { |
209 | // need to return a custom dlg code or we'll never get it | |
210 | return DLGC_WANTMESSAGE; | |
211 | } | |
212 | } | |
213 | break; | |
f6bcfd97 BP |
214 | } |
215 | ||
216 | return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit, hWnd, message, wParam, lParam); | |
217 | } | |
218 | ||
f6bcfd97 | 219 | // ---------------------------------------------------------------------------- |
3a7d5f7c | 220 | // wxComboBox callbacks |
f6bcfd97 BP |
221 | // ---------------------------------------------------------------------------- |
222 | ||
c140b7e7 | 223 | WXLRESULT wxComboBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) |
3a7d5f7c | 224 | { |
5301d1f7 VZ |
225 | // TODO: handle WM_CTLCOLOR messages from our EDIT control to be able to |
226 | // set its colour correctly (to be the same as our own one) | |
682214d5 | 227 | |
3a7d5f7c VZ |
228 | switch ( nMsg ) |
229 | { | |
682214d5 | 230 | case WM_SIZE: |
3bce55ac JS |
231 | // wxStaticBox can generate this message, when modifying the control's style. |
232 | // This causes the content of the combobox to be selected, for some reason. | |
233 | case WM_STYLECHANGED: | |
5301d1f7 VZ |
234 | { |
235 | // combobox selection sometimes spontaneously changes when its | |
236 | // size changes, restore it to the old value if necessary | |
e9717bd5 VZ |
237 | if ( !GetEditHWNDIfAvailable() ) |
238 | break; | |
239 | ||
5301d1f7 VZ |
240 | long fromOld, toOld; |
241 | GetSelection(&fromOld, &toOld); | |
428f8b06 VZ |
242 | |
243 | // if an editable combobox has a not empty text not from the | |
244 | // list, it tries to autocomplete it from the list when it is | |
245 | // resized, but we don't want this to happen as it doesn't seem | |
246 | // to make any sense, so we forcefully restore the old text | |
247 | wxString textOld; | |
248 | if ( !HasFlag(wxCB_READONLY) && GetCurrentSelection() == -1 ) | |
249 | textOld = GetValue(); | |
250 | ||
251 | // eliminate flickering during following hacks | |
252 | wxWindowUpdateLocker lock(this); | |
253 | ||
5301d1f7 | 254 | WXLRESULT result = wxChoice::MSWWindowProc(nMsg, wParam, lParam); |
682214d5 | 255 | |
428f8b06 VZ |
256 | if ( !textOld.empty() && GetValue() != textOld ) |
257 | SetLabel(textOld); | |
258 | ||
5301d1f7 VZ |
259 | long fromNew, toNew; |
260 | GetSelection(&fromNew, &toNew); | |
682214d5 | 261 | |
5301d1f7 VZ |
262 | if ( fromOld != fromNew || toOld != toNew ) |
263 | { | |
264 | SetSelection(fromOld, toOld); | |
265 | } | |
682214d5 | 266 | |
5301d1f7 VZ |
267 | return result; |
268 | } | |
3a7d5f7c VZ |
269 | } |
270 | ||
5301d1f7 | 271 | return wxChoice::MSWWindowProc(nMsg, wParam, lParam); |
3a7d5f7c VZ |
272 | } |
273 | ||
f6bcfd97 BP |
274 | bool wxComboBox::MSWProcessEditMsg(WXUINT msg, WXWPARAM wParam, WXLPARAM lParam) |
275 | { | |
276 | switch ( msg ) | |
277 | { | |
278 | case WM_CHAR: | |
2b5f62a0 VZ |
279 | // for compatibility with wxTextCtrl, generate a special message |
280 | // when Enter is pressed | |
281 | if ( wParam == VK_RETURN ) | |
282 | { | |
a2634d81 RR |
283 | if (SendMessage(GetHwnd(), CB_GETDROPPEDSTATE, 0, 0)) |
284 | return false; | |
f4322df6 | 285 | |
2b5f62a0 | 286 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); |
593ac33e VZ |
287 | |
288 | const int sel = GetSelection(); | |
289 | event.SetInt(sel); | |
2b5f62a0 | 290 | event.SetString(GetValue()); |
593ac33e VZ |
291 | InitCommandEventWithItems(event, sel); |
292 | ||
ffb1629f VZ |
293 | if ( ProcessCommand(event) ) |
294 | { | |
295 | // don't let the event through to the native control | |
296 | // because it doesn't need it and may generate an annoying | |
297 | // beep if it gets it | |
298 | return true; | |
299 | } | |
2b5f62a0 | 300 | } |
a1254663 | 301 | // fall through |
2b5f62a0 | 302 | |
a1254663 | 303 | case WM_SYSCHAR: |
b6885972 | 304 | return HandleChar(wParam, lParam); |
889f0b7c | 305 | |
a1254663 | 306 | case WM_SYSKEYDOWN: |
f6bcfd97 BP |
307 | case WM_KEYDOWN: |
308 | return HandleKeyDown(wParam, lParam); | |
309 | ||
a1254663 | 310 | case WM_SYSKEYUP: |
f6bcfd97 BP |
311 | case WM_KEYUP: |
312 | return HandleKeyUp(wParam, lParam); | |
53c3a78b VZ |
313 | |
314 | case WM_SETFOCUS: | |
315 | return HandleSetFocus((WXHWND)wParam); | |
316 | ||
317 | case WM_KILLFOCUS: | |
318 | return HandleKillFocus((WXHWND)wParam); | |
b65f16da VS |
319 | |
320 | case WM_CUT: | |
321 | case WM_COPY: | |
322 | case WM_PASTE: | |
323 | return HandleClipboardEvent(msg); | |
f6bcfd97 BP |
324 | } |
325 | ||
02b7b6b0 | 326 | return false; |
f6bcfd97 BP |
327 | } |
328 | ||
6ba93d23 | 329 | bool wxComboBox::MSWCommand(WXUINT param, WXWORD id) |
2bda0e17 | 330 | { |
fddfd9e1 | 331 | int sel = -1; |
7d90194c VZ |
332 | wxString value; |
333 | ||
cd0b1709 | 334 | switch ( param ) |
8c1c5302 | 335 | { |
ae3b1487 VZ |
336 | case CBN_DROPDOWN: |
337 | // remember the last selection, just as wxChoice does | |
338 | m_lastAcceptedSelection = GetCurrentSelection(); | |
339 | if ( m_lastAcceptedSelection == -1 ) | |
340 | { | |
341 | // but unlike with wxChoice we may have no selection but still | |
342 | // have some text and we should avoid erasing it if the drop | |
343 | // down is cancelled (see #8474) | |
344 | m_lastAcceptedSelection = wxID_NONE; | |
345 | } | |
8933fbc6 VZ |
346 | { |
347 | wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_DROPDOWN, GetId()); | |
348 | event.SetEventObject(this); | |
349 | ProcessCommand(event); | |
350 | } | |
351 | break; | |
352 | case CBN_CLOSEUP: | |
353 | { | |
354 | wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_CLOSEUP, GetId()); | |
355 | event.SetEventObject(this); | |
356 | ProcessCommand(event); | |
357 | } | |
ae3b1487 | 358 | break; |
78a87a5d | 359 | case CBN_SELENDOK: |
8e25c198 | 360 | #ifndef __SMARTPHONE__ |
7d90194c VZ |
361 | // we need to reset this to prevent the selection from being undone |
362 | // by wxChoice, see wxChoice::MSWCommand() and comments there | |
363 | m_lastAcceptedSelection = wxID_NONE; | |
8e25c198 | 364 | #endif |
13bcc348 | 365 | |
7d90194c VZ |
366 | // set these variables so that they could be also fixed in |
367 | // CBN_EDITCHANGE below | |
368 | sel = GetSelection(); | |
f5a45ee2 | 369 | value = GetStringSelection(); |
d0b6344d VZ |
370 | |
371 | // this string is going to become the new combobox value soon but | |
372 | // we need it to be done right now, otherwise the event handler | |
373 | // could get a wrong value when it calls our GetValue() | |
e0a050e3 | 374 | ::SetWindowText(GetHwnd(), value.wx_str()); |
d0b6344d | 375 | |
cd0b1709 VZ |
376 | { |
377 | wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId()); | |
7d90194c VZ |
378 | event.SetInt(sel); |
379 | event.SetString(value); | |
593ac33e VZ |
380 | InitCommandEventWithItems(event, sel); |
381 | ||
cd0b1709 VZ |
382 | ProcessCommand(event); |
383 | } | |
fddfd9e1 VZ |
384 | |
385 | // fall through: for compability with wxGTK, also send the text | |
386 | // update event when the selection changes (this also seems more | |
387 | // logical as the text does change) | |
29006414 | 388 | |
cd0b1709 | 389 | case CBN_EDITCHANGE: |
b7bfef38 | 390 | if ( m_allowTextEvents ) |
cd0b1709 | 391 | { |
7ba166dd | 392 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId()); |
3adc70bf | 393 | |
7d90194c | 394 | // if sel != -1, value was already initialized above |
7ba166dd | 395 | if ( sel == -1 ) |
3adc70bf | 396 | { |
7d90194c | 397 | value = wxGetWindowText(GetHwnd()); |
3adc70bf VZ |
398 | } |
399 | ||
7d90194c | 400 | event.SetString(value); |
593ac33e VZ |
401 | InitCommandEventWithItems(event, sel); |
402 | ||
0ef2ebba | 403 | ProcessCommand(event); |
cd0b1709 VZ |
404 | } |
405 | break; | |
6ba93d23 VZ |
406 | |
407 | default: | |
408 | return wxChoice::MSWCommand(param, id); | |
cd0b1709 | 409 | } |
29006414 | 410 | |
7d90194c | 411 | // skip wxChoice version as it would generate its own events for |
ae3b1487 | 412 | // CBN_SELENDOK and also interfere with our handling of CBN_DROPDOWN |
8cc5e8cf | 413 | return true; |
2bda0e17 KB |
414 | } |
415 | ||
90ac8b50 RR |
416 | bool wxComboBox::MSWShouldPreProcessMessage(WXMSG *pMsg) |
417 | { | |
418 | // prevent command accelerators from stealing editing | |
419 | // hotkeys when we have the focus | |
420 | if (wxIsCtrlDown()) | |
421 | { | |
422 | WPARAM vkey = pMsg->wParam; | |
6b54668b | 423 | |
90ac8b50 RR |
424 | switch (vkey) |
425 | { | |
426 | case 'C': | |
427 | case 'V': | |
428 | case 'X': | |
429 | case VK_INSERT: | |
430 | case VK_DELETE: | |
431 | case VK_HOME: | |
432 | case VK_END: | |
433 | return false; | |
434 | } | |
435 | } | |
6b54668b | 436 | |
90ac8b50 RR |
437 | return wxChoice::MSWShouldPreProcessMessage(pMsg); |
438 | } | |
439 | ||
e9717bd5 VZ |
440 | WXHWND wxComboBox::GetEditHWNDIfAvailable() const |
441 | { | |
14ca3a3b | 442 | #if wxUSE_DYNLIB_CLASS |
1690c2ce | 443 | #if defined(WINVER) && WINVER >= 0x0500 |
e558f953 JS |
444 | typedef BOOL (WINAPI *GetComboBoxInfo_t)(HWND, COMBOBOXINFO*); |
445 | static GetComboBoxInfo_t s_pfnGetComboBoxInfo = NULL; | |
446 | static bool s_triedToLoad = false; | |
447 | if ( !s_triedToLoad ) | |
448 | { | |
449 | s_triedToLoad = true; | |
450 | wxLoadedDLL dllUser32("user32.dll"); | |
451 | wxDL_INIT_FUNC(s_pfn, GetComboBoxInfo, dllUser32); | |
452 | } | |
453 | ||
454 | if ( s_pfnGetComboBoxInfo ) | |
455 | { | |
456 | WinStruct<COMBOBOXINFO> info; | |
457 | (*s_pfnGetComboBoxInfo)(GetHwnd(), &info); | |
458 | return info.hwndItem; | |
459 | } | |
14ca3a3b VZ |
460 | #endif // WINVER >= 0x0500 |
461 | #endif // wxUSE_DYNLIB_CLASS | |
e558f953 JS |
462 | |
463 | if (HasFlag(wxCB_SIMPLE)) | |
464 | { | |
465 | POINT pt; | |
466 | pt.x = pt.y = 4; | |
467 | return (WXHWND) ::ChildWindowFromPoint(GetHwnd(), pt); | |
468 | } | |
fe56156f | 469 | |
1690c2ce JS |
470 | // notice that a slightly safer alternative could be to use FindWindowEx() |
471 | // but it's not available under WinCE so just take the first child for now | |
472 | // to keep one version of the code for all platforms and fix it later if | |
473 | // problems are discovered | |
474 | ||
74052fe8 VZ |
475 | // we assume that the only child of the combobox is the edit window |
476 | return (WXHWND)::GetWindow(GetHwnd(), GW_CHILD); | |
e9717bd5 VZ |
477 | } |
478 | ||
f6bcfd97 BP |
479 | WXHWND wxComboBox::GetEditHWND() const |
480 | { | |
481 | // this function should not be called for wxCB_READONLY controls, it is | |
e9717bd5 | 482 | // the callers responsibility to check this |
fa2f57be | 483 | wxASSERT_MSG( !HasFlag(wxCB_READONLY), |
9a83f860 | 484 | wxT("read-only combobox doesn't have any edit control") ); |
f6bcfd97 | 485 | |
e9717bd5 | 486 | WXHWND hWndEdit = GetEditHWNDIfAvailable(); |
9a83f860 | 487 | wxASSERT_MSG( hWndEdit, wxT("combobox without edit control?") ); |
f6bcfd97 | 488 | |
e9717bd5 | 489 | return hWndEdit; |
f6bcfd97 BP |
490 | } |
491 | ||
63f7d502 VZ |
492 | wxWindow *wxComboBox::GetEditableWindow() |
493 | { | |
494 | wxASSERT_MSG( !HasFlag(wxCB_READONLY), | |
9a83f860 | 495 | wxT("read-only combobox doesn't have any edit control") ); |
63f7d502 VZ |
496 | |
497 | return this; | |
498 | } | |
499 | ||
71e57cd6 VZ |
500 | // ---------------------------------------------------------------------------- |
501 | // wxComboBox creation | |
502 | // ---------------------------------------------------------------------------- | |
503 | ||
debe6624 | 504 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, |
c085e333 VZ |
505 | const wxString& value, |
506 | const wxPoint& pos, | |
507 | const wxSize& size, | |
508 | int n, const wxString choices[], | |
509 | long style, | |
510 | const wxValidator& validator, | |
511 | const wxString& name) | |
2bda0e17 | 512 | { |
bdf5c30d VZ |
513 | // pretend that wxComboBox is hidden while it is positioned and resized and |
514 | // show it only right before leaving this method because otherwise there is | |
515 | // some noticeable flicker while the control rearranges itself | |
02b7b6b0 | 516 | m_isShown = false; |
bdf5c30d | 517 | |
71e57cd6 VZ |
518 | if ( !CreateAndInit(parent, id, pos, size, n, choices, style, |
519 | validator, name) ) | |
02b7b6b0 | 520 | return false; |
f6bcfd97 | 521 | |
6341d824 VZ |
522 | // we shouldn't call SetValue() for an empty string because this would |
523 | // (correctly) result in an assert with a read only combobox and is useless | |
524 | // for the other ones anyhow | |
525 | if ( !value.empty() ) | |
b9b8a2b5 | 526 | SetValue(value); |
db34b147 | 527 | |
f6bcfd97 BP |
528 | // a (not read only) combobox is, in fact, 2 controls: the combobox itself |
529 | // and an edit control inside it and if we want to catch events from this | |
530 | // edit control, we must subclass it as well | |
531 | if ( !(style & wxCB_READONLY) ) | |
532 | { | |
94972183 | 533 | gs_wndprocEdit = wxSetWindowProc((HWND)GetEditHWND(), wxComboEditWndProc); |
f6bcfd97 | 534 | } |
2bda0e17 | 535 | |
bdf5c30d | 536 | // and finally, show the control |
02b7b6b0 | 537 | Show(true); |
bdf5c30d | 538 | |
02b7b6b0 | 539 | return true; |
2bda0e17 KB |
540 | } |
541 | ||
584ad2a3 MB |
542 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, |
543 | const wxString& value, | |
544 | const wxPoint& pos, | |
545 | const wxSize& size, | |
546 | const wxArrayString& choices, | |
547 | long style, | |
548 | const wxValidator& validator, | |
549 | const wxString& name) | |
550 | { | |
551 | wxCArrayString chs(choices); | |
552 | return Create(parent, id, value, pos, size, chs.GetCount(), | |
553 | chs.GetStrings(), style, validator, name); | |
554 | } | |
555 | ||
71e57cd6 VZ |
556 | WXDWORD wxComboBox::MSWGetStyle(long style, WXDWORD *exstyle) const |
557 | { | |
558 | // we never have an external border | |
559 | WXDWORD msStyle = wxChoice::MSWGetStyle | |
560 | ( | |
561 | (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle | |
562 | ); | |
563 | ||
1dfd425a VZ |
564 | // usually WS_TABSTOP is added by wxControl::MSWGetStyle() but as we're |
565 | // created hidden (see Create() above), it is not done for us but we still | |
566 | // want to have this style | |
567 | msStyle |= WS_TABSTOP; | |
568 | ||
71e57cd6 VZ |
569 | // remove the style always added by wxChoice |
570 | msStyle &= ~CBS_DROPDOWNLIST; | |
571 | ||
572 | if ( style & wxCB_READONLY ) | |
573 | msStyle |= CBS_DROPDOWNLIST; | |
574 | #ifndef __WXWINCE__ | |
575 | else if ( style & wxCB_SIMPLE ) | |
576 | msStyle |= CBS_SIMPLE; // A list (shown always) and edit control | |
577 | #endif | |
578 | else | |
579 | msStyle |= CBS_DROPDOWN; | |
580 | ||
581 | // there is no reason to not always use CBS_AUTOHSCROLL, so do use it | |
582 | msStyle |= CBS_AUTOHSCROLL; | |
583 | ||
584 | // NB: we used to also add CBS_NOINTEGRALHEIGHT here but why? | |
585 | ||
586 | return msStyle; | |
587 | } | |
588 | ||
589 | // ---------------------------------------------------------------------------- | |
590 | // wxComboBox text control-like methods | |
591 | // ---------------------------------------------------------------------------- | |
592 | ||
e9717bd5 VZ |
593 | wxString wxComboBox::GetValue() const |
594 | { | |
595 | return HasFlag(wxCB_READONLY) ? GetStringSelection() | |
596 | : wxTextEntry::GetValue(); | |
597 | } | |
598 | ||
2bda0e17 KB |
599 | void wxComboBox::SetValue(const wxString& value) |
600 | { | |
2b5f62a0 VZ |
601 | if ( HasFlag(wxCB_READONLY) ) |
602 | SetStringSelection(value); | |
b38f3ff3 | 603 | else |
fa2f57be | 604 | wxTextEntry::SetValue(value); |
150e31d2 JS |
605 | } |
606 | ||
e6a84162 VZ |
607 | void wxComboBox::Clear() |
608 | { | |
609 | wxChoice::Clear(); | |
610 | if ( !HasFlag(wxCB_READONLY) ) | |
611 | wxTextEntry::Clear(); | |
612 | } | |
613 | ||
e9717bd5 VZ |
614 | void wxComboBox::GetSelection(long *from, long *to) const |
615 | { | |
616 | if ( !HasFlag(wxCB_READONLY) ) | |
617 | { | |
618 | wxTextEntry::GetSelection(from, to); | |
619 | } | |
620 | else // text selection doesn't make sense for read only comboboxes | |
621 | { | |
622 | if ( from ) | |
623 | *from = -1; | |
624 | if ( to ) | |
625 | *to = -1; | |
626 | } | |
627 | } | |
628 | ||
150e31d2 JS |
629 | bool wxComboBox::IsEditable() const |
630 | { | |
fa2f57be | 631 | return !HasFlag(wxCB_READONLY) && wxTextEntry::IsEditable(); |
3f5ca6b1 RN |
632 | } |
633 | ||
150e31d2 JS |
634 | // ---------------------------------------------------------------------------- |
635 | // standard event handling | |
636 | // ---------------------------------------------------------------------------- | |
637 | ||
638 | void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event)) | |
639 | { | |
640 | Cut(); | |
641 | } | |
642 | ||
643 | void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
644 | { | |
645 | Copy(); | |
646 | } | |
647 | ||
648 | void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
649 | { | |
650 | Paste(); | |
651 | } | |
652 | ||
653 | void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
654 | { | |
655 | Undo(); | |
656 | } | |
657 | ||
658 | void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
659 | { | |
660 | Redo(); | |
661 | } | |
662 | ||
663 | void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event)) | |
664 | { | |
5a25f858 | 665 | RemoveSelection(); |
150e31d2 JS |
666 | } |
667 | ||
668 | void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event)) | |
669 | { | |
e976429d | 670 | SelectAll(); |
150e31d2 JS |
671 | } |
672 | ||
673 | void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event) | |
674 | { | |
675 | event.Enable( CanCut() ); | |
676 | } | |
677 | ||
678 | void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event) | |
679 | { | |
680 | event.Enable( CanCopy() ); | |
681 | } | |
682 | ||
683 | void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event) | |
684 | { | |
685 | event.Enable( CanPaste() ); | |
686 | } | |
687 | ||
688 | void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event) | |
689 | { | |
c0e15042 | 690 | event.Enable( IsEditable() && CanUndo() ); |
150e31d2 JS |
691 | } |
692 | ||
693 | void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event) | |
694 | { | |
c0e15042 | 695 | event.Enable( IsEditable() && CanRedo() ); |
150e31d2 JS |
696 | } |
697 | ||
698 | void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event) | |
699 | { | |
6b54668b | 700 | event.Enable(IsEditable() && HasSelection()); |
150e31d2 JS |
701 | } |
702 | ||
703 | void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event) | |
704 | { | |
e976429d | 705 | event.Enable(IsEditable() && !wxTextEntry::IsEmpty()); |
150e31d2 JS |
706 | } |
707 | ||
d1d1f817 VZ |
708 | void wxComboBox::MSWDoPopupOrDismiss(bool show) |
709 | { | |
710 | wxASSERT_MSG( !HasFlag(wxCB_SIMPLE), | |
711 | wxT("can't popup/dismiss the list for simple combo box") ); | |
712 | ||
713 | // we *must* set focus to the combobox before showing or hiding the drop | |
714 | // down as without this we get WM_LBUTTONDOWN messages with invalid HWND | |
715 | // when hiding it (whether programmatically or manually) resulting in a | |
716 | // crash when we pass them to IsDialogMessage() | |
717 | // | |
718 | // this can be seen in the combo page of the widgets sample under Windows 7 | |
719 | SetFocus(); | |
720 | ||
721 | ::SendMessage(GetHwnd(), CB_SHOWDROPDOWN, show, 0); | |
722 | } | |
723 | ||
d6b9cc87 VZ |
724 | #if wxUSE_TOOLTIPS |
725 | ||
726 | void wxComboBox::DoSetToolTip(wxToolTip *tip) | |
727 | { | |
728 | wxChoice::DoSetToolTip(tip); | |
729 | ||
730 | if ( tip && !HasFlag(wxCB_READONLY) ) | |
731 | tip->Add(GetEditHWND()); | |
732 | } | |
733 | ||
734 | #endif // wxUSE_TOOLTIPS | |
735 | ||
63f7d502 VZ |
736 | #if wxUSE_UXTHEME |
737 | ||
738 | bool wxComboBox::SetHint(const wxString& hintOrig) | |
739 | { | |
740 | wxString hint(hintOrig); | |
741 | if ( wxUxThemeEngine::GetIfActive() ) | |
742 | { | |
743 | // under XP (but not Vista) there is a bug in cue banners | |
744 | // implementation for combobox edit control: the first character is | |
745 | // partially chopped off, so prepend a space to make it fully visible | |
746 | hint.insert(0, " "); | |
747 | } | |
748 | ||
749 | return wxTextEntry::SetHint(hint); | |
750 | } | |
751 | ||
752 | #endif // wxUSE_UXTHEME | |
753 | ||
71e57cd6 | 754 | #endif // wxUSE_COMBOBOX |