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 if (GetSelection() > -1)
43 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
44 event
.SetInt(GetSelection());
45 event
.SetEventObject(this);
46 event
.SetString(GetStringSelection());
47 ProcessCommand(event
);
52 else if (param
== CBN_EDITCHANGE
)
54 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
55 event
.SetString(GetValue());
56 event
.SetEventObject(this);
57 ProcessCommand(event
);
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()) ;
78 SetForegroundColour(parent
->GetForegroundColour()) ;
80 m_windowStyle
= style
;
83 m_windowId
= (int)NewControlId();
92 long msStyle
= WS_CHILD
| WS_TABSTOP
| WS_VISIBLE
| WS_HSCROLL
| WS_VSCROLL
|
95 if (m_windowStyle
& wxCB_READONLY
)
96 msStyle
|= CBS_DROPDOWNLIST
;
97 else if (m_windowStyle
& wxCB_SIMPLE
)
98 msStyle
|= CBS_SIMPLE
; // A list (shown always) and edit control
100 msStyle
|= CBS_DROPDOWN
;
102 if (m_windowStyle
& wxCB_SORT
)
106 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
108 // Even with extended styles, need to combine with WS_BORDER
109 // for them to look right.
110 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
111 msStyle
|= WS_BORDER
;
113 m_hWnd
= (WXHWND
)::CreateWindowEx(exStyle
, wxT("COMBOBOX"), NULL
,
115 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
116 wxGetInstance(), NULL
);
118 wxCHECK_MSG( m_hWnd
, FALSE
, wxT("Failed to create combobox") );
124 Ctl3dSubclassCtl(wx_combo);
130 // Subclass again for purposes of dialog editing mode
133 SetFont(parent
->GetFont());
135 for (i
= 0; i
< n
; i
++)
142 SetSize(x
, y
, width
, height
);
143 if ( !value
.IsEmpty() )
151 void wxComboBox::SetValue(const wxString
& value
)
153 // If newlines are denoted by just 10, must stick 13 in front.
155 int len
= value
.Length();
157 for (i
= 0; i
< len
; i
++)
159 if ((i
> 0) && (value
[i
] == 10) && (value
[i
-1] != 13))
164 wxChar
*tmp
= new wxChar
[len
+ singletons
+ 1];
166 for (i
= 0; i
< len
; i
++)
168 if ((i
> 0) && (value
[i
] == 10) && (value
[i
-1] != 13))
177 SetWindowText(GetHwnd(), tmp
);
181 SetWindowText(GetHwnd(), value
);
184 // Clipboard operations
185 void wxComboBox::Copy()
187 HWND hWnd
= GetHwnd();
188 SendMessage(hWnd
, WM_COPY
, 0, 0L);
191 void wxComboBox::Cut()
193 HWND hWnd
= GetHwnd();
194 SendMessage(hWnd
, WM_CUT
, 0, 0L);
197 void wxComboBox::Paste()
199 HWND hWnd
= GetHwnd();
200 SendMessage(hWnd
, WM_PASTE
, 0, 0L);
203 void wxComboBox::SetEditable(bool editable
)
205 // Can't implement in MSW?
206 // HWND hWnd = GetHwnd();
207 // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
210 void wxComboBox::SetInsertionPoint(long pos
)
213 HWND hWnd = GetHwnd();
215 SendMessage(hWnd, EM_SETSEL, pos, pos);
216 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
218 SendMessage(hWnd, EM_SETSEL, 0, MAKELPARAM(pos, pos));
221 SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)nothing);
225 void wxComboBox::SetInsertionPointEnd()
228 long pos = GetLastPosition();
229 SetInsertionPoint(pos);
233 long wxComboBox::GetInsertionPoint() const
236 DWORD Pos=(DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L);
242 long wxComboBox::GetLastPosition() const
245 HWND hWnd = GetHwnd();
247 // Will always return a number > 0 (according to docs)
248 int noLines = (int)SendMessage(hWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0L);
250 // This gets the char index for the _beginning_ of the last line
251 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)(noLines-1), (LPARAM)0L);
253 // Get number of characters in the last line. We'll add this to the character
254 // index for the last line, 1st position.
255 int lineLength = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0L);
257 return (long)(charIndex + lineLength);
262 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
265 HWND hWnd
= GetHwnd();
266 long fromChar
= from
;
269 // Set selection and remove it
271 SendMessage(hWnd
, CB_SETEDITSEL
, fromChar
, toChar
);
273 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
275 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
277 // Now replace with 'value', by pasting.
278 wxSetClipboardData(wxDF_TEXT
, (wxObject
*)(const wxChar
*)value
, 0, 0);
280 // Paste into edit control
281 SendMessage(hWnd
, WM_PASTE
, (WPARAM
)0, (LPARAM
)0L);
285 void wxComboBox::Remove(long from
, long to
)
287 HWND hWnd
= GetHwnd();
288 long fromChar
= from
;
291 // Cut all selected text
293 SendMessage(hWnd
, CB_SETEDITSEL
, fromChar
, toChar
);
295 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
297 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
300 void wxComboBox::SetSelection(long from
, long to
)
302 HWND hWnd
= GetHwnd();
303 long fromChar
= from
;
305 // if from and to are both -1, it means
306 // (in wxWindows) that all text should be selected.
307 // This translates into Windows convention
308 if ((from
== -1) && (to
== -1))
315 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)fromChar
, (LPARAM
)toChar
);
316 // SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
318 // WPARAM is 0: selection is scrolled into view
319 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
323 void wxComboBox::DoSetSize(int x
, int y
,
324 int width
, int height
,
327 wxControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);