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