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
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
37 // ----------------------------------------------------------------------------
38 // wxAccelList: a list of wxAcceleratorEntries
39 // ----------------------------------------------------------------------------
41 WX_DECLARE_LIST(wxAcceleratorEntry
, wxAccelList
);
42 #include "wx/listimpl.cpp"
43 WX_DEFINE_LIST(wxAccelList
)
45 // ----------------------------------------------------------------------------
46 // wxAccelRefData: the data used by wxAcceleratorTable
47 // ----------------------------------------------------------------------------
49 class wxAccelRefData
: public wxObjectRefData
56 wxAccelRefData(const wxAccelRefData
& data
)
59 m_accels
= data
.m_accels
;
62 virtual ~wxAccelRefData()
64 WX_CLEAR_LIST(wxAccelList
, m_accels
);
70 // macro which can be used to access wxAccelRefData from wxAcceleratorTable
71 #define M_ACCELDATA ((wxAccelRefData *)m_refData)
74 // ============================================================================
76 // ============================================================================
78 // ----------------------------------------------------------------------------
79 // wxAcceleratorTable ctors
80 // ----------------------------------------------------------------------------
82 IMPLEMENT_DYNAMIC_CLASS(wxAcceleratorTable
, wxObject
)
84 wxAcceleratorTable::wxAcceleratorTable()
88 wxAcceleratorTable::wxAcceleratorTable(int n
, const wxAcceleratorEntry entries
[])
90 m_refData
= new wxAccelRefData
;
92 for ( int i
= 0; i
< n
; i
++ )
94 const wxAcceleratorEntry
& entry
= entries
[i
];
96 int keycode
= entry
.GetKeyCode();
97 if ( isascii(keycode
) )
98 keycode
= toupper(keycode
);
100 M_ACCELDATA
->m_accels
.Append(new wxAcceleratorEntry(entry
.GetFlags(),
102 entry
.GetCommand()));
106 wxAcceleratorTable::~wxAcceleratorTable()
110 bool wxAcceleratorTable::Ok() const
112 return m_refData
!= NULL
;
115 // ----------------------------------------------------------------------------
116 // wxAcceleratorTable updating
117 // ----------------------------------------------------------------------------
119 void wxAcceleratorTable::Add(const wxAcceleratorEntry
& entry
)
125 m_refData
= new wxAccelRefData
;
128 M_ACCELDATA
->m_accels
.Append(new wxAcceleratorEntry(entry
));
131 void wxAcceleratorTable::Remove(const wxAcceleratorEntry
& entry
)
135 wxAccelList::compatibility_iterator node
= M_ACCELDATA
->m_accels
.GetFirst();
138 const wxAcceleratorEntry
*entryCur
= node
->GetData();
140 // given entry contains only the information of the accelerator key
141 // because it was set that way in wxGetAccelFromString()
142 // so do not perform full ( *entryCur == entry ) comparison
143 if ((entryCur
->GetKeyCode() == entry
.GetKeyCode()) &&
144 (entryCur
->GetFlags() == entry
.GetFlags()))
146 delete node
->GetData();
147 M_ACCELDATA
->m_accels
.Erase(node
);
152 node
= node
->GetNext();
155 wxFAIL_MSG(_T("deleting inexistent accel from wxAcceleratorTable"));
158 // ----------------------------------------------------------------------------
159 // wxAcceleratorTable: find a command for the given key press
160 // ----------------------------------------------------------------------------
162 const wxAcceleratorEntry
*
163 wxAcceleratorTable::GetEntry(const wxKeyEvent
& event
) const
167 // not an error, the accel table is just empty
171 wxAccelList::compatibility_iterator node
= M_ACCELDATA
->m_accels
.GetFirst();
174 const wxAcceleratorEntry
*entry
= node
->GetData();
176 // is the key the same?
177 if ( event
.m_keyCode
== entry
->GetKeyCode() )
179 int flags
= entry
->GetFlags();
182 if ( (((flags
& wxACCEL_CTRL
) != 0) == event
.ControlDown()) &&
183 (((flags
& wxACCEL_SHIFT
) != 0) == event
.ShiftDown()) &&
184 (((flags
& wxACCEL_ALT
) != 0) == event
.AltDown()) )
190 node
= node
->GetNext();
196 wxMenuItem
*wxAcceleratorTable::GetMenuItem(const wxKeyEvent
& event
) const
198 const wxAcceleratorEntry
*entry
= GetEntry(event
);
200 return entry
? entry
->GetMenuItem() : NULL
;
203 int wxAcceleratorTable::GetCommand(const wxKeyEvent
& event
) const
205 const wxAcceleratorEntry
*entry
= GetEntry(event
);
207 return entry
? entry
->GetCommand() : -1;
210 wxObjectRefData
*wxAcceleratorTable::CreateRefData() const
212 return new wxAccelRefData
;
215 wxObjectRefData
*wxAcceleratorTable::CloneRefData(const wxObjectRefData
*data
) const
217 return new wxAccelRefData(*(wxAccelRefData
*)data
);
220 #endif // wxUSE_ACCEL