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
58 m_accels
.DeleteContents(TRUE
);
61 wxAccelRefData(const wxAccelRefData
& data
)
64 m_accels
.DeleteContents(TRUE
);
65 m_accels
= data
.m_accels
;
71 // macro which can be used to access wxAccelRefData from wxAcceleratorTable
72 #define M_ACCELDATA ((wxAccelRefData *)m_refData)
75 // ============================================================================
77 // ============================================================================
79 // ----------------------------------------------------------------------------
80 // wxAcceleratorTable ctors
81 // ----------------------------------------------------------------------------
83 IMPLEMENT_DYNAMIC_CLASS(wxAcceleratorTable
, wxObject
)
85 wxAcceleratorTable::wxAcceleratorTable()
89 wxAcceleratorTable::wxAcceleratorTable(int n
, wxAcceleratorEntry entries
[])
91 m_refData
= new wxAccelRefData
;
93 for ( int i
= 0; i
< n
; i
++ )
95 const wxAcceleratorEntry
& entry
= entries
[i
];
97 int keycode
= entry
.GetKeyCode();
98 if ( wxIslower(keycode
) )
99 keycode
= wxToupper(keycode
);
101 M_ACCELDATA
->m_accels
.Append(new wxAcceleratorEntry(entry
.GetFlags(),
103 entry
.GetCommand()));
107 wxAcceleratorTable::~wxAcceleratorTable()
111 bool wxAcceleratorTable::Ok() const
113 return m_refData
!= NULL
;
116 // ----------------------------------------------------------------------------
117 // wxAcceleratorTable updating
118 // ----------------------------------------------------------------------------
120 void wxAcceleratorTable::Add(const wxAcceleratorEntry
& entry
)
126 m_refData
= new wxAccelRefData
;
129 M_ACCELDATA
->m_accels
.Append(new wxAcceleratorEntry(entry
));
132 void wxAcceleratorTable::Remove(const wxAcceleratorEntry
& entry
)
136 wxAccelList::Node
*node
= M_ACCELDATA
->m_accels
.GetFirst();
139 const wxAcceleratorEntry
*entryCur
= node
->GetData();
141 if ( *entryCur
== entry
)
143 M_ACCELDATA
->m_accels
.DeleteNode(node
);
148 node
= node
->GetNext();
151 wxFAIL_MSG(_T("deleting inexistent accel from wxAcceleratorTable"));
154 // ----------------------------------------------------------------------------
155 // wxAcceleratorTable: find a command for the given key press
156 // ----------------------------------------------------------------------------
158 const wxAcceleratorEntry
*
159 wxAcceleratorTable::GetEntry(const wxKeyEvent
& event
) const
163 // not an error, the accel table is just empty
167 wxAccelList::Node
*node
= M_ACCELDATA
->m_accels
.GetFirst();
170 const wxAcceleratorEntry
*entry
= node
->GetData();
172 // is the key the same?
173 if ( event
.m_keyCode
== entry
->GetKeyCode() )
175 int flags
= entry
->GetFlags();
178 if ( (((flags
& wxACCEL_CTRL
) != 0) == event
.ControlDown()) &&
179 (((flags
& wxACCEL_SHIFT
) != 0) == event
.ShiftDown()) &&
180 (((flags
& wxACCEL_ALT
) != 0) ==
181 (event
.AltDown() || event
.MetaDown())) )
187 node
= node
->GetNext();
193 wxMenuItem
*wxAcceleratorTable::GetMenuItem(const wxKeyEvent
& event
) const
195 const wxAcceleratorEntry
*entry
= GetEntry(event
);
197 return entry
? entry
->GetMenuItem() : NULL
;
200 int wxAcceleratorTable::GetCommand(const wxKeyEvent
& event
) const
202 const wxAcceleratorEntry
*entry
= GetEntry(event
);
204 return entry
? entry
->GetCommand() : -1;
207 wxObjectRefData
*wxAcceleratorTable::CreateRefData() const
209 return new wxAccelRefData
;
212 wxObjectRefData
*wxAcceleratorTable::CloneRefData(const wxObjectRefData
*data
) const
214 return new wxAccelRefData(*(wxAccelRefData
*)data
);
217 #endif // wxUSE_ACCEL