1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/combobox.cpp
3 // Purpose: wxComboBox class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "combobox.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/settings.h"
38 #include "wx/combobox.h"
40 #include "wx/clipbrd.h"
41 #include "wx/msw/private.h"
44 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
47 #include "wx/tooltip.h"
48 #endif // wxUSE_TOOLTIPS
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
56 // ----------------------------------------------------------------------------
57 // function prototypes
58 // ----------------------------------------------------------------------------
60 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
65 // ---------------------------------------------------------------------------
67 // ---------------------------------------------------------------------------
69 // the pointer to standard radio button wnd proc
70 static WXFARPROC gs_wndprocEdit
= (WXFARPROC
)NULL
;
72 // ============================================================================
74 // ============================================================================
76 // ----------------------------------------------------------------------------
77 // wnd proc for subclassed edit control
78 // ----------------------------------------------------------------------------
80 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
85 HWND hwndCombo
= ::GetParent(hWnd
);
86 wxWindow
*win
= wxFindWinFromHandle((WXHWND
)hwndCombo
);
90 // forward some messages to the combobox to generate the appropriate
98 wxComboBox
*combo
= wxDynamicCast(win
, wxComboBox
);
99 wxCHECK_MSG( combo
, 0, _T("should have combo as parent") );
101 if ( combo
->MSWProcessEditMsg(message
, wParam
, lParam
) )
109 wxCHECK_MSG( win
, 0, _T("should have a parent") );
111 if ( win
->GetWindowStyle() & wxPROCESS_ENTER
)
113 // need to return a custom dlg code or we'll never get it
114 return DLGC_WANTMESSAGE
;
120 // deal with tooltips here
121 #if wxUSE_TOOLTIPS && defined(TTN_NEEDTEXT)
124 wxCHECK_MSG( win
, 0, _T("should have a parent") );
126 NMHDR
* hdr
= (NMHDR
*)lParam
;
127 if ( (int)hdr
->code
== TTN_NEEDTEXT
)
129 wxToolTip
*tooltip
= win
->GetToolTip();
132 TOOLTIPTEXT
*ttt
= (TOOLTIPTEXT
*)lParam
;
133 ttt
->lpszText
= (wxChar
*)tooltip
->GetTip().c_str();
141 #endif // wxUSE_TOOLTIPS
144 return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit
, hWnd
, message
, wParam
, lParam
);
147 WXHBRUSH
wxComboBox::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
153 WXUINT
WXUNUSED(message
),
154 WXWPARAM
WXUNUSED(wParam
),
155 WXLPARAM
WXUNUSED(lParam
)
162 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
163 return (WXHBRUSH
) hbrush
;
165 #endif // wxUSE_CTL3D
168 if (GetParent()->GetTransparentBackground())
169 SetBkMode(hdc
, TRANSPARENT
);
171 SetBkMode(hdc
, OPAQUE
);
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 return HandleChar(wParam
, lParam
, TRUE
/* isASCII */);
198 return HandleKeyDown(wParam
, lParam
);
201 return HandleKeyUp(wParam
, lParam
);
204 return HandleSetFocus((WXHWND
)wParam
);
207 return HandleKillFocus((WXHWND
)wParam
);
213 bool wxComboBox::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
220 sel
= GetSelection();
223 value
= GetString(sel
);
225 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId());
227 event
.SetEventObject(this);
228 event
.SetString(value
);
229 ProcessCommand(event
);
236 // fall through: for compability with wxGTK, also send the text
237 // update event when the selection changes (this also seems more
238 // logical as the text does change)
242 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
244 // if sel != -1, value was initialized above (and we can't use
245 // GetValue() here as it would return the old selection and we
251 else // we're synthesizing text updated event from sel change
253 // we need to do this because the user code expects
254 // wxComboBox::GetValue() to return the new value from
255 // "text updated" handler but it hadn't been updated yet
259 event
.SetString(value
);
260 event
.SetEventObject(this);
261 ProcessCommand(event
);
266 // there is no return value for the CBN_ notifications, so always return
267 // FALSE from here to pass the message to DefWindowProc()
271 WXHWND
wxComboBox::GetEditHWND() const
273 // this function should not be called for wxCB_READONLY controls, it is
274 // the callers responsability to check this
275 wxASSERT_MSG( !(GetWindowStyle() & wxCB_READONLY
),
276 _T("read-only combobox doesn't have any edit control") );
280 HWND hwndEdit
= ::ChildWindowFromPoint(GetHwnd(), pt
);
281 if ( !hwndEdit
|| hwndEdit
== GetHwnd() )
283 wxFAIL_MSG(_T("not read only combobox without edit control?"));
286 return (WXHWND
)hwndEdit
;
289 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
290 const wxString
& value
,
293 int n
, const wxString choices
[],
295 const wxValidator
& validator
,
296 const wxString
& name
)
298 // first create wxWin object
299 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
302 // get the right style
303 long msStyle
= WS_TABSTOP
| WS_VSCROLL
| WS_HSCROLL
|
304 CBS_AUTOHSCROLL
| CBS_NOINTEGRALHEIGHT
/* | WS_CLIPSIBLINGS */;
305 if ( style
& wxCB_READONLY
)
306 msStyle
|= CBS_DROPDOWNLIST
;
307 else if ( style
& wxCB_SIMPLE
)
308 msStyle
|= CBS_SIMPLE
; // A list (shown always) and edit control
310 msStyle
|= CBS_DROPDOWN
;
312 if ( style
& wxCB_SORT
)
315 if ( style
& wxCLIP_SIBLINGS
)
316 msStyle
|= WS_CLIPSIBLINGS
;
319 // and now create the MSW control
320 if ( !MSWCreateControl(_T("COMBOBOX"), msStyle
) )
323 // A choice/combobox normally has a white background (or other, depending
324 // on global settings) rather than inheriting the parent's background colour.
325 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
327 for ( int i
= 0; i
< n
; i
++ )
332 if ( !value
.IsEmpty() )
337 // do this after appending the values to the combobox so that autosizing
339 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
341 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
342 // and an edit control inside it and if we want to catch events from this
343 // edit control, we must subclass it as well
344 if ( !(style
& wxCB_READONLY
) )
346 gs_wndprocEdit
= (WXFARPROC
)::SetWindowLong
350 (LPARAM
)wxComboEditWndProc
357 // TODO: update and clear all this horrible mess (VZ)
359 void wxComboBox::SetValue(const wxString
& value
)
361 // If newlines are denoted by just 10, must stick 13 in front.
363 int len
= value
.Length();
365 for (i
= 0; i
< len
; i
++)
367 if ((i
> 0) && (value
[i
] == 10) && (value
[i
-1] != 13))
372 wxChar
*tmp
= new wxChar
[len
+ singletons
+ 1];
374 for (i
= 0; i
< len
; i
++)
376 if ((i
> 0) && (value
[i
] == 10) && (value
[i
-1] != 13))
385 SetWindowText(GetHwnd(), tmp
);
389 SetWindowText(GetHwnd(), value
);
392 // Clipboard operations
393 void wxComboBox::Copy()
395 HWND hWnd
= GetHwnd();
396 SendMessage(hWnd
, WM_COPY
, 0, 0L);
399 void wxComboBox::Cut()
401 HWND hWnd
= GetHwnd();
402 SendMessage(hWnd
, WM_CUT
, 0, 0L);
405 void wxComboBox::Paste()
407 HWND hWnd
= GetHwnd();
408 SendMessage(hWnd
, 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
)_T(""));
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"));