no real changes, just use more readable helper wxTextEntry methods (patch 1910165 )
[wxWidgets.git] / src / msw / combobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/combobox.cpp
3 // Purpose: wxComboBox class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_COMBOBOX
28
29 #include "wx/combobox.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #include "wx/settings.h"
34 #include "wx/log.h"
35 // for wxEVT_COMMAND_TEXT_ENTER
36 #include "wx/textctrl.h"
37 #include "wx/app.h"
38 #include "wx/brush.h"
39 #endif
40
41 #include "wx/clipbrd.h"
42 #include "wx/msw/private.h"
43
44 #if wxUSE_TOOLTIPS
45 #include "wx/tooltip.h"
46 #endif // wxUSE_TOOLTIPS
47
48 // ----------------------------------------------------------------------------
49 // wxWin macros
50 // ----------------------------------------------------------------------------
51
52 #if wxUSE_EXTENDED_RTTI
53 WX_DEFINE_FLAGS( wxComboBoxStyle )
54
55 wxBEGIN_FLAGS( wxComboBoxStyle )
56 // new style border flags, we put them first to
57 // use them for streaming out
58 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
59 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
60 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
61 wxFLAGS_MEMBER(wxBORDER_RAISED)
62 wxFLAGS_MEMBER(wxBORDER_STATIC)
63 wxFLAGS_MEMBER(wxBORDER_NONE)
64
65 // old style border flags
66 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
67 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
68 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
69 wxFLAGS_MEMBER(wxRAISED_BORDER)
70 wxFLAGS_MEMBER(wxSTATIC_BORDER)
71 wxFLAGS_MEMBER(wxBORDER)
72
73 // standard window styles
74 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
75 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
76 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
77 wxFLAGS_MEMBER(wxWANTS_CHARS)
78 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
79 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
80 wxFLAGS_MEMBER(wxVSCROLL)
81 wxFLAGS_MEMBER(wxHSCROLL)
82
83 wxFLAGS_MEMBER(wxCB_SIMPLE)
84 wxFLAGS_MEMBER(wxCB_SORT)
85 wxFLAGS_MEMBER(wxCB_READONLY)
86 wxFLAGS_MEMBER(wxCB_DROPDOWN)
87
88 wxEND_FLAGS( wxComboBoxStyle )
89
90 IMPLEMENT_DYNAMIC_CLASS_XTI(wxComboBox, wxChoice,"wx/combobox.h")
91
92 wxBEGIN_PROPERTIES_TABLE(wxComboBox)
93 wxEVENT_PROPERTY( Select , wxEVT_COMMAND_COMBOBOX_SELECTED , wxCommandEvent )
94 wxEVENT_PROPERTY( TextEnter , wxEVT_COMMAND_TEXT_ENTER , wxCommandEvent )
95
96 // TODO DELEGATES
97 wxPROPERTY( Font , wxFont , SetFont , GetFont , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
98 wxPROPERTY_COLLECTION( Choices , wxArrayString , wxString , AppendString , GetStrings , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
99 wxPROPERTY( Value ,wxString, SetValue, GetValue, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
100 wxPROPERTY( Selection ,int, SetSelection, GetSelection, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
101 wxPROPERTY_FLAGS( WindowStyle , wxComboBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
102 wxEND_PROPERTIES_TABLE()
103
104 wxBEGIN_HANDLERS_TABLE(wxComboBox)
105 wxEND_HANDLERS_TABLE()
106
107 wxCONSTRUCTOR_5( wxComboBox , wxWindow* , Parent , wxWindowID , Id , wxString , Value , wxPoint , Position , wxSize , Size )
108
109 #else
110
111 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxChoice)
112
113 #endif
114
115 BEGIN_EVENT_TABLE(wxComboBox, wxControl)
116 EVT_MENU(wxID_CUT, wxComboBox::OnCut)
117 EVT_MENU(wxID_COPY, wxComboBox::OnCopy)
118 EVT_MENU(wxID_PASTE, wxComboBox::OnPaste)
119 EVT_MENU(wxID_UNDO, wxComboBox::OnUndo)
120 EVT_MENU(wxID_REDO, wxComboBox::OnRedo)
121 EVT_MENU(wxID_CLEAR, wxComboBox::OnDelete)
122 EVT_MENU(wxID_SELECTALL, wxComboBox::OnSelectAll)
123
124 EVT_UPDATE_UI(wxID_CUT, wxComboBox::OnUpdateCut)
125 EVT_UPDATE_UI(wxID_COPY, wxComboBox::OnUpdateCopy)
126 EVT_UPDATE_UI(wxID_PASTE, wxComboBox::OnUpdatePaste)
127 EVT_UPDATE_UI(wxID_UNDO, wxComboBox::OnUpdateUndo)
128 EVT_UPDATE_UI(wxID_REDO, wxComboBox::OnUpdateRedo)
129 EVT_UPDATE_UI(wxID_CLEAR, wxComboBox::OnUpdateDelete)
130 EVT_UPDATE_UI(wxID_SELECTALL, wxComboBox::OnUpdateSelectAll)
131 END_EVENT_TABLE()
132
133 // ----------------------------------------------------------------------------
134 // function prototypes
135 // ----------------------------------------------------------------------------
136
137 LRESULT APIENTRY _EXPORT wxComboEditWndProc(HWND hWnd,
138 UINT message,
139 WPARAM wParam,
140 LPARAM lParam);
141
142 // ---------------------------------------------------------------------------
143 // global vars
144 // ---------------------------------------------------------------------------
145
146 // the pointer to standard radio button wnd proc
147 static WNDPROC gs_wndprocEdit = (WNDPROC)NULL;
148
149 // ============================================================================
150 // implementation
151 // ============================================================================
152
153 // ----------------------------------------------------------------------------
154 // wnd proc for subclassed edit control
155 // ----------------------------------------------------------------------------
156
157 LRESULT APIENTRY _EXPORT wxComboEditWndProc(HWND hWnd,
158 UINT message,
159 WPARAM wParam,
160 LPARAM lParam)
161 {
162 HWND hwndCombo = ::GetParent(hWnd);
163 wxWindow *win = wxFindWinFromHandle((WXHWND)hwndCombo);
164
165 switch ( message )
166 {
167 // forward some messages to the combobox to generate the appropriate
168 // wxEvents from them
169 case WM_KEYUP:
170 case WM_KEYDOWN:
171 case WM_CHAR:
172 case WM_SYSCHAR:
173 case WM_SYSKEYDOWN:
174 case WM_SYSKEYUP:
175 case WM_SETFOCUS:
176 case WM_KILLFOCUS:
177 {
178 wxComboBox *combo = wxDynamicCast(win, wxComboBox);
179 if ( !combo )
180 {
181 // we can get WM_KILLFOCUS while our parent is already half
182 // destroyed and hence doesn't look like a combobx any
183 // longer, check for it to avoid bogus assert failures
184 if ( !win->IsBeingDeleted() )
185 {
186 wxFAIL_MSG( _T("should have combo as parent") );
187 }
188 }
189 else if ( combo->MSWProcessEditMsg(message, wParam, lParam) )
190 {
191 // handled by parent
192 return 0;
193 }
194 }
195 break;
196
197 case WM_GETDLGCODE:
198 {
199 wxCHECK_MSG( win, 0, _T("should have a parent") );
200
201 if ( win->GetWindowStyle() & wxTE_PROCESS_ENTER )
202 {
203 // need to return a custom dlg code or we'll never get it
204 return DLGC_WANTMESSAGE;
205 }
206 }
207 break;
208 }
209
210 return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit, hWnd, message, wParam, lParam);
211 }
212
213 // ----------------------------------------------------------------------------
214 // wxComboBox callbacks
215 // ----------------------------------------------------------------------------
216
217 WXLRESULT wxComboBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
218 {
219 // TODO: handle WM_CTLCOLOR messages from our EDIT control to be able to
220 // set its colour correctly (to be the same as our own one)
221
222 switch ( nMsg )
223 {
224 case WM_SIZE:
225 // wxStaticBox can generate this message, when modifying the control's style.
226 // This causes the content of the combobox to be selected, for some reason.
227 case WM_STYLECHANGED:
228 {
229 // combobox selection sometimes spontaneously changes when its
230 // size changes, restore it to the old value if necessary
231 if ( !GetEditHWNDIfAvailable() )
232 break;
233
234 long fromOld, toOld;
235 GetSelection(&fromOld, &toOld);
236 WXLRESULT result = wxChoice::MSWWindowProc(nMsg, wParam, lParam);
237
238 long fromNew, toNew;
239 GetSelection(&fromNew, &toNew);
240
241 if ( fromOld != fromNew || toOld != toNew )
242 {
243 SetSelection(fromOld, toOld);
244 }
245
246 return result;
247 }
248 }
249
250 return wxChoice::MSWWindowProc(nMsg, wParam, lParam);
251 }
252
253 bool wxComboBox::MSWProcessEditMsg(WXUINT msg, WXWPARAM wParam, WXLPARAM lParam)
254 {
255 switch ( msg )
256 {
257 case WM_CHAR:
258 // for compatibility with wxTextCtrl, generate a special message
259 // when Enter is pressed
260 if ( wParam == VK_RETURN )
261 {
262 if (SendMessage(GetHwnd(), CB_GETDROPPEDSTATE, 0, 0))
263 return false;
264
265 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
266
267 const int sel = GetSelection();
268 event.SetInt(sel);
269 event.SetString(GetValue());
270 InitCommandEventWithItems(event, sel);
271
272 if ( ProcessCommand(event) )
273 {
274 // don't let the event through to the native control
275 // because it doesn't need it and may generate an annoying
276 // beep if it gets it
277 return true;
278 }
279 }
280 // fall through
281
282 case WM_SYSCHAR:
283 return HandleChar(wParam, lParam, true /* isASCII */);
284
285 case WM_SYSKEYDOWN:
286 case WM_KEYDOWN:
287 return HandleKeyDown(wParam, lParam);
288
289 case WM_SYSKEYUP:
290 case WM_KEYUP:
291 return HandleKeyUp(wParam, lParam);
292
293 case WM_SETFOCUS:
294 return HandleSetFocus((WXHWND)wParam);
295
296 case WM_KILLFOCUS:
297 return HandleKillFocus((WXHWND)wParam);
298
299 case WM_CUT:
300 case WM_COPY:
301 case WM_PASTE:
302 return HandleClipboardEvent(msg);
303 }
304
305 return false;
306 }
307
308 bool wxComboBox::MSWCommand(WXUINT param, WXWORD id)
309 {
310 int sel = -1;
311 wxString value;
312
313 switch ( param )
314 {
315 case CBN_SELENDOK:
316 #ifndef __SMARTPHONE__
317 // we need to reset this to prevent the selection from being undone
318 // by wxChoice, see wxChoice::MSWCommand() and comments there
319 m_lastAcceptedSelection = wxID_NONE;
320 #endif
321
322 // set these variables so that they could be also fixed in
323 // CBN_EDITCHANGE below
324 sel = GetSelection();
325 value = GetStringSelection();
326
327 // this string is going to become the new combobox value soon but
328 // we need it to be done right now, otherwise the event handler
329 // could get a wrong value when it calls our GetValue()
330 ::SetWindowText(GetHwnd(), value.wx_str());
331
332 {
333 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId());
334 event.SetInt(sel);
335 event.SetString(value);
336 InitCommandEventWithItems(event, sel);
337
338 ProcessCommand(event);
339 }
340
341 // fall through: for compability with wxGTK, also send the text
342 // update event when the selection changes (this also seems more
343 // logical as the text does change)
344
345 case CBN_EDITCHANGE:
346 {
347 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
348
349 // if sel != -1, value was already initialized above
350 if ( sel == -1 )
351 {
352 value = wxGetWindowText(GetHwnd());
353 }
354
355 event.SetString(value);
356 InitCommandEventWithItems(event, sel);
357
358 ProcessCommand(event);
359 }
360 break;
361
362 default:
363 return wxChoice::MSWCommand(param, id);
364 }
365
366 // skip wxChoice version as it would generate its own events for
367 // CBN_SELENDOK
368 return true;
369 }
370
371 bool wxComboBox::MSWShouldPreProcessMessage(WXMSG *pMsg)
372 {
373 // prevent command accelerators from stealing editing
374 // hotkeys when we have the focus
375 if (wxIsCtrlDown())
376 {
377 WPARAM vkey = pMsg->wParam;
378
379 switch (vkey)
380 {
381 case 'C':
382 case 'V':
383 case 'X':
384 case VK_INSERT:
385 case VK_DELETE:
386 case VK_HOME:
387 case VK_END:
388 return false;
389 }
390 }
391
392 return wxChoice::MSWShouldPreProcessMessage(pMsg);
393 }
394
395 WXHWND wxComboBox::GetEditHWNDIfAvailable() const
396 {
397 // notice that a slightly safer alternative could be to use FindWindowEx()
398 // but it's not available under WinCE so just take the first child for now
399 // to keep one version of the code for all platforms and fix it later if
400 // problems are discovered
401
402 // we assume that the only child of the combobox is the edit window
403 return (WXHWND)::GetWindow(GetHwnd(), GW_CHILD);
404 }
405
406 WXHWND wxComboBox::GetEditHWND() const
407 {
408 // this function should not be called for wxCB_READONLY controls, it is
409 // the callers responsibility to check this
410 wxASSERT_MSG( !HasFlag(wxCB_READONLY),
411 _T("read-only combobox doesn't have any edit control") );
412
413 WXHWND hWndEdit = GetEditHWNDIfAvailable();
414 wxASSERT_MSG( hWndEdit, _T("combobox without edit control?") );
415
416 return hWndEdit;
417 }
418
419 // ----------------------------------------------------------------------------
420 // wxComboBox creation
421 // ----------------------------------------------------------------------------
422
423 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
424 const wxString& value,
425 const wxPoint& pos,
426 const wxSize& size,
427 int n, const wxString choices[],
428 long style,
429 const wxValidator& validator,
430 const wxString& name)
431 {
432 // pretend that wxComboBox is hidden while it is positioned and resized and
433 // show it only right before leaving this method because otherwise there is
434 // some noticeable flicker while the control rearranges itself
435 m_isShown = false;
436
437 if ( !CreateAndInit(parent, id, pos, size, n, choices, style,
438 validator, name) )
439 return false;
440
441 // we shouldn't call SetValue() for an empty string because this would
442 // (correctly) result in an assert with a read only combobox and is useless
443 // for the other ones anyhow
444 if ( !value.empty() )
445 SetValue(value);
446
447 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
448 // and an edit control inside it and if we want to catch events from this
449 // edit control, we must subclass it as well
450 if ( !(style & wxCB_READONLY) )
451 {
452 gs_wndprocEdit = wxSetWindowProc((HWND)GetEditHWND(), wxComboEditWndProc);
453 }
454
455 // and finally, show the control
456 Show(true);
457
458 return true;
459 }
460
461 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
462 const wxString& value,
463 const wxPoint& pos,
464 const wxSize& size,
465 const wxArrayString& choices,
466 long style,
467 const wxValidator& validator,
468 const wxString& name)
469 {
470 wxCArrayString chs(choices);
471 return Create(parent, id, value, pos, size, chs.GetCount(),
472 chs.GetStrings(), style, validator, name);
473 }
474
475 WXDWORD wxComboBox::MSWGetStyle(long style, WXDWORD *exstyle) const
476 {
477 // we never have an external border
478 WXDWORD msStyle = wxChoice::MSWGetStyle
479 (
480 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle
481 );
482
483 // usually WS_TABSTOP is added by wxControl::MSWGetStyle() but as we're
484 // created hidden (see Create() above), it is not done for us but we still
485 // want to have this style
486 msStyle |= WS_TABSTOP;
487
488 // remove the style always added by wxChoice
489 msStyle &= ~CBS_DROPDOWNLIST;
490
491 if ( style & wxCB_READONLY )
492 msStyle |= CBS_DROPDOWNLIST;
493 #ifndef __WXWINCE__
494 else if ( style & wxCB_SIMPLE )
495 msStyle |= CBS_SIMPLE; // A list (shown always) and edit control
496 #endif
497 else
498 msStyle |= CBS_DROPDOWN;
499
500 // there is no reason to not always use CBS_AUTOHSCROLL, so do use it
501 msStyle |= CBS_AUTOHSCROLL;
502
503 // NB: we used to also add CBS_NOINTEGRALHEIGHT here but why?
504
505 return msStyle;
506 }
507
508 // ----------------------------------------------------------------------------
509 // wxComboBox text control-like methods
510 // ----------------------------------------------------------------------------
511
512 wxString wxComboBox::GetValue() const
513 {
514 return HasFlag(wxCB_READONLY) ? GetStringSelection()
515 : wxTextEntry::GetValue();
516 }
517
518 void wxComboBox::SetValue(const wxString& value)
519 {
520 if ( HasFlag(wxCB_READONLY) )
521 SetStringSelection(value);
522 else
523 wxTextEntry::SetValue(value);
524 }
525
526 void wxComboBox::Clear()
527 {
528 wxChoice::Clear();
529 if ( !HasFlag(wxCB_READONLY) )
530 wxTextEntry::Clear();
531 }
532
533 void wxComboBox::GetSelection(long *from, long *to) const
534 {
535 if ( !HasFlag(wxCB_READONLY) )
536 {
537 wxTextEntry::GetSelection(from, to);
538 }
539 else // text selection doesn't make sense for read only comboboxes
540 {
541 if ( from )
542 *from = -1;
543 if ( to )
544 *to = -1;
545 }
546 }
547
548 bool wxComboBox::IsEditable() const
549 {
550 return !HasFlag(wxCB_READONLY) && wxTextEntry::IsEditable();
551 }
552
553 // ----------------------------------------------------------------------------
554 // standard event handling
555 // ----------------------------------------------------------------------------
556
557 void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
558 {
559 Cut();
560 }
561
562 void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
563 {
564 Copy();
565 }
566
567 void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
568 {
569 Paste();
570 }
571
572 void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
573 {
574 Undo();
575 }
576
577 void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
578 {
579 Redo();
580 }
581
582 void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
583 {
584 long from, to;
585 GetSelection(& from, & to);
586 if (from != -1 && to != -1)
587 Remove(from, to);
588 }
589
590 void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
591 {
592 SelectAll();
593 }
594
595 void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
596 {
597 event.Enable( CanCut() );
598 }
599
600 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
601 {
602 event.Enable( CanCopy() );
603 }
604
605 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
606 {
607 event.Enable( CanPaste() );
608 }
609
610 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
611 {
612 event.Enable( IsEditable() && CanUndo() );
613 }
614
615 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
616 {
617 event.Enable( IsEditable() && CanRedo() );
618 }
619
620 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
621 {
622 event.Enable(IsEditable() && HasSelection());
623 }
624
625 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
626 {
627 event.Enable(IsEditable() && !wxTextEntry::IsEmpty());
628 }
629
630 #if wxUSE_TOOLTIPS
631
632 void wxComboBox::DoSetToolTip(wxToolTip *tip)
633 {
634 wxChoice::DoSetToolTip(tip);
635
636 if ( tip && !HasFlag(wxCB_READONLY) )
637 tip->Add(GetEditHWND());
638 }
639
640 #endif // wxUSE_TOOLTIPS
641
642 #endif // wxUSE_COMBOBOX