Code

Using translator-credits
[inkscape.git] / src / ui / dialog / filedialog.cpp
1 /*
2  * Implementation of the file dialog interfaces defined in filedialog.h
3  *
4  * Authors:
5  *   Bob Jamison
6  *   Other dudes from The Inkscape Organization
7  *
8  * Copyright (C) 2006 Johan Engelen <johan@shouraizou.nl>
9  * Copyright (C) 2004-2006 The Inkscape Organization
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
14 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif
20 //Temporary ugly hack
21 //Remove these after the get_filter() calls in
22 //show() on both classes are fixed
23 #include <gtk/gtkfilechooser.h>
25 //Another hack
26 #include <gtk/gtkentry.h>
27 #include <gtk/gtkexpander.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
31 #include <glibmm/i18n.h>
32 #include <gtkmm/box.h>
33 #include <gtkmm/colorbutton.h>
34 #include <gtkmm/frame.h>
35 #include <gtkmm/filechooserdialog.h>
36 #include <gtkmm/menubar.h>
37 #include <gtkmm/menu.h>
38 #include <gtkmm/entry.h>
39 #include <gtkmm/expander.h>
40 #include <gtkmm/comboboxtext.h>
41 #include <gtkmm/stock.h>
42 #include <gdkmm/pixbuf.h>
44 #include "prefs-utils.h"
45 #include <dialogs/dialog-events.h>
46 #include <extension/input.h>
47 #include <extension/output.h>
48 #include <extension/db.h>
49 #include "inkscape.h"
50 #include "svg-view-widget.h"
51 #include "filedialog.h"
52 #include "gc-core.h"
54 //For export dialog
55 #include "ui/widget/scalar-unit.h"
58 #undef INK_DUMP_FILENAME_CONV
60 #ifdef INK_DUMP_FILENAME_CONV
61 void dump_str( const gchar* str, const gchar* prefix );
62 void dump_ustr( const Glib::ustring& ustr );
63 #endif
65 namespace Inkscape
66 {
67 namespace UI
68 {
69 namespace Dialog
70 {
76 //########################################################################
77 //### U T I L I T Y
78 //########################################################################
80 /**
81     \brief  A quick function to turn a standard extension into a searchable
82             pattern for the file dialogs
83     \param  pattern  The patter that the extension should be written to
84     \param  in_file_extension  The C string that represents the extension
86     This function just goes through the string, and takes all characters
87     and puts a [<upper><lower>] so that both are searched and shown in
88     the file dialog.  This function edits the pattern string to make
89     this happen.
90 */
91 static void
92 fileDialogExtensionToPattern(Glib::ustring &pattern,
93                       Glib::ustring &extension)
94 {
95     for (unsigned int i = 0; i < extension.length(); i++ )
96         {
97         Glib::ustring::value_type ch = extension[i];
98         if ( Glib::Unicode::isalpha(ch) )
99             {
100             pattern += '[';
101             pattern += Glib::Unicode::toupper(ch);
102             pattern += Glib::Unicode::tolower(ch);
103             pattern += ']';
104             }
105         else
106             {
107             pattern += ch;
108             }
109         }
113 /**
114  *  Hack:  Find all entry widgets in a container
115  */
116 static void
117 findEntryWidgets(Gtk::Container *parent,
118                  std::vector<Gtk::Entry *> &result)
120     if (!parent)
121         return;
122     std::vector<Gtk::Widget *> children = parent->get_children();
123     for (unsigned int i=0; i<children.size() ; i++)
124         {
125         Gtk::Widget *child = children[i];
126         GtkWidget *wid = child->gobj();
127         if (GTK_IS_ENTRY(wid))
128            result.push_back((Gtk::Entry *)child);
129         else if (GTK_IS_CONTAINER(wid))
130             findEntryWidgets((Gtk::Container *)child, result);
131         }
138 /**
139  *  Hack:  Find all expander widgets in a container
140  */
141 static void
142 findExpanderWidgets(Gtk::Container *parent,
143                     std::vector<Gtk::Expander *> &result)
145     if (!parent)
146         return;
147     std::vector<Gtk::Widget *> children = parent->get_children();
148     for (unsigned int i=0; i<children.size() ; i++)
149         {
150         Gtk::Widget *child = children[i];
151         GtkWidget *wid = child->gobj();
152         if (GTK_IS_EXPANDER(wid))
153            result.push_back((Gtk::Expander *)child);
154         else if (GTK_IS_CONTAINER(wid))
155             findExpanderWidgets((Gtk::Container *)child, result);
156         }
161 /*#########################################################################
162 ### SVG Preview Widget
163 #########################################################################*/
165 /**
166  * Simple class for displaying an SVG file in the "preview widget."
167  * Currently, this is just a wrapper of the sp_svg_view Gtk widget.
168  * Hopefully we will eventually replace with a pure Gtkmm widget.
169  */
170 class SVGPreview : public Gtk::VBox
172 public:
174     SVGPreview();
176     ~SVGPreview();
178     bool setDocument(SPDocument *doc);
180     bool setFileName(Glib::ustring &fileName);
182     bool setFromMem(char const *xmlBuffer);
184     bool set(Glib::ustring &fileName, int dialogType);
186     bool setURI(URI &uri);
188     /**
189      * Show image embedded in SVG
190      */
191     void showImage(Glib::ustring &fileName);
193     /**
194      * Show the "No preview" image
195      */
196     void showNoPreview();
198     /**
199      * Show the "Too large" image
200      */
201     void showTooLarge(long fileLength);
203 private:
204     /**
205      * The svg document we are currently showing
206      */
207     SPDocument *document;
209     /**
210      * The sp_svg_view widget
211      */
212     GtkWidget *viewerGtk;
214     /**
215      * are we currently showing the "no preview" image?
216      */
217     bool showingNoPreview;
219 };
222 bool SVGPreview::setDocument(SPDocument *doc)
224     if (document)
225         sp_document_unref(document);
227     sp_document_ref(doc);
228     document = doc;
230     //This should remove it from the box, and free resources
231     if (viewerGtk)
232         gtk_widget_destroy(viewerGtk);
234     viewerGtk  = sp_svg_view_widget_new(doc);
235     GtkWidget *vbox = (GtkWidget *)gobj();
236     gtk_box_pack_start(GTK_BOX(vbox), viewerGtk, TRUE, TRUE, 0);
237     gtk_widget_show(viewerGtk);
239     return true;
242 bool SVGPreview::setFileName(Glib::ustring &theFileName)
244     Glib::ustring fileName = theFileName;
246     fileName = Glib::filename_to_utf8(fileName);
248     SPDocument *doc = sp_document_new (fileName.c_str(), 0);
249     if (!doc) {
250         g_warning("SVGView: error loading document '%s'\n", fileName.c_str());
251         return false;
252     }
254     setDocument(doc);
256     sp_document_unref(doc);
258     return true;
263 bool SVGPreview::setFromMem(char const *xmlBuffer)
265     if (!xmlBuffer)
266         return false;
268     gint len = (gint)strlen(xmlBuffer);
269     SPDocument *doc = sp_document_new_from_mem(xmlBuffer, len, 0);
270     if (!doc) {
271         g_warning("SVGView: error loading buffer '%s'\n",xmlBuffer);
272         return false;
273     }
275     setDocument(doc);
277     sp_document_unref(doc);
279     Inkscape::GC::request_early_collection();
281     return true;
286 void SVGPreview::showImage(Glib::ustring &theFileName)
288     Glib::ustring fileName = theFileName;
291     /*#####################################
292     # LET'S HAVE SOME FUN WITH SVG!
293     # Instead of just loading an image, why
294     # don't we make a lovely little svg and
295     # display it nicely?
296     #####################################*/
298     //Arbitrary size of svg doc -- rather 'portrait' shaped
299     gint previewWidth  = 400;
300     gint previewHeight = 600;
302     //Get some image info. Smart pointer does not need to be deleted
303     Glib::RefPtr<Gdk::Pixbuf> img = Gdk::Pixbuf::create_from_file(fileName);
304     gint imgWidth  = img->get_width();
305     gint imgHeight = img->get_height();
307     //Find the minimum scale to fit the image inside the preview area
308     double scaleFactorX = (0.9 *(double)previewWidth)  / ((double)imgWidth);
309     double scaleFactorY = (0.9 *(double)previewHeight) / ((double)imgHeight);
310     double scaleFactor = scaleFactorX;
311     if (scaleFactorX > scaleFactorY)
312         scaleFactor = scaleFactorY;
314     //Now get the resized values
315     gint scaledImgWidth  = (int) (scaleFactor * (double)imgWidth);
316     gint scaledImgHeight = (int) (scaleFactor * (double)imgHeight);
318     //center the image on the area
319     gint imgX = (previewWidth  - scaledImgWidth)  / 2;
320     gint imgY = (previewHeight - scaledImgHeight) / 2;
322     //wrap a rectangle around the image
323     gint rectX      = imgX-1;
324     gint rectY      = imgY-1;
325     gint rectWidth  = scaledImgWidth +2;
326     gint rectHeight = scaledImgHeight+2;
328     //Our template.  Modify to taste
329     gchar const *xformat =
330           "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
331           "<svg\n"
332           "xmlns=\"http://www.w3.org/2000/svg\"\n"
333           "xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
334           "width=\"%d\" height=\"%d\">\n"
335           "<rect\n"
336           "  style=\"fill:#eeeeee;stroke:none\"\n"
337           "  x=\"-100\" y=\"-100\" width=\"4000\" height=\"4000\"/>\n"
338           "<image x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\"\n"
339           "xlink:href=\"%s\"/>\n"
340           "<rect\n"
341           "  style=\"fill:none;"
342           "    stroke:#000000;stroke-width:1.0;"
343           "    stroke-linejoin:miter;stroke-opacity:1.0000000;"
344           "    stroke-miterlimit:4.0000000;stroke-dasharray:none\"\n"
345           "  x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\"/>\n"
346           "<text\n"
347           "  style=\"font-size:24.000000;font-style:normal;font-weight:normal;"
348           "    fill:#000000;fill-opacity:1.0000000;stroke:none;"
349           "    font-family:Bitstream Vera Sans\"\n"
350           "  x=\"10\" y=\"26\">%d x %d</text>\n"
351           "</svg>\n\n";
353     //if (!Glib::get_charset()) //If we are not utf8
354     fileName = Glib::filename_to_utf8(fileName);
356     //Fill in the template
357     /* FIXME: Do proper XML quoting for fileName. */
358     gchar *xmlBuffer = g_strdup_printf(xformat,
359            previewWidth, previewHeight,
360            imgX, imgY, scaledImgWidth, scaledImgHeight,
361            fileName.c_str(),
362            rectX, rectY, rectWidth, rectHeight,
363            imgWidth, imgHeight);
365     //g_message("%s\n", xmlBuffer);
367     //now show it!
368     setFromMem(xmlBuffer);
369     g_free(xmlBuffer);
374 void SVGPreview::showNoPreview()
376     //Are we already showing it?
377     if (showingNoPreview)
378         return;
380     //Arbitrary size of svg doc -- rather 'portrait' shaped
381     gint previewWidth  = 300;
382     gint previewHeight = 600;
384     //Our template.  Modify to taste
385     gchar const *xformat =
386           "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
387           "<svg\n"
388           "xmlns=\"http://www.w3.org/2000/svg\"\n"
389           "xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
390           "width=\"%d\" height=\"%d\">\n"
391           "<g transform=\"translate(-190,24.27184)\" style=\"opacity:0.12\">\n"
392           "<path\n"
393           "style=\"font-size:12;fill:#ffffff;fill-rule:evenodd;stroke:#000000;stroke-width:0.936193pt\"\n"
394           "d=\"M 397.64309 320.25301 L 280.39197 282.517 L 250.74227 124.83447 L 345.08225 "
395           "29.146783 L 393.59996 46.667064 L 483.89679 135.61619 L 397.64309 320.25301 z \"\n"
396           "id=\"whiteSpace\" />\n"
397           "<path\n"
398           "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
399           "d=\"M 476.95792 339.17168 C 495.78197 342.93607 499.54842 356.11361 495.78197 359.87802 "
400           "C 492.01856 363.6434 482.6065 367.40781 475.07663 361.76014 C 467.54478 "
401           "356.11361 467.54478 342.93607 476.95792 339.17168 z \"\n"
402           "id=\"droplet01\" />\n"
403           "<path\n"
404           "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
405           "d=\"M 286.46194 340.42914 C 284.6277 340.91835 269.30405 327.71337 257.16909 333.8338 "
406           "C 245.03722 339.95336 236.89276 353.65666 248.22676 359.27982 C 259.56184 364.90298 "
407           "267.66433 358.41867 277.60113 351.44119 C 287.53903 344.46477 "
408           "287.18046 343.1206 286.46194 340.42914 z \"\n"
409           "id=\"droplet02\" />\n"
410           "<path\n"
411           "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
412           "d=\"M 510.35756 306.92856 C 520.59494 304.36879 544.24333 306.92856 540.47688 321.98634 "
413           "C 536.71354 337.04806 504.71297 331.39827 484.00371 323.87156 C 482.12141 "
414           "308.81083 505.53237 308.13423 510.35756 306.92856 z \"\n"
415           "id=\"droplet03\" />\n"
416           "<path\n"
417           "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
418           "d=\"M 359.2403 21.362537 C 347.92693 21.362537 336.6347 25.683095 327.96556 34.35223 "
419           "L 173.87387 188.41466 C 165.37697 196.9114 161.1116 207.95813 160.94269 219.04577 "
420           "L 160.88418 219.04577 C 160.88418 219.08524 160.94076 219.12322 160.94269 219.16279 "
421           "C 160.94033 219.34888 160.88418 219.53256 160.88418 219.71865 L 161.14748 219.71865 "
422           "C 164.0966 230.93917 240.29699 245.24198 248.79866 253.74346 C 261.63771 266.58263 "
423           "199.5652 276.01151 212.4041 288.85074 C 225.24316 301.68979 289.99433 313.6933 302.8346 "
424           "326.53254 C 315.67368 339.37161 276.5961 353.04289 289.43532 365.88196 C 302.27439 "
425           "378.72118 345.40201 362.67257 337.5908 396.16198 C 354.92909 413.50026 391.10302 "
426           "405.2208 415.32417 387.88252 C 428.16323 375.04345 390.6948 376.17577 403.53397 "
427           "363.33668 C 416.37304 350.49745 448.78128 350.4282 476.08902 319.71589 C 465.09739 "
428           "302.62116 429.10801 295.34136 441.94719 282.50217 C 454.78625 269.66311 479.74708 "
429           "276.18423 533.60644 251.72479 C 559.89837 239.78398 557.72636 230.71459 557.62567 "
430           "219.71865 C 557.62356 219.48727 557.62567 219.27892 557.62567 219.04577 L 557.56716 "
431           "219.04577 C 557.3983 207.95812 553.10345 196.9114 544.60673 188.41466 L 390.54428 "
432           "34.35223 C 381.87515 25.683095 370.55366 21.362537 359.2403 21.362537 z M 357.92378 "
433           "41.402939 C 362.95327 41.533963 367.01541 45.368018 374.98006 50.530832 L 447.76915 "
434           "104.50827 C 448.56596 105.02498 449.32484 105.564 450.02187 106.11735 C 450.7189 106.67062 "
435           "451.3556 107.25745 451.95277 107.84347 C 452.54997 108.42842 453.09281 109.01553 453.59111 "
436           "109.62808 C 454.08837 110.24052 454.53956 110.86661 454.93688 111.50048 C 455.33532 112.13538 "
437           "455.69164 112.78029 455.9901 113.43137 C 456.28877 114.08363 456.52291 114.75639 456.7215 "
438           "115.42078 C 456.92126 116.08419 457.08982 116.73973 457.18961 117.41019 C 457.28949 "
439           "118.08184 457.33588 118.75535 457.33588 119.42886 L 414.21245 98.598549 L 409.9118 "
440           "131.16055 L 386.18512 120.04324 L 349.55654 144.50131 L 335.54288 96.1703 L 317.4919 "
441           "138.4453 L 267.08369 143.47735 L 267.63956 121.03795 C 267.63956 115.64823 296.69685 "
442           "77.915899 314.39075 68.932902 L 346.77721 45.674327 C 351.55594 42.576634 354.90608 "
443           "41.324327 357.92378 41.402939 z M 290.92738 261.61333 C 313.87149 267.56365 339.40299 "
444           "275.37038 359.88393 275.50997 L 360.76161 284.72563 C 343.2235 282.91785 306.11346 "
445           "274.45012 297.36372 269.98057 L 290.92738 261.61333 z \"\n"
446           "id=\"mountainDroplet\" />\n"
447           "</g> <g transform=\"translate(-20,0)\">\n"
448           "<text xml:space=\"preserve\"\n"
449           "style=\"font-size:32.000000;font-style:normal;font-variant:normal;font-weight:bold;"
450           "font-stretch:normal;fill:#000000;fill-opacity:1.0000000;stroke:none;stroke-width:1.0000000pt;"
451           "stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1.0000000;"
452           "font-family:Bitstream Vera Sans;text-anchor:middle;writing-mode:lr\"\n"
453           "x=\"190\" y=\"240\">%s</text></g>\n"
454           "</svg>\n\n";
456     //Fill in the template
457     gchar *xmlBuffer = g_strdup_printf(xformat,
458            previewWidth, previewHeight, _("No preview"));
460     //g_message("%s\n", xmlBuffer);
462     //now show it!
463     setFromMem(xmlBuffer);
464     g_free(xmlBuffer);
465     showingNoPreview = true;
469 void SVGPreview::showTooLarge(long fileLength)
472     //Arbitrary size of svg doc -- rather 'portrait' shaped
473     gint previewWidth  = 300;
474     gint previewHeight = 600;
476     //Our template.  Modify to taste
477     gchar const *xformat =
478           "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
479           "<svg\n"
480           "xmlns=\"http://www.w3.org/2000/svg\"\n"
481           "xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
482           "width=\"%d\" height=\"%d\">\n"
483           "<g transform=\"translate(-170,24.27184)\" style=\"opacity:0.12\">\n"
484           "<path\n"
485           "style=\"font-size:12;fill:#ffffff;fill-rule:evenodd;stroke:#000000;stroke-width:0.936193pt\"\n"
486           "d=\"M 397.64309 320.25301 L 280.39197 282.517 L 250.74227 124.83447 L 345.08225 "
487           "29.146783 L 393.59996 46.667064 L 483.89679 135.61619 L 397.64309 320.25301 z \"\n"
488           "id=\"whiteSpace\" />\n"
489           "<path\n"
490           "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
491           "d=\"M 476.95792 339.17168 C 495.78197 342.93607 499.54842 356.11361 495.78197 359.87802 "
492           "C 492.01856 363.6434 482.6065 367.40781 475.07663 361.76014 C 467.54478 "
493           "356.11361 467.54478 342.93607 476.95792 339.17168 z \"\n"
494           "id=\"droplet01\" />\n"
495           "<path\n"
496           "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
497           "d=\"M 286.46194 340.42914 C 284.6277 340.91835 269.30405 327.71337 257.16909 333.8338 "
498           "C 245.03722 339.95336 236.89276 353.65666 248.22676 359.27982 C 259.56184 364.90298 "
499           "267.66433 358.41867 277.60113 351.44119 C 287.53903 344.46477 "
500           "287.18046 343.1206 286.46194 340.42914 z \"\n"
501           "id=\"droplet02\" />\n"
502           "<path\n"
503           "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
504           "d=\"M 510.35756 306.92856 C 520.59494 304.36879 544.24333 306.92856 540.47688 321.98634 "
505           "C 536.71354 337.04806 504.71297 331.39827 484.00371 323.87156 C 482.12141 "
506           "308.81083 505.53237 308.13423 510.35756 306.92856 z \"\n"
507           "id=\"droplet03\" />\n"
508           "<path\n"
509           "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
510           "d=\"M 359.2403 21.362537 C 347.92693 21.362537 336.6347 25.683095 327.96556 34.35223 "
511           "L 173.87387 188.41466 C 165.37697 196.9114 161.1116 207.95813 160.94269 219.04577 "
512           "L 160.88418 219.04577 C 160.88418 219.08524 160.94076 219.12322 160.94269 219.16279 "
513           "C 160.94033 219.34888 160.88418 219.53256 160.88418 219.71865 L 161.14748 219.71865 "
514           "C 164.0966 230.93917 240.29699 245.24198 248.79866 253.74346 C 261.63771 266.58263 "
515           "199.5652 276.01151 212.4041 288.85074 C 225.24316 301.68979 289.99433 313.6933 302.8346 "
516           "326.53254 C 315.67368 339.37161 276.5961 353.04289 289.43532 365.88196 C 302.27439 "
517           "378.72118 345.40201 362.67257 337.5908 396.16198 C 354.92909 413.50026 391.10302 "
518           "405.2208 415.32417 387.88252 C 428.16323 375.04345 390.6948 376.17577 403.53397 "
519           "363.33668 C 416.37304 350.49745 448.78128 350.4282 476.08902 319.71589 C 465.09739 "
520           "302.62116 429.10801 295.34136 441.94719 282.50217 C 454.78625 269.66311 479.74708 "
521           "276.18423 533.60644 251.72479 C 559.89837 239.78398 557.72636 230.71459 557.62567 "
522           "219.71865 C 557.62356 219.48727 557.62567 219.27892 557.62567 219.04577 L 557.56716 "
523           "219.04577 C 557.3983 207.95812 553.10345 196.9114 544.60673 188.41466 L 390.54428 "
524           "34.35223 C 381.87515 25.683095 370.55366 21.362537 359.2403 21.362537 z M 357.92378 "
525           "41.402939 C 362.95327 41.533963 367.01541 45.368018 374.98006 50.530832 L 447.76915 "
526           "104.50827 C 448.56596 105.02498 449.32484 105.564 450.02187 106.11735 C 450.7189 106.67062 "
527           "451.3556 107.25745 451.95277 107.84347 C 452.54997 108.42842 453.09281 109.01553 453.59111 "
528           "109.62808 C 454.08837 110.24052 454.53956 110.86661 454.93688 111.50048 C 455.33532 112.13538 "
529           "455.69164 112.78029 455.9901 113.43137 C 456.28877 114.08363 456.52291 114.75639 456.7215 "
530           "115.42078 C 456.92126 116.08419 457.08982 116.73973 457.18961 117.41019 C 457.28949 "
531           "118.08184 457.33588 118.75535 457.33588 119.42886 L 414.21245 98.598549 L 409.9118 "
532           "131.16055 L 386.18512 120.04324 L 349.55654 144.50131 L 335.54288 96.1703 L 317.4919 "
533           "138.4453 L 267.08369 143.47735 L 267.63956 121.03795 C 267.63956 115.64823 296.69685 "
534           "77.915899 314.39075 68.932902 L 346.77721 45.674327 C 351.55594 42.576634 354.90608 "
535           "41.324327 357.92378 41.402939 z M 290.92738 261.61333 C 313.87149 267.56365 339.40299 "
536           "275.37038 359.88393 275.50997 L 360.76161 284.72563 C 343.2235 282.91785 306.11346 "
537           "274.45012 297.36372 269.98057 L 290.92738 261.61333 z \"\n"
538           "id=\"mountainDroplet\" />\n"
539           "</g>\n"
540           "<text xml:space=\"preserve\"\n"
541           "style=\"font-size:32.000000;font-style:normal;font-variant:normal;font-weight:bold;"
542           "font-stretch:normal;fill:#000000;fill-opacity:1.0000000;stroke:none;stroke-width:1.0000000pt;"
543           "stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1.0000000;"
544           "font-family:Bitstream Vera Sans;text-anchor:middle;writing-mode:lr\"\n"
545           "x=\"170\" y=\"215\">%5.1f MB</text>\n"
546           "<text xml:space=\"preserve\"\n"
547           "style=\"font-size:24.000000;font-style:normal;font-variant:normal;font-weight:bold;"
548           "font-stretch:normal;fill:#000000;fill-opacity:1.0000000;stroke:none;stroke-width:1.0000000pt;"
549           "stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1.0000000;"
550           "font-family:Bitstream Vera Sans;text-anchor:middle;writing-mode:lr\"\n"
551           "x=\"180\" y=\"245\">%s</text>\n"
552           "</svg>\n\n";
554     //Fill in the template
555     double floatFileLength = ((double)fileLength) / 1048576.0;
556     //printf("%ld %f\n", fileLength, floatFileLength);
557     gchar *xmlBuffer = g_strdup_printf(xformat,
558            previewWidth, previewHeight, floatFileLength,
559            _("too large for preview"));
561     //g_message("%s\n", xmlBuffer);
563     //now show it!
564     setFromMem(xmlBuffer);
565     g_free(xmlBuffer);
570 /**
571  * Return true if the string ends with the given suffix
572  */ 
573 static bool
574 hasSuffix(Glib::ustring &str, Glib::ustring &ext)
576     int strLen = str.length();
577     int extLen = ext.length();
578     if (extLen > strLen)
579         return false;
580     int strpos = strLen-1;
581     for (int extpos = extLen-1 ; extpos>=0 ; extpos--, strpos--)
582         {
583         Glib::ustring::value_type ch = str[strpos];
584         if (ch != ext[extpos])
585             {
586             if ( ((ch & 0xff80) != 0) ||
587                  static_cast<Glib::ustring::value_type>( g_ascii_tolower( static_cast<gchar>(0x07f & ch) ) ) != ext[extpos] )
588                 {
589                 return false;
590                 }
591             }
592         }
593     return true;
597 /**
598  * Return true if the image is loadable by Gdk, else false
599  */
600 static bool
601 isValidImageFile(Glib::ustring &fileName)
603     std::vector<Gdk::PixbufFormat>formats = Gdk::Pixbuf::get_formats();
604     for (unsigned int i=0; i<formats.size(); i++)
605         {
606         Gdk::PixbufFormat format = formats[i];
607         std::vector<Glib::ustring>extensions = format.get_extensions();
608         for (unsigned int j=0; j<extensions.size(); j++)
609             {
610             Glib::ustring ext = extensions[j];
611             if (hasSuffix(fileName, ext))
612                 return true;
613             }
614         }
615     return false;
618 bool SVGPreview::set(Glib::ustring &fileName, int dialogType)
621     if (!Glib::file_test(fileName, Glib::FILE_TEST_EXISTS))
622         return false;
624     gchar *fName = (gchar *)fileName.c_str();
625     //g_message("fname:%s\n", fName);
627     if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
628         showNoPreview();
629         return false;
630     }
632     if (Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR))
633         {
634         struct stat info;
635         if (stat(fName, &info))
636             {
637             return FALSE;
638             }
639         long fileLen = info.st_size;
640         if (fileLen > 0x150000L)
641             {
642             showingNoPreview = false;
643             showTooLarge(fileLen);
644             return FALSE;
645             }
646         }
648     Glib::ustring svg = ".svg";
649     Glib::ustring svgz = ".svgz";
651     if ((dialogType == SVG_TYPES || dialogType == IMPORT_TYPES) &&
652            (hasSuffix(fileName, svg) || hasSuffix(fileName, svgz)   )
653         ) {
654         bool retval = setFileName(fileName);
655         showingNoPreview = false;
656         return retval;
657     } else if (isValidImageFile(fileName)) {
658         showImage(fileName);
659         showingNoPreview = false;
660         return true;
661     } else {
662         showNoPreview();
663         return false;
664     }
668 SVGPreview::SVGPreview()
670     if (!INKSCAPE)
671         inkscape_application_init("",false);
672     document = NULL;
673     viewerGtk = NULL;
674     set_size_request(150,150);
675     showingNoPreview = false;
678 SVGPreview::~SVGPreview()
687 /*#########################################################################
688 ### F I L E     D I A L O G    B A S E    C L A S S
689 #########################################################################*/
691 /**
692  * This class is the base implementation for the others.  This
693  * reduces redundancies and bugs.
694  */
695 class FileDialogBase : public Gtk::FileChooserDialog
697 public:
699     /**
700      *
701      */
702     FileDialogBase(const Glib::ustring &title) :
703                         Gtk::FileChooserDialog(title)
704         {
705         }
707     /**
708      *
709      */
710     FileDialogBase(const Glib::ustring &title,
711                    Gtk::FileChooserAction dialogType) :
712                    Gtk::FileChooserDialog(title, dialogType)
713         {
714         }
716     /**
717      *
718      */
719     virtual ~FileDialogBase()
720         {}
722 };
726 /*#########################################################################
727 ### F I L E    O P E N
728 #########################################################################*/
730 /**
731  * Our implementation class for the FileOpenDialog interface..
732  */
733 class FileOpenDialogImpl : public FileOpenDialog, public FileDialogBase
735 public:
737     FileOpenDialogImpl(const Glib::ustring &dir,
738                        FileDialogType fileTypes,
739                        const Glib::ustring &title);
741     virtual ~FileOpenDialogImpl();
743     bool show();
745     Inkscape::Extension::Extension *getSelectionType();
747     Glib::ustring getFilename();
749     std::vector<Glib::ustring> getFilenames ();
751 protected:
755 private:
758     /**
759      * What type of 'open' are we? (open, import, place, etc)
760      */
761     FileDialogType dialogType;
763     /**
764      * Our svg preview widget
765      */
766     SVGPreview svgPreview;
768     /**
769      * Callback for seeing if the preview needs to be drawn
770      */
771     void updatePreviewCallback();
773     /**
774      *  Create a filter menu for this type of dialog
775      */
776     void createFilterMenu();
778     /**
779      * Filter name->extension lookup
780      */
781     std::map<Glib::ustring, Inkscape::Extension::Extension *> extensionMap;
783     /**
784      * The extension to use to write this file
785      */
786     Inkscape::Extension::Extension *extension;
788     /**
789      * Filename that was given
790      */
791     Glib::ustring myFilename;
793 };
799 /**
800  * Callback for checking if the preview needs to be redrawn
801  */
802 void FileOpenDialogImpl::updatePreviewCallback()
804     Glib::ustring fileName = get_preview_filename();
805     if (fileName.length() < 1)
806         return;
807     svgPreview.set(fileName, dialogType);
816 void FileOpenDialogImpl::createFilterMenu()
818     //patterns added dynamically below
819     Gtk::FileFilter allImageFilter;
820     allImageFilter.set_name(_("All Images"));
821     extensionMap[Glib::ustring(_("All Images"))]=NULL;
822     add_filter(allImageFilter);
824     Gtk::FileFilter allFilter;
825     allFilter.set_name(_("All Files"));
826     extensionMap[Glib::ustring(_("All Files"))]=NULL;
827     allFilter.add_pattern("*");
828     add_filter(allFilter);
830     //patterns added dynamically below
831     Gtk::FileFilter allInkscapeFilter;
832     allInkscapeFilter.set_name(_("All Inkscape Files"));
833     extensionMap[Glib::ustring(_("All Inkscape Files"))]=NULL;
834     add_filter(allInkscapeFilter);
836     Inkscape::Extension::DB::InputList extension_list;
837     Inkscape::Extension::db.get_input_list(extension_list);
839     for (Inkscape::Extension::DB::InputList::iterator current_item = extension_list.begin();
840          current_item != extension_list.end(); current_item++)
841     {
842         Inkscape::Extension::Input * imod = *current_item;
844         // FIXME: would be nice to grey them out instead of not listing them
845         if (imod->deactivated()) continue;
847         Glib::ustring upattern("*");
848         Glib::ustring extension = imod->get_extension();
849         fileDialogExtensionToPattern(upattern, extension);
851         Gtk::FileFilter filter;
852         Glib::ustring uname(_(imod->get_filetypename()));
853         filter.set_name(uname);
854         filter.add_pattern(upattern);
855         add_filter(filter);
856         extensionMap[uname] = imod;
858         //g_message("ext %s:%s '%s'\n", ioext->name, ioext->mimetype, upattern.c_str());
859         allInkscapeFilter.add_pattern(upattern);
860         if ( strncmp("image", imod->get_mimetype(), 5)==0 )
861             allImageFilter.add_pattern(upattern);
862     }
864     return;
869 /**
870  * Constructor.  Not called directly.  Use the factory.
871  */
872 FileOpenDialogImpl::FileOpenDialogImpl(const Glib::ustring &dir,
873                                        FileDialogType fileTypes,
874                                        const Glib::ustring &title) :
875                                        FileDialogBase(title)
879     /* One file at a time */
880     /* And also Multiple Files */
881     set_select_multiple(true);
883     /* Initalize to Autodetect */
884     extension = NULL;
885     /* No filename to start out with */
886     myFilename = "";
888     /* Set our dialog type (open, import, etc...)*/
889     dialogType = fileTypes;
892     /* Set the pwd and/or the filename */
893     if (dir.size() > 0)
894         {
895         Glib::ustring udir(dir);
896         Glib::ustring::size_type len = udir.length();
897         // leaving a trailing backslash on the directory name leads to the infamous
898         // double-directory bug on win32
899         if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
900         set_current_folder(udir.c_str());
901         }
903     //###### Add the file types menu
904     createFilterMenu();
906     //###### Add a preview widget
907     set_preview_widget(svgPreview);
908     set_preview_widget_active(true);
909     set_use_preview_label (false);
911     //Catch selection-changed events, so we can adjust the text widget
912     signal_update_preview().connect(
913          sigc::mem_fun(*this, &FileOpenDialogImpl::updatePreviewCallback) );
915     add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
916     set_default(*add_button(Gtk::Stock::OPEN,   Gtk::RESPONSE_OK));
924 /**
925  * Public factory.  Called by file.cpp, among others.
926  */
927 FileOpenDialog *FileOpenDialog::create(const Glib::ustring &path,
928                                        FileDialogType fileTypes,
929                                        const Glib::ustring &title)
931     FileOpenDialog *dialog = new FileOpenDialogImpl(path, fileTypes, title);
932     return dialog;
938 /**
939  * Destructor
940  */
941 FileOpenDialogImpl::~FileOpenDialogImpl()
947 /**
948  * Show this dialog modally.  Return true if user hits [OK]
949  */
950 bool
951 FileOpenDialogImpl::show()
953     Glib::ustring s = Glib::filename_to_utf8 (get_current_folder());
954     if (s.length() == 0) 
955         s = getcwd (NULL, 0);
956     set_current_folder(Glib::filename_from_utf8(s)); //hack to force initial dir listing
957     set_modal (TRUE);                      //Window
958     sp_transientize((GtkWidget *)gobj());  //Make transient
959     gint b = run();                        //Dialog
960     svgPreview.showNoPreview();
961     hide();
963     if (b == Gtk::RESPONSE_OK)
964         {
965         //This is a hack, to avoid the warning messages that
966         //Gtk::FileChooser::get_filter() returns
967         //should be:  Gtk::FileFilter *filter = get_filter();
968         GtkFileChooser *gtkFileChooser = Gtk::FileChooser::gobj();
969         GtkFileFilter *filter = gtk_file_chooser_get_filter(gtkFileChooser);
970         if (filter)
971             {
972             //Get which extension was chosen, if any
973             extension = extensionMap[gtk_file_filter_get_name(filter)];
974             }
975         myFilename = get_filename();
976         return TRUE;
977         }
978     else
979        {
980        return FALSE;
981        }
987 /**
988  * Get the file extension type that was selected by the user. Valid after an [OK]
989  */
990 Inkscape::Extension::Extension *
991 FileOpenDialogImpl::getSelectionType()
993     return extension;
997 /**
998  * Get the file name chosen by the user.   Valid after an [OK]
999  */
1000 Glib::ustring
1001 FileOpenDialogImpl::getFilename (void)
1003     return g_strdup(myFilename.c_str());
1007 /**
1008  * To Get Multiple filenames selected at-once.
1009  */
1010 std::vector<Glib::ustring>FileOpenDialogImpl::getFilenames()
1011 {    
1012     std::vector<Glib::ustring> result = get_filenames();
1013     return result;
1021 //########################################################################
1022 //# F I L E    S A V E
1023 //########################################################################
1025 class FileType
1027     public:
1028     FileType() {}
1029     ~FileType() {}
1030     Glib::ustring name;
1031     Glib::ustring pattern;
1032     Inkscape::Extension::Extension *extension;
1033 };
1035 /**
1036  * Our implementation of the FileSaveDialog interface.
1037  */
1038 class FileSaveDialogImpl : public FileSaveDialog, public FileDialogBase
1041 public:
1042     FileSaveDialogImpl(const Glib::ustring &dir,
1043                        FileDialogType fileTypes,
1044                        const Glib::ustring &title,
1045                        const Glib::ustring &default_key);
1047     virtual ~FileSaveDialogImpl();
1049     bool show();
1051     Inkscape::Extension::Extension *getSelectionType();
1053     Glib::ustring getFilename();
1055     void change_title(const Glib::ustring& title);
1056     void change_path(const Glib::ustring& dir);
1059 private:
1061     /**
1062      * What type of 'open' are we? (save, export, etc)
1063      */
1064     FileDialogType dialogType;
1066     /**
1067      * Our svg preview widget
1068      */
1069     SVGPreview svgPreview;
1071     /**
1072      * Fix to allow the user to type the file name
1073      */
1074     Gtk::Entry *fileNameEntry;
1076     /**
1077      * Callback for seeing if the preview needs to be drawn
1078      */
1079     void updatePreviewCallback();
1083     /**
1084      * Allow the specification of the output file type
1085      */
1086     Gtk::HBox fileTypeBox;
1088     /**
1089      * Allow the specification of the output file type
1090      */
1091     Gtk::ComboBoxText fileTypeComboBox;
1094     /**
1095      *  Data mirror of the combo box
1096      */
1097     std::vector<FileType> fileTypes;
1099     //# Child widgets
1100     Gtk::CheckButton fileTypeCheckbox;
1103     /**
1104      * Callback for user input into fileNameEntry
1105      */
1106     void fileTypeChangedCallback();
1108     /**
1109      *  Create a filter menu for this type of dialog
1110      */
1111     void createFileTypeMenu();
1114     bool append_extension;
1116     /**
1117      * The extension to use to write this file
1118      */
1119     Inkscape::Extension::Extension *extension;
1121     /**
1122      * Callback for user input into fileNameEntry
1123      */
1124     void fileNameEntryChangedCallback();
1126     /**
1127      * Filename that was given
1128      */
1129     Glib::ustring myFilename;
1130 };
1137 /**
1138  * Callback for checking if the preview needs to be redrawn
1139  */
1140 void FileSaveDialogImpl::updatePreviewCallback()
1142     Glib::ustring fileName = get_preview_filename();
1143     if (!fileName.c_str())
1144         return;
1145     bool retval = svgPreview.set(fileName, dialogType);
1146     set_preview_widget_active(retval);
1151 /**
1152  * Callback for fileNameEntry widget
1153  */
1154 void FileSaveDialogImpl::fileNameEntryChangedCallback()
1156     if (!fileNameEntry)
1157         return;
1159     Glib::ustring fileName = fileNameEntry->get_text();
1160     if (!Glib::get_charset()) //If we are not utf8
1161         fileName = Glib::filename_to_utf8(fileName);
1163     //g_message("User hit return.  Text is '%s'\n", fileName.c_str());
1165     if (!Glib::path_is_absolute(fileName)) {
1166         //try appending to the current path
1167         // not this way: fileName = get_current_folder() + "/" + fileName;
1168         std::vector<Glib::ustring> pathSegments;
1169         pathSegments.push_back( get_current_folder() );
1170         pathSegments.push_back( fileName );
1171         fileName = Glib::build_filename(pathSegments);
1172     }
1174     //g_message("path:'%s'\n", fileName.c_str());
1176     if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
1177         set_current_folder(fileName);
1178     } else if (/*Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR)*/1) {
1179         //dialog with either (1) select a regular file or (2) cd to dir
1180         //simulate an 'OK'
1181         set_filename(fileName);
1182         response(Gtk::RESPONSE_OK);
1183     }
1188 /**
1189  * Callback for fileNameEntry widget
1190  */
1191 void FileSaveDialogImpl::fileTypeChangedCallback()
1193     int sel = fileTypeComboBox.get_active_row_number();
1194     if (sel<0 || sel >= (int)fileTypes.size())
1195         return;
1196     FileType type = fileTypes[sel];
1197     //g_message("selected: %s\n", type.name.c_str());
1198     Gtk::FileFilter filter;
1199     filter.add_pattern(type.pattern);
1200     set_filter(filter);
1205 void FileSaveDialogImpl::createFileTypeMenu()
1207     Inkscape::Extension::DB::OutputList extension_list;
1208     Inkscape::Extension::db.get_output_list(extension_list);
1210     for (Inkscape::Extension::DB::OutputList::iterator current_item = extension_list.begin();
1211          current_item != extension_list.end(); current_item++)
1212     {
1213         Inkscape::Extension::Output * omod = *current_item;
1215         // FIXME: would be nice to grey them out instead of not listing them
1216         if (omod->deactivated()) continue;
1218         FileType type;
1219         type.name     = (_(omod->get_filetypename()));
1220         type.pattern  = "*";
1221         Glib::ustring extension = omod->get_extension();
1222         fileDialogExtensionToPattern (type.pattern, extension);
1223         type.extension= omod;
1224         fileTypeComboBox.append_text(type.name);
1225         fileTypes.push_back(type);
1226     }
1228     //#Let user choose
1229     FileType guessType;
1230     guessType.name = _("Guess from extension");
1231     guessType.pattern = "*";
1232     guessType.extension = NULL;
1233     fileTypeComboBox.append_text(guessType.name);
1234     fileTypes.push_back(guessType);
1237     fileTypeComboBox.set_active(0);
1238     fileTypeChangedCallback(); //call at least once to set the filter
1243 /**
1244  * Constructor
1245  */
1246 FileSaveDialogImpl::FileSaveDialogImpl(const Glib::ustring &dir,
1247             FileDialogType fileTypes,
1248             const Glib::ustring &title,
1249             const Glib::ustring &default_key) :
1250             FileDialogBase(title, Gtk::FILE_CHOOSER_ACTION_SAVE)
1252     append_extension = (bool)prefs_get_int_attribute("dialogs.save_as",
1253                                                   "append_extension", 1);
1255     /* One file at a time */
1256     set_select_multiple(false);
1258     /* Initalize to Autodetect */
1259     extension = NULL;
1260     /* No filename to start out with */
1261     myFilename = "";
1263     /* Set our dialog type (save, export, etc...)*/
1264     dialogType = fileTypes;
1266     /* Set the pwd and/or the filename */
1267     if (dir.size() > 0)
1268         {
1269         Glib::ustring udir(dir);
1270         Glib::ustring::size_type len = udir.length();
1271         // leaving a trailing backslash on the directory name leads to the infamous
1272         // double-directory bug on win32
1273         if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
1274         myFilename = udir;
1275         }
1277     //###### Add the file types menu
1278     //createFilterMenu();
1280     //###### Do we want the .xxx extension automatically added?
1281     fileTypeCheckbox.set_label(Glib::ustring(_("Append filename extension automatically")));
1282     fileTypeCheckbox.set_active(append_extension);
1284     fileTypeBox.pack_start(fileTypeCheckbox);
1285     createFileTypeMenu();
1286     fileTypeComboBox.set_size_request(200,40);
1287     fileTypeComboBox.signal_changed().connect(
1288          sigc::mem_fun(*this, &FileSaveDialogImpl::fileTypeChangedCallback) );
1290     fileTypeBox.pack_start(fileTypeComboBox);
1292     set_extra_widget(fileTypeBox);
1293     //get_vbox()->pack_start(fileTypeBox, false, false, 0);
1294     //get_vbox()->reorder_child(fileTypeBox, 2);
1296     //###### Add a preview widget
1297     set_preview_widget(svgPreview);
1298     set_preview_widget_active(true);
1299     set_use_preview_label (false);
1301     //Catch selection-changed events, so we can adjust the text widget
1302     signal_update_preview().connect(
1303          sigc::mem_fun(*this, &FileSaveDialogImpl::updatePreviewCallback) );
1306     //Let's do some customization
1307     fileNameEntry = NULL;
1308     Gtk::Container *cont = get_toplevel();
1309     std::vector<Gtk::Entry *> entries;
1310     findEntryWidgets(cont, entries);
1311     //g_message("Found %d entry widgets\n", entries.size());
1312     if (entries.size() >=1 )
1313         {
1314         //Catch when user hits [return] on the text field
1315         fileNameEntry = entries[0];
1316         fileNameEntry->signal_activate().connect(
1317              sigc::mem_fun(*this, &FileSaveDialogImpl::fileNameEntryChangedCallback) );
1318         }
1320     //Let's do more customization
1321     std::vector<Gtk::Expander *> expanders;
1322     findExpanderWidgets(cont, expanders);
1323     //g_message("Found %d expander widgets\n", expanders.size());
1324     if (expanders.size() >=1 )
1325         {
1326         //Always show the file list
1327         Gtk::Expander *expander = expanders[0];
1328         expander->set_expanded(true);
1329         }
1332     //if (extension == NULL)
1333     //    checkbox.set_sensitive(FALSE);
1335     add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1336     set_default(*add_button(Gtk::Stock::SAVE,   Gtk::RESPONSE_OK));
1338     show_all_children();
1343 /**
1344  * Public factory method.  Used in file.cpp
1345  */
1346 FileSaveDialog *FileSaveDialog::create(const Glib::ustring &path,
1347                                        FileDialogType fileTypes,
1348                                        const Glib::ustring &title,
1349                                        const Glib::ustring &default_key)
1351     FileSaveDialog *dialog = new FileSaveDialogImpl(path, fileTypes, title, default_key);
1352     return dialog;
1359 /**
1360  * Destructor
1361  */
1362 FileSaveDialogImpl::~FileSaveDialogImpl()
1368 /**
1369  * Show this dialog modally.  Return true if user hits [OK]
1370  */
1371 bool
1372 FileSaveDialogImpl::show()
1374     change_path(myFilename);
1375     set_modal (TRUE);                      //Window
1376     sp_transientize((GtkWidget *)gobj());  //Make transient
1377     gint b = run();                        //Dialog
1378     svgPreview.showNoPreview();
1379     hide();
1381     if (b == Gtk::RESPONSE_OK)
1382         {
1383         int sel = fileTypeComboBox.get_active_row_number ();
1384         if (sel>=0 && sel< (int)fileTypes.size())
1385             {
1386             FileType &type = fileTypes[sel];
1387             extension = type.extension;
1388             }
1389         myFilename = get_filename();
1391         /*
1393         // FIXME: Why do we have more code
1395         append_extension = checkbox.get_active();
1396         prefs_set_int_attribute("dialogs.save_as", "append_extension", append_extension);
1397         prefs_set_string_attribute("dialogs.save_as", "default",
1398                   ( extension != NULL ? extension->get_id() : "" ));
1399         */
1400         return TRUE;
1401         }
1402     else
1403         {
1404         return FALSE;
1405         }
1409 /**
1410  * Get the file extension type that was selected by the user. Valid after an [OK]
1411  */
1412 Inkscape::Extension::Extension *
1413 FileSaveDialogImpl::getSelectionType()
1415     return extension;
1419 /**
1420  * Get the file name chosen by the user.   Valid after an [OK]
1421  */
1422 Glib::ustring
1423 FileSaveDialogImpl::getFilename()
1425     return myFilename;
1429 void 
1430 FileSaveDialogImpl::change_title(const Glib::ustring& title)
1432     this->set_title(title);
1435 /**
1436   * Change the default save path location.
1437   */
1438 void 
1439 FileSaveDialogImpl::change_path(const Glib::ustring& path)
1441     myFilename = path;
1442     if (Glib::file_test(myFilename, Glib::FILE_TEST_IS_DIR)) {
1443         //fprintf(stderr,"set_current_folder(%s)\n",myFilename.c_str());
1444         set_current_folder(myFilename);
1445     } else {
1446         //fprintf(stderr,"set_filename(%s)\n",myFilename.c_str());
1447         set_filename(myFilename);
1448         Glib::ustring basename = Glib::path_get_basename(myFilename);
1449         //fprintf(stderr,"set_current_name(%s)\n",basename.c_str());
1450         set_current_name(basename);
1451     }
1459 //########################################################################
1460 //# F I L E     E X P O R T
1461 //########################################################################
1464 /**
1465  * Our implementation of the FileExportDialog interface.
1466  */
1467 class FileExportDialogImpl : public FileExportDialog, public FileDialogBase
1470 public:
1471     FileExportDialogImpl(const Glib::ustring &dir,
1472                        FileDialogType fileTypes,
1473                        const Glib::ustring &title,
1474                        const Glib::ustring &default_key);
1476     virtual ~FileExportDialogImpl();
1478     bool show();
1480     Inkscape::Extension::Extension *getSelectionType();
1482     Glib::ustring getFilename();
1485     /**
1486      * Return the scope of the export.  One of the enumerated types
1487      * in ScopeType     
1488      */
1489     ScopeType getScope()
1490         { 
1491         if (pageButton.get_active())
1492             return SCOPE_PAGE;
1493         else if (selectionButton.get_active())
1494             return SCOPE_SELECTION;
1495         else if (customButton.get_active())
1496             return SCOPE_CUSTOM;
1497         else
1498             return SCOPE_DOCUMENT;
1500         }
1501     
1502     /**
1503      * Return left side of the exported region
1504      */
1505     double getSourceX()
1506         { return sourceX0Spinner.getValue(); }
1507     
1508     /**
1509      * Return the top of the exported region
1510      */
1511     double getSourceY()
1512         { return sourceY1Spinner.getValue(); }
1513     
1514     /**
1515      * Return the width of the exported region
1516      */
1517     double getSourceWidth()
1518         { return sourceWidthSpinner.getValue(); }
1519     
1520     /**
1521      * Return the height of the exported region
1522      */
1523     double getSourceHeight()
1524         { return sourceHeightSpinner.getValue(); }
1526     /**
1527      * Return the units of the coordinates of exported region
1528      */
1529     Glib::ustring getSourceUnits()
1530         { return sourceUnitsSpinner.getUnitAbbr(); }
1532     /**
1533      * Return the width of the destination document
1534      */
1535     double getDestinationWidth()
1536         { return destWidthSpinner.getValue(); }
1538     /**
1539      * Return the height of the destination document
1540      */
1541     double getDestinationHeight()
1542         { return destHeightSpinner.getValue(); }
1544     /**
1545      * Return the height of the exported region
1546      */
1547     Glib::ustring getDestinationUnits()
1548         { return destUnitsSpinner.getUnitAbbr(); }
1550     /**
1551      * Return the destination DPI image resulution, if bitmap
1552      */
1553     double getDestinationDPI()
1554         { return destDPISpinner.getValue(); }
1556     /**
1557      * Return whether we should use Cairo for rendering
1558      */
1559     bool getUseCairo()
1560         { return cairoButton.get_active(); }
1562     /**
1563      * Return whether we should use antialiasing
1564      */
1565     bool getUseAntialias()
1566         { return antiAliasButton.get_active(); }
1568     /**
1569      * Return the background color for exporting
1570      */
1571     unsigned long getBackground()
1572         { return backgroundButton.get_color().get_pixel(); }
1574 private:
1576     /**
1577      * What type of 'open' are we? (save, export, etc)
1578      */
1579     FileDialogType dialogType;
1581     /**
1582      * Our svg preview widget
1583      */
1584     SVGPreview svgPreview;
1586     /**
1587      * Fix to allow the user to type the file name
1588      */
1589     Gtk::Entry *fileNameEntry;
1591     /**
1592      * Callback for seeing if the preview needs to be drawn
1593      */
1594     void updatePreviewCallback();
1596     //##########################################
1597     //# EXTRA WIDGET -- SOURCE SIDE
1598     //##########################################
1600     Gtk::Frame            sourceFrame;
1601     Gtk::VBox             sourceBox;
1603     Gtk::HBox             scopeBox;
1604     Gtk::RadioButtonGroup scopeGroup;
1605     Gtk::RadioButton      documentButton;
1606     Gtk::RadioButton      pageButton;
1607     Gtk::RadioButton      selectionButton;
1608     Gtk::RadioButton      customButton;
1610     Gtk::Table                      sourceTable;
1611     Inkscape::UI::Widget::Scalar    sourceX0Spinner;
1612     Inkscape::UI::Widget::Scalar    sourceY0Spinner;
1613     Inkscape::UI::Widget::Scalar    sourceX1Spinner;
1614     Inkscape::UI::Widget::Scalar    sourceY1Spinner;
1615     Inkscape::UI::Widget::Scalar    sourceWidthSpinner;
1616     Inkscape::UI::Widget::Scalar    sourceHeightSpinner;
1617     Inkscape::UI::Widget::UnitMenu  sourceUnitsSpinner;
1620     //##########################################
1621     //# EXTRA WIDGET -- DESTINATION SIDE
1622     //##########################################
1624     Gtk::Frame       destFrame;
1625     Gtk::VBox        destBox;
1627     Gtk::Table                      destTable;
1628     Inkscape::UI::Widget::Scalar    destWidthSpinner;
1629     Inkscape::UI::Widget::Scalar    destHeightSpinner;
1630     Inkscape::UI::Widget::Scalar    destDPISpinner;
1631     Inkscape::UI::Widget::UnitMenu  destUnitsSpinner;
1633     Gtk::HBox        otherOptionBox;
1634     Gtk::CheckButton cairoButton;
1635     Gtk::CheckButton antiAliasButton;
1636     Gtk::ColorButton backgroundButton;
1639     /**
1640      * 'Extra' widget that holds two boxes above
1641      */
1642     Gtk::HBox exportOptionsBox;
1645     //# Child widgets
1646     Gtk::CheckButton fileTypeCheckbox;
1648     /**
1649      * Allow the specification of the output file type
1650      */
1651     Gtk::ComboBoxText fileTypeComboBox;
1654     /**
1655      *  Data mirror of the combo box
1656      */
1657     std::vector<FileType> fileTypes;
1661     /**
1662      * Callback for user input into fileNameEntry
1663      */
1664     void fileTypeChangedCallback();
1666     /**
1667      *  Create a filter menu for this type of dialog
1668      */
1669     void createFileTypeMenu();
1672     bool append_extension;
1674     /**
1675      * The extension to use to write this file
1676      */
1677     Inkscape::Extension::Extension *extension;
1679     /**
1680      * Callback for user input into fileNameEntry
1681      */
1682     void fileNameEntryChangedCallback();
1684     /**
1685      * Filename that was given
1686      */
1687     Glib::ustring myFilename;
1688 };
1695 /**
1696  * Callback for checking if the preview needs to be redrawn
1697  */
1698 void FileExportDialogImpl::updatePreviewCallback()
1700     Glib::ustring fileName = get_preview_filename();
1701     if (!fileName.c_str())
1702         return;
1703     bool retval = svgPreview.set(fileName, dialogType);
1704     set_preview_widget_active(retval);
1709 /**
1710  * Callback for fileNameEntry widget
1711  */
1712 void FileExportDialogImpl::fileNameEntryChangedCallback()
1714     if (!fileNameEntry)
1715         return;
1717     Glib::ustring fileName = fileNameEntry->get_text();
1718     if (!Glib::get_charset()) //If we are not utf8
1719         fileName = Glib::filename_to_utf8(fileName);
1721     //g_message("User hit return.  Text is '%s'\n", fileName.c_str());
1723     if (!Glib::path_is_absolute(fileName)) {
1724         //try appending to the current path
1725         // not this way: fileName = get_current_folder() + "/" + fileName;
1726         std::vector<Glib::ustring> pathSegments;
1727         pathSegments.push_back( get_current_folder() );
1728         pathSegments.push_back( fileName );
1729         fileName = Glib::build_filename(pathSegments);
1730     }
1732     //g_message("path:'%s'\n", fileName.c_str());
1734     if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
1735         set_current_folder(fileName);
1736     } else if (/*Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR)*/1) {
1737         //dialog with either (1) select a regular file or (2) cd to dir
1738         //simulate an 'OK'
1739         set_filename(fileName);
1740         response(Gtk::RESPONSE_OK);
1741     }
1746 /**
1747  * Callback for fileNameEntry widget
1748  */
1749 void FileExportDialogImpl::fileTypeChangedCallback()
1751     int sel = fileTypeComboBox.get_active_row_number();
1752     if (sel<0 || sel >= (int)fileTypes.size())
1753         return;
1754     FileType type = fileTypes[sel];
1755     //g_message("selected: %s\n", type.name.c_str());
1756     Gtk::FileFilter filter;
1757     filter.add_pattern(type.pattern);
1758     set_filter(filter);
1763 void FileExportDialogImpl::createFileTypeMenu()
1765     Inkscape::Extension::DB::OutputList extension_list;
1766     Inkscape::Extension::db.get_output_list(extension_list);
1768     for (Inkscape::Extension::DB::OutputList::iterator current_item = extension_list.begin();
1769          current_item != extension_list.end(); current_item++)
1770     {
1771         Inkscape::Extension::Output * omod = *current_item;
1773         // FIXME: would be nice to grey them out instead of not listing them
1774         if (omod->deactivated()) continue;
1776         FileType type;
1777         type.name     = (_(omod->get_filetypename()));
1778         type.pattern  = "*";
1779         Glib::ustring extension = omod->get_extension();
1780         fileDialogExtensionToPattern (type.pattern, extension);
1781         type.extension= omod;
1782         fileTypeComboBox.append_text(type.name);
1783         fileTypes.push_back(type);
1784     }
1786     //#Let user choose
1787     FileType guessType;
1788     guessType.name = _("Guess from extension");
1789     guessType.pattern = "*";
1790     guessType.extension = NULL;
1791     fileTypeComboBox.append_text(guessType.name);
1792     fileTypes.push_back(guessType);
1795     fileTypeComboBox.set_active(0);
1796     fileTypeChangedCallback(); //call at least once to set the filter
1800 /**
1801  * Constructor
1802  */
1803 FileExportDialogImpl::FileExportDialogImpl(const Glib::ustring &dir,
1804             FileDialogType fileTypes,
1805             const Glib::ustring &title,
1806             const Glib::ustring &default_key) :
1807             FileDialogBase(title, Gtk::FILE_CHOOSER_ACTION_SAVE),
1808             sourceX0Spinner("X0",         _("Source left bound")),
1809             sourceY0Spinner("Y0",         _("Source top bound")),
1810             sourceX1Spinner("X1",         _("Source right bound")),
1811             sourceY1Spinner("Y1",         _("Source bottom bound")),
1812             sourceWidthSpinner("Width",   _("Source width")),
1813             sourceHeightSpinner("Height", _("Source height")),
1814             destWidthSpinner("Width",     _("Destination width")),
1815             destHeightSpinner("Height",   _("Destination height")),
1816             destDPISpinner("DPI",         _("Dots per inch resolution"))
1818     append_extension = (bool)prefs_get_int_attribute("dialogs.save_as", "append_extension", 1);
1820     /* One file at a time */
1821     set_select_multiple(false);
1823     /* Initalize to Autodetect */
1824     extension = NULL;
1825     /* No filename to start out with */
1826     myFilename = "";
1828     /* Set our dialog type (save, export, etc...)*/
1829     dialogType = fileTypes;
1831     /* Set the pwd and/or the filename */
1832     if (dir.size()>0)
1833         {
1834         Glib::ustring udir(dir);
1835         Glib::ustring::size_type len = udir.length();
1836         // leaving a trailing backslash on the directory name leads to the infamous
1837         // double-directory bug on win32
1838         if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
1839         set_current_folder(udir.c_str());
1840         }
1842     //#########################################
1843     //## EXTRA WIDGET -- SOURCE SIDE
1844     //#########################################
1846     //##### Export options buttons/spinners, etc
1847     documentButton.set_label(_("Document"));
1848     scopeBox.pack_start(documentButton);
1849     scopeGroup = documentButton.get_group();
1851     pageButton.set_label(_("Page"));
1852     pageButton.set_group(scopeGroup);
1853     scopeBox.pack_start(pageButton);
1855     selectionButton.set_label(_("Selection"));
1856     selectionButton.set_group(scopeGroup);
1857     scopeBox.pack_start(selectionButton);
1859     customButton.set_label(_("Custom"));
1860     customButton.set_group(scopeGroup);
1861     scopeBox.pack_start(customButton);
1863     sourceBox.pack_start(scopeBox);
1867     //dimension buttons
1868     sourceTable.resize(3,3);
1869     sourceTable.attach(sourceX0Spinner,     0,1,0,1);
1870     sourceTable.attach(sourceY0Spinner,     1,2,0,1);
1871     sourceUnitsSpinner.setUnitType(UNIT_TYPE_LINEAR);
1872     sourceTable.attach(sourceUnitsSpinner,  2,3,0,1);
1873     sourceTable.attach(sourceX1Spinner,     0,1,1,2);
1874     sourceTable.attach(sourceY1Spinner,     1,2,1,2);
1875     sourceTable.attach(sourceWidthSpinner,  0,1,2,3);
1876     sourceTable.attach(sourceHeightSpinner, 1,2,2,3);
1878     sourceBox.pack_start(sourceTable);
1879     sourceFrame.set_label(_("Source"));
1880     sourceFrame.add(sourceBox);
1881     exportOptionsBox.pack_start(sourceFrame);
1884     //#########################################
1885     //## EXTRA WIDGET -- SOURCE SIDE
1886     //#########################################
1889     destTable.resize(3,3);
1890     destTable.attach(destWidthSpinner,    0,1,0,1);
1891     destTable.attach(destHeightSpinner,   1,2,0,1);
1892     destUnitsSpinner.setUnitType(UNIT_TYPE_LINEAR);
1893     destTable.attach(destUnitsSpinner,    2,3,0,1);
1894     destTable.attach(destDPISpinner,      0,1,1,2);
1896     destBox.pack_start(destTable);
1899     cairoButton.set_label(_("Cairo"));
1900     otherOptionBox.pack_start(cairoButton);
1902     antiAliasButton.set_label(_("Antialias"));
1903     otherOptionBox.pack_start(antiAliasButton);
1905     backgroundButton.set_label(_("Background"));
1906     otherOptionBox.pack_start(backgroundButton);
1908     destBox.pack_start(otherOptionBox);
1914     //###### File options
1915     //###### Do we want the .xxx extension automatically added?
1916     fileTypeCheckbox.set_label(Glib::ustring(_("Append filename extension automatically")));
1917     fileTypeCheckbox.set_active(append_extension);
1918     destBox.pack_start(fileTypeCheckbox);
1920     //###### File type menu
1921     createFileTypeMenu();
1922     fileTypeComboBox.set_size_request(200,40);
1923     fileTypeComboBox.signal_changed().connect(
1924          sigc::mem_fun(*this, &FileExportDialogImpl::fileTypeChangedCallback) );
1926     destBox.pack_start(fileTypeComboBox);
1928     destFrame.set_label(_("Destination"));
1929     destFrame.add(destBox);
1930     exportOptionsBox.pack_start(destFrame);
1932     //##### Put the two boxes and their parent onto the dialog    
1933     exportOptionsBox.pack_start(sourceFrame);
1934     exportOptionsBox.pack_start(destFrame);
1936     set_extra_widget(exportOptionsBox);
1941     //###### PREVIEW WIDGET
1942     set_preview_widget(svgPreview);
1943     set_preview_widget_active(true);
1944     set_use_preview_label (false);
1946     //Catch selection-changed events, so we can adjust the text widget
1947     signal_update_preview().connect(
1948          sigc::mem_fun(*this, &FileExportDialogImpl::updatePreviewCallback) );
1951     //Let's do some customization
1952     fileNameEntry = NULL;
1953     Gtk::Container *cont = get_toplevel();
1954     std::vector<Gtk::Entry *> entries;
1955     findEntryWidgets(cont, entries);
1956     //g_message("Found %d entry widgets\n", entries.size());
1957     if (entries.size() >=1 )
1958         {
1959         //Catch when user hits [return] on the text field
1960         fileNameEntry = entries[0];
1961         fileNameEntry->signal_activate().connect(
1962              sigc::mem_fun(*this, &FileExportDialogImpl::fileNameEntryChangedCallback) );
1963         }
1965     //Let's do more customization
1966     std::vector<Gtk::Expander *> expanders;
1967     findExpanderWidgets(cont, expanders);
1968     //g_message("Found %d expander widgets\n", expanders.size());
1969     if (expanders.size() >=1 )
1970         {
1971         //Always show the file list
1972         Gtk::Expander *expander = expanders[0];
1973         expander->set_expanded(true);
1974         }
1977     //if (extension == NULL)
1978     //    checkbox.set_sensitive(FALSE);
1980     add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1981     set_default(*add_button(Gtk::Stock::SAVE,   Gtk::RESPONSE_OK));
1983     show_all_children();
1988 /**
1989  * Public factory method.  Used in file.cpp
1990  */
1991 FileExportDialog *FileExportDialog::create(const Glib::ustring &path,
1992                                        FileDialogType fileTypes,
1993                                        const Glib::ustring &title,
1994                                        const Glib::ustring &default_key)
1996     FileExportDialog *dialog = new FileExportDialogImpl(path, fileTypes, title, default_key);
1997     return dialog;
2004 /**
2005  * Destructor
2006  */
2007 FileExportDialogImpl::~FileExportDialogImpl()
2013 /**
2014  * Show this dialog modally.  Return true if user hits [OK]
2015  */
2016 bool
2017 FileExportDialogImpl::show()
2019     Glib::ustring s = Glib::filename_to_utf8 (get_current_folder());
2020     if (s.length() == 0) 
2021         s = getcwd (NULL, 0);
2022     set_current_folder(Glib::filename_from_utf8(s)); //hack to force initial dir listing
2023     set_modal (TRUE);                      //Window
2024     sp_transientize((GtkWidget *)gobj());  //Make transient
2025     gint b = run();                        //Dialog
2026     svgPreview.showNoPreview();
2027     hide();
2029     if (b == Gtk::RESPONSE_OK)
2030         {
2031         int sel = fileTypeComboBox.get_active_row_number ();
2032         if (sel>=0 && sel< (int)fileTypes.size())
2033             {
2034             FileType &type = fileTypes[sel];
2035             extension = type.extension;
2036             }
2037         myFilename = get_filename();
2039         /*
2041         // FIXME: Why do we have more code
2043         append_extension = checkbox.get_active();
2044         prefs_set_int_attribute("dialogs.save_as", "append_extension", append_extension);
2045         prefs_set_string_attribute("dialogs.save_as", "default",
2046                   ( extension != NULL ? extension->get_id() : "" ));
2047         */
2048         return TRUE;
2049         }
2050     else
2051         {
2052         return FALSE;
2053         }
2057 /**
2058  * Get the file extension type that was selected by the user. Valid after an [OK]
2059  */
2060 Inkscape::Extension::Extension *
2061 FileExportDialogImpl::getSelectionType()
2063     return extension;
2067 /**
2068  * Get the file name chosen by the user.   Valid after an [OK]
2069  */
2070 Glib::ustring
2071 FileExportDialogImpl::getFilename()
2073     return myFilename;
2079 } //namespace Dialog
2080 } //namespace UI
2081 } //namespace Inkscape
2084 /*
2085   Local Variables:
2086   mode:c++
2087   c-file-style:"stroustrup"
2088   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2089   indent-tabs-mode:nil
2090   fill-column:99
2091   End:
2092 */
2093 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :