Convert menu widgets to Menu, MenuItem, MenuBox, and MenuBar classes.

This commit is contained in:
David Capello 2011-04-22 23:00:35 -03:00
parent d85305322d
commit 5be14417f3
14 changed files with 754 additions and 860 deletions

View File

@ -300,7 +300,7 @@ add_library(aseprite-library
widgets/fileview.cpp widgets/fileview.cpp
widgets/groupbut.cpp widgets/groupbut.cpp
widgets/hex_color_entry.cpp widgets/hex_color_entry.cpp
widgets/menuitem.cpp widgets/menuitem2.cpp
widgets/palette_view.cpp widgets/palette_view.cpp
widgets/popup_frame_pin.cpp widgets/popup_frame_pin.cpp
widgets/statebar.cpp widgets/statebar.cpp

View File

@ -54,7 +54,7 @@
#include "widgets/color_bar.h" #include "widgets/color_bar.h"
#include "widgets/editor/editor.h" #include "widgets/editor/editor.h"
#include "widgets/editor/editor_view.h" #include "widgets/editor/editor_view.h"
#include "widgets/menuitem.h" #include "widgets/menuitem2.h"
#include "widgets/statebar.h" #include "widgets/statebar.h"
#include "widgets/tabs.h" #include "widgets/tabs.h"
#include "widgets/toolbar.h" #include "widgets/toolbar.h"
@ -101,7 +101,7 @@ 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_toolbar = NULL; /* box where the tools bar is */
static Widget* box_statusbar = NULL; /* box where the status 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* box_tabsbar = NULL; /* box where the tabs bar is */
static Widget* menubar = NULL; /* the menu bar widget */ static MenuBar* menubar = NULL; /* the menu bar widget */
static StatusBar* statusbar = NULL; /* the status bar widget */ static StatusBar* statusbar = NULL; /* the status bar widget */
static ColorBar* colorbar = NULL; /* the color bar widget */ static ColorBar* colorbar = NULL; /* the color bar widget */
static Widget* toolbar = NULL; /* the tool bar widget */ static Widget* toolbar = NULL; /* the tool bar widget */
@ -180,7 +180,7 @@ int App::run()
box_statusbar = top_window->findChild("statusbar"); box_statusbar = top_window->findChild("statusbar");
box_tabsbar = top_window->findChild("tabsbar"); box_tabsbar = top_window->findChild("tabsbar");
menubar = jmenubar_new(); menubar = new MenuBar();
statusbar = new StatusBar(); statusbar = new StatusBar();
colorbar = new ColorBar(box_colorbar->getAlign()); colorbar = new ColorBar(box_colorbar->getAlign());
toolbar = toolbar_new(); toolbar = toolbar_new();
@ -200,7 +200,7 @@ int App::run()
view->attachToView(editor); view->attachToView(editor);
/* setup the menus */ /* setup the menus */
jmenubar_set_menu(menubar, get_root_menu()); menubar->setMenu(get_root_menu());
/* start text of status bar */ /* start text of status bar */
app_default_statusbar_message(); app_default_statusbar_message();
@ -288,7 +288,7 @@ int App::run()
// Remove the root-menu from the menu-bar (because the rootmenu // Remove the root-menu from the menu-bar (because the rootmenu
// module should destroy it). // module should destroy it).
jmenubar_set_menu(menubar, NULL); menubar->setMenu(NULL);
// Destroy the top-window // Destroy the top-window
jwidget_free(top_window); jwidget_free(top_window);
@ -402,25 +402,25 @@ void app_update_document_tab(const Document* document)
*/ */
bool app_realloc_recent_list() bool app_realloc_recent_list()
{ {
Widget* list_menuitem = get_recent_list_menuitem(); MenuItem* list_menuitem = get_recent_list_menuitem();
Widget* menuitem; MenuItem* menuitem;
/* update the recent file list menu item */ /* update the recent file list menu item */
if (list_menuitem) { if (list_menuitem) {
if (jmenuitem_has_submenu_opened(list_menuitem)) if (list_menuitem->hasSubmenuOpened())
return false; return false;
Command* cmd_open_file = CommandsModule::instance()->getCommandByName(CommandId::OpenFile); Command* cmd_open_file = CommandsModule::instance()->getCommandByName(CommandId::OpenFile);
Widget* submenu = jmenuitem_get_submenu(list_menuitem); Menu* submenu = list_menuitem->getSubmenu();
if (submenu) { if (submenu) {
jmenuitem_set_submenu(list_menuitem, NULL); list_menuitem->setSubmenu(NULL);
jwidget_free(submenu); jwidget_free(submenu);
} }
// Build the menu of recent files // Build the menu of recent files
submenu = jmenu_new(); submenu = new Menu();
jmenuitem_set_submenu(list_menuitem, submenu); list_menuitem->setSubmenu(submenu);
RecentFiles::const_iterator it = App::instance()->getRecentFiles()->begin(); RecentFiles::const_iterator it = App::instance()->getRecentFiles()->begin();
RecentFiles::const_iterator end = App::instance()->getRecentFiles()->end(); RecentFiles::const_iterator end = App::instance()->getRecentFiles()->end();
@ -433,12 +433,12 @@ bool app_realloc_recent_list()
params.set("filename", filename); params.set("filename", filename);
menuitem = menuitem_new(get_filename(filename), cmd_open_file, &params); menuitem = new MenuItem2(get_filename(filename), cmd_open_file, &params);
submenu->addChild(menuitem); submenu->addChild(menuitem);
} }
} }
else { else {
menuitem = menuitem_new("Nothing", NULL, NULL); menuitem = new MenuItem2("Nothing", NULL, NULL);
menuitem->setEnabled(false); menuitem->setEnabled(false);
submenu->addChild(menuitem); submenu->addChild(menuitem);
} }
@ -462,7 +462,7 @@ int app_get_current_image_type()
} }
Frame* app_get_top_window() { return top_window; } Frame* app_get_top_window() { return top_window; }
Widget* app_get_menubar() { return menubar; } MenuBar* app_get_menubar() { return menubar; }
StatusBar* app_get_statusbar() { return statusbar; } StatusBar* app_get_statusbar() { return statusbar; }
ColorBar* app_get_colorbar() { return colorbar; } ColorBar* app_get_colorbar() { return colorbar; }
Widget* app_get_toolbar() { return toolbar; } Widget* app_get_toolbar() { return toolbar; }

View File

@ -34,6 +34,7 @@ class Frame;
class Layer; class Layer;
class LegacyModules; class LegacyModules;
class LoggerModule; class LoggerModule;
class MenuBar;
class Params; class Params;
class RecentFiles; class RecentFiles;
class StatusBar; class StatusBar;
@ -96,7 +97,7 @@ bool app_realloc_recent_list();
int app_get_current_image_type(); int app_get_current_image_type();
Frame* app_get_top_window(); Frame* app_get_top_window();
Widget* app_get_menubar(); MenuBar* app_get_menubar();
StatusBar* app_get_statusbar(); StatusBar* app_get_statusbar();
ColorBar* app_get_colorbar(); ColorBar* app_get_colorbar();
Widget* app_get_toolbar(); Widget* app_get_toolbar();

View File

@ -529,9 +529,9 @@ bool AnimationEditor::onProcessMessage(Message* msg)
// Show the frame pop-up menu. // Show the frame pop-up menu.
if (msg->mouse.right) { if (msg->mouse.right) {
if (m_clk_frame == m_hot_frame) { if (m_clk_frame == m_hot_frame) {
JWidget popup_menu = get_frame_popup_menu(); Menu* popup_menu = get_frame_popup_menu();
if (popup_menu != NULL) { if (popup_menu != NULL) {
jmenu_popup(popup_menu, msg->mouse.x, msg->mouse.y); popup_menu->showPopup(msg->mouse.x, msg->mouse.y);
destroy_thumbnails(); destroy_thumbnails();
invalidate(); invalidate();
@ -571,9 +571,9 @@ bool AnimationEditor::onProcessMessage(Message* msg)
// Show the layer pop-up menu. // Show the layer pop-up menu.
if (msg->mouse.right) { if (msg->mouse.right) {
if (m_clk_layer == m_hot_layer) { if (m_clk_layer == m_hot_layer) {
JWidget popup_menu = get_layer_popup_menu(); Menu* popup_menu = get_layer_popup_menu();
if (popup_menu != NULL) { if (popup_menu != NULL) {
jmenu_popup(popup_menu, msg->mouse.x, msg->mouse.y); popup_menu->showPopup(msg->mouse.x, msg->mouse.y);
destroy_thumbnails(); destroy_thumbnails();
invalidate(); invalidate();
@ -654,10 +654,10 @@ bool AnimationEditor::onProcessMessage(Message* msg)
// Show the cel pop-up menu. // Show the cel pop-up menu.
if (msg->mouse.right) { if (msg->mouse.right) {
JWidget popup_menu = movement ? get_cel_movement_popup_menu(): Menu* popup_menu = movement ? get_cel_movement_popup_menu():
get_cel_popup_menu(); get_cel_popup_menu();
if (popup_menu != NULL) { if (popup_menu != NULL) {
jmenu_popup(popup_menu, msg->mouse.x, msg->mouse.y); popup_menu->showPopup(msg->mouse.x, msg->mouse.y);
destroy_thumbnails(); destroy_thumbnails();
regenerateLayers(); regenerateLayers();

File diff suppressed because it is too large Load Diff

View File

@ -7,27 +7,134 @@
#ifndef GUI_MENU_H_INCLUDED #ifndef GUI_MENU_H_INCLUDED
#define GUI_MENU_H_INCLUDED #define GUI_MENU_H_INCLUDED
#include "gui/base.h" #include "base/compiler_specific.h"
#include "base/signal.h"
#include "gui/widget.h"
Widget* jmenu_new(); class MenuItem;
Widget* jmenubar_new(); struct MenuBaseData;
Widget* jmenubox_new();
Widget* jmenuitem_new(const char *text);
Widget* jmenubox_get_menu(Widget* menubox); class Menu : public Widget
Widget* jmenubar_get_menu(Widget* menubar); {
Widget* jmenuitem_get_submenu(Widget* menuitem); public:
JAccel jmenuitem_get_accel(Widget* menuitem); Menu();
bool jmenuitem_has_submenu_opened(Widget* menuitem); ~Menu();
void jmenubox_set_menu(Widget* menubox, Widget* menu); void showPopup(int x, int y);
void jmenubar_set_menu(Widget* menubar, Widget* menu);
void jmenuitem_set_submenu(Widget* menuitem, Widget* menu);
void jmenuitem_set_accel(Widget* menuitem, JAccel accel);
int jmenuitem_is_highlight(Widget* menuitem); // Returns the MenuItem that has as submenu this menu.
MenuItem* getOwnerMenuItem() {
return m_menuitem;
}
void jmenu_popup(Widget* menu, int x, int y); protected:
virtual bool onProcessMessage(Message* msg) OVERRIDE;
private:
void setOwnerMenuItem(MenuItem* ownerMenuItem) {
m_menuitem = ownerMenuItem;
}
void requestSize(int* w, int* h);
void set_position(JRect rect);
void closeAll();
MenuItem* getHighlightedItem();
void highlightItem(MenuItem* menuitem, bool click, bool open_submenu, bool select_first_child);
void unhighlightItem();
MenuItem* m_menuitem; // From where the menu was open
friend class MenuBox;
friend class MenuItem;
};
class MenuBox : public Widget
{
public:
MenuBox(int type = JI_MENUBOX);
~MenuBox();
Menu* getMenu();
void setMenu(Menu* menu);
MenuBaseData* getBase() {
return m_base;
}
protected:
virtual bool onProcessMessage(Message* msg) OVERRIDE;
MenuBaseData* createBase();
private:
void requestSize(int* w, int* h);
void set_position(JRect rect);
void closePopup();
MenuBaseData* m_base;
friend class Menu;
};
class MenuBar : public MenuBox
{
public:
MenuBar();
};
class MenuItem : public Widget
{
public:
MenuItem(const char *text);
~MenuItem();
Menu* getSubmenu();
void setSubmenu(Menu* submenu);
JAccel getAccel();
void setAccel(JAccel accel);
bool isHighlighted() const;
void setHighlighted(bool state);
// Returns true if the MenuItem has a submenu.
bool hasSubmenu() const;
// Returns true if the submenu is opened.
bool hasSubmenuOpened() const {
return (m_submenu_menubox != NULL);
}
// Returns the menu-box where the sub-menu has been opened, or just
// NULL if the sub-menu is closed.
MenuBox* getSubmenuContainer() const {
return m_submenu_menubox;
}
// Fired when the menu item is clicked.
Signal0<void> Click;
protected:
virtual bool onProcessMessage(Message* msg) OVERRIDE;
virtual void onClick();
private:
void requestSize(int* w, int* h);
void openSubmenu(bool select_first);
void closeSubmenu(bool last_of_close_chain);
void startTimer();
void stopTimer();
void executeClick();
JAccel m_accel; // Hot-key
bool m_highlighted; // Is it highlighted?
Menu* m_submenu; // The sub-menu
MenuBox* m_submenu_menubox; // The opened menubox for this menu-item
int m_submenu_timer; // Timer to open the submenu
friend class Menu;
friend class MenuBox;
};
int jm_open_menuitem(); int jm_open_menuitem();
int jm_close_menuitem(); int jm_close_menuitem();

View File

@ -14,6 +14,8 @@ struct FONT;
class ButtonBase; class ButtonBase;
class Entry; class Entry;
class Menu;
class MenuItem;
class PaintEvent; class PaintEvent;
class Theme class Theme
@ -53,8 +55,8 @@ public:
virtual void paintLinkLabel(PaintEvent& ev) = 0; virtual void paintLinkLabel(PaintEvent& ev) = 0;
virtual void draw_listbox(JWidget widget, JRect clip) = 0; virtual void draw_listbox(JWidget widget, JRect clip) = 0;
virtual void draw_listitem(JWidget widget, JRect clip) = 0; virtual void draw_listitem(JWidget widget, JRect clip) = 0;
virtual void draw_menu(JWidget widget, JRect clip) = 0; virtual void draw_menu(Menu* menu, JRect clip) = 0;
virtual void draw_menuitem(JWidget widget, JRect clip) = 0; virtual void draw_menuitem(MenuItem* menuitem, JRect clip) = 0;
virtual void draw_panel(JWidget widget, JRect clip) = 0; virtual void draw_panel(JWidget widget, JRect clip) = 0;
virtual void paintRadioButton(PaintEvent& ev) = 0; virtual void paintRadioButton(PaintEvent& ev) = 0;
virtual void draw_separator(JWidget widget, JRect clip) = 0; virtual void draw_separator(JWidget widget, JRect clip) = 0;

View File

@ -35,23 +35,23 @@
#include "modules/rootmenu.h" #include "modules/rootmenu.h"
#include "tools/tool_box.h" #include "tools/tool_box.h"
#include "util/filetoks.h" #include "util/filetoks.h"
#include "widgets/menuitem.h" #include "widgets/menuitem2.h"
#include "tinyxml.h" #include "tinyxml.h"
static JWidget root_menu; static Menu* root_menu;
static JWidget recent_list_menuitem; static MenuItem* recent_list_menuitem;
static JWidget layer_popup_menu; static Menu* layer_popup_menu;
static JWidget frame_popup_menu; static Menu* frame_popup_menu;
static JWidget cel_popup_menu; static Menu* cel_popup_menu;
static JWidget cel_movement_popup_menu; static Menu* cel_movement_popup_menu;
static int load_root_menu(); static int load_root_menu();
static JWidget load_menu_by_id(TiXmlHandle& handle, const char *id); static Menu* load_menu_by_id(TiXmlHandle& handle, const char *id);
static JWidget convert_xmlelem_to_menu(TiXmlElement* elem); static Menu* convert_xmlelem_to_menu(TiXmlElement* elem);
static JWidget convert_xmlelem_to_menuitem(TiXmlElement* elem); static Widget* convert_xmlelem_to_menuitem(TiXmlElement* elem);
static void apply_shortcut_to_menuitems_with_command(JWidget menu, Command* command, Params* params, JAccel accel); static void apply_shortcut_to_menuitems_with_command(Menu* menu, Command* command, Params* params, JAccel accel);
int init_module_rootmenu() int init_module_rootmenu()
{ {
@ -74,18 +74,18 @@ void exit_module_rootmenu()
delete cel_movement_popup_menu; delete cel_movement_popup_menu;
} }
JWidget get_root_menu() { return root_menu; } Menu* get_root_menu() { return root_menu; }
JWidget get_recent_list_menuitem() { return recent_list_menuitem; } MenuItem* get_recent_list_menuitem() { return recent_list_menuitem; }
JWidget get_layer_popup_menu() { return layer_popup_menu; } Menu* get_layer_popup_menu() { return layer_popup_menu; }
JWidget get_frame_popup_menu() { return frame_popup_menu; } Menu* get_frame_popup_menu() { return frame_popup_menu; }
JWidget get_cel_popup_menu() { return cel_popup_menu; } Menu* get_cel_popup_menu() { return cel_popup_menu; }
JWidget get_cel_movement_popup_menu() { return cel_movement_popup_menu; } Menu* get_cel_movement_popup_menu() { return cel_movement_popup_menu; }
static int load_root_menu() static int load_root_menu()
{ {
if (app_get_menubar()) if (app_get_menubar())
jmenubar_set_menu(app_get_menubar(), NULL); app_get_menubar()->setMenu(NULL);
// destroy `root-menu' // destroy `root-menu'
delete root_menu; // widget delete root_menu; // widget
@ -229,7 +229,7 @@ static int load_root_menu()
// Sets the "menu" of the "menu-bar" to the new "root-menu" // Sets the "menu" of the "menu-bar" to the new "root-menu"
if (app_get_menubar()) { if (app_get_menubar()) {
jmenubar_set_menu(app_get_menubar(), root_menu); app_get_menubar()->setMenu(root_menu);
app_get_top_window()->remap_window(); app_get_top_window()->remap_window();
app_get_top_window()->invalidate(); app_get_top_window()->invalidate();
} }
@ -237,7 +237,7 @@ static int load_root_menu()
return 0; return 0;
} }
static JWidget load_menu_by_id(TiXmlHandle& handle, const char* id) static Menu* load_menu_by_id(TiXmlHandle& handle, const char* id)
{ {
ASSERT(id != NULL); ASSERT(id != NULL);
@ -261,15 +261,15 @@ static JWidget load_menu_by_id(TiXmlHandle& handle, const char* id)
return NULL; return NULL;
} }
static JWidget convert_xmlelem_to_menu(TiXmlElement* elem) static Menu* convert_xmlelem_to_menu(TiXmlElement* elem)
{ {
JWidget menu = jmenu_new(); Menu* menu = new Menu();
//PRINTF("convert_xmlelem_to_menu(%s, %s, %s)\n", elem->Value(), elem->Attribute("id"), elem->Attribute("text")); //PRINTF("convert_xmlelem_to_menu(%s, %s, %s)\n", elem->Value(), elem->Attribute("id"), elem->Attribute("text"));
TiXmlElement* child = elem->FirstChildElement(); TiXmlElement* child = elem->FirstChildElement();
while (child) { while (child) {
JWidget menuitem = convert_xmlelem_to_menuitem(child); Widget* menuitem = convert_xmlelem_to_menuitem(child);
if (menuitem) if (menuitem)
menu->addChild(menuitem); menu->addChild(menuitem);
else else
@ -282,7 +282,7 @@ static JWidget convert_xmlelem_to_menu(TiXmlElement* elem)
return menu; return menu;
} }
static JWidget convert_xmlelem_to_menuitem(TiXmlElement* elem) static Widget* convert_xmlelem_to_menuitem(TiXmlElement* elem)
{ {
// is it a <separator>? // is it a <separator>?
if (strcmp(elem->Value(), "separator") == 0) if (strcmp(elem->Value(), "separator") == 0)
@ -309,7 +309,7 @@ static JWidget convert_xmlelem_to_menuitem(TiXmlElement* elem)
} }
/* create the item */ /* create the item */
JWidget menuitem = menuitem_new(elem->Attribute("text"), MenuItem2* menuitem = new MenuItem2(elem->Attribute("text"),
command, command ? &params: NULL); command, command ? &params: NULL);
if (!menuitem) if (!menuitem)
return NULL; return NULL;
@ -323,42 +323,43 @@ static JWidget convert_xmlelem_to_menuitem(TiXmlElement* elem)
} }
} }
/* has it a sub-menu (<menu>)? */ // Has it a sub-menu (<menu>)?
if (strcmp(elem->Value(), "menu") == 0) { if (strcmp(elem->Value(), "menu") == 0) {
/* create the sub-menu */ // Create the sub-menu
JWidget sub_menu = convert_xmlelem_to_menu(elem); Menu* subMenu = convert_xmlelem_to_menu(elem);
if (!sub_menu) if (!subMenu)
throw base::Exception("Error reading the sub-menu\n"); throw base::Exception("Error reading the sub-menu\n");
jmenuitem_set_submenu(menuitem, sub_menu); menuitem->setSubmenu(subMenu);
} }
return menuitem; return menuitem;
} }
static void apply_shortcut_to_menuitems_with_command(JWidget menu, Command *command, Params* params, JAccel accel) static void apply_shortcut_to_menuitems_with_command(Menu* menu, Command *command, Params* params, JAccel accel)
{ {
JList children = menu->getChildren(); JList children = menu->getChildren();
JWidget menuitem, submenu;
JLink link; JLink link;
JI_LIST_FOR_EACH(children, link) { JI_LIST_FOR_EACH(children, link) {
menuitem = (JWidget)link->data; Widget* child = (Widget*)link->data;
if (menuitem->getType() == JI_MENUITEM) { if (child->getType() == JI_MENUITEM) {
Command* mi_command = menuitem_get_command(menuitem); ASSERT(dynamic_cast<MenuItem2*>(child) != NULL);
Params* mi_params = menuitem_get_params(menuitem);
MenuItem2* menuitem = static_cast<MenuItem2*>(child);
Command* mi_command = menuitem->getCommand();
Params* mi_params = menuitem->getParams();
if (mi_command && if (mi_command &&
ustricmp(mi_command->short_name(), command->short_name()) == 0 && ustricmp(mi_command->short_name(), command->short_name()) == 0 &&
((mi_params && *mi_params == *params) || ((mi_params && *mi_params == *params) ||
(Params() == *params))) { (Params() == *params))) {
// Set the accelerator to be shown in this menu-item // Set the accelerator to be shown in this menu-item
jmenuitem_set_accel(menuitem, jaccel_new_copy(accel)); menuitem->setAccel(jaccel_new_copy(accel));
} }
submenu = jmenuitem_get_submenu(menuitem); if (Menu* submenu = menuitem->getSubmenu())
if (submenu)
apply_shortcut_to_menuitems_with_command(submenu, command, params, accel); apply_shortcut_to_menuitems_with_command(submenu, command, params, accel);
} }
} }

View File

@ -21,6 +21,9 @@
#include "gui/base.h" #include "gui/base.h"
class Menu;
class MenuItem;
enum { enum {
ACCEL_FOR_UNDO, ACCEL_FOR_UNDO,
ACCEL_FOR_REDO, ACCEL_FOR_REDO,
@ -32,14 +35,12 @@ enum {
int init_module_rootmenu(); int init_module_rootmenu();
void exit_module_rootmenu(); void exit_module_rootmenu();
JWidget get_root_menu(); Menu* get_root_menu();
JWidget get_recent_list_menuitem(); MenuItem* get_recent_list_menuitem();
JWidget get_layer_popup_menu(); Menu* get_layer_popup_menu();
JWidget get_frame_popup_menu(); Menu* get_frame_popup_menu();
JWidget get_cel_popup_menu(); Menu* get_cel_popup_menu();
JWidget get_cel_movement_popup_menu(); Menu* get_cel_movement_popup_menu();
/* void show_fx_popup_menu(); */
#endif #endif

View File

@ -1017,12 +1017,12 @@ void SkinTheme::draw_listitem(JWidget widget, JRect clip)
} }
} }
void SkinTheme::draw_menu(JWidget widget, JRect clip) void SkinTheme::draw_menu(Menu* widget, JRect clip)
{ {
jdraw_rectfill(widget->rc, BGCOLOR); jdraw_rectfill(widget->rc, BGCOLOR);
} }
void SkinTheme::draw_menuitem(JWidget widget, JRect clip) void SkinTheme::draw_menuitem(MenuItem* widget, JRect clip)
{ {
int c, bg, fg, bar; int c, bg, fg, bar;
int x1, y1, x2, y2; int x1, y1, x2, y2;
@ -1040,7 +1040,7 @@ void SkinTheme::draw_menuitem(JWidget widget, JRect clip)
bg = get_menuitem_normal_face_color(); bg = get_menuitem_normal_face_color();
} }
else { else {
if (jmenuitem_is_highlight(widget)) { if (widget->isHighlighted()) {
fg = get_menuitem_highlight_text_color(); fg = get_menuitem_highlight_text_color();
bg = get_menuitem_highlight_face_color(); bg = get_menuitem_highlight_face_color();
} }
@ -1090,7 +1090,7 @@ void SkinTheme::draw_menuitem(JWidget widget, JRect clip)
/* for menu-box */ /* for menu-box */
if (!bar) { if (!bar) {
/* draw the arrown (to indicate which this menu has a sub-menu) */ /* draw the arrown (to indicate which this menu has a sub-menu) */
if (jmenuitem_get_submenu(widget)) { if (widget->getSubmenu()) {
int scale = jguiscale(); int scale = jguiscale();
/* enabled */ /* enabled */
@ -1116,14 +1116,14 @@ void SkinTheme::draw_menuitem(JWidget widget, JRect clip)
} }
} }
/* draw the keyboard shortcut */ /* draw the keyboard shortcut */
else if (jmenuitem_get_accel(widget)) { else if (widget->getAccel()) {
int old_align = widget->getAlign(); int old_align = widget->getAlign();
char buf[256]; char buf[256];
pos = jwidget_get_rect(widget); pos = jwidget_get_rect(widget);
pos->x2 -= widget->child_spacing/4; pos->x2 -= widget->child_spacing/4;
jaccel_to_string(jmenuitem_get_accel(widget), buf); jaccel_to_string(widget->getAccel(), buf);
widget->setAlign(JI_RIGHT | JI_MIDDLE); widget->setAlign(JI_RIGHT | JI_MIDDLE);
draw_textstring(buf, fg, bg, false, widget, pos, 0); draw_textstring(buf, fg, bg, false, widget, pos, 0);

View File

@ -74,8 +74,8 @@ public:
void paintLinkLabel(PaintEvent& ev); void paintLinkLabel(PaintEvent& ev);
void draw_listbox(JWidget widget, JRect clip); void draw_listbox(JWidget widget, JRect clip);
void draw_listitem(JWidget widget, JRect clip); void draw_listitem(JWidget widget, JRect clip);
void draw_menu(JWidget widget, JRect clip); void draw_menu(Menu* menu, JRect clip);
void draw_menuitem(JWidget widget, JRect clip); void draw_menuitem(MenuItem* menuitem, JRect clip);
void draw_panel(JWidget widget, JRect clip); void draw_panel(JWidget widget, JRect clip);
void paintRadioButton(PaintEvent& ev); void paintRadioButton(PaintEvent& ev);
void draw_separator(JWidget widget, JRect clip); void draw_separator(JWidget widget, JRect clip);

View File

@ -1,155 +0,0 @@
/* 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;
}

98
src/widgets/menuitem2.cpp Normal file
View File

@ -0,0 +1,98 @@
/* 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 "widgets/menuitem2.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"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
MenuItem2::MenuItem2(const char* text, Command* command, Params* params)
: MenuItem(text)
, m_command(command)
, m_params(params ? params->clone(): NULL)
{
}
MenuItem2::~MenuItem2()
{
delete m_params;
}
bool MenuItem2::onProcessMessage(Message* msg)
{
switch (msg->type) {
case JM_OPEN: {
UIContext* context = UIContext::instance();
if (m_command) {
if (m_params)
m_command->loadParams(m_params);
setEnabled(m_command->isEnabled(context));
setSelected(m_command->isChecked(context));
}
break;
}
case JM_CLOSE:
// disable the menu (the keyboard shortcuts are processed by "manager_msg_proc")
setEnabled(false);
break;
case JM_SIGNAL:
if (msg->signal.num == JI_SIGNAL_MENUITEM_SELECT) {
UIContext* context = UIContext::instance();
if (m_command) {
if (m_params)
m_command->loadParams(m_params);
if (m_command->isEnabled(context)) {
context->executeCommand(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 MenuItem::onProcessMessage(msg);
}

View File

@ -19,14 +19,31 @@
#ifndef WIDGETS_MENUITEM_H_INCLUDED #ifndef WIDGETS_MENUITEM_H_INCLUDED
#define WIDGETS_MENUITEM_H_INCLUDED #define WIDGETS_MENUITEM_H_INCLUDED
#include "gui/base.h" #include "gui/menu.h"
class Command; class Command;
class Params; class Params;
JWidget menuitem_new(const char* text, Command* command, Params* params); // A widget that represent a menu item of the application.
//
// It's like a MenuItme, 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).
class MenuItem2 : public MenuItem
{
public:
MenuItem2(const char* text, Command* command, Params* params);
~MenuItem2();
Command* menuitem_get_command(JWidget menuitem); Command* getCommand() { return m_command; }
Params* menuitem_get_params(JWidget menuitem); Params* getParams() { return m_params; }
protected:
bool onProcessMessage(Message* msg) OVERRIDE;
private:
Command* m_command;
Params* m_params;
};
#endif #endif