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