]> git.saurik.com Git - wxWidgets.git/blame - src/msw/accel.cpp
changed exceptions handling to work under wxGTK
[wxWidgets.git] / src / msw / accel.cpp
CommitLineData
110f3205 1/////////////////////////////////////////////////////////////////////////////
a21d4ad1 2// Name: msw/accel.cpp
110f3205
JS
3// Purpose: wxAcceleratorTable
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
110f3205
JS
10/////////////////////////////////////////////////////////////////////////////
11
a21d4ad1
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
14f355c2 16#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
a21d4ad1 17 #pragma implementation "accel.h"
110f3205
JS
18#endif
19
a21d4ad1
VZ
20// ----------------------------------------------------------------------------
21// headers
22// ----------------------------------------------------------------------------
23
110f3205
JS
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
a21d4ad1 28 #pragma hdrstop
110f3205
JS
29#endif
30
a21d4ad1
VZ
31#if wxUSE_ACCEL
32
110f3205 33#ifndef WX_PRECOMP
1e6feb95 34 #include "wx/window.h"
110f3205
JS
35#endif
36
1e6feb95 37#include "wx/accel.h"
110f3205 38
48d1144b
JS
39#include "wx/msw/private.h"
40
a21d4ad1
VZ
41extern WXWORD wxCharCodeWXToMSW(int id, bool *isVirtual);
42
110f3205 43IMPLEMENT_DYNAMIC_CLASS(wxAcceleratorTable, wxObject)
110f3205 44
a21d4ad1
VZ
45// ----------------------------------------------------------------------------
46// data defining wxAcceleratorTable
47// ----------------------------------------------------------------------------
48
110f3205
JS
49class WXDLLEXPORT wxAcceleratorRefData: public wxObjectRefData
50{
51 friend class WXDLLEXPORT wxAcceleratorTable;
52public:
c50f1fb9
VZ
53 wxAcceleratorRefData();
54 ~wxAcceleratorRefData();
110f3205
JS
55
56 inline HACCEL GetHACCEL() const { return m_hAccel; }
57protected:
58 HACCEL m_hAccel;
59 bool m_ok;
22f3361e
VZ
60
61 DECLARE_NO_COPY_CLASS(wxAcceleratorRefData)
110f3205
JS
62};
63
a21d4ad1
VZ
64// ============================================================================
65// implementation
66// ============================================================================
67
68// ----------------------------------------------------------------------------
69// wxAcceleratorRefData
70// ----------------------------------------------------------------------------
71
110f3205
JS
72#define M_ACCELDATA ((wxAcceleratorRefData *)m_refData)
73
74wxAcceleratorRefData::wxAcceleratorRefData()
75{
a21d4ad1
VZ
76 m_ok = false;
77 m_hAccel = 0;
110f3205
JS
78}
79
80wxAcceleratorRefData::~wxAcceleratorRefData()
81{
a21d4ad1
VZ
82 if (m_hAccel)
83 {
84 DestroyAcceleratorTable((HACCEL) m_hAccel);
85 }
110f3205
JS
86}
87
a21d4ad1
VZ
88// ----------------------------------------------------------------------------
89// wxAcceleratorTable
90// ----------------------------------------------------------------------------
110f3205
JS
91
92// Load from .rc resource
93wxAcceleratorTable::wxAcceleratorTable(const wxString& resource)
94{
95 m_refData = new wxAcceleratorRefData;
96
a21d4ad1 97 HACCEL hAccel = ::LoadAccelerators(wxGetInstance(), resource);
110f3205 98 M_ACCELDATA->m_hAccel = hAccel;
a21d4ad1 99 M_ACCELDATA->m_ok = hAccel != 0;
110f3205
JS
100}
101
110f3205 102// Create from an array
252eb8fd 103wxAcceleratorTable::wxAcceleratorTable(int n, const wxAcceleratorEntry entries[])
110f3205
JS
104{
105 m_refData = new wxAcceleratorRefData;
106
107 ACCEL* arr = new ACCEL[n];
1e6feb95 108 for ( int i = 0; i < n; i++ )
110f3205 109 {
1e6feb95
VZ
110 int flags = entries[i].GetFlags();
111
110f3205 112 BYTE fVirt = 0;
1e6feb95
VZ
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;
110f3205
JS
119
120 bool isVirtual;
1e6feb95 121
373a5fb3 122 WORD key = wxCharCodeWXToMSW(entries[i].GetKeyCode(), &isVirtual);
2317bff2
JS
123 if (isVirtual)
124 fVirt |= FVIRTKEY;
110f3205 125
110f3205
JS
126 arr[i].fVirt = fVirt;
127 arr[i].key = key;
373a5fb3 128 arr[i].cmd = (WORD)entries[i].GetCommand();
110f3205
JS
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
a21d4ad1
VZ
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
c50f1fb9 147bool wxAcceleratorTable::Ok() const
110f3205
JS
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
c50f1fb9
VZ
167bool wxAcceleratorTable::Translate(wxWindow *window, WXMSG *wxmsg) const
168{
169 MSG *msg = (MSG *)wxmsg;
01dba85a
JS
170 return Ok() && ::TranslateAccelerator(GetHwndOf(window), GetHaccel(), msg);
171}
172
a21d4ad1
VZ
173#endif // wxUSE_ACCEL
174