]> git.saurik.com Git - wxWidgets.git/blame - src/msw/choice.cpp
Added support for item clicks
[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
8d99be5f
VZ
88// ----------------------------------------------------------------------------
89// adding/deleting items to/from the list
90// ----------------------------------------------------------------------------
2bda0e17 91
def6fb9b 92int wxChoice::DoAppend(const wxString& item)
8d99be5f 93{
def6fb9b
VZ
94 int n = (int)SendMessage(GetHwnd(), CB_ADDSTRING, 0, (LONG)item.c_str());
95 if ( n == CB_ERR )
96 {
97 wxLogLastError("SendMessage(CB_ADDSTRING)");
98 }
99
100 return n;
2bda0e17
KB
101}
102
debe6624 103void wxChoice::Delete(int n)
2bda0e17 104{
223d09f6 105 wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") );
8d99be5f
VZ
106
107 SendMessage(GetHwnd(), CB_DELETESTRING, n, 0);
2bda0e17
KB
108}
109
8d99be5f 110void wxChoice::Clear()
2bda0e17 111{
8d99be5f 112 SendMessage(GetHwnd(), CB_RESETCONTENT, 0, 0);
2bda0e17
KB
113}
114
8d99be5f
VZ
115// ----------------------------------------------------------------------------
116// selection
117// ----------------------------------------------------------------------------
2bda0e17 118
8d99be5f 119int wxChoice::GetSelection() const
2bda0e17 120{
8d99be5f 121 return (int)SendMessage(GetHwnd(), CB_GETCURSEL, 0, 0);
2bda0e17
KB
122}
123
debe6624 124void wxChoice::SetSelection(int n)
2bda0e17 125{
8d99be5f
VZ
126 SendMessage(GetHwnd(), CB_SETCURSEL, n, 0);
127}
128
129// ----------------------------------------------------------------------------
130// string list functions
131// ----------------------------------------------------------------------------
132
133int wxChoice::GetCount() const
134{
135 return (int)SendMessage(GetHwnd(), CB_GETCOUNT, 0, 0);
2bda0e17
KB
136}
137
138int wxChoice::FindString(const wxString& s) const
139{
140#if defined(__WATCOMC__) && defined(__WIN386__)
8d99be5f
VZ
141 // For some reason, Watcom in WIN386 mode crashes in the CB_FINDSTRINGEXACT message.
142 // wxChoice::Do it the long way instead.
143 int count = GetCount();
144 for ( int i = 0; i < count; i++ )
145 {
146 // as CB_FINDSTRINGEXACT is case insensitive, be case insensitive too
147 if ( GetString(i).IsSameAs(s, FALSE) )
148 return i;
149 }
150
151 return wxNOT_FOUND;
152#else // !Watcom
153 int pos = (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT,
154 (WPARAM)-1, (LPARAM)s.c_str());
155
156 return pos == LB_ERR ? wxNOT_FOUND : pos;
157#endif // Watcom/!Watcom
2bda0e17
KB
158}
159
debe6624 160wxString wxChoice::GetString(int n) const
2bda0e17 161{
4438caf4 162 size_t len = (size_t)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN, n, 0);
21d72d17
RD
163 wxString str = "";
164 if (len) {
165 if ( ::SendMessage(GetHwnd(), CB_GETLBTEXT, n,
166 (LPARAM)str.GetWriteBuf(len)) == CB_ERR ) {
167 wxLogLastError("SendMessage(CB_GETLBTEXT)");
168 }
169 str.UngetWriteBuf();
4438caf4 170 }
2bda0e17 171
4438caf4
VZ
172 return str;
173}
2bda0e17 174
8d99be5f
VZ
175// ----------------------------------------------------------------------------
176// client data
177// ----------------------------------------------------------------------------
178
179void wxChoice::DoSetClientData( int n, void* clientData )
180{
181 if ( SendMessage(GetHwnd(), CB_SETITEMDATA, n, (LPARAM)clientData) == CB_ERR )
182 {
223d09f6 183 wxLogLastError(wxT("CB_SETITEMDATA"));
8d99be5f
VZ
184 }
185}
186
187void* wxChoice::DoGetClientData( int n ) const
188{
189 LPARAM rc = SendMessage(GetHwnd(), CB_GETITEMDATA, n, 0);
190 if ( rc == CB_ERR )
191 {
223d09f6 192 wxLogLastError(wxT("CB_GETITEMDATA"));
8d99be5f
VZ
193
194 // unfortunately, there is no way to return an error code to the user
8a4df159 195 rc = (LPARAM) NULL;
8d99be5f
VZ
196 }
197
198 return (void *)rc;
199}
200
201void wxChoice::DoSetClientObject( int n, wxClientData* clientData )
202{
203 DoSetClientData(n, clientData);
204}
205
206wxClientData* wxChoice::DoGetClientObject( int n ) const
207{
208 return (wxClientData *)DoGetClientData(n);
209}
210
211// ----------------------------------------------------------------------------
212// wxMSW specific helpers
213// ----------------------------------------------------------------------------
214
4438caf4
VZ
215void wxChoice::DoSetSize(int x, int y,
216 int width, int height,
217 int sizeFlags)
218{
219 // Ignore height parameter because height doesn't mean 'initially
220 // displayed' height, it refers to the drop-down menu as well. The
221 // wxWindows interpretation is different; also, getting the size returns
222 // the _displayed_ size (NOT the drop down menu size) so
223 // setting-getting-setting size would not work.
224 wxControl::DoSetSize(x, y, width, -1, sizeFlags);
225}
2bda0e17 226
4438caf4
VZ
227wxSize wxChoice::DoGetBestSize()
228{
229 // find the widest string
230 int wLine;
231 int wChoice = 0;
8d99be5f
VZ
232 int nItems = GetCount();
233 for ( int i = 0; i < nItems; i++ )
2bda0e17 234 {
2bda0e17 235 wxString str(GetString(i));
4438caf4
VZ
236 GetTextExtent(str, &wLine, NULL);
237 if ( wLine > wChoice )
238 wChoice = wLine;
2bda0e17 239 }
fd3f686c 240
4438caf4
VZ
241 // give it some reasonable default value if there are no strings in the
242 // list
243 if ( wChoice == 0 )
244 wChoice = 100;
2bda0e17 245
4438caf4
VZ
246 // the combobox should be larger than the widest string
247 int cx, cy;
248 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
2bda0e17 249
4438caf4 250 wChoice += 5*cx;
2bda0e17 251
4438caf4 252 // Choice drop-down list depends on number of items (limited to 10)
8d99be5f 253 size_t nStrings = nItems == 0 ? 10 : wxMin(10, nItems) + 1;
4438caf4 254 int hChoice = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*nStrings;
2bda0e17 255
4438caf4 256 return wxSize(wChoice, hChoice);
2bda0e17
KB
257}
258
2bda0e17
KB
259long wxChoice::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
260{
8d99be5f 261 if ( nMsg == WM_LBUTTONUP )
2bda0e17 262 {
2bda0e17
KB
263 int x = (int)LOWORD(lParam);
264 int y = (int)HIWORD(lParam);
265
8d99be5f
VZ
266 // Ok, this is truly weird, but if a panel with a wxChoice loses the
267 // focus, then you get a *fake* WM_LBUTTONUP message with x = 65535 and
268 // y = 65535. Filter out this nonsense.
269 //
270 // VZ: I'd like to know how to reproduce this please...
271 if ( x == 65535 && y == 65535 )
272 return 0;
2bda0e17
KB
273 }
274
8d99be5f 275 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
2bda0e17
KB
276}
277
8d99be5f 278bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
2bda0e17 279{
8d99be5f 280 if ( param != CBN_SELCHANGE)
2bda0e17 281 {
8d99be5f
VZ
282 // "selection changed" is the only event we're after
283 return FALSE;
2bda0e17 284 }
2bda0e17 285
8d99be5f
VZ
286 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId);
287 event.SetInt(GetSelection());
288 event.SetEventObject(this);
289 event.SetString(GetStringSelection());
290 ProcessCommand(event);
2bda0e17 291
8d99be5f
VZ
292 return TRUE;
293}
2bda0e17 294