]> git.saurik.com Git - wxWidgets.git/blob - src/msw/accel.cpp
added missing cast for delete
[wxWidgets.git] / src / msw / accel.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: accel.cpp
3 // Purpose: wxAcceleratorTable
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "accel.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include <stdio.h>
25 #include "wx/setup.h"
26 #include "wx/window.h"
27 #endif
28
29 #include "wx/msw/accel.h"
30
31 #include "wx/msw/private.h"
32
33 IMPLEMENT_DYNAMIC_CLASS(wxAcceleratorTable, wxObject)
34
35 class WXDLLEXPORT wxAcceleratorRefData: public wxObjectRefData
36 {
37 friend class WXDLLEXPORT wxAcceleratorTable;
38 public:
39 wxAcceleratorRefData();
40 ~wxAcceleratorRefData();
41
42 inline HACCEL GetHACCEL() const { return m_hAccel; }
43 protected:
44 HACCEL m_hAccel;
45 bool m_ok;
46 };
47
48 #define M_ACCELDATA ((wxAcceleratorRefData *)m_refData)
49
50 wxAcceleratorRefData::wxAcceleratorRefData()
51 {
52 m_ok = FALSE;
53 m_hAccel = 0;
54 }
55
56 wxAcceleratorRefData::~wxAcceleratorRefData()
57 {
58 if (m_hAccel)
59 {
60 // This function not available in WIN16
61 #if !defined(__WIN16__) && !defined(__TWIN32__)
62 DestroyAcceleratorTable((HACCEL) m_hAccel);
63 #endif
64 }
65 m_hAccel = 0 ;
66 }
67
68 wxAcceleratorTable::wxAcceleratorTable()
69 {
70 m_refData = NULL;
71 }
72
73 wxAcceleratorTable::~wxAcceleratorTable()
74 {
75 }
76
77 // Load from .rc resource
78 wxAcceleratorTable::wxAcceleratorTable(const wxString& resource)
79 {
80 m_refData = new wxAcceleratorRefData;
81
82 HACCEL hAccel =
83 #if defined(__WIN32__) && !defined(__TWIN32__)
84 #ifdef UNICODE
85 ::LoadAcceleratorsW(wxGetInstance(), (const wxChar *)resource);
86 #else
87 ::LoadAcceleratorsA(wxGetInstance(), (const char *)resource);
88 #endif
89 #else
90 ::LoadAccelerators(wxGetInstance(), (const wxChar *)resource);
91 #endif
92 M_ACCELDATA->m_hAccel = hAccel;
93 M_ACCELDATA->m_ok = (hAccel != 0);
94 }
95
96 extern int wxCharCodeWXToMSW(int id, bool *isVirtual);
97
98 // Create from an array
99 #if !defined(__WIN16__) && !defined(__TWIN32__) && !defined(__WXWINE__)
100 wxAcceleratorTable::wxAcceleratorTable(int n, const wxAcceleratorEntry entries[])
101 {
102 // Not available in WIN16
103 m_refData = new wxAcceleratorRefData;
104
105 ACCEL* arr = new ACCEL[n];
106 int i;
107 for (i = 0; i < n; i++)
108 {
109 BYTE fVirt = 0;
110 if (entries[i].m_flags & wxACCEL_ALT)
111 {
112 fVirt |= FALT;
113 fVirt |= FVIRTKEY;
114 }
115 if (entries[i].m_flags & wxACCEL_SHIFT)
116 {
117 fVirt |= FSHIFT;
118 fVirt |= FVIRTKEY;
119 }
120 if (entries[i].m_flags & wxACCEL_CTRL)
121 {
122 fVirt |= FCONTROL;
123 fVirt |= FVIRTKEY;
124 }
125
126 bool isVirtual;
127 WORD key = wxCharCodeWXToMSW(entries[i].m_keyCode, & isVirtual);
128 if (isVirtual)
129 fVirt |= FVIRTKEY;
130
131 WORD cmd = entries[i].m_command;
132
133 arr[i].fVirt = fVirt;
134 arr[i].key = key;
135 arr[i].cmd = cmd;
136 }
137
138 M_ACCELDATA->m_hAccel = ::CreateAcceleratorTable(arr, n);
139 delete[] arr;
140
141 M_ACCELDATA->m_ok = (M_ACCELDATA->m_hAccel != 0);
142 }
143 #else // Win16
144 wxAcceleratorTable::wxAcceleratorTable(int WXUNUSED(n), const wxAcceleratorEntry WXUNUSED(entries)[])
145 {
146 // No, we simply gracefully degrade; we don't expect the
147 // developer to pepper their code with #ifdefs just for this.
148 // wxFAIL_MSG("not implemented");
149 }
150 #endif // Win32/16
151
152 bool wxAcceleratorTable::Ok() const
153 {
154 return (M_ACCELDATA && (M_ACCELDATA->m_ok));
155 }
156
157 void wxAcceleratorTable::SetHACCEL(WXHACCEL hAccel)
158 {
159 if (!M_ACCELDATA)
160 m_refData = new wxAcceleratorRefData;
161
162 M_ACCELDATA->m_hAccel = (HACCEL) hAccel;
163 }
164
165 WXHACCEL wxAcceleratorTable::GetHACCEL() const
166 {
167 if (!M_ACCELDATA)
168 return 0;
169 return (WXHACCEL) M_ACCELDATA->m_hAccel;
170 }
171
172 bool wxAcceleratorTable::Translate(wxWindow *window, WXMSG *wxmsg) const
173 {
174 MSG *msg = (MSG *)wxmsg;
175 return Ok() && ::TranslateAccelerator(GetHwndOf(window), GetHaccel(), msg);
176 }
177