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