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