]> git.saurik.com Git - wxWidgets.git/blob - src/msw/accel.cpp
Hack compilation using the latest MinGW release.
[wxWidgets.git] / src / msw / accel.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/accel.cpp
3 // Purpose: wxAcceleratorTable
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_ACCEL
27
28 #ifndef WX_PRECOMP
29 #include "wx/window.h"
30 #endif
31
32 #include "wx/accel.h"
33
34 #include "wx/msw/private.h"
35 #include "wx/msw/private/keyboard.h"
36
37 IMPLEMENT_DYNAMIC_CLASS(wxAcceleratorTable, wxObject)
38
39 // ----------------------------------------------------------------------------
40 // data defining wxAcceleratorTable
41 // ----------------------------------------------------------------------------
42
43 class WXDLLEXPORT wxAcceleratorRefData: public wxObjectRefData
44 {
45 friend class WXDLLIMPEXP_FWD_CORE wxAcceleratorTable;
46 public:
47 wxAcceleratorRefData();
48 virtual ~wxAcceleratorRefData();
49
50 inline HACCEL GetHACCEL() const { return m_hAccel; }
51 protected:
52 HACCEL m_hAccel;
53 bool m_ok;
54
55 wxDECLARE_NO_COPY_CLASS(wxAcceleratorRefData);
56 };
57
58 // ============================================================================
59 // implementation
60 // ============================================================================
61
62 // ----------------------------------------------------------------------------
63 // wxAcceleratorRefData
64 // ----------------------------------------------------------------------------
65
66 #define M_ACCELDATA ((wxAcceleratorRefData *)m_refData)
67
68 wxAcceleratorRefData::wxAcceleratorRefData()
69 {
70 m_ok = false;
71 m_hAccel = 0;
72 }
73
74 wxAcceleratorRefData::~wxAcceleratorRefData()
75 {
76 if (m_hAccel)
77 {
78 DestroyAcceleratorTable((HACCEL) m_hAccel);
79 }
80 }
81
82 // ----------------------------------------------------------------------------
83 // wxAcceleratorTable
84 // ----------------------------------------------------------------------------
85
86 // Load from .rc resource
87 wxAcceleratorTable::wxAcceleratorTable(const wxString& resource)
88 {
89 m_refData = new wxAcceleratorRefData;
90
91 HACCEL hAccel = ::LoadAccelerators(wxGetInstance(), resource.t_str());
92 M_ACCELDATA->m_hAccel = hAccel;
93 M_ACCELDATA->m_ok = hAccel != 0;
94 }
95
96 // Create from an array
97 wxAcceleratorTable::wxAcceleratorTable(int n, const wxAcceleratorEntry entries[])
98 {
99 m_refData = new wxAcceleratorRefData;
100
101 ACCEL* arr = new ACCEL[n];
102 for ( int i = 0; i < n; i++ )
103 {
104 int flags = entries[i].GetFlags();
105
106 BYTE fVirt = FVIRTKEY;
107 if ( flags & wxACCEL_ALT )
108 fVirt |= FALT;
109 if ( flags & wxACCEL_SHIFT )
110 fVirt |= FSHIFT;
111 if ( flags & wxACCEL_CTRL )
112 fVirt |= FCONTROL;
113
114 WORD key = wxMSWKeyboard::WXToVK(entries[i].GetKeyCode());
115
116 arr[i].fVirt = fVirt;
117 arr[i].key = key;
118 arr[i].cmd = (WORD)entries[i].GetCommand();
119 }
120
121 M_ACCELDATA->m_hAccel = ::CreateAcceleratorTable(arr, n);
122 delete[] arr;
123
124 M_ACCELDATA->m_ok = (M_ACCELDATA->m_hAccel != 0);
125 }
126
127 bool wxAcceleratorTable::IsOk() const
128 {
129 return (M_ACCELDATA && (M_ACCELDATA->m_ok));
130 }
131
132 void wxAcceleratorTable::SetHACCEL(WXHACCEL hAccel)
133 {
134 if (!M_ACCELDATA)
135 m_refData = new wxAcceleratorRefData;
136
137 M_ACCELDATA->m_hAccel = (HACCEL) hAccel;
138 }
139
140 WXHACCEL wxAcceleratorTable::GetHACCEL() const
141 {
142 if (!M_ACCELDATA)
143 return 0;
144 return (WXHACCEL) M_ACCELDATA->m_hAccel;
145 }
146
147 bool wxAcceleratorTable::Translate(wxWindow *window, WXMSG *wxmsg) const
148 {
149 MSG *msg = (MSG *)wxmsg;
150 return IsOk() && ::TranslateAccelerator(GetHwndOf(window), GetHaccel(), msg);
151 }
152
153 #endif // wxUSE_ACCEL
154