| 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 | |
| 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
| 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 |
| 24 | #include "wx/window.h" |
| 25 | #endif |
| 26 | |
| 27 | #include "wx/accel.h" |
| 28 | |
| 29 | #if wxUSE_ACCEL |
| 30 | |
| 31 | #include "wx/msw/private.h" |
| 32 | |
| 33 | IMPLEMENT_DYNAMIC_CLASS(wxAcceleratorTable, wxObject) |
| 34 | |
| 35 | class WXDLLEXPORT wxAcceleratorRefData: public wxObjectRefData |
| 36 | { |
| 37 | friend class WXDLLEXPORT wxAcceleratorTable; |
| 38 | public: |
| 39 | wxAcceleratorRefData(); |
| 40 | ~wxAcceleratorRefData(); |
| 41 | |
| 42 | inline HACCEL GetHACCEL() const { return m_hAccel; } |
| 43 | protected: |
| 44 | HACCEL m_hAccel; |
| 45 | bool m_ok; |
| 46 | |
| 47 | DECLARE_NO_COPY_CLASS(wxAcceleratorRefData) |
| 48 | }; |
| 49 | |
| 50 | #define M_ACCELDATA ((wxAcceleratorRefData *)m_refData) |
| 51 | |
| 52 | wxAcceleratorRefData::wxAcceleratorRefData() |
| 53 | { |
| 54 | m_ok = FALSE; |
| 55 | m_hAccel = 0; |
| 56 | } |
| 57 | |
| 58 | wxAcceleratorRefData::~wxAcceleratorRefData() |
| 59 | { |
| 60 | if (m_hAccel) |
| 61 | { |
| 62 | DestroyAcceleratorTable((HACCEL) m_hAccel); |
| 63 | } |
| 64 | m_hAccel = 0 ; |
| 65 | } |
| 66 | |
| 67 | wxAcceleratorTable::wxAcceleratorTable() |
| 68 | { |
| 69 | m_refData = NULL; |
| 70 | } |
| 71 | |
| 72 | wxAcceleratorTable::~wxAcceleratorTable() |
| 73 | { |
| 74 | } |
| 75 | |
| 76 | // Load from .rc resource |
| 77 | wxAcceleratorTable::wxAcceleratorTable(const wxString& resource) |
| 78 | { |
| 79 | m_refData = new wxAcceleratorRefData; |
| 80 | |
| 81 | HACCEL hAccel = |
| 82 | #if defined(__WIN32__) |
| 83 | #ifdef UNICODE |
| 84 | ::LoadAcceleratorsW(wxGetInstance(), (const wxChar *)resource); |
| 85 | #else |
| 86 | ::LoadAcceleratorsA(wxGetInstance(), (const char *)resource); |
| 87 | #endif |
| 88 | #else |
| 89 | ::LoadAccelerators(wxGetInstance(), (const wxChar *)resource); |
| 90 | #endif |
| 91 | M_ACCELDATA->m_hAccel = hAccel; |
| 92 | M_ACCELDATA->m_ok = (hAccel != 0); |
| 93 | } |
| 94 | |
| 95 | extern int wxCharCodeWXToMSW(int id, bool *isVirtual); |
| 96 | |
| 97 | // Create from an array |
| 98 | wxAcceleratorTable::wxAcceleratorTable(int n, const wxAcceleratorEntry entries[]) |
| 99 | { |
| 100 | m_refData = new wxAcceleratorRefData; |
| 101 | |
| 102 | ACCEL* arr = new ACCEL[n]; |
| 103 | for ( int i = 0; i < n; i++ ) |
| 104 | { |
| 105 | int flags = entries[i].GetFlags(); |
| 106 | |
| 107 | BYTE fVirt = 0; |
| 108 | if ( flags & wxACCEL_ALT ) |
| 109 | fVirt |= FALT | FVIRTKEY; |
| 110 | if ( flags & wxACCEL_SHIFT ) |
| 111 | fVirt |= FSHIFT | FVIRTKEY; |
| 112 | if ( flags & wxACCEL_CTRL ) |
| 113 | fVirt |= FCONTROL | FVIRTKEY; |
| 114 | |
| 115 | bool isVirtual; |
| 116 | |
| 117 | WORD key = wxCharCodeWXToMSW(entries[i].GetKeyCode(), &isVirtual); |
| 118 | if (isVirtual) |
| 119 | fVirt |= FVIRTKEY; |
| 120 | |
| 121 | arr[i].fVirt = fVirt; |
| 122 | arr[i].key = key; |
| 123 | arr[i].cmd = entries[i].GetCommand(); |
| 124 | } |
| 125 | |
| 126 | M_ACCELDATA->m_hAccel = ::CreateAcceleratorTable(arr, n); |
| 127 | delete[] arr; |
| 128 | |
| 129 | M_ACCELDATA->m_ok = (M_ACCELDATA->m_hAccel != 0); |
| 130 | } |
| 131 | |
| 132 | bool wxAcceleratorTable::Ok() const |
| 133 | { |
| 134 | return (M_ACCELDATA && (M_ACCELDATA->m_ok)); |
| 135 | } |
| 136 | |
| 137 | void wxAcceleratorTable::SetHACCEL(WXHACCEL hAccel) |
| 138 | { |
| 139 | if (!M_ACCELDATA) |
| 140 | m_refData = new wxAcceleratorRefData; |
| 141 | |
| 142 | M_ACCELDATA->m_hAccel = (HACCEL) hAccel; |
| 143 | } |
| 144 | |
| 145 | WXHACCEL wxAcceleratorTable::GetHACCEL() const |
| 146 | { |
| 147 | if (!M_ACCELDATA) |
| 148 | return 0; |
| 149 | return (WXHACCEL) M_ACCELDATA->m_hAccel; |
| 150 | } |
| 151 | |
| 152 | bool wxAcceleratorTable::Translate(wxWindow *window, WXMSG *wxmsg) const |
| 153 | { |
| 154 | MSG *msg = (MSG *)wxmsg; |
| 155 | return Ok() && ::TranslateAccelerator(GetHwndOf(window), GetHaccel(), msg); |
| 156 | } |
| 157 | |
| 158 | #endif |