1 // SPDX-License-Identifier: GPL-2.0-or-later
2
3 /**
4 * @file
5 * Deprecated Gtk::Action that provides a widget for an Inkscape verb
6 */
7 /* Authors:
8 * MenTaLguY <mental@rydia.net>
9 * Lauris Kaplinski <lauris@kaplinski.com>
10 * bulia byak <buliabyak@users.sf.net>
11 * Frank Felfe <innerspace@iname.com>
12 * John Cliff <simarilius@yahoo.com>
13 * David Turner <novalis@gnu.org>
14 * Josh Andler <scislac@scislac.com>
15 * Jon A. Cruz <jon@joncruz.org>
16 * Maximilian Albert <maximilian.albert@gmail.com>
17 * Tavmjong Bah <tavmjong@free.fr>
18 * Abhishek Sharma
19 * Kris De Gussem <Kris.DeGussem@gmail.com>
20 * Jabiertxo Arraiza <jabier.arraiza@marker.es>
21 *
22 * Copyright (C) 2004 David Turner
23 * Copyright (C) 2003 MenTaLguY
24 * Copyright (C) 1999-2015 authors
25 * Copyright (C) 2001-2002 Ximian, Inc.
26 *
27 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
28 */
29
30 #include "verb-action.h"
31
32 #include <vector>
33
34 #include <glibmm/i18n.h>
35 #include <gtkmm/toolitem.h>
36
37 #include "verbs.h"
38 #include "helper/action.h"
39 #include "ui/widget/button.h"
40 #include "widgets/toolbox.h"
41
42 static GtkToolItem * sp_toolbox_button_item_new_from_verb_with_doubleclick( GtkWidget *t, GtkIconSize size, Inkscape::UI::Widget::ButtonType type,
43 Inkscape::Verb *verb, Inkscape::Verb *doubleclick_verb,
44 Inkscape::UI::View::View *view);
45
sp_toolbox_button_item_new_from_verb_with_doubleclick(GtkWidget * t,GtkIconSize size,Inkscape::UI::Widget::ButtonType type,Inkscape::Verb * verb,Inkscape::Verb * doubleclick_verb,Inkscape::UI::View::View * view)46 GtkToolItem * sp_toolbox_button_item_new_from_verb_with_doubleclick(GtkWidget *t, GtkIconSize size, Inkscape::UI::Widget::ButtonType type,
47 Inkscape::Verb *verb, Inkscape::Verb *doubleclick_verb,
48 Inkscape::UI::View::View *view)
49 {
50 SPAction *action = verb->get_action(Inkscape::ActionContext(view));
51 if (!action) {
52 return nullptr;
53 }
54
55 SPAction *doubleclick_action;
56 if (doubleclick_verb) {
57 doubleclick_action = doubleclick_verb->get_action(Inkscape::ActionContext(view));
58 } else {
59 doubleclick_action = nullptr;
60 }
61
62 /* fixme: Handle sensitive/unsensitive */
63 /* fixme: Implement Inkscape::UI::Widget::Button construction from action */
64 auto b = Gtk::manage(new Inkscape::UI::Widget::Button(size, type, action, doubleclick_action));
65 b->show();
66 auto b_toolitem = Gtk::manage(new Gtk::ToolItem());
67 b_toolitem->add(*b);
68
69 return GTK_TOOL_ITEM(b_toolitem->gobj());
70 }
71
create(Inkscape::Verb * verb,Inkscape::Verb * verb2,Inkscape::UI::View::View * view)72 Glib::RefPtr<VerbAction> VerbAction::create(Inkscape::Verb* verb, Inkscape::Verb* verb2, Inkscape::UI::View::View *view)
73 {
74 Glib::RefPtr<VerbAction> result;
75 SPAction *action = verb->get_action(Inkscape::ActionContext(view));
76 if ( action ) {
77 //SPAction* action2 = verb2 ? verb2->get_action(Inkscape::ActionContext(view)) : 0;
78 result = Glib::RefPtr<VerbAction>(new VerbAction(verb, verb2, view));
79 }
80
81 return result;
82 }
83
VerbAction(Inkscape::Verb * verb,Inkscape::Verb * verb2,Inkscape::UI::View::View * view)84 VerbAction::VerbAction(Inkscape::Verb* verb, Inkscape::Verb* verb2, Inkscape::UI::View::View *view) :
85 Gtk::Action(Glib::ustring(verb->get_id()), verb->get_image(), Glib::ustring(g_dpgettext2(nullptr, "ContextVerb", verb->get_name())), Glib::ustring(_(verb->get_tip()))),
86 verb(verb),
87 verb2(verb2),
88 view(view),
89 active(false)
90 {
91 }
92
93 VerbAction::~VerbAction()
94 = default;
95
create_menu_item_vfunc()96 Gtk::Widget* VerbAction::create_menu_item_vfunc()
97 {
98 Gtk::Widget* widg = Gtk::Action::create_menu_item_vfunc();
99 // g_message("create_menu_item_vfunc() = %p for '%s'", widg, verb->get_id());
100 return widg;
101 }
102
create_tool_item_vfunc()103 Gtk::Widget* VerbAction::create_tool_item_vfunc()
104 {
105 // Gtk::Widget* widg = Gtk::Action::create_tool_item_vfunc();
106 GtkIconSize toolboxSize = Inkscape::UI::ToolboxFactory::prefToSize("/toolbox/tools/small");
107 GtkWidget* toolbox = nullptr;
108 auto holder = Glib::wrap(sp_toolbox_button_item_new_from_verb_with_doubleclick( toolbox, toolboxSize,
109 Inkscape::UI::Widget::BUTTON_TYPE_TOGGLE,
110 verb,
111 verb2,
112 view ));
113
114 auto button_widget = static_cast<Inkscape::UI::Widget::Button *>(holder->get_child());
115
116 if ( active ) {
117 button_widget->toggle_set_down(active);
118 }
119 button_widget->show_all();
120
121 // g_message("create_tool_item_vfunc() = %p for '%s'", holder, verb->get_id());
122 return holder;
123 }
124
connect_proxy_vfunc(Gtk::Widget * proxy)125 void VerbAction::connect_proxy_vfunc(Gtk::Widget* proxy)
126 {
127 // g_message("connect_proxy_vfunc(%p) for '%s'", proxy, verb->get_id());
128 Gtk::Action::connect_proxy_vfunc(proxy);
129 }
130
disconnect_proxy_vfunc(Gtk::Widget * proxy)131 void VerbAction::disconnect_proxy_vfunc(Gtk::Widget* proxy)
132 {
133 // g_message("disconnect_proxy_vfunc(%p) for '%s'", proxy, verb->get_id());
134 Gtk::Action::disconnect_proxy_vfunc(proxy);
135 }
136
set_active(bool active)137 void VerbAction::set_active(bool active)
138 {
139 this->active = active;
140 std::vector<Gtk::Widget*> proxies = get_proxies();
141 for (auto proxie : proxies) {
142 Gtk::ToolItem* ti = dynamic_cast<Gtk::ToolItem*>(proxie);
143 if (ti) {
144 // *should* have one child that is the Inkscape::UI::Widget::Button
145 auto child = dynamic_cast<Inkscape::UI::Widget::Button *>(ti->get_child());
146 if (child) {
147 child->toggle_set_down(active);
148 }
149 }
150 }
151 }
152
on_activate()153 void VerbAction::on_activate()
154 {
155 if ( verb ) {
156 SPAction *action = verb->get_action(Inkscape::ActionContext(view));
157 if ( action ) {
158 sp_action_perform(action, nullptr);
159 }
160 }
161 }
162
163
164