]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/wince/choicece.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/wince/choicece.cpp
3 // Purpose: wxChoice implementation for Smartphones
4 // Author: Wlodzimierz ABX Skiba
8 // Copyright: (c) Wlodzimierz Skiba
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
22 #pragma implementation "choicece.h"
25 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
33 #include "wx/choice.h"
36 #include "wx/spinbutt.h" // for wxSpinnerBestSize
39 #include "wx/msw/missing.h"
40 #include "wx/msw/winundef.h"
42 #if wxUSE_CHOICE && defined(__SMARTPHONE__)
44 #if wxUSE_EXTENDED_RTTI
47 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
50 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
52 #define IsVertical(wxStyle) ( (wxStyle & wxSP_HORIZONTAL) != wxSP_HORIZONTAL )
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 // the margin between the up-down control and its buddy (can be arbitrary,
59 // choose what you like - or may be decide during run-time depending on the
61 static const int MARGIN_BETWEEN
= 0;
63 // ============================================================================
65 // ============================================================================
67 wxArrayChoiceSpins
wxChoice::ms_allChoiceSpins
;
69 // ----------------------------------------------------------------------------
70 // wnd proc for the buddy text ctrl
71 // ----------------------------------------------------------------------------
73 LRESULT APIENTRY _EXPORT
wxBuddyChoiceWndProc(HWND hwnd
,
78 wxChoice
*spin
= (wxChoice
*)wxGetWindowUserData(hwnd
);
80 // forward some messages (the key and focus ones only so far) to
85 // if the focus comes from the spin control itself, don't set it
86 // back to it -- we don't want to go into an infinite loop
87 if ( (WXHWND
)wParam
== spin
->GetHWND() )
96 spin
->MSWWindowProc(message
, wParam
, lParam
);
98 // The control may have been deleted at this point, so check.
99 if ( !::IsWindow(hwnd
) || wxGetWindowUserData(hwnd
) != spin
)
104 // we want to get WXK_RETURN in order to generate the event for it
105 return DLGC_WANTCHARS
;
108 return ::CallWindowProc(CASTWNDPROC spin
->GetBuddyWndProc(),
109 hwnd
, message
, wParam
, lParam
);
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
116 bool wxChoice::Create(wxWindow
*parent
,
120 int n
, const wxString choices
[],
122 const wxValidator
& validator
,
123 const wxString
& name
)
125 return CreateAndInit(parent
, id
, pos
, size
, n
, choices
, style
,
129 bool wxChoice::CreateAndInit(wxWindow
*parent
,
133 int n
, const wxString choices
[],
135 const wxValidator
& validator
,
136 const wxString
& name
)
138 if ( !(style
& wxSP_VERTICAL
) )
139 style
|= wxSP_HORIZONTAL
;
141 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
142 style
|= wxBORDER_SIMPLE
;
144 style
|= wxSP_ARROW_KEYS
;
146 SetWindowStyle(style
);
149 WXDWORD msStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
151 wxSize
sizeText(size
), sizeBtn(size
);
152 sizeBtn
.x
= GetBestSpinerSize(IsVertical(style
)).x
;
156 if ( sizeText
.x
== wxDefaultCoord
)
158 // DEFAULT_ITEM_WIDTH is the default width for the text control
159 sizeText
.x
= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
+ sizeBtn
.x
;
162 sizeText
.x
-= sizeBtn
.x
+ MARGIN_BETWEEN
;
163 if ( sizeText
.x
<= 0 )
165 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
169 posBtn
.x
+= sizeText
.x
+ MARGIN_BETWEEN
;
171 // we must create the list control before the spin button for the purpose
172 // of the dialog navigation: if there is a static text just before the spin
173 // control, activating it by Alt-letter should give focus to the text
174 // control, not the spin and the dialog navigation code will give focus to
175 // the next control (at Windows level), not the one after it
177 // create the text window
179 m_hwndBuddy
= (WXHWND
)::CreateWindowEx
181 exStyle
, // sunken border
182 _T("LISTBOX"), // window class
183 NULL
, // no window title
184 msStyle
, // style (will be shown later)
185 pos
.x
, pos
.y
, // position
186 0, 0, // size (will be set later)
187 GetHwndOf(parent
), // parent
188 (HMENU
)-1, // control id
189 wxGetInstance(), // app instance
190 NULL
// unused client data
195 wxLogLastError(wxT("CreateWindow(buddy text window)"));
200 // initialize wxControl
201 if ( !CreateControl(parent
, id
, posBtn
, sizeBtn
, style
, validator
, name
) )
204 // now create the real HWND
205 WXDWORD spiner_style
= WS_VISIBLE
|
211 if ( !IsVertical(style
) )
212 spiner_style
|= UDS_HORZ
;
214 if ( style
& wxSP_WRAP
)
215 spiner_style
|= UDS_WRAP
;
217 if ( !MSWCreateControl(UPDOWN_CLASS
, spiner_style
, posBtn
, sizeBtn
, _T(""), 0) )
220 // subclass the text ctrl to be able to intercept some events
221 wxSetWindowUserData(GetBuddyHwnd(), this);
222 m_wndProcBuddy
= (WXFARPROC
)wxSetWindowProc(GetBuddyHwnd(),
223 wxBuddyChoiceWndProc
);
225 // set up fonts and colours (This is nomally done in MSWCreateControl)
228 SetFont(GetDefaultAttributes().font
);
230 // set the size of the text window - can do it only now, because we
231 // couldn't call DoGetBestSize() before as font wasn't set
232 if ( sizeText
.y
<= 0 )
235 wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont());
237 sizeText
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
242 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW
);
244 // associate the list window with the spin button
245 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)GetBuddyHwnd(), 0);
247 // do it after finishing with m_hwndBuddy creation to avoid generating
248 // initial wxEVT_COMMAND_TEXT_UPDATED message
249 ms_allChoiceSpins
.Add(this);
251 // initialize the controls contents
252 for ( int i
= 0; i
< n
; i
++ )
260 bool wxChoice::Create(wxWindow
*parent
,
264 const wxArrayString
& choices
,
266 const wxValidator
& validator
,
267 const wxString
& name
)
269 wxCArrayString
chs(choices
);
270 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
271 style
, validator
, name
);
274 WXDWORD
wxChoice::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
276 // we never have an external border
277 WXDWORD msStyle
= wxControl::MSWGetStyle
279 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
282 msStyle
|= WS_VISIBLE
;
284 // wxChoice-specific styles
285 msStyle
|= LBS_NOINTEGRALHEIGHT
;
286 if ( style
& wxCB_SORT
)
292 wxChoice::~wxChoice()
297 // ----------------------------------------------------------------------------
298 // adding/deleting items to/from the list
299 // ----------------------------------------------------------------------------
301 int wxChoice::DoAppend(const wxString
& item
)
303 int n
= (int)SendMessage(GetBuddyHwnd(), LB_ADDSTRING
, 0, (LPARAM
)item
.c_str());
307 wxLogLastError(wxT("SendMessage(LB_ADDSTRING)"));
313 int wxChoice::DoInsert(const wxString
& item
, int pos
)
315 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into choice"));
316 wxCHECK_MSG((pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index"));
318 int n
= (int)SendMessage(GetBuddyHwnd(), LB_INSERTSTRING
, pos
, (LPARAM
)item
.c_str());
321 wxLogLastError(wxT("SendMessage(LB_INSERTSTRING)"));
327 void wxChoice::Delete(int n
)
329 wxCHECK_RET( n
< GetCount(), wxT("invalid item index in wxChoice::Delete") );
331 if ( HasClientObjectData() )
333 delete GetClientObject(n
);
336 SendMessage(GetBuddyHwnd(), LB_DELETESTRING
, n
, 0);
339 void wxChoice::Clear()
343 SendMessage(GetBuddyHwnd(), LB_RESETCONTENT
, 0, 0);
346 void wxChoice::Free()
348 if ( HasClientObjectData() )
350 size_t count
= GetCount();
351 for ( size_t n
= 0; n
< count
; n
++ )
353 delete GetClientObject(n
);
358 // ----------------------------------------------------------------------------
360 // ----------------------------------------------------------------------------
362 int wxChoice::GetSelection() const
364 return (int)SendMessage(GetBuddyHwnd(), LB_GETCURSEL
, 0, 0);
367 void wxChoice::SetSelection(int n
)
369 SendMessage(GetBuddyHwnd(), LB_SETCURSEL
, n
, 0);
372 // ----------------------------------------------------------------------------
373 // string list functions
374 // ----------------------------------------------------------------------------
376 int wxChoice::GetCount() const
378 return (int)SendMessage(GetBuddyHwnd(), LB_GETCOUNT
, 0, 0);
381 int wxChoice::FindString(const wxString
& s
) const
383 int pos
= (int)SendMessage(GetBuddyHwnd(), LB_FINDSTRINGEXACT
,
384 (WPARAM
)-1, (LPARAM
)s
.c_str());
386 return pos
== LB_ERR
? wxNOT_FOUND
: pos
;
389 void wxChoice::SetString(int n
, const wxString
& s
)
391 wxCHECK_RET( n
>= 0 && n
< GetCount(),
392 wxT("invalid item index in wxChoice::SetString") );
394 // we have to delete and add back the string as there is no way to change a
397 // we need to preserve the client data
399 if ( m_clientDataItemsType
!= wxClientData_None
)
401 data
= DoGetItemClientData(n
);
403 else // no client data
408 ::SendMessage(GetBuddyHwnd(), LB_DELETESTRING
, n
, 0);
409 ::SendMessage(GetBuddyHwnd(), LB_INSERTSTRING
, n
, (LPARAM
)s
.c_str() );
413 DoSetItemClientData(n
, data
);
415 //else: it's already NULL by default
418 wxString
wxChoice::GetString(int n
) const
420 int len
= (int)::SendMessage(GetBuddyHwnd(), LB_GETTEXTLEN
, n
, 0);
423 if ( len
!= LB_ERR
&& len
> 0 )
430 (LPARAM
)(wxChar
*)wxStringBuffer(str
, len
)
433 wxLogLastError(wxT("SendMessage(LB_GETLBTEXT)"));
440 // ----------------------------------------------------------------------------
442 // ----------------------------------------------------------------------------
444 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
446 if ( ::SendMessage(GetHwnd(), LB_SETITEMDATA
,
447 n
, (LPARAM
)clientData
) == LB_ERR
)
449 wxLogLastError(wxT("LB_SETITEMDATA"));
453 void* wxChoice::DoGetItemClientData( int n
) const
455 LPARAM rc
= SendMessage(GetHwnd(), LB_GETITEMDATA
, n
, 0);
458 wxLogLastError(wxT("LB_GETITEMDATA"));
460 // unfortunately, there is no way to return an error code to the user
467 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
469 DoSetItemClientData(n
, clientData
);
472 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
474 return (wxClientData
*)DoGetItemClientData(n
);
477 // ----------------------------------------------------------------------------
479 // ----------------------------------------------------------------------------
481 wxSize
wxChoice::DoGetBestSize() const
483 wxSize sizeBtn
= GetBestSpinerSize(IsVertical(GetWindowStyle()));
484 sizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
487 wxGetCharSize(GetHWND(), NULL
, &y
, GetFont());
488 y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
);
490 // JACS: we should always use the height calculated
491 // from above, because otherwise we'll get a spin control
492 // that's too big. So never use the height calculated
493 // from wxSpinButton::DoGetBestSize().
495 // if ( sizeBtn.y < y )
497 // make the text tall enough
504 void wxChoice::DoMoveWindow(int x
, int y
, int width
, int height
)
506 int widthBtn
= GetBestSpinerSize(IsVertical(GetWindowStyle())).x
;
507 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
508 if ( widthText
<= 0 )
510 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
513 if ( !::MoveWindow(GetBuddyHwnd(), x
, y
, widthText
, height
, TRUE
) )
515 wxLogLastError(wxT("MoveWindow(buddy)"));
518 x
+= widthText
+ MARGIN_BETWEEN
;
519 if ( !::MoveWindow(GetHwnd(), x
, y
, widthBtn
, height
, TRUE
) )
521 wxLogLastError(wxT("MoveWindow"));
525 // get total size of the control
526 void wxChoice::DoGetSize(int *x
, int *y
) const
528 RECT spinrect
, textrect
, ctrlrect
;
529 GetWindowRect(GetHwnd(), &spinrect
);
530 GetWindowRect(GetBuddyHwnd(), &textrect
);
531 UnionRect(&ctrlrect
, &textrect
, &spinrect
);
534 *x
= ctrlrect
.right
- ctrlrect
.left
;
536 *y
= ctrlrect
.bottom
- ctrlrect
.top
;
539 void wxChoice::DoGetPosition(int *x
, int *y
) const
541 // hack: pretend that our HWND is the text control just for a moment
542 WXHWND hWnd
= GetHWND();
543 wxConstCast(this, wxChoice
)->m_hWnd
= m_hwndBuddy
;
545 wxChoiceBase::DoGetPosition(x
, y
);
547 wxConstCast(this, wxChoice
)->m_hWnd
= hWnd
;
550 #endif // wxUSE_CHOICE && __SMARTPHONE__