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