]> git.saurik.com Git - wxWidgets.git/blame - src/motif/checklst.cpp
fix bug in Borland (bcc32 really hates ?)
[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
16#ifdef __GNUG__
17#pragma implementation "checklst.h"
18#endif
19
f6045f99
GD
20#include "wx/defs.h"
21
4bb6408c
JS
22#include "wx/checklst.h"
23
24// ============================================================================
25// implementation
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 )
fd280707 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
ef41d80c 77bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
c266945c
JS
78 const wxPoint& pos,
79 const wxSize& size,
80 int n, const wxString choices[],
81 long style,
82 const wxValidator& validator,
ef41d80c
MB
83 const wxString& name)
84{
2b5f62a0
VZ
85 // wxListBox::Create calls set, which adds the prefixes
86 bool retVal = wxListBox::Create(parent, id, pos, size, n, choices,
ef41d80c 87 style, validator, name);
ef41d80c
MB
88 return retVal;
89}
90
4bb6408c
JS
91// check items
92// -----------
93
ef41d80c
MB
94bool wxCheckListBox::IsChecked(size_t uiIndex) const
95{
96 return ::IsChecked(wxListBox::GetString(uiIndex));
97}
98
99void wxCheckListBox::Check(size_t uiIndex, bool bCheck)
4bb6408c 100{
ef41d80c
MB
101 wxString label = wxListBox::GetString(uiIndex);
102 if(::IsChecked(label) == bCheck) return;
103 label[1u] = bCheck ? checkChar : uncheckChar;
104 wxListBox::SetString(uiIndex, label);
4bb6408c
JS
105}
106
ef41d80c 107void wxCheckListBox::DoToggleItem( int n, int x )
4bb6408c 108{
ef41d80c
MB
109 if( x < 23 )
110 {
111 wxString label = wxListBox::GetString(n);
112 label[1u] = (!::IsChecked(label)) ? checkChar : uncheckChar;
113 wxListBox::SetString(n, label);
114
115 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, GetId());
116 if( HasClientObjectData() )
117 event.SetClientObject( GetClientObject(n) );
118 else if( HasClientUntypedData() )
119 event.SetClientData( GetClientData(n) );
120 event.m_commandInt = n;
121 event.m_extraLong = TRUE;
122 event.SetEventObject(this);
123 event.SetString( GetString( n ) );
124
125 GetEventHandler()->ProcessEvent(event);
126 }
4bb6408c
JS
127}
128
ef41d80c
MB
129int wxCheckListBox::DoAppend(const wxString& item)
130{
131 return wxListBox::DoAppend( Prefix(FALSE) + item );
132}
133
134int wxCheckListBox::FindString(const wxString& s) const
135{
136 int n1 = wxListBox::FindString(Prefix(FALSE) + s);
137 int n2 = wxListBox::FindString(Prefix(TRUE) + s);
138 int min = wxMin(n1, n2), max = wxMax(n1, n2);
139
140 // why this works:
141 // n1 == -1, n2 == -1 => return -1 OK
142 // n1 != -1 || n2 != -1 => min == -1 => return the other one
143 // both != -1 => return the first one.
144 if( min == -1 ) return max;
145 return min;
146}
147
148void wxCheckListBox::SetString(int n, const wxString& s)
149{
150 wxListBox::SetString( n, Prefix(IsChecked(n)) + s );
151}
4bb6408c 152
ef41d80c
MB
153wxString wxCheckListBox::GetString(int n) const
154{
155 return wxListBox::GetString(n).substr(4);
156}
157
158void wxCheckListBox::DoInsertItems(const wxArrayString& items, int pos)
159{
160 wxArrayString copy;
161 CopyStringsAddingPrefix(items, copy);
162 wxListBox::DoInsertItems(copy, pos);
163}
164
165void wxCheckListBox::DoSetItems(const wxArrayString& items, void **clientData)
166{
167 wxArrayString copy;
168 CopyStringsAddingPrefix(items, copy);
169 wxListBox::DoSetItems(copy, clientData);
170}