Code

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