| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: choice.cpp |
| 3 | // Purpose: wxChoice |
| 4 | // Author: David Webster |
| 5 | // Modified by: |
| 6 | // Created: 10/13/99 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) David Webster |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // For compilers that support precompilation, includes "wx.h". |
| 13 | #include "wx/wxprec.h" |
| 14 | |
| 15 | #ifndef WX_PRECOMP |
| 16 | #include "wx/choice.h" |
| 17 | #include "wx/utils.h" |
| 18 | #include "wx/log.h" |
| 19 | #include "wx/settings.h" |
| 20 | #endif |
| 21 | |
| 22 | #include "wx/os2/private.h" |
| 23 | |
| 24 | IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl) |
| 25 | |
| 26 | bool wxChoice::Create( |
| 27 | wxWindow* pParent |
| 28 | , wxWindowID vId |
| 29 | , const wxPoint& rPos |
| 30 | , const wxSize& rSize |
| 31 | , int n |
| 32 | , const wxString asChoices[] |
| 33 | , long lStyle |
| 34 | , const wxValidator& rValidator |
| 35 | , const wxString& rsName |
| 36 | ) |
| 37 | { |
| 38 | long lSstyle; |
| 39 | |
| 40 | if (!CreateControl( pParent |
| 41 | ,vId |
| 42 | ,rPos |
| 43 | ,rSize |
| 44 | ,lStyle |
| 45 | ,rValidator |
| 46 | ,rsName |
| 47 | )) |
| 48 | return FALSE; |
| 49 | lSstyle = CBS_DROPDOWNLIST | |
| 50 | WS_TABSTOP | |
| 51 | WS_VISIBLE; |
| 52 | |
| 53 | if (lStyle & wxCLIP_SIBLINGS ) |
| 54 | lSstyle |= WS_CLIPSIBLINGS; |
| 55 | |
| 56 | wxASSERT_MSG( !(lStyle & wxCB_DROPDOWN) && |
| 57 | !(lStyle & wxCB_READONLY) && |
| 58 | !(lStyle & wxCB_SIMPLE), |
| 59 | wxT("this style flag is ignored by wxChoice, you " |
| 60 | "probably want to use a wxComboBox") ); |
| 61 | |
| 62 | if (!OS2CreateControl( wxT("COMBOBOX") |
| 63 | ,lSstyle |
| 64 | )) |
| 65 | return FALSE; |
| 66 | |
| 67 | // |
| 68 | // A choice/combobox normally has a white background (or other, depending |
| 69 | // on global settings) rather than inheriting the parent's background colour. |
| 70 | // |
| 71 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
| 72 | for (int i = 0; i < n; i++) |
| 73 | { |
| 74 | Append(asChoices[i]); |
| 75 | } |
| 76 | wxFont* pTextFont = new wxFont( 10 |
| 77 | ,wxMODERN |
| 78 | ,wxNORMAL |
| 79 | ,wxNORMAL |
| 80 | ); |
| 81 | SetFont(*pTextFont); |
| 82 | SetSize( rPos.x |
| 83 | ,rPos.y |
| 84 | ,rSize.x |
| 85 | ,rSize.y |
| 86 | ); |
| 87 | delete pTextFont; |
| 88 | return TRUE; |
| 89 | } // end of wxChoice::Create |
| 90 | |
| 91 | // ---------------------------------------------------------------------------- |
| 92 | // adding/deleting items to/from the list |
| 93 | // ---------------------------------------------------------------------------- |
| 94 | |
| 95 | int wxChoice::DoAppend( |
| 96 | const wxString& rsItem |
| 97 | ) |
| 98 | { |
| 99 | int nIndex; |
| 100 | SHORT nIndexType = 0; |
| 101 | |
| 102 | if (m_windowStyle & wxLB_SORT) |
| 103 | nIndexType = LIT_SORTASCENDING; |
| 104 | else |
| 105 | nIndexType = LIT_END; |
| 106 | nIndex = (int)::WinSendMsg( GetHwnd() |
| 107 | ,LM_INSERTITEM |
| 108 | ,(MPARAM)nIndexType |
| 109 | ,(MPARAM)rsItem.c_str() |
| 110 | ); |
| 111 | return nIndex; |
| 112 | } // end of wxChoice::DoAppend |
| 113 | |
| 114 | void wxChoice::Delete( |
| 115 | int n |
| 116 | ) |
| 117 | { |
| 118 | wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") ); |
| 119 | ::WinSendMsg(GetHwnd(), LM_DELETEITEM, (MPARAM)n, (MPARAM)0); |
| 120 | } // end of wxChoice::Delete |
| 121 | |
| 122 | void wxChoice::Clear() |
| 123 | { |
| 124 | Free(); |
| 125 | ::WinSendMsg(GetHwnd(), LM_DELETEALL, (MPARAM)0, (MPARAM)0); |
| 126 | } // end of wxChoice::Clear |
| 127 | |
| 128 | // ---------------------------------------------------------------------------- |
| 129 | // selection |
| 130 | // ---------------------------------------------------------------------------- |
| 131 | |
| 132 | int wxChoice::GetSelection() const |
| 133 | { |
| 134 | return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION, (MPARAM)LIT_FIRST, (MPARAM)0))); |
| 135 | } // end of wxChoice::GetSelection |
| 136 | |
| 137 | void wxChoice::SetSelection( |
| 138 | int n |
| 139 | ) |
| 140 | { |
| 141 | ::WinSendMsg( GetHwnd() |
| 142 | ,LM_SELECTITEM |
| 143 | ,(MPARAM)n |
| 144 | ,(MPARAM)TRUE |
| 145 | ); |
| 146 | } // end of wxChoice::SetSelection |
| 147 | |
| 148 | // ---------------------------------------------------------------------------- |
| 149 | // string list functions |
| 150 | // ---------------------------------------------------------------------------- |
| 151 | |
| 152 | int wxChoice::GetCount() const |
| 153 | { |
| 154 | return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMCOUNT, (MPARAM)0, (MPARAM)0))); |
| 155 | } // end of wxChoice::GetCount |
| 156 | |
| 157 | int wxChoice::FindString( |
| 158 | const wxString& rsStr |
| 159 | ) const |
| 160 | { |
| 161 | int nPos; |
| 162 | int nTextLength; |
| 163 | PSZ zStr; |
| 164 | int nItemCount; |
| 165 | |
| 166 | nItemCount = (int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMCOUNT, (MPARAM)0, (MPARAM)0)); |
| 167 | for (nPos = 0; nPos < nItemCount; nPos++) |
| 168 | { |
| 169 | nTextLength = (int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH, (MPARAM)nPos, (MPARAM)0)); |
| 170 | zStr = new char[nTextLength + 1]; |
| 171 | ::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXT, MPFROM2SHORT((SHORT)nPos, (SHORT)nTextLength), (MPARAM)zStr); |
| 172 | if (rsStr == (char*)zStr) |
| 173 | { |
| 174 | delete [] zStr; |
| 175 | break; |
| 176 | } |
| 177 | delete [] zStr; |
| 178 | } |
| 179 | return nPos; |
| 180 | } // end of wxChoice::FindString |
| 181 | |
| 182 | void wxChoice::SetString( |
| 183 | int n |
| 184 | , const wxString& rsStr |
| 185 | ) |
| 186 | { |
| 187 | SHORT nIndexType = 0; |
| 188 | void* pData; |
| 189 | |
| 190 | if ( m_clientDataItemsType != wxClientData_None ) |
| 191 | { |
| 192 | pData = DoGetItemClientData(n); |
| 193 | } |
| 194 | else // no client data |
| 195 | { |
| 196 | pData = NULL; |
| 197 | } |
| 198 | |
| 199 | ::WinSendMsg(GetHwnd(), LM_DELETEITEM, (MPARAM)n, 0); |
| 200 | |
| 201 | if (m_windowStyle & wxLB_SORT) |
| 202 | nIndexType = LIT_SORTASCENDING; |
| 203 | else |
| 204 | nIndexType = LIT_END; |
| 205 | ::WinSendMsg( GetHwnd() |
| 206 | ,LM_INSERTITEM |
| 207 | ,(MPARAM)nIndexType |
| 208 | ,(MPARAM)rsStr.c_str() |
| 209 | ); |
| 210 | |
| 211 | if (pData) |
| 212 | { |
| 213 | DoSetItemClientData( n |
| 214 | ,pData |
| 215 | ); |
| 216 | } |
| 217 | } // end of wxChoice::SetString |
| 218 | |
| 219 | wxString wxChoice::GetString( |
| 220 | int n |
| 221 | ) const |
| 222 | { |
| 223 | size_t nLen = 0; |
| 224 | wxString sStr = ""; |
| 225 | char* zBuf; |
| 226 | |
| 227 | nLen = (size_t)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH, (MPARAM)n, (MPARAM)0)); |
| 228 | if (nLen != LIT_ERROR && nLen > 0) |
| 229 | { |
| 230 | zBuf = new char[nLen + 1]; |
| 231 | ::WinSendMsg( GetHwnd() |
| 232 | ,LM_QUERYITEMTEXT |
| 233 | ,MPFROM2SHORT((SHORT)n, (SHORT)nLen) |
| 234 | ,(MPARAM)zBuf |
| 235 | ); |
| 236 | sStr = zBuf; |
| 237 | delete [] zBuf; |
| 238 | } |
| 239 | return sStr; |
| 240 | } // end of wxChoice::GetString |
| 241 | |
| 242 | // ---------------------------------------------------------------------------- |
| 243 | // client data |
| 244 | // ---------------------------------------------------------------------------- |
| 245 | |
| 246 | void wxChoice::DoSetItemClientData( |
| 247 | int n |
| 248 | , void* pClientData |
| 249 | ) |
| 250 | { |
| 251 | ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE, (MPARAM)n, MPFROMP(pClientData)); |
| 252 | } // end of wxChoice::DoSetItemClientData |
| 253 | |
| 254 | void* wxChoice::DoGetItemClientData( int n ) const |
| 255 | { |
| 256 | MRESULT rc = 0L; |
| 257 | |
| 258 | rc = ::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE, (MPARAM)n, (MPARAM)0); |
| 259 | return((void*)rc); |
| 260 | } // end of wxChoice::DoSetItemClientData |
| 261 | |
| 262 | void wxChoice::DoSetItemClientObject( |
| 263 | int n |
| 264 | , wxClientData* pClientData |
| 265 | ) |
| 266 | { |
| 267 | DoSetItemClientData( n |
| 268 | ,pClientData |
| 269 | ); |
| 270 | } // end of wxChoice::DoSetItemClientObject |
| 271 | |
| 272 | wxClientData* wxChoice::DoGetItemClientObject( |
| 273 | int n |
| 274 | ) const |
| 275 | { |
| 276 | return (wxClientData *)DoGetItemClientData(n); |
| 277 | } // end of wxChoice::DoGetItemClientObject |
| 278 | |
| 279 | // ---------------------------------------------------------------------------- |
| 280 | // wxOS2 specific helpers |
| 281 | // ---------------------------------------------------------------------------- |
| 282 | |
| 283 | void wxChoice::DoSetSize( |
| 284 | int nX |
| 285 | , int nY |
| 286 | , int nWidth |
| 287 | , int nHeight |
| 288 | , int nSizeFlags |
| 289 | ) |
| 290 | { |
| 291 | // |
| 292 | // Ignore height parameter because height doesn't mean 'initially |
| 293 | // displayed' height, it refers to the drop-down menu as well. The |
| 294 | // wxWindows interpretation is different; also, getting the size returns |
| 295 | // the _displayed_ size (NOT the drop down menu size) so |
| 296 | // setting-getting-setting size would not work. |
| 297 | // |
| 298 | wxControl::DoSetSize( nX |
| 299 | ,nY |
| 300 | ,nWidth |
| 301 | ,-1 |
| 302 | ,nSizeFlags |
| 303 | ); |
| 304 | } // end of wxChoice::DoSetSize |
| 305 | |
| 306 | wxSize wxChoice::DoGetBestSize() const |
| 307 | { |
| 308 | // |
| 309 | // Find the widest string |
| 310 | // |
| 311 | int nLineWidth; |
| 312 | int nChoiceWidth = 0; |
| 313 | int nItems = GetCount(); |
| 314 | int nCx; |
| 315 | int nCy; |
| 316 | |
| 317 | for (int i = 0; i < nItems; i++) |
| 318 | { |
| 319 | wxString sStr(GetString(i)); |
| 320 | |
| 321 | GetTextExtent( sStr |
| 322 | ,&nLineWidth |
| 323 | ,NULL |
| 324 | ); |
| 325 | if (nLineWidth > nChoiceWidth) |
| 326 | nChoiceWidth = nLineWidth; |
| 327 | } |
| 328 | |
| 329 | // |
| 330 | // Give it some reasonable default value if there are no strings in the |
| 331 | // list |
| 332 | // |
| 333 | if (nChoiceWidth == 0L) |
| 334 | nChoiceWidth = 100L; |
| 335 | |
| 336 | // |
| 337 | // The combobox should be larger than the widest string |
| 338 | // |
| 339 | wxGetCharSize( GetHWND() |
| 340 | ,&nCx |
| 341 | ,&nCy |
| 342 | ,(wxFont*)&GetFont() |
| 343 | ); |
| 344 | nChoiceWidth += 5 * nCx; |
| 345 | |
| 346 | // |
| 347 | // Choice drop-down list depends on number of items (limited to 10) |
| 348 | // |
| 349 | size_t nStrings = nItems == 0 ? 10 : wxMin(10, nItems) + 1; |
| 350 | int nChoiceHeight = EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy) * nStrings; |
| 351 | |
| 352 | return wxSize( nChoiceWidth |
| 353 | ,nChoiceHeight |
| 354 | ); |
| 355 | } // end of wxChoice::DoGetBestSize |
| 356 | |
| 357 | MRESULT wxChoice::OS2WindowProc( |
| 358 | WXUINT uMsg |
| 359 | , WXWPARAM wParam |
| 360 | , WXLPARAM lParam |
| 361 | ) |
| 362 | { |
| 363 | return wxWindow::OS2WindowProc( uMsg |
| 364 | ,wParam |
| 365 | ,lParam |
| 366 | ); |
| 367 | } // end of wxChoice::OS2WindowProc |
| 368 | |
| 369 | bool wxChoice::OS2Command( |
| 370 | WXUINT uParam |
| 371 | , WXWORD WXUNUSED(wId) |
| 372 | ) |
| 373 | { |
| 374 | if (uParam != LN_SELECT) |
| 375 | { |
| 376 | // |
| 377 | // "selection changed" is the only event we're after |
| 378 | // |
| 379 | return FALSE; |
| 380 | } |
| 381 | int n = GetSelection(); |
| 382 | |
| 383 | if (n > -1) |
| 384 | { |
| 385 | wxCommandEvent vEvent( wxEVT_COMMAND_CHOICE_SELECTED |
| 386 | ,m_windowId |
| 387 | ); |
| 388 | |
| 389 | vEvent.SetInt(n); |
| 390 | vEvent.SetEventObject(this); |
| 391 | vEvent.SetString((char*)GetStringSelection().c_str()); |
| 392 | if (HasClientObjectData()) |
| 393 | vEvent.SetClientObject(GetClientObject(n)); |
| 394 | else if (HasClientUntypedData()) |
| 395 | vEvent.SetClientData(GetClientData(n)); |
| 396 | ProcessCommand(vEvent); |
| 397 | } |
| 398 | return TRUE; |
| 399 | } // end of wxChoice::OS2Command |
| 400 | |
| 401 | void wxChoice::Free() |
| 402 | { |
| 403 | if (HasClientObjectData()) |
| 404 | { |
| 405 | size_t nCount = GetCount(); |
| 406 | |
| 407 | for (size_t n = 0; n < nCount; n++) |
| 408 | { |
| 409 | delete GetClientObject(n); |
| 410 | } |
| 411 | } |
| 412 | } // end of wxhoice::Free |