Code

a1773d39a317c36775d91815e5fe0141a5e62cf5
[inkscape.git] / src / ui / dialog / filedialogimpl-gtkmm.cpp
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) 2004-2007 The Inkscape Organization
13  * Copyright (C) 2007 Joel Holdsworth
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)
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         }
118 /*#########################################################################
119 ### SVG Preview Widget
120 #########################################################################*/
122 bool SVGPreview::setDocument(SPDocument *doc)
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;
143 bool SVGPreview::setFileName(Glib::ustring &theFileName)
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;
168 bool SVGPreview::setFromMem(char const *xmlBuffer)
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;
191 void SVGPreview::showImage(Glib::ustring &theFileName)
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);
298 void SVGPreview::showNoPreview()
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;
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)
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);
499 /**
500  * Return true if the string ends with the given suffix
501  */
502 static bool
503 hasSuffix(Glib::ustring &str, Glib::ustring &ext)
505     int strLen = str.length();
506     int extLen = ext.length();
507     if (extLen > strLen)
508         return false;
509     int strpos = strLen-1;
510     for (int extpos = extLen-1 ; extpos>=0 ; extpos--, strpos--)
511         {
512         Glib::ustring::value_type ch = str[strpos];
513         if (ch != ext[extpos])
514             {
515             if ( ((ch & 0xff80) != 0) ||
516                  static_cast<Glib::ustring::value_type>( g_ascii_tolower( static_cast<gchar>(0x07f & ch) ) ) != ext[extpos] )
517                 {
518                 return false;
519                 }
520             }
521         }
522     return true;
526 /**
527  * Return true if the image is loadable by Gdk, else false
528  */
529 static bool
530 isValidImageFile(Glib::ustring &fileName)
532     std::vector<Gdk::PixbufFormat>formats = Gdk::Pixbuf::get_formats();
533     for (unsigned int i=0; i<formats.size(); i++)
534         {
535         Gdk::PixbufFormat format = formats[i];
536         std::vector<Glib::ustring>extensions = format.get_extensions();
537         for (unsigned int j=0; j<extensions.size(); j++)
538             {
539             Glib::ustring ext = extensions[j];
540             if (hasSuffix(fileName, ext))
541                 return true;
542             }
543         }
544     return false;
547 bool SVGPreview::set(Glib::ustring &fileName, int dialogType)
550     if (!Glib::file_test(fileName, Glib::FILE_TEST_EXISTS))
551         return false;
553     //g_message("fname:%s", fileName.c_str());
555     if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
556         showNoPreview();
557         return false;
558     }
560     if (Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR))
561         {
562         Glib::ustring fileNameUtf8 = Glib::filename_to_utf8(fileName);
563         gchar *fName = (gchar *)fileNameUtf8.c_str();
564         struct stat info;
565         if (g_stat(fName, &info))
566             {
567             g_warning("SVGPreview::set() : %s : %s",
568                            fName, strerror(errno));
569             return FALSE;
570             }
571         long fileLen = info.st_size;
572         if (fileLen > 0x150000L)
573             {
574             showingNoPreview = false;
575             showTooLarge(fileLen);
576             return FALSE;
577             }
578         }
580     Glib::ustring svg = ".svg";
581     Glib::ustring svgz = ".svgz";
583     if ((dialogType == SVG_TYPES || dialogType == IMPORT_TYPES) &&
584            (hasSuffix(fileName, svg) || hasSuffix(fileName, svgz)   )
585         ) {
586         bool retval = setFileName(fileName);
587         showingNoPreview = false;
588         return retval;
589     } else if (isValidImageFile(fileName)) {
590         showImage(fileName);
591         showingNoPreview = false;
592         return true;
593     } else {
594         showNoPreview();
595         return false;
596     }
600 SVGPreview::SVGPreview()
602     if (!INKSCAPE)
603         inkscape_application_init("",false);
604     document = NULL;
605     viewerGtk = NULL;
606     set_size_request(150,150);
607     showingNoPreview = false;
610 SVGPreview::~SVGPreview()
616 /*#########################################################################
617 ### F I L E     D I A L O G    B A S E    C L A S S
618 #########################################################################*/
620 void FileDialogBaseGtk::internalSetup()
622     bool enablePreview =
623         (bool)prefs_get_int_attribute( preferenceBase.c_str(),
624              "enable_preview", 1 );
626     previewCheckbox.set_label( Glib::ustring(_("Enable preview")) );
627     previewCheckbox.set_active( enablePreview );
629     previewCheckbox.signal_toggled().connect(
630         sigc::mem_fun(*this, &FileDialogBaseGtk::_previewEnabledCB) );
632     //Catch selection-changed events, so we can adjust the text widget
633     signal_update_preview().connect(
634          sigc::mem_fun(*this, &FileDialogBaseGtk::_updatePreviewCallback) );
636     //###### Add a preview widget
637     set_preview_widget(svgPreview);
638     set_preview_widget_active( enablePreview );
639     set_use_preview_label (false);
644 void FileDialogBaseGtk::cleanup( bool showConfirmed )
646     if ( showConfirmed )
647         prefs_set_int_attribute( preferenceBase.c_str(),
648                "enable_preview", previewCheckbox.get_active() );
652 void FileDialogBaseGtk::_previewEnabledCB()
654     bool enabled = previewCheckbox.get_active();
655     set_preview_widget_active(enabled);
656     if ( enabled ) {
657         _updatePreviewCallback();
658     }
663 /**
664  * Callback for checking if the preview needs to be redrawn
665  */
666 void FileDialogBaseGtk::_updatePreviewCallback()
668     Glib::ustring fileName = get_preview_filename();
670 #ifdef WITH_GNOME_VFS
671     if ( fileName.empty() && gnome_vfs_initialized() ) {
672         fileName = get_preview_uri();
673     }
674 #endif
676     if (fileName.empty()) {
677         return;
678     }
680     svgPreview.set(fileName, dialogType);
684 /*#########################################################################
685 ### F I L E    O P E N
686 #########################################################################*/
688 /**
689  * Constructor.  Not called directly.  Use the factory.
690  */
691 FileOpenDialogImplGtk::FileOpenDialogImplGtk(Gtk::Window& parentWindow,
692                                        const Glib::ustring &dir,
693                                        FileDialogType fileTypes,
694                                        const Glib::ustring &title) :
695     FileDialogBaseGtk(parentWindow, title, fileTypes, "dialogs.open")
699     /* One file at a time */
700     /* And also Multiple Files */
701     set_select_multiple(true);
703 #ifdef WITH_GNOME_VFS
704     if (gnome_vfs_initialized()) {
705         set_local_only(false);
706     }
707 #endif
709     /* Initalize to Autodetect */
710     extension = NULL;
711     /* No filename to start out with */
712     myFilename = "";
714     /* Set our dialog type (open, import, etc...)*/
715     dialogType = fileTypes;
718     /* Set the pwd and/or the filename */
719     if (dir.size() > 0)
720         {
721         Glib::ustring udir(dir);
722         Glib::ustring::size_type len = udir.length();
723         // leaving a trailing backslash on the directory name leads to the infamous
724         // double-directory bug on win32
725         if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
726         set_current_folder(udir.c_str());
727         }
730     set_extra_widget( previewCheckbox );
733     //###### Add the file types menu
734     createFilterMenu();
736     add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
737     set_default(*add_button(Gtk::Stock::OPEN,   Gtk::RESPONSE_OK));
738         
739         //###### Allow easy access to our examples folder                
740     if(Inkscape::IO::file_test(INKSCAPE_EXAMPLESDIR,
741                 (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
742         {
743         add_shortcut_folder(INKSCAPE_EXAMPLESDIR);
744     }
747 /**
748  * Destructor
749  */
750 FileOpenDialogImplGtk::~FileOpenDialogImplGtk()
755 void FileOpenDialogImplGtk::createFilterMenu()
757     Gtk::FileFilter allInkscapeFilter;
758     allInkscapeFilter.set_name(_("All Inkscape Files"));
759     extensionMap[Glib::ustring(_("All Inkscape Files"))]=NULL;
760     add_filter(allInkscapeFilter);
762     Gtk::FileFilter allFilter;
763     allFilter.set_name(_("All Files"));
764     extensionMap[Glib::ustring(_("All Files"))]=NULL;
765     allFilter.add_pattern("*");
766     add_filter(allFilter);
768     Gtk::FileFilter allImageFilter;
769     allImageFilter.set_name(_("All Images"));
770     extensionMap[Glib::ustring(_("All Images"))]=NULL;
771     add_filter(allImageFilter);
773     //patterns added dynamically below
774     Inkscape::Extension::DB::InputList extension_list;
775     Inkscape::Extension::db.get_input_list(extension_list);
777     for (Inkscape::Extension::DB::InputList::iterator current_item = extension_list.begin();
778          current_item != extension_list.end(); current_item++)
779     {
780         Inkscape::Extension::Input * imod = *current_item;
782         // FIXME: would be nice to grey them out instead of not listing them
783         if (imod->deactivated()) continue;
785         Glib::ustring upattern("*");
786         Glib::ustring extension = imod->get_extension();
787         fileDialogExtensionToPattern(upattern, extension);
789         Gtk::FileFilter filter;
790         Glib::ustring uname(_(imod->get_filetypename()));
791         filter.set_name(uname);
792         filter.add_pattern(upattern);
793         add_filter(filter);
794         extensionMap[uname] = imod;
796         //g_message("ext %s:%s '%s'\n", ioext->name, ioext->mimetype, upattern.c_str());
797         allInkscapeFilter.add_pattern(upattern);
798         if ( strncmp("image", imod->get_mimetype(), 5)==0 )
799             allImageFilter.add_pattern(upattern);
800     }
802     return;
805 /**
806  * Show this dialog modally.  Return true if user hits [OK]
807  */
808 bool
809 FileOpenDialogImplGtk::show()
811     Glib::ustring s = Glib::filename_to_utf8 (get_current_folder());
812     if (s.length() == 0)
813         s = getcwd (NULL, 0);
814     set_current_folder(Glib::filename_from_utf8(s)); //hack to force initial dir listing
815     set_modal (TRUE);                      //Window
816     sp_transientize((GtkWidget *)gobj());  //Make transient
817     gint b = run();                        //Dialog
818     svgPreview.showNoPreview();
819     hide();
821     if (b == Gtk::RESPONSE_OK)
822         {
823         //This is a hack, to avoid the warning messages that
824         //Gtk::FileChooser::get_filter() returns
825         //should be:  Gtk::FileFilter *filter = get_filter();
826         GtkFileChooser *gtkFileChooser = Gtk::FileChooser::gobj();
827         GtkFileFilter *filter = gtk_file_chooser_get_filter(gtkFileChooser);
828         if (filter)
829             {
830             //Get which extension was chosen, if any
831             extension = extensionMap[gtk_file_filter_get_name(filter)];
832             }
833         myFilename = get_filename();
834 #ifdef WITH_GNOME_VFS
835         if (myFilename.empty() && gnome_vfs_initialized())
836             myFilename = get_uri();
837 #endif
838         cleanup( true );
839         return TRUE;
840         }
841     else
842        {
843        cleanup( false );
844        return FALSE;
845        }
851 /**
852  * Get the file extension type that was selected by the user. Valid after an [OK]
853  */
854 Inkscape::Extension::Extension *
855 FileOpenDialogImplGtk::getSelectionType()
857     return extension;
861 /**
862  * Get the file name chosen by the user.   Valid after an [OK]
863  */
864 Glib::ustring
865 FileOpenDialogImplGtk::getFilename (void)
867     return g_strdup(myFilename.c_str());
871 /**
872  * To Get Multiple filenames selected at-once.
873  */
874 std::vector<Glib::ustring>FileOpenDialogImplGtk::getFilenames()
876     std::vector<Glib::ustring> result = get_filenames();
877 #ifdef WITH_GNOME_VFS
878     if (result.empty() && gnome_vfs_initialized())
879         result = get_uris();
880 #endif
881     return result;
889 //########################################################################
890 //# F I L E    S A V E
891 //########################################################################
893 /**
894  * Constructor
895  */
896 FileSaveDialogImplGtk::FileSaveDialogImplGtk( Gtk::Window &parentWindow,
897                                               const Glib::ustring &dir,
898                                               FileDialogType fileTypes,
899                                               const Glib::ustring &title,
900                                               const Glib::ustring &/*default_key*/ ) :
901     FileDialogBaseGtk(parentWindow, title, Gtk::FILE_CHOOSER_ACTION_SAVE, fileTypes, "dialogs.save_as")
903     /* One file at a time */
904     set_select_multiple(false);
906 #ifdef WITH_GNOME_VFS
907     if (gnome_vfs_initialized()) {
908         set_local_only(false);
909     }
910 #endif
912     /* Initalize to Autodetect */
913     extension = NULL;
914     /* No filename to start out with */
915     myFilename = "";
917     /* Set our dialog type (save, export, etc...)*/
918     dialogType = fileTypes;
920     /* Set the pwd and/or the filename */
921     if (dir.size() > 0)
922         {
923         Glib::ustring udir(dir);
924         Glib::ustring::size_type len = udir.length();
925         // leaving a trailing backslash on the directory name leads to the infamous
926         // double-directory bug on win32
927         if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
928         myFilename = udir;
929         }
931     //###### Add the file types menu
932     //createFilterMenu();
934     //###### Do we want the .xxx extension automatically added?
935     fileTypeCheckbox.set_label(Glib::ustring(_("Append filename extension automatically")));
936     fileTypeCheckbox.set_active( (bool)prefs_get_int_attribute("dialogs.save_as",
937                                                                "append_extension", 1) );
939     createFileTypeMenu();
940     fileTypeComboBox.set_size_request(200,40);
941     fileTypeComboBox.signal_changed().connect(
942          sigc::mem_fun(*this, &FileSaveDialogImplGtk::fileTypeChangedCallback) );
945     childBox.pack_start( checksBox );
946     childBox.pack_end( fileTypeComboBox );
947     checksBox.pack_start( fileTypeCheckbox );
948     checksBox.pack_start( previewCheckbox );
950     set_extra_widget( childBox );
952     //Let's do some customization
953     fileNameEntry = NULL;
954     Gtk::Container *cont = get_toplevel();
955     std::vector<Gtk::Entry *> entries;
956     findEntryWidgets(cont, entries);
957     //g_message("Found %d entry widgets\n", entries.size());
958     if (entries.size() >=1 )
959         {
960         //Catch when user hits [return] on the text field
961         fileNameEntry = entries[0];
962         fileNameEntry->signal_activate().connect(
963              sigc::mem_fun(*this, &FileSaveDialogImplGtk::fileNameEntryChangedCallback) );
964         }
966     //Let's do more customization
967     std::vector<Gtk::Expander *> expanders;
968     findExpanderWidgets(cont, expanders);
969     //g_message("Found %d expander widgets\n", expanders.size());
970     if (expanders.size() >=1 )
971         {
972         //Always show the file list
973         Gtk::Expander *expander = expanders[0];
974         expander->set_expanded(true);
975         }
978     //if (extension == NULL)
979     //    checkbox.set_sensitive(FALSE);
981     add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
982     set_default(*add_button(Gtk::Stock::SAVE,   Gtk::RESPONSE_OK));
984     show_all_children();
987 /**
988  * Destructor
989  */
990 FileSaveDialogImplGtk::~FileSaveDialogImplGtk()
994 /**
995  * Callback for fileNameEntry widget
996  */
997 void FileSaveDialogImplGtk::fileNameEntryChangedCallback()
999     if (!fileNameEntry)
1000         return;
1002     Glib::ustring fileName = fileNameEntry->get_text();
1003     if (!Glib::get_charset()) //If we are not utf8
1004         fileName = Glib::filename_to_utf8(fileName);
1006     //g_message("User hit return.  Text is '%s'\n", fileName.c_str());
1008     if (!Glib::path_is_absolute(fileName)) {
1009         //try appending to the current path
1010         // not this way: fileName = get_current_folder() + "/" + fileName;
1011         std::vector<Glib::ustring> pathSegments;
1012         pathSegments.push_back( get_current_folder() );
1013         pathSegments.push_back( fileName );
1014         fileName = Glib::build_filename(pathSegments);
1015     }
1017     //g_message("path:'%s'\n", fileName.c_str());
1019     if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
1020         set_current_folder(fileName);
1021     } else if (/*Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR)*/1) {
1022         //dialog with either (1) select a regular file or (2) cd to dir
1023         //simulate an 'OK'
1024         set_filename(fileName);
1025         response(Gtk::RESPONSE_OK);
1026     }
1031 /**
1032  * Callback for fileNameEntry widget
1033  */
1034 void FileSaveDialogImplGtk::fileTypeChangedCallback()
1036     int sel = fileTypeComboBox.get_active_row_number();
1037     if (sel<0 || sel >= (int)fileTypes.size())
1038         return;
1039     FileType type = fileTypes[sel];
1040     //g_message("selected: %s\n", type.name.c_str());
1042     extension = type.extension;
1043     Gtk::FileFilter filter;
1044     filter.add_pattern(type.pattern);
1045     set_filter(filter);
1047     updateNameAndExtension();
1052 void FileSaveDialogImplGtk::createFileTypeMenu()
1054     Inkscape::Extension::DB::OutputList extension_list;
1055     Inkscape::Extension::db.get_output_list(extension_list);
1056     knownExtensions.clear();
1058     for (Inkscape::Extension::DB::OutputList::iterator current_item = extension_list.begin();
1059          current_item != extension_list.end(); current_item++)
1060     {
1061         Inkscape::Extension::Output * omod = *current_item;
1063         // FIXME: would be nice to grey them out instead of not listing them
1064         if (omod->deactivated()) continue;
1066         FileType type;
1067         type.name     = (_(omod->get_filetypename()));
1068         type.pattern  = "*";
1069         Glib::ustring extension = omod->get_extension();
1070         knownExtensions.insert( extension.casefold() );
1071         fileDialogExtensionToPattern (type.pattern, extension);
1072         type.extension= omod;
1073         fileTypeComboBox.append_text(type.name);
1074         fileTypes.push_back(type);
1075     }
1077     //#Let user choose
1078     FileType guessType;
1079     guessType.name = _("Guess from extension");
1080     guessType.pattern = "*";
1081     guessType.extension = NULL;
1082     fileTypeComboBox.append_text(guessType.name);
1083     fileTypes.push_back(guessType);
1086     fileTypeComboBox.set_active(0);
1087     fileTypeChangedCallback(); //call at least once to set the filter
1094 /**
1095  * Show this dialog modally.  Return true if user hits [OK]
1096  */
1097 bool
1098 FileSaveDialogImplGtk::show()
1100     change_path(myFilename);
1101     set_modal (TRUE);                      //Window
1102     sp_transientize((GtkWidget *)gobj());  //Make transient
1103     gint b = run();                        //Dialog
1104     svgPreview.showNoPreview();
1105     set_preview_widget_active(false);
1106     hide();
1108     if (b == Gtk::RESPONSE_OK)
1109         {
1110         updateNameAndExtension();
1112         // Store changes of the "Append filename automatically" checkbox back to preferences.
1113         prefs_set_int_attribute("dialogs.save_as", "append_extension", fileTypeCheckbox.get_active());
1115         // Store the last used save-as filetype to preferences.
1116         prefs_set_string_attribute("dialogs.save_as", "default",
1117                                    ( extension != NULL ? extension->get_id() : "" ));
1119         cleanup( true );
1121         return TRUE;
1122         }
1123     else
1124         {
1125         cleanup( false );
1127         return FALSE;
1128         }
1132 /**
1133  * Get the file extension type that was selected by the user. Valid after an [OK]
1134  */
1135 Inkscape::Extension::Extension *
1136 FileSaveDialogImplGtk::getSelectionType()
1138     return extension;
1141 void FileSaveDialogImplGtk::setSelectionType( Inkscape::Extension::Extension * key )
1143     // If no pointer to extension is passed in, look up based on filename extension.
1144     if ( !key ) {
1145         // Not quite UTF-8 here.
1146         gchar *filenameLower = g_ascii_strdown(myFilename.c_str(), -1);
1147         for ( int i = 0; !key && (i < (int)fileTypes.size()); i++ ) {
1148             Inkscape::Extension::Output *ext = dynamic_cast<Inkscape::Extension::Output*>(fileTypes[i].extension);
1149             if ( ext && ext->get_extension() ) {
1150                 gchar *extensionLower = g_ascii_strdown( ext->get_extension(), -1 );
1151                 if ( g_str_has_suffix(filenameLower, extensionLower) ) {
1152                     key = fileTypes[i].extension;
1153                 }
1154                 g_free(extensionLower);
1155             }
1156         }
1157         g_free(filenameLower);
1158     }
1160     // Ensure the proper entry in the combo box is selected.
1161     if ( key ) {
1162         extension = key;
1163         gchar const * extensionID = extension->get_id();
1164         if ( extensionID ) {
1165             for ( int i = 0; i < (int)fileTypes.size(); i++ ) {
1166                 Inkscape::Extension::Extension *ext = fileTypes[i].extension;
1167                 if ( ext ) {
1168                     gchar const * id = ext->get_id();
1169                     if ( id && ( strcmp(extensionID, id) == 0) ) {
1170                         int oldSel = fileTypeComboBox.get_active_row_number();
1171                         if ( i != oldSel ) {
1172                             fileTypeComboBox.set_active(i);
1173                         }
1174                         break;
1175                     }
1176                 }
1177             }
1178         }
1179     }
1183 /**
1184  * Get the file name chosen by the user.   Valid after an [OK]
1185  */
1186 Glib::ustring
1187 FileSaveDialogImplGtk::getFilename()
1189     return myFilename;
1193 void
1194 FileSaveDialogImplGtk::change_title(const Glib::ustring& title)
1196     this->set_title(title);
1199 /**
1200   * Change the default save path location.
1201   */
1202 void
1203 FileSaveDialogImplGtk::change_path(const Glib::ustring& path)
1205     myFilename = path;
1206     if (Glib::file_test(myFilename, Glib::FILE_TEST_IS_DIR)) {
1207         //fprintf(stderr,"set_current_folder(%s)\n",myFilename.c_str());
1208         set_current_folder(myFilename);
1209     } else {
1210         //fprintf(stderr,"set_filename(%s)\n",myFilename.c_str());
1211         if ( Glib::file_test( myFilename, Glib::FILE_TEST_EXISTS ) ) {
1212             set_filename(myFilename);
1213         } else {
1214             std::string dirName = Glib::path_get_dirname( myFilename  );
1215             if ( dirName != get_current_folder() ) {
1216                 set_current_folder(dirName);
1217             }
1218         }
1219         Glib::ustring basename = Glib::path_get_basename(myFilename);
1220         //fprintf(stderr,"set_current_name(%s)\n",basename.c_str());
1221         try {
1222             set_current_name( Glib::filename_to_utf8(basename) );
1223         } catch ( Glib::ConvertError& e ) {
1224             g_warning( "Error converting save filename to UTF-8." );
1225             // try a fallback.
1226             set_current_name( basename );
1227         }
1228     }
1231 void FileSaveDialogImplGtk::updateNameAndExtension()
1233     // Pick up any changes the user has typed in.
1234     Glib::ustring tmp = get_filename();
1235 #ifdef WITH_GNOME_VFS
1236     if ( tmp.empty() && gnome_vfs_initialized() ) {
1237         tmp = get_uri();
1238     }
1239 #endif
1240     if ( !tmp.empty() ) {
1241         myFilename = tmp;
1242     }
1244     Inkscape::Extension::Output* newOut = extension ? dynamic_cast<Inkscape::Extension::Output*>(extension) : 0;
1245     if ( fileTypeCheckbox.get_active() && newOut ) {
1246         try {
1247             bool appendExtension = true;
1248             Glib::ustring utf8Name = Glib::filename_to_utf8( myFilename );
1249             Glib::ustring::size_type pos = utf8Name.rfind('.');
1250             if ( pos != Glib::ustring::npos ) {
1251                 Glib::ustring trail = utf8Name.substr( pos );
1252                 Glib::ustring foldedTrail = trail.casefold();
1253                 if ( (trail == ".")
1254                      | (foldedTrail != Glib::ustring( newOut->get_extension() ).casefold()
1255                         && ( knownExtensions.find(foldedTrail) != knownExtensions.end() ) ) ) {
1256                     utf8Name = utf8Name.erase( pos );
1257                 } else {
1258                     appendExtension = false;
1259                 }
1260             }
1262             if (appendExtension) {
1263                 utf8Name = utf8Name + newOut->get_extension();
1264                 myFilename = Glib::filename_from_utf8( utf8Name );
1265                 change_path(myFilename);
1266             }
1267         } catch ( Glib::ConvertError& e ) {
1268             // ignore
1269         }
1270     }
1274 //########################################################################
1275 //# F I L E     E X P O R T
1276 //########################################################################
1278 /**
1279  * Callback for fileNameEntry widget
1280  */
1281 void FileExportDialogImpl::fileNameEntryChangedCallback()
1283     if (!fileNameEntry)
1284         return;
1286     Glib::ustring fileName = fileNameEntry->get_text();
1287     if (!Glib::get_charset()) //If we are not utf8
1288         fileName = Glib::filename_to_utf8(fileName);
1290     //g_message("User hit return.  Text is '%s'\n", fileName.c_str());
1292     if (!Glib::path_is_absolute(fileName)) {
1293         //try appending to the current path
1294         // not this way: fileName = get_current_folder() + "/" + fileName;
1295         std::vector<Glib::ustring> pathSegments;
1296         pathSegments.push_back( get_current_folder() );
1297         pathSegments.push_back( fileName );
1298         fileName = Glib::build_filename(pathSegments);
1299     }
1301     //g_message("path:'%s'\n", fileName.c_str());
1303     if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
1304         set_current_folder(fileName);
1305     } else if (/*Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR)*/1) {
1306         //dialog with either (1) select a regular file or (2) cd to dir
1307         //simulate an 'OK'
1308         set_filename(fileName);
1309         response(Gtk::RESPONSE_OK);
1310     }
1315 /**
1316  * Callback for fileNameEntry widget
1317  */
1318 void FileExportDialogImpl::fileTypeChangedCallback()
1320     int sel = fileTypeComboBox.get_active_row_number();
1321     if (sel<0 || sel >= (int)fileTypes.size())
1322         return;
1323     FileType type = fileTypes[sel];
1324     //g_message("selected: %s\n", type.name.c_str());
1325     Gtk::FileFilter filter;
1326     filter.add_pattern(type.pattern);
1327     set_filter(filter);
1332 void FileExportDialogImpl::createFileTypeMenu()
1334     Inkscape::Extension::DB::OutputList extension_list;
1335     Inkscape::Extension::db.get_output_list(extension_list);
1337     for (Inkscape::Extension::DB::OutputList::iterator current_item = extension_list.begin();
1338          current_item != extension_list.end(); current_item++)
1339     {
1340         Inkscape::Extension::Output * omod = *current_item;
1342         // FIXME: would be nice to grey them out instead of not listing them
1343         if (omod->deactivated()) continue;
1345         FileType type;
1346         type.name     = (_(omod->get_filetypename()));
1347         type.pattern  = "*";
1348         Glib::ustring extension = omod->get_extension();
1349         fileDialogExtensionToPattern (type.pattern, extension);
1350         type.extension= omod;
1351         fileTypeComboBox.append_text(type.name);
1352         fileTypes.push_back(type);
1353     }
1355     //#Let user choose
1356     FileType guessType;
1357     guessType.name = _("Guess from extension");
1358     guessType.pattern = "*";
1359     guessType.extension = NULL;
1360     fileTypeComboBox.append_text(guessType.name);
1361     fileTypes.push_back(guessType);
1364     fileTypeComboBox.set_active(0);
1365     fileTypeChangedCallback(); //call at least once to set the filter
1369 /**
1370  * Constructor
1371  */
1372 FileExportDialogImpl::FileExportDialogImpl( Gtk::Window& parentWindow,
1373                                             const Glib::ustring &dir,
1374                                             FileDialogType fileTypes,
1375                                             const Glib::ustring &title,
1376                                             const Glib::ustring &/*default_key*/ ) :
1377             FileDialogBaseGtk(parentWindow, title, Gtk::FILE_CHOOSER_ACTION_SAVE, fileTypes, "dialogs.export"),
1378             sourceX0Spinner("X0",         _("Left edge of source")),
1379             sourceY0Spinner("Y0",         _("Top edge of source")),
1380             sourceX1Spinner("X1",         _("Right edge of source")),
1381             sourceY1Spinner("Y1",         _("Bottom edge of source")),
1382             sourceWidthSpinner("Width",   _("Source width")),
1383             sourceHeightSpinner("Height", _("Source height")),
1384             destWidthSpinner("Width",     _("Destination width")),
1385             destHeightSpinner("Height",   _("Destination height")),
1386             destDPISpinner("DPI",         _("Resolution (dots per inch)"))
1388     append_extension = (bool)prefs_get_int_attribute("dialogs.save_as", "append_extension", 1);
1390     /* One file at a time */
1391     set_select_multiple(false);
1393 #ifdef WITH_GNOME_VFS
1394     if (gnome_vfs_initialized()) {
1395         set_local_only(false);
1396     }
1397 #endif
1399     /* Initalize to Autodetect */
1400     extension = NULL;
1401     /* No filename to start out with */
1402     myFilename = "";
1404     /* Set our dialog type (save, export, etc...)*/
1405     dialogType = fileTypes;
1407     /* Set the pwd and/or the filename */
1408     if (dir.size()>0)
1409         {
1410         Glib::ustring udir(dir);
1411         Glib::ustring::size_type len = udir.length();
1412         // leaving a trailing backslash on the directory name leads to the infamous
1413         // double-directory bug on win32
1414         if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
1415         set_current_folder(udir.c_str());
1416         }
1418     //#########################################
1419     //## EXTRA WIDGET -- SOURCE SIDE
1420     //#########################################
1422     //##### Export options buttons/spinners, etc
1423     documentButton.set_label(_("Document"));
1424     scopeBox.pack_start(documentButton);
1425     scopeGroup = documentButton.get_group();
1427     pageButton.set_label(_("Page"));
1428     pageButton.set_group(scopeGroup);
1429     scopeBox.pack_start(pageButton);
1431     selectionButton.set_label(_("Selection"));
1432     selectionButton.set_group(scopeGroup);
1433     scopeBox.pack_start(selectionButton);
1435     customButton.set_label(_("Custom"));
1436     customButton.set_group(scopeGroup);
1437     scopeBox.pack_start(customButton);
1439     sourceBox.pack_start(scopeBox);
1443     //dimension buttons
1444     sourceTable.resize(3,3);
1445     sourceTable.attach(sourceX0Spinner,     0,1,0,1);
1446     sourceTable.attach(sourceY0Spinner,     1,2,0,1);
1447     sourceUnitsSpinner.setUnitType(UNIT_TYPE_LINEAR);
1448     sourceTable.attach(sourceUnitsSpinner,  2,3,0,1);
1449     sourceTable.attach(sourceX1Spinner,     0,1,1,2);
1450     sourceTable.attach(sourceY1Spinner,     1,2,1,2);
1451     sourceTable.attach(sourceWidthSpinner,  0,1,2,3);
1452     sourceTable.attach(sourceHeightSpinner, 1,2,2,3);
1454     sourceBox.pack_start(sourceTable);
1455     sourceFrame.set_label(_("Source"));
1456     sourceFrame.add(sourceBox);
1457     exportOptionsBox.pack_start(sourceFrame);
1460     //#########################################
1461     //## EXTRA WIDGET -- SOURCE SIDE
1462     //#########################################
1465     destTable.resize(3,3);
1466     destTable.attach(destWidthSpinner,    0,1,0,1);
1467     destTable.attach(destHeightSpinner,   1,2,0,1);
1468     destUnitsSpinner.setUnitType(UNIT_TYPE_LINEAR);
1469     destTable.attach(destUnitsSpinner,    2,3,0,1);
1470     destTable.attach(destDPISpinner,      0,1,1,2);
1472     destBox.pack_start(destTable);
1475     cairoButton.set_label(_("Cairo"));
1476     otherOptionBox.pack_start(cairoButton);
1478     antiAliasButton.set_label(_("Antialias"));
1479     otherOptionBox.pack_start(antiAliasButton);
1481     backgroundButton.set_label(_("Background"));
1482     otherOptionBox.pack_start(backgroundButton);
1484     destBox.pack_start(otherOptionBox);
1490     //###### File options
1491     //###### Do we want the .xxx extension automatically added?
1492     fileTypeCheckbox.set_label(Glib::ustring(_("Append filename extension automatically")));
1493     fileTypeCheckbox.set_active(append_extension);
1494     destBox.pack_start(fileTypeCheckbox);
1496     //###### File type menu
1497     createFileTypeMenu();
1498     fileTypeComboBox.set_size_request(200,40);
1499     fileTypeComboBox.signal_changed().connect(
1500          sigc::mem_fun(*this, &FileExportDialogImpl::fileTypeChangedCallback) );
1502     destBox.pack_start(fileTypeComboBox);
1504     destFrame.set_label(_("Destination"));
1505     destFrame.add(destBox);
1506     exportOptionsBox.pack_start(destFrame);
1508     //##### Put the two boxes and their parent onto the dialog
1509     exportOptionsBox.pack_start(sourceFrame);
1510     exportOptionsBox.pack_start(destFrame);
1512     set_extra_widget(exportOptionsBox);
1517     //Let's do some customization
1518     fileNameEntry = NULL;
1519     Gtk::Container *cont = get_toplevel();
1520     std::vector<Gtk::Entry *> entries;
1521     findEntryWidgets(cont, entries);
1522     //g_message("Found %d entry widgets\n", entries.size());
1523     if (entries.size() >=1 )
1524         {
1525         //Catch when user hits [return] on the text field
1526         fileNameEntry = entries[0];
1527         fileNameEntry->signal_activate().connect(
1528              sigc::mem_fun(*this, &FileExportDialogImpl::fileNameEntryChangedCallback) );
1529         }
1531     //Let's do more customization
1532     std::vector<Gtk::Expander *> expanders;
1533     findExpanderWidgets(cont, expanders);
1534     //g_message("Found %d expander widgets\n", expanders.size());
1535     if (expanders.size() >=1 )
1536         {
1537         //Always show the file list
1538         Gtk::Expander *expander = expanders[0];
1539         expander->set_expanded(true);
1540         }
1543     //if (extension == NULL)
1544     //    checkbox.set_sensitive(FALSE);
1546     add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1547     set_default(*add_button(Gtk::Stock::SAVE,   Gtk::RESPONSE_OK));
1549     show_all_children();
1552 /**
1553  * Destructor
1554  */
1555 FileExportDialogImpl::~FileExportDialogImpl()
1561 /**
1562  * Show this dialog modally.  Return true if user hits [OK]
1563  */
1564 bool
1565 FileExportDialogImpl::show()
1567     Glib::ustring s = Glib::filename_to_utf8 (get_current_folder());
1568     if (s.length() == 0)
1569         s = getcwd (NULL, 0);
1570     set_current_folder(Glib::filename_from_utf8(s)); //hack to force initial dir listing
1571     set_modal (TRUE);                      //Window
1572     sp_transientize((GtkWidget *)gobj());  //Make transient
1573     gint b = run();                        //Dialog
1574     svgPreview.showNoPreview();
1575     hide();
1577     if (b == Gtk::RESPONSE_OK)
1578         {
1579         int sel = fileTypeComboBox.get_active_row_number ();
1580         if (sel>=0 && sel< (int)fileTypes.size())
1581             {
1582             FileType &type = fileTypes[sel];
1583             extension = type.extension;
1584             }
1585         myFilename = get_filename();
1586 #ifdef WITH_GNOME_VFS
1587         if ( myFilename.empty() && gnome_vfs_initialized() ) {
1588             myFilename = get_uri();
1589         }
1590 #endif
1592         /*
1594         // FIXME: Why do we have more code
1596         append_extension = checkbox.get_active();
1597         prefs_set_int_attribute("dialogs.save_as", "append_extension", append_extension);
1598         prefs_set_string_attribute("dialogs.save_as", "default",
1599                   ( extension != NULL ? extension->get_id() : "" ));
1600         */
1601         return TRUE;
1602         }
1603     else
1604         {
1605         return FALSE;
1606         }
1610 /**
1611  * Get the file extension type that was selected by the user. Valid after an [OK]
1612  */
1613 Inkscape::Extension::Extension *
1614 FileExportDialogImpl::getSelectionType()
1616     return extension;
1620 /**
1621  * Get the file name chosen by the user.   Valid after an [OK]
1622  */
1623 Glib::ustring
1624 FileExportDialogImpl::getFilename()
1626     return myFilename;
1630 } //namespace Dialog
1631 } //namespace UI
1632 } //namespace Inkscape
1634 /*
1635   Local Variables:
1636   mode:c++
1637   c-file-style:"stroustrup"
1638   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1639   indent-tabs-mode:nil
1640   fill-column:99
1641   End:
1642 */
1643 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :