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"
37 #include "wx/combobox.h"
39 #include "wx/clipbrd.h"
40 #include "wx/msw/private.h"
43 #ifndef __GNUWIN32_OLD__
46 #include "wx/tooltip.h"
47 #endif // wxUSE_TOOLTIPS
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
55 // ----------------------------------------------------------------------------
56 // function prototypes
57 // ----------------------------------------------------------------------------
59 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
64 // ---------------------------------------------------------------------------
66 // ---------------------------------------------------------------------------
68 // the pointer to standard radio button wnd proc
69 static WXFARPROC gs_wndprocEdit
= (WXFARPROC
)NULL
;
71 // ============================================================================
73 // ============================================================================
75 // ----------------------------------------------------------------------------
76 // wnd proc for subclassed edit control
77 // ----------------------------------------------------------------------------
79 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
84 HWND hwndCombo
= ::GetParent(hWnd
);
85 wxWindow
*win
= wxFindWinFromHandle((WXHWND
)hwndCombo
);
89 // forward some messages to the combobox
94 wxComboBox
*combo
= wxDynamicCast(win
, wxComboBox
);
95 wxCHECK_MSG( combo
, 0, _T("should have combo as parent") );
97 if ( combo
->MSWProcessEditMsg(message
, wParam
, lParam
) )
105 wxCHECK_MSG( win
, 0, _T("should have a parent") );
107 if ( win
->GetWindowStyle() & wxPROCESS_ENTER
)
109 // need to return a custom dlg code or we'll never get it
110 return DLGC_WANTMESSAGE
;
116 // deal with tooltips here
120 wxCHECK_MSG( win
, 0, _T("should have a parent") );
122 NMHDR
* hdr
= (NMHDR
*)lParam
;
123 if ( (int)hdr
->code
== TTN_NEEDTEXT
)
125 wxToolTip
*tooltip
= win
->GetToolTip();
128 TOOLTIPTEXT
*ttt
= (TOOLTIPTEXT
*)lParam
;
129 ttt
->lpszText
= (wxChar
*)tooltip
->GetTip().c_str();
137 #endif // wxUSE_TOOLTIPS
140 return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit
, hWnd
, message
, wParam
, lParam
);
143 WXHBRUSH
wxComboBox::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
151 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
152 return (WXHBRUSH
) hbrush
;
154 #endif // wxUSE_CTL3D
157 if (GetParent()->GetTransparentBackground())
158 SetBkMode(hdc
, TRANSPARENT
);
160 SetBkMode(hdc
, OPAQUE
);
162 wxColour colBack
= GetBackgroundColour();
165 colBack
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
167 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
168 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
170 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
172 return (WXHBRUSH
)brush
->GetResourceHandle();
175 // ----------------------------------------------------------------------------
177 // ----------------------------------------------------------------------------
179 bool wxComboBox::MSWProcessEditMsg(WXUINT msg
, WXWPARAM wParam
, WXLPARAM lParam
)
184 return HandleChar(wParam
, lParam
, TRUE
/* isASCII */);
187 return HandleKeyDown(wParam
, lParam
);
190 return HandleKeyUp(wParam
, lParam
);
196 bool wxComboBox::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
201 if (GetSelection() > -1)
203 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId());
204 event
.SetInt(GetSelection());
205 event
.SetEventObject(this);
206 event
.SetString(GetStringSelection());
207 ProcessCommand(event
);
213 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
214 event
.SetString(GetValue());
215 event
.SetEventObject(this);
216 ProcessCommand(event
);
221 // there is no return value for the CBN_ notifications, so always return
222 // FALSE from here to pass the message to DefWindowProc()
226 WXHWND
wxComboBox::GetEditHWND() const
228 // this function should not be called for wxCB_READONLY controls, it is
229 // the callers responsability to check this
230 wxASSERT_MSG( !(GetWindowStyle() & wxCB_READONLY
),
231 _T("read-only combobox doesn't have any edit control") );
235 HWND hwndEdit
= ::ChildWindowFromPoint(GetHwnd(), pt
);
236 if ( !hwndEdit
|| hwndEdit
== GetHwnd() )
238 wxFAIL_MSG(_T("not read only combobox without edit control?"));
241 return (WXHWND
)hwndEdit
;
244 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
245 const wxString
& value
,
248 int n
, const wxString choices
[],
250 const wxValidator
& validator
,
251 const wxString
& name
)
253 // first create wxWin object
254 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
257 // get the right style
258 long msStyle
= WS_TABSTOP
| WS_VSCROLL
| WS_HSCROLL
|
259 CBS_AUTOHSCROLL
| CBS_NOINTEGRALHEIGHT
/* | WS_CLIPSIBLINGS */;
260 if ( style
& wxCB_READONLY
)
261 msStyle
|= CBS_DROPDOWNLIST
;
262 else if ( style
& wxCB_SIMPLE
)
263 msStyle
|= CBS_SIMPLE
; // A list (shown always) and edit control
265 msStyle
|= CBS_DROPDOWN
;
267 if ( style
& wxCB_SORT
)
270 // and now create the MSW control
271 if ( !MSWCreateControl(_T("COMBOBOX"), msStyle
) )
274 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
276 // A choice/combobox normally has a white background (or other, depending
277 // on global settings) rather than inheriting the parent's background colour.
278 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
280 for ( int i
= 0; i
< n
; i
++ )
285 if ( !value
.IsEmpty() )
290 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
291 // and an edit control inside it and if we want to catch events from this
292 // edit control, we must subclass it as well
293 if ( !(style
& wxCB_READONLY
) )
295 gs_wndprocEdit
= (WXFARPROC
)::SetWindowLong
299 (LPARAM
)wxComboEditWndProc
306 // TODO: update and clear all this horrible mess (VZ)
308 void wxComboBox::SetValue(const wxString
& value
)
310 // If newlines are denoted by just 10, must stick 13 in front.
312 int len
= value
.Length();
314 for (i
= 0; i
< len
; i
++)
316 if ((i
> 0) && (value
[i
] == 10) && (value
[i
-1] != 13))
321 wxChar
*tmp
= new wxChar
[len
+ singletons
+ 1];
323 for (i
= 0; i
< len
; i
++)
325 if ((i
> 0) && (value
[i
] == 10) && (value
[i
-1] != 13))
334 SetWindowText(GetHwnd(), tmp
);
338 SetWindowText(GetHwnd(), value
);
341 // Clipboard operations
342 void wxComboBox::Copy()
344 HWND hWnd
= GetHwnd();
345 SendMessage(hWnd
, WM_COPY
, 0, 0L);
348 void wxComboBox::Cut()
350 HWND hWnd
= GetHwnd();
351 SendMessage(hWnd
, WM_CUT
, 0, 0L);
354 void wxComboBox::Paste()
356 HWND hWnd
= GetHwnd();
357 SendMessage(hWnd
, WM_PASTE
, 0, 0L);
360 void wxComboBox::SetEditable(bool editable
)
362 // Can't implement in MSW?
363 // HWND hWnd = GetHwnd();
364 // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
367 void wxComboBox::SetInsertionPoint(long pos
)
369 if ( GetWindowStyle() & wxCB_READONLY
)
373 HWND hWnd
= GetHwnd();
374 ::SendMessage(hWnd
, CB_SETEDITSEL
, 0, MAKELPARAM(pos
, pos
));
375 HWND hEditWnd
= (HWND
) GetEditHWND() ;
378 // Scroll insertion point into view
379 SendMessage(hEditWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
380 // Why is this necessary? (Copied from wxTextCtrl::SetInsertionPoint)
381 SendMessage(hEditWnd
, EM_REPLACESEL
, 0, (LPARAM
)_T(""));
386 void wxComboBox::SetInsertionPointEnd()
388 // setting insertion point doesn't make sense for read only comboboxes
389 if ( !(GetWindowStyle() & wxCB_READONLY
) )
391 long pos
= GetLastPosition();
392 SetInsertionPoint(pos
);
396 long wxComboBox::GetInsertionPoint() const
399 DWORD Pos
=(DWORD
)SendMessage(GetHwnd(), CB_GETEDITSEL
, 0, 0L);
406 long wxComboBox::GetLastPosition() const
408 HWND hEditWnd
= (HWND
) GetEditHWND();
410 // Get number of characters in the last (only) line. We'll add this to the character
411 // index for the last line, 1st position.
412 int lineLength
= (int)SendMessage(hEditWnd
, EM_LINELENGTH
, (WPARAM
) 0, (LPARAM
)0L);
414 return (long)(lineLength
);
417 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
420 HWND hWnd
= GetHwnd();
421 long fromChar
= from
;
424 // Set selection and remove it
426 SendMessage(hWnd
, CB_SETEDITSEL
, fromChar
, toChar
);
428 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
430 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
432 // Now replace with 'value', by pasting.
433 wxSetClipboardData(wxDF_TEXT
, (wxObject
*)(const wxChar
*)value
, 0, 0);
435 // Paste into edit control
436 SendMessage(hWnd
, WM_PASTE
, (WPARAM
)0, (LPARAM
)0L);
440 void wxComboBox::Remove(long from
, long to
)
442 HWND hWnd
= GetHwnd();
443 long fromChar
= from
;
446 // Cut all selected text
448 SendMessage(hWnd
, CB_SETEDITSEL
, fromChar
, toChar
);
450 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
452 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
455 void wxComboBox::SetSelection(long from
, long to
)
457 HWND hWnd
= GetHwnd();
458 long fromChar
= from
;
460 // if from and to are both -1, it means
461 // (in wxWindows) that all text should be selected.
462 // This translates into Windows convention
463 if ((from
== -1) && (to
== -1))
470 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)fromChar
, (LPARAM
)toChar
);
471 // SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
473 // WPARAM is 0: selection is scrolled into view
474 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
478 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
)
480 // here is why this is necessary: if the width is negative, the combobox
481 // window proc makes the window of the size width*height instead of
482 // interpreting height in the usual manner (meaning the height of the drop
483 // down list - usually the height specified in the call to MoveWindow()
484 // will not change the height of combo box per se)
486 // this behaviour is not documented anywhere, but this is just how it is
487 // here (NT 4.4) and, anyhow, the check shouldn't hurt - however without
488 // the check, constraints/sizers using combos may break the height
489 // constraint will have not at all the same value as expected
494 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
496 // what should the height of the drop down list be? we choose 10 items by
497 // default and also 10 items max (if we always use n, the list will never
498 // have vertical scrollbar)
500 if ( !n
|| (n
> 10) )
503 height
= (n
+ 1)* EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
505 wxControl::DoMoveWindow(x
, y
, width
, height
);
508 wxSize
wxComboBox::DoGetBestSize() const
510 // the choice calculates the horz size correctly, but not the vertical
511 // component: correct it
512 wxSize size
= wxChoice::DoGetBestSize();
515 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
516 size
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);