lleditmenuhandler.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. * @file lleditmenuhandler.h
  3. * @authors Aaron Yonas, James Cook
  4. *
  5. * $LicenseInfo:firstyear=2006&license=viewergpl$
  6. *
  7. * Copyright (c) 2006-2009, Linden Research, Inc.
  8. * Copyright (c) 2009-2023, Henri Beauchamp.
  9. *
  10. * Second Life Viewer Source Code
  11. * The source code in this file ("Source Code") is provided by Linden Lab
  12. * to you under the terms of the GNU General Public License, version 2.0
  13. * ("GPL"), unless you have obtained a separate licensing agreement
  14. * ("Other License"), formally executed by you and Linden Lab. Terms of
  15. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  16. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  17. *
  18. * There are special exceptions to the terms and conditions of the GPL as
  19. * it is applied to this Source Code. View the full text of the exception
  20. * in the file doc/FLOSS-exception.txt in this software distribution, or
  21. * online at
  22. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  23. *
  24. * By copying, modifying or distributing this software, you acknowledge
  25. * that you have read and understood your obligations described above,
  26. * and agree to abide by those obligations.
  27. *
  28. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  29. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  30. * COMPLETENESS OR PERFORMANCE.
  31. * $/LicenseInfo$
  32. */
  33. #ifndef LL_LLEDITMENUHANDLER_H
  34. #define LL_LLEDITMENUHANDLER_H
  35. #include "llview.h"
  36. class LLMenuGL;
  37. struct HBContextMenuData
  38. {
  39. // Operation types, for use in mOperation
  40. enum { SET = 0, CUT = 1, COPY = 2, PASTE = 3 };
  41. std::string mMenuType;
  42. U32 mHandlerID;
  43. S32 mOperation;
  44. };
  45. constexpr U32 NO_CONTEXT_MENU = 0x00;
  46. constexpr U32 HAS_CONTEXT_MENU = 0x01;
  47. constexpr U32 HAS_UNDO_REDO = 0x02;
  48. constexpr U32 HAS_CUSTOM = 0x04;
  49. // Custom menu entries global callback: the HBContextMenuData structure
  50. // instance pointed to by datap is created by LLEditMenuHandler and must be
  51. // deleted by the callback. HB
  52. typedef void (*context_menu_cb_t)(HBContextMenuData* datap);
  53. // Interface used by menu system for plug-in hotkey/menu handling
  54. class LLEditMenuHandler
  55. {
  56. public:
  57. LLEditMenuHandler(U32 context_menu_flags = NO_CONTEXT_MENU);
  58. virtual ~LLEditMenuHandler();
  59. LL_INLINE U32 getID() const { return mID; }
  60. // Used by the text and line editors.
  61. LL_INLINE virtual void cut() {}
  62. LL_INLINE virtual bool canCut() const { return false; }
  63. LL_INLINE virtual void copy() {}
  64. LL_INLINE virtual bool canCopy() const { return false; }
  65. LL_INLINE virtual void paste() {}
  66. LL_INLINE virtual bool canPaste() const { return false; }
  67. // "doDelete" since "delete" is a C++ keyword...
  68. LL_INLINE virtual void doDelete() {}
  69. LL_INLINE virtual bool canDoDelete() const { return false; }
  70. LL_INLINE virtual void selectAll() {}
  71. LL_INLINE virtual bool canSelectAll() const { return false; }
  72. LL_INLINE virtual void deselect() {}
  73. LL_INLINE virtual bool canDeselect() const { return false; }
  74. // Used by the text editor and the selection manager.
  75. LL_INLINE virtual void undo() {}
  76. LL_INLINE virtual bool canUndo() const { return false; }
  77. LL_INLINE virtual void redo() {}
  78. LL_INLINE virtual bool canRedo() const { return false; }
  79. // Used only by the selection manager.
  80. LL_INLINE virtual void duplicate() {}
  81. LL_INLINE virtual bool canDuplicate() const { return false; }
  82. // Used to set the 'type' of the editor handler, which is an arbitrary
  83. // string used to determine how to deal with the menu data in the global
  84. // custom callback (e.g. for a "script" editor, or a "note card" editor).
  85. // Whenever the custom callback is already set, it gets triggered by this
  86. // method with a SET operation type. HB
  87. void setCustomMenuType(const char* type);
  88. // Used to set the labels for the context menu custom entries, passing
  89. // an empty string (or omitting it) causes the corresponding entry to be
  90. // hidden. HB
  91. LL_INLINE void setCustomMenu(const std::string& cut = LLStringUtil::null,
  92. const std::string& copy = LLStringUtil::null,
  93. const std::string& paste = LLStringUtil::null)
  94. {
  95. mCustomCutLabel = cut;
  96. mCustomCopyLabel = copy;
  97. mCustomPasteLabel = paste;
  98. updateCustomEntries();
  99. }
  100. // Same as above, but using a menu handler Id: typically used via a Lua
  101. // callback, in reply to a SET operation. Returns true when successful
  102. // (i.e. when menu_handler_id is valid). HB
  103. static bool setCustomMenu(U32 menu_handler_id,
  104. const std::string& cut = LLStringUtil::null,
  105. const std::string& copy = LLStringUtil::null,
  106. const std::string& paste = LLStringUtil::null);
  107. // Used to set the the global custom callback for all context menus. This
  108. // is currently only used by the Lua automation script, since this is why
  109. // I implemented the custom menu entries in the first place. HB
  110. LL_INLINE static void setCustomCallback(context_menu_cb_t callback)
  111. {
  112. sContextMenuCallback = callback;
  113. }
  114. // Called, maybe asynchronously, as a result of a PASTE action sent to
  115. // sContextMenuCallback, to actually paste the text into the UI element
  116. // linked to this menu handler. Returns true when menu_handler_id was valid
  117. // and the text could be pasted, false otherwise. HB
  118. static bool pasteTo(U32 menu_handler_id);
  119. protected:
  120. // Grabs (sets to 'this') unconditionally the global menu handler pointer.
  121. void grabMenuHandler();
  122. // Releases (sets to NULL) the global menu handler pointer if it is
  123. // currently held by this instance (set to 'this').
  124. void releaseMenuHandler();
  125. // When it does not exist (mPopupMenuHandle.get() == NULL), creates a
  126. // context menu and returns its pointer. When the menu already exists,
  127. // it returns the pointer for the current menu. When it is passed false
  128. // for with_spell_separator, it does not add a menu item separator at the
  129. // end of the menu (used to separate editing actions items from spell
  130. // checking menu entries and suggestions). HB
  131. LLMenuGL* createContextMenu(bool with_spell_separator = true);
  132. // Returns the pointer associated to mPopupMenuHandle, which may be NULL
  133. // when the menu has not yet been created or got deleted for this menu
  134. // handler. This also updates the custom menu entries labels and visibility
  135. // as needed. HB
  136. LLMenuGL* getContextMenu();
  137. private:
  138. void updateCustomEntries();
  139. // Context menu actions
  140. static void contextSelectall(void* data);
  141. static void contextCut(void* data);
  142. static void contextCutCustom(void* data);
  143. static void contextCopy(void* data);
  144. static void contextCopyCustom(void* data);
  145. static void contextPaste(void* data);
  146. static void contextPasteCustom(void* data);
  147. static void contextDelete(void* data);
  148. static void contextUndo(void* data);
  149. static void contextRedo(void* data);
  150. static bool contextEnableSelectall(void* data);
  151. static bool contextEnableCut(void* data);
  152. static bool contextEnableCopy(void* data);
  153. static bool contextEnablePaste(void* data);
  154. static bool contextEnableDelete(void* data);
  155. static bool contextEnableUndo(void* data);
  156. static bool contextEnableRedo(void* data);
  157. private:
  158. LLHandle<LLView> mPopupMenuHandle;
  159. U32 mID;
  160. const U32 mContextMenuFlags;
  161. std::string mCustomMenuType;
  162. std::string mCustomCutLabel;
  163. std::string mCustomCopyLabel;
  164. std::string mCustomPasteLabel;
  165. static context_menu_cb_t sContextMenuCallback;
  166. typedef flat_hmap<U32, LLEditMenuHandler*> map_t;
  167. static map_t sMenuHandlersMap;
  168. static U32 sNextID;
  169. };
  170. extern LLEditMenuHandler* gEditMenuHandlerp;
  171. #endif // LL_LLEDITMENUHANDLER_H