Menu corruption fix
[wxWidgets.git] / src / os2 / accel.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: accel.cpp
3 // Purpose: wxAcceleratorTable
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/13/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifndef WX_PRECOMP
16 #include <stdio.h>
17 #include "wx/setup.h"
18 #include "wx/window.h"
19 #include "wx/app.h"
20 #include "wx/frame.h"
21 #endif
22
23 #include "wx/os2/accel.h"
24
25 #include "wx/os2/private.h"
26
27
28 IMPLEMENT_DYNAMIC_CLASS(wxAcceleratorTable, wxObject)
29
30 class WXDLLEXPORT wxAcceleratorRefData: public wxObjectRefData
31 {
32 friend class WXDLLEXPORT wxAcceleratorTable;
33 public:
34 wxAcceleratorRefData();
35 ~wxAcceleratorRefData();
36
37 inline HACCEL GetHACCEL() const { return m_hAccel; }
38 protected:
39 HACCEL m_hAccel;
40 bool m_ok;
41 };
42
43 #define M_ACCELDATA ((wxAcceleratorRefData *)m_refData)
44
45 wxAcceleratorRefData::wxAcceleratorRefData()
46 {
47 m_ok = FALSE;
48 m_hAccel = 0;
49 } // end of wxAcceleratorRefData::wxAcceleratorRefData
50
51 wxAcceleratorRefData::~wxAcceleratorRefData()
52 {
53 if (m_hAccel)
54 {
55 WinDestroyAccelTable((HACCEL) m_hAccel);
56 }
57 m_hAccel = 0 ;
58 } // end of wxAcceleratorRefData::~wxAcceleratorRefData
59
60 wxAcceleratorTable::wxAcceleratorTable()
61 {
62 m_refData = NULL;
63 } // end of wxAcceleratorTable::wxAcceleratorTable
64
65 wxAcceleratorTable::~wxAcceleratorTable()
66 {
67 } // end of wxAcceleratorTable::~wxAcceleratorTable
68
69 // Load from .rc resource
70 wxAcceleratorTable::wxAcceleratorTable(
71 const wxString& rResource
72 )
73 {
74 HACCEL hAccel;
75 ULONG ulId;
76
77 m_refData = new wxAcceleratorRefData;
78
79 ulId = atol((char*)rResource.c_str());
80 hAccel = ::WinLoadAccelTable( vHabmain
81 ,NULL // resources always in .exe
82 ,(ULONG)ulId
83 );
84 if (wxTheApp->GetTopWindow() != NULL)
85 {
86 //
87 // If we have accelerators the top window is the frame
88 //
89 wxFrame* pFrame = (wxFrame*)wxTheApp->GetTopWindow();
90
91 ::WinSetAccelTable( vHabmain
92 ,hAccel
93 ,(HWND)pFrame->GetFrame()
94 );
95 }
96 M_ACCELDATA->m_hAccel = hAccel;
97 M_ACCELDATA->m_ok = (hAccel != 0);
98 }
99
100 extern int wxCharCodeWXToOS2(
101 int nId
102 , bool* pbIsVirtual
103 );
104
105 // Create from an array
106 wxAcceleratorTable::wxAcceleratorTable(
107 int n
108 , const wxAcceleratorEntry vaEntries[]
109 )
110 {
111 int nAccelLength = ((sizeof(ACCEL) * n) + sizeof(ACCELTABLE));
112 PACCELTABLE pArr;
113 int i;
114
115 m_refData = new wxAcceleratorRefData;
116 pArr = (PACCELTABLE) new BYTE[nAccelLength];
117
118 for (i = 0; i < n; i++)
119 {
120 USHORT uVirt = AF_CHAR;
121
122 if (vaEntries[i].GetFlags() & wxACCEL_ALT)
123 {
124 uVirt |= AF_ALT;
125 uVirt |= AF_VIRTUALKEY;
126 }
127 if (vaEntries[i].GetFlags() & wxACCEL_SHIFT)
128 {
129 uVirt |= AF_SHIFT;
130 uVirt |= AF_VIRTUALKEY;
131 }
132 if (vaEntries[i].GetFlags() & wxACCEL_CTRL)
133 {
134 uVirt |= AF_CONTROL;
135 uVirt |= AF_VIRTUALKEY;
136 }
137
138 bool bIsVirtual;
139 USHORT uKey = wxCharCodeWXToOS2( vaEntries[i].GetKeyCode()
140 ,&bIsVirtual
141 );
142 if (bIsVirtual)
143 uVirt = AF_CHAR | AF_VIRTUALKEY;
144
145 USHORT uCmd = vaEntries[i].GetCommand();
146
147 pArr->aaccel[i].fs = uVirt;
148 pArr->aaccel[i].key = uKey;
149 pArr->aaccel[i].cmd = uCmd;
150 }
151 pArr->codepage = 437;
152 pArr->cAccel = (USHORT)n;
153 M_ACCELDATA->m_hAccel = ::WinCreateAccelTable( vHabmain
154 ,pArr
155 );
156 if (wxTheApp->GetTopWindow() != NULL)
157 {
158 //
159 // If we have accelerators the top window is the frame
160 //
161 wxFrame* pFrame = (wxFrame*)wxTheApp->GetTopWindow();
162
163 ::WinSetAccelTable( vHabmain
164 ,M_ACCELDATA->m_hAccel
165 ,(HWND)pFrame->GetFrame()
166 );
167 }
168
169 delete[] pArr;
170 M_ACCELDATA->m_ok = (M_ACCELDATA->m_hAccel != 0);
171 } // end of wxAcceleratorTable::wxAcceleratorTable
172
173 bool wxAcceleratorTable::Ok() const
174 {
175 return(M_ACCELDATA && (M_ACCELDATA->m_ok));
176 } // end of wxAcceleratorTable::Ok
177
178 void wxAcceleratorTable::SetHACCEL(WXHACCEL hAccel)
179 {
180 if (!M_ACCELDATA)
181 m_refData = new wxAcceleratorRefData;
182
183 M_ACCELDATA->m_hAccel = (HACCEL) hAccel;
184 }
185
186 WXHACCEL wxAcceleratorTable::GetHACCEL() const
187 {
188 if (!M_ACCELDATA)
189 return 0;
190 return (WXHACCEL) M_ACCELDATA->m_hAccel;
191 }
192
193 bool wxAcceleratorTable::Translate(
194 WXHWND hWnd
195 , WXMSG* pWxmsg
196 ) const
197 {
198 PQMSG pMsg = (PQMSG)pWxmsg;
199 BOOL rc = FALSE;
200
201 rc = ::WinTranslateAccel( vHabmain
202 ,(HWND)hWnd
203 ,GetHaccel()
204 ,pMsg
205 );
206 if (rc)
207 {
208 int x = 1;
209 }
210 return (Ok() && rc);
211 } // end of wxAcceleratorTable::Translate
212