1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxAcceleratorTable
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
30 #include "wx/window.h"
35 #include "wx/msw/private.h"
37 extern WXWORD
wxCharCodeWXToMSW(int id
, bool *isVirtual
);
39 IMPLEMENT_DYNAMIC_CLASS(wxAcceleratorTable
, wxObject
)
41 // ----------------------------------------------------------------------------
42 // data defining wxAcceleratorTable
43 // ----------------------------------------------------------------------------
45 class WXDLLEXPORT wxAcceleratorRefData
: public wxObjectRefData
47 friend class WXDLLEXPORT wxAcceleratorTable
;
49 wxAcceleratorRefData();
50 ~wxAcceleratorRefData();
52 inline HACCEL
GetHACCEL() const { return m_hAccel
; }
57 DECLARE_NO_COPY_CLASS(wxAcceleratorRefData
)
60 // ============================================================================
62 // ============================================================================
64 // ----------------------------------------------------------------------------
65 // wxAcceleratorRefData
66 // ----------------------------------------------------------------------------
68 #define M_ACCELDATA ((wxAcceleratorRefData *)m_refData)
70 wxAcceleratorRefData::wxAcceleratorRefData()
76 wxAcceleratorRefData::~wxAcceleratorRefData()
80 DestroyAcceleratorTable((HACCEL
) m_hAccel
);
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 // Load from .rc resource
89 wxAcceleratorTable::wxAcceleratorTable(const wxString
& resource
)
91 m_refData
= new wxAcceleratorRefData
;
93 HACCEL hAccel
= ::LoadAccelerators(wxGetInstance(), resource
);
94 M_ACCELDATA
->m_hAccel
= hAccel
;
95 M_ACCELDATA
->m_ok
= hAccel
!= 0;
98 // Create from an array
99 wxAcceleratorTable::wxAcceleratorTable(int n
, const wxAcceleratorEntry entries
[])
101 m_refData
= new wxAcceleratorRefData
;
103 ACCEL
* arr
= new ACCEL
[n
];
104 for ( int i
= 0; i
< n
; i
++ )
106 int flags
= entries
[i
].GetFlags();
109 if ( flags
& wxACCEL_ALT
)
110 fVirt
|= FALT
| FVIRTKEY
;
111 if ( flags
& wxACCEL_SHIFT
)
112 fVirt
|= FSHIFT
| FVIRTKEY
;
113 if ( flags
& wxACCEL_CTRL
)
114 fVirt
|= FCONTROL
| FVIRTKEY
;
118 WORD key
= wxCharCodeWXToMSW(entries
[i
].GetKeyCode(), &isVirtual
);
122 arr
[i
].fVirt
= fVirt
;
124 arr
[i
].cmd
= (WORD
)entries
[i
].GetCommand();
127 M_ACCELDATA
->m_hAccel
= ::CreateAcceleratorTable(arr
, n
);
130 M_ACCELDATA
->m_ok
= (M_ACCELDATA
->m_hAccel
!= 0);
133 bool wxAcceleratorTable::operator==(const wxAcceleratorTable
& accel
) const
135 const wxAcceleratorRefData
*
136 accelData
= (wxAcceleratorRefData
*)accel
.m_refData
;
138 return m_refData
? (accelData
&&
139 M_ACCELDATA
->m_hAccel
== accelData
->m_hAccel
)
143 bool wxAcceleratorTable::Ok() const
145 return (M_ACCELDATA
&& (M_ACCELDATA
->m_ok
));
148 void wxAcceleratorTable::SetHACCEL(WXHACCEL hAccel
)
151 m_refData
= new wxAcceleratorRefData
;
153 M_ACCELDATA
->m_hAccel
= (HACCEL
) hAccel
;
156 WXHACCEL
wxAcceleratorTable::GetHACCEL() const
160 return (WXHACCEL
) M_ACCELDATA
->m_hAccel
;
163 bool wxAcceleratorTable::Translate(wxWindow
*window
, WXMSG
*wxmsg
) const
166 // calling TranslateAccelerator() with child window doesn't do anything so
167 // it's probably a bug
168 wxASSERT_MSG( window
->IsTopLevel(),
169 _T("TranslateAccelerator() needs a top level window") );
172 MSG
*msg
= (MSG
*)wxmsg
;
173 return Ok() && ::TranslateAccelerator(GetHwndOf(window
), GetHaccel(), msg
);
176 #endif // wxUSE_ACCEL