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 // ----------------------------------------------------------------------------
19 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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
60 wxAccelRefData(const wxAccelRefData
& data
)
63 m_accels
= data
.m_accels
;
66 virtual ~wxAccelRefData()
68 WX_CLEAR_LIST(wxAccelList
, m_accels
);
74 // macro which can be used to access wxAccelRefData from wxAcceleratorTable
75 #define M_ACCELDATA ((wxAccelRefData *)m_refData)
78 // ============================================================================
80 // ============================================================================
82 // ----------------------------------------------------------------------------
83 // wxAcceleratorTable ctors
84 // ----------------------------------------------------------------------------
86 IMPLEMENT_DYNAMIC_CLASS(wxAcceleratorTable
, wxObject
)
88 wxAcceleratorTable::wxAcceleratorTable()
92 wxAcceleratorTable::wxAcceleratorTable(int n
, const wxAcceleratorEntry entries
[])
94 m_refData
= new wxAccelRefData
;
96 for ( int i
= 0; i
< n
; i
++ )
98 const wxAcceleratorEntry
& entry
= entries
[i
];
100 int keycode
= wxToupper(entry
.GetKeyCode());
102 M_ACCELDATA
->m_accels
.Append(new wxAcceleratorEntry(entry
.GetFlags(),
104 entry
.GetCommand()));
108 wxAcceleratorTable::~wxAcceleratorTable()
112 bool wxAcceleratorTable::Ok() const
114 return m_refData
!= NULL
;
117 // ----------------------------------------------------------------------------
118 // wxAcceleratorTable updating
119 // ----------------------------------------------------------------------------
121 void wxAcceleratorTable::Add(const wxAcceleratorEntry
& entry
)
127 m_refData
= new wxAccelRefData
;
130 M_ACCELDATA
->m_accels
.Append(new wxAcceleratorEntry(entry
));
133 void wxAcceleratorTable::Remove(const wxAcceleratorEntry
& entry
)
137 wxAccelList::compatibility_iterator node
= M_ACCELDATA
->m_accels
.GetFirst();
140 const wxAcceleratorEntry
*entryCur
= node
->GetData();
142 // given entry contains only the information of the accelerator key
143 // because it was set that way in wxGetAccelFromString()
144 // so do not perform full ( *entryCur == entry ) comparison
145 if ((entryCur
->GetKeyCode() == entry
.GetKeyCode()) &&
146 (entryCur
->GetFlags() == entry
.GetFlags()))
148 delete node
->GetData();
149 M_ACCELDATA
->m_accels
.Erase(node
);
154 node
= node
->GetNext();
157 wxFAIL_MSG(_T("deleting inexistent accel from wxAcceleratorTable"));
160 // ----------------------------------------------------------------------------
161 // wxAcceleratorTable: find a command for the given key press
162 // ----------------------------------------------------------------------------
164 const wxAcceleratorEntry
*
165 wxAcceleratorTable::GetEntry(const wxKeyEvent
& event
) const
169 // not an error, the accel table is just empty
173 wxAccelList::compatibility_iterator node
= M_ACCELDATA
->m_accels
.GetFirst();
176 const wxAcceleratorEntry
*entry
= node
->GetData();
178 // is the key the same?
179 if ( event
.m_keyCode
== entry
->GetKeyCode() )
181 int flags
= entry
->GetFlags();
184 if ( (((flags
& wxACCEL_CTRL
) != 0) == event
.ControlDown()) &&
185 (((flags
& wxACCEL_SHIFT
) != 0) == event
.ShiftDown()) &&
186 (((flags
& wxACCEL_ALT
) != 0) == event
.AltDown()) )
192 node
= node
->GetNext();
198 wxMenuItem
*wxAcceleratorTable::GetMenuItem(const wxKeyEvent
& event
) const
200 const wxAcceleratorEntry
*entry
= GetEntry(event
);
202 return entry
? entry
->GetMenuItem() : NULL
;
205 int wxAcceleratorTable::GetCommand(const wxKeyEvent
& event
) const
207 const wxAcceleratorEntry
*entry
= GetEntry(event
);
209 return entry
? entry
->GetCommand() : -1;
212 wxObjectRefData
*wxAcceleratorTable::CreateRefData() const
214 return new wxAccelRefData
;
217 wxObjectRefData
*wxAcceleratorTable::CloneRefData(const wxObjectRefData
*data
) const
219 return new wxAccelRefData(*(wxAccelRefData
*)data
);
222 #endif // wxUSE_ACCEL