1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/wince/choicece.cpp
3 // Purpose: wxChoice implementation for smart phones driven by WinCE
4 // Author: Wlodzimierz ABX Skiba
8 // Copyright: (c) Wlodzimierz Skiba
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_CHOICE && defined(__SMARTPHONE__) && defined(__WXWINCE__)
30 #include "wx/choice.h"
32 #include "wx/msw/missing.h"
33 #include "wx/msw/winundef.h"
36 #include "wx/spinbutt.h" // for wxSpinnerBestSize
38 #if wxUSE_EXTENDED_RTTI
41 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
44 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
46 #define IsVertical(wxStyle) ( (wxStyle & wxSP_HORIZONTAL) != wxSP_HORIZONTAL )
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 // the margin between the up-down control and its buddy (can be arbitrary,
53 // choose what you like - or may be decide during run-time depending on the
55 static const int MARGIN_BETWEEN
= 0;
57 // ============================================================================
59 // ============================================================================
61 wxArrayChoiceSpins
wxChoice::ms_allChoiceSpins
;
63 // ----------------------------------------------------------------------------
64 // wnd proc for the buddy text ctrl
65 // ----------------------------------------------------------------------------
67 LRESULT APIENTRY _EXPORT
wxBuddyChoiceWndProc(HWND hwnd
,
72 wxChoice
*spin
= (wxChoice
*)wxGetWindowUserData(hwnd
);
74 // forward some messages (the key and focus ones only so far) to
79 // if the focus comes from the spin control itself, don't set it
80 // back to it -- we don't want to go into an infinite loop
81 if ( (WXHWND
)wParam
== spin
->GetHWND() )
90 spin
->MSWWindowProc(message
, wParam
, lParam
);
92 // The control may have been deleted at this point, so check.
93 if ( !::IsWindow(hwnd
) || wxGetWindowUserData(hwnd
) != spin
)
98 // we want to get WXK_RETURN in order to generate the event for it
99 return DLGC_WANTCHARS
;
102 return ::CallWindowProc(CASTWNDPROC spin
->GetBuddyWndProc(),
103 hwnd
, message
, wParam
, lParam
);
106 wxChoice
*wxChoice::GetChoiceForListBox(WXHWND hwndBuddy
)
108 wxChoice
*choice
= (wxChoice
*)wxGetWindowUserData((HWND
)hwndBuddy
);
110 int i
= ms_allChoiceSpins
.Index(choice
);
112 if ( i
== wxNOT_FOUND
)
116 wxASSERT_MSG( choice
->m_hwndBuddy
== hwndBuddy
,
117 _T("wxChoice has incorrect buddy HWND!") );
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 bool wxChoice::Create(wxWindow
*parent
,
130 int n
, const wxString choices
[],
132 const wxValidator
& validator
,
133 const wxString
& name
)
135 return CreateAndInit(parent
, id
, pos
, size
, n
, choices
, style
,
139 bool wxChoice::CreateAndInit(wxWindow
*parent
,
143 int n
, const wxString choices
[],
145 const wxValidator
& validator
,
146 const wxString
& name
)
148 if ( !(style
& wxSP_VERTICAL
) )
149 style
|= wxSP_HORIZONTAL
;
151 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
152 style
|= wxBORDER_SIMPLE
;
154 style
|= wxSP_ARROW_KEYS
;
156 SetWindowStyle(style
);
159 WXDWORD msStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
161 wxSize
sizeText(size
), sizeBtn(size
);
162 sizeBtn
.x
= GetBestSpinnerSize(IsVertical(style
)).x
;
164 if ( sizeText
.x
== wxDefaultCoord
)
166 // DEFAULT_ITEM_WIDTH is the default width for the text control
167 sizeText
.x
= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
+ sizeBtn
.x
;
170 sizeText
.x
-= sizeBtn
.x
+ MARGIN_BETWEEN
;
171 if ( sizeText
.x
<= 0 )
173 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
177 posBtn
.x
+= sizeText
.x
+ MARGIN_BETWEEN
;
179 // we must create the list control before the spin button for the purpose
180 // of the dialog navigation: if there is a static text just before the spin
181 // control, activating it by Alt-letter should give focus to the text
182 // control, not the spin and the dialog navigation code will give focus to
183 // the next control (at Windows level), not the one after it
185 // create the text window
187 m_hwndBuddy
= (WXHWND
)::CreateWindowEx
189 exStyle
, // sunken border
190 _T("LISTBOX"), // window class
191 NULL
, // no window title
192 msStyle
, // style (will be shown later)
193 pos
.x
, pos
.y
, // position
194 0, 0, // size (will be set later)
195 GetHwndOf(parent
), // parent
196 (HMENU
)-1, // control id
197 wxGetInstance(), // app instance
198 NULL
// unused client data
203 wxLogLastError(wxT("CreateWindow(buddy text window)"));
208 // initialize wxControl
209 if ( !CreateControl(parent
, id
, posBtn
, sizeBtn
, style
, validator
, name
) )
212 // now create the real HWND
213 WXDWORD spiner_style
= WS_VISIBLE
|
219 if ( !IsVertical(style
) )
220 spiner_style
|= UDS_HORZ
;
222 if ( style
& wxSP_WRAP
)
223 spiner_style
|= UDS_WRAP
;
225 if ( !MSWCreateControl(UPDOWN_CLASS
, spiner_style
, posBtn
, sizeBtn
, wxEmptyString
, 0) )
228 // subclass the text ctrl to be able to intercept some events
229 wxSetWindowUserData(GetBuddyHwnd(), this);
230 m_wndProcBuddy
= (WXFARPROC
)wxSetWindowProc(GetBuddyHwnd(),
231 wxBuddyChoiceWndProc
);
233 // set up fonts and colours (This is nomally done in MSWCreateControl)
236 SetFont(GetDefaultAttributes().font
);
238 // set the size of the text window - can do it only now, because we
239 // couldn't call DoGetBestSize() before as font wasn't set
240 if ( sizeText
.y
<= 0 )
243 wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont());
245 sizeText
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
250 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW
);
252 // associate the list window with the spin button
253 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)GetBuddyHwnd(), 0);
255 // do it after finishing with m_hwndBuddy creation to avoid generating
256 // initial wxEVT_COMMAND_TEXT_UPDATED message
257 ms_allChoiceSpins
.Add(this);
259 // initialize the controls contents
260 for ( int i
= 0; i
< n
; i
++ )
268 bool wxChoice::Create(wxWindow
*parent
,
272 const wxArrayString
& choices
,
274 const wxValidator
& validator
,
275 const wxString
& name
)
277 wxCArrayString
chs(choices
);
278 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
279 style
, validator
, name
);
282 WXDWORD
wxChoice::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
284 // we never have an external border
285 WXDWORD msStyle
= wxControl::MSWGetStyle
287 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
290 msStyle
|= WS_VISIBLE
;
292 // wxChoice-specific styles
293 msStyle
|= LBS_NOINTEGRALHEIGHT
;
294 if ( style
& wxCB_SORT
)
297 msStyle
|= LBS_NOTIFY
;
302 bool wxChoice::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
304 if ( param
!= LBN_SELCHANGE
)
306 // "selection changed" is the only event we're after
310 int n
= GetSelection();
313 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
315 event
.SetEventObject(this);
316 event
.SetString(GetStringSelection());
317 if ( HasClientObjectData() )
318 event
.SetClientObject( GetClientObject(n
) );
319 else if ( HasClientUntypedData() )
320 event
.SetClientData( GetClientData(n
) );
321 ProcessCommand(event
);
327 wxChoice::~wxChoice()
332 // ----------------------------------------------------------------------------
333 // adding/deleting items to/from the list
334 // ----------------------------------------------------------------------------
336 int wxChoice::DoAppend(const wxString
& item
)
338 int n
= (int)::SendMessage(GetBuddyHwnd(), LB_ADDSTRING
, 0, (LPARAM
)item
.c_str());
342 wxLogLastError(wxT("SendMessage(LB_ADDSTRING)"));
348 int wxChoice::DoInsert(const wxString
& item
, unsigned int pos
)
350 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into choice"));
351 wxCHECK_MSG(IsValidInsert(pos
), -1, wxT("invalid index"));
353 int n
= (int)::SendMessage(GetBuddyHwnd(), LB_INSERTSTRING
, pos
, (LPARAM
)item
.c_str());
356 wxLogLastError(wxT("SendMessage(LB_INSERTSTRING)"));
362 void wxChoice::Delete(unsigned int n
)
364 wxCHECK_RET( IsValid(n
), wxT("invalid item index in wxChoice::Delete") );
366 if ( HasClientObjectData() )
368 delete GetClientObject(n
);
371 ::SendMessage(GetBuddyHwnd(), LB_DELETESTRING
, n
, 0);
374 void wxChoice::Clear()
378 ::SendMessage(GetBuddyHwnd(), LB_RESETCONTENT
, 0, 0);
381 void wxChoice::Free()
383 if ( HasClientObjectData() )
385 unsigned int count
= GetCount();
386 for ( unsigned int n
= 0; n
< count
; n
++ )
388 delete GetClientObject(n
);
393 // ----------------------------------------------------------------------------
395 // ----------------------------------------------------------------------------
397 int wxChoice::GetSelection() const
399 return (int)::SendMessage(GetBuddyHwnd(), LB_GETCURSEL
, 0, 0);
402 void wxChoice::SetSelection(int n
)
404 ::SendMessage(GetBuddyHwnd(), LB_SETCURSEL
, n
, 0);
407 // ----------------------------------------------------------------------------
408 // string list functions
409 // ----------------------------------------------------------------------------
411 unsigned int wxChoice::GetCount() const
413 return (unsigned int)::SendMessage(GetBuddyHwnd(), LB_GETCOUNT
, 0, 0);
416 int wxChoice::FindString(const wxString
& s
, bool bCase
) const
418 // back to base class search for not native search type
420 return wxItemContainerImmutable::FindString( s
, bCase
);
422 int pos
= (int)::SendMessage(GetBuddyHwnd(), LB_FINDSTRINGEXACT
,
423 (WPARAM
)-1, (LPARAM
)s
.c_str());
425 return pos
== LB_ERR
? wxNOT_FOUND
: pos
;
428 void wxChoice::SetString(unsigned int n
, const wxString
& s
)
430 wxCHECK_RET( IsValid(n
),
431 wxT("invalid item index in wxChoice::SetString") );
433 // we have to delete and add back the string as there is no way to change a
436 // we need to preserve the client data
438 if ( m_clientDataItemsType
!= wxClientData_None
)
440 data
= DoGetItemClientData(n
);
442 else // no client data
447 ::SendMessage(GetBuddyHwnd(), LB_DELETESTRING
, n
, 0);
448 ::SendMessage(GetBuddyHwnd(), LB_INSERTSTRING
, n
, (LPARAM
)s
.c_str() );
452 DoSetItemClientData(n
, data
);
454 //else: it's already NULL by default
457 wxString
wxChoice::GetString(unsigned int n
) const
459 int len
= (int)::SendMessage(GetBuddyHwnd(), LB_GETTEXTLEN
, n
, 0);
462 if ( len
!= LB_ERR
&& len
> 0 )
469 (LPARAM
)(wxChar
*)wxStringBuffer(str
, len
)
472 wxLogLastError(wxT("SendMessage(LB_GETLBTEXT)"));
479 // ----------------------------------------------------------------------------
481 // ----------------------------------------------------------------------------
483 void wxChoice::DoSetItemClientData(unsigned int n
, void* clientData
)
485 if ( ::SendMessage(GetHwnd(), LB_SETITEMDATA
,
486 n
, (LPARAM
)clientData
) == LB_ERR
)
488 wxLogLastError(wxT("LB_SETITEMDATA"));
492 void* wxChoice::DoGetItemClientData(unsigned int n
) const
494 LPARAM rc
= ::SendMessage(GetHwnd(), LB_GETITEMDATA
, n
, 0);
497 wxLogLastError(wxT("LB_GETITEMDATA"));
499 // unfortunately, there is no way to return an error code to the user
506 void wxChoice::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
508 DoSetItemClientData(n
, clientData
);
511 wxClientData
* wxChoice::DoGetItemClientObject(unsigned int n
) const
513 return (wxClientData
*)DoGetItemClientData(n
);
516 // ----------------------------------------------------------------------------
518 // ----------------------------------------------------------------------------
520 wxSize
wxChoice::DoGetBestSize() const
522 wxSize sizeBtn
= GetBestSpinnerSize(IsVertical(GetWindowStyle()));
523 sizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
526 wxGetCharSize(GetHWND(), NULL
, &y
, GetFont());
527 y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
);
529 // JACS: we should always use the height calculated
530 // from above, because otherwise we'll get a spin control
531 // that's too big. So never use the height calculated
532 // from wxSpinButton::DoGetBestSize().
534 // if ( sizeBtn.y < y )
536 // make the text tall enough
543 void wxChoice::DoMoveWindow(int x
, int y
, int width
, int height
)
545 int widthBtn
= GetBestSpinnerSize(IsVertical(GetWindowStyle())).x
;
546 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
547 if ( widthText
<= 0 )
549 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
552 if ( !::MoveWindow(GetBuddyHwnd(), x
, y
, widthText
, height
, TRUE
) )
554 wxLogLastError(wxT("MoveWindow(buddy)"));
557 x
+= widthText
+ MARGIN_BETWEEN
;
558 if ( !::MoveWindow(GetHwnd(), x
, y
, widthBtn
, height
, TRUE
) )
560 wxLogLastError(wxT("MoveWindow"));
564 // get total size of the control
565 void wxChoice::DoGetSize(int *x
, int *y
) const
567 RECT spinrect
, textrect
, ctrlrect
;
568 GetWindowRect(GetHwnd(), &spinrect
);
569 GetWindowRect(GetBuddyHwnd(), &textrect
);
570 UnionRect(&ctrlrect
, &textrect
, &spinrect
);
573 *x
= ctrlrect
.right
- ctrlrect
.left
;
575 *y
= ctrlrect
.bottom
- ctrlrect
.top
;
578 void wxChoice::DoGetPosition(int *x
, int *y
) const
580 // hack: pretend that our HWND is the text control just for a moment
581 WXHWND hWnd
= GetHWND();
582 wxConstCast(this, wxChoice
)->m_hWnd
= m_hwndBuddy
;
584 wxChoiceBase::DoGetPosition(x
, y
);
586 wxConstCast(this, wxChoice
)->m_hWnd
= hWnd
;
589 #endif // wxUSE_CHOICE && __SMARTPHONE__ && __WXWINCE__