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