]>
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 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/setup.h" | |
25 | #endif | |
26 | ||
47d67540 | 27 | #if wxUSE_COMBOBOX |
2bda0e17 KB |
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 | ||
debe6624 | 37 | bool wxComboBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) |
2bda0e17 KB |
38 | { |
39 | if (param == CBN_SELCHANGE) | |
40 | { | |
5de76427 | 41 | wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId); |
2bda0e17 KB |
42 | event.SetInt(GetSelection()); |
43 | event.SetEventObject(this); | |
29006414 | 44 | event.SetString(GetStringSelection()); |
2bda0e17 | 45 | ProcessCommand(event); |
29006414 | 46 | |
2bda0e17 KB |
47 | return TRUE; |
48 | } | |
5de76427 JS |
49 | else if (param == CBN_EDITCHANGE) |
50 | { | |
51 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId); | |
29006414 | 52 | event.SetString(GetValue()); |
088a95f5 | 53 | event.SetEventObject(this); |
5de76427 | 54 | ProcessCommand(event); |
29006414 | 55 | |
5de76427 JS |
56 | return TRUE; |
57 | } | |
29006414 VZ |
58 | else |
59 | return FALSE; | |
2bda0e17 KB |
60 | } |
61 | ||
debe6624 | 62 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, |
c085e333 VZ |
63 | const wxString& value, |
64 | const wxPoint& pos, | |
65 | const wxSize& size, | |
66 | int n, const wxString choices[], | |
67 | long style, | |
68 | const wxValidator& validator, | |
69 | const wxString& name) | |
2bda0e17 KB |
70 | { |
71 | SetName(name); | |
72 | SetValidator(validator); | |
73 | if (parent) parent->AddChild(this); | |
fd71308f JS |
74 | SetBackgroundColour(parent->GetBackgroundColour()) ; |
75 | SetForegroundColour(parent->GetForegroundColour()) ; | |
2bda0e17 KB |
76 | |
77 | m_windowStyle = style; | |
78 | ||
79 | if ( id == -1 ) | |
c085e333 | 80 | m_windowId = (int)NewControlId(); |
2bda0e17 | 81 | else |
c085e333 | 82 | m_windowId = id; |
2bda0e17 KB |
83 | |
84 | int x = pos.x; | |
85 | int y = pos.y; | |
86 | int width = size.x; | |
87 | int height = size.y; | |
88 | ||
b2a9d5b4 JS |
89 | long msStyle = WS_CHILD | WS_TABSTOP | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | |
90 | CBS_NOINTEGRALHEIGHT; | |
c085e333 | 91 | |
2bda0e17 KB |
92 | if (m_windowStyle & wxCB_READONLY) |
93 | msStyle |= CBS_DROPDOWNLIST; | |
c085e333 VZ |
94 | else if (m_windowStyle & wxCB_SIMPLE) |
95 | msStyle |= CBS_SIMPLE; // A list (shown always) and edit control | |
2bda0e17 KB |
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. | |
c085e333 | 107 | if ( want3D || wxStyleHasBorder(m_windowStyle) ) |
2bda0e17 KB |
108 | msStyle |= WS_BORDER; |
109 | ||
837e5743 | 110 | m_hWnd = (WXHWND)::CreateWindowEx(exStyle, _T("COMBOBOX"), NULL, |
2bda0e17 KB |
111 | msStyle, |
112 | 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId, | |
113 | wxGetInstance(), NULL); | |
c085e333 | 114 | |
837e5743 | 115 | wxCHECK_MSG( m_hWnd, FALSE, _T("Failed to create combobox") ); |
c085e333 | 116 | |
2bda0e17 | 117 | /* |
1f112209 | 118 | #if wxUSE_CTL3D |
2bda0e17 KB |
119 | if (want3D) |
120 | { | |
121 | Ctl3dSubclassCtl(wx_combo); | |
122 | m_useCtl3D = TRUE; | |
123 | } | |
124 | #endif | |
125 | */ | |
126 | ||
2bda0e17 | 127 | // Subclass again for purposes of dialog editing mode |
c085e333 | 128 | SubclassWin(m_hWnd); |
2bda0e17 | 129 | |
c0ed460c | 130 | SetFont(parent->GetFont()); |
2bda0e17 KB |
131 | int i; |
132 | for (i = 0; i < n; i++) | |
c085e333 VZ |
133 | { |
134 | Append(choices[i]); | |
135 | } | |
136 | ||
137 | SetSelection(i); | |
2bda0e17 KB |
138 | |
139 | SetSize(x, y, width, height); | |
c085e333 VZ |
140 | if ( !value.IsEmpty() ) |
141 | { | |
142 | SetValue(value); | |
143 | } | |
2bda0e17 KB |
144 | |
145 | return TRUE; | |
146 | } | |
147 | ||
2bda0e17 KB |
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 | { | |
837e5743 | 161 | wxChar *tmp = new wxChar[len + singletons + 1]; |
2bda0e17 KB |
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; | |
4438caf4 | 174 | SetWindowText(GetHwnd(), tmp); |
2bda0e17 KB |
175 | delete[] tmp; |
176 | } | |
177 | else | |
4438caf4 | 178 | SetWindowText(GetHwnd(), value); |
2bda0e17 KB |
179 | } |
180 | ||
181 | // Clipboard operations | |
1c4a764c | 182 | void wxComboBox::Copy() |
2bda0e17 | 183 | { |
4438caf4 | 184 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
185 | SendMessage(hWnd, WM_COPY, 0, 0L); |
186 | } | |
187 | ||
1c4a764c | 188 | void wxComboBox::Cut() |
2bda0e17 | 189 | { |
4438caf4 | 190 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
191 | SendMessage(hWnd, WM_CUT, 0, 0L); |
192 | } | |
193 | ||
1c4a764c | 194 | void wxComboBox::Paste() |
2bda0e17 | 195 | { |
4438caf4 | 196 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
197 | SendMessage(hWnd, WM_PASTE, 0, 0L); |
198 | } | |
199 | ||
debe6624 | 200 | void wxComboBox::SetEditable(bool editable) |
2bda0e17 KB |
201 | { |
202 | // Can't implement in MSW? | |
4438caf4 | 203 | // HWND hWnd = GetHwnd(); |
2bda0e17 KB |
204 | // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L); |
205 | } | |
206 | ||
debe6624 | 207 | void wxComboBox::SetInsertionPoint(long pos) |
2bda0e17 KB |
208 | { |
209 | /* | |
4438caf4 | 210 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
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 | ||
1c4a764c | 222 | void wxComboBox::SetInsertionPointEnd() |
2bda0e17 KB |
223 | { |
224 | /* | |
225 | long pos = GetLastPosition(); | |
226 | SetInsertionPoint(pos); | |
227 | */ | |
228 | } | |
229 | ||
1c4a764c | 230 | long wxComboBox::GetInsertionPoint() const |
2bda0e17 KB |
231 | { |
232 | /* | |
4438caf4 | 233 | DWORD Pos=(DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L); |
2bda0e17 KB |
234 | return Pos&0xFFFF; |
235 | */ | |
236 | return 0; | |
237 | } | |
238 | ||
1c4a764c | 239 | long wxComboBox::GetLastPosition() const |
2bda0e17 KB |
240 | { |
241 | /* | |
4438caf4 | 242 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
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); | |
4438caf4 | 249 | |
2bda0e17 KB |
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 | ||
debe6624 | 259 | void wxComboBox::Replace(long from, long to, const wxString& value) |
2bda0e17 | 260 | { |
47d67540 | 261 | #if wxUSE_CLIPBOARD |
4438caf4 | 262 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
263 | long fromChar = from; |
264 | long toChar = to; | |
4438caf4 | 265 | |
2bda0e17 KB |
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. | |
837e5743 | 275 | wxSetClipboardData(wxDF_TEXT, (wxObject *)(const wxChar *)value, 0, 0); |
2bda0e17 KB |
276 | |
277 | // Paste into edit control | |
278 | SendMessage(hWnd, WM_PASTE, (WPARAM)0, (LPARAM)0L); | |
279 | #endif | |
280 | } | |
281 | ||
debe6624 | 282 | void wxComboBox::Remove(long from, long to) |
2bda0e17 | 283 | { |
4438caf4 | 284 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
285 | long fromChar = from; |
286 | long toChar = to; | |
4438caf4 | 287 | |
2bda0e17 KB |
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 | ||
debe6624 | 297 | void wxComboBox::SetSelection(long from, long to) |
2bda0e17 | 298 | { |
4438caf4 | 299 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
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 | } | |
4438caf4 | 310 | |
2bda0e17 KB |
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 | ||
4438caf4 VZ |
320 | void wxComboBox::DoSetSize(int x, int y, |
321 | int width, int height, | |
322 | int sizeFlags) | |
323 | { | |
324 | wxControl::DoSetSize(x, y, width, height, sizeFlags); | |
4438caf4 VZ |
325 | } |
326 | ||
2bda0e17 | 327 | #endif |
47d67540 | 328 | // wxUSE_COMBOBOX |
2bda0e17 | 329 |