]>
Commit | Line | Data |
---|---|---|
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 | #include "wx/brush.h" | |
38 | #endif | |
39 | ||
40 | #include "wx/clipbrd.h" | |
41 | #include "wx/msw/private.h" | |
42 | ||
43 | // include <commctrl.h> "properly" | |
44 | #include "wx/msw/wrapcctl.h" | |
45 | ||
46 | #if wxUSE_TOOLTIPS | |
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 | case WM_CUT: | |
212 | case WM_COPY: | |
213 | case WM_PASTE: | |
214 | if( win->HandleClipboardEvent( message ) ) | |
215 | return 0; | |
216 | break; | |
217 | } | |
218 | ||
219 | return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit, hWnd, message, wParam, lParam); | |
220 | } | |
221 | ||
222 | // ---------------------------------------------------------------------------- | |
223 | // wxComboBox callbacks | |
224 | // ---------------------------------------------------------------------------- | |
225 | ||
226 | WXLRESULT wxComboBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) | |
227 | { | |
228 | // TODO: handle WM_CTLCOLOR messages from our EDIT control to be able to | |
229 | // set its colour correctly (to be the same as our own one) | |
230 | ||
231 | switch ( nMsg ) | |
232 | { | |
233 | case WM_SIZE: | |
234 | // wxStaticBox can generate this message, when modifying the control's style. | |
235 | // This causes the content of the combobox to be selected, for some reason. | |
236 | case WM_STYLECHANGED: | |
237 | { | |
238 | // combobox selection sometimes spontaneously changes when its | |
239 | // size changes, restore it to the old value if necessary | |
240 | long fromOld, toOld; | |
241 | GetSelection(&fromOld, &toOld); | |
242 | WXLRESULT result = wxChoice::MSWWindowProc(nMsg, wParam, lParam); | |
243 | ||
244 | long fromNew, toNew; | |
245 | GetSelection(&fromNew, &toNew); | |
246 | ||
247 | if ( fromOld != fromNew || toOld != toNew ) | |
248 | { | |
249 | SetSelection(fromOld, toOld); | |
250 | } | |
251 | ||
252 | return result; | |
253 | } | |
254 | } | |
255 | ||
256 | return wxChoice::MSWWindowProc(nMsg, wParam, lParam); | |
257 | } | |
258 | ||
259 | bool wxComboBox::MSWProcessEditMsg(WXUINT msg, WXWPARAM wParam, WXLPARAM lParam) | |
260 | { | |
261 | switch ( msg ) | |
262 | { | |
263 | case WM_CHAR: | |
264 | // for compatibility with wxTextCtrl, generate a special message | |
265 | // when Enter is pressed | |
266 | if ( wParam == VK_RETURN ) | |
267 | { | |
268 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
269 | ||
270 | const int sel = GetSelection(); | |
271 | event.SetInt(sel); | |
272 | event.SetString(GetValue()); | |
273 | InitCommandEventWithItems(event, sel); | |
274 | ||
275 | if ( ProcessCommand(event) ) | |
276 | { | |
277 | // don't let the event through to the native control | |
278 | // because it doesn't need it and may generate an annoying | |
279 | // beep if it gets it | |
280 | return true; | |
281 | } | |
282 | } | |
283 | // fall through | |
284 | ||
285 | case WM_SYSCHAR: | |
286 | return HandleChar(wParam, lParam, true /* isASCII */); | |
287 | ||
288 | case WM_SYSKEYDOWN: | |
289 | case WM_KEYDOWN: | |
290 | return HandleKeyDown(wParam, lParam); | |
291 | ||
292 | case WM_SYSKEYUP: | |
293 | case WM_KEYUP: | |
294 | return HandleKeyUp(wParam, lParam); | |
295 | ||
296 | case WM_SETFOCUS: | |
297 | return HandleSetFocus((WXHWND)wParam); | |
298 | ||
299 | case WM_KILLFOCUS: | |
300 | return HandleKillFocus((WXHWND)wParam); | |
301 | } | |
302 | ||
303 | return false; | |
304 | } | |
305 | ||
306 | bool wxComboBox::MSWCommand(WXUINT param, WXWORD id) | |
307 | { | |
308 | int sel = -1; | |
309 | wxString value; | |
310 | ||
311 | switch ( param ) | |
312 | { | |
313 | case CBN_SELENDOK: | |
314 | #ifndef __SMARTPHONE__ | |
315 | // we need to reset this to prevent the selection from being undone | |
316 | // by wxChoice, see wxChoice::MSWCommand() and comments there | |
317 | m_lastAcceptedSelection = wxID_NONE; | |
318 | #endif | |
319 | ||
320 | // set these variables so that they could be also fixed in | |
321 | // CBN_EDITCHANGE below | |
322 | sel = GetSelection(); | |
323 | value = GetStringSelection(); | |
324 | { | |
325 | wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId()); | |
326 | event.SetInt(sel); | |
327 | event.SetString(value); | |
328 | InitCommandEventWithItems(event, sel); | |
329 | ||
330 | ProcessCommand(event); | |
331 | } | |
332 | ||
333 | // fall through: for compability with wxGTK, also send the text | |
334 | // update event when the selection changes (this also seems more | |
335 | // logical as the text does change) | |
336 | ||
337 | case CBN_EDITCHANGE: | |
338 | { | |
339 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId()); | |
340 | ||
341 | // if sel != -1, value was already initialized above | |
342 | if ( sel == -1 ) | |
343 | { | |
344 | value = wxGetWindowText(GetHwnd()); | |
345 | } | |
346 | ||
347 | event.SetString(value); | |
348 | InitCommandEventWithItems(event, sel); | |
349 | ||
350 | ProcessCommand(event); | |
351 | } | |
352 | break; | |
353 | ||
354 | default: | |
355 | return wxChoice::MSWCommand(param, id); | |
356 | } | |
357 | ||
358 | // skip wxChoice version as it would generate its own events for | |
359 | // CBN_SELENDOK | |
360 | return wxControl::MSWCommand(param, id); | |
361 | } | |
362 | ||
363 | WXHWND wxComboBox::GetEditHWND() const | |
364 | { | |
365 | // this function should not be called for wxCB_READONLY controls, it is | |
366 | // the callers responsability to check this | |
367 | wxASSERT_MSG( !(GetWindowStyle() & wxCB_READONLY), | |
368 | _T("read-only combobox doesn't have any edit control") ); | |
369 | ||
370 | POINT pt; | |
371 | pt.x = pt.y = 4; | |
372 | HWND hwndEdit = ::ChildWindowFromPoint(GetHwnd(), pt); | |
373 | if ( !hwndEdit || hwndEdit == GetHwnd() ) | |
374 | { | |
375 | wxFAIL_MSG(_T("not read only combobox without edit control?")); | |
376 | } | |
377 | ||
378 | return (WXHWND)hwndEdit; | |
379 | } | |
380 | ||
381 | // ---------------------------------------------------------------------------- | |
382 | // wxComboBox creation | |
383 | // ---------------------------------------------------------------------------- | |
384 | ||
385 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, | |
386 | const wxString& value, | |
387 | const wxPoint& pos, | |
388 | const wxSize& size, | |
389 | int n, const wxString choices[], | |
390 | long style, | |
391 | const wxValidator& validator, | |
392 | const wxString& name) | |
393 | { | |
394 | // pretend that wxComboBox is hidden while it is positioned and resized and | |
395 | // show it only right before leaving this method because otherwise there is | |
396 | // some noticeable flicker while the control rearranges itself | |
397 | m_isShown = false; | |
398 | ||
399 | if ( !CreateAndInit(parent, id, pos, size, n, choices, style, | |
400 | validator, name) ) | |
401 | return false; | |
402 | ||
403 | // we shouldn't call SetValue() for an empty string because this would | |
404 | // (correctly) result in an assert with a read only combobox and is useless | |
405 | // for the other ones anyhow | |
406 | if ( !value.empty() ) | |
407 | SetValue(value); | |
408 | ||
409 | // a (not read only) combobox is, in fact, 2 controls: the combobox itself | |
410 | // and an edit control inside it and if we want to catch events from this | |
411 | // edit control, we must subclass it as well | |
412 | if ( !(style & wxCB_READONLY) ) | |
413 | { | |
414 | gs_wndprocEdit = wxSetWindowProc((HWND)GetEditHWND(), | |
415 | wxComboEditWndProc); | |
416 | } | |
417 | ||
418 | // and finally, show the control | |
419 | Show(true); | |
420 | ||
421 | return true; | |
422 | } | |
423 | ||
424 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, | |
425 | const wxString& value, | |
426 | const wxPoint& pos, | |
427 | const wxSize& size, | |
428 | const wxArrayString& choices, | |
429 | long style, | |
430 | const wxValidator& validator, | |
431 | const wxString& name) | |
432 | { | |
433 | wxCArrayString chs(choices); | |
434 | return Create(parent, id, value, pos, size, chs.GetCount(), | |
435 | chs.GetStrings(), style, validator, name); | |
436 | } | |
437 | ||
438 | WXDWORD wxComboBox::MSWGetStyle(long style, WXDWORD *exstyle) const | |
439 | { | |
440 | // we never have an external border | |
441 | WXDWORD msStyle = wxChoice::MSWGetStyle | |
442 | ( | |
443 | (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle | |
444 | ); | |
445 | ||
446 | // usually WS_TABSTOP is added by wxControl::MSWGetStyle() but as we're | |
447 | // created hidden (see Create() above), it is not done for us but we still | |
448 | // want to have this style | |
449 | msStyle |= WS_TABSTOP; | |
450 | ||
451 | // remove the style always added by wxChoice | |
452 | msStyle &= ~CBS_DROPDOWNLIST; | |
453 | ||
454 | if ( style & wxCB_READONLY ) | |
455 | msStyle |= CBS_DROPDOWNLIST; | |
456 | #ifndef __WXWINCE__ | |
457 | else if ( style & wxCB_SIMPLE ) | |
458 | msStyle |= CBS_SIMPLE; // A list (shown always) and edit control | |
459 | #endif | |
460 | else | |
461 | msStyle |= CBS_DROPDOWN; | |
462 | ||
463 | // there is no reason to not always use CBS_AUTOHSCROLL, so do use it | |
464 | msStyle |= CBS_AUTOHSCROLL; | |
465 | ||
466 | // NB: we used to also add CBS_NOINTEGRALHEIGHT here but why? | |
467 | ||
468 | return msStyle; | |
469 | } | |
470 | ||
471 | // ---------------------------------------------------------------------------- | |
472 | // wxComboBox text control-like methods | |
473 | // ---------------------------------------------------------------------------- | |
474 | ||
475 | wxString wxComboBox::GetValue() const | |
476 | { | |
477 | return wxGetWindowText(m_hWnd); | |
478 | } | |
479 | ||
480 | void wxComboBox::SetValue(const wxString& value) | |
481 | { | |
482 | if ( HasFlag(wxCB_READONLY) ) | |
483 | SetStringSelection(value); | |
484 | else | |
485 | SetWindowText(GetHwnd(), value.c_str()); | |
486 | } | |
487 | ||
488 | // Clipboard operations | |
489 | void wxComboBox::Copy() | |
490 | { | |
491 | SendMessage(GetHwnd(), WM_COPY, 0, 0L); | |
492 | } | |
493 | ||
494 | void wxComboBox::Cut() | |
495 | { | |
496 | SendMessage(GetHwnd(), WM_CUT, 0, 0L); | |
497 | } | |
498 | ||
499 | void wxComboBox::Paste() | |
500 | { | |
501 | SendMessage(GetHwnd(), WM_PASTE, 0, 0L); | |
502 | } | |
503 | ||
504 | void wxComboBox::Undo() | |
505 | { | |
506 | if (CanUndo()) | |
507 | { | |
508 | HWND hEditWnd = (HWND) GetEditHWND(); | |
509 | if ( hEditWnd ) | |
510 | ::SendMessage(hEditWnd, EM_UNDO, 0, 0); | |
511 | } | |
512 | } | |
513 | ||
514 | void wxComboBox::Redo() | |
515 | { | |
516 | if (CanUndo()) | |
517 | { | |
518 | // Same as Undo, since Undo undoes the undo, i.e. a redo. | |
519 | HWND hEditWnd = (HWND) GetEditHWND(); | |
520 | if ( hEditWnd ) | |
521 | ::SendMessage(hEditWnd, EM_UNDO, 0, 0); | |
522 | } | |
523 | } | |
524 | ||
525 | void wxComboBox::SelectAll() | |
526 | { | |
527 | SetSelection(0, GetLastPosition()); | |
528 | } | |
529 | ||
530 | bool wxComboBox::CanUndo() const | |
531 | { | |
532 | if (!IsEditable()) | |
533 | return false; | |
534 | ||
535 | HWND hEditWnd = (HWND) GetEditHWND(); | |
536 | if ( hEditWnd ) | |
537 | return ::SendMessage(hEditWnd, EM_CANUNDO, 0, 0) != 0; | |
538 | else | |
539 | return false; | |
540 | } | |
541 | ||
542 | bool wxComboBox::CanRedo() const | |
543 | { | |
544 | if (!IsEditable()) | |
545 | return false; | |
546 | ||
547 | HWND hEditWnd = (HWND) GetEditHWND(); | |
548 | if ( hEditWnd ) | |
549 | return ::SendMessage(hEditWnd, EM_CANUNDO, 0, 0) != 0; | |
550 | else | |
551 | return false; | |
552 | } | |
553 | ||
554 | bool wxComboBox::HasSelection() const | |
555 | { | |
556 | long from, to; | |
557 | GetSelection(&from, &to); | |
558 | return from != to; | |
559 | } | |
560 | ||
561 | bool wxComboBox::CanCopy() const | |
562 | { | |
563 | // Can copy if there's a selection | |
564 | return HasSelection(); | |
565 | } | |
566 | ||
567 | bool wxComboBox::CanCut() const | |
568 | { | |
569 | return IsEditable() && CanCopy(); | |
570 | } | |
571 | ||
572 | bool wxComboBox::CanPaste() const | |
573 | { | |
574 | if ( !IsEditable() ) | |
575 | return false; | |
576 | ||
577 | // Standard edit control: check for straight text on clipboard | |
578 | if ( !::OpenClipboard(GetHwndOf(wxTheApp->GetTopWindow())) ) | |
579 | return false; | |
580 | ||
581 | bool isTextAvailable = ::IsClipboardFormatAvailable(CF_TEXT) != 0; | |
582 | ::CloseClipboard(); | |
583 | ||
584 | return isTextAvailable; | |
585 | } | |
586 | ||
587 | bool wxComboBox::IsEditable() const | |
588 | { | |
589 | return !HasFlag(wxCB_READONLY); | |
590 | } | |
591 | ||
592 | void wxComboBox::SetEditable(bool WXUNUSED(editable)) | |
593 | { | |
594 | // Can't implement in MSW? | |
595 | // HWND hWnd = GetHwnd(); | |
596 | // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L); | |
597 | } | |
598 | ||
599 | void wxComboBox::SetInsertionPoint(long pos) | |
600 | { | |
601 | if ( GetWindowStyle() & wxCB_READONLY ) | |
602 | return; | |
603 | ||
604 | #ifdef __WIN32__ | |
605 | HWND hWnd = GetHwnd(); | |
606 | ::SendMessage(hWnd, CB_SETEDITSEL, 0, MAKELPARAM(pos, pos)); | |
607 | HWND hEditWnd = (HWND) GetEditHWND(); | |
608 | if ( hEditWnd ) | |
609 | { | |
610 | // Scroll insertion point into view | |
611 | SendMessage(hEditWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0); | |
612 | // Why is this necessary? (Copied from wxTextCtrl::SetInsertionPoint) | |
613 | SendMessage(hEditWnd, EM_REPLACESEL, 0, (LPARAM) wxEmptyString); | |
614 | } | |
615 | #endif // __WIN32__ | |
616 | } | |
617 | ||
618 | void wxComboBox::SetInsertionPointEnd() | |
619 | { | |
620 | // setting insertion point doesn't make sense for read only comboboxes | |
621 | if ( !(GetWindowStyle() & wxCB_READONLY) ) | |
622 | { | |
623 | wxTextPos pos = GetLastPosition(); | |
624 | SetInsertionPoint(pos); | |
625 | } | |
626 | } | |
627 | ||
628 | long wxComboBox::GetInsertionPoint() const | |
629 | { | |
630 | // CB_GETEDITSEL returns the index of the first character of the selection in | |
631 | // its low-order word | |
632 | DWORD pos= (DWORD)::SendMessage(GetHwnd(), CB_GETEDITSEL, 0, 0L); | |
633 | return LOWORD(pos); | |
634 | } | |
635 | ||
636 | wxTextPos wxComboBox::GetLastPosition() const | |
637 | { | |
638 | HWND hEditWnd = (HWND) GetEditHWND(); | |
639 | ||
640 | // Get number of characters in the last (only) line. We'll add this to the character | |
641 | // index for the last line, 1st position. | |
642 | wxTextPos lineLength = (wxTextPos)SendMessage(hEditWnd, EM_LINELENGTH, (WPARAM) 0, (LPARAM)0L); | |
643 | ||
644 | return lineLength; | |
645 | } | |
646 | ||
647 | void wxComboBox::Replace(long from, long to, const wxString& value) | |
648 | { | |
649 | #if wxUSE_CLIPBOARD | |
650 | Remove(from, to); | |
651 | ||
652 | // Now replace with 'value', by pasting. | |
653 | wxSetClipboardData(wxDF_TEXT, (wxObject *)(const wxChar *)value, 0, 0); | |
654 | ||
655 | // Paste into edit control | |
656 | SendMessage(GetHwnd(), WM_PASTE, (WPARAM)0, (LPARAM)0L); | |
657 | #else | |
658 | wxUnusedVar(from); | |
659 | wxUnusedVar(to); | |
660 | wxUnusedVar(value); | |
661 | #endif | |
662 | } | |
663 | ||
664 | void wxComboBox::Remove(long from, long to) | |
665 | { | |
666 | // Set selection and remove it | |
667 | SetSelection(from, to); | |
668 | SendMessage(GetHwnd(), WM_CUT, (WPARAM)0, (LPARAM)0); | |
669 | } | |
670 | ||
671 | void wxComboBox::SetSelection(long from, long to) | |
672 | { | |
673 | // if from and to are both -1, it means (in wxWidgets) that all text should | |
674 | // be selected, translate this into Windows convention | |
675 | if ( (from == -1) && (to == -1) ) | |
676 | { | |
677 | from = 0; | |
678 | } | |
679 | ||
680 | if ( SendMessage(GetHwnd(), CB_SETEDITSEL, | |
681 | 0, (LPARAM)MAKELONG(from, to)) == CB_ERR ) | |
682 | { | |
683 | wxLogDebug(_T("CB_SETEDITSEL failed")); | |
684 | } | |
685 | } | |
686 | ||
687 | void wxComboBox::GetSelection(long* from, long* to) const | |
688 | { | |
689 | DWORD dwStart, dwEnd; | |
690 | if ( ::SendMessage(GetHwnd(), CB_GETEDITSEL, | |
691 | (WPARAM)&dwStart, (LPARAM)&dwEnd) == CB_ERR ) | |
692 | { | |
693 | *from = | |
694 | *to = 0; | |
695 | } | |
696 | else | |
697 | { | |
698 | *from = dwStart; | |
699 | *to = dwEnd; | |
700 | } | |
701 | } | |
702 | ||
703 | // ---------------------------------------------------------------------------- | |
704 | // standard event handling | |
705 | // ---------------------------------------------------------------------------- | |
706 | ||
707 | void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event)) | |
708 | { | |
709 | Cut(); | |
710 | } | |
711 | ||
712 | void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
713 | { | |
714 | Copy(); | |
715 | } | |
716 | ||
717 | void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
718 | { | |
719 | Paste(); | |
720 | } | |
721 | ||
722 | void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
723 | { | |
724 | Undo(); | |
725 | } | |
726 | ||
727 | void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
728 | { | |
729 | Redo(); | |
730 | } | |
731 | ||
732 | void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event)) | |
733 | { | |
734 | long from, to; | |
735 | GetSelection(& from, & to); | |
736 | if (from != -1 && to != -1) | |
737 | Remove(from, to); | |
738 | } | |
739 | ||
740 | void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event)) | |
741 | { | |
742 | SetSelection(-1, -1); | |
743 | } | |
744 | ||
745 | void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event) | |
746 | { | |
747 | event.Enable( CanCut() ); | |
748 | } | |
749 | ||
750 | void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event) | |
751 | { | |
752 | event.Enable( CanCopy() ); | |
753 | } | |
754 | ||
755 | void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event) | |
756 | { | |
757 | event.Enable( CanPaste() ); | |
758 | } | |
759 | ||
760 | void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event) | |
761 | { | |
762 | event.Enable( CanUndo() ); | |
763 | } | |
764 | ||
765 | void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event) | |
766 | { | |
767 | event.Enable( CanRedo() ); | |
768 | } | |
769 | ||
770 | void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event) | |
771 | { | |
772 | event.Enable(HasSelection() && IsEditable()); | |
773 | } | |
774 | ||
775 | void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event) | |
776 | { | |
777 | event.Enable(GetLastPosition() > 0); | |
778 | } | |
779 | ||
780 | #endif // wxUSE_COMBOBOX |