]> git.saurik.com Git - wxWidgets.git/blob - src/msw/combobox.cpp
replaced the old wxApp with wxAppConsole::ms_appInstance of type wxAppConsole; wxTheA...
[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 #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 // 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 // function prototypes
60 // ----------------------------------------------------------------------------
61
62 LRESULT APIENTRY _EXPORT wxComboEditWndProc(HWND hWnd,
63 UINT message,
64 WPARAM wParam,
65 LPARAM lParam);
66
67 // ---------------------------------------------------------------------------
68 // global vars
69 // ---------------------------------------------------------------------------
70
71 // the pointer to standard radio button wnd proc
72 static WXFARPROC gs_wndprocEdit = (WXFARPROC)NULL;
73
74 // ============================================================================
75 // implementation
76 // ============================================================================
77
78 // ----------------------------------------------------------------------------
79 // wnd proc for subclassed edit control
80 // ----------------------------------------------------------------------------
81
82 LRESULT APIENTRY _EXPORT wxComboEditWndProc(HWND hWnd,
83 UINT message,
84 WPARAM wParam,
85 LPARAM lParam)
86 {
87 HWND hwndCombo = ::GetParent(hWnd);
88 wxWindow *win = wxFindWinFromHandle((WXHWND)hwndCombo);
89
90 switch ( message )
91 {
92 // forward some messages to the combobox to generate the appropriate
93 // wxEvents from them
94 case WM_KEYUP:
95 case WM_KEYDOWN:
96 case WM_CHAR:
97 case WM_SETFOCUS:
98 case WM_KILLFOCUS:
99 {
100 wxComboBox *combo = wxDynamicCast(win, wxComboBox);
101 if ( !combo )
102 {
103 // we can get WM_KILLFOCUS while our parent is already half
104 // destroyed and hence doesn't look like a combobx any
105 // longer, check for it to avoid bogus assert failures
106 if ( !win->IsBeingDeleted() )
107 {
108 wxFAIL_MSG( _T("should have combo as parent") );
109 }
110 }
111 else if ( combo->MSWProcessEditMsg(message, wParam, lParam) )
112 {
113 // handled by parent
114 return 0;
115 }
116 }
117 break;
118
119 case WM_GETDLGCODE:
120 {
121 wxCHECK_MSG( win, 0, _T("should have a parent") );
122
123 if ( win->GetWindowStyle() & wxPROCESS_ENTER )
124 {
125 // need to return a custom dlg code or we'll never get it
126 return DLGC_WANTMESSAGE;
127 }
128 }
129 break;
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 ( 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 WXUINT WXUNUSED(message),
160 WXWPARAM WXUNUSED(wParam),
161 WXLPARAM WXUNUSED(lParam)
162 )
163 {
164 HDC hdc = (HDC)pDC;
165 wxColour colBack = GetBackgroundColour();
166
167 if (!IsEnabled())
168 colBack = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
169
170 ::SetBkColor(hdc, wxColourToRGB(colBack));
171 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
172
173 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
174
175 return (WXHBRUSH)brush->GetResourceHandle();
176 }
177
178 // ----------------------------------------------------------------------------
179 // wxComboBox
180 // ----------------------------------------------------------------------------
181
182 bool wxComboBox::MSWProcessEditMsg(WXUINT msg, WXWPARAM wParam, WXLPARAM lParam)
183 {
184 switch ( msg )
185 {
186 case WM_CHAR:
187 // for compatibility with wxTextCtrl, generate a special message
188 // when Enter is pressed
189 if ( wParam == VK_RETURN )
190 {
191 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
192 InitCommandEvent(event);
193 event.SetString(GetValue());
194 event.SetInt(GetSelection());
195 ProcessCommand(event);
196 }
197
198 return HandleChar(wParam, lParam, TRUE /* isASCII */);
199
200 case WM_KEYDOWN:
201 return HandleKeyDown(wParam, lParam);
202
203 case WM_KEYUP:
204 return HandleKeyUp(wParam, lParam);
205
206 case WM_SETFOCUS:
207 return HandleSetFocus((WXHWND)wParam);
208
209 case WM_KILLFOCUS:
210 return HandleKillFocus((WXHWND)wParam);
211 }
212
213 return FALSE;
214 }
215
216 bool wxComboBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
217 {
218 wxString value;
219 int sel = -1;
220 switch ( param )
221 {
222 case CBN_SELCHANGE:
223 sel = GetSelection();
224 if ( sel > -1 )
225 {
226 value = GetString(sel);
227
228 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId());
229 event.SetInt(sel);
230 event.SetEventObject(this);
231 event.SetString(value);
232 ProcessCommand(event);
233 }
234 else
235 {
236 break;
237 }
238
239 // fall through: for compability with wxGTK, also send the text
240 // update event when the selection changes (this also seems more
241 // logical as the text does change)
242
243 case CBN_EDITCHANGE:
244 {
245 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
246
247 // if sel != -1, value was initialized above (and we can't use
248 // GetValue() here as it would return the old selection and we
249 // want the new one)
250 if ( sel == -1 )
251 {
252 value = GetValue();
253 }
254 else // we're synthesizing text updated event from sel change
255 {
256 // we need to do this because the user code expects
257 // wxComboBox::GetValue() to return the new value from
258 // "text updated" handler but it hadn't been updated yet
259 SetValue(value);
260 }
261
262 event.SetString(value);
263 event.SetEventObject(this);
264 ProcessCommand(event);
265 }
266 break;
267 }
268
269 // there is no return value for the CBN_ notifications, so always return
270 // FALSE from here to pass the message to DefWindowProc()
271 return FALSE;
272 }
273
274 WXHWND wxComboBox::GetEditHWND() const
275 {
276 // this function should not be called for wxCB_READONLY controls, it is
277 // the callers responsability to check this
278 wxASSERT_MSG( !(GetWindowStyle() & wxCB_READONLY),
279 _T("read-only combobox doesn't have any edit control") );
280
281 POINT pt;
282 pt.x = pt.y = 4;
283 HWND hwndEdit = ::ChildWindowFromPoint(GetHwnd(), pt);
284 if ( !hwndEdit || hwndEdit == GetHwnd() )
285 {
286 wxFAIL_MSG(_T("not read only combobox without edit control?"));
287 }
288
289 return (WXHWND)hwndEdit;
290 }
291
292 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
293 const wxString& value,
294 const wxPoint& pos,
295 const wxSize& size,
296 int n, const wxString choices[],
297 long style,
298 const wxValidator& validator,
299 const wxString& name)
300 {
301 // pretend that wxComboBox is hidden while it is positioned and resized and
302 // show it only right before leaving this method because otherwise there is
303 // some noticeable flicker while the control rearranges itself
304 m_isShown = FALSE;
305
306 // first create wxWin object
307 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
308 return FALSE;
309
310 // get the right style
311 long msStyle = WS_TABSTOP | WS_VSCROLL | WS_HSCROLL |
312 CBS_AUTOHSCROLL | CBS_NOINTEGRALHEIGHT /* | WS_CLIPSIBLINGS */;
313 if ( style & wxCB_READONLY )
314 msStyle |= CBS_DROPDOWNLIST;
315 #ifndef __WXWINCE__
316 else if ( style & wxCB_SIMPLE )
317 msStyle |= CBS_SIMPLE; // A list (shown always) and edit control
318 #endif
319 else
320 msStyle |= CBS_DROPDOWN;
321
322 if ( style & wxCB_SORT )
323 msStyle |= CBS_SORT;
324
325 if ( style & wxCLIP_SIBLINGS )
326 msStyle |= WS_CLIPSIBLINGS;
327
328
329 // and now create the MSW control
330 if ( !MSWCreateControl(_T("COMBOBOX"), msStyle) )
331 return FALSE;
332
333 // A choice/combobox normally has a white background (or other, depending
334 // on global settings) rather than inheriting the parent's background colour.
335 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
336
337 for ( int i = 0; i < n; i++ )
338 {
339 Append(choices[i]);
340 }
341
342 if ( !value.IsEmpty() )
343 {
344 SetValue(value);
345 }
346
347 // do this after appending the values to the combobox so that autosizing
348 // works correctly
349 SetSize(pos.x, pos.y, size.x, size.y);
350
351 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
352 // and an edit control inside it and if we want to catch events from this
353 // edit control, we must subclass it as well
354 if ( !(style & wxCB_READONLY) )
355 {
356 gs_wndprocEdit = (WXFARPROC)::SetWindowLong
357 (
358 (HWND)GetEditHWND(),
359 GWL_WNDPROC,
360 (LPARAM)wxComboEditWndProc
361 );
362 }
363
364 // and finally, show the control
365 Show(TRUE);
366
367 return TRUE;
368 }
369
370 void wxComboBox::SetValue(const wxString& value)
371 {
372 if ( HasFlag(wxCB_READONLY) )
373 SetStringSelection(value);
374 else
375 SetWindowText(GetHwnd(), value.c_str());
376 }
377
378 // Clipboard operations
379 void wxComboBox::Copy()
380 {
381 SendMessage(GetHwnd(), WM_COPY, 0, 0L);
382 }
383
384 void wxComboBox::Cut()
385 {
386 SendMessage(GetHwnd(), WM_CUT, 0, 0L);
387 }
388
389 void wxComboBox::Paste()
390 {
391 SendMessage(GetHwnd(), WM_PASTE, 0, 0L);
392 }
393
394 void wxComboBox::SetEditable(bool WXUNUSED(editable))
395 {
396 // Can't implement in MSW?
397 // HWND hWnd = GetHwnd();
398 // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
399 }
400
401 void wxComboBox::SetInsertionPoint(long pos)
402 {
403 if ( GetWindowStyle() & wxCB_READONLY )
404 return;
405
406 #ifdef __WIN32__
407 HWND hWnd = GetHwnd();
408 ::SendMessage(hWnd, CB_SETEDITSEL, 0, MAKELPARAM(pos, pos));
409 HWND hEditWnd = (HWND) GetEditHWND() ;
410 if ( hEditWnd )
411 {
412 // Scroll insertion point into view
413 SendMessage(hEditWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
414 // Why is this necessary? (Copied from wxTextCtrl::SetInsertionPoint)
415 SendMessage(hEditWnd, EM_REPLACESEL, 0, (LPARAM) wxEmptyString);
416 }
417 #endif // __WIN32__
418 }
419
420 void wxComboBox::SetInsertionPointEnd()
421 {
422 // setting insertion point doesn't make sense for read only comboboxes
423 if ( !(GetWindowStyle() & wxCB_READONLY) )
424 {
425 long pos = GetLastPosition();
426 SetInsertionPoint(pos);
427 }
428 }
429
430 long wxComboBox::GetInsertionPoint() const
431 {
432 #ifdef __WIN32__
433 DWORD Pos=(DWORD)SendMessage(GetHwnd(), CB_GETEDITSEL, 0, 0L);
434 return Pos&0xFFFF;
435 #else
436 return 0;
437 #endif
438 }
439
440 long wxComboBox::GetLastPosition() const
441 {
442 HWND hEditWnd = (HWND) GetEditHWND();
443
444 // Get number of characters in the last (only) line. We'll add this to the character
445 // index for the last line, 1st position.
446 int lineLength = (int)SendMessage(hEditWnd, EM_LINELENGTH, (WPARAM) 0, (LPARAM)0L);
447
448 return (long)(lineLength);
449 }
450
451 void wxComboBox::Replace(long from, long to, const wxString& value)
452 {
453 #if wxUSE_CLIPBOARD
454 Remove(from, to);
455
456 // Now replace with 'value', by pasting.
457 wxSetClipboardData(wxDF_TEXT, (wxObject *)(const wxChar *)value, 0, 0);
458
459 // Paste into edit control
460 SendMessage(GetHwnd(), WM_PASTE, (WPARAM)0, (LPARAM)0L);
461 #endif
462 }
463
464 void wxComboBox::Remove(long from, long to)
465 {
466 // Set selection and remove it
467 SetSelection(from, to);
468 SendMessage(GetHwnd(), WM_CUT, (WPARAM)0, (LPARAM)0);
469 }
470
471 void wxComboBox::SetSelection(long from, long to)
472 {
473 HWND hWnd = GetHwnd();
474 long fromChar = from;
475 long toChar = to;
476 // if from and to are both -1, it means
477 // (in wxWindows) that all text should be selected.
478 // This translates into Windows convention
479 if ((from == -1) && (to == -1))
480 {
481 fromChar = 0;
482 toChar = -1;
483 }
484
485 if (
486 #ifdef __WIN32__
487 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar))
488 #else // Win16
489 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)fromChar, (LPARAM)toChar)
490 #endif
491 == CB_ERR )
492 {
493 wxLogDebug(_T("CB_SETEDITSEL failed"));
494 }
495 }
496
497 #endif
498 // wxUSE_COMBOBOX
499