]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/accel.cpp
don't test for WINVER inside the library, we do all the tests at run-time, not compil...
[wxWidgets.git] / src / msw / accel.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: msw/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// ============================================================================
13// declarations
14// ============================================================================
15
16#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma implementation "accel.h"
18#endif
19
20// ----------------------------------------------------------------------------
21// headers
22// ----------------------------------------------------------------------------
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
31#if wxUSE_ACCEL
32
33#ifndef WX_PRECOMP
34 #include "wx/window.h"
35#endif
36
37#include "wx/accel.h"
38
39#include "wx/msw/private.h"
40
41extern WXWORD wxCharCodeWXToMSW(int id, bool *isVirtual);
42
43IMPLEMENT_DYNAMIC_CLASS(wxAcceleratorTable, wxObject)
44
45// ----------------------------------------------------------------------------
46// data defining wxAcceleratorTable
47// ----------------------------------------------------------------------------
48
49class WXDLLEXPORT wxAcceleratorRefData: public wxObjectRefData
50{
51 friend class WXDLLEXPORT wxAcceleratorTable;
52public:
53 wxAcceleratorRefData();
54 ~wxAcceleratorRefData();
55
56 inline HACCEL GetHACCEL() const { return m_hAccel; }
57protected:
58 HACCEL m_hAccel;
59 bool m_ok;
60
61 DECLARE_NO_COPY_CLASS(wxAcceleratorRefData)
62};
63
64// ============================================================================
65// implementation
66// ============================================================================
67
68// ----------------------------------------------------------------------------
69// wxAcceleratorRefData
70// ----------------------------------------------------------------------------
71
72#define M_ACCELDATA ((wxAcceleratorRefData *)m_refData)
73
74wxAcceleratorRefData::wxAcceleratorRefData()
75{
76 m_ok = false;
77 m_hAccel = 0;
78}
79
80wxAcceleratorRefData::~wxAcceleratorRefData()
81{
82 if (m_hAccel)
83 {
84 DestroyAcceleratorTable((HACCEL) m_hAccel);
85 }
86}
87
88// ----------------------------------------------------------------------------
89// wxAcceleratorTable
90// ----------------------------------------------------------------------------
91
92// Load from .rc resource
93wxAcceleratorTable::wxAcceleratorTable(const wxString& resource)
94{
95 m_refData = new wxAcceleratorRefData;
96
97 HACCEL hAccel = ::LoadAccelerators(wxGetInstance(), resource);
98 M_ACCELDATA->m_hAccel = hAccel;
99 M_ACCELDATA->m_ok = hAccel != 0;
100}
101
102// Create from an array
103wxAcceleratorTable::wxAcceleratorTable(int n, const wxAcceleratorEntry entries[])
104{
105 m_refData = new wxAcceleratorRefData;
106
107 ACCEL* arr = new ACCEL[n];
108 for ( int i = 0; i < n; i++ )
109 {
110 int flags = entries[i].GetFlags();
111
112 BYTE fVirt = 0;
113 if ( flags & wxACCEL_ALT )
114 fVirt |= FALT | FVIRTKEY;
115 if ( flags & wxACCEL_SHIFT )
116 fVirt |= FSHIFT | FVIRTKEY;
117 if ( flags & wxACCEL_CTRL )
118 fVirt |= FCONTROL | FVIRTKEY;
119
120 bool isVirtual;
121
122 WORD key = wxCharCodeWXToMSW(entries[i].GetKeyCode(), &isVirtual);
123 if (isVirtual)
124 fVirt |= FVIRTKEY;
125
126 arr[i].fVirt = fVirt;
127 arr[i].key = key;
128 arr[i].cmd = (WORD)entries[i].GetCommand();
129 }
130
131 M_ACCELDATA->m_hAccel = ::CreateAcceleratorTable(arr, n);
132 delete[] arr;
133
134 M_ACCELDATA->m_ok = (M_ACCELDATA->m_hAccel != 0);
135}
136
137bool wxAcceleratorTable::operator==(const wxAcceleratorTable& accel) const
138{
139 const wxAcceleratorRefData *
140 accelData = (wxAcceleratorRefData *)accel.m_refData;
141
142 return m_refData ? (accelData &&
143 M_ACCELDATA->m_hAccel == accelData->m_hAccel)
144 : !accelData;
145}
146
147bool wxAcceleratorTable::Ok() const
148{
149 return (M_ACCELDATA && (M_ACCELDATA->m_ok));
150}
151
152void wxAcceleratorTable::SetHACCEL(WXHACCEL hAccel)
153{
154 if (!M_ACCELDATA)
155 m_refData = new wxAcceleratorRefData;
156
157 M_ACCELDATA->m_hAccel = (HACCEL) hAccel;
158}
159
160WXHACCEL wxAcceleratorTable::GetHACCEL() const
161{
162 if (!M_ACCELDATA)
163 return 0;
164 return (WXHACCEL) M_ACCELDATA->m_hAccel;
165}
166
167bool wxAcceleratorTable::Translate(wxWindow *window, WXMSG *wxmsg) const
168{
169 MSG *msg = (MSG *)wxmsg;
170 return Ok() && ::TranslateAccelerator(GetHwndOf(window), GetHaccel(), msg);
171}
172
173#endif // wxUSE_ACCEL
174