Commit | Line | Data |
---|---|---|
110f3205 | 1 | ///////////////////////////////////////////////////////////////////////////// |
80fdcdb9 | 2 | // Name: src/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 | 35 | #include "wx/msw/private.h" |
0c03f52d | 36 | #include "wx/msw/private/keyboard.h" |
48d1144b | 37 | |
110f3205 | 38 | IMPLEMENT_DYNAMIC_CLASS(wxAcceleratorTable, wxObject) |
110f3205 | 39 | |
a21d4ad1 VZ |
40 | // ---------------------------------------------------------------------------- |
41 | // data defining wxAcceleratorTable | |
42 | // ---------------------------------------------------------------------------- | |
43 | ||
110f3205 JS |
44 | class WXDLLEXPORT wxAcceleratorRefData: public wxObjectRefData |
45 | { | |
f5851311 | 46 | friend class WXDLLIMPEXP_FWD_CORE wxAcceleratorTable; |
110f3205 | 47 | public: |
c50f1fb9 | 48 | wxAcceleratorRefData(); |
d3c7fc99 | 49 | virtual ~wxAcceleratorRefData(); |
110f3205 JS |
50 | |
51 | inline HACCEL GetHACCEL() const { return m_hAccel; } | |
52 | protected: | |
53 | HACCEL m_hAccel; | |
54 | bool m_ok; | |
22f3361e | 55 | |
c0c133e1 | 56 | wxDECLARE_NO_COPY_CLASS(wxAcceleratorRefData); |
110f3205 JS |
57 | }; |
58 | ||
a21d4ad1 VZ |
59 | // ============================================================================ |
60 | // implementation | |
61 | // ============================================================================ | |
62 | ||
63 | // ---------------------------------------------------------------------------- | |
64 | // wxAcceleratorRefData | |
65 | // ---------------------------------------------------------------------------- | |
66 | ||
110f3205 JS |
67 | #define M_ACCELDATA ((wxAcceleratorRefData *)m_refData) |
68 | ||
69 | wxAcceleratorRefData::wxAcceleratorRefData() | |
70 | { | |
a21d4ad1 VZ |
71 | m_ok = false; |
72 | m_hAccel = 0; | |
110f3205 JS |
73 | } |
74 | ||
75 | wxAcceleratorRefData::~wxAcceleratorRefData() | |
76 | { | |
a21d4ad1 VZ |
77 | if (m_hAccel) |
78 | { | |
79 | DestroyAcceleratorTable((HACCEL) m_hAccel); | |
80 | } | |
110f3205 JS |
81 | } |
82 | ||
a21d4ad1 VZ |
83 | // ---------------------------------------------------------------------------- |
84 | // wxAcceleratorTable | |
85 | // ---------------------------------------------------------------------------- | |
110f3205 JS |
86 | |
87 | // Load from .rc resource | |
88 | wxAcceleratorTable::wxAcceleratorTable(const wxString& resource) | |
89 | { | |
90 | m_refData = new wxAcceleratorRefData; | |
91 | ||
017dc06b | 92 | HACCEL hAccel = ::LoadAccelerators(wxGetInstance(), resource.t_str()); |
110f3205 | 93 | M_ACCELDATA->m_hAccel = hAccel; |
a21d4ad1 | 94 | M_ACCELDATA->m_ok = hAccel != 0; |
110f3205 JS |
95 | } |
96 | ||
110f3205 | 97 | // Create from an array |
252eb8fd | 98 | wxAcceleratorTable::wxAcceleratorTable(int n, const wxAcceleratorEntry entries[]) |
110f3205 JS |
99 | { |
100 | m_refData = new wxAcceleratorRefData; | |
101 | ||
102 | ACCEL* arr = new ACCEL[n]; | |
1e6feb95 | 103 | for ( int i = 0; i < n; i++ ) |
110f3205 | 104 | { |
1e6feb95 VZ |
105 | int flags = entries[i].GetFlags(); |
106 | ||
4f04a4fd | 107 | BYTE fVirt = FVIRTKEY; |
1e6feb95 | 108 | if ( flags & wxACCEL_ALT ) |
4f04a4fd | 109 | fVirt |= FALT; |
1e6feb95 | 110 | if ( flags & wxACCEL_SHIFT ) |
4f04a4fd | 111 | fVirt |= FSHIFT; |
1e6feb95 | 112 | if ( flags & wxACCEL_CTRL ) |
4f04a4fd | 113 | fVirt |= FCONTROL; |
1e6feb95 | 114 | |
0c03f52d | 115 | WORD key = wxMSWKeyboard::WXToVK(entries[i].GetKeyCode()); |
110f3205 | 116 | |
110f3205 JS |
117 | arr[i].fVirt = fVirt; |
118 | arr[i].key = key; | |
373a5fb3 | 119 | arr[i].cmd = (WORD)entries[i].GetCommand(); |
110f3205 JS |
120 | } |
121 | ||
122 | M_ACCELDATA->m_hAccel = ::CreateAcceleratorTable(arr, n); | |
123 | delete[] arr; | |
124 | ||
125 | M_ACCELDATA->m_ok = (M_ACCELDATA->m_hAccel != 0); | |
126 | } | |
127 | ||
b7cacb43 | 128 | bool wxAcceleratorTable::IsOk() const |
110f3205 JS |
129 | { |
130 | return (M_ACCELDATA && (M_ACCELDATA->m_ok)); | |
131 | } | |
132 | ||
133 | void wxAcceleratorTable::SetHACCEL(WXHACCEL hAccel) | |
134 | { | |
135 | if (!M_ACCELDATA) | |
136 | m_refData = new wxAcceleratorRefData; | |
137 | ||
138 | M_ACCELDATA->m_hAccel = (HACCEL) hAccel; | |
139 | } | |
140 | ||
141 | WXHACCEL wxAcceleratorTable::GetHACCEL() const | |
142 | { | |
143 | if (!M_ACCELDATA) | |
144 | return 0; | |
145 | return (WXHACCEL) M_ACCELDATA->m_hAccel; | |
146 | } | |
147 | ||
c50f1fb9 VZ |
148 | bool wxAcceleratorTable::Translate(wxWindow *window, WXMSG *wxmsg) const |
149 | { | |
150 | MSG *msg = (MSG *)wxmsg; | |
a1b806b9 | 151 | return IsOk() && ::TranslateAccelerator(GetHwndOf(window), GetHaccel(), msg); |
01dba85a JS |
152 | } |
153 | ||
a21d4ad1 VZ |
154 | #endif // wxUSE_ACCEL |
155 |