]> git.saurik.com Git - wxWidgets.git/blob - src/generic/accel.cpp
A couple demo tweaks
[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 #ifdef __GNUG__
20 #pragma implementation "accel.h"
21 #endif
22
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
25
26 #ifdef __BORLANDC__
27 #pragma hdrstop
28 #endif
29
30 #if wxUSE_ACCEL
31
32 #ifndef WX_PRECOMP
33 #include "wx/event.h"
34 #include "wx/list.h"
35 #endif // WX_PRECOMP
36
37 #include "wx/accel.h"
38
39 #include <ctype.h>
40
41 // ----------------------------------------------------------------------------
42 // wxAccelList: a list of wxAcceleratorEntries
43 // ----------------------------------------------------------------------------
44
45 WX_DECLARE_LIST(wxAcceleratorEntry, wxAccelList);
46 #include "wx/listimpl.cpp"
47 WX_DEFINE_LIST(wxAccelList);
48
49 // ----------------------------------------------------------------------------
50 // wxAccelRefData: the data used by wxAcceleratorTable
51 // ----------------------------------------------------------------------------
52
53 class wxAccelRefData : public wxObjectRefData
54 {
55 public:
56 wxAccelRefData()
57 {
58 m_accels.DeleteContents(TRUE);
59 }
60
61 wxAccelRefData(const wxAccelRefData& data)
62 {
63 m_accels.DeleteContents(TRUE);
64 m_accels = data.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, 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 = entry.GetKeyCode();
97 if ( wxIslower(keycode) )
98 keycode = wxToupper(keycode);
99
100 M_ACCELDATA->m_accels.Append(new wxAcceleratorEntry(entry.GetFlags(),
101 keycode,
102 entry.GetCommand()));
103 }
104 }
105
106 wxAcceleratorTable::~wxAcceleratorTable()
107 {
108 }
109
110 bool wxAcceleratorTable::Ok() const
111 {
112 return m_refData != NULL;
113 }
114
115 // ----------------------------------------------------------------------------
116 // wxAcceleratorTable updating
117 // ----------------------------------------------------------------------------
118
119 void wxAcceleratorTable::Add(const wxAcceleratorEntry& entry)
120 {
121 AllocExclusive();
122
123 if ( !m_refData )
124 {
125 m_refData = new wxAccelRefData;
126 }
127
128 M_ACCELDATA->m_accels.Append(new wxAcceleratorEntry(entry));
129 }
130
131 void wxAcceleratorTable::Remove(const wxAcceleratorEntry& entry)
132 {
133 AllocExclusive();
134
135 wxAccelList::Node *node = M_ACCELDATA->m_accels.GetFirst();
136 while ( node )
137 {
138 const wxAcceleratorEntry *entryCur = node->GetData();
139
140 if ( *entryCur == entry )
141 {
142 M_ACCELDATA->m_accels.DeleteNode(node);
143
144 return;
145 }
146
147 node = node->GetNext();
148 }
149
150 wxFAIL_MSG(_T("deleting inexistent accel from wxAcceleratorTable"));
151 }
152
153 // ----------------------------------------------------------------------------
154 // wxAcceleratorTable: find a command for the given key press
155 // ----------------------------------------------------------------------------
156
157 const wxAcceleratorEntry *
158 wxAcceleratorTable::GetEntry(const wxKeyEvent& event) const
159 {
160 if ( !Ok() )
161 {
162 // not an error, the accel table is just empty
163 return NULL;
164 }
165
166 wxAccelList::Node *node = M_ACCELDATA->m_accels.GetFirst();
167 while ( node )
168 {
169 const wxAcceleratorEntry *entry = node->GetData();
170
171 // is the key the same?
172 if ( event.m_keyCode == entry->GetKeyCode() )
173 {
174 int flags = entry->GetFlags();
175
176 // now check flags
177 if ( (((flags & wxACCEL_CTRL) != 0) == event.ControlDown()) &&
178 (((flags & wxACCEL_SHIFT) != 0) == event.ShiftDown()) &&
179 (((flags & wxACCEL_ALT) != 0) ==
180 (event.AltDown() || event.MetaDown())) )
181 {
182 return entry;
183 }
184 }
185
186 node = node->GetNext();
187 }
188
189 return NULL;
190 }
191
192 wxMenuItem *wxAcceleratorTable::GetMenuItem(const wxKeyEvent& event) const
193 {
194 const wxAcceleratorEntry *entry = GetEntry(event);
195
196 return entry ? entry->GetMenuItem() : NULL;
197 }
198
199 int wxAcceleratorTable::GetCommand(const wxKeyEvent& event) const
200 {
201 const wxAcceleratorEntry *entry = GetEntry(event);
202
203 return entry ? entry->GetCommand() : -1;
204 }
205
206 wxObjectRefData *wxAcceleratorTable::CreateRefData() const
207 {
208 return new wxAccelRefData;
209 }
210
211 wxObjectRefData *wxAcceleratorTable::CloneRefData(const wxObjectRefData *data) const
212 {
213 return new wxAccelRefData(*(wxAccelRefData *)data);
214 }
215
216 #endif // wxUSE_ACCEL
217