]> git.saurik.com Git - wxWidgets.git/blame - src/motif/checklst.cpp
keep initial size from changing when frame extents become known
[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
4bb6408c 54// def ctor: use Create() to really create the control
ef41d80c 55wxCheckListBox::wxCheckListBox() : wxCheckListBoxBase()
4bb6408c
JS
56{
57}
58
59// ctor which creates the associated control
60wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
61 const wxPoint& pos, const wxSize& size,
62 int nStrings, const wxString choices[],
63 long style, const wxValidator& val,
64 const wxString& name)
ef41d80c 65 : wxCheckListBoxBase()
4bb6408c 66{
ef41d80c
MB
67 Create(parent, id, pos, size, nStrings, choices,
68 style, val, name);
4bb6408c
JS
69}
70
584ad2a3
MB
71wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
72 const wxPoint& pos, const wxSize& size,
73 const wxArrayString& choices,
74 long style, const wxValidator& val,
75 const wxString& name)
76 : wxCheckListBoxBase()
77{
78 Create(parent, id, pos, size, choices,
79 style, val, name);
80}
81
ef41d80c 82bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
c266945c
JS
83 const wxPoint& pos,
84 const wxSize& size,
85 int n, const wxString choices[],
86 long style,
87 const wxValidator& validator,
ef41d80c
MB
88 const wxString& name)
89{
2b5f62a0
VZ
90 // wxListBox::Create calls set, which adds the prefixes
91 bool retVal = wxListBox::Create(parent, id, pos, size, n, choices,
ef41d80c 92 style, validator, name);
ef41d80c 93 return retVal;
11e62fe6 94}
ef41d80c 95
584ad2a3
MB
96bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
97 const wxPoint& pos,
98 const wxSize& size,
99 const wxArrayString& choices,
100 long style,
101 const wxValidator& validator,
102 const wxString& name)
103{
104 // wxListBox::Create calls set, which adds the prefixes
105 bool retVal = wxListBox::Create(parent, id, pos, size, choices,
106 style, validator, name);
107 return retVal;
11e62fe6 108}
584ad2a3 109
4bb6408c
JS
110// check items
111// -----------
112
aa61d352 113bool wxCheckListBox::IsChecked(unsigned int uiIndex) const
ef41d80c
MB
114{
115 return ::IsChecked(wxListBox::GetString(uiIndex));
116}
117
aa61d352 118void wxCheckListBox::Check(unsigned int uiIndex, bool bCheck)
4bb6408c 119{
ef41d80c
MB
120 wxString label = wxListBox::GetString(uiIndex);
121 if(::IsChecked(label) == bCheck) return;
122 label[1u] = bCheck ? checkChar : uncheckChar;
123 wxListBox::SetString(uiIndex, label);
4bb6408c
JS
124}
125
ef41d80c 126void wxCheckListBox::DoToggleItem( int n, int x )
4bb6408c 127{
105fbe1f 128 if( x > 0 && x < 23 )
ef41d80c
MB
129 {
130 wxString label = wxListBox::GetString(n);
131 label[1u] = (!::IsChecked(label)) ? checkChar : uncheckChar;
132 wxListBox::SetString(n, label);
133
134 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, GetId());
135 if( HasClientObjectData() )
136 event.SetClientObject( GetClientObject(n) );
137 else if( HasClientUntypedData() )
138 event.SetClientData( GetClientData(n) );
687706f5 139 event.SetInt(n);
96be256b 140 event.SetExtraLong(true);
ef41d80c 141 event.SetEventObject(this);
aa61d352 142 event.SetString(GetString(n));
ef41d80c 143
937013e0 144 HandleWindowEvent(event);
ef41d80c 145 }
4bb6408c
JS
146}
147
11e62fe6 148int wxCheckListBox::FindString(const wxString& s, bool bCase) const
ef41d80c 149{
11e62fe6
WS
150 int n1 = wxListBox::FindString(Prefix(false) + s, bCase);
151 int n2 = wxListBox::FindString(Prefix(true) + s, bCase);
ef41d80c
MB
152 int min = wxMin(n1, n2), max = wxMax(n1, n2);
153
154 // why this works:
155 // n1 == -1, n2 == -1 => return -1 OK
156 // n1 != -1 || n2 != -1 => min == -1 => return the other one
157 // both != -1 => return the first one.
158 if( min == -1 ) return max;
159 return min;
160}
161
aa61d352 162void wxCheckListBox::SetString(unsigned int n, const wxString& s)
ef41d80c 163{
aa61d352 164 wxListBox::SetString(n, Prefix(IsChecked(n)) + s);
ef41d80c 165}
4bb6408c 166
aa61d352 167wxString wxCheckListBox::GetString(unsigned int n) const
ef41d80c
MB
168{
169 return wxListBox::GetString(n).substr(4);
170}
171
a236aa20
VZ
172int wxCheckListBox::DoInsertItems(const wxArrayStringsAdapter& items,
173 unsigned int pos,
174 void **clientData, wxClientDataType type)
ef41d80c
MB
175{
176 wxArrayString copy;
a236aa20
VZ
177 copy.reserve(pos);
178 for ( size_t i = 0; i < items.GetCount(); ++i )
179 copy.push_back( Prefix(false) + items[i] );
ef41d80c 180
a236aa20 181 return wxListBox::DoInsertItems(copy, pos, clientData, type);
ef41d80c 182}
aa61d352
VZ
183
184#endif // wxUSE_CHECKLISTBOX