1 /**
2 * Implementation of the file dialog interfaces defined in filedialogimpl.h
3 *
4 * Authors:
5 * Bob Jamison
6 * Joel Holdsworth
7 * Bruno Dilly
8 * Other dudes from The Inkscape Organization
9 *
10 * Copyright (C) 2004-2007 Bob Jamison
11 * Copyright (C) 2006 Johan Engelen <johan@shouraizou.nl>
12 * Copyright (C) 2007-2008 Joel Holdsworth
13 * Copyright (C) 2004-2007 The Inkscape Organization
14 *
15 * Released under GNU GPL, read the file 'COPYING' for more information
16 */
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
22 #include "filedialogimpl-gtkmm.h"
23 #include "dialogs/dialog-events.h"
24 #include "interface.h"
25 #include "io/sys.h"
26 #include "path-prefix.h"
28 #ifdef WITH_GNOME_VFS
29 # include <libgnomevfs/gnome-vfs.h>
30 #endif
32 //Routines from file.cpp
33 #undef INK_DUMP_FILENAME_CONV
35 #ifdef INK_DUMP_FILENAME_CONV
36 void dump_str( const gchar* str, const gchar* prefix );
37 void dump_ustr( const Glib::ustring& ustr );
38 #endif
42 namespace Inkscape
43 {
44 namespace UI
45 {
46 namespace Dialog
47 {
53 //########################################################################
54 //### U T I L I T Y
55 //########################################################################
57 void
58 fileDialogExtensionToPattern(Glib::ustring &pattern,
59 Glib::ustring &extension)
60 {
61 for (unsigned int i = 0; i < extension.length(); i++ )
62 {
63 Glib::ustring::value_type ch = extension[i];
64 if ( Glib::Unicode::isalpha(ch) )
65 {
66 pattern += '[';
67 pattern += Glib::Unicode::toupper(ch);
68 pattern += Glib::Unicode::tolower(ch);
69 pattern += ']';
70 }
71 else
72 {
73 pattern += ch;
74 }
75 }
76 }
79 void
80 findEntryWidgets(Gtk::Container *parent,
81 std::vector<Gtk::Entry *> &result)
82 {
83 if (!parent)
84 return;
85 std::vector<Gtk::Widget *> children = parent->get_children();
86 for (unsigned int i=0; i<children.size() ; i++)
87 {
88 Gtk::Widget *child = children[i];
89 GtkWidget *wid = child->gobj();
90 if (GTK_IS_ENTRY(wid))
91 result.push_back((Gtk::Entry *)child);
92 else if (GTK_IS_CONTAINER(wid))
93 findEntryWidgets((Gtk::Container *)child, result);
94 }
96 }
98 void
99 findExpanderWidgets(Gtk::Container *parent,
100 std::vector<Gtk::Expander *> &result)
101 {
102 if (!parent)
103 return;
104 std::vector<Gtk::Widget *> children = parent->get_children();
105 for (unsigned int i=0; i<children.size() ; i++)
106 {
107 Gtk::Widget *child = children[i];
108 GtkWidget *wid = child->gobj();
109 if (GTK_IS_EXPANDER(wid))
110 result.push_back((Gtk::Expander *)child);
111 else if (GTK_IS_CONTAINER(wid))
112 findExpanderWidgets((Gtk::Container *)child, result);
113 }
115 }
118 /*#########################################################################
119 ### SVG Preview Widget
120 #########################################################################*/
122 bool SVGPreview::setDocument(SPDocument *doc)
123 {
124 if (document)
125 sp_document_unref(document);
127 sp_document_ref(doc);
128 document = doc;
130 //This should remove it from the box, and free resources
131 if (viewerGtk)
132 gtk_widget_destroy(viewerGtk);
134 viewerGtk = sp_svg_view_widget_new(doc);
135 GtkWidget *vbox = (GtkWidget *)gobj();
136 gtk_box_pack_start(GTK_BOX(vbox), viewerGtk, TRUE, TRUE, 0);
137 gtk_widget_show(viewerGtk);
139 return true;
140 }
143 bool SVGPreview::setFileName(Glib::ustring &theFileName)
144 {
145 Glib::ustring fileName = theFileName;
147 fileName = Glib::filename_to_utf8(fileName);
149 /**
150 * I don't know why passing false to keepalive is bad. But it
151 * prevents the display of an svg with a non-ascii filename
152 */
153 SPDocument *doc = sp_document_new (fileName.c_str(), true);
154 if (!doc) {
155 g_warning("SVGView: error loading document '%s'\n", fileName.c_str());
156 return false;
157 }
159 setDocument(doc);
161 sp_document_unref(doc);
163 return true;
164 }
168 bool SVGPreview::setFromMem(char const *xmlBuffer)
169 {
170 if (!xmlBuffer)
171 return false;
173 gint len = (gint)strlen(xmlBuffer);
174 SPDocument *doc = sp_document_new_from_mem(xmlBuffer, len, 0);
175 if (!doc) {
176 g_warning("SVGView: error loading buffer '%s'\n",xmlBuffer);
177 return false;
178 }
180 setDocument(doc);
182 sp_document_unref(doc);
184 Inkscape::GC::request_early_collection();
186 return true;
187 }
191 void SVGPreview::showImage(Glib::ustring &theFileName)
192 {
193 Glib::ustring fileName = theFileName;
196 /*#####################################
197 # LET'S HAVE SOME FUN WITH SVG!
198 # Instead of just loading an image, why
199 # don't we make a lovely little svg and
200 # display it nicely?
201 #####################################*/
203 //Arbitrary size of svg doc -- rather 'portrait' shaped
204 gint previewWidth = 400;
205 gint previewHeight = 600;
207 //Get some image info. Smart pointer does not need to be deleted
208 Glib::RefPtr<Gdk::Pixbuf> img(NULL);
209 try {
210 img = Gdk::Pixbuf::create_from_file(fileName);
211 }
212 catch (const Glib::FileError & e)
213 {
214 g_message("caught Glib::FileError in SVGPreview::showImage");
215 return;
216 }
217 catch (const Gdk::PixbufError & e)
218 {
219 g_message("Gdk::PixbufError in SVGPreview::showImage");
220 return;
221 }
222 catch (...)
223 {
224 g_message("Caught ... in SVGPreview::showImage");
225 return;
226 }
228 gint imgWidth = img->get_width();
229 gint imgHeight = img->get_height();
231 //Find the minimum scale to fit the image inside the preview area
232 double scaleFactorX = (0.9 *(double)previewWidth) / ((double)imgWidth);
233 double scaleFactorY = (0.9 *(double)previewHeight) / ((double)imgHeight);
234 double scaleFactor = scaleFactorX;
235 if (scaleFactorX > scaleFactorY)
236 scaleFactor = scaleFactorY;
238 //Now get the resized values
239 gint scaledImgWidth = (int) (scaleFactor * (double)imgWidth);
240 gint scaledImgHeight = (int) (scaleFactor * (double)imgHeight);
242 //center the image on the area
243 gint imgX = (previewWidth - scaledImgWidth) / 2;
244 gint imgY = (previewHeight - scaledImgHeight) / 2;
246 //wrap a rectangle around the image
247 gint rectX = imgX-1;
248 gint rectY = imgY-1;
249 gint rectWidth = scaledImgWidth +2;
250 gint rectHeight = scaledImgHeight+2;
252 //Our template. Modify to taste
253 gchar const *xformat =
254 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
255 "<svg\n"
256 "xmlns=\"http://www.w3.org/2000/svg\"\n"
257 "xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
258 "width=\"%d\" height=\"%d\">\n" //# VALUES HERE
259 "<rect\n"
260 " style=\"fill:#eeeeee;stroke:none\"\n"
261 " x=\"-100\" y=\"-100\" width=\"4000\" height=\"4000\"/>\n"
262 "<image x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\"\n"
263 "xlink:href=\"%s\"/>\n"
264 "<rect\n"
265 " style=\"fill:none;"
266 " stroke:#000000;stroke-width:1.0;"
267 " stroke-linejoin:miter;stroke-opacity:1.0000000;"
268 " stroke-miterlimit:4.0000000;stroke-dasharray:none\"\n"
269 " x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\"/>\n"
270 "<text\n"
271 " style=\"font-size:24.000000;font-style:normal;font-weight:normal;"
272 " fill:#000000;fill-opacity:1.0000000;stroke:none;"
273 " font-family:Bitstream Vera Sans\"\n"
274 " x=\"10\" y=\"26\">%d x %d</text>\n" //# VALUES HERE
275 "</svg>\n\n";
277 //if (!Glib::get_charset()) //If we are not utf8
278 fileName = Glib::filename_to_utf8(fileName);
280 //Fill in the template
281 /* FIXME: Do proper XML quoting for fileName. */
282 gchar *xmlBuffer = g_strdup_printf(xformat,
283 previewWidth, previewHeight,
284 imgX, imgY, scaledImgWidth, scaledImgHeight,
285 fileName.c_str(),
286 rectX, rectY, rectWidth, rectHeight,
287 imgWidth, imgHeight);
289 //g_message("%s\n", xmlBuffer);
291 //now show it!
292 setFromMem(xmlBuffer);
293 g_free(xmlBuffer);
294 }
298 void SVGPreview::showNoPreview()
299 {
300 //Are we already showing it?
301 if (showingNoPreview)
302 return;
304 //Arbitrary size of svg doc -- rather 'portrait' shaped
305 gint previewWidth = 300;
306 gint previewHeight = 600;
308 //Our template. Modify to taste
309 gchar const *xformat =
310 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
311 "<svg\n"
312 "xmlns=\"http://www.w3.org/2000/svg\"\n"
313 "xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
314 "width=\"%d\" height=\"%d\">\n" //# VALUES HERE
315 "<g transform=\"translate(-190,24.27184)\" style=\"opacity:0.12\">\n"
316 "<path\n"
317 "style=\"font-size:12;fill:#ffffff;fill-rule:evenodd;stroke:#000000;stroke-width:0.936193pt\"\n"
318 "d=\"M 397.64309 320.25301 L 280.39197 282.517 L 250.74227 124.83447 L 345.08225 "
319 "29.146783 L 393.59996 46.667064 L 483.89679 135.61619 L 397.64309 320.25301 z \"\n"
320 "id=\"whiteSpace\" />\n"
321 "<path\n"
322 "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
323 "d=\"M 476.95792 339.17168 C 495.78197 342.93607 499.54842 356.11361 495.78197 359.87802 "
324 "C 492.01856 363.6434 482.6065 367.40781 475.07663 361.76014 C 467.54478 "
325 "356.11361 467.54478 342.93607 476.95792 339.17168 z \"\n"
326 "id=\"droplet01\" />\n"
327 "<path\n"
328 "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
329 "d=\"M 286.46194 340.42914 C 284.6277 340.91835 269.30405 327.71337 257.16909 333.8338 "
330 "C 245.03722 339.95336 236.89276 353.65666 248.22676 359.27982 C 259.56184 364.90298 "
331 "267.66433 358.41867 277.60113 351.44119 C 287.53903 344.46477 "
332 "287.18046 343.1206 286.46194 340.42914 z \"\n"
333 "id=\"droplet02\" />\n"
334 "<path\n"
335 "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
336 "d=\"M 510.35756 306.92856 C 520.59494 304.36879 544.24333 306.92856 540.47688 321.98634 "
337 "C 536.71354 337.04806 504.71297 331.39827 484.00371 323.87156 C 482.12141 "
338 "308.81083 505.53237 308.13423 510.35756 306.92856 z \"\n"
339 "id=\"droplet03\" />\n"
340 "<path\n"
341 "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
342 "d=\"M 359.2403 21.362537 C 347.92693 21.362537 336.6347 25.683095 327.96556 34.35223 "
343 "L 173.87387 188.41466 C 165.37697 196.9114 161.1116 207.95813 160.94269 219.04577 "
344 "L 160.88418 219.04577 C 160.88418 219.08524 160.94076 219.12322 160.94269 219.16279 "
345 "C 160.94033 219.34888 160.88418 219.53256 160.88418 219.71865 L 161.14748 219.71865 "
346 "C 164.0966 230.93917 240.29699 245.24198 248.79866 253.74346 C 261.63771 266.58263 "
347 "199.5652 276.01151 212.4041 288.85074 C 225.24316 301.68979 289.99433 313.6933 302.8346 "
348 "326.53254 C 315.67368 339.37161 276.5961 353.04289 289.43532 365.88196 C 302.27439 "
349 "378.72118 345.40201 362.67257 337.5908 396.16198 C 354.92909 413.50026 391.10302 "
350 "405.2208 415.32417 387.88252 C 428.16323 375.04345 390.6948 376.17577 403.53397 "
351 "363.33668 C 416.37304 350.49745 448.78128 350.4282 476.08902 319.71589 C 465.09739 "
352 "302.62116 429.10801 295.34136 441.94719 282.50217 C 454.78625 269.66311 479.74708 "
353 "276.18423 533.60644 251.72479 C 559.89837 239.78398 557.72636 230.71459 557.62567 "
354 "219.71865 C 557.62356 219.48727 557.62567 219.27892 557.62567 219.04577 L 557.56716 "
355 "219.04577 C 557.3983 207.95812 553.10345 196.9114 544.60673 188.41466 L 390.54428 "
356 "34.35223 C 381.87515 25.683095 370.55366 21.362537 359.2403 21.362537 z M 357.92378 "
357 "41.402939 C 362.95327 41.533963 367.01541 45.368018 374.98006 50.530832 L 447.76915 "
358 "104.50827 C 448.56596 105.02498 449.32484 105.564 450.02187 106.11735 C 450.7189 106.67062 "
359 "451.3556 107.25745 451.95277 107.84347 C 452.54997 108.42842 453.09281 109.01553 453.59111 "
360 "109.62808 C 454.08837 110.24052 454.53956 110.86661 454.93688 111.50048 C 455.33532 112.13538 "
361 "455.69164 112.78029 455.9901 113.43137 C 456.28877 114.08363 456.52291 114.75639 456.7215 "
362 "115.42078 C 456.92126 116.08419 457.08982 116.73973 457.18961 117.41019 C 457.28949 "
363 "118.08184 457.33588 118.75535 457.33588 119.42886 L 414.21245 98.598549 L 409.9118 "
364 "131.16055 L 386.18512 120.04324 L 349.55654 144.50131 L 335.54288 96.1703 L 317.4919 "
365 "138.4453 L 267.08369 143.47735 L 267.63956 121.03795 C 267.63956 115.64823 296.69685 "
366 "77.915899 314.39075 68.932902 L 346.77721 45.674327 C 351.55594 42.576634 354.90608 "
367 "41.324327 357.92378 41.402939 z M 290.92738 261.61333 C 313.87149 267.56365 339.40299 "
368 "275.37038 359.88393 275.50997 L 360.76161 284.72563 C 343.2235 282.91785 306.11346 "
369 "274.45012 297.36372 269.98057 L 290.92738 261.61333 z \"\n"
370 "id=\"mountainDroplet\" />\n"
371 "</g> <g transform=\"translate(-20,0)\">\n"
372 "<text xml:space=\"preserve\"\n"
373 "style=\"font-size:32.000000;font-style:normal;font-variant:normal;font-weight:bold;"
374 "font-stretch:normal;fill:#000000;fill-opacity:1.0000000;stroke:none;stroke-width:1.0000000pt;"
375 "stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1.0000000;"
376 "font-family:Bitstream Vera Sans;text-anchor:middle;writing-mode:lr\"\n"
377 "x=\"190\" y=\"240\">%s</text></g>\n" //# VALUE HERE
378 "</svg>\n\n";
380 //Fill in the template
381 gchar *xmlBuffer = g_strdup_printf(xformat,
382 previewWidth, previewHeight, _("No preview"));
384 //g_message("%s\n", xmlBuffer);
386 //now show it!
387 setFromMem(xmlBuffer);
388 g_free(xmlBuffer);
389 showingNoPreview = true;
391 }
394 /**
395 * Inform the user that the svg file is too large to be displayed.
396 * This does not check for sizes of embedded images (yet)
397 */
398 void SVGPreview::showTooLarge(long fileLength)
399 {
401 //Arbitrary size of svg doc -- rather 'portrait' shaped
402 gint previewWidth = 300;
403 gint previewHeight = 600;
405 //Our template. Modify to taste
406 gchar const *xformat =
407 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
408 "<svg\n"
409 "xmlns=\"http://www.w3.org/2000/svg\"\n"
410 "xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
411 "width=\"%d\" height=\"%d\">\n" //# VALUES HERE
412 "<g transform=\"translate(-170,24.27184)\" style=\"opacity:0.12\">\n"
413 "<path\n"
414 "style=\"font-size:12;fill:#ffffff;fill-rule:evenodd;stroke:#000000;stroke-width:0.936193pt\"\n"
415 "d=\"M 397.64309 320.25301 L 280.39197 282.517 L 250.74227 124.83447 L 345.08225 "
416 "29.146783 L 393.59996 46.667064 L 483.89679 135.61619 L 397.64309 320.25301 z \"\n"
417 "id=\"whiteSpace\" />\n"
418 "<path\n"
419 "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
420 "d=\"M 476.95792 339.17168 C 495.78197 342.93607 499.54842 356.11361 495.78197 359.87802 "
421 "C 492.01856 363.6434 482.6065 367.40781 475.07663 361.76014 C 467.54478 "
422 "356.11361 467.54478 342.93607 476.95792 339.17168 z \"\n"
423 "id=\"droplet01\" />\n"
424 "<path\n"
425 "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
426 "d=\"M 286.46194 340.42914 C 284.6277 340.91835 269.30405 327.71337 257.16909 333.8338 "
427 "C 245.03722 339.95336 236.89276 353.65666 248.22676 359.27982 C 259.56184 364.90298 "
428 "267.66433 358.41867 277.60113 351.44119 C 287.53903 344.46477 "
429 "287.18046 343.1206 286.46194 340.42914 z \"\n"
430 "id=\"droplet02\" />\n"
431 "<path\n"
432 "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
433 "d=\"M 510.35756 306.92856 C 520.59494 304.36879 544.24333 306.92856 540.47688 321.98634 "
434 "C 536.71354 337.04806 504.71297 331.39827 484.00371 323.87156 C 482.12141 "
435 "308.81083 505.53237 308.13423 510.35756 306.92856 z \"\n"
436 "id=\"droplet03\" />\n"
437 "<path\n"
438 "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
439 "d=\"M 359.2403 21.362537 C 347.92693 21.362537 336.6347 25.683095 327.96556 34.35223 "
440 "L 173.87387 188.41466 C 165.37697 196.9114 161.1116 207.95813 160.94269 219.04577 "
441 "L 160.88418 219.04577 C 160.88418 219.08524 160.94076 219.12322 160.94269 219.16279 "
442 "C 160.94033 219.34888 160.88418 219.53256 160.88418 219.71865 L 161.14748 219.71865 "
443 "C 164.0966 230.93917 240.29699 245.24198 248.79866 253.74346 C 261.63771 266.58263 "
444 "199.5652 276.01151 212.4041 288.85074 C 225.24316 301.68979 289.99433 313.6933 302.8346 "
445 "326.53254 C 315.67368 339.37161 276.5961 353.04289 289.43532 365.88196 C 302.27439 "
446 "378.72118 345.40201 362.67257 337.5908 396.16198 C 354.92909 413.50026 391.10302 "
447 "405.2208 415.32417 387.88252 C 428.16323 375.04345 390.6948 376.17577 403.53397 "
448 "363.33668 C 416.37304 350.49745 448.78128 350.4282 476.08902 319.71589 C 465.09739 "
449 "302.62116 429.10801 295.34136 441.94719 282.50217 C 454.78625 269.66311 479.74708 "
450 "276.18423 533.60644 251.72479 C 559.89837 239.78398 557.72636 230.71459 557.62567 "
451 "219.71865 C 557.62356 219.48727 557.62567 219.27892 557.62567 219.04577 L 557.56716 "
452 "219.04577 C 557.3983 207.95812 553.10345 196.9114 544.60673 188.41466 L 390.54428 "
453 "34.35223 C 381.87515 25.683095 370.55366 21.362537 359.2403 21.362537 z M 357.92378 "
454 "41.402939 C 362.95327 41.533963 367.01541 45.368018 374.98006 50.530832 L 447.76915 "
455 "104.50827 C 448.56596 105.02498 449.32484 105.564 450.02187 106.11735 C 450.7189 106.67062 "
456 "451.3556 107.25745 451.95277 107.84347 C 452.54997 108.42842 453.09281 109.01553 453.59111 "
457 "109.62808 C 454.08837 110.24052 454.53956 110.86661 454.93688 111.50048 C 455.33532 112.13538 "
458 "455.69164 112.78029 455.9901 113.43137 C 456.28877 114.08363 456.52291 114.75639 456.7215 "
459 "115.42078 C 456.92126 116.08419 457.08982 116.73973 457.18961 117.41019 C 457.28949 "
460 "118.08184 457.33588 118.75535 457.33588 119.42886 L 414.21245 98.598549 L 409.9118 "
461 "131.16055 L 386.18512 120.04324 L 349.55654 144.50131 L 335.54288 96.1703 L 317.4919 "
462 "138.4453 L 267.08369 143.47735 L 267.63956 121.03795 C 267.63956 115.64823 296.69685 "
463 "77.915899 314.39075 68.932902 L 346.77721 45.674327 C 351.55594 42.576634 354.90608 "
464 "41.324327 357.92378 41.402939 z M 290.92738 261.61333 C 313.87149 267.56365 339.40299 "
465 "275.37038 359.88393 275.50997 L 360.76161 284.72563 C 343.2235 282.91785 306.11346 "
466 "274.45012 297.36372 269.98057 L 290.92738 261.61333 z \"\n"
467 "id=\"mountainDroplet\" />\n"
468 "</g>\n"
469 "<text xml:space=\"preserve\"\n"
470 "style=\"font-size:32.000000;font-style:normal;font-variant:normal;font-weight:bold;"
471 "font-stretch:normal;fill:#000000;fill-opacity:1.0000000;stroke:none;stroke-width:1.0000000pt;"
472 "stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1.0000000;"
473 "font-family:Bitstream Vera Sans;text-anchor:middle;writing-mode:lr\"\n"
474 "x=\"170\" y=\"215\">%5.1f MB</text>\n" //# VALUE HERE
475 "<text xml:space=\"preserve\"\n"
476 "style=\"font-size:24.000000;font-style:normal;font-variant:normal;font-weight:bold;"
477 "font-stretch:normal;fill:#000000;fill-opacity:1.0000000;stroke:none;stroke-width:1.0000000pt;"
478 "stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1.0000000;"
479 "font-family:Bitstream Vera Sans;text-anchor:middle;writing-mode:lr\"\n"
480 "x=\"180\" y=\"245\">%s</text>\n" //# VALUE HERE
481 "</svg>\n\n";
483 //Fill in the template
484 double floatFileLength = ((double)fileLength) / 1048576.0;
485 //printf("%ld %f\n", fileLength, floatFileLength);
486 gchar *xmlBuffer = g_strdup_printf(xformat,
487 previewWidth, previewHeight, floatFileLength,
488 _("too large for preview"));
490 //g_message("%s\n", xmlBuffer);
492 //now show it!
493 setFromMem(xmlBuffer);
494 g_free(xmlBuffer);
496 }
498 bool SVGPreview::set(Glib::ustring &fileName, int dialogType)
499 {
501 if (!Glib::file_test(fileName, Glib::FILE_TEST_EXISTS))
502 return false;
504 //g_message("fname:%s", fileName.c_str());
506 if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
507 showNoPreview();
508 return false;
509 }
511 if (Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR))
512 {
513 Glib::ustring fileNameUtf8 = Glib::filename_to_utf8(fileName);
514 gchar *fName = (gchar *)fileNameUtf8.c_str();
515 struct stat info;
516 if (g_stat(fName, &info))
517 {
518 g_warning("SVGPreview::set() : %s : %s",
519 fName, strerror(errno));
520 return FALSE;
521 }
522 long fileLen = info.st_size;
523 if (fileLen > 0x150000L)
524 {
525 showingNoPreview = false;
526 showTooLarge(fileLen);
527 return FALSE;
528 }
529 }
531 Glib::ustring svg = ".svg";
532 Glib::ustring svgz = ".svgz";
534 if ((dialogType == SVG_TYPES || dialogType == IMPORT_TYPES) &&
535 (hasSuffix(fileName, svg) || hasSuffix(fileName, svgz) )
536 ) {
537 bool retval = setFileName(fileName);
538 showingNoPreview = false;
539 return retval;
540 } else if (isValidImageFile(fileName)) {
541 showImage(fileName);
542 showingNoPreview = false;
543 return true;
544 } else {
545 showNoPreview();
546 return false;
547 }
548 }
551 SVGPreview::SVGPreview()
552 {
553 if (!INKSCAPE)
554 inkscape_application_init("",false);
555 document = NULL;
556 viewerGtk = NULL;
557 set_size_request(150,150);
558 showingNoPreview = false;
559 }
561 SVGPreview::~SVGPreview()
562 {
564 }
567 /*#########################################################################
568 ### F I L E D I A L O G B A S E C L A S S
569 #########################################################################*/
571 void FileDialogBaseGtk::internalSetup()
572 {
573 bool enablePreview =
574 (bool)prefs_get_int_attribute( preferenceBase.c_str(),
575 "enable_preview", 1 );
577 previewCheckbox.set_label( Glib::ustring(_("Enable preview")) );
578 previewCheckbox.set_active( enablePreview );
580 previewCheckbox.signal_toggled().connect(
581 sigc::mem_fun(*this, &FileDialogBaseGtk::_previewEnabledCB) );
583 //Catch selection-changed events, so we can adjust the text widget
584 signal_update_preview().connect(
585 sigc::mem_fun(*this, &FileDialogBaseGtk::_updatePreviewCallback) );
587 //###### Add a preview widget
588 set_preview_widget(svgPreview);
589 set_preview_widget_active( enablePreview );
590 set_use_preview_label (false);
592 }
595 void FileDialogBaseGtk::cleanup( bool showConfirmed )
596 {
597 if ( showConfirmed )
598 prefs_set_int_attribute( preferenceBase.c_str(),
599 "enable_preview", previewCheckbox.get_active() );
600 }
603 void FileDialogBaseGtk::_previewEnabledCB()
604 {
605 bool enabled = previewCheckbox.get_active();
606 set_preview_widget_active(enabled);
607 if ( enabled ) {
608 _updatePreviewCallback();
609 }
610 }
614 /**
615 * Callback for checking if the preview needs to be redrawn
616 */
617 void FileDialogBaseGtk::_updatePreviewCallback()
618 {
619 Glib::ustring fileName = get_preview_filename();
621 #ifdef WITH_GNOME_VFS
622 if ( fileName.empty() && gnome_vfs_initialized() ) {
623 fileName = get_preview_uri();
624 }
625 #endif
627 if (fileName.empty()) {
628 return;
629 }
631 svgPreview.set(fileName, _dialogType);
632 }
635 /*#########################################################################
636 ### F I L E O P E N
637 #########################################################################*/
639 /**
640 * Constructor. Not called directly. Use the factory.
641 */
642 FileOpenDialogImplGtk::FileOpenDialogImplGtk(Gtk::Window& parentWindow,
643 const Glib::ustring &dir,
644 FileDialogType fileTypes,
645 const Glib::ustring &title) :
646 FileDialogBaseGtk(parentWindow, title, Gtk::FILE_CHOOSER_ACTION_OPEN, fileTypes, "dialogs.open")
647 {
650 /* One file at a time */
651 /* And also Multiple Files */
652 set_select_multiple(true);
654 #ifdef WITH_GNOME_VFS
655 if (gnome_vfs_initialized()) {
656 set_local_only(false);
657 }
658 #endif
660 /* Initalize to Autodetect */
661 extension = NULL;
662 /* No filename to start out with */
663 myFilename = "";
665 /* Set our dialog type (open, import, etc...)*/
666 _dialogType = fileTypes;
669 /* Set the pwd and/or the filename */
670 if (dir.size() > 0)
671 {
672 Glib::ustring udir(dir);
673 Glib::ustring::size_type len = udir.length();
674 // leaving a trailing backslash on the directory name leads to the infamous
675 // double-directory bug on win32
676 if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
677 set_current_folder(udir.c_str());
678 }
681 set_extra_widget( previewCheckbox );
684 //###### Add the file types menu
685 createFilterMenu();
687 add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
688 set_default(*add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK));
690 //###### Allow easy access to our examples folder
691 if ( Inkscape::IO::file_test(INKSCAPE_EXAMPLESDIR, G_FILE_TEST_EXISTS)
692 && Inkscape::IO::file_test(INKSCAPE_EXAMPLESDIR, G_FILE_TEST_IS_DIR)
693 && g_path_is_absolute(INKSCAPE_EXAMPLESDIR)
694 )
695 {
696 add_shortcut_folder(INKSCAPE_EXAMPLESDIR);
697 }
698 }
700 /**
701 * Destructor
702 */
703 FileOpenDialogImplGtk::~FileOpenDialogImplGtk()
704 {
706 }
708 void FileOpenDialogImplGtk::createFilterMenu()
709 {
710 Gtk::FileFilter allInkscapeFilter;
711 allInkscapeFilter.set_name(_("All Inkscape Files"));
712 extensionMap[Glib::ustring(_("All Inkscape Files"))]=NULL;
713 add_filter(allInkscapeFilter);
715 Gtk::FileFilter allFilter;
716 allFilter.set_name(_("All Files"));
717 extensionMap[Glib::ustring(_("All Files"))]=NULL;
718 allFilter.add_pattern("*");
719 add_filter(allFilter);
721 Gtk::FileFilter allImageFilter;
722 allImageFilter.set_name(_("All Images"));
723 extensionMap[Glib::ustring(_("All Images"))]=NULL;
724 add_filter(allImageFilter);
726 //patterns added dynamically below
727 Inkscape::Extension::DB::InputList extension_list;
728 Inkscape::Extension::db.get_input_list(extension_list);
730 for (Inkscape::Extension::DB::InputList::iterator current_item = extension_list.begin();
731 current_item != extension_list.end(); current_item++)
732 {
733 Inkscape::Extension::Input * imod = *current_item;
735 // FIXME: would be nice to grey them out instead of not listing them
736 if (imod->deactivated()) continue;
738 Glib::ustring upattern("*");
739 Glib::ustring extension = imod->get_extension();
740 fileDialogExtensionToPattern(upattern, extension);
742 Gtk::FileFilter filter;
743 Glib::ustring uname(_(imod->get_filetypename()));
744 filter.set_name(uname);
745 filter.add_pattern(upattern);
746 add_filter(filter);
747 extensionMap[uname] = imod;
749 //g_message("ext %s:%s '%s'\n", ioext->name, ioext->mimetype, upattern.c_str());
750 allInkscapeFilter.add_pattern(upattern);
751 if ( strncmp("image", imod->get_mimetype(), 5)==0 )
752 allImageFilter.add_pattern(upattern);
753 }
755 return;
756 }
758 /**
759 * Show this dialog modally. Return true if user hits [OK]
760 */
761 bool
762 FileOpenDialogImplGtk::show()
763 {
764 Glib::ustring s = Glib::filename_to_utf8 (get_current_folder());
765 if (s.length() == 0)
766 s = getcwd (NULL, 0);
767 set_current_folder(Glib::filename_from_utf8(s)); //hack to force initial dir listing
768 set_modal (TRUE); //Window
769 sp_transientize((GtkWidget *)gobj()); //Make transient
770 gint b = run(); //Dialog
771 svgPreview.showNoPreview();
772 hide();
774 if (b == Gtk::RESPONSE_OK)
775 {
776 //This is a hack, to avoid the warning messages that
777 //Gtk::FileChooser::get_filter() returns
778 //should be: Gtk::FileFilter *filter = get_filter();
779 GtkFileChooser *gtkFileChooser = Gtk::FileChooser::gobj();
780 GtkFileFilter *filter = gtk_file_chooser_get_filter(gtkFileChooser);
781 if (filter)
782 {
783 //Get which extension was chosen, if any
784 extension = extensionMap[gtk_file_filter_get_name(filter)];
785 }
786 myFilename = get_filename();
787 #ifdef WITH_GNOME_VFS
788 if (myFilename.empty() && gnome_vfs_initialized())
789 myFilename = get_uri();
790 #endif
791 cleanup( true );
792 return TRUE;
793 }
794 else
795 {
796 cleanup( false );
797 return FALSE;
798 }
799 }
804 /**
805 * Get the file extension type that was selected by the user. Valid after an [OK]
806 */
807 Inkscape::Extension::Extension *
808 FileOpenDialogImplGtk::getSelectionType()
809 {
810 return extension;
811 }
814 /**
815 * Get the file name chosen by the user. Valid after an [OK]
816 */
817 Glib::ustring
818 FileOpenDialogImplGtk::getFilename (void)
819 {
820 return myFilename;
821 }
824 /**
825 * To Get Multiple filenames selected at-once.
826 */
827 std::vector<Glib::ustring>FileOpenDialogImplGtk::getFilenames()
828 {
829 std::vector<Glib::ustring> result = get_filenames();
830 #ifdef WITH_GNOME_VFS
831 if (result.empty() && gnome_vfs_initialized())
832 result = get_uris();
833 #endif
834 return result;
835 }
837 Glib::ustring FileOpenDialogImplGtk::getCurrentDirectory()
838 {
839 return get_current_folder();
840 }
845 //########################################################################
846 //# F I L E S A V E
847 //########################################################################
849 /**
850 * Constructor
851 */
852 FileSaveDialogImplGtk::FileSaveDialogImplGtk( Gtk::Window &parentWindow,
853 const Glib::ustring &dir,
854 FileDialogType fileTypes,
855 const Glib::ustring &title,
856 const Glib::ustring &/*default_key*/ ) :
857 FileDialogBaseGtk(parentWindow, title, Gtk::FILE_CHOOSER_ACTION_SAVE, fileTypes, "dialogs.save_as")
858 {
859 /* One file at a time */
860 set_select_multiple(false);
862 #ifdef WITH_GNOME_VFS
863 if (gnome_vfs_initialized()) {
864 set_local_only(false);
865 }
866 #endif
868 /* Initalize to Autodetect */
869 extension = NULL;
870 /* No filename to start out with */
871 myFilename = "";
873 /* Set our dialog type (save, export, etc...)*/
874 _dialogType = fileTypes;
876 /* Set the pwd and/or the filename */
877 if (dir.size() > 0)
878 {
879 Glib::ustring udir(dir);
880 Glib::ustring::size_type len = udir.length();
881 // leaving a trailing backslash on the directory name leads to the infamous
882 // double-directory bug on win32
883 if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
884 myFilename = udir;
885 }
887 //###### Add the file types menu
888 //createFilterMenu();
890 //###### Do we want the .xxx extension automatically added?
891 fileTypeCheckbox.set_label(Glib::ustring(_("Append filename extension automatically")));
892 fileTypeCheckbox.set_active( (bool)prefs_get_int_attribute("dialogs.save_as",
893 "append_extension", 1) );
895 createFileTypeMenu();
896 fileTypeComboBox.set_size_request(200,40);
897 fileTypeComboBox.signal_changed().connect(
898 sigc::mem_fun(*this, &FileSaveDialogImplGtk::fileTypeChangedCallback) );
901 childBox.pack_start( checksBox );
902 childBox.pack_end( fileTypeComboBox );
903 checksBox.pack_start( fileTypeCheckbox );
904 checksBox.pack_start( previewCheckbox );
906 set_extra_widget( childBox );
908 //Let's do some customization
909 fileNameEntry = NULL;
910 Gtk::Container *cont = get_toplevel();
911 std::vector<Gtk::Entry *> entries;
912 findEntryWidgets(cont, entries);
913 //g_message("Found %d entry widgets\n", entries.size());
914 if (entries.size() >=1 )
915 {
916 //Catch when user hits [return] on the text field
917 fileNameEntry = entries[0];
918 fileNameEntry->signal_activate().connect(
919 sigc::mem_fun(*this, &FileSaveDialogImplGtk::fileNameEntryChangedCallback) );
920 }
922 //Let's do more customization
923 std::vector<Gtk::Expander *> expanders;
924 findExpanderWidgets(cont, expanders);
925 //g_message("Found %d expander widgets\n", expanders.size());
926 if (expanders.size() >=1 )
927 {
928 //Always show the file list
929 Gtk::Expander *expander = expanders[0];
930 expander->set_expanded(true);
931 }
933 // allow easy access to the user's own templates folder
934 gchar *templates = profile_path ("templates");
935 if ( Inkscape::IO::file_test(templates, G_FILE_TEST_EXISTS)
936 && Inkscape::IO::file_test(templates, G_FILE_TEST_IS_DIR)
937 && g_path_is_absolute(templates)
938 )
939 {
940 add_shortcut_folder(templates);
941 }
942 g_free (templates);
945 //if (extension == NULL)
946 // checkbox.set_sensitive(FALSE);
948 add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
949 set_default(*add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK));
951 show_all_children();
952 }
954 /**
955 * Destructor
956 */
957 FileSaveDialogImplGtk::~FileSaveDialogImplGtk()
958 {
959 }
961 /**
962 * Callback for fileNameEntry widget
963 */
964 void FileSaveDialogImplGtk::fileNameEntryChangedCallback()
965 {
966 if (!fileNameEntry)
967 return;
969 Glib::ustring fileName = fileNameEntry->get_text();
970 if (!Glib::get_charset()) //If we are not utf8
971 fileName = Glib::filename_to_utf8(fileName);
973 //g_message("User hit return. Text is '%s'\n", fileName.c_str());
975 if (!Glib::path_is_absolute(fileName)) {
976 //try appending to the current path
977 // not this way: fileName = get_current_folder() + "/" + fileName;
978 std::vector<Glib::ustring> pathSegments;
979 pathSegments.push_back( get_current_folder() );
980 pathSegments.push_back( fileName );
981 fileName = Glib::build_filename(pathSegments);
982 }
984 //g_message("path:'%s'\n", fileName.c_str());
986 if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
987 set_current_folder(fileName);
988 } else if (/*Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR)*/1) {
989 //dialog with either (1) select a regular file or (2) cd to dir
990 //simulate an 'OK'
991 set_filename(fileName);
992 response(Gtk::RESPONSE_OK);
993 }
994 }
998 /**
999 * Callback for fileNameEntry widget
1000 */
1001 void FileSaveDialogImplGtk::fileTypeChangedCallback()
1002 {
1003 int sel = fileTypeComboBox.get_active_row_number();
1004 if (sel<0 || sel >= (int)fileTypes.size())
1005 return;
1006 FileType type = fileTypes[sel];
1007 //g_message("selected: %s\n", type.name.c_str());
1009 extension = type.extension;
1010 Gtk::FileFilter filter;
1011 filter.add_pattern(type.pattern);
1012 set_filter(filter);
1014 updateNameAndExtension();
1015 }
1019 void FileSaveDialogImplGtk::createFileTypeMenu()
1020 {
1021 Inkscape::Extension::DB::OutputList extension_list;
1022 Inkscape::Extension::db.get_output_list(extension_list);
1023 knownExtensions.clear();
1025 for (Inkscape::Extension::DB::OutputList::iterator current_item = extension_list.begin();
1026 current_item != extension_list.end(); current_item++)
1027 {
1028 Inkscape::Extension::Output * omod = *current_item;
1030 // FIXME: would be nice to grey them out instead of not listing them
1031 if (omod->deactivated()) continue;
1033 FileType type;
1034 type.name = (_(omod->get_filetypename()));
1035 type.pattern = "*";
1036 Glib::ustring extension = omod->get_extension();
1037 knownExtensions.insert( extension.casefold() );
1038 fileDialogExtensionToPattern (type.pattern, extension);
1039 type.extension= omod;
1040 fileTypeComboBox.append_text(type.name);
1041 fileTypes.push_back(type);
1042 }
1044 //#Let user choose
1045 FileType guessType;
1046 guessType.name = _("Guess from extension");
1047 guessType.pattern = "*";
1048 guessType.extension = NULL;
1049 fileTypeComboBox.append_text(guessType.name);
1050 fileTypes.push_back(guessType);
1053 fileTypeComboBox.set_active(0);
1054 fileTypeChangedCallback(); //call at least once to set the filter
1055 }
1061 /**
1062 * Show this dialog modally. Return true if user hits [OK]
1063 */
1064 bool
1065 FileSaveDialogImplGtk::show()
1066 {
1067 change_path(myFilename);
1068 set_modal (TRUE); //Window
1069 sp_transientize((GtkWidget *)gobj()); //Make transient
1070 gint b = run(); //Dialog
1071 svgPreview.showNoPreview();
1072 set_preview_widget_active(false);
1073 hide();
1075 if (b == Gtk::RESPONSE_OK)
1076 {
1077 updateNameAndExtension();
1079 // Store changes of the "Append filename automatically" checkbox back to preferences.
1080 prefs_set_int_attribute("dialogs.save_as", "append_extension", fileTypeCheckbox.get_active());
1082 // Store the last used save-as filetype to preferences.
1083 prefs_set_string_attribute("dialogs.save_as", "default",
1084 ( extension != NULL ? extension->get_id() : "" ));
1086 cleanup( true );
1088 return TRUE;
1089 }
1090 else
1091 {
1092 cleanup( false );
1094 return FALSE;
1095 }
1096 }
1099 /**
1100 * Get the file extension type that was selected by the user. Valid after an [OK]
1101 */
1102 Inkscape::Extension::Extension *
1103 FileSaveDialogImplGtk::getSelectionType()
1104 {
1105 return extension;
1106 }
1108 void FileSaveDialogImplGtk::setSelectionType( Inkscape::Extension::Extension * key )
1109 {
1110 // If no pointer to extension is passed in, look up based on filename extension.
1111 if ( !key ) {
1112 // Not quite UTF-8 here.
1113 gchar *filenameLower = g_ascii_strdown(myFilename.c_str(), -1);
1114 for ( int i = 0; !key && (i < (int)fileTypes.size()); i++ ) {
1115 Inkscape::Extension::Output *ext = dynamic_cast<Inkscape::Extension::Output*>(fileTypes[i].extension);
1116 if ( ext && ext->get_extension() ) {
1117 gchar *extensionLower = g_ascii_strdown( ext->get_extension(), -1 );
1118 if ( g_str_has_suffix(filenameLower, extensionLower) ) {
1119 key = fileTypes[i].extension;
1120 }
1121 g_free(extensionLower);
1122 }
1123 }
1124 g_free(filenameLower);
1125 }
1127 // Ensure the proper entry in the combo box is selected.
1128 if ( key ) {
1129 extension = key;
1130 gchar const * extensionID = extension->get_id();
1131 if ( extensionID ) {
1132 for ( int i = 0; i < (int)fileTypes.size(); i++ ) {
1133 Inkscape::Extension::Extension *ext = fileTypes[i].extension;
1134 if ( ext ) {
1135 gchar const * id = ext->get_id();
1136 if ( id && ( strcmp(extensionID, id) == 0) ) {
1137 int oldSel = fileTypeComboBox.get_active_row_number();
1138 if ( i != oldSel ) {
1139 fileTypeComboBox.set_active(i);
1140 }
1141 break;
1142 }
1143 }
1144 }
1145 }
1146 }
1147 }
1149 Glib::ustring FileSaveDialogImplGtk::getCurrentDirectory()
1150 {
1151 return get_current_folder();
1152 }
1155 /*void
1156 FileSaveDialogImplGtk::change_title(const Glib::ustring& title)
1157 {
1158 set_title(title);
1159 }*/
1161 /**
1162 * Change the default save path location.
1163 */
1164 void
1165 FileSaveDialogImplGtk::change_path(const Glib::ustring& path)
1166 {
1167 myFilename = path;
1169 if (Glib::file_test(myFilename, Glib::FILE_TEST_IS_DIR)) {
1170 //fprintf(stderr,"set_current_folder(%s)\n",myFilename.c_str());
1171 set_current_folder(myFilename);
1172 } else {
1173 //fprintf(stderr,"set_filename(%s)\n",myFilename.c_str());
1174 if ( Glib::file_test( myFilename, Glib::FILE_TEST_EXISTS ) ) {
1175 set_filename(myFilename);
1176 } else {
1177 std::string dirName = Glib::path_get_dirname( myFilename );
1178 if ( dirName != get_current_folder() ) {
1179 set_current_folder(dirName);
1180 }
1181 }
1182 Glib::ustring basename = Glib::path_get_basename(myFilename);
1183 //fprintf(stderr,"set_current_name(%s)\n",basename.c_str());
1184 try {
1185 set_current_name( Glib::filename_to_utf8(basename) );
1186 } catch ( Glib::ConvertError& e ) {
1187 g_warning( "Error converting save filename to UTF-8." );
1188 // try a fallback.
1189 set_current_name( basename );
1190 }
1191 }
1192 }
1194 void FileSaveDialogImplGtk::updateNameAndExtension()
1195 {
1196 // Pick up any changes the user has typed in.
1197 Glib::ustring tmp = get_filename();
1198 #ifdef WITH_GNOME_VFS
1199 if ( tmp.empty() && gnome_vfs_initialized() ) {
1200 tmp = get_uri();
1201 }
1202 #endif
1203 if ( !tmp.empty() ) {
1204 myFilename = tmp;
1205 }
1207 Inkscape::Extension::Output* newOut = extension ? dynamic_cast<Inkscape::Extension::Output*>(extension) : 0;
1208 if ( fileTypeCheckbox.get_active() && newOut ) {
1209 // Append the file extension if it's not already present
1210 appendExtension(myFilename, newOut);
1211 }
1212 }
1215 //########################################################################
1216 //# F I L E E X P O R T
1217 //########################################################################
1219 /**
1220 * Callback for fileNameEntry widget
1221 */
1222 void FileExportDialogImpl::fileNameEntryChangedCallback()
1223 {
1224 if (!fileNameEntry)
1225 return;
1227 Glib::ustring fileName = fileNameEntry->get_text();
1228 if (!Glib::get_charset()) //If we are not utf8
1229 fileName = Glib::filename_to_utf8(fileName);
1231 //g_message("User hit return. Text is '%s'\n", fileName.c_str());
1233 if (!Glib::path_is_absolute(fileName)) {
1234 //try appending to the current path
1235 // not this way: fileName = get_current_folder() + "/" + fileName;
1236 std::vector<Glib::ustring> pathSegments;
1237 pathSegments.push_back( get_current_folder() );
1238 pathSegments.push_back( fileName );
1239 fileName = Glib::build_filename(pathSegments);
1240 }
1242 //g_message("path:'%s'\n", fileName.c_str());
1244 if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
1245 set_current_folder(fileName);
1246 } else if (/*Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR)*/1) {
1247 //dialog with either (1) select a regular file or (2) cd to dir
1248 //simulate an 'OK'
1249 set_filename(fileName);
1250 response(Gtk::RESPONSE_OK);
1251 }
1252 }
1256 /**
1257 * Callback for fileNameEntry widget
1258 */
1259 void FileExportDialogImpl::fileTypeChangedCallback()
1260 {
1261 int sel = fileTypeComboBox.get_active_row_number();
1262 if (sel<0 || sel >= (int)fileTypes.size())
1263 return;
1264 FileType type = fileTypes[sel];
1265 //g_message("selected: %s\n", type.name.c_str());
1266 Gtk::FileFilter filter;
1267 filter.add_pattern(type.pattern);
1268 set_filter(filter);
1269 }
1273 void FileExportDialogImpl::createFileTypeMenu()
1274 {
1275 Inkscape::Extension::DB::OutputList extension_list;
1276 Inkscape::Extension::db.get_output_list(extension_list);
1278 for (Inkscape::Extension::DB::OutputList::iterator current_item = extension_list.begin();
1279 current_item != extension_list.end(); current_item++)
1280 {
1281 Inkscape::Extension::Output * omod = *current_item;
1283 // FIXME: would be nice to grey them out instead of not listing them
1284 if (omod->deactivated()) continue;
1286 FileType type;
1287 type.name = (_(omod->get_filetypename()));
1288 type.pattern = "*";
1289 Glib::ustring extension = omod->get_extension();
1290 fileDialogExtensionToPattern (type.pattern, extension);
1291 type.extension= omod;
1292 fileTypeComboBox.append_text(type.name);
1293 fileTypes.push_back(type);
1294 }
1296 //#Let user choose
1297 FileType guessType;
1298 guessType.name = _("Guess from extension");
1299 guessType.pattern = "*";
1300 guessType.extension = NULL;
1301 fileTypeComboBox.append_text(guessType.name);
1302 fileTypes.push_back(guessType);
1305 fileTypeComboBox.set_active(0);
1306 fileTypeChangedCallback(); //call at least once to set the filter
1307 }
1310 /**
1311 * Constructor
1312 */
1313 FileExportDialogImpl::FileExportDialogImpl( Gtk::Window& parentWindow,
1314 const Glib::ustring &dir,
1315 FileDialogType fileTypes,
1316 const Glib::ustring &title,
1317 const Glib::ustring &/*default_key*/ ) :
1318 FileDialogBaseGtk(parentWindow, title, Gtk::FILE_CHOOSER_ACTION_SAVE, fileTypes, "dialogs.export"),
1319 sourceX0Spinner("X0", _("Left edge of source")),
1320 sourceY0Spinner("Y0", _("Top edge of source")),
1321 sourceX1Spinner("X1", _("Right edge of source")),
1322 sourceY1Spinner("Y1", _("Bottom edge of source")),
1323 sourceWidthSpinner("Width", _("Source width")),
1324 sourceHeightSpinner("Height", _("Source height")),
1325 destWidthSpinner("Width", _("Destination width")),
1326 destHeightSpinner("Height", _("Destination height")),
1327 destDPISpinner("DPI", _("Resolution (dots per inch)"))
1328 {
1329 append_extension = (bool)prefs_get_int_attribute("dialogs.save_as", "append_extension", 1);
1331 /* One file at a time */
1332 set_select_multiple(false);
1334 #ifdef WITH_GNOME_VFS
1335 if (gnome_vfs_initialized()) {
1336 set_local_only(false);
1337 }
1338 #endif
1340 /* Initalize to Autodetect */
1341 extension = NULL;
1342 /* No filename to start out with */
1343 myFilename = "";
1345 /* Set our dialog type (save, export, etc...)*/
1346 _dialogType = fileTypes;
1348 /* Set the pwd and/or the filename */
1349 if (dir.size()>0)
1350 {
1351 Glib::ustring udir(dir);
1352 Glib::ustring::size_type len = udir.length();
1353 // leaving a trailing backslash on the directory name leads to the infamous
1354 // double-directory bug on win32
1355 if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
1356 set_current_folder(udir.c_str());
1357 }
1359 //#########################################
1360 //## EXTRA WIDGET -- SOURCE SIDE
1361 //#########################################
1363 //##### Export options buttons/spinners, etc
1364 documentButton.set_label(_("Document"));
1365 scopeBox.pack_start(documentButton);
1366 scopeGroup = documentButton.get_group();
1368 pageButton.set_label(_("Page"));
1369 pageButton.set_group(scopeGroup);
1370 scopeBox.pack_start(pageButton);
1372 selectionButton.set_label(_("Selection"));
1373 selectionButton.set_group(scopeGroup);
1374 scopeBox.pack_start(selectionButton);
1376 customButton.set_label(_("Custom"));
1377 customButton.set_group(scopeGroup);
1378 scopeBox.pack_start(customButton);
1380 sourceBox.pack_start(scopeBox);
1384 //dimension buttons
1385 sourceTable.resize(3,3);
1386 sourceTable.attach(sourceX0Spinner, 0,1,0,1);
1387 sourceTable.attach(sourceY0Spinner, 1,2,0,1);
1388 sourceUnitsSpinner.setUnitType(UNIT_TYPE_LINEAR);
1389 sourceTable.attach(sourceUnitsSpinner, 2,3,0,1);
1390 sourceTable.attach(sourceX1Spinner, 0,1,1,2);
1391 sourceTable.attach(sourceY1Spinner, 1,2,1,2);
1392 sourceTable.attach(sourceWidthSpinner, 0,1,2,3);
1393 sourceTable.attach(sourceHeightSpinner, 1,2,2,3);
1395 sourceBox.pack_start(sourceTable);
1396 sourceFrame.set_label(_("Source"));
1397 sourceFrame.add(sourceBox);
1398 exportOptionsBox.pack_start(sourceFrame);
1401 //#########################################
1402 //## EXTRA WIDGET -- SOURCE SIDE
1403 //#########################################
1406 destTable.resize(3,3);
1407 destTable.attach(destWidthSpinner, 0,1,0,1);
1408 destTable.attach(destHeightSpinner, 1,2,0,1);
1409 destUnitsSpinner.setUnitType(UNIT_TYPE_LINEAR);
1410 destTable.attach(destUnitsSpinner, 2,3,0,1);
1411 destTable.attach(destDPISpinner, 0,1,1,2);
1413 destBox.pack_start(destTable);
1416 cairoButton.set_label(_("Cairo"));
1417 otherOptionBox.pack_start(cairoButton);
1419 antiAliasButton.set_label(_("Antialias"));
1420 otherOptionBox.pack_start(antiAliasButton);
1422 backgroundButton.set_label(_("Background"));
1423 otherOptionBox.pack_start(backgroundButton);
1425 destBox.pack_start(otherOptionBox);
1431 //###### File options
1432 //###### Do we want the .xxx extension automatically added?
1433 fileTypeCheckbox.set_label(Glib::ustring(_("Append filename extension automatically")));
1434 fileTypeCheckbox.set_active(append_extension);
1435 destBox.pack_start(fileTypeCheckbox);
1437 //###### File type menu
1438 createFileTypeMenu();
1439 fileTypeComboBox.set_size_request(200,40);
1440 fileTypeComboBox.signal_changed().connect(
1441 sigc::mem_fun(*this, &FileExportDialogImpl::fileTypeChangedCallback) );
1443 destBox.pack_start(fileTypeComboBox);
1445 destFrame.set_label(_("Destination"));
1446 destFrame.add(destBox);
1447 exportOptionsBox.pack_start(destFrame);
1449 //##### Put the two boxes and their parent onto the dialog
1450 exportOptionsBox.pack_start(sourceFrame);
1451 exportOptionsBox.pack_start(destFrame);
1453 set_extra_widget(exportOptionsBox);
1458 //Let's do some customization
1459 fileNameEntry = NULL;
1460 Gtk::Container *cont = get_toplevel();
1461 std::vector<Gtk::Entry *> entries;
1462 findEntryWidgets(cont, entries);
1463 //g_message("Found %d entry widgets\n", entries.size());
1464 if (entries.size() >=1 )
1465 {
1466 //Catch when user hits [return] on the text field
1467 fileNameEntry = entries[0];
1468 fileNameEntry->signal_activate().connect(
1469 sigc::mem_fun(*this, &FileExportDialogImpl::fileNameEntryChangedCallback) );
1470 }
1472 //Let's do more customization
1473 std::vector<Gtk::Expander *> expanders;
1474 findExpanderWidgets(cont, expanders);
1475 //g_message("Found %d expander widgets\n", expanders.size());
1476 if (expanders.size() >=1 )
1477 {
1478 //Always show the file list
1479 Gtk::Expander *expander = expanders[0];
1480 expander->set_expanded(true);
1481 }
1484 //if (extension == NULL)
1485 // checkbox.set_sensitive(FALSE);
1487 add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1488 set_default(*add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK));
1490 show_all_children();
1491 }
1493 /**
1494 * Destructor
1495 */
1496 FileExportDialogImpl::~FileExportDialogImpl()
1497 {
1498 }
1502 /**
1503 * Show this dialog modally. Return true if user hits [OK]
1504 */
1505 bool
1506 FileExportDialogImpl::show()
1507 {
1508 Glib::ustring s = Glib::filename_to_utf8 (get_current_folder());
1509 if (s.length() == 0)
1510 s = getcwd (NULL, 0);
1511 set_current_folder(Glib::filename_from_utf8(s)); //hack to force initial dir listing
1512 set_modal (TRUE); //Window
1513 sp_transientize((GtkWidget *)gobj()); //Make transient
1514 gint b = run(); //Dialog
1515 svgPreview.showNoPreview();
1516 hide();
1518 if (b == Gtk::RESPONSE_OK)
1519 {
1520 int sel = fileTypeComboBox.get_active_row_number ();
1521 if (sel>=0 && sel< (int)fileTypes.size())
1522 {
1523 FileType &type = fileTypes[sel];
1524 extension = type.extension;
1525 }
1526 myFilename = get_filename();
1527 #ifdef WITH_GNOME_VFS
1528 if ( myFilename.empty() && gnome_vfs_initialized() ) {
1529 myFilename = get_uri();
1530 }
1531 #endif
1533 /*
1535 // FIXME: Why do we have more code
1537 append_extension = checkbox.get_active();
1538 prefs_set_int_attribute("dialogs.save_as", "append_extension", append_extension);
1539 prefs_set_string_attribute("dialogs.save_as", "default",
1540 ( extension != NULL ? extension->get_id() : "" ));
1541 */
1542 return TRUE;
1543 }
1544 else
1545 {
1546 return FALSE;
1547 }
1548 }
1551 /**
1552 * Get the file extension type that was selected by the user. Valid after an [OK]
1553 */
1554 Inkscape::Extension::Extension *
1555 FileExportDialogImpl::getSelectionType()
1556 {
1557 return extension;
1558 }
1561 /**
1562 * Get the file name chosen by the user. Valid after an [OK]
1563 */
1564 Glib::ustring
1565 FileExportDialogImpl::getFilename()
1566 {
1567 return myFilename;
1568 }
1571 } //namespace Dialog
1572 } //namespace UI
1573 } //namespace Inkscape
1575 /*
1576 Local Variables:
1577 mode:c++
1578 c-file-style:"stroustrup"
1579 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1580 indent-tabs-mode:nil
1581 fill-column:99
1582 End:
1583 */
1584 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :