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