Code

convert almost all libnrtype to Geom::
[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) 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)
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);
498 bool SVGPreview::set(Glib::ustring &fileName, int dialogType)
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     }
551 SVGPreview::SVGPreview()
553     if (!INKSCAPE)
554         inkscape_application_init("",false);
555     document = NULL;
556     viewerGtk = NULL;
557     set_size_request(150,150);
558     showingNoPreview = false;
561 SVGPreview::~SVGPreview()
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()
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);
595 void FileDialogBaseGtk::cleanup( bool showConfirmed )
597     if ( showConfirmed )
598         prefs_set_int_attribute( preferenceBase.c_str(),
599                "enable_preview", previewCheckbox.get_active() );
603 void FileDialogBaseGtk::_previewEnabledCB()
605     bool enabled = previewCheckbox.get_active();
606     set_preview_widget_active(enabled);
607     if ( enabled ) {
608         _updatePreviewCallback();
609     }
614 /**
615  * Callback for checking if the preview needs to be redrawn
616  */
617 void FileDialogBaseGtk::_updatePreviewCallback()
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);
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")
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     }
700 /**
701  * Destructor
702  */
703 FileOpenDialogImplGtk::~FileOpenDialogImplGtk()
708 void FileOpenDialogImplGtk::createFilterMenu()
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;
758 /**
759  * Show this dialog modally.  Return true if user hits [OK]
760  */
761 bool
762 FileOpenDialogImplGtk::show()
764     set_modal (TRUE);                      //Window
765     sp_transientize((GtkWidget *)gobj());  //Make transient
766     gint b = run();                        //Dialog
767     svgPreview.showNoPreview();
768     hide();
770     if (b == Gtk::RESPONSE_OK)
771         {
772         //This is a hack, to avoid the warning messages that
773         //Gtk::FileChooser::get_filter() returns
774         //should be:  Gtk::FileFilter *filter = get_filter();
775         GtkFileChooser *gtkFileChooser = Gtk::FileChooser::gobj();
776         GtkFileFilter *filter = gtk_file_chooser_get_filter(gtkFileChooser);
777         if (filter)
778             {
779             //Get which extension was chosen, if any
780             extension = extensionMap[gtk_file_filter_get_name(filter)];
781             }
782         myFilename = get_filename();
783 #ifdef WITH_GNOME_VFS
784         if (myFilename.empty() && gnome_vfs_initialized())
785             myFilename = get_uri();
786 #endif
787         cleanup( true );
788         return TRUE;
789         }
790     else
791        {
792        cleanup( false );
793        return FALSE;
794        }
800 /**
801  * Get the file extension type that was selected by the user. Valid after an [OK]
802  */
803 Inkscape::Extension::Extension *
804 FileOpenDialogImplGtk::getSelectionType()
806     return extension;
810 /**
811  * Get the file name chosen by the user.   Valid after an [OK]
812  */
813 Glib::ustring
814 FileOpenDialogImplGtk::getFilename (void)
816     return myFilename;
820 /**
821  * To Get Multiple filenames selected at-once.
822  */
823 std::vector<Glib::ustring>FileOpenDialogImplGtk::getFilenames()
825     std::vector<Glib::ustring> result = get_filenames();
826 #ifdef WITH_GNOME_VFS
827     if (result.empty() && gnome_vfs_initialized())
828         result = get_uris();
829 #endif
830     return result;
833 Glib::ustring FileOpenDialogImplGtk::getCurrentDirectory()
835     return get_current_folder();
841 //########################################################################
842 //# F I L E    S A V E
843 //########################################################################
845 /**
846  * Constructor
847  */
848 FileSaveDialogImplGtk::FileSaveDialogImplGtk( Gtk::Window &parentWindow,
849                                               const Glib::ustring &dir,
850                                               FileDialogType fileTypes,
851                                               const Glib::ustring &title,
852                                               const Glib::ustring &/*default_key*/,
853                                               const gchar* docTitle) :
854     FileDialogBaseGtk(parentWindow, title, Gtk::FILE_CHOOSER_ACTION_SAVE, fileTypes, "dialogs.save_as")
856     FileSaveDialog::myDocTitle = docTitle;
858     /* One file at a time */
859     set_select_multiple(false);
861 #ifdef WITH_GNOME_VFS
862     if (gnome_vfs_initialized()) {
863         set_local_only(false);
864     }
865 #endif
867     /* Initalize to Autodetect */
868     extension = NULL;
869     /* No filename to start out with */
870     myFilename = "";
872     /* Set our dialog type (save, export, etc...)*/
873     _dialogType = fileTypes;
875     /* Set the pwd and/or the filename */
876     if (dir.size() > 0)
877         {
878         Glib::ustring udir(dir);
879         Glib::ustring::size_type len = udir.length();
880         // leaving a trailing backslash on the directory name leads to the infamous
881         // double-directory bug on win32
882         if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
883         myFilename = udir;
884         }
886     //###### Add the file types menu
887     //createFilterMenu();
889     //###### Do we want the .xxx extension automatically added?
890     fileTypeCheckbox.set_label(Glib::ustring(_("Append filename extension automatically")));
891     fileTypeCheckbox.set_active( (bool)prefs_get_int_attribute("dialogs.save_as",
892                                                                "append_extension", 1) );
894     createFileTypeMenu();
895     fileTypeComboBox.set_size_request(200,40);
896     fileTypeComboBox.signal_changed().connect(
897          sigc::mem_fun(*this, &FileSaveDialogImplGtk::fileTypeChangedCallback) );
900     childBox.pack_start( checksBox );
901     childBox.pack_end( fileTypeComboBox );
902     checksBox.pack_start( fileTypeCheckbox );
903     checksBox.pack_start( previewCheckbox );
905     set_extra_widget( childBox );
907     //Let's do some customization
908     fileNameEntry = NULL;
909     Gtk::Container *cont = get_toplevel();
910     std::vector<Gtk::Entry *> entries;
911     findEntryWidgets(cont, entries);
912     //g_message("Found %d entry widgets\n", entries.size());
913     if (entries.size() >=1 )
914         {
915         //Catch when user hits [return] on the text field
916         fileNameEntry = entries[0];
917         fileNameEntry->signal_activate().connect(
918              sigc::mem_fun(*this, &FileSaveDialogImplGtk::fileNameEntryChangedCallback) );
919         }
921     //Let's do more customization
922     std::vector<Gtk::Expander *> expanders;
923     findExpanderWidgets(cont, expanders);
924     //g_message("Found %d expander widgets\n", expanders.size());
925     if (expanders.size() >=1 )
926         {
927         //Always show the file list
928         Gtk::Expander *expander = expanders[0];
929         expander->set_expanded(true);
930         }
932     // allow easy access to the user's own templates folder
933     gchar *templates = profile_path ("templates");
934     if ( Inkscape::IO::file_test(templates, G_FILE_TEST_EXISTS)
935          && Inkscape::IO::file_test(templates, G_FILE_TEST_IS_DIR)
936          && g_path_is_absolute(templates)
937         )
938     {
939         add_shortcut_folder(templates);
940     }
941     g_free (templates);
944     //if (extension == NULL)
945     //    checkbox.set_sensitive(FALSE);
947     add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
948     set_default(*add_button(Gtk::Stock::SAVE,   Gtk::RESPONSE_OK));
950     show_all_children();
953 /**
954  * Destructor
955  */
956 FileSaveDialogImplGtk::~FileSaveDialogImplGtk()
960 /**
961  * Callback for fileNameEntry widget
962  */
963 void FileSaveDialogImplGtk::fileNameEntryChangedCallback()
965     if (!fileNameEntry)
966         return;
968     Glib::ustring fileName = fileNameEntry->get_text();
969     if (!Glib::get_charset()) //If we are not utf8
970         fileName = Glib::filename_to_utf8(fileName);
972     //g_message("User hit return.  Text is '%s'\n", fileName.c_str());
974     if (!Glib::path_is_absolute(fileName)) {
975         //try appending to the current path
976         // not this way: fileName = get_current_folder() + "/" + fileName;
977         std::vector<Glib::ustring> pathSegments;
978         pathSegments.push_back( get_current_folder() );
979         pathSegments.push_back( fileName );
980         fileName = Glib::build_filename(pathSegments);
981     }
983     //g_message("path:'%s'\n", fileName.c_str());
985     if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
986         set_current_folder(fileName);
987     } else if (/*Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR)*/1) {
988         //dialog with either (1) select a regular file or (2) cd to dir
989         //simulate an 'OK'
990         set_filename(fileName);
991         response(Gtk::RESPONSE_OK);
992     }
997 /**
998  * Callback for fileNameEntry widget
999  */
1000 void FileSaveDialogImplGtk::fileTypeChangedCallback()
1002     int sel = fileTypeComboBox.get_active_row_number();
1003     if (sel<0 || sel >= (int)fileTypes.size())
1004         return;
1005     FileType type = fileTypes[sel];
1006     //g_message("selected: %s\n", type.name.c_str());
1008     extension = type.extension;
1009     Gtk::FileFilter filter;
1010     filter.add_pattern(type.pattern);
1011     set_filter(filter);
1013     updateNameAndExtension();
1018 void FileSaveDialogImplGtk::createFileTypeMenu()
1020     Inkscape::Extension::DB::OutputList extension_list;
1021     Inkscape::Extension::db.get_output_list(extension_list);
1022     knownExtensions.clear();
1024     for (Inkscape::Extension::DB::OutputList::iterator current_item = extension_list.begin();
1025          current_item != extension_list.end(); current_item++)
1026     {
1027         Inkscape::Extension::Output * omod = *current_item;
1029         // FIXME: would be nice to grey them out instead of not listing them
1030         if (omod->deactivated()) continue;
1032         FileType type;
1033         type.name     = (_(omod->get_filetypename()));
1034         type.pattern  = "*";
1035         Glib::ustring extension = omod->get_extension();
1036         knownExtensions.insert( extension.casefold() );
1037         fileDialogExtensionToPattern (type.pattern, extension);
1038         type.extension= omod;
1039         fileTypeComboBox.append_text(type.name);
1040         fileTypes.push_back(type);
1041     }
1043     //#Let user choose
1044     FileType guessType;
1045     guessType.name = _("Guess from extension");
1046     guessType.pattern = "*";
1047     guessType.extension = NULL;
1048     fileTypeComboBox.append_text(guessType.name);
1049     fileTypes.push_back(guessType);
1052     fileTypeComboBox.set_active(0);
1053     fileTypeChangedCallback(); //call at least once to set the filter
1060 /**
1061  * Show this dialog modally.  Return true if user hits [OK]
1062  */
1063 bool
1064 FileSaveDialogImplGtk::show()
1066     change_path(myFilename);
1067     set_modal (TRUE);                      //Window
1068     sp_transientize((GtkWidget *)gobj());  //Make transient
1069     gint b = run();                        //Dialog
1070     svgPreview.showNoPreview();
1071     set_preview_widget_active(false);
1072     hide();
1074     if (b == Gtk::RESPONSE_OK)
1075         {
1076         updateNameAndExtension();
1078         // Store changes of the "Append filename automatically" checkbox back to preferences.
1079         prefs_set_int_attribute("dialogs.save_as", "append_extension", fileTypeCheckbox.get_active());
1081         // Store the last used save-as filetype to preferences.
1082         prefs_set_string_attribute("dialogs.save_as", "default",
1083                                    ( extension != NULL ? extension->get_id() : "" ));
1085         cleanup( true );
1087         return TRUE;
1088         }
1089     else
1090         {
1091         cleanup( false );
1093         return FALSE;
1094         }
1098 /**
1099  * Get the file extension type that was selected by the user. Valid after an [OK]
1100  */
1101 Inkscape::Extension::Extension *
1102 FileSaveDialogImplGtk::getSelectionType()
1104     return extension;
1107 void FileSaveDialogImplGtk::setSelectionType( Inkscape::Extension::Extension * key )
1109     // If no pointer to extension is passed in, look up based on filename extension.
1110     if ( !key ) {
1111         // Not quite UTF-8 here.
1112         gchar *filenameLower = g_ascii_strdown(myFilename.c_str(), -1);
1113         for ( int i = 0; !key && (i < (int)fileTypes.size()); i++ ) {
1114             Inkscape::Extension::Output *ext = dynamic_cast<Inkscape::Extension::Output*>(fileTypes[i].extension);
1115             if ( ext && ext->get_extension() ) {
1116                 gchar *extensionLower = g_ascii_strdown( ext->get_extension(), -1 );
1117                 if ( g_str_has_suffix(filenameLower, extensionLower) ) {
1118                     key = fileTypes[i].extension;
1119                 }
1120                 g_free(extensionLower);
1121             }
1122         }
1123         g_free(filenameLower);
1124     }
1126     // Ensure the proper entry in the combo box is selected.
1127     if ( key ) {
1128         extension = key;
1129         gchar const * extensionID = extension->get_id();
1130         if ( extensionID ) {
1131             for ( int i = 0; i < (int)fileTypes.size(); i++ ) {
1132                 Inkscape::Extension::Extension *ext = fileTypes[i].extension;
1133                 if ( ext ) {
1134                     gchar const * id = ext->get_id();
1135                     if ( id && ( strcmp(extensionID, id) == 0) ) {
1136                         int oldSel = fileTypeComboBox.get_active_row_number();
1137                         if ( i != oldSel ) {
1138                             fileTypeComboBox.set_active(i);
1139                         }
1140                         break;
1141                     }
1142                 }
1143             }
1144         }
1145     }
1148 Glib::ustring FileSaveDialogImplGtk::getCurrentDirectory()
1150     return get_current_folder();
1154 /*void
1155 FileSaveDialogImplGtk::change_title(const Glib::ustring& title)
1157     set_title(title);
1158 }*/
1160 /**
1161   * Change the default save path location.
1162   */
1163 void
1164 FileSaveDialogImplGtk::change_path(const Glib::ustring& path)
1166     myFilename = path;
1168     if (Glib::file_test(myFilename, Glib::FILE_TEST_IS_DIR)) {
1169         //fprintf(stderr,"set_current_folder(%s)\n",myFilename.c_str());
1170         set_current_folder(myFilename);
1171     } else {
1172         //fprintf(stderr,"set_filename(%s)\n",myFilename.c_str());
1173         if ( Glib::file_test( myFilename, Glib::FILE_TEST_EXISTS ) ) {
1174             set_filename(myFilename);
1175         } else {
1176             std::string dirName = Glib::path_get_dirname( myFilename  );
1177             if ( dirName != get_current_folder() ) {
1178                 set_current_folder(dirName);
1179             }
1180         }
1181         Glib::ustring basename = Glib::path_get_basename(myFilename);
1182         //fprintf(stderr,"set_current_name(%s)\n",basename.c_str());
1183         try {
1184             set_current_name( Glib::filename_to_utf8(basename) );
1185         } catch ( Glib::ConvertError& e ) {
1186             g_warning( "Error converting save filename to UTF-8." );
1187             // try a fallback.
1188             set_current_name( basename );
1189         }
1190     }
1193 void FileSaveDialogImplGtk::updateNameAndExtension()
1195     // Pick up any changes the user has typed in.
1196     Glib::ustring tmp = get_filename();
1197 #ifdef WITH_GNOME_VFS
1198     if ( tmp.empty() && gnome_vfs_initialized() ) {
1199         tmp = get_uri();
1200     }
1201 #endif
1202     if ( !tmp.empty() ) {
1203         myFilename = tmp;
1204     }
1206     Inkscape::Extension::Output* newOut = extension ? dynamic_cast<Inkscape::Extension::Output*>(extension) : 0;
1207     if ( fileTypeCheckbox.get_active() && newOut ) {
1208         // Append the file extension if it's not already present
1209         appendExtension(myFilename, newOut);
1210     }
1214 //########################################################################
1215 //# F I L E     E X P O R T
1216 //########################################################################
1218 /**
1219  * Callback for fileNameEntry widget
1220  */
1221 void FileExportDialogImpl::fileNameEntryChangedCallback()
1223     if (!fileNameEntry)
1224         return;
1226     Glib::ustring fileName = fileNameEntry->get_text();
1227     if (!Glib::get_charset()) //If we are not utf8
1228         fileName = Glib::filename_to_utf8(fileName);
1230     //g_message("User hit return.  Text is '%s'\n", fileName.c_str());
1232     if (!Glib::path_is_absolute(fileName)) {
1233         //try appending to the current path
1234         // not this way: fileName = get_current_folder() + "/" + fileName;
1235         std::vector<Glib::ustring> pathSegments;
1236         pathSegments.push_back( get_current_folder() );
1237         pathSegments.push_back( fileName );
1238         fileName = Glib::build_filename(pathSegments);
1239     }
1241     //g_message("path:'%s'\n", fileName.c_str());
1243     if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
1244         set_current_folder(fileName);
1245     } else if (/*Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR)*/1) {
1246         //dialog with either (1) select a regular file or (2) cd to dir
1247         //simulate an 'OK'
1248         set_filename(fileName);
1249         response(Gtk::RESPONSE_OK);
1250     }
1255 /**
1256  * Callback for fileNameEntry widget
1257  */
1258 void FileExportDialogImpl::fileTypeChangedCallback()
1260     int sel = fileTypeComboBox.get_active_row_number();
1261     if (sel<0 || sel >= (int)fileTypes.size())
1262         return;
1263     FileType type = fileTypes[sel];
1264     //g_message("selected: %s\n", type.name.c_str());
1265     Gtk::FileFilter filter;
1266     filter.add_pattern(type.pattern);
1267     set_filter(filter);
1272 void FileExportDialogImpl::createFileTypeMenu()
1274     Inkscape::Extension::DB::OutputList extension_list;
1275     Inkscape::Extension::db.get_output_list(extension_list);
1277     for (Inkscape::Extension::DB::OutputList::iterator current_item = extension_list.begin();
1278          current_item != extension_list.end(); current_item++)
1279     {
1280         Inkscape::Extension::Output * omod = *current_item;
1282         // FIXME: would be nice to grey them out instead of not listing them
1283         if (omod->deactivated()) continue;
1285         FileType type;
1286         type.name     = (_(omod->get_filetypename()));
1287         type.pattern  = "*";
1288         Glib::ustring extension = omod->get_extension();
1289         fileDialogExtensionToPattern (type.pattern, extension);
1290         type.extension= omod;
1291         fileTypeComboBox.append_text(type.name);
1292         fileTypes.push_back(type);
1293     }
1295     //#Let user choose
1296     FileType guessType;
1297     guessType.name = _("Guess from extension");
1298     guessType.pattern = "*";
1299     guessType.extension = NULL;
1300     fileTypeComboBox.append_text(guessType.name);
1301     fileTypes.push_back(guessType);
1304     fileTypeComboBox.set_active(0);
1305     fileTypeChangedCallback(); //call at least once to set the filter
1309 /**
1310  * Constructor
1311  */
1312 FileExportDialogImpl::FileExportDialogImpl( Gtk::Window& parentWindow,
1313                                             const Glib::ustring &dir,
1314                                             FileDialogType fileTypes,
1315                                             const Glib::ustring &title,
1316                                             const Glib::ustring &/*default_key*/ ) :
1317             FileDialogBaseGtk(parentWindow, title, Gtk::FILE_CHOOSER_ACTION_SAVE, fileTypes, "dialogs.export"),
1318             sourceX0Spinner("X0",         _("Left edge of source")),
1319             sourceY0Spinner("Y0",         _("Top edge of source")),
1320             sourceX1Spinner("X1",         _("Right edge of source")),
1321             sourceY1Spinner("Y1",         _("Bottom edge of source")),
1322             sourceWidthSpinner("Width",   _("Source width")),
1323             sourceHeightSpinner("Height", _("Source height")),
1324             destWidthSpinner("Width",     _("Destination width")),
1325             destHeightSpinner("Height",   _("Destination height")),
1326             destDPISpinner("DPI",         _("Resolution (dots per inch)"))
1328     append_extension = (bool)prefs_get_int_attribute("dialogs.save_as", "append_extension", 1);
1330     /* One file at a time */
1331     set_select_multiple(false);
1333 #ifdef WITH_GNOME_VFS
1334     if (gnome_vfs_initialized()) {
1335         set_local_only(false);
1336     }
1337 #endif
1339     /* Initalize to Autodetect */
1340     extension = NULL;
1341     /* No filename to start out with */
1342     myFilename = "";
1344     /* Set our dialog type (save, export, etc...)*/
1345     _dialogType = fileTypes;
1347     /* Set the pwd and/or the filename */
1348     if (dir.size()>0)
1349         {
1350         Glib::ustring udir(dir);
1351         Glib::ustring::size_type len = udir.length();
1352         // leaving a trailing backslash on the directory name leads to the infamous
1353         // double-directory bug on win32
1354         if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
1355         set_current_folder(udir.c_str());
1356         }
1358     //#########################################
1359     //## EXTRA WIDGET -- SOURCE SIDE
1360     //#########################################
1362     //##### Export options buttons/spinners, etc
1363     documentButton.set_label(_("Document"));
1364     scopeBox.pack_start(documentButton);
1365     scopeGroup = documentButton.get_group();
1367     pageButton.set_label(_("Page"));
1368     pageButton.set_group(scopeGroup);
1369     scopeBox.pack_start(pageButton);
1371     selectionButton.set_label(_("Selection"));
1372     selectionButton.set_group(scopeGroup);
1373     scopeBox.pack_start(selectionButton);
1375     customButton.set_label(_("Custom"));
1376     customButton.set_group(scopeGroup);
1377     scopeBox.pack_start(customButton);
1379     sourceBox.pack_start(scopeBox);
1383     //dimension buttons
1384     sourceTable.resize(3,3);
1385     sourceTable.attach(sourceX0Spinner,     0,1,0,1);
1386     sourceTable.attach(sourceY0Spinner,     1,2,0,1);
1387     sourceUnitsSpinner.setUnitType(UNIT_TYPE_LINEAR);
1388     sourceTable.attach(sourceUnitsSpinner,  2,3,0,1);
1389     sourceTable.attach(sourceX1Spinner,     0,1,1,2);
1390     sourceTable.attach(sourceY1Spinner,     1,2,1,2);
1391     sourceTable.attach(sourceWidthSpinner,  0,1,2,3);
1392     sourceTable.attach(sourceHeightSpinner, 1,2,2,3);
1394     sourceBox.pack_start(sourceTable);
1395     sourceFrame.set_label(_("Source"));
1396     sourceFrame.add(sourceBox);
1397     exportOptionsBox.pack_start(sourceFrame);
1400     //#########################################
1401     //## EXTRA WIDGET -- SOURCE SIDE
1402     //#########################################
1405     destTable.resize(3,3);
1406     destTable.attach(destWidthSpinner,    0,1,0,1);
1407     destTable.attach(destHeightSpinner,   1,2,0,1);
1408     destUnitsSpinner.setUnitType(UNIT_TYPE_LINEAR);
1409     destTable.attach(destUnitsSpinner,    2,3,0,1);
1410     destTable.attach(destDPISpinner,      0,1,1,2);
1412     destBox.pack_start(destTable);
1415     cairoButton.set_label(_("Cairo"));
1416     otherOptionBox.pack_start(cairoButton);
1418     antiAliasButton.set_label(_("Antialias"));
1419     otherOptionBox.pack_start(antiAliasButton);
1421     backgroundButton.set_label(_("Background"));
1422     otherOptionBox.pack_start(backgroundButton);
1424     destBox.pack_start(otherOptionBox);
1430     //###### File options
1431     //###### Do we want the .xxx extension automatically added?
1432     fileTypeCheckbox.set_label(Glib::ustring(_("Append filename extension automatically")));
1433     fileTypeCheckbox.set_active(append_extension);
1434     destBox.pack_start(fileTypeCheckbox);
1436     //###### File type menu
1437     createFileTypeMenu();
1438     fileTypeComboBox.set_size_request(200,40);
1439     fileTypeComboBox.signal_changed().connect(
1440          sigc::mem_fun(*this, &FileExportDialogImpl::fileTypeChangedCallback) );
1442     destBox.pack_start(fileTypeComboBox);
1444     destFrame.set_label(_("Destination"));
1445     destFrame.add(destBox);
1446     exportOptionsBox.pack_start(destFrame);
1448     //##### Put the two boxes and their parent onto the dialog
1449     exportOptionsBox.pack_start(sourceFrame);
1450     exportOptionsBox.pack_start(destFrame);
1452     set_extra_widget(exportOptionsBox);
1457     //Let's do some customization
1458     fileNameEntry = NULL;
1459     Gtk::Container *cont = get_toplevel();
1460     std::vector<Gtk::Entry *> entries;
1461     findEntryWidgets(cont, entries);
1462     //g_message("Found %d entry widgets\n", entries.size());
1463     if (entries.size() >=1 )
1464         {
1465         //Catch when user hits [return] on the text field
1466         fileNameEntry = entries[0];
1467         fileNameEntry->signal_activate().connect(
1468              sigc::mem_fun(*this, &FileExportDialogImpl::fileNameEntryChangedCallback) );
1469         }
1471     //Let's do more customization
1472     std::vector<Gtk::Expander *> expanders;
1473     findExpanderWidgets(cont, expanders);
1474     //g_message("Found %d expander widgets\n", expanders.size());
1475     if (expanders.size() >=1 )
1476         {
1477         //Always show the file list
1478         Gtk::Expander *expander = expanders[0];
1479         expander->set_expanded(true);
1480         }
1483     //if (extension == NULL)
1484     //    checkbox.set_sensitive(FALSE);
1486     add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1487     set_default(*add_button(Gtk::Stock::SAVE,   Gtk::RESPONSE_OK));
1489     show_all_children();
1492 /**
1493  * Destructor
1494  */
1495 FileExportDialogImpl::~FileExportDialogImpl()
1501 /**
1502  * Show this dialog modally.  Return true if user hits [OK]
1503  */
1504 bool
1505 FileExportDialogImpl::show()
1507     Glib::ustring s = Glib::filename_to_utf8 (get_current_folder());
1508     if (s.length() == 0)
1509         s = getcwd (NULL, 0);
1510     set_current_folder(Glib::filename_from_utf8(s)); //hack to force initial dir listing
1511     set_modal (TRUE);                      //Window
1512     sp_transientize((GtkWidget *)gobj());  //Make transient
1513     gint b = run();                        //Dialog
1514     svgPreview.showNoPreview();
1515     hide();
1517     if (b == Gtk::RESPONSE_OK)
1518         {
1519         int sel = fileTypeComboBox.get_active_row_number ();
1520         if (sel>=0 && sel< (int)fileTypes.size())
1521             {
1522             FileType &type = fileTypes[sel];
1523             extension = type.extension;
1524             }
1525         myFilename = get_filename();
1526 #ifdef WITH_GNOME_VFS
1527         if ( myFilename.empty() && gnome_vfs_initialized() ) {
1528             myFilename = get_uri();
1529         }
1530 #endif
1532         /*
1534         // FIXME: Why do we have more code
1536         append_extension = checkbox.get_active();
1537         prefs_set_int_attribute("dialogs.save_as", "append_extension", append_extension);
1538         prefs_set_string_attribute("dialogs.save_as", "default",
1539                   ( extension != NULL ? extension->get_id() : "" ));
1540         */
1541         return TRUE;
1542         }
1543     else
1544         {
1545         return FALSE;
1546         }
1550 /**
1551  * Get the file extension type that was selected by the user. Valid after an [OK]
1552  */
1553 Inkscape::Extension::Extension *
1554 FileExportDialogImpl::getSelectionType()
1556     return extension;
1560 /**
1561  * Get the file name chosen by the user.   Valid after an [OK]
1562  */
1563 Glib::ustring
1564 FileExportDialogImpl::getFilename()
1566     return myFilename;
1570 } //namespace Dialog
1571 } //namespace UI
1572 } //namespace Inkscape
1574 /*
1575   Local Variables:
1576   mode:c++
1577   c-file-style:"stroustrup"
1578   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1579   indent-tabs-mode:nil
1580   fill-column:99
1581   End:
1582 */
1583 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :