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" 
  22 #include "wx/arrstr.h" 
  24 // ============================================================================ 
  26 // ============================================================================ 
  28 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
) 
  30 // ---------------------------------------------------------------------------- 
  31 // implementation of wxCheckListBox class 
  32 // ---------------------------------------------------------------------------- 
  36 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
) 
  42 static const wxString prefixChecked 
= "[x] "; 
  43 static const wxString prefixUnchecked 
= "[ ] "; 
  44 static const char checkChar 
= 'x', uncheckChar 
= ' '; 
  46 static inline const wxString
& Prefix(bool checked
) 
  47     { return checked 
? prefixChecked 
: prefixUnchecked
; } 
  48 static inline bool IsChecked(const wxString
& s
) 
  49     { wxASSERT(s
.length() >=4); return s
[1] == checkChar
; } 
  51 static void CopyStringsAddingPrefix(const wxArrayString
& orig
, 
  56     for(size_t i 
= 0; i 
< orig
.GetCount(); ++i 
) 
  57         copy
.Add( Prefix(false) + orig
[i
] ); 
  60 // def ctor: use Create() to really create the control 
  61 wxCheckListBox::wxCheckListBox() : wxCheckListBoxBase() 
  65 // ctor which creates the associated control 
  66 wxCheckListBox::wxCheckListBox(wxWindow 
*parent
, wxWindowID id
, 
  67                                const wxPoint
& pos
, const wxSize
& size
, 
  68                                int nStrings
, const wxString choices
[], 
  69                                long style
, const wxValidator
& val
, 
  71                                : wxCheckListBoxBase() 
  73     Create(parent
, id
, pos
, size
, nStrings
, choices
, 
  77 wxCheckListBox::wxCheckListBox(wxWindow 
*parent
, wxWindowID id
, 
  78                                const wxPoint
& pos
, const wxSize
& size
, 
  79                                const wxArrayString
& choices
, 
  80                                long style
, const wxValidator
& val
, 
  82                                : wxCheckListBoxBase() 
  84     Create(parent
, id
, pos
, size
, choices
, 
  88 bool wxCheckListBox::Create(wxWindow 
*parent
, wxWindowID id
, 
  91                             int n
, const wxString choices
[], 
  93                             const wxValidator
& validator
, 
  96     // wxListBox::Create calls set, which adds the prefixes 
  97     bool retVal 
= wxListBox::Create(parent
, id
, pos
, size
, n
, choices
, 
  98                                     style
, validator
, name
); 
 102 bool wxCheckListBox::Create(wxWindow 
*parent
, wxWindowID id
, 
 105                             const wxArrayString
& choices
, 
 107                             const wxValidator
& validator
, 
 108                             const wxString
& name
) 
 110     // wxListBox::Create calls set, which adds the prefixes 
 111     bool retVal 
= wxListBox::Create(parent
, id
, pos
, size
, choices
, 
 112                                     style
, validator
, name
); 
 119 bool wxCheckListBox::IsChecked(unsigned int uiIndex
) const 
 121     return ::IsChecked(wxListBox::GetString(uiIndex
)); 
 124 void wxCheckListBox::Check(unsigned int uiIndex
, bool bCheck
) 
 126     wxString label 
= wxListBox::GetString(uiIndex
); 
 127     if(::IsChecked(label
) == bCheck
) return; 
 128     label
[1u] = bCheck 
? checkChar 
: uncheckChar
; 
 129     wxListBox::SetString(uiIndex
, label
); 
 132 void wxCheckListBox::DoToggleItem( int n
, int x 
) 
 136         wxString label 
= wxListBox::GetString(n
); 
 137         label
[1u] = (!::IsChecked(label
)) ? checkChar 
: uncheckChar
; 
 138         wxListBox::SetString(n
, label
); 
 140         wxCommandEvent 
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, GetId()); 
 141         if( HasClientObjectData() ) 
 142             event
.SetClientObject( GetClientObject(n
) ); 
 143         else if( HasClientUntypedData() ) 
 144             event
.SetClientData( GetClientData(n
) ); 
 146         event
.SetExtraLong(true); 
 147         event
.SetEventObject(this); 
 148         event
.SetString(GetString(n
)); 
 150         GetEventHandler()->ProcessEvent(event
); 
 154 int wxCheckListBox::DoAppend(const wxString
& item
) 
 156     return wxListBox::DoAppend( Prefix(false) + item 
); 
 159 int wxCheckListBox::FindString(const wxString
& s
, bool bCase
) const 
 161     int n1 
= wxListBox::FindString(Prefix(false) + s
, bCase
); 
 162     int n2 
= wxListBox::FindString(Prefix(true) + s
, bCase
); 
 163     int min 
= wxMin(n1
, n2
), max 
= wxMax(n1
, n2
); 
 166     // n1 == -1, n2 == -1 => return -1 OK 
 167     // n1 != -1 || n2 != -1 => min == -1 => return the other one 
 168     // both != -1 => return the first one. 
 169     if( min 
== -1 ) return max
; 
 173 void wxCheckListBox::SetString(unsigned int n
, const wxString
& s
) 
 175     wxListBox::SetString(n
, Prefix(IsChecked(n
)) + s
); 
 178 wxString 
wxCheckListBox::GetString(unsigned int n
) const 
 180     return wxListBox::GetString(n
).substr(4); 
 183 void wxCheckListBox::DoInsertItems(const wxArrayString
& items
, unsigned int pos
) 
 186     CopyStringsAddingPrefix(items
, copy
); 
 187     wxListBox::DoInsertItems(copy
, pos
); 
 190 void wxCheckListBox::DoSetItems(const wxArrayString
& items
, void **clientData
) 
 193     CopyStringsAddingPrefix(items
, copy
); 
 194     wxListBox::DoSetItems(copy
, clientData
); 
 197 #endif // wxUSE_CHECKLISTBOX