]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/choice.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/choice.cpp
4 // Author: David Webster
7 // Copyright: (c) David Webster
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
16 #include "wx/choice.h"
21 #include "wx/settings.h"
24 #include "wx/os2/private.h"
26 bool wxChoice::Create(
31 , const wxArrayString
& asChoices
33 , const wxValidator
& rValidator
34 , const wxString
& rsName
37 wxCArrayString
chs(asChoices
);
39 return Create(pParent
, vId
, rPos
, rSize
, chs
.GetCount(), chs
.GetStrings(),
40 lStyle
, rValidator
, rsName
);
43 bool wxChoice::Create(
49 , const wxString asChoices
[]
51 , const wxValidator
& rValidator
52 , const wxString
& rsName
57 if (!CreateControl( pParent
66 lSstyle
= CBS_DROPDOWNLIST
|
70 // clipping siblings does not yet work
71 // if (lStyle & wxCLIP_SIBLINGS )
72 // lSstyle |= WS_CLIPSIBLINGS;
74 wxASSERT_MSG( !(lStyle
& wxCB_DROPDOWN
) &&
75 !(lStyle
& wxCB_READONLY
) &&
76 !(lStyle
& wxCB_SIMPLE
),
77 wxT("this style flag is ignored by wxChoice, you "
78 "probably want to use a wxComboBox") );
80 if (!OS2CreateControl( wxT("COMBOBOX")
86 // A choice/combobox normally has a white background (or other, depending
87 // on global settings) rather than inheriting the parent's background colour.
89 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
91 // initialize the controls contents
92 for (int i
= 0; i
< n
; i
++)
102 // Set height to use with sizers i.e. without the dropdown listbox
103 wxFont vFont
= GetFont();
105 wxGetCharSize( GetHWND(), NULL
, &nEditHeight
, &vFont
);
106 nEditHeight
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nEditHeight
);
107 SetInitialSize(wxSize(-1,nEditHeight
+4)); // +2x2 for the border
110 } // end of wxChoice::Create
112 wxChoice::~wxChoice()
117 // ----------------------------------------------------------------------------
118 // adding/deleting items to/from the list
119 // ----------------------------------------------------------------------------
121 int wxChoice::DoInsertItems(const wxArrayStringsAdapter
& items
124 , wxClientDataType type
127 int nIndex
= wxNOT_FOUND
;
129 bool incrementPos
= false;
131 nIndexType
= LIT_SORTASCENDING
;
132 else if (pos
== GetCount())
133 nIndexType
= LIT_END
;
140 const unsigned int count
= items
.GetCount();
141 for( unsigned int i
= 0; i
< count
; ++i
)
143 nIndex
= (int)::WinSendMsg( GetHwnd()
146 ,(MPARAM
)items
[i
].wx_str()
150 nIndex
= wxNOT_FOUND
;
153 AssignNewItemClientData(nIndex
, clientData
, i
, type
);
159 } // end of wxChoice::DoInsertAppendItemsWithData
161 void wxChoice::DoDeleteOneItem(unsigned int n
)
163 wxCHECK_RET( IsValid(n
), wxT("invalid item index in wxChoice::Delete") );
165 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)n
, (MPARAM
)0);
166 } // end of wxChoice::Delete
168 void wxChoice::DoClear()
170 ::WinSendMsg(GetHwnd(), LM_DELETEALL
, (MPARAM
)0, (MPARAM
)0);
171 } // end of wxChoice::Clear
173 // ----------------------------------------------------------------------------
175 // ----------------------------------------------------------------------------
177 int wxChoice::GetSelection() const
179 return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)LIT_FIRST
, (MPARAM
)0)));
180 } // end of wxChoice::GetSelection
182 void wxChoice::SetSelection(
186 ::WinSendMsg( GetHwnd()
191 } // end of wxChoice::SetSelection
193 // ----------------------------------------------------------------------------
194 // string list functions
195 // ----------------------------------------------------------------------------
197 unsigned int wxChoice::GetCount() const
199 return((unsigned int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMCOUNT
, (MPARAM
)0, (MPARAM
)0)));
200 } // end of wxChoice::GetCount
202 void wxChoice::SetString(unsigned int n
, const wxString
& rsStr
)
207 if ( HasClientData() )
209 pData
= DoGetItemClientData(n
);
211 else // no client data
216 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)n
, 0);
218 if (m_windowStyle
& wxCB_SORT
)
219 nIndexType
= LIT_SORTASCENDING
;
221 nIndexType
= LIT_END
;
222 ::WinSendMsg( GetHwnd()
225 ,(MPARAM
)rsStr
.wx_str()
230 DoSetItemClientData(n
, pData
);
232 } // end of wxChoice::SetString
234 wxString
wxChoice::GetString(unsigned int n
) const
237 wxString sStr
= wxEmptyString
;
240 nLen
= (size_t)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)n
, (MPARAM
)0));
241 if (nLen
!= LIT_ERROR
&& nLen
> 0)
243 zBuf
= new wxChar
[++nLen
];
244 ::WinSendMsg( GetHwnd()
246 ,MPFROM2SHORT((SHORT
)n
, (SHORT
)nLen
)
253 } // end of wxChoice::GetString
255 // ----------------------------------------------------------------------------
257 // ----------------------------------------------------------------------------
259 void wxChoice::DoSetItemClientData(unsigned int n
, void* pClientData
)
261 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, (MPARAM
)n
, MPFROMP(pClientData
));
262 } // end of wxChoice::DoSetItemClientData
264 void* wxChoice::DoGetItemClientData(unsigned int n
) const
266 MRESULT rc
= ::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE
, (MPARAM
)n
, (MPARAM
)0);
268 } // end of wxChoice::DoGetItemClientData
270 // ----------------------------------------------------------------------------
271 // wxOS2 specific helpers
272 // ----------------------------------------------------------------------------
274 void wxChoice::DoSetSize(int nX
,
277 int WXUNUSED(nHeight
),
281 // Ignore height parameter because height doesn't mean 'initially
282 // displayed' height, it refers to the drop-down menu as well. The
283 // wxWidgets interpretation is different; also, getting the size returns
284 // the _displayed_ size (NOT the drop down menu size) so
285 // setting-getting-setting size would not work.
287 wxControl::DoSetSize( nX
293 } // end of wxChoice::DoSetSize
295 wxSize
wxChoice::DoGetBestSize() const
298 // Find the widest string
301 int nChoiceWidth
= 0;
304 wxFont vFont
= (wxFont
)GetFont();
306 const unsigned int nItems
= GetCount();
308 for (unsigned int i
= 0; i
< nItems
; i
++)
310 wxString
sStr(GetString(i
));
311 GetTextExtent( sStr
, &nLineWidth
, NULL
);
312 if (nLineWidth
> nChoiceWidth
)
313 nChoiceWidth
= nLineWidth
;
317 // Give it some reasonable default value if there are no strings in the
320 if (nChoiceWidth
== 0L)
324 // The combobox should be larger than the widest string
326 wxGetCharSize( GetHWND(), &nCx
, &nCy
, &vFont
);
327 nChoiceWidth
+= 5 * nCx
;
330 // Choice drop-down list depends on number of items (limited to 10)
332 size_t nStrings
= nItems
== 0 ? 10 : wxMin(10, nItems
) + 1;
333 int nChoiceHeight
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy
) * nStrings
;
335 return wxSize(nChoiceWidth
, nChoiceHeight
);
336 } // end of wxChoice::DoGetBestSize
338 MRESULT
wxChoice::OS2WindowProc(
344 return wxWindow::OS2WindowProc( uMsg
348 } // end of wxChoice::OS2WindowProc
350 bool wxChoice::OS2Command(
352 , WXWORD
WXUNUSED(wId
)
355 if (uParam
!= LN_SELECT
)
358 // "selection changed" is the only event we're after
362 int n
= GetSelection();
366 wxCommandEvent
vEvent( wxEVT_CHOICE
371 vEvent
.SetEventObject(this);
372 vEvent
.SetString(GetStringSelection());
373 if (HasClientObjectData())
374 vEvent
.SetClientObject(GetClientObject(n
));
375 else if (HasClientUntypedData())
376 vEvent
.SetClientData(GetClientData(n
));
377 ProcessCommand(vEvent
);
380 } // end of wxChoice::OS2Command
382 #endif // wxUSE_CHOICE