Fix a crash when running single-threaded and dragging the time slider.
[synfig.git] / synfig-studio / trunk / src / gtkmm / canvasproperties.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file canvasproperties.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "canvasproperties.h"
33 #include <gtkmm/frame.h>
34 #include <gtkmm/table.h>
35 #include <gtkmm/label.h>
36 #include <gtkmm/alignment.h>
37 #include <synfigapp/canvasinterface.h>
38 #include "metadatatreestore.h"
39 #include <gtkmm/treeview.h>
40 #include <gtkmm/scrolledwindow.h>
41 #include "app.h"
42
43 #endif
44
45 /* === U S I N G =========================================================== */
46
47 using namespace std;
48 using namespace etl;
49 using namespace synfig;
50 using namespace studio;
51
52 /* === M A C R O S ========================================================= */
53
54 /* === G L O B A L S ======================================================= */
55
56 /* === P R O C E D U R E S ================================================= */
57
58 /* === M E T H O D S ======================================================= */
59
60 CanvasProperties::CanvasProperties(Gtk::Window& parent,handle<synfigapp::CanvasInterface> canvas_interface):
61         Gtk::Dialog(_("Canvas Properties"),parent,false,true),
62         canvas_interface_(canvas_interface)
63 {
64         widget_rend_desc.show();
65         widget_rend_desc.signal_changed().connect(sigc::mem_fun(*this,&studio::CanvasProperties::on_rend_desc_changed));
66
67         Gtk::Alignment *dialogPadding = manage(new Gtk::Alignment(0, 0, 1, 1));
68         dialogPadding->set_padding(12, 12, 12, 12);
69         get_vbox()->pack_start(*dialogPadding, false, false, 0);
70
71         Gtk::VBox *dialogBox = manage(new Gtk::VBox(false, 12));
72         dialogPadding->add(*dialogBox);
73
74         Gtk::Frame *info_frame=manage(new Gtk::Frame(_("Canvas Info")));
75         info_frame->set_shadow_type(Gtk::SHADOW_NONE);
76         ((Gtk::Label *) info_frame->get_label_widget())->set_markup(_("<b>Canvas Info</b>"));
77         dialogBox->pack_start(*info_frame, false, false, 0);
78
79         Gtk::Alignment *infoPadding = manage(new Gtk::Alignment(0, 0, 1, 1));
80         infoPadding->set_padding(6, 0, 24, 0);
81         info_frame->add(*infoPadding);
82
83         Gtk::Table *info_table=manage(new Gtk::Table(2,2,false));
84         info_table->set_row_spacings(6);
85         info_table->set_col_spacings(12);
86         infoPadding->add(*info_table);
87
88         // The root canvas doesn't have an ID, so don't
89         // display it if this is a root canvas.
90         if(!canvas_interface_->get_canvas()->is_root())
91         {
92                 Gtk::Label *idLabel = manage(new Gtk::Label(_("_ID"), true));
93                 idLabel->set_alignment(0, 0.5);
94                 idLabel->set_mnemonic_widget(entry_id);
95                 info_table->attach(*idLabel, 0, 1, 0, 1, Gtk::SHRINK|Gtk::FILL, Gtk::SHRINK|Gtk::FILL, 0, 0);
96                 info_table->attach(entry_id, 1, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK|Gtk::FILL, 0, 0);
97         }
98         Gtk::Label *nameLabel = manage(new Gtk::Label(_("_Name"), true));
99         nameLabel->set_alignment(0, 0.5);
100         nameLabel->set_mnemonic_widget(entry_name);
101         Gtk::Label *descriptionLabel = manage(new Gtk::Label(_("_Description"), true));
102         descriptionLabel->set_alignment(0, 0.5);
103         descriptionLabel->set_mnemonic_widget(entry_description);
104         info_table->attach(*nameLabel, 0, 1, 1, 2, Gtk::SHRINK|Gtk::FILL, Gtk::SHRINK|Gtk::FILL, 0, 0);
105         info_table->attach(*descriptionLabel, 0, 1, 2, 3, Gtk::SHRINK|Gtk::FILL, Gtk::SHRINK|Gtk::FILL, 0, 0);
106         info_table->attach(entry_name, 1, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK|Gtk::FILL, 0, 0);
107         info_table->attach(entry_description, 1, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK|Gtk::FILL, 0, 0);
108
109         dialogBox->pack_start(widget_rend_desc, false, false, 0);
110
111         canvas_interface_->signal_rend_desc_changed().connect(sigc::mem_fun(*this,&studio::CanvasProperties::refresh));
112
113         Gtk::Button *ok_button(manage(new class Gtk::Button(Gtk::StockID("gtk-ok"))));
114         ok_button->show();
115         add_action_widget(*ok_button,2);
116         ok_button->signal_clicked().connect(sigc::mem_fun(*this, &studio::CanvasProperties::on_ok_pressed));
117
118         Gtk::Button *apply_button(manage(new class Gtk::Button(Gtk::StockID("gtk-apply"))));
119         apply_button->show();
120         add_action_widget(*apply_button,1);
121         apply_button->signal_clicked().connect(sigc::mem_fun(*this, &studio::CanvasProperties::on_apply_pressed));
122
123         Gtk::Button *cancel_button(manage(new class Gtk::Button(Gtk::StockID("gtk-close"))));
124         cancel_button->show();
125         add_action_widget(*cancel_button,0);
126         cancel_button->signal_clicked().connect(sigc::mem_fun(*this, &studio::CanvasProperties::on_cancel_pressed));
127
128         //set_default_response(1);
129
130         get_vbox()->show_all();
131         refresh();
132
133         update_title();
134 }
135
136 Gtk::Widget&
137 CanvasProperties::create_meta_data_view()
138 {
139         MetaDataTreeStore::Model model;
140         meta_data_tree_view=(manage(new class Gtk::TreeView()));
141
142         meta_data_tree_view->append_column(_("Key"),model.key);
143         meta_data_tree_view->append_column_editable(_("Data"),model.data);
144         meta_data_tree_view->set_model(MetaDataTreeStore::create(canvas_interface_));
145         meta_data_tree_view->set_rules_hint();
146         meta_data_tree_view->show();
147
148         Gtk::ScrolledWindow *scrolledwindow = manage(new class Gtk::ScrolledWindow());
149         scrolledwindow->set_flags(Gtk::CAN_FOCUS);
150         scrolledwindow->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
151         scrolledwindow->add(*meta_data_tree_view);
152         scrolledwindow->set_shadow_type(Gtk::SHADOW_ETCHED_IN);
153         scrolledwindow->show();
154
155
156
157         Gtk::Table *table=manage(new Gtk::Table());
158         table->attach(*scrolledwindow, 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
159
160         Gtk::Button* button_add(manage(new Gtk::Button(Gtk::StockID("gtk-add"))));
161         button_add->show();
162         button_add->signal_clicked().connect(sigc::mem_fun(*this,&CanvasProperties::on_button_meta_data_add));
163         table->attach(*button_add, 0, 1, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK|Gtk::FILL, 0, 0);
164
165         Gtk::Button* button_delete(manage(new Gtk::Button(Gtk::StockID("gtk-delete"))));
166         button_delete->show();
167         button_delete->signal_clicked().connect(sigc::mem_fun(*this,&CanvasProperties::on_button_meta_data_delete));
168         table->attach(*button_delete, 1, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK|Gtk::FILL, 0, 0);
169
170         table->show();
171         return *table;
172 }
173
174 void
175 CanvasProperties::on_button_meta_data_add()
176 {
177         synfig::String key;
178         if(App::dialog_entry("New MetaData Entry", "Please enter the name of the key",key) && !key.empty())
179         {
180                 canvas_interface_->set_meta_data(key," ");
181         }
182
183 }
184
185 void
186 CanvasProperties::on_button_meta_data_delete()
187 {
188 }
189
190 void
191 CanvasProperties::update_title()
192 {
193         set_title(_("Properties")+String(" - ")+canvas_interface_->get_canvas()->get_name());
194 }
195
196 void
197 CanvasProperties::refresh()
198 {
199         widget_rend_desc.set_rend_desc(canvas_interface_->get_canvas()->rend_desc());
200         entry_id.set_text(canvas_interface_->get_canvas()->get_id());
201         entry_name.set_text(canvas_interface_->get_canvas()->get_name());
202         entry_description.set_text(canvas_interface_->get_canvas()->get_description());
203
204         dirty_rend_desc=false;
205
206         update_title();
207 }
208
209 CanvasProperties::~CanvasProperties()
210 {
211 }
212
213 void
214 CanvasProperties::on_rend_desc_changed()
215 {
216         dirty_rend_desc=true;
217 }
218
219 void
220 CanvasProperties::on_apply_pressed()
221 {
222         if(dirty_rend_desc)
223                 canvas_interface_->set_rend_desc(widget_rend_desc.get_rend_desc());
224         if(entry_id.get_text()!=canvas_interface_->get_canvas()->get_id())
225                 canvas_interface_->set_id(entry_id.get_text());
226         if(entry_name.get_text()!=canvas_interface_->get_canvas()->get_name())
227                 canvas_interface_->set_name(entry_name.get_text());
228         if(entry_description.get_text()!=canvas_interface_->get_canvas()->get_description())
229                 canvas_interface_->set_description(entry_description.get_text());
230
231         dirty_rend_desc=false;
232 }
233
234 void
235 CanvasProperties::on_ok_pressed()
236 {
237         on_apply_pressed();
238         hide();
239 }
240
241 void
242 CanvasProperties::on_cancel_pressed()
243 {
244         refresh();
245         hide();
246 }