]>
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 | #ifndef WX_PRECOMP | |
30 | #include "wx/settings.h" | |
31 | #include "wx/log.h" | |
32 | // for wxEVT_COMMAND_TEXT_ENTER | |
33 | #include "wx/textctrl.h" | |
34 | #endif | |
35 | ||
36 | #include "wx/app.h" | |
37 | #include "wx/combobox.h" | |
38 | #include "wx/brush.h" | |
39 | #include "wx/clipbrd.h" | |
40 | #include "wx/msw/private.h" | |
41 | ||
42 | #if wxUSE_TOOLTIPS | |
43 | #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__) | |
44 | #include <commctrl.h> | |
45 | #endif | |
46 | #include "wx/tooltip.h" | |
47 | #endif // wxUSE_TOOLTIPS | |
48 | ||
49 | // ---------------------------------------------------------------------------- | |
50 | // wxWin macros | |
51 | // ---------------------------------------------------------------------------- | |
52 | ||
53 | #if wxUSE_EXTENDED_RTTI | |
54 | WX_DEFINE_FLAGS( wxComboBoxStyle ) | |
55 | ||
56 | wxBEGIN_FLAGS( wxComboBoxStyle ) | |
57 | // new style border flags, we put them first to | |
58 | // use them for streaming out | |
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) | |
65 | ||
66 | // old style border flags | |
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) | |
72 | wxFLAGS_MEMBER(wxBORDER) | |
73 | ||
74 | // standard window styles | |
75 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) | |
76 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
77 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
78 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
79 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) | |
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 | ||
89 | wxEND_FLAGS( wxComboBoxStyle ) | |
90 | ||
91 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxComboBox, wxControl,"wx/combobox.h") | |
92 | ||
93 | wxBEGIN_PROPERTIES_TABLE(wxComboBox) | |
94 | wxEVENT_PROPERTY( Select , wxEVT_COMMAND_COMBOBOX_SELECTED , wxCommandEvent ) | |
95 | wxEVENT_PROPERTY( TextEnter , wxEVT_COMMAND_TEXT_ENTER , wxCommandEvent ) | |
96 | ||
97 | // TODO DELEGATES | |
98 | wxPROPERTY( Font , wxFont , SetFont , GetFont , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
99 | wxPROPERTY_COLLECTION( Choices , wxArrayString , wxString , AppendString , GetStrings , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
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")) | |
102 | wxPROPERTY_FLAGS( WindowStyle , wxComboBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style | |
103 | wxEND_PROPERTIES_TABLE() | |
104 | ||
105 | wxBEGIN_HANDLERS_TABLE(wxComboBox) | |
106 | wxEND_HANDLERS_TABLE() | |
107 | ||
108 | wxCONSTRUCTOR_5( wxComboBox , wxWindow* , Parent , wxWindowID , Id , wxString , Value , wxPoint , Position , wxSize , Size ) | |
109 | ||
110 | #else | |
111 | ||
112 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl) | |
113 | ||
114 | #endif | |
115 | ||
116 | BEGIN_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) | |
132 | END_EVENT_TABLE() | |
133 | ||
134 | // ---------------------------------------------------------------------------- | |
135 | // function prototypes | |
136 | // ---------------------------------------------------------------------------- | |
137 | ||
138 | LRESULT 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 | |
148 | static WNDPROC gs_wndprocEdit = (WNDPROC)NULL; | |
149 | ||
150 | // ============================================================================ | |
151 | // implementation | |
152 | // ============================================================================ | |
153 | ||
154 | // ---------------------------------------------------------------------------- | |
155 | // wnd proc for subclassed edit control | |
156 | // ---------------------------------------------------------------------------- | |
157 | ||
158 | LRESULT 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 | { | |
168 | // forward some messages to the combobox to generate the appropriate | |
169 | // wxEvents from them | |
170 | case WM_KEYUP: | |
171 | case WM_KEYDOWN: | |
172 | case WM_CHAR: | |
173 | case WM_SYSCHAR: | |
174 | case WM_SYSKEYDOWN: | |
175 | case WM_SYSKEYUP: | |
176 | case WM_SETFOCUS: | |
177 | case WM_KILLFOCUS: | |
178 | { | |
179 | wxComboBox *combo = wxDynamicCast(win, wxComboBox); | |
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 | |
193 | return 0; | |
194 | } | |
195 | } | |
196 | break; | |
197 | ||
198 | case WM_GETDLGCODE: | |
199 | { | |
200 | wxCHECK_MSG( win, 0, _T("should have a parent") ); | |
201 | ||
202 | if ( win->GetWindowStyle() & wxTE_PROCESS_ENTER ) | |
203 | { | |
204 | // need to return a custom dlg code or we'll never get it | |
205 | return DLGC_WANTMESSAGE; | |
206 | } | |
207 | } | |
208 | break; | |
209 | ||
210 | // deal with tooltips here | |
211 | #if wxUSE_TOOLTIPS && defined(TTN_NEEDTEXT) | |
212 | case WM_NOTIFY: | |
213 | { | |
214 | wxCHECK_MSG( win, 0, _T("should have a parent") ); | |
215 | ||
216 | NMHDR* hdr = (NMHDR *)lParam; | |
217 | if ( hdr->code == TTN_NEEDTEXT ) | |
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 | ||
237 | // ---------------------------------------------------------------------------- | |
238 | // wxComboBox callbacks | |
239 | // ---------------------------------------------------------------------------- | |
240 | ||
241 | WXLRESULT wxComboBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) | |
242 | { | |
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) | |
245 | ||
246 | switch ( nMsg ) | |
247 | { | |
248 | case WM_SIZE: | |
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: | |
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); | |
258 | ||
259 | long fromNew, toNew; | |
260 | GetSelection(&fromNew, &toNew); | |
261 | ||
262 | if ( fromOld != fromNew || toOld != toNew ) | |
263 | { | |
264 | SetSelection(fromOld, toOld); | |
265 | } | |
266 | ||
267 | return result; | |
268 | } | |
269 | } | |
270 | ||
271 | return wxChoice::MSWWindowProc(nMsg, wParam, lParam); | |
272 | } | |
273 | ||
274 | bool wxComboBox::MSWProcessEditMsg(WXUINT msg, WXWPARAM wParam, WXLPARAM lParam) | |
275 | { | |
276 | switch ( msg ) | |
277 | { | |
278 | case WM_CHAR: | |
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()); | |
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 | } | |
294 | } | |
295 | // fall through | |
296 | ||
297 | case WM_SYSCHAR: | |
298 | return HandleChar(wParam, lParam, true /* isASCII */); | |
299 | ||
300 | case WM_SYSKEYDOWN: | |
301 | case WM_KEYDOWN: | |
302 | return HandleKeyDown(wParam, lParam); | |
303 | ||
304 | case WM_SYSKEYUP: | |
305 | case WM_KEYUP: | |
306 | return HandleKeyUp(wParam, lParam); | |
307 | ||
308 | case WM_SETFOCUS: | |
309 | return HandleSetFocus((WXHWND)wParam); | |
310 | ||
311 | case WM_KILLFOCUS: | |
312 | return HandleKillFocus((WXHWND)wParam); | |
313 | } | |
314 | ||
315 | return false; | |
316 | } | |
317 | ||
318 | bool wxComboBox::MSWCommand(WXUINT param, WXWORD id) | |
319 | { | |
320 | int sel = -1; | |
321 | wxString value; | |
322 | ||
323 | switch ( param ) | |
324 | { | |
325 | case CBN_SELENDOK: | |
326 | #ifndef __SMARTPHONE__ | |
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; | |
330 | #endif | |
331 | ||
332 | // set these variables so that they could be also fixed in | |
333 | // CBN_EDITCHANGE below | |
334 | sel = GetSelection(); | |
335 | value = GetStringSelection(); | |
336 | { | |
337 | wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId()); | |
338 | event.SetEventObject(this); | |
339 | event.SetInt(sel); | |
340 | event.SetString(value); | |
341 | ProcessCommand(event); | |
342 | } | |
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) | |
347 | ||
348 | case CBN_EDITCHANGE: | |
349 | { | |
350 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId()); | |
351 | event.SetEventObject(this); | |
352 | ||
353 | // if sel != -1, value was already initialized above | |
354 | if ( sel == -1 ) | |
355 | { | |
356 | value = wxGetWindowText(GetHwnd()); | |
357 | } | |
358 | ||
359 | event.SetString(value); | |
360 | ProcessCommand(event); | |
361 | } | |
362 | break; | |
363 | ||
364 | default: | |
365 | return wxChoice::MSWCommand(param, id); | |
366 | } | |
367 | ||
368 | // skip wxChoice version as it would generate its own events for | |
369 | // CBN_SELENDOK | |
370 | return wxControl::MSWCommand(param, id); | |
371 | } | |
372 | ||
373 | WXHWND 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 | ||
391 | // ---------------------------------------------------------------------------- | |
392 | // wxComboBox creation | |
393 | // ---------------------------------------------------------------------------- | |
394 | ||
395 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, | |
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) | |
403 | { | |
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 | |
407 | m_isShown = false; | |
408 | ||
409 | if ( !CreateAndInit(parent, id, pos, size, n, choices, style, | |
410 | validator, name) ) | |
411 | return false; | |
412 | ||
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() ) | |
417 | SetValue(value); | |
418 | ||
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 | { | |
424 | gs_wndprocEdit = wxSetWindowProc((HWND)GetEditHWND(), | |
425 | wxComboEditWndProc); | |
426 | } | |
427 | ||
428 | // and finally, show the control | |
429 | Show(true); | |
430 | ||
431 | return true; | |
432 | } | |
433 | ||
434 | bool 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 | ||
448 | WXDWORD 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 | ||
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 | ||
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 | ||
485 | wxString wxComboBox::GetValue() const | |
486 | { | |
487 | return wxGetWindowText(m_hWnd); | |
488 | } | |
489 | ||
490 | void wxComboBox::SetValue(const wxString& value) | |
491 | { | |
492 | if ( HasFlag(wxCB_READONLY) ) | |
493 | SetStringSelection(value); | |
494 | else | |
495 | SetWindowText(GetHwnd(), value.c_str()); | |
496 | } | |
497 | ||
498 | // Clipboard operations | |
499 | void wxComboBox::Copy() | |
500 | { | |
501 | SendMessage(GetHwnd(), WM_COPY, 0, 0L); | |
502 | } | |
503 | ||
504 | void wxComboBox::Cut() | |
505 | { | |
506 | SendMessage(GetHwnd(), WM_CUT, 0, 0L); | |
507 | } | |
508 | ||
509 | void wxComboBox::Paste() | |
510 | { | |
511 | SendMessage(GetHwnd(), WM_PASTE, 0, 0L); | |
512 | } | |
513 | ||
514 | void wxComboBox::Undo() | |
515 | { | |
516 | if (CanUndo()) | |
517 | { | |
518 | HWND hEditWnd = (HWND) GetEditHWND(); | |
519 | if ( hEditWnd ) | |
520 | ::SendMessage(hEditWnd, EM_UNDO, 0, 0); | |
521 | } | |
522 | } | |
523 | ||
524 | void wxComboBox::Redo() | |
525 | { | |
526 | if (CanUndo()) | |
527 | { | |
528 | // Same as Undo, since Undo undoes the undo, i.e. a redo. | |
529 | HWND hEditWnd = (HWND) GetEditHWND(); | |
530 | if ( hEditWnd ) | |
531 | ::SendMessage(hEditWnd, EM_UNDO, 0, 0); | |
532 | } | |
533 | } | |
534 | ||
535 | void wxComboBox::SelectAll() | |
536 | { | |
537 | SetSelection(0, GetLastPosition()); | |
538 | } | |
539 | ||
540 | bool wxComboBox::CanUndo() const | |
541 | { | |
542 | if (!IsEditable()) | |
543 | return false; | |
544 | ||
545 | HWND hEditWnd = (HWND) GetEditHWND(); | |
546 | if ( hEditWnd ) | |
547 | return ::SendMessage(hEditWnd, EM_CANUNDO, 0, 0) != 0; | |
548 | else | |
549 | return false; | |
550 | } | |
551 | ||
552 | bool wxComboBox::CanRedo() const | |
553 | { | |
554 | if (!IsEditable()) | |
555 | return false; | |
556 | ||
557 | HWND hEditWnd = (HWND) GetEditHWND(); | |
558 | if ( hEditWnd ) | |
559 | return ::SendMessage(hEditWnd, EM_CANUNDO, 0, 0) != 0; | |
560 | else | |
561 | return false; | |
562 | } | |
563 | ||
564 | bool wxComboBox::HasSelection() const | |
565 | { | |
566 | long from, to; | |
567 | GetSelection(&from, &to); | |
568 | return from != to; | |
569 | } | |
570 | ||
571 | bool wxComboBox::CanCopy() const | |
572 | { | |
573 | // Can copy if there's a selection | |
574 | return HasSelection(); | |
575 | } | |
576 | ||
577 | bool wxComboBox::CanCut() const | |
578 | { | |
579 | return IsEditable() && CanCopy(); | |
580 | } | |
581 | ||
582 | bool 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 | ||
597 | bool wxComboBox::IsEditable() const | |
598 | { | |
599 | return !HasFlag(wxCB_READONLY); | |
600 | } | |
601 | ||
602 | void wxComboBox::SetEditable(bool WXUNUSED(editable)) | |
603 | { | |
604 | // Can't implement in MSW? | |
605 | // HWND hWnd = GetHwnd(); | |
606 | // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L); | |
607 | } | |
608 | ||
609 | void wxComboBox::SetInsertionPoint(long pos) | |
610 | { | |
611 | if ( GetWindowStyle() & wxCB_READONLY ) | |
612 | return; | |
613 | ||
614 | #ifdef __WIN32__ | |
615 | HWND hWnd = GetHwnd(); | |
616 | ::SendMessage(hWnd, CB_SETEDITSEL, 0, MAKELPARAM(pos, pos)); | |
617 | HWND hEditWnd = (HWND) GetEditHWND(); | |
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) | |
623 | SendMessage(hEditWnd, EM_REPLACESEL, 0, (LPARAM) wxEmptyString); | |
624 | } | |
625 | #endif // __WIN32__ | |
626 | } | |
627 | ||
628 | void wxComboBox::SetInsertionPointEnd() | |
629 | { | |
630 | // setting insertion point doesn't make sense for read only comboboxes | |
631 | if ( !(GetWindowStyle() & wxCB_READONLY) ) | |
632 | { | |
633 | wxTextPos pos = GetLastPosition(); | |
634 | SetInsertionPoint(pos); | |
635 | } | |
636 | } | |
637 | ||
638 | long wxComboBox::GetInsertionPoint() const | |
639 | { | |
640 | // CB_GETEDITSEL returns the index of the first character of the selection in | |
641 | // its low-order word | |
642 | DWORD pos= (DWORD)::SendMessage(GetHwnd(), CB_GETEDITSEL, 0, 0L); | |
643 | return LOWORD(pos); | |
644 | } | |
645 | ||
646 | wxTextPos wxComboBox::GetLastPosition() const | |
647 | { | |
648 | HWND hEditWnd = (HWND) GetEditHWND(); | |
649 | ||
650 | // Get number of characters in the last (only) line. We'll add this to the character | |
651 | // index for the last line, 1st position. | |
652 | wxTextPos lineLength = (wxTextPos)SendMessage(hEditWnd, EM_LINELENGTH, (WPARAM) 0, (LPARAM)0L); | |
653 | ||
654 | return lineLength; | |
655 | } | |
656 | ||
657 | void wxComboBox::Replace(long from, long to, const wxString& value) | |
658 | { | |
659 | #if wxUSE_CLIPBOARD | |
660 | Remove(from, to); | |
661 | ||
662 | // Now replace with 'value', by pasting. | |
663 | wxSetClipboardData(wxDF_TEXT, (wxObject *)(const wxChar *)value, 0, 0); | |
664 | ||
665 | // Paste into edit control | |
666 | SendMessage(GetHwnd(), WM_PASTE, (WPARAM)0, (LPARAM)0L); | |
667 | #else | |
668 | wxUnusedVar(from); | |
669 | wxUnusedVar(to); | |
670 | wxUnusedVar(value); | |
671 | #endif | |
672 | } | |
673 | ||
674 | void wxComboBox::Remove(long from, long to) | |
675 | { | |
676 | // Set selection and remove it | |
677 | SetSelection(from, to); | |
678 | SendMessage(GetHwnd(), WM_CUT, (WPARAM)0, (LPARAM)0); | |
679 | } | |
680 | ||
681 | void wxComboBox::SetSelection(long from, long to) | |
682 | { | |
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) ) | |
686 | { | |
687 | from = 0; | |
688 | } | |
689 | ||
690 | if ( SendMessage(GetHwnd(), CB_SETEDITSEL, | |
691 | 0, (LPARAM)MAKELONG(from, to)) == CB_ERR ) | |
692 | { | |
693 | wxLogDebug(_T("CB_SETEDITSEL failed")); | |
694 | } | |
695 | } | |
696 | ||
697 | void wxComboBox::GetSelection(long* from, long* to) const | |
698 | { | |
699 | DWORD dwStart, dwEnd; | |
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 | } | |
711 | } | |
712 | ||
713 | // ---------------------------------------------------------------------------- | |
714 | // standard event handling | |
715 | // ---------------------------------------------------------------------------- | |
716 | ||
717 | void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event)) | |
718 | { | |
719 | Cut(); | |
720 | } | |
721 | ||
722 | void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
723 | { | |
724 | Copy(); | |
725 | } | |
726 | ||
727 | void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
728 | { | |
729 | Paste(); | |
730 | } | |
731 | ||
732 | void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
733 | { | |
734 | Undo(); | |
735 | } | |
736 | ||
737 | void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
738 | { | |
739 | Redo(); | |
740 | } | |
741 | ||
742 | void 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 | ||
750 | void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event)) | |
751 | { | |
752 | SetSelection(-1, -1); | |
753 | } | |
754 | ||
755 | void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event) | |
756 | { | |
757 | event.Enable( CanCut() ); | |
758 | } | |
759 | ||
760 | void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event) | |
761 | { | |
762 | event.Enable( CanCopy() ); | |
763 | } | |
764 | ||
765 | void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event) | |
766 | { | |
767 | event.Enable( CanPaste() ); | |
768 | } | |
769 | ||
770 | void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event) | |
771 | { | |
772 | event.Enable( CanUndo() ); | |
773 | } | |
774 | ||
775 | void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event) | |
776 | { | |
777 | event.Enable( CanRedo() ); | |
778 | } | |
779 | ||
780 | void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event) | |
781 | { | |
782 | event.Enable(HasSelection() && IsEditable()); | |
783 | } | |
784 | ||
785 | void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event) | |
786 | { | |
787 | event.Enable(GetLastPosition() > 0); | |
788 | } | |
789 | ||
790 | #endif // wxUSE_COMBOBOX |