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