2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
|
|
|
// Copyright (C) 2001-2015 David Capello
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License version 2 as
|
|
|
|
// published by the Free Software Foundation.
|
2011-04-23 10:00:35 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2011-04-23 10:00:35 +08:00
|
|
|
#include "config.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#endif
|
2011-04-23 10:00:35 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/ui/app_menuitem.h"
|
2011-04-23 10:00:35 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/commands/command.h"
|
|
|
|
#include "app/commands/params.h"
|
|
|
|
#include "app/modules/gui.h"
|
2014-10-29 22:58:03 +08:00
|
|
|
#include "app/ui/keyboard_shortcuts.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/ui_context.h"
|
2014-10-29 22:58:03 +08:00
|
|
|
#include "ui/accelerator.h"
|
2012-06-18 09:49:58 +08:00
|
|
|
#include "ui/menu.h"
|
|
|
|
#include "ui/message.h"
|
2014-10-29 22:58:03 +08:00
|
|
|
#include "ui/preferred_size_event.h"
|
2012-06-18 09:49:58 +08:00
|
|
|
#include "ui/widget.h"
|
2011-04-23 10:00:35 +08:00
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
namespace app {
|
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
using namespace ui;
|
|
|
|
|
2014-10-29 22:58:03 +08:00
|
|
|
AppMenuItem::AppMenuItem(const char* text, Command* command, const Params* params)
|
2011-04-23 10:00:35 +08:00
|
|
|
: MenuItem(text)
|
2014-10-29 22:58:03 +08:00
|
|
|
, m_key(NULL)
|
2011-04-23 10:00:35 +08:00
|
|
|
, m_command(command)
|
|
|
|
, m_params(params ? params->clone(): NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
AppMenuItem::~AppMenuItem()
|
2011-04-23 10:00:35 +08:00
|
|
|
{
|
|
|
|
delete m_params;
|
|
|
|
}
|
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
bool AppMenuItem::onProcessMessage(Message* msg)
|
2011-04-23 10:00:35 +08:00
|
|
|
{
|
2013-07-29 08:17:07 +08:00
|
|
|
switch (msg->type()) {
|
2011-04-23 10:00:35 +08:00
|
|
|
|
2013-04-05 08:53:29 +08:00
|
|
|
case kCloseMessage:
|
2011-04-23 10:00:35 +08:00
|
|
|
// disable the menu (the keyboard shortcuts are processed by "manager_msg_proc")
|
|
|
|
setEnabled(false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2014-04-29 12:11:50 +08:00
|
|
|
if (msg->type() == kOpenMessage ||
|
|
|
|
msg->type() == kOpenMenuItemMessage) {
|
2012-01-06 06:45:03 +08:00
|
|
|
// 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();
|
2014-04-29 12:11:50 +08:00
|
|
|
|
|
|
|
if (m_command) {
|
|
|
|
if (m_params)
|
|
|
|
m_command->loadParams(m_params);
|
|
|
|
|
|
|
|
setEnabled(m_command->isEnabled(context));
|
|
|
|
setSelected(m_command->isChecked(context));
|
|
|
|
}
|
2011-04-23 10:00:35 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MenuItem::onProcessMessage(msg);
|
|
|
|
}
|
2012-06-16 10:37:59 +08:00
|
|
|
|
2014-10-29 22:58:03 +08:00
|
|
|
void AppMenuItem::onPreferredSize(PreferredSizeEvent& ev)
|
|
|
|
{
|
|
|
|
gfx::Size size(0, 0);
|
|
|
|
|
|
|
|
if (hasText()) {
|
|
|
|
size.w =
|
|
|
|
+ this->border_width.l
|
|
|
|
+ getTextWidth()
|
|
|
|
+ (inBar() ? this->child_spacing/4: this->child_spacing)
|
|
|
|
+ this->border_width.r;
|
|
|
|
|
|
|
|
size.h =
|
|
|
|
+ this->border_width.t
|
|
|
|
+ getTextHeight()
|
|
|
|
+ this->border_width.b;
|
|
|
|
|
|
|
|
if (m_key && !m_key->accels().empty()) {
|
|
|
|
size.w += Graphics::measureUIStringLength(
|
|
|
|
m_key->accels().front().toString().c_str(), getFont());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ev.setPreferredSize(size);
|
|
|
|
}
|
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
void AppMenuItem::onClick()
|
2012-06-16 10:37:59 +08:00
|
|
|
{
|
|
|
|
MenuItem::onClick();
|
|
|
|
|
|
|
|
if (m_command) {
|
|
|
|
if (m_params)
|
|
|
|
m_command->loadParams(m_params);
|
|
|
|
|
|
|
|
UIContext* context = UIContext::instance();
|
|
|
|
if (m_command->isEnabled(context))
|
|
|
|
context->executeCommand(m_command);
|
|
|
|
}
|
|
|
|
}
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
} // namespace app
|