| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/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 | #include "wx/msw/private/keyboard.h" |
| 37 | |
| 38 | IMPLEMENT_DYNAMIC_CLASS(wxAcceleratorTable, wxObject) |
| 39 | |
| 40 | // ---------------------------------------------------------------------------- |
| 41 | // data defining wxAcceleratorTable |
| 42 | // ---------------------------------------------------------------------------- |
| 43 | |
| 44 | class WXDLLEXPORT wxAcceleratorRefData: public wxObjectRefData |
| 45 | { |
| 46 | friend class WXDLLIMPEXP_FWD_CORE wxAcceleratorTable; |
| 47 | public: |
| 48 | wxAcceleratorRefData(); |
| 49 | virtual ~wxAcceleratorRefData(); |
| 50 | |
| 51 | inline HACCEL GetHACCEL() const { return m_hAccel; } |
| 52 | protected: |
| 53 | HACCEL m_hAccel; |
| 54 | bool m_ok; |
| 55 | |
| 56 | wxDECLARE_NO_COPY_CLASS(wxAcceleratorRefData); |
| 57 | }; |
| 58 | |
| 59 | // ============================================================================ |
| 60 | // implementation |
| 61 | // ============================================================================ |
| 62 | |
| 63 | // ---------------------------------------------------------------------------- |
| 64 | // wxAcceleratorRefData |
| 65 | // ---------------------------------------------------------------------------- |
| 66 | |
| 67 | #define M_ACCELDATA ((wxAcceleratorRefData *)m_refData) |
| 68 | |
| 69 | wxAcceleratorRefData::wxAcceleratorRefData() |
| 70 | { |
| 71 | m_ok = false; |
| 72 | m_hAccel = 0; |
| 73 | } |
| 74 | |
| 75 | wxAcceleratorRefData::~wxAcceleratorRefData() |
| 76 | { |
| 77 | if (m_hAccel) |
| 78 | { |
| 79 | DestroyAcceleratorTable((HACCEL) m_hAccel); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // ---------------------------------------------------------------------------- |
| 84 | // wxAcceleratorTable |
| 85 | // ---------------------------------------------------------------------------- |
| 86 | |
| 87 | // Load from .rc resource |
| 88 | wxAcceleratorTable::wxAcceleratorTable(const wxString& resource) |
| 89 | { |
| 90 | m_refData = new wxAcceleratorRefData; |
| 91 | |
| 92 | HACCEL hAccel = ::LoadAccelerators(wxGetInstance(), resource.wx_str()); |
| 93 | M_ACCELDATA->m_hAccel = hAccel; |
| 94 | M_ACCELDATA->m_ok = hAccel != 0; |
| 95 | } |
| 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 = FVIRTKEY; |
| 108 | if ( flags & wxACCEL_ALT ) |
| 109 | fVirt |= FALT; |
| 110 | if ( flags & wxACCEL_SHIFT ) |
| 111 | fVirt |= FSHIFT; |
| 112 | if ( flags & wxACCEL_CTRL ) |
| 113 | fVirt |= FCONTROL; |
| 114 | |
| 115 | WORD key = wxMSWKeyboard::WXToVK(entries[i].GetKeyCode()); |
| 116 | |
| 117 | arr[i].fVirt = fVirt; |
| 118 | arr[i].key = key; |
| 119 | arr[i].cmd = (WORD)entries[i].GetCommand(); |
| 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 | |
| 128 | bool wxAcceleratorTable::IsOk() const |
| 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 | |
| 148 | bool wxAcceleratorTable::Translate(wxWindow *window, WXMSG *wxmsg) const |
| 149 | { |
| 150 | MSG *msg = (MSG *)wxmsg; |
| 151 | return IsOk() && ::TranslateAccelerator(GetHwndOf(window), GetHaccel(), msg); |
| 152 | } |
| 153 | |
| 154 | #endif // wxUSE_ACCEL |
| 155 | |