1 #include "ToolButton.h"
2 
3 #include <utility>
4 
5 #include "gui/widgets/gtkmenutooltogglebutton.h"
6 
ToolButton(ActionHandler * handler,string id,ActionType type,string iconName,string description,GtkWidget * menuitem)7 ToolButton::ToolButton(ActionHandler* handler, string id, ActionType type, string iconName, string description,
8                        GtkWidget* menuitem):
9         AbstractToolItem(std::move(id), handler, type, menuitem) {
10     this->iconName = std::move(iconName);
11     this->description = std::move(description);
12 }
13 
ToolButton(ActionHandler * handler,string id,ActionType type,ActionGroup group,bool toolToggleOnlyEnable,string iconName,string description,GtkWidget * menuitem)14 ToolButton::ToolButton(ActionHandler* handler, string id, ActionType type, ActionGroup group, bool toolToggleOnlyEnable,
15                        string iconName, string description, GtkWidget* menuitem):
16         AbstractToolItem(std::move(id), handler, type, menuitem) {
17     this->iconName = std::move(iconName);
18     this->description = std::move(description);
19     this->group = group;
20     this->toolToggleOnlyEnable = toolToggleOnlyEnable;
21 }
22 
23 ToolButton::~ToolButton() = default;
24 
25 /**
26  * Register a popup menu entry, create a popup menu, if none is there
27  *
28  * @param name The name of the item
29  * @return The created menu item
30  */
registerPopupMenuEntry(const string & name,const string & iconName)31 auto ToolButton::registerPopupMenuEntry(const string& name, const string& iconName) -> GtkWidget* {
32     if (this->popupMenu == nullptr) {
33         setPopupMenu(gtk_menu_new());
34     }
35 
36     GtkWidget* menuItem = nullptr;
37     if (iconName.empty()) {
38         menuItem = gtk_check_menu_item_new_with_label(name.c_str());
39     } else {
40         menuItem = gtk_check_menu_item_new();
41 
42         GtkWidget* box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
43         gtk_container_add(GTK_CONTAINER(box),
44                           gtk_image_new_from_icon_name(iconName.c_str(), GTK_ICON_SIZE_SMALL_TOOLBAR));
45 
46         GtkWidget* label = gtk_label_new(name.c_str());
47         gtk_label_set_xalign(GTK_LABEL(label), 0.0);
48         gtk_box_pack_end(GTK_BOX(box), label, true, true, 0);
49 
50         gtk_container_add(GTK_CONTAINER(menuItem), box);
51     }
52 
53     gtk_check_menu_item_set_draw_as_radio(GTK_CHECK_MENU_ITEM(menuItem), true);
54     gtk_widget_show_all(menuItem);
55     gtk_container_add(GTK_CONTAINER(this->popupMenu), menuItem);
56 
57     return menuItem;
58 }
59 
updateDescription(const string & description)60 void ToolButton::updateDescription(const string& description) {
61     this->description = description;
62     if (GTK_IS_TOOL_ITEM(item)) {
63         gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(item), description.c_str());
64         gtk_tool_button_set_label(GTK_TOOL_BUTTON(item), description.c_str());
65     }
66 }
67 
newItem()68 auto ToolButton::newItem() -> GtkToolItem* {
69     GtkToolItem* it = nullptr;
70 
71     if (group != GROUP_NOGROUP) {
72         if (popupMenu) {
73             it = gtk_menu_tool_toggle_button_new(
74                     gtk_image_new_from_icon_name(iconName.c_str(), GTK_ICON_SIZE_SMALL_TOOLBAR), description.c_str());
75             gtk_menu_tool_toggle_button_set_menu(GTK_MENU_TOOL_TOGGLE_BUTTON(it), popupMenu);
76         } else {
77             it = gtk_toggle_tool_button_new();
78             gtk_tool_button_set_icon_widget(
79                     GTK_TOOL_BUTTON(it), gtk_image_new_from_icon_name(iconName.c_str(), GTK_ICON_SIZE_SMALL_TOOLBAR));
80         }
81     } else {
82         if (popupMenu) {
83             it = gtk_menu_tool_button_new(gtk_image_new_from_icon_name(iconName.c_str(), GTK_ICON_SIZE_SMALL_TOOLBAR),
84                                           description.c_str());
85             gtk_menu_tool_button_set_menu(GTK_MENU_TOOL_BUTTON(it), popupMenu);
86         } else {
87             it = gtk_tool_button_new(gtk_image_new_from_icon_name(iconName.c_str(), GTK_ICON_SIZE_SMALL_TOOLBAR),
88                                      description.c_str());
89         }
90     }
91     gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(it), description.c_str());
92     gtk_tool_button_set_label(GTK_TOOL_BUTTON(it), description.c_str());
93 
94     return it;
95 }
96 
getToolDisplayName()97 auto ToolButton::getToolDisplayName() -> string { return this->description; }
98 
getNewToolIcon()99 auto ToolButton::getNewToolIcon() -> GtkWidget* {
100     return gtk_image_new_from_icon_name(iconName.c_str(), GTK_ICON_SIZE_SMALL_TOOLBAR);
101 }
102 
setActive(bool active)103 void ToolButton::setActive(bool active) {
104     if (GTK_IS_TOGGLE_TOOL_BUTTON(item)) {
105         gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(item), active);
106     }
107 }
108