Code

d642805c18f5a132bd001676b146b3c04bc8b83c
[inkscape.git] / src / ui / dialog / extension-editor.cpp
1 /**
2  * \brief Extension editor dialog
3  *
4  * Authors:
5  *   Bryce W. Harrington <bryce@bryceharrington.org>
6  *   Ted Gould <ted@gould.cx>
7  *
8  * Copyright (C) 2004-2006 Authors
9  *
10  * Released under GNU GPL.  Read the file 'COPYING' for more information.
11  */
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
17 #include <glibmm/i18n.h>
19 #include <gtkmm/frame.h>
20 #include <gtkmm/scrolledwindow.h>
21 #include <gtkmm/alignment.h>
22 #include <gtkmm/notebook.h>
24 #include "extension-editor.h"
25 #include "verbs.h"
26 #include "prefs-utils.h"
27 #include "interface.h"
29 #include "extension/extension.h"
30 #include "extension/db.h"
32 namespace Inkscape {
33 namespace UI {
34 namespace Dialog {
36 /** \brief  Create a new ExtensionEditor dialog
37     \return None
39     This function creates a new extension editor dialog.  The dialog
40     consists of two basic areas.  The left side is a tree widget, which
41     is only used as a list.  And the right side is a notebook of information
42     about the selected extension.  A handler is set up so that when
43     a new extension is selected, the notebooks are changed appropriately.
44 */
45 ExtensionEditor::ExtensionEditor()
46     : Dialog ("dialogs.extensioneditor", SP_VERB_DIALOG_EXTENSIONEDITOR)
47 {
48     _notebook_info.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
49     _notebook_help.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
50     _notebook_params.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
51  
52     //Main HBox
53     Gtk::HBox* hbox_list_page = Gtk::manage(new Gtk::HBox());
54     hbox_list_page->set_border_width(12);
55     hbox_list_page->set_spacing(12);
56     this->get_vbox()->add(*hbox_list_page);
59     //Pagelist
60     Gtk::Frame* list_frame = Gtk::manage(new Gtk::Frame());
61     Gtk::ScrolledWindow* scrolled_window = Gtk::manage(new Gtk::ScrolledWindow());
62     hbox_list_page->pack_start(*list_frame, false, true, 0);
63     _page_list.set_headers_visible(false);
64     scrolled_window->set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
65     scrolled_window->add(_page_list);
66     list_frame->set_shadow_type(Gtk::SHADOW_IN);
67     list_frame->add(*scrolled_window);
68     _page_list_model = Gtk::TreeStore::create(_page_list_columns);
69     _page_list.set_model(_page_list_model);
70     _page_list.append_column("name",_page_list_columns._col_name);
71     Glib::RefPtr<Gtk::TreeSelection> page_list_selection = _page_list.get_selection();
72     page_list_selection->signal_changed().connect(sigc::mem_fun(*this, &ExtensionEditor::on_pagelist_selection_changed));
73     page_list_selection->set_mode(Gtk::SELECTION_BROWSE);
76     //Pages
77     Gtk::VBox* vbox_page = Gtk::manage(new Gtk::VBox());
78     hbox_list_page->pack_start(*vbox_page, true, true, 0);
79     Gtk::Notebook * notebook = Gtk::manage(new Gtk::Notebook());
80     notebook->append_page(_notebook_info, *Gtk::manage(new Gtk::Label(_("Information"))));
81     notebook->append_page(_notebook_help, *Gtk::manage(new Gtk::Label(_("Help"))));
82     notebook->append_page(_notebook_params, *Gtk::manage(new Gtk::Label(_("Parameters"))));
83     vbox_page->pack_start(*notebook, true, true, 0);
85     Inkscape::Extension::db.foreach(dbfunc, this);
87     gchar const * defaultext = prefs_get_string_attribute("dialogs.extensioneditor", "selected-extension");
88     if (defaultext == NULL) defaultext = "org.inkscape.input.svg";
89     this->setExtension(defaultext);
91     show_all_children();
92 }
94 /** \brief  Destroys the extension editor dialog
95     \return None
96 */
97 ExtensionEditor::~ExtensionEditor()
98 {
99 }
101 void
102 ExtensionEditor::setExtension(Glib::ustring extension_id) {
103     _selection_search = extension_id;
104     _page_list_model->foreach_iter(sigc::mem_fun(*this, &ExtensionEditor::setExtensionIter));
105     return;
108 bool
109 ExtensionEditor::setExtensionIter(const Gtk::TreeModel::iterator &iter)
111     Gtk::TreeModel::Row row = *iter;
112     if (row[_page_list_columns._col_id] == _selection_search) {
113         _page_list.get_selection()->select(iter);
114         return true;
115     }
116     return false;
119 /** \brief  Called every time a new extention is selected
120     \return None
122     This function is set up to handle the signal for a changed extension
123     from the tree view in the left pane.  It figure out which extension
124     is selected and updates the widgets to have data for that extension.
125 */
126 void
127 ExtensionEditor::on_pagelist_selection_changed (void)
129     Glib::RefPtr<Gtk::TreeSelection> selection = _page_list.get_selection();
130     Gtk::TreeModel::iterator iter = selection->get_selected();
131     if (iter) {
132         /* Get the row info */
133         Gtk::TreeModel::Row row = *iter;
134         Glib::ustring id = row[_page_list_columns._col_id];
135         Glib::ustring name = row[_page_list_columns._col_name];
137         /* Set the selection in the preferences */
138         prefs_set_string_attribute("dialogs.extensioneditor", "selected-extension", id.c_str());
140         /* Adjust the dialog's title */
141         gchar title[500];
142         sp_ui_dialog_title_string (Inkscape::Verb::get(SP_VERB_DIALOG_EXTENSIONEDITOR), title);
143         Glib::ustring utitle(title);
144         set_title(utitle + ": " + name);
146         /* Clear the notbook pages */
147         _notebook_info.remove();
148         _notebook_help.remove();
149         _notebook_params.remove();
151         /* Make sure we have all the widges */
152         Gtk::Widget * info;
153         info = row[_page_list_columns._col_info];
154         if (info == NULL) {
155             info = Inkscape::Extension::db.get(id.c_str())->get_info_widget();
156             row[_page_list_columns._col_info] = info;
157             info->reference();
158         } else {
159             info->unparent();
160         }
162         Gtk::Widget * help;
163         help = row[_page_list_columns._col_help];
164         if (help == NULL) {
165             help = Inkscape::Extension::db.get(id.c_str())->get_help_widget();
166             row[_page_list_columns._col_help] = help;
167             help->reference();
168         } else {
169             help->unparent();
170         }
172         Gtk::Widget * params;
173         params = row[_page_list_columns._col_params];
174         if (params == NULL) {
175             params = Inkscape::Extension::db.get(id.c_str())->get_params_widget();
176             row[_page_list_columns._col_params] = params;
177             params->reference();
178         } else {
179             params->unparent();
180         }
182         /* Place them in the pages */
183         if (info != NULL) {
184             _notebook_info.add(*info);
185         }
186         if (help != NULL) {
187             _notebook_help.add(*help);
188         }
189         if (params != NULL) {
190             _notebook_params.add(*params);
191         }
193     }
195     return;
198 /** \brief  A function to pass to the iterator in the Extensions Database
199     \param  in_plug  The extension to evaluate
200     \param  in_data  A pointer to the Extension Editor class
201     \return None
203     This function is a static function with the prototype required for
204     the Extension Database's foreach function.  It will get called for
205     every extension in the database, and will then turn around and
206     call the more object oriented function \c add_extension in the
207     ExtensionEditor.
208 */
209 void
210 ExtensionEditor::dbfunc (Inkscape::Extension::Extension * in_plug, gpointer in_data)
212     ExtensionEditor * ee = reinterpret_cast<ExtensionEditor *>(in_data);
213     ee->add_extension(in_plug);
214     return;
217 /** \brief  Adds an extension into the tree model
218     \param  ext  The extension to add
219     \return The iterator representing the location in the tree model
221     This function takes the data out of the extension and puts it
222     into the tree model for the dialog.
223 */
224 Gtk::TreeModel::iterator
225 ExtensionEditor::add_extension (Inkscape::Extension::Extension * ext)
227     Gtk::TreeModel::iterator iter;
229     iter = _page_list_model->append();
231     Gtk::TreeModel::Row row = *iter;
232     row[_page_list_columns._col_name] = ext->get_name();
233     row[_page_list_columns._col_id] =   ext->get_id();
234     row[_page_list_columns._col_info] = NULL;
235     row[_page_list_columns._col_help] = NULL;
236     row[_page_list_columns._col_params] = NULL;
238     return iter;
241 } // namespace Dialog
242 } // namespace UI
243 } // namespace Inkscape
245 /*
246   Local Variables:
247   mode:c++
248   c-file-style:"stroustrup"
249   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
250   indent-tabs-mode:nil
251   fill-column:99
252   End:
253 */
254 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :