]> git.saurik.com Git - wxWidgets.git/blob - src/msw/combobox.cpp
the modal dialogs restore the focus to the control which had it before the
[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 #ifndef WX_PRECOMP
24 #include "wx/setup.h"
25 #endif
26
27 #if USE_COMBOBOX
28
29 #include "wx/combobox.h"
30 #include "wx/clipbrd.h"
31 #include "wx/msw/private.h"
32
33 #if !USE_SHARED_LIBRARY
34 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
35 #endif
36
37 bool wxComboBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
38 {
39 if (param == CBN_SELCHANGE)
40 {
41 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId);
42 event.SetInt(GetSelection());
43 event.SetEventObject(this);
44 event.SetString(copystring(GetStringSelection()));
45 ProcessCommand(event);
46 delete[] event.GetString();
47 return TRUE;
48 }
49 else if (param == CBN_EDITCHANGE)
50 {
51 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
52 event.SetString(copystring(GetValue()));
53 ProcessCommand(event);
54 delete[] event.GetString();
55 return TRUE;
56 }
57 else return FALSE;
58 }
59
60 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
61 const wxString& value,
62 const wxPoint& pos,
63 const wxSize& size,
64 int n, const wxString choices[],
65 long style,
66 const wxValidator& validator,
67 const wxString& name)
68 {
69 SetName(name);
70 SetValidator(validator);
71 if (parent) parent->AddChild(this);
72 SetBackgroundColour(parent->GetDefaultBackgroundColour()) ;
73 SetForegroundColour(parent->GetDefaultForegroundColour()) ;
74 no_strings = n;
75
76 m_windowStyle = style;
77
78 if ( id == -1 )
79 m_windowId = (int)NewControlId();
80 else
81 m_windowId = id;
82
83 int x = pos.x;
84 int y = pos.y;
85 int width = size.x;
86 int height = size.y;
87
88 long msStyle = WS_CHILD | WS_HSCROLL | WS_VSCROLL
89 | WS_TABSTOP | WS_VISIBLE | CBS_NOINTEGRALHEIGHT;
90 if (m_windowStyle & wxCB_READONLY)
91 msStyle |= CBS_DROPDOWNLIST;
92 else if (m_windowStyle & wxCB_SIMPLE) // A list (shown always) and edit control
93 msStyle |= CBS_SIMPLE;
94 else
95 msStyle |= CBS_DROPDOWN;
96
97 if (m_windowStyle & wxCB_SORT)
98 msStyle |= CBS_SORT;
99
100 bool want3D;
101 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ;
102
103 // Even with extended styles, need to combine with WS_BORDER
104 // for them to look right.
105 if (want3D || (m_windowStyle & wxSIMPLE_BORDER) || (m_windowStyle & wxRAISED_BORDER) ||
106 (m_windowStyle & wxSUNKEN_BORDER) || (m_windowStyle & wxDOUBLE_BORDER))
107 msStyle |= WS_BORDER;
108
109 HWND wx_combo = CreateWindowEx(exStyle, "COMBOBOX", NULL,
110 msStyle,
111 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
112 wxGetInstance(), NULL);
113 /*
114 #if CTL3D
115 if (want3D)
116 {
117 Ctl3dSubclassCtl(wx_combo);
118 m_useCtl3D = TRUE;
119 }
120 #endif
121 */
122
123 m_hWnd = (WXHWND)wx_combo;
124
125 // Subclass again for purposes of dialog editing mode
126 SubclassWin((WXHWND)wx_combo);
127
128 SetFont(* parent->GetFont());
129 int i;
130 for (i = 0; i < n; i++)
131 SendMessage(wx_combo, CB_INSERTSTRING, i, (LONG)(const char *)choices[i]);
132 SendMessage(wx_combo, CB_SETCURSEL, i, 0);
133
134 SetSize(x, y, width, height);
135 if ( value != "" )
136 SetWindowText(wx_combo, (const char *)value);
137
138 return TRUE;
139 }
140
141 wxString wxComboBox::GetValue(void) const
142 {
143 GetWindowText((HWND) GetHWND(), wxBuffer, 500);
144 return wxString(wxBuffer);
145 }
146
147 void wxComboBox::SetValue(const wxString& value)
148 {
149 // If newlines are denoted by just 10, must stick 13 in front.
150 int singletons = 0;
151 int len = value.Length();
152 int i;
153 for (i = 0; i < len; i ++)
154 {
155 if ((i > 0) && (value[i] == 10) && (value[i-1] != 13))
156 singletons ++;
157 }
158 if (singletons > 0)
159 {
160 char *tmp = new char[len + singletons + 1];
161 int j = 0;
162 for (i = 0; i < len; i ++)
163 {
164 if ((i > 0) && (value[i] == 10) && (value[i-1] != 13))
165 {
166 tmp[j] = 13;
167 j ++;
168 }
169 tmp[j] = value[i];
170 j ++;
171 }
172 tmp[j] = 0;
173 SetWindowText((HWND) GetHWND(), tmp);
174 delete[] tmp;
175 }
176 else
177 SetWindowText((HWND) GetHWND(), (const char *)value);
178 }
179
180 // Clipboard operations
181 void wxComboBox::Copy(void)
182 {
183 HWND hWnd = (HWND) GetHWND();
184 SendMessage(hWnd, WM_COPY, 0, 0L);
185 }
186
187 void wxComboBox::Cut(void)
188 {
189 HWND hWnd = (HWND) GetHWND();
190 SendMessage(hWnd, WM_CUT, 0, 0L);
191 }
192
193 void wxComboBox::Paste(void)
194 {
195 HWND hWnd = (HWND) GetHWND();
196 SendMessage(hWnd, WM_PASTE, 0, 0L);
197 }
198
199 void wxComboBox::SetEditable(bool editable)
200 {
201 // Can't implement in MSW?
202 // HWND hWnd = (HWND) GetHWND();
203 // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
204 }
205
206 void wxComboBox::SetInsertionPoint(long pos)
207 {
208 /*
209 HWND hWnd = (HWND) GetHWND();
210 #ifdef __WIN32__
211 SendMessage(hWnd, EM_SETSEL, pos, pos);
212 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
213 #else
214 SendMessage(hWnd, EM_SETSEL, 0, MAKELPARAM(pos, pos));
215 #endif
216 char *nothing = "";
217 SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)nothing);
218 */
219 }
220
221 void wxComboBox::SetInsertionPointEnd(void)
222 {
223 /*
224 long pos = GetLastPosition();
225 SetInsertionPoint(pos);
226 */
227 }
228
229 long wxComboBox::GetInsertionPoint(void) const
230 {
231 /*
232 DWORD Pos=(DWORD)SendMessage((HWND) GetHWND(), EM_GETSEL, 0, 0L);
233 return Pos&0xFFFF;
234 */
235 return 0;
236 }
237
238 long wxComboBox::GetLastPosition(void) const
239 {
240 /*
241 HWND hWnd = (HWND) GetHWND();
242
243 // Will always return a number > 0 (according to docs)
244 int noLines = (int)SendMessage(hWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0L);
245
246 // This gets the char index for the _beginning_ of the last line
247 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)(noLines-1), (LPARAM)0L);
248
249 // Get number of characters in the last line. We'll add this to the character
250 // index for the last line, 1st position.
251 int lineLength = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0L);
252
253 return (long)(charIndex + lineLength);
254 */
255 return 0;
256 }
257
258 void wxComboBox::Replace(long from, long to, const wxString& value)
259 {
260 #if USE_CLIPBOARD
261 HWND hWnd = (HWND) GetHWND();
262 long fromChar = from;
263 long toChar = to;
264
265 // Set selection and remove it
266 #ifdef __WIN32__
267 SendMessage(hWnd, CB_SETEDITSEL, fromChar, toChar);
268 #else
269 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
270 #endif
271 SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
272
273 // Now replace with 'value', by pasting.
274 wxSetClipboardData(wxDF_TEXT, (wxObject *)(const char *)value, 0, 0);
275
276 // Paste into edit control
277 SendMessage(hWnd, WM_PASTE, (WPARAM)0, (LPARAM)0L);
278 #endif
279 }
280
281 void wxComboBox::Remove(long from, long to)
282 {
283 HWND hWnd = (HWND) GetHWND();
284 long fromChar = from;
285 long toChar = to;
286
287 // Cut all selected text
288 #ifdef __WIN32__
289 SendMessage(hWnd, CB_SETEDITSEL, fromChar, toChar);
290 #else
291 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
292 #endif
293 SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
294 }
295
296 void wxComboBox::SetSelection(long from, long to)
297 {
298 HWND hWnd = (HWND) GetHWND();
299 long fromChar = from;
300 long toChar = to;
301 // if from and to are both -1, it means
302 // (in wxWindows) that all text should be selected.
303 // This translates into Windows convention
304 if ((from == -1) && (to == -1))
305 {
306 fromChar = 0;
307 toChar = -1;
308 }
309
310 #ifdef __WIN32__
311 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)fromChar, (LPARAM)toChar);
312 // SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
313 #else
314 // WPARAM is 0: selection is scrolled into view
315 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
316 #endif
317 }
318
319 #endif
320 // USE_COMBOBOX
321