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