]> git.saurik.com Git - wxWidgets.git/blob - src/msw/combobox.cpp
fixed compilation without PCH
[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 wxCHECK_MSG( combo, 0, _T("should have combo as parent") );
100
101 if ( combo->MSWProcessEditMsg(message, wParam, lParam) )
102 return 0;
103 }
104 break;
105
106 #if 0
107 case WM_GETDLGCODE:
108 {
109 wxCHECK_MSG( win, 0, _T("should have a parent") );
110
111 if ( win->GetWindowStyle() & wxPROCESS_ENTER )
112 {
113 // need to return a custom dlg code or we'll never get it
114 return DLGC_WANTMESSAGE;
115 }
116 }
117 break;
118 #endif // 0
119
120 // deal with tooltips here
121 #if wxUSE_TOOLTIPS && defined(TTN_NEEDTEXT)
122 case WM_NOTIFY:
123 {
124 wxCHECK_MSG( win, 0, _T("should have a parent") );
125
126 NMHDR* hdr = (NMHDR *)lParam;
127 if ( (int)hdr->code == TTN_NEEDTEXT )
128 {
129 wxToolTip *tooltip = win->GetToolTip();
130 if ( tooltip )
131 {
132 TOOLTIPTEXT *ttt = (TOOLTIPTEXT *)lParam;
133 ttt->lpszText = (wxChar *)tooltip->GetTip().c_str();
134 }
135
136 // processed
137 return 0;
138 }
139 }
140 break;
141 #endif // wxUSE_TOOLTIPS
142 }
143
144 return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit, hWnd, message, wParam, lParam);
145 }
146
147 WXHBRUSH wxComboBox::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
148 #if wxUSE_CTL3D
149 WXUINT message,
150 WXWPARAM wParam,
151 WXLPARAM lParam
152 #else
153 WXUINT WXUNUSED(message),
154 WXWPARAM WXUNUSED(wParam),
155 WXLPARAM WXUNUSED(lParam)
156 #endif
157 )
158 {
159 #if wxUSE_CTL3D
160 if ( m_useCtl3D )
161 {
162 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
163 return (WXHBRUSH) hbrush;
164 }
165 #endif // wxUSE_CTL3D
166
167 HDC hdc = (HDC)pDC;
168 if (GetParent()->GetTransparentBackground())
169 SetBkMode(hdc, TRANSPARENT);
170 else
171 SetBkMode(hdc, OPAQUE);
172
173 wxColour colBack = GetBackgroundColour();
174
175 if (!IsEnabled())
176 colBack = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
177
178 ::SetBkColor(hdc, wxColourToRGB(colBack));
179 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
180
181 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
182
183 return (WXHBRUSH)brush->GetResourceHandle();
184 }
185
186 // ----------------------------------------------------------------------------
187 // wxComboBox
188 // ----------------------------------------------------------------------------
189
190 bool wxComboBox::MSWProcessEditMsg(WXUINT msg, WXWPARAM wParam, WXLPARAM lParam)
191 {
192 switch ( msg )
193 {
194 case WM_CHAR:
195 return HandleChar(wParam, lParam, TRUE /* isASCII */);
196
197 case WM_KEYDOWN:
198 return HandleKeyDown(wParam, lParam);
199
200 case WM_KEYUP:
201 return HandleKeyUp(wParam, lParam);
202
203 case WM_SETFOCUS:
204 return HandleSetFocus((WXHWND)wParam);
205
206 case WM_KILLFOCUS:
207 return HandleKillFocus((WXHWND)wParam);
208 }
209
210 return FALSE;
211 }
212
213 bool wxComboBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
214 {
215 wxString value;
216 int sel = -1;
217 switch ( param )
218 {
219 case CBN_SELCHANGE:
220 sel = GetSelection();
221 if ( sel > -1 )
222 {
223 value = GetString(sel);
224
225 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId());
226 event.SetInt(sel);
227 event.SetEventObject(this);
228 event.SetString(value);
229 ProcessCommand(event);
230 }
231 else
232 {
233 break;
234 }
235
236 // fall through: for compability with wxGTK, also send the text
237 // update event when the selection changes (this also seems more
238 // logical as the text does change)
239
240 case CBN_EDITCHANGE:
241 {
242 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
243
244 // if sel != -1, value was initialized above (and we can't use
245 // GetValue() here as it would return the old selection and we
246 // want the new one)
247 if ( sel == -1 )
248 {
249 value = GetValue();
250 }
251 else // we're synthesizing text updated event from sel change
252 {
253 // we need to do this because the user code expects
254 // wxComboBox::GetValue() to return the new value from
255 // "text updated" handler but it hadn't been updated yet
256 SetValue(value);
257 }
258
259 event.SetString(value);
260 event.SetEventObject(this);
261 ProcessCommand(event);
262 }
263 break;
264 }
265
266 // there is no return value for the CBN_ notifications, so always return
267 // FALSE from here to pass the message to DefWindowProc()
268 return FALSE;
269 }
270
271 WXHWND wxComboBox::GetEditHWND() const
272 {
273 // this function should not be called for wxCB_READONLY controls, it is
274 // the callers responsability to check this
275 wxASSERT_MSG( !(GetWindowStyle() & wxCB_READONLY),
276 _T("read-only combobox doesn't have any edit control") );
277
278 POINT pt;
279 pt.x = pt.y = 4;
280 HWND hwndEdit = ::ChildWindowFromPoint(GetHwnd(), pt);
281 if ( !hwndEdit || hwndEdit == GetHwnd() )
282 {
283 wxFAIL_MSG(_T("not read only combobox without edit control?"));
284 }
285
286 return (WXHWND)hwndEdit;
287 }
288
289 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
290 const wxString& value,
291 const wxPoint& pos,
292 const wxSize& size,
293 int n, const wxString choices[],
294 long style,
295 const wxValidator& validator,
296 const wxString& name)
297 {
298 // first create wxWin object
299 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
300 return FALSE;
301
302 // get the right style
303 long msStyle = WS_TABSTOP | WS_VSCROLL | WS_HSCROLL |
304 CBS_AUTOHSCROLL | CBS_NOINTEGRALHEIGHT /* | WS_CLIPSIBLINGS */;
305 if ( style & wxCB_READONLY )
306 msStyle |= CBS_DROPDOWNLIST;
307 else if ( style & wxCB_SIMPLE )
308 msStyle |= CBS_SIMPLE; // A list (shown always) and edit control
309 else
310 msStyle |= CBS_DROPDOWN;
311
312 if ( style & wxCB_SORT )
313 msStyle |= CBS_SORT;
314
315 if ( style & wxCLIP_SIBLINGS )
316 msStyle |= WS_CLIPSIBLINGS;
317
318
319 // and now create the MSW control
320 if ( !MSWCreateControl(_T("COMBOBOX"), msStyle) )
321 return FALSE;
322
323 // A choice/combobox normally has a white background (or other, depending
324 // on global settings) rather than inheriting the parent's background colour.
325 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
326
327 for ( int i = 0; i < n; i++ )
328 {
329 Append(choices[i]);
330 }
331
332 if ( !value.IsEmpty() )
333 {
334 SetValue(value);
335 }
336
337 // do this after appending the values to the combobox so that autosizing
338 // works correctly
339 SetSize(pos.x, pos.y, size.x, size.y);
340
341 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
342 // and an edit control inside it and if we want to catch events from this
343 // edit control, we must subclass it as well
344 if ( !(style & wxCB_READONLY) )
345 {
346 gs_wndprocEdit = (WXFARPROC)::SetWindowLong
347 (
348 (HWND)GetEditHWND(),
349 GWL_WNDPROC,
350 (LPARAM)wxComboEditWndProc
351 );
352 }
353
354 return TRUE;
355 }
356
357 // TODO: update and clear all this horrible mess (VZ)
358
359 void wxComboBox::SetValue(const wxString& value)
360 {
361 // If newlines are denoted by just 10, must stick 13 in front.
362 int singletons = 0;
363 int len = value.Length();
364 int i;
365 for (i = 0; i < len; i ++)
366 {
367 if ((i > 0) && (value[i] == 10) && (value[i-1] != 13))
368 singletons ++;
369 }
370 if (singletons > 0)
371 {
372 wxChar *tmp = new wxChar[len + singletons + 1];
373 int j = 0;
374 for (i = 0; i < len; i ++)
375 {
376 if ((i > 0) && (value[i] == 10) && (value[i-1] != 13))
377 {
378 tmp[j] = 13;
379 j ++;
380 }
381 tmp[j] = value[i];
382 j ++;
383 }
384 tmp[j] = 0;
385 SetWindowText(GetHwnd(), tmp);
386 delete[] tmp;
387 }
388 else
389 SetWindowText(GetHwnd(), value);
390 }
391
392 // Clipboard operations
393 void wxComboBox::Copy()
394 {
395 HWND hWnd = GetHwnd();
396 SendMessage(hWnd, WM_COPY, 0, 0L);
397 }
398
399 void wxComboBox::Cut()
400 {
401 HWND hWnd = GetHwnd();
402 SendMessage(hWnd, WM_CUT, 0, 0L);
403 }
404
405 void wxComboBox::Paste()
406 {
407 HWND hWnd = GetHwnd();
408 SendMessage(hWnd, 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