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