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