Code

Moved EventLog from SPDocument to SPDesktop to prevent it from being
[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);
1141     void change_path(const Glib::ustring& dir);
1144 private:
1146     /**
1147      * What type of 'open' are we? (save, export, etc)
1148      */
1149     FileDialogType dialogType;
1151     /**
1152      * Our svg preview widget
1153      */
1154     SVGPreview svgPreview;
1156     /**
1157      * Fix to allow the user to type the file name
1158      */
1159     Gtk::Entry *fileNameEntry;
1161     /**
1162      * Callback for seeing if the preview needs to be drawn
1163      */
1164     void updatePreviewCallback();
1168     /**
1169      * Allow the specification of the output file type
1170      */
1171     Gtk::HBox fileTypeBox;
1173     /**
1174      * Allow the specification of the output file type
1175      */
1176     Gtk::ComboBoxText fileTypeComboBox;
1179     /**
1180      *  Data mirror of the combo box
1181      */
1182     std::vector<FileType> fileTypes;
1184     //# Child widgets
1185     Gtk::CheckButton fileTypeCheckbox;
1188     /**
1189      * Callback for user input into fileNameEntry
1190      */
1191     void fileTypeChangedCallback();
1193     /**
1194      *  Create a filter menu for this type of dialog
1195      */
1196     void createFileTypeMenu();
1199     bool append_extension;
1201     /**
1202      * The extension to use to write this file
1203      */
1204     Inkscape::Extension::Extension *extension;
1206     /**
1207      * Callback for user input into fileNameEntry
1208      */
1209     void fileNameEntryChangedCallback();
1211     /**
1212      * Filename that was given
1213      */
1214     Glib::ustring myFilename;
1215 };
1222 /**
1223  * Callback for checking if the preview needs to be redrawn
1224  */
1225 void FileSaveDialogImpl::updatePreviewCallback()
1227     Glib::ustring fileName = get_preview_filename();
1228     if (!fileName.c_str())
1229         return;
1230     bool retval = svgPreview.set(fileName, dialogType);
1231     set_preview_widget_active(retval);
1236 /**
1237  * Callback for fileNameEntry widget
1238  */
1239 void FileSaveDialogImpl::fileNameEntryChangedCallback()
1241     if (!fileNameEntry)
1242         return;
1244     Glib::ustring fileName = fileNameEntry->get_text();
1245     if (!Glib::get_charset()) //If we are not utf8
1246         fileName = Glib::filename_to_utf8(fileName);
1248     //g_message("User hit return.  Text is '%s'\n", fileName.c_str());
1250     if (!Glib::path_is_absolute(fileName)) {
1251         //try appending to the current path
1252         // not this way: fileName = get_current_folder() + "/" + fileName;
1253         std::vector<Glib::ustring> pathSegments;
1254         pathSegments.push_back( get_current_folder() );
1255         pathSegments.push_back( fileName );
1256         fileName = Glib::build_filename(pathSegments);
1257     }
1259     //g_message("path:'%s'\n", fileName.c_str());
1261     if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
1262         set_current_folder(fileName);
1263     } else if (/*Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR)*/1) {
1264         //dialog with either (1) select a regular file or (2) cd to dir
1265         //simulate an 'OK'
1266         set_filename(fileName);
1267         response(Gtk::RESPONSE_OK);
1268     }
1273 /**
1274  * Callback for fileNameEntry widget
1275  */
1276 void FileSaveDialogImpl::fileTypeChangedCallback()
1278     int sel = fileTypeComboBox.get_active_row_number();
1279     if (sel<0 || sel >= (int)fileTypes.size())
1280         return;
1281     FileType type = fileTypes[sel];
1282     //g_message("selected: %s\n", type.name.c_str());
1283     Gtk::FileFilter filter;
1284     filter.add_pattern(type.pattern);
1285     set_filter(filter);
1290 void FileSaveDialogImpl::createFileTypeMenu()
1292     Inkscape::Extension::DB::OutputList extension_list;
1293     Inkscape::Extension::db.get_output_list(extension_list);
1295     for (Inkscape::Extension::DB::OutputList::iterator current_item = extension_list.begin();
1296          current_item != extension_list.end(); current_item++)
1297     {
1298         Inkscape::Extension::Output * omod = *current_item;
1300         // FIXME: would be nice to grey them out instead of not listing them
1301         if (omod->deactivated()) continue;
1303         FileType type;
1304         type.name     = (_(omod->get_filetypename()));
1305         type.pattern  = "*";
1306         Glib::ustring extension = omod->get_extension();
1307         fileDialogExtensionToPattern (type.pattern, extension);
1308         type.extension= omod;
1309         fileTypeComboBox.append_text(type.name);
1310         fileTypes.push_back(type);
1311     }
1313     //#Let user choose
1314     FileType guessType;
1315     guessType.name = _("Guess from extension");
1316     guessType.pattern = "*";
1317     guessType.extension = NULL;
1318     fileTypeComboBox.append_text(guessType.name);
1319     fileTypes.push_back(guessType);
1322     fileTypeComboBox.set_active(0);
1323     fileTypeChangedCallback(); //call at least once to set the filter
1328 /**
1329  * Constructor
1330  */
1331 FileSaveDialogImpl::FileSaveDialogImpl(const Glib::ustring &dir,
1332             FileDialogType fileTypes,
1333             const Glib::ustring &title,
1334             const Glib::ustring &default_key) :
1335             FileDialogBase(title, Gtk::FILE_CHOOSER_ACTION_SAVE)
1337     append_extension = (bool)prefs_get_int_attribute("dialogs.save_as",
1338                                                   "append_extension", 1);
1340     /* One file at a time */
1341     set_select_multiple(false);
1343     /* Initalize to Autodetect */
1344     extension = NULL;
1345     /* No filename to start out with */
1346     myFilename = "";
1348     /* Set our dialog type (save, export, etc...)*/
1349     dialogType = fileTypes;
1351     /* Set the pwd and/or the filename */
1352     if (dir.size() > 0)
1353         {
1354         Glib::ustring udir(dir);
1355         Glib::ustring::size_type len = udir.length();
1356         // leaving a trailing backslash on the directory name leads to the infamous
1357         // double-directory bug on win32
1358         if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
1359         myFilename = udir;
1360         }
1362     //###### Add the file types menu
1363     //createFilterMenu();
1365     //###### Do we want the .xxx extension automatically added?
1366     fileTypeCheckbox.set_label(Glib::ustring(_("Append filename extension automatically")));
1367     fileTypeCheckbox.set_active(append_extension);
1369     fileTypeBox.pack_start(fileTypeCheckbox);
1370     createFileTypeMenu();
1371     fileTypeComboBox.set_size_request(200,40);
1372     fileTypeComboBox.signal_changed().connect(
1373          sigc::mem_fun(*this, &FileSaveDialogImpl::fileTypeChangedCallback) );
1375     fileTypeBox.pack_start(fileTypeComboBox);
1377     set_extra_widget(fileTypeBox);
1378     //get_vbox()->pack_start(fileTypeBox, false, false, 0);
1379     //get_vbox()->reorder_child(fileTypeBox, 2);
1381     //###### Add a preview widget
1382     set_preview_widget(svgPreview);
1383     set_preview_widget_active(true);
1384     set_use_preview_label (false);
1386     //Catch selection-changed events, so we can adjust the text widget
1387     signal_update_preview().connect(
1388          sigc::mem_fun(*this, &FileSaveDialogImpl::updatePreviewCallback) );
1391     //Let's do some customization
1392     fileNameEntry = NULL;
1393     Gtk::Container *cont = get_toplevel();
1394     std::vector<Gtk::Entry *> entries;
1395     findEntryWidgets(cont, entries);
1396     //g_message("Found %d entry widgets\n", entries.size());
1397     if (entries.size() >=1 )
1398         {
1399         //Catch when user hits [return] on the text field
1400         fileNameEntry = entries[0];
1401         fileNameEntry->signal_activate().connect(
1402              sigc::mem_fun(*this, &FileSaveDialogImpl::fileNameEntryChangedCallback) );
1403         }
1405     //Let's do more customization
1406     std::vector<Gtk::Expander *> expanders;
1407     findExpanderWidgets(cont, expanders);
1408     //g_message("Found %d expander widgets\n", expanders.size());
1409     if (expanders.size() >=1 )
1410         {
1411         //Always show the file list
1412         Gtk::Expander *expander = expanders[0];
1413         expander->set_expanded(true);
1414         }
1417     //if (extension == NULL)
1418     //    checkbox.set_sensitive(FALSE);
1420     add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1421     add_button(Gtk::Stock::SAVE,   Gtk::RESPONSE_OK);
1423     show_all_children();
1428 /**
1429  * Public factory method.  Used in file.cpp
1430  */
1431 FileSaveDialog *FileSaveDialog::create(const Glib::ustring &path,
1432                                        FileDialogType fileTypes,
1433                                        const Glib::ustring &title,
1434                                        const Glib::ustring &default_key)
1436     FileSaveDialog *dialog = new FileSaveDialogImpl(path, fileTypes, title, default_key);
1437     return dialog;
1444 /**
1445  * Destructor
1446  */
1447 FileSaveDialogImpl::~FileSaveDialogImpl()
1453 /**
1454  * Show this dialog modally.  Return true if user hits [OK]
1455  */
1456 bool
1457 FileSaveDialogImpl::show()
1459     change_path(myFilename);
1460     set_modal (TRUE);                      //Window
1461     sp_transientize((GtkWidget *)gobj());  //Make transient
1462     gint b = run();                        //Dialog
1463     svgPreview.showNoPreview();
1464     hide();
1466     if (b == Gtk::RESPONSE_OK)
1467         {
1468         int sel = fileTypeComboBox.get_active_row_number ();
1469         if (sel>=0 && sel< (int)fileTypes.size())
1470             {
1471             FileType &type = fileTypes[sel];
1472             extension = type.extension;
1473             }
1474         myFilename = get_filename();
1476         /*
1478         // FIXME: Why do we have more code
1480         append_extension = checkbox.get_active();
1481         prefs_set_int_attribute("dialogs.save_as", "append_extension", append_extension);
1482         prefs_set_string_attribute("dialogs.save_as", "default",
1483                   ( extension != NULL ? extension->get_id() : "" ));
1484         */
1485         return TRUE;
1486         }
1487     else
1488         {
1489         return FALSE;
1490         }
1494 /**
1495  * Get the file extension type that was selected by the user. Valid after an [OK]
1496  */
1497 Inkscape::Extension::Extension *
1498 FileSaveDialogImpl::getSelectionType()
1500     return extension;
1504 /**
1505  * Get the file name chosen by the user.   Valid after an [OK]
1506  */
1507 Glib::ustring
1508 FileSaveDialogImpl::getFilename()
1510     return myFilename;
1514 void 
1515 FileSaveDialogImpl::change_title(const Glib::ustring& title)
1517     this->set_title(title);
1520 /**
1521   * Change the default save path location.
1522   */
1523 void 
1524 FileSaveDialogImpl::change_path(const Glib::ustring& path)
1526     myFilename = path;
1527     if (Glib::file_test(myFilename, Glib::FILE_TEST_IS_DIR)) {
1528         //fprintf(stderr,"set_current_folder(%s)\n",myFilename.c_str());
1529         set_current_folder(myFilename);
1530     } else {
1531         //fprintf(stderr,"set_filename(%s)\n",myFilename.c_str());
1532         set_filename(myFilename);
1533         Glib::ustring basename = Glib::path_get_basename(myFilename);
1534         //fprintf(stderr,"set_current_name(%s)\n",basename.c_str());
1535         set_current_name(basename);
1536     }
1544 //########################################################################
1545 //# F I L E     E X P O R T
1546 //########################################################################
1549 /**
1550  * Our implementation of the FileExportDialog interface.
1551  */
1552 class FileExportDialogImpl : public FileExportDialog, public FileDialogBase
1555 public:
1556     FileExportDialogImpl(const Glib::ustring &dir,
1557                        FileDialogType fileTypes,
1558                        const Glib::ustring &title,
1559                        const Glib::ustring &default_key);
1561     virtual ~FileExportDialogImpl();
1563     bool show();
1565     Inkscape::Extension::Extension *getSelectionType();
1567     Glib::ustring getFilename();
1570     /**
1571      * Return the scope of the export.  One of the enumerated types
1572      * in ScopeType     
1573      */
1574     ScopeType getScope()
1575         { 
1576         if (pageButton.get_active())
1577             return SCOPE_PAGE;
1578         else if (selectionButton.get_active())
1579             return SCOPE_SELECTION;
1580         else if (customButton.get_active())
1581             return SCOPE_CUSTOM;
1582         else
1583             return SCOPE_DOCUMENT;
1585         }
1586     
1587     /**
1588      * Return left side of the exported region
1589      */
1590     double getSourceX()
1591         { return sourceX0Spinner.getValue(); }
1592     
1593     /**
1594      * Return the top of the exported region
1595      */
1596     double getSourceY()
1597         { return sourceY1Spinner.getValue(); }
1598     
1599     /**
1600      * Return the width of the exported region
1601      */
1602     double getSourceWidth()
1603         { return sourceWidthSpinner.getValue(); }
1604     
1605     /**
1606      * Return the height of the exported region
1607      */
1608     double getSourceHeight()
1609         { return sourceHeightSpinner.getValue(); }
1611     /**
1612      * Return the units of the coordinates of exported region
1613      */
1614     Glib::ustring getSourceUnits()
1615         { return sourceUnitsSpinner.getUnitAbbr(); }
1617     /**
1618      * Return the width of the destination document
1619      */
1620     double getDestinationWidth()
1621         { return destWidthSpinner.getValue(); }
1623     /**
1624      * Return the height of the destination document
1625      */
1626     double getDestinationHeight()
1627         { return destHeightSpinner.getValue(); }
1629     /**
1630      * Return the height of the exported region
1631      */
1632     Glib::ustring getDestinationUnits()
1633         { return destUnitsSpinner.getUnitAbbr(); }
1635     /**
1636      * Return the destination DPI image resulution, if bitmap
1637      */
1638     double getDestinationDPI()
1639         { return destDPISpinner.getValue(); }
1641     /**
1642      * Return whether we should use Cairo for rendering
1643      */
1644     bool getUseCairo()
1645         { return cairoButton.get_active(); }
1647     /**
1648      * Return whether we should use antialiasing
1649      */
1650     bool getUseAntialias()
1651         { return antiAliasButton.get_active(); }
1653     /**
1654      * Return the background color for exporting
1655      */
1656     unsigned long getBackground()
1657         { return backgroundButton.get_color().get_pixel(); }
1659 private:
1661     /**
1662      * What type of 'open' are we? (save, export, etc)
1663      */
1664     FileDialogType dialogType;
1666     /**
1667      * Our svg preview widget
1668      */
1669     SVGPreview svgPreview;
1671     /**
1672      * Fix to allow the user to type the file name
1673      */
1674     Gtk::Entry *fileNameEntry;
1676     /**
1677      * Callback for seeing if the preview needs to be drawn
1678      */
1679     void updatePreviewCallback();
1681     //##########################################
1682     //# EXTRA WIDGET -- SOURCE SIDE
1683     //##########################################
1685     Gtk::Frame            sourceFrame;
1686     Gtk::VBox             sourceBox;
1688     Gtk::HBox             scopeBox;
1689     Gtk::RadioButtonGroup scopeGroup;
1690     Gtk::RadioButton      documentButton;
1691     Gtk::RadioButton      pageButton;
1692     Gtk::RadioButton      selectionButton;
1693     Gtk::RadioButton      customButton;
1695     Gtk::Table                      sourceTable;
1696     Inkscape::UI::Widget::Scalar    sourceX0Spinner;
1697     Inkscape::UI::Widget::Scalar    sourceY0Spinner;
1698     Inkscape::UI::Widget::Scalar    sourceX1Spinner;
1699     Inkscape::UI::Widget::Scalar    sourceY1Spinner;
1700     Inkscape::UI::Widget::Scalar    sourceWidthSpinner;
1701     Inkscape::UI::Widget::Scalar    sourceHeightSpinner;
1702     Inkscape::UI::Widget::UnitMenu  sourceUnitsSpinner;
1705     //##########################################
1706     //# EXTRA WIDGET -- DESTINATION SIDE
1707     //##########################################
1709     Gtk::Frame       destFrame;
1710     Gtk::VBox        destBox;
1712     Gtk::Table                      destTable;
1713     Inkscape::UI::Widget::Scalar    destWidthSpinner;
1714     Inkscape::UI::Widget::Scalar    destHeightSpinner;
1715     Inkscape::UI::Widget::Scalar    destDPISpinner;
1716     Inkscape::UI::Widget::UnitMenu  destUnitsSpinner;
1718     Gtk::HBox        otherOptionBox;
1719     Gtk::CheckButton cairoButton;
1720     Gtk::CheckButton antiAliasButton;
1721     Gtk::ColorButton backgroundButton;
1724     /**
1725      * 'Extra' widget that holds two boxes above
1726      */
1727     Gtk::HBox exportOptionsBox;
1730     //# Child widgets
1731     Gtk::CheckButton fileTypeCheckbox;
1733     /**
1734      * Allow the specification of the output file type
1735      */
1736     Gtk::ComboBoxText fileTypeComboBox;
1739     /**
1740      *  Data mirror of the combo box
1741      */
1742     std::vector<FileType> fileTypes;
1746     /**
1747      * Callback for user input into fileNameEntry
1748      */
1749     void fileTypeChangedCallback();
1751     /**
1752      *  Create a filter menu for this type of dialog
1753      */
1754     void createFileTypeMenu();
1757     bool append_extension;
1759     /**
1760      * The extension to use to write this file
1761      */
1762     Inkscape::Extension::Extension *extension;
1764     /**
1765      * Callback for user input into fileNameEntry
1766      */
1767     void fileNameEntryChangedCallback();
1769     /**
1770      * Filename that was given
1771      */
1772     Glib::ustring myFilename;
1773 };
1780 /**
1781  * Callback for checking if the preview needs to be redrawn
1782  */
1783 void FileExportDialogImpl::updatePreviewCallback()
1785     Glib::ustring fileName = get_preview_filename();
1786     if (!fileName.c_str())
1787         return;
1788     bool retval = svgPreview.set(fileName, dialogType);
1789     set_preview_widget_active(retval);
1794 /**
1795  * Callback for fileNameEntry widget
1796  */
1797 void FileExportDialogImpl::fileNameEntryChangedCallback()
1799     if (!fileNameEntry)
1800         return;
1802     Glib::ustring fileName = fileNameEntry->get_text();
1803     if (!Glib::get_charset()) //If we are not utf8
1804         fileName = Glib::filename_to_utf8(fileName);
1806     //g_message("User hit return.  Text is '%s'\n", fileName.c_str());
1808     if (!Glib::path_is_absolute(fileName)) {
1809         //try appending to the current path
1810         // not this way: fileName = get_current_folder() + "/" + fileName;
1811         std::vector<Glib::ustring> pathSegments;
1812         pathSegments.push_back( get_current_folder() );
1813         pathSegments.push_back( fileName );
1814         fileName = Glib::build_filename(pathSegments);
1815     }
1817     //g_message("path:'%s'\n", fileName.c_str());
1819     if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
1820         set_current_folder(fileName);
1821     } else if (/*Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR)*/1) {
1822         //dialog with either (1) select a regular file or (2) cd to dir
1823         //simulate an 'OK'
1824         set_filename(fileName);
1825         response(Gtk::RESPONSE_OK);
1826     }
1831 /**
1832  * Callback for fileNameEntry widget
1833  */
1834 void FileExportDialogImpl::fileTypeChangedCallback()
1836     int sel = fileTypeComboBox.get_active_row_number();
1837     if (sel<0 || sel >= (int)fileTypes.size())
1838         return;
1839     FileType type = fileTypes[sel];
1840     //g_message("selected: %s\n", type.name.c_str());
1841     Gtk::FileFilter filter;
1842     filter.add_pattern(type.pattern);
1843     set_filter(filter);
1848 void FileExportDialogImpl::createFileTypeMenu()
1850     Inkscape::Extension::DB::OutputList extension_list;
1851     Inkscape::Extension::db.get_output_list(extension_list);
1853     for (Inkscape::Extension::DB::OutputList::iterator current_item = extension_list.begin();
1854          current_item != extension_list.end(); current_item++)
1855     {
1856         Inkscape::Extension::Output * omod = *current_item;
1858         // FIXME: would be nice to grey them out instead of not listing them
1859         if (omod->deactivated()) continue;
1861         FileType type;
1862         type.name     = (_(omod->get_filetypename()));
1863         type.pattern  = "*";
1864         Glib::ustring extension = omod->get_extension();
1865         fileDialogExtensionToPattern (type.pattern, extension);
1866         type.extension= omod;
1867         fileTypeComboBox.append_text(type.name);
1868         fileTypes.push_back(type);
1869     }
1871     //#Let user choose
1872     FileType guessType;
1873     guessType.name = _("Guess from extension");
1874     guessType.pattern = "*";
1875     guessType.extension = NULL;
1876     fileTypeComboBox.append_text(guessType.name);
1877     fileTypes.push_back(guessType);
1880     fileTypeComboBox.set_active(0);
1881     fileTypeChangedCallback(); //call at least once to set the filter
1885 /**
1886  * Constructor
1887  */
1888 FileExportDialogImpl::FileExportDialogImpl(const Glib::ustring &dir,
1889             FileDialogType fileTypes,
1890             const Glib::ustring &title,
1891             const Glib::ustring &default_key) :
1892             FileDialogBase(title, Gtk::FILE_CHOOSER_ACTION_SAVE),
1893             sourceX0Spinner("X0",         _("Source left bound")),
1894             sourceY0Spinner("Y0",         _("Source top bound")),
1895             sourceX1Spinner("X1",         _("Source right bound")),
1896             sourceY1Spinner("Y1",         _("Source bottom bound")),
1897             sourceWidthSpinner("Width",   _("Source width")),
1898             sourceHeightSpinner("Height", _("Source height")),
1899             destWidthSpinner("Width",     _("Destination width")),
1900             destHeightSpinner("Height",   _("Destination height")),
1901             destDPISpinner("DPI",         _("Dots per inch resolution"))
1903     append_extension = (bool)prefs_get_int_attribute("dialogs.save_as", "append_extension", 1);
1905     /* One file at a time */
1906     set_select_multiple(false);
1908     /* Initalize to Autodetect */
1909     extension = NULL;
1910     /* No filename to start out with */
1911     myFilename = "";
1913     /* Set our dialog type (save, export, etc...)*/
1914     dialogType = fileTypes;
1916     /* Set the pwd and/or the filename */
1917     if (dir.size()>0)
1918         {
1919         Glib::ustring udir(dir);
1920         Glib::ustring::size_type len = udir.length();
1921         // leaving a trailing backslash on the directory name leads to the infamous
1922         // double-directory bug on win32
1923         if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
1924         set_current_folder(udir.c_str());
1925         }
1927     //#########################################
1928     //## EXTRA WIDGET -- SOURCE SIDE
1929     //#########################################
1931     //##### Export options buttons/spinners, etc
1932     documentButton.set_label(_("Document"));
1933     scopeBox.pack_start(documentButton);
1934     scopeGroup = documentButton.get_group();
1936     pageButton.set_label(_("Page"));
1937     pageButton.set_group(scopeGroup);
1938     scopeBox.pack_start(pageButton);
1940     selectionButton.set_label(_("Selection"));
1941     selectionButton.set_group(scopeGroup);
1942     scopeBox.pack_start(selectionButton);
1944     customButton.set_label(_("Custom"));
1945     customButton.set_group(scopeGroup);
1946     scopeBox.pack_start(customButton);
1948     sourceBox.pack_start(scopeBox);
1952     //dimension buttons
1953     sourceTable.resize(3,3);
1954     sourceTable.attach(sourceX0Spinner,     0,1,0,1);
1955     sourceTable.attach(sourceY0Spinner,     1,2,0,1);
1956     sourceUnitsSpinner.setUnitType(UNIT_TYPE_LINEAR);
1957     sourceTable.attach(sourceUnitsSpinner,  2,3,0,1);
1958     sourceTable.attach(sourceX1Spinner,     0,1,1,2);
1959     sourceTable.attach(sourceY1Spinner,     1,2,1,2);
1960     sourceTable.attach(sourceWidthSpinner,  0,1,2,3);
1961     sourceTable.attach(sourceHeightSpinner, 1,2,2,3);
1963     sourceBox.pack_start(sourceTable);
1964     sourceFrame.set_label(_("Source"));
1965     sourceFrame.add(sourceBox);
1966     exportOptionsBox.pack_start(sourceFrame);
1969     //#########################################
1970     //## EXTRA WIDGET -- SOURCE SIDE
1971     //#########################################
1974     destTable.resize(3,3);
1975     destTable.attach(destWidthSpinner,    0,1,0,1);
1976     destTable.attach(destHeightSpinner,   1,2,0,1);
1977     destUnitsSpinner.setUnitType(UNIT_TYPE_LINEAR);
1978     destTable.attach(destUnitsSpinner,    2,3,0,1);
1979     destTable.attach(destDPISpinner,      0,1,1,2);
1981     destBox.pack_start(destTable);
1984     cairoButton.set_label(_("Cairo"));
1985     otherOptionBox.pack_start(cairoButton);
1987     antiAliasButton.set_label(_("Antialias"));
1988     otherOptionBox.pack_start(antiAliasButton);
1990     backgroundButton.set_label(_("Background"));
1991     otherOptionBox.pack_start(backgroundButton);
1993     destBox.pack_start(otherOptionBox);
1999     //###### File options
2000     //###### Do we want the .xxx extension automatically added?
2001     fileTypeCheckbox.set_label(Glib::ustring(_("Append filename extension automatically")));
2002     fileTypeCheckbox.set_active(append_extension);
2003     destBox.pack_start(fileTypeCheckbox);
2005     //###### File type menu
2006     createFileTypeMenu();
2007     fileTypeComboBox.set_size_request(200,40);
2008     fileTypeComboBox.signal_changed().connect(
2009          sigc::mem_fun(*this, &FileExportDialogImpl::fileTypeChangedCallback) );
2011     destBox.pack_start(fileTypeComboBox);
2013     destFrame.set_label(_("Destination"));
2014     destFrame.add(destBox);
2015     exportOptionsBox.pack_start(destFrame);
2017     //##### Put the two boxes and their parent onto the dialog    
2018     exportOptionsBox.pack_start(sourceFrame);
2019     exportOptionsBox.pack_start(destFrame);
2021     set_extra_widget(exportOptionsBox);
2026     //###### PREVIEW WIDGET
2027     set_preview_widget(svgPreview);
2028     set_preview_widget_active(true);
2029     set_use_preview_label (false);
2031     //Catch selection-changed events, so we can adjust the text widget
2032     signal_update_preview().connect(
2033          sigc::mem_fun(*this, &FileExportDialogImpl::updatePreviewCallback) );
2036     //Let's do some customization
2037     fileNameEntry = NULL;
2038     Gtk::Container *cont = get_toplevel();
2039     std::vector<Gtk::Entry *> entries;
2040     findEntryWidgets(cont, entries);
2041     //g_message("Found %d entry widgets\n", entries.size());
2042     if (entries.size() >=1 )
2043         {
2044         //Catch when user hits [return] on the text field
2045         fileNameEntry = entries[0];
2046         fileNameEntry->signal_activate().connect(
2047              sigc::mem_fun(*this, &FileExportDialogImpl::fileNameEntryChangedCallback) );
2048         }
2050     //Let's do more customization
2051     std::vector<Gtk::Expander *> expanders;
2052     findExpanderWidgets(cont, expanders);
2053     //g_message("Found %d expander widgets\n", expanders.size());
2054     if (expanders.size() >=1 )
2055         {
2056         //Always show the file list
2057         Gtk::Expander *expander = expanders[0];
2058         expander->set_expanded(true);
2059         }
2062     //if (extension == NULL)
2063     //    checkbox.set_sensitive(FALSE);
2065     add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
2066     add_button(Gtk::Stock::SAVE,   Gtk::RESPONSE_OK);
2068     show_all_children();
2073 /**
2074  * Public factory method.  Used in file.cpp
2075  */
2076 FileExportDialog *FileExportDialog::create(const Glib::ustring &path,
2077                                        FileDialogType fileTypes,
2078                                        const Glib::ustring &title,
2079                                        const Glib::ustring &default_key)
2081     FileExportDialog *dialog = new FileExportDialogImpl(path, fileTypes, title, default_key);
2082     return dialog;
2089 /**
2090  * Destructor
2091  */
2092 FileExportDialogImpl::~FileExportDialogImpl()
2098 /**
2099  * Show this dialog modally.  Return true if user hits [OK]
2100  */
2101 bool
2102 FileExportDialogImpl::show()
2104     Glib::ustring s = Glib::filename_to_utf8 (get_current_folder());
2105     if (s.length() == 0) 
2106         s = getcwd (NULL, 0);
2107     set_current_folder(Glib::filename_from_utf8(s)); //hack to force initial dir listing
2108     set_modal (TRUE);                      //Window
2109     sp_transientize((GtkWidget *)gobj());  //Make transient
2110     gint b = run();                        //Dialog
2111     svgPreview.showNoPreview();
2112     hide();
2114     if (b == Gtk::RESPONSE_OK)
2115         {
2116         int sel = fileTypeComboBox.get_active_row_number ();
2117         if (sel>=0 && sel< (int)fileTypes.size())
2118             {
2119             FileType &type = fileTypes[sel];
2120             extension = type.extension;
2121             }
2122         myFilename = get_filename();
2124         /*
2126         // FIXME: Why do we have more code
2128         append_extension = checkbox.get_active();
2129         prefs_set_int_attribute("dialogs.save_as", "append_extension", append_extension);
2130         prefs_set_string_attribute("dialogs.save_as", "default",
2131                   ( extension != NULL ? extension->get_id() : "" ));
2132         */
2133         return TRUE;
2134         }
2135     else
2136         {
2137         return FALSE;
2138         }
2142 /**
2143  * Get the file extension type that was selected by the user. Valid after an [OK]
2144  */
2145 Inkscape::Extension::Extension *
2146 FileExportDialogImpl::getSelectionType()
2148     return extension;
2152 /**
2153  * Get the file name chosen by the user.   Valid after an [OK]
2154  */
2155 Glib::ustring
2156 FileExportDialogImpl::getFilename()
2158     return myFilename;
2164 } //namespace Dialog
2165 } //namespace UI
2166 } //namespace Inkscape
2169 /*
2170   Local Variables:
2171   mode:c++
2172   c-file-style:"stroustrup"
2173   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2174   indent-tabs-mode:nil
2175   fill-column:99
2176   End:
2177 */
2178 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :