1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxAcceleratorTable
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma implementation "accel.h"
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/window.h"
39 #include "wx/msw/private.h"
41 extern WXWORD
wxCharCodeWXToMSW(int id
, bool *isVirtual
);
43 IMPLEMENT_DYNAMIC_CLASS(wxAcceleratorTable
, wxObject
)
45 // ----------------------------------------------------------------------------
46 // data defining wxAcceleratorTable
47 // ----------------------------------------------------------------------------
49 class WXDLLEXPORT wxAcceleratorRefData
: public wxObjectRefData
51 friend class WXDLLEXPORT wxAcceleratorTable
;
53 wxAcceleratorRefData();
54 ~wxAcceleratorRefData();
56 inline HACCEL
GetHACCEL() const { return m_hAccel
; }
61 DECLARE_NO_COPY_CLASS(wxAcceleratorRefData
)
64 // ============================================================================
66 // ============================================================================
68 // ----------------------------------------------------------------------------
69 // wxAcceleratorRefData
70 // ----------------------------------------------------------------------------
72 #define M_ACCELDATA ((wxAcceleratorRefData *)m_refData)
74 wxAcceleratorRefData::wxAcceleratorRefData()
80 wxAcceleratorRefData::~wxAcceleratorRefData()
84 DestroyAcceleratorTable((HACCEL
) m_hAccel
);
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
92 // Load from .rc resource
93 wxAcceleratorTable::wxAcceleratorTable(const wxString
& resource
)
95 m_refData
= new wxAcceleratorRefData
;
97 HACCEL hAccel
= ::LoadAccelerators(wxGetInstance(), resource
);
98 M_ACCELDATA
->m_hAccel
= hAccel
;
99 M_ACCELDATA
->m_ok
= hAccel
!= 0;
102 // Create from an array
103 wxAcceleratorTable::wxAcceleratorTable(int n
, const wxAcceleratorEntry entries
[])
105 m_refData
= new wxAcceleratorRefData
;
107 ACCEL
* arr
= new ACCEL
[n
];
108 for ( int i
= 0; i
< n
; i
++ )
110 int flags
= entries
[i
].GetFlags();
113 if ( flags
& wxACCEL_ALT
)
114 fVirt
|= FALT
| FVIRTKEY
;
115 if ( flags
& wxACCEL_SHIFT
)
116 fVirt
|= FSHIFT
| FVIRTKEY
;
117 if ( flags
& wxACCEL_CTRL
)
118 fVirt
|= FCONTROL
| FVIRTKEY
;
122 WORD key
= wxCharCodeWXToMSW(entries
[i
].GetKeyCode(), &isVirtual
);
126 arr
[i
].fVirt
= fVirt
;
128 arr
[i
].cmd
= (WORD
)entries
[i
].GetCommand();
131 M_ACCELDATA
->m_hAccel
= ::CreateAcceleratorTable(arr
, n
);
134 M_ACCELDATA
->m_ok
= (M_ACCELDATA
->m_hAccel
!= 0);
137 bool wxAcceleratorTable::operator==(const wxAcceleratorTable
& accel
) const
139 const wxAcceleratorRefData
*
140 accelData
= (wxAcceleratorRefData
*)accel
.m_refData
;
142 return m_refData
? (accelData
&&
143 M_ACCELDATA
->m_hAccel
== accelData
->m_hAccel
)
147 bool wxAcceleratorTable::Ok() const
149 return (M_ACCELDATA
&& (M_ACCELDATA
->m_ok
));
152 void wxAcceleratorTable::SetHACCEL(WXHACCEL hAccel
)
155 m_refData
= new wxAcceleratorRefData
;
157 M_ACCELDATA
->m_hAccel
= (HACCEL
) hAccel
;
160 WXHACCEL
wxAcceleratorTable::GetHACCEL() const
164 return (WXHACCEL
) M_ACCELDATA
->m_hAccel
;
167 bool wxAcceleratorTable::Translate(wxWindow
*window
, WXMSG
*wxmsg
) const
169 MSG
*msg
= (MSG
*)wxmsg
;
170 return Ok() && ::TranslateAccelerator(GetHwndOf(window
), GetHaccel(), msg
);
173 #endif // wxUSE_ACCEL