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"
29 #include "wx/combobox.h"
30 #include "wx/clipbrd.h"
31 #include "wx/msw/private.h"
33 #if !USE_SHARED_LIBRARY
34 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
37 bool wxComboBox::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
39 if (param
== CBN_SELCHANGE
)
41 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
42 event
.SetInt(GetSelection());
43 event
.SetEventObject(this);
44 event
.SetString(GetStringSelection());
45 ProcessCommand(event
);
49 else if (param
== CBN_EDITCHANGE
)
51 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
52 event
.SetString(GetValue());
53 event
.SetEventObject(this);
54 ProcessCommand(event
);
62 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
63 const wxString
& value
,
66 int n
, const wxString choices
[],
68 const wxValidator
& validator
,
72 SetValidator(validator
);
73 if (parent
) parent
->AddChild(this);
74 SetBackgroundColour(parent
->GetBackgroundColour()) ;
75 SetForegroundColour(parent
->GetForegroundColour()) ;
78 m_windowStyle
= style
;
81 m_windowId
= (int)NewControlId();
90 long msStyle
= WS_CHILD
| WS_HSCROLL
| WS_VSCROLL
|
91 WS_TABSTOP
| WS_VISIBLE
| CBS_NOINTEGRALHEIGHT
;
93 if (m_windowStyle
& wxCB_READONLY
)
94 msStyle
|= CBS_DROPDOWNLIST
;
95 else if (m_windowStyle
& wxCB_SIMPLE
)
96 msStyle
|= CBS_SIMPLE
; // A list (shown always) and edit control
98 msStyle
|= CBS_DROPDOWN
;
100 if (m_windowStyle
& wxCB_SORT
)
104 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
106 // Even with extended styles, need to combine with WS_BORDER
107 // for them to look right.
108 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
109 msStyle
|= WS_BORDER
;
111 m_hWnd
= (WXHWND
)::CreateWindowEx(exStyle
, _T("COMBOBOX"), NULL
,
113 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
114 wxGetInstance(), NULL
);
116 wxCHECK_MSG( m_hWnd
, FALSE
, _T("Failed to create combobox") );
122 Ctl3dSubclassCtl(wx_combo);
128 // Subclass again for purposes of dialog editing mode
131 SetFont(parent
->GetFont());
133 for (i
= 0; i
< n
; i
++)
140 SetSize(x
, y
, width
, height
);
141 if ( !value
.IsEmpty() )
149 void wxComboBox::SetValue(const wxString
& value
)
151 // If newlines are denoted by just 10, must stick 13 in front.
153 int len
= value
.Length();
155 for (i
= 0; i
< len
; i
++)
157 if ((i
> 0) && (value
[i
] == 10) && (value
[i
-1] != 13))
162 wxChar
*tmp
= new wxChar
[len
+ singletons
+ 1];
164 for (i
= 0; i
< len
; i
++)
166 if ((i
> 0) && (value
[i
] == 10) && (value
[i
-1] != 13))
175 SetWindowText(GetHwnd(), tmp
);
179 SetWindowText(GetHwnd(), value
);
182 // Clipboard operations
183 void wxComboBox::Copy()
185 HWND hWnd
= GetHwnd();
186 SendMessage(hWnd
, WM_COPY
, 0, 0L);
189 void wxComboBox::Cut()
191 HWND hWnd
= GetHwnd();
192 SendMessage(hWnd
, WM_CUT
, 0, 0L);
195 void wxComboBox::Paste()
197 HWND hWnd
= GetHwnd();
198 SendMessage(hWnd
, WM_PASTE
, 0, 0L);
201 void wxComboBox::SetEditable(bool editable
)
203 // Can't implement in MSW?
204 // HWND hWnd = GetHwnd();
205 // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
208 void wxComboBox::SetInsertionPoint(long pos
)
211 HWND hWnd = GetHwnd();
213 SendMessage(hWnd, EM_SETSEL, pos, pos);
214 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
216 SendMessage(hWnd, EM_SETSEL, 0, MAKELPARAM(pos, pos));
219 SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)nothing);
223 void wxComboBox::SetInsertionPointEnd()
226 long pos = GetLastPosition();
227 SetInsertionPoint(pos);
231 long wxComboBox::GetInsertionPoint() const
234 DWORD Pos=(DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L);
240 long wxComboBox::GetLastPosition() const
243 HWND hWnd = GetHwnd();
245 // Will always return a number > 0 (according to docs)
246 int noLines = (int)SendMessage(hWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0L);
248 // This gets the char index for the _beginning_ of the last line
249 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)(noLines-1), (LPARAM)0L);
251 // Get number of characters in the last line. We'll add this to the character
252 // index for the last line, 1st position.
253 int lineLength = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0L);
255 return (long)(charIndex + lineLength);
260 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
263 HWND hWnd
= GetHwnd();
264 long fromChar
= from
;
267 // Set selection and remove it
269 SendMessage(hWnd
, CB_SETEDITSEL
, fromChar
, toChar
);
271 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
273 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
275 // Now replace with 'value', by pasting.
276 wxSetClipboardData(wxDF_TEXT
, (wxObject
*)(const wxChar
*)value
, 0, 0);
278 // Paste into edit control
279 SendMessage(hWnd
, WM_PASTE
, (WPARAM
)0, (LPARAM
)0L);
283 void wxComboBox::Remove(long from
, long to
)
285 HWND hWnd
= GetHwnd();
286 long fromChar
= from
;
289 // Cut all selected text
291 SendMessage(hWnd
, CB_SETEDITSEL
, fromChar
, toChar
);
293 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
295 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
298 void wxComboBox::SetSelection(long from
, long to
)
300 HWND hWnd
= GetHwnd();
301 long fromChar
= from
;
303 // if from and to are both -1, it means
304 // (in wxWindows) that all text should be selected.
305 // This translates into Windows convention
306 if ((from
== -1) && (to
== -1))
313 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)fromChar
, (LPARAM
)toChar
);
314 // SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
316 // WPARAM is 0: selection is scrolled into view
317 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
321 void wxComboBox::DoSetSize(int x
, int y
,
322 int width
, int height
,
325 wxControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);