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 #if wxUSE_EXTENDED_RTTI
57 IMPLEMENT_DYNAMIC_CLASS_XTI(wxComboBox
, wxControl
,"wx/combobox.h")
59 WX_BEGIN_PROPERTIES_TABLE(wxComboBox
)
61 WX_PROPERTY( Font
, wxFont
, SetFont
, GetFont
, )
62 WX_PROPERTY_COLLECTION( Choices
, wxArrayString
, wxString
, AppendString
, GetStrings
)
63 WX_PROPERTY( Value
,wxString
, SetValue
, GetValue
, )
64 WX_PROPERTY( Selection
,int, SetSelection
, GetSelection
, )
65 WX_END_PROPERTIES_TABLE()
67 WX_BEGIN_HANDLERS_TABLE(wxComboBox
)
68 WX_END_HANDLERS_TABLE()
70 WX_CONSTRUCTOR_5( wxComboBox
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Value
, wxPoint
, Position
, wxSize
, Size
)
72 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
75 // ----------------------------------------------------------------------------
76 // function prototypes
77 // ----------------------------------------------------------------------------
79 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
84 // ---------------------------------------------------------------------------
86 // ---------------------------------------------------------------------------
88 // the pointer to standard radio button wnd proc
89 static WXFARPROC gs_wndprocEdit
= (WXFARPROC
)NULL
;
91 // ============================================================================
93 // ============================================================================
95 // ----------------------------------------------------------------------------
96 // wnd proc for subclassed edit control
97 // ----------------------------------------------------------------------------
99 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
104 HWND hwndCombo
= ::GetParent(hWnd
);
105 wxWindow
*win
= wxFindWinFromHandle((WXHWND
)hwndCombo
);
109 // forward some messages to the combobox to generate the appropriate
110 // wxEvents from them
117 wxComboBox
*combo
= wxDynamicCast(win
, wxComboBox
);
120 // we can get WM_KILLFOCUS while our parent is already half
121 // destroyed and hence doesn't look like a combobx any
122 // longer, check for it to avoid bogus assert failures
123 if ( !win
->IsBeingDeleted() )
125 wxFAIL_MSG( _T("should have combo as parent") );
128 else if ( combo
->MSWProcessEditMsg(message
, wParam
, lParam
) )
138 wxCHECK_MSG( win
, 0, _T("should have a parent") );
140 if ( win
->GetWindowStyle() & wxPROCESS_ENTER
)
142 // need to return a custom dlg code or we'll never get it
143 return DLGC_WANTMESSAGE
;
148 // deal with tooltips here
149 #if wxUSE_TOOLTIPS && defined(TTN_NEEDTEXT)
152 wxCHECK_MSG( win
, 0, _T("should have a parent") );
154 NMHDR
* hdr
= (NMHDR
*)lParam
;
155 if ( hdr
->code
== TTN_NEEDTEXT
)
157 wxToolTip
*tooltip
= win
->GetToolTip();
160 TOOLTIPTEXT
*ttt
= (TOOLTIPTEXT
*)lParam
;
161 ttt
->lpszText
= (wxChar
*)tooltip
->GetTip().c_str();
169 #endif // wxUSE_TOOLTIPS
172 return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit
, hWnd
, message
, wParam
, lParam
);
175 WXHBRUSH
wxComboBox::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
176 WXUINT
WXUNUSED(message
),
177 WXWPARAM
WXUNUSED(wParam
),
178 WXLPARAM
WXUNUSED(lParam
)
182 wxColour colBack
= GetBackgroundColour();
185 colBack
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
187 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
188 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
190 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
192 return (WXHBRUSH
)brush
->GetResourceHandle();
195 // ----------------------------------------------------------------------------
197 // ----------------------------------------------------------------------------
199 bool wxComboBox::MSWProcessEditMsg(WXUINT msg
, WXWPARAM wParam
, WXLPARAM lParam
)
204 // for compatibility with wxTextCtrl, generate a special message
205 // when Enter is pressed
206 if ( wParam
== VK_RETURN
)
208 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
209 InitCommandEvent(event
);
210 event
.SetString(GetValue());
211 event
.SetInt(GetSelection());
212 ProcessCommand(event
);
215 return HandleChar(wParam
, lParam
, TRUE
/* isASCII */);
218 return HandleKeyDown(wParam
, lParam
);
221 return HandleKeyUp(wParam
, lParam
);
224 return HandleSetFocus((WXHWND
)wParam
);
227 return HandleKillFocus((WXHWND
)wParam
);
233 bool wxComboBox::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
240 sel
= GetSelection();
243 value
= GetString(sel
);
245 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId());
247 event
.SetEventObject(this);
248 event
.SetString(value
);
249 ProcessCommand(event
);
256 // fall through: for compability with wxGTK, also send the text
257 // update event when the selection changes (this also seems more
258 // logical as the text does change)
262 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
264 // if sel != -1, value was initialized above (and we can't use
265 // GetValue() here as it would return the old selection and we
271 else // we're synthesizing text updated event from sel change
273 // we need to do this because the user code expects
274 // wxComboBox::GetValue() to return the new value from
275 // "text updated" handler but it hadn't been updated yet
279 event
.SetString(value
);
280 event
.SetEventObject(this);
281 ProcessCommand(event
);
286 // there is no return value for the CBN_ notifications, so always return
287 // FALSE from here to pass the message to DefWindowProc()
291 WXHWND
wxComboBox::GetEditHWND() const
293 // this function should not be called for wxCB_READONLY controls, it is
294 // the callers responsability to check this
295 wxASSERT_MSG( !(GetWindowStyle() & wxCB_READONLY
),
296 _T("read-only combobox doesn't have any edit control") );
300 HWND hwndEdit
= ::ChildWindowFromPoint(GetHwnd(), pt
);
301 if ( !hwndEdit
|| hwndEdit
== GetHwnd() )
303 wxFAIL_MSG(_T("not read only combobox without edit control?"));
306 return (WXHWND
)hwndEdit
;
309 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
310 const wxString
& value
,
313 int n
, const wxString choices
[],
315 const wxValidator
& validator
,
316 const wxString
& name
)
318 // pretend that wxComboBox is hidden while it is positioned and resized and
319 // show it only right before leaving this method because otherwise there is
320 // some noticeable flicker while the control rearranges itself
323 // first create wxWin object
324 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
327 // get the right style
328 long msStyle
= WS_TABSTOP
| WS_VSCROLL
| WS_HSCROLL
|
329 CBS_AUTOHSCROLL
| CBS_NOINTEGRALHEIGHT
/* | WS_CLIPSIBLINGS */;
330 if ( style
& wxCB_READONLY
)
331 msStyle
|= CBS_DROPDOWNLIST
;
333 else if ( style
& wxCB_SIMPLE
)
334 msStyle
|= CBS_SIMPLE
; // A list (shown always) and edit control
337 msStyle
|= CBS_DROPDOWN
;
339 if ( style
& wxCB_SORT
)
342 if ( style
& wxCLIP_SIBLINGS
)
343 msStyle
|= WS_CLIPSIBLINGS
;
346 // and now create the MSW control
347 if ( !MSWCreateControl(_T("COMBOBOX"), msStyle
) )
350 // A choice/combobox normally has a white background (or other, depending
351 // on global settings) rather than inheriting the parent's background colour.
352 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
354 for ( int i
= 0; i
< n
; i
++ )
359 if ( !value
.IsEmpty() )
364 // do this after appending the values to the combobox so that autosizing
366 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
368 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
369 // and an edit control inside it and if we want to catch events from this
370 // edit control, we must subclass it as well
371 if ( !(style
& wxCB_READONLY
) )
373 gs_wndprocEdit
= (WXFARPROC
)::SetWindowLong
377 (LPARAM
)wxComboEditWndProc
381 // and finally, show the control
387 void wxComboBox::SetValue(const wxString
& value
)
389 if ( HasFlag(wxCB_READONLY
) )
390 SetStringSelection(value
);
392 SetWindowText(GetHwnd(), value
.c_str());
395 // Clipboard operations
396 void wxComboBox::Copy()
398 SendMessage(GetHwnd(), WM_COPY
, 0, 0L);
401 void wxComboBox::Cut()
403 SendMessage(GetHwnd(), WM_CUT
, 0, 0L);
406 void wxComboBox::Paste()
408 SendMessage(GetHwnd(), WM_PASTE
, 0, 0L);
411 void wxComboBox::SetEditable(bool WXUNUSED(editable
))
413 // Can't implement in MSW?
414 // HWND hWnd = GetHwnd();
415 // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
418 void wxComboBox::SetInsertionPoint(long pos
)
420 if ( GetWindowStyle() & wxCB_READONLY
)
424 HWND hWnd
= GetHwnd();
425 ::SendMessage(hWnd
, CB_SETEDITSEL
, 0, MAKELPARAM(pos
, pos
));
426 HWND hEditWnd
= (HWND
) GetEditHWND() ;
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
) wxEmptyString
);
437 void wxComboBox::SetInsertionPointEnd()
439 // setting insertion point doesn't make sense for read only comboboxes
440 if ( !(GetWindowStyle() & wxCB_READONLY
) )
442 long pos
= GetLastPosition();
443 SetInsertionPoint(pos
);
447 long wxComboBox::GetInsertionPoint() const
450 DWORD Pos
=(DWORD
)SendMessage(GetHwnd(), CB_GETEDITSEL
, 0, 0L);
457 long wxComboBox::GetLastPosition() const
459 HWND hEditWnd
= (HWND
) GetEditHWND();
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);
465 return (long)(lineLength
);
468 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
473 // Now replace with 'value', by pasting.
474 wxSetClipboardData(wxDF_TEXT
, (wxObject
*)(const wxChar
*)value
, 0, 0);
476 // Paste into edit control
477 SendMessage(GetHwnd(), WM_PASTE
, (WPARAM
)0, (LPARAM
)0L);
481 void wxComboBox::Remove(long from
, long to
)
483 // Set selection and remove it
484 SetSelection(from
, to
);
485 SendMessage(GetHwnd(), WM_CUT
, (WPARAM
)0, (LPARAM
)0);
488 void wxComboBox::SetSelection(long from
, long to
)
490 HWND hWnd
= GetHwnd();
491 long fromChar
= from
;
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))
504 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
))
506 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)fromChar
, (LPARAM
)toChar
)
510 wxLogDebug(_T("CB_SETEDITSEL failed"));