aseprite/src/app.cpp

497 lines
13 KiB
C++
Raw Normal View History

2007-11-16 18:25:45 +00:00
/* ASE - Allegro Sprite Editor
2010-02-01 21:25:40 +00:00
* Copyright (C) 2001-2010 David 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"
#include <allegro.h>
2010-01-29 03:15:33 +00:00
#include <assert.h>
2007-09-18 23:57:02 +00:00
/* #include <allegro/internal/aintern.h> */
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "jinete/jinete.h"
#include "jinete/jintern.h"
2007-09-18 23:57:02 +00:00
#include "Vaca/String.h"
2010-01-29 03:15:33 +00:00
#include "app.h"
#include "ase_exception.h"
2007-09-23 20:13:58 +00:00
#include "commands/commands.h"
#include "commands/params.h"
#include "console.h"
2007-09-18 23:57:02 +00:00
#include "core/cfg.h"
2010-01-29 03:15:33 +00:00
#include "core/check_args.h"
2007-09-18 23:57:02 +00:00
#include "core/core.h"
#include "core/drop_files.h"
#include "core/file_system.h"
2007-09-18 23:57:02 +00:00
#include "core/modules.h"
#include "dialogs/options.h"
#include "file/file.h"
#include "intl/intl.h"
#include "modules/editors.h"
#include "modules/gfx.h"
#include "modules/gui.h"
#include "modules/palettes.h"
2007-09-18 23:57:02 +00:00
#include "modules/recent.h"
#include "modules/rootmenu.h"
#include "raster/image.h"
#include "raster/layer.h"
#include "raster/palette.h"
2007-09-18 23:57:02 +00:00
#include "raster/sprite.h"
#include "tools/toolbox.h"
2010-01-29 03:15:33 +00:00
#include "ui_context.h"
#include "util/boundary.h"
2007-09-18 23:57:02 +00:00
#include "util/recscr.h"
#include "widgets/colbar.h"
#include "widgets/editor.h"
2007-09-23 20:13:58 +00:00
#include "widgets/menuitem.h"
2007-09-18 23:57:02 +00:00
#include "widgets/statebar.h"
#include "widgets/tabs.h"
2007-09-18 23:57:02 +00:00
#include "widgets/toolbar.h"
#ifdef ALLEGRO_WINDOWS
2009-08-17 21:38:00 +00:00
#include <winalleg.h>
#endif
2010-01-29 03:15:33 +00:00
/**
* ASE modules.
*/
class App::Modules
{
public:
2010-01-29 03:15:33 +00:00
// ASE Modules
ConfigModule m_config_module;
CheckArgs m_checkArgs;
2010-01-29 03:15:33 +00:00
LoggerModule m_logger_module;
IntlModule m_intl_module;
FileSystemModule m_file_system_module;
ToolBox m_toolbox;
2010-01-29 03:15:33 +00:00
RasterModule m_raster;
CommandsModule m_commands_modules;
UIContext m_ui_context;
};
2007-09-18 23:57:02 +00:00
App* App::m_instance = NULL;
static Frame* top_window = NULL; /* top level window (the desktop) */
static Widget* box_menubar = NULL; /* box where the menu bar is */
static Widget* box_colorbar = NULL; /* box where the color bar is */
static Widget* box_toolbar = NULL; /* box where the tools bar is */
static Widget* box_statusbar = NULL; /* box where the status bar is */
static Widget* box_tabsbar = NULL; /* box where the tabs bar is */
static Widget* menubar = NULL; /* the menu bar widget */
static Widget* statusbar = NULL; /* the status bar widget */
static Widget* colorbar = NULL; /* the color bar widget */
static Widget* toolbar = NULL; /* the tool bar widget */
static Widget* tabsbar = NULL; /* the tabs bar widget */
2007-09-18 23:57:02 +00:00
static char *palette_filename = NULL;
2010-01-29 03:15:33 +00:00
static void tabsbar_select_callback(Widget* tabs, void *data, int button);
2010-03-01 00:09:46 +00:00
// Initializes the application loading the modules, setting the
// graphics mode, loading the configuration and resources, etc.
App::App()
2010-01-29 03:15:33 +00:00
: m_modules(NULL)
, m_legacy(NULL)
2007-09-18 23:57:02 +00:00
{
assert(m_instance == NULL);
m_instance = this;
2010-01-29 03:15:33 +00:00
// create private implementation data
m_modules = new Modules();
2010-01-29 03:15:33 +00:00
m_legacy = new LegacyModules(ase_mode & MODE_GUI ? REQUIRE_INTERFACE: 0);
// init editor cursor
Editor::editor_cursor_init();
2007-09-18 23:57:02 +00:00
/* custom default palette? */
if (palette_filename) {
Palette *pal;
2007-09-18 23:57:02 +00:00
PRINTF("Loading custom palette file: %s\n", palette_filename);
pal = palette_load(palette_filename);
if (pal == NULL)
throw ase_exception("Error loading default palette from: %s",
static_cast<const char*>(palette_filename));
2007-09-18 23:57:02 +00:00
set_default_palette(pal);
palette_free(pal);
2007-09-18 23:57:02 +00:00
}
/* set system palette to the default one */
set_current_palette(NULL, true);
2007-09-18 23:57:02 +00:00
}
2010-03-01 00:09:46 +00:00
// Runs the ASE application. In GUI mode it's the top-level window, in
// console/scripting it just runs the specified scripts.
int App::run()
2007-09-18 23:57:02 +00:00
{
/* initialize GUI interface */
if (ase_mode & MODE_GUI) {
Widget* view;
Editor* editor;
2007-09-18 23:57:02 +00:00
PRINTF("GUI mode\n");
/* setup the GUI screen */
jmouse_set_cursor(JI_CURSOR_NORMAL);
jmanager_refresh_screen();
2007-09-18 23:57:02 +00:00
// load main window
top_window = static_cast<Frame*>(load_widget("main.jid", "main_window"));
if (!top_window) {
allegro_message("Error loading data data/jids/main.jid file.\n"
"You have to reinstall the program.\n");
return 1;
}
2007-09-18 23:57:02 +00:00
box_menubar = jwidget_find_name(top_window, "menubar");
box_editors = jwidget_find_name(top_window, "editor");
box_colorbar = jwidget_find_name(top_window, "colorbar");
box_toolbar = jwidget_find_name(top_window, "toolbar");
box_statusbar = jwidget_find_name(top_window, "statusbar");
box_tabsbar = jwidget_find_name(top_window, "tabsbar");
menubar = jmenubar_new();
statusbar = statusbar_new();
colorbar = colorbar_new(box_colorbar->getAlign());
toolbar = toolbar_new();
tabsbar = tabs_new(tabsbar_select_callback);
view = editor_view_new();
editor = create_new_editor();
2007-09-18 23:57:02 +00:00
// configure all widgets to expansives
jwidget_expansive(menubar, true);
jwidget_expansive(statusbar, true);
jwidget_expansive(colorbar, true);
jwidget_expansive(toolbar, true);
jwidget_expansive(tabsbar, true);
jwidget_expansive(view, true);
2007-09-18 23:57:02 +00:00
/* prepare the first editor */
jview_attach(view, editor);
2007-09-18 23:57:02 +00:00
/* setup the menus */
jmenubar_set_menu(menubar, get_root_menu());
2007-09-18 23:57:02 +00:00
/* start text of status bar */
app_default_statusbar_message();
2007-09-18 23:57:02 +00:00
/* add the widgets in the boxes */
if (box_menubar) jwidget_add_child(box_menubar, menubar);
if (box_editors) jwidget_add_child(box_editors, view);
if (box_colorbar) jwidget_add_child(box_colorbar, colorbar);
if (box_toolbar) jwidget_add_child(box_toolbar, toolbar);
if (box_statusbar) jwidget_add_child(box_statusbar, statusbar);
if (box_tabsbar) jwidget_add_child(box_tabsbar, tabsbar);
2007-09-18 23:57:02 +00:00
/* prepare the window */
top_window->remap_window();
2007-09-18 23:57:02 +00:00
/* rebuild menus */
app_realloc_sprite_list();
app_realloc_recent_list();
2007-09-18 23:57:02 +00:00
/* set current editor */
set_current_editor(editor);
2007-09-18 23:57:02 +00:00
/* open the window */
top_window->open_window();
2007-09-18 23:57:02 +00:00
/* refresh the screen */
jmanager_refresh_screen();
2007-09-18 23:57:02 +00:00
}
/* set background mode for non-GUI modes */
/* if (!(ase_mode & MODE_GUI)) */
/* set_display_switch_mode(SWITCH_BACKAMNESIA); */
set_display_switch_mode(SWITCH_BACKGROUND);
2007-09-18 23:57:02 +00:00
// procress options
2007-09-23 20:13:58 +00:00
PRINTF("Processing options...\n");
2010-01-29 03:15:33 +00:00
for (CheckArgs::iterator
it = m_modules->m_checkArgs.begin();
it != m_modules->m_checkArgs.end(); ++it) {
2010-01-29 03:15:33 +00:00
CheckArgs::Option* option = *it;
2007-09-18 23:57:02 +00:00
switch (option->type()) {
2007-09-18 23:57:02 +00:00
2010-01-29 03:15:33 +00:00
case CheckArgs::Option::OpenSprite: {
2007-09-18 23:57:02 +00:00
/* load the sprite */
Sprite* sprite = sprite_load(Vaca::convert_to<std::string>(option->data()).c_str());
2007-09-18 23:57:02 +00:00
if (!sprite) {
/* error */
if (ase_mode & MODE_GUI)
jalert(_("Error<<Error loading file \"%s\"||&Close"), option->data());
2007-09-18 23:57:02 +00:00
else
user_printf(_("Error loading file \"%s\"\n"), option->data());
2007-09-18 23:57:02 +00:00
}
else {
/* mount and select the sprite */
UIContext* context = UIContext::instance();
context->add_sprite(sprite);
context->set_current_sprite(sprite);
2007-09-18 23:57:02 +00:00
if (ase_mode & MODE_GUI) {
/* show it */
set_sprite_in_more_reliable_editor(context->get_first_sprite());
2007-09-18 23:57:02 +00:00
/* recent file */
recent_file(Vaca::convert_to<std::string>(option->data()).c_str());
2007-09-18 23:57:02 +00:00
}
}
break;
}
}
}
m_modules->m_checkArgs.clear();
2007-09-18 23:57:02 +00:00
/* just batch mode */
if (ase_mode & MODE_BATCH) {
PRINTF("Batch mode\n");
}
2010-03-24 20:24:28 +00:00
// Run the GUI
2007-09-18 23:57:02 +00:00
else if (ase_mode & MODE_GUI) {
2010-03-24 20:24:28 +00:00
// Select language
dialogs_select_language(false);
2007-09-18 23:57:02 +00:00
// support to drop files from Windows explorer
install_drop_files();
gui_run();
2007-09-18 23:57:02 +00:00
uninstall_drop_files();
2007-09-18 23:57:02 +00:00
/* stop recording */
if (is_rec_screen())
rec_screen_off();
/* remove the root-menu from the menu-bar (because the rootmenu
module should destroy it) */
jmenubar_set_menu(menubar, NULL);
2007-09-18 23:57:02 +00:00
// destroy the top-window
2007-09-18 23:57:02 +00:00
jwidget_free(top_window);
top_window = NULL;
}
2010-01-29 03:15:33 +00:00
return 0;
2007-09-18 23:57:02 +00:00
}
2010-03-01 00:09:46 +00:00
// Finishes the ASE application.
App::~App()
2007-09-18 23:57:02 +00:00
{
try {
assert(m_instance == this);
2010-03-01 00:09:46 +00:00
// Remove ASE handlers
2010-01-29 03:15:33 +00:00
PRINTF("ASE: Uninstalling\n");
2007-09-18 23:57:02 +00:00
// Fire App Exit signal
App::instance()->Exit();
2010-03-01 00:09:46 +00:00
// Finalize modules, configuration and core
Editor::editor_cursor_exit();
boundary_exit();
2010-01-29 03:15:33 +00:00
delete m_legacy;
delete m_modules;
m_instance = NULL;
}
catch (...) {
2010-01-29 03:15:33 +00:00
allegro_message("Error closing ASE.\n(uncaught exception)");
// no re-throw
}
}
ToolBox* App::get_toolbox()
{
assert(m_modules != NULL);
return &m_modules->m_toolbox;
}
/**
* Updates palette and redraw the screen.
*/
void app_refresh_screen(const Sprite* sprite)
2007-09-18 23:57:02 +00:00
{
assert(screen != NULL);
if (sprite)
set_current_palette(sprite_get_palette(sprite, sprite->frame), false);
else
set_current_palette(NULL, false);
2007-09-18 23:57:02 +00:00
// redraw the screen
jmanager_refresh_screen();
2007-09-18 23:57:02 +00:00
}
/**
* Regenerates the label for each tab in the @em tabsbar.
*/
void app_realloc_sprite_list()
2007-09-18 23:57:02 +00:00
{
UIContext* context = UIContext::instance();
const SpriteList& list = context->get_sprite_list();
/* insert all other sprites */
for (SpriteList::const_iterator
it = list.begin(); it != list.end(); ++it) {
Sprite* sprite = *it;
tabs_set_text_for_tab(tabsbar, get_filename(sprite->filename), sprite);
}
2007-09-18 23:57:02 +00:00
}
/**
* Updates the recent list menu.
*
* @warning This routine can't be used when a menu callback was
* called, because, it destroy the menus, you should use
* schedule_rebuild_recent_list() instead (src/modules/gui.c).
*/
bool app_realloc_recent_list()
2007-09-18 23:57:02 +00:00
{
Widget* list_menuitem = get_recent_list_menuitem();
Widget* menuitem;
2007-09-18 23:57:02 +00:00
/* update the recent file list menu item */
if (list_menuitem) {
if (jmenuitem_has_submenu_opened(list_menuitem))
return false;
Command *cmd_open_file = CommandsModule::instance()->get_command_by_name(CommandId::open_file);
Widget* submenu = jmenuitem_get_submenu(list_menuitem);
2007-09-18 23:57:02 +00:00
if (submenu) {
jmenuitem_set_submenu(list_menuitem, NULL);
jwidget_free(submenu);
2007-09-18 23:57:02 +00:00
}
2007-09-23 20:13:58 +00:00
submenu = jmenu_new();
jmenuitem_set_submenu(list_menuitem, submenu);
2007-09-18 23:57:02 +00:00
if (jlist_first(get_recent_files_list())) {
2007-09-23 20:13:58 +00:00
const char *filename;
Params params;
2007-09-18 23:57:02 +00:00
JLink link;
JI_LIST_FOR_EACH(get_recent_files_list(), link) {
2008-09-30 21:01:54 +00:00
filename = reinterpret_cast<const char*>(link->data);
2007-09-18 23:57:02 +00:00
params.set("filename", filename);
menuitem = menuitem_new(get_filename(filename), cmd_open_file, &params);
2007-09-23 20:13:58 +00:00
jwidget_add_child(submenu, menuitem);
2007-09-18 23:57:02 +00:00
}
}
else {
2007-09-23 20:13:58 +00:00
menuitem = menuitem_new(_("Nothing"), NULL, NULL);
jwidget_disable(menuitem);
jwidget_add_child(submenu, menuitem);
2007-09-18 23:57:02 +00:00
}
}
return true;
2007-09-18 23:57:02 +00:00
}
int app_get_current_image_type()
2007-09-18 23:57:02 +00:00
{
Context* context = UIContext::instance();
assert(context != NULL);
Sprite* sprite = context->get_current_sprite();
if (sprite != NULL)
return sprite->imgtype;
else if (screen != NULL && bitmap_color_depth(screen) == 8)
2007-09-18 23:57:02 +00:00
return IMAGE_INDEXED;
else
return IMAGE_RGB;
2007-09-18 23:57:02 +00:00
}
Frame* app_get_top_window() { return top_window; }
Widget* app_get_menubar() { return menubar; }
Widget* app_get_statusbar() { return statusbar; }
Widget* app_get_colorbar() { return colorbar; }
Widget* app_get_toolbar() { return toolbar; }
Widget* app_get_tabsbar() { return tabsbar; }
void app_default_statusbar_message()
{
statusbar_set_text(app_get_statusbar(), 250,
2010-03-22 02:39:19 +00:00
"%s %s | %s", PACKAGE, VERSION, COPYRIGHT);
}
2007-09-18 23:57:02 +00:00
int app_get_fg_color(Sprite *sprite)
{
color_t c = colorbar_get_fg_color(colorbar);
assert(sprite != NULL);
if (sprite->layer != NULL)
return get_color_for_layer(sprite->layer, c);
else
return get_color_for_image(sprite->imgtype, c);
}
int app_get_bg_color(Sprite *sprite)
{
color_t c = colorbar_get_bg_color(colorbar);
assert(sprite != NULL);
if (sprite->layer != NULL)
return get_color_for_layer(sprite->layer, c);
else
return get_color_for_image(sprite->imgtype, c);
}
int app_get_color_to_clear_layer(Layer *layer)
{
/* all transparent layers are cleared with the mask color */
color_t color = color_mask();
/* the `Background' is erased with the `Background Color' */
if (layer != NULL && layer->is_background())
color = colorbar_get_bg_color(colorbar);
return get_color_for_layer(layer, color);
}
static void tabsbar_select_callback(Widget* tabs, void *data, int button)
2007-09-18 23:57:02 +00:00
{
// Note: data can be NULL (the "Nothing" tab)
Sprite* sprite = (Sprite*)data;
// put as current sprite
set_sprite_in_more_reliable_editor(sprite);
// middle-button: close the sprite
if (data && (button & 4)) {
Command* close_file_cmd =
CommandsModule::instance()->get_command_by_name(CommandId::close_file);
UIContext::instance()->execute_command(close_file_cmd, NULL);
}
2007-09-18 23:57:02 +00:00
}