1 ///////////////////////////////////////////////////////////////////////////// 
   4 // Author:      David Webster 
   8 // Copyright:   (c) David Webster 
   9 // Licence:     wxWindows licence 
  10 ///////////////////////////////////////////////////////////////////////////// 
  12 // For compilers that support precompilation, includes "wx.h". 
  13 #include "wx/wxprec.h" 
  16     #include "wx/choice.h" 
  19     #include "wx/settings.h" 
  22 #include "wx/os2/private.h" 
  24 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
) 
  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     if (lStyle 
& wxCLIP_SIBLINGS 
) 
  71         lSstyle 
|= WS_CLIPSIBLINGS
; 
  73     wxASSERT_MSG( !(lStyle 
& wxCB_DROPDOWN
) && 
  74                   !(lStyle 
& wxCB_READONLY
) && 
  75                   !(lStyle 
& wxCB_SIMPLE
), 
  76                   wxT("this style flag is ignored by wxChoice, you " 
  77                      "probably want to use a wxComboBox") ); 
  79     if (!OS2CreateControl( wxT("COMBOBOX") 
  85     // A choice/combobox normally has a white background (or other, depending 
  86     // on global settings) rather than inheriting the parent's background colour. 
  88     SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
)); 
  89     for (int i 
= 0; i 
< n
; i
++) 
  93     wxFont
*                          pTextFont 
= new wxFont( 10 
 106 } // end of wxChoice::Create 
 108 // ---------------------------------------------------------------------------- 
 109 // adding/deleting items to/from the list 
 110 // ---------------------------------------------------------------------------- 
 112 int wxChoice::DoAppend( 
 113   const wxString
&                   rsItem
 
 119     if (m_windowStyle 
& wxLB_SORT
) 
 120         nIndexType 
= LIT_SORTASCENDING
; 
 122         nIndexType 
= LIT_END
; 
 123     nIndex 
= (int)::WinSendMsg( GetHwnd() 
 126                                ,(MPARAM
)rsItem
.c_str() 
 129 } // end of wxChoice::DoAppend 
 131 int wxChoice::DoInsert( 
 132   const wxString
&                   rsItem
, 
 136     wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list")); 
 137     wxCHECK_MSG((pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index")); 
 139     if (pos 
== GetCount()) 
 140         return DoAppend(rsItem
); 
 145     if (m_windowStyle 
& wxLB_SORT
) 
 146         nIndexType 
= LIT_SORTASCENDING
; 
 149     nIndex 
= (int)::WinSendMsg( GetHwnd() 
 152                                ,(MPARAM
)rsItem
.c_str() 
 155 } // end of wxChoice::DoInsert 
 157 void wxChoice::Delete( 
 161     wxCHECK_RET( n 
< GetCount(), wxT("invalid item index in wxChoice::Delete") ); 
 162     ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)n
, (MPARAM
)0); 
 163 } // end of wxChoice::Delete 
 165 void wxChoice::Clear() 
 168     ::WinSendMsg(GetHwnd(), LM_DELETEALL
, (MPARAM
)0, (MPARAM
)0); 
 169 } // end of wxChoice::Clear 
 171 // ---------------------------------------------------------------------------- 
 173 // ---------------------------------------------------------------------------- 
 175 int wxChoice::GetSelection() const 
 177     return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)LIT_FIRST
, (MPARAM
)0))); 
 178 } // end of wxChoice::GetSelection 
 180 void wxChoice::SetSelection( 
 184     ::WinSendMsg( GetHwnd() 
 189 } // end of wxChoice::SetSelection 
 191 // ---------------------------------------------------------------------------- 
 192 // string list functions 
 193 // ---------------------------------------------------------------------------- 
 195 int wxChoice::GetCount() const 
 197     return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMCOUNT
, (MPARAM
)0, (MPARAM
)0))); 
 198 } // end of wxChoice::GetCount 
 200 int wxChoice::FindString( 
 201   const wxString
&                   rsStr
 
 209     nItemCount 
= (int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMCOUNT
, (MPARAM
)0, (MPARAM
)0)); 
 210     for (nPos 
= 0; nPos 
< nItemCount
; nPos
++) 
 212         nTextLength 
= (int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)nPos
, (MPARAM
)0)); 
 213         zStr 
= new char[nTextLength 
+ 1]; 
 214         ::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXT
, MPFROM2SHORT((SHORT
)nPos
, (SHORT
)nTextLength
), (MPARAM
)zStr
); 
 215         if (rsStr 
== (char*)zStr
) 
 223 } // end of wxChoice::FindString 
 225 void wxChoice::SetString( 
 227 , const wxString
&                   rsStr
 
 233     if ( m_clientDataItemsType 
!= wxClientData_None 
) 
 235         pData 
= DoGetItemClientData(n
); 
 237     else // no client data 
 242     ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)n
, 0); 
 244     if (m_windowStyle 
& wxLB_SORT
) 
 245         nIndexType 
= LIT_SORTASCENDING
; 
 247         nIndexType 
= LIT_END
; 
 248     ::WinSendMsg( GetHwnd() 
 251                  ,(MPARAM
)rsStr
.c_str() 
 256         DoSetItemClientData( n
 
 260 } // end of wxChoice::SetString 
 262 wxString 
wxChoice::GetString( 
 270     nLen 
= (size_t)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)n
, (MPARAM
)0)); 
 271     if (nLen 
!= LIT_ERROR 
&& nLen 
> 0) 
 273         zBuf 
= new char[nLen 
+ 1]; 
 274         ::WinSendMsg( GetHwnd() 
 276                      ,MPFROM2SHORT((SHORT
)n
, (SHORT
)nLen
) 
 283 } // end of wxChoice::GetString 
 285 // ---------------------------------------------------------------------------- 
 287 // ---------------------------------------------------------------------------- 
 289 void wxChoice::DoSetItemClientData( 
 294     ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, (MPARAM
)n
, MPFROMP(pClientData
)); 
 295 } // end of wxChoice::DoSetItemClientData 
 297 void* wxChoice::DoGetItemClientData( int n 
) const 
 301     rc 
= ::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE
, (MPARAM
)n
, (MPARAM
)0); 
 303 } // end of wxChoice::DoSetItemClientData 
 305 void wxChoice::DoSetItemClientObject( 
 307 , wxClientData
*                     pClientData
 
 310     DoSetItemClientData( n
 
 313 } // end of wxChoice::DoSetItemClientObject 
 315 wxClientData
* wxChoice::DoGetItemClientObject( 
 319     return (wxClientData 
*)DoGetItemClientData(n
); 
 320 } // end of wxChoice::DoGetItemClientObject 
 322 // ---------------------------------------------------------------------------- 
 323 // wxOS2 specific helpers 
 324 // ---------------------------------------------------------------------------- 
 326 void wxChoice::DoSetSize( 
 335     // Ignore height parameter because height doesn't mean 'initially 
 336     // displayed' height, it refers to the drop-down menu as well. The 
 337     // wxWidgets interpretation is different; also, getting the size returns 
 338     // the _displayed_ size (NOT the drop down menu size) so 
 339     // setting-getting-setting size would not work. 
 341     wxControl::DoSetSize( nX
 
 347 } // end of wxChoice::DoSetSize 
 349 wxSize 
wxChoice::DoGetBestSize() const 
 352     // Find the widest string 
 355     int                             nChoiceWidth 
= 0; 
 356     int                             nItems 
= GetCount(); 
 360     for (int i 
= 0; i 
< nItems
; i
++) 
 362         wxString                    
sStr(GetString(i
)); 
 368         if (nLineWidth 
> nChoiceWidth
) 
 369             nChoiceWidth 
= nLineWidth
; 
 373     // Give it some reasonable default value if there are no strings in the 
 376     if (nChoiceWidth 
== 0L) 
 380     // The combobox should be larger than the widest string 
 382     wxGetCharSize( GetHWND() 
 387     nChoiceWidth 
+= 5 * nCx
; 
 390     // Choice drop-down list depends on number of items (limited to 10) 
 392     size_t                          nStrings 
= nItems 
== 0 ? 10 : wxMin(10, nItems
) + 1; 
 393     int                             nChoiceHeight 
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy
) * nStrings
; 
 395     return wxSize( nChoiceWidth
 
 398 } // end of wxChoice::DoGetBestSize 
 400 MRESULT 
wxChoice::OS2WindowProc( 
 406     return wxWindow::OS2WindowProc( uMsg
 
 410 } // end of wxChoice::OS2WindowProc 
 412 bool wxChoice::OS2Command( 
 414 , WXWORD                            
WXUNUSED(wId
) 
 417     if (uParam 
!= LN_SELECT
) 
 420         // "selection changed" is the only event we're after 
 424     int                             n 
= GetSelection(); 
 428         wxCommandEvent              
vEvent( wxEVT_COMMAND_CHOICE_SELECTED
 
 433         vEvent
.SetEventObject(this); 
 434         vEvent
.SetString((char*)GetStringSelection().c_str()); 
 435         if (HasClientObjectData()) 
 436             vEvent
.SetClientObject(GetClientObject(n
)); 
 437         else if (HasClientUntypedData()) 
 438             vEvent
.SetClientData(GetClientData(n
)); 
 439         ProcessCommand(vEvent
); 
 442 } // end of wxChoice::OS2Command 
 444 void wxChoice::Free() 
 446     if (HasClientObjectData()) 
 448         size_t                      nCount 
= GetCount(); 
 450         for (size_t n 
= 0; n 
< nCount
; n
++) 
 452             delete GetClientObject(n
); 
 455 } // end of wxhoice::Free