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