]> git.saurik.com Git - wxWidgets.git/blame - src/msw/choice.cpp
xti additions / changes, trying to reduce dependencies
[wxWidgets.git] / src / msw / choice.cpp
CommitLineData
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$
6c9a19aa
JS
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
8d99be5f
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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
1e6feb95
VZ
31#if wxUSE_CHOICE
32
2bda0e17 33#ifndef WX_PRECOMP
8d99be5f
VZ
34 #include "wx/choice.h"
35 #include "wx/utils.h"
36 #include "wx/log.h"
f6bcfd97 37 #include "wx/brush.h"
c7401637 38 #include "wx/settings.h"
2bda0e17
KB
39#endif
40
41#include "wx/msw/private.h"
42
6a89f9ee
SC
43#if wxUSE_EXTENDED_RTTI
44IMPLEMENT_DYNAMIC_CLASS_XTI(wxChoice, wxControl,"wx/checkbox.h")
45
46WX_BEGIN_PROPERTIES_TABLE(wxChoice)
47 // TODO DELEGATES
f0a126fe 48 WX_PROPERTY( Font , wxFont , SetFont , GetFont , )
6a89f9ee
SC
49 WX_PROPERTY_COLLECTION( Choices , wxArrayString , wxString , AppendString , GetStrings )
50 WX_PROPERTY( Selection ,int, SetSelection, GetSelection, )
51WX_END_PROPERTIES_TABLE()
52
53WX_BEGIN_HANDLERS_TABLE(wxChoice)
54WX_END_HANDLERS_TABLE()
2bda0e17 55
6a89f9ee
SC
56WX_CONSTRUCTOR_4( wxChoice , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size )
57#else
58IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
59#endif
066f1b7a
SC
60/*
61 TODO PROPERTIES
62 selection (long)
63 content (list)
64 item
65*/
66
8d99be5f
VZ
67// ============================================================================
68// implementation
69// ============================================================================
70
71// ----------------------------------------------------------------------------
72// creation
73// ----------------------------------------------------------------------------
74
75bool wxChoice::Create(wxWindow *parent,
76 wxWindowID id,
77 const wxPoint& pos,
78 const wxSize& size,
79 int n, const wxString choices[],
80 long style,
81 const wxValidator& validator,
82 const wxString& name)
2bda0e17 83{
8d99be5f
VZ
84 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
85 return FALSE;
29006414 86
f6bcfd97 87 long msStyle = WS_CHILD | CBS_DROPDOWNLIST | WS_TABSTOP | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL /* | WS_CLIPSIBLINGS */;
8d99be5f
VZ
88 if ( style & wxCB_SORT )
89 msStyle |= CBS_SORT;
2bda0e17 90
b0766406
JS
91 if ( style & wxCLIP_SIBLINGS )
92 msStyle |= WS_CLIPSIBLINGS;
93
94
f6bcfd97 95 // Experience shows that wxChoice vs. wxComboBox distinction confuses
8d99be5f
VZ
96 // quite a few people - try to help them
97 wxASSERT_MSG( !(style & wxCB_DROPDOWN) &&
98 !(style & wxCB_READONLY) &&
99 !(style & wxCB_SIMPLE),
f6bcfd97
BP
100 _T("this style flag is ignored by wxChoice, you ")
101 _T("probably want to use a wxComboBox") );
2bda0e17 102
223d09f6 103 if ( !MSWCreateControl(wxT("COMBOBOX"), msStyle) )
8d99be5f 104 return FALSE;
2bda0e17 105
c92d798f
JS
106 // A choice/combobox normally has a white background (or other, depending
107 // on global settings) rather than inheriting the parent's background colour.
a756f210 108 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
c92d798f 109
8d99be5f
VZ
110 for ( int i = 0; i < n; i++ )
111 {
112 Append(choices[i]);
113 }
2bda0e17 114
8d99be5f 115 SetSize(pos.x, pos.y, size.x, size.y);
2bda0e17 116
8d99be5f 117 return TRUE;
2bda0e17
KB
118}
119
8ee9d618
VZ
120wxChoice::~wxChoice()
121{
122 Free();
123}
124
8d99be5f
VZ
125// ----------------------------------------------------------------------------
126// adding/deleting items to/from the list
127// ----------------------------------------------------------------------------
2bda0e17 128
def6fb9b 129int wxChoice::DoAppend(const wxString& item)
8d99be5f 130{
def6fb9b
VZ
131 int n = (int)SendMessage(GetHwnd(), CB_ADDSTRING, 0, (LONG)item.c_str());
132 if ( n == CB_ERR )
133 {
f6bcfd97 134 wxLogLastError(wxT("SendMessage(CB_ADDSTRING)"));
def6fb9b
VZ
135 }
136
137 return n;
2bda0e17
KB
138}
139
243dbf1a
VZ
140int wxChoice::DoInsert(const wxString& item, int pos)
141{
142 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list"));
143 wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index"));
144
145 int n = (int)SendMessage(GetHwnd(), CB_INSERTSTRING, pos, (LONG)item.c_str());
146 if ( n == CB_ERR )
147 {
148 wxLogLastError(wxT("SendMessage(CB_INSERTSTRING)"));
149 }
150
151 return n;
152}
153
debe6624 154void wxChoice::Delete(int n)
2bda0e17 155{
223d09f6 156 wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") );
8d99be5f 157
6c8a980f
VZ
158 if ( HasClientObjectData() )
159 {
160 delete GetClientObject(n);
161 }
162
8d99be5f 163 SendMessage(GetHwnd(), CB_DELETESTRING, n, 0);
2bda0e17
KB
164}
165
8d99be5f 166void wxChoice::Clear()
8ee9d618 167{
92d2389e 168 Free();
8ee9d618
VZ
169
170 SendMessage(GetHwnd(), CB_RESETCONTENT, 0, 0);
171}
172
173void wxChoice::Free()
2bda0e17 174{
6c8a980f
VZ
175 if ( HasClientObjectData() )
176 {
177 size_t count = GetCount();
178 for ( size_t n = 0; n < count; n++ )
179 {
180 delete GetClientObject(n);
181 }
182 }
2bda0e17
KB
183}
184
8d99be5f
VZ
185// ----------------------------------------------------------------------------
186// selection
187// ----------------------------------------------------------------------------
2bda0e17 188
8d99be5f 189int wxChoice::GetSelection() const
2bda0e17 190{
8d99be5f 191 return (int)SendMessage(GetHwnd(), CB_GETCURSEL, 0, 0);
2bda0e17
KB
192}
193
debe6624 194void wxChoice::SetSelection(int n)
2bda0e17 195{
8d99be5f
VZ
196 SendMessage(GetHwnd(), CB_SETCURSEL, n, 0);
197}
198
199// ----------------------------------------------------------------------------
200// string list functions
201// ----------------------------------------------------------------------------
202
203int wxChoice::GetCount() const
204{
205 return (int)SendMessage(GetHwnd(), CB_GETCOUNT, 0, 0);
2bda0e17
KB
206}
207
208int wxChoice::FindString(const wxString& s) const
209{
210#if defined(__WATCOMC__) && defined(__WIN386__)
8d99be5f
VZ
211 // For some reason, Watcom in WIN386 mode crashes in the CB_FINDSTRINGEXACT message.
212 // wxChoice::Do it the long way instead.
213 int count = GetCount();
214 for ( int i = 0; i < count; i++ )
215 {
216 // as CB_FINDSTRINGEXACT is case insensitive, be case insensitive too
217 if ( GetString(i).IsSameAs(s, FALSE) )
218 return i;
219 }
220
221 return wxNOT_FOUND;
222#else // !Watcom
223 int pos = (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT,
224 (WPARAM)-1, (LPARAM)s.c_str());
225
226 return pos == LB_ERR ? wxNOT_FOUND : pos;
227#endif // Watcom/!Watcom
2bda0e17
KB
228}
229
b4bfa452 230void wxChoice::SetString(int n, const wxString& s)
6c8a980f 231{
2b5f62a0
VZ
232 wxCHECK_RET( n >= 0 && n < GetCount(),
233 wxT("invalid item index in wxChoice::SetString") );
234
235 // we have to delete and add back the string as there is no way to change a
236 // string in place
237
238 // we need to preserve the client data
239 void *data;
240 if ( m_clientDataItemsType != wxClientData_None )
241 {
242 data = DoGetItemClientData(n);
243 }
244 else // no client data
245 {
246 data = NULL;
247 }
248
249 ::SendMessage(GetHwnd(), CB_DELETESTRING, n, 0);
250 ::SendMessage(GetHwnd(), CB_INSERTSTRING, n, (LPARAM)s.c_str() );
251
252 if ( data )
253 {
254 DoSetItemClientData(n, data);
255 }
256 //else: it's already NULL by default
6c8a980f
VZ
257}
258
debe6624 259wxString wxChoice::GetString(int n) const
2bda0e17 260{
478cabab
VZ
261 int len = (int)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN, n, 0);
262
6c8a980f 263 wxString str;
478cabab
VZ
264 if ( len != CB_ERR && len > 0 )
265 {
266 if ( ::SendMessage
267 (
268 GetHwnd(),
269 CB_GETLBTEXT,
270 n,
271 (LPARAM)(wxChar *)wxStringBuffer(str, len)
272 ) == CB_ERR )
273 {
f6bcfd97 274 wxLogLastError(wxT("SendMessage(CB_GETLBTEXT)"));
21d72d17 275 }
4438caf4 276 }
2bda0e17 277
4438caf4
VZ
278 return str;
279}
2bda0e17 280
8d99be5f
VZ
281// ----------------------------------------------------------------------------
282// client data
283// ----------------------------------------------------------------------------
284
6c8a980f 285void wxChoice::DoSetItemClientData( int n, void* clientData )
8d99be5f 286{
2b5f62a0
VZ
287 if ( ::SendMessage(GetHwnd(), CB_SETITEMDATA,
288 n, (LPARAM)clientData) == CB_ERR )
8d99be5f 289 {
223d09f6 290 wxLogLastError(wxT("CB_SETITEMDATA"));
8d99be5f
VZ
291 }
292}
293
6c8a980f 294void* wxChoice::DoGetItemClientData( int n ) const
8d99be5f
VZ
295{
296 LPARAM rc = SendMessage(GetHwnd(), CB_GETITEMDATA, n, 0);
297 if ( rc == CB_ERR )
298 {
223d09f6 299 wxLogLastError(wxT("CB_GETITEMDATA"));
8d99be5f
VZ
300
301 // unfortunately, there is no way to return an error code to the user
8ee9d618 302 rc = (LPARAM) NULL;
8d99be5f
VZ
303 }
304
305 return (void *)rc;
306}
307
6c8a980f 308void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
8d99be5f 309{
6c8a980f 310 DoSetItemClientData(n, clientData);
8d99be5f
VZ
311}
312
6c8a980f 313wxClientData* wxChoice::DoGetItemClientObject( int n ) const
8d99be5f 314{
6c8a980f 315 return (wxClientData *)DoGetItemClientData(n);
8d99be5f
VZ
316}
317
318// ----------------------------------------------------------------------------
319// wxMSW specific helpers
320// ----------------------------------------------------------------------------
321
18c50997
VZ
322void wxChoice::DoMoveWindow(int x, int y, int width, int height)
323{
324 // here is why this is necessary: if the width is negative, the combobox
325 // window proc makes the window of the size width*height instead of
326 // interpreting height in the usual manner (meaning the height of the drop
327 // down list - usually the height specified in the call to MoveWindow()
328 // will not change the height of combo box per se)
329 //
330 // this behaviour is not documented anywhere, but this is just how it is
331 // here (NT 4.4) and, anyhow, the check shouldn't hurt - however without
332 // the check, constraints/sizers using combos may break the height
333 // constraint will have not at all the same value as expected
334 if ( width < 0 )
335 return;
336
337 wxControl::DoMoveWindow(x, y, width, height);
338}
339
4438caf4 340void wxChoice::DoSetSize(int x, int y,
33ac7e6f 341 int width, int WXUNUSED(height),
4438caf4
VZ
342 int sizeFlags)
343{
344 // Ignore height parameter because height doesn't mean 'initially
345 // displayed' height, it refers to the drop-down menu as well. The
346 // wxWindows interpretation is different; also, getting the size returns
347 // the _displayed_ size (NOT the drop down menu size) so
348 // setting-getting-setting size would not work.
a8e65eee 349
5a224901 350 wxControl::DoSetSize(x, y, width, -1, sizeFlags);
4438caf4 351}
2bda0e17 352
882a8f40 353wxSize wxChoice::DoGetBestSize() const
4438caf4
VZ
354{
355 // find the widest string
356 int wLine;
357 int wChoice = 0;
8d99be5f
VZ
358 int nItems = GetCount();
359 for ( int i = 0; i < nItems; i++ )
2bda0e17 360 {
2bda0e17 361 wxString str(GetString(i));
4438caf4
VZ
362 GetTextExtent(str, &wLine, NULL);
363 if ( wLine > wChoice )
364 wChoice = wLine;
2bda0e17 365 }
fd3f686c 366
4438caf4
VZ
367 // give it some reasonable default value if there are no strings in the
368 // list
369 if ( wChoice == 0 )
370 wChoice = 100;
2bda0e17 371
4438caf4
VZ
372 // the combobox should be larger than the widest string
373 int cx, cy;
374 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
2bda0e17 375
4438caf4 376 wChoice += 5*cx;
2bda0e17 377
4438caf4 378 // Choice drop-down list depends on number of items (limited to 10)
8d99be5f 379 size_t nStrings = nItems == 0 ? 10 : wxMin(10, nItems) + 1;
4438caf4 380 int hChoice = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*nStrings;
2bda0e17 381
4438caf4 382 return wxSize(wChoice, hChoice);
2bda0e17
KB
383}
384
2bda0e17
KB
385long wxChoice::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
386{
8d99be5f 387 if ( nMsg == WM_LBUTTONUP )
2bda0e17 388 {
2bda0e17
KB
389 int x = (int)LOWORD(lParam);
390 int y = (int)HIWORD(lParam);
391
8d99be5f
VZ
392 // Ok, this is truly weird, but if a panel with a wxChoice loses the
393 // focus, then you get a *fake* WM_LBUTTONUP message with x = 65535 and
394 // y = 65535. Filter out this nonsense.
395 //
396 // VZ: I'd like to know how to reproduce this please...
397 if ( x == 65535 && y == 65535 )
398 return 0;
2bda0e17
KB
399 }
400
8d99be5f 401 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
2bda0e17
KB
402}
403
8d99be5f 404bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
2bda0e17 405{
8d99be5f 406 if ( param != CBN_SELCHANGE)
2bda0e17 407 {
8d99be5f
VZ
408 // "selection changed" is the only event we're after
409 return FALSE;
2bda0e17 410 }
2bda0e17 411
2b273975 412 int n = GetSelection();
8c1c5302
JS
413 if (n > -1)
414 {
415 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId);
416 event.SetInt(n);
417 event.SetEventObject(this);
418 event.SetString(GetStringSelection());
419 if ( HasClientObjectData() )
420 event.SetClientObject( GetClientObject(n) );
421 else if ( HasClientUntypedData() )
422 event.SetClientData( GetClientData(n) );
423 ProcessCommand(event);
424 }
2bda0e17 425
8d99be5f
VZ
426 return TRUE;
427}
2bda0e17 428
33ac7e6f
KB
429WXHBRUSH wxChoice::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
430 WXUINT WXUNUSED(message),
431 WXWPARAM WXUNUSED(wParam),
788722ac 432 WXLPARAM WXUNUSED(lParam)
788722ac 433 )
f6bcfd97 434{
f6bcfd97 435 HDC hdc = (HDC)pDC;
f6bcfd97
BP
436 wxColour colBack = GetBackgroundColour();
437
438 if (!IsEnabled())
a756f210 439 colBack = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
f6bcfd97
BP
440
441 ::SetBkColor(hdc, wxColourToRGB(colBack));
442 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
443
444 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
445
446 return (WXHBRUSH)brush->GetResourceHandle();
447}
448
1e6feb95 449#endif // wxUSE_CHOICE