| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/motif/checklst.cpp |
| 3 | // Purpose: implementation of wxCheckListBox class |
| 4 | // Author: Julian Smart |
| 5 | // Modified by: |
| 6 | // Created: 17/09/98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Julian Smart |
| 9 | // Licence: wxWindows licence |
| 10 | /////////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // headers & declarations |
| 14 | // ============================================================================ |
| 15 | |
| 16 | // For compilers that support precompilation, includes "wx.h". |
| 17 | #include "wx/wxprec.h" |
| 18 | |
| 19 | #if wxUSE_CHECKLISTBOX |
| 20 | |
| 21 | #include "wx/checklst.h" |
| 22 | |
| 23 | #ifndef WX_PRECOMP |
| 24 | #include "wx/arrstr.h" |
| 25 | #endif |
| 26 | |
| 27 | // ============================================================================ |
| 28 | // implementation |
| 29 | // ============================================================================ |
| 30 | |
| 31 | IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox) |
| 32 | |
| 33 | // ---------------------------------------------------------------------------- |
| 34 | // implementation of wxCheckListBox class |
| 35 | // ---------------------------------------------------------------------------- |
| 36 | |
| 37 | // define event table |
| 38 | // ------------------ |
| 39 | BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox) |
| 40 | END_EVENT_TABLE() |
| 41 | |
| 42 | // control creation |
| 43 | // ---------------- |
| 44 | |
| 45 | static const wxString prefixChecked = "[x] "; |
| 46 | static const wxString prefixUnchecked = "[ ] "; |
| 47 | static const char checkChar = 'x', uncheckChar = ' '; |
| 48 | |
| 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; } |
| 53 | |
| 54 | // def ctor: use Create() to really create the control |
| 55 | wxCheckListBox::wxCheckListBox() : wxCheckListBoxBase() |
| 56 | { |
| 57 | } |
| 58 | |
| 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, |
| 64 | const wxString& name) |
| 65 | : wxCheckListBoxBase() |
| 66 | { |
| 67 | Create(parent, id, pos, size, nStrings, choices, |
| 68 | style, val, name); |
| 69 | } |
| 70 | |
| 71 | wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id, |
| 72 | const wxPoint& pos, const wxSize& size, |
| 73 | const wxArrayString& choices, |
| 74 | long style, const wxValidator& val, |
| 75 | const wxString& name) |
| 76 | : wxCheckListBoxBase() |
| 77 | { |
| 78 | Create(parent, id, pos, size, choices, |
| 79 | style, val, name); |
| 80 | } |
| 81 | |
| 82 | bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id, |
| 83 | const wxPoint& pos, |
| 84 | const wxSize& size, |
| 85 | int n, const wxString choices[], |
| 86 | long style, |
| 87 | const wxValidator& validator, |
| 88 | const wxString& name) |
| 89 | { |
| 90 | // wxListBox::Create calls set, which adds the prefixes |
| 91 | bool retVal = wxListBox::Create(parent, id, pos, size, n, choices, |
| 92 | style, validator, name); |
| 93 | return retVal; |
| 94 | } |
| 95 | |
| 96 | bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id, |
| 97 | const wxPoint& pos, |
| 98 | const wxSize& size, |
| 99 | const wxArrayString& choices, |
| 100 | long style, |
| 101 | const wxValidator& validator, |
| 102 | const wxString& name) |
| 103 | { |
| 104 | // wxListBox::Create calls set, which adds the prefixes |
| 105 | bool retVal = wxListBox::Create(parent, id, pos, size, choices, |
| 106 | style, validator, name); |
| 107 | return retVal; |
| 108 | } |
| 109 | |
| 110 | // check items |
| 111 | // ----------- |
| 112 | |
| 113 | bool wxCheckListBox::IsChecked(unsigned int uiIndex) const |
| 114 | { |
| 115 | return ::IsChecked(wxListBox::GetString(uiIndex)); |
| 116 | } |
| 117 | |
| 118 | void wxCheckListBox::Check(unsigned int uiIndex, bool bCheck) |
| 119 | { |
| 120 | wxString label = wxListBox::GetString(uiIndex); |
| 121 | if(::IsChecked(label) == bCheck) return; |
| 122 | label[1u] = bCheck ? checkChar : uncheckChar; |
| 123 | wxListBox::SetString(uiIndex, label); |
| 124 | } |
| 125 | |
| 126 | void wxCheckListBox::DoToggleItem( int n, int x ) |
| 127 | { |
| 128 | if( x > 0 && x < 23 ) |
| 129 | { |
| 130 | wxString label = wxListBox::GetString(n); |
| 131 | label[1u] = (!::IsChecked(label)) ? checkChar : uncheckChar; |
| 132 | wxListBox::SetString(n, label); |
| 133 | |
| 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) ); |
| 139 | event.SetInt(n); |
| 140 | event.SetExtraLong(true); |
| 141 | event.SetEventObject(this); |
| 142 | event.SetString(GetString(n)); |
| 143 | |
| 144 | HandleWindowEvent(event); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | int wxCheckListBox::FindString(const wxString& s, bool bCase) const |
| 149 | { |
| 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); |
| 153 | |
| 154 | // why this works: |
| 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; |
| 159 | return min; |
| 160 | } |
| 161 | |
| 162 | void wxCheckListBox::SetString(unsigned int n, const wxString& s) |
| 163 | { |
| 164 | wxListBox::SetString(n, Prefix(IsChecked(n)) + s); |
| 165 | } |
| 166 | |
| 167 | wxString wxCheckListBox::GetString(unsigned int n) const |
| 168 | { |
| 169 | return wxListBox::GetString(n).substr(4); |
| 170 | } |
| 171 | |
| 172 | int wxCheckListBox::DoInsertItems(const wxArrayStringsAdapter& items, |
| 173 | unsigned int pos, |
| 174 | void **clientData, wxClientDataType type) |
| 175 | { |
| 176 | wxArrayString copy; |
| 177 | copy.reserve(pos); |
| 178 | for ( size_t i = 0; i < items.GetCount(); ++i ) |
| 179 | copy.push_back( Prefix(false) + items[i] ); |
| 180 | |
| 181 | return wxListBox::DoInsertItems(copy, pos, clientData, type); |
| 182 | } |
| 183 | |
| 184 | #endif // wxUSE_CHECKLISTBOX |