]> git.saurik.com Git - wxWidgets.git/blob - src/msw/combobox.cpp
Adjusted filelist.txt and wxUniv.dsp to use generic
[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 case WM_GETDLGCODE:
118 {
119 wxCHECK_MSG( win, 0, _T("should have a parent") );
120
121 if ( win->GetWindowStyle() & wxPROCESS_ENTER )
122 {
123 // need to return a custom dlg code or we'll never get it
124 return DLGC_WANTMESSAGE;
125 }
126 }
127 break;
128
129 // deal with tooltips here
130 #if wxUSE_TOOLTIPS && defined(TTN_NEEDTEXT)
131 case WM_NOTIFY:
132 {
133 wxCHECK_MSG( win, 0, _T("should have a parent") );
134
135 NMHDR* hdr = (NMHDR *)lParam;
136 if ( (int)hdr->code == TTN_NEEDTEXT )
137 {
138 wxToolTip *tooltip = win->GetToolTip();
139 if ( tooltip )
140 {
141 TOOLTIPTEXT *ttt = (TOOLTIPTEXT *)lParam;
142 ttt->lpszText = (wxChar *)tooltip->GetTip().c_str();
143 }
144
145 // processed
146 return 0;
147 }
148 }
149 break;
150 #endif // wxUSE_TOOLTIPS
151 }
152
153 return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit, hWnd, message, wParam, lParam);
154 }
155
156 WXHBRUSH wxComboBox::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
157 #if wxUSE_CTL3D
158 WXUINT message,
159 WXWPARAM wParam,
160 WXLPARAM lParam
161 #else
162 WXUINT WXUNUSED(message),
163 WXWPARAM WXUNUSED(wParam),
164 WXLPARAM WXUNUSED(lParam)
165 #endif
166 )
167 {
168 #if wxUSE_CTL3D
169 if ( m_useCtl3D )
170 {
171 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
172 return (WXHBRUSH) hbrush;
173 }
174 #endif // wxUSE_CTL3D
175
176 HDC hdc = (HDC)pDC;
177 if (GetParent()->GetTransparentBackground())
178 SetBkMode(hdc, TRANSPARENT);
179 else
180 SetBkMode(hdc, OPAQUE);
181
182 wxColour colBack = GetBackgroundColour();
183
184 if (!IsEnabled())
185 colBack = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
186
187 ::SetBkColor(hdc, wxColourToRGB(colBack));
188 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
189
190 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
191
192 return (WXHBRUSH)brush->GetResourceHandle();
193 }
194
195 // ----------------------------------------------------------------------------
196 // wxComboBox
197 // ----------------------------------------------------------------------------
198
199 bool wxComboBox::MSWProcessEditMsg(WXUINT msg, WXWPARAM wParam, WXLPARAM lParam)
200 {
201 switch ( msg )
202 {
203 case WM_CHAR:
204 return HandleChar(wParam, lParam, TRUE /* isASCII */);
205
206 case WM_KEYDOWN:
207 return HandleKeyDown(wParam, lParam);
208
209 case WM_KEYUP:
210 return HandleKeyUp(wParam, lParam);
211
212 case WM_SETFOCUS:
213 return HandleSetFocus((WXHWND)wParam);
214
215 case WM_KILLFOCUS:
216 return HandleKillFocus((WXHWND)wParam);
217 }
218
219 return FALSE;
220 }
221
222 bool wxComboBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
223 {
224 wxString value;
225 int sel = -1;
226 switch ( param )
227 {
228 case CBN_SELCHANGE:
229 sel = GetSelection();
230 if ( sel > -1 )
231 {
232 value = GetString(sel);
233
234 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId());
235 event.SetInt(sel);
236 event.SetEventObject(this);
237 event.SetString(value);
238 ProcessCommand(event);
239 }
240 else
241 {
242 break;
243 }
244
245 // fall through: for compability with wxGTK, also send the text
246 // update event when the selection changes (this also seems more
247 // logical as the text does change)
248
249 case CBN_EDITCHANGE:
250 {
251 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
252
253 // if sel != -1, value was initialized above (and we can't use
254 // GetValue() here as it would return the old selection and we
255 // want the new one)
256 if ( sel == -1 )
257 {
258 value = GetValue();
259 }
260 else // we're synthesizing text updated event from sel change
261 {
262 // we need to do this because the user code expects
263 // wxComboBox::GetValue() to return the new value from
264 // "text updated" handler but it hadn't been updated yet
265 SetValue(value);
266 }
267
268 event.SetString(value);
269 event.SetEventObject(this);
270 ProcessCommand(event);
271 }
272 break;
273 }
274
275 // there is no return value for the CBN_ notifications, so always return
276 // FALSE from here to pass the message to DefWindowProc()
277 return FALSE;
278 }
279
280 WXHWND wxComboBox::GetEditHWND() const
281 {
282 // this function should not be called for wxCB_READONLY controls, it is
283 // the callers responsability to check this
284 wxASSERT_MSG( !(GetWindowStyle() & wxCB_READONLY),
285 _T("read-only combobox doesn't have any edit control") );
286
287 POINT pt;
288 pt.x = pt.y = 4;
289 HWND hwndEdit = ::ChildWindowFromPoint(GetHwnd(), pt);
290 if ( !hwndEdit || hwndEdit == GetHwnd() )
291 {
292 wxFAIL_MSG(_T("not read only combobox without edit control?"));
293 }
294
295 return (WXHWND)hwndEdit;
296 }
297
298 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
299 const wxString& value,
300 const wxPoint& pos,
301 const wxSize& size,
302 int n, const wxString choices[],
303 long style,
304 const wxValidator& validator,
305 const wxString& name)
306 {
307 // pretend that wxComboBox is hidden while it is positioned and resized and
308 // show it only right before leaving this method because otherwise there is
309 // some noticeable flicker while the control rearranges itself
310 m_isShown = FALSE;
311
312 // first create wxWin object
313 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
314 return FALSE;
315
316 // get the right style
317 long msStyle = WS_TABSTOP | WS_VSCROLL | WS_HSCROLL |
318 CBS_AUTOHSCROLL | CBS_NOINTEGRALHEIGHT /* | WS_CLIPSIBLINGS */;
319 if ( style & wxCB_READONLY )
320 msStyle |= CBS_DROPDOWNLIST;
321 else if ( style & wxCB_SIMPLE )
322 msStyle |= CBS_SIMPLE; // A list (shown always) and edit control
323 else
324 msStyle |= CBS_DROPDOWN;
325
326 if ( style & wxCB_SORT )
327 msStyle |= CBS_SORT;
328
329 if ( style & wxCLIP_SIBLINGS )
330 msStyle |= WS_CLIPSIBLINGS;
331
332
333 // and now create the MSW control
334 if ( !MSWCreateControl(_T("COMBOBOX"), msStyle) )
335 return FALSE;
336
337 // A choice/combobox normally has a white background (or other, depending
338 // on global settings) rather than inheriting the parent's background colour.
339 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
340
341 for ( int i = 0; i < n; i++ )
342 {
343 Append(choices[i]);
344 }
345
346 if ( !value.IsEmpty() )
347 {
348 SetValue(value);
349 }
350
351 // do this after appending the values to the combobox so that autosizing
352 // works correctly
353 SetSize(pos.x, pos.y, size.x, size.y);
354
355 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
356 // and an edit control inside it and if we want to catch events from this
357 // edit control, we must subclass it as well
358 if ( !(style & wxCB_READONLY) )
359 {
360 gs_wndprocEdit = (WXFARPROC)::SetWindowLong
361 (
362 (HWND)GetEditHWND(),
363 GWL_WNDPROC,
364 (LPARAM)wxComboEditWndProc
365 );
366 }
367
368 // and finally, show the control
369 Show(TRUE);
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 size_t singletons = 0;
380 size_t len = value.Length();
381 size_t 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 wxString tmp;
390 tmp.Alloc(len + singletons);
391 size_t j = 0;
392 for (i = 0; i < len; i ++)
393 {
394 if ((i > 0) && (value[i] == 10) && (value[i-1] != 13))
395 {
396 tmp[j] = 13;
397 j ++;
398 }
399 tmp[j] = value[i];
400 j ++;
401 }
402 if (GetWindowStyle() & wxCB_READONLY)
403 SetStringSelection(tmp);
404 else
405 SetWindowText(GetHwnd(), tmp.c_str());
406 }
407 else
408 if (GetWindowStyle() & wxCB_READONLY)
409 SetStringSelection(value);
410 else
411 SetWindowText(GetHwnd(), value.c_str());
412 }
413
414 // Clipboard operations
415 void wxComboBox::Copy()
416 {
417 HWND hWnd = GetHwnd();
418 SendMessage(hWnd, WM_COPY, 0, 0L);
419 }
420
421 void wxComboBox::Cut()
422 {
423 HWND hWnd = GetHwnd();
424 SendMessage(hWnd, WM_CUT, 0, 0L);
425 }
426
427 void wxComboBox::Paste()
428 {
429 HWND hWnd = GetHwnd();
430 SendMessage(hWnd, WM_PASTE, 0, 0L);
431 }
432
433 void wxComboBox::SetEditable(bool WXUNUSED(editable))
434 {
435 // Can't implement in MSW?
436 // HWND hWnd = GetHwnd();
437 // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
438 }
439
440 void wxComboBox::SetInsertionPoint(long pos)
441 {
442 if ( GetWindowStyle() & wxCB_READONLY )
443 return;
444
445 #ifdef __WIN32__
446 HWND hWnd = GetHwnd();
447 ::SendMessage(hWnd, CB_SETEDITSEL, 0, MAKELPARAM(pos, pos));
448 HWND hEditWnd = (HWND) GetEditHWND() ;
449 if ( hEditWnd )
450 {
451 // Scroll insertion point into view
452 SendMessage(hEditWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
453 // Why is this necessary? (Copied from wxTextCtrl::SetInsertionPoint)
454 SendMessage(hEditWnd, EM_REPLACESEL, 0, (LPARAM)_T(""));
455 }
456 #endif // __WIN32__
457 }
458
459 void wxComboBox::SetInsertionPointEnd()
460 {
461 // setting insertion point doesn't make sense for read only comboboxes
462 if ( !(GetWindowStyle() & wxCB_READONLY) )
463 {
464 long pos = GetLastPosition();
465 SetInsertionPoint(pos);
466 }
467 }
468
469 long wxComboBox::GetInsertionPoint() const
470 {
471 #ifdef __WIN32__
472 DWORD Pos=(DWORD)SendMessage(GetHwnd(), CB_GETEDITSEL, 0, 0L);
473 return Pos&0xFFFF;
474 #else
475 return 0;
476 #endif
477 }
478
479 long wxComboBox::GetLastPosition() const
480 {
481 HWND hEditWnd = (HWND) GetEditHWND();
482
483 // Get number of characters in the last (only) line. We'll add this to the character
484 // index for the last line, 1st position.
485 int lineLength = (int)SendMessage(hEditWnd, EM_LINELENGTH, (WPARAM) 0, (LPARAM)0L);
486
487 return (long)(lineLength);
488 }
489
490 void wxComboBox::Replace(long from, long to, const wxString& value)
491 {
492 #if wxUSE_CLIPBOARD
493 Remove(from, to);
494
495 // Now replace with 'value', by pasting.
496 wxSetClipboardData(wxDF_TEXT, (wxObject *)(const wxChar *)value, 0, 0);
497
498 // Paste into edit control
499 SendMessage(GetHwnd(), WM_PASTE, (WPARAM)0, (LPARAM)0L);
500 #endif
501 }
502
503 void wxComboBox::Remove(long from, long to)
504 {
505 // Set selection and remove it
506 SetSelection(from, to);
507 SendMessage(GetHwnd(), WM_CUT, (WPARAM)0, (LPARAM)0);
508 }
509
510 void wxComboBox::SetSelection(long from, long to)
511 {
512 HWND hWnd = GetHwnd();
513 long fromChar = from;
514 long toChar = to;
515 // if from and to are both -1, it means
516 // (in wxWindows) that all text should be selected.
517 // This translates into Windows convention
518 if ((from == -1) && (to == -1))
519 {
520 fromChar = 0;
521 toChar = -1;
522 }
523
524 if (
525 #ifdef __WIN32__
526 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar))
527 #else // Win16
528 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)fromChar, (LPARAM)toChar)
529 #endif
530 == CB_ERR )
531 {
532 wxLogDebug(_T("CB_SETEDITSEL failed"));
533 }
534 }
535
536 #endif
537 // wxUSE_COMBOBOX
538