aseprite/src/widgets/menuitem.cpp

156 lines
4.0 KiB
C++

/* ASE - Allegro Sprite Editor
* Copyright (C) 2001-2011 David Capello
*
* 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 <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "commands/command.h"
#include "commands/params.h"
#include "gui/hook.h"
#include "gui/menu.h"
#include "gui/message.h"
#include "gui/widget.h"
#include "modules/gui.h"
#include "ui_context.h"
struct MenuItem
{
Command *m_command;
Params *m_params;
MenuItem(Command *command, Params* params)
: m_command(command)
, m_params(params ? params->clone(): NULL)
{
}
~MenuItem()
{
delete m_params;
}
};
static int menuitem_type();
static bool menuitem_msg_proc(JWidget widget, Message* msg);
/**
* A widget that represent a menu item of the application.
*
* It's like a jmenuitem, but it has a extra properties: the name of
* the command to be executed when it's clicked (also that command is
* used to check the availability of the command).
*
* @see jmenuitem_new
*/
JWidget menuitem_new(const char* text, Command* command, Params* params)
{
JWidget widget = jmenuitem_new(text);
MenuItem* menuitem = new MenuItem(command, params);
jwidget_add_hook(widget,
menuitem_type(),
menuitem_msg_proc,
menuitem);
return widget;
}
Command* menuitem_get_command(JWidget widget)
{
MenuItem* menuitem = reinterpret_cast<MenuItem*>(jwidget_get_data(widget, menuitem_type()));
return menuitem->m_command;
}
Params* menuitem_get_params(JWidget widget)
{
MenuItem* menuitem = reinterpret_cast<MenuItem*>(jwidget_get_data(widget, menuitem_type()));
return menuitem->m_params;
}
static int menuitem_type()
{
static int type = 0;
if (!type)
type = ji_register_widget_type();
return type;
}
static bool menuitem_msg_proc(JWidget widget, Message* msg)
{
switch (msg->type) {
case JM_DESTROY: {
MenuItem* menuitem = reinterpret_cast<MenuItem*>(jwidget_get_data(widget, menuitem_type()));
delete menuitem;
break;
}
case JM_OPEN: {
MenuItem* menuitem = reinterpret_cast<MenuItem*>(jwidget_get_data(widget, menuitem_type()));
UIContext* context = UIContext::instance();
if (menuitem->m_command) {
if (menuitem->m_params)
menuitem->m_command->loadParams(menuitem->m_params);
widget->setEnabled(menuitem->m_command->isEnabled(context));
widget->setSelected(menuitem->m_command->isChecked(context));
}
break;
}
case JM_CLOSE:
// disable the menu (the keyboard shortcuts are processed by "manager_msg_proc")
widget->setEnabled(false);
break;
case JM_SIGNAL:
if (msg->signal.num == JI_SIGNAL_MENUITEM_SELECT) {
MenuItem* menuitem = reinterpret_cast<MenuItem*>(jwidget_get_data(widget, menuitem_type()));
UIContext* context = UIContext::instance();
if (menuitem->m_command) {
if (menuitem->m_params)
menuitem->m_command->loadParams(menuitem->m_params);
if (menuitem->m_command->isEnabled(context)) {
context->executeCommand(menuitem->m_command);
return true;
}
}
}
break;
default:
if (msg->type == jm_open_menuitem()) {
// Update the context flags after opening the menuitem's
// submenu to update the "enabled" flag of each command
// correctly.
Context* context = UIContext::instance();
context->updateFlags();
}
break;
}
return false;
}