]> git.saurik.com Git - wxWidgets.git/blame - src/generic/accel.cpp
Make wxOwnerDrawnComboBox::DoGetBestSize() twice as fast.
[wxWidgets.git] / src / generic / accel.cpp
CommitLineData
b90c32b4 1///////////////////////////////////////////////////////////////////////////////
8ecff181 2// Name: src/generic/accel.cpp
b90c32b4
VZ
3// Purpose: generic implementation of wxAcceleratorTable class
4// Author: Robert Roebling
5// Modified: VZ pn 31.05.01: use typed lists, Unicode cleanup, Add/Remove
b90c32b4 6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
b90c32b4
VZ
8///////////////////////////////////////////////////////////////////////////////
9
10// ============================================================================
11// declarations
12// ============================================================================
13
14// ----------------------------------------------------------------------------
15// headers
16// ----------------------------------------------------------------------------
17
b90c32b4
VZ
18// For compilers that support precompilation, includes "wx.h".
19#include "wx/wxprec.h"
20
21#ifdef __BORLANDC__
22 #pragma hdrstop
23#endif
24
25#if wxUSE_ACCEL
26
27#ifndef WX_PRECOMP
b90c32b4 28 #include "wx/list.h"
8ecff181 29 #include "wx/event.h"
b90c32b4
VZ
30#endif // WX_PRECOMP
31
32#include "wx/accel.h"
33
34#include <ctype.h>
35
36// ----------------------------------------------------------------------------
37// wxAccelList: a list of wxAcceleratorEntries
38// ----------------------------------------------------------------------------
39
40WX_DECLARE_LIST(wxAcceleratorEntry, wxAccelList);
41#include "wx/listimpl.cpp"
17a1ebd1 42WX_DEFINE_LIST(wxAccelList)
b90c32b4
VZ
43
44// ----------------------------------------------------------------------------
45// wxAccelRefData: the data used by wxAcceleratorTable
46// ----------------------------------------------------------------------------
47
48class wxAccelRefData : public wxObjectRefData
49{
50public:
fa03793d
VS
51 wxAccelRefData()
52 {
fa03793d
VS
53 }
54
55 wxAccelRefData(const wxAccelRefData& data)
d84afea9 56 : wxObjectRefData()
fa03793d 57 {
fa03793d
VS
58 m_accels = data.m_accels;
59 }
b90c32b4 60
222ed1d6
MB
61 virtual ~wxAccelRefData()
62 {
63 WX_CLEAR_LIST(wxAccelList, m_accels);
64 }
65
b90c32b4
VZ
66 wxAccelList m_accels;
67};
68
69// macro which can be used to access wxAccelRefData from wxAcceleratorTable
70#define M_ACCELDATA ((wxAccelRefData *)m_refData)
71
fa03793d 72
b90c32b4
VZ
73// ============================================================================
74// implementation
75// ============================================================================
76
77// ----------------------------------------------------------------------------
78// wxAcceleratorTable ctors
79// ----------------------------------------------------------------------------
80
81IMPLEMENT_DYNAMIC_CLASS(wxAcceleratorTable, wxObject)
82
83wxAcceleratorTable::wxAcceleratorTable()
84{
85}
86
3c674514 87wxAcceleratorTable::wxAcceleratorTable(int n, const wxAcceleratorEntry entries[])
b90c32b4
VZ
88{
89 m_refData = new wxAccelRefData;
90
91 for ( int i = 0; i < n; i++ )
92 {
93 const wxAcceleratorEntry& entry = entries[i];
94
8c03c8be 95 int keycode = entry.GetKeyCode();
7a0079d5
VZ
96 if ( wxIsascii(keycode) )
97 keycode = wxToupper(keycode);
b90c32b4
VZ
98
99 M_ACCELDATA->m_accels.Append(new wxAcceleratorEntry(entry.GetFlags(),
100 keycode,
101 entry.GetCommand()));
102 }
103}
104
105wxAcceleratorTable::~wxAcceleratorTable()
106{
107}
108
b7cacb43 109bool wxAcceleratorTable::IsOk() const
b90c32b4
VZ
110{
111 return m_refData != NULL;
112}
113
114// ----------------------------------------------------------------------------
115// wxAcceleratorTable updating
116// ----------------------------------------------------------------------------
117
118void wxAcceleratorTable::Add(const wxAcceleratorEntry& entry)
119{
fa03793d
VS
120 AllocExclusive();
121
b90c32b4
VZ
122 if ( !m_refData )
123 {
124 m_refData = new wxAccelRefData;
125 }
126
127 M_ACCELDATA->m_accels.Append(new wxAcceleratorEntry(entry));
128}
129
130void wxAcceleratorTable::Remove(const wxAcceleratorEntry& entry)
131{
fa03793d
VS
132 AllocExclusive();
133
222ed1d6 134 wxAccelList::compatibility_iterator node = M_ACCELDATA->m_accels.GetFirst();
b90c32b4
VZ
135 while ( node )
136 {
137 const wxAcceleratorEntry *entryCur = node->GetData();
138
d61f7200 139 // given entry contains only the information of the accelerator key
90527a50
VZ
140 // because it was set that way during creation so do not use the
141 // comparison operator which also checks the command field
d61f7200
WS
142 if ((entryCur->GetKeyCode() == entry.GetKeyCode()) &&
143 (entryCur->GetFlags() == entry.GetFlags()))
b90c32b4 144 {
222ed1d6
MB
145 delete node->GetData();
146 M_ACCELDATA->m_accels.Erase(node);
b90c32b4
VZ
147
148 return;
149 }
150
151 node = node->GetNext();
152 }
153
9a83f860 154 wxFAIL_MSG(wxT("deleting inexistent accel from wxAcceleratorTable"));
b90c32b4
VZ
155}
156
157// ----------------------------------------------------------------------------
158// wxAcceleratorTable: find a command for the given key press
159// ----------------------------------------------------------------------------
160
161const wxAcceleratorEntry *
162wxAcceleratorTable::GetEntry(const wxKeyEvent& event) const
163{
a1b806b9 164 if ( !IsOk() )
b90c32b4
VZ
165 {
166 // not an error, the accel table is just empty
167 return NULL;
168 }
169
222ed1d6 170 wxAccelList::compatibility_iterator node = M_ACCELDATA->m_accels.GetFirst();
b90c32b4
VZ
171 while ( node )
172 {
173 const wxAcceleratorEntry *entry = node->GetData();
174
175 // is the key the same?
176 if ( event.m_keyCode == entry->GetKeyCode() )
177 {
178 int flags = entry->GetFlags();
179
180 // now check flags
181 if ( (((flags & wxACCEL_CTRL) != 0) == event.ControlDown()) &&
182 (((flags & wxACCEL_SHIFT) != 0) == event.ShiftDown()) &&
cea43339 183 (((flags & wxACCEL_ALT) != 0) == event.AltDown()) )
b90c32b4
VZ
184 {
185 return entry;
186 }
187 }
188
189 node = node->GetNext();
190 }
191
192 return NULL;
193}
194
195wxMenuItem *wxAcceleratorTable::GetMenuItem(const wxKeyEvent& event) const
196{
197 const wxAcceleratorEntry *entry = GetEntry(event);
198
199 return entry ? entry->GetMenuItem() : NULL;
200}
201
202int wxAcceleratorTable::GetCommand(const wxKeyEvent& event) const
203{
204 const wxAcceleratorEntry *entry = GetEntry(event);
205
206 return entry ? entry->GetCommand() : -1;
207}
208
fa03793d
VS
209wxObjectRefData *wxAcceleratorTable::CreateRefData() const
210{
211 return new wxAccelRefData;
212}
213
214wxObjectRefData *wxAcceleratorTable::CloneRefData(const wxObjectRefData *data) const
215{
216 return new wxAccelRefData(*(wxAccelRefData *)data);
217}
218
b90c32b4 219#endif // wxUSE_ACCEL