]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
f6bcfd97 | 2 | // Name: 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$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
c085e333 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
f6bcfd97 BP |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
2bda0e17 KB |
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__ | |
fddfd9e1 | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
af79c52d VZ |
31 | #if wxUSE_COMBOBOX |
32 | ||
2bda0e17 | 33 | #ifndef WX_PRECOMP |
fddfd9e1 VZ |
34 | #include "wx/settings.h" |
35 | #include "wx/log.h" | |
2bda0e17 KB |
36 | #endif |
37 | ||
2bda0e17 | 38 | #include "wx/combobox.h" |
f6bcfd97 | 39 | #include "wx/brush.h" |
2bda0e17 KB |
40 | #include "wx/clipbrd.h" |
41 | #include "wx/msw/private.h" | |
42 | ||
f6bcfd97 | 43 | #if wxUSE_TOOLTIPS |
ae090fdb | 44 | #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__) |
f6bcfd97 BP |
45 | #include <commctrl.h> |
46 | #endif | |
47 | #include "wx/tooltip.h" | |
48 | #endif // wxUSE_TOOLTIPS | |
49 | ||
50 | // ---------------------------------------------------------------------------- | |
51 | // wxWin macros | |
52 | // ---------------------------------------------------------------------------- | |
53 | ||
2bda0e17 | 54 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl) |
2bda0e17 | 55 | |
f6bcfd97 BP |
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 | |
ae090fdb | 118 | #if wxUSE_TOOLTIPS && defined(TTN_NEEDTEXT) |
f6bcfd97 BP |
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 | ||
33ac7e6f | 144 | WXHBRUSH wxComboBox::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor), |
788722ac JS |
145 | #if wxUSE_CTL3D |
146 | WXUINT message, | |
147 | WXWPARAM wParam, | |
148 | WXLPARAM lParam | |
149 | #else | |
33ac7e6f KB |
150 | WXUINT WXUNUSED(message), |
151 | WXWPARAM WXUNUSED(wParam), | |
788722ac JS |
152 | WXLPARAM WXUNUSED(lParam) |
153 | #endif | |
154 | ) | |
f6bcfd97 BP |
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 */); | |
889f0b7c | 193 | |
f6bcfd97 BP |
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 | ||
debe6624 | 204 | bool wxComboBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) |
2bda0e17 | 205 | { |
fddfd9e1 VZ |
206 | wxString value; |
207 | int sel = -1; | |
cd0b1709 | 208 | switch ( param ) |
8c1c5302 | 209 | { |
cd0b1709 | 210 | case CBN_SELCHANGE: |
fddfd9e1 VZ |
211 | sel = GetSelection(); |
212 | if ( sel > -1 ) | |
cd0b1709 | 213 | { |
fddfd9e1 VZ |
214 | value = GetString(sel); |
215 | ||
cd0b1709 | 216 | wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId()); |
fddfd9e1 | 217 | event.SetInt(sel); |
cd0b1709 | 218 | event.SetEventObject(this); |
fddfd9e1 | 219 | event.SetString(value); |
cd0b1709 VZ |
220 | ProcessCommand(event); |
221 | } | |
fddfd9e1 VZ |
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) | |
29006414 | 230 | |
cd0b1709 VZ |
231 | case CBN_EDITCHANGE: |
232 | { | |
fddfd9e1 VZ |
233 | // if sel != -1, value was initialized above (and we can't use |
234 | // GetValue() here as it would return the old selection and we | |
235 | // want the new one) | |
cd0b1709 | 236 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId()); |
fddfd9e1 | 237 | event.SetString(sel == -1 ? GetValue() : value); |
cd0b1709 VZ |
238 | event.SetEventObject(this); |
239 | ProcessCommand(event); | |
240 | } | |
241 | break; | |
242 | } | |
29006414 | 243 | |
cd0b1709 VZ |
244 | // there is no return value for the CBN_ notifications, so always return |
245 | // FALSE from here to pass the message to DefWindowProc() | |
246 | return FALSE; | |
2bda0e17 KB |
247 | } |
248 | ||
f6bcfd97 BP |
249 | WXHWND wxComboBox::GetEditHWND() const |
250 | { | |
251 | // this function should not be called for wxCB_READONLY controls, it is | |
252 | // the callers responsability to check this | |
253 | wxASSERT_MSG( !(GetWindowStyle() & wxCB_READONLY), | |
254 | _T("read-only combobox doesn't have any edit control") ); | |
255 | ||
256 | POINT pt; | |
257 | pt.x = pt.y = 4; | |
258 | HWND hwndEdit = ::ChildWindowFromPoint(GetHwnd(), pt); | |
259 | if ( !hwndEdit || hwndEdit == GetHwnd() ) | |
260 | { | |
261 | wxFAIL_MSG(_T("not read only combobox without edit control?")); | |
262 | } | |
263 | ||
264 | return (WXHWND)hwndEdit; | |
265 | } | |
266 | ||
debe6624 | 267 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, |
c085e333 VZ |
268 | const wxString& value, |
269 | const wxPoint& pos, | |
270 | const wxSize& size, | |
271 | int n, const wxString choices[], | |
272 | long style, | |
273 | const wxValidator& validator, | |
274 | const wxString& name) | |
2bda0e17 | 275 | { |
f6bcfd97 BP |
276 | // first create wxWin object |
277 | if ( !CreateControl(parent, id, pos, size, style, validator, name) ) | |
278 | return FALSE; | |
279 | ||
280 | // get the right style | |
281 | long msStyle = WS_TABSTOP | WS_VSCROLL | WS_HSCROLL | | |
282 | CBS_AUTOHSCROLL | CBS_NOINTEGRALHEIGHT /* | WS_CLIPSIBLINGS */; | |
283 | if ( style & wxCB_READONLY ) | |
284 | msStyle |= CBS_DROPDOWNLIST; | |
285 | else if ( style & wxCB_SIMPLE ) | |
286 | msStyle |= CBS_SIMPLE; // A list (shown always) and edit control | |
287 | else | |
288 | msStyle |= CBS_DROPDOWN; | |
289 | ||
290 | if ( style & wxCB_SORT ) | |
291 | msStyle |= CBS_SORT; | |
292 | ||
b0766406 JS |
293 | if ( style & wxCLIP_SIBLINGS ) |
294 | msStyle |= WS_CLIPSIBLINGS; | |
295 | ||
296 | ||
f6bcfd97 BP |
297 | // and now create the MSW control |
298 | if ( !MSWCreateControl(_T("COMBOBOX"), msStyle) ) | |
299 | return FALSE; | |
300 | ||
301 | SetSize(pos.x, pos.y, size.x, size.y); | |
302 | ||
303 | // A choice/combobox normally has a white background (or other, depending | |
304 | // on global settings) rather than inheriting the parent's background colour. | |
305 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW)); | |
306 | ||
307 | for ( int i = 0; i < n; i++ ) | |
308 | { | |
309 | Append(choices[i]); | |
310 | } | |
c085e333 | 311 | |
f6bcfd97 BP |
312 | if ( !value.IsEmpty() ) |
313 | { | |
314 | SetValue(value); | |
315 | } | |
2bda0e17 | 316 | |
f6bcfd97 BP |
317 | // a (not read only) combobox is, in fact, 2 controls: the combobox itself |
318 | // and an edit control inside it and if we want to catch events from this | |
319 | // edit control, we must subclass it as well | |
320 | if ( !(style & wxCB_READONLY) ) | |
321 | { | |
322 | gs_wndprocEdit = (WXFARPROC)::SetWindowLong | |
323 | ( | |
324 | (HWND)GetEditHWND(), | |
325 | GWL_WNDPROC, | |
326 | (LPARAM)wxComboEditWndProc | |
327 | ); | |
328 | } | |
2bda0e17 | 329 | |
f6bcfd97 | 330 | return TRUE; |
2bda0e17 KB |
331 | } |
332 | ||
f6bcfd97 BP |
333 | // TODO: update and clear all this horrible mess (VZ) |
334 | ||
2bda0e17 KB |
335 | void wxComboBox::SetValue(const wxString& value) |
336 | { | |
337 | // If newlines are denoted by just 10, must stick 13 in front. | |
338 | int singletons = 0; | |
339 | int len = value.Length(); | |
340 | int i; | |
341 | for (i = 0; i < len; i ++) | |
342 | { | |
343 | if ((i > 0) && (value[i] == 10) && (value[i-1] != 13)) | |
344 | singletons ++; | |
345 | } | |
346 | if (singletons > 0) | |
347 | { | |
837e5743 | 348 | wxChar *tmp = new wxChar[len + singletons + 1]; |
2bda0e17 KB |
349 | int j = 0; |
350 | for (i = 0; i < len; i ++) | |
351 | { | |
352 | if ((i > 0) && (value[i] == 10) && (value[i-1] != 13)) | |
353 | { | |
354 | tmp[j] = 13; | |
355 | j ++; | |
356 | } | |
357 | tmp[j] = value[i]; | |
358 | j ++; | |
359 | } | |
360 | tmp[j] = 0; | |
4438caf4 | 361 | SetWindowText(GetHwnd(), tmp); |
2bda0e17 KB |
362 | delete[] tmp; |
363 | } | |
364 | else | |
4438caf4 | 365 | SetWindowText(GetHwnd(), value); |
2bda0e17 KB |
366 | } |
367 | ||
368 | // Clipboard operations | |
1c4a764c | 369 | void wxComboBox::Copy() |
2bda0e17 | 370 | { |
4438caf4 | 371 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
372 | SendMessage(hWnd, WM_COPY, 0, 0L); |
373 | } | |
374 | ||
1c4a764c | 375 | void wxComboBox::Cut() |
2bda0e17 | 376 | { |
4438caf4 | 377 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
378 | SendMessage(hWnd, WM_CUT, 0, 0L); |
379 | } | |
380 | ||
1c4a764c | 381 | void wxComboBox::Paste() |
2bda0e17 | 382 | { |
4438caf4 | 383 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
384 | SendMessage(hWnd, WM_PASTE, 0, 0L); |
385 | } | |
386 | ||
33ac7e6f | 387 | void wxComboBox::SetEditable(bool WXUNUSED(editable)) |
2bda0e17 KB |
388 | { |
389 | // Can't implement in MSW? | |
4438caf4 | 390 | // HWND hWnd = GetHwnd(); |
2bda0e17 KB |
391 | // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L); |
392 | } | |
393 | ||
debe6624 | 394 | void wxComboBox::SetInsertionPoint(long pos) |
2bda0e17 | 395 | { |
6d14cac7 VZ |
396 | if ( GetWindowStyle() & wxCB_READONLY ) |
397 | return; | |
398 | ||
2bda0e17 | 399 | #ifdef __WIN32__ |
6d14cac7 VZ |
400 | HWND hWnd = GetHwnd(); |
401 | ::SendMessage(hWnd, CB_SETEDITSEL, 0, MAKELPARAM(pos, pos)); | |
402 | HWND hEditWnd = (HWND) GetEditHWND() ; | |
403 | if ( hEditWnd ) | |
404 | { | |
405 | // Scroll insertion point into view | |
406 | SendMessage(hEditWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0); | |
407 | // Why is this necessary? (Copied from wxTextCtrl::SetInsertionPoint) | |
408 | SendMessage(hEditWnd, EM_REPLACESEL, 0, (LPARAM)_T("")); | |
409 | } | |
410 | #endif // __WIN32__ | |
2bda0e17 KB |
411 | } |
412 | ||
1c4a764c | 413 | void wxComboBox::SetInsertionPointEnd() |
2bda0e17 | 414 | { |
6d14cac7 VZ |
415 | // setting insertion point doesn't make sense for read only comboboxes |
416 | if ( !(GetWindowStyle() & wxCB_READONLY) ) | |
417 | { | |
418 | long pos = GetLastPosition(); | |
419 | SetInsertionPoint(pos); | |
420 | } | |
2bda0e17 KB |
421 | } |
422 | ||
1c4a764c | 423 | long wxComboBox::GetInsertionPoint() const |
2bda0e17 | 424 | { |
f7703477 JS |
425 | #ifdef __WIN32__ |
426 | DWORD Pos=(DWORD)SendMessage(GetHwnd(), CB_GETEDITSEL, 0, 0L); | |
427 | return Pos&0xFFFF; | |
428 | #else | |
429 | return 0; | |
430 | #endif | |
2bda0e17 KB |
431 | } |
432 | ||
1c4a764c | 433 | long wxComboBox::GetLastPosition() const |
2bda0e17 | 434 | { |
f7703477 | 435 | HWND hEditWnd = (HWND) GetEditHWND(); |
4438caf4 | 436 | |
f7703477 | 437 | // Get number of characters in the last (only) line. We'll add this to the character |
2bda0e17 | 438 | // index for the last line, 1st position. |
f7703477 | 439 | int lineLength = (int)SendMessage(hEditWnd, EM_LINELENGTH, (WPARAM) 0, (LPARAM)0L); |
2bda0e17 | 440 | |
f7703477 | 441 | return (long)(lineLength); |
2bda0e17 KB |
442 | } |
443 | ||
debe6624 | 444 | void wxComboBox::Replace(long from, long to, const wxString& value) |
2bda0e17 | 445 | { |
47d67540 | 446 | #if wxUSE_CLIPBOARD |
fddfd9e1 | 447 | Remove(from, to); |
2bda0e17 KB |
448 | |
449 | // Now replace with 'value', by pasting. | |
837e5743 | 450 | wxSetClipboardData(wxDF_TEXT, (wxObject *)(const wxChar *)value, 0, 0); |
2bda0e17 KB |
451 | |
452 | // Paste into edit control | |
fddfd9e1 | 453 | SendMessage(GetHwnd(), WM_PASTE, (WPARAM)0, (LPARAM)0L); |
2bda0e17 KB |
454 | #endif |
455 | } | |
456 | ||
debe6624 | 457 | void wxComboBox::Remove(long from, long to) |
2bda0e17 | 458 | { |
fddfd9e1 VZ |
459 | // Set selection and remove it |
460 | SetSelection(from, to); | |
461 | SendMessage(GetHwnd(), WM_CUT, (WPARAM)0, (LPARAM)0); | |
2bda0e17 KB |
462 | } |
463 | ||
debe6624 | 464 | void wxComboBox::SetSelection(long from, long to) |
2bda0e17 | 465 | { |
4438caf4 | 466 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
467 | long fromChar = from; |
468 | long toChar = to; | |
469 | // if from and to are both -1, it means | |
470 | // (in wxWindows) that all text should be selected. | |
471 | // This translates into Windows convention | |
472 | if ((from == -1) && (to == -1)) | |
473 | { | |
474 | fromChar = 0; | |
475 | toChar = -1; | |
476 | } | |
4438caf4 | 477 | |
33ac7e6f | 478 | if ( |
2bda0e17 | 479 | #ifdef __WIN32__ |
fddfd9e1 VZ |
480 | SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar)) |
481 | #else // Win16 | |
482 | SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)fromChar, (LPARAM)toChar) | |
2bda0e17 | 483 | #endif |
fddfd9e1 VZ |
484 | == CB_ERR ) |
485 | { | |
486 | wxLogDebug(_T("CB_SETEDITSEL failed")); | |
487 | } | |
2bda0e17 KB |
488 | } |
489 | ||
490 | #endif | |
47d67540 | 491 | // wxUSE_COMBOBOX |
2bda0e17 | 492 |