Code

3417ba06c929fd09d0369b2f1e317c084c43abf5
[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);
628     if (Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR))
629         {
630         struct stat info;
631         if (stat(fName, &info))
632             {
633             return FALSE;
634             }
635         long fileLen = info.st_size;
636         if (fileLen > 0x150000L)
637             {
638             showingNoPreview = false;
639             showTooLarge(fileLen);
640             return FALSE;
641             }
642         }
644     Glib::ustring svg = ".svg";
645     Glib::ustring svgz = ".svgz";
647     if ((dialogType == SVG_TYPES || dialogType == IMPORT_TYPES) &&
648            (hasSuffix(fileName, svg) || hasSuffix(fileName, svgz)   )
649          )
650         {
651         bool retval = setFileName(fileName);
652         showingNoPreview = false;
653         return retval;
654         }
655     else if (isValidImageFile(fileName))
656         {
657         showImage(fileName);
658         showingNoPreview = false;
659         return true;
660         }
661     else
662         {
663         showNoPreview();
664         return false;
665         }
669 SVGPreview::SVGPreview()
671     if (!INKSCAPE)
672         inkscape_application_init("",false);
673     document = NULL;
674     viewerGtk = NULL;
675     set_size_request(150,150);
676     showingNoPreview = false;
679 SVGPreview::~SVGPreview()
688 /*#########################################################################
689 ### F I L E     D I A L O G    B A S E    C L A S S
690 #########################################################################*/
692 /**
693  * This class is the base implementation for the others.  This
694  * reduces redundancies and bugs.
695  */
696 class FileDialogBase : public Gtk::FileChooserDialog
698 public:
700     /**
701      *
702      */
703     FileDialogBase(const Glib::ustring &title) :
704                         Gtk::FileChooserDialog(title)
705         {
706         }
708     /**
709      *
710      */
711     FileDialogBase(const Glib::ustring &title,
712                    Gtk::FileChooserAction dialogType) :
713                    Gtk::FileChooserDialog(title, dialogType)
714         {
715         }
717     /**
718      *
719      */
720     virtual ~FileDialogBase()
721         {}
723 };
727 /*#########################################################################
728 ### F I L E    O P E N
729 #########################################################################*/
731 /**
732  * Our implementation class for the FileOpenDialog interface..
733  */
734 class FileOpenDialogImpl : public FileOpenDialog, public FileDialogBase
736 public:
738     FileOpenDialogImpl(const Glib::ustring &dir,
739                        FileDialogType fileTypes,
740                        const Glib::ustring &title);
742     virtual ~FileOpenDialogImpl();
744     bool show();
746     Inkscape::Extension::Extension *getSelectionType();
748     Glib::ustring getFilename();
750     std::vector<Glib::ustring> getFilenames ();
752 protected:
756 private:
759     /**
760      * What type of 'open' are we? (open, import, place, etc)
761      */
762     FileDialogType dialogType;
764     /**
765      * Our svg preview widget
766      */
767     SVGPreview svgPreview;
769     /**
770      * Callback for seeing if the preview needs to be drawn
771      */
772     void updatePreviewCallback();
774     /**
775      * Fix to allow the user to type the file name
776      */
777     Gtk::Entry fileNameEntry;
779     /**
780      *  Create a filter menu for this type of dialog
781      */
782     void createFilterMenu();
784     /**
785      * Callback for user input into fileNameEntry
786      */
787     void fileNameEntryChangedCallback();
789     /**
790      * Callback for user changing which item is selected on the list
791      */
792     void fileSelectedCallback();
795     /**
796      * Filter name->extension lookup
797      */
798     std::map<Glib::ustring, Inkscape::Extension::Extension *> extensionMap;
800     /**
801      * The extension to use to write this file
802      */
803     Inkscape::Extension::Extension *extension;
805     /**
806      * Filename that was given
807      */
808     Glib::ustring myFilename;
810 };
816 /**
817  * Callback for checking if the preview needs to be redrawn
818  */
819 void FileOpenDialogImpl::updatePreviewCallback()
821     Glib::ustring fileName = get_preview_filename();
822     if (fileName.length() < 1)
823         return;
824     svgPreview.set(fileName, dialogType);
831 /**
832  * Callback for fileNameEntry widget
833  */
834 void FileOpenDialogImpl::fileNameEntryChangedCallback()
836     Glib::ustring rawFileName = fileNameEntry.get_text();
838     Glib::ustring fileName = Glib::filename_from_utf8(rawFileName);
840     //g_message("User hit return.  Text is '%s'\n", fileName.c_str());
842     if (!Glib::path_is_absolute(fileName)) {
843         //try appending to the current path
844         // not this way: fileName = get_current_folder() + "/" + fName;
845         std::vector<Glib::ustring> pathSegments;
846         pathSegments.push_back( get_current_folder() );
847         pathSegments.push_back( fileName );
848         fileName = Glib::build_filename(pathSegments);
849     }
851     //g_message("path:'%s'\n", fName.c_str());
853     if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
854         set_current_folder(fileName);
855     } else if (Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR)) {
856         //dialog with either (1) select a regular file or (2) cd to dir
857         //simulate an 'OK'
858         set_filename(fileName);
859         response(Gtk::RESPONSE_OK);
860     }
867 /**
868  * Callback for fileNameEntry widget
869  */
870 void FileOpenDialogImpl::fileSelectedCallback()
872     Glib::ustring fileName     = get_filename();
873     if (!Glib::get_charset()) //If we are not utf8
874         fileName = Glib::filename_to_utf8(fileName);
875     //g_message("User selected '%s'\n",
876     //       filename().c_str());
878 #ifdef INK_DUMP_FILENAME_CONV
879     ::dump_ustr( get_filename() );
880 #endif
881     fileNameEntry.set_text(fileName);
887 void FileOpenDialogImpl::createFilterMenu()
889     //patterns added dynamically below
890     Gtk::FileFilter allImageFilter;
891     allImageFilter.set_name(_("All Images"));
892     extensionMap[Glib::ustring(_("All Images"))]=NULL;
893     add_filter(allImageFilter);
895     Gtk::FileFilter allFilter;
896     allFilter.set_name(_("All Files"));
897     extensionMap[Glib::ustring(_("All Files"))]=NULL;
898     allFilter.add_pattern("*");
899     add_filter(allFilter);
901     //patterns added dynamically below
902     Gtk::FileFilter allInkscapeFilter;
903     allInkscapeFilter.set_name(_("All Inkscape Files"));
904     extensionMap[Glib::ustring(_("All Inkscape Files"))]=NULL;
905     add_filter(allInkscapeFilter);
907     Inkscape::Extension::DB::InputList extension_list;
908     Inkscape::Extension::db.get_input_list(extension_list);
910     for (Inkscape::Extension::DB::InputList::iterator current_item = extension_list.begin();
911          current_item != extension_list.end(); current_item++)
912     {
913         Inkscape::Extension::Input * imod = *current_item;
915         // FIXME: would be nice to grey them out instead of not listing them
916         if (imod->deactivated()) continue;
918         Glib::ustring upattern("*");
919         Glib::ustring extension = imod->get_extension();
920         fileDialogExtensionToPattern(upattern, extension);
922         Gtk::FileFilter filter;
923         Glib::ustring uname(_(imod->get_filetypename()));
924         filter.set_name(uname);
925         filter.add_pattern(upattern);
926         add_filter(filter);
927         extensionMap[uname] = imod;
929         //g_message("ext %s:%s '%s'\n", ioext->name, ioext->mimetype, upattern.c_str());
930         allInkscapeFilter.add_pattern(upattern);
931         if ( strncmp("image", imod->get_mimetype(), 5)==0 )
932             allImageFilter.add_pattern(upattern);
933     }
935     return;
940 /**
941  * Constructor.  Not called directly.  Use the factory.
942  */
943 FileOpenDialogImpl::FileOpenDialogImpl(const Glib::ustring &dir,
944                                        FileDialogType fileTypes,
945                                        const Glib::ustring &title) :
946                                        FileDialogBase(title)
950     /* One file at a time */
951     /* And also Multiple Files */
952     set_select_multiple(true);
954     /* Initalize to Autodetect */
955     extension = NULL;
956     /* No filename to start out with */
957     myFilename = "";
959     /* Set our dialog type (open, import, etc...)*/
960     dialogType = fileTypes;
963     /* Set the pwd and/or the filename */
964     if (dir.size() > 0)
965         {
966         Glib::ustring udir(dir);
967         Glib::ustring::size_type len = udir.length();
968         // leaving a trailing backslash on the directory name leads to the infamous
969         // double-directory bug on win32
970         if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
971         set_current_folder(udir.c_str());
972         }
974     //###### Add the file types menu
975     createFilterMenu();
977     //###### Add a preview widget
978     set_preview_widget(svgPreview);
979     set_preview_widget_active(true);
980     set_use_preview_label (false);
982     //Catch selection-changed events, so we can adjust the text widget
983     signal_update_preview().connect(
984          sigc::mem_fun(*this, &FileOpenDialogImpl::updatePreviewCallback) );
987     //###### Add a text entry bar, and tie it to file chooser events
988     fileNameEntry.set_text(get_current_folder());
989     set_extra_widget(fileNameEntry);
990     fileNameEntry.grab_focus();
992     //Catch when user hits [return] on the text field
993     fileNameEntry.signal_activate().connect(
994          sigc::mem_fun(*this, &FileOpenDialogImpl::fileNameEntryChangedCallback) );
996     //Catch selection-changed events, so we can adjust the text widget
997     signal_selection_changed().connect(
998          sigc::mem_fun(*this, &FileOpenDialogImpl::fileSelectedCallback) );
1000     add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1001     add_button(Gtk::Stock::OPEN,   Gtk::RESPONSE_OK);
1009 /**
1010  * Public factory.  Called by file.cpp, among others.
1011  */
1012 FileOpenDialog *FileOpenDialog::create(const Glib::ustring &path,
1013                                        FileDialogType fileTypes,
1014                                        const Glib::ustring &title)
1016     FileOpenDialog *dialog = new FileOpenDialogImpl(path, fileTypes, title);
1017     return dialog;
1023 /**
1024  * Destructor
1025  */
1026 FileOpenDialogImpl::~FileOpenDialogImpl()
1032 /**
1033  * Show this dialog modally.  Return true if user hits [OK]
1034  */
1035 bool
1036 FileOpenDialogImpl::show()
1038     Glib::ustring s = Glib::filename_to_utf8 (get_current_folder());
1039     if (s.length() == 0) 
1040         s = getcwd (NULL, 0);
1041     set_current_folder(Glib::filename_from_utf8(s)); //hack to force initial dir listing
1042     set_modal (TRUE);                      //Window
1043     sp_transientize((GtkWidget *)gobj());  //Make transient
1044     gint b = run();                        //Dialog
1045     svgPreview.showNoPreview();
1046     hide();
1048     if (b == Gtk::RESPONSE_OK)
1049         {
1050         //This is a hack, to avoid the warning messages that
1051         //Gtk::FileChooser::get_filter() returns
1052         //should be:  Gtk::FileFilter *filter = get_filter();
1053         GtkFileChooser *gtkFileChooser = Gtk::FileChooser::gobj();
1054         GtkFileFilter *filter = gtk_file_chooser_get_filter(gtkFileChooser);
1055         if (filter)
1056             {
1057             //Get which extension was chosen, if any
1058             extension = extensionMap[gtk_file_filter_get_name(filter)];
1059             }
1060         myFilename = get_filename();
1061         return TRUE;
1062         }
1063     else
1064        {
1065        return FALSE;
1066        }
1072 /**
1073  * Get the file extension type that was selected by the user. Valid after an [OK]
1074  */
1075 Inkscape::Extension::Extension *
1076 FileOpenDialogImpl::getSelectionType()
1078     return extension;
1082 /**
1083  * Get the file name chosen by the user.   Valid after an [OK]
1084  */
1085 Glib::ustring
1086 FileOpenDialogImpl::getFilename (void)
1088     return g_strdup(myFilename.c_str());
1092 /**
1093  * To Get Multiple filenames selected at-once.
1094  */
1095 std::vector<Glib::ustring>FileOpenDialogImpl::getFilenames()
1096 {    
1097     std::vector<Glib::ustring> result = get_filenames();
1098     return result;
1106 //########################################################################
1107 //# F I L E    S A V E
1108 //########################################################################
1110 class FileType
1112     public:
1113     FileType() {}
1114     ~FileType() {}
1115     Glib::ustring name;
1116     Glib::ustring pattern;
1117     Inkscape::Extension::Extension *extension;
1118 };
1120 /**
1121  * Our implementation of the FileSaveDialog interface.
1122  */
1123 class FileSaveDialogImpl : public FileSaveDialog, public FileDialogBase
1126 public:
1127     FileSaveDialogImpl(const Glib::ustring &dir,
1128                        FileDialogType fileTypes,
1129                        const Glib::ustring &title,
1130                        const Glib::ustring &default_key);
1132     virtual ~FileSaveDialogImpl();
1134     bool show();
1136     Inkscape::Extension::Extension *getSelectionType();
1138     Glib::ustring getFilename();
1140     void change_title(const Glib::ustring& title);
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         set_current_folder(udir.c_str());
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     Glib::ustring s = Glib::filename_to_utf8 (get_current_folder());
1459     if (s.length() == 0) 
1460         s = getcwd (NULL, 0);
1461     set_current_folder(Glib::filename_from_utf8(s)); //hack to force initial dir listing
1462     set_modal (TRUE);                      //Window
1463     sp_transientize((GtkWidget *)gobj());  //Make transient
1464     gint b = run();                        //Dialog
1465     svgPreview.showNoPreview();
1466     hide();
1468     if (b == Gtk::RESPONSE_OK)
1469         {
1470         int sel = fileTypeComboBox.get_active_row_number ();
1471         if (sel>=0 && sel< (int)fileTypes.size())
1472             {
1473             FileType &type = fileTypes[sel];
1474             extension = type.extension;
1475             }
1476         myFilename = get_filename();
1478         /*
1480         // FIXME: Why do we have more code
1482         append_extension = checkbox.get_active();
1483         prefs_set_int_attribute("dialogs.save_as", "append_extension", append_extension);
1484         prefs_set_string_attribute("dialogs.save_as", "default",
1485                   ( extension != NULL ? extension->get_id() : "" ));
1486         */
1487         return TRUE;
1488         }
1489     else
1490         {
1491         return FALSE;
1492         }
1496 /**
1497  * Get the file extension type that was selected by the user. Valid after an [OK]
1498  */
1499 Inkscape::Extension::Extension *
1500 FileSaveDialogImpl::getSelectionType()
1502     return extension;
1506 /**
1507  * Get the file name chosen by the user.   Valid after an [OK]
1508  */
1509 Glib::ustring
1510 FileSaveDialogImpl::getFilename()
1512     return myFilename;
1516 void 
1517 FileSaveDialogImpl::change_title(const Glib::ustring& title)
1519     this->set_title(title);
1528 //########################################################################
1529 //# F I L E     E X P O R T
1530 //########################################################################
1533 /**
1534  * Our implementation of the FileExportDialog interface.
1535  */
1536 class FileExportDialogImpl : public FileExportDialog, public FileDialogBase
1539 public:
1540     FileExportDialogImpl(const Glib::ustring &dir,
1541                        FileDialogType fileTypes,
1542                        const Glib::ustring &title,
1543                        const Glib::ustring &default_key);
1545     virtual ~FileExportDialogImpl();
1547     bool show();
1549     Inkscape::Extension::Extension *getSelectionType();
1551     Glib::ustring getFilename();
1554     /**
1555      * Return the scope of the export.  One of the enumerated types
1556      * in ScopeType     
1557      */
1558     ScopeType getScope()
1559         { 
1560         if (pageButton.get_active())
1561             return SCOPE_PAGE;
1562         else if (selectionButton.get_active())
1563             return SCOPE_SELECTION;
1564         else if (customButton.get_active())
1565             return SCOPE_CUSTOM;
1566         else
1567             return SCOPE_DOCUMENT;
1569         }
1570     
1571     /**
1572      * Return left side of the exported region
1573      */
1574     double getSourceX()
1575         { return sourceX0Spinner.getValue(); }
1576     
1577     /**
1578      * Return the top of the exported region
1579      */
1580     double getSourceY()
1581         { return sourceY1Spinner.getValue(); }
1582     
1583     /**
1584      * Return the width of the exported region
1585      */
1586     double getSourceWidth()
1587         { return sourceWidthSpinner.getValue(); }
1588     
1589     /**
1590      * Return the height of the exported region
1591      */
1592     double getSourceHeight()
1593         { return sourceHeightSpinner.getValue(); }
1595     /**
1596      * Return the units of the coordinates of exported region
1597      */
1598     Glib::ustring getSourceUnits()
1599         { return sourceUnitsSpinner.getUnitAbbr(); }
1601     /**
1602      * Return the width of the destination document
1603      */
1604     double getDestinationWidth()
1605         { return destWidthSpinner.getValue(); }
1607     /**
1608      * Return the height of the destination document
1609      */
1610     double getDestinationHeight()
1611         { return destHeightSpinner.getValue(); }
1613     /**
1614      * Return the height of the exported region
1615      */
1616     Glib::ustring getDestinationUnits()
1617         { return destUnitsSpinner.getUnitAbbr(); }
1619     /**
1620      * Return the destination DPI image resulution, if bitmap
1621      */
1622     double getDestinationDPI()
1623         { return destDPISpinner.getValue(); }
1625     /**
1626      * Return whether we should use Cairo for rendering
1627      */
1628     bool getUseCairo()
1629         { return cairoButton.get_active(); }
1631     /**
1632      * Return whether we should use antialiasing
1633      */
1634     bool getUseAntialias()
1635         { return antiAliasButton.get_active(); }
1637     /**
1638      * Return the background color for exporting
1639      */
1640     unsigned long getBackground()
1641         { return backgroundButton.get_color().get_pixel(); }
1643 private:
1645     /**
1646      * What type of 'open' are we? (save, export, etc)
1647      */
1648     FileDialogType dialogType;
1650     /**
1651      * Our svg preview widget
1652      */
1653     SVGPreview svgPreview;
1655     /**
1656      * Fix to allow the user to type the file name
1657      */
1658     Gtk::Entry *fileNameEntry;
1660     /**
1661      * Callback for seeing if the preview needs to be drawn
1662      */
1663     void updatePreviewCallback();
1665     //##########################################
1666     //# EXTRA WIDGET -- SOURCE SIDE
1667     //##########################################
1669     Gtk::Frame            sourceFrame;
1670     Gtk::VBox             sourceBox;
1672     Gtk::HBox             scopeBox;
1673     Gtk::RadioButtonGroup scopeGroup;
1674     Gtk::RadioButton      documentButton;
1675     Gtk::RadioButton      pageButton;
1676     Gtk::RadioButton      selectionButton;
1677     Gtk::RadioButton      customButton;
1679     Gtk::Table                      sourceTable;
1680     Inkscape::UI::Widget::Scalar    sourceX0Spinner;
1681     Inkscape::UI::Widget::Scalar    sourceY0Spinner;
1682     Inkscape::UI::Widget::Scalar    sourceX1Spinner;
1683     Inkscape::UI::Widget::Scalar    sourceY1Spinner;
1684     Inkscape::UI::Widget::Scalar    sourceWidthSpinner;
1685     Inkscape::UI::Widget::Scalar    sourceHeightSpinner;
1686     Inkscape::UI::Widget::UnitMenu  sourceUnitsSpinner;
1689     //##########################################
1690     //# EXTRA WIDGET -- DESTINATION SIDE
1691     //##########################################
1693     Gtk::Frame       destFrame;
1694     Gtk::VBox        destBox;
1696     Gtk::Table                      destTable;
1697     Inkscape::UI::Widget::Scalar    destWidthSpinner;
1698     Inkscape::UI::Widget::Scalar    destHeightSpinner;
1699     Inkscape::UI::Widget::Scalar    destDPISpinner;
1700     Inkscape::UI::Widget::UnitMenu  destUnitsSpinner;
1702     Gtk::HBox        otherOptionBox;
1703     Gtk::CheckButton cairoButton;
1704     Gtk::CheckButton antiAliasButton;
1705     Gtk::ColorButton backgroundButton;
1708     /**
1709      * 'Extra' widget that holds two boxes above
1710      */
1711     Gtk::HBox exportOptionsBox;
1714     //# Child widgets
1715     Gtk::CheckButton fileTypeCheckbox;
1717     /**
1718      * Allow the specification of the output file type
1719      */
1720     Gtk::ComboBoxText fileTypeComboBox;
1723     /**
1724      *  Data mirror of the combo box
1725      */
1726     std::vector<FileType> fileTypes;
1730     /**
1731      * Callback for user input into fileNameEntry
1732      */
1733     void fileTypeChangedCallback();
1735     /**
1736      *  Create a filter menu for this type of dialog
1737      */
1738     void createFileTypeMenu();
1741     bool append_extension;
1743     /**
1744      * The extension to use to write this file
1745      */
1746     Inkscape::Extension::Extension *extension;
1748     /**
1749      * Callback for user input into fileNameEntry
1750      */
1751     void fileNameEntryChangedCallback();
1753     /**
1754      * Filename that was given
1755      */
1756     Glib::ustring myFilename;
1757 };
1764 /**
1765  * Callback for checking if the preview needs to be redrawn
1766  */
1767 void FileExportDialogImpl::updatePreviewCallback()
1769     Glib::ustring fileName = get_preview_filename();
1770     if (!fileName.c_str())
1771         return;
1772     bool retval = svgPreview.set(fileName, dialogType);
1773     set_preview_widget_active(retval);
1778 /**
1779  * Callback for fileNameEntry widget
1780  */
1781 void FileExportDialogImpl::fileNameEntryChangedCallback()
1783     if (!fileNameEntry)
1784         return;
1786     Glib::ustring fileName = fileNameEntry->get_text();
1787     if (!Glib::get_charset()) //If we are not utf8
1788         fileName = Glib::filename_to_utf8(fileName);
1790     //g_message("User hit return.  Text is '%s'\n", fileName.c_str());
1792     if (!Glib::path_is_absolute(fileName)) {
1793         //try appending to the current path
1794         // not this way: fileName = get_current_folder() + "/" + fileName;
1795         std::vector<Glib::ustring> pathSegments;
1796         pathSegments.push_back( get_current_folder() );
1797         pathSegments.push_back( fileName );
1798         fileName = Glib::build_filename(pathSegments);
1799     }
1801     //g_message("path:'%s'\n", fileName.c_str());
1803     if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
1804         set_current_folder(fileName);
1805     } else if (/*Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR)*/1) {
1806         //dialog with either (1) select a regular file or (2) cd to dir
1807         //simulate an 'OK'
1808         set_filename(fileName);
1809         response(Gtk::RESPONSE_OK);
1810     }
1815 /**
1816  * Callback for fileNameEntry widget
1817  */
1818 void FileExportDialogImpl::fileTypeChangedCallback()
1820     int sel = fileTypeComboBox.get_active_row_number();
1821     if (sel<0 || sel >= (int)fileTypes.size())
1822         return;
1823     FileType type = fileTypes[sel];
1824     //g_message("selected: %s\n", type.name.c_str());
1825     Gtk::FileFilter filter;
1826     filter.add_pattern(type.pattern);
1827     set_filter(filter);
1832 void FileExportDialogImpl::createFileTypeMenu()
1834     Inkscape::Extension::DB::OutputList extension_list;
1835     Inkscape::Extension::db.get_output_list(extension_list);
1837     for (Inkscape::Extension::DB::OutputList::iterator current_item = extension_list.begin();
1838          current_item != extension_list.end(); current_item++)
1839     {
1840         Inkscape::Extension::Output * omod = *current_item;
1842         // FIXME: would be nice to grey them out instead of not listing them
1843         if (omod->deactivated()) continue;
1845         FileType type;
1846         type.name     = (_(omod->get_filetypename()));
1847         type.pattern  = "*";
1848         Glib::ustring extension = omod->get_extension();
1849         fileDialogExtensionToPattern (type.pattern, extension);
1850         type.extension= omod;
1851         fileTypeComboBox.append_text(type.name);
1852         fileTypes.push_back(type);
1853     }
1855     //#Let user choose
1856     FileType guessType;
1857     guessType.name = _("Guess from extension");
1858     guessType.pattern = "*";
1859     guessType.extension = NULL;
1860     fileTypeComboBox.append_text(guessType.name);
1861     fileTypes.push_back(guessType);
1864     fileTypeComboBox.set_active(0);
1865     fileTypeChangedCallback(); //call at least once to set the filter
1869 /**
1870  * Constructor
1871  */
1872 FileExportDialogImpl::FileExportDialogImpl(const Glib::ustring &dir,
1873             FileDialogType fileTypes,
1874             const Glib::ustring &title,
1875             const Glib::ustring &default_key) :
1876             FileDialogBase(title, Gtk::FILE_CHOOSER_ACTION_SAVE),
1877             sourceX0Spinner("X0",         _("Source left bound")),
1878             sourceY0Spinner("Y0",         _("Source top bound")),
1879             sourceX1Spinner("X1",         _("Source right bound")),
1880             sourceY1Spinner("Y1",         _("Source bottom bound")),
1881             sourceWidthSpinner("Width",   _("Source width")),
1882             sourceHeightSpinner("Height", _("Source height")),
1883             destWidthSpinner("Width",     _("Destination width")),
1884             destHeightSpinner("Height",   _("Destination height")),
1885             destDPISpinner("DPI",         _("Dots per inch resolution"))
1887     append_extension = (bool)prefs_get_int_attribute("dialogs.save_as", "append_extension", 1);
1889     /* One file at a time */
1890     set_select_multiple(false);
1892     /* Initalize to Autodetect */
1893     extension = NULL;
1894     /* No filename to start out with */
1895     myFilename = "";
1897     /* Set our dialog type (save, export, etc...)*/
1898     dialogType = fileTypes;
1900     /* Set the pwd and/or the filename */
1901     if (dir.size()>0)
1902         {
1903         Glib::ustring udir(dir);
1904         Glib::ustring::size_type len = udir.length();
1905         // leaving a trailing backslash on the directory name leads to the infamous
1906         // double-directory bug on win32
1907         if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
1908         set_current_folder(udir.c_str());
1909         }
1911     //#########################################
1912     //## EXTRA WIDGET -- SOURCE SIDE
1913     //#########################################
1915     //##### Export options buttons/spinners, etc
1916     documentButton.set_label(_("Document"));
1917     scopeBox.pack_start(documentButton);
1918     scopeGroup = documentButton.get_group();
1920     pageButton.set_label(_("Page"));
1921     pageButton.set_group(scopeGroup);
1922     scopeBox.pack_start(pageButton);
1924     selectionButton.set_label(_("Selection"));
1925     selectionButton.set_group(scopeGroup);
1926     scopeBox.pack_start(selectionButton);
1928     customButton.set_label(_("Custom"));
1929     customButton.set_group(scopeGroup);
1930     scopeBox.pack_start(customButton);
1932     sourceBox.pack_start(scopeBox);
1936     //dimension buttons
1937     sourceTable.resize(3,3);
1938     sourceTable.attach(sourceX0Spinner,     0,1,0,1);
1939     sourceTable.attach(sourceY0Spinner,     1,2,0,1);
1940     sourceUnitsSpinner.setUnitType(UNIT_TYPE_LINEAR);
1941     sourceTable.attach(sourceUnitsSpinner,  2,3,0,1);
1942     sourceTable.attach(sourceX1Spinner,     0,1,1,2);
1943     sourceTable.attach(sourceY1Spinner,     1,2,1,2);
1944     sourceTable.attach(sourceWidthSpinner,  0,1,2,3);
1945     sourceTable.attach(sourceHeightSpinner, 1,2,2,3);
1947     sourceBox.pack_start(sourceTable);
1948     sourceFrame.set_label(_("Source"));
1949     sourceFrame.add(sourceBox);
1950     exportOptionsBox.pack_start(sourceFrame);
1953     //#########################################
1954     //## EXTRA WIDGET -- SOURCE SIDE
1955     //#########################################
1958     destTable.resize(3,3);
1959     destTable.attach(destWidthSpinner,    0,1,0,1);
1960     destTable.attach(destHeightSpinner,   1,2,0,1);
1961     destUnitsSpinner.setUnitType(UNIT_TYPE_LINEAR);
1962     destTable.attach(destUnitsSpinner,    2,3,0,1);
1963     destTable.attach(destDPISpinner,      0,1,1,2);
1965     destBox.pack_start(destTable);
1968     cairoButton.set_label(_("Cairo"));
1969     otherOptionBox.pack_start(cairoButton);
1971     antiAliasButton.set_label(_("Antialias"));
1972     otherOptionBox.pack_start(antiAliasButton);
1974     backgroundButton.set_label(_("Background"));
1975     otherOptionBox.pack_start(backgroundButton);
1977     destBox.pack_start(otherOptionBox);
1983     //###### File options
1984     //###### Do we want the .xxx extension automatically added?
1985     fileTypeCheckbox.set_label(Glib::ustring(_("Append filename extension automatically")));
1986     fileTypeCheckbox.set_active(append_extension);
1987     destBox.pack_start(fileTypeCheckbox);
1989     //###### File type menu
1990     createFileTypeMenu();
1991     fileTypeComboBox.set_size_request(200,40);
1992     fileTypeComboBox.signal_changed().connect(
1993          sigc::mem_fun(*this, &FileExportDialogImpl::fileTypeChangedCallback) );
1995     destBox.pack_start(fileTypeComboBox);
1997     destFrame.set_label(_("Destination"));
1998     destFrame.add(destBox);
1999     exportOptionsBox.pack_start(destFrame);
2001     //##### Put the two boxes and their parent onto the dialog    
2002     exportOptionsBox.pack_start(sourceFrame);
2003     exportOptionsBox.pack_start(destFrame);
2005     set_extra_widget(exportOptionsBox);
2010     //###### PREVIEW WIDGET
2011     set_preview_widget(svgPreview);
2012     set_preview_widget_active(true);
2013     set_use_preview_label (false);
2015     //Catch selection-changed events, so we can adjust the text widget
2016     signal_update_preview().connect(
2017          sigc::mem_fun(*this, &FileExportDialogImpl::updatePreviewCallback) );
2020     //Let's do some customization
2021     fileNameEntry = NULL;
2022     Gtk::Container *cont = get_toplevel();
2023     std::vector<Gtk::Entry *> entries;
2024     findEntryWidgets(cont, entries);
2025     //g_message("Found %d entry widgets\n", entries.size());
2026     if (entries.size() >=1 )
2027         {
2028         //Catch when user hits [return] on the text field
2029         fileNameEntry = entries[0];
2030         fileNameEntry->signal_activate().connect(
2031              sigc::mem_fun(*this, &FileExportDialogImpl::fileNameEntryChangedCallback) );
2032         }
2034     //Let's do more customization
2035     std::vector<Gtk::Expander *> expanders;
2036     findExpanderWidgets(cont, expanders);
2037     //g_message("Found %d expander widgets\n", expanders.size());
2038     if (expanders.size() >=1 )
2039         {
2040         //Always show the file list
2041         Gtk::Expander *expander = expanders[0];
2042         expander->set_expanded(true);
2043         }
2046     //if (extension == NULL)
2047     //    checkbox.set_sensitive(FALSE);
2049     add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
2050     add_button(Gtk::Stock::SAVE,   Gtk::RESPONSE_OK);
2052     show_all_children();
2057 /**
2058  * Public factory method.  Used in file.cpp
2059  */
2060 FileExportDialog *FileExportDialog::create(const Glib::ustring &path,
2061                                        FileDialogType fileTypes,
2062                                        const Glib::ustring &title,
2063                                        const Glib::ustring &default_key)
2065     FileExportDialog *dialog = new FileExportDialogImpl(path, fileTypes, title, default_key);
2066     return dialog;
2073 /**
2074  * Destructor
2075  */
2076 FileExportDialogImpl::~FileExportDialogImpl()
2082 /**
2083  * Show this dialog modally.  Return true if user hits [OK]
2084  */
2085 bool
2086 FileExportDialogImpl::show()
2088     Glib::ustring s = Glib::filename_to_utf8 (get_current_folder());
2089     if (s.length() == 0) 
2090         s = getcwd (NULL, 0);
2091     set_current_folder(Glib::filename_from_utf8(s)); //hack to force initial dir listing
2092     set_modal (TRUE);                      //Window
2093     sp_transientize((GtkWidget *)gobj());  //Make transient
2094     gint b = run();                        //Dialog
2095     svgPreview.showNoPreview();
2096     hide();
2098     if (b == Gtk::RESPONSE_OK)
2099         {
2100         int sel = fileTypeComboBox.get_active_row_number ();
2101         if (sel>=0 && sel< (int)fileTypes.size())
2102             {
2103             FileType &type = fileTypes[sel];
2104             extension = type.extension;
2105             }
2106         myFilename = get_filename();
2108         /*
2110         // FIXME: Why do we have more code
2112         append_extension = checkbox.get_active();
2113         prefs_set_int_attribute("dialogs.save_as", "append_extension", append_extension);
2114         prefs_set_string_attribute("dialogs.save_as", "default",
2115                   ( extension != NULL ? extension->get_id() : "" ));
2116         */
2117         return TRUE;
2118         }
2119     else
2120         {
2121         return FALSE;
2122         }
2126 /**
2127  * Get the file extension type that was selected by the user. Valid after an [OK]
2128  */
2129 Inkscape::Extension::Extension *
2130 FileExportDialogImpl::getSelectionType()
2132     return extension;
2136 /**
2137  * Get the file name chosen by the user.   Valid after an [OK]
2138  */
2139 Glib::ustring
2140 FileExportDialogImpl::getFilename()
2142     return myFilename;
2148 } //namespace Dialog
2149 } //namespace UI
2150 } //namespace Inkscape
2153 /*
2154   Local Variables:
2155   mode:c++
2156   c-file-style:"stroustrup"
2157   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2158   indent-tabs-mode:nil
2159   fill-column:99
2160   End:
2161 */
2162 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :