]> git.saurik.com Git - wxWidgets.git/blob - src/msw/combobox.cpp
old mingw32 compilation fix
[wxWidgets.git] / src / msw / combobox.cpp
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 to generate the appropriate
91 // wxEvents from them
92 case WM_KEYUP:
93 case WM_KEYDOWN:
94 case WM_CHAR:
95 case WM_SETFOCUS:
96 case WM_KILLFOCUS:
97 {
98 wxComboBox *combo = wxDynamicCast(win, wxComboBox);
99 if ( !combo )
100 {
101 // we can get WM_KILLFOCUS while our parent is already half
102 // destroyed and hence doesn't look like a combobx any
103 // longer, check for it to avoid bogus assert failures
104 if ( !win->IsBeingDeleted() )
105 {
106 wxFAIL_MSG( _T("should have combo as parent") );
107 }
108 }
109 else if ( combo->MSWProcessEditMsg(message, wParam, lParam) )
110 {
111 // handled by parent
112 return 0;
113 }
114 }
115 break;
116
117 #if 0
118 case WM_GETDLGCODE:
119 {
120 wxCHECK_MSG( win, 0, _T("should have a parent") );
121
122 if ( win->GetWindowStyle() & wxPROCESS_ENTER )
123 {
124 // need to return a custom dlg code or we'll never get it
125 return DLGC_WANTMESSAGE;
126 }
127 }
128 break;
129 #endif // 0
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 ( (int)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 return HandleChar(wParam, lParam, TRUE /* isASCII */);
207
208 case WM_KEYDOWN:
209 return HandleKeyDown(wParam, lParam);
210
211 case WM_KEYUP:
212 return HandleKeyUp(wParam, lParam);
213
214 case WM_SETFOCUS:
215 return HandleSetFocus((WXHWND)wParam);
216
217 case WM_KILLFOCUS:
218 return HandleKillFocus((WXHWND)wParam);
219 }
220
221 return FALSE;
222 }
223
224 bool wxComboBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
225 {
226 wxString value;
227 int sel = -1;
228 switch ( param )
229 {
230 case CBN_SELCHANGE:
231 sel = GetSelection();
232 if ( sel > -1 )
233 {
234 value = GetString(sel);
235
236 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId());
237 event.SetInt(sel);
238 event.SetEventObject(this);
239 event.SetString(value);
240 ProcessCommand(event);
241 }
242 else
243 {
244 break;
245 }
246
247 // fall through: for compability with wxGTK, also send the text
248 // update event when the selection changes (this also seems more
249 // logical as the text does change)
250
251 case CBN_EDITCHANGE:
252 {
253 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
254
255 // if sel != -1, value was initialized above (and we can't use
256 // GetValue() here as it would return the old selection and we
257 // want the new one)
258 if ( sel == -1 )
259 {
260 value = GetValue();
261 }
262 else // we're synthesizing text updated event from sel change
263 {
264 // we need to do this because the user code expects
265 // wxComboBox::GetValue() to return the new value from
266 // "text updated" handler but it hadn't been updated yet
267 SetValue(value);
268 }
269
270 event.SetString(value);
271 event.SetEventObject(this);
272 return ProcessCommand(event);
273 }
274 break;
275 }
276
277 // I don't think the following should be true. The return value is
278 // whether the event has been handled, not the status of the handling.
279 // So, we only return false if the switch fell through. This will
280 // resolve the same event being sent multiple times by MS Windows.
281 // mea 05-22-01
282
283 // there is no return value for the CBN_ notifications, so always return
284 // FALSE from here to pass the message to DefWindowProc()
285 return FALSE;
286 }
287
288 WXHWND wxComboBox::GetEditHWND() const
289 {
290 // this function should not be called for wxCB_READONLY controls, it is
291 // the callers responsability to check this
292 wxASSERT_MSG( !(GetWindowStyle() & wxCB_READONLY),
293 _T("read-only combobox doesn't have any edit control") );
294
295 POINT pt;
296 pt.x = pt.y = 4;
297 HWND hwndEdit = ::ChildWindowFromPoint(GetHwnd(), pt);
298 if ( !hwndEdit || hwndEdit == GetHwnd() )
299 {
300 wxFAIL_MSG(_T("not read only combobox without edit control?"));
301 }
302
303 return (WXHWND)hwndEdit;
304 }
305
306 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
307 const wxString& value,
308 const wxPoint& pos,
309 const wxSize& size,
310 int n, const wxString choices[],
311 long style,
312 const wxValidator& validator,
313 const wxString& name)
314 {
315 // first create wxWin object
316 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
317 return FALSE;
318
319 // get the right style
320 long msStyle = WS_TABSTOP | WS_VSCROLL | WS_HSCROLL |
321 CBS_AUTOHSCROLL | CBS_NOINTEGRALHEIGHT /* | WS_CLIPSIBLINGS */;
322 if ( style & wxCB_READONLY )
323 msStyle |= CBS_DROPDOWNLIST;
324 else if ( style & wxCB_SIMPLE )
325 msStyle |= CBS_SIMPLE; // A list (shown always) and edit control
326 else
327 msStyle |= CBS_DROPDOWN;
328
329 if ( style & wxCB_SORT )
330 msStyle |= CBS_SORT;
331
332 if ( style & wxCLIP_SIBLINGS )
333 msStyle |= WS_CLIPSIBLINGS;
334
335
336 // and now create the MSW control
337 if ( !MSWCreateControl(_T("COMBOBOX"), msStyle) )
338 return FALSE;
339
340 // A choice/combobox normally has a white background (or other, depending
341 // on global settings) rather than inheriting the parent's background colour.
342 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
343
344 for ( int i = 0; i < n; i++ )
345 {
346 Append(choices[i]);
347 }
348
349 if ( !value.IsEmpty() )
350 {
351 SetValue(value);
352 }
353
354 // do this after appending the values to the combobox so that autosizing
355 // works correctly
356 SetSize(pos.x, pos.y, size.x, size.y);
357
358 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
359 // and an edit control inside it and if we want to catch events from this
360 // edit control, we must subclass it as well
361 if ( !(style & wxCB_READONLY) )
362 {
363 gs_wndprocEdit = (WXFARPROC)::SetWindowLong
364 (
365 (HWND)GetEditHWND(),
366 GWL_WNDPROC,
367 (LPARAM)wxComboEditWndProc
368 );
369 }
370
371 return TRUE;
372 }
373
374 // TODO: update and clear all this horrible mess (VZ)
375
376 void wxComboBox::SetValue(const wxString& value)
377 {
378 // If newlines are denoted by just 10, must stick 13 in front.
379 int singletons = 0;
380 int len = value.Length();
381 int i;
382 for (i = 0; i < len; i ++)
383 {
384 if ((i > 0) && (value[i] == 10) && (value[i-1] != 13))
385 singletons ++;
386 }
387 if (singletons > 0)
388 {
389 wxChar *tmp = new wxChar[len + singletons + 1];
390 int j = 0;
391 for (i = 0; i < len; i ++)
392 {
393 if ((i > 0) && (value[i] == 10) && (value[i-1] != 13))
394 {
395 tmp[j] = 13;
396 j ++;
397 }
398 tmp[j] = value[i];
399 j ++;
400 }
401 tmp[j] = 0;
402 SetWindowText(GetHwnd(), tmp);
403 delete[] tmp;
404 }
405 else
406 SetWindowText(GetHwnd(), value);
407 }
408
409 // Clipboard operations
410 void wxComboBox::Copy()
411 {
412 HWND hWnd = GetHwnd();
413 SendMessage(hWnd, WM_COPY, 0, 0L);
414 }
415
416 void wxComboBox::Cut()
417 {
418 HWND hWnd = GetHwnd();
419 SendMessage(hWnd, WM_CUT, 0, 0L);
420 }
421
422 void wxComboBox::Paste()
423 {
424 HWND hWnd = GetHwnd();
425 SendMessage(hWnd, WM_PASTE, 0, 0L);
426 }
427
428 void wxComboBox::SetEditable(bool WXUNUSED(editable))
429 {
430 // Can't implement in MSW?
431 // HWND hWnd = GetHwnd();
432 // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
433 }
434
435 void wxComboBox::SetInsertionPoint(long pos)
436 {
437 if ( GetWindowStyle() & wxCB_READONLY )
438 return;
439
440 #ifdef __WIN32__
441 HWND hWnd = GetHwnd();
442 ::SendMessage(hWnd, CB_SETEDITSEL, 0, MAKELPARAM(pos, pos));
443 HWND hEditWnd = (HWND) GetEditHWND() ;
444 if ( hEditWnd )
445 {
446 // Scroll insertion point into view
447 SendMessage(hEditWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
448 // Why is this necessary? (Copied from wxTextCtrl::SetInsertionPoint)
449 SendMessage(hEditWnd, EM_REPLACESEL, 0, (LPARAM)_T(""));
450 }
451 #endif // __WIN32__
452 }
453
454 void wxComboBox::SetInsertionPointEnd()
455 {
456 // setting insertion point doesn't make sense for read only comboboxes
457 if ( !(GetWindowStyle() & wxCB_READONLY) )
458 {
459 long pos = GetLastPosition();
460 SetInsertionPoint(pos);
461 }
462 }
463
464 long wxComboBox::GetInsertionPoint() const
465 {
466 #ifdef __WIN32__
467 DWORD Pos=(DWORD)SendMessage(GetHwnd(), CB_GETEDITSEL, 0, 0L);
468 return Pos&0xFFFF;
469 #else
470 return 0;
471 #endif
472 }
473
474 long wxComboBox::GetLastPosition() const
475 {
476 HWND hEditWnd = (HWND) GetEditHWND();
477
478 // Get number of characters in the last (only) line. We'll add this to the character
479 // index for the last line, 1st position.
480 int lineLength = (int)SendMessage(hEditWnd, EM_LINELENGTH, (WPARAM) 0, (LPARAM)0L);
481
482 return (long)(lineLength);
483 }
484
485 void wxComboBox::Replace(long from, long to, const wxString& value)
486 {
487 #if wxUSE_CLIPBOARD
488 Remove(from, to);
489
490 // Now replace with 'value', by pasting.
491 wxSetClipboardData(wxDF_TEXT, (wxObject *)(const wxChar *)value, 0, 0);
492
493 // Paste into edit control
494 SendMessage(GetHwnd(), WM_PASTE, (WPARAM)0, (LPARAM)0L);
495 #endif
496 }
497
498 void wxComboBox::Remove(long from, long to)
499 {
500 // Set selection and remove it
501 SetSelection(from, to);
502 SendMessage(GetHwnd(), WM_CUT, (WPARAM)0, (LPARAM)0);
503 }
504
505 void wxComboBox::SetSelection(long from, long to)
506 {
507 HWND hWnd = GetHwnd();
508 long fromChar = from;
509 long toChar = to;
510 // if from and to are both -1, it means
511 // (in wxWindows) that all text should be selected.
512 // This translates into Windows convention
513 if ((from == -1) && (to == -1))
514 {
515 fromChar = 0;
516 toChar = -1;
517 }
518
519 if (
520 #ifdef __WIN32__
521 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar))
522 #else // Win16
523 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)fromChar, (LPARAM)toChar)
524 #endif
525 == CB_ERR )
526 {
527 wxLogDebug(_T("CB_SETEDITSEL failed"));
528 }
529 }
530
531 #endif
532 // wxUSE_COMBOBOX
533