accel.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/accel.h
  3. // Purpose: wxAcceleratorEntry and wxAcceleratorTable classes
  4. // Author: Julian Smart, Robert Roebling, Vadim Zeitlin
  5. // Modified by:
  6. // Created: 31.05.01 (extracted from other files)
  7. // Copyright: (c) wxWidgets team
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_ACCEL_H_BASE_
  11. #define _WX_ACCEL_H_BASE_
  12. #include "wx/defs.h"
  13. #if wxUSE_ACCEL
  14. #include "wx/object.h"
  15. class WXDLLIMPEXP_FWD_CORE wxAcceleratorTable;
  16. class WXDLLIMPEXP_FWD_CORE wxMenuItem;
  17. class WXDLLIMPEXP_FWD_CORE wxKeyEvent;
  18. // ----------------------------------------------------------------------------
  19. // constants
  20. // ----------------------------------------------------------------------------
  21. // wxAcceleratorEntry flags
  22. enum wxAcceleratorEntryFlags
  23. {
  24. wxACCEL_NORMAL = 0x0000, // no modifiers
  25. wxACCEL_ALT = 0x0001, // hold Alt key down
  26. wxACCEL_CTRL = 0x0002, // hold Ctrl key down
  27. wxACCEL_SHIFT = 0x0004, // hold Shift key down
  28. #if defined(__WXMAC__) || defined(__WXCOCOA__)
  29. wxACCEL_RAW_CTRL= 0x0008, //
  30. #else
  31. wxACCEL_RAW_CTRL= wxACCEL_CTRL,
  32. #endif
  33. wxACCEL_CMD = wxACCEL_CTRL
  34. };
  35. // ----------------------------------------------------------------------------
  36. // an entry in wxAcceleratorTable corresponds to one accelerator
  37. // ----------------------------------------------------------------------------
  38. class WXDLLIMPEXP_CORE wxAcceleratorEntry
  39. {
  40. public:
  41. wxAcceleratorEntry(int flags = 0, int keyCode = 0, int cmd = 0,
  42. wxMenuItem *item = NULL)
  43. : m_flags(flags)
  44. , m_keyCode(keyCode)
  45. , m_command(cmd)
  46. , m_item(item)
  47. { }
  48. wxAcceleratorEntry(const wxAcceleratorEntry& entry)
  49. : m_flags(entry.m_flags)
  50. , m_keyCode(entry.m_keyCode)
  51. , m_command(entry.m_command)
  52. , m_item(entry.m_item)
  53. { }
  54. // create accelerator corresponding to the specified string, return NULL if
  55. // string couldn't be parsed or a pointer to be deleted by the caller
  56. static wxAcceleratorEntry *Create(const wxString& str);
  57. wxAcceleratorEntry& operator=(const wxAcceleratorEntry& entry)
  58. {
  59. if (&entry != this)
  60. Set(entry.m_flags, entry.m_keyCode, entry.m_command, entry.m_item);
  61. return *this;
  62. }
  63. void Set(int flags, int keyCode, int cmd, wxMenuItem *item = NULL)
  64. {
  65. m_flags = flags;
  66. m_keyCode = keyCode;
  67. m_command = cmd;
  68. m_item = item;
  69. }
  70. void SetMenuItem(wxMenuItem *item) { m_item = item; }
  71. int GetFlags() const { return m_flags; }
  72. int GetKeyCode() const { return m_keyCode; }
  73. int GetCommand() const { return m_command; }
  74. wxMenuItem *GetMenuItem() const { return m_item; }
  75. bool operator==(const wxAcceleratorEntry& entry) const
  76. {
  77. return m_flags == entry.m_flags &&
  78. m_keyCode == entry.m_keyCode &&
  79. m_command == entry.m_command &&
  80. m_item == entry.m_item;
  81. }
  82. bool operator!=(const wxAcceleratorEntry& entry) const
  83. { return !(*this == entry); }
  84. #if defined(__WXMOTIF__)
  85. // Implementation use only
  86. bool MatchesEvent(const wxKeyEvent& event) const;
  87. #endif
  88. bool IsOk() const
  89. {
  90. return m_keyCode != 0;
  91. }
  92. // string <-> wxAcceleratorEntry conversion
  93. // ----------------------------------------
  94. // returns a wxString for the this accelerator.
  95. // this function formats it using the <flags>-<keycode> format
  96. // where <flags> maybe a hyphen-separated list of "shift|alt|ctrl"
  97. wxString ToString() const { return AsPossiblyLocalizedString(true); }
  98. // same as above but without translating, useful if the string is meant to
  99. // be stored in a file or otherwise stored, instead of being shown to the
  100. // user
  101. wxString ToRawString() const { return AsPossiblyLocalizedString(false); }
  102. // returns true if the given string correctly initialized this object
  103. // (i.e. if IsOk() returns true after this call)
  104. bool FromString(const wxString& str);
  105. private:
  106. wxString AsPossiblyLocalizedString(bool localized) const;
  107. // common part of Create() and FromString()
  108. static bool ParseAccel(const wxString& str, int *flags, int *keycode);
  109. int m_flags; // combination of wxACCEL_XXX constants
  110. int m_keyCode; // ASCII or virtual keycode
  111. int m_command; // Command id to generate
  112. // the menu item this entry corresponds to, may be NULL
  113. wxMenuItem *m_item;
  114. // for compatibility with old code, use accessors now!
  115. friend class WXDLLIMPEXP_FWD_CORE wxMenu;
  116. };
  117. // ----------------------------------------------------------------------------
  118. // include wxAcceleratorTable class declaration, it is only used by the library
  119. // and so doesn't have any published user visible interface
  120. // ----------------------------------------------------------------------------
  121. #if defined(__WXUNIVERSAL__)
  122. #include "wx/generic/accel.h"
  123. #elif defined(__WXMSW__)
  124. #include "wx/msw/accel.h"
  125. #elif defined(__WXMOTIF__)
  126. #include "wx/motif/accel.h"
  127. #elif defined(__WXGTK20__)
  128. #include "wx/gtk/accel.h"
  129. #elif defined(__WXGTK__)
  130. #include "wx/gtk1/accel.h"
  131. #elif defined(__WXMAC__)
  132. #include "wx/osx/accel.h"
  133. #elif defined(__WXCOCOA__)
  134. #include "wx/generic/accel.h"
  135. #elif defined(__WXPM__)
  136. #include "wx/os2/accel.h"
  137. #endif
  138. extern WXDLLIMPEXP_DATA_CORE(wxAcceleratorTable) wxNullAcceleratorTable;
  139. #endif // wxUSE_ACCEL
  140. #endif
  141. // _WX_ACCEL_H_BASE_