aseprite/src/modules/rootmenu.c

332 lines
8.8 KiB
C
Raw Normal View History

2007-11-16 18:25:45 +00:00
/* ASE - Allegro Sprite Editor
2007-09-23 20:13:58 +00:00
* Copyright (C) 2001-2005, 2007 David A. Capello
2007-09-18 23:57:02 +00:00
*
* 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 version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty 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 License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#ifndef USE_PRECOMPILED_HEADER
#include <stdio.h>
#include <string.h>
#include "jinete/jinete.h"
2007-09-18 23:57:02 +00:00
2007-09-23 20:13:58 +00:00
#include "commands/commands.h"
#include "console/console.h"
2007-09-18 23:57:02 +00:00
#include "core/app.h"
#include "core/core.h"
#include "core/dirs.h"
#include "intl/intl.h"
#include "modules/rootmenu.h"
#include "util/filetoks.h"
2007-09-23 20:13:58 +00:00
#include "widgets/menuitem.h"
2007-09-18 23:57:02 +00:00
#endif
static JWidget root_menu;
static JWidget recent_list_menuitem;
static JWidget layer_popup_menuitem;
static JWidget frame_popup_menuitem;
static JWidget cel_popup_menuitem;
2007-09-18 23:57:02 +00:00
static JWidget filters_popup_menuitem;
2007-09-23 20:13:58 +00:00
static JWidget convert_xmlelem_to_menu(JXmlElem elem);
static JWidget convert_xmlelem_to_menuitem(JXmlElem elem);
static void apply_shortcut_to_menuitems_with_command(JWidget menu, Command *command);
int init_module_rootmenu(void)
2007-09-18 23:57:02 +00:00
{
root_menu = 0;
2007-09-23 20:13:58 +00:00
return load_root_menu();
2007-09-18 23:57:02 +00:00
}
2007-09-23 20:13:58 +00:00
void exit_module_rootmenu(void)
2007-09-18 23:57:02 +00:00
{
recent_list_menuitem = NULL;
layer_popup_menuitem = NULL;
frame_popup_menuitem = NULL;
cel_popup_menuitem = NULL;
filters_popup_menuitem = NULL;
2007-09-18 23:57:02 +00:00
command_reset_keys();
2007-09-23 20:13:58 +00:00
jwidget_free(root_menu);
2007-09-18 23:57:02 +00:00
}
2007-09-23 20:13:58 +00:00
int load_root_menu(void)
2007-09-18 23:57:02 +00:00
{
2007-09-23 20:13:58 +00:00
JLink link, link2;
2007-09-18 23:57:02 +00:00
DIRS *dirs, *dir;
2007-09-23 20:13:58 +00:00
JXmlElem elem;
JXml xml;
2007-09-18 23:57:02 +00:00
2007-09-23 20:13:58 +00:00
if (app_get_menu_bar())
jmenubar_set_menu(app_get_menu_bar(), NULL);
2007-09-18 23:57:02 +00:00
/* destroy `root-menu' if it exists */
if (root_menu) {
command_reset_keys();
2007-09-23 20:13:58 +00:00
jwidget_free(root_menu);
}
2007-09-18 23:57:02 +00:00
/* create a new empty-menu */
2007-09-23 20:13:58 +00:00
root_menu = NULL;
recent_list_menuitem = NULL;
layer_popup_menuitem = NULL;
frame_popup_menuitem = NULL;
cel_popup_menuitem = NULL;
filters_popup_menuitem = NULL;
2007-09-18 23:57:02 +00:00
2007-09-23 20:13:58 +00:00
dirs = filename_in_datadir("usergui.xml");
{
char buf[256];
2007-11-08 00:54:31 +00:00
sprintf(buf, "gui-%s.xml", intl_get_lang());
dirs_cat_dirs(dirs, filename_in_datadir(buf));
2007-11-08 00:54:31 +00:00
dirs_cat_dirs(dirs, filename_in_datadir("gui-en.xml"));
}
2007-09-18 23:57:02 +00:00
for (dir=dirs; dir; dir=dir->next) {
2007-09-23 20:13:58 +00:00
PRINTF("Trying to load GUI definition file from \"%s\"...\n", dir->path);
/* open the XML menu definition file */
xml = jxml_new_from_file(dir->path);
if (xml && jxml_get_root(xml)) {
/**************************************************/
/* load menus */
PRINTF("Trying to menus from \"%s\"...\n", dir->path);
/* get the <menu> element with id="main_menu" */
elem = jxml_get_elem_by_id(xml, "main_menu");
if (elem) {
/* is it a <menu> element? */
if (strcmp(jxmlelem_get_name(elem), "menu") == 0) {
/* ok, convert it to a menu JWidget */
root_menu = convert_xmlelem_to_menu(elem);
if (!root_menu) {
PRINTF("Error loading main menu from \"%s\" file.\n", dir->path);
return -1;
}
}
else {
PRINTF("Invalid element with id=\"main_menu\" in \"%s\"\n", dir->path);
return -1;
}
}
else {
PRINTF("main_menu element couldn't be found...\n");
2007-09-18 23:57:02 +00:00
}
2007-09-23 20:13:58 +00:00
/**************************************************/
/* load keyboard shortcuts */
PRINTF("Trying to load keyboard shortcuts from \"%s\"...\n", dir->path);
/* find the <keyboard> element */
JI_LIST_FOR_EACH(((JXmlNode)jxml_get_root(xml))->children, link) {
JXmlNode child = (JXmlNode)link->data;
/* is it <keyboard>? */
if (child->type == JI_XML_ELEM &&
strcmp(jxmlelem_get_name((JXmlElem)child), "keyboard") == 0) {
/* for each children in <keyboard>...</keyboard> */
JI_LIST_FOR_EACH(child->children, link2) {
JXmlNode child2 = (JXmlNode)link2->data;
/* it is a <key> element? */
if (child2->type == JI_XML_ELEM &&
strcmp(jxmlelem_get_name((JXmlElem)child2), "key") == 0) {
/* finally, we can read the <key /> */
const char *command_name = jxmlelem_get_attr((JXmlElem)child2, "command");
const char *command_key = jxmlelem_get_attr((JXmlElem)child2, "shortcut");
if (command_name && command_key) {
Command *command = command_get_by_name(command_name);
if (command) {
bool first_shortcut = !command->accel;
/* add the keyboard shortcut to the command */
command_add_key(command, command_key);
/* add the shortcut to the menuitems with this
command (this is only visual, the
"manager_msg_proc" is the only one that process
keyboard shortcuts) */
if (first_shortcut)
apply_shortcut_to_menuitems_with_command(root_menu, command);
}
}
}
}
break;
}
}
/* free the XML file */
jxml_free(xml);
}
2007-09-18 23:57:02 +00:00
2007-09-23 20:13:58 +00:00
if (root_menu) {
PRINTF("Main menu loaded.\n");
2007-09-18 23:57:02 +00:00
break;
}
}
2007-09-23 20:13:58 +00:00
dirs_free(dirs);
/* no menus? */
if (!root_menu) {
user_printf(_("Error loading main menu\n"));
return -1;
}
2007-09-18 23:57:02 +00:00
/* sets the "menu" of the "menu-bar" to the new "root-menu" */
2007-09-23 20:13:58 +00:00
if (app_get_menu_bar()) {
jmenubar_set_menu(app_get_menu_bar(), root_menu);
jwindow_remap(app_get_top_window());
jwidget_dirty(app_get_top_window());
2007-09-18 23:57:02 +00:00
}
2007-09-23 20:13:58 +00:00
return 0;
2007-09-18 23:57:02 +00:00
}
2007-09-23 20:13:58 +00:00
JWidget get_root_menu(void) { return root_menu; }
JWidget get_recent_list_menuitem(void) { return recent_list_menuitem; }
JWidget get_layer_popup_menuitem(void) { return layer_popup_menuitem; }
JWidget get_frame_popup_menuitem(void) { return frame_popup_menuitem; }
JWidget get_cel_popup_menuitem(void) { return cel_popup_menuitem; }
2007-09-23 20:13:58 +00:00
void show_fx_popup_menu(void)
2007-09-18 23:57:02 +00:00
{
2007-09-23 20:13:58 +00:00
if (is_interactive() &&
2007-09-18 23:57:02 +00:00
filters_popup_menuitem &&
2007-09-23 20:13:58 +00:00
jmenuitem_get_submenu(filters_popup_menuitem)) {
jmenu_popup(jmenuitem_get_submenu(filters_popup_menuitem),
jmouse_x(0), jmouse_y(0));
2007-09-18 23:57:02 +00:00
}
}
2007-09-23 20:13:58 +00:00
static JWidget convert_xmlelem_to_menu(JXmlElem elem)
2007-09-18 23:57:02 +00:00
{
2007-09-23 20:13:58 +00:00
JWidget menu = jmenu_new();
JWidget menuitem;
JLink link;
JI_LIST_FOR_EACH(((JXmlNode)elem)->children, link) {
JXmlNode child = (JXmlNode)link->data;
/* PRINTF("convert_xmlelem_to_menu: child->value = %p (%s)\n", */
/* child->value, child->value ? child->value: ""); */
2007-09-23 20:13:58 +00:00
if (child->type == JI_XML_ELEM) {
menuitem = convert_xmlelem_to_menuitem((JXmlElem)child);
if (menuitem)
jwidget_add_child(menu, menuitem);
else {
PRINTF("Error converting the element \"%s\" to a menu-item.\n",
jxmlelem_get_name((JXmlElem)child));
return NULL;
}
}
2007-09-18 23:57:02 +00:00
}
2007-09-23 20:13:58 +00:00
return menu;
2007-09-18 23:57:02 +00:00
}
2007-09-23 20:13:58 +00:00
static JWidget convert_xmlelem_to_menuitem(JXmlElem elem)
2007-09-18 23:57:02 +00:00
{
JWidget menuitem;
2007-09-23 20:13:58 +00:00
const char *id;
2007-09-18 23:57:02 +00:00
2007-09-23 20:13:58 +00:00
/* is it a separator? */
if (strcmp(jxmlelem_get_name(elem), "separator") == 0)
return ji_separator_new(NULL, JI_HORIZONTAL);
2007-09-18 23:57:02 +00:00
/* create the item */
2007-09-23 20:13:58 +00:00
menuitem = menuitem_new(jxmlelem_get_attr(elem, "name"),
command_get_by_name(jxmlelem_get_attr(elem,
"command")),
NULL);
2007-09-18 23:57:02 +00:00
if (!menuitem)
return 0;
2007-09-23 20:13:58 +00:00
/* has it a ID? */
id = jxmlelem_get_attr(elem, "id");
if (id) {
2007-09-18 23:57:02 +00:00
/* recent list menu */
if (strcmp(id, "recent_list") == 0) {
2007-09-18 23:57:02 +00:00
recent_list_menuitem = menuitem;
}
/* layer popup menu */
2007-09-23 20:13:58 +00:00
else if (strcmp(id, "layer_popup") == 0) {
2007-09-18 23:57:02 +00:00
layer_popup_menuitem = menuitem;
2007-09-23 20:13:58 +00:00
}
2007-09-18 23:57:02 +00:00
/* frame popup menu */
2007-09-23 20:13:58 +00:00
else if (strcmp(id, "frame_popup") == 0) {
2007-09-18 23:57:02 +00:00
frame_popup_menuitem = menuitem;
2007-09-23 20:13:58 +00:00
}
/* cel popup menu */
else if (strcmp(id, "cel_popup") == 0) {
cel_popup_menuitem = menuitem;
}
2007-09-18 23:57:02 +00:00
/* filters popup menu */
2007-09-23 20:13:58 +00:00
else if (strcmp(id, "fx_popup") == 0) {
2007-09-18 23:57:02 +00:00
filters_popup_menuitem = menuitem;
2007-09-23 20:13:58 +00:00
}
2007-09-18 23:57:02 +00:00
}
2007-09-23 20:13:58 +00:00
/* has it a sub-menu? */
if (strcmp(jxmlelem_get_name(elem), "menu") == 0) {
2007-09-18 23:57:02 +00:00
JWidget sub_menu;
/* create the sub-menu */
2007-09-23 20:13:58 +00:00
sub_menu = convert_xmlelem_to_menu(elem);
if (!sub_menu) {
PRINTF("Error reading the sub-menu\n");
2007-09-18 23:57:02 +00:00
return menuitem;
}
2007-09-23 20:13:58 +00:00
jmenuitem_set_submenu(menuitem, sub_menu);
}
2007-09-18 23:57:02 +00:00
2007-09-23 20:13:58 +00:00
return menuitem;
}
2007-09-18 23:57:02 +00:00
2007-09-23 20:13:58 +00:00
static void apply_shortcut_to_menuitems_with_command(JWidget menu, Command *command)
{
JList children = jwidget_get_children(menu);
JWidget menuitem, submenu;
JLink link;
2007-09-18 23:57:02 +00:00
2007-09-23 20:13:58 +00:00
JI_LIST_FOR_EACH(children, link) {
menuitem = (JWidget)link->data;
2007-09-18 23:57:02 +00:00
2007-09-23 20:13:58 +00:00
if (jwidget_get_type(menuitem) == JI_MENUITEM) {
if (menuitem_get_command(menuitem) == command) {
jmenuitem_set_accel(menuitem, jaccel_new_copy(command->accel));
2007-09-23 20:13:58 +00:00
}
2007-09-18 23:57:02 +00:00
2007-09-23 20:13:58 +00:00
submenu = jmenuitem_get_submenu(menuitem);
if (submenu) {
apply_shortcut_to_menuitems_with_command(submenu, command);
}
2007-09-18 23:57:02 +00:00
}
}
2007-09-23 20:13:58 +00:00
jlist_free(children);
2007-09-18 23:57:02 +00:00
}