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