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
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
20 #pragma implementation "accel.h"
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
41 // ----------------------------------------------------------------------------
42 // wxAccelList: a list of wxAcceleratorEntries
43 // ----------------------------------------------------------------------------
45 WX_DECLARE_LIST(wxAcceleratorEntry
, wxAccelList
);
46 #include "wx/listimpl.cpp"
47 WX_DEFINE_LIST(wxAccelList
);
49 // ----------------------------------------------------------------------------
50 // wxAccelRefData: the data used by wxAcceleratorTable
51 // ----------------------------------------------------------------------------
53 class wxAccelRefData
: public wxObjectRefData
56 wxAccelRefData() { m_accels
.DeleteContents(TRUE
); }
61 // macro which can be used to access wxAccelRefData from wxAcceleratorTable
62 #define M_ACCELDATA ((wxAccelRefData *)m_refData)
64 // ============================================================================
66 // ============================================================================
68 // ----------------------------------------------------------------------------
69 // wxAcceleratorTable ctors
70 // ----------------------------------------------------------------------------
72 IMPLEMENT_DYNAMIC_CLASS(wxAcceleratorTable
, wxObject
)
74 wxAcceleratorTable::wxAcceleratorTable()
78 wxAcceleratorTable::wxAcceleratorTable(int n
, wxAcceleratorEntry entries
[])
80 m_refData
= new wxAccelRefData
;
82 for ( int i
= 0; i
< n
; i
++ )
84 const wxAcceleratorEntry
& entry
= entries
[i
];
86 int keycode
= entry
.GetKeyCode();
87 if ( wxIslower(keycode
) )
88 keycode
= wxToupper(keycode
);
90 M_ACCELDATA
->m_accels
.Append(new wxAcceleratorEntry(entry
.GetFlags(),
96 wxAcceleratorTable::~wxAcceleratorTable()
100 bool wxAcceleratorTable::Ok() const
102 return m_refData
!= NULL
;
105 // ----------------------------------------------------------------------------
106 // wxAcceleratorTable updating
107 // ----------------------------------------------------------------------------
109 void wxAcceleratorTable::Add(const wxAcceleratorEntry
& entry
)
113 m_refData
= new wxAccelRefData
;
116 M_ACCELDATA
->m_accels
.Append(new wxAcceleratorEntry(entry
));
119 void wxAcceleratorTable::Remove(const wxAcceleratorEntry
& entry
)
121 wxAccelList::Node
*node
= M_ACCELDATA
->m_accels
.GetFirst();
124 const wxAcceleratorEntry
*entryCur
= node
->GetData();
126 if ( *entryCur
== entry
)
128 M_ACCELDATA
->m_accels
.DeleteNode(node
);
133 node
= node
->GetNext();
136 wxFAIL_MSG(_T("deleting inexistent accel from wxAcceleratorTable"));
139 // ----------------------------------------------------------------------------
140 // wxAcceleratorTable: find a command for the given key press
141 // ----------------------------------------------------------------------------
143 const wxAcceleratorEntry
*
144 wxAcceleratorTable::GetEntry(const wxKeyEvent
& event
) const
148 // not an error, the accel table is just empty
152 wxAccelList::Node
*node
= M_ACCELDATA
->m_accels
.GetFirst();
155 const wxAcceleratorEntry
*entry
= node
->GetData();
157 // is the key the same?
158 if ( event
.m_keyCode
== entry
->GetKeyCode() )
160 int flags
= entry
->GetFlags();
163 if ( (((flags
& wxACCEL_CTRL
) != 0) == event
.ControlDown()) &&
164 (((flags
& wxACCEL_SHIFT
) != 0) == event
.ShiftDown()) &&
165 (((flags
& wxACCEL_ALT
) != 0) ==
166 (event
.AltDown() || event
.MetaDown())) )
172 node
= node
->GetNext();
178 wxMenuItem
*wxAcceleratorTable::GetMenuItem(const wxKeyEvent
& event
) const
180 const wxAcceleratorEntry
*entry
= GetEntry(event
);
182 return entry
? entry
->GetMenuItem() : NULL
;
185 int wxAcceleratorTable::GetCommand(const wxKeyEvent
& event
) const
187 const wxAcceleratorEntry
*entry
= GetEntry(event
);
189 return entry
? entry
->GetCommand() : -1;
192 #endif // wxUSE_ACCEL