]> git.saurik.com Git - wxWidgets.git/blob - src/msw/choice.cpp
help search is much faster now (7 times! that's what I call optimization ;-)
[wxWidgets.git] / src / msw / choice.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: choice.cpp
3 // Purpose: wxChoice
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin to derive from wxChoiceBase
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "choice.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/choice.h"
33 #include "wx/utils.h"
34 #include "wx/log.h"
35 #include "wx/settings.h"
36 #endif
37
38 #include "wx/msw/private.h"
39
40 IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
41
42 // ============================================================================
43 // implementation
44 // ============================================================================
45
46 // ----------------------------------------------------------------------------
47 // creation
48 // ----------------------------------------------------------------------------
49
50 bool wxChoice::Create(wxWindow *parent,
51 wxWindowID id,
52 const wxPoint& pos,
53 const wxSize& size,
54 int n, const wxString choices[],
55 long style,
56 const wxValidator& validator,
57 const wxString& name)
58 {
59 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
60 return FALSE;
61
62 long msStyle = WS_CHILD | CBS_DROPDOWNLIST | WS_TABSTOP | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL;
63 if ( style & wxCB_SORT )
64 msStyle |= CBS_SORT;
65
66 // the experience shows that wxChoice vs. wxComboBox distinction confuses
67 // quite a few people - try to help them
68 wxASSERT_MSG( !(style & wxCB_DROPDOWN) &&
69 !(style & wxCB_READONLY) &&
70 !(style & wxCB_SIMPLE),
71 wxT("this style flag is ignored by wxChoice, you "
72 "probably want to use a wxComboBox") );
73
74 if ( !MSWCreateControl(wxT("COMBOBOX"), msStyle) )
75 return FALSE;
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
81 for ( int i = 0; i < n; i++ )
82 {
83 Append(choices[i]);
84 }
85
86 SetSize(pos.x, pos.y, size.x, size.y);
87
88 return TRUE;
89 }
90
91 wxChoice::~wxChoice()
92 {
93 Free();
94 }
95
96 // ----------------------------------------------------------------------------
97 // adding/deleting items to/from the list
98 // ----------------------------------------------------------------------------
99
100 int wxChoice::DoAppend(const wxString& item)
101 {
102 int n = (int)SendMessage(GetHwnd(), CB_ADDSTRING, 0, (LONG)item.c_str());
103 if ( n == CB_ERR )
104 {
105 wxLogLastError("SendMessage(CB_ADDSTRING)");
106 }
107
108 return n;
109 }
110
111 void wxChoice::Delete(int n)
112 {
113 wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") );
114
115 if ( HasClientObjectData() )
116 {
117 delete GetClientObject(n);
118 }
119
120 SendMessage(GetHwnd(), CB_DELETESTRING, n, 0);
121 }
122
123 void wxChoice::Clear()
124 {
125 Free();
126
127 SendMessage(GetHwnd(), CB_RESETCONTENT, 0, 0);
128 }
129
130 void wxChoice::Free()
131 {
132 if ( HasClientObjectData() )
133 {
134 size_t count = GetCount();
135 for ( size_t n = 0; n < count; n++ )
136 {
137 delete GetClientObject(n);
138 }
139 }
140 }
141
142 // ----------------------------------------------------------------------------
143 // selection
144 // ----------------------------------------------------------------------------
145
146 int wxChoice::GetSelection() const
147 {
148 return (int)SendMessage(GetHwnd(), CB_GETCURSEL, 0, 0);
149 }
150
151 void wxChoice::SetSelection(int n)
152 {
153 SendMessage(GetHwnd(), CB_SETCURSEL, n, 0);
154 }
155
156 // ----------------------------------------------------------------------------
157 // string list functions
158 // ----------------------------------------------------------------------------
159
160 int wxChoice::GetCount() const
161 {
162 return (int)SendMessage(GetHwnd(), CB_GETCOUNT, 0, 0);
163 }
164
165 int wxChoice::FindString(const wxString& s) const
166 {
167 #if defined(__WATCOMC__) && defined(__WIN386__)
168 // For some reason, Watcom in WIN386 mode crashes in the CB_FINDSTRINGEXACT message.
169 // wxChoice::Do it the long way instead.
170 int count = GetCount();
171 for ( int i = 0; i < count; i++ )
172 {
173 // as CB_FINDSTRINGEXACT is case insensitive, be case insensitive too
174 if ( GetString(i).IsSameAs(s, FALSE) )
175 return i;
176 }
177
178 return wxNOT_FOUND;
179 #else // !Watcom
180 int pos = (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT,
181 (WPARAM)-1, (LPARAM)s.c_str());
182
183 return pos == LB_ERR ? wxNOT_FOUND : pos;
184 #endif // Watcom/!Watcom
185 }
186
187 void wxChoice::SetString(int n, const wxString& s)
188 {
189 wxFAIL_MSG(wxT("not implemented"));
190
191 #if 0 // should do this, but no Insert() so far
192 Delete(n);
193 Insert(n + 1, s);
194 #endif
195 }
196
197 wxString wxChoice::GetString(int n) const
198 {
199 size_t len = (size_t)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN, n, 0);
200 wxString str;
201 if (len) {
202 if ( ::SendMessage(GetHwnd(), CB_GETLBTEXT, n,
203 (LPARAM)str.GetWriteBuf(len)) == CB_ERR ) {
204 wxLogLastError("SendMessage(CB_GETLBTEXT)");
205 }
206 str.UngetWriteBuf();
207 }
208
209 return str;
210 }
211
212 // ----------------------------------------------------------------------------
213 // client data
214 // ----------------------------------------------------------------------------
215
216 void wxChoice::DoSetItemClientData( int n, void* clientData )
217 {
218 if ( SendMessage(GetHwnd(), CB_SETITEMDATA, n, (LPARAM)clientData) == CB_ERR )
219 {
220 wxLogLastError(wxT("CB_SETITEMDATA"));
221 }
222 }
223
224 void* wxChoice::DoGetItemClientData( int n ) const
225 {
226 LPARAM rc = SendMessage(GetHwnd(), CB_GETITEMDATA, n, 0);
227 if ( rc == CB_ERR )
228 {
229 wxLogLastError(wxT("CB_GETITEMDATA"));
230
231 // unfortunately, there is no way to return an error code to the user
232 rc = (LPARAM) NULL;
233 }
234
235 return (void *)rc;
236 }
237
238 void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
239 {
240 DoSetItemClientData(n, clientData);
241 }
242
243 wxClientData* wxChoice::DoGetItemClientObject( int n ) const
244 {
245 return (wxClientData *)DoGetItemClientData(n);
246 }
247
248 // ----------------------------------------------------------------------------
249 // wxMSW specific helpers
250 // ----------------------------------------------------------------------------
251
252 void wxChoice::DoSetSize(int x, int y,
253 int width, int height,
254 int sizeFlags)
255 {
256 // Ignore height parameter because height doesn't mean 'initially
257 // displayed' height, it refers to the drop-down menu as well. The
258 // wxWindows interpretation is different; also, getting the size returns
259 // the _displayed_ size (NOT the drop down menu size) so
260 // setting-getting-setting size would not work.
261 wxControl::DoSetSize(x, y, width, -1, sizeFlags);
262 }
263
264 wxSize wxChoice::DoGetBestSize() const
265 {
266 // find the widest string
267 int wLine;
268 int wChoice = 0;
269 int nItems = GetCount();
270 for ( int i = 0; i < nItems; i++ )
271 {
272 wxString str(GetString(i));
273 GetTextExtent(str, &wLine, NULL);
274 if ( wLine > wChoice )
275 wChoice = wLine;
276 }
277
278 // give it some reasonable default value if there are no strings in the
279 // list
280 if ( wChoice == 0 )
281 wChoice = 100;
282
283 // the combobox should be larger than the widest string
284 int cx, cy;
285 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
286
287 wChoice += 5*cx;
288
289 // Choice drop-down list depends on number of items (limited to 10)
290 size_t nStrings = nItems == 0 ? 10 : wxMin(10, nItems) + 1;
291 int hChoice = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*nStrings;
292
293 return wxSize(wChoice, hChoice);
294 }
295
296 long wxChoice::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
297 {
298 if ( nMsg == WM_LBUTTONUP )
299 {
300 int x = (int)LOWORD(lParam);
301 int y = (int)HIWORD(lParam);
302
303 // Ok, this is truly weird, but if a panel with a wxChoice loses the
304 // focus, then you get a *fake* WM_LBUTTONUP message with x = 65535 and
305 // y = 65535. Filter out this nonsense.
306 //
307 // VZ: I'd like to know how to reproduce this please...
308 if ( x == 65535 && y == 65535 )
309 return 0;
310 }
311
312 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
313 }
314
315 bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
316 {
317 if ( param != CBN_SELCHANGE)
318 {
319 // "selection changed" is the only event we're after
320 return FALSE;
321 }
322
323 int n = GetSelection();
324 if (n > -1)
325 {
326 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId);
327 event.SetInt(n);
328 event.SetEventObject(this);
329 event.SetString(GetStringSelection());
330 if ( HasClientObjectData() )
331 event.SetClientObject( GetClientObject(n) );
332 else if ( HasClientUntypedData() )
333 event.SetClientData( GetClientData(n) );
334 ProcessCommand(event);
335 }
336
337 return TRUE;
338 }
339