]> git.saurik.com Git - wxWidgets.git/blame - src/msw/choice.cpp
Corrected wxToolBar::ToolSetBitmapSize()
[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$
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"
f6bcfd97 35 #include "wx/brush.h"
c7401637 36 #include "wx/settings.h"
2bda0e17
KB
37#endif
38
39#include "wx/msw/private.h"
40
8d99be5f 41 IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
2bda0e17 42
8d99be5f
VZ
43// ============================================================================
44// implementation
45// ============================================================================
46
47// ----------------------------------------------------------------------------
48// creation
49// ----------------------------------------------------------------------------
50
51bool 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
f6bcfd97 63 long msStyle = WS_CHILD | CBS_DROPDOWNLIST | WS_TABSTOP | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL /* | WS_CLIPSIBLINGS */;
8d99be5f
VZ
64 if ( style & wxCB_SORT )
65 msStyle |= CBS_SORT;
2bda0e17 66
f6bcfd97 67 // Experience shows that wxChoice vs. wxComboBox distinction confuses
8d99be5f
VZ
68 // quite a few people - try to help them
69 wxASSERT_MSG( !(style & wxCB_DROPDOWN) &&
70 !(style & wxCB_READONLY) &&
71 !(style & wxCB_SIMPLE),
f6bcfd97
BP
72 _T("this style flag is ignored by wxChoice, you ")
73 _T("probably want to use a wxComboBox") );
2bda0e17 74
223d09f6 75 if ( !MSWCreateControl(wxT("COMBOBOX"), msStyle) )
8d99be5f 76 return FALSE;
2bda0e17 77
c92d798f
JS
78 // A choice/combobox normally has a white background (or other, depending
79 // on global settings) rather than inheriting the parent's background colour.
80 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
81
8d99be5f
VZ
82 for ( int i = 0; i < n; i++ )
83 {
84 Append(choices[i]);
85 }
2bda0e17 86
8d99be5f 87 SetSize(pos.x, pos.y, size.x, size.y);
2bda0e17 88
8d99be5f 89 return TRUE;
2bda0e17
KB
90}
91
8ee9d618
VZ
92wxChoice::~wxChoice()
93{
94 Free();
95}
96
8d99be5f
VZ
97// ----------------------------------------------------------------------------
98// adding/deleting items to/from the list
99// ----------------------------------------------------------------------------
2bda0e17 100
def6fb9b 101int wxChoice::DoAppend(const wxString& item)
8d99be5f 102{
def6fb9b
VZ
103 int n = (int)SendMessage(GetHwnd(), CB_ADDSTRING, 0, (LONG)item.c_str());
104 if ( n == CB_ERR )
105 {
f6bcfd97 106 wxLogLastError(wxT("SendMessage(CB_ADDSTRING)"));
def6fb9b
VZ
107 }
108
109 return n;
2bda0e17
KB
110}
111
debe6624 112void wxChoice::Delete(int n)
2bda0e17 113{
223d09f6 114 wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") );
8d99be5f 115
6c8a980f
VZ
116 if ( HasClientObjectData() )
117 {
118 delete GetClientObject(n);
119 }
120
8d99be5f 121 SendMessage(GetHwnd(), CB_DELETESTRING, n, 0);
2bda0e17
KB
122}
123
8d99be5f 124void wxChoice::Clear()
8ee9d618 125{
92d2389e 126 Free();
8ee9d618
VZ
127
128 SendMessage(GetHwnd(), CB_RESETCONTENT, 0, 0);
129}
130
131void wxChoice::Free()
2bda0e17 132{
6c8a980f
VZ
133 if ( HasClientObjectData() )
134 {
135 size_t count = GetCount();
136 for ( size_t n = 0; n < count; n++ )
137 {
138 delete GetClientObject(n);
139 }
140 }
2bda0e17
KB
141}
142
8d99be5f
VZ
143// ----------------------------------------------------------------------------
144// selection
145// ----------------------------------------------------------------------------
2bda0e17 146
8d99be5f 147int wxChoice::GetSelection() const
2bda0e17 148{
8d99be5f 149 return (int)SendMessage(GetHwnd(), CB_GETCURSEL, 0, 0);
2bda0e17
KB
150}
151
debe6624 152void wxChoice::SetSelection(int n)
2bda0e17 153{
8d99be5f
VZ
154 SendMessage(GetHwnd(), CB_SETCURSEL, n, 0);
155}
156
157// ----------------------------------------------------------------------------
158// string list functions
159// ----------------------------------------------------------------------------
160
161int wxChoice::GetCount() const
162{
163 return (int)SendMessage(GetHwnd(), CB_GETCOUNT, 0, 0);
2bda0e17
KB
164}
165
166int wxChoice::FindString(const wxString& s) const
167{
168#if defined(__WATCOMC__) && defined(__WIN386__)
8d99be5f
VZ
169 // For some reason, Watcom in WIN386 mode crashes in the CB_FINDSTRINGEXACT message.
170 // wxChoice::Do it the long way instead.
171 int count = GetCount();
172 for ( int i = 0; i < count; i++ )
173 {
174 // as CB_FINDSTRINGEXACT is case insensitive, be case insensitive too
175 if ( GetString(i).IsSameAs(s, FALSE) )
176 return i;
177 }
178
179 return wxNOT_FOUND;
180#else // !Watcom
181 int pos = (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT,
182 (WPARAM)-1, (LPARAM)s.c_str());
183
184 return pos == LB_ERR ? wxNOT_FOUND : pos;
185#endif // Watcom/!Watcom
2bda0e17
KB
186}
187
33ac7e6f 188void wxChoice::SetString(int WXUNUSED(n), const wxString& WXUNUSED(s))
6c8a980f
VZ
189{
190 wxFAIL_MSG(wxT("not implemented"));
191
192#if 0 // should do this, but no Insert() so far
193 Delete(n);
194 Insert(n + 1, s);
195#endif
196}
197
debe6624 198wxString wxChoice::GetString(int n) const
2bda0e17 199{
4438caf4 200 size_t len = (size_t)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN, n, 0);
6c8a980f 201 wxString str;
21d72d17
RD
202 if (len) {
203 if ( ::SendMessage(GetHwnd(), CB_GETLBTEXT, n,
204 (LPARAM)str.GetWriteBuf(len)) == CB_ERR ) {
f6bcfd97 205 wxLogLastError(wxT("SendMessage(CB_GETLBTEXT)"));
21d72d17
RD
206 }
207 str.UngetWriteBuf();
4438caf4 208 }
2bda0e17 209
4438caf4
VZ
210 return str;
211}
2bda0e17 212
8d99be5f
VZ
213// ----------------------------------------------------------------------------
214// client data
215// ----------------------------------------------------------------------------
216
6c8a980f 217void wxChoice::DoSetItemClientData( int n, void* clientData )
8d99be5f
VZ
218{
219 if ( SendMessage(GetHwnd(), CB_SETITEMDATA, n, (LPARAM)clientData) == CB_ERR )
220 {
223d09f6 221 wxLogLastError(wxT("CB_SETITEMDATA"));
8d99be5f
VZ
222 }
223}
224
6c8a980f 225void* wxChoice::DoGetItemClientData( int n ) const
8d99be5f
VZ
226{
227 LPARAM rc = SendMessage(GetHwnd(), CB_GETITEMDATA, n, 0);
228 if ( rc == CB_ERR )
229 {
223d09f6 230 wxLogLastError(wxT("CB_GETITEMDATA"));
8d99be5f
VZ
231
232 // unfortunately, there is no way to return an error code to the user
8ee9d618 233 rc = (LPARAM) NULL;
8d99be5f
VZ
234 }
235
236 return (void *)rc;
237}
238
6c8a980f 239void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
8d99be5f 240{
6c8a980f 241 DoSetItemClientData(n, clientData);
8d99be5f
VZ
242}
243
6c8a980f 244wxClientData* wxChoice::DoGetItemClientObject( int n ) const
8d99be5f 245{
6c8a980f 246 return (wxClientData *)DoGetItemClientData(n);
8d99be5f
VZ
247}
248
249// ----------------------------------------------------------------------------
250// wxMSW specific helpers
251// ----------------------------------------------------------------------------
252
4438caf4 253void wxChoice::DoSetSize(int x, int y,
33ac7e6f 254 int width, int WXUNUSED(height),
4438caf4
VZ
255 int sizeFlags)
256{
257 // Ignore height parameter because height doesn't mean 'initially
258 // displayed' height, it refers to the drop-down menu as well. The
259 // wxWindows interpretation is different; also, getting the size returns
260 // the _displayed_ size (NOT the drop down menu size) so
261 // setting-getting-setting size would not work.
262 wxControl::DoSetSize(x, y, width, -1, sizeFlags);
263}
2bda0e17 264
882a8f40 265wxSize wxChoice::DoGetBestSize() const
4438caf4
VZ
266{
267 // find the widest string
268 int wLine;
269 int wChoice = 0;
8d99be5f
VZ
270 int nItems = GetCount();
271 for ( int i = 0; i < nItems; i++ )
2bda0e17 272 {
2bda0e17 273 wxString str(GetString(i));
4438caf4
VZ
274 GetTextExtent(str, &wLine, NULL);
275 if ( wLine > wChoice )
276 wChoice = wLine;
2bda0e17 277 }
fd3f686c 278
4438caf4
VZ
279 // give it some reasonable default value if there are no strings in the
280 // list
281 if ( wChoice == 0 )
282 wChoice = 100;
2bda0e17 283
4438caf4
VZ
284 // the combobox should be larger than the widest string
285 int cx, cy;
286 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
2bda0e17 287
4438caf4 288 wChoice += 5*cx;
2bda0e17 289
4438caf4 290 // Choice drop-down list depends on number of items (limited to 10)
8d99be5f 291 size_t nStrings = nItems == 0 ? 10 : wxMin(10, nItems) + 1;
4438caf4 292 int hChoice = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*nStrings;
2bda0e17 293
4438caf4 294 return wxSize(wChoice, hChoice);
2bda0e17
KB
295}
296
2bda0e17
KB
297long wxChoice::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
298{
8d99be5f 299 if ( nMsg == WM_LBUTTONUP )
2bda0e17 300 {
2bda0e17
KB
301 int x = (int)LOWORD(lParam);
302 int y = (int)HIWORD(lParam);
303
8d99be5f
VZ
304 // Ok, this is truly weird, but if a panel with a wxChoice loses the
305 // focus, then you get a *fake* WM_LBUTTONUP message with x = 65535 and
306 // y = 65535. Filter out this nonsense.
307 //
308 // VZ: I'd like to know how to reproduce this please...
309 if ( x == 65535 && y == 65535 )
310 return 0;
2bda0e17
KB
311 }
312
8d99be5f 313 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
2bda0e17
KB
314}
315
8d99be5f 316bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
2bda0e17 317{
8d99be5f 318 if ( param != CBN_SELCHANGE)
2bda0e17 319 {
8d99be5f
VZ
320 // "selection changed" is the only event we're after
321 return FALSE;
2bda0e17 322 }
2bda0e17 323
2b273975 324 int n = GetSelection();
8c1c5302
JS
325 if (n > -1)
326 {
327 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId);
328 event.SetInt(n);
329 event.SetEventObject(this);
330 event.SetString(GetStringSelection());
331 if ( HasClientObjectData() )
332 event.SetClientObject( GetClientObject(n) );
333 else if ( HasClientUntypedData() )
334 event.SetClientData( GetClientData(n) );
335 ProcessCommand(event);
336 }
2bda0e17 337
8d99be5f
VZ
338 return TRUE;
339}
2bda0e17 340
33ac7e6f
KB
341WXHBRUSH wxChoice::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
342 WXUINT WXUNUSED(message),
343 WXWPARAM WXUNUSED(wParam),
344 WXLPARAM WXUNUSED(lParam))
f6bcfd97
BP
345{
346#if wxUSE_CTL3D
347 if ( m_useCtl3D )
348 {
349 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
350 return (WXHBRUSH) hbrush;
351 }
352#endif // wxUSE_CTL3D
353
354 HDC hdc = (HDC)pDC;
355 if (GetParent()->GetTransparentBackground())
356 SetBkMode(hdc, TRANSPARENT);
357 else
358 SetBkMode(hdc, OPAQUE);
359
360 wxColour colBack = GetBackgroundColour();
361
362 if (!IsEnabled())
363 colBack = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
364
365 ::SetBkColor(hdc, wxColourToRGB(colBack));
366 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
367
368 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
369
370 return (WXHBRUSH)brush->GetResourceHandle();
371}
372
373