]> git.saurik.com Git - wxWidgets.git/blob - src/msw/accel.cpp
fixes for wxBase RPM
[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 "wx/window.h"
25 #endif
26
27 #include "wx/accel.h"
28
29 #include "wx/msw/private.h"
30
31 IMPLEMENT_DYNAMIC_CLASS(wxAcceleratorTable, wxObject)
32
33 class WXDLLEXPORT wxAcceleratorRefData: public wxObjectRefData
34 {
35 friend class WXDLLEXPORT wxAcceleratorTable;
36 public:
37 wxAcceleratorRefData();
38 ~wxAcceleratorRefData();
39
40 inline HACCEL GetHACCEL() const { return m_hAccel; }
41 protected:
42 HACCEL m_hAccel;
43 bool m_ok;
44 };
45
46 #define M_ACCELDATA ((wxAcceleratorRefData *)m_refData)
47
48 wxAcceleratorRefData::wxAcceleratorRefData()
49 {
50 m_ok = FALSE;
51 m_hAccel = 0;
52 }
53
54 wxAcceleratorRefData::~wxAcceleratorRefData()
55 {
56 if (m_hAccel)
57 {
58 // This function not available in WIN16
59 #if !defined(__WIN16__) && !defined(__TWIN32__)
60 DestroyAcceleratorTable((HACCEL) m_hAccel);
61 #endif
62 }
63 m_hAccel = 0 ;
64 }
65
66 wxAcceleratorTable::wxAcceleratorTable()
67 {
68 m_refData = NULL;
69 }
70
71 wxAcceleratorTable::~wxAcceleratorTable()
72 {
73 }
74
75 // Load from .rc resource
76 wxAcceleratorTable::wxAcceleratorTable(const wxString& resource)
77 {
78 m_refData = new wxAcceleratorRefData;
79
80 HACCEL hAccel =
81 #if defined(__WIN32__) && !defined(__TWIN32__)
82 #ifdef UNICODE
83 ::LoadAcceleratorsW(wxGetInstance(), (const wxChar *)resource);
84 #else
85 ::LoadAcceleratorsA(wxGetInstance(), (const char *)resource);
86 #endif
87 #else
88 ::LoadAccelerators(wxGetInstance(), (const wxChar *)resource);
89 #endif
90 M_ACCELDATA->m_hAccel = hAccel;
91 M_ACCELDATA->m_ok = (hAccel != 0);
92 }
93
94 extern int wxCharCodeWXToMSW(int id, bool *isVirtual);
95
96 // Create from an array
97 #if !defined(__WIN16__) && !defined(__TWIN32__) && !defined(__WXWINE__)
98 wxAcceleratorTable::wxAcceleratorTable(int n, const wxAcceleratorEntry entries[])
99 {
100 // Not available in WIN16
101 m_refData = new wxAcceleratorRefData;
102
103 ACCEL* arr = new ACCEL[n];
104 for ( int i = 0; i < n; i++ )
105 {
106 int flags = entries[i].GetFlags();
107
108 BYTE fVirt = 0;
109 if ( flags & wxACCEL_ALT )
110 fVirt |= FALT | FVIRTKEY;
111 if ( flags & wxACCEL_SHIFT )
112 fVirt |= FSHIFT | FVIRTKEY;
113 if ( flags & wxACCEL_CTRL )
114 fVirt |= FCONTROL | FVIRTKEY;
115
116 bool isVirtual;
117
118 WORD key = wxCharCodeWXToMSW(entries[i].GetKeyCode(), &isVirtual);
119 if (isVirtual)
120 fVirt |= FVIRTKEY;
121
122 arr[i].fVirt = fVirt;
123 arr[i].key = key;
124 arr[i].cmd = entries[i].GetCommand();
125 }
126
127 M_ACCELDATA->m_hAccel = ::CreateAcceleratorTable(arr, n);
128 delete[] arr;
129
130 M_ACCELDATA->m_ok = (M_ACCELDATA->m_hAccel != 0);
131 }
132 #else // Win16
133 wxAcceleratorTable::wxAcceleratorTable(int WXUNUSED(n), const wxAcceleratorEntry WXUNUSED(entries)[])
134 {
135 // No, we simply gracefully degrade; we don't expect the
136 // developer to pepper their code with #ifdefs just for this.
137 // wxFAIL_MSG("not implemented");
138 }
139 #endif // Win32/16
140
141 bool wxAcceleratorTable::Ok() const
142 {
143 return (M_ACCELDATA && (M_ACCELDATA->m_ok));
144 }
145
146 void wxAcceleratorTable::SetHACCEL(WXHACCEL hAccel)
147 {
148 if (!M_ACCELDATA)
149 m_refData = new wxAcceleratorRefData;
150
151 M_ACCELDATA->m_hAccel = (HACCEL) hAccel;
152 }
153
154 WXHACCEL wxAcceleratorTable::GetHACCEL() const
155 {
156 if (!M_ACCELDATA)
157 return 0;
158 return (WXHACCEL) M_ACCELDATA->m_hAccel;
159 }
160
161 bool wxAcceleratorTable::Translate(wxWindow *window, WXMSG *wxmsg) const
162 {
163 MSG *msg = (MSG *)wxmsg;
164 return Ok() && ::TranslateAccelerator(GetHwndOf(window), GetHaccel(), msg);
165 }
166