]> git.saurik.com Git - wxWidgets.git/blame - src/msw/wince/choicece.cpp
Fixed non-precompiled headers compilation
[wxWidgets.git] / src / msw / wince / choicece.cpp
CommitLineData
1550d5f8
WS
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/msw/wince/choicece.cpp
8300f815 3// Purpose: wxChoice implementation for smart phones driven by WinCE
1550d5f8
WS
4// Author: Wlodzimierz ABX Skiba
5// Modified by:
6// Created: 29.07.2004
7// RCS-ID: $Id$
8// Copyright: (c) Wlodzimierz Skiba
9// License: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
1550d5f8
WS
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
1550d5f8
WS
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
8228b893
WS
27#if wxUSE_CHOICE && defined(__SMARTPHONE__) && defined(__WXWINCE__)
28
b36e08d0
WS
29#include "wx/choice.h"
30
1550d5f8 31#ifndef WX_PRECOMP
57bd4c60 32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
1550d5f8
WS
33#endif
34
35#include "wx/spinbutt.h" // for wxSpinnerBestSize
36
1550d5f8
WS
37#if wxUSE_EXTENDED_RTTI
38// TODO
39#else
40IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
41#endif
42
43#define GetBuddyHwnd() (HWND)(m_hwndBuddy)
44
45#define IsVertical(wxStyle) ( (wxStyle & wxSP_HORIZONTAL) != wxSP_HORIZONTAL )
46
47// ----------------------------------------------------------------------------
48// constants
49// ----------------------------------------------------------------------------
50
51// the margin between the up-down control and its buddy (can be arbitrary,
52// choose what you like - or may be decide during run-time depending on the
53// font size?)
54static const int MARGIN_BETWEEN = 0;
55
56// ============================================================================
57// implementation
58// ============================================================================
59
60wxArrayChoiceSpins wxChoice::ms_allChoiceSpins;
61
62// ----------------------------------------------------------------------------
63// wnd proc for the buddy text ctrl
64// ----------------------------------------------------------------------------
65
66LRESULT APIENTRY _EXPORT wxBuddyChoiceWndProc(HWND hwnd,
67 UINT message,
68 WPARAM wParam,
69 LPARAM lParam)
70{
71 wxChoice *spin = (wxChoice *)wxGetWindowUserData(hwnd);
72
73 // forward some messages (the key and focus ones only so far) to
74 // the spin ctrl
75 switch ( message )
76 {
77 case WM_SETFOCUS:
78 // if the focus comes from the spin control itself, don't set it
79 // back to it -- we don't want to go into an infinite loop
80 if ( (WXHWND)wParam == spin->GetHWND() )
81 break;
82 //else: fall through
83
84 case WM_KILLFOCUS:
85 case WM_CHAR:
86 case WM_DEADCHAR:
87 case WM_KEYUP:
88 case WM_KEYDOWN:
89 spin->MSWWindowProc(message, wParam, lParam);
90
91 // The control may have been deleted at this point, so check.
92 if ( !::IsWindow(hwnd) || wxGetWindowUserData(hwnd) != spin )
93 return 0;
94 break;
95
96 case WM_GETDLGCODE:
97 // we want to get WXK_RETURN in order to generate the event for it
98 return DLGC_WANTCHARS;
99 }
100
101 return ::CallWindowProc(CASTWNDPROC spin->GetBuddyWndProc(),
102 hwnd, message, wParam, lParam);
103}
104
01d2bf4d
WS
105wxChoice *wxChoice::GetChoiceForListBox(WXHWND hwndBuddy)
106{
107 wxChoice *choice = (wxChoice *)wxGetWindowUserData((HWND)hwndBuddy);
108
109 int i = ms_allChoiceSpins.Index(choice);
110
111 if ( i == wxNOT_FOUND )
112 return NULL;
113
114 // sanity check
115 wxASSERT_MSG( choice->m_hwndBuddy == hwndBuddy,
116 _T("wxChoice has incorrect buddy HWND!") );
117
118 return choice;
119}
120
1550d5f8
WS
121// ----------------------------------------------------------------------------
122// creation
123// ----------------------------------------------------------------------------
124
125bool wxChoice::Create(wxWindow *parent,
126 wxWindowID id,
127 const wxPoint& pos,
128 const wxSize& size,
129 int n, const wxString choices[],
130 long style,
131 const wxValidator& validator,
132 const wxString& name)
133{
134 return CreateAndInit(parent, id, pos, size, n, choices, style,
135 validator, name);
136}
137
138bool wxChoice::CreateAndInit(wxWindow *parent,
139 wxWindowID id,
140 const wxPoint& pos,
141 const wxSize& size,
142 int n, const wxString choices[],
143 long style,
144 const wxValidator& validator,
145 const wxString& name)
146{
147 if ( !(style & wxSP_VERTICAL) )
148 style |= wxSP_HORIZONTAL;
149
150 if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT )
151 style |= wxBORDER_SIMPLE;
152
153 style |= wxSP_ARROW_KEYS;
154
155 SetWindowStyle(style);
156
157 WXDWORD exStyle = 0;
158 WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), & exStyle) ;
159
160 wxSize sizeText(size), sizeBtn(size);
5aaabb37 161 sizeBtn.x = GetBestSpinnerSize(IsVertical(style)).x;
1550d5f8 162
1550d5f8
WS
163 if ( sizeText.x == wxDefaultCoord )
164 {
165 // DEFAULT_ITEM_WIDTH is the default width for the text control
166 sizeText.x = DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN + sizeBtn.x;
167 }
168
169 sizeText.x -= sizeBtn.x + MARGIN_BETWEEN;
170 if ( sizeText.x <= 0 )
171 {
172 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
173 }
174
175 wxPoint posBtn(pos);
176 posBtn.x += sizeText.x + MARGIN_BETWEEN;
177
178 // we must create the list control before the spin button for the purpose
179 // of the dialog navigation: if there is a static text just before the spin
180 // control, activating it by Alt-letter should give focus to the text
181 // control, not the spin and the dialog navigation code will give focus to
182 // the next control (at Windows level), not the one after it
183
184 // create the text window
185
186 m_hwndBuddy = (WXHWND)::CreateWindowEx
187 (
188 exStyle, // sunken border
189 _T("LISTBOX"), // window class
190 NULL, // no window title
191 msStyle, // style (will be shown later)
192 pos.x, pos.y, // position
193 0, 0, // size (will be set later)
194 GetHwndOf(parent), // parent
195 (HMENU)-1, // control id
196 wxGetInstance(), // app instance
197 NULL // unused client data
198 );
199
200 if ( !m_hwndBuddy )
201 {
202 wxLogLastError(wxT("CreateWindow(buddy text window)"));
203
204 return false;
205 }
206
207 // initialize wxControl
208 if ( !CreateControl(parent, id, posBtn, sizeBtn, style, validator, name) )
209 return false;
210
211 // now create the real HWND
212 WXDWORD spiner_style = WS_VISIBLE |
213 UDS_ALIGNRIGHT |
214 UDS_ARROWKEYS |
215 UDS_SETBUDDYINT |
216 UDS_EXPANDABLE;
217
218 if ( !IsVertical(style) )
219 spiner_style |= UDS_HORZ;
220
221 if ( style & wxSP_WRAP )
222 spiner_style |= UDS_WRAP;
223
11e62fe6 224 if ( !MSWCreateControl(UPDOWN_CLASS, spiner_style, posBtn, sizeBtn, wxEmptyString, 0) )
1550d5f8
WS
225 return false;
226
227 // subclass the text ctrl to be able to intercept some events
228 wxSetWindowUserData(GetBuddyHwnd(), this);
229 m_wndProcBuddy = (WXFARPROC)wxSetWindowProc(GetBuddyHwnd(),
230 wxBuddyChoiceWndProc);
231
232 // set up fonts and colours (This is nomally done in MSWCreateControl)
233 InheritAttributes();
234 if (!m_hasFont)
235 SetFont(GetDefaultAttributes().font);
236
237 // set the size of the text window - can do it only now, because we
238 // couldn't call DoGetBestSize() before as font wasn't set
239 if ( sizeText.y <= 0 )
240 {
241 int cx, cy;
242 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
243
244 sizeText.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
245 }
246
170acdc9 247 SetInitialSize(size);
1550d5f8
WS
248
249 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW);
250
251 // associate the list window with the spin button
252 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)GetBuddyHwnd(), 0);
253
254 // do it after finishing with m_hwndBuddy creation to avoid generating
255 // initial wxEVT_COMMAND_TEXT_UPDATED message
256 ms_allChoiceSpins.Add(this);
257
258 // initialize the controls contents
259 for ( int i = 0; i < n; i++ )
260 {
261 Append(choices[i]);
262 }
263
264 return true;
265}
266
267bool wxChoice::Create(wxWindow *parent,
268 wxWindowID id,
269 const wxPoint& pos,
270 const wxSize& size,
271 const wxArrayString& choices,
272 long style,
273 const wxValidator& validator,
274 const wxString& name)
275{
276 wxCArrayString chs(choices);
277 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
278 style, validator, name);
279}
280
281WXDWORD wxChoice::MSWGetStyle(long style, WXDWORD *exstyle) const
282{
283 // we never have an external border
284 WXDWORD msStyle = wxControl::MSWGetStyle
285 (
286 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle
287 );
288
289 msStyle |= WS_VISIBLE;
290
291 // wxChoice-specific styles
292 msStyle |= LBS_NOINTEGRALHEIGHT;
293 if ( style & wxCB_SORT )
294 msStyle |= LBS_SORT;
295
01d2bf4d
WS
296 msStyle |= LBS_NOTIFY;
297
1550d5f8
WS
298 return msStyle;
299}
300
01d2bf4d
WS
301bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
302{
303 if ( param != LBN_SELCHANGE)
304 {
305 // "selection changed" is the only event we're after
306 return false;
307 }
308
309 int n = GetSelection();
310 if (n > -1)
311 {
312 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId);
313 event.SetInt(n);
314 event.SetEventObject(this);
315 event.SetString(GetStringSelection());
316 if ( HasClientObjectData() )
317 event.SetClientObject( GetClientObject(n) );
318 else if ( HasClientUntypedData() )
319 event.SetClientData( GetClientData(n) );
320 ProcessCommand(event);
321 }
322
323 return true;
324}
325
1550d5f8
WS
326wxChoice::~wxChoice()
327{
a236aa20 328 Clear();
1550d5f8
WS
329}
330
331// ----------------------------------------------------------------------------
332// adding/deleting items to/from the list
333// ----------------------------------------------------------------------------
334
a236aa20
VZ
335int wxChoice::DoInsertItems(const wxArrayStringsAdapter& items,
336 unsigned int pos,
337 void **clientData,
338 wxClientDataType type)
1550d5f8 339{
a236aa20 340 MSWAllocStorage(items, LB_INITSTORAGE);
1550d5f8 341
a236aa20
VZ
342 const bool append = pos == GetCount();
343 const unsigned msg = append ? LB_ADDSTRING : LB_INSERTSTRING;
344 if ( append )
345 pos = 0;
1550d5f8 346
a236aa20 347 int n = wxNOT_FOUND;
1550d5f8 348
a236aa20
VZ
349 const unsigned int numItems = items.GetCount();
350 for ( unsigned int i = 0; i < numItems; ++i )
1550d5f8 351 {
a236aa20
VZ
352 n = MSWInsertOrAppendItem(pos, items[i], msg);
353 if ( !append )
354 pos++;
355
356 AssignNewItemClientData(n, clientData, i, type);
1550d5f8
WS
357 }
358
359 return n;
360}
361
a236aa20 362void wxChoice::DoDeleteOneItem(unsigned int n)
1550d5f8 363{
8228b893 364 wxCHECK_RET( IsValid(n), wxT("invalid item index in wxChoice::Delete") );
1550d5f8 365
39fc096d 366 ::SendMessage(GetBuddyHwnd(), LB_DELETESTRING, n, 0);
1550d5f8
WS
367}
368
a236aa20 369void wxChoice::DoClear()
1550d5f8 370{
39fc096d 371 ::SendMessage(GetBuddyHwnd(), LB_RESETCONTENT, 0, 0);
1550d5f8
WS
372}
373
1550d5f8
WS
374// ----------------------------------------------------------------------------
375// selection
376// ----------------------------------------------------------------------------
377
378int wxChoice::GetSelection() const
379{
39fc096d 380 return (int)::SendMessage(GetBuddyHwnd(), LB_GETCURSEL, 0, 0);
1550d5f8
WS
381}
382
383void wxChoice::SetSelection(int n)
384{
39fc096d 385 ::SendMessage(GetBuddyHwnd(), LB_SETCURSEL, n, 0);
1550d5f8
WS
386}
387
388// ----------------------------------------------------------------------------
389// string list functions
390// ----------------------------------------------------------------------------
391
aa61d352 392unsigned int wxChoice::GetCount() const
1550d5f8 393{
aa61d352 394 return (unsigned int)::SendMessage(GetBuddyHwnd(), LB_GETCOUNT, 0, 0);
1550d5f8
WS
395}
396
11e62fe6 397int wxChoice::FindString(const wxString& s, bool bCase) const
1550d5f8 398{
11e62fe6
WS
399 // back to base class search for not native search type
400 if (bCase)
401 return wxItemContainerImmutable::FindString( s, bCase );
402
39fc096d 403 int pos = (int)::SendMessage(GetBuddyHwnd(), LB_FINDSTRINGEXACT,
1550d5f8
WS
404 (WPARAM)-1, (LPARAM)s.c_str());
405
406 return pos == LB_ERR ? wxNOT_FOUND : pos;
407}
408
aa61d352 409void wxChoice::SetString(unsigned int n, const wxString& s)
1550d5f8 410{
8228b893 411 wxCHECK_RET( IsValid(n),
1550d5f8
WS
412 wxT("invalid item index in wxChoice::SetString") );
413
414 // we have to delete and add back the string as there is no way to change a
415 // string in place
416
417 // we need to preserve the client data
418 void *data;
419 if ( m_clientDataItemsType != wxClientData_None )
420 {
421 data = DoGetItemClientData(n);
422 }
423 else // no client data
424 {
425 data = NULL;
426 }
427
428 ::SendMessage(GetBuddyHwnd(), LB_DELETESTRING, n, 0);
429 ::SendMessage(GetBuddyHwnd(), LB_INSERTSTRING, n, (LPARAM)s.c_str() );
430
431 if ( data )
432 {
433 DoSetItemClientData(n, data);
434 }
435 //else: it's already NULL by default
436}
437
aa61d352 438wxString wxChoice::GetString(unsigned int n) const
1550d5f8
WS
439{
440 int len = (int)::SendMessage(GetBuddyHwnd(), LB_GETTEXTLEN, n, 0);
441
442 wxString str;
443 if ( len != LB_ERR && len > 0 )
444 {
445 if ( ::SendMessage
446 (
447 GetBuddyHwnd(),
448 LB_GETTEXT,
449 n,
450 (LPARAM)(wxChar *)wxStringBuffer(str, len)
451 ) == LB_ERR )
452 {
453 wxLogLastError(wxT("SendMessage(LB_GETLBTEXT)"));
454 }
455 }
456
457 return str;
458}
459
460// ----------------------------------------------------------------------------
461// client data
462// ----------------------------------------------------------------------------
463
aa61d352 464void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
1550d5f8
WS
465{
466 if ( ::SendMessage(GetHwnd(), LB_SETITEMDATA,
467 n, (LPARAM)clientData) == LB_ERR )
468 {
469 wxLogLastError(wxT("LB_SETITEMDATA"));
470 }
471}
472
aa61d352 473void* wxChoice::DoGetItemClientData(unsigned int n) const
1550d5f8 474{
39fc096d 475 LPARAM rc = ::SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0);
1550d5f8
WS
476 if ( rc == LB_ERR )
477 {
478 wxLogLastError(wxT("LB_GETITEMDATA"));
479
480 // unfortunately, there is no way to return an error code to the user
481 rc = (LPARAM) NULL;
482 }
483
484 return (void *)rc;
485}
486
1550d5f8
WS
487// ----------------------------------------------------------------------------
488// size calculations
489// ----------------------------------------------------------------------------
490
491wxSize wxChoice::DoGetBestSize() const
492{
5aaabb37 493 wxSize sizeBtn = GetBestSpinnerSize(IsVertical(GetWindowStyle()));
1550d5f8
WS
494 sizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN;
495
496 int y;
497 wxGetCharSize(GetHWND(), NULL, &y, GetFont());
498 y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(y);
499
500 // JACS: we should always use the height calculated
501 // from above, because otherwise we'll get a spin control
502 // that's too big. So never use the height calculated
503 // from wxSpinButton::DoGetBestSize().
504
505 // if ( sizeBtn.y < y )
506 {
507 // make the text tall enough
508 sizeBtn.y = y;
509 }
510
511 return sizeBtn;
512}
513
514void wxChoice::DoMoveWindow(int x, int y, int width, int height)
515{
5aaabb37 516 int widthBtn = GetBestSpinnerSize(IsVertical(GetWindowStyle())).x;
1550d5f8
WS
517 int widthText = width - widthBtn - MARGIN_BETWEEN;
518 if ( widthText <= 0 )
519 {
520 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
521 }
522
523 if ( !::MoveWindow(GetBuddyHwnd(), x, y, widthText, height, TRUE) )
524 {
525 wxLogLastError(wxT("MoveWindow(buddy)"));
526 }
527
528 x += widthText + MARGIN_BETWEEN;
529 if ( !::MoveWindow(GetHwnd(), x, y, widthBtn, height, TRUE) )
530 {
531 wxLogLastError(wxT("MoveWindow"));
532 }
533}
534
535// get total size of the control
536void wxChoice::DoGetSize(int *x, int *y) const
537{
538 RECT spinrect, textrect, ctrlrect;
539 GetWindowRect(GetHwnd(), &spinrect);
540 GetWindowRect(GetBuddyHwnd(), &textrect);
541 UnionRect(&ctrlrect, &textrect, &spinrect);
542
543 if ( x )
544 *x = ctrlrect.right - ctrlrect.left;
545 if ( y )
546 *y = ctrlrect.bottom - ctrlrect.top;
547}
548
549void wxChoice::DoGetPosition(int *x, int *y) const
550{
551 // hack: pretend that our HWND is the text control just for a moment
552 WXHWND hWnd = GetHWND();
553 wxConstCast(this, wxChoice)->m_hWnd = m_hwndBuddy;
554
555 wxChoiceBase::DoGetPosition(x, y);
556
557 wxConstCast(this, wxChoice)->m_hWnd = hWnd;
558}
559
8300f815 560#endif // wxUSE_CHOICE && __SMARTPHONE__ && __WXWINCE__