Code

Cleanup of device internal handling and API.
[inkscape.git] / src / ui / dialog / layer-properties.cpp
1 /** @file
2  * @brief Dialog for renaming layers
3  */
4 /* Author:
5  *   Bryce W. Harrington <bryce@bryceharrington.com>
6  *   Andrius R. <knutux@gmail.com>
7  *
8  * Copyright (C) 2004 Bryce Harrington
9  * Copyright (C) 2006 Andrius R.
10  *
11  * Released under GNU GPL.  Read the file 'COPYING' for more information
12  */
14 #include <gtkmm/stock.h>
15 #include <glibmm/i18n.h>
16 #include "inkscape.h"
17 #include "desktop.h"
18 #include "document.h"
19 #include "layer-manager.h"
20 #include "message-stack.h"
21 #include "desktop-handles.h"
22 #include "sp-object.h"
23 #include "sp-item.h"
25 #include "layer-properties.h"
27 namespace Inkscape {
28 namespace UI {
29 namespace Dialogs {
31 LayerPropertiesDialog::LayerPropertiesDialog()
32 : _strategy(NULL), _desktop(NULL), _layer(NULL), _position_visible(false)
33 {
34     Gtk::VBox *mainVBox = get_vbox();
36     _layout_table.set_spacings(4);
37     _layout_table.resize (1, 2);
39     // Layer name widgets
40     _layer_name_entry.set_activates_default(true);
41     _layer_name_label.set_label(_("Layer name:"));
42     _layer_name_label.set_alignment(1.0, 0.5);
44     _layout_table.attach(_layer_name_label,
45                          0, 1, 0, 1, Gtk::FILL, Gtk::FILL);
46     _layout_table.attach(_layer_name_entry,
47                          1, 2, 0, 1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL);
48     mainVBox->pack_start(_layout_table, false, false, 4);
50     // Buttons
51     _close_button.set_use_stock(true);
52     _close_button.set_label(Gtk::Stock::CANCEL.id);
53     _close_button.set_flags(Gtk::CAN_DEFAULT);
55     _apply_button.set_use_underline(true);
56     _apply_button.set_flags(Gtk::CAN_DEFAULT);
58     _close_button.signal_clicked()
59         .connect(sigc::mem_fun(*this, &LayerPropertiesDialog::_close));
60     _apply_button.signal_clicked()
61         .connect(sigc::mem_fun(*this, &LayerPropertiesDialog::_apply));
63     signal_delete_event().connect(
64         sigc::bind_return(
65             sigc::hide(sigc::mem_fun(*this, &LayerPropertiesDialog::_close)),
66             true
67         )
68     );
70     add_action_widget(_close_button, Gtk::RESPONSE_CLOSE);
71     add_action_widget(_apply_button, Gtk::RESPONSE_APPLY);
73     _apply_button.grab_default();
75     show_all_children();
76 }
78 LayerPropertiesDialog::~LayerPropertiesDialog() {
79     _setDesktop(NULL);
80     _setLayer(NULL);
81 }
83 void LayerPropertiesDialog::_showDialog(LayerPropertiesDialog::Strategy &strategy,
84                                         SPDesktop *desktop, SPObject *layer)
85 {
86     LayerPropertiesDialog *dialog = new LayerPropertiesDialog();
88     dialog->_strategy = &strategy;
89     dialog->_setDesktop(desktop);
90     dialog->_setLayer(layer);
92     dialog->_strategy->setup(*dialog);
94     dialog->set_modal(true);
95     desktop->setWindowTransient (dialog->gobj());
96     dialog->property_destroy_with_parent() = true;
98     dialog->show();
99     dialog->present();
102 void
103 LayerPropertiesDialog::_apply()
105     g_assert(_strategy != NULL);
107     _strategy->perform(*this);
108     sp_document_done(sp_desktop_document(SP_ACTIVE_DESKTOP), SP_VERB_NONE,
109                      _("Add layer"));
111     _close();
114 void
115 LayerPropertiesDialog::_close()
117     _setLayer(NULL);
118     _setDesktop(NULL);
119     destroy_();
120     Glib::signal_idle().connect(
121         sigc::bind_return(
122             sigc::bind(sigc::ptr_fun(&::operator delete), this),
123             false 
124         )
125     );
128 void
129 LayerPropertiesDialog::_setup_position_controls() {
130     if ( NULL == _layer || _desktop->currentRoot() == _layer ) {
131         // no layers yet, so option above/below/sublayer is useless
132         return;
133     }
135     _position_visible = true;
136     _dropdown_list = Gtk::ListStore::create(_dropdown_columns);
137     _layer_position_combo.set_model(_dropdown_list);
138     _layer_position_combo.pack_start(_label_renderer);
139     _layer_position_combo.set_cell_data_func(_label_renderer,
140                                              sigc::mem_fun(*this, &LayerPropertiesDialog::_prepareLabelRenderer));
142     _layout_table.resize (2, 2);
144     Gtk::ListStore::iterator row;
145     row = _dropdown_list->append();
146     row->set_value(_dropdown_columns.position, LPOS_ABOVE);
147     row->set_value(_dropdown_columns.name, Glib::ustring(_("Above current")));
148     _layer_position_combo.set_active(row);
149     row = _dropdown_list->append();
150     row->set_value(_dropdown_columns.position, LPOS_BELOW);
151     row->set_value(_dropdown_columns.name, Glib::ustring(_("Below current")));
152     row = _dropdown_list->append();
153     row->set_value(_dropdown_columns.position, LPOS_CHILD);
154     row->set_value(_dropdown_columns.name, Glib::ustring(_("As sublayer of current")));
156     _layout_table.attach(_layer_position_combo,
157                          1, 2, 1, 2, Gtk::FILL | Gtk::EXPAND, Gtk::FILL);
158     _layer_position_label.set_label(_("Position:"));
159     _layer_position_label.set_alignment(1.0, 0.5);
160     _layout_table.attach(_layer_position_label,
161                          0, 1, 1, 2, Gtk::FILL, Gtk::FILL);
162     show_all_children();
165 /** Formats the label for a given layer row 
166  */
167 void LayerPropertiesDialog::_prepareLabelRenderer(
168     Gtk::TreeModel::const_iterator const &row
169 ) {
170     Glib::ustring name=(*row)[_dropdown_columns.name];
171     _label_renderer.property_markup() = name.c_str();
174 void LayerPropertiesDialog::Rename::setup(LayerPropertiesDialog &dialog) {
175     SPDesktop *desktop=dialog._desktop;
176     dialog.set_title(_("Rename Layer"));
177     gchar const *name = desktop->currentLayer()->label();
178     dialog._layer_name_entry.set_text(( name ? name : _("Layer") ));
179     dialog._apply_button.set_label(_("_Rename"));
182 void LayerPropertiesDialog::Rename::perform(LayerPropertiesDialog &dialog) {
183     SPDesktop *desktop=dialog._desktop;
184     Glib::ustring name(dialog._layer_name_entry.get_text());
185     if (name.empty())
186         return;
187     desktop->layer_manager->renameLayer( desktop->currentLayer(),
188                                          (gchar *)name.c_str(),
189                                          FALSE
190     );
191     sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, 
192                      _("Rename layer"));
193     // TRANSLATORS: This means "The layer has been renamed"
194     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Renamed layer"));
197 void LayerPropertiesDialog::Create::setup(LayerPropertiesDialog &dialog) {
198     dialog.set_title(_("Add Layer"));
199     //TODO: find an unused layer number, forming name from _("Layer ") + "%d"
200     dialog._layer_name_entry.set_text(_("Layer"));
201     dialog._apply_button.set_label(_("_Add"));
202     dialog._setup_position_controls();
205 void LayerPropertiesDialog::Create::perform(LayerPropertiesDialog &dialog) {
206     SPDesktop *desktop=dialog._desktop;
208     LayerRelativePosition position = LPOS_ABOVE;
209     
210     if (dialog._position_visible) {
211         Gtk::ListStore::iterator activeRow(dialog._layer_position_combo.get_active());
212         position = activeRow->get_value(dialog._dropdown_columns.position);
213     }
214     Glib::ustring name(dialog._layer_name_entry.get_text());
215     if (name.empty())
216         return;
218     SPObject *new_layer=Inkscape::create_layer(desktop->currentRoot(), dialog._layer, position);
219     
220     if (!name.empty()) {
221         desktop->layer_manager->renameLayer( new_layer, (gchar *)name.c_str(), TRUE );
222     }
223     sp_desktop_selection(desktop)->clear();
224     desktop->setCurrentLayer(new_layer);
225     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("New layer created."));
228 void LayerPropertiesDialog::_setDesktop(SPDesktop *desktop) {
229     if (desktop) {
230         Inkscape::GC::anchor (desktop);
231     }
232     if (_desktop) {
233         Inkscape::GC::release (_desktop);
234     }
235     _desktop = desktop;
238 void LayerPropertiesDialog::_setLayer(SPObject *layer) {
239     if (layer) {
240         sp_object_ref(layer, NULL);
241     }
242     if (_layer) {
243         sp_object_unref(_layer, NULL);
244     }
245     _layer = layer;
248 } // namespace
249 } // namespace
250 } // namespace
253 /*
254   Local Variables:
255   mode:c++
256   c-file-style:"stroustrup"
257   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
258   indent-tabs-mode:nil
259   fill-column:99
260   End:
261 */
262 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :