]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/choice.cpp
removed dialup from list of files because of compilation problem with
[wxWidgets.git] / src / msw / choice.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: choice.cpp
3// Purpose: wxChoice
4// Author: Julian Smart
5// Modified by: Vadim Zeitlin to derive from wxChoiceBase
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
21 #pragma implementation "choice.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
31#ifndef WX_PRECOMP
32 #include "wx/choice.h"
33 #include "wx/utils.h"
34 #include "wx/log.h"
35 #include "wx/brush.h"
36 #include "wx/settings.h"
37#endif
38
39#include "wx/msw/private.h"
40
41IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
42
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)
59{
60 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
61 return FALSE;
62
63 long msStyle = WS_CHILD | CBS_DROPDOWNLIST | WS_TABSTOP | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL /* | WS_CLIPSIBLINGS */;
64 if ( style & wxCB_SORT )
65 msStyle |= CBS_SORT;
66
67 if ( style & wxCLIP_SIBLINGS )
68 msStyle |= WS_CLIPSIBLINGS;
69
70
71 // Experience shows that wxChoice vs. wxComboBox distinction confuses
72 // quite a few people - try to help them
73 wxASSERT_MSG( !(style & wxCB_DROPDOWN) &&
74 !(style & wxCB_READONLY) &&
75 !(style & wxCB_SIMPLE),
76 _T("this style flag is ignored by wxChoice, you ")
77 _T("probably want to use a wxComboBox") );
78
79 if ( !MSWCreateControl(wxT("COMBOBOX"), msStyle) )
80 return FALSE;
81
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
86 for ( int i = 0; i < n; i++ )
87 {
88 Append(choices[i]);
89 }
90
91 SetSize(pos.x, pos.y, size.x, size.y);
92
93 return TRUE;
94}
95
96wxChoice::~wxChoice()
97{
98 Free();
99}
100
101// ----------------------------------------------------------------------------
102// adding/deleting items to/from the list
103// ----------------------------------------------------------------------------
104
105int wxChoice::DoAppend(const wxString& item)
106{
107 int n = (int)SendMessage(GetHwnd(), CB_ADDSTRING, 0, (LONG)item.c_str());
108 if ( n == CB_ERR )
109 {
110 wxLogLastError(wxT("SendMessage(CB_ADDSTRING)"));
111 }
112
113 return n;
114}
115
116void wxChoice::Delete(int n)
117{
118 wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") );
119
120 if ( HasClientObjectData() )
121 {
122 delete GetClientObject(n);
123 }
124
125 SendMessage(GetHwnd(), CB_DELETESTRING, n, 0);
126}
127
128void wxChoice::Clear()
129{
130 Free();
131
132 SendMessage(GetHwnd(), CB_RESETCONTENT, 0, 0);
133}
134
135void wxChoice::Free()
136{
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 }
145}
146
147// ----------------------------------------------------------------------------
148// selection
149// ----------------------------------------------------------------------------
150
151int wxChoice::GetSelection() const
152{
153 return (int)SendMessage(GetHwnd(), CB_GETCURSEL, 0, 0);
154}
155
156void wxChoice::SetSelection(int n)
157{
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);
168}
169
170int wxChoice::FindString(const wxString& s) const
171{
172#if defined(__WATCOMC__) && defined(__WIN386__)
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
190}
191
192void wxChoice::SetString(int WXUNUSED(n), const wxString& WXUNUSED(s))
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
202wxString wxChoice::GetString(int n) const
203{
204 size_t len = (size_t)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN, n, 0);
205 wxString str;
206 if (len) {
207 if ( ::SendMessage(GetHwnd(), CB_GETLBTEXT, n,
208 (LPARAM)str.GetWriteBuf(len)) == CB_ERR ) {
209 wxLogLastError(wxT("SendMessage(CB_GETLBTEXT)"));
210 }
211 str.UngetWriteBuf();
212 }
213
214 return str;
215}
216
217// ----------------------------------------------------------------------------
218// client data
219// ----------------------------------------------------------------------------
220
221void wxChoice::DoSetItemClientData( int n, void* clientData )
222{
223 if ( SendMessage(GetHwnd(), CB_SETITEMDATA, n, (LPARAM)clientData) == CB_ERR )
224 {
225 wxLogLastError(wxT("CB_SETITEMDATA"));
226 }
227}
228
229void* wxChoice::DoGetItemClientData( int n ) const
230{
231 LPARAM rc = SendMessage(GetHwnd(), CB_GETITEMDATA, n, 0);
232 if ( rc == CB_ERR )
233 {
234 wxLogLastError(wxT("CB_GETITEMDATA"));
235
236 // unfortunately, there is no way to return an error code to the user
237 rc = (LPARAM) NULL;
238 }
239
240 return (void *)rc;
241}
242
243void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
244{
245 DoSetItemClientData(n, clientData);
246}
247
248wxClientData* wxChoice::DoGetItemClientObject( int n ) const
249{
250 return (wxClientData *)DoGetItemClientData(n);
251}
252
253// ----------------------------------------------------------------------------
254// wxMSW specific helpers
255// ----------------------------------------------------------------------------
256
257void wxChoice::DoMoveWindow(int x, int y, int width, int height)
258{
259 // here is why this is necessary: if the width is negative, the combobox
260 // window proc makes the window of the size width*height instead of
261 // interpreting height in the usual manner (meaning the height of the drop
262 // down list - usually the height specified in the call to MoveWindow()
263 // will not change the height of combo box per se)
264 //
265 // this behaviour is not documented anywhere, but this is just how it is
266 // here (NT 4.4) and, anyhow, the check shouldn't hurt - however without
267 // the check, constraints/sizers using combos may break the height
268 // constraint will have not at all the same value as expected
269 if ( width < 0 )
270 return;
271
272 wxControl::DoMoveWindow(x, y, width, height);
273}
274
275void wxChoice::DoSetSize(int x, int y,
276 int width, int WXUNUSED(height),
277 int sizeFlags)
278{
279 // Ignore height parameter because height doesn't mean 'initially
280 // displayed' height, it refers to the drop-down menu as well. The
281 // wxWindows interpretation is different; also, getting the size returns
282 // the _displayed_ size (NOT the drop down menu size) so
283 // setting-getting-setting size would not work.
284 wxControl::DoSetSize(x, y, width, -1, sizeFlags);
285}
286
287wxSize wxChoice::DoGetBestSize() const
288{
289 // find the widest string
290 int wLine;
291 int wChoice = 0;
292 int nItems = GetCount();
293 for ( int i = 0; i < nItems; i++ )
294 {
295 wxString str(GetString(i));
296 GetTextExtent(str, &wLine, NULL);
297 if ( wLine > wChoice )
298 wChoice = wLine;
299 }
300
301 // give it some reasonable default value if there are no strings in the
302 // list
303 if ( wChoice == 0 )
304 wChoice = 100;
305
306 // the combobox should be larger than the widest string
307 int cx, cy;
308 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
309
310 wChoice += 5*cx;
311
312 // Choice drop-down list depends on number of items (limited to 10)
313 size_t nStrings = nItems == 0 ? 10 : wxMin(10, nItems) + 1;
314 int hChoice = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*nStrings;
315
316 return wxSize(wChoice, hChoice);
317}
318
319long wxChoice::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
320{
321 if ( nMsg == WM_LBUTTONUP )
322 {
323 int x = (int)LOWORD(lParam);
324 int y = (int)HIWORD(lParam);
325
326 // Ok, this is truly weird, but if a panel with a wxChoice loses the
327 // focus, then you get a *fake* WM_LBUTTONUP message with x = 65535 and
328 // y = 65535. Filter out this nonsense.
329 //
330 // VZ: I'd like to know how to reproduce this please...
331 if ( x == 65535 && y == 65535 )
332 return 0;
333 }
334
335 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
336}
337
338bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
339{
340 if ( param != CBN_SELCHANGE)
341 {
342 // "selection changed" is the only event we're after
343 return FALSE;
344 }
345
346 int n = GetSelection();
347 if (n > -1)
348 {
349 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId);
350 event.SetInt(n);
351 event.SetEventObject(this);
352 event.SetString(GetStringSelection());
353 if ( HasClientObjectData() )
354 event.SetClientObject( GetClientObject(n) );
355 else if ( HasClientUntypedData() )
356 event.SetClientData( GetClientData(n) );
357 ProcessCommand(event);
358 }
359
360 return TRUE;
361}
362
363WXHBRUSH wxChoice::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
364#if wxUSE_CTL3D
365 WXUINT message,
366 WXWPARAM wParam,
367 WXLPARAM lParam
368#else
369 WXUINT WXUNUSED(message),
370 WXWPARAM WXUNUSED(wParam),
371 WXLPARAM WXUNUSED(lParam)
372#endif
373 )
374{
375#if wxUSE_CTL3D
376 if ( m_useCtl3D )
377 {
378 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
379 return (WXHBRUSH) hbrush;
380 }
381#endif // wxUSE_CTL3D
382
383 HDC hdc = (HDC)pDC;
384 if (GetParent()->GetTransparentBackground())
385 SetBkMode(hdc, TRANSPARENT);
386 else
387 SetBkMode(hdc, OPAQUE);
388
389 wxColour colBack = GetBackgroundColour();
390
391 if (!IsEnabled())
392 colBack = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
393
394 ::SetBkColor(hdc, wxColourToRGB(colBack));
395 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
396
397 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
398
399 return (WXHBRUSH)brush->GetResourceHandle();
400}
401
402