1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxComboBox class
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/combobox.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
23 #include "wx/combobox.h"
24 #include "wx/clipbrd.h"
25 #include "wx/os2/private.h"
27 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
29 bool wxComboBox::OS2Command(WXUINT param
, WXWORD
WXUNUSED(id
))
33 if (param == CBN_SELCHANGE)
35 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId);
36 event.SetInt(GetSelection());
37 event.SetEventObject(this);
38 event.SetString(GetStringSelection());
39 ProcessCommand(event);
43 else if (param == CBN_EDITCHANGE)
45 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
46 event.SetString(GetValue());
47 event.SetEventObject(this);
48 ProcessCommand(event);
58 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
59 const wxString
& value
,
62 int n
, const wxString choices
[],
65 const wxValidator
& validator
,
71 SetValidator(validator
);
73 if (parent
) parent
->AddChild(this);
74 SetBackgroundColour(parent
->GetBackgroundColour()) ;
75 SetForegroundColour(parent
->GetForegroundColour()) ;
77 m_windowStyle
= style
;
80 m_windowId
= (int)NewControlId();
90 long msStyle = WS_CHILD | WS_TABSTOP | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL |
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, wxT("COMBOBOX"), NULL,
113 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
114 wxGetInstance(), NULL);
116 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create combobox") );
118 // Subclass again for purposes of dialog editing mode
121 SetFont(parent->GetFont());
123 for (i = 0; i < n; i++)
130 SetSize(x, y, width, height);
131 if ( !value.IsEmpty() )
139 void wxComboBox::SetValue(const wxString
& value
)
141 // If newlines are denoted by just 10, must stick 13 in front.
143 int len
= value
.Length();
145 for (i
= 0; i
< len
; i
++)
147 if ((i
> 0) && (value
[i
] == 10) && (value
[i
-1] != 13))
152 wxChar
*tmp
= new wxChar
[len
+ singletons
+ 1];
154 for (i
= 0; i
< len
; i
++)
156 if ((i
> 0) && (value
[i
] == 10) && (value
[i
-1] != 13))
165 // SetWindowText(GetHwnd(), tmp);
169 // SetWindowText(GetHwnd(), value);
172 // Clipboard operations
173 void wxComboBox::Copy()
175 HWND hWnd
= GetHwnd();
176 // SendMessage(hWnd, WM_COPY, 0, 0L);
179 void wxComboBox::Cut()
181 HWND hWnd
= GetHwnd();
182 // SendMessage(hWnd, WM_CUT, 0, 0L);
185 void wxComboBox::Paste()
187 HWND hWnd
= GetHwnd();
188 // SendMessage(hWnd, WM_PASTE, 0, 0L);
191 void wxComboBox::SetEditable(bool editable
)
193 // Can't implement in MSW?
194 // HWND hWnd = GetHwnd();
195 // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
198 void wxComboBox::SetInsertionPoint(long pos
)
201 HWND hWnd = GetHwnd();
202 SendMessage(hWnd, EM_SETSEL, pos, pos);
203 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
205 SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)nothing);
209 void wxComboBox::SetInsertionPointEnd()
212 long pos = GetLastPosition();
213 SetInsertionPoint(pos);
217 long wxComboBox::GetInsertionPoint() const
220 DWORD Pos=(DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L);
226 long wxComboBox::GetLastPosition() const
229 HWND hWnd = GetHwnd();
231 // Will always return a number > 0 (according to docs)
232 int noLines = (int)SendMessage(hWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0L);
234 // This gets the char index for the _beginning_ of the last line
235 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)(noLines-1), (LPARAM)0L);
237 // Get number of characters in the last line. We'll add this to the character
238 // index for the last line, 1st position.
239 int lineLength = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0L);
241 return (long)(charIndex + lineLength);
246 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
249 HWND hWnd
= GetHwnd();
250 long fromChar
= from
;
253 // Set selection and remove it
254 // SendMessage(hWnd, CB_SETEDITSEL, fromChar, toChar);
255 // SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
257 // Now replace with 'value', by pasting.
258 wxSetClipboardData(wxDF_TEXT
, (wxObject
*)(const wxChar
*)value
, 0, 0);
260 // Paste into edit control
261 // SendMessage(hWnd, WM_PASTE, (WPARAM)0, (LPARAM)0L);
265 void wxComboBox::Remove(long from
, long to
)
267 HWND hWnd
= GetHwnd();
268 long fromChar
= from
;
271 // Cut all selected text
272 // SendMessage(hWnd, CB_SETEDITSEL, fromChar, toChar);
273 // SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
276 void wxComboBox::SetSelection(long from
, long to
)
278 HWND hWnd
= GetHwnd();
279 long fromChar
= from
;
281 // if from and to are both -1, it means
282 // (in wxWindows) that all text should be selected.
283 // This translates into Windows convention
284 if ((from
== -1) && (to
== -1))
290 // SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)fromChar, (LPARAM)toChar);
291 // SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
294 void wxComboBox::DoSetSize(int x
, int y
,
295 int width
, int height
,
298 wxControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);