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 // ----------------------------------------------------------------------------
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
)
58 // ----------------------------------------------------------------------------
59 // function prototypes
60 // ----------------------------------------------------------------------------
62 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
67 // ---------------------------------------------------------------------------
69 // ---------------------------------------------------------------------------
71 // the pointer to standard radio button wnd proc
72 static WXFARPROC gs_wndprocEdit
= (WXFARPROC
)NULL
;
74 // ============================================================================
76 // ============================================================================
78 // ----------------------------------------------------------------------------
79 // wnd proc for subclassed edit control
80 // ----------------------------------------------------------------------------
82 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
87 HWND hwndCombo
= ::GetParent(hWnd
);
88 wxWindow
*win
= wxFindWinFromHandle((WXHWND
)hwndCombo
);
92 // forward some messages to the combobox to generate the appropriate
100 wxComboBox
*combo
= wxDynamicCast(win
, wxComboBox
);
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() )
108 wxFAIL_MSG( _T("should have combo as parent") );
111 else if ( combo
->MSWProcessEditMsg(message
, wParam
, lParam
) )
121 wxCHECK_MSG( win
, 0, _T("should have a parent") );
123 if ( win
->GetWindowStyle() & wxPROCESS_ENTER
)
125 // need to return a custom dlg code or we'll never get it
126 return DLGC_WANTMESSAGE
;
131 // deal with tooltips here
132 #if wxUSE_TOOLTIPS && defined(TTN_NEEDTEXT)
135 wxCHECK_MSG( win
, 0, _T("should have a parent") );
137 NMHDR
* hdr
= (NMHDR
*)lParam
;
138 if ( hdr
->code
== TTN_NEEDTEXT
)
140 wxToolTip
*tooltip
= win
->GetToolTip();
143 TOOLTIPTEXT
*ttt
= (TOOLTIPTEXT
*)lParam
;
144 ttt
->lpszText
= (wxChar
*)tooltip
->GetTip().c_str();
152 #endif // wxUSE_TOOLTIPS
155 return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit
, hWnd
, message
, wParam
, lParam
);
158 WXHBRUSH
wxComboBox::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
159 WXUINT
WXUNUSED(message
),
160 WXWPARAM
WXUNUSED(wParam
),
161 WXLPARAM
WXUNUSED(lParam
)
165 if (GetParent()->GetTransparentBackground())
166 SetBkMode(hdc
, TRANSPARENT
);
168 SetBkMode(hdc
, OPAQUE
);
170 wxColour colBack
= GetBackgroundColour();
173 colBack
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
175 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
176 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
178 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
180 return (WXHBRUSH
)brush
->GetResourceHandle();
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
187 bool wxComboBox::MSWProcessEditMsg(WXUINT msg
, WXWPARAM wParam
, WXLPARAM lParam
)
192 // for compatibility with wxTextCtrl, generate a special message
193 // when Enter is pressed
194 if ( wParam
== VK_RETURN
)
196 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
197 InitCommandEvent(event
);
198 event
.SetString(GetValue());
199 event
.SetInt(GetSelection());
200 ProcessCommand(event
);
203 return HandleChar(wParam
, lParam
, TRUE
/* isASCII */);
206 return HandleKeyDown(wParam
, lParam
);
209 return HandleKeyUp(wParam
, lParam
);
212 return HandleSetFocus((WXHWND
)wParam
);
215 return HandleKillFocus((WXHWND
)wParam
);
221 bool wxComboBox::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
228 sel
= GetSelection();
231 value
= GetString(sel
);
233 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId());
235 event
.SetEventObject(this);
236 event
.SetString(value
);
237 ProcessCommand(event
);
244 // fall through: for compability with wxGTK, also send the text
245 // update event when the selection changes (this also seems more
246 // logical as the text does change)
250 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
252 // if sel != -1, value was initialized above (and we can't use
253 // GetValue() here as it would return the old selection and we
259 else // we're synthesizing text updated event from sel change
261 // we need to do this because the user code expects
262 // wxComboBox::GetValue() to return the new value from
263 // "text updated" handler but it hadn't been updated yet
267 event
.SetString(value
);
268 event
.SetEventObject(this);
269 ProcessCommand(event
);
274 // there is no return value for the CBN_ notifications, so always return
275 // FALSE from here to pass the message to DefWindowProc()
279 WXHWND
wxComboBox::GetEditHWND() const
281 // this function should not be called for wxCB_READONLY controls, it is
282 // the callers responsability to check this
283 wxASSERT_MSG( !(GetWindowStyle() & wxCB_READONLY
),
284 _T("read-only combobox doesn't have any edit control") );
288 HWND hwndEdit
= ::ChildWindowFromPoint(GetHwnd(), pt
);
289 if ( !hwndEdit
|| hwndEdit
== GetHwnd() )
291 wxFAIL_MSG(_T("not read only combobox without edit control?"));
294 return (WXHWND
)hwndEdit
;
297 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
298 const wxString
& value
,
301 int n
, const wxString choices
[],
303 const wxValidator
& validator
,
304 const wxString
& name
)
306 // pretend that wxComboBox is hidden while it is positioned and resized and
307 // show it only right before leaving this method because otherwise there is
308 // some noticeable flicker while the control rearranges itself
311 // first create wxWin object
312 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
315 // get the right style
316 long msStyle
= WS_TABSTOP
| WS_VSCROLL
| WS_HSCROLL
|
317 CBS_AUTOHSCROLL
| CBS_NOINTEGRALHEIGHT
/* | WS_CLIPSIBLINGS */;
318 if ( style
& wxCB_READONLY
)
319 msStyle
|= CBS_DROPDOWNLIST
;
320 else if ( style
& wxCB_SIMPLE
)
321 msStyle
|= CBS_SIMPLE
; // A list (shown always) and edit control
323 msStyle
|= CBS_DROPDOWN
;
325 if ( style
& wxCB_SORT
)
328 if ( style
& wxCLIP_SIBLINGS
)
329 msStyle
|= WS_CLIPSIBLINGS
;
332 // and now create the MSW control
333 if ( !MSWCreateControl(_T("COMBOBOX"), msStyle
) )
336 // A choice/combobox normally has a white background (or other, depending
337 // on global settings) rather than inheriting the parent's background colour.
338 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
340 for ( int i
= 0; i
< n
; i
++ )
345 if ( !value
.IsEmpty() )
350 // do this after appending the values to the combobox so that autosizing
352 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
354 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
355 // and an edit control inside it and if we want to catch events from this
356 // edit control, we must subclass it as well
357 if ( !(style
& wxCB_READONLY
) )
359 gs_wndprocEdit
= (WXFARPROC
)::SetWindowLong
363 (LPARAM
)wxComboEditWndProc
367 // and finally, show the control
373 void wxComboBox::SetValue(const wxString
& value
)
375 if ( HasFlag(wxCB_READONLY
) )
376 SetStringSelection(value
);
378 SetWindowText(GetHwnd(), value
.c_str());
381 // Clipboard operations
382 void wxComboBox::Copy()
384 SendMessage(GetHwnd(), WM_COPY
, 0, 0L);
387 void wxComboBox::Cut()
389 SendMessage(GetHwnd(), WM_CUT
, 0, 0L);
392 void wxComboBox::Paste()
394 SendMessage(GetHwnd(), WM_PASTE
, 0, 0L);
397 void wxComboBox::SetEditable(bool WXUNUSED(editable
))
399 // Can't implement in MSW?
400 // HWND hWnd = GetHwnd();
401 // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
404 void wxComboBox::SetInsertionPoint(long pos
)
406 if ( GetWindowStyle() & wxCB_READONLY
)
410 HWND hWnd
= GetHwnd();
411 ::SendMessage(hWnd
, CB_SETEDITSEL
, 0, MAKELPARAM(pos
, pos
));
412 HWND hEditWnd
= (HWND
) GetEditHWND() ;
415 // Scroll insertion point into view
416 SendMessage(hEditWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
417 // Why is this necessary? (Copied from wxTextCtrl::SetInsertionPoint)
418 SendMessage(hEditWnd
, EM_REPLACESEL
, 0, (LPARAM
)_T(""));
423 void wxComboBox::SetInsertionPointEnd()
425 // setting insertion point doesn't make sense for read only comboboxes
426 if ( !(GetWindowStyle() & wxCB_READONLY
) )
428 long pos
= GetLastPosition();
429 SetInsertionPoint(pos
);
433 long wxComboBox::GetInsertionPoint() const
436 DWORD Pos
=(DWORD
)SendMessage(GetHwnd(), CB_GETEDITSEL
, 0, 0L);
443 long wxComboBox::GetLastPosition() const
445 HWND hEditWnd
= (HWND
) GetEditHWND();
447 // Get number of characters in the last (only) line. We'll add this to the character
448 // index for the last line, 1st position.
449 int lineLength
= (int)SendMessage(hEditWnd
, EM_LINELENGTH
, (WPARAM
) 0, (LPARAM
)0L);
451 return (long)(lineLength
);
454 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
459 // Now replace with 'value', by pasting.
460 wxSetClipboardData(wxDF_TEXT
, (wxObject
*)(const wxChar
*)value
, 0, 0);
462 // Paste into edit control
463 SendMessage(GetHwnd(), WM_PASTE
, (WPARAM
)0, (LPARAM
)0L);
467 void wxComboBox::Remove(long from
, long to
)
469 // Set selection and remove it
470 SetSelection(from
, to
);
471 SendMessage(GetHwnd(), WM_CUT
, (WPARAM
)0, (LPARAM
)0);
474 void wxComboBox::SetSelection(long from
, long to
)
476 HWND hWnd
= GetHwnd();
477 long fromChar
= from
;
479 // if from and to are both -1, it means
480 // (in wxWindows) that all text should be selected.
481 // This translates into Windows convention
482 if ((from
== -1) && (to
== -1))
490 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
))
492 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)fromChar
, (LPARAM
)toChar
)
496 wxLogDebug(_T("CB_SETEDITSEL failed"));