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__)
29 #include "wx/choice.h"
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
35 #include "wx/spinbutt.h" // for wxSpinnerBestSize
37 #if wxUSE_EXTENDED_RTTI
40 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
43 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
45 #define IsVertical(wxStyle) ( (wxStyle & wxSP_HORIZONTAL) != wxSP_HORIZONTAL )
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
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
54 static const int MARGIN_BETWEEN
= 0;
56 // ============================================================================
58 // ============================================================================
60 wxArrayChoiceSpins
wxChoice::ms_allChoiceSpins
;
62 // ----------------------------------------------------------------------------
63 // wnd proc for the buddy text ctrl
64 // ----------------------------------------------------------------------------
66 LRESULT APIENTRY _EXPORT
wxBuddyChoiceWndProc(HWND hwnd
,
71 wxChoice
*spin
= (wxChoice
*)wxGetWindowUserData(hwnd
);
73 // forward some messages (the key and focus ones only so far) to
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() )
89 spin
->MSWWindowProc(message
, wParam
, lParam
);
91 // The control may have been deleted at this point, so check.
92 if ( !::IsWindow(hwnd
) || wxGetWindowUserData(hwnd
) != spin
)
97 // we want to get WXK_RETURN in order to generate the event for it
98 return DLGC_WANTCHARS
;
101 return ::CallWindowProc(CASTWNDPROC spin
->GetBuddyWndProc(),
102 hwnd
, message
, wParam
, lParam
);
105 wxChoice
*wxChoice::GetChoiceForListBox(WXHWND hwndBuddy
)
107 wxChoice
*choice
= (wxChoice
*)wxGetWindowUserData((HWND
)hwndBuddy
);
109 int i
= ms_allChoiceSpins
.Index(choice
);
111 if ( i
== wxNOT_FOUND
)
115 wxASSERT_MSG( choice
->m_hwndBuddy
== hwndBuddy
,
116 _T("wxChoice has incorrect buddy HWND!") );
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
125 bool wxChoice::Create(wxWindow
*parent
,
129 int n
, const wxString choices
[],
131 const wxValidator
& validator
,
132 const wxString
& name
)
134 return CreateAndInit(parent
, id
, pos
, size
, n
, choices
, style
,
138 bool wxChoice::CreateAndInit(wxWindow
*parent
,
142 int n
, const wxString choices
[],
144 const wxValidator
& validator
,
145 const wxString
& name
)
147 if ( !(style
& wxSP_VERTICAL
) )
148 style
|= wxSP_HORIZONTAL
;
150 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
151 style
|= wxBORDER_SIMPLE
;
153 style
|= wxSP_ARROW_KEYS
;
155 SetWindowStyle(style
);
158 WXDWORD msStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
160 wxSize
sizeText(size
), sizeBtn(size
);
161 sizeBtn
.x
= GetBestSpinnerSize(IsVertical(style
)).x
;
163 if ( sizeText
.x
== wxDefaultCoord
)
165 // DEFAULT_ITEM_WIDTH is the default width for the text control
166 sizeText
.x
= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
+ sizeBtn
.x
;
169 sizeText
.x
-= sizeBtn
.x
+ MARGIN_BETWEEN
;
170 if ( sizeText
.x
<= 0 )
172 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
176 posBtn
.x
+= sizeText
.x
+ MARGIN_BETWEEN
;
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
184 // create the text window
186 m_hwndBuddy
= (WXHWND
)::CreateWindowEx
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
202 wxLogLastError(wxT("CreateWindow(buddy text window)"));
207 // initialize wxControl
208 if ( !CreateControl(parent
, id
, posBtn
, sizeBtn
, style
, validator
, name
) )
211 // now create the real HWND
212 WXDWORD spiner_style
= WS_VISIBLE
|
218 if ( !IsVertical(style
) )
219 spiner_style
|= UDS_HORZ
;
221 if ( style
& wxSP_WRAP
)
222 spiner_style
|= UDS_WRAP
;
224 if ( !MSWCreateControl(UPDOWN_CLASS
, spiner_style
, posBtn
, sizeBtn
, wxEmptyString
, 0) )
227 // subclass the text ctrl to be able to intercept some events
228 wxSetWindowUserData(GetBuddyHwnd(), this);
229 m_wndProcBuddy
= (WXFARPROC
)wxSetWindowProc(GetBuddyHwnd(),
230 wxBuddyChoiceWndProc
);
232 // set up fonts and colours (This is nomally done in MSWCreateControl)
235 SetFont(GetDefaultAttributes().font
);
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 )
242 wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont());
244 sizeText
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
247 SetInitialSize(size
);
249 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW
);
251 // associate the list window with the spin button
252 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)GetBuddyHwnd(), 0);
254 // do it after finishing with m_hwndBuddy creation to avoid generating
255 // initial wxEVT_COMMAND_TEXT_UPDATED message
256 ms_allChoiceSpins
.Add(this);
258 // initialize the controls contents
259 for ( int i
= 0; i
< n
; i
++ )
267 bool wxChoice::Create(wxWindow
*parent
,
271 const wxArrayString
& choices
,
273 const wxValidator
& validator
,
274 const wxString
& name
)
276 wxCArrayString
chs(choices
);
277 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
278 style
, validator
, name
);
281 WXDWORD
wxChoice::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
283 // we never have an external border
284 WXDWORD msStyle
= wxControl::MSWGetStyle
286 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
289 msStyle
|= WS_VISIBLE
;
291 // wxChoice-specific styles
292 msStyle
|= LBS_NOINTEGRALHEIGHT
;
293 if ( style
& wxCB_SORT
)
296 msStyle
|= LBS_NOTIFY
;
301 bool wxChoice::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
303 if ( param
!= LBN_SELCHANGE
)
305 // "selection changed" is the only event we're after
309 int n
= GetSelection();
312 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
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
);
326 wxChoice::~wxChoice()
331 // ----------------------------------------------------------------------------
332 // adding/deleting items to/from the list
333 // ----------------------------------------------------------------------------
335 int wxChoice::DoInsertItems(const wxArrayStringsAdapter
& items
,
338 wxClientDataType type
)
340 MSWAllocStorage(items
, LB_INITSTORAGE
);
342 const bool append
= pos
== GetCount();
343 const unsigned msg
= append
? LB_ADDSTRING
: LB_INSERTSTRING
;
349 const unsigned int numItems
= items
.GetCount();
350 for ( unsigned int i
= 0; i
< numItems
; ++i
)
352 n
= MSWInsertOrAppendItem(pos
, items
[i
], msg
);
356 AssignNewItemClientData(n
, clientData
, i
, type
);
362 void wxChoice::DoDeleteOneItem(unsigned int n
)
364 wxCHECK_RET( IsValid(n
), wxT("invalid item index in wxChoice::Delete") );
366 ::SendMessage(GetBuddyHwnd(), LB_DELETESTRING
, n
, 0);
369 void wxChoice::DoClear()
371 ::SendMessage(GetBuddyHwnd(), LB_RESETCONTENT
, 0, 0);
374 // ----------------------------------------------------------------------------
376 // ----------------------------------------------------------------------------
378 int wxChoice::GetSelection() const
380 return (int)::SendMessage(GetBuddyHwnd(), LB_GETCURSEL
, 0, 0);
383 void wxChoice::SetSelection(int n
)
385 ::SendMessage(GetBuddyHwnd(), LB_SETCURSEL
, n
, 0);
388 // ----------------------------------------------------------------------------
389 // string list functions
390 // ----------------------------------------------------------------------------
392 unsigned int wxChoice::GetCount() const
394 return (unsigned int)::SendMessage(GetBuddyHwnd(), LB_GETCOUNT
, 0, 0);
397 int wxChoice::FindString(const wxString
& s
, bool bCase
) const
399 // back to base class search for not native search type
401 return wxItemContainerImmutable::FindString( s
, bCase
);
403 int pos
= (int)::SendMessage(GetBuddyHwnd(), LB_FINDSTRINGEXACT
,
404 (WPARAM
)-1, (LPARAM
)s
.c_str());
406 return pos
== LB_ERR
? wxNOT_FOUND
: pos
;
409 void wxChoice::SetString(unsigned int n
, const wxString
& s
)
411 wxCHECK_RET( IsValid(n
),
412 wxT("invalid item index in wxChoice::SetString") );
414 // we have to delete and add back the string as there is no way to change a
417 // we need to preserve the client data
419 if ( m_clientDataItemsType
!= wxClientData_None
)
421 data
= DoGetItemClientData(n
);
423 else // no client data
428 ::SendMessage(GetBuddyHwnd(), LB_DELETESTRING
, n
, 0);
429 ::SendMessage(GetBuddyHwnd(), LB_INSERTSTRING
, n
, (LPARAM
)s
.c_str() );
433 DoSetItemClientData(n
, data
);
435 //else: it's already NULL by default
438 wxString
wxChoice::GetString(unsigned int n
) const
440 int len
= (int)::SendMessage(GetBuddyHwnd(), LB_GETTEXTLEN
, n
, 0);
443 if ( len
!= LB_ERR
&& len
> 0 )
450 (LPARAM
)(wxChar
*)wxStringBuffer(str
, len
)
453 wxLogLastError(wxT("SendMessage(LB_GETLBTEXT)"));
460 // ----------------------------------------------------------------------------
462 // ----------------------------------------------------------------------------
464 void wxChoice::DoSetItemClientData(unsigned int n
, void* clientData
)
466 if ( ::SendMessage(GetHwnd(), LB_SETITEMDATA
,
467 n
, (LPARAM
)clientData
) == LB_ERR
)
469 wxLogLastError(wxT("LB_SETITEMDATA"));
473 void* wxChoice::DoGetItemClientData(unsigned int n
) const
475 LPARAM rc
= ::SendMessage(GetHwnd(), LB_GETITEMDATA
, n
, 0);
478 wxLogLastError(wxT("LB_GETITEMDATA"));
480 // unfortunately, there is no way to return an error code to the user
487 // ----------------------------------------------------------------------------
489 // ----------------------------------------------------------------------------
491 wxSize
wxChoice::DoGetBestSize() const
493 wxSize sizeBtn
= GetBestSpinnerSize(IsVertical(GetWindowStyle()));
494 sizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
497 wxGetCharSize(GetHWND(), NULL
, &y
, GetFont());
498 y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
);
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().
505 // if ( sizeBtn.y < y )
507 // make the text tall enough
514 void wxChoice::DoMoveWindow(int x
, int y
, int width
, int height
)
516 int widthBtn
= GetBestSpinnerSize(IsVertical(GetWindowStyle())).x
;
517 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
518 if ( widthText
<= 0 )
520 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
523 if ( !::MoveWindow(GetBuddyHwnd(), x
, y
, widthText
, height
, TRUE
) )
525 wxLogLastError(wxT("MoveWindow(buddy)"));
528 x
+= widthText
+ MARGIN_BETWEEN
;
529 if ( !::MoveWindow(GetHwnd(), x
, y
, widthBtn
, height
, TRUE
) )
531 wxLogLastError(wxT("MoveWindow"));
535 // get total size of the control
536 void wxChoice::DoGetSize(int *x
, int *y
) const
538 RECT spinrect
, textrect
, ctrlrect
;
539 GetWindowRect(GetHwnd(), &spinrect
);
540 GetWindowRect(GetBuddyHwnd(), &textrect
);
541 UnionRect(&ctrlrect
, &textrect
, &spinrect
);
544 *x
= ctrlrect
.right
- ctrlrect
.left
;
546 *y
= ctrlrect
.bottom
- ctrlrect
.top
;
549 void wxChoice::DoGetPosition(int *x
, int *y
) const
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
;
555 wxChoiceBase::DoGetPosition(x
, y
);
557 wxConstCast(this, wxChoice
)->m_hWnd
= hWnd
;
560 #endif // wxUSE_CHOICE && __SMARTPHONE__ && __WXWINCE__