]> git.saurik.com Git - wxWidgets.git/blob - src/motif/checklst.cpp
Applied patch [ 832096 ] Final separation for GUI and console for Open Watcom
[wxWidgets.git] / src / motif / checklst.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma implementation "checklst.h"
18 #endif
19
20 #include "wx/defs.h"
21
22 #include "wx/checklst.h"
23 #include "wx/arrstr.h"
24
25 // ============================================================================
26 // implementation of wxCheckListBoxBase
27 // ============================================================================
28
29 wxCheckListBoxBase::wxCheckListBoxBase()
30 {
31 }
32
33 // ============================================================================
34 // implementation of wxCheckListBox
35 // ============================================================================
36
37 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
38
39 // ----------------------------------------------------------------------------
40 // implementation of wxCheckListBox class
41 // ----------------------------------------------------------------------------
42
43 // define event table
44 // ------------------
45 BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
46 END_EVENT_TABLE()
47
48 // control creation
49 // ----------------
50
51 static const wxString prefixChecked = "[x] ";
52 static const wxString prefixUnchecked = "[ ] ";
53 static const char checkChar = 'x', uncheckChar = ' ';
54
55 static inline const wxString& Prefix(bool checked)
56 { return checked ? prefixChecked : prefixUnchecked; }
57 static inline bool IsChecked(const wxString& s)
58 { wxASSERT(s.length() >=4); return s[1] == checkChar; }
59
60 static void CopyStringsAddingPrefix(const wxArrayString& orig,
61 wxArrayString& copy)
62 {
63 copy.Clear();
64
65 for(size_t i = 0; i < orig.GetCount(); ++i )
66 copy.Add( Prefix(FALSE) + orig[i] );
67 }
68
69 // def ctor: use Create() to really create the control
70 wxCheckListBox::wxCheckListBox() : wxCheckListBoxBase()
71 {
72 }
73
74 // ctor which creates the associated control
75 wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
76 const wxPoint& pos, const wxSize& size,
77 int nStrings, const wxString choices[],
78 long style, const wxValidator& val,
79 const wxString& name)
80 : wxCheckListBoxBase()
81 {
82 Create(parent, id, pos, size, nStrings, choices,
83 style, val, name);
84 }
85
86 bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
87 const wxPoint& pos,
88 const wxSize& size,
89 int n, const wxString choices[],
90 long style,
91 const wxValidator& validator,
92 const wxString& name)
93 {
94 // wxListBox::Create calls set, which adds the prefixes
95 bool retVal = wxListBox::Create(parent, id, pos, size, n, choices,
96 style, validator, name);
97 return retVal;
98 }
99
100 // check items
101 // -----------
102
103 bool wxCheckListBox::IsChecked(size_t uiIndex) const
104 {
105 return ::IsChecked(wxListBox::GetString(uiIndex));
106 }
107
108 void wxCheckListBox::Check(size_t uiIndex, bool bCheck)
109 {
110 wxString label = wxListBox::GetString(uiIndex);
111 if(::IsChecked(label) == bCheck) return;
112 label[1u] = bCheck ? checkChar : uncheckChar;
113 wxListBox::SetString(uiIndex, label);
114 }
115
116 void wxCheckListBox::DoToggleItem( int n, int x )
117 {
118 if( x < 23 )
119 {
120 wxString label = wxListBox::GetString(n);
121 label[1u] = (!::IsChecked(label)) ? checkChar : uncheckChar;
122 wxListBox::SetString(n, label);
123
124 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, GetId());
125 if( HasClientObjectData() )
126 event.SetClientObject( GetClientObject(n) );
127 else if( HasClientUntypedData() )
128 event.SetClientData( GetClientData(n) );
129 event.m_commandInt = n;
130 event.m_extraLong = TRUE;
131 event.SetEventObject(this);
132 event.SetString( GetString( n ) );
133
134 GetEventHandler()->ProcessEvent(event);
135 }
136 }
137
138 int wxCheckListBox::DoAppend(const wxString& item)
139 {
140 return wxListBox::DoAppend( Prefix(FALSE) + item );
141 }
142
143 int wxCheckListBox::FindString(const wxString& s) const
144 {
145 int n1 = wxListBox::FindString(Prefix(FALSE) + s);
146 int n2 = wxListBox::FindString(Prefix(TRUE) + s);
147 int min = wxMin(n1, n2), max = wxMax(n1, n2);
148
149 // why this works:
150 // n1 == -1, n2 == -1 => return -1 OK
151 // n1 != -1 || n2 != -1 => min == -1 => return the other one
152 // both != -1 => return the first one.
153 if( min == -1 ) return max;
154 return min;
155 }
156
157 void wxCheckListBox::SetString(int n, const wxString& s)
158 {
159 wxListBox::SetString( n, Prefix(IsChecked(n)) + s );
160 }
161
162 wxString wxCheckListBox::GetString(int n) const
163 {
164 return wxListBox::GetString(n).substr(4);
165 }
166
167 void wxCheckListBox::DoInsertItems(const wxArrayString& items, int pos)
168 {
169 wxArrayString copy;
170 CopyStringsAddingPrefix(items, copy);
171 wxListBox::DoInsertItems(copy, pos);
172 }
173
174 void wxCheckListBox::DoSetItems(const wxArrayString& items, void **clientData)
175 {
176 wxArrayString copy;
177 CopyStringsAddingPrefix(items, copy);
178 wxListBox::DoSetItems(copy, clientData);
179 }