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