1 /**
2 * \brief Toolbox Widget - A detachable toolbar for buttons and other widgets.
3 *
4 * Author:
5 * Derek P. Moore <derekm@hackunix.org>
6 *
7 * Copyright (C) 2004 Derek P. Moore
8 *
9 * Released under GNU GPL. Read the file 'COPYING' for more information.
10 */
12 #ifdef HAVE_CONFIG_H
13 # include <config.h>
14 #endif
16 #include <gtkmm/radioaction.h>
17 #include <gtk/gtkmain.h>
18 #include "ui/widget/toolbox.h"
19 #include "path-prefix.h"
21 namespace Inkscape {
22 namespace UI {
23 namespace Widget {
25 Toolbox::Toolbox(Gtk::Toolbar *toolbar,
26 Gtk::Orientation orientation)
27 : HandleBox(toolbar)
28 {
29 init_actions();
30 init_orientation(orientation);
31 init_style(toolbar->get_toolbar_style());
32 }
34 Toolbox::Toolbox(Gtk::Toolbar *toolbar,
35 Gtk::ToolbarStyle style,
36 Gtk::Orientation orientation)
37 : HandleBox(toolbar)
38 {
39 init_actions();
40 init_orientation(orientation);
41 init_style(style);
42 }
44 Gtk::Toolbar&
45 Toolbox::get_toolbar()
46 {
47 return static_cast<Gtk::Toolbar&>(*_widget);
48 }
50 static Glib::ustring
51 get_uidir_filename(char const *basename_utf8)
52 {
53 char *const ret_str = g_build_filename(INKSCAPE_UIDIR, basename_utf8, NULL);
54 Glib::ustring const ret(ret_str);
55 g_free(ret_str);
56 return ret;
57 }
59 void
60 Toolbox::init_actions()
61 {
62 _action_grp = Gtk::ActionGroup::create();
64 Gtk::RadioAction::Group icons;
65 _action_grp->add(Gtk::RadioAction::create(icons, "Icons", "Icons Only"),
66 sigc::mem_fun(*this, &Toolbox::on_change_style_icons));
67 _action_grp->add(Gtk::RadioAction::create(icons, "Text", "Text Only"),
68 sigc::mem_fun(*this, &Toolbox::on_change_style_text));
69 _action_grp->add(Gtk::RadioAction::create(icons, "Both", "Text Below Icons"),
70 sigc::mem_fun(*this, &Toolbox::on_change_style_both));
71 _action_grp->add(Gtk::RadioAction::create(icons, "BothHoriz", "Text Beside Icons"),
72 sigc::mem_fun(*this, &Toolbox::on_change_style_both_horiz));
74 _detach_grp = Gtk::ActionGroup::create();
76 Gtk::RadioAction::Group orient;
77 _detach_grp->add(Gtk::RadioAction::create(orient, "OrientHoriz", "Horizontal"),
78 sigc::mem_fun(*this, &Toolbox::on_change_orient_horiz));
79 _detach_grp->add(Gtk::RadioAction::create(orient, "OrientVert", "Vertical"),
80 sigc::mem_fun(*this, &Toolbox::on_change_orient_vert));
82 _detach_grp->add(Gtk::ToggleAction::create("ShowArrow", "Show Arrow",
83 Glib::ustring(), true),
84 sigc::mem_fun(*this, &Toolbox::on_show_arrow));
86 _ui_mgr = Gtk::UIManager::create();
87 _ui_mgr->insert_action_group(_action_grp);
88 _ui_mgr->insert_action_group(_detach_grp);
89 _ui_mgr->add_ui_from_file(get_uidir_filename("toolbox.xml"));
91 _context_menu = static_cast<Gtk::Menu*>(_ui_mgr->get_widget("/ToolboxMenu"));
93 static_cast<Gtk::Toolbar*>(_widget)->signal_popup_context_menu()
94 .connect(sigc::mem_fun(*this, &Toolbox::on_popup_context_menu));
96 _detach_grp->set_sensitive(false);
97 }
99 void
100 Toolbox::init_orientation(Gtk::Orientation const &orientation)
101 {
102 static_cast<Gtk::Toolbar*>(_widget)->set_orientation(orientation);
103 if (orientation == Gtk::ORIENTATION_VERTICAL) {
104 set_handle_position(Gtk::POS_TOP);
105 }
106 switch (orientation) {
107 case Gtk::ORIENTATION_HORIZONTAL: {
108 Glib::RefPtr<Gtk::RadioAction>::cast_static(_detach_grp->get_action("OrientHoriz"))
109 ->set_active();
110 break;
111 }
112 case Gtk::ORIENTATION_VERTICAL: {
113 Glib::RefPtr<Gtk::RadioAction>::cast_static(_detach_grp->get_action("OrientVert"))
114 ->set_active();
115 break;
116 }
117 }
118 }
120 void
121 Toolbox::init_style(Gtk::ToolbarStyle const &style)
122 {
123 switch (style) {
124 case Gtk::TOOLBAR_ICONS: {
125 Glib::RefPtr<Gtk::RadioAction>::cast_static(_action_grp->get_action("Icons"))
126 ->activate();
127 break;
128 }
129 case Gtk::TOOLBAR_TEXT: {
130 Glib::RefPtr<Gtk::RadioAction>::cast_static(_action_grp->get_action("Text"))
131 ->activate();
132 break;
133 }
134 case Gtk::TOOLBAR_BOTH: {
135 Glib::RefPtr<Gtk::RadioAction>::cast_static(_action_grp->get_action("Both"))
136 ->activate();
137 break;
138 }
139 case Gtk::TOOLBAR_BOTH_HORIZ: {
140 Glib::RefPtr<Gtk::RadioAction>::cast_static(_action_grp->get_action("BothHoriz"))
141 ->activate();
142 break;
143 }
144 }
145 }
147 bool
148 Toolbox::on_popup_context_menu(int x, int y, int button)
149 {
150 (void)x;
151 (void)y;
152 _context_menu->popup(button, gtk_get_current_event_time());
153 return true;
154 }
156 void
157 Toolbox::on_child_attached(Gtk::Widget *widget)
158 {
159 (void)widget;
160 Gtk::Toolbar *toolbar = static_cast<Gtk::Toolbar*>(_widget);
162 if (!(toolbar->get_show_arrow())) {
163 Glib::RefPtr<Gtk::RadioAction>::cast_static(_detach_grp->get_action("ShowArrow"))
164 ->activate();
165 }
167 if (get_handle_position() == Gtk::POS_LEFT
168 && toolbar->get_orientation() != Gtk::ORIENTATION_HORIZONTAL) {
169 Glib::RefPtr<Gtk::RadioAction>::cast_static(_detach_grp->get_action("OrientHoriz"))
170 ->activate();
171 } else if (get_handle_position() == Gtk::POS_TOP
172 && toolbar->get_orientation() != Gtk::ORIENTATION_VERTICAL) {
173 Glib::RefPtr<Gtk::RadioAction>::cast_static(_detach_grp->get_action("OrientVert"))
174 ->activate();
175 }
177 _detach_grp->set_sensitive(false);
178 }
180 void
181 Toolbox::on_child_detached(Gtk::Widget *widget)
182 {
183 (void)widget;
184 _detach_grp->set_sensitive(true);
186 Glib::RefPtr<Gtk::RadioAction>::cast_static(_detach_grp->get_action("ShowArrow"))
187 ->set_active(false);
188 }
190 void
191 Toolbox::on_change_style_icons()
192 {
193 Glib::RefPtr<Gtk::RadioAction> action = Glib::RefPtr<Gtk::RadioAction>::cast_static(_action_grp->get_action("Icons"));
194 if (action->get_active()) {
195 static_cast<Gtk::Toolbar*>(_widget)->set_toolbar_style(Gtk::TOOLBAR_ICONS);
196 }
197 }
199 void
200 Toolbox::on_change_style_text()
201 {
202 Glib::RefPtr<Gtk::RadioAction> action = Glib::RefPtr<Gtk::RadioAction>::cast_static(_action_grp->get_action("Text"));
203 if (action->get_active()) {
204 static_cast<Gtk::Toolbar*>(_widget)->set_toolbar_style(Gtk::TOOLBAR_TEXT);
205 }
206 }
208 void
209 Toolbox::on_change_style_both()
210 {
211 Glib::RefPtr<Gtk::RadioAction> action = Glib::RefPtr<Gtk::RadioAction>::cast_static(_action_grp->get_action("Both"));
212 if (action->get_active()) {
213 static_cast<Gtk::Toolbar*>(_widget)->set_toolbar_style(Gtk::TOOLBAR_BOTH);
214 }
215 }
217 void
218 Toolbox::on_change_style_both_horiz()
219 {
220 Glib::RefPtr<Gtk::RadioAction> action = Glib::RefPtr<Gtk::RadioAction>::cast_static(_action_grp->get_action("BothHoriz"));
221 if (action->get_active()) {
222 static_cast<Gtk::Toolbar*>(_widget)->set_toolbar_style(Gtk::TOOLBAR_BOTH_HORIZ);
223 }
224 }
226 void
227 Toolbox::on_change_orient_horiz()
228 {
229 Glib::RefPtr<Gtk::RadioAction> action = Glib::RefPtr<Gtk::RadioAction>::cast_static(_detach_grp->get_action("OrientHoriz"));
230 if (action->get_active()) {
231 static_cast<Gtk::Toolbar*>(_widget)->set_orientation(Gtk::ORIENTATION_HORIZONTAL);
232 }
233 }
235 void
236 Toolbox::on_change_orient_vert()
237 {
238 Glib::RefPtr<Gtk::RadioAction> action = Glib::RefPtr<Gtk::RadioAction>::cast_static(_detach_grp->get_action("OrientVert"));
239 if (action->get_active()) {
240 static_cast<Gtk::Toolbar*>(_widget)->set_orientation(Gtk::ORIENTATION_VERTICAL);
241 }
242 }
244 void
245 Toolbox::on_show_arrow()
246 {
247 Glib::RefPtr<Gtk::RadioAction> action = Glib::RefPtr<Gtk::RadioAction>::cast_static(_detach_grp->get_action("ShowArrow"));
248 Gtk::Toolbar *toolbar = static_cast<Gtk::Toolbar*>(_widget);
249 if (action->get_active()) {
250 toolbar->set_show_arrow(true);
251 } else {
252 toolbar->set_show_arrow(false);
253 }
254 }
256 } // namespace Widget
257 } // namespace UI
258 } // namespace Inkscape
260 /*
261 Local Variables:
262 mode:c++
263 c-file-style:"stroustrup"
264 c-file-offsets:((innamespace . 0)(inline-open . 0))
265 indent-tabs-mode:nil
266 fill-column:99
267 End:
268 */
269 // vim: filetype=c++:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :