1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 // For compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
36 // ----------------------------------------------------------------------------
37 // wxAccelList: a list of wxAcceleratorEntries
38 // ----------------------------------------------------------------------------
40 WX_DECLARE_LIST(wxAcceleratorEntry
, wxAccelList
);
41 #include "wx/listimpl.cpp"
42 WX_DEFINE_LIST(wxAccelList
)
44 // ----------------------------------------------------------------------------
45 // wxAccelRefData: the data used by wxAcceleratorTable
46 // ----------------------------------------------------------------------------
48 class wxAccelRefData
: public wxObjectRefData
55 wxAccelRefData(const wxAccelRefData
& data
)
58 m_accels
= data
.m_accels
;
61 virtual ~wxAccelRefData()
63 WX_CLEAR_LIST(wxAccelList
, m_accels
);
69 // macro which can be used to access wxAccelRefData from wxAcceleratorTable
70 #define M_ACCELDATA ((wxAccelRefData *)m_refData)
73 // ============================================================================
75 // ============================================================================
77 // ----------------------------------------------------------------------------
78 // wxAcceleratorTable ctors
79 // ----------------------------------------------------------------------------
81 IMPLEMENT_DYNAMIC_CLASS(wxAcceleratorTable
, wxObject
)
83 wxAcceleratorTable::wxAcceleratorTable()
87 wxAcceleratorTable::wxAcceleratorTable(int n
, const wxAcceleratorEntry entries
[])
89 m_refData
= new wxAccelRefData
;
91 for ( int i
= 0; i
< n
; i
++ )
93 const wxAcceleratorEntry
& entry
= entries
[i
];
95 int keycode
= entry
.GetKeyCode();
96 if ( wxIsascii(keycode
) )
97 keycode
= wxToupper(keycode
);
99 M_ACCELDATA
->m_accels
.Append(new wxAcceleratorEntry(entry
.GetFlags(),
101 entry
.GetCommand()));
105 wxAcceleratorTable::~wxAcceleratorTable()
109 bool wxAcceleratorTable::IsOk() const
111 return m_refData
!= NULL
;
114 // ----------------------------------------------------------------------------
115 // wxAcceleratorTable updating
116 // ----------------------------------------------------------------------------
118 void wxAcceleratorTable::Add(const wxAcceleratorEntry
& entry
)
124 m_refData
= new wxAccelRefData
;
127 M_ACCELDATA
->m_accels
.Append(new wxAcceleratorEntry(entry
));
130 void wxAcceleratorTable::Remove(const wxAcceleratorEntry
& entry
)
134 wxAccelList::compatibility_iterator node
= M_ACCELDATA
->m_accels
.GetFirst();
137 const wxAcceleratorEntry
*entryCur
= node
->GetData();
139 // given entry contains only the information of the accelerator key
140 // because it was set that way during creation so do not use the
141 // comparison operator which also checks the command field
142 if ((entryCur
->GetKeyCode() == entry
.GetKeyCode()) &&
143 (entryCur
->GetFlags() == entry
.GetFlags()))
145 delete node
->GetData();
146 M_ACCELDATA
->m_accels
.Erase(node
);
151 node
= node
->GetNext();
154 wxFAIL_MSG(wxT("deleting inexistent accel from wxAcceleratorTable"));
157 // ----------------------------------------------------------------------------
158 // wxAcceleratorTable: find a command for the given key press
159 // ----------------------------------------------------------------------------
161 const wxAcceleratorEntry
*
162 wxAcceleratorTable::GetEntry(const wxKeyEvent
& event
) const
166 // not an error, the accel table is just empty
170 wxAccelList::compatibility_iterator node
= M_ACCELDATA
->m_accels
.GetFirst();
173 const wxAcceleratorEntry
*entry
= node
->GetData();
175 // is the key the same?
176 if ( event
.m_keyCode
== entry
->GetKeyCode() )
178 int flags
= entry
->GetFlags();
181 if ( (((flags
& wxACCEL_CTRL
) != 0) == event
.ControlDown()) &&
182 (((flags
& wxACCEL_SHIFT
) != 0) == event
.ShiftDown()) &&
183 (((flags
& wxACCEL_ALT
) != 0) == event
.AltDown()) )
189 node
= node
->GetNext();
195 wxMenuItem
*wxAcceleratorTable::GetMenuItem(const wxKeyEvent
& event
) const
197 const wxAcceleratorEntry
*entry
= GetEntry(event
);
199 return entry
? entry
->GetMenuItem() : NULL
;
202 int wxAcceleratorTable::GetCommand(const wxKeyEvent
& event
) const
204 const wxAcceleratorEntry
*entry
= GetEntry(event
);
206 return entry
? entry
->GetCommand() : -1;
209 wxObjectRefData
*wxAcceleratorTable::CreateRefData() const
211 return new wxAccelRefData
;
214 wxObjectRefData
*wxAcceleratorTable::CloneRefData(const wxObjectRefData
*data
) const
216 return new wxAccelRefData(*(wxAccelRefData
*)data
);
219 #endif // wxUSE_ACCEL