]> git.saurik.com Git - wxWidgets.git/blob - src/msw/combobox.cpp
Updates to fix Watcom C/C++ 11.0 compiler warning problems. Now compiles
[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 #ifndef __GNUWIN32_OLD__
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
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 WXUINT WXUNUSED(message),
146 WXWPARAM WXUNUSED(wParam),
147 WXLPARAM WXUNUSED(lParam))
148 {
149 #if wxUSE_CTL3D
150 if ( m_useCtl3D )
151 {
152 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
153 return (WXHBRUSH) hbrush;
154 }
155 #endif // wxUSE_CTL3D
156
157 HDC hdc = (HDC)pDC;
158 if (GetParent()->GetTransparentBackground())
159 SetBkMode(hdc, TRANSPARENT);
160 else
161 SetBkMode(hdc, OPAQUE);
162
163 wxColour colBack = GetBackgroundColour();
164
165 if (!IsEnabled())
166 colBack = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
167
168 ::SetBkColor(hdc, wxColourToRGB(colBack));
169 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
170
171 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
172
173 return (WXHBRUSH)brush->GetResourceHandle();
174 }
175
176 // ----------------------------------------------------------------------------
177 // wxComboBox
178 // ----------------------------------------------------------------------------
179
180 bool wxComboBox::MSWProcessEditMsg(WXUINT msg, WXWPARAM wParam, WXLPARAM lParam)
181 {
182 switch ( msg )
183 {
184 case WM_CHAR:
185 return HandleChar(wParam, lParam, TRUE /* isASCII */);
186
187 case WM_KEYDOWN:
188 return HandleKeyDown(wParam, lParam);
189
190 case WM_KEYUP:
191 return HandleKeyUp(wParam, lParam);
192 }
193
194 return FALSE;
195 }
196
197 bool wxComboBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
198 {
199 wxString value;
200 int sel = -1;
201 switch ( param )
202 {
203 case CBN_SELCHANGE:
204 sel = GetSelection();
205 if ( sel > -1 )
206 {
207 value = GetString(sel);
208
209 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId());
210 event.SetInt(sel);
211 event.SetEventObject(this);
212 event.SetString(value);
213 ProcessCommand(event);
214 }
215 else
216 {
217 break;
218 }
219
220 // fall through: for compability with wxGTK, also send the text
221 // update event when the selection changes (this also seems more
222 // logical as the text does change)
223
224 case CBN_EDITCHANGE:
225 {
226 // if sel != -1, value was initialized above (and we can't use
227 // GetValue() here as it would return the old selection and we
228 // want the new one)
229 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
230 event.SetString(sel == -1 ? GetValue() : value);
231 event.SetEventObject(this);
232 ProcessCommand(event);
233 }
234 break;
235 }
236
237 // there is no return value for the CBN_ notifications, so always return
238 // FALSE from here to pass the message to DefWindowProc()
239 return FALSE;
240 }
241
242 WXHWND wxComboBox::GetEditHWND() const
243 {
244 // this function should not be called for wxCB_READONLY controls, it is
245 // the callers responsability to check this
246 wxASSERT_MSG( !(GetWindowStyle() & wxCB_READONLY),
247 _T("read-only combobox doesn't have any edit control") );
248
249 POINT pt;
250 pt.x = pt.y = 4;
251 HWND hwndEdit = ::ChildWindowFromPoint(GetHwnd(), pt);
252 if ( !hwndEdit || hwndEdit == GetHwnd() )
253 {
254 wxFAIL_MSG(_T("not read only combobox without edit control?"));
255 }
256
257 return (WXHWND)hwndEdit;
258 }
259
260 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
261 const wxString& value,
262 const wxPoint& pos,
263 const wxSize& size,
264 int n, const wxString choices[],
265 long style,
266 const wxValidator& validator,
267 const wxString& name)
268 {
269 // first create wxWin object
270 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
271 return FALSE;
272
273 // get the right style
274 long msStyle = WS_TABSTOP | WS_VSCROLL | WS_HSCROLL |
275 CBS_AUTOHSCROLL | CBS_NOINTEGRALHEIGHT /* | WS_CLIPSIBLINGS */;
276 if ( style & wxCB_READONLY )
277 msStyle |= CBS_DROPDOWNLIST;
278 else if ( style & wxCB_SIMPLE )
279 msStyle |= CBS_SIMPLE; // A list (shown always) and edit control
280 else
281 msStyle |= CBS_DROPDOWN;
282
283 if ( style & wxCB_SORT )
284 msStyle |= CBS_SORT;
285
286 // and now create the MSW control
287 if ( !MSWCreateControl(_T("COMBOBOX"), msStyle) )
288 return FALSE;
289
290 SetSize(pos.x, pos.y, size.x, size.y);
291
292 // A choice/combobox normally has a white background (or other, depending
293 // on global settings) rather than inheriting the parent's background colour.
294 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
295
296 for ( int i = 0; i < n; i++ )
297 {
298 Append(choices[i]);
299 }
300
301 if ( !value.IsEmpty() )
302 {
303 SetValue(value);
304 }
305
306 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
307 // and an edit control inside it and if we want to catch events from this
308 // edit control, we must subclass it as well
309 if ( !(style & wxCB_READONLY) )
310 {
311 gs_wndprocEdit = (WXFARPROC)::SetWindowLong
312 (
313 (HWND)GetEditHWND(),
314 GWL_WNDPROC,
315 (LPARAM)wxComboEditWndProc
316 );
317 }
318
319 return TRUE;
320 }
321
322 // TODO: update and clear all this horrible mess (VZ)
323
324 void wxComboBox::SetValue(const wxString& value)
325 {
326 // If newlines are denoted by just 10, must stick 13 in front.
327 int singletons = 0;
328 int len = value.Length();
329 int i;
330 for (i = 0; i < len; i ++)
331 {
332 if ((i > 0) && (value[i] == 10) && (value[i-1] != 13))
333 singletons ++;
334 }
335 if (singletons > 0)
336 {
337 wxChar *tmp = new wxChar[len + singletons + 1];
338 int j = 0;
339 for (i = 0; i < len; i ++)
340 {
341 if ((i > 0) && (value[i] == 10) && (value[i-1] != 13))
342 {
343 tmp[j] = 13;
344 j ++;
345 }
346 tmp[j] = value[i];
347 j ++;
348 }
349 tmp[j] = 0;
350 SetWindowText(GetHwnd(), tmp);
351 delete[] tmp;
352 }
353 else
354 SetWindowText(GetHwnd(), value);
355 }
356
357 // Clipboard operations
358 void wxComboBox::Copy()
359 {
360 HWND hWnd = GetHwnd();
361 SendMessage(hWnd, WM_COPY, 0, 0L);
362 }
363
364 void wxComboBox::Cut()
365 {
366 HWND hWnd = GetHwnd();
367 SendMessage(hWnd, WM_CUT, 0, 0L);
368 }
369
370 void wxComboBox::Paste()
371 {
372 HWND hWnd = GetHwnd();
373 SendMessage(hWnd, WM_PASTE, 0, 0L);
374 }
375
376 void wxComboBox::SetEditable(bool WXUNUSED(editable))
377 {
378 // Can't implement in MSW?
379 // HWND hWnd = GetHwnd();
380 // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
381 }
382
383 void wxComboBox::SetInsertionPoint(long pos)
384 {
385 if ( GetWindowStyle() & wxCB_READONLY )
386 return;
387
388 #ifdef __WIN32__
389 HWND hWnd = GetHwnd();
390 ::SendMessage(hWnd, CB_SETEDITSEL, 0, MAKELPARAM(pos, pos));
391 HWND hEditWnd = (HWND) GetEditHWND() ;
392 if ( hEditWnd )
393 {
394 // Scroll insertion point into view
395 SendMessage(hEditWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
396 // Why is this necessary? (Copied from wxTextCtrl::SetInsertionPoint)
397 SendMessage(hEditWnd, EM_REPLACESEL, 0, (LPARAM)_T(""));
398 }
399 #endif // __WIN32__
400 }
401
402 void wxComboBox::SetInsertionPointEnd()
403 {
404 // setting insertion point doesn't make sense for read only comboboxes
405 if ( !(GetWindowStyle() & wxCB_READONLY) )
406 {
407 long pos = GetLastPosition();
408 SetInsertionPoint(pos);
409 }
410 }
411
412 long wxComboBox::GetInsertionPoint() const
413 {
414 #ifdef __WIN32__
415 DWORD Pos=(DWORD)SendMessage(GetHwnd(), CB_GETEDITSEL, 0, 0L);
416 return Pos&0xFFFF;
417 #else
418 return 0;
419 #endif
420 }
421
422 long wxComboBox::GetLastPosition() const
423 {
424 HWND hEditWnd = (HWND) GetEditHWND();
425
426 // Get number of characters in the last (only) line. We'll add this to the character
427 // index for the last line, 1st position.
428 int lineLength = (int)SendMessage(hEditWnd, EM_LINELENGTH, (WPARAM) 0, (LPARAM)0L);
429
430 return (long)(lineLength);
431 }
432
433 void wxComboBox::Replace(long from, long to, const wxString& value)
434 {
435 #if wxUSE_CLIPBOARD
436 Remove(from, to);
437
438 // Now replace with 'value', by pasting.
439 wxSetClipboardData(wxDF_TEXT, (wxObject *)(const wxChar *)value, 0, 0);
440
441 // Paste into edit control
442 SendMessage(GetHwnd(), WM_PASTE, (WPARAM)0, (LPARAM)0L);
443 #endif
444 }
445
446 void wxComboBox::Remove(long from, long to)
447 {
448 // Set selection and remove it
449 SetSelection(from, to);
450 SendMessage(GetHwnd(), WM_CUT, (WPARAM)0, (LPARAM)0);
451 }
452
453 void wxComboBox::SetSelection(long from, long to)
454 {
455 HWND hWnd = GetHwnd();
456 long fromChar = from;
457 long toChar = to;
458 // if from and to are both -1, it means
459 // (in wxWindows) that all text should be selected.
460 // This translates into Windows convention
461 if ((from == -1) && (to == -1))
462 {
463 fromChar = 0;
464 toChar = -1;
465 }
466
467 if (
468 #ifdef __WIN32__
469 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar))
470 #else // Win16
471 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)fromChar, (LPARAM)toChar)
472 #endif
473 == CB_ERR )
474 {
475 wxLogDebug(_T("CB_SETEDITSEL failed"));
476 }
477 }
478
479 void wxComboBox::DoMoveWindow(int x, int y, int width, int height)
480 {
481 // here is why this is necessary: if the width is negative, the combobox
482 // window proc makes the window of the size width*height instead of
483 // interpreting height in the usual manner (meaning the height of the drop
484 // down list - usually the height specified in the call to MoveWindow()
485 // will not change the height of combo box per se)
486 //
487 // this behaviour is not documented anywhere, but this is just how it is
488 // here (NT 4.4) and, anyhow, the check shouldn't hurt - however without
489 // the check, constraints/sizers using combos may break the height
490 // constraint will have not at all the same value as expected
491 if ( width < 0 )
492 return;
493
494 int cx, cy;
495 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
496
497 // what should the height of the drop down list be? we choose 10 items by
498 // default and also 10 items max (if we always use n, the list will never
499 // have vertical scrollbar)
500 int n = GetCount();
501 if ( !n || (n > 10) )
502 n = 10;
503
504 height = (n + 1)* EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
505
506 wxControl::DoMoveWindow(x, y, width, height);
507 }
508
509 wxSize wxComboBox::DoGetBestSize() const
510 {
511 // the choice calculates the horz size correctly, but not the vertical
512 // component: correct it
513 wxSize size = wxChoice::DoGetBestSize();
514
515 int cx, cy;
516 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
517 size.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
518
519 return size;
520 }
521
522 #endif
523 // wxUSE_COMBOBOX
524