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