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
95 wxComboBox
*combo
= wxDynamicCast(win
, wxComboBox
);
96 wxCHECK_MSG( combo
, 0, _T("should have combo as parent") );
98 if ( combo
->MSWProcessEditMsg(message
, wParam
, lParam
) )
106 wxCHECK_MSG( win
, 0, _T("should have a parent") );
108 if ( win
->GetWindowStyle() & wxPROCESS_ENTER
)
110 // need to return a custom dlg code or we'll never get it
111 return DLGC_WANTMESSAGE
;
117 // deal with tooltips here
118 #if wxUSE_TOOLTIPS && defined(TTN_NEEDTEXT)
121 wxCHECK_MSG( win
, 0, _T("should have a parent") );
123 NMHDR
* hdr
= (NMHDR
*)lParam
;
124 if ( (int)hdr
->code
== TTN_NEEDTEXT
)
126 wxToolTip
*tooltip
= win
->GetToolTip();
129 TOOLTIPTEXT
*ttt
= (TOOLTIPTEXT
*)lParam
;
130 ttt
->lpszText
= (wxChar
*)tooltip
->GetTip().c_str();
138 #endif // wxUSE_TOOLTIPS
141 return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit
, hWnd
, message
, wParam
, lParam
);
144 WXHBRUSH
wxComboBox::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
150 WXUINT
WXUNUSED(message
),
151 WXWPARAM
WXUNUSED(wParam
),
152 WXLPARAM
WXUNUSED(lParam
)
159 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
160 return (WXHBRUSH
) hbrush
;
162 #endif // wxUSE_CTL3D
165 if (GetParent()->GetTransparentBackground())
166 SetBkMode(hdc
, TRANSPARENT
);
168 SetBkMode(hdc
, OPAQUE
);
170 wxColour colBack
= GetBackgroundColour();
173 colBack
= wxSystemSettings::GetSystemColour(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 return HandleChar(wParam
, lParam
, TRUE
/* isASCII */);
195 return HandleKeyDown(wParam
, lParam
);
198 return HandleKeyUp(wParam
, lParam
);
204 bool wxComboBox::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
211 sel
= GetSelection();
214 value
= GetString(sel
);
216 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId());
218 event
.SetEventObject(this);
219 event
.SetString(value
);
220 ProcessCommand(event
);
227 // fall through: for compability with wxGTK, also send the text
228 // update event when the selection changes (this also seems more
229 // logical as the text does change)
233 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
235 // if sel != -1, value was initialized above (and we can't use
236 // GetValue() here as it would return the old selection and we
242 else // we're synthesizing text updated event from sel change
244 // we need to do this because the user code expects
245 // wxComboBox::GetValue() to return the new value from
246 // "text updated" handler but it hadn't been updated yet
250 event
.SetString(value
);
251 event
.SetEventObject(this);
252 ProcessCommand(event
);
257 // there is no return value for the CBN_ notifications, so always return
258 // FALSE from here to pass the message to DefWindowProc()
262 WXHWND
wxComboBox::GetEditHWND() const
264 // this function should not be called for wxCB_READONLY controls, it is
265 // the callers responsability to check this
266 wxASSERT_MSG( !(GetWindowStyle() & wxCB_READONLY
),
267 _T("read-only combobox doesn't have any edit control") );
271 HWND hwndEdit
= ::ChildWindowFromPoint(GetHwnd(), pt
);
272 if ( !hwndEdit
|| hwndEdit
== GetHwnd() )
274 wxFAIL_MSG(_T("not read only combobox without edit control?"));
277 return (WXHWND
)hwndEdit
;
280 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
281 const wxString
& value
,
284 int n
, const wxString choices
[],
286 const wxValidator
& validator
,
287 const wxString
& name
)
289 // first create wxWin object
290 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
293 // get the right style
294 long msStyle
= WS_TABSTOP
| WS_VSCROLL
| WS_HSCROLL
|
295 CBS_AUTOHSCROLL
| CBS_NOINTEGRALHEIGHT
/* | WS_CLIPSIBLINGS */;
296 if ( style
& wxCB_READONLY
)
297 msStyle
|= CBS_DROPDOWNLIST
;
298 else if ( style
& wxCB_SIMPLE
)
299 msStyle
|= CBS_SIMPLE
; // A list (shown always) and edit control
301 msStyle
|= CBS_DROPDOWN
;
303 if ( style
& wxCB_SORT
)
306 if ( style
& wxCLIP_SIBLINGS
)
307 msStyle
|= WS_CLIPSIBLINGS
;
310 // and now create the MSW control
311 if ( !MSWCreateControl(_T("COMBOBOX"), msStyle
) )
314 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
316 // A choice/combobox normally has a white background (or other, depending
317 // on global settings) rather than inheriting the parent's background colour.
318 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
320 for ( int i
= 0; i
< n
; i
++ )
325 if ( !value
.IsEmpty() )
330 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
331 // and an edit control inside it and if we want to catch events from this
332 // edit control, we must subclass it as well
333 if ( !(style
& wxCB_READONLY
) )
335 gs_wndprocEdit
= (WXFARPROC
)::SetWindowLong
339 (LPARAM
)wxComboEditWndProc
346 // TODO: update and clear all this horrible mess (VZ)
348 void wxComboBox::SetValue(const wxString
& value
)
350 // If newlines are denoted by just 10, must stick 13 in front.
352 int len
= value
.Length();
354 for (i
= 0; i
< len
; i
++)
356 if ((i
> 0) && (value
[i
] == 10) && (value
[i
-1] != 13))
361 wxChar
*tmp
= new wxChar
[len
+ singletons
+ 1];
363 for (i
= 0; i
< len
; i
++)
365 if ((i
> 0) && (value
[i
] == 10) && (value
[i
-1] != 13))
374 SetWindowText(GetHwnd(), tmp
);
378 SetWindowText(GetHwnd(), value
);
381 // Clipboard operations
382 void wxComboBox::Copy()
384 HWND hWnd
= GetHwnd();
385 SendMessage(hWnd
, WM_COPY
, 0, 0L);
388 void wxComboBox::Cut()
390 HWND hWnd
= GetHwnd();
391 SendMessage(hWnd
, WM_CUT
, 0, 0L);
394 void wxComboBox::Paste()
396 HWND hWnd
= GetHwnd();
397 SendMessage(hWnd
, WM_PASTE
, 0, 0L);
400 void wxComboBox::SetEditable(bool WXUNUSED(editable
))
402 // Can't implement in MSW?
403 // HWND hWnd = GetHwnd();
404 // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
407 void wxComboBox::SetInsertionPoint(long pos
)
409 if ( GetWindowStyle() & wxCB_READONLY
)
413 HWND hWnd
= GetHwnd();
414 ::SendMessage(hWnd
, CB_SETEDITSEL
, 0, MAKELPARAM(pos
, pos
));
415 HWND hEditWnd
= (HWND
) GetEditHWND() ;
418 // Scroll insertion point into view
419 SendMessage(hEditWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
420 // Why is this necessary? (Copied from wxTextCtrl::SetInsertionPoint)
421 SendMessage(hEditWnd
, EM_REPLACESEL
, 0, (LPARAM
)_T(""));
426 void wxComboBox::SetInsertionPointEnd()
428 // setting insertion point doesn't make sense for read only comboboxes
429 if ( !(GetWindowStyle() & wxCB_READONLY
) )
431 long pos
= GetLastPosition();
432 SetInsertionPoint(pos
);
436 long wxComboBox::GetInsertionPoint() const
439 DWORD Pos
=(DWORD
)SendMessage(GetHwnd(), CB_GETEDITSEL
, 0, 0L);
446 long wxComboBox::GetLastPosition() const
448 HWND hEditWnd
= (HWND
) GetEditHWND();
450 // Get number of characters in the last (only) line. We'll add this to the character
451 // index for the last line, 1st position.
452 int lineLength
= (int)SendMessage(hEditWnd
, EM_LINELENGTH
, (WPARAM
) 0, (LPARAM
)0L);
454 return (long)(lineLength
);
457 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
462 // Now replace with 'value', by pasting.
463 wxSetClipboardData(wxDF_TEXT
, (wxObject
*)(const wxChar
*)value
, 0, 0);
465 // Paste into edit control
466 SendMessage(GetHwnd(), WM_PASTE
, (WPARAM
)0, (LPARAM
)0L);
470 void wxComboBox::Remove(long from
, long to
)
472 // Set selection and remove it
473 SetSelection(from
, to
);
474 SendMessage(GetHwnd(), WM_CUT
, (WPARAM
)0, (LPARAM
)0);
477 void wxComboBox::SetSelection(long from
, long to
)
479 HWND hWnd
= GetHwnd();
480 long fromChar
= from
;
482 // if from and to are both -1, it means
483 // (in wxWindows) that all text should be selected.
484 // This translates into Windows convention
485 if ((from
== -1) && (to
== -1))
493 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
))
495 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)fromChar
, (LPARAM
)toChar
)
499 wxLogDebug(_T("CB_SETEDITSEL failed"));