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
)
370 HWND hWnd
= GetHwnd();
371 SendMessage(hWnd
, CB_SETEDITSEL
, 0, MAKELPARAM(pos
, pos
));
372 HWND hEditWnd
= (HWND
) GetEditHWND() ;
375 // Scroll insertion point into view
376 SendMessage(hEditWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
377 // Why is this necessary? (Copied from wxTextCtrl::SetInsertionPoint)
378 static const wxChar
*nothing
= _T("");
379 SendMessage(hEditWnd
, EM_REPLACESEL
, 0, (LPARAM
)nothing
);
384 void wxComboBox::SetInsertionPointEnd()
386 long pos
= GetLastPosition();
387 SetInsertionPoint(pos
);
390 long wxComboBox::GetInsertionPoint() const
393 DWORD Pos
=(DWORD
)SendMessage(GetHwnd(), CB_GETEDITSEL
, 0, 0L);
400 long wxComboBox::GetLastPosition() const
402 HWND hEditWnd
= (HWND
) GetEditHWND();
404 // Get number of characters in the last (only) line. We'll add this to the character
405 // index for the last line, 1st position.
406 int lineLength
= (int)SendMessage(hEditWnd
, EM_LINELENGTH
, (WPARAM
) 0, (LPARAM
)0L);
408 return (long)(lineLength
);
411 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
414 HWND hWnd
= GetHwnd();
415 long fromChar
= from
;
418 // Set selection and remove it
420 SendMessage(hWnd
, CB_SETEDITSEL
, fromChar
, toChar
);
422 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
424 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
426 // Now replace with 'value', by pasting.
427 wxSetClipboardData(wxDF_TEXT
, (wxObject
*)(const wxChar
*)value
, 0, 0);
429 // Paste into edit control
430 SendMessage(hWnd
, WM_PASTE
, (WPARAM
)0, (LPARAM
)0L);
434 void wxComboBox::Remove(long from
, long to
)
436 HWND hWnd
= GetHwnd();
437 long fromChar
= from
;
440 // Cut all selected text
442 SendMessage(hWnd
, CB_SETEDITSEL
, fromChar
, toChar
);
444 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
446 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
449 void wxComboBox::SetSelection(long from
, long to
)
451 HWND hWnd
= GetHwnd();
452 long fromChar
= from
;
454 // if from and to are both -1, it means
455 // (in wxWindows) that all text should be selected.
456 // This translates into Windows convention
457 if ((from
== -1) && (to
== -1))
464 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)fromChar
, (LPARAM
)toChar
);
465 // SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
467 // WPARAM is 0: selection is scrolled into view
468 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
472 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
)
474 // here is why this is necessary: if the width is negative, the combobox
475 // window proc makes the window of the size width*height instead of
476 // interpreting height in the usual manner (meaning the height of the drop
477 // down list - usually the height specified in the call to MoveWindow()
478 // will not change the height of combo box per se)
480 // this behaviour is not documented anywhere, but this is just how it is
481 // here (NT 4.4) and, anyhow, the check shouldn't hurt - however without
482 // the check, constraints/sizers using combos may break the height
483 // constraint will have not at all the same value as expected
488 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
490 // what should the height of the drop down list be? we choose 10 items by
491 // default and also 10 items max (if we always use n, the list will never
492 // have vertical scrollbar)
494 if ( !n
|| (n
> 10) )
497 height
= (n
+ 1)* EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
499 wxControl::DoMoveWindow(x
, y
, width
, height
);
502 wxSize
wxComboBox::DoGetBestSize() const
504 // the choice calculates the horz size correctly, but not the vertical
505 // component: correct it
506 wxSize size
= wxChoice::DoGetBestSize();
509 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
510 size
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);