1 #include "AbstractToolItem.h"
2 
3 #include <utility>
4 
AbstractToolItem(string id,ActionHandler * handler,ActionType type,GtkWidget * menuitem)5 AbstractToolItem::AbstractToolItem(string id, ActionHandler* handler, ActionType type, GtkWidget* menuitem):
6         AbstractItem(std::move(id), handler, type, menuitem) {}
7 
~AbstractToolItem()8 AbstractToolItem::~AbstractToolItem() {
9     this->item = nullptr;
10     if (this->popupMenu) {
11         g_object_unref(G_OBJECT(this->popupMenu));
12         this->popupMenu = nullptr;
13     }
14 }
15 
selected(ActionGroup group,ActionType action)16 void AbstractToolItem::selected(ActionGroup group, ActionType action) {
17     if (this->item == nullptr) {
18         return;
19     }
20 
21     if (!GTK_IS_TOGGLE_TOOL_BUTTON(this->item)) {
22         g_warning("selected action %i (group=%i) which is not a toggle action!", action, group);
23         return;
24     }
25 
26     if (gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(this->item)) != (this->action == action)) {
27         this->toolToggleButtonActive = (this->action == action);
28         gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(this->item), this->toolToggleButtonActive);
29     }
30 }
31 
toolButtonCallback(GtkToolButton * toolbutton,AbstractToolItem * item)32 void AbstractToolItem::toolButtonCallback(GtkToolButton* toolbutton, AbstractToolItem* item) {
33     if (toolbutton && GTK_IS_TOGGLE_TOOL_BUTTON(toolbutton)) {
34         bool selected = gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(toolbutton));
35 
36         // ignore this event... GTK Broadcast to much events, e.g. if you call set_active
37         if (item->toolToggleButtonActive == selected) {
38             return;
39         }
40 
41         // don't allow deselect this button
42         if (item->toolToggleOnlyEnable && !selected) {
43             gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(toolbutton), true);
44             return;
45         }
46 
47         item->toolToggleButtonActive = selected;
48     }
49 
50     item->activated(nullptr, nullptr, toolbutton);
51 }
52 
createItem(bool horizontal)53 auto AbstractToolItem::createItem(bool horizontal) -> GtkToolItem* {
54     if (this->item) {
55         return this->item;
56     }
57 
58     this->item = createTmpItem(horizontal);
59     g_object_ref(this->item);
60 
61     if (GTK_IS_TOOL_BUTTON(this->item) || GTK_IS_TOGGLE_TOOL_BUTTON(this->item)) {
62         g_signal_connect(this->item, "clicked", G_CALLBACK(&toolButtonCallback), this);
63     }
64 
65     return this->item;
66 }
67 
createTmpItem(bool horizontal)68 auto AbstractToolItem::createTmpItem(bool horizontal) -> GtkToolItem* {
69     GtkToolItem* item = newItem();
70 
71     if (GTK_IS_TOOL_ITEM(item)) {
72         gtk_tool_item_set_homogeneous(GTK_TOOL_ITEM(item), false);
73     }
74 
75     gtk_widget_show_all(GTK_WIDGET(item));
76     return item;
77 }
78 
setPopupMenu(GtkWidget * popupMenu)79 void AbstractToolItem::setPopupMenu(GtkWidget* popupMenu) {
80     if (this->popupMenu) {
81         g_object_unref(this->popupMenu);
82     }
83     if (popupMenu) {
84         g_object_ref(popupMenu);
85     }
86 
87     this->popupMenu = popupMenu;
88 }
89 
isUsed() const90 auto AbstractToolItem::isUsed() const -> bool { return used; }
91 
setUsed(bool used)92 void AbstractToolItem::setUsed(bool used) { this->used = used; }
93 
94 /**
95  * Enable / Disable the tool item
96  */
enable(bool enabled)97 void AbstractToolItem::enable(bool enabled) {
98     if (this->item) {
99         gtk_widget_set_sensitive(GTK_WIDGET(this->item), enabled);
100     }
101 }
102