f282449dd0994bda824c573b3e4363e953c69630
1 /**
2 * \brief A desktop dock pane to dock dialogs.
3 *
4 * Author:
5 * Gustav Broberg <broberg@kth.se>
6 *
7 * Copyright (C) 2007 Authors
8 *
9 * Released under GNU GPL. Read the file 'COPYING' for more information.
10 */
12 #include "inkscape.h"
13 #include "prefs-utils.h"
14 #include "desktop.h"
16 #include "dock.h"
18 #include <gtkmm/adjustment.h>
20 namespace Inkscape {
21 namespace UI {
22 namespace Widget {
24 namespace {
26 void hideCallback(GtkObject *object, gpointer dock_ptr)
27 {
28 g_return_if_fail( dock_ptr != NULL );
30 Dock *dock = (Dock *)dock_ptr;
31 dock->hide();
32 }
34 void unhideCallback(GtkObject *object, gpointer dock_ptr)
35 {
36 g_return_if_fail( dock_ptr != NULL );
38 Dock *dock = (Dock *)dock_ptr;
39 dock->show();
40 }
42 }
44 const int Dock::_default_empty_width = 0;
45 const int Dock::_default_dock_bar_width = 36;
48 Dock::Dock(Gtk::Orientation orientation)
49 : _gdl_dock (GDL_DOCK (gdl_dock_new())),
50 _gdl_dock_bar (GDL_DOCK_BAR (gdl_dock_bar_new(GDL_DOCK(_gdl_dock)))),
51 _scrolled_window (Gtk::manage(new Gtk::ScrolledWindow))
52 {
53 gdl_dock_bar_set_orientation(_gdl_dock_bar, static_cast<GtkOrientation>(orientation));
55 switch (orientation) {
56 case Gtk::ORIENTATION_VERTICAL:
57 _dock_box = Gtk::manage(new Gtk::HBox());
58 _paned = Gtk::manage(new Gtk::VPaned());
59 break;
60 case Gtk::ORIENTATION_HORIZONTAL:
61 _dock_box = Gtk::manage(new Gtk::VBox());
62 _paned = Gtk::manage(new Gtk::HPaned());
63 }
65 _scrolled_window->add(*_dock_box);
66 _scrolled_window->set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
68 _paned->pack1(*Glib::wrap(GTK_WIDGET(_gdl_dock)), false, false);
69 _paned->pack2(_filler, true, false);
71 _dock_box->pack_start(*_paned, Gtk::PACK_EXPAND_WIDGET);
72 _dock_box->pack_end(*Gtk::manage(Glib::wrap(GTK_WIDGET(_gdl_dock_bar))), Gtk::PACK_SHRINK);
73 _dock_box->get_parent()->set_resize_mode(Gtk::RESIZE_PARENT);
75 _scrolled_window->set_size_request(0);
77 GdlSwitcherStyle gdl_switcher_style =
78 static_cast<GdlSwitcherStyle>(prefs_get_int_attribute_limited("options.dock", "switcherstyle",
79 GDL_SWITCHER_STYLE_BOTH, 0, 4));
81 g_object_set (GDL_DOCK_OBJECT(_gdl_dock)->master,
82 "switcher-style", gdl_switcher_style,
83 NULL);
85 GdlDockBarStyle gdl_dock_bar_style =
86 static_cast<GdlDockBarStyle>(prefs_get_int_attribute_limited("options.dock", "dockbarstyle",
87 GDL_DOCK_BAR_BOTH, 0, 3));
89 gdl_dock_bar_set_style(_gdl_dock_bar, gdl_dock_bar_style);
91 g_signal_connect(G_OBJECT(INKSCAPE), "dialogs_hide", G_CALLBACK(hideCallback), (void *)this);
92 g_signal_connect(G_OBJECT(INKSCAPE), "dialogs_unhide", G_CALLBACK(unhideCallback), (void *)this);
94 signal_layout_changed().connect(sigc::mem_fun(*this, &Inkscape::UI::Widget::Dock::_onLayoutChanged));
95 }
97 Dock::~Dock()
98 {
99 g_free(_gdl_dock);
100 g_free(_gdl_dock_bar);
101 }
103 void
104 Dock::addItem(DockItem& item, DockItem::Placement placement)
105 {
106 _dock_items.push_back(&item);
107 gdl_dock_add_item(_gdl_dock, GDL_DOCK_ITEM(item.gobj()), (GdlDockPlacement)placement);
109 // FIXME: This is a hack to prevent the dock from expanding the main window, this can't be done
110 // initially as the paned doesn't exist.
111 if (Gtk::Paned *paned = getParentPaned())
112 paned->set_resize_mode(Gtk::RESIZE_QUEUE);
113 }
115 Gtk::Widget&
116 Dock::getWidget()
117 {
118 return *_scrolled_window;
119 }
121 Gtk::Paned *
122 Dock::getParentPaned()
123 {
124 g_return_val_if_fail(_dock_box, 0);
125 Gtk::Container *parent = getWidget().get_parent();
126 return (parent != 0 ? dynamic_cast<Gtk::Paned *>(parent) : 0);
127 }
130 Gtk::Paned *
131 Dock::getPaned()
132 {
133 return _paned;
134 }
137 bool
138 Dock::isEmpty() const
139 {
140 std::list<const DockItem *>::const_iterator
141 i = _dock_items.begin(),
142 e = _dock_items.end();
144 for (; i != e; ++i)
145 if ((*i)->getState() == DockItem::DOCKED_STATE)
146 return false;
148 return true;
149 }
151 bool
152 Dock::hasIconifiedItems() const
153 {
154 std::list<const DockItem *>::const_iterator
155 i = _dock_items.begin(),
156 e = _dock_items.end();
158 for (; i != e; ++i)
159 if ((*i)->isIconified())
160 return true;
162 return false;
163 }
165 void
166 Dock::hide()
167 {
168 getWidget().hide();
169 }
171 void
172 Dock::show()
173 {
174 getWidget().show();
175 }
177 void
178 Dock::toggleDockable(int width, int height)
179 {
180 static int prev_horizontal_position, prev_vertical_position;
182 Gtk::Paned *parent_paned = getParentPaned();
184 if (width > 0 && height > 0) {
185 prev_horizontal_position = parent_paned->get_position();
186 prev_vertical_position = _paned->get_position();
188 if (getWidget().get_width() < width)
189 parent_paned->set_position(parent_paned->get_width() - width);
191 if (_paned->get_position() < height)
192 _paned->set_position(height);
194 } else {
195 parent_paned->set_position(prev_horizontal_position);
196 _paned->set_position(prev_vertical_position);
197 }
199 }
201 void
202 Dock::scrollToItem(DockItem& item)
203 {
204 int item_x, item_y;
205 item.getWidget().translate_coordinates(getWidget(), 0, 0, item_x, item_y);
207 int dock_height = getWidget().get_height(), item_height = item.getWidget().get_height();
208 double vadjustment = _scrolled_window->get_vadjustment()->get_value();
210 if (item_y < 0)
211 _scrolled_window->get_vadjustment()->set_value(vadjustment + item_y);
212 else if (item_y + item_height > dock_height)
213 _scrolled_window->get_vadjustment()->set_value(
214 vadjustment + ((item_y + item_height) - dock_height));
215 }
217 Glib::SignalProxy0<void>
218 Dock::signal_layout_changed()
219 {
220 return Glib::SignalProxy0<void>(Glib::wrap(GTK_WIDGET(_gdl_dock)),
221 &_signal_layout_changed_proxy);
222 }
224 void
225 Dock::_onLayoutChanged()
226 {
227 if (isEmpty()) {
229 if (hasIconifiedItems())
230 _scrolled_window->set_size_request(_default_dock_bar_width);
231 else
232 _scrolled_window->set_size_request(_default_empty_width);
234 getParentPaned()->set_position(INT_MAX);
235 } else {
236 _scrolled_window->set_size_request(-1);
237 }
238 }
241 const Glib::SignalProxyInfo
242 Dock::_signal_layout_changed_proxy =
243 {
244 "layout-changed",
245 (GCallback) &Glib::SignalProxyNormal::slot0_void_callback,
246 (GCallback) &Glib::SignalProxyNormal::slot0_void_callback
247 };
250 } // namespace Widget
251 } // namespace UI
252 } // namespace Inkscape
255 /*
256 Local Variables:
257 mode:c++
258 c-file-style:"stroustrup"
259 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
260 indent-tabs-mode:nil
261 fill-column:99
262 End:
263 */
264 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99