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