1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxComboBox class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "combobox.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
26 #include "wx/settings.h"
29 #include "wx/combobox.h"
30 #include "wx/clipbrd.h"
31 #include "wx/msw/private.h"
33 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
35 bool wxComboBox::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
40 if (GetSelection() > -1)
42 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId());
43 event
.SetInt(GetSelection());
44 event
.SetEventObject(this);
45 event
.SetString(GetStringSelection());
46 ProcessCommand(event
);
52 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
53 event
.SetString(GetValue());
54 event
.SetEventObject(this);
55 ProcessCommand(event
);
60 // there is no return value for the CBN_ notifications, so always return
61 // FALSE from here to pass the message to DefWindowProc()
65 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
66 const wxString
& value
,
69 int n
, const wxString choices
[],
71 const wxValidator
& validator
,
75 SetValidator(validator
);
76 if (parent
) parent
->AddChild(this);
77 // SetBackgroundColour(parent->GetBackgroundColour()) ;
79 // A choice/combobox normally has a white background (or other, depending
80 // on global settings) rather than inheriting the parent's background colour.
81 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
83 SetForegroundColour(parent
->GetForegroundColour()) ;
85 m_windowStyle
= style
;
88 m_windowId
= (int)NewControlId();
97 long msStyle
= WS_CHILD
| WS_TABSTOP
| WS_VISIBLE
| WS_HSCROLL
| WS_VSCROLL
|
100 if (m_windowStyle
& wxCB_READONLY
)
101 msStyle
|= CBS_DROPDOWNLIST
;
102 else if (m_windowStyle
& wxCB_SIMPLE
)
103 msStyle
|= CBS_SIMPLE
; // A list (shown always) and edit control
105 msStyle
|= CBS_DROPDOWN
;
107 if (m_windowStyle
& wxCB_SORT
)
111 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
113 // Even with extended styles, need to combine with WS_BORDER
114 // for them to look right.
115 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
116 msStyle
|= WS_BORDER
;
118 m_hWnd
= (WXHWND
)::CreateWindowEx(exStyle
, wxT("COMBOBOX"), NULL
,
120 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
121 wxGetInstance(), NULL
);
123 wxCHECK_MSG( m_hWnd
, FALSE
, wxT("Failed to create combobox") );
129 Ctl3dSubclassCtl(wx_combo);
135 // Subclass again for purposes of dialog editing mode
138 SetFont(parent
->GetFont());
140 for (i
= 0; i
< n
; i
++)
147 SetSize(x
, y
, width
, height
);
148 if ( !value
.IsEmpty() )
156 void wxComboBox::SetValue(const wxString
& value
)
158 // If newlines are denoted by just 10, must stick 13 in front.
160 int len
= value
.Length();
162 for (i
= 0; i
< len
; i
++)
164 if ((i
> 0) && (value
[i
] == 10) && (value
[i
-1] != 13))
169 wxChar
*tmp
= new wxChar
[len
+ singletons
+ 1];
171 for (i
= 0; i
< len
; i
++)
173 if ((i
> 0) && (value
[i
] == 10) && (value
[i
-1] != 13))
182 SetWindowText(GetHwnd(), tmp
);
186 SetWindowText(GetHwnd(), value
);
189 // Clipboard operations
190 void wxComboBox::Copy()
192 HWND hWnd
= GetHwnd();
193 SendMessage(hWnd
, WM_COPY
, 0, 0L);
196 void wxComboBox::Cut()
198 HWND hWnd
= GetHwnd();
199 SendMessage(hWnd
, WM_CUT
, 0, 0L);
202 void wxComboBox::Paste()
204 HWND hWnd
= GetHwnd();
205 SendMessage(hWnd
, WM_PASTE
, 0, 0L);
208 void wxComboBox::SetEditable(bool editable
)
210 // Can't implement in MSW?
211 // HWND hWnd = GetHwnd();
212 // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
215 void wxComboBox::SetInsertionPoint(long pos
)
218 HWND hWnd = GetHwnd();
220 SendMessage(hWnd, EM_SETSEL, pos, pos);
221 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
223 SendMessage(hWnd, EM_SETSEL, 0, MAKELPARAM(pos, pos));
226 SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)nothing);
230 void wxComboBox::SetInsertionPointEnd()
233 long pos = GetLastPosition();
234 SetInsertionPoint(pos);
238 long wxComboBox::GetInsertionPoint() const
241 DWORD Pos=(DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L);
247 long wxComboBox::GetLastPosition() const
250 HWND hWnd = GetHwnd();
252 // Will always return a number > 0 (according to docs)
253 int noLines = (int)SendMessage(hWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0L);
255 // This gets the char index for the _beginning_ of the last line
256 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)(noLines-1), (LPARAM)0L);
258 // Get number of characters in the last line. We'll add this to the character
259 // index for the last line, 1st position.
260 int lineLength = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0L);
262 return (long)(charIndex + lineLength);
267 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
270 HWND hWnd
= GetHwnd();
271 long fromChar
= from
;
274 // Set selection and remove it
276 SendMessage(hWnd
, CB_SETEDITSEL
, fromChar
, toChar
);
278 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
280 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
282 // Now replace with 'value', by pasting.
283 wxSetClipboardData(wxDF_TEXT
, (wxObject
*)(const wxChar
*)value
, 0, 0);
285 // Paste into edit control
286 SendMessage(hWnd
, WM_PASTE
, (WPARAM
)0, (LPARAM
)0L);
290 void wxComboBox::Remove(long from
, long to
)
292 HWND hWnd
= GetHwnd();
293 long fromChar
= from
;
296 // Cut all selected text
298 SendMessage(hWnd
, CB_SETEDITSEL
, fromChar
, toChar
);
300 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
302 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
305 void wxComboBox::SetSelection(long from
, long to
)
307 HWND hWnd
= GetHwnd();
308 long fromChar
= from
;
310 // if from and to are both -1, it means
311 // (in wxWindows) that all text should be selected.
312 // This translates into Windows convention
313 if ((from
== -1) && (to
== -1))
320 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)fromChar
, (LPARAM
)toChar
);
321 // SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
323 // WPARAM is 0: selection is scrolled into view
324 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
328 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
)
331 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
337 height
= n
* EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
339 wxControl::DoMoveWindow(x
, y
, width
, height
);
342 wxSize
wxComboBox::DoGetBestSize() const
344 // the choice calculates the horz size correctly, but not the vertical
345 // component: correct it
346 wxSize size
= wxChoice::DoGetBestSize();
349 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
350 size
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);