]> git.saurik.com Git - wxWidgets.git/blob - src/msw/combobox.cpp
grid autosize fixes/changes
[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 #if wxUSE_VALIDATORS
76 SetValidator(validator);
77 #endif // wxUSE_VALIDATORS
78 if (parent) parent->AddChild(this);
79 // SetBackgroundColour(parent->GetBackgroundColour()) ;
80
81 // A choice/combobox normally has a white background (or other, depending
82 // on global settings) rather than inheriting the parent's background colour.
83 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
84
85 SetForegroundColour(parent->GetForegroundColour()) ;
86
87 m_windowStyle = style;
88
89 if ( id == -1 )
90 m_windowId = (int)NewControlId();
91 else
92 m_windowId = id;
93
94 int x = pos.x;
95 int y = pos.y;
96 int width = size.x;
97 int height = size.y;
98
99 long msStyle = WS_CHILD | WS_TABSTOP | WS_VISIBLE |
100 WS_VSCROLL | WS_HSCROLL | CBS_AUTOHSCROLL | CBS_NOINTEGRALHEIGHT;
101
102 if (m_windowStyle & wxCB_READONLY)
103 msStyle |= CBS_DROPDOWNLIST;
104 else if (m_windowStyle & wxCB_SIMPLE)
105 msStyle |= CBS_SIMPLE; // A list (shown always) and edit control
106 else
107 msStyle |= CBS_DROPDOWN;
108
109 if (m_windowStyle & wxCB_SORT)
110 msStyle |= CBS_SORT;
111
112 bool want3D;
113 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ;
114
115 // Even with extended styles, need to combine with WS_BORDER
116 // for them to look right.
117 if ( want3D || wxStyleHasBorder(m_windowStyle) )
118 msStyle |= WS_BORDER;
119
120 m_hWnd = (WXHWND)::CreateWindowEx(exStyle, wxT("COMBOBOX"), NULL,
121 msStyle,
122 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
123 wxGetInstance(), NULL);
124
125 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create combobox") );
126
127 /*
128 #if wxUSE_CTL3D
129 if (want3D)
130 {
131 Ctl3dSubclassCtl(wx_combo);
132 m_useCtl3D = TRUE;
133 }
134 #endif
135 */
136
137 // Subclass again for purposes of dialog editing mode
138 SubclassWin(m_hWnd);
139
140 SetFont(parent->GetFont());
141 int i;
142 for (i = 0; i < n; i++)
143 {
144 Append(choices[i]);
145 }
146
147 SetSelection(i);
148
149 SetSize(x, y, width, height);
150 if ( !value.IsEmpty() )
151 {
152 SetValue(value);
153 }
154
155 return TRUE;
156 }
157
158 void wxComboBox::SetValue(const wxString& value)
159 {
160 // If newlines are denoted by just 10, must stick 13 in front.
161 int singletons = 0;
162 int len = value.Length();
163 int i;
164 for (i = 0; i < len; i ++)
165 {
166 if ((i > 0) && (value[i] == 10) && (value[i-1] != 13))
167 singletons ++;
168 }
169 if (singletons > 0)
170 {
171 wxChar *tmp = new wxChar[len + singletons + 1];
172 int j = 0;
173 for (i = 0; i < len; i ++)
174 {
175 if ((i > 0) && (value[i] == 10) && (value[i-1] != 13))
176 {
177 tmp[j] = 13;
178 j ++;
179 }
180 tmp[j] = value[i];
181 j ++;
182 }
183 tmp[j] = 0;
184 SetWindowText(GetHwnd(), tmp);
185 delete[] tmp;
186 }
187 else
188 SetWindowText(GetHwnd(), value);
189 }
190
191 // Clipboard operations
192 void wxComboBox::Copy()
193 {
194 HWND hWnd = GetHwnd();
195 SendMessage(hWnd, WM_COPY, 0, 0L);
196 }
197
198 void wxComboBox::Cut()
199 {
200 HWND hWnd = GetHwnd();
201 SendMessage(hWnd, WM_CUT, 0, 0L);
202 }
203
204 void wxComboBox::Paste()
205 {
206 HWND hWnd = GetHwnd();
207 SendMessage(hWnd, WM_PASTE, 0, 0L);
208 }
209
210 void wxComboBox::SetEditable(bool editable)
211 {
212 // Can't implement in MSW?
213 // HWND hWnd = GetHwnd();
214 // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
215 }
216
217 void wxComboBox::SetInsertionPoint(long pos)
218 {
219 /*
220 HWND hWnd = GetHwnd();
221 #ifdef __WIN32__
222 SendMessage(hWnd, EM_SETSEL, pos, pos);
223 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
224 #else
225 SendMessage(hWnd, EM_SETSEL, 0, MAKELPARAM(pos, pos));
226 #endif
227 char *nothing = "";
228 SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)nothing);
229 */
230 }
231
232 void wxComboBox::SetInsertionPointEnd()
233 {
234 /*
235 long pos = GetLastPosition();
236 SetInsertionPoint(pos);
237 */
238 }
239
240 long wxComboBox::GetInsertionPoint() const
241 {
242 /*
243 DWORD Pos=(DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L);
244 return Pos&0xFFFF;
245 */
246 return 0;
247 }
248
249 long wxComboBox::GetLastPosition() const
250 {
251 /*
252 HWND hWnd = GetHwnd();
253
254 // Will always return a number > 0 (according to docs)
255 int noLines = (int)SendMessage(hWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0L);
256
257 // This gets the char index for the _beginning_ of the last line
258 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)(noLines-1), (LPARAM)0L);
259
260 // Get number of characters in the last line. We'll add this to the character
261 // index for the last line, 1st position.
262 int lineLength = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0L);
263
264 return (long)(charIndex + lineLength);
265 */
266 return 0;
267 }
268
269 void wxComboBox::Replace(long from, long to, const wxString& value)
270 {
271 #if wxUSE_CLIPBOARD
272 HWND hWnd = GetHwnd();
273 long fromChar = from;
274 long toChar = to;
275
276 // Set selection and remove it
277 #ifdef __WIN32__
278 SendMessage(hWnd, CB_SETEDITSEL, fromChar, toChar);
279 #else
280 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
281 #endif
282 SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
283
284 // Now replace with 'value', by pasting.
285 wxSetClipboardData(wxDF_TEXT, (wxObject *)(const wxChar *)value, 0, 0);
286
287 // Paste into edit control
288 SendMessage(hWnd, WM_PASTE, (WPARAM)0, (LPARAM)0L);
289 #endif
290 }
291
292 void wxComboBox::Remove(long from, long to)
293 {
294 HWND hWnd = GetHwnd();
295 long fromChar = from;
296 long toChar = to;
297
298 // Cut all selected text
299 #ifdef __WIN32__
300 SendMessage(hWnd, CB_SETEDITSEL, fromChar, toChar);
301 #else
302 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
303 #endif
304 SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
305 }
306
307 void wxComboBox::SetSelection(long from, long to)
308 {
309 HWND hWnd = GetHwnd();
310 long fromChar = from;
311 long toChar = to;
312 // if from and to are both -1, it means
313 // (in wxWindows) that all text should be selected.
314 // This translates into Windows convention
315 if ((from == -1) && (to == -1))
316 {
317 fromChar = 0;
318 toChar = -1;
319 }
320
321 #ifdef __WIN32__
322 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)fromChar, (LPARAM)toChar);
323 // SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
324 #else
325 // WPARAM is 0: selection is scrolled into view
326 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
327 #endif
328 }
329
330 void wxComboBox::DoMoveWindow(int x, int y, int width, int height)
331 {
332 int cx, cy;
333 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
334
335 // what should the height of the drop down list be? we choose 10 items by
336 // default and also 10 items max (if we always use n, the list will never
337 // have vertical scrollbar)
338 int n = GetCount();
339 if ( !n )
340 n = 10;
341 else if ( n > 10 )
342 n = 10;
343
344 height = n * EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
345
346 wxControl::DoMoveWindow(x, y, width, height);
347 }
348
349 wxSize wxComboBox::DoGetBestSize() const
350 {
351 // the choice calculates the horz size correctly, but not the vertical
352 // component: correct it
353 wxSize size = wxChoice::DoGetBestSize();
354
355 int cx, cy;
356 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
357 size.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
358
359 return size;
360 }
361
362 #endif
363 // wxUSE_COMBOBOX
364