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