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