]> git.saurik.com Git - wxWidgets.git/blame - src/msw/accel.cpp
fix memory leak (coverity checker CID 53)
[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
a21d4ad1
VZ
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
110f3205
JS
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
a21d4ad1 24 #pragma hdrstop
110f3205
JS
25#endif
26
a21d4ad1
VZ
27#if wxUSE_ACCEL
28
110f3205 29#ifndef WX_PRECOMP
1e6feb95 30 #include "wx/window.h"
110f3205
JS
31#endif
32
1e6feb95 33#include "wx/accel.h"
110f3205 34
48d1144b
JS
35#include "wx/msw/private.h"
36
a21d4ad1
VZ
37extern WXWORD wxCharCodeWXToMSW(int id, bool *isVirtual);
38
110f3205 39IMPLEMENT_DYNAMIC_CLASS(wxAcceleratorTable, wxObject)
110f3205 40
a21d4ad1
VZ
41// ----------------------------------------------------------------------------
42// data defining wxAcceleratorTable
43// ----------------------------------------------------------------------------
44
110f3205
JS
45class WXDLLEXPORT wxAcceleratorRefData: public wxObjectRefData
46{
47 friend class WXDLLEXPORT wxAcceleratorTable;
48public:
c50f1fb9
VZ
49 wxAcceleratorRefData();
50 ~wxAcceleratorRefData();
110f3205
JS
51
52 inline HACCEL GetHACCEL() const { return m_hAccel; }
53protected:
54 HACCEL m_hAccel;
55 bool m_ok;
22f3361e
VZ
56
57 DECLARE_NO_COPY_CLASS(wxAcceleratorRefData)
110f3205
JS
58};
59
a21d4ad1
VZ
60// ============================================================================
61// implementation
62// ============================================================================
63
64// ----------------------------------------------------------------------------
65// wxAcceleratorRefData
66// ----------------------------------------------------------------------------
67
110f3205
JS
68#define M_ACCELDATA ((wxAcceleratorRefData *)m_refData)
69
70wxAcceleratorRefData::wxAcceleratorRefData()
71{
a21d4ad1
VZ
72 m_ok = false;
73 m_hAccel = 0;
110f3205
JS
74}
75
76wxAcceleratorRefData::~wxAcceleratorRefData()
77{
a21d4ad1
VZ
78 if (m_hAccel)
79 {
80 DestroyAcceleratorTable((HACCEL) m_hAccel);
81 }
110f3205
JS
82}
83
a21d4ad1
VZ
84// ----------------------------------------------------------------------------
85// wxAcceleratorTable
86// ----------------------------------------------------------------------------
110f3205
JS
87
88// Load from .rc resource
89wxAcceleratorTable::wxAcceleratorTable(const wxString& resource)
90{
91 m_refData = new wxAcceleratorRefData;
92
a21d4ad1 93 HACCEL hAccel = ::LoadAccelerators(wxGetInstance(), resource);
110f3205 94 M_ACCELDATA->m_hAccel = hAccel;
a21d4ad1 95 M_ACCELDATA->m_ok = hAccel != 0;
110f3205
JS
96}
97
110f3205 98// Create from an array
252eb8fd 99wxAcceleratorTable::wxAcceleratorTable(int n, const wxAcceleratorEntry entries[])
110f3205
JS
100{
101 m_refData = new wxAcceleratorRefData;
102
103 ACCEL* arr = new ACCEL[n];
1e6feb95 104 for ( int i = 0; i < n; i++ )
110f3205 105 {
1e6feb95
VZ
106 int flags = entries[i].GetFlags();
107
110f3205 108 BYTE fVirt = 0;
1e6feb95
VZ
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;
110f3205
JS
115
116 bool isVirtual;
1e6feb95 117
373a5fb3 118 WORD key = wxCharCodeWXToMSW(entries[i].GetKeyCode(), &isVirtual);
2317bff2
JS
119 if (isVirtual)
120 fVirt |= FVIRTKEY;
110f3205 121
110f3205
JS
122 arr[i].fVirt = fVirt;
123 arr[i].key = key;
373a5fb3 124 arr[i].cmd = (WORD)entries[i].GetCommand();
110f3205
JS
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
a21d4ad1
VZ
133bool wxAcceleratorTable::operator==(const wxAcceleratorTable& accel) const
134{
135 const wxAcceleratorRefData *
136 accelData = (wxAcceleratorRefData *)accel.m_refData;
137
138 return m_refData ? (accelData &&
139 M_ACCELDATA->m_hAccel == accelData->m_hAccel)
140 : !accelData;
141}
142
c50f1fb9 143bool wxAcceleratorTable::Ok() const
110f3205
JS
144{
145 return (M_ACCELDATA && (M_ACCELDATA->m_ok));
146}
147
148void wxAcceleratorTable::SetHACCEL(WXHACCEL hAccel)
149{
150 if (!M_ACCELDATA)
151 m_refData = new wxAcceleratorRefData;
152
153 M_ACCELDATA->m_hAccel = (HACCEL) hAccel;
154}
155
156WXHACCEL wxAcceleratorTable::GetHACCEL() const
157{
158 if (!M_ACCELDATA)
159 return 0;
160 return (WXHACCEL) M_ACCELDATA->m_hAccel;
161}
162
c50f1fb9
VZ
163bool wxAcceleratorTable::Translate(wxWindow *window, WXMSG *wxmsg) const
164{
6f02a879
VZ
165#if 0
166 // calling TranslateAccelerator() with child window doesn't do anything so
167 // it's probably a bug
168 wxASSERT_MSG( window->IsTopLevel(),
169 _T("TranslateAccelerator() needs a top level window") );
170#endif
171
c50f1fb9 172 MSG *msg = (MSG *)wxmsg;
01dba85a
JS
173 return Ok() && ::TranslateAccelerator(GetHwndOf(window), GetHaccel(), msg);
174}
175
a21d4ad1
VZ
176#endif // wxUSE_ACCEL
177