Code

Pot and Dutch translation update
[inkscape.git] / src / ui / dialog / filedialogimpl-gtkmm.cpp
1 /** @file
2  * @brief Implementation of the file dialog interfaces defined in filedialogimpl.h
3  */
4 /* Authors:
5  *   Bob Jamison
6  *   Joel Holdsworth
7  *   Bruno Dilly
8  *   Other dudes from The Inkscape Organization
9  *
10  * Copyright (C) 2004-2007 Bob Jamison
11  * Copyright (C) 2006 Johan Engelen <johan@shouraizou.nl>
12  * Copyright (C) 2007-2008 Joel Holdsworth
13  * Copyright (C) 2004-2007 The Inkscape Organization
14  *
15  * Released under GNU GPL, read the file 'COPYING' for more information
16  */
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
22 #include "filedialogimpl-gtkmm.h"
23 #include "dialogs/dialog-events.h"
24 #include "interface.h"
25 #include "io/sys.h"
26 #include "path-prefix.h"
27 #include "preferences.h"
29 #ifdef WITH_GNOME_VFS
30 # include <libgnomevfs/gnome-vfs.h>
31 #endif
33 //Routines from file.cpp
34 #undef INK_DUMP_FILENAME_CONV
36 #ifdef INK_DUMP_FILENAME_CONV
37 void dump_str( const gchar* str, const gchar* prefix );
38 void dump_ustr( const Glib::ustring& ustr );
39 #endif
43 namespace Inkscape
44 {
45 namespace UI
46 {
47 namespace Dialog
48 {
54 //########################################################################
55 //### U T I L I T Y
56 //########################################################################
58 void
59 fileDialogExtensionToPattern(Glib::ustring &pattern,
60                       Glib::ustring &extension)
61 {
62     for (unsigned int i = 0; i < extension.length(); i++ )
63         {
64         Glib::ustring::value_type ch = extension[i];
65         if ( Glib::Unicode::isalpha(ch) )
66             {
67             pattern += '[';
68             pattern += Glib::Unicode::toupper(ch);
69             pattern += Glib::Unicode::tolower(ch);
70             pattern += ']';
71             }
72         else
73             {
74             pattern += ch;
75             }
76         }
77 }
80 void
81 findEntryWidgets(Gtk::Container *parent,
82                  std::vector<Gtk::Entry *> &result)
83 {
84     if (!parent)
85         return;
86     std::vector<Gtk::Widget *> children = parent->get_children();
87     for (unsigned int i=0; i<children.size() ; i++)
88         {
89         Gtk::Widget *child = children[i];
90         GtkWidget *wid = child->gobj();
91         if (GTK_IS_ENTRY(wid))
92            result.push_back((Gtk::Entry *)child);
93         else if (GTK_IS_CONTAINER(wid))
94             findEntryWidgets((Gtk::Container *)child, result);
95         }
97 }
99 void
100 findExpanderWidgets(Gtk::Container *parent,
101                     std::vector<Gtk::Expander *> &result)
103     if (!parent)
104         return;
105     std::vector<Gtk::Widget *> children = parent->get_children();
106     for (unsigned int i=0; i<children.size() ; i++)
107         {
108         Gtk::Widget *child = children[i];
109         GtkWidget *wid = child->gobj();
110         if (GTK_IS_EXPANDER(wid))
111            result.push_back((Gtk::Expander *)child);
112         else if (GTK_IS_CONTAINER(wid))
113             findExpanderWidgets((Gtk::Container *)child, result);
114         }
119 /*#########################################################################
120 ### SVG Preview Widget
121 #########################################################################*/
123 bool SVGPreview::setDocument(SPDocument *doc)
125     if (document)
126         sp_document_unref(document);
128     sp_document_ref(doc);
129     document = doc;
131     //This should remove it from the box, and free resources
132     if (viewerGtk)
133         gtk_widget_destroy(viewerGtk);
135     viewerGtk  = sp_svg_view_widget_new(doc);
136     GtkWidget *vbox = (GtkWidget *)gobj();
137     gtk_box_pack_start(GTK_BOX(vbox), viewerGtk, TRUE, TRUE, 0);
138     gtk_widget_show(viewerGtk);
140     return true;
144 bool SVGPreview::setFileName(Glib::ustring &theFileName)
146     Glib::ustring fileName = theFileName;
148     fileName = Glib::filename_to_utf8(fileName);
150     /**
151      * I don't know why passing false to keepalive is bad.  But it
152      * prevents the display of an svg with a non-ascii filename
153      */
154     SPDocument *doc = sp_document_new (fileName.c_str(), true);
155     if (!doc) {
156         g_warning("SVGView: error loading document '%s'\n", fileName.c_str());
157         return false;
158     }
160     setDocument(doc);
162     sp_document_unref(doc);
164     return true;
169 bool SVGPreview::setFromMem(char const *xmlBuffer)
171     if (!xmlBuffer)
172         return false;
174     gint len = (gint)strlen(xmlBuffer);
175     SPDocument *doc = sp_document_new_from_mem(xmlBuffer, len, 0);
176     if (!doc) {
177         g_warning("SVGView: error loading buffer '%s'\n",xmlBuffer);
178         return false;
179     }
181     setDocument(doc);
183     sp_document_unref(doc);
185     Inkscape::GC::request_early_collection();
187     return true;
192 void SVGPreview::showImage(Glib::ustring &theFileName)
194     Glib::ustring fileName = theFileName;
197     /*#####################################
198     # LET'S HAVE SOME FUN WITH SVG!
199     # Instead of just loading an image, why
200     # don't we make a lovely little svg and
201     # display it nicely?
202     #####################################*/
204     //Arbitrary size of svg doc -- rather 'portrait' shaped
205     gint previewWidth  = 400;
206     gint previewHeight = 600;
208     //Get some image info. Smart pointer does not need to be deleted
209     Glib::RefPtr<Gdk::Pixbuf> img(NULL);
210     try {
211         img = Gdk::Pixbuf::create_from_file(fileName);
212     }
213     catch (const Glib::FileError & e)
214     {
215         g_message("caught Glib::FileError in SVGPreview::showImage");
216         return;
217     }
218     catch (const Gdk::PixbufError & e)
219     {
220         g_message("Gdk::PixbufError in SVGPreview::showImage");
221         return;
222     }
223     catch (...)
224     {
225         g_message("Caught ... in SVGPreview::showImage");
226         return;
227     }
229     gint imgWidth  = img->get_width();
230     gint imgHeight = img->get_height();
232     //Find the minimum scale to fit the image inside the preview area
233     double scaleFactorX = (0.9 *(double)previewWidth)  / ((double)imgWidth);
234     double scaleFactorY = (0.9 *(double)previewHeight) / ((double)imgHeight);
235     double scaleFactor = scaleFactorX;
236     if (scaleFactorX > scaleFactorY)
237         scaleFactor = scaleFactorY;
239     //Now get the resized values
240     gint scaledImgWidth  = (int) (scaleFactor * (double)imgWidth);
241     gint scaledImgHeight = (int) (scaleFactor * (double)imgHeight);
243     //center the image on the area
244     gint imgX = (previewWidth  - scaledImgWidth)  / 2;
245     gint imgY = (previewHeight - scaledImgHeight) / 2;
247     //wrap a rectangle around the image
248     gint rectX      = imgX-1;
249     gint rectY      = imgY-1;
250     gint rectWidth  = scaledImgWidth +2;
251     gint rectHeight = scaledImgHeight+2;
253     //Our template.  Modify to taste
254     gchar const *xformat =
255           "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
256           "<svg\n"
257           "xmlns=\"http://www.w3.org/2000/svg\"\n"
258           "xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
259           "width=\"%d\" height=\"%d\">\n"  //# VALUES HERE
260           "<rect\n"
261           "  style=\"fill:#eeeeee;stroke:none\"\n"
262           "  x=\"-100\" y=\"-100\" width=\"4000\" height=\"4000\"/>\n"
263           "<image x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\"\n"
264           "xlink:href=\"%s\"/>\n"
265           "<rect\n"
266           "  style=\"fill:none;"
267           "    stroke:#000000;stroke-width:1.0;"
268           "    stroke-linejoin:miter;stroke-opacity:1.0000000;"
269           "    stroke-miterlimit:4.0000000;stroke-dasharray:none\"\n"
270           "  x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\"/>\n"
271           "<text\n"
272           "  style=\"font-size:24.000000;font-style:normal;font-weight:normal;"
273           "    fill:#000000;fill-opacity:1.0000000;stroke:none;"
274           "    font-family:Sans\"\n"
275           "  x=\"10\" y=\"26\">%d x %d</text>\n" //# VALUES HERE
276           "</svg>\n\n";
278     //if (!Glib::get_charset()) //If we are not utf8
279     fileName = Glib::filename_to_utf8(fileName);
281     //Fill in the template
282     /* FIXME: Do proper XML quoting for fileName. */
283     gchar *xmlBuffer = g_strdup_printf(xformat,
284            previewWidth, previewHeight,
285            imgX, imgY, scaledImgWidth, scaledImgHeight,
286            fileName.c_str(),
287            rectX, rectY, rectWidth, rectHeight,
288            imgWidth, imgHeight);
290     //g_message("%s\n", xmlBuffer);
292     //now show it!
293     setFromMem(xmlBuffer);
294     g_free(xmlBuffer);
299 void SVGPreview::showNoPreview()
301     //Are we already showing it?
302     if (showingNoPreview)
303         return;
305     //Arbitrary size of svg doc -- rather 'portrait' shaped
306     gint previewWidth  = 300;
307     gint previewHeight = 600;
309     //Our template.  Modify to taste
310     gchar const *xformat =
311           "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
312           "<svg\n"
313           "xmlns=\"http://www.w3.org/2000/svg\"\n"
314           "xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
315           "width=\"%d\" height=\"%d\">\n" //# VALUES HERE
316           "<g transform=\"translate(-190,24.27184)\" style=\"opacity:0.12\">\n"
317           "<path\n"
318           "style=\"font-size:12;fill:#ffffff;fill-rule:evenodd;stroke:#000000;stroke-width:0.936193pt\"\n"
319           "d=\"M 397.64309 320.25301 L 280.39197 282.517 L 250.74227 124.83447 L 345.08225 "
320           "29.146783 L 393.59996 46.667064 L 483.89679 135.61619 L 397.64309 320.25301 z \"\n"
321           "id=\"whiteSpace\" />\n"
322           "<path\n"
323           "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
324           "d=\"M 476.95792 339.17168 C 495.78197 342.93607 499.54842 356.11361 495.78197 359.87802 "
325           "C 492.01856 363.6434 482.6065 367.40781 475.07663 361.76014 C 467.54478 "
326           "356.11361 467.54478 342.93607 476.95792 339.17168 z \"\n"
327           "id=\"droplet01\" />\n"
328           "<path\n"
329           "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
330           "d=\"M 286.46194 340.42914 C 284.6277 340.91835 269.30405 327.71337 257.16909 333.8338 "
331           "C 245.03722 339.95336 236.89276 353.65666 248.22676 359.27982 C 259.56184 364.90298 "
332           "267.66433 358.41867 277.60113 351.44119 C 287.53903 344.46477 "
333           "287.18046 343.1206 286.46194 340.42914 z \"\n"
334           "id=\"droplet02\" />\n"
335           "<path\n"
336           "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
337           "d=\"M 510.35756 306.92856 C 520.59494 304.36879 544.24333 306.92856 540.47688 321.98634 "
338           "C 536.71354 337.04806 504.71297 331.39827 484.00371 323.87156 C 482.12141 "
339           "308.81083 505.53237 308.13423 510.35756 306.92856 z \"\n"
340           "id=\"droplet03\" />\n"
341           "<path\n"
342           "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
343           "d=\"M 359.2403 21.362537 C 347.92693 21.362537 336.6347 25.683095 327.96556 34.35223 "
344           "L 173.87387 188.41466 C 165.37697 196.9114 161.1116 207.95813 160.94269 219.04577 "
345           "L 160.88418 219.04577 C 160.88418 219.08524 160.94076 219.12322 160.94269 219.16279 "
346           "C 160.94033 219.34888 160.88418 219.53256 160.88418 219.71865 L 161.14748 219.71865 "
347           "C 164.0966 230.93917 240.29699 245.24198 248.79866 253.74346 C 261.63771 266.58263 "
348           "199.5652 276.01151 212.4041 288.85074 C 225.24316 301.68979 289.99433 313.6933 302.8346 "
349           "326.53254 C 315.67368 339.37161 276.5961 353.04289 289.43532 365.88196 C 302.27439 "
350           "378.72118 345.40201 362.67257 337.5908 396.16198 C 354.92909 413.50026 391.10302 "
351           "405.2208 415.32417 387.88252 C 428.16323 375.04345 390.6948 376.17577 403.53397 "
352           "363.33668 C 416.37304 350.49745 448.78128 350.4282 476.08902 319.71589 C 465.09739 "
353           "302.62116 429.10801 295.34136 441.94719 282.50217 C 454.78625 269.66311 479.74708 "
354           "276.18423 533.60644 251.72479 C 559.89837 239.78398 557.72636 230.71459 557.62567 "
355           "219.71865 C 557.62356 219.48727 557.62567 219.27892 557.62567 219.04577 L 557.56716 "
356           "219.04577 C 557.3983 207.95812 553.10345 196.9114 544.60673 188.41466 L 390.54428 "
357           "34.35223 C 381.87515 25.683095 370.55366 21.362537 359.2403 21.362537 z M 357.92378 "
358           "41.402939 C 362.95327 41.533963 367.01541 45.368018 374.98006 50.530832 L 447.76915 "
359           "104.50827 C 448.56596 105.02498 449.32484 105.564 450.02187 106.11735 C 450.7189 106.67062 "
360           "451.3556 107.25745 451.95277 107.84347 C 452.54997 108.42842 453.09281 109.01553 453.59111 "
361           "109.62808 C 454.08837 110.24052 454.53956 110.86661 454.93688 111.50048 C 455.33532 112.13538 "
362           "455.69164 112.78029 455.9901 113.43137 C 456.28877 114.08363 456.52291 114.75639 456.7215 "
363           "115.42078 C 456.92126 116.08419 457.08982 116.73973 457.18961 117.41019 C 457.28949 "
364           "118.08184 457.33588 118.75535 457.33588 119.42886 L 414.21245 98.598549 L 409.9118 "
365           "131.16055 L 386.18512 120.04324 L 349.55654 144.50131 L 335.54288 96.1703 L 317.4919 "
366           "138.4453 L 267.08369 143.47735 L 267.63956 121.03795 C 267.63956 115.64823 296.69685 "
367           "77.915899 314.39075 68.932902 L 346.77721 45.674327 C 351.55594 42.576634 354.90608 "
368           "41.324327 357.92378 41.402939 z M 290.92738 261.61333 C 313.87149 267.56365 339.40299 "
369           "275.37038 359.88393 275.50997 L 360.76161 284.72563 C 343.2235 282.91785 306.11346 "
370           "274.45012 297.36372 269.98057 L 290.92738 261.61333 z \"\n"
371           "id=\"mountainDroplet\" />\n"
372           "</g> <g transform=\"translate(-20,0)\">\n"
373           "<text xml:space=\"preserve\"\n"
374           "style=\"font-size:32.000000;font-style:normal;font-variant:normal;font-weight:bold;"
375           "font-stretch:normal;fill:#000000;fill-opacity:1.0000000;stroke:none;stroke-width:1.0000000pt;"
376           "stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1.0000000;"
377           "font-family:Sans;text-anchor:middle;writing-mode:lr\"\n"
378           "x=\"190\" y=\"240\">%s</text></g>\n"  //# VALUE HERE
379           "</svg>\n\n";
381     //Fill in the template
382     gchar *xmlBuffer = g_strdup_printf(xformat,
383            previewWidth, previewHeight, _("No preview"));
385     //g_message("%s\n", xmlBuffer);
387     //now show it!
388     setFromMem(xmlBuffer);
389     g_free(xmlBuffer);
390     showingNoPreview = true;
395 /**
396  * Inform the user that the svg file is too large to be displayed.
397  * This does not check for sizes of embedded images (yet)
398  */
399 void SVGPreview::showTooLarge(long fileLength)
402     //Arbitrary size of svg doc -- rather 'portrait' shaped
403     gint previewWidth  = 300;
404     gint previewHeight = 600;
406     //Our template.  Modify to taste
407     gchar const *xformat =
408           "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
409           "<svg\n"
410           "xmlns=\"http://www.w3.org/2000/svg\"\n"
411           "xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
412           "width=\"%d\" height=\"%d\">\n"  //# VALUES HERE
413           "<g transform=\"translate(-170,24.27184)\" style=\"opacity:0.12\">\n"
414           "<path\n"
415           "style=\"font-size:12;fill:#ffffff;fill-rule:evenodd;stroke:#000000;stroke-width:0.936193pt\"\n"
416           "d=\"M 397.64309 320.25301 L 280.39197 282.517 L 250.74227 124.83447 L 345.08225 "
417           "29.146783 L 393.59996 46.667064 L 483.89679 135.61619 L 397.64309 320.25301 z \"\n"
418           "id=\"whiteSpace\" />\n"
419           "<path\n"
420           "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
421           "d=\"M 476.95792 339.17168 C 495.78197 342.93607 499.54842 356.11361 495.78197 359.87802 "
422           "C 492.01856 363.6434 482.6065 367.40781 475.07663 361.76014 C 467.54478 "
423           "356.11361 467.54478 342.93607 476.95792 339.17168 z \"\n"
424           "id=\"droplet01\" />\n"
425           "<path\n"
426           "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
427           "d=\"M 286.46194 340.42914 C 284.6277 340.91835 269.30405 327.71337 257.16909 333.8338 "
428           "C 245.03722 339.95336 236.89276 353.65666 248.22676 359.27982 C 259.56184 364.90298 "
429           "267.66433 358.41867 277.60113 351.44119 C 287.53903 344.46477 "
430           "287.18046 343.1206 286.46194 340.42914 z \"\n"
431           "id=\"droplet02\" />\n"
432           "<path\n"
433           "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
434           "d=\"M 510.35756 306.92856 C 520.59494 304.36879 544.24333 306.92856 540.47688 321.98634 "
435           "C 536.71354 337.04806 504.71297 331.39827 484.00371 323.87156 C 482.12141 "
436           "308.81083 505.53237 308.13423 510.35756 306.92856 z \"\n"
437           "id=\"droplet03\" />\n"
438           "<path\n"
439           "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
440           "d=\"M 359.2403 21.362537 C 347.92693 21.362537 336.6347 25.683095 327.96556 34.35223 "
441           "L 173.87387 188.41466 C 165.37697 196.9114 161.1116 207.95813 160.94269 219.04577 "
442           "L 160.88418 219.04577 C 160.88418 219.08524 160.94076 219.12322 160.94269 219.16279 "
443           "C 160.94033 219.34888 160.88418 219.53256 160.88418 219.71865 L 161.14748 219.71865 "
444           "C 164.0966 230.93917 240.29699 245.24198 248.79866 253.74346 C 261.63771 266.58263 "
445           "199.5652 276.01151 212.4041 288.85074 C 225.24316 301.68979 289.99433 313.6933 302.8346 "
446           "326.53254 C 315.67368 339.37161 276.5961 353.04289 289.43532 365.88196 C 302.27439 "
447           "378.72118 345.40201 362.67257 337.5908 396.16198 C 354.92909 413.50026 391.10302 "
448           "405.2208 415.32417 387.88252 C 428.16323 375.04345 390.6948 376.17577 403.53397 "
449           "363.33668 C 416.37304 350.49745 448.78128 350.4282 476.08902 319.71589 C 465.09739 "
450           "302.62116 429.10801 295.34136 441.94719 282.50217 C 454.78625 269.66311 479.74708 "
451           "276.18423 533.60644 251.72479 C 559.89837 239.78398 557.72636 230.71459 557.62567 "
452           "219.71865 C 557.62356 219.48727 557.62567 219.27892 557.62567 219.04577 L 557.56716 "
453           "219.04577 C 557.3983 207.95812 553.10345 196.9114 544.60673 188.41466 L 390.54428 "
454           "34.35223 C 381.87515 25.683095 370.55366 21.362537 359.2403 21.362537 z M 357.92378 "
455           "41.402939 C 362.95327 41.533963 367.01541 45.368018 374.98006 50.530832 L 447.76915 "
456           "104.50827 C 448.56596 105.02498 449.32484 105.564 450.02187 106.11735 C 450.7189 106.67062 "
457           "451.3556 107.25745 451.95277 107.84347 C 452.54997 108.42842 453.09281 109.01553 453.59111 "
458           "109.62808 C 454.08837 110.24052 454.53956 110.86661 454.93688 111.50048 C 455.33532 112.13538 "
459           "455.69164 112.78029 455.9901 113.43137 C 456.28877 114.08363 456.52291 114.75639 456.7215 "
460           "115.42078 C 456.92126 116.08419 457.08982 116.73973 457.18961 117.41019 C 457.28949 "
461           "118.08184 457.33588 118.75535 457.33588 119.42886 L 414.21245 98.598549 L 409.9118 "
462           "131.16055 L 386.18512 120.04324 L 349.55654 144.50131 L 335.54288 96.1703 L 317.4919 "
463           "138.4453 L 267.08369 143.47735 L 267.63956 121.03795 C 267.63956 115.64823 296.69685 "
464           "77.915899 314.39075 68.932902 L 346.77721 45.674327 C 351.55594 42.576634 354.90608 "
465           "41.324327 357.92378 41.402939 z M 290.92738 261.61333 C 313.87149 267.56365 339.40299 "
466           "275.37038 359.88393 275.50997 L 360.76161 284.72563 C 343.2235 282.91785 306.11346 "
467           "274.45012 297.36372 269.98057 L 290.92738 261.61333 z \"\n"
468           "id=\"mountainDroplet\" />\n"
469           "</g>\n"
470           "<text xml:space=\"preserve\"\n"
471           "style=\"font-size:32.000000;font-style:normal;font-variant:normal;font-weight:bold;"
472           "font-stretch:normal;fill:#000000;fill-opacity:1.0000000;stroke:none;stroke-width:1.0000000pt;"
473           "stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1.0000000;"
474           "font-family:Sans;text-anchor:middle;writing-mode:lr\"\n"
475           "x=\"170\" y=\"215\">%5.1f MB</text>\n" //# VALUE HERE
476           "<text xml:space=\"preserve\"\n"
477           "style=\"font-size:24.000000;font-style:normal;font-variant:normal;font-weight:bold;"
478           "font-stretch:normal;fill:#000000;fill-opacity:1.0000000;stroke:none;stroke-width:1.0000000pt;"
479           "stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1.0000000;"
480           "font-family:Sans;text-anchor:middle;writing-mode:lr\"\n"
481           "x=\"180\" y=\"245\">%s</text>\n" //# VALUE HERE
482           "</svg>\n\n";
484     //Fill in the template
485     double floatFileLength = ((double)fileLength) / 1048576.0;
486     //printf("%ld %f\n", fileLength, floatFileLength);
487     gchar *xmlBuffer = g_strdup_printf(xformat,
488            previewWidth, previewHeight, floatFileLength,
489            _("too large for preview"));
491     //g_message("%s\n", xmlBuffer);
493     //now show it!
494     setFromMem(xmlBuffer);
495     g_free(xmlBuffer);
499 bool SVGPreview::set(Glib::ustring &fileName, int dialogType)
502     if (!Glib::file_test(fileName, Glib::FILE_TEST_EXISTS))
503         return false;
505     //g_message("fname:%s", fileName.c_str());
507     if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
508         showNoPreview();
509         return false;
510     }
512     if (Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR))
513         {
514         Glib::ustring fileNameUtf8 = Glib::filename_to_utf8(fileName);
515         gchar *fName = (gchar *)fileNameUtf8.c_str();
516         struct stat info;
517         if (g_stat(fName, &info))
518             {
519             g_warning("SVGPreview::set() : %s : %s",
520                            fName, strerror(errno));
521             return FALSE;
522             }
523         long fileLen = info.st_size;
524         if (fileLen > 0xA00000L)
525             {
526             showingNoPreview = false;
527             showTooLarge(fileLen);
528             return FALSE;
529             }
530         }
532     Glib::ustring svg = ".svg";
533     Glib::ustring svgz = ".svgz";
535     if ((dialogType == SVG_TYPES || dialogType == IMPORT_TYPES) &&
536            (hasSuffix(fileName, svg) || hasSuffix(fileName, svgz)   )
537         ) {
538         bool retval = setFileName(fileName);
539         showingNoPreview = false;
540         return retval;
541     } else if (isValidImageFile(fileName)) {
542         showImage(fileName);
543         showingNoPreview = false;
544         return true;
545     } else {
546         showNoPreview();
547         return false;
548     }
552 SVGPreview::SVGPreview()
554     if (!INKSCAPE)
555         inkscape_application_init("",false);
556     document = NULL;
557     viewerGtk = NULL;
558     set_size_request(150,150);
559     showingNoPreview = false;
562 SVGPreview::~SVGPreview()
568 /*#########################################################################
569 ### F I L E     D I A L O G    B A S E    C L A S S
570 #########################################################################*/
572 void FileDialogBaseGtk::internalSetup()
574     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
575     bool enablePreview = prefs->getBool( preferenceBase + "/enable_preview", true);
577     previewCheckbox.set_label( Glib::ustring(_("Enable preview")) );
578     previewCheckbox.set_active( enablePreview );
580     previewCheckbox.signal_toggled().connect(
581         sigc::mem_fun(*this, &FileDialogBaseGtk::_previewEnabledCB) );
583     //Catch selection-changed events, so we can adjust the text widget
584     signal_update_preview().connect(
585          sigc::mem_fun(*this, &FileDialogBaseGtk::_updatePreviewCallback) );
587     //###### Add a preview widget
588     set_preview_widget(svgPreview);
589     set_preview_widget_active( enablePreview );
590     set_use_preview_label (false);
595 void FileDialogBaseGtk::cleanup( bool showConfirmed )
597     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
598     if ( showConfirmed )
599         prefs->setBool( preferenceBase + "/enable_preview", previewCheckbox.get_active() );
603 void FileDialogBaseGtk::_previewEnabledCB()
605     bool enabled = previewCheckbox.get_active();
606     set_preview_widget_active(enabled);
607     if ( enabled ) {
608         _updatePreviewCallback();
609     }
614 /**
615  * Callback for checking if the preview needs to be redrawn
616  */
617 void FileDialogBaseGtk::_updatePreviewCallback()
619     Glib::ustring fileName = get_preview_filename();
621 #ifdef WITH_GNOME_VFS
622     if ( fileName.empty() && gnome_vfs_initialized() ) {
623         fileName = get_preview_uri();
624     }
625 #endif
627     if (fileName.empty()) {
628         return;
629     }
631     svgPreview.set(fileName, _dialogType);
635 /*#########################################################################
636 ### F I L E    O P E N
637 #########################################################################*/
639 /**
640  * Constructor.  Not called directly.  Use the factory.
641  */
642 FileOpenDialogImplGtk::FileOpenDialogImplGtk(Gtk::Window& parentWindow,
643                                        const Glib::ustring &dir,
644                                        FileDialogType fileTypes,
645                                        const Glib::ustring &title) :
646     FileDialogBaseGtk(parentWindow, title, Gtk::FILE_CHOOSER_ACTION_OPEN, fileTypes, "/dialogs/open")
650     /* One file at a time */
651     /* And also Multiple Files */
652     set_select_multiple(true);
654 #ifdef WITH_GNOME_VFS
655     if (gnome_vfs_initialized()) {
656         set_local_only(false);
657     }
658 #endif
660     /* Initalize to Autodetect */
661     extension = NULL;
662     /* No filename to start out with */
663     myFilename = "";
665     /* Set our dialog type (open, import, etc...)*/
666     _dialogType = fileTypes;
669     /* Set the pwd and/or the filename */
670     if (dir.size() > 0)
671         {
672         Glib::ustring udir(dir);
673         Glib::ustring::size_type len = udir.length();
674         // leaving a trailing backslash on the directory name leads to the infamous
675         // double-directory bug on win32
676         if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
677         set_current_folder(udir.c_str());
678         }
681     set_extra_widget( previewCheckbox );
684     //###### Add the file types menu
685     createFilterMenu();
687     add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
688     set_default(*add_button(Gtk::Stock::OPEN,   Gtk::RESPONSE_OK));
690     //###### Allow easy access to our examples folder
691     if ( Inkscape::IO::file_test(INKSCAPE_EXAMPLESDIR, G_FILE_TEST_EXISTS)
692          && Inkscape::IO::file_test(INKSCAPE_EXAMPLESDIR, G_FILE_TEST_IS_DIR)
693          && g_path_is_absolute(INKSCAPE_EXAMPLESDIR)
694         )
695     {
696         add_shortcut_folder(INKSCAPE_EXAMPLESDIR);
697     }
700 /**
701  * Destructor
702  */
703 FileOpenDialogImplGtk::~FileOpenDialogImplGtk()
708 void FileOpenDialogImplGtk::createFilterMenu()
710     Gtk::FileFilter allInkscapeFilter;
711     allInkscapeFilter.set_name(_("All Inkscape Files"));
712     extensionMap[Glib::ustring(_("All Inkscape Files"))]=NULL;
713     add_filter(allInkscapeFilter);
715     Gtk::FileFilter allFilter;
716     allFilter.set_name(_("All Files"));
717     extensionMap[Glib::ustring(_("All Files"))]=NULL;
718     allFilter.add_pattern("*");
719     add_filter(allFilter);
721     Gtk::FileFilter allImageFilter;
722     allImageFilter.set_name(_("All Images"));
723     extensionMap[Glib::ustring(_("All Images"))]=NULL;
724     add_filter(allImageFilter);
726     Gtk::FileFilter allVectorFilter;
727     allVectorFilter.set_name(_("All Vectors"));
728     extensionMap[Glib::ustring(_("All Vectors"))]=NULL;
729     add_filter(allVectorFilter);
731     Gtk::FileFilter allBitmapFilter;
732     allBitmapFilter.set_name(_("All Bitmaps"));
733     extensionMap[Glib::ustring(_("All Bitmaps"))]=NULL;
734     add_filter(allBitmapFilter);
736     //patterns added dynamically below
737     Inkscape::Extension::DB::InputList extension_list;
738     Inkscape::Extension::db.get_input_list(extension_list);
740     for (Inkscape::Extension::DB::InputList::iterator current_item = extension_list.begin();
741          current_item != extension_list.end(); current_item++)
742     {
743         Inkscape::Extension::Input * imod = *current_item;
745         // FIXME: would be nice to grey them out instead of not listing them
746         if (imod->deactivated()) continue;
748         Glib::ustring upattern("*");
749         Glib::ustring extension = imod->get_extension();
750         fileDialogExtensionToPattern(upattern, extension);
752         Gtk::FileFilter filter;
753         Glib::ustring uname(_(imod->get_filetypename()));
754         filter.set_name(uname);
755         filter.add_pattern(upattern);
756         add_filter(filter);
757         extensionMap[uname] = imod;
759         //g_message("ext %s:%s '%s'\n", ioext->name, ioext->mimetype, upattern.c_str());
760         allInkscapeFilter.add_pattern(upattern);
761         if ( strncmp("image", imod->get_mimetype(), 5)==0 )
762             allImageFilter.add_pattern(upattern);
764         // uncomment this to find out all mime types supported by Inkscape import/open
765         // g_print ("%s\n", imod->get_mimetype());
767         // I don't know of any other way to define "bitmap" formats other than by listing them
768         if ( 
769             strncmp("image/png", imod->get_mimetype(), 9)==0 ||
770             strncmp("image/jpeg", imod->get_mimetype(), 10)==0 ||
771             strncmp("image/gif", imod->get_mimetype(), 9)==0 ||
772             strncmp("image/x-icon", imod->get_mimetype(), 12)==0 ||
773             strncmp("image/x-navi-animation", imod->get_mimetype(), 22)==0 ||
774             strncmp("image/x-cmu-raster", imod->get_mimetype(), 18)==0 ||
775             strncmp("image/x-xpixmap", imod->get_mimetype(), 15)==0 ||
776             strncmp("image/bmp", imod->get_mimetype(), 9)==0 ||
777             strncmp("image/vnd.wap.wbmp", imod->get_mimetype(), 18)==0 ||
778             strncmp("image/tiff", imod->get_mimetype(), 10)==0 ||
779             strncmp("image/x-xbitmap", imod->get_mimetype(), 15)==0 ||
780             strncmp("image/x-tga", imod->get_mimetype(), 11)==0 ||
781             strncmp("image/x-pcx", imod->get_mimetype(), 11)==0 
782            )
783             allBitmapFilter.add_pattern(upattern);
784         else 
785             allVectorFilter.add_pattern(upattern);
786     }
788     return;
791 /**
792  * Show this dialog modally.  Return true if user hits [OK]
793  */
794 bool
795 FileOpenDialogImplGtk::show()
797     set_modal (TRUE);                      //Window
798     sp_transientize((GtkWidget *)gobj());  //Make transient
799     gint b = run();                        //Dialog
800     svgPreview.showNoPreview();
801     hide();
803     if (b == Gtk::RESPONSE_OK)
804         {
805         //This is a hack, to avoid the warning messages that
806         //Gtk::FileChooser::get_filter() returns
807         //should be:  Gtk::FileFilter *filter = get_filter();
808         GtkFileChooser *gtkFileChooser = Gtk::FileChooser::gobj();
809         GtkFileFilter *filter = gtk_file_chooser_get_filter(gtkFileChooser);
810         if (filter)
811             {
812             //Get which extension was chosen, if any
813             extension = extensionMap[gtk_file_filter_get_name(filter)];
814             }
815         myFilename = get_filename();
816 #ifdef WITH_GNOME_VFS
817         if (myFilename.empty() && gnome_vfs_initialized())
818             myFilename = get_uri();
819 #endif
820         cleanup( true );
821         return TRUE;
822         }
823     else
824        {
825        cleanup( false );
826        return FALSE;
827        }
833 /**
834  * Get the file extension type that was selected by the user. Valid after an [OK]
835  */
836 Inkscape::Extension::Extension *
837 FileOpenDialogImplGtk::getSelectionType()
839     return extension;
843 /**
844  * Get the file name chosen by the user.   Valid after an [OK]
845  */
846 Glib::ustring
847 FileOpenDialogImplGtk::getFilename (void)
849     return myFilename;
853 /**
854  * To Get Multiple filenames selected at-once.
855  */
856 std::vector<Glib::ustring>FileOpenDialogImplGtk::getFilenames()
858     std::vector<Glib::ustring> result = get_filenames();
859 #ifdef WITH_GNOME_VFS
860     if (result.empty() && gnome_vfs_initialized())
861         result = get_uris();
862 #endif
863     return result;
866 Glib::ustring FileOpenDialogImplGtk::getCurrentDirectory()
868     return get_current_folder();
874 //########################################################################
875 //# F I L E    S A V E
876 //########################################################################
878 /**
879  * Constructor
880  */
881 FileSaveDialogImplGtk::FileSaveDialogImplGtk( Gtk::Window &parentWindow,
882                                               const Glib::ustring &dir,
883                                               FileDialogType fileTypes,
884                                               const Glib::ustring &title,
885                                               const Glib::ustring &/*default_key*/,
886                                               const gchar* docTitle,
887                                               const Inkscape::Extension::FileSaveMethod save_method) :
888     FileDialogBaseGtk(parentWindow, title, Gtk::FILE_CHOOSER_ACTION_SAVE, fileTypes,
889                       (save_method == Inkscape::Extension::FILE_SAVE_METHOD_SAVE_COPY) ? "/dialogs/save_copy" : "/dialogs/save_as"),
890     save_method(save_method)
892     FileSaveDialog::myDocTitle = docTitle;
894     /* One file at a time */
895     set_select_multiple(false);
897 #ifdef WITH_GNOME_VFS
898     if (gnome_vfs_initialized()) {
899         set_local_only(false);
900     }
901 #endif
903     /* Initalize to Autodetect */
904     extension = NULL;
905     /* No filename to start out with */
906     myFilename = "";
908     /* Set our dialog type (save, export, etc...)*/
909     _dialogType = fileTypes;
911     /* Set the pwd and/or the filename */
912     if (dir.size() > 0)
913         {
914         Glib::ustring udir(dir);
915         Glib::ustring::size_type len = udir.length();
916         // leaving a trailing backslash on the directory name leads to the infamous
917         // double-directory bug on win32
918         if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
919         myFilename = udir;
920         }
922     //###### Add the file types menu
923     //createFilterMenu();
925     //###### Do we want the .xxx extension automatically added?
926     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
927     fileTypeCheckbox.set_label(Glib::ustring(_("Append filename extension automatically")));
928     if (save_method == Inkscape::Extension::FILE_SAVE_METHOD_SAVE_COPY) {
929         fileTypeCheckbox.set_active(prefs->getBool("/dialogs/save_copy/append_extension", true));
930     } else {
931         fileTypeCheckbox.set_active(prefs->getBool("/dialogs/save_as/append_extension", true));
932     }
934     createFileTypeMenu();
935     fileTypeComboBox.set_size_request(200,40);
936     fileTypeComboBox.signal_changed().connect(
937          sigc::mem_fun(*this, &FileSaveDialogImplGtk::fileTypeChangedCallback) );
940     childBox.pack_start( checksBox );
941     childBox.pack_end( fileTypeComboBox );
942     checksBox.pack_start( fileTypeCheckbox );
943     checksBox.pack_start( previewCheckbox );
945     set_extra_widget( childBox );
947     //Let's do some customization
948     fileNameEntry = NULL;
949     Gtk::Container *cont = get_toplevel();
950     std::vector<Gtk::Entry *> entries;
951     findEntryWidgets(cont, entries);
952     //g_message("Found %d entry widgets\n", entries.size());
953     if (entries.size() >=1 )
954         {
955         //Catch when user hits [return] on the text field
956         fileNameEntry = entries[0];
957         fileNameEntry->signal_activate().connect(
958              sigc::mem_fun(*this, &FileSaveDialogImplGtk::fileNameEntryChangedCallback) );
959         }
961     //Let's do more customization
962     std::vector<Gtk::Expander *> expanders;
963     findExpanderWidgets(cont, expanders);
964     //g_message("Found %d expander widgets\n", expanders.size());
965     if (expanders.size() >=1 )
966         {
967         //Always show the file list
968         Gtk::Expander *expander = expanders[0];
969         expander->set_expanded(true);
970         }
972     // allow easy access to the user's own templates folder
973     gchar *templates = profile_path ("templates");
974     if ( Inkscape::IO::file_test(templates, G_FILE_TEST_EXISTS)
975          && Inkscape::IO::file_test(templates, G_FILE_TEST_IS_DIR)
976          && g_path_is_absolute(templates)
977         )
978     {
979         add_shortcut_folder(templates);
980     }
981     g_free (templates);
984     //if (extension == NULL)
985     //    checkbox.set_sensitive(FALSE);
987     add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
988     set_default(*add_button(Gtk::Stock::SAVE,   Gtk::RESPONSE_OK));
990     show_all_children();
993 /**
994  * Destructor
995  */
996 FileSaveDialogImplGtk::~FileSaveDialogImplGtk()
1000 /**
1001  * Callback for fileNameEntry widget
1002  */
1003 void FileSaveDialogImplGtk::fileNameEntryChangedCallback()
1005     if (!fileNameEntry)
1006         return;
1008     Glib::ustring fileName = fileNameEntry->get_text();
1009     if (!Glib::get_charset()) //If we are not utf8
1010         fileName = Glib::filename_to_utf8(fileName);
1012     //g_message("User hit return.  Text is '%s'\n", fileName.c_str());
1014     if (!Glib::path_is_absolute(fileName)) {
1015         //try appending to the current path
1016         // not this way: fileName = get_current_folder() + "/" + fileName;
1017         std::vector<Glib::ustring> pathSegments;
1018         pathSegments.push_back( get_current_folder() );
1019         pathSegments.push_back( fileName );
1020         fileName = Glib::build_filename(pathSegments);
1021     }
1023     //g_message("path:'%s'\n", fileName.c_str());
1025     if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
1026         set_current_folder(fileName);
1027     } else if (/*Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR)*/1) {
1028         //dialog with either (1) select a regular file or (2) cd to dir
1029         //simulate an 'OK'
1030         set_filename(fileName);
1031         response(Gtk::RESPONSE_OK);
1032     }
1037 /**
1038  * Callback for fileNameEntry widget
1039  */
1040 void FileSaveDialogImplGtk::fileTypeChangedCallback()
1042     int sel = fileTypeComboBox.get_active_row_number();
1043     if (sel<0 || sel >= (int)fileTypes.size())
1044         return;
1045     FileType type = fileTypes[sel];
1046     //g_message("selected: %s\n", type.name.c_str());
1048     extension = type.extension;
1049     Gtk::FileFilter filter;
1050     filter.add_pattern(type.pattern);
1051     set_filter(filter);
1053     updateNameAndExtension();
1058 void FileSaveDialogImplGtk::createFileTypeMenu()
1060     Inkscape::Extension::DB::OutputList extension_list;
1061     Inkscape::Extension::db.get_output_list(extension_list);
1062     knownExtensions.clear();
1064     for (Inkscape::Extension::DB::OutputList::iterator current_item = extension_list.begin();
1065          current_item != extension_list.end(); current_item++)
1066     {
1067         Inkscape::Extension::Output * omod = *current_item;
1069         // FIXME: would be nice to grey them out instead of not listing them
1070         if (omod->deactivated()) continue;
1072         FileType type;
1073         type.name     = (_(omod->get_filetypename()));
1074         type.pattern  = "*";
1075         Glib::ustring extension = omod->get_extension();
1076         knownExtensions.insert( extension.casefold() );
1077         fileDialogExtensionToPattern (type.pattern, extension);
1078         type.extension= omod;
1079         fileTypeComboBox.append_text(type.name);
1080         fileTypes.push_back(type);
1081     }
1083     //#Let user choose
1084     FileType guessType;
1085     guessType.name = _("Guess from extension");
1086     guessType.pattern = "*";
1087     guessType.extension = NULL;
1088     fileTypeComboBox.append_text(guessType.name);
1089     fileTypes.push_back(guessType);
1092     fileTypeComboBox.set_active(0);
1093     fileTypeChangedCallback(); //call at least once to set the filter
1100 /**
1101  * Show this dialog modally.  Return true if user hits [OK]
1102  */
1103 bool
1104 FileSaveDialogImplGtk::show()
1106     change_path(myFilename);
1107     set_modal (TRUE);                      //Window
1108     sp_transientize((GtkWidget *)gobj());  //Make transient
1109     gint b = run();                        //Dialog
1110     svgPreview.showNoPreview();
1111     set_preview_widget_active(false);
1112     hide();
1114     if (b == Gtk::RESPONSE_OK) {
1115         updateNameAndExtension();
1116         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1117         
1118         // Store changes of the "Append filename automatically" checkbox back to preferences.
1119         if (save_method == Inkscape::Extension::FILE_SAVE_METHOD_SAVE_COPY) {
1120             prefs->setBool("/dialogs/save_copy/append_extension", fileTypeCheckbox.get_active());
1121         } else {
1122             prefs->setBool("/dialogs/save_as/append_extension", fileTypeCheckbox.get_active());
1123         }
1125         Inkscape::Extension::store_file_extension_in_prefs ((extension != NULL ? extension->get_id() : "" ), save_method);
1127         cleanup( true );
1129         return TRUE;
1130     } else {
1131         cleanup( false );
1132         return FALSE;
1133     }
1137 /**
1138  * Get the file extension type that was selected by the user. Valid after an [OK]
1139  */
1140 Inkscape::Extension::Extension *
1141 FileSaveDialogImplGtk::getSelectionType()
1143     return extension;
1146 void FileSaveDialogImplGtk::setSelectionType( Inkscape::Extension::Extension * key )
1148     // If no pointer to extension is passed in, look up based on filename extension.
1149     if ( !key ) {
1150         // Not quite UTF-8 here.
1151         gchar *filenameLower = g_ascii_strdown(myFilename.c_str(), -1);
1152         for ( int i = 0; !key && (i < (int)fileTypes.size()); i++ ) {
1153             Inkscape::Extension::Output *ext = dynamic_cast<Inkscape::Extension::Output*>(fileTypes[i].extension);
1154             if ( ext && ext->get_extension() ) {
1155                 gchar *extensionLower = g_ascii_strdown( ext->get_extension(), -1 );
1156                 if ( g_str_has_suffix(filenameLower, extensionLower) ) {
1157                     key = fileTypes[i].extension;
1158                 }
1159                 g_free(extensionLower);
1160             }
1161         }
1162         g_free(filenameLower);
1163     }
1165     // Ensure the proper entry in the combo box is selected.
1166     if ( key ) {
1167         extension = key;
1168         gchar const * extensionID = extension->get_id();
1169         if ( extensionID ) {
1170             for ( int i = 0; i < (int)fileTypes.size(); i++ ) {
1171                 Inkscape::Extension::Extension *ext = fileTypes[i].extension;
1172                 if ( ext ) {
1173                     gchar const * id = ext->get_id();
1174                     if ( id && ( strcmp(extensionID, id) == 0) ) {
1175                         int oldSel = fileTypeComboBox.get_active_row_number();
1176                         if ( i != oldSel ) {
1177                             fileTypeComboBox.set_active(i);
1178                         }
1179                         break;
1180                     }
1181                 }
1182             }
1183         }
1184     }
1187 Glib::ustring FileSaveDialogImplGtk::getCurrentDirectory()
1189     return get_current_folder();
1193 /*void
1194 FileSaveDialogImplGtk::change_title(const Glib::ustring& title)
1196     set_title(title);
1197 }*/
1199 /**
1200   * Change the default save path location.
1201   */
1202 void
1203 FileSaveDialogImplGtk::change_path(const Glib::ustring& path)
1205     myFilename = path;
1207     if (Glib::file_test(myFilename, Glib::FILE_TEST_IS_DIR)) {
1208         //fprintf(stderr,"set_current_folder(%s)\n",myFilename.c_str());
1209         set_current_folder(myFilename);
1210     } else {
1211         //fprintf(stderr,"set_filename(%s)\n",myFilename.c_str());
1212         if ( Glib::file_test( myFilename, Glib::FILE_TEST_EXISTS ) ) {
1213             set_filename(myFilename);
1214         } else {
1215             std::string dirName = Glib::path_get_dirname( myFilename  );
1216             if ( dirName != get_current_folder() ) {
1217                 set_current_folder(dirName);
1218             }
1219         }
1220         Glib::ustring basename = Glib::path_get_basename(myFilename);
1221         //fprintf(stderr,"set_current_name(%s)\n",basename.c_str());
1222         try {
1223             set_current_name( Glib::filename_to_utf8(basename) );
1224         } catch ( Glib::ConvertError& e ) {
1225             g_warning( "Error converting save filename to UTF-8." );
1226             // try a fallback.
1227             set_current_name( basename );
1228         }
1229     }
1232 void FileSaveDialogImplGtk::updateNameAndExtension()
1234     // Pick up any changes the user has typed in.
1235     Glib::ustring tmp = get_filename();
1236 #ifdef WITH_GNOME_VFS
1237     if ( tmp.empty() && gnome_vfs_initialized() ) {
1238         tmp = get_uri();
1239     }
1240 #endif
1241     if ( !tmp.empty() ) {
1242         myFilename = tmp;
1243     }
1245     Inkscape::Extension::Output* newOut = extension ? dynamic_cast<Inkscape::Extension::Output*>(extension) : 0;
1246     if ( fileTypeCheckbox.get_active() && newOut ) {
1247         // Append the file extension if it's not already present and display it in the file name entry field
1248         appendExtension(myFilename, newOut);
1249         change_path(myFilename);
1250     }
1254 #ifdef NEW_EXPORT_DIALOG
1256 //########################################################################
1257 //# F I L E     E X P O R T
1258 //########################################################################
1260 /**
1261  * Callback for fileNameEntry widget
1262  */
1263 void FileExportDialogImpl::fileNameEntryChangedCallback()
1265     if (!fileNameEntry)
1266         return;
1268     Glib::ustring fileName = fileNameEntry->get_text();
1269     if (!Glib::get_charset()) //If we are not utf8
1270         fileName = Glib::filename_to_utf8(fileName);
1272     //g_message("User hit return.  Text is '%s'\n", fileName.c_str());
1274     if (!Glib::path_is_absolute(fileName)) {
1275         //try appending to the current path
1276         // not this way: fileName = get_current_folder() + "/" + fileName;
1277         std::vector<Glib::ustring> pathSegments;
1278         pathSegments.push_back( get_current_folder() );
1279         pathSegments.push_back( fileName );
1280         fileName = Glib::build_filename(pathSegments);
1281     }
1283     //g_message("path:'%s'\n", fileName.c_str());
1285     if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
1286         set_current_folder(fileName);
1287     } else if (/*Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR)*/1) {
1288         //dialog with either (1) select a regular file or (2) cd to dir
1289         //simulate an 'OK'
1290         set_filename(fileName);
1291         response(Gtk::RESPONSE_OK);
1292     }
1297 /**
1298  * Callback for fileNameEntry widget
1299  */
1300 void FileExportDialogImpl::fileTypeChangedCallback()
1302     int sel = fileTypeComboBox.get_active_row_number();
1303     if (sel<0 || sel >= (int)fileTypes.size())
1304         return;
1305     FileType type = fileTypes[sel];
1306     //g_message("selected: %s\n", type.name.c_str());
1307     Gtk::FileFilter filter;
1308     filter.add_pattern(type.pattern);
1309     set_filter(filter);
1314 void FileExportDialogImpl::createFileTypeMenu()
1316     Inkscape::Extension::DB::OutputList extension_list;
1317     Inkscape::Extension::db.get_output_list(extension_list);
1319     for (Inkscape::Extension::DB::OutputList::iterator current_item = extension_list.begin();
1320          current_item != extension_list.end(); current_item++)
1321     {
1322         Inkscape::Extension::Output * omod = *current_item;
1324         // FIXME: would be nice to grey them out instead of not listing them
1325         if (omod->deactivated()) continue;
1327         FileType type;
1328         type.name     = (_(omod->get_filetypename()));
1329         type.pattern  = "*";
1330         Glib::ustring extension = omod->get_extension();
1331         fileDialogExtensionToPattern (type.pattern, extension);
1332         type.extension= omod;
1333         fileTypeComboBox.append_text(type.name);
1334         fileTypes.push_back(type);
1335     }
1337     //#Let user choose
1338     FileType guessType;
1339     guessType.name = _("Guess from extension");
1340     guessType.pattern = "*";
1341     guessType.extension = NULL;
1342     fileTypeComboBox.append_text(guessType.name);
1343     fileTypes.push_back(guessType);
1346     fileTypeComboBox.set_active(0);
1347     fileTypeChangedCallback(); //call at least once to set the filter
1351 /**
1352  * Constructor
1353  */
1354 FileExportDialogImpl::FileExportDialogImpl( Gtk::Window& parentWindow,
1355                                             const Glib::ustring &dir,
1356                                             FileDialogType fileTypes,
1357                                             const Glib::ustring &title,
1358                                             const Glib::ustring &/*default_key*/ ) :
1359             FileDialogBaseGtk(parentWindow, title, Gtk::FILE_CHOOSER_ACTION_SAVE, fileTypes, "/dialogs/export"),
1360             sourceX0Spinner("X0",         _("Left edge of source")),
1361             sourceY0Spinner("Y0",         _("Top edge of source")),
1362             sourceX1Spinner("X1",         _("Right edge of source")),
1363             sourceY1Spinner("Y1",         _("Bottom edge of source")),
1364             sourceWidthSpinner("Width",   _("Source width")),
1365             sourceHeightSpinner("Height", _("Source height")),
1366             destWidthSpinner("Width",     _("Destination width")),
1367             destHeightSpinner("Height",   _("Destination height")),
1368             destDPISpinner("DPI",         _("Resolution (dots per inch)"))
1370     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1371     append_extension = prefs->getBool("/dialogs/save_export/append_extension", true);
1373     /* One file at a time */
1374     set_select_multiple(false);
1376 #ifdef WITH_GNOME_VFS
1377     if (gnome_vfs_initialized()) {
1378         set_local_only(false);
1379     }
1380 #endif
1382     /* Initalize to Autodetect */
1383     extension = NULL;
1384     /* No filename to start out with */
1385     myFilename = "";
1387     /* Set our dialog type (save, export, etc...)*/
1388     _dialogType = fileTypes;
1390     /* Set the pwd and/or the filename */
1391     if (dir.size()>0)
1392         {
1393         Glib::ustring udir(dir);
1394         Glib::ustring::size_type len = udir.length();
1395         // leaving a trailing backslash on the directory name leads to the infamous
1396         // double-directory bug on win32
1397         if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
1398         set_current_folder(udir.c_str());
1399         }
1401     //#########################################
1402     //## EXTRA WIDGET -- SOURCE SIDE
1403     //#########################################
1405     //##### Export options buttons/spinners, etc
1406     documentButton.set_label(_("Document"));
1407     scopeBox.pack_start(documentButton);
1408     scopeGroup = documentButton.get_group();
1410     pageButton.set_label(_("Page"));
1411     pageButton.set_group(scopeGroup);
1412     scopeBox.pack_start(pageButton);
1414     selectionButton.set_label(_("Selection"));
1415     selectionButton.set_group(scopeGroup);
1416     scopeBox.pack_start(selectionButton);
1418     customButton.set_label(_("Custom"));
1419     customButton.set_group(scopeGroup);
1420     scopeBox.pack_start(customButton);
1422     sourceBox.pack_start(scopeBox);
1426     //dimension buttons
1427     sourceTable.resize(3,3);
1428     sourceTable.attach(sourceX0Spinner,     0,1,0,1);
1429     sourceTable.attach(sourceY0Spinner,     1,2,0,1);
1430     sourceUnitsSpinner.setUnitType(UNIT_TYPE_LINEAR);
1431     sourceTable.attach(sourceUnitsSpinner,  2,3,0,1);
1432     sourceTable.attach(sourceX1Spinner,     0,1,1,2);
1433     sourceTable.attach(sourceY1Spinner,     1,2,1,2);
1434     sourceTable.attach(sourceWidthSpinner,  0,1,2,3);
1435     sourceTable.attach(sourceHeightSpinner, 1,2,2,3);
1437     sourceBox.pack_start(sourceTable);
1438     sourceFrame.set_label(_("Source"));
1439     sourceFrame.add(sourceBox);
1440     exportOptionsBox.pack_start(sourceFrame);
1443     //#########################################
1444     //## EXTRA WIDGET -- SOURCE SIDE
1445     //#########################################
1448     destTable.resize(3,3);
1449     destTable.attach(destWidthSpinner,    0,1,0,1);
1450     destTable.attach(destHeightSpinner,   1,2,0,1);
1451     destUnitsSpinner.setUnitType(UNIT_TYPE_LINEAR);
1452     destTable.attach(destUnitsSpinner,    2,3,0,1);
1453     destTable.attach(destDPISpinner,      0,1,1,2);
1455     destBox.pack_start(destTable);
1458     cairoButton.set_label(_("Cairo"));
1459     otherOptionBox.pack_start(cairoButton);
1461     antiAliasButton.set_label(_("Antialias"));
1462     otherOptionBox.pack_start(antiAliasButton);
1464     backgroundButton.set_label(_("Background"));
1465     otherOptionBox.pack_start(backgroundButton);
1467     destBox.pack_start(otherOptionBox);
1473     //###### File options
1474     //###### Do we want the .xxx extension automatically added?
1475     fileTypeCheckbox.set_label(Glib::ustring(_("Append filename extension automatically")));
1476     fileTypeCheckbox.set_active(append_extension);
1477     destBox.pack_start(fileTypeCheckbox);
1479     //###### File type menu
1480     createFileTypeMenu();
1481     fileTypeComboBox.set_size_request(200,40);
1482     fileTypeComboBox.signal_changed().connect(
1483          sigc::mem_fun(*this, &FileExportDialogImpl::fileTypeChangedCallback) );
1485     destBox.pack_start(fileTypeComboBox);
1487     destFrame.set_label(_("Destination"));
1488     destFrame.add(destBox);
1489     exportOptionsBox.pack_start(destFrame);
1491     //##### Put the two boxes and their parent onto the dialog
1492     exportOptionsBox.pack_start(sourceFrame);
1493     exportOptionsBox.pack_start(destFrame);
1495     set_extra_widget(exportOptionsBox);
1500     //Let's do some customization
1501     fileNameEntry = NULL;
1502     Gtk::Container *cont = get_toplevel();
1503     std::vector<Gtk::Entry *> entries;
1504     findEntryWidgets(cont, entries);
1505     //g_message("Found %d entry widgets\n", entries.size());
1506     if (entries.size() >=1 )
1507         {
1508         //Catch when user hits [return] on the text field
1509         fileNameEntry = entries[0];
1510         fileNameEntry->signal_activate().connect(
1511              sigc::mem_fun(*this, &FileExportDialogImpl::fileNameEntryChangedCallback) );
1512         }
1514     //Let's do more customization
1515     std::vector<Gtk::Expander *> expanders;
1516     findExpanderWidgets(cont, expanders);
1517     //g_message("Found %d expander widgets\n", expanders.size());
1518     if (expanders.size() >=1 )
1519         {
1520         //Always show the file list
1521         Gtk::Expander *expander = expanders[0];
1522         expander->set_expanded(true);
1523         }
1526     //if (extension == NULL)
1527     //    checkbox.set_sensitive(FALSE);
1529     add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1530     set_default(*add_button(Gtk::Stock::SAVE,   Gtk::RESPONSE_OK));
1532     show_all_children();
1535 /**
1536  * Destructor
1537  */
1538 FileExportDialogImpl::~FileExportDialogImpl()
1544 /**
1545  * Show this dialog modally.  Return true if user hits [OK]
1546  */
1547 bool
1548 FileExportDialogImpl::show()
1550     Glib::ustring s = Glib::filename_to_utf8 (get_current_folder());
1551     if (s.length() == 0)
1552         s = getcwd (NULL, 0);
1553     set_current_folder(Glib::filename_from_utf8(s)); //hack to force initial dir listing
1554     set_modal (TRUE);                      //Window
1555     sp_transientize((GtkWidget *)gobj());  //Make transient
1556     gint b = run();                        //Dialog
1557     svgPreview.showNoPreview();
1558     hide();
1560     if (b == Gtk::RESPONSE_OK)
1561         {
1562         int sel = fileTypeComboBox.get_active_row_number ();
1563         if (sel>=0 && sel< (int)fileTypes.size())
1564             {
1565             FileType &type = fileTypes[sel];
1566             extension = type.extension;
1567             }
1568         myFilename = get_filename();
1569 #ifdef WITH_GNOME_VFS
1570         if ( myFilename.empty() && gnome_vfs_initialized() ) {
1571             myFilename = get_uri();
1572         }
1573 #endif
1575         /*
1577         // FIXME: Why do we have more code
1579         append_extension = checkbox.get_active();
1580         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1581         prefs->setBool("/dialogs/save_export/append_extension", append_extension);
1582         prefs->setBool("/dialogs/save_export/default", ( extension != NULL ? extension->get_id() : "" ));
1583         */
1584         return TRUE;
1585         }
1586     else
1587         {
1588         return FALSE;
1589         }
1593 /**
1594  * Get the file extension type that was selected by the user. Valid after an [OK]
1595  */
1596 Inkscape::Extension::Extension *
1597 FileExportDialogImpl::getSelectionType()
1599     return extension;
1603 /**
1604  * Get the file name chosen by the user.   Valid after an [OK]
1605  */
1606 Glib::ustring
1607 FileExportDialogImpl::getFilename()
1609     return myFilename;
1612 #endif // NEW_EXPORT_DIALOG
1615 } //namespace Dialog
1616 } //namespace UI
1617 } //namespace Inkscape
1619 /*
1620   Local Variables:
1621   mode:c++
1622   c-file-style:"stroustrup"
1623   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1624   indent-tabs-mode:nil
1625   fill-column:99
1626   End:
1627 */
1628 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :