lleditmenuhandler.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /**
  2. * @file lleditmenuhandler.cpp
  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. #include "linden_common.h"
  34. #include "lleditmenuhandler.h"
  35. #include "llmenugl.h"
  36. // Global variable
  37. LLEditMenuHandler* gEditMenuHandlerp = NULL;
  38. // Static member variable
  39. U32 LLEditMenuHandler::sNextID = 0;
  40. LLEditMenuHandler::map_t LLEditMenuHandler::sMenuHandlersMap;
  41. context_menu_cb_t LLEditMenuHandler::sContextMenuCallback = NULL;
  42. LLEditMenuHandler::LLEditMenuHandler(U32 context_menu_flags)
  43. : mID(sNextID++),
  44. mContextMenuFlags(context_menu_flags)
  45. {
  46. // Avoids a crash on startup seen with some compilers (e.g. clang with LTO)
  47. // due to the fact that LLSelectMgr is an LLEditMenuHandler and gets
  48. // initialized as a global gSelectMgr instance, before sMenuHandlersMap is
  49. // itself initialized on program startup (this is a compiler or linker bug:
  50. // either of them did not properly determine the order in which the modules
  51. // static and global variables/instances must be created on startup)...
  52. // For this kind of LLEditMenuHandler, context_menu_flags==0 since these
  53. // instances are not line or text editors, and they do not care about,
  54. // neither make any use of sMenuHandlersMap anyway. HB
  55. if (context_menu_flags)
  56. {
  57. sMenuHandlersMap.emplace(mID, this);
  58. }
  59. }
  60. //virtual
  61. LLEditMenuHandler::~LLEditMenuHandler()
  62. {
  63. releaseMenuHandler();
  64. if (mContextMenuFlags)
  65. {
  66. sMenuHandlersMap.erase(mID);
  67. }
  68. LLView::deleteViewByHandle(mPopupMenuHandle);
  69. }
  70. void LLEditMenuHandler::grabMenuHandler()
  71. {
  72. gEditMenuHandlerp = this;
  73. }
  74. void LLEditMenuHandler::releaseMenuHandler()
  75. {
  76. if (gEditMenuHandlerp == this)
  77. {
  78. gEditMenuHandlerp = NULL;
  79. }
  80. }
  81. void LLEditMenuHandler::setCustomMenuType(const char* type)
  82. {
  83. if (mContextMenuFlags | HAS_CUSTOM)
  84. {
  85. mCustomMenuType = type;
  86. if (sContextMenuCallback)
  87. {
  88. HBContextMenuData* menudatap = new HBContextMenuData;
  89. menudatap->mHandlerID = mID;
  90. menudatap->mMenuType = mCustomMenuType;
  91. menudatap->mOperation = HBContextMenuData::SET;
  92. sContextMenuCallback(menudatap);
  93. }
  94. }
  95. }
  96. //static
  97. bool LLEditMenuHandler::setCustomMenu(U32 menu_handler_id,
  98. const std::string& cut_label,
  99. const std::string& copy_label,
  100. const std::string& paste_label)
  101. {
  102. map_t::const_iterator it = sMenuHandlersMap.find(menu_handler_id);
  103. if (it == sMenuHandlersMap.end() ||
  104. !(it->second->mContextMenuFlags | HAS_CUSTOM))
  105. {
  106. return false;
  107. }
  108. it->second->setCustomMenu(cut_label, copy_label, paste_label);
  109. return true;
  110. }
  111. //static
  112. void LLEditMenuHandler::contextSelectall(void* data)
  113. {
  114. LLEditMenuHandler* self = (LLEditMenuHandler*)data;
  115. if (self)
  116. {
  117. self->selectAll();
  118. }
  119. }
  120. //static
  121. bool LLEditMenuHandler::contextEnableSelectall(void* data)
  122. {
  123. LLEditMenuHandler* self = (LLEditMenuHandler*)data;
  124. return self && self->canSelectAll();
  125. }
  126. //static
  127. void LLEditMenuHandler::contextCut(void* data)
  128. {
  129. LLEditMenuHandler* self = (LLEditMenuHandler*)data;
  130. if (self)
  131. {
  132. self->cut();
  133. }
  134. }
  135. //static
  136. void LLEditMenuHandler::contextCutCustom(void* data)
  137. {
  138. LLEditMenuHandler* self = (LLEditMenuHandler*)data;
  139. if (!self || !sContextMenuCallback)
  140. {
  141. return;
  142. }
  143. self->cut();
  144. HBContextMenuData* menudatap = new HBContextMenuData;
  145. menudatap->mHandlerID = self->mID;
  146. menudatap->mMenuType = self->mCustomMenuType;
  147. menudatap->mOperation = HBContextMenuData::CUT;
  148. sContextMenuCallback(menudatap);
  149. }
  150. //static
  151. bool LLEditMenuHandler::contextEnableCut(void* data)
  152. {
  153. LLEditMenuHandler* self = (LLEditMenuHandler*)data;
  154. return self && self->canCut();
  155. }
  156. //static
  157. void LLEditMenuHandler::contextCopy(void* data)
  158. {
  159. LLEditMenuHandler* self = (LLEditMenuHandler*)data;
  160. if (self)
  161. {
  162. self->copy();
  163. }
  164. }
  165. void LLEditMenuHandler::contextCopyCustom(void* data)
  166. {
  167. LLEditMenuHandler* self = (LLEditMenuHandler*)data;
  168. if (!self || !sContextMenuCallback)
  169. {
  170. return;
  171. }
  172. self->copy();
  173. HBContextMenuData* menudatap = new HBContextMenuData;
  174. menudatap->mHandlerID = self->mID;
  175. menudatap->mMenuType = self->mCustomMenuType;
  176. menudatap->mOperation = HBContextMenuData::COPY;
  177. sContextMenuCallback(menudatap);
  178. }
  179. //static
  180. bool LLEditMenuHandler::contextEnableCopy(void* data)
  181. {
  182. LLEditMenuHandler* self = (LLEditMenuHandler*)data;
  183. return self && self->canCopy();
  184. }
  185. //static
  186. void LLEditMenuHandler::contextPaste(void* data)
  187. {
  188. LLEditMenuHandler* self = (LLEditMenuHandler*)data;
  189. if (self)
  190. {
  191. self->paste();
  192. }
  193. }
  194. //static
  195. void LLEditMenuHandler::contextPasteCustom(void* data)
  196. {
  197. LLEditMenuHandler* self = (LLEditMenuHandler*)data;
  198. if (!self || !sContextMenuCallback)
  199. {
  200. return;
  201. }
  202. HBContextMenuData* menudatap = new HBContextMenuData;
  203. menudatap->mHandlerID = self->mID;
  204. menudatap->mMenuType = self->mCustomMenuType;
  205. menudatap->mOperation = HBContextMenuData::PASTE;
  206. sContextMenuCallback(menudatap);
  207. }
  208. //static
  209. bool LLEditMenuHandler::pasteTo(U32 id)
  210. {
  211. map_t::const_iterator it = sMenuHandlersMap.find(id);
  212. if (it != sMenuHandlersMap.end() && it->second->canPaste())
  213. {
  214. it->second->paste();
  215. return true;
  216. }
  217. return false;
  218. }
  219. //static
  220. bool LLEditMenuHandler::contextEnablePaste(void* data)
  221. {
  222. LLEditMenuHandler* self = (LLEditMenuHandler*)data;
  223. return self && self->canPaste();
  224. }
  225. //static
  226. void LLEditMenuHandler::contextDelete(void* data)
  227. {
  228. LLEditMenuHandler* self = (LLEditMenuHandler*)data;
  229. if (self)
  230. {
  231. self->doDelete();
  232. }
  233. }
  234. //static
  235. bool LLEditMenuHandler::contextEnableDelete(void* data)
  236. {
  237. LLEditMenuHandler* self = (LLEditMenuHandler*)data;
  238. return self && self->canDoDelete();
  239. }
  240. //static
  241. void LLEditMenuHandler::contextUndo(void* data)
  242. {
  243. LLEditMenuHandler* self = (LLEditMenuHandler*)data;
  244. if (self)
  245. {
  246. self->undo();
  247. }
  248. }
  249. //static
  250. bool LLEditMenuHandler::contextEnableUndo(void* data)
  251. {
  252. LLEditMenuHandler* self = (LLEditMenuHandler*)data;
  253. return self && self->canUndo();
  254. }
  255. //static
  256. void LLEditMenuHandler::contextRedo(void* data)
  257. {
  258. LLEditMenuHandler* self = (LLEditMenuHandler*)data;
  259. if (self)
  260. {
  261. self->redo();
  262. }
  263. }
  264. //static
  265. bool LLEditMenuHandler::contextEnableRedo(void* data)
  266. {
  267. LLEditMenuHandler* self = (LLEditMenuHandler*)data;
  268. return self && self->canRedo();
  269. }
  270. LLMenuGL* LLEditMenuHandler::createContextMenu(bool with_spell_separator)
  271. {
  272. if (!mContextMenuFlags)
  273. {
  274. return NULL;
  275. }
  276. LLMenuGL* menup = (LLMenuGL*)mPopupMenuHandle.get();
  277. if (menup)
  278. {
  279. return menup;
  280. }
  281. menup = new LLMenuGL("editor_context_menu");
  282. mPopupMenuHandle = menup->getHandle();
  283. // *TODO: translate
  284. menup->append(new LLMenuItemCallGL("Select all", contextSelectall,
  285. contextEnableSelectall, this));
  286. menup->appendSeparator("sep1");
  287. menup->append(new LLMenuItemCallGL("Cut", contextCut,
  288. contextEnableCut, this));
  289. menup->append(new LLMenuItemCallGL("Copy", contextCopy,
  290. contextEnableCopy, this));
  291. menup->append(new LLMenuItemCallGL("Paste", contextPaste,
  292. contextEnablePaste, this));
  293. menup->append(new LLMenuItemCallGL("Delete", contextDelete,
  294. contextEnableDelete, this));
  295. if (mContextMenuFlags | HAS_UNDO_REDO)
  296. {
  297. menup->append(new LLMenuItemCallGL("Undo", contextUndo,
  298. contextEnableUndo, this));
  299. menup->append(new LLMenuItemCallGL("Redo", contextRedo,
  300. contextEnableRedo, this));
  301. }
  302. if (mContextMenuFlags | HAS_CUSTOM)
  303. {
  304. menup->appendSeparator("custom_sep");
  305. menup->append(new LLMenuItemCallGL("Custom cut", contextCutCustom,
  306. contextEnableCut, this));
  307. menup->append(new LLMenuItemCallGL("Custom copy", contextCopyCustom,
  308. contextEnableCopy, this));
  309. menup->append(new LLMenuItemCallGL("Custom paste", contextPasteCustom,
  310. contextEnablePaste, this));
  311. updateCustomEntries();
  312. }
  313. if (with_spell_separator)
  314. {
  315. menup->appendSeparator("spell_sep");
  316. menup->setItemVisible("spell_sep", false);
  317. }
  318. menup->setCanTearOff(false);
  319. menup->setVisible(false);
  320. return menup;
  321. }
  322. LLMenuGL* LLEditMenuHandler::getContextMenu()
  323. {
  324. LLMenuGL* menup = (LLMenuGL*)mPopupMenuHandle.get();
  325. if (menup && (mContextMenuFlags | HAS_CUSTOM))
  326. {
  327. updateCustomEntries();
  328. }
  329. return menup;
  330. }
  331. void LLEditMenuHandler::updateCustomEntries()
  332. {
  333. LLMenuGL* menup = (LLMenuGL*)mPopupMenuHandle.get();
  334. if (!menup || !(mContextMenuFlags | HAS_CUSTOM))
  335. {
  336. return;
  337. }
  338. bool has_custom_callback = sContextMenuCallback != NULL;
  339. bool sep_visible = false;
  340. LLMenuItemGL* itemp = menup->getItem("Custom cut");
  341. if (itemp)
  342. {
  343. if (!has_custom_callback || mCustomCutLabel.empty())
  344. {
  345. itemp->setVisible(false);
  346. }
  347. else
  348. {
  349. itemp->setLabel(mCustomCutLabel);
  350. itemp->setVisible(true);
  351. sep_visible = true;
  352. }
  353. }
  354. itemp = menup->getItem("Custom copy");
  355. if (itemp)
  356. {
  357. if (!has_custom_callback || mCustomCopyLabel.empty())
  358. {
  359. itemp->setVisible(false);
  360. }
  361. else
  362. {
  363. itemp->setLabel(mCustomCopyLabel);
  364. itemp->setVisible(true);
  365. sep_visible = true;
  366. }
  367. }
  368. itemp = menup->getItem("Custom paste");
  369. if (itemp)
  370. {
  371. if (!has_custom_callback || mCustomPasteLabel.empty())
  372. {
  373. itemp->setVisible(false);
  374. }
  375. else
  376. {
  377. itemp->setLabel(mCustomPasteLabel);
  378. itemp->setVisible(true);
  379. sep_visible = true;
  380. }
  381. }
  382. menup->setItemVisible("custom_sep", sep_visible);
  383. }