ToolsMenu/cx.c

214 рядки
4.2 KiB
C
Executable File

/*
ToolsMenu - Add tools to the Workbench Tools menu
Copyright (C) 2015, 2018 Kim Fastrup Larsen
This program is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, either ver-
sion 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be use-
ful, but WITHOUT ANY WARRANTY; without even the implied war-
ranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public Li-
cense along with this program. If not, see
<http://www.gnu.org/licenses/>.
The author can be contacted on <kimflarsen@hotmail.com>
*/
#include <dos/dos.h>
#include <exec/memory.h>
#include <clib/alib_protos.h>
#include <clib/exec_protos.h>
#ifdef USE_PRAGMAS
#include <proto/exec.h>
#endif
#include <stdlib.h>
#define CATCOMP_NUMBERS
#include "args.h"
#include "catalog.h"
#include "common.h"
#include "cx.h"
#include "gui.h"
#include "io.h"
#include "strings.h"
#include "wb.h"
struct List *tools;
static Bool enabled = YES;
static void free_tool(Tool *tool)
{
FreeVec(tool->node.ln_Name);
FreeVec(tool->path);
FreeMem(tool, sizeof *tool);
}
static Tool *create_tool(char *name, char *path)
{
Tool *t;
if ((t = AllocMem(sizeof *t, MEMF_PUBLIC | MEMF_CLEAR)) == NULL)
return NULL;
if ((t->node.ln_Name = copy_of(name)) == NULL)
goto create_tool_failed;
if ((t->path = copy_of(path)) == NULL)
goto create_tool_failed;
return t;
create_tool_failed:
free_tool(t);
return NULL;
}
static Tool *add_tool(char *name, char *path, struct Node *pred)
{
Tool *tool;
if ((tool = create_tool(name, path)) != NULL) {
Insert(tools, &tool->node, pred);
if (enabled)
wb_add_tool(tool);
}
return tool;
}
static void remove_tool(Tool *tool)
{
if (enabled)
wb_remove_tool(tool);
Remove(&tool->node);
free_tool(tool);
}
static Tool *do_update(Tool *(*f)(Tool *, char *, char *), Tool *tool,
char *name, char *path)
{
Tool *result;
gui_begin_update();
result = (*f)(tool, name, path);
gui_end_update();
return result;
}
static Tool *append(Tool *unused, char *name, char *path)
{
return add_tool(name, path, tools->lh_TailPred);
}
/* Actually creates a new tool and then deletes the old one, to simplify low
memory failure scenarios. If name is blank, it will simply delete the
tool. */
static Tool *modify(Tool *tool, char *name, char *path)
{
Tool *new_tool = NULL;
if (*name != 0 && (new_tool = add_tool(name, path, &tool->node)) == NULL)
return tool;
remove_tool(tool);
return new_tool;
}
static void add_to_menu(void *tool)
{
wb_add_tool((Tool *) tool);
}
static void remove_from_menu(void *tool)
{
wb_remove_tool((Tool *) tool);
}
void cx_set_up()
{
if ((tools = AllocMem(sizeof *tools, MEMF_PUBLIC)) == NULL)
exit(RETURN_FAIL);
NewList(tools);
io_read_definitions(args.exe.dir, args.exe.filename);
}
Tool *cx_add_tool(char *name, char *path)
{
Tool *tool = do_update(append, NULL, name, path);
gui_edit(tool);
return tool;
}
Tool *cx_modify_tool(Tool *tool, char *name, char *path)
{
return do_update(modify, tool, name, path);
}
void cx_enable()
{
if (!enabled) {
enabled = YES;
for_all(tools, add_to_menu);
}
}
void cx_disable()
{
if (enabled) {
enabled = NO;
for_all(tools, remove_from_menu);
}
}
void cx_show()
{
gui_show();
}
void cx_hide()
{
gui_hide();
}
void cx_save_tools()
{
gui_begin_busy();
io_write_definitions(args.exe.dir, args.exe.filename);
gui_end_busy();
}
void cx_about()
{
gui_message(get_string(MSG_GAD_CONTINUE),
TITLE,
COPYRIGHT,
"",
"This program comes with ABSOLUTELY NO WARRANTY.",
"This is free software under the terms of",
"version 3 of the GNU General Public License.",
NULL);
}
void cx_quit()
{
cx_hide();
cx_disable();
exit(RETURN_OK);
}
void cx_clean_up()
{
if (tools != NULL) {
for_all(tools, free_tool);
FreeMem(tools, sizeof *tools);
}
}