home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / wxos2240.zip / wxWindows-2.4.0 / src / generic / accel.cpp next >
C/C++ Source or Header  |  2002-05-28  |  6KB  |  219 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name:        generic/accel.cpp
  3. // Purpose:     generic implementation of wxAcceleratorTable class
  4. // Author:      Robert Roebling
  5. // Modified:    VZ pn 31.05.01: use typed lists, Unicode cleanup, Add/Remove
  6. // Id:          $Id: accel.cpp,v 1.5 2002/05/28 16:51:14 VZ Exp $
  7. // Copyright:   (c) 1998 Robert Roebling
  8. // Licence:     wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10.  
  11. // ============================================================================
  12. // declarations
  13. // ============================================================================
  14.  
  15. // ----------------------------------------------------------------------------
  16. // headers
  17. // ----------------------------------------------------------------------------
  18.  
  19. #ifdef __GNUG__
  20.     #pragma implementation "accel.h"
  21. #endif
  22.  
  23. // For compilers that support precompilation, includes "wx.h".
  24. #include "wx/wxprec.h"
  25.  
  26. #ifdef __BORLANDC__
  27.     #pragma hdrstop
  28. #endif
  29.  
  30. #if wxUSE_ACCEL
  31.  
  32. #ifndef WX_PRECOMP
  33.     #include "wx/event.h"
  34.     #include "wx/list.h"
  35. #endif // WX_PRECOMP
  36.  
  37. #include "wx/accel.h"
  38.  
  39. #include <ctype.h>
  40.  
  41. // ----------------------------------------------------------------------------
  42. // wxAccelList: a list of wxAcceleratorEntries
  43. // ----------------------------------------------------------------------------
  44.  
  45. WX_DECLARE_LIST(wxAcceleratorEntry, wxAccelList);
  46. #include "wx/listimpl.cpp"
  47. WX_DEFINE_LIST(wxAccelList);
  48.  
  49. // ----------------------------------------------------------------------------
  50. // wxAccelRefData: the data used by wxAcceleratorTable
  51. // ----------------------------------------------------------------------------
  52.  
  53. class wxAccelRefData : public wxObjectRefData
  54. {
  55. public:
  56.     wxAccelRefData()
  57.     {
  58.         m_accels.DeleteContents(TRUE);
  59.     }
  60.  
  61.     wxAccelRefData(const wxAccelRefData& data)
  62.         : wxObjectRefData()
  63.     {
  64.         m_accels.DeleteContents(TRUE);
  65.         m_accels = data.m_accels;
  66.     }
  67.  
  68.     wxAccelList m_accels;
  69. };
  70.  
  71. // macro which can be used to access wxAccelRefData from wxAcceleratorTable
  72. #define M_ACCELDATA ((wxAccelRefData *)m_refData)
  73.  
  74.  
  75. // ============================================================================
  76. // implementation
  77. // ============================================================================
  78.  
  79. // ----------------------------------------------------------------------------
  80. // wxAcceleratorTable ctors
  81. // ----------------------------------------------------------------------------
  82.  
  83. IMPLEMENT_DYNAMIC_CLASS(wxAcceleratorTable, wxObject)
  84.  
  85. wxAcceleratorTable::wxAcceleratorTable()
  86. {
  87. }
  88.  
  89. wxAcceleratorTable::wxAcceleratorTable(int n, const wxAcceleratorEntry entries[])
  90. {
  91.     m_refData = new wxAccelRefData;
  92.  
  93.     for ( int i = 0; i < n; i++ )
  94.     {
  95.         const wxAcceleratorEntry& entry = entries[i];
  96.  
  97.         int keycode = entry.GetKeyCode();
  98.         if ( wxIslower(keycode) )
  99.             keycode = wxToupper(keycode);
  100.  
  101.         M_ACCELDATA->m_accels.Append(new wxAcceleratorEntry(entry.GetFlags(),
  102.                                                             keycode,
  103.                                                             entry.GetCommand()));
  104.     }
  105. }
  106.  
  107. wxAcceleratorTable::~wxAcceleratorTable()
  108. {
  109. }
  110.  
  111. bool wxAcceleratorTable::Ok() const
  112. {
  113.     return m_refData != NULL;
  114. }
  115.  
  116. // ----------------------------------------------------------------------------
  117. // wxAcceleratorTable updating
  118. // ----------------------------------------------------------------------------
  119.  
  120. void wxAcceleratorTable::Add(const wxAcceleratorEntry& entry)
  121. {
  122.     AllocExclusive();
  123.  
  124.     if ( !m_refData )
  125.     {
  126.         m_refData = new wxAccelRefData;
  127.     }
  128.  
  129.     M_ACCELDATA->m_accels.Append(new wxAcceleratorEntry(entry));
  130. }
  131.  
  132. void wxAcceleratorTable::Remove(const wxAcceleratorEntry& entry)
  133. {
  134.     AllocExclusive();
  135.  
  136.     wxAccelList::Node *node = M_ACCELDATA->m_accels.GetFirst();
  137.     while ( node )
  138.     {
  139.         const wxAcceleratorEntry *entryCur = node->GetData();
  140.  
  141.         if ( *entryCur == entry )
  142.         {
  143.             M_ACCELDATA->m_accels.DeleteNode(node);
  144.  
  145.             return;
  146.         }
  147.  
  148.         node = node->GetNext();
  149.     }
  150.  
  151.     wxFAIL_MSG(_T("deleting inexistent accel from wxAcceleratorTable"));
  152. }
  153.  
  154. // ----------------------------------------------------------------------------
  155. // wxAcceleratorTable: find a command for the given key press
  156. // ----------------------------------------------------------------------------
  157.  
  158. const wxAcceleratorEntry *
  159. wxAcceleratorTable::GetEntry(const wxKeyEvent& event) const
  160. {
  161.     if ( !Ok() )
  162.     {
  163.         // not an error, the accel table is just empty
  164.         return NULL;
  165.     }
  166.  
  167.     wxAccelList::Node *node = M_ACCELDATA->m_accels.GetFirst();
  168.     while ( node )
  169.     {
  170.         const wxAcceleratorEntry *entry = node->GetData();
  171.  
  172.         // is the key the same?
  173.         if ( event.m_keyCode == entry->GetKeyCode() )
  174.         {
  175.             int flags = entry->GetFlags();
  176.  
  177.             // now check flags
  178.             if ( (((flags & wxACCEL_CTRL) != 0) == event.ControlDown()) &&
  179.                  (((flags & wxACCEL_SHIFT) != 0) == event.ShiftDown()) &&
  180.                  (((flags & wxACCEL_ALT) != 0) ==
  181.                     (event.AltDown() || event.MetaDown())) )
  182.             {
  183.                 return entry;
  184.             }
  185.         }
  186.  
  187.         node = node->GetNext();
  188.     }
  189.  
  190.     return NULL;
  191. }
  192.  
  193. wxMenuItem *wxAcceleratorTable::GetMenuItem(const wxKeyEvent& event) const
  194. {
  195.     const wxAcceleratorEntry *entry = GetEntry(event);
  196.  
  197.     return entry ? entry->GetMenuItem() : NULL;
  198. }
  199.  
  200. int wxAcceleratorTable::GetCommand(const wxKeyEvent& event) const
  201. {
  202.     const wxAcceleratorEntry *entry = GetEntry(event);
  203.  
  204.     return entry ? entry->GetCommand() : -1;
  205. }
  206.  
  207. wxObjectRefData *wxAcceleratorTable::CreateRefData() const
  208. {
  209.     return new wxAccelRefData;
  210. }
  211.  
  212. wxObjectRefData *wxAcceleratorTable::CloneRefData(const wxObjectRefData *data) const
  213. {
  214.     return new wxAccelRefData(*(wxAccelRefData *)data);
  215. }
  216.  
  217. #endif // wxUSE_ACCEL
  218.  
  219.