Add tools to the Workbench Tools menu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cx.c 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. ToolsMenu - Add tools to the Workbench Tools menu
  3. Copyright (C) 2015, 2018 Kim Fastrup Larsen
  4. This program is free software: you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation, either ver-
  7. sion 3 of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be use-
  9. ful, but WITHOUT ANY WARRANTY; without even the implied war-
  10. ranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. See the GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public Li-
  13. cense along with this program. If not, see
  14. <http://www.gnu.org/licenses/>.
  15. The author can be contacted on <kimflarsen@hotmail.com>
  16. */
  17. #include <dos/dos.h>
  18. #include <exec/memory.h>
  19. #include <clib/alib_protos.h>
  20. #include <clib/exec_protos.h>
  21. #ifdef USE_PRAGMAS
  22. #include <proto/exec.h>
  23. #endif
  24. #include <stdlib.h>
  25. #define CATCOMP_NUMBERS
  26. #include "args.h"
  27. #include "catalog.h"
  28. #include "common.h"
  29. #include "cx.h"
  30. #include "gui.h"
  31. #include "io.h"
  32. #include "strings.h"
  33. #include "wb.h"
  34. struct List *tools;
  35. static Bool enabled = YES;
  36. static void free_tool(Tool *tool)
  37. {
  38. FreeVec(tool->node.ln_Name);
  39. FreeVec(tool->path);
  40. FreeMem(tool, sizeof *tool);
  41. }
  42. static Tool *create_tool(char *name, char *path)
  43. {
  44. Tool *t;
  45. if ((t = AllocMem(sizeof *t, MEMF_PUBLIC | MEMF_CLEAR)) == NULL)
  46. return NULL;
  47. if ((t->node.ln_Name = copy_of(name)) == NULL)
  48. goto create_tool_failed;
  49. if ((t->path = copy_of(path)) == NULL)
  50. goto create_tool_failed;
  51. return t;
  52. create_tool_failed:
  53. free_tool(t);
  54. return NULL;
  55. }
  56. static Tool *add_tool(char *name, char *path, struct Node *pred)
  57. {
  58. Tool *tool;
  59. if ((tool = create_tool(name, path)) != NULL) {
  60. Insert(tools, &tool->node, pred);
  61. if (enabled)
  62. wb_add_tool(tool);
  63. }
  64. return tool;
  65. }
  66. static void remove_tool(Tool *tool)
  67. {
  68. if (enabled)
  69. wb_remove_tool(tool);
  70. Remove(&tool->node);
  71. free_tool(tool);
  72. }
  73. static Tool *do_update(Tool *(*f)(Tool *, char *, char *), Tool *tool,
  74. char *name, char *path)
  75. {
  76. Tool *result;
  77. gui_begin_update();
  78. result = (*f)(tool, name, path);
  79. gui_end_update();
  80. return result;
  81. }
  82. static Tool *append(Tool *unused, char *name, char *path)
  83. {
  84. return add_tool(name, path, tools->lh_TailPred);
  85. }
  86. /* Actually creates a new tool and then deletes the old one, to simplify low
  87. memory failure scenarios. If name is blank, it will simply delete the
  88. tool. */
  89. static Tool *modify(Tool *tool, char *name, char *path)
  90. {
  91. Tool *new_tool = NULL;
  92. if (*name != 0 && (new_tool = add_tool(name, path, &tool->node)) == NULL)
  93. return tool;
  94. remove_tool(tool);
  95. return new_tool;
  96. }
  97. static void add_to_menu(void *tool)
  98. {
  99. wb_add_tool((Tool *) tool);
  100. }
  101. static void remove_from_menu(void *tool)
  102. {
  103. wb_remove_tool((Tool *) tool);
  104. }
  105. void cx_set_up()
  106. {
  107. if ((tools = AllocMem(sizeof *tools, MEMF_PUBLIC)) == NULL)
  108. exit(RETURN_FAIL);
  109. NewList(tools);
  110. io_read_definitions(args.exe.dir, args.exe.filename);
  111. }
  112. Tool *cx_add_tool(char *name, char *path)
  113. {
  114. Tool *tool = do_update(append, NULL, name, path);
  115. gui_edit(tool);
  116. return tool;
  117. }
  118. Tool *cx_modify_tool(Tool *tool, char *name, char *path)
  119. {
  120. return do_update(modify, tool, name, path);
  121. }
  122. void cx_enable()
  123. {
  124. if (!enabled) {
  125. enabled = YES;
  126. for_all(tools, add_to_menu);
  127. }
  128. }
  129. void cx_disable()
  130. {
  131. if (enabled) {
  132. enabled = NO;
  133. for_all(tools, remove_from_menu);
  134. }
  135. }
  136. void cx_show()
  137. {
  138. gui_show();
  139. }
  140. void cx_hide()
  141. {
  142. gui_hide();
  143. }
  144. void cx_save_tools()
  145. {
  146. gui_begin_busy();
  147. io_write_definitions(args.exe.dir, args.exe.filename);
  148. gui_end_busy();
  149. }
  150. void cx_about()
  151. {
  152. gui_message(get_string(MSG_GAD_CONTINUE),
  153. TITLE,
  154. COPYRIGHT,
  155. "",
  156. "This program comes with ABSOLUTELY NO WARRANTY.",
  157. "This is free software under the terms of",
  158. "version 3 of the GNU General Public License.",
  159. NULL);
  160. }
  161. void cx_quit()
  162. {
  163. cx_hide();
  164. cx_disable();
  165. exit(RETURN_OK);
  166. }
  167. void cx_clean_up()
  168. {
  169. if (tools != NULL) {
  170. for_all(tools, free_tool);
  171. FreeMem(tools, sizeof *tools);
  172. }
  173. }