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