updates for DoGetBestSize
[wxWidgets.git] / src / os2 / choice.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: choice.cpp
3 // Purpose: wxChoice
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/13/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/choice.h"
17 #include "wx/utils.h"
18 #include "wx/log.h"
19 #endif
20
21 #include "wx/os2/private.h"
22
23 #if !USE_SHARED_LIBRARY
24 IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
25 #endif
26
27 bool wxChoice::Create(wxWindow *parent,
28 wxWindowID id,
29 const wxPoint& pos,
30 const wxSize& size,
31 int n, const wxString choices[],
32 long style,
33 #if wxUSE_VALIDATORS
34 const wxValidator& validator,
35 #endif
36 const wxString& name)
37 {
38 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
39 return FALSE;
40 // TODO:
41 /*
42 long msStyle = WS_CHILD | CBS_DROPDOWNLIST | WS_TABSTOP | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL;
43 if ( style & wxCB_SORT )
44 msStyle |= CBS_SORT;
45
46 // the experience shows that wxChoice vs. wxComboBox distinction confuses
47 // quite a few people - try to help them
48 wxASSERT_MSG( !(style & wxCB_DROPDOWN) &&
49 !(style & wxCB_READONLY) &&
50 !(style & wxCB_SIMPLE),
51 wxT("this style flag is ignored by wxChoice, you "
52 "probably want to use a wxComboBox") );
53
54 if ( !OS2CreateControl(wxT("COMBOBOX"), msStyle) )
55 return FALSE;
56
57 for ( int i = 0; i < n; i++ )
58 {
59 Append(choices[i]);
60 }
61 */
62 SetSize(pos.x, pos.y, size.x, size.y);
63
64 return TRUE;
65 }
66
67 // ----------------------------------------------------------------------------
68 // adding/deleting items to/from the list
69 // ----------------------------------------------------------------------------
70
71 int wxChoice::DoAppend(const wxString& item)
72 {
73 // TODO:
74 /*
75 int n = (int)SendMessage(GetHwnd(), CB_ADDSTRING, 0, (LONG)item.c_str());
76 if ( n == CB_ERR )
77 {
78 wxLogLastError("SendMessage(CB_ADDSTRING)");
79 }
80 */
81 return 0; //n
82 }
83
84 void wxChoice::Delete(int n)
85 {
86 wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") );
87
88 // TODO: SendMessage(GetHwnd(), CB_DELETESTRING, n, 0);
89 }
90
91 void wxChoice::Clear()
92 {
93 // TODO: SendMessage(GetHwnd(), CB_RESETCONTENT, 0, 0);
94 }
95
96 // ----------------------------------------------------------------------------
97 // selection
98 // ----------------------------------------------------------------------------
99
100 int wxChoice::GetSelection() const
101 {
102 // TODO: return (int)SendMessage(GetHwnd(), CB_GETCURSEL, 0, 0);
103 return 0;
104 }
105
106 void wxChoice::SetSelection(int n)
107 {
108 // TODO: SendMessage(GetHwnd(), CB_SETCURSEL, n, 0);
109 }
110
111 // ----------------------------------------------------------------------------
112 // string list functions
113 // ----------------------------------------------------------------------------
114
115 int wxChoice::GetCount() const
116 {
117 // TODO: return (int)SendMessage(GetHwnd(), CB_GETCOUNT, 0, 0);
118 return 0;
119 }
120
121 int wxChoice::FindString(const wxString& s) const
122 {
123 // TODO:
124 /*
125 int pos = (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT,
126 (WPARAM)-1, (LPARAM)s.c_str());
127
128 return pos == LB_ERR ? wxNOT_FOUND : pos;
129 */
130 return 0;
131 }
132
133 void wxChoice::SetString(int n, const wxString& s)
134 {
135 wxFAIL_MSG(wxT("not implemented"));
136
137 #if 0 // should do this, but no Insert() so far
138 Delete(n);
139 Insert(n + 1, s);
140 #endif
141 }
142
143 wxString wxChoice::GetString(int n) const
144 {
145 size_t len = 0; // TODO: (size_t)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN, n, 0);
146 wxString str = "";
147 // TODO:
148 /*
149 if (len) {
150 if ( ::SendMessage(GetHwnd(), CB_GETLBTEXT, n,
151 (LPARAM)str.GetWriteBuf(len)) == CB_ERR ) {
152 wxLogLastError("SendMessage(CB_GETLBTEXT)");
153 }
154 str.UngetWriteBuf();
155 }
156 */
157 return str;
158 }
159
160 // ----------------------------------------------------------------------------
161 // client data
162 // ----------------------------------------------------------------------------
163
164 void wxChoice::DoSetItemClientData( int n, void* clientData )
165 {
166 // TODO:
167 /*
168 if ( SendMessage(GetHwnd(), CB_SETITEMDATA, n, (LPARAM)clientData) == CB_ERR )
169 {
170 wxLogLastError(wxT("CB_SETITEMDATA"));
171 }
172 */
173 }
174
175 void* wxChoice::DoGetItemClientData( int n ) const
176 {
177 // TODO:
178 /*
179 LPARAM rc = SendMessage(GetHwnd(), CB_GETITEMDATA, n, 0);
180 if ( rc == CB_ERR )
181 {
182 wxLogLastError(wxT("CB_GETITEMDATA"));
183
184 // unfortunately, there is no way to return an error code to the user
185 rc = (LPARAM) NULL;
186 }
187
188 return (void *)rc;
189 */
190 return NULL;
191 }
192
193 void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
194 {
195 DoSetItemClientData(n, clientData);
196 }
197
198 wxClientData* wxChoice::DoGetItemClientObject( int n ) const
199 {
200 return (wxClientData *)DoGetItemClientData(n);
201 }
202
203 // ----------------------------------------------------------------------------
204 // wxOS2 specific helpers
205 // ----------------------------------------------------------------------------
206
207 void wxChoice::DoSetSize(int x, int y,
208 int width, int height,
209 int sizeFlags)
210 {
211 // Ignore height parameter because height doesn't mean 'initially
212 // displayed' height, it refers to the drop-down menu as well. The
213 // wxWindows interpretation is different; also, getting the size returns
214 // the _displayed_ size (NOT the drop down menu size) so
215 // setting-getting-setting size would not work.
216 wxControl::DoSetSize(x, y, width, -1, sizeFlags);
217 }
218
219 wxSize wxChoice::DoGetBestSize() const
220 {
221 // find the widest string
222 int wLine;
223 int wChoice = 0;
224 int nItems = GetCount();
225 for ( int i = 0; i < nItems; i++ )
226 {
227 wxString str(GetString(i));
228 GetTextExtent(str, &wLine, NULL);
229 if ( wLine > wChoice )
230 wChoice = wLine;
231 }
232
233 // give it some reasonable default value if there are no strings in the
234 // list
235 if ( wChoice == 0 )
236 wChoice = 100;
237
238 // the combobox should be larger than the widest string
239 int cx, cy;
240 wxGetCharSize(GetHWND(), &cx, &cy, (wxFont*)&GetFont());
241
242 wChoice += 5*cx;
243
244 // Choice drop-down list depends on number of items (limited to 10)
245 size_t nStrings = nItems == 0 ? 10 : wxMin(10, nItems) + 1;
246 int hChoice = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*nStrings;
247
248 return wxSize(wChoice, hChoice);
249 }
250
251 MRESULT wxChoice::OS2WindowProc(HWND hwnd, WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
252 {
253 // TODO:
254 /*
255 if ( nMsg == WM_LBUTTONUP )
256 {
257 int x = (int)LOWORD(lParam);
258 int y = (int)HIWORD(lParam);
259
260 // Ok, this is truly weird, but if a panel with a wxChoice loses the
261 // focus, then you get a *fake* WM_LBUTTONUP message with x = 65535 and
262 // y = 65535. Filter out this nonsense.
263 //
264 // VZ: I'd like to know how to reproduce this please...
265 if ( x == 65535 && y == 65535 )
266 return 0;
267 }
268 */
269 return wxWindow::OS2WindowProc(hwnd, nMsg, wParam, lParam);
270 }
271
272 bool wxChoice::OS2Command(WXUINT param, WXWORD WXUNUSED(id))
273 {
274 // TODO:
275 /*
276 if ( param != CBN_SELCHANGE)
277 {
278 // "selection changed" is the only event we're after
279 return FALSE;
280 }
281 */
282 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId);
283 event.SetInt(GetSelection());
284 event.SetEventObject(this);
285 // TODO: event.SetString(GetStringSelection());
286 ProcessCommand(event);
287
288 return TRUE;
289 }
290