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