]> git.saurik.com Git - wxWidgets.git/blame - src/generic/accel.cpp
Added wxPaintDCEx class, to handle the case where an HDC
[wxWidgets.git] / src / generic / accel.cpp
CommitLineData
b90c32b4
VZ
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
45WX_DECLARE_LIST(wxAcceleratorEntry, wxAccelList);
46#include "wx/listimpl.cpp"
47WX_DEFINE_LIST(wxAccelList);
48
49// ----------------------------------------------------------------------------
50// wxAccelRefData: the data used by wxAcceleratorTable
51// ----------------------------------------------------------------------------
52
53class wxAccelRefData : public wxObjectRefData
54{
55public:
fa03793d
VS
56 wxAccelRefData()
57 {
fa03793d
VS
58 }
59
60 wxAccelRefData(const wxAccelRefData& data)
d84afea9 61 : wxObjectRefData()
fa03793d 62 {
fa03793d
VS
63 m_accels = data.m_accels;
64 }
b90c32b4 65
222ed1d6
MB
66 virtual ~wxAccelRefData()
67 {
68 WX_CLEAR_LIST(wxAccelList, m_accels);
69 }
70
b90c32b4
VZ
71 wxAccelList m_accels;
72};
73
74// macro which can be used to access wxAccelRefData from wxAcceleratorTable
75#define M_ACCELDATA ((wxAccelRefData *)m_refData)
76
fa03793d 77
b90c32b4
VZ
78// ============================================================================
79// implementation
80// ============================================================================
81
82// ----------------------------------------------------------------------------
83// wxAcceleratorTable ctors
84// ----------------------------------------------------------------------------
85
86IMPLEMENT_DYNAMIC_CLASS(wxAcceleratorTable, wxObject)
87
88wxAcceleratorTable::wxAcceleratorTable()
89{
90}
91
3c674514 92wxAcceleratorTable::wxAcceleratorTable(int n, const wxAcceleratorEntry entries[])
b90c32b4
VZ
93{
94 m_refData = new wxAccelRefData;
95
96 for ( int i = 0; i < n; i++ )
97 {
98 const wxAcceleratorEntry& entry = entries[i];
99
100 int keycode = entry.GetKeyCode();
101 if ( wxIslower(keycode) )
102 keycode = wxToupper(keycode);
103
104 M_ACCELDATA->m_accels.Append(new wxAcceleratorEntry(entry.GetFlags(),
105 keycode,
106 entry.GetCommand()));
107 }
108}
109
110wxAcceleratorTable::~wxAcceleratorTable()
111{
112}
113
114bool wxAcceleratorTable::Ok() const
115{
116 return m_refData != NULL;
117}
118
119// ----------------------------------------------------------------------------
120// wxAcceleratorTable updating
121// ----------------------------------------------------------------------------
122
123void wxAcceleratorTable::Add(const wxAcceleratorEntry& entry)
124{
fa03793d
VS
125 AllocExclusive();
126
b90c32b4
VZ
127 if ( !m_refData )
128 {
129 m_refData = new wxAccelRefData;
130 }
131
132 M_ACCELDATA->m_accels.Append(new wxAcceleratorEntry(entry));
133}
134
135void wxAcceleratorTable::Remove(const wxAcceleratorEntry& entry)
136{
fa03793d
VS
137 AllocExclusive();
138
222ed1d6 139 wxAccelList::compatibility_iterator node = M_ACCELDATA->m_accels.GetFirst();
b90c32b4
VZ
140 while ( node )
141 {
142 const wxAcceleratorEntry *entryCur = node->GetData();
143
144 if ( *entryCur == entry )
145 {
222ed1d6
MB
146 delete node->GetData();
147 M_ACCELDATA->m_accels.Erase(node);
b90c32b4
VZ
148
149 return;
150 }
151
152 node = node->GetNext();
153 }
154
155 wxFAIL_MSG(_T("deleting inexistent accel from wxAcceleratorTable"));
156}
157
158// ----------------------------------------------------------------------------
159// wxAcceleratorTable: find a command for the given key press
160// ----------------------------------------------------------------------------
161
162const wxAcceleratorEntry *
163wxAcceleratorTable::GetEntry(const wxKeyEvent& event) const
164{
165 if ( !Ok() )
166 {
167 // not an error, the accel table is just empty
168 return NULL;
169 }
170
222ed1d6 171 wxAccelList::compatibility_iterator node = M_ACCELDATA->m_accels.GetFirst();
b90c32b4
VZ
172 while ( node )
173 {
174 const wxAcceleratorEntry *entry = node->GetData();
175
176 // is the key the same?
177 if ( event.m_keyCode == entry->GetKeyCode() )
178 {
179 int flags = entry->GetFlags();
180
181 // now check flags
182 if ( (((flags & wxACCEL_CTRL) != 0) == event.ControlDown()) &&
183 (((flags & wxACCEL_SHIFT) != 0) == event.ShiftDown()) &&
184 (((flags & wxACCEL_ALT) != 0) ==
185 (event.AltDown() || event.MetaDown())) )
186 {
187 return entry;
188 }
189 }
190
191 node = node->GetNext();
192 }
193
194 return NULL;
195}
196
197wxMenuItem *wxAcceleratorTable::GetMenuItem(const wxKeyEvent& event) const
198{
199 const wxAcceleratorEntry *entry = GetEntry(event);
200
201 return entry ? entry->GetMenuItem() : NULL;
202}
203
204int wxAcceleratorTable::GetCommand(const wxKeyEvent& event) const
205{
206 const wxAcceleratorEntry *entry = GetEntry(event);
207
208 return entry ? entry->GetCommand() : -1;
209}
210
fa03793d
VS
211wxObjectRefData *wxAcceleratorTable::CreateRefData() const
212{
213 return new wxAccelRefData;
214}
215
216wxObjectRefData *wxAcceleratorTable::CloneRefData(const wxObjectRefData *data) const
217{
218 return new wxAccelRefData(*(wxAccelRefData *)data);
219}
220
b90c32b4
VZ
221#endif // wxUSE_ACCEL
222