]> git.saurik.com Git - wxWidgets.git/blob - src/msw/choice.cpp
1. DoSetSize() simplified, DoGetBestSize() introduced
[wxWidgets.git] / src / msw / choice.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: choice.cpp
3 // Purpose: wxChoice
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "choice.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/choice.h"
25 #include "wx/utils.h"
26 #endif
27
28 #include "wx/msw/private.h"
29
30 #if !USE_SHARED_LIBRARY
31 IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
32 #endif
33
34 bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
35 {
36 if (param == CBN_SELCHANGE)
37 {
38 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId);
39 event.SetInt(GetSelection());
40 event.SetEventObject(this);
41 event.SetString(GetStringSelection());
42 ProcessCommand(event);
43
44 return TRUE;
45 }
46 else
47 return FALSE;
48 }
49
50 bool wxChoice::Create(wxWindow *parent, wxWindowID id,
51 const wxPoint& pos,
52 const wxSize& size,
53 int n, const wxString choices[],
54 long style,
55 const wxValidator& validator,
56 const wxString& name)
57 {
58 SetName(name);
59 SetValidator(validator);
60 if (parent) parent->AddChild(this);
61 SetBackgroundColour(parent->GetBackgroundColour()) ;
62 SetForegroundColour(parent->GetForegroundColour()) ;
63 m_noStrings = 0;
64
65 m_windowStyle = style;
66
67 if ( id == -1 )
68 m_windowId = (int)NewControlId();
69 else
70 m_windowId = id;
71
72 int x = pos.x;
73 int y = pos.y;
74 int width = size.x;
75 int height = size.y;
76
77 long msStyle = WS_CHILD | CBS_DROPDOWNLIST | WS_HSCROLL | WS_VSCROLL
78 | WS_TABSTOP | WS_VISIBLE;
79 if (m_windowStyle & wxCB_SORT)
80 msStyle |= CBS_SORT;
81
82 bool want3D;
83 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ;
84
85 // Even with extended styles, need to combine with WS_BORDER
86 // for them to look right.
87 if ( want3D || wxStyleHasBorder(m_windowStyle) )
88 msStyle |= WS_BORDER;
89
90 m_hWnd = (WXHWND)::CreateWindowEx(exStyle, _T("COMBOBOX"), NULL,
91 msStyle,
92 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
93 wxGetInstance(), NULL);
94
95 wxCHECK_MSG( m_hWnd, FALSE, _T("Failed to create combobox") );
96
97 /*
98 #if wxUSE_CTL3D
99 if (want3D)
100 {
101 m_useCtl3D = TRUE;
102 Ctl3dSubclassCtl(wx_combo); // Does CTL3D affect the combobox? I think not.
103 }
104 #endif
105 */
106
107 // Subclass again for purposes of dialog editing mode
108 SubclassWin(m_hWnd);
109
110 SetFont(parent->GetFont());
111
112 int i;
113 for (i = 0; i < n; i++)
114 {
115 Append(choices[i]);
116 }
117 SetSelection(n);
118
119 SetSize(x, y, width, height);
120
121 return TRUE;
122 }
123
124 void wxChoice::Append(const wxString& item)
125 {
126 SendMessage(GetHwnd(), CB_ADDSTRING, 0, (LONG)(const wxChar *)item);
127
128 m_noStrings ++;
129 }
130
131 void wxChoice::Delete(int n)
132 {
133 m_noStrings = (int)SendMessage(GetHwnd(), CB_DELETESTRING, n, 0);
134 }
135
136 void wxChoice::Clear(void)
137 {
138 SendMessage(GetHwnd(), CB_RESETCONTENT, 0, 0);
139
140 m_noStrings = 0;
141 }
142
143
144 int wxChoice::GetSelection(void) const
145 {
146 return (int)SendMessage(GetHwnd(), CB_GETCURSEL, 0, 0);
147 }
148
149 void wxChoice::SetSelection(int n)
150 {
151 SendMessage(GetHwnd(), CB_SETCURSEL, n, 0);
152 }
153
154 int wxChoice::FindString(const wxString& s) const
155 {
156 #if defined(__WATCOMC__) && defined(__WIN386__)
157 // For some reason, Watcom in WIN386 mode crashes in the CB_FINDSTRINGEXACT message.
158 // Do it the long way instead.
159 char buf[512];
160 for (int i = 0; i < Number(); i++)
161 {
162 int len = (int)SendMessage(GetHwnd(), CB_GETLBTEXT, i, (LPARAM)(LPSTR)buf);
163 buf[len] = 0;
164 if (strcmp(buf, (const char *)s) == 0)
165 return i;
166 }
167 return -1;
168 #else
169 int pos = (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT, (WPARAM)-1, (LPARAM)(LPSTR)(const wxChar *)s);
170 if (pos == LB_ERR)
171 return -1;
172 else
173 return pos;
174 #endif
175 }
176
177 wxString wxChoice::GetString(int n) const
178 {
179 size_t len = (size_t)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN, n, 0);
180 wxString str;
181 if ( ::SendMessage(GetHwnd(), CB_GETLBTEXT, n,
182 (LPARAM)str.GetWriteBuf(len)) == CB_ERR )
183 {
184 wxLogLastError("SendMessage(CB_GETLBTEXT)");
185 }
186
187 str.UngetWriteBuf();
188
189 return str;
190 }
191
192 void wxChoice::DoSetSize(int x, int y,
193 int width, int height,
194 int sizeFlags)
195 {
196 // Ignore height parameter because height doesn't mean 'initially
197 // displayed' height, it refers to the drop-down menu as well. The
198 // wxWindows interpretation is different; also, getting the size returns
199 // the _displayed_ size (NOT the drop down menu size) so
200 // setting-getting-setting size would not work.
201 wxControl::DoSetSize(x, y, width, -1, sizeFlags);
202 }
203
204 wxSize wxChoice::DoGetBestSize()
205 {
206 // find the widest string
207 int wLine;
208 int wChoice = 0;
209 for ( int i = 0; i < m_noStrings; i++ )
210 {
211 wxString str(GetString(i));
212 GetTextExtent(str, &wLine, NULL);
213 if ( wLine > wChoice )
214 wChoice = wLine;
215 }
216
217 // give it some reasonable default value if there are no strings in the
218 // list
219 if ( wChoice == 0 )
220 wChoice = 100;
221
222 // the combobox should be larger than the widest string
223 int cx, cy;
224 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
225
226 wChoice += 5*cx;
227
228 // Choice drop-down list depends on number of items (limited to 10)
229 size_t nStrings = m_noStrings == 0 ? 10 : wxMin(10, m_noStrings) + 1;
230 int hChoice = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*nStrings;
231
232 return wxSize(wChoice, hChoice);
233 }
234
235 WXHBRUSH wxChoice::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
236 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
237 {
238 return 0;
239 }
240
241 long wxChoice::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
242 {
243 switch (nMsg)
244 {
245 /*
246 case WM_GETDLGCODE:
247 {
248 if (GetWindowStyleFlag() & wxPROCESS_ENTER)
249 return DLGC_WANTALLKEYS;
250 break;
251 }
252 */
253 /*
254 case WM_CHAR: // Always an ASCII character
255 {
256 if (wParam == VK_RETURN)
257 {
258 wxCommandEvent event(wxEVENT_TYPE_TEXT_ENTER_COMMAND);
259 event.commandString = ((wxTextCtrl *)item)->GetValue();
260 event.eventObject = item;
261 item->ProcessCommand(event);
262 return FALSE;
263 }
264 break;
265 }
266 */
267 case WM_LBUTTONUP:
268 {
269 int x = (int)LOWORD(lParam);
270 int y = (int)HIWORD(lParam);
271
272 // Ok, this is truly weird, but if a panel with a wxChoice loses the
273 // focus, then you get a *fake* WM_LBUTTONUP message
274 // with x = 65535 and y = 65535.
275 // Filter out this nonsense.
276 if (x == 65535 && y == 65535)
277 return 0;
278 break;
279 }
280 }
281
282 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
283 }
284
285 wxString wxChoice::GetStringSelection (void) const
286 {
287 int sel = GetSelection ();
288 if (sel > -1)
289 return wxString(this->GetString (sel));
290 else
291 return wxString(_T(""));
292 }
293
294 bool wxChoice::SetStringSelection (const wxString& s)
295 {
296 int sel = FindString (s);
297 if (sel > -1)
298 {
299 SetSelection (sel);
300 return TRUE;
301 }
302 else
303 return FALSE;
304 }
305
306 void wxChoice::Command(wxCommandEvent & event)
307 {
308 SetSelection (event.GetInt());
309 ProcessCommand (event);
310 }
311
312
313