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 #ifndef __GNUWIN32_OLD__
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
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
),
145 WXUINT
WXUNUSED(message
),
146 WXWPARAM
WXUNUSED(wParam
),
147 WXLPARAM
WXUNUSED(lParam
))
152 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
153 return (WXHBRUSH
) hbrush
;
155 #endif // wxUSE_CTL3D
158 if (GetParent()->GetTransparentBackground())
159 SetBkMode(hdc
, TRANSPARENT
);
161 SetBkMode(hdc
, OPAQUE
);
163 wxColour colBack
= GetBackgroundColour();
166 colBack
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
168 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
169 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
171 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
173 return (WXHBRUSH
)brush
->GetResourceHandle();
176 // ----------------------------------------------------------------------------
178 // ----------------------------------------------------------------------------
180 bool wxComboBox::MSWProcessEditMsg(WXUINT msg
, WXWPARAM wParam
, WXLPARAM lParam
)
185 return HandleChar(wParam
, lParam
, TRUE
/* isASCII */);
188 return HandleKeyDown(wParam
, lParam
);
191 return HandleKeyUp(wParam
, lParam
);
197 bool wxComboBox::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
204 sel
= GetSelection();
207 value
= GetString(sel
);
209 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId());
211 event
.SetEventObject(this);
212 event
.SetString(value
);
213 ProcessCommand(event
);
220 // fall through: for compability with wxGTK, also send the text
221 // update event when the selection changes (this also seems more
222 // logical as the text does change)
226 // if sel != -1, value was initialized above (and we can't use
227 // GetValue() here as it would return the old selection and we
229 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
230 event
.SetString(sel
== -1 ? GetValue() : value
);
231 event
.SetEventObject(this);
232 ProcessCommand(event
);
237 // there is no return value for the CBN_ notifications, so always return
238 // FALSE from here to pass the message to DefWindowProc()
242 WXHWND
wxComboBox::GetEditHWND() const
244 // this function should not be called for wxCB_READONLY controls, it is
245 // the callers responsability to check this
246 wxASSERT_MSG( !(GetWindowStyle() & wxCB_READONLY
),
247 _T("read-only combobox doesn't have any edit control") );
251 HWND hwndEdit
= ::ChildWindowFromPoint(GetHwnd(), pt
);
252 if ( !hwndEdit
|| hwndEdit
== GetHwnd() )
254 wxFAIL_MSG(_T("not read only combobox without edit control?"));
257 return (WXHWND
)hwndEdit
;
260 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
261 const wxString
& value
,
264 int n
, const wxString choices
[],
266 const wxValidator
& validator
,
267 const wxString
& name
)
269 // first create wxWin object
270 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
273 // get the right style
274 long msStyle
= WS_TABSTOP
| WS_VSCROLL
| WS_HSCROLL
|
275 CBS_AUTOHSCROLL
| CBS_NOINTEGRALHEIGHT
/* | WS_CLIPSIBLINGS */;
276 if ( style
& wxCB_READONLY
)
277 msStyle
|= CBS_DROPDOWNLIST
;
278 else if ( style
& wxCB_SIMPLE
)
279 msStyle
|= CBS_SIMPLE
; // A list (shown always) and edit control
281 msStyle
|= CBS_DROPDOWN
;
283 if ( style
& wxCB_SORT
)
286 // and now create the MSW control
287 if ( !MSWCreateControl(_T("COMBOBOX"), msStyle
) )
290 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
292 // A choice/combobox normally has a white background (or other, depending
293 // on global settings) rather than inheriting the parent's background colour.
294 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
296 for ( int i
= 0; i
< n
; i
++ )
301 if ( !value
.IsEmpty() )
306 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
307 // and an edit control inside it and if we want to catch events from this
308 // edit control, we must subclass it as well
309 if ( !(style
& wxCB_READONLY
) )
311 gs_wndprocEdit
= (WXFARPROC
)::SetWindowLong
315 (LPARAM
)wxComboEditWndProc
322 // TODO: update and clear all this horrible mess (VZ)
324 void wxComboBox::SetValue(const wxString
& value
)
326 // If newlines are denoted by just 10, must stick 13 in front.
328 int len
= value
.Length();
330 for (i
= 0; i
< len
; i
++)
332 if ((i
> 0) && (value
[i
] == 10) && (value
[i
-1] != 13))
337 wxChar
*tmp
= new wxChar
[len
+ singletons
+ 1];
339 for (i
= 0; i
< len
; i
++)
341 if ((i
> 0) && (value
[i
] == 10) && (value
[i
-1] != 13))
350 SetWindowText(GetHwnd(), tmp
);
354 SetWindowText(GetHwnd(), value
);
357 // Clipboard operations
358 void wxComboBox::Copy()
360 HWND hWnd
= GetHwnd();
361 SendMessage(hWnd
, WM_COPY
, 0, 0L);
364 void wxComboBox::Cut()
366 HWND hWnd
= GetHwnd();
367 SendMessage(hWnd
, WM_CUT
, 0, 0L);
370 void wxComboBox::Paste()
372 HWND hWnd
= GetHwnd();
373 SendMessage(hWnd
, WM_PASTE
, 0, 0L);
376 void wxComboBox::SetEditable(bool WXUNUSED(editable
))
378 // Can't implement in MSW?
379 // HWND hWnd = GetHwnd();
380 // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
383 void wxComboBox::SetInsertionPoint(long pos
)
385 if ( GetWindowStyle() & wxCB_READONLY
)
389 HWND hWnd
= GetHwnd();
390 ::SendMessage(hWnd
, CB_SETEDITSEL
, 0, MAKELPARAM(pos
, pos
));
391 HWND hEditWnd
= (HWND
) GetEditHWND() ;
394 // Scroll insertion point into view
395 SendMessage(hEditWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
396 // Why is this necessary? (Copied from wxTextCtrl::SetInsertionPoint)
397 SendMessage(hEditWnd
, EM_REPLACESEL
, 0, (LPARAM
)_T(""));
402 void wxComboBox::SetInsertionPointEnd()
404 // setting insertion point doesn't make sense for read only comboboxes
405 if ( !(GetWindowStyle() & wxCB_READONLY
) )
407 long pos
= GetLastPosition();
408 SetInsertionPoint(pos
);
412 long wxComboBox::GetInsertionPoint() const
415 DWORD Pos
=(DWORD
)SendMessage(GetHwnd(), CB_GETEDITSEL
, 0, 0L);
422 long wxComboBox::GetLastPosition() const
424 HWND hEditWnd
= (HWND
) GetEditHWND();
426 // Get number of characters in the last (only) line. We'll add this to the character
427 // index for the last line, 1st position.
428 int lineLength
= (int)SendMessage(hEditWnd
, EM_LINELENGTH
, (WPARAM
) 0, (LPARAM
)0L);
430 return (long)(lineLength
);
433 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
438 // Now replace with 'value', by pasting.
439 wxSetClipboardData(wxDF_TEXT
, (wxObject
*)(const wxChar
*)value
, 0, 0);
441 // Paste into edit control
442 SendMessage(GetHwnd(), WM_PASTE
, (WPARAM
)0, (LPARAM
)0L);
446 void wxComboBox::Remove(long from
, long to
)
448 // Set selection and remove it
449 SetSelection(from
, to
);
450 SendMessage(GetHwnd(), WM_CUT
, (WPARAM
)0, (LPARAM
)0);
453 void wxComboBox::SetSelection(long from
, long to
)
455 HWND hWnd
= GetHwnd();
456 long fromChar
= from
;
458 // if from and to are both -1, it means
459 // (in wxWindows) that all text should be selected.
460 // This translates into Windows convention
461 if ((from
== -1) && (to
== -1))
469 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
))
471 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)fromChar
, (LPARAM
)toChar
)
475 wxLogDebug(_T("CB_SETEDITSEL failed"));
479 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
)
481 // here is why this is necessary: if the width is negative, the combobox
482 // window proc makes the window of the size width*height instead of
483 // interpreting height in the usual manner (meaning the height of the drop
484 // down list - usually the height specified in the call to MoveWindow()
485 // will not change the height of combo box per se)
487 // this behaviour is not documented anywhere, but this is just how it is
488 // here (NT 4.4) and, anyhow, the check shouldn't hurt - however without
489 // the check, constraints/sizers using combos may break the height
490 // constraint will have not at all the same value as expected
495 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
497 // what should the height of the drop down list be? we choose 10 items by
498 // default and also 10 items max (if we always use n, the list will never
499 // have vertical scrollbar)
501 if ( !n
|| (n
> 10) )
504 height
= (n
+ 1)* EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
506 wxControl::DoMoveWindow(x
, y
, width
, height
);
509 wxSize
wxComboBox::DoGetBestSize() const
511 // the choice calculates the horz size correctly, but not the vertical
512 // component: correct it
513 wxSize size
= wxChoice::DoGetBestSize();
516 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
517 size
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);