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