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
))
37 if (param
== CBN_SELCHANGE
)
39 if (GetSelection() > -1)
41 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
42 event
.SetInt(GetSelection());
43 event
.SetEventObject(this);
44 event
.SetString(GetStringSelection());
45 ProcessCommand(event
);
50 else if (param
== CBN_EDITCHANGE
)
52 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
53 event
.SetString(GetValue());
54 event
.SetEventObject(this);
55 ProcessCommand(event
);
63 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
64 const wxString
& value
,
67 int n
, const wxString choices
[],
69 const wxValidator
& validator
,
73 SetValidator(validator
);
74 if (parent
) parent
->AddChild(this);
75 // SetBackgroundColour(parent->GetBackgroundColour()) ;
77 // A choice/combobox normally has a white background (or other, depending
78 // on global settings) rather than inheriting the parent's background colour.
79 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
81 SetForegroundColour(parent
->GetForegroundColour()) ;
83 m_windowStyle
= style
;
86 m_windowId
= (int)NewControlId();
95 long msStyle
= WS_CHILD
| WS_TABSTOP
| WS_VISIBLE
| WS_HSCROLL
| WS_VSCROLL
|
98 if (m_windowStyle
& wxCB_READONLY
)
99 msStyle
|= CBS_DROPDOWNLIST
;
100 else if (m_windowStyle
& wxCB_SIMPLE
)
101 msStyle
|= CBS_SIMPLE
; // A list (shown always) and edit control
103 msStyle
|= CBS_DROPDOWN
;
105 if (m_windowStyle
& wxCB_SORT
)
109 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
111 // Even with extended styles, need to combine with WS_BORDER
112 // for them to look right.
113 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
114 msStyle
|= WS_BORDER
;
116 m_hWnd
= (WXHWND
)::CreateWindowEx(exStyle
, wxT("COMBOBOX"), NULL
,
118 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
119 wxGetInstance(), NULL
);
121 wxCHECK_MSG( m_hWnd
, FALSE
, wxT("Failed to create combobox") );
127 Ctl3dSubclassCtl(wx_combo);
133 // Subclass again for purposes of dialog editing mode
136 SetFont(parent
->GetFont());
138 for (i
= 0; i
< n
; i
++)
145 SetSize(x
, y
, width
, height
);
146 if ( !value
.IsEmpty() )
154 void wxComboBox::SetValue(const wxString
& value
)
156 // If newlines are denoted by just 10, must stick 13 in front.
158 int len
= value
.Length();
160 for (i
= 0; i
< len
; i
++)
162 if ((i
> 0) && (value
[i
] == 10) && (value
[i
-1] != 13))
167 wxChar
*tmp
= new wxChar
[len
+ singletons
+ 1];
169 for (i
= 0; i
< len
; i
++)
171 if ((i
> 0) && (value
[i
] == 10) && (value
[i
-1] != 13))
180 SetWindowText(GetHwnd(), tmp
);
184 SetWindowText(GetHwnd(), value
);
187 // Clipboard operations
188 void wxComboBox::Copy()
190 HWND hWnd
= GetHwnd();
191 SendMessage(hWnd
, WM_COPY
, 0, 0L);
194 void wxComboBox::Cut()
196 HWND hWnd
= GetHwnd();
197 SendMessage(hWnd
, WM_CUT
, 0, 0L);
200 void wxComboBox::Paste()
202 HWND hWnd
= GetHwnd();
203 SendMessage(hWnd
, WM_PASTE
, 0, 0L);
206 void wxComboBox::SetEditable(bool editable
)
208 // Can't implement in MSW?
209 // HWND hWnd = GetHwnd();
210 // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
213 void wxComboBox::SetInsertionPoint(long pos
)
216 HWND hWnd = GetHwnd();
218 SendMessage(hWnd, EM_SETSEL, pos, pos);
219 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
221 SendMessage(hWnd, EM_SETSEL, 0, MAKELPARAM(pos, pos));
224 SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)nothing);
228 void wxComboBox::SetInsertionPointEnd()
231 long pos = GetLastPosition();
232 SetInsertionPoint(pos);
236 long wxComboBox::GetInsertionPoint() const
239 DWORD Pos=(DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L);
245 long wxComboBox::GetLastPosition() const
248 HWND hWnd = GetHwnd();
250 // Will always return a number > 0 (according to docs)
251 int noLines = (int)SendMessage(hWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0L);
253 // This gets the char index for the _beginning_ of the last line
254 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)(noLines-1), (LPARAM)0L);
256 // Get number of characters in the last line. We'll add this to the character
257 // index for the last line, 1st position.
258 int lineLength = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0L);
260 return (long)(charIndex + lineLength);
265 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
268 HWND hWnd
= GetHwnd();
269 long fromChar
= from
;
272 // Set selection and remove it
274 SendMessage(hWnd
, CB_SETEDITSEL
, fromChar
, toChar
);
276 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
278 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
280 // Now replace with 'value', by pasting.
281 wxSetClipboardData(wxDF_TEXT
, (wxObject
*)(const wxChar
*)value
, 0, 0);
283 // Paste into edit control
284 SendMessage(hWnd
, WM_PASTE
, (WPARAM
)0, (LPARAM
)0L);
288 void wxComboBox::Remove(long from
, long to
)
290 HWND hWnd
= GetHwnd();
291 long fromChar
= from
;
294 // Cut all selected text
296 SendMessage(hWnd
, CB_SETEDITSEL
, fromChar
, toChar
);
298 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
300 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
303 void wxComboBox::SetSelection(long from
, long to
)
305 HWND hWnd
= GetHwnd();
306 long fromChar
= from
;
308 // if from and to are both -1, it means
309 // (in wxWindows) that all text should be selected.
310 // This translates into Windows convention
311 if ((from
== -1) && (to
== -1))
318 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)fromChar
, (LPARAM
)toChar
);
319 // SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
321 // WPARAM is 0: selection is scrolled into view
322 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
326 void wxComboBox::DoSetSize(int x
, int y
,
327 int width
, int height
,
330 wxControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);