Code

patch 1590039, whitespace
[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      * Fix to allow the user to type the file name
775      */
776     Gtk::Entry fileNameEntry;
778     /**
779      *  Create a filter menu for this type of dialog
780      */
781     void createFilterMenu();
783     /**
784      * Callback for user input into fileNameEntry
785      */
786     void fileNameEntryChangedCallback();
788     /**
789      * Callback for user changing which item is selected on the list
790      */
791     void fileSelectedCallback();
794     /**
795      * Filter name->extension lookup
796      */
797     std::map<Glib::ustring, Inkscape::Extension::Extension *> extensionMap;
799     /**
800      * The extension to use to write this file
801      */
802     Inkscape::Extension::Extension *extension;
804     /**
805      * Filename that was given
806      */
807     Glib::ustring myFilename;
809 };
815 /**
816  * Callback for checking if the preview needs to be redrawn
817  */
818 void FileOpenDialogImpl::updatePreviewCallback()
820     Glib::ustring fileName = get_preview_filename();
821     if (fileName.length() < 1)
822         return;
823     svgPreview.set(fileName, dialogType);
830 /**
831  * Callback for fileNameEntry widget
832  */
833 void FileOpenDialogImpl::fileNameEntryChangedCallback()
835     Glib::ustring rawFileName = fileNameEntry.get_text();
837     Glib::ustring fileName = Glib::filename_from_utf8(rawFileName);
839     //g_message("User hit return.  Text is '%s'\n", fileName.c_str());
841     if (!Glib::path_is_absolute(fileName)) {
842         //try appending to the current path
843         // not this way: fileName = get_current_folder() + "/" + fName;
844         std::vector<Glib::ustring> pathSegments;
845         pathSegments.push_back( get_current_folder() );
846         pathSegments.push_back( fileName );
847         fileName = Glib::build_filename(pathSegments);
848     }
850     //g_message("path:'%s'\n", fName.c_str());
852     if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
853         set_current_folder(fileName);
854     } else if (Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR)) {
855         //dialog with either (1) select a regular file or (2) cd to dir
856         //simulate an 'OK'
857         set_filename(fileName);
858         response(Gtk::RESPONSE_OK);
859     }
866 /**
867  * Callback for fileNameEntry widget
868  */
869 void FileOpenDialogImpl::fileSelectedCallback()
871     Glib::ustring fileName     = get_filename();
872     if (!Glib::get_charset()) //If we are not utf8
873         fileName = Glib::filename_to_utf8(fileName);
874     //g_message("User selected '%s'\n",
875     //       filename().c_str());
877 #ifdef INK_DUMP_FILENAME_CONV
878     ::dump_ustr( get_filename() );
879 #endif
880     fileNameEntry.set_text(fileName);
886 void FileOpenDialogImpl::createFilterMenu()
888     //patterns added dynamically below
889     Gtk::FileFilter allImageFilter;
890     allImageFilter.set_name(_("All Images"));
891     extensionMap[Glib::ustring(_("All Images"))]=NULL;
892     add_filter(allImageFilter);
894     Gtk::FileFilter allFilter;
895     allFilter.set_name(_("All Files"));
896     extensionMap[Glib::ustring(_("All Files"))]=NULL;
897     allFilter.add_pattern("*");
898     add_filter(allFilter);
900     //patterns added dynamically below
901     Gtk::FileFilter allInkscapeFilter;
902     allInkscapeFilter.set_name(_("All Inkscape Files"));
903     extensionMap[Glib::ustring(_("All Inkscape Files"))]=NULL;
904     add_filter(allInkscapeFilter);
906     Inkscape::Extension::DB::InputList extension_list;
907     Inkscape::Extension::db.get_input_list(extension_list);
909     for (Inkscape::Extension::DB::InputList::iterator current_item = extension_list.begin();
910          current_item != extension_list.end(); current_item++)
911     {
912         Inkscape::Extension::Input * imod = *current_item;
914         // FIXME: would be nice to grey them out instead of not listing them
915         if (imod->deactivated()) continue;
917         Glib::ustring upattern("*");
918         Glib::ustring extension = imod->get_extension();
919         fileDialogExtensionToPattern(upattern, extension);
921         Gtk::FileFilter filter;
922         Glib::ustring uname(_(imod->get_filetypename()));
923         filter.set_name(uname);
924         filter.add_pattern(upattern);
925         add_filter(filter);
926         extensionMap[uname] = imod;
928         //g_message("ext %s:%s '%s'\n", ioext->name, ioext->mimetype, upattern.c_str());
929         allInkscapeFilter.add_pattern(upattern);
930         if ( strncmp("image", imod->get_mimetype(), 5)==0 )
931             allImageFilter.add_pattern(upattern);
932     }
934     return;
939 /**
940  * Constructor.  Not called directly.  Use the factory.
941  */
942 FileOpenDialogImpl::FileOpenDialogImpl(const Glib::ustring &dir,
943                                        FileDialogType fileTypes,
944                                        const Glib::ustring &title) :
945                                        FileDialogBase(title)
949     /* One file at a time */
950     /* And also Multiple Files */
951     set_select_multiple(true);
953     /* Initalize to Autodetect */
954     extension = NULL;
955     /* No filename to start out with */
956     myFilename = "";
958     /* Set our dialog type (open, import, etc...)*/
959     dialogType = fileTypes;
962     /* Set the pwd and/or the filename */
963     if (dir.size() > 0)
964         {
965         Glib::ustring udir(dir);
966         Glib::ustring::size_type len = udir.length();
967         // leaving a trailing backslash on the directory name leads to the infamous
968         // double-directory bug on win32
969         if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
970         set_current_folder(udir.c_str());
971         }
973     //###### Add the file types menu
974     createFilterMenu();
976     //###### Add a preview widget
977     set_preview_widget(svgPreview);
978     set_preview_widget_active(true);
979     set_use_preview_label (false);
981     //Catch selection-changed events, so we can adjust the text widget
982     signal_update_preview().connect(
983          sigc::mem_fun(*this, &FileOpenDialogImpl::updatePreviewCallback) );
986     //###### Add a text entry bar, and tie it to file chooser events
987     fileNameEntry.set_text(get_current_folder());
988     set_extra_widget(fileNameEntry);
989     fileNameEntry.grab_focus();
991     //Catch when user hits [return] on the text field
992     fileNameEntry.signal_activate().connect(
993          sigc::mem_fun(*this, &FileOpenDialogImpl::fileNameEntryChangedCallback) );
995     //Catch selection-changed events, so we can adjust the text widget
996     signal_selection_changed().connect(
997          sigc::mem_fun(*this, &FileOpenDialogImpl::fileSelectedCallback) );
999     add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1000     add_button(Gtk::Stock::OPEN,   Gtk::RESPONSE_OK);
1008 /**
1009  * Public factory.  Called by file.cpp, among others.
1010  */
1011 FileOpenDialog *FileOpenDialog::create(const Glib::ustring &path,
1012                                        FileDialogType fileTypes,
1013                                        const Glib::ustring &title)
1015     FileOpenDialog *dialog = new FileOpenDialogImpl(path, fileTypes, title);
1016     return dialog;
1022 /**
1023  * Destructor
1024  */
1025 FileOpenDialogImpl::~FileOpenDialogImpl()
1031 /**
1032  * Show this dialog modally.  Return true if user hits [OK]
1033  */
1034 bool
1035 FileOpenDialogImpl::show()
1037     Glib::ustring s = Glib::filename_to_utf8 (get_current_folder());
1038     if (s.length() == 0) 
1039         s = getcwd (NULL, 0);
1040     set_current_folder(Glib::filename_from_utf8(s)); //hack to force initial dir listing
1041     set_modal (TRUE);                      //Window
1042     sp_transientize((GtkWidget *)gobj());  //Make transient
1043     gint b = run();                        //Dialog
1044     svgPreview.showNoPreview();
1045     hide();
1047     if (b == Gtk::RESPONSE_OK)
1048         {
1049         //This is a hack, to avoid the warning messages that
1050         //Gtk::FileChooser::get_filter() returns
1051         //should be:  Gtk::FileFilter *filter = get_filter();
1052         GtkFileChooser *gtkFileChooser = Gtk::FileChooser::gobj();
1053         GtkFileFilter *filter = gtk_file_chooser_get_filter(gtkFileChooser);
1054         if (filter)
1055             {
1056             //Get which extension was chosen, if any
1057             extension = extensionMap[gtk_file_filter_get_name(filter)];
1058             }
1059         myFilename = get_filename();
1060         return TRUE;
1061         }
1062     else
1063        {
1064        return FALSE;
1065        }
1071 /**
1072  * Get the file extension type that was selected by the user. Valid after an [OK]
1073  */
1074 Inkscape::Extension::Extension *
1075 FileOpenDialogImpl::getSelectionType()
1077     return extension;
1081 /**
1082  * Get the file name chosen by the user.   Valid after an [OK]
1083  */
1084 Glib::ustring
1085 FileOpenDialogImpl::getFilename (void)
1087     return g_strdup(myFilename.c_str());
1091 /**
1092  * To Get Multiple filenames selected at-once.
1093  */
1094 std::vector<Glib::ustring>FileOpenDialogImpl::getFilenames()
1095 {    
1096     std::vector<Glib::ustring> result = get_filenames();
1097     return result;
1105 //########################################################################
1106 //# F I L E    S A V E
1107 //########################################################################
1109 class FileType
1111     public:
1112     FileType() {}
1113     ~FileType() {}
1114     Glib::ustring name;
1115     Glib::ustring pattern;
1116     Inkscape::Extension::Extension *extension;
1117 };
1119 /**
1120  * Our implementation of the FileSaveDialog interface.
1121  */
1122 class FileSaveDialogImpl : public FileSaveDialog, public FileDialogBase
1125 public:
1126     FileSaveDialogImpl(const Glib::ustring &dir,
1127                        FileDialogType fileTypes,
1128                        const Glib::ustring &title,
1129                        const Glib::ustring &default_key);
1131     virtual ~FileSaveDialogImpl();
1133     bool show();
1135     Inkscape::Extension::Extension *getSelectionType();
1137     Glib::ustring getFilename();
1139     void change_title(const Glib::ustring& title);
1140     void change_path(const Glib::ustring& dir);
1143 private:
1145     /**
1146      * What type of 'open' are we? (save, export, etc)
1147      */
1148     FileDialogType dialogType;
1150     /**
1151      * Our svg preview widget
1152      */
1153     SVGPreview svgPreview;
1155     /**
1156      * Fix to allow the user to type the file name
1157      */
1158     Gtk::Entry *fileNameEntry;
1160     /**
1161      * Callback for seeing if the preview needs to be drawn
1162      */
1163     void updatePreviewCallback();
1167     /**
1168      * Allow the specification of the output file type
1169      */
1170     Gtk::HBox fileTypeBox;
1172     /**
1173      * Allow the specification of the output file type
1174      */
1175     Gtk::ComboBoxText fileTypeComboBox;
1178     /**
1179      *  Data mirror of the combo box
1180      */
1181     std::vector<FileType> fileTypes;
1183     //# Child widgets
1184     Gtk::CheckButton fileTypeCheckbox;
1187     /**
1188      * Callback for user input into fileNameEntry
1189      */
1190     void fileTypeChangedCallback();
1192     /**
1193      *  Create a filter menu for this type of dialog
1194      */
1195     void createFileTypeMenu();
1198     bool append_extension;
1200     /**
1201      * The extension to use to write this file
1202      */
1203     Inkscape::Extension::Extension *extension;
1205     /**
1206      * Callback for user input into fileNameEntry
1207      */
1208     void fileNameEntryChangedCallback();
1210     /**
1211      * Filename that was given
1212      */
1213     Glib::ustring myFilename;
1214 };
1221 /**
1222  * Callback for checking if the preview needs to be redrawn
1223  */
1224 void FileSaveDialogImpl::updatePreviewCallback()
1226     Glib::ustring fileName = get_preview_filename();
1227     if (!fileName.c_str())
1228         return;
1229     bool retval = svgPreview.set(fileName, dialogType);
1230     set_preview_widget_active(retval);
1235 /**
1236  * Callback for fileNameEntry widget
1237  */
1238 void FileSaveDialogImpl::fileNameEntryChangedCallback()
1240     if (!fileNameEntry)
1241         return;
1243     Glib::ustring fileName = fileNameEntry->get_text();
1244     if (!Glib::get_charset()) //If we are not utf8
1245         fileName = Glib::filename_to_utf8(fileName);
1247     //g_message("User hit return.  Text is '%s'\n", fileName.c_str());
1249     if (!Glib::path_is_absolute(fileName)) {
1250         //try appending to the current path
1251         // not this way: fileName = get_current_folder() + "/" + fileName;
1252         std::vector<Glib::ustring> pathSegments;
1253         pathSegments.push_back( get_current_folder() );
1254         pathSegments.push_back( fileName );
1255         fileName = Glib::build_filename(pathSegments);
1256     }
1258     //g_message("path:'%s'\n", fileName.c_str());
1260     if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
1261         set_current_folder(fileName);
1262     } else if (/*Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR)*/1) {
1263         //dialog with either (1) select a regular file or (2) cd to dir
1264         //simulate an 'OK'
1265         set_filename(fileName);
1266         response(Gtk::RESPONSE_OK);
1267     }
1272 /**
1273  * Callback for fileNameEntry widget
1274  */
1275 void FileSaveDialogImpl::fileTypeChangedCallback()
1277     int sel = fileTypeComboBox.get_active_row_number();
1278     if (sel<0 || sel >= (int)fileTypes.size())
1279         return;
1280     FileType type = fileTypes[sel];
1281     //g_message("selected: %s\n", type.name.c_str());
1282     Gtk::FileFilter filter;
1283     filter.add_pattern(type.pattern);
1284     set_filter(filter);
1289 void FileSaveDialogImpl::createFileTypeMenu()
1291     Inkscape::Extension::DB::OutputList extension_list;
1292     Inkscape::Extension::db.get_output_list(extension_list);
1294     for (Inkscape::Extension::DB::OutputList::iterator current_item = extension_list.begin();
1295          current_item != extension_list.end(); current_item++)
1296     {
1297         Inkscape::Extension::Output * omod = *current_item;
1299         // FIXME: would be nice to grey them out instead of not listing them
1300         if (omod->deactivated()) continue;
1302         FileType type;
1303         type.name     = (_(omod->get_filetypename()));
1304         type.pattern  = "*";
1305         Glib::ustring extension = omod->get_extension();
1306         fileDialogExtensionToPattern (type.pattern, extension);
1307         type.extension= omod;
1308         fileTypeComboBox.append_text(type.name);
1309         fileTypes.push_back(type);
1310     }
1312     //#Let user choose
1313     FileType guessType;
1314     guessType.name = _("Guess from extension");
1315     guessType.pattern = "*";
1316     guessType.extension = NULL;
1317     fileTypeComboBox.append_text(guessType.name);
1318     fileTypes.push_back(guessType);
1321     fileTypeComboBox.set_active(0);
1322     fileTypeChangedCallback(); //call at least once to set the filter
1327 /**
1328  * Constructor
1329  */
1330 FileSaveDialogImpl::FileSaveDialogImpl(const Glib::ustring &dir,
1331             FileDialogType fileTypes,
1332             const Glib::ustring &title,
1333             const Glib::ustring &default_key) :
1334             FileDialogBase(title, Gtk::FILE_CHOOSER_ACTION_SAVE)
1336     append_extension = (bool)prefs_get_int_attribute("dialogs.save_as",
1337                                                   "append_extension", 1);
1339     /* One file at a time */
1340     set_select_multiple(false);
1342     /* Initalize to Autodetect */
1343     extension = NULL;
1344     /* No filename to start out with */
1345     myFilename = "";
1347     /* Set our dialog type (save, export, etc...)*/
1348     dialogType = fileTypes;
1350     /* Set the pwd and/or the filename */
1351     if (dir.size() > 0)
1352         {
1353         Glib::ustring udir(dir);
1354         Glib::ustring::size_type len = udir.length();
1355         // leaving a trailing backslash on the directory name leads to the infamous
1356         // double-directory bug on win32
1357         if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
1358         myFilename = udir;
1359         }
1361     //###### Add the file types menu
1362     //createFilterMenu();
1364     //###### Do we want the .xxx extension automatically added?
1365     fileTypeCheckbox.set_label(Glib::ustring(_("Append filename extension automatically")));
1366     fileTypeCheckbox.set_active(append_extension);
1368     fileTypeBox.pack_start(fileTypeCheckbox);
1369     createFileTypeMenu();
1370     fileTypeComboBox.set_size_request(200,40);
1371     fileTypeComboBox.signal_changed().connect(
1372          sigc::mem_fun(*this, &FileSaveDialogImpl::fileTypeChangedCallback) );
1374     fileTypeBox.pack_start(fileTypeComboBox);
1376     set_extra_widget(fileTypeBox);
1377     //get_vbox()->pack_start(fileTypeBox, false, false, 0);
1378     //get_vbox()->reorder_child(fileTypeBox, 2);
1380     //###### Add a preview widget
1381     set_preview_widget(svgPreview);
1382     set_preview_widget_active(true);
1383     set_use_preview_label (false);
1385     //Catch selection-changed events, so we can adjust the text widget
1386     signal_update_preview().connect(
1387          sigc::mem_fun(*this, &FileSaveDialogImpl::updatePreviewCallback) );
1390     //Let's do some customization
1391     fileNameEntry = NULL;
1392     Gtk::Container *cont = get_toplevel();
1393     std::vector<Gtk::Entry *> entries;
1394     findEntryWidgets(cont, entries);
1395     //g_message("Found %d entry widgets\n", entries.size());
1396     if (entries.size() >=1 )
1397         {
1398         //Catch when user hits [return] on the text field
1399         fileNameEntry = entries[0];
1400         fileNameEntry->signal_activate().connect(
1401              sigc::mem_fun(*this, &FileSaveDialogImpl::fileNameEntryChangedCallback) );
1402         }
1404     //Let's do more customization
1405     std::vector<Gtk::Expander *> expanders;
1406     findExpanderWidgets(cont, expanders);
1407     //g_message("Found %d expander widgets\n", expanders.size());
1408     if (expanders.size() >=1 )
1409         {
1410         //Always show the file list
1411         Gtk::Expander *expander = expanders[0];
1412         expander->set_expanded(true);
1413         }
1416     //if (extension == NULL)
1417     //    checkbox.set_sensitive(FALSE);
1419     add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1420     add_button(Gtk::Stock::SAVE,   Gtk::RESPONSE_OK);
1422     show_all_children();
1427 /**
1428  * Public factory method.  Used in file.cpp
1429  */
1430 FileSaveDialog *FileSaveDialog::create(const Glib::ustring &path,
1431                                        FileDialogType fileTypes,
1432                                        const Glib::ustring &title,
1433                                        const Glib::ustring &default_key)
1435     FileSaveDialog *dialog = new FileSaveDialogImpl(path, fileTypes, title, default_key);
1436     return dialog;
1443 /**
1444  * Destructor
1445  */
1446 FileSaveDialogImpl::~FileSaveDialogImpl()
1452 /**
1453  * Show this dialog modally.  Return true if user hits [OK]
1454  */
1455 bool
1456 FileSaveDialogImpl::show()
1458     change_path(myFilename);
1459     set_modal (TRUE);                      //Window
1460     sp_transientize((GtkWidget *)gobj());  //Make transient
1461     gint b = run();                        //Dialog
1462     svgPreview.showNoPreview();
1463     hide();
1465     if (b == Gtk::RESPONSE_OK)
1466         {
1467         int sel = fileTypeComboBox.get_active_row_number ();
1468         if (sel>=0 && sel< (int)fileTypes.size())
1469             {
1470             FileType &type = fileTypes[sel];
1471             extension = type.extension;
1472             }
1473         myFilename = get_filename();
1475         /*
1477         // FIXME: Why do we have more code
1479         append_extension = checkbox.get_active();
1480         prefs_set_int_attribute("dialogs.save_as", "append_extension", append_extension);
1481         prefs_set_string_attribute("dialogs.save_as", "default",
1482                   ( extension != NULL ? extension->get_id() : "" ));
1483         */
1484         return TRUE;
1485         }
1486     else
1487         {
1488         return FALSE;
1489         }
1493 /**
1494  * Get the file extension type that was selected by the user. Valid after an [OK]
1495  */
1496 Inkscape::Extension::Extension *
1497 FileSaveDialogImpl::getSelectionType()
1499     return extension;
1503 /**
1504  * Get the file name chosen by the user.   Valid after an [OK]
1505  */
1506 Glib::ustring
1507 FileSaveDialogImpl::getFilename()
1509     return myFilename;
1513 void 
1514 FileSaveDialogImpl::change_title(const Glib::ustring& title)
1516     this->set_title(title);
1519 /**
1520   * Change the default save path location.
1521   */
1522 void 
1523 FileSaveDialogImpl::change_path(const Glib::ustring& path)
1525     myFilename = path;
1526     if (Glib::file_test(myFilename, Glib::FILE_TEST_IS_DIR)) {
1527         //fprintf(stderr,"set_current_folder(%s)\n",myFilename.c_str());
1528         set_current_folder(myFilename);
1529     } else {
1530         //fprintf(stderr,"set_filename(%s)\n",myFilename.c_str());
1531         set_filename(myFilename);
1532         Glib::ustring basename = Glib::path_get_basename(myFilename);
1533         //fprintf(stderr,"set_current_name(%s)\n",basename.c_str());
1534         set_current_name(basename);
1535     }
1543 //########################################################################
1544 //# F I L E     E X P O R T
1545 //########################################################################
1548 /**
1549  * Our implementation of the FileExportDialog interface.
1550  */
1551 class FileExportDialogImpl : public FileExportDialog, public FileDialogBase
1554 public:
1555     FileExportDialogImpl(const Glib::ustring &dir,
1556                        FileDialogType fileTypes,
1557                        const Glib::ustring &title,
1558                        const Glib::ustring &default_key);
1560     virtual ~FileExportDialogImpl();
1562     bool show();
1564     Inkscape::Extension::Extension *getSelectionType();
1566     Glib::ustring getFilename();
1569     /**
1570      * Return the scope of the export.  One of the enumerated types
1571      * in ScopeType     
1572      */
1573     ScopeType getScope()
1574         { 
1575         if (pageButton.get_active())
1576             return SCOPE_PAGE;
1577         else if (selectionButton.get_active())
1578             return SCOPE_SELECTION;
1579         else if (customButton.get_active())
1580             return SCOPE_CUSTOM;
1581         else
1582             return SCOPE_DOCUMENT;
1584         }
1585     
1586     /**
1587      * Return left side of the exported region
1588      */
1589     double getSourceX()
1590         { return sourceX0Spinner.getValue(); }
1591     
1592     /**
1593      * Return the top of the exported region
1594      */
1595     double getSourceY()
1596         { return sourceY1Spinner.getValue(); }
1597     
1598     /**
1599      * Return the width of the exported region
1600      */
1601     double getSourceWidth()
1602         { return sourceWidthSpinner.getValue(); }
1603     
1604     /**
1605      * Return the height of the exported region
1606      */
1607     double getSourceHeight()
1608         { return sourceHeightSpinner.getValue(); }
1610     /**
1611      * Return the units of the coordinates of exported region
1612      */
1613     Glib::ustring getSourceUnits()
1614         { return sourceUnitsSpinner.getUnitAbbr(); }
1616     /**
1617      * Return the width of the destination document
1618      */
1619     double getDestinationWidth()
1620         { return destWidthSpinner.getValue(); }
1622     /**
1623      * Return the height of the destination document
1624      */
1625     double getDestinationHeight()
1626         { return destHeightSpinner.getValue(); }
1628     /**
1629      * Return the height of the exported region
1630      */
1631     Glib::ustring getDestinationUnits()
1632         { return destUnitsSpinner.getUnitAbbr(); }
1634     /**
1635      * Return the destination DPI image resulution, if bitmap
1636      */
1637     double getDestinationDPI()
1638         { return destDPISpinner.getValue(); }
1640     /**
1641      * Return whether we should use Cairo for rendering
1642      */
1643     bool getUseCairo()
1644         { return cairoButton.get_active(); }
1646     /**
1647      * Return whether we should use antialiasing
1648      */
1649     bool getUseAntialias()
1650         { return antiAliasButton.get_active(); }
1652     /**
1653      * Return the background color for exporting
1654      */
1655     unsigned long getBackground()
1656         { return backgroundButton.get_color().get_pixel(); }
1658 private:
1660     /**
1661      * What type of 'open' are we? (save, export, etc)
1662      */
1663     FileDialogType dialogType;
1665     /**
1666      * Our svg preview widget
1667      */
1668     SVGPreview svgPreview;
1670     /**
1671      * Fix to allow the user to type the file name
1672      */
1673     Gtk::Entry *fileNameEntry;
1675     /**
1676      * Callback for seeing if the preview needs to be drawn
1677      */
1678     void updatePreviewCallback();
1680     //##########################################
1681     //# EXTRA WIDGET -- SOURCE SIDE
1682     //##########################################
1684     Gtk::Frame            sourceFrame;
1685     Gtk::VBox             sourceBox;
1687     Gtk::HBox             scopeBox;
1688     Gtk::RadioButtonGroup scopeGroup;
1689     Gtk::RadioButton      documentButton;
1690     Gtk::RadioButton      pageButton;
1691     Gtk::RadioButton      selectionButton;
1692     Gtk::RadioButton      customButton;
1694     Gtk::Table                      sourceTable;
1695     Inkscape::UI::Widget::Scalar    sourceX0Spinner;
1696     Inkscape::UI::Widget::Scalar    sourceY0Spinner;
1697     Inkscape::UI::Widget::Scalar    sourceX1Spinner;
1698     Inkscape::UI::Widget::Scalar    sourceY1Spinner;
1699     Inkscape::UI::Widget::Scalar    sourceWidthSpinner;
1700     Inkscape::UI::Widget::Scalar    sourceHeightSpinner;
1701     Inkscape::UI::Widget::UnitMenu  sourceUnitsSpinner;
1704     //##########################################
1705     //# EXTRA WIDGET -- DESTINATION SIDE
1706     //##########################################
1708     Gtk::Frame       destFrame;
1709     Gtk::VBox        destBox;
1711     Gtk::Table                      destTable;
1712     Inkscape::UI::Widget::Scalar    destWidthSpinner;
1713     Inkscape::UI::Widget::Scalar    destHeightSpinner;
1714     Inkscape::UI::Widget::Scalar    destDPISpinner;
1715     Inkscape::UI::Widget::UnitMenu  destUnitsSpinner;
1717     Gtk::HBox        otherOptionBox;
1718     Gtk::CheckButton cairoButton;
1719     Gtk::CheckButton antiAliasButton;
1720     Gtk::ColorButton backgroundButton;
1723     /**
1724      * 'Extra' widget that holds two boxes above
1725      */
1726     Gtk::HBox exportOptionsBox;
1729     //# Child widgets
1730     Gtk::CheckButton fileTypeCheckbox;
1732     /**
1733      * Allow the specification of the output file type
1734      */
1735     Gtk::ComboBoxText fileTypeComboBox;
1738     /**
1739      *  Data mirror of the combo box
1740      */
1741     std::vector<FileType> fileTypes;
1745     /**
1746      * Callback for user input into fileNameEntry
1747      */
1748     void fileTypeChangedCallback();
1750     /**
1751      *  Create a filter menu for this type of dialog
1752      */
1753     void createFileTypeMenu();
1756     bool append_extension;
1758     /**
1759      * The extension to use to write this file
1760      */
1761     Inkscape::Extension::Extension *extension;
1763     /**
1764      * Callback for user input into fileNameEntry
1765      */
1766     void fileNameEntryChangedCallback();
1768     /**
1769      * Filename that was given
1770      */
1771     Glib::ustring myFilename;
1772 };
1779 /**
1780  * Callback for checking if the preview needs to be redrawn
1781  */
1782 void FileExportDialogImpl::updatePreviewCallback()
1784     Glib::ustring fileName = get_preview_filename();
1785     if (!fileName.c_str())
1786         return;
1787     bool retval = svgPreview.set(fileName, dialogType);
1788     set_preview_widget_active(retval);
1793 /**
1794  * Callback for fileNameEntry widget
1795  */
1796 void FileExportDialogImpl::fileNameEntryChangedCallback()
1798     if (!fileNameEntry)
1799         return;
1801     Glib::ustring fileName = fileNameEntry->get_text();
1802     if (!Glib::get_charset()) //If we are not utf8
1803         fileName = Glib::filename_to_utf8(fileName);
1805     //g_message("User hit return.  Text is '%s'\n", fileName.c_str());
1807     if (!Glib::path_is_absolute(fileName)) {
1808         //try appending to the current path
1809         // not this way: fileName = get_current_folder() + "/" + fileName;
1810         std::vector<Glib::ustring> pathSegments;
1811         pathSegments.push_back( get_current_folder() );
1812         pathSegments.push_back( fileName );
1813         fileName = Glib::build_filename(pathSegments);
1814     }
1816     //g_message("path:'%s'\n", fileName.c_str());
1818     if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
1819         set_current_folder(fileName);
1820     } else if (/*Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR)*/1) {
1821         //dialog with either (1) select a regular file or (2) cd to dir
1822         //simulate an 'OK'
1823         set_filename(fileName);
1824         response(Gtk::RESPONSE_OK);
1825     }
1830 /**
1831  * Callback for fileNameEntry widget
1832  */
1833 void FileExportDialogImpl::fileTypeChangedCallback()
1835     int sel = fileTypeComboBox.get_active_row_number();
1836     if (sel<0 || sel >= (int)fileTypes.size())
1837         return;
1838     FileType type = fileTypes[sel];
1839     //g_message("selected: %s\n", type.name.c_str());
1840     Gtk::FileFilter filter;
1841     filter.add_pattern(type.pattern);
1842     set_filter(filter);
1847 void FileExportDialogImpl::createFileTypeMenu()
1849     Inkscape::Extension::DB::OutputList extension_list;
1850     Inkscape::Extension::db.get_output_list(extension_list);
1852     for (Inkscape::Extension::DB::OutputList::iterator current_item = extension_list.begin();
1853          current_item != extension_list.end(); current_item++)
1854     {
1855         Inkscape::Extension::Output * omod = *current_item;
1857         // FIXME: would be nice to grey them out instead of not listing them
1858         if (omod->deactivated()) continue;
1860         FileType type;
1861         type.name     = (_(omod->get_filetypename()));
1862         type.pattern  = "*";
1863         Glib::ustring extension = omod->get_extension();
1864         fileDialogExtensionToPattern (type.pattern, extension);
1865         type.extension= omod;
1866         fileTypeComboBox.append_text(type.name);
1867         fileTypes.push_back(type);
1868     }
1870     //#Let user choose
1871     FileType guessType;
1872     guessType.name = _("Guess from extension");
1873     guessType.pattern = "*";
1874     guessType.extension = NULL;
1875     fileTypeComboBox.append_text(guessType.name);
1876     fileTypes.push_back(guessType);
1879     fileTypeComboBox.set_active(0);
1880     fileTypeChangedCallback(); //call at least once to set the filter
1884 /**
1885  * Constructor
1886  */
1887 FileExportDialogImpl::FileExportDialogImpl(const Glib::ustring &dir,
1888             FileDialogType fileTypes,
1889             const Glib::ustring &title,
1890             const Glib::ustring &default_key) :
1891             FileDialogBase(title, Gtk::FILE_CHOOSER_ACTION_SAVE),
1892             sourceX0Spinner("X0",         _("Source left bound")),
1893             sourceY0Spinner("Y0",         _("Source top bound")),
1894             sourceX1Spinner("X1",         _("Source right bound")),
1895             sourceY1Spinner("Y1",         _("Source bottom bound")),
1896             sourceWidthSpinner("Width",   _("Source width")),
1897             sourceHeightSpinner("Height", _("Source height")),
1898             destWidthSpinner("Width",     _("Destination width")),
1899             destHeightSpinner("Height",   _("Destination height")),
1900             destDPISpinner("DPI",         _("Dots per inch resolution"))
1902     append_extension = (bool)prefs_get_int_attribute("dialogs.save_as", "append_extension", 1);
1904     /* One file at a time */
1905     set_select_multiple(false);
1907     /* Initalize to Autodetect */
1908     extension = NULL;
1909     /* No filename to start out with */
1910     myFilename = "";
1912     /* Set our dialog type (save, export, etc...)*/
1913     dialogType = fileTypes;
1915     /* Set the pwd and/or the filename */
1916     if (dir.size()>0)
1917         {
1918         Glib::ustring udir(dir);
1919         Glib::ustring::size_type len = udir.length();
1920         // leaving a trailing backslash on the directory name leads to the infamous
1921         // double-directory bug on win32
1922         if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
1923         set_current_folder(udir.c_str());
1924         }
1926     //#########################################
1927     //## EXTRA WIDGET -- SOURCE SIDE
1928     //#########################################
1930     //##### Export options buttons/spinners, etc
1931     documentButton.set_label(_("Document"));
1932     scopeBox.pack_start(documentButton);
1933     scopeGroup = documentButton.get_group();
1935     pageButton.set_label(_("Page"));
1936     pageButton.set_group(scopeGroup);
1937     scopeBox.pack_start(pageButton);
1939     selectionButton.set_label(_("Selection"));
1940     selectionButton.set_group(scopeGroup);
1941     scopeBox.pack_start(selectionButton);
1943     customButton.set_label(_("Custom"));
1944     customButton.set_group(scopeGroup);
1945     scopeBox.pack_start(customButton);
1947     sourceBox.pack_start(scopeBox);
1951     //dimension buttons
1952     sourceTable.resize(3,3);
1953     sourceTable.attach(sourceX0Spinner,     0,1,0,1);
1954     sourceTable.attach(sourceY0Spinner,     1,2,0,1);
1955     sourceUnitsSpinner.setUnitType(UNIT_TYPE_LINEAR);
1956     sourceTable.attach(sourceUnitsSpinner,  2,3,0,1);
1957     sourceTable.attach(sourceX1Spinner,     0,1,1,2);
1958     sourceTable.attach(sourceY1Spinner,     1,2,1,2);
1959     sourceTable.attach(sourceWidthSpinner,  0,1,2,3);
1960     sourceTable.attach(sourceHeightSpinner, 1,2,2,3);
1962     sourceBox.pack_start(sourceTable);
1963     sourceFrame.set_label(_("Source"));
1964     sourceFrame.add(sourceBox);
1965     exportOptionsBox.pack_start(sourceFrame);
1968     //#########################################
1969     //## EXTRA WIDGET -- SOURCE SIDE
1970     //#########################################
1973     destTable.resize(3,3);
1974     destTable.attach(destWidthSpinner,    0,1,0,1);
1975     destTable.attach(destHeightSpinner,   1,2,0,1);
1976     destUnitsSpinner.setUnitType(UNIT_TYPE_LINEAR);
1977     destTable.attach(destUnitsSpinner,    2,3,0,1);
1978     destTable.attach(destDPISpinner,      0,1,1,2);
1980     destBox.pack_start(destTable);
1983     cairoButton.set_label(_("Cairo"));
1984     otherOptionBox.pack_start(cairoButton);
1986     antiAliasButton.set_label(_("Antialias"));
1987     otherOptionBox.pack_start(antiAliasButton);
1989     backgroundButton.set_label(_("Background"));
1990     otherOptionBox.pack_start(backgroundButton);
1992     destBox.pack_start(otherOptionBox);
1998     //###### File options
1999     //###### Do we want the .xxx extension automatically added?
2000     fileTypeCheckbox.set_label(Glib::ustring(_("Append filename extension automatically")));
2001     fileTypeCheckbox.set_active(append_extension);
2002     destBox.pack_start(fileTypeCheckbox);
2004     //###### File type menu
2005     createFileTypeMenu();
2006     fileTypeComboBox.set_size_request(200,40);
2007     fileTypeComboBox.signal_changed().connect(
2008          sigc::mem_fun(*this, &FileExportDialogImpl::fileTypeChangedCallback) );
2010     destBox.pack_start(fileTypeComboBox);
2012     destFrame.set_label(_("Destination"));
2013     destFrame.add(destBox);
2014     exportOptionsBox.pack_start(destFrame);
2016     //##### Put the two boxes and their parent onto the dialog    
2017     exportOptionsBox.pack_start(sourceFrame);
2018     exportOptionsBox.pack_start(destFrame);
2020     set_extra_widget(exportOptionsBox);
2025     //###### PREVIEW WIDGET
2026     set_preview_widget(svgPreview);
2027     set_preview_widget_active(true);
2028     set_use_preview_label (false);
2030     //Catch selection-changed events, so we can adjust the text widget
2031     signal_update_preview().connect(
2032          sigc::mem_fun(*this, &FileExportDialogImpl::updatePreviewCallback) );
2035     //Let's do some customization
2036     fileNameEntry = NULL;
2037     Gtk::Container *cont = get_toplevel();
2038     std::vector<Gtk::Entry *> entries;
2039     findEntryWidgets(cont, entries);
2040     //g_message("Found %d entry widgets\n", entries.size());
2041     if (entries.size() >=1 )
2042         {
2043         //Catch when user hits [return] on the text field
2044         fileNameEntry = entries[0];
2045         fileNameEntry->signal_activate().connect(
2046              sigc::mem_fun(*this, &FileExportDialogImpl::fileNameEntryChangedCallback) );
2047         }
2049     //Let's do more customization
2050     std::vector<Gtk::Expander *> expanders;
2051     findExpanderWidgets(cont, expanders);
2052     //g_message("Found %d expander widgets\n", expanders.size());
2053     if (expanders.size() >=1 )
2054         {
2055         //Always show the file list
2056         Gtk::Expander *expander = expanders[0];
2057         expander->set_expanded(true);
2058         }
2061     //if (extension == NULL)
2062     //    checkbox.set_sensitive(FALSE);
2064     add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
2065     add_button(Gtk::Stock::SAVE,   Gtk::RESPONSE_OK);
2067     show_all_children();
2072 /**
2073  * Public factory method.  Used in file.cpp
2074  */
2075 FileExportDialog *FileExportDialog::create(const Glib::ustring &path,
2076                                        FileDialogType fileTypes,
2077                                        const Glib::ustring &title,
2078                                        const Glib::ustring &default_key)
2080     FileExportDialog *dialog = new FileExportDialogImpl(path, fileTypes, title, default_key);
2081     return dialog;
2088 /**
2089  * Destructor
2090  */
2091 FileExportDialogImpl::~FileExportDialogImpl()
2097 /**
2098  * Show this dialog modally.  Return true if user hits [OK]
2099  */
2100 bool
2101 FileExportDialogImpl::show()
2103     Glib::ustring s = Glib::filename_to_utf8 (get_current_folder());
2104     if (s.length() == 0) 
2105         s = getcwd (NULL, 0);
2106     set_current_folder(Glib::filename_from_utf8(s)); //hack to force initial dir listing
2107     set_modal (TRUE);                      //Window
2108     sp_transientize((GtkWidget *)gobj());  //Make transient
2109     gint b = run();                        //Dialog
2110     svgPreview.showNoPreview();
2111     hide();
2113     if (b == Gtk::RESPONSE_OK)
2114         {
2115         int sel = fileTypeComboBox.get_active_row_number ();
2116         if (sel>=0 && sel< (int)fileTypes.size())
2117             {
2118             FileType &type = fileTypes[sel];
2119             extension = type.extension;
2120             }
2121         myFilename = get_filename();
2123         /*
2125         // FIXME: Why do we have more code
2127         append_extension = checkbox.get_active();
2128         prefs_set_int_attribute("dialogs.save_as", "append_extension", append_extension);
2129         prefs_set_string_attribute("dialogs.save_as", "default",
2130                   ( extension != NULL ? extension->get_id() : "" ));
2131         */
2132         return TRUE;
2133         }
2134     else
2135         {
2136         return FALSE;
2137         }
2141 /**
2142  * Get the file extension type that was selected by the user. Valid after an [OK]
2143  */
2144 Inkscape::Extension::Extension *
2145 FileExportDialogImpl::getSelectionType()
2147     return extension;
2151 /**
2152  * Get the file name chosen by the user.   Valid after an [OK]
2153  */
2154 Glib::ustring
2155 FileExportDialogImpl::getFilename()
2157     return myFilename;
2163 } //namespace Dialog
2164 } //namespace UI
2165 } //namespace Inkscape
2168 /*
2169   Local Variables:
2170   mode:c++
2171   c-file-style:"stroustrup"
2172   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2173   indent-tabs-mode:nil
2174   fill-column:99
2175   End:
2176 */
2177 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :