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