]> git.saurik.com Git - wxWidgets.git/blob - src/msw/combobox.cpp
wxCalendarCtrl works under MSW too
[wxWidgets.git] / src / msw / combobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: combobox.cpp
3 // Purpose: wxComboBox class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "combobox.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #if wxUSE_COMBOBOX
24
25 #ifndef WX_PRECOMP
26 #include "wx/settings.h"
27 #endif
28
29 #include "wx/combobox.h"
30 #include "wx/clipbrd.h"
31 #include "wx/msw/private.h"
32
33 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
34
35 bool wxComboBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
36 {
37 switch ( param )
38 {
39 case CBN_SELCHANGE:
40 if (GetSelection() > -1)
41 {
42 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId());
43 event.SetInt(GetSelection());
44 event.SetEventObject(this);
45 event.SetString(GetStringSelection());
46 ProcessCommand(event);
47 }
48 break;
49
50 case CBN_EDITCHANGE:
51 {
52 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
53 event.SetString(GetValue());
54 event.SetEventObject(this);
55 ProcessCommand(event);
56 }
57 break;
58 }
59
60 // there is no return value for the CBN_ notifications, so always return
61 // FALSE from here to pass the message to DefWindowProc()
62 return FALSE;
63 }
64
65 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
66 const wxString& value,
67 const wxPoint& pos,
68 const wxSize& size,
69 int n, const wxString choices[],
70 long style,
71 const wxValidator& validator,
72 const wxString& name)
73 {
74 SetName(name);
75 SetValidator(validator);
76 if (parent) parent->AddChild(this);
77 // SetBackgroundColour(parent->GetBackgroundColour()) ;
78
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));
82
83 SetForegroundColour(parent->GetForegroundColour()) ;
84
85 m_windowStyle = style;
86
87 if ( id == -1 )
88 m_windowId = (int)NewControlId();
89 else
90 m_windowId = id;
91
92 int x = pos.x;
93 int y = pos.y;
94 int width = size.x;
95 int height = size.y;
96
97 long msStyle = WS_CHILD | WS_TABSTOP | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL |
98 CBS_NOINTEGRALHEIGHT;
99
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
104 else
105 msStyle |= CBS_DROPDOWN;
106
107 if (m_windowStyle & wxCB_SORT)
108 msStyle |= CBS_SORT;
109
110 bool want3D;
111 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ;
112
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;
117
118 m_hWnd = (WXHWND)::CreateWindowEx(exStyle, wxT("COMBOBOX"), NULL,
119 msStyle,
120 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
121 wxGetInstance(), NULL);
122
123 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create combobox") );
124
125 /*
126 #if wxUSE_CTL3D
127 if (want3D)
128 {
129 Ctl3dSubclassCtl(wx_combo);
130 m_useCtl3D = TRUE;
131 }
132 #endif
133 */
134
135 // Subclass again for purposes of dialog editing mode
136 SubclassWin(m_hWnd);
137
138 SetFont(parent->GetFont());
139 int i;
140 for (i = 0; i < n; i++)
141 {
142 Append(choices[i]);
143 }
144
145 SetSelection(i);
146
147 SetSize(x, y, width, height);
148 if ( !value.IsEmpty() )
149 {
150 SetValue(value);
151 }
152
153 return TRUE;
154 }
155
156 void wxComboBox::SetValue(const wxString& value)
157 {
158 // If newlines are denoted by just 10, must stick 13 in front.
159 int singletons = 0;
160 int len = value.Length();
161 int i;
162 for (i = 0; i < len; i ++)
163 {
164 if ((i > 0) && (value[i] == 10) && (value[i-1] != 13))
165 singletons ++;
166 }
167 if (singletons > 0)
168 {
169 wxChar *tmp = new wxChar[len + singletons + 1];
170 int j = 0;
171 for (i = 0; i < len; i ++)
172 {
173 if ((i > 0) && (value[i] == 10) && (value[i-1] != 13))
174 {
175 tmp[j] = 13;
176 j ++;
177 }
178 tmp[j] = value[i];
179 j ++;
180 }
181 tmp[j] = 0;
182 SetWindowText(GetHwnd(), tmp);
183 delete[] tmp;
184 }
185 else
186 SetWindowText(GetHwnd(), value);
187 }
188
189 // Clipboard operations
190 void wxComboBox::Copy()
191 {
192 HWND hWnd = GetHwnd();
193 SendMessage(hWnd, WM_COPY, 0, 0L);
194 }
195
196 void wxComboBox::Cut()
197 {
198 HWND hWnd = GetHwnd();
199 SendMessage(hWnd, WM_CUT, 0, 0L);
200 }
201
202 void wxComboBox::Paste()
203 {
204 HWND hWnd = GetHwnd();
205 SendMessage(hWnd, WM_PASTE, 0, 0L);
206 }
207
208 void wxComboBox::SetEditable(bool editable)
209 {
210 // Can't implement in MSW?
211 // HWND hWnd = GetHwnd();
212 // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
213 }
214
215 void wxComboBox::SetInsertionPoint(long pos)
216 {
217 /*
218 HWND hWnd = GetHwnd();
219 #ifdef __WIN32__
220 SendMessage(hWnd, EM_SETSEL, pos, pos);
221 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
222 #else
223 SendMessage(hWnd, EM_SETSEL, 0, MAKELPARAM(pos, pos));
224 #endif
225 char *nothing = "";
226 SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)nothing);
227 */
228 }
229
230 void wxComboBox::SetInsertionPointEnd()
231 {
232 /*
233 long pos = GetLastPosition();
234 SetInsertionPoint(pos);
235 */
236 }
237
238 long wxComboBox::GetInsertionPoint() const
239 {
240 /*
241 DWORD Pos=(DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L);
242 return Pos&0xFFFF;
243 */
244 return 0;
245 }
246
247 long wxComboBox::GetLastPosition() const
248 {
249 /*
250 HWND hWnd = GetHwnd();
251
252 // Will always return a number > 0 (according to docs)
253 int noLines = (int)SendMessage(hWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0L);
254
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);
257
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);
261
262 return (long)(charIndex + lineLength);
263 */
264 return 0;
265 }
266
267 void wxComboBox::Replace(long from, long to, const wxString& value)
268 {
269 #if wxUSE_CLIPBOARD
270 HWND hWnd = GetHwnd();
271 long fromChar = from;
272 long toChar = to;
273
274 // Set selection and remove it
275 #ifdef __WIN32__
276 SendMessage(hWnd, CB_SETEDITSEL, fromChar, toChar);
277 #else
278 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
279 #endif
280 SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
281
282 // Now replace with 'value', by pasting.
283 wxSetClipboardData(wxDF_TEXT, (wxObject *)(const wxChar *)value, 0, 0);
284
285 // Paste into edit control
286 SendMessage(hWnd, WM_PASTE, (WPARAM)0, (LPARAM)0L);
287 #endif
288 }
289
290 void wxComboBox::Remove(long from, long to)
291 {
292 HWND hWnd = GetHwnd();
293 long fromChar = from;
294 long toChar = to;
295
296 // Cut all selected text
297 #ifdef __WIN32__
298 SendMessage(hWnd, CB_SETEDITSEL, fromChar, toChar);
299 #else
300 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
301 #endif
302 SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
303 }
304
305 void wxComboBox::SetSelection(long from, long to)
306 {
307 HWND hWnd = GetHwnd();
308 long fromChar = from;
309 long toChar = to;
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))
314 {
315 fromChar = 0;
316 toChar = -1;
317 }
318
319 #ifdef __WIN32__
320 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)fromChar, (LPARAM)toChar);
321 // SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
322 #else
323 // WPARAM is 0: selection is scrolled into view
324 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
325 #endif
326 }
327
328 void wxComboBox::DoMoveWindow(int x, int y, int width, int height)
329 {
330 int cx, cy;
331 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
332
333 int n = GetCount();
334 if ( !n )
335 n = 10;
336
337 height = n * EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
338
339 wxControl::DoMoveWindow(x, y, width, height);
340 }
341
342 wxSize wxComboBox::DoGetBestSize() const
343 {
344 // the choice calculates the horz size correctly, but not the vertical
345 // component: correct it
346 wxSize size = wxChoice::DoGetBestSize();
347
348 int cx, cy;
349 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
350 size.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
351
352 return size;
353 }
354
355 #endif
356 // wxUSE_COMBOBOX
357