MainMenu.xys 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*********************
  2. @Author : liuyuqi
  3. @Contact : liuyuqi.gov@msn.cn
  4. @Time : 2019/07/23 06:04:23
  5. @Version : 1.0
  6. @License : (C)Copyright 2019 liuyuqi.
  7. @Desc : None
  8. *********************/
  9. "User Tweaks||4 : _getTweaks"
  10. Global $GT_FORCE_INI_USE = false;
  11. Global $GT_INI_FILE = "<xyscripts>\.config\TheQwertysScripts.ini";
  12. Global $GT_INI_SECTION = 'MAIN_MENU';
  13. Global $GT_UPDATE_MENU_ON_BETAS = false;
  14. "- : _-" //---------------------------------------------------------------------
  15. "_Initialize"
  16. Global $G_INITIALIZED;
  17. if (! $G_INITIALIZED) {
  18. Status 'Initializing script...',, 'progress';
  19. Sub('_readOptions');
  20. }
  21. $G_INITIALIZED = true;
  22. "Get Configuration||4 : _readOptions"
  23. Status 'Reading configuration...',, 'progress';
  24. Sub('_getTweaks');
  25. Global $GT_INI_FILE, $GT_FORCE_INI_USE, $GT_INI_SECTION;
  26. Global $GS_LAST_MODIFIED;
  27. Global $GS_MENU_TITLES, $GS_MENU_CMD_DATA_SPLITTER, $GS_MENU_CMD_LIST, $GS_XY_VERSION;
  28. if ($GT_FORCE_INI_USE) {
  29. $ScriptRetainPVs = false;
  30. } else {
  31. $ScriptRetainPVs = GetKey('ScriptRetainPVs', 'Settings');
  32. }
  33. if ($ScriptRetainPVs && IsSet($P_THEQWERTY_MAIN_MENU_LAST_MODIFIED)) {
  34. Status 'Read configuration from permanent variables.',, 'progress';
  35. $GS_LAST_MODIFIED = $P_THEQWERTY_MAIN_MENU_LAST_MODIFIED;
  36. // Options
  37. $GS_MENU_TITLES = $P_THEQWERTY_MAIN_MENU_MENU_TITLES;
  38. $GS_MENU_CMD_DATA_SPLITTER = $P_THEQWERTY_MAIN_MENU_MENU_CMD_DATA_SPLITTER;
  39. $GS_MENU_CMD_LIST = $P_THEQWERTY_MAIN_MENU_MENU_CMD_LIST;
  40. $GS_XY_VERSION = $P_THEQWERTY_MAIN_MENU_XY_VERSION;
  41. } else {
  42. if (1 == Exists($GT_INI_FILE)) {
  43. $iniOptions = GetKey('XY_Version', $GT_INI_SECTION, $GT_INI_FILE);
  44. if ($iniOptions) {
  45. Status 'Read configuration from INI file.',, 'progress';
  46. $GS_LAST_MODIFIED = GetKey('LAST_MODIFIED', $GT_INI_SECTION, $GT_INI_FILE);
  47. // Options
  48. $GS_MENU_TITLES = GetKey('Menu_Titles', $GT_INI_SECTION, $GT_INI_FILE);
  49. $GS_MENU_CMD_DATA_SPLITTER = GetKey('Menu_Cmd_Data_Splitter', $GT_INI_SECTION, $GT_INI_FILE);
  50. $GS_MENU_CMD_LIST = GetKey('Menu_Cmd_List', $GT_INI_SECTION, $GT_INI_FILE);
  51. $GS_XY_VERSION = GetKey('XY_Version', $GT_INI_SECTION, $GT_INI_FILE);
  52. // Extra handling for INI files.
  53. $GS_MENU_CMD_LIST = Replace($GS_MENU_CMD_LIST, '¶', "<crlf>");
  54. $GS_MENU_CMD_LIST = Replace($GS_MENU_CMD_LIST, '&PILCROW;', '¶');
  55. }
  56. Unset $iniOptions;
  57. }
  58. }
  59. Unset $ScriptRetainPVs;
  60. "Set Configuration||4 : _setOptions"
  61. Status 'Saving configuration...',, 'progress';
  62. Sub('_getTweaks');
  63. Global $GT_INI_FILE, $GT_FORCE_INI_USE, $GT_INI_SECTION;
  64. Global $GS_LAST_MODIFIED = Now('yyyy-mm-dd_hh.nn.ss');
  65. Global $GS_MENU_TITLES, $GS_MENU_CMD_DATA_SPLITTER, $GS_MENU_CMD_LIST, $GS_XY_VERSION;
  66. if ($GT_FORCE_INI_USE) {
  67. $ScriptRetainPVs = false;
  68. } else {
  69. $ScriptRetainPVs = GetKey('ScriptRetainPVs', 'Settings');
  70. }
  71. if ($ScriptRetainPVs) {
  72. Perm $P_THEQWERTY_MAIN_MENU_LAST_MODIFIED = $GS_LAST_MODIFIED;
  73. // Options
  74. Perm $P_THEQWERTY_MAIN_MENU_MENU_TITLES = $GS_MENU_TITLES;
  75. Perm $P_THEQWERTY_MAIN_MENU_MENU_CMD_DATA_SPLITTER = $GS_MENU_CMD_DATA_SPLITTER;
  76. Perm $P_THEQWERTY_MAIN_MENU_MENU_CMD_LIST = $GS_MENU_CMD_LIST;
  77. Perm $P_THEQWERTY_MAIN_MENU_XY_VERSION = $GS_XY_VERSION;
  78. Status 'Saved configuration to permanent variable.',, 'progress';
  79. } else {
  80. if (1 != Exists($GT_INI_FILE)) {
  81. Status 'Creating INI file...',, 'progress';
  82. New($GT_INI_FILE, 'file');
  83. }
  84. // Extra handling for INI files.
  85. $cmdList = Replace($GS_MENU_CMD_LIST, '¶', '&PILCROW;');
  86. $cmdList = Replace($cmdList, "<crlf>", '¶');
  87. SetKey $GS_LAST_MODIFIED, 'LAST_MODIFIED', $GT_INI_SECTION, $GT_INI_FILE;
  88. // Options
  89. SetKey $GS_MENU_TITLES, 'Menu_Titles', $GT_INI_SECTION, $GT_INI_FILE;
  90. SetKey $GS_MENU_CMD_DATA_SPLITTER, 'Menu_Cmd_Data_Splitter', $GT_INI_SECTION, $GT_INI_FILE;
  91. SetKey $cmdList, 'Menu_Cmd_List', $GT_INI_SECTION, $GT_INI_FILE, 1;
  92. SetKey $GS_XY_VERSION, 'XY_Version', $GT_INI_SECTION, $GT_INI_FILE;
  93. Status 'Saved configuration to INI file.',, 'progress';
  94. Unset $cmdList;
  95. }
  96. Unset $ScriptRetainPVs;
  97. "- : _-" //---------------------------------------------------------------------
  98. "Retrieve Commands||4 : _getCommands"
  99. Status 'Retrieving command list...',, 'progress';
  100. Global $G_CMD_LIST;
  101. Msg <<<#INSTRUCTIONS
  102. To recreate the menu this script needs some help from you.
  103. You will only be asked to do this once per stable release of XYplorer.
  104. When the List of All Commands Dialog appears:
  105. 1) Right-click the list of commands.
  106. 2) Select 'Copy All Items'.
  107. 3) Close the List of All Commands dialog by clicking 'Cancel'.
  108. After this the text of your clipboard will be restored.
  109. #INSTRUCTIONS, 1;
  110. $cb = "<clipboard>";
  111. CopyText '';
  112. Status "In 'List of All Commands' dialog: Right-Click > 'Copy All Items' then Cancel.",, 'progress';
  113. #705; //List of Functions
  114. $G_CMD_LIST = "<clipboard>";
  115. CopyText $cb;
  116. Status 'Clipboard restored.',, 'progress';
  117. Unset $cb;
  118. End $G_CMD_LIST == '', <<<#END_MSG
  119. It appears you didn't copy the items from the
  120. List of All Commands dialog.
  121. This script cannot continue without that information.
  122. #END_MSG;
  123. "Create Menu Data||4 : _genMenuData"
  124. Status 'Creating menu data....',, 'progress';
  125. $ME = Self('Caption');
  126. Unset $GS_MENU_TITLES, $GS_MENU_CMD_DATA_SPLITTER, $GS_MENU_CMD_LIST, $GS_XY_VERSION;
  127. Global $G_CMD_LIST = '';
  128. Sub('_getCommands');
  129. End $G_CMD_LIST == '', "Error [$ME]: Command list is empty.";
  130. $__CMD_DATA_SPLITTER__ = "__CMD_DATA_SPLITTER__";
  131. $cmdList = ''; // List of menu items.
  132. $titleList = ''; // List of menu titles.
  133. $assigned = ''; // List of assigned accelerator keys.
  134. $cSub = ''; // Name of the currently being processed submenu.
  135. foreach ($line, $G_CMD_LIST, "<crlf>") {
  136. if (! $line) { continue; }
  137. // Description
  138. $des = GetToken($line, 2, "<tab>", 't', 2);
  139. // Continue if we're within a submenu.
  140. if ($des Like "$cSub | *") { continue; }
  141. $cSub = '';
  142. // Start a new submenu.
  143. if ($des LikeI '*- submenu -*') {
  144. $cSub = RegexReplace($des, ' \| - submenu -.*?$');
  145. }
  146. // Command ID
  147. $cid = GetToken($line, 1, "<tab>", 't');
  148. $cid = SubStr($cid, 1);
  149. // Caption (from Command ID)
  150. $caption = Get('MenuCaption', $cid, 1);
  151. // Menu Title (from Description)
  152. $title = GetToken($des, 1, ' | ', 't', 1);
  153. // Add to our list of menu titles.
  154. if (Replace("|$titleList|", '&') UnLike "*|$title|*") {
  155. $ampCount = GetToken(RegexMatches($title, '&', '|'), 'Count', '|');
  156. if ($ampCount == 0 || $ampCount % 2 != 0) {
  157. $i = 0;
  158. $len = StrLen($title);
  159. // Locate a character we can make the accelerator.
  160. while ($i < $len) {
  161. $char = SubStr($title, $i, 1);
  162. $i++;
  163. // We will cheat and only add them to [a-z].
  164. if (! RegexMatches($char, '[a-z]')) { continue; }
  165. // Avoid duplicates.
  166. if ($assigned UnLikeI "*$char*") {
  167. $assigned = $assigned . $char;
  168. $title = Replace($title, $char, "&$char", 0, 1, 1);
  169. break;
  170. }
  171. }
  172. Unset $i, $len, $char;
  173. }
  174. $titleList = $titleList . "|$title";
  175. }
  176. // Build command list.
  177. $cmdList = $cmdList . GetToken($titleList, -1, '|') . " | $caption$__CMD_DATA_SPLITTER__$cid<crlf>";
  178. }
  179. Unset $assigned, $cSub, $line, $des, $cid, $caption, $title, $ampCount, $G_CMD_LIST;
  180. $titleList = SubStr($titleList, 1); // Remove leading pipe.
  181. Global $GS_MENU_TITLES = $titleList;
  182. End $GS_MENU_TITLES == '', "Error [$ME]: Failed to generate menu title list.";
  183. Global $GS_MENU_CMD_DATA_SPLITTER = $__CMD_DATA_SPLITTER__;
  184. End $GS_MENU_CMD_DATA_SPLITTER == '', "Error [$ME]: Failed to generate command / data splitter.";
  185. Global $GS_MENU_CMD_LIST = $cmdList;
  186. End $GS_MENU_CMD_LIST == '', "Error [$ME]: Failed to generate command list.";
  187. Global $GS_XY_VERSION = "<xyver>";
  188. End $GS_XY_VERSION == '', "Error [$ME]: Failed to retrieve XYplorer version.";
  189. Unset $__CMD_DATA_SPLITTER__, $cmdList, $titleList, $ME;
  190. "Refresh Menu Data||4 : _refreshMenuData"
  191. Status 'Checking freshness of menu data....',, 'progress';
  192. Global $GS_MENU_TITLES, $GS_MENU_CMD_DATA_SPLITTER, $GS_MENU_CMD_LIST, $GS_XY_VERSION;
  193. $update = $GS_MENU_TITLES == '';
  194. $update = $update || $GS_MENU_CMD_DATA_SPLITTER == '';
  195. $update = $update || $GS_MENU_CMD_LIST == '';
  196. $update = $update || $GS_XY_VERSION == '';
  197. if (! $update) {
  198. Global $GT_UPDATE_MENU_ON_BETAS;
  199. if ($GT_UPDATE_MENU_ON_BETAS) {
  200. $idx = -1; // Use the full version.
  201. } else {
  202. $idx = 2; // Use the first two tokens.
  203. }
  204. $menuVer = GetToken($GS_XY_VERSION, $idx, '.', 't', 1);
  205. $xyVer = GetToken("<xyver>", $idx, '.', 't', 1);
  206. $update = $menuVer < $xyVer;
  207. Unset $idx, $menuVer, $xyVer;
  208. }
  209. if ($update) {
  210. Sub '_updateMenuData';
  211. }
  212. Unset $update;
  213. "Update Menu Data : _updateMenuData"
  214. Status 'Updating menu data...',, 'progress';
  215. Sub '_genMenuData';
  216. Sub '_setOptions';
  217. "- : _-" //---------------------------------------------------------------------
  218. "Show Menu||1 : showMenu"
  219. Status 'Preparing to show menu...',, 'progress';
  220. $ME = Self('Caption');
  221. Sub '_Initialize';
  222. Sub '_refreshMenuData';
  223. Global $GS_MENU_TITLES;
  224. End $GS_MENU_TITLES == '', "Error [$ME]: Missing menu title list.";
  225. Global $GS_MENU_CMD_DATA_SPLITTER;
  226. End $GS_MENU_CMD_DATA_SPLITTER == '', "Error [$ME]: Missing command / data splitter.";
  227. Global $GS_MENU_CMD_LIST;
  228. End $GS_MENU_CMD_LIST == '', "Error [$ME]: Missing command list.";
  229. Status 'Showing menu titles...',, 'progress';
  230. $response = PopUpMenu($GS_MENU_TITLES . '|-|-|&>>>',,,,,, '|');
  231. End ! $response;
  232. if ($response == '&>>>') {
  233. Status 'Displaying script menu...',, 'progress';
  234. Load '*', 'showMenu;_updateMenuData;-;_createCallerScript;_createCTB;-;_toggleMenu';
  235. } else {
  236. // Filter the command list based on user's title selection.
  237. $list = FormatList($GS_MENU_CMD_LIST, 'f', "<crlf>", "$response | *");
  238. $list = RegexReplace($list, "^$response\s*\|\s*");
  239. // Show title's corresponding menu.
  240. Status 'Displaying menu...',,' progress';
  241. $response = PopUpMenu($list,,,,,, "<crlf>", $GS_MENU_CMD_DATA_SPLITTER);
  242. End ! $response;
  243. Status "Executing #$response: " . Get('MenuCaption', $response, 2),, 'ready';
  244. Load "#$response;",, 's';
  245. Unset $response, $list, $ME;
  246. }
  247. "Create Script to Show Menu : _createCallerScript"
  248. Status 'Generating calling script...',, 'progress';
  249. $scriptFile = Self('File');
  250. End ! $scriptFile, 'This requires the main script be saved to a file.';
  251. Status 'Displaying calling script.',, 'ready';
  252. Text <<<#SCRIPT
  253. Load '$scriptFile', 'showMenu', 'f';
  254. #SCRIPT, , , 'Script to Show Menu';
  255. Unset $scriptFile;
  256. "Create Custom Toolbar Button : _createCTB"
  257. Status 'Generating custom toolbar button...',, 'progress';
  258. $scriptFile = Self('File');
  259. End ! $scriptFile, 'This requires the main script be saved to a file.';
  260. $unresolvedIconPath = '<xypath>\<xyexe>';
  261. Snippet <<<#SNIPPET
  262. Snip: CTB 1
  263. XYplorer <xyver>, <date>
  264. Action
  265. NewUserButton
  266. Name
  267. Show Menu
  268. Icon
  269. $unresolvedIconPath
  270. ScriptL
  271. Load '$scriptFile', 'showMenu', 'f';
  272. ScriptR
  273. "Update Menu Data" Load '$scriptFile', '_updateMenuData', 'f';
  274. "Create Script to Show Menu" Load '$scriptFile', '_createCallerScript', 'f';
  275. "Show/Hide Main Menu" Load '$scriptFile', '_toggleMenu', 'f';
  276. FireClick
  277. 1
  278. #SNIPPET;
  279. Status 'Custom Toolbar Button created.',, 'ready';
  280. Unset $scriptFile, $unresolvedIconPath;
  281. "Show/Hide Main Menu : _toggleMenu"
  282. Status 'Toggling main menu display....',, 'progress';
  283. #1061; //Miscellaneous | General Functions | Show/Hide Main Menu
  284. Status 'Toggled main menu display.',, 'ready';