]> git.saurik.com Git - wxWidgets.git/blob - src/msw/combobox.cpp
added support for gcc precompiled headers
[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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 // for wxEVT_COMMAND_TEXT_ENTER
37 #include "wx/textctrl.h"
38 #endif
39
40 #include "wx/combobox.h"
41 #include "wx/brush.h"
42 #include "wx/clipbrd.h"
43 #include "wx/msw/private.h"
44
45 #if wxUSE_TOOLTIPS
46 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
47 #include <commctrl.h>
48 #endif
49 #include "wx/tooltip.h"
50 #endif // wxUSE_TOOLTIPS
51
52 // ----------------------------------------------------------------------------
53 // wxWin macros
54 // ----------------------------------------------------------------------------
55
56 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
57
58 /*
59 TODO PROPERTIES
60 selection (long¨)
61 content
62 value
63 item
64 */
65
66 // ----------------------------------------------------------------------------
67 // function prototypes
68 // ----------------------------------------------------------------------------
69
70 LRESULT APIENTRY _EXPORT wxComboEditWndProc(HWND hWnd,
71 UINT message,
72 WPARAM wParam,
73 LPARAM lParam);
74
75 // ---------------------------------------------------------------------------
76 // global vars
77 // ---------------------------------------------------------------------------
78
79 // the pointer to standard radio button wnd proc
80 static WXFARPROC gs_wndprocEdit = (WXFARPROC)NULL;
81
82 // ============================================================================
83 // implementation
84 // ============================================================================
85
86 // ----------------------------------------------------------------------------
87 // wnd proc for subclassed edit control
88 // ----------------------------------------------------------------------------
89
90 LRESULT APIENTRY _EXPORT wxComboEditWndProc(HWND hWnd,
91 UINT message,
92 WPARAM wParam,
93 LPARAM lParam)
94 {
95 HWND hwndCombo = ::GetParent(hWnd);
96 wxWindow *win = wxFindWinFromHandle((WXHWND)hwndCombo);
97
98 switch ( message )
99 {
100 // forward some messages to the combobox to generate the appropriate
101 // wxEvents from them
102 case WM_KEYUP:
103 case WM_KEYDOWN:
104 case WM_CHAR:
105 case WM_SETFOCUS:
106 case WM_KILLFOCUS:
107 {
108 wxComboBox *combo = wxDynamicCast(win, wxComboBox);
109 if ( !combo )
110 {
111 // we can get WM_KILLFOCUS while our parent is already half
112 // destroyed and hence doesn't look like a combobx any
113 // longer, check for it to avoid bogus assert failures
114 if ( !win->IsBeingDeleted() )
115 {
116 wxFAIL_MSG( _T("should have combo as parent") );
117 }
118 }
119 else if ( combo->MSWProcessEditMsg(message, wParam, lParam) )
120 {
121 // handled by parent
122 return 0;
123 }
124 }
125 break;
126
127 case WM_GETDLGCODE:
128 {
129 wxCHECK_MSG( win, 0, _T("should have a parent") );
130
131 if ( win->GetWindowStyle() & wxPROCESS_ENTER )
132 {
133 // need to return a custom dlg code or we'll never get it
134 return DLGC_WANTMESSAGE;
135 }
136 }
137 break;
138
139 // deal with tooltips here
140 #if wxUSE_TOOLTIPS && defined(TTN_NEEDTEXT)
141 case WM_NOTIFY:
142 {
143 wxCHECK_MSG( win, 0, _T("should have a parent") );
144
145 NMHDR* hdr = (NMHDR *)lParam;
146 if ( hdr->code == TTN_NEEDTEXT )
147 {
148 wxToolTip *tooltip = win->GetToolTip();
149 if ( tooltip )
150 {
151 TOOLTIPTEXT *ttt = (TOOLTIPTEXT *)lParam;
152 ttt->lpszText = (wxChar *)tooltip->GetTip().c_str();
153 }
154
155 // processed
156 return 0;
157 }
158 }
159 break;
160 #endif // wxUSE_TOOLTIPS
161 }
162
163 return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit, hWnd, message, wParam, lParam);
164 }
165
166 WXHBRUSH wxComboBox::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
167 WXUINT WXUNUSED(message),
168 WXWPARAM WXUNUSED(wParam),
169 WXLPARAM WXUNUSED(lParam)
170 )
171 {
172 HDC hdc = (HDC)pDC;
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 // for compatibility with wxTextCtrl, generate a special message
196 // when Enter is pressed
197 if ( wParam == VK_RETURN )
198 {
199 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
200 InitCommandEvent(event);
201 event.SetString(GetValue());
202 event.SetInt(GetSelection());
203 ProcessCommand(event);
204 }
205
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 ProcessCommand(event);
273 }
274 break;
275 }
276
277 // there is no return value for the CBN_ notifications, so always return
278 // FALSE from here to pass the message to DefWindowProc()
279 return FALSE;
280 }
281
282 WXHWND wxComboBox::GetEditHWND() const
283 {
284 // this function should not be called for wxCB_READONLY controls, it is
285 // the callers responsability to check this
286 wxASSERT_MSG( !(GetWindowStyle() & wxCB_READONLY),
287 _T("read-only combobox doesn't have any edit control") );
288
289 POINT pt;
290 pt.x = pt.y = 4;
291 HWND hwndEdit = ::ChildWindowFromPoint(GetHwnd(), pt);
292 if ( !hwndEdit || hwndEdit == GetHwnd() )
293 {
294 wxFAIL_MSG(_T("not read only combobox without edit control?"));
295 }
296
297 return (WXHWND)hwndEdit;
298 }
299
300 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
301 const wxString& value,
302 const wxPoint& pos,
303 const wxSize& size,
304 int n, const wxString choices[],
305 long style,
306 const wxValidator& validator,
307 const wxString& name)
308 {
309 // pretend that wxComboBox is hidden while it is positioned and resized and
310 // show it only right before leaving this method because otherwise there is
311 // some noticeable flicker while the control rearranges itself
312 m_isShown = FALSE;
313
314 // first create wxWin object
315 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
316 return FALSE;
317
318 // get the right style
319 long msStyle = WS_TABSTOP | WS_VSCROLL | WS_HSCROLL |
320 CBS_AUTOHSCROLL | CBS_NOINTEGRALHEIGHT /* | WS_CLIPSIBLINGS */;
321 if ( style & wxCB_READONLY )
322 msStyle |= CBS_DROPDOWNLIST;
323 #ifndef __WXWINCE__
324 else if ( style & wxCB_SIMPLE )
325 msStyle |= CBS_SIMPLE; // A list (shown always) and edit control
326 #endif
327 else
328 msStyle |= CBS_DROPDOWN;
329
330 if ( style & wxCB_SORT )
331 msStyle |= CBS_SORT;
332
333 if ( style & wxCLIP_SIBLINGS )
334 msStyle |= WS_CLIPSIBLINGS;
335
336
337 // and now create the MSW control
338 if ( !MSWCreateControl(_T("COMBOBOX"), msStyle) )
339 return FALSE;
340
341 // A choice/combobox normally has a white background (or other, depending
342 // on global settings) rather than inheriting the parent's background colour.
343 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
344
345 for ( int i = 0; i < n; i++ )
346 {
347 Append(choices[i]);
348 }
349
350 if ( !value.IsEmpty() )
351 {
352 SetValue(value);
353 }
354
355 // do this after appending the values to the combobox so that autosizing
356 // works correctly
357 SetSize(pos.x, pos.y, size.x, size.y);
358
359 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
360 // and an edit control inside it and if we want to catch events from this
361 // edit control, we must subclass it as well
362 if ( !(style & wxCB_READONLY) )
363 {
364 gs_wndprocEdit = (WXFARPROC)::SetWindowLong
365 (
366 (HWND)GetEditHWND(),
367 GWL_WNDPROC,
368 (LPARAM)wxComboEditWndProc
369 );
370 }
371
372 // and finally, show the control
373 Show(TRUE);
374
375 return TRUE;
376 }
377
378 void wxComboBox::SetValue(const wxString& value)
379 {
380 if ( HasFlag(wxCB_READONLY) )
381 SetStringSelection(value);
382 else
383 SetWindowText(GetHwnd(), value.c_str());
384 }
385
386 // Clipboard operations
387 void wxComboBox::Copy()
388 {
389 SendMessage(GetHwnd(), WM_COPY, 0, 0L);
390 }
391
392 void wxComboBox::Cut()
393 {
394 SendMessage(GetHwnd(), WM_CUT, 0, 0L);
395 }
396
397 void wxComboBox::Paste()
398 {
399 SendMessage(GetHwnd(), WM_PASTE, 0, 0L);
400 }
401
402 void wxComboBox::SetEditable(bool WXUNUSED(editable))
403 {
404 // Can't implement in MSW?
405 // HWND hWnd = GetHwnd();
406 // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
407 }
408
409 void wxComboBox::SetInsertionPoint(long pos)
410 {
411 if ( GetWindowStyle() & wxCB_READONLY )
412 return;
413
414 #ifdef __WIN32__
415 HWND hWnd = GetHwnd();
416 ::SendMessage(hWnd, CB_SETEDITSEL, 0, MAKELPARAM(pos, pos));
417 HWND hEditWnd = (HWND) GetEditHWND() ;
418 if ( hEditWnd )
419 {
420 // Scroll insertion point into view
421 SendMessage(hEditWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
422 // Why is this necessary? (Copied from wxTextCtrl::SetInsertionPoint)
423 SendMessage(hEditWnd, EM_REPLACESEL, 0, (LPARAM) wxEmptyString);
424 }
425 #endif // __WIN32__
426 }
427
428 void wxComboBox::SetInsertionPointEnd()
429 {
430 // setting insertion point doesn't make sense for read only comboboxes
431 if ( !(GetWindowStyle() & wxCB_READONLY) )
432 {
433 long pos = GetLastPosition();
434 SetInsertionPoint(pos);
435 }
436 }
437
438 long wxComboBox::GetInsertionPoint() const
439 {
440 #ifdef __WIN32__
441 DWORD Pos=(DWORD)SendMessage(GetHwnd(), CB_GETEDITSEL, 0, 0L);
442 return Pos&0xFFFF;
443 #else
444 return 0;
445 #endif
446 }
447
448 long wxComboBox::GetLastPosition() const
449 {
450 HWND hEditWnd = (HWND) GetEditHWND();
451
452 // Get number of characters in the last (only) line. We'll add this to the character
453 // index for the last line, 1st position.
454 int lineLength = (int)SendMessage(hEditWnd, EM_LINELENGTH, (WPARAM) 0, (LPARAM)0L);
455
456 return (long)(lineLength);
457 }
458
459 void wxComboBox::Replace(long from, long to, const wxString& value)
460 {
461 #if wxUSE_CLIPBOARD
462 Remove(from, to);
463
464 // Now replace with 'value', by pasting.
465 wxSetClipboardData(wxDF_TEXT, (wxObject *)(const wxChar *)value, 0, 0);
466
467 // Paste into edit control
468 SendMessage(GetHwnd(), WM_PASTE, (WPARAM)0, (LPARAM)0L);
469 #endif
470 }
471
472 void wxComboBox::Remove(long from, long to)
473 {
474 // Set selection and remove it
475 SetSelection(from, to);
476 SendMessage(GetHwnd(), WM_CUT, (WPARAM)0, (LPARAM)0);
477 }
478
479 void wxComboBox::SetSelection(long from, long to)
480 {
481 HWND hWnd = GetHwnd();
482 long fromChar = from;
483 long toChar = to;
484 // if from and to are both -1, it means
485 // (in wxWindows) that all text should be selected.
486 // This translates into Windows convention
487 if ((from == -1) && (to == -1))
488 {
489 fromChar = 0;
490 toChar = -1;
491 }
492
493 if (
494 #ifdef __WIN32__
495 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar))
496 #else // Win16
497 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)fromChar, (LPARAM)toChar)
498 #endif
499 == CB_ERR )
500 {
501 wxLogDebug(_T("CB_SETEDITSEL failed"));
502 }
503 }
504
505 #endif
506 // wxUSE_COMBOBOX
507