1 /////////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/motif/checklst.cpp 
   3 // Purpose:     implementation of wxCheckListBox class 
   4 // Author:      Julian Smart 
   8 // Copyright:   (c) Julian Smart 
   9 // Licence:     wxWindows licence 
  10 /////////////////////////////////////////////////////////////////////////////// 
  12 // ============================================================================ 
  13 // headers & declarations 
  14 // ============================================================================ 
  16 // For compilers that support precompilation, includes "wx.h". 
  17 #include "wx/wxprec.h" 
  19 #if wxUSE_CHECKLISTBOX 
  21 #include "wx/checklst.h" 
  24     #include "wx/arrstr.h" 
  27 // ============================================================================ 
  29 // ============================================================================ 
  31 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
) 
  33 // ---------------------------------------------------------------------------- 
  34 // implementation of wxCheckListBox class 
  35 // ---------------------------------------------------------------------------- 
  39 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
) 
  45 static const wxString prefixChecked 
= "[x] "; 
  46 static const wxString prefixUnchecked 
= "[ ] "; 
  47 static const char checkChar 
= 'x', uncheckChar 
= ' '; 
  49 static inline const wxString
& Prefix(bool checked
) 
  50     { return checked 
? prefixChecked 
: prefixUnchecked
; } 
  51 static inline bool IsChecked(const wxString
& s
) 
  52     { wxASSERT(s
.length() >=4); return s
[1] == checkChar
; } 
  54 // def ctor: use Create() to really create the control 
  55 wxCheckListBox::wxCheckListBox() : wxCheckListBoxBase() 
  59 // ctor which creates the associated control 
  60 wxCheckListBox::wxCheckListBox(wxWindow 
*parent
, wxWindowID id
, 
  61                                const wxPoint
& pos
, const wxSize
& size
, 
  62                                int nStrings
, const wxString choices
[], 
  63                                long style
, const wxValidator
& val
, 
  65                                : wxCheckListBoxBase() 
  67     Create(parent
, id
, pos
, size
, nStrings
, choices
, 
  71 wxCheckListBox::wxCheckListBox(wxWindow 
*parent
, wxWindowID id
, 
  72                                const wxPoint
& pos
, const wxSize
& size
, 
  73                                const wxArrayString
& choices
, 
  74                                long style
, const wxValidator
& val
, 
  76                                : wxCheckListBoxBase() 
  78     Create(parent
, id
, pos
, size
, choices
, 
  82 bool wxCheckListBox::Create(wxWindow 
*parent
, wxWindowID id
, 
  85                             int n
, const wxString choices
[], 
  87                             const wxValidator
& validator
, 
  90     // wxListBox::Create calls set, which adds the prefixes 
  91     bool retVal 
= wxListBox::Create(parent
, id
, pos
, size
, n
, choices
, 
  92                                     style
, validator
, name
); 
  96 bool wxCheckListBox::Create(wxWindow 
*parent
, wxWindowID id
, 
  99                             const wxArrayString
& choices
, 
 101                             const wxValidator
& validator
, 
 102                             const wxString
& name
) 
 104     // wxListBox::Create calls set, which adds the prefixes 
 105     bool retVal 
= wxListBox::Create(parent
, id
, pos
, size
, choices
, 
 106                                     style
, validator
, name
); 
 113 bool wxCheckListBox::IsChecked(unsigned int uiIndex
) const 
 115     return ::IsChecked(wxListBox::GetString(uiIndex
)); 
 118 void wxCheckListBox::Check(unsigned int uiIndex
, bool bCheck
) 
 120     wxString label 
= wxListBox::GetString(uiIndex
); 
 121     if(::IsChecked(label
) == bCheck
) return; 
 122     label
[1u] = bCheck 
? checkChar 
: uncheckChar
; 
 123     wxListBox::SetString(uiIndex
, label
); 
 126 void wxCheckListBox::DoToggleItem( int n
, int x 
) 
 128     if( x 
> 0 && x 
< 23 ) 
 130         wxString label 
= wxListBox::GetString(n
); 
 131         label
[1u] = (!::IsChecked(label
)) ? checkChar 
: uncheckChar
; 
 132         wxListBox::SetString(n
, label
); 
 134         wxCommandEvent 
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, GetId()); 
 135         if( HasClientObjectData() ) 
 136             event
.SetClientObject( GetClientObject(n
) ); 
 137         else if( HasClientUntypedData() ) 
 138             event
.SetClientData( GetClientData(n
) ); 
 140         event
.SetExtraLong(true); 
 141         event
.SetEventObject(this); 
 142         event
.SetString(GetString(n
)); 
 144         HandleWindowEvent(event
); 
 148 int wxCheckListBox::FindString(const wxString
& s
, bool bCase
) const 
 150     int n1 
= wxListBox::FindString(Prefix(false) + s
, bCase
); 
 151     int n2 
= wxListBox::FindString(Prefix(true) + s
, bCase
); 
 152     int min 
= wxMin(n1
, n2
), max 
= wxMax(n1
, n2
); 
 155     // n1 == -1, n2 == -1 => return -1 OK 
 156     // n1 != -1 || n2 != -1 => min == -1 => return the other one 
 157     // both != -1 => return the first one. 
 158     if( min 
== -1 ) return max
; 
 162 void wxCheckListBox::SetString(unsigned int n
, const wxString
& s
) 
 164     wxListBox::SetString(n
, Prefix(IsChecked(n
)) + s
); 
 167 wxString 
wxCheckListBox::GetString(unsigned int n
) const 
 169     return wxListBox::GetString(n
).substr(4); 
 172 int wxCheckListBox::DoInsertItems(const wxArrayStringsAdapter
& items
, 
 174                                   void **clientData
, wxClientDataType type
) 
 178     for ( size_t i 
= 0; i 
< items
.GetCount(); ++i 
) 
 179         copy
.push_back( Prefix(false) + items
[i
] ); 
 181     return wxListBox::DoInsertItems(copy
, pos
, clientData
, type
); 
 184 #endif // wxUSE_CHECKLISTBOX