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