]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: 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 and Markus Holzem | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #ifdef __GNUG__ | |
21 | #pragma implementation "combobox.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | #if wxUSE_COMBOBOX | |
32 | ||
33 | #ifndef WX_PRECOMP | |
34 | #include "wx/settings.h" | |
35 | #include "wx/log.h" | |
36 | // for wxEVT_COMMAND_TEXT_ENTER | |
37 | #include "wx/textctrl.h" | |
38 | #endif | |
39 | ||
40 | #include "wx/combobox.h" | |
41 | #include "wx/brush.h" | |
42 | #include "wx/clipbrd.h" | |
43 | #include "wx/msw/private.h" | |
44 | ||
45 | #if wxUSE_TOOLTIPS | |
46 | #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__) | |
47 | #include <commctrl.h> | |
48 | #endif | |
49 | #include "wx/tooltip.h" | |
50 | #endif // wxUSE_TOOLTIPS | |
51 | ||
52 | // ---------------------------------------------------------------------------- | |
53 | // wxWin macros | |
54 | // ---------------------------------------------------------------------------- | |
55 | ||
56 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl) | |
57 | ||
58 | // ---------------------------------------------------------------------------- | |
59 | // function prototypes | |
60 | // ---------------------------------------------------------------------------- | |
61 | ||
62 | LRESULT APIENTRY _EXPORT wxComboEditWndProc(HWND hWnd, | |
63 | UINT message, | |
64 | WPARAM wParam, | |
65 | LPARAM lParam); | |
66 | ||
67 | // --------------------------------------------------------------------------- | |
68 | // global vars | |
69 | // --------------------------------------------------------------------------- | |
70 | ||
71 | // the pointer to standard radio button wnd proc | |
72 | static WXFARPROC gs_wndprocEdit = (WXFARPROC)NULL; | |
73 | ||
74 | // ============================================================================ | |
75 | // implementation | |
76 | // ============================================================================ | |
77 | ||
78 | // ---------------------------------------------------------------------------- | |
79 | // wnd proc for subclassed edit control | |
80 | // ---------------------------------------------------------------------------- | |
81 | ||
82 | LRESULT APIENTRY _EXPORT wxComboEditWndProc(HWND hWnd, | |
83 | UINT message, | |
84 | WPARAM wParam, | |
85 | LPARAM lParam) | |
86 | { | |
87 | HWND hwndCombo = ::GetParent(hWnd); | |
88 | wxWindow *win = wxFindWinFromHandle((WXHWND)hwndCombo); | |
89 | ||
90 | switch ( message ) | |
91 | { | |
92 | // forward some messages to the combobox to generate the appropriate | |
93 | // wxEvents from them | |
94 | case WM_KEYUP: | |
95 | case WM_KEYDOWN: | |
96 | case WM_CHAR: | |
97 | case WM_SETFOCUS: | |
98 | case WM_KILLFOCUS: | |
99 | { | |
100 | wxComboBox *combo = wxDynamicCast(win, wxComboBox); | |
101 | if ( !combo ) | |
102 | { | |
103 | // we can get WM_KILLFOCUS while our parent is already half | |
104 | // destroyed and hence doesn't look like a combobx any | |
105 | // longer, check for it to avoid bogus assert failures | |
106 | if ( !win->IsBeingDeleted() ) | |
107 | { | |
108 | wxFAIL_MSG( _T("should have combo as parent") ); | |
109 | } | |
110 | } | |
111 | else if ( combo->MSWProcessEditMsg(message, wParam, lParam) ) | |
112 | { | |
113 | // handled by parent | |
114 | return 0; | |
115 | } | |
116 | } | |
117 | break; | |
118 | ||
119 | case WM_GETDLGCODE: | |
120 | { | |
121 | wxCHECK_MSG( win, 0, _T("should have a parent") ); | |
122 | ||
123 | if ( win->GetWindowStyle() & wxPROCESS_ENTER ) | |
124 | { | |
125 | // need to return a custom dlg code or we'll never get it | |
126 | return DLGC_WANTMESSAGE; | |
127 | } | |
128 | } | |
129 | break; | |
130 | ||
131 | // deal with tooltips here | |
132 | #if wxUSE_TOOLTIPS && defined(TTN_NEEDTEXT) | |
133 | case WM_NOTIFY: | |
134 | { | |
135 | wxCHECK_MSG( win, 0, _T("should have a parent") ); | |
136 | ||
137 | NMHDR* hdr = (NMHDR *)lParam; | |
138 | if ( hdr->code == TTN_NEEDTEXT ) | |
139 | { | |
140 | wxToolTip *tooltip = win->GetToolTip(); | |
141 | if ( tooltip ) | |
142 | { | |
143 | TOOLTIPTEXT *ttt = (TOOLTIPTEXT *)lParam; | |
144 | ttt->lpszText = (wxChar *)tooltip->GetTip().c_str(); | |
145 | } | |
146 | ||
147 | // processed | |
148 | return 0; | |
149 | } | |
150 | } | |
151 | break; | |
152 | #endif // wxUSE_TOOLTIPS | |
153 | } | |
154 | ||
155 | return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit, hWnd, message, wParam, lParam); | |
156 | } | |
157 | ||
158 | WXHBRUSH wxComboBox::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor), | |
159 | #if wxUSE_CTL3D | |
160 | WXUINT message, | |
161 | WXWPARAM wParam, | |
162 | WXLPARAM lParam | |
163 | #else | |
164 | WXUINT WXUNUSED(message), | |
165 | WXWPARAM WXUNUSED(wParam), | |
166 | WXLPARAM WXUNUSED(lParam) | |
167 | #endif | |
168 | ) | |
169 | { | |
170 | #if wxUSE_CTL3D | |
171 | if ( m_useCtl3D ) | |
172 | { | |
173 | HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam); | |
174 | return (WXHBRUSH) hbrush; | |
175 | } | |
176 | #endif // wxUSE_CTL3D | |
177 | ||
178 | HDC hdc = (HDC)pDC; | |
179 | if (GetParent()->GetTransparentBackground()) | |
180 | SetBkMode(hdc, TRANSPARENT); | |
181 | else | |
182 | SetBkMode(hdc, OPAQUE); | |
183 | ||
184 | wxColour colBack = GetBackgroundColour(); | |
185 | ||
186 | if (!IsEnabled()) | |
187 | colBack = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); | |
188 | ||
189 | ::SetBkColor(hdc, wxColourToRGB(colBack)); | |
190 | ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour())); | |
191 | ||
192 | wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID); | |
193 | ||
194 | return (WXHBRUSH)brush->GetResourceHandle(); | |
195 | } | |
196 | ||
197 | // ---------------------------------------------------------------------------- | |
198 | // wxComboBox | |
199 | // ---------------------------------------------------------------------------- | |
200 | ||
201 | bool wxComboBox::MSWProcessEditMsg(WXUINT msg, WXWPARAM wParam, WXLPARAM lParam) | |
202 | { | |
203 | switch ( msg ) | |
204 | { | |
205 | case WM_CHAR: | |
206 | // for compatibility with wxTextCtrl, generate a special message | |
207 | // when Enter is pressed | |
208 | if ( wParam == VK_RETURN ) | |
209 | { | |
210 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
211 | InitCommandEvent(event); | |
212 | event.SetString(GetValue()); | |
213 | event.SetInt(GetSelection()); | |
214 | ProcessCommand(event); | |
215 | } | |
216 | ||
217 | return HandleChar(wParam, lParam, TRUE /* isASCII */); | |
218 | ||
219 | case WM_KEYDOWN: | |
220 | return HandleKeyDown(wParam, lParam); | |
221 | ||
222 | case WM_KEYUP: | |
223 | return HandleKeyUp(wParam, lParam); | |
224 | ||
225 | case WM_SETFOCUS: | |
226 | return HandleSetFocus((WXHWND)wParam); | |
227 | ||
228 | case WM_KILLFOCUS: | |
229 | return HandleKillFocus((WXHWND)wParam); | |
230 | } | |
231 | ||
232 | return FALSE; | |
233 | } | |
234 | ||
235 | bool wxComboBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) | |
236 | { | |
237 | wxString value; | |
238 | int sel = -1; | |
239 | switch ( param ) | |
240 | { | |
241 | case CBN_SELCHANGE: | |
242 | sel = GetSelection(); | |
243 | if ( sel > -1 ) | |
244 | { | |
245 | value = GetString(sel); | |
246 | ||
247 | wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId()); | |
248 | event.SetInt(sel); | |
249 | event.SetEventObject(this); | |
250 | event.SetString(value); | |
251 | ProcessCommand(event); | |
252 | } | |
253 | else | |
254 | { | |
255 | break; | |
256 | } | |
257 | ||
258 | // fall through: for compability with wxGTK, also send the text | |
259 | // update event when the selection changes (this also seems more | |
260 | // logical as the text does change) | |
261 | ||
262 | case CBN_EDITCHANGE: | |
263 | { | |
264 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId()); | |
265 | ||
266 | // if sel != -1, value was initialized above (and we can't use | |
267 | // GetValue() here as it would return the old selection and we | |
268 | // want the new one) | |
269 | if ( sel == -1 ) | |
270 | { | |
271 | value = GetValue(); | |
272 | } | |
273 | else // we're synthesizing text updated event from sel change | |
274 | { | |
275 | // we need to do this because the user code expects | |
276 | // wxComboBox::GetValue() to return the new value from | |
277 | // "text updated" handler but it hadn't been updated yet | |
278 | SetValue(value); | |
279 | } | |
280 | ||
281 | event.SetString(value); | |
282 | event.SetEventObject(this); | |
283 | ProcessCommand(event); | |
284 | } | |
285 | break; | |
286 | } | |
287 | ||
288 | // there is no return value for the CBN_ notifications, so always return | |
289 | // FALSE from here to pass the message to DefWindowProc() | |
290 | return FALSE; | |
291 | } | |
292 | ||
293 | WXHWND wxComboBox::GetEditHWND() const | |
294 | { | |
295 | // this function should not be called for wxCB_READONLY controls, it is | |
296 | // the callers responsability to check this | |
297 | wxASSERT_MSG( !(GetWindowStyle() & wxCB_READONLY), | |
298 | _T("read-only combobox doesn't have any edit control") ); | |
299 | ||
300 | POINT pt; | |
301 | pt.x = pt.y = 4; | |
302 | HWND hwndEdit = ::ChildWindowFromPoint(GetHwnd(), pt); | |
303 | if ( !hwndEdit || hwndEdit == GetHwnd() ) | |
304 | { | |
305 | wxFAIL_MSG(_T("not read only combobox without edit control?")); | |
306 | } | |
307 | ||
308 | return (WXHWND)hwndEdit; | |
309 | } | |
310 | ||
311 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, | |
312 | const wxString& value, | |
313 | const wxPoint& pos, | |
314 | const wxSize& size, | |
315 | int n, const wxString choices[], | |
316 | long style, | |
317 | const wxValidator& validator, | |
318 | const wxString& name) | |
319 | { | |
320 | // pretend that wxComboBox is hidden while it is positioned and resized and | |
321 | // show it only right before leaving this method because otherwise there is | |
322 | // some noticeable flicker while the control rearranges itself | |
323 | m_isShown = FALSE; | |
324 | ||
325 | // first create wxWin object | |
326 | if ( !CreateControl(parent, id, pos, size, style, validator, name) ) | |
327 | return FALSE; | |
328 | ||
329 | // get the right style | |
330 | long msStyle = WS_TABSTOP | WS_VSCROLL | WS_HSCROLL | | |
331 | CBS_AUTOHSCROLL | CBS_NOINTEGRALHEIGHT /* | WS_CLIPSIBLINGS */; | |
332 | if ( style & wxCB_READONLY ) | |
333 | msStyle |= CBS_DROPDOWNLIST; | |
334 | else if ( style & wxCB_SIMPLE ) | |
335 | msStyle |= CBS_SIMPLE; // A list (shown always) and edit control | |
336 | else | |
337 | msStyle |= CBS_DROPDOWN; | |
338 | ||
339 | if ( style & wxCB_SORT ) | |
340 | msStyle |= CBS_SORT; | |
341 | ||
342 | if ( style & wxCLIP_SIBLINGS ) | |
343 | msStyle |= WS_CLIPSIBLINGS; | |
344 | ||
345 | ||
346 | // and now create the MSW control | |
347 | if ( !MSWCreateControl(_T("COMBOBOX"), msStyle) ) | |
348 | return FALSE; | |
349 | ||
350 | // A choice/combobox normally has a white background (or other, depending | |
351 | // on global settings) rather than inheriting the parent's background colour. | |
352 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); | |
353 | ||
354 | for ( int i = 0; i < n; i++ ) | |
355 | { | |
356 | Append(choices[i]); | |
357 | } | |
358 | ||
359 | if ( !value.IsEmpty() ) | |
360 | { | |
361 | SetValue(value); | |
362 | } | |
363 | ||
364 | // do this after appending the values to the combobox so that autosizing | |
365 | // works correctly | |
366 | SetSize(pos.x, pos.y, size.x, size.y); | |
367 | ||
368 | // a (not read only) combobox is, in fact, 2 controls: the combobox itself | |
369 | // and an edit control inside it and if we want to catch events from this | |
370 | // edit control, we must subclass it as well | |
371 | if ( !(style & wxCB_READONLY) ) | |
372 | { | |
373 | gs_wndprocEdit = (WXFARPROC)::SetWindowLong | |
374 | ( | |
375 | (HWND)GetEditHWND(), | |
376 | GWL_WNDPROC, | |
377 | (LPARAM)wxComboEditWndProc | |
378 | ); | |
379 | } | |
380 | ||
381 | // and finally, show the control | |
382 | Show(TRUE); | |
383 | ||
384 | return TRUE; | |
385 | } | |
386 | ||
387 | void wxComboBox::SetValue(const wxString& value) | |
388 | { | |
389 | if ( HasFlag(wxCB_READONLY) ) | |
390 | SetStringSelection(value); | |
391 | else | |
392 | SetWindowText(GetHwnd(), value.c_str()); | |
393 | } | |
394 | ||
395 | // Clipboard operations | |
396 | void wxComboBox::Copy() | |
397 | { | |
398 | SendMessage(GetHwnd(), WM_COPY, 0, 0L); | |
399 | } | |
400 | ||
401 | void wxComboBox::Cut() | |
402 | { | |
403 | SendMessage(GetHwnd(), WM_CUT, 0, 0L); | |
404 | } | |
405 | ||
406 | void wxComboBox::Paste() | |
407 | { | |
408 | SendMessage(GetHwnd(), WM_PASTE, 0, 0L); | |
409 | } | |
410 | ||
411 | void wxComboBox::SetEditable(bool WXUNUSED(editable)) | |
412 | { | |
413 | // Can't implement in MSW? | |
414 | // HWND hWnd = GetHwnd(); | |
415 | // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L); | |
416 | } | |
417 | ||
418 | void wxComboBox::SetInsertionPoint(long pos) | |
419 | { | |
420 | if ( GetWindowStyle() & wxCB_READONLY ) | |
421 | return; | |
422 | ||
423 | #ifdef __WIN32__ | |
424 | HWND hWnd = GetHwnd(); | |
425 | ::SendMessage(hWnd, CB_SETEDITSEL, 0, MAKELPARAM(pos, pos)); | |
426 | HWND hEditWnd = (HWND) GetEditHWND() ; | |
427 | if ( hEditWnd ) | |
428 | { | |
429 | // Scroll insertion point into view | |
430 | SendMessage(hEditWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0); | |
431 | // Why is this necessary? (Copied from wxTextCtrl::SetInsertionPoint) | |
432 | SendMessage(hEditWnd, EM_REPLACESEL, 0, (LPARAM)_T("")); | |
433 | } | |
434 | #endif // __WIN32__ | |
435 | } | |
436 | ||
437 | void wxComboBox::SetInsertionPointEnd() | |
438 | { | |
439 | // setting insertion point doesn't make sense for read only comboboxes | |
440 | if ( !(GetWindowStyle() & wxCB_READONLY) ) | |
441 | { | |
442 | long pos = GetLastPosition(); | |
443 | SetInsertionPoint(pos); | |
444 | } | |
445 | } | |
446 | ||
447 | long wxComboBox::GetInsertionPoint() const | |
448 | { | |
449 | #ifdef __WIN32__ | |
450 | DWORD Pos=(DWORD)SendMessage(GetHwnd(), CB_GETEDITSEL, 0, 0L); | |
451 | return Pos&0xFFFF; | |
452 | #else | |
453 | return 0; | |
454 | #endif | |
455 | } | |
456 | ||
457 | long wxComboBox::GetLastPosition() const | |
458 | { | |
459 | HWND hEditWnd = (HWND) GetEditHWND(); | |
460 | ||
461 | // Get number of characters in the last (only) line. We'll add this to the character | |
462 | // index for the last line, 1st position. | |
463 | int lineLength = (int)SendMessage(hEditWnd, EM_LINELENGTH, (WPARAM) 0, (LPARAM)0L); | |
464 | ||
465 | return (long)(lineLength); | |
466 | } | |
467 | ||
468 | void wxComboBox::Replace(long from, long to, const wxString& value) | |
469 | { | |
470 | #if wxUSE_CLIPBOARD | |
471 | Remove(from, to); | |
472 | ||
473 | // Now replace with 'value', by pasting. | |
474 | wxSetClipboardData(wxDF_TEXT, (wxObject *)(const wxChar *)value, 0, 0); | |
475 | ||
476 | // Paste into edit control | |
477 | SendMessage(GetHwnd(), WM_PASTE, (WPARAM)0, (LPARAM)0L); | |
478 | #endif | |
479 | } | |
480 | ||
481 | void wxComboBox::Remove(long from, long to) | |
482 | { | |
483 | // Set selection and remove it | |
484 | SetSelection(from, to); | |
485 | SendMessage(GetHwnd(), WM_CUT, (WPARAM)0, (LPARAM)0); | |
486 | } | |
487 | ||
488 | void wxComboBox::SetSelection(long from, long to) | |
489 | { | |
490 | HWND hWnd = GetHwnd(); | |
491 | long fromChar = from; | |
492 | long toChar = to; | |
493 | // if from and to are both -1, it means | |
494 | // (in wxWindows) that all text should be selected. | |
495 | // This translates into Windows convention | |
496 | if ((from == -1) && (to == -1)) | |
497 | { | |
498 | fromChar = 0; | |
499 | toChar = -1; | |
500 | } | |
501 | ||
502 | if ( | |
503 | #ifdef __WIN32__ | |
504 | SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar)) | |
505 | #else // Win16 | |
506 | SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)fromChar, (LPARAM)toChar) | |
507 | #endif | |
508 | == CB_ERR ) | |
509 | { | |
510 | wxLogDebug(_T("CB_SETEDITSEL failed")); | |
511 | } | |
512 | } | |
513 | ||
514 | #endif | |
515 | // wxUSE_COMBOBOX | |
516 |