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