]> git.saurik.com Git - wxWidgets.git/blame - src/motif/checklst.cpp
Now works when derived from wxTextCtrlBase, using wxScrollHelper.
[wxWidgets.git] / src / motif / checklst.cpp
CommitLineData
4bb6408c 1///////////////////////////////////////////////////////////////////////////////
11e62fe6 2// Name: src/motif/checklst.cpp
4bb6408c
JS
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
65571936 9// Licence: wxWindows licence
4bb6408c
JS
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// headers & declarations
14// ============================================================================
15
1248b41f
MB
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
f6045f99
GD
19#include "wx/defs.h"
20
4bb6408c 21#include "wx/checklst.h"
a9711a4d 22#include "wx/arrstr.h"
4bb6408c
JS
23
24// ============================================================================
6463b9f5 25// implementation
4bb6408c
JS
26// ============================================================================
27
2d120f83 28IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
4bb6408c
JS
29
30// ----------------------------------------------------------------------------
31// implementation of wxCheckListBox class
32// ----------------------------------------------------------------------------
33
34// define event table
35// ------------------
36BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
37END_EVENT_TABLE()
38
39// control creation
40// ----------------
41
ef41d80c
MB
42static const wxString prefixChecked = "[x] ";
43static const wxString prefixUnchecked = "[ ] ";
44static const char checkChar = 'x', uncheckChar = ' ';
45
46static inline const wxString& Prefix(bool checked)
47 { return checked ? prefixChecked : prefixUnchecked; }
48static inline bool IsChecked(const wxString& s)
49 { wxASSERT(s.length() >=4); return s[1] == checkChar; }
50
51static void CopyStringsAddingPrefix(const wxArrayString& orig,
52 wxArrayString& copy)
53{
54 copy.Clear();
55
56 for(size_t i = 0; i < orig.GetCount(); ++i )
96be256b 57 copy.Add( Prefix(false) + orig[i] );
ef41d80c
MB
58}
59
4bb6408c 60// def ctor: use Create() to really create the control
ef41d80c 61wxCheckListBox::wxCheckListBox() : wxCheckListBoxBase()
4bb6408c
JS
62{
63}
64
65// ctor which creates the associated control
66wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
67 const wxPoint& pos, const wxSize& size,
68 int nStrings, const wxString choices[],
69 long style, const wxValidator& val,
70 const wxString& name)
ef41d80c 71 : wxCheckListBoxBase()
4bb6408c 72{
ef41d80c
MB
73 Create(parent, id, pos, size, nStrings, choices,
74 style, val, name);
4bb6408c
JS
75}
76
584ad2a3
MB
77wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
78 const wxPoint& pos, const wxSize& size,
79 const wxArrayString& choices,
80 long style, const wxValidator& val,
81 const wxString& name)
82 : wxCheckListBoxBase()
83{
84 Create(parent, id, pos, size, choices,
85 style, val, name);
86}
87
ef41d80c 88bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
c266945c
JS
89 const wxPoint& pos,
90 const wxSize& size,
91 int n, const wxString choices[],
92 long style,
93 const wxValidator& validator,
ef41d80c
MB
94 const wxString& name)
95{
2b5f62a0
VZ
96 // wxListBox::Create calls set, which adds the prefixes
97 bool retVal = wxListBox::Create(parent, id, pos, size, n, choices,
ef41d80c 98 style, validator, name);
ef41d80c 99 return retVal;
11e62fe6 100}
ef41d80c 101
584ad2a3
MB
102bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
103 const wxPoint& pos,
104 const wxSize& size,
105 const wxArrayString& choices,
106 long style,
107 const wxValidator& validator,
108 const wxString& name)
109{
110 // wxListBox::Create calls set, which adds the prefixes
111 bool retVal = wxListBox::Create(parent, id, pos, size, choices,
112 style, validator, name);
113 return retVal;
11e62fe6 114}
584ad2a3 115
4bb6408c
JS
116// check items
117// -----------
118
ef41d80c
MB
119bool wxCheckListBox::IsChecked(size_t uiIndex) const
120{
121 return ::IsChecked(wxListBox::GetString(uiIndex));
122}
123
124void wxCheckListBox::Check(size_t uiIndex, bool bCheck)
4bb6408c 125{
ef41d80c
MB
126 wxString label = wxListBox::GetString(uiIndex);
127 if(::IsChecked(label) == bCheck) return;
128 label[1u] = bCheck ? checkChar : uncheckChar;
129 wxListBox::SetString(uiIndex, label);
4bb6408c
JS
130}
131
ef41d80c 132void wxCheckListBox::DoToggleItem( int n, int x )
4bb6408c 133{
ef41d80c
MB
134 if( x < 23 )
135 {
136 wxString label = wxListBox::GetString(n);
137 label[1u] = (!::IsChecked(label)) ? checkChar : uncheckChar;
138 wxListBox::SetString(n, label);
139
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) );
687706f5 145 event.SetInt(n);
96be256b 146 event.SetExtraLong(true);
ef41d80c
MB
147 event.SetEventObject(this);
148 event.SetString( GetString( n ) );
149
150 GetEventHandler()->ProcessEvent(event);
151 }
4bb6408c
JS
152}
153
ef41d80c
MB
154int wxCheckListBox::DoAppend(const wxString& item)
155{
96be256b 156 return wxListBox::DoAppend( Prefix(false) + item );
ef41d80c
MB
157}
158
11e62fe6 159int wxCheckListBox::FindString(const wxString& s, bool bCase) const
ef41d80c 160{
11e62fe6
WS
161 int n1 = wxListBox::FindString(Prefix(false) + s, bCase);
162 int n2 = wxListBox::FindString(Prefix(true) + s, bCase);
ef41d80c
MB
163 int min = wxMin(n1, n2), max = wxMax(n1, n2);
164
165 // why this works:
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;
170 return min;
171}
172
173void wxCheckListBox::SetString(int n, const wxString& s)
174{
175 wxListBox::SetString( n, Prefix(IsChecked(n)) + s );
176}
4bb6408c 177
ef41d80c
MB
178wxString wxCheckListBox::GetString(int n) const
179{
180 return wxListBox::GetString(n).substr(4);
181}
182
183void wxCheckListBox::DoInsertItems(const wxArrayString& items, int pos)
184{
185 wxArrayString copy;
186 CopyStringsAddingPrefix(items, copy);
187 wxListBox::DoInsertItems(copy, pos);
188}
189
190void wxCheckListBox::DoSetItems(const wxArrayString& items, void **clientData)
191{
192 wxArrayString copy;
193 CopyStringsAddingPrefix(items, copy);
194 wxListBox::DoSetItems(copy, clientData);
195}