fixed event generation for wxComboBox to be consistent with the other platforms and...
[wxWidgets.git] / src / msw / combobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #ifndef WX_PRECOMP
30 #include "wx/settings.h"
31 #include "wx/log.h"
32 // for wxEVT_COMMAND_TEXT_ENTER
33 #include "wx/textctrl.h"
34 #endif
35
36 #include "wx/app.h"
37 #include "wx/combobox.h"
38 #include "wx/brush.h"
39 #include "wx/clipbrd.h"
40 #include "wx/msw/private.h"
41
42 #if wxUSE_TOOLTIPS
43 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
44 #include <commctrl.h>
45 #endif
46 #include "wx/tooltip.h"
47 #endif // wxUSE_TOOLTIPS
48
49 // ----------------------------------------------------------------------------
50 // wxWin macros
51 // ----------------------------------------------------------------------------
52
53 #if wxUSE_EXTENDED_RTTI
54 WX_DEFINE_FLAGS( wxComboBoxStyle )
55
56 wxBEGIN_FLAGS( wxComboBoxStyle )
57 // new style border flags, we put them first to
58 // use them for streaming out
59 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
60 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
61 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
62 wxFLAGS_MEMBER(wxBORDER_RAISED)
63 wxFLAGS_MEMBER(wxBORDER_STATIC)
64 wxFLAGS_MEMBER(wxBORDER_NONE)
65
66 // old style border flags
67 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
68 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
69 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
70 wxFLAGS_MEMBER(wxRAISED_BORDER)
71 wxFLAGS_MEMBER(wxSTATIC_BORDER)
72 wxFLAGS_MEMBER(wxBORDER)
73
74 // standard window styles
75 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
76 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
77 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
78 wxFLAGS_MEMBER(wxWANTS_CHARS)
79 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
80 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
81 wxFLAGS_MEMBER(wxVSCROLL)
82 wxFLAGS_MEMBER(wxHSCROLL)
83
84 wxFLAGS_MEMBER(wxCB_SIMPLE)
85 wxFLAGS_MEMBER(wxCB_SORT)
86 wxFLAGS_MEMBER(wxCB_READONLY)
87 wxFLAGS_MEMBER(wxCB_DROPDOWN)
88
89 wxEND_FLAGS( wxComboBoxStyle )
90
91 IMPLEMENT_DYNAMIC_CLASS_XTI(wxComboBox, wxControl,"wx/combobox.h")
92
93 wxBEGIN_PROPERTIES_TABLE(wxComboBox)
94 wxEVENT_PROPERTY( Select , wxEVT_COMMAND_COMBOBOX_SELECTED , wxCommandEvent )
95 wxEVENT_PROPERTY( TextEnter , wxEVT_COMMAND_TEXT_ENTER , wxCommandEvent )
96
97 // TODO DELEGATES
98 wxPROPERTY( Font , wxFont , SetFont , GetFont , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
99 wxPROPERTY_COLLECTION( Choices , wxArrayString , wxString , AppendString , GetStrings , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
100 wxPROPERTY( Value ,wxString, SetValue, GetValue, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
101 wxPROPERTY( Selection ,int, SetSelection, GetSelection, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
102 wxPROPERTY_FLAGS( WindowStyle , wxComboBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
103 wxEND_PROPERTIES_TABLE()
104
105 wxBEGIN_HANDLERS_TABLE(wxComboBox)
106 wxEND_HANDLERS_TABLE()
107
108 wxCONSTRUCTOR_5( wxComboBox , wxWindow* , Parent , wxWindowID , Id , wxString , Value , wxPoint , Position , wxSize , Size )
109
110 #else
111
112 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
113
114 #endif
115
116 BEGIN_EVENT_TABLE(wxComboBox, wxControl)
117 EVT_MENU(wxID_CUT, wxComboBox::OnCut)
118 EVT_MENU(wxID_COPY, wxComboBox::OnCopy)
119 EVT_MENU(wxID_PASTE, wxComboBox::OnPaste)
120 EVT_MENU(wxID_UNDO, wxComboBox::OnUndo)
121 EVT_MENU(wxID_REDO, wxComboBox::OnRedo)
122 EVT_MENU(wxID_CLEAR, wxComboBox::OnDelete)
123 EVT_MENU(wxID_SELECTALL, wxComboBox::OnSelectAll)
124
125 EVT_UPDATE_UI(wxID_CUT, wxComboBox::OnUpdateCut)
126 EVT_UPDATE_UI(wxID_COPY, wxComboBox::OnUpdateCopy)
127 EVT_UPDATE_UI(wxID_PASTE, wxComboBox::OnUpdatePaste)
128 EVT_UPDATE_UI(wxID_UNDO, wxComboBox::OnUpdateUndo)
129 EVT_UPDATE_UI(wxID_REDO, wxComboBox::OnUpdateRedo)
130 EVT_UPDATE_UI(wxID_CLEAR, wxComboBox::OnUpdateDelete)
131 EVT_UPDATE_UI(wxID_SELECTALL, wxComboBox::OnUpdateSelectAll)
132 END_EVENT_TABLE()
133
134 // ----------------------------------------------------------------------------
135 // function prototypes
136 // ----------------------------------------------------------------------------
137
138 LRESULT APIENTRY _EXPORT wxComboEditWndProc(HWND hWnd,
139 UINT message,
140 WPARAM wParam,
141 LPARAM lParam);
142
143 // ---------------------------------------------------------------------------
144 // global vars
145 // ---------------------------------------------------------------------------
146
147 // the pointer to standard radio button wnd proc
148 static WNDPROC gs_wndprocEdit = (WNDPROC)NULL;
149
150 // ============================================================================
151 // implementation
152 // ============================================================================
153
154 // ----------------------------------------------------------------------------
155 // wnd proc for subclassed edit control
156 // ----------------------------------------------------------------------------
157
158 LRESULT APIENTRY _EXPORT wxComboEditWndProc(HWND hWnd,
159 UINT message,
160 WPARAM wParam,
161 LPARAM lParam)
162 {
163 HWND hwndCombo = ::GetParent(hWnd);
164 wxWindow *win = wxFindWinFromHandle((WXHWND)hwndCombo);
165
166 switch ( message )
167 {
168 // forward some messages to the combobox to generate the appropriate
169 // wxEvents from them
170 case WM_KEYUP:
171 case WM_KEYDOWN:
172 case WM_CHAR:
173 case WM_SYSCHAR:
174 case WM_SYSKEYDOWN:
175 case WM_SYSKEYUP:
176 case WM_SETFOCUS:
177 case WM_KILLFOCUS:
178 {
179 wxComboBox *combo = wxDynamicCast(win, wxComboBox);
180 if ( !combo )
181 {
182 // we can get WM_KILLFOCUS while our parent is already half
183 // destroyed and hence doesn't look like a combobx any
184 // longer, check for it to avoid bogus assert failures
185 if ( !win->IsBeingDeleted() )
186 {
187 wxFAIL_MSG( _T("should have combo as parent") );
188 }
189 }
190 else if ( combo->MSWProcessEditMsg(message, wParam, lParam) )
191 {
192 // handled by parent
193 return 0;
194 }
195 }
196 break;
197
198 case WM_GETDLGCODE:
199 {
200 wxCHECK_MSG( win, 0, _T("should have a parent") );
201
202 if ( win->GetWindowStyle() & wxPROCESS_ENTER )
203 {
204 // need to return a custom dlg code or we'll never get it
205 return DLGC_WANTMESSAGE;
206 }
207 }
208 break;
209
210 // deal with tooltips here
211 #if wxUSE_TOOLTIPS && defined(TTN_NEEDTEXT)
212 case WM_NOTIFY:
213 {
214 wxCHECK_MSG( win, 0, _T("should have a parent") );
215
216 NMHDR* hdr = (NMHDR *)lParam;
217 if ( hdr->code == TTN_NEEDTEXT )
218 {
219 wxToolTip *tooltip = win->GetToolTip();
220 if ( tooltip )
221 {
222 TOOLTIPTEXT *ttt = (TOOLTIPTEXT *)lParam;
223 ttt->lpszText = (wxChar *)tooltip->GetTip().c_str();
224 }
225
226 // processed
227 return 0;
228 }
229 }
230 break;
231 #endif // wxUSE_TOOLTIPS
232 }
233
234 return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit, hWnd, message, wParam, lParam);
235 }
236
237 // ----------------------------------------------------------------------------
238 // wxComboBox callbacks
239 // ----------------------------------------------------------------------------
240
241 WXLRESULT wxComboBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
242 {
243 // TODO: handle WM_CTLCOLOR messages from our EDIT control to be able to
244 // set its colour correctly (to be the same as our own one)
245
246 switch ( nMsg )
247 {
248 case WM_SIZE:
249 // wxStaticBox can generate this message, when modifying the control's style.
250 // This causes the content of the combobox to be selected, for some reason.
251 case WM_STYLECHANGED:
252 {
253 // combobox selection sometimes spontaneously changes when its
254 // size changes, restore it to the old value if necessary
255 long fromOld, toOld;
256 GetSelection(&fromOld, &toOld);
257 WXLRESULT result = wxChoice::MSWWindowProc(nMsg, wParam, lParam);
258
259 long fromNew, toNew;
260 GetSelection(&fromNew, &toNew);
261
262 if ( fromOld != fromNew || toOld != toNew )
263 {
264 SetSelection(fromOld, toOld);
265 }
266
267 return result;
268 }
269 }
270
271 return wxChoice::MSWWindowProc(nMsg, wParam, lParam);
272 }
273
274 bool wxComboBox::MSWProcessEditMsg(WXUINT msg, WXWPARAM wParam, WXLPARAM lParam)
275 {
276 switch ( msg )
277 {
278 case WM_CHAR:
279 // for compatibility with wxTextCtrl, generate a special message
280 // when Enter is pressed
281 if ( wParam == VK_RETURN )
282 {
283 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
284 InitCommandEvent(event);
285 event.SetString(GetValue());
286 event.SetInt(GetSelection());
287 if ( ProcessCommand(event) )
288 {
289 // don't let the event through to the native control
290 // because it doesn't need it and may generate an annoying
291 // beep if it gets it
292 return true;
293 }
294 }
295 // fall through
296
297 case WM_SYSCHAR:
298 return HandleChar(wParam, lParam, true /* isASCII */);
299
300 case WM_SYSKEYDOWN:
301 case WM_KEYDOWN:
302 return HandleKeyDown(wParam, lParam);
303
304 case WM_SYSKEYUP:
305 case WM_KEYUP:
306 return HandleKeyUp(wParam, lParam);
307
308 case WM_SETFOCUS:
309 return HandleSetFocus((WXHWND)wParam);
310
311 case WM_KILLFOCUS:
312 return HandleKillFocus((WXHWND)wParam);
313 }
314
315 return false;
316 }
317
318 bool wxComboBox::MSWCommand(WXUINT param, WXWORD id)
319 {
320 int sel = -1;
321 wxString value;
322
323 switch ( param )
324 {
325 case CBN_SELENDOK:
326 // we need to reset this to prevent the selection from being undone
327 // by wxChoice, see wxChoice::MSWCommand() and comments there
328 m_lastAcceptedSelection = wxID_NONE;
329
330 // set these variables so that they could be also fixed in
331 // CBN_EDITCHANGE below
332 sel = GetSelection();
333 value = GetValue();
334 {
335 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId());
336 event.SetEventObject(this);
337 event.SetInt(sel);
338 event.SetString(value);
339 ProcessCommand(event);
340 }
341
342 // fall through: for compability with wxGTK, also send the text
343 // update event when the selection changes (this also seems more
344 // logical as the text does change)
345
346 case CBN_EDITCHANGE:
347 {
348 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
349 event.SetEventObject(this);
350
351 // if sel != -1, value was already initialized above
352 if ( sel == -1 )
353 {
354 value = wxGetWindowText(GetHwnd());
355 }
356
357 event.SetString(value);
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 wxControl::MSWCommand(param, id);
369 }
370
371 WXHWND wxComboBox::GetEditHWND() const
372 {
373 // this function should not be called for wxCB_READONLY controls, it is
374 // the callers responsability to check this
375 wxASSERT_MSG( !(GetWindowStyle() & wxCB_READONLY),
376 _T("read-only combobox doesn't have any edit control") );
377
378 POINT pt;
379 pt.x = pt.y = 4;
380 HWND hwndEdit = ::ChildWindowFromPoint(GetHwnd(), pt);
381 if ( !hwndEdit || hwndEdit == GetHwnd() )
382 {
383 wxFAIL_MSG(_T("not read only combobox without edit control?"));
384 }
385
386 return (WXHWND)hwndEdit;
387 }
388
389 // ----------------------------------------------------------------------------
390 // wxComboBox creation
391 // ----------------------------------------------------------------------------
392
393 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
394 const wxString& value,
395 const wxPoint& pos,
396 const wxSize& size,
397 int n, const wxString choices[],
398 long style,
399 const wxValidator& validator,
400 const wxString& name)
401 {
402 // pretend that wxComboBox is hidden while it is positioned and resized and
403 // show it only right before leaving this method because otherwise there is
404 // some noticeable flicker while the control rearranges itself
405 m_isShown = false;
406
407 if ( !CreateAndInit(parent, id, pos, size, n, choices, style,
408 validator, name) )
409 return false;
410
411 // we shouldn't call SetValue() for an empty string because this would
412 // (correctly) result in an assert with a read only combobox and is useless
413 // for the other ones anyhow
414 if ( !value.empty() )
415 SetValue(value);
416
417 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
418 // and an edit control inside it and if we want to catch events from this
419 // edit control, we must subclass it as well
420 if ( !(style & wxCB_READONLY) )
421 {
422 gs_wndprocEdit = wxSetWindowProc((HWND)GetEditHWND(),
423 wxComboEditWndProc);
424 }
425
426 // and finally, show the control
427 Show(true);
428
429 return true;
430 }
431
432 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
433 const wxString& value,
434 const wxPoint& pos,
435 const wxSize& size,
436 const wxArrayString& choices,
437 long style,
438 const wxValidator& validator,
439 const wxString& name)
440 {
441 wxCArrayString chs(choices);
442 return Create(parent, id, value, pos, size, chs.GetCount(),
443 chs.GetStrings(), style, validator, name);
444 }
445
446 WXDWORD wxComboBox::MSWGetStyle(long style, WXDWORD *exstyle) const
447 {
448 // we never have an external border
449 WXDWORD msStyle = wxChoice::MSWGetStyle
450 (
451 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle
452 );
453
454 // usually WS_TABSTOP is added by wxControl::MSWGetStyle() but as we're
455 // created hidden (see Create() above), it is not done for us but we still
456 // want to have this style
457 msStyle |= WS_TABSTOP;
458
459 // remove the style always added by wxChoice
460 msStyle &= ~CBS_DROPDOWNLIST;
461
462 if ( style & wxCB_READONLY )
463 msStyle |= CBS_DROPDOWNLIST;
464 #ifndef __WXWINCE__
465 else if ( style & wxCB_SIMPLE )
466 msStyle |= CBS_SIMPLE; // A list (shown always) and edit control
467 #endif
468 else
469 msStyle |= CBS_DROPDOWN;
470
471 // there is no reason to not always use CBS_AUTOHSCROLL, so do use it
472 msStyle |= CBS_AUTOHSCROLL;
473
474 // NB: we used to also add CBS_NOINTEGRALHEIGHT here but why?
475
476 return msStyle;
477 }
478
479 // ----------------------------------------------------------------------------
480 // wxComboBox text control-like methods
481 // ----------------------------------------------------------------------------
482
483 wxString wxComboBox::GetValue() const
484 {
485 return wxGetWindowText(m_hWnd);
486 }
487
488 void wxComboBox::SetValue(const wxString& value)
489 {
490 if ( HasFlag(wxCB_READONLY) )
491 SetStringSelection(value);
492 else
493 SetWindowText(GetHwnd(), value.c_str());
494 }
495
496 // Clipboard operations
497 void wxComboBox::Copy()
498 {
499 SendMessage(GetHwnd(), WM_COPY, 0, 0L);
500 }
501
502 void wxComboBox::Cut()
503 {
504 SendMessage(GetHwnd(), WM_CUT, 0, 0L);
505 }
506
507 void wxComboBox::Paste()
508 {
509 SendMessage(GetHwnd(), WM_PASTE, 0, 0L);
510 }
511
512 void wxComboBox::Undo()
513 {
514 if (CanUndo())
515 {
516 HWND hEditWnd = (HWND) GetEditHWND();
517 if ( hEditWnd )
518 ::SendMessage(hEditWnd, EM_UNDO, 0, 0);
519 }
520 }
521
522 void wxComboBox::Redo()
523 {
524 if (CanUndo())
525 {
526 // Same as Undo, since Undo undoes the undo, i.e. a redo.
527 HWND hEditWnd = (HWND) GetEditHWND();
528 if ( hEditWnd )
529 ::SendMessage(hEditWnd, EM_UNDO, 0, 0);
530 }
531 }
532
533 void wxComboBox::SelectAll()
534 {
535 SetSelection(0, GetLastPosition());
536 }
537
538 bool wxComboBox::CanUndo() const
539 {
540 if (!IsEditable())
541 return false;
542
543 HWND hEditWnd = (HWND) GetEditHWND();
544 if ( hEditWnd )
545 return ::SendMessage(hEditWnd, EM_CANUNDO, 0, 0) != 0;
546 else
547 return false;
548 }
549
550 bool wxComboBox::CanRedo() const
551 {
552 if (!IsEditable())
553 return false;
554
555 HWND hEditWnd = (HWND) GetEditHWND();
556 if ( hEditWnd )
557 return ::SendMessage(hEditWnd, EM_CANUNDO, 0, 0) != 0;
558 else
559 return false;
560 }
561
562 bool wxComboBox::HasSelection() const
563 {
564 long from, to;
565 GetSelection(&from, &to);
566 return from != to;
567 }
568
569 bool wxComboBox::CanCopy() const
570 {
571 // Can copy if there's a selection
572 return HasSelection();
573 }
574
575 bool wxComboBox::CanCut() const
576 {
577 return IsEditable() && CanCopy();
578 }
579
580 bool wxComboBox::CanPaste() const
581 {
582 if ( !IsEditable() )
583 return false;
584
585 // Standard edit control: check for straight text on clipboard
586 if ( !::OpenClipboard(GetHwndOf(wxTheApp->GetTopWindow())) )
587 return false;
588
589 bool isTextAvailable = ::IsClipboardFormatAvailable(CF_TEXT) != 0;
590 ::CloseClipboard();
591
592 return isTextAvailable;
593 }
594
595 bool wxComboBox::IsEditable() const
596 {
597 return !HasFlag(wxCB_READONLY);
598 }
599
600 void wxComboBox::SetEditable(bool WXUNUSED(editable))
601 {
602 // Can't implement in MSW?
603 // HWND hWnd = GetHwnd();
604 // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
605 }
606
607 void wxComboBox::SetInsertionPoint(long pos)
608 {
609 if ( GetWindowStyle() & wxCB_READONLY )
610 return;
611
612 #ifdef __WIN32__
613 HWND hWnd = GetHwnd();
614 ::SendMessage(hWnd, CB_SETEDITSEL, 0, MAKELPARAM(pos, pos));
615 HWND hEditWnd = (HWND) GetEditHWND();
616 if ( hEditWnd )
617 {
618 // Scroll insertion point into view
619 SendMessage(hEditWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
620 // Why is this necessary? (Copied from wxTextCtrl::SetInsertionPoint)
621 SendMessage(hEditWnd, EM_REPLACESEL, 0, (LPARAM) wxEmptyString);
622 }
623 #endif // __WIN32__
624 }
625
626 void wxComboBox::SetInsertionPointEnd()
627 {
628 // setting insertion point doesn't make sense for read only comboboxes
629 if ( !(GetWindowStyle() & wxCB_READONLY) )
630 {
631 wxTextPos pos = GetLastPosition();
632 SetInsertionPoint(pos);
633 }
634 }
635
636 long wxComboBox::GetInsertionPoint() const
637 {
638 // CB_GETEDITSEL returns the index of the first character of the selection in
639 // its low-order word
640 DWORD pos= (DWORD)::SendMessage(GetHwnd(), CB_GETEDITSEL, 0, 0L);
641 return LOWORD(pos);
642 }
643
644 wxTextPos wxComboBox::GetLastPosition() const
645 {
646 HWND hEditWnd = (HWND) GetEditHWND();
647
648 // Get number of characters in the last (only) line. We'll add this to the character
649 // index for the last line, 1st position.
650 wxTextPos lineLength = (wxTextPos)SendMessage(hEditWnd, EM_LINELENGTH, (WPARAM) 0, (LPARAM)0L);
651
652 return lineLength;
653 }
654
655 void wxComboBox::Replace(long from, long to, const wxString& value)
656 {
657 #if wxUSE_CLIPBOARD
658 Remove(from, to);
659
660 // Now replace with 'value', by pasting.
661 wxSetClipboardData(wxDF_TEXT, (wxObject *)(const wxChar *)value, 0, 0);
662
663 // Paste into edit control
664 SendMessage(GetHwnd(), WM_PASTE, (WPARAM)0, (LPARAM)0L);
665 #else
666 wxUnusedVar(from);
667 wxUnusedVar(to);
668 wxUnusedVar(value);
669 #endif
670 }
671
672 void wxComboBox::Remove(long from, long to)
673 {
674 // Set selection and remove it
675 SetSelection(from, to);
676 SendMessage(GetHwnd(), WM_CUT, (WPARAM)0, (LPARAM)0);
677 }
678
679 void wxComboBox::SetSelection(long from, long to)
680 {
681 // if from and to are both -1, it means (in wxWidgets) that all text should
682 // be selected, translate this into Windows convention
683 if ( (from == -1) && (to == -1) )
684 {
685 from = 0;
686 }
687
688 if ( SendMessage(GetHwnd(), CB_SETEDITSEL,
689 0, (LPARAM)MAKELONG(from, to)) == CB_ERR )
690 {
691 wxLogDebug(_T("CB_SETEDITSEL failed"));
692 }
693 }
694
695 void wxComboBox::GetSelection(long* from, long* to) const
696 {
697 DWORD dwStart, dwEnd;
698 if ( ::SendMessage(GetHwnd(), CB_GETEDITSEL,
699 (WPARAM)&dwStart, (LPARAM)&dwEnd) == CB_ERR )
700 {
701 *from =
702 *to = 0;
703 }
704 else
705 {
706 *from = dwStart;
707 *to = dwEnd;
708 }
709 }
710
711 // ----------------------------------------------------------------------------
712 // standard event handling
713 // ----------------------------------------------------------------------------
714
715 void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
716 {
717 Cut();
718 }
719
720 void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
721 {
722 Copy();
723 }
724
725 void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
726 {
727 Paste();
728 }
729
730 void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
731 {
732 Undo();
733 }
734
735 void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
736 {
737 Redo();
738 }
739
740 void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
741 {
742 long from, to;
743 GetSelection(& from, & to);
744 if (from != -1 && to != -1)
745 Remove(from, to);
746 }
747
748 void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
749 {
750 SetSelection(-1, -1);
751 }
752
753 void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
754 {
755 event.Enable( CanCut() );
756 }
757
758 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
759 {
760 event.Enable( CanCopy() );
761 }
762
763 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
764 {
765 event.Enable( CanPaste() );
766 }
767
768 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
769 {
770 event.Enable( CanUndo() );
771 }
772
773 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
774 {
775 event.Enable( CanRedo() );
776 }
777
778 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
779 {
780 event.Enable(HasSelection() && IsEditable());
781 }
782
783 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
784 {
785 event.Enable(GetLastPosition() > 0);
786 }
787
788 #endif // wxUSE_COMBOBOX
789