749a67b288fbb31bf31a923675ea83f3348f26dd
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)
102 {
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 }
116 }
119 /*#########################################################################
120 ### SVG Preview Widget
121 #########################################################################*/
123 bool SVGPreview::setDocument(SPDocument *doc)
124 {
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;
141 }
144 bool SVGPreview::setFileName(Glib::ustring &theFileName)
145 {
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;
165 }
169 bool SVGPreview::setFromMem(char const *xmlBuffer)
170 {
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;
188 }
192 void SVGPreview::showImage(Glib::ustring &theFileName)
193 {
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:Bitstream Vera 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);
295 }
299 void SVGPreview::showNoPreview()
300 {
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:Bitstream Vera 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;
392 }
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)
400 {
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:Bitstream Vera 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:Bitstream Vera 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);
497 }
499 bool SVGPreview::set(Glib::ustring &fileName, int dialogType)
500 {
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 > 0x150000L)
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 }
549 }
552 SVGPreview::SVGPreview()
553 {
554 if (!INKSCAPE)
555 inkscape_application_init("",false);
556 document = NULL;
557 viewerGtk = NULL;
558 set_size_request(150,150);
559 showingNoPreview = false;
560 }
562 SVGPreview::~SVGPreview()
563 {
565 }
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()
573 {
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);
592 }
595 void FileDialogBaseGtk::cleanup( bool showConfirmed )
596 {
597 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
598 if ( showConfirmed )
599 prefs->setBool( preferenceBase + "/enable_preview", previewCheckbox.get_active() );
600 }
603 void FileDialogBaseGtk::_previewEnabledCB()
604 {
605 bool enabled = previewCheckbox.get_active();
606 set_preview_widget_active(enabled);
607 if ( enabled ) {
608 _updatePreviewCallback();
609 }
610 }
614 /**
615 * Callback for checking if the preview needs to be redrawn
616 */
617 void FileDialogBaseGtk::_updatePreviewCallback()
618 {
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);
632 }
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")
647 {
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 }
698 }
700 /**
701 * Destructor
702 */
703 FileOpenDialogImplGtk::~FileOpenDialogImplGtk()
704 {
706 }
708 void FileOpenDialogImplGtk::createFilterMenu()
709 {
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;
789 }
791 /**
792 * Show this dialog modally. Return true if user hits [OK]
793 */
794 bool
795 FileOpenDialogImplGtk::show()
796 {
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 }
828 }
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()
838 {
839 return extension;
840 }
843 /**
844 * Get the file name chosen by the user. Valid after an [OK]
845 */
846 Glib::ustring
847 FileOpenDialogImplGtk::getFilename (void)
848 {
849 return myFilename;
850 }
853 /**
854 * To Get Multiple filenames selected at-once.
855 */
856 std::vector<Glib::ustring>FileOpenDialogImplGtk::getFilenames()
857 {
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;
864 }
866 Glib::ustring FileOpenDialogImplGtk::getCurrentDirectory()
867 {
868 return get_current_folder();
869 }
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 FileDialogBaseGtk(parentWindow, title, Gtk::FILE_CHOOSER_ACTION_SAVE, fileTypes, "/dialogs/save_as")
888 {
889 FileSaveDialog::myDocTitle = docTitle;
891 /* One file at a time */
892 set_select_multiple(false);
894 #ifdef WITH_GNOME_VFS
895 if (gnome_vfs_initialized()) {
896 set_local_only(false);
897 }
898 #endif
900 /* Initalize to Autodetect */
901 extension = NULL;
902 /* No filename to start out with */
903 myFilename = "";
905 /* Set our dialog type (save, export, etc...)*/
906 _dialogType = fileTypes;
908 /* Set the pwd and/or the filename */
909 if (dir.size() > 0)
910 {
911 Glib::ustring udir(dir);
912 Glib::ustring::size_type len = udir.length();
913 // leaving a trailing backslash on the directory name leads to the infamous
914 // double-directory bug on win32
915 if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
916 myFilename = udir;
917 }
919 //###### Add the file types menu
920 //createFilterMenu();
922 //###### Do we want the .xxx extension automatically added?
923 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
924 fileTypeCheckbox.set_label(Glib::ustring(_("Append filename extension automatically")));
925 fileTypeCheckbox.set_active(prefs->getBool("/dialogs/save_as/append_extension", true));
927 createFileTypeMenu();
928 fileTypeComboBox.set_size_request(200,40);
929 fileTypeComboBox.signal_changed().connect(
930 sigc::mem_fun(*this, &FileSaveDialogImplGtk::fileTypeChangedCallback) );
933 childBox.pack_start( checksBox );
934 childBox.pack_end( fileTypeComboBox );
935 checksBox.pack_start( fileTypeCheckbox );
936 checksBox.pack_start( previewCheckbox );
938 set_extra_widget( childBox );
940 //Let's do some customization
941 fileNameEntry = NULL;
942 Gtk::Container *cont = get_toplevel();
943 std::vector<Gtk::Entry *> entries;
944 findEntryWidgets(cont, entries);
945 //g_message("Found %d entry widgets\n", entries.size());
946 if (entries.size() >=1 )
947 {
948 //Catch when user hits [return] on the text field
949 fileNameEntry = entries[0];
950 fileNameEntry->signal_activate().connect(
951 sigc::mem_fun(*this, &FileSaveDialogImplGtk::fileNameEntryChangedCallback) );
952 }
954 //Let's do more customization
955 std::vector<Gtk::Expander *> expanders;
956 findExpanderWidgets(cont, expanders);
957 //g_message("Found %d expander widgets\n", expanders.size());
958 if (expanders.size() >=1 )
959 {
960 //Always show the file list
961 Gtk::Expander *expander = expanders[0];
962 expander->set_expanded(true);
963 }
965 // allow easy access to the user's own templates folder
966 gchar *templates = profile_path ("templates");
967 if ( Inkscape::IO::file_test(templates, G_FILE_TEST_EXISTS)
968 && Inkscape::IO::file_test(templates, G_FILE_TEST_IS_DIR)
969 && g_path_is_absolute(templates)
970 )
971 {
972 add_shortcut_folder(templates);
973 }
974 g_free (templates);
977 //if (extension == NULL)
978 // checkbox.set_sensitive(FALSE);
980 add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
981 set_default(*add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK));
983 show_all_children();
984 }
986 /**
987 * Destructor
988 */
989 FileSaveDialogImplGtk::~FileSaveDialogImplGtk()
990 {
991 }
993 /**
994 * Callback for fileNameEntry widget
995 */
996 void FileSaveDialogImplGtk::fileNameEntryChangedCallback()
997 {
998 if (!fileNameEntry)
999 return;
1001 Glib::ustring fileName = fileNameEntry->get_text();
1002 if (!Glib::get_charset()) //If we are not utf8
1003 fileName = Glib::filename_to_utf8(fileName);
1005 //g_message("User hit return. Text is '%s'\n", fileName.c_str());
1007 if (!Glib::path_is_absolute(fileName)) {
1008 //try appending to the current path
1009 // not this way: fileName = get_current_folder() + "/" + fileName;
1010 std::vector<Glib::ustring> pathSegments;
1011 pathSegments.push_back( get_current_folder() );
1012 pathSegments.push_back( fileName );
1013 fileName = Glib::build_filename(pathSegments);
1014 }
1016 //g_message("path:'%s'\n", fileName.c_str());
1018 if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
1019 set_current_folder(fileName);
1020 } else if (/*Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR)*/1) {
1021 //dialog with either (1) select a regular file or (2) cd to dir
1022 //simulate an 'OK'
1023 set_filename(fileName);
1024 response(Gtk::RESPONSE_OK);
1025 }
1026 }
1030 /**
1031 * Callback for fileNameEntry widget
1032 */
1033 void FileSaveDialogImplGtk::fileTypeChangedCallback()
1034 {
1035 int sel = fileTypeComboBox.get_active_row_number();
1036 if (sel<0 || sel >= (int)fileTypes.size())
1037 return;
1038 FileType type = fileTypes[sel];
1039 //g_message("selected: %s\n", type.name.c_str());
1041 extension = type.extension;
1042 Gtk::FileFilter filter;
1043 filter.add_pattern(type.pattern);
1044 set_filter(filter);
1046 updateNameAndExtension();
1047 }
1051 void FileSaveDialogImplGtk::createFileTypeMenu()
1052 {
1053 Inkscape::Extension::DB::OutputList extension_list;
1054 Inkscape::Extension::db.get_output_list(extension_list);
1055 knownExtensions.clear();
1057 for (Inkscape::Extension::DB::OutputList::iterator current_item = extension_list.begin();
1058 current_item != extension_list.end(); current_item++)
1059 {
1060 Inkscape::Extension::Output * omod = *current_item;
1062 // FIXME: would be nice to grey them out instead of not listing them
1063 if (omod->deactivated()) continue;
1065 FileType type;
1066 type.name = (_(omod->get_filetypename()));
1067 type.pattern = "*";
1068 Glib::ustring extension = omod->get_extension();
1069 knownExtensions.insert( extension.casefold() );
1070 fileDialogExtensionToPattern (type.pattern, extension);
1071 type.extension= omod;
1072 fileTypeComboBox.append_text(type.name);
1073 fileTypes.push_back(type);
1074 }
1076 //#Let user choose
1077 FileType guessType;
1078 guessType.name = _("Guess from extension");
1079 guessType.pattern = "*";
1080 guessType.extension = NULL;
1081 fileTypeComboBox.append_text(guessType.name);
1082 fileTypes.push_back(guessType);
1085 fileTypeComboBox.set_active(0);
1086 fileTypeChangedCallback(); //call at least once to set the filter
1087 }
1093 /**
1094 * Show this dialog modally. Return true if user hits [OK]
1095 */
1096 bool
1097 FileSaveDialogImplGtk::show()
1098 {
1099 change_path(myFilename);
1100 set_modal (TRUE); //Window
1101 sp_transientize((GtkWidget *)gobj()); //Make transient
1102 gint b = run(); //Dialog
1103 svgPreview.showNoPreview();
1104 set_preview_widget_active(false);
1105 hide();
1107 if (b == Gtk::RESPONSE_OK) {
1108 updateNameAndExtension();
1109 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1111 // Store changes of the "Append filename automatically" checkbox back to preferences.
1112 prefs->setBool("/dialogs/save_as/append_extension", fileTypeCheckbox.get_active());
1114 // Store the last used save-as filetype to preferences.
1115 prefs->setString("/dialogs/save_as/default", ( extension != NULL ? extension->get_id() : "" ));
1117 cleanup( true );
1119 return TRUE;
1120 } else {
1121 cleanup( false );
1122 return FALSE;
1123 }
1124 }
1127 /**
1128 * Get the file extension type that was selected by the user. Valid after an [OK]
1129 */
1130 Inkscape::Extension::Extension *
1131 FileSaveDialogImplGtk::getSelectionType()
1132 {
1133 return extension;
1134 }
1136 void FileSaveDialogImplGtk::setSelectionType( Inkscape::Extension::Extension * key )
1137 {
1138 // If no pointer to extension is passed in, look up based on filename extension.
1139 if ( !key ) {
1140 // Not quite UTF-8 here.
1141 gchar *filenameLower = g_ascii_strdown(myFilename.c_str(), -1);
1142 for ( int i = 0; !key && (i < (int)fileTypes.size()); i++ ) {
1143 Inkscape::Extension::Output *ext = dynamic_cast<Inkscape::Extension::Output*>(fileTypes[i].extension);
1144 if ( ext && ext->get_extension() ) {
1145 gchar *extensionLower = g_ascii_strdown( ext->get_extension(), -1 );
1146 if ( g_str_has_suffix(filenameLower, extensionLower) ) {
1147 key = fileTypes[i].extension;
1148 }
1149 g_free(extensionLower);
1150 }
1151 }
1152 g_free(filenameLower);
1153 }
1155 // Ensure the proper entry in the combo box is selected.
1156 if ( key ) {
1157 extension = key;
1158 gchar const * extensionID = extension->get_id();
1159 if ( extensionID ) {
1160 for ( int i = 0; i < (int)fileTypes.size(); i++ ) {
1161 Inkscape::Extension::Extension *ext = fileTypes[i].extension;
1162 if ( ext ) {
1163 gchar const * id = ext->get_id();
1164 if ( id && ( strcmp(extensionID, id) == 0) ) {
1165 int oldSel = fileTypeComboBox.get_active_row_number();
1166 if ( i != oldSel ) {
1167 fileTypeComboBox.set_active(i);
1168 }
1169 break;
1170 }
1171 }
1172 }
1173 }
1174 }
1175 }
1177 Glib::ustring FileSaveDialogImplGtk::getCurrentDirectory()
1178 {
1179 return get_current_folder();
1180 }
1183 /*void
1184 FileSaveDialogImplGtk::change_title(const Glib::ustring& title)
1185 {
1186 set_title(title);
1187 }*/
1189 /**
1190 * Change the default save path location.
1191 */
1192 void
1193 FileSaveDialogImplGtk::change_path(const Glib::ustring& path)
1194 {
1195 myFilename = path;
1197 if (Glib::file_test(myFilename, Glib::FILE_TEST_IS_DIR)) {
1198 //fprintf(stderr,"set_current_folder(%s)\n",myFilename.c_str());
1199 set_current_folder(myFilename);
1200 } else {
1201 //fprintf(stderr,"set_filename(%s)\n",myFilename.c_str());
1202 if ( Glib::file_test( myFilename, Glib::FILE_TEST_EXISTS ) ) {
1203 set_filename(myFilename);
1204 } else {
1205 std::string dirName = Glib::path_get_dirname( myFilename );
1206 if ( dirName != get_current_folder() ) {
1207 set_current_folder(dirName);
1208 }
1209 }
1210 Glib::ustring basename = Glib::path_get_basename(myFilename);
1211 //fprintf(stderr,"set_current_name(%s)\n",basename.c_str());
1212 try {
1213 set_current_name( Glib::filename_to_utf8(basename) );
1214 } catch ( Glib::ConvertError& e ) {
1215 g_warning( "Error converting save filename to UTF-8." );
1216 // try a fallback.
1217 set_current_name( basename );
1218 }
1219 }
1220 }
1222 void FileSaveDialogImplGtk::updateNameAndExtension()
1223 {
1224 // Pick up any changes the user has typed in.
1225 Glib::ustring tmp = get_filename();
1226 #ifdef WITH_GNOME_VFS
1227 if ( tmp.empty() && gnome_vfs_initialized() ) {
1228 tmp = get_uri();
1229 }
1230 #endif
1231 if ( !tmp.empty() ) {
1232 myFilename = tmp;
1233 }
1235 Inkscape::Extension::Output* newOut = extension ? dynamic_cast<Inkscape::Extension::Output*>(extension) : 0;
1236 if ( fileTypeCheckbox.get_active() && newOut ) {
1237 // Append the file extension if it's not already present and display it in the file name entry field
1238 appendExtension(myFilename, newOut);
1239 change_path(myFilename);
1240 }
1241 }
1244 //########################################################################
1245 //# F I L E E X P O R T
1246 //########################################################################
1248 /**
1249 * Callback for fileNameEntry widget
1250 */
1251 void FileExportDialogImpl::fileNameEntryChangedCallback()
1252 {
1253 if (!fileNameEntry)
1254 return;
1256 Glib::ustring fileName = fileNameEntry->get_text();
1257 if (!Glib::get_charset()) //If we are not utf8
1258 fileName = Glib::filename_to_utf8(fileName);
1260 //g_message("User hit return. Text is '%s'\n", fileName.c_str());
1262 if (!Glib::path_is_absolute(fileName)) {
1263 //try appending to the current path
1264 // not this way: fileName = get_current_folder() + "/" + fileName;
1265 std::vector<Glib::ustring> pathSegments;
1266 pathSegments.push_back( get_current_folder() );
1267 pathSegments.push_back( fileName );
1268 fileName = Glib::build_filename(pathSegments);
1269 }
1271 //g_message("path:'%s'\n", fileName.c_str());
1273 if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
1274 set_current_folder(fileName);
1275 } else if (/*Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR)*/1) {
1276 //dialog with either (1) select a regular file or (2) cd to dir
1277 //simulate an 'OK'
1278 set_filename(fileName);
1279 response(Gtk::RESPONSE_OK);
1280 }
1281 }
1285 /**
1286 * Callback for fileNameEntry widget
1287 */
1288 void FileExportDialogImpl::fileTypeChangedCallback()
1289 {
1290 int sel = fileTypeComboBox.get_active_row_number();
1291 if (sel<0 || sel >= (int)fileTypes.size())
1292 return;
1293 FileType type = fileTypes[sel];
1294 //g_message("selected: %s\n", type.name.c_str());
1295 Gtk::FileFilter filter;
1296 filter.add_pattern(type.pattern);
1297 set_filter(filter);
1298 }
1302 void FileExportDialogImpl::createFileTypeMenu()
1303 {
1304 Inkscape::Extension::DB::OutputList extension_list;
1305 Inkscape::Extension::db.get_output_list(extension_list);
1307 for (Inkscape::Extension::DB::OutputList::iterator current_item = extension_list.begin();
1308 current_item != extension_list.end(); current_item++)
1309 {
1310 Inkscape::Extension::Output * omod = *current_item;
1312 // FIXME: would be nice to grey them out instead of not listing them
1313 if (omod->deactivated()) continue;
1315 FileType type;
1316 type.name = (_(omod->get_filetypename()));
1317 type.pattern = "*";
1318 Glib::ustring extension = omod->get_extension();
1319 fileDialogExtensionToPattern (type.pattern, extension);
1320 type.extension= omod;
1321 fileTypeComboBox.append_text(type.name);
1322 fileTypes.push_back(type);
1323 }
1325 //#Let user choose
1326 FileType guessType;
1327 guessType.name = _("Guess from extension");
1328 guessType.pattern = "*";
1329 guessType.extension = NULL;
1330 fileTypeComboBox.append_text(guessType.name);
1331 fileTypes.push_back(guessType);
1334 fileTypeComboBox.set_active(0);
1335 fileTypeChangedCallback(); //call at least once to set the filter
1336 }
1339 /**
1340 * Constructor
1341 */
1342 FileExportDialogImpl::FileExportDialogImpl( Gtk::Window& parentWindow,
1343 const Glib::ustring &dir,
1344 FileDialogType fileTypes,
1345 const Glib::ustring &title,
1346 const Glib::ustring &/*default_key*/ ) :
1347 FileDialogBaseGtk(parentWindow, title, Gtk::FILE_CHOOSER_ACTION_SAVE, fileTypes, "/dialogs/export"),
1348 sourceX0Spinner("X0", _("Left edge of source")),
1349 sourceY0Spinner("Y0", _("Top edge of source")),
1350 sourceX1Spinner("X1", _("Right edge of source")),
1351 sourceY1Spinner("Y1", _("Bottom edge of source")),
1352 sourceWidthSpinner("Width", _("Source width")),
1353 sourceHeightSpinner("Height", _("Source height")),
1354 destWidthSpinner("Width", _("Destination width")),
1355 destHeightSpinner("Height", _("Destination height")),
1356 destDPISpinner("DPI", _("Resolution (dots per inch)"))
1357 {
1358 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1359 append_extension = prefs->getBool("/dialogs/save_as/append_extension", true);
1361 /* One file at a time */
1362 set_select_multiple(false);
1364 #ifdef WITH_GNOME_VFS
1365 if (gnome_vfs_initialized()) {
1366 set_local_only(false);
1367 }
1368 #endif
1370 /* Initalize to Autodetect */
1371 extension = NULL;
1372 /* No filename to start out with */
1373 myFilename = "";
1375 /* Set our dialog type (save, export, etc...)*/
1376 _dialogType = fileTypes;
1378 /* Set the pwd and/or the filename */
1379 if (dir.size()>0)
1380 {
1381 Glib::ustring udir(dir);
1382 Glib::ustring::size_type len = udir.length();
1383 // leaving a trailing backslash on the directory name leads to the infamous
1384 // double-directory bug on win32
1385 if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
1386 set_current_folder(udir.c_str());
1387 }
1389 //#########################################
1390 //## EXTRA WIDGET -- SOURCE SIDE
1391 //#########################################
1393 //##### Export options buttons/spinners, etc
1394 documentButton.set_label(_("Document"));
1395 scopeBox.pack_start(documentButton);
1396 scopeGroup = documentButton.get_group();
1398 pageButton.set_label(_("Page"));
1399 pageButton.set_group(scopeGroup);
1400 scopeBox.pack_start(pageButton);
1402 selectionButton.set_label(_("Selection"));
1403 selectionButton.set_group(scopeGroup);
1404 scopeBox.pack_start(selectionButton);
1406 customButton.set_label(_("Custom"));
1407 customButton.set_group(scopeGroup);
1408 scopeBox.pack_start(customButton);
1410 sourceBox.pack_start(scopeBox);
1414 //dimension buttons
1415 sourceTable.resize(3,3);
1416 sourceTable.attach(sourceX0Spinner, 0,1,0,1);
1417 sourceTable.attach(sourceY0Spinner, 1,2,0,1);
1418 sourceUnitsSpinner.setUnitType(UNIT_TYPE_LINEAR);
1419 sourceTable.attach(sourceUnitsSpinner, 2,3,0,1);
1420 sourceTable.attach(sourceX1Spinner, 0,1,1,2);
1421 sourceTable.attach(sourceY1Spinner, 1,2,1,2);
1422 sourceTable.attach(sourceWidthSpinner, 0,1,2,3);
1423 sourceTable.attach(sourceHeightSpinner, 1,2,2,3);
1425 sourceBox.pack_start(sourceTable);
1426 sourceFrame.set_label(_("Source"));
1427 sourceFrame.add(sourceBox);
1428 exportOptionsBox.pack_start(sourceFrame);
1431 //#########################################
1432 //## EXTRA WIDGET -- SOURCE SIDE
1433 //#########################################
1436 destTable.resize(3,3);
1437 destTable.attach(destWidthSpinner, 0,1,0,1);
1438 destTable.attach(destHeightSpinner, 1,2,0,1);
1439 destUnitsSpinner.setUnitType(UNIT_TYPE_LINEAR);
1440 destTable.attach(destUnitsSpinner, 2,3,0,1);
1441 destTable.attach(destDPISpinner, 0,1,1,2);
1443 destBox.pack_start(destTable);
1446 cairoButton.set_label(_("Cairo"));
1447 otherOptionBox.pack_start(cairoButton);
1449 antiAliasButton.set_label(_("Antialias"));
1450 otherOptionBox.pack_start(antiAliasButton);
1452 backgroundButton.set_label(_("Background"));
1453 otherOptionBox.pack_start(backgroundButton);
1455 destBox.pack_start(otherOptionBox);
1461 //###### File options
1462 //###### Do we want the .xxx extension automatically added?
1463 fileTypeCheckbox.set_label(Glib::ustring(_("Append filename extension automatically")));
1464 fileTypeCheckbox.set_active(append_extension);
1465 destBox.pack_start(fileTypeCheckbox);
1467 //###### File type menu
1468 createFileTypeMenu();
1469 fileTypeComboBox.set_size_request(200,40);
1470 fileTypeComboBox.signal_changed().connect(
1471 sigc::mem_fun(*this, &FileExportDialogImpl::fileTypeChangedCallback) );
1473 destBox.pack_start(fileTypeComboBox);
1475 destFrame.set_label(_("Destination"));
1476 destFrame.add(destBox);
1477 exportOptionsBox.pack_start(destFrame);
1479 //##### Put the two boxes and their parent onto the dialog
1480 exportOptionsBox.pack_start(sourceFrame);
1481 exportOptionsBox.pack_start(destFrame);
1483 set_extra_widget(exportOptionsBox);
1488 //Let's do some customization
1489 fileNameEntry = NULL;
1490 Gtk::Container *cont = get_toplevel();
1491 std::vector<Gtk::Entry *> entries;
1492 findEntryWidgets(cont, entries);
1493 //g_message("Found %d entry widgets\n", entries.size());
1494 if (entries.size() >=1 )
1495 {
1496 //Catch when user hits [return] on the text field
1497 fileNameEntry = entries[0];
1498 fileNameEntry->signal_activate().connect(
1499 sigc::mem_fun(*this, &FileExportDialogImpl::fileNameEntryChangedCallback) );
1500 }
1502 //Let's do more customization
1503 std::vector<Gtk::Expander *> expanders;
1504 findExpanderWidgets(cont, expanders);
1505 //g_message("Found %d expander widgets\n", expanders.size());
1506 if (expanders.size() >=1 )
1507 {
1508 //Always show the file list
1509 Gtk::Expander *expander = expanders[0];
1510 expander->set_expanded(true);
1511 }
1514 //if (extension == NULL)
1515 // checkbox.set_sensitive(FALSE);
1517 add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1518 set_default(*add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK));
1520 show_all_children();
1521 }
1523 /**
1524 * Destructor
1525 */
1526 FileExportDialogImpl::~FileExportDialogImpl()
1527 {
1528 }
1532 /**
1533 * Show this dialog modally. Return true if user hits [OK]
1534 */
1535 bool
1536 FileExportDialogImpl::show()
1537 {
1538 Glib::ustring s = Glib::filename_to_utf8 (get_current_folder());
1539 if (s.length() == 0)
1540 s = getcwd (NULL, 0);
1541 set_current_folder(Glib::filename_from_utf8(s)); //hack to force initial dir listing
1542 set_modal (TRUE); //Window
1543 sp_transientize((GtkWidget *)gobj()); //Make transient
1544 gint b = run(); //Dialog
1545 svgPreview.showNoPreview();
1546 hide();
1548 if (b == Gtk::RESPONSE_OK)
1549 {
1550 int sel = fileTypeComboBox.get_active_row_number ();
1551 if (sel>=0 && sel< (int)fileTypes.size())
1552 {
1553 FileType &type = fileTypes[sel];
1554 extension = type.extension;
1555 }
1556 myFilename = get_filename();
1557 #ifdef WITH_GNOME_VFS
1558 if ( myFilename.empty() && gnome_vfs_initialized() ) {
1559 myFilename = get_uri();
1560 }
1561 #endif
1563 /*
1565 // FIXME: Why do we have more code
1567 append_extension = checkbox.get_active();
1568 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1569 prefs->setBool("/dialogs/save_as/append_extension", append_extension);
1570 prefs->setBool("/dialogs/save_as/default", ( extension != NULL ? extension->get_id() : "" ));
1571 */
1572 return TRUE;
1573 }
1574 else
1575 {
1576 return FALSE;
1577 }
1578 }
1581 /**
1582 * Get the file extension type that was selected by the user. Valid after an [OK]
1583 */
1584 Inkscape::Extension::Extension *
1585 FileExportDialogImpl::getSelectionType()
1586 {
1587 return extension;
1588 }
1591 /**
1592 * Get the file name chosen by the user. Valid after an [OK]
1593 */
1594 Glib::ustring
1595 FileExportDialogImpl::getFilename()
1596 {
1597 return myFilename;
1598 }
1601 } //namespace Dialog
1602 } //namespace UI
1603 } //namespace Inkscape
1605 /*
1606 Local Variables:
1607 mode:c++
1608 c-file-style:"stroustrup"
1609 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1610 indent-tabs-mode:nil
1611 fill-column:99
1612 End:
1613 */
1614 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :