]> git.saurik.com Git - wxWidgets.git/blame - src/msw/accel.cpp
fixed wxEVT_CONTEXT_MENU generation (it works differently under w2k/XP than under...
[wxWidgets.git] / src / msw / accel.cpp
CommitLineData
110f3205
JS
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
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
110f3205
JS
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
1e6feb95 24 #include "wx/window.h"
110f3205
JS
25#endif
26
1e6feb95 27#include "wx/accel.h"
110f3205 28
461dae94
VZ
29#if wxUSE_ACCEL
30
48d1144b
JS
31#include "wx/msw/private.h"
32
110f3205 33IMPLEMENT_DYNAMIC_CLASS(wxAcceleratorTable, wxObject)
110f3205
JS
34
35class WXDLLEXPORT wxAcceleratorRefData: public wxObjectRefData
36{
37 friend class WXDLLEXPORT wxAcceleratorTable;
38public:
c50f1fb9
VZ
39 wxAcceleratorRefData();
40 ~wxAcceleratorRefData();
110f3205
JS
41
42 inline HACCEL GetHACCEL() const { return m_hAccel; }
43protected:
44 HACCEL m_hAccel;
45 bool m_ok;
22f3361e
VZ
46
47 DECLARE_NO_COPY_CLASS(wxAcceleratorRefData)
110f3205
JS
48};
49
50#define M_ACCELDATA ((wxAcceleratorRefData *)m_refData)
51
52wxAcceleratorRefData::wxAcceleratorRefData()
53{
54 m_ok = FALSE;
55 m_hAccel = 0;
56}
57
58wxAcceleratorRefData::~wxAcceleratorRefData()
59{
60 if (m_hAccel)
61 {
1e6d9499 62 // This function not available in WIN16
b39dbf34 63#if !defined(__WIN16__)
110f3205 64 DestroyAcceleratorTable((HACCEL) m_hAccel);
1e6d9499 65#endif
110f3205
JS
66 }
67 m_hAccel = 0 ;
68}
69
70wxAcceleratorTable::wxAcceleratorTable()
71{
72 m_refData = NULL;
73}
74
75wxAcceleratorTable::~wxAcceleratorTable()
76{
77}
78
79// Load from .rc resource
80wxAcceleratorTable::wxAcceleratorTable(const wxString& resource)
81{
82 m_refData = new wxAcceleratorRefData;
83
84 HACCEL hAccel =
b39dbf34 85#if defined(__WIN32__)
110f3205 86#ifdef UNICODE
837e5743 87 ::LoadAcceleratorsW(wxGetInstance(), (const wxChar *)resource);
110f3205
JS
88#else
89 ::LoadAcceleratorsA(wxGetInstance(), (const char *)resource);
90#endif
91#else
837e5743 92 ::LoadAccelerators(wxGetInstance(), (const wxChar *)resource);
110f3205
JS
93#endif
94 M_ACCELDATA->m_hAccel = hAccel;
95 M_ACCELDATA->m_ok = (hAccel != 0);
96}
97
98extern int wxCharCodeWXToMSW(int id, bool *isVirtual);
99
100// Create from an array
b39dbf34 101#if !defined(__WIN16__)
252eb8fd 102wxAcceleratorTable::wxAcceleratorTable(int n, const wxAcceleratorEntry entries[])
110f3205 103{
1e6d9499 104 // Not available in WIN16
110f3205
JS
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
VZ
121
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;
1e6feb95 128 arr[i].cmd = 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}
c50f1fb9 136#else // Win16
8870c26e
JS
137wxAcceleratorTable::wxAcceleratorTable(int WXUNUSED(n), const wxAcceleratorEntry WXUNUSED(entries)[])
138{
1044a386
JS
139 // No, we simply gracefully degrade; we don't expect the
140 // developer to pepper their code with #ifdefs just for this.
141 // wxFAIL_MSG("not implemented");
8870c26e 142}
c50f1fb9 143#endif // Win32/16
110f3205 144
c50f1fb9 145bool wxAcceleratorTable::Ok() const
110f3205
JS
146{
147 return (M_ACCELDATA && (M_ACCELDATA->m_ok));
148}
149
150void wxAcceleratorTable::SetHACCEL(WXHACCEL hAccel)
151{
152 if (!M_ACCELDATA)
153 m_refData = new wxAcceleratorRefData;
154
155 M_ACCELDATA->m_hAccel = (HACCEL) hAccel;
156}
157
158WXHACCEL wxAcceleratorTable::GetHACCEL() const
159{
160 if (!M_ACCELDATA)
161 return 0;
162 return (WXHACCEL) M_ACCELDATA->m_hAccel;
163}
164
c50f1fb9
VZ
165bool wxAcceleratorTable::Translate(wxWindow *window, WXMSG *wxmsg) const
166{
167 MSG *msg = (MSG *)wxmsg;
01dba85a
JS
168 return Ok() && ::TranslateAccelerator(GetHwndOf(window), GetHaccel(), msg);
169}
170
461dae94 171#endif