aseprite/src/app/ui/app_menuitem.cpp
David Capello f939ef5f02 Add keyboard shortcuts customization (close #253)
Changes:
* Add KeyboardShortcutsCommand and window
* Add SelectAccelerator window
* Replace modules/gui.cpp functions with app::KeyboardShortcuts and
  app::Key with the logic to load/save/handle keyboard shortcuts
* Change ui::Accelerator concept: now it represent just one keyboard
  shortcut, not a set of shortcuts
* Remove ui::Accelerator from ui::MenuItem, now the key is associated
  in app::AppMenuItem and it's a app::Key
* Add Command::onGetFriendlyName() to get a user friendly name of the
  command depending on its parameters
2014-10-29 11:58:03 -03:00

129 lines
3.1 KiB
C++

/* Aseprite
* Copyright (C) 2001-2013 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
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/ui/app_menuitem.h"
#include "app/commands/command.h"
#include "app/commands/params.h"
#include "app/modules/gui.h"
#include "app/ui/keyboard_shortcuts.h"
#include "app/ui_context.h"
#include "ui/accelerator.h"
#include "ui/menu.h"
#include "ui/message.h"
#include "ui/preferred_size_event.h"
#include "ui/widget.h"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
namespace app {
using namespace ui;
AppMenuItem::AppMenuItem(const char* text, Command* command, const Params* params)
: MenuItem(text)
, m_key(NULL)
, m_command(command)
, m_params(params ? params->clone(): NULL)
{
}
AppMenuItem::~AppMenuItem()
{
delete m_params;
}
bool AppMenuItem::onProcessMessage(Message* msg)
{
switch (msg->type()) {
case kCloseMessage:
// disable the menu (the keyboard shortcuts are processed by "manager_msg_proc")
setEnabled(false);
break;
default:
if (msg->type() == kOpenMessage ||
msg->type() == kOpenMenuItemMessage) {
// 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();
if (m_command) {
if (m_params)
m_command->loadParams(m_params);
setEnabled(m_command->isEnabled(context));
setSelected(m_command->isChecked(context));
}
}
break;
}
return MenuItem::onProcessMessage(msg);
}
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);
}
void AppMenuItem::onClick()
{
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);
}
}
} // namespace app