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