Code

fix bug #1568462 boolean operation remove mask/clip-path
[inkscape.git] / src / extension / paramenum.cpp
1 /** \file\r
2  * extension parameter for enumerations. \r
3  *\r
4  * It uses a Gtk:ComboBoxText widget in the extension UI.\r
5  */\r
6 \r
7 /*\r
8  * Author:\r
9  *   Johan Engelen <johan@shouraizou.nl>\r
10  *\r
11  * Copyright (C) 2006 Author\r
12  *\r
13  * Released under GNU GPL, read the file 'COPYING' for more information\r
14  */\r
15 \r
16 #ifdef HAVE_CONFIG_H\r
17 # include "config.h"\r
18 #endif\r
19 \r
20 \r
21 #include <gtkmm/box.h>\r
22 #include <gtkmm/comboboxtext.h>\r
23 #include <gtkmm/tooltips.h>\r
24 #include <gtkmm/label.h>\r
25 \r
26 #include <glibmm/i18n.h>\r
27 \r
28 #include <xml/node.h>\r
29 \r
30 #include "extension.h"\r
31 #include "prefs-utils.h"\r
32 #include "document-private.h"\r
33 #include "sp-object.h"\r
34 \r
35 #include "paramenum.h"\r
36 \r
37 /** \brief  The root directory in the preferences database for extension\r
38             related parameters. */\r
39 #define PREF_DIR "extensions"\r
40 \r
41 namespace Inkscape {\r
42 namespace Extension {\r
43 \r
44 ParamComboBox::ParamComboBox (const gchar * name, const gchar * guitext, const gchar * desc, const Parameter::_scope_t scope, Inkscape::Extension::Extension * ext, Inkscape::XML::Node * xml) :\r
45     Parameter(name, guitext, desc, scope, ext)\r
46 {              \r
47     choices = NULL;\r
48     _value = NULL;\r
49     \r
50     // Read XML tree to add enumeration items:\r
51     // printf("Extension Constructor: ");\r
52     if (xml != NULL) {\r
53         Inkscape::XML::Node *child_repr = sp_repr_children(xml);\r
54         while (child_repr != NULL) {\r
55             char const * chname = child_repr->name();\r
56             if (!strcmp(chname, "item")) {\r
57                 Glib::ustring * newitem = NULL;\r
58                 const char * contents = sp_repr_children(child_repr)->content();\r
59                 if (contents != NULL)\r
60                      newitem = new Glib::ustring(contents);\r
61                 if (newitem != NULL) choices = g_slist_append(choices, newitem);\r
62             }\r
63             child_repr = sp_repr_next(child_repr);\r
64         }\r
65     }\r
66     \r
67     // Initialize _value with the default value from xml\r
68     // for simplicity : default to the contents of the first xml-child\r
69     const char * defaultval = NULL;\r
70     if (sp_repr_children(sp_repr_children(xml)) != NULL)\r
71         defaultval = sp_repr_children(sp_repr_children(xml))->content();\r
72     \r
73     gchar * pref_name = this->pref_name();\r
74     const gchar * paramval = prefs_get_string_attribute(PREF_DIR, pref_name);\r
75     g_free(pref_name);\r
76 \r
77     if (paramval != NULL)\r
78         defaultval = paramval;\r
79     if (defaultval != NULL)\r
80         _value = g_strdup(defaultval);  // allocate space for _value\r
81         \r
82     return;\r
83 }\r
84 \r
85 ParamComboBox::~ParamComboBox (void)\r
86 {                  \r
87     //destroy choice strings\r
88     for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {\r
89         Glib::ustring * text = reinterpret_cast<Glib::ustring *>(list->data);\r
90         delete text;\r
91     }\r
92     g_slist_free(choices);\r
93 \r
94     g_free(_value);\r
95 }\r
96 \r
97 \r
98 /** \brief  A function to set the \c _value\r
99     \param  in   The value to set\r
100     \param  doc  A document that should be used to set the value.\r
101     \param  node The node where the value may be placed\r
102 \r
103     This function sets ONLY the internal value, but it also sets the value\r
104     in the preferences structure.  To put it in the right place, \c PREF_DIR\r
105     and \c pref_name() are used.\r
106 \r
107     To copy the data into _value the old memory must be free'd first.\r
108     It is important to note that \c g_free handles \c NULL just fine.  Then\r
109     the passed in value is duplicated using \c g_strdup().\r
110 */\r
111 const gchar *\r
112 ParamComboBox::set (const gchar * in, SPDocument * doc, Inkscape::XML::Node * node)\r
113 {\r
114     if (in == NULL) return NULL; /* Can't have NULL string */\r
115 \r
116     if (_value != NULL)\r
117         g_free(_value);\r
118     _value = g_strdup(in);\r
119 \r
120     gchar * prefname = this->pref_name();\r
121     prefs_set_string_attribute(PREF_DIR, prefname, _value);\r
122     g_free(prefname);\r
123 \r
124     return _value;\r
125 }\r
126 \r
127 \r
128 /**\r
129     \brief  A function to get the currentpage and the parameters in a string form\r
130     \return A string with the 'value' and all the parameters on all pages as command line arguments\r
131 */\r
132 Glib::ustring *\r
133 ParamComboBox::string (void)\r
134 {\r
135     Glib::ustring * param_string = new Glib::ustring("");\r
136 \r
137     *param_string += "\"";\r
138     *param_string += _value;\r
139     *param_string += "\"";\r
140 \r
141     return param_string;\r
142 }\r
143 \r
144 /** \brief  A special category of Gtk::Entry to handle string parameteres */\r
145 class ParamComboBoxEntry : public Gtk::ComboBoxText {\r
146 private:\r
147     ParamComboBox * _pref;\r
148     SPDocument * _doc;\r
149     Inkscape::XML::Node * _node;\r
150 public:\r
151     /** \brief  Build a string preference for the given parameter\r
152         \param  pref  Where to get the string from, and where to put it\r
153                       when it changes.\r
154     */\r
155     ParamComboBoxEntry (ParamComboBox * pref, SPDocument * doc, Inkscape::XML::Node * node) :\r
156         Gtk::ComboBoxText(), _pref(pref), _doc(doc), _node(node) {\r
157         this->signal_changed().connect(sigc::mem_fun(this, &ParamComboBoxEntry::changed));\r
158     };\r
159     void changed (void);\r
160 };\r
161 \r
162 /** \brief  Respond to the text box changing\r
163 \r
164     This function responds to the box changing by grabbing the value\r
165     from the text box and putting it in the parameter.\r
166 */\r
167 void\r
168 ParamComboBoxEntry::changed (void)\r
169 {\r
170     Glib::ustring data = this->get_active_text();\r
171     _pref->set(data.c_str(), _doc, _node);\r
172     return;\r
173 }\r
174 \r
175 \r
176 \r
177 /**\r
178     \brief  Creates a combobox widget for an enumeration parameter\r
179 */\r
180 Gtk::Widget *\r
181 ParamComboBox::get_widget (SPDocument * doc, Inkscape::XML::Node * node)\r
182 {\r
183     Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox(false, 4));\r
184 \r
185     Gtk::Label * label = Gtk::manage(new Gtk::Label(_(_text), Gtk::ALIGN_LEFT));\r
186     label->show();\r
187     hbox->pack_start(*label, false, false);\r
188 \r
189     ParamComboBoxEntry * combo = Gtk::manage(new ParamComboBoxEntry(this, doc, node));\r
190     // add choice strings:         \r
191     for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {\r
192         Glib::ustring * text = reinterpret_cast<Glib::ustring *>(list->data);\r
193         combo->append_text(*text);\r
194     }\r
195     combo->set_active_text(Glib::ustring(_value));\r
196 \r
197     combo->show();\r
198     hbox->pack_start(*combo, true, true);\r
199 \r
200     hbox->show();\r
201 \r
202     return dynamic_cast<Gtk::Widget *>(hbox);\r
203 }\r
204 \r
205 \r
206 }  /* namespace Extension */\r
207 }  /* namespace Inkscape */\r
208 \r
209 /*\r
210   Local Variables:\r
211   mode:c++\r
212   c-file-style:"stroustrup"\r
213   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))\r
214   indent-tabs-mode:nil\r
215   fill-column:99\r
216   End:\r
217 */\r
218 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :\r