1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/combobox.cpp
3 // Purpose: wxComboBox class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "combobox.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/settings.h"
36 // for wxEVT_COMMAND_TEXT_ENTER
37 #include "wx/textctrl.h"
40 #include "wx/combobox.h"
42 #include "wx/clipbrd.h"
43 #include "wx/msw/private.h"
46 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
49 #include "wx/tooltip.h"
50 #endif // wxUSE_TOOLTIPS
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
66 // ----------------------------------------------------------------------------
67 // function prototypes
68 // ----------------------------------------------------------------------------
70 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
75 // ---------------------------------------------------------------------------
77 // ---------------------------------------------------------------------------
79 // the pointer to standard radio button wnd proc
80 static WXFARPROC gs_wndprocEdit
= (WXFARPROC
)NULL
;
82 // ============================================================================
84 // ============================================================================
86 // ----------------------------------------------------------------------------
87 // wnd proc for subclassed edit control
88 // ----------------------------------------------------------------------------
90 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
95 HWND hwndCombo
= ::GetParent(hWnd
);
96 wxWindow
*win
= wxFindWinFromHandle((WXHWND
)hwndCombo
);
100 // forward some messages to the combobox to generate the appropriate
101 // wxEvents from them
108 wxComboBox
*combo
= wxDynamicCast(win
, wxComboBox
);
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() )
116 wxFAIL_MSG( _T("should have combo as parent") );
119 else if ( combo
->MSWProcessEditMsg(message
, wParam
, lParam
) )
129 wxCHECK_MSG( win
, 0, _T("should have a parent") );
131 if ( win
->GetWindowStyle() & wxPROCESS_ENTER
)
133 // need to return a custom dlg code or we'll never get it
134 return DLGC_WANTMESSAGE
;
139 // deal with tooltips here
140 #if wxUSE_TOOLTIPS && defined(TTN_NEEDTEXT)
143 wxCHECK_MSG( win
, 0, _T("should have a parent") );
145 NMHDR
* hdr
= (NMHDR
*)lParam
;
146 if ( hdr
->code
== TTN_NEEDTEXT
)
148 wxToolTip
*tooltip
= win
->GetToolTip();
151 TOOLTIPTEXT
*ttt
= (TOOLTIPTEXT
*)lParam
;
152 ttt
->lpszText
= (wxChar
*)tooltip
->GetTip().c_str();
160 #endif // wxUSE_TOOLTIPS
163 return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit
, hWnd
, message
, wParam
, lParam
);
166 WXHBRUSH
wxComboBox::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
167 WXUINT
WXUNUSED(message
),
168 WXWPARAM
WXUNUSED(wParam
),
169 WXLPARAM
WXUNUSED(lParam
)
173 wxColour colBack
= GetBackgroundColour();
176 colBack
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
178 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
179 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
181 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
183 return (WXHBRUSH
)brush
->GetResourceHandle();
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
190 bool wxComboBox::MSWProcessEditMsg(WXUINT msg
, WXWPARAM wParam
, WXLPARAM lParam
)
195 // for compatibility with wxTextCtrl, generate a special message
196 // when Enter is pressed
197 if ( wParam
== VK_RETURN
)
199 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
200 InitCommandEvent(event
);
201 event
.SetString(GetValue());
202 event
.SetInt(GetSelection());
203 ProcessCommand(event
);
206 return HandleChar(wParam
, lParam
, TRUE
/* isASCII */);
209 return HandleKeyDown(wParam
, lParam
);
212 return HandleKeyUp(wParam
, lParam
);
215 return HandleSetFocus((WXHWND
)wParam
);
218 return HandleKillFocus((WXHWND
)wParam
);
224 bool wxComboBox::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
231 sel
= GetSelection();
234 value
= GetString(sel
);
236 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId());
238 event
.SetEventObject(this);
239 event
.SetString(value
);
240 ProcessCommand(event
);
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)
253 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
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
262 else // we're synthesizing text updated event from sel change
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
270 event
.SetString(value
);
271 event
.SetEventObject(this);
272 ProcessCommand(event
);
277 // there is no return value for the CBN_ notifications, so always return
278 // FALSE from here to pass the message to DefWindowProc()
282 WXHWND
wxComboBox::GetEditHWND() const
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") );
291 HWND hwndEdit
= ::ChildWindowFromPoint(GetHwnd(), pt
);
292 if ( !hwndEdit
|| hwndEdit
== GetHwnd() )
294 wxFAIL_MSG(_T("not read only combobox without edit control?"));
297 return (WXHWND
)hwndEdit
;
300 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
301 const wxString
& value
,
304 int n
, const wxString choices
[],
306 const wxValidator
& validator
,
307 const wxString
& name
)
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
314 // first create wxWin object
315 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
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
;
324 else if ( style
& wxCB_SIMPLE
)
325 msStyle
|= CBS_SIMPLE
; // A list (shown always) and edit control
328 msStyle
|= CBS_DROPDOWN
;
330 if ( style
& wxCB_SORT
)
333 if ( style
& wxCLIP_SIBLINGS
)
334 msStyle
|= WS_CLIPSIBLINGS
;
337 // and now create the MSW control
338 if ( !MSWCreateControl(_T("COMBOBOX"), msStyle
) )
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
));
345 for ( int i
= 0; i
< n
; i
++ )
350 if ( !value
.IsEmpty() )
355 // do this after appending the values to the combobox so that autosizing
357 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
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
) )
364 gs_wndprocEdit
= (WXFARPROC
)::SetWindowLong
368 (LPARAM
)wxComboEditWndProc
372 // and finally, show the control
378 void wxComboBox::SetValue(const wxString
& value
)
380 if ( HasFlag(wxCB_READONLY
) )
381 SetStringSelection(value
);
383 SetWindowText(GetHwnd(), value
.c_str());
386 // Clipboard operations
387 void wxComboBox::Copy()
389 SendMessage(GetHwnd(), WM_COPY
, 0, 0L);
392 void wxComboBox::Cut()
394 SendMessage(GetHwnd(), WM_CUT
, 0, 0L);
397 void wxComboBox::Paste()
399 SendMessage(GetHwnd(), WM_PASTE
, 0, 0L);
402 void wxComboBox::SetEditable(bool WXUNUSED(editable
))
404 // Can't implement in MSW?
405 // HWND hWnd = GetHwnd();
406 // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
409 void wxComboBox::SetInsertionPoint(long pos
)
411 if ( GetWindowStyle() & wxCB_READONLY
)
415 HWND hWnd
= GetHwnd();
416 ::SendMessage(hWnd
, CB_SETEDITSEL
, 0, MAKELPARAM(pos
, pos
));
417 HWND hEditWnd
= (HWND
) GetEditHWND() ;
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
);
428 void wxComboBox::SetInsertionPointEnd()
430 // setting insertion point doesn't make sense for read only comboboxes
431 if ( !(GetWindowStyle() & wxCB_READONLY
) )
433 long pos
= GetLastPosition();
434 SetInsertionPoint(pos
);
438 long wxComboBox::GetInsertionPoint() const
441 DWORD Pos
=(DWORD
)SendMessage(GetHwnd(), CB_GETEDITSEL
, 0, 0L);
448 long wxComboBox::GetLastPosition() const
450 HWND hEditWnd
= (HWND
) GetEditHWND();
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);
456 return (long)(lineLength
);
459 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
464 // Now replace with 'value', by pasting.
465 wxSetClipboardData(wxDF_TEXT
, (wxObject
*)(const wxChar
*)value
, 0, 0);
467 // Paste into edit control
468 SendMessage(GetHwnd(), WM_PASTE
, (WPARAM
)0, (LPARAM
)0L);
472 void wxComboBox::Remove(long from
, long to
)
474 // Set selection and remove it
475 SetSelection(from
, to
);
476 SendMessage(GetHwnd(), WM_CUT
, (WPARAM
)0, (LPARAM
)0);
479 void wxComboBox::SetSelection(long from
, long to
)
481 HWND hWnd
= GetHwnd();
482 long fromChar
= from
;
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))
495 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
))
497 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)fromChar
, (LPARAM
)toChar
)
501 wxLogDebug(_T("CB_SETEDITSEL failed"));