]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: choice.cpp | |
3 | // Purpose: wxChoice | |
4 | // Author: Julian Smart | |
8d99be5f | 5 | // Modified by: Vadim Zeitlin to derive from wxChoiceBase |
2bda0e17 KB |
6 | // Created: 04/01/98 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
c085e333 | 9 | // Licence: wxWindows license |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
8d99be5f VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
2bda0e17 | 20 | #ifdef __GNUG__ |
8d99be5f | 21 | #pragma implementation "choice.h" |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
8d99be5f | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
31 | #ifndef WX_PRECOMP | |
8d99be5f VZ |
32 | #include "wx/choice.h" |
33 | #include "wx/utils.h" | |
34 | #include "wx/log.h" | |
2bda0e17 KB |
35 | #endif |
36 | ||
37 | #include "wx/msw/private.h" | |
38 | ||
39 | #if !USE_SHARED_LIBRARY | |
8d99be5f | 40 | IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl) |
2bda0e17 KB |
41 | #endif |
42 | ||
8d99be5f VZ |
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) | |
2bda0e17 | 59 | { |
8d99be5f VZ |
60 | if ( !CreateControl(parent, id, pos, size, style, validator, name) ) |
61 | return FALSE; | |
29006414 | 62 | |
8d99be5f VZ |
63 | long msStyle = WS_CHILD | CBS_DROPDOWNLIST | WS_TABSTOP | WS_VISIBLE; |
64 | if ( style & wxCB_SORT ) | |
65 | msStyle |= CBS_SORT; | |
2bda0e17 | 66 | |
8d99be5f VZ |
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 | _T("this style flag is ignored by wxChoice, you " | |
73 | "probably want to use a wxComboBox") ); | |
2bda0e17 | 74 | |
8d99be5f VZ |
75 | if ( !MSWCreateControl(_T("COMBOBOX"), msStyle) ) |
76 | return FALSE; | |
2bda0e17 | 77 | |
8d99be5f VZ |
78 | for ( int i = 0; i < n; i++ ) |
79 | { | |
80 | Append(choices[i]); | |
81 | } | |
2bda0e17 | 82 | |
8d99be5f | 83 | SetSize(pos.x, pos.y, size.x, size.y); |
2bda0e17 | 84 | |
8d99be5f | 85 | return TRUE; |
2bda0e17 KB |
86 | } |
87 | ||
8d99be5f VZ |
88 | // ---------------------------------------------------------------------------- |
89 | // adding/deleting items to/from the list | |
90 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 91 | |
8d99be5f VZ |
92 | void wxChoice::DoAppend(const wxString& item) |
93 | { | |
94 | SendMessage(GetHwnd(), CB_ADDSTRING, 0, (LONG)item.c_str()); | |
2bda0e17 KB |
95 | } |
96 | ||
debe6624 | 97 | void wxChoice::Delete(int n) |
2bda0e17 | 98 | { |
8d99be5f VZ |
99 | wxCHECK_RET( n < GetCount(), _T("invalid item index in wxChoice::Delete") ); |
100 | ||
101 | SendMessage(GetHwnd(), CB_DELETESTRING, n, 0); | |
2bda0e17 KB |
102 | } |
103 | ||
8d99be5f | 104 | void wxChoice::Clear() |
2bda0e17 | 105 | { |
8d99be5f | 106 | SendMessage(GetHwnd(), CB_RESETCONTENT, 0, 0); |
2bda0e17 KB |
107 | } |
108 | ||
8d99be5f VZ |
109 | // ---------------------------------------------------------------------------- |
110 | // selection | |
111 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 112 | |
8d99be5f | 113 | int wxChoice::GetSelection() const |
2bda0e17 | 114 | { |
8d99be5f | 115 | return (int)SendMessage(GetHwnd(), CB_GETCURSEL, 0, 0); |
2bda0e17 KB |
116 | } |
117 | ||
debe6624 | 118 | void wxChoice::SetSelection(int n) |
2bda0e17 | 119 | { |
8d99be5f VZ |
120 | SendMessage(GetHwnd(), CB_SETCURSEL, n, 0); |
121 | } | |
122 | ||
123 | // ---------------------------------------------------------------------------- | |
124 | // string list functions | |
125 | // ---------------------------------------------------------------------------- | |
126 | ||
127 | int wxChoice::GetCount() const | |
128 | { | |
129 | return (int)SendMessage(GetHwnd(), CB_GETCOUNT, 0, 0); | |
2bda0e17 KB |
130 | } |
131 | ||
132 | int wxChoice::FindString(const wxString& s) const | |
133 | { | |
134 | #if defined(__WATCOMC__) && defined(__WIN386__) | |
8d99be5f VZ |
135 | // For some reason, Watcom in WIN386 mode crashes in the CB_FINDSTRINGEXACT message. |
136 | // wxChoice::Do it the long way instead. | |
137 | int count = GetCount(); | |
138 | for ( int i = 0; i < count; i++ ) | |
139 | { | |
140 | // as CB_FINDSTRINGEXACT is case insensitive, be case insensitive too | |
141 | if ( GetString(i).IsSameAs(s, FALSE) ) | |
142 | return i; | |
143 | } | |
144 | ||
145 | return wxNOT_FOUND; | |
146 | #else // !Watcom | |
147 | int pos = (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT, | |
148 | (WPARAM)-1, (LPARAM)s.c_str()); | |
149 | ||
150 | return pos == LB_ERR ? wxNOT_FOUND : pos; | |
151 | #endif // Watcom/!Watcom | |
2bda0e17 KB |
152 | } |
153 | ||
debe6624 | 154 | wxString wxChoice::GetString(int n) const |
2bda0e17 | 155 | { |
4438caf4 VZ |
156 | size_t len = (size_t)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN, n, 0); |
157 | wxString str; | |
158 | if ( ::SendMessage(GetHwnd(), CB_GETLBTEXT, n, | |
159 | (LPARAM)str.GetWriteBuf(len)) == CB_ERR ) | |
160 | { | |
161 | wxLogLastError("SendMessage(CB_GETLBTEXT)"); | |
162 | } | |
2bda0e17 | 163 | |
4438caf4 | 164 | str.UngetWriteBuf(); |
2bda0e17 | 165 | |
4438caf4 VZ |
166 | return str; |
167 | } | |
2bda0e17 | 168 | |
8d99be5f VZ |
169 | // ---------------------------------------------------------------------------- |
170 | // client data | |
171 | // ---------------------------------------------------------------------------- | |
172 | ||
173 | void wxChoice::DoSetClientData( int n, void* clientData ) | |
174 | { | |
175 | if ( SendMessage(GetHwnd(), CB_SETITEMDATA, n, (LPARAM)clientData) == CB_ERR ) | |
176 | { | |
177 | wxLogLastError(_T("CB_SETITEMDATA")); | |
178 | } | |
179 | } | |
180 | ||
181 | void* wxChoice::DoGetClientData( int n ) const | |
182 | { | |
183 | LPARAM rc = SendMessage(GetHwnd(), CB_GETITEMDATA, n, 0); | |
184 | if ( rc == CB_ERR ) | |
185 | { | |
186 | wxLogLastError(_T("CB_GETITEMDATA")); | |
187 | ||
188 | // unfortunately, there is no way to return an error code to the user | |
8a4df159 | 189 | rc = (LPARAM) NULL; |
8d99be5f VZ |
190 | } |
191 | ||
192 | return (void *)rc; | |
193 | } | |
194 | ||
195 | void wxChoice::DoSetClientObject( int n, wxClientData* clientData ) | |
196 | { | |
197 | DoSetClientData(n, clientData); | |
198 | } | |
199 | ||
200 | wxClientData* wxChoice::DoGetClientObject( int n ) const | |
201 | { | |
202 | return (wxClientData *)DoGetClientData(n); | |
203 | } | |
204 | ||
205 | // ---------------------------------------------------------------------------- | |
206 | // wxMSW specific helpers | |
207 | // ---------------------------------------------------------------------------- | |
208 | ||
4438caf4 VZ |
209 | void wxChoice::DoSetSize(int x, int y, |
210 | int width, int height, | |
211 | int sizeFlags) | |
212 | { | |
213 | // Ignore height parameter because height doesn't mean 'initially | |
214 | // displayed' height, it refers to the drop-down menu as well. The | |
215 | // wxWindows interpretation is different; also, getting the size returns | |
216 | // the _displayed_ size (NOT the drop down menu size) so | |
217 | // setting-getting-setting size would not work. | |
218 | wxControl::DoSetSize(x, y, width, -1, sizeFlags); | |
219 | } | |
2bda0e17 | 220 | |
4438caf4 VZ |
221 | wxSize wxChoice::DoGetBestSize() |
222 | { | |
223 | // find the widest string | |
224 | int wLine; | |
225 | int wChoice = 0; | |
8d99be5f VZ |
226 | int nItems = GetCount(); |
227 | for ( int i = 0; i < nItems; i++ ) | |
2bda0e17 | 228 | { |
2bda0e17 | 229 | wxString str(GetString(i)); |
4438caf4 VZ |
230 | GetTextExtent(str, &wLine, NULL); |
231 | if ( wLine > wChoice ) | |
232 | wChoice = wLine; | |
2bda0e17 | 233 | } |
fd3f686c | 234 | |
4438caf4 VZ |
235 | // give it some reasonable default value if there are no strings in the |
236 | // list | |
237 | if ( wChoice == 0 ) | |
238 | wChoice = 100; | |
2bda0e17 | 239 | |
4438caf4 VZ |
240 | // the combobox should be larger than the widest string |
241 | int cx, cy; | |
242 | wxGetCharSize(GetHWND(), &cx, &cy, &GetFont()); | |
2bda0e17 | 243 | |
4438caf4 | 244 | wChoice += 5*cx; |
2bda0e17 | 245 | |
4438caf4 | 246 | // Choice drop-down list depends on number of items (limited to 10) |
8d99be5f | 247 | size_t nStrings = nItems == 0 ? 10 : wxMin(10, nItems) + 1; |
4438caf4 | 248 | int hChoice = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*nStrings; |
2bda0e17 | 249 | |
4438caf4 | 250 | return wxSize(wChoice, hChoice); |
2bda0e17 KB |
251 | } |
252 | ||
2bda0e17 KB |
253 | long wxChoice::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) |
254 | { | |
8d99be5f | 255 | if ( nMsg == WM_LBUTTONUP ) |
2bda0e17 | 256 | { |
2bda0e17 KB |
257 | int x = (int)LOWORD(lParam); |
258 | int y = (int)HIWORD(lParam); | |
259 | ||
8d99be5f VZ |
260 | // Ok, this is truly weird, but if a panel with a wxChoice loses the |
261 | // focus, then you get a *fake* WM_LBUTTONUP message with x = 65535 and | |
262 | // y = 65535. Filter out this nonsense. | |
263 | // | |
264 | // VZ: I'd like to know how to reproduce this please... | |
265 | if ( x == 65535 && y == 65535 ) | |
266 | return 0; | |
2bda0e17 KB |
267 | } |
268 | ||
8d99be5f | 269 | return wxWindow::MSWWindowProc(nMsg, wParam, lParam); |
2bda0e17 KB |
270 | } |
271 | ||
8d99be5f | 272 | bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) |
2bda0e17 | 273 | { |
8d99be5f | 274 | if ( param != CBN_SELCHANGE) |
2bda0e17 | 275 | { |
8d99be5f VZ |
276 | // "selection changed" is the only event we're after |
277 | return FALSE; | |
2bda0e17 | 278 | } |
2bda0e17 | 279 | |
8d99be5f VZ |
280 | wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId); |
281 | event.SetInt(GetSelection()); | |
282 | event.SetEventObject(this); | |
283 | event.SetString(GetStringSelection()); | |
284 | ProcessCommand(event); | |
2bda0e17 | 285 | |
8d99be5f VZ |
286 | return TRUE; |
287 | } | |
2bda0e17 | 288 |