1 /**
2 * 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"
28 #ifdef WITH_GNOME_VFS
29 # include <libgnomevfs/gnome-vfs.h>
30 #endif
32 //Routines from file.cpp
33 #undef INK_DUMP_FILENAME_CONV
35 #ifdef INK_DUMP_FILENAME_CONV
36 void dump_str( const gchar* str, const gchar* prefix );
37 void dump_ustr( const Glib::ustring& ustr );
38 #endif
42 namespace Inkscape
43 {
44 namespace UI
45 {
46 namespace Dialog
47 {
53 //########################################################################
54 //### U T I L I T Y
55 //########################################################################
57 void
58 fileDialogExtensionToPattern(Glib::ustring &pattern,
59 Glib::ustring &extension)
60 {
61 for (unsigned int i = 0; i < extension.length(); i++ )
62 {
63 Glib::ustring::value_type ch = extension[i];
64 if ( Glib::Unicode::isalpha(ch) )
65 {
66 pattern += '[';
67 pattern += Glib::Unicode::toupper(ch);
68 pattern += Glib::Unicode::tolower(ch);
69 pattern += ']';
70 }
71 else
72 {
73 pattern += ch;
74 }
75 }
76 }
79 void
80 findEntryWidgets(Gtk::Container *parent,
81 std::vector<Gtk::Entry *> &result)
82 {
83 if (!parent)
84 return;
85 std::vector<Gtk::Widget *> children = parent->get_children();
86 for (unsigned int i=0; i<children.size() ; i++)
87 {
88 Gtk::Widget *child = children[i];
89 GtkWidget *wid = child->gobj();
90 if (GTK_IS_ENTRY(wid))
91 result.push_back((Gtk::Entry *)child);
92 else if (GTK_IS_CONTAINER(wid))
93 findEntryWidgets((Gtk::Container *)child, result);
94 }
96 }
98 void
99 findExpanderWidgets(Gtk::Container *parent,
100 std::vector<Gtk::Expander *> &result)
101 {
102 if (!parent)
103 return;
104 std::vector<Gtk::Widget *> children = parent->get_children();
105 for (unsigned int i=0; i<children.size() ; i++)
106 {
107 Gtk::Widget *child = children[i];
108 GtkWidget *wid = child->gobj();
109 if (GTK_IS_EXPANDER(wid))
110 result.push_back((Gtk::Expander *)child);
111 else if (GTK_IS_CONTAINER(wid))
112 findExpanderWidgets((Gtk::Container *)child, result);
113 }
115 }
118 /*#########################################################################
119 ### SVG Preview Widget
120 #########################################################################*/
122 bool SVGPreview::setDocument(SPDocument *doc)
123 {
124 if (document)
125 sp_document_unref(document);
127 sp_document_ref(doc);
128 document = doc;
130 //This should remove it from the box, and free resources
131 if (viewerGtk)
132 gtk_widget_destroy(viewerGtk);
134 viewerGtk = sp_svg_view_widget_new(doc);
135 GtkWidget *vbox = (GtkWidget *)gobj();
136 gtk_box_pack_start(GTK_BOX(vbox), viewerGtk, TRUE, TRUE, 0);
137 gtk_widget_show(viewerGtk);
139 return true;
140 }
143 bool SVGPreview::setFileName(Glib::ustring &theFileName)
144 {
145 Glib::ustring fileName = theFileName;
147 fileName = Glib::filename_to_utf8(fileName);
149 /**
150 * I don't know why passing false to keepalive is bad. But it
151 * prevents the display of an svg with a non-ascii filename
152 */
153 SPDocument *doc = sp_document_new (fileName.c_str(), true);
154 if (!doc) {
155 g_warning("SVGView: error loading document '%s'\n", fileName.c_str());
156 return false;
157 }
159 setDocument(doc);
161 sp_document_unref(doc);
163 return true;
164 }
168 bool SVGPreview::setFromMem(char const *xmlBuffer)
169 {
170 if (!xmlBuffer)
171 return false;
173 gint len = (gint)strlen(xmlBuffer);
174 SPDocument *doc = sp_document_new_from_mem(xmlBuffer, len, 0);
175 if (!doc) {
176 g_warning("SVGView: error loading buffer '%s'\n",xmlBuffer);
177 return false;
178 }
180 setDocument(doc);
182 sp_document_unref(doc);
184 Inkscape::GC::request_early_collection();
186 return true;
187 }
191 void SVGPreview::showImage(Glib::ustring &theFileName)
192 {
193 Glib::ustring fileName = theFileName;
196 /*#####################################
197 # LET'S HAVE SOME FUN WITH SVG!
198 # Instead of just loading an image, why
199 # don't we make a lovely little svg and
200 # display it nicely?
201 #####################################*/
203 //Arbitrary size of svg doc -- rather 'portrait' shaped
204 gint previewWidth = 400;
205 gint previewHeight = 600;
207 //Get some image info. Smart pointer does not need to be deleted
208 Glib::RefPtr<Gdk::Pixbuf> img(NULL);
209 try {
210 img = Gdk::Pixbuf::create_from_file(fileName);
211 }
212 catch (const Glib::FileError & e)
213 {
214 g_message("caught Glib::FileError in SVGPreview::showImage");
215 return;
216 }
217 catch (const Gdk::PixbufError & e)
218 {
219 g_message("Gdk::PixbufError in SVGPreview::showImage");
220 return;
221 }
222 catch (...)
223 {
224 g_message("Caught ... in SVGPreview::showImage");
225 return;
226 }
228 gint imgWidth = img->get_width();
229 gint imgHeight = img->get_height();
231 //Find the minimum scale to fit the image inside the preview area
232 double scaleFactorX = (0.9 *(double)previewWidth) / ((double)imgWidth);
233 double scaleFactorY = (0.9 *(double)previewHeight) / ((double)imgHeight);
234 double scaleFactor = scaleFactorX;
235 if (scaleFactorX > scaleFactorY)
236 scaleFactor = scaleFactorY;
238 //Now get the resized values
239 gint scaledImgWidth = (int) (scaleFactor * (double)imgWidth);
240 gint scaledImgHeight = (int) (scaleFactor * (double)imgHeight);
242 //center the image on the area
243 gint imgX = (previewWidth - scaledImgWidth) / 2;
244 gint imgY = (previewHeight - scaledImgHeight) / 2;
246 //wrap a rectangle around the image
247 gint rectX = imgX-1;
248 gint rectY = imgY-1;
249 gint rectWidth = scaledImgWidth +2;
250 gint rectHeight = scaledImgHeight+2;
252 //Our template. Modify to taste
253 gchar const *xformat =
254 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
255 "<svg\n"
256 "xmlns=\"http://www.w3.org/2000/svg\"\n"
257 "xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
258 "width=\"%d\" height=\"%d\">\n" //# VALUES HERE
259 "<rect\n"
260 " style=\"fill:#eeeeee;stroke:none\"\n"
261 " x=\"-100\" y=\"-100\" width=\"4000\" height=\"4000\"/>\n"
262 "<image x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\"\n"
263 "xlink:href=\"%s\"/>\n"
264 "<rect\n"
265 " style=\"fill:none;"
266 " stroke:#000000;stroke-width:1.0;"
267 " stroke-linejoin:miter;stroke-opacity:1.0000000;"
268 " stroke-miterlimit:4.0000000;stroke-dasharray:none\"\n"
269 " x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\"/>\n"
270 "<text\n"
271 " style=\"font-size:24.000000;font-style:normal;font-weight:normal;"
272 " fill:#000000;fill-opacity:1.0000000;stroke:none;"
273 " font-family:Bitstream Vera Sans\"\n"
274 " x=\"10\" y=\"26\">%d x %d</text>\n" //# VALUES HERE
275 "</svg>\n\n";
277 //if (!Glib::get_charset()) //If we are not utf8
278 fileName = Glib::filename_to_utf8(fileName);
280 //Fill in the template
281 /* FIXME: Do proper XML quoting for fileName. */
282 gchar *xmlBuffer = g_strdup_printf(xformat,
283 previewWidth, previewHeight,
284 imgX, imgY, scaledImgWidth, scaledImgHeight,
285 fileName.c_str(),
286 rectX, rectY, rectWidth, rectHeight,
287 imgWidth, imgHeight);
289 //g_message("%s\n", xmlBuffer);
291 //now show it!
292 setFromMem(xmlBuffer);
293 g_free(xmlBuffer);
294 }
298 void SVGPreview::showNoPreview()
299 {
300 //Are we already showing it?
301 if (showingNoPreview)
302 return;
304 //Arbitrary size of svg doc -- rather 'portrait' shaped
305 gint previewWidth = 300;
306 gint previewHeight = 600;
308 //Our template. Modify to taste
309 gchar const *xformat =
310 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
311 "<svg\n"
312 "xmlns=\"http://www.w3.org/2000/svg\"\n"
313 "xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
314 "width=\"%d\" height=\"%d\">\n" //# VALUES HERE
315 "<g transform=\"translate(-190,24.27184)\" style=\"opacity:0.12\">\n"
316 "<path\n"
317 "style=\"font-size:12;fill:#ffffff;fill-rule:evenodd;stroke:#000000;stroke-width:0.936193pt\"\n"
318 "d=\"M 397.64309 320.25301 L 280.39197 282.517 L 250.74227 124.83447 L 345.08225 "
319 "29.146783 L 393.59996 46.667064 L 483.89679 135.61619 L 397.64309 320.25301 z \"\n"
320 "id=\"whiteSpace\" />\n"
321 "<path\n"
322 "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
323 "d=\"M 476.95792 339.17168 C 495.78197 342.93607 499.54842 356.11361 495.78197 359.87802 "
324 "C 492.01856 363.6434 482.6065 367.40781 475.07663 361.76014 C 467.54478 "
325 "356.11361 467.54478 342.93607 476.95792 339.17168 z \"\n"
326 "id=\"droplet01\" />\n"
327 "<path\n"
328 "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
329 "d=\"M 286.46194 340.42914 C 284.6277 340.91835 269.30405 327.71337 257.16909 333.8338 "
330 "C 245.03722 339.95336 236.89276 353.65666 248.22676 359.27982 C 259.56184 364.90298 "
331 "267.66433 358.41867 277.60113 351.44119 C 287.53903 344.46477 "
332 "287.18046 343.1206 286.46194 340.42914 z \"\n"
333 "id=\"droplet02\" />\n"
334 "<path\n"
335 "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
336 "d=\"M 510.35756 306.92856 C 520.59494 304.36879 544.24333 306.92856 540.47688 321.98634 "
337 "C 536.71354 337.04806 504.71297 331.39827 484.00371 323.87156 C 482.12141 "
338 "308.81083 505.53237 308.13423 510.35756 306.92856 z \"\n"
339 "id=\"droplet03\" />\n"
340 "<path\n"
341 "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
342 "d=\"M 359.2403 21.362537 C 347.92693 21.362537 336.6347 25.683095 327.96556 34.35223 "
343 "L 173.87387 188.41466 C 165.37697 196.9114 161.1116 207.95813 160.94269 219.04577 "
344 "L 160.88418 219.04577 C 160.88418 219.08524 160.94076 219.12322 160.94269 219.16279 "
345 "C 160.94033 219.34888 160.88418 219.53256 160.88418 219.71865 L 161.14748 219.71865 "
346 "C 164.0966 230.93917 240.29699 245.24198 248.79866 253.74346 C 261.63771 266.58263 "
347 "199.5652 276.01151 212.4041 288.85074 C 225.24316 301.68979 289.99433 313.6933 302.8346 "
348 "326.53254 C 315.67368 339.37161 276.5961 353.04289 289.43532 365.88196 C 302.27439 "
349 "378.72118 345.40201 362.67257 337.5908 396.16198 C 354.92909 413.50026 391.10302 "
350 "405.2208 415.32417 387.88252 C 428.16323 375.04345 390.6948 376.17577 403.53397 "
351 "363.33668 C 416.37304 350.49745 448.78128 350.4282 476.08902 319.71589 C 465.09739 "
352 "302.62116 429.10801 295.34136 441.94719 282.50217 C 454.78625 269.66311 479.74708 "
353 "276.18423 533.60644 251.72479 C 559.89837 239.78398 557.72636 230.71459 557.62567 "
354 "219.71865 C 557.62356 219.48727 557.62567 219.27892 557.62567 219.04577 L 557.56716 "
355 "219.04577 C 557.3983 207.95812 553.10345 196.9114 544.60673 188.41466 L 390.54428 "
356 "34.35223 C 381.87515 25.683095 370.55366 21.362537 359.2403 21.362537 z M 357.92378 "
357 "41.402939 C 362.95327 41.533963 367.01541 45.368018 374.98006 50.530832 L 447.76915 "
358 "104.50827 C 448.56596 105.02498 449.32484 105.564 450.02187 106.11735 C 450.7189 106.67062 "
359 "451.3556 107.25745 451.95277 107.84347 C 452.54997 108.42842 453.09281 109.01553 453.59111 "
360 "109.62808 C 454.08837 110.24052 454.53956 110.86661 454.93688 111.50048 C 455.33532 112.13538 "
361 "455.69164 112.78029 455.9901 113.43137 C 456.28877 114.08363 456.52291 114.75639 456.7215 "
362 "115.42078 C 456.92126 116.08419 457.08982 116.73973 457.18961 117.41019 C 457.28949 "
363 "118.08184 457.33588 118.75535 457.33588 119.42886 L 414.21245 98.598549 L 409.9118 "
364 "131.16055 L 386.18512 120.04324 L 349.55654 144.50131 L 335.54288 96.1703 L 317.4919 "
365 "138.4453 L 267.08369 143.47735 L 267.63956 121.03795 C 267.63956 115.64823 296.69685 "
366 "77.915899 314.39075 68.932902 L 346.77721 45.674327 C 351.55594 42.576634 354.90608 "
367 "41.324327 357.92378 41.402939 z M 290.92738 261.61333 C 313.87149 267.56365 339.40299 "
368 "275.37038 359.88393 275.50997 L 360.76161 284.72563 C 343.2235 282.91785 306.11346 "
369 "274.45012 297.36372 269.98057 L 290.92738 261.61333 z \"\n"
370 "id=\"mountainDroplet\" />\n"
371 "</g> <g transform=\"translate(-20,0)\">\n"
372 "<text xml:space=\"preserve\"\n"
373 "style=\"font-size:32.000000;font-style:normal;font-variant:normal;font-weight:bold;"
374 "font-stretch:normal;fill:#000000;fill-opacity:1.0000000;stroke:none;stroke-width:1.0000000pt;"
375 "stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1.0000000;"
376 "font-family:Bitstream Vera Sans;text-anchor:middle;writing-mode:lr\"\n"
377 "x=\"190\" y=\"240\">%s</text></g>\n" //# VALUE HERE
378 "</svg>\n\n";
380 //Fill in the template
381 gchar *xmlBuffer = g_strdup_printf(xformat,
382 previewWidth, previewHeight, _("No preview"));
384 //g_message("%s\n", xmlBuffer);
386 //now show it!
387 setFromMem(xmlBuffer);
388 g_free(xmlBuffer);
389 showingNoPreview = true;
391 }
394 /**
395 * Inform the user that the svg file is too large to be displayed.
396 * This does not check for sizes of embedded images (yet)
397 */
398 void SVGPreview::showTooLarge(long fileLength)
399 {
401 //Arbitrary size of svg doc -- rather 'portrait' shaped
402 gint previewWidth = 300;
403 gint previewHeight = 600;
405 //Our template. Modify to taste
406 gchar const *xformat =
407 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
408 "<svg\n"
409 "xmlns=\"http://www.w3.org/2000/svg\"\n"
410 "xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
411 "width=\"%d\" height=\"%d\">\n" //# VALUES HERE
412 "<g transform=\"translate(-170,24.27184)\" style=\"opacity:0.12\">\n"
413 "<path\n"
414 "style=\"font-size:12;fill:#ffffff;fill-rule:evenodd;stroke:#000000;stroke-width:0.936193pt\"\n"
415 "d=\"M 397.64309 320.25301 L 280.39197 282.517 L 250.74227 124.83447 L 345.08225 "
416 "29.146783 L 393.59996 46.667064 L 483.89679 135.61619 L 397.64309 320.25301 z \"\n"
417 "id=\"whiteSpace\" />\n"
418 "<path\n"
419 "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
420 "d=\"M 476.95792 339.17168 C 495.78197 342.93607 499.54842 356.11361 495.78197 359.87802 "
421 "C 492.01856 363.6434 482.6065 367.40781 475.07663 361.76014 C 467.54478 "
422 "356.11361 467.54478 342.93607 476.95792 339.17168 z \"\n"
423 "id=\"droplet01\" />\n"
424 "<path\n"
425 "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
426 "d=\"M 286.46194 340.42914 C 284.6277 340.91835 269.30405 327.71337 257.16909 333.8338 "
427 "C 245.03722 339.95336 236.89276 353.65666 248.22676 359.27982 C 259.56184 364.90298 "
428 "267.66433 358.41867 277.60113 351.44119 C 287.53903 344.46477 "
429 "287.18046 343.1206 286.46194 340.42914 z \"\n"
430 "id=\"droplet02\" />\n"
431 "<path\n"
432 "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
433 "d=\"M 510.35756 306.92856 C 520.59494 304.36879 544.24333 306.92856 540.47688 321.98634 "
434 "C 536.71354 337.04806 504.71297 331.39827 484.00371 323.87156 C 482.12141 "
435 "308.81083 505.53237 308.13423 510.35756 306.92856 z \"\n"
436 "id=\"droplet03\" />\n"
437 "<path\n"
438 "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
439 "d=\"M 359.2403 21.362537 C 347.92693 21.362537 336.6347 25.683095 327.96556 34.35223 "
440 "L 173.87387 188.41466 C 165.37697 196.9114 161.1116 207.95813 160.94269 219.04577 "
441 "L 160.88418 219.04577 C 160.88418 219.08524 160.94076 219.12322 160.94269 219.16279 "
442 "C 160.94033 219.34888 160.88418 219.53256 160.88418 219.71865 L 161.14748 219.71865 "
443 "C 164.0966 230.93917 240.29699 245.24198 248.79866 253.74346 C 261.63771 266.58263 "
444 "199.5652 276.01151 212.4041 288.85074 C 225.24316 301.68979 289.99433 313.6933 302.8346 "
445 "326.53254 C 315.67368 339.37161 276.5961 353.04289 289.43532 365.88196 C 302.27439 "
446 "378.72118 345.40201 362.67257 337.5908 396.16198 C 354.92909 413.50026 391.10302 "
447 "405.2208 415.32417 387.88252 C 428.16323 375.04345 390.6948 376.17577 403.53397 "
448 "363.33668 C 416.37304 350.49745 448.78128 350.4282 476.08902 319.71589 C 465.09739 "
449 "302.62116 429.10801 295.34136 441.94719 282.50217 C 454.78625 269.66311 479.74708 "
450 "276.18423 533.60644 251.72479 C 559.89837 239.78398 557.72636 230.71459 557.62567 "
451 "219.71865 C 557.62356 219.48727 557.62567 219.27892 557.62567 219.04577 L 557.56716 "
452 "219.04577 C 557.3983 207.95812 553.10345 196.9114 544.60673 188.41466 L 390.54428 "
453 "34.35223 C 381.87515 25.683095 370.55366 21.362537 359.2403 21.362537 z M 357.92378 "
454 "41.402939 C 362.95327 41.533963 367.01541 45.368018 374.98006 50.530832 L 447.76915 "
455 "104.50827 C 448.56596 105.02498 449.32484 105.564 450.02187 106.11735 C 450.7189 106.67062 "
456 "451.3556 107.25745 451.95277 107.84347 C 452.54997 108.42842 453.09281 109.01553 453.59111 "
457 "109.62808 C 454.08837 110.24052 454.53956 110.86661 454.93688 111.50048 C 455.33532 112.13538 "
458 "455.69164 112.78029 455.9901 113.43137 C 456.28877 114.08363 456.52291 114.75639 456.7215 "
459 "115.42078 C 456.92126 116.08419 457.08982 116.73973 457.18961 117.41019 C 457.28949 "
460 "118.08184 457.33588 118.75535 457.33588 119.42886 L 414.21245 98.598549 L 409.9118 "
461 "131.16055 L 386.18512 120.04324 L 349.55654 144.50131 L 335.54288 96.1703 L 317.4919 "
462 "138.4453 L 267.08369 143.47735 L 267.63956 121.03795 C 267.63956 115.64823 296.69685 "
463 "77.915899 314.39075 68.932902 L 346.77721 45.674327 C 351.55594 42.576634 354.90608 "
464 "41.324327 357.92378 41.402939 z M 290.92738 261.61333 C 313.87149 267.56365 339.40299 "
465 "275.37038 359.88393 275.50997 L 360.76161 284.72563 C 343.2235 282.91785 306.11346 "
466 "274.45012 297.36372 269.98057 L 290.92738 261.61333 z \"\n"
467 "id=\"mountainDroplet\" />\n"
468 "</g>\n"
469 "<text xml:space=\"preserve\"\n"
470 "style=\"font-size:32.000000;font-style:normal;font-variant:normal;font-weight:bold;"
471 "font-stretch:normal;fill:#000000;fill-opacity:1.0000000;stroke:none;stroke-width:1.0000000pt;"
472 "stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1.0000000;"
473 "font-family:Bitstream Vera Sans;text-anchor:middle;writing-mode:lr\"\n"
474 "x=\"170\" y=\"215\">%5.1f MB</text>\n" //# VALUE HERE
475 "<text xml:space=\"preserve\"\n"
476 "style=\"font-size:24.000000;font-style:normal;font-variant:normal;font-weight:bold;"
477 "font-stretch:normal;fill:#000000;fill-opacity:1.0000000;stroke:none;stroke-width:1.0000000pt;"
478 "stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1.0000000;"
479 "font-family:Bitstream Vera Sans;text-anchor:middle;writing-mode:lr\"\n"
480 "x=\"180\" y=\"245\">%s</text>\n" //# VALUE HERE
481 "</svg>\n\n";
483 //Fill in the template
484 double floatFileLength = ((double)fileLength) / 1048576.0;
485 //printf("%ld %f\n", fileLength, floatFileLength);
486 gchar *xmlBuffer = g_strdup_printf(xformat,
487 previewWidth, previewHeight, floatFileLength,
488 _("too large for preview"));
490 //g_message("%s\n", xmlBuffer);
492 //now show it!
493 setFromMem(xmlBuffer);
494 g_free(xmlBuffer);
496 }
498 bool SVGPreview::set(Glib::ustring &fileName, int dialogType)
499 {
501 if (!Glib::file_test(fileName, Glib::FILE_TEST_EXISTS))
502 return false;
504 //g_message("fname:%s", fileName.c_str());
506 if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
507 showNoPreview();
508 return false;
509 }
511 if (Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR))
512 {
513 Glib::ustring fileNameUtf8 = Glib::filename_to_utf8(fileName);
514 gchar *fName = (gchar *)fileNameUtf8.c_str();
515 struct stat info;
516 if (g_stat(fName, &info))
517 {
518 g_warning("SVGPreview::set() : %s : %s",
519 fName, strerror(errno));
520 return FALSE;
521 }
522 long fileLen = info.st_size;
523 if (fileLen > 0x150000L)
524 {
525 showingNoPreview = false;
526 showTooLarge(fileLen);
527 return FALSE;
528 }
529 }
531 Glib::ustring svg = ".svg";
532 Glib::ustring svgz = ".svgz";
534 if ((dialogType == SVG_TYPES || dialogType == IMPORT_TYPES) &&
535 (hasSuffix(fileName, svg) || hasSuffix(fileName, svgz) )
536 ) {
537 bool retval = setFileName(fileName);
538 showingNoPreview = false;
539 return retval;
540 } else if (isValidImageFile(fileName)) {
541 showImage(fileName);
542 showingNoPreview = false;
543 return true;
544 } else {
545 showNoPreview();
546 return false;
547 }
548 }
551 SVGPreview::SVGPreview()
552 {
553 if (!INKSCAPE)
554 inkscape_application_init("",false);
555 document = NULL;
556 viewerGtk = NULL;
557 set_size_request(150,150);
558 showingNoPreview = false;
559 }
561 SVGPreview::~SVGPreview()
562 {
564 }
567 /*#########################################################################
568 ### F I L E D I A L O G B A S E C L A S S
569 #########################################################################*/
571 void FileDialogBaseGtk::internalSetup()
572 {
573 bool enablePreview =
574 (bool)prefs_get_int_attribute( preferenceBase.c_str(),
575 "enable_preview", 1 );
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 if ( showConfirmed )
598 prefs_set_int_attribute( preferenceBase.c_str(),
599 "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 //patterns added dynamically below
727 Inkscape::Extension::DB::InputList extension_list;
728 Inkscape::Extension::db.get_input_list(extension_list);
730 for (Inkscape::Extension::DB::InputList::iterator current_item = extension_list.begin();
731 current_item != extension_list.end(); current_item++)
732 {
733 Inkscape::Extension::Input * imod = *current_item;
735 // FIXME: would be nice to grey them out instead of not listing them
736 if (imod->deactivated()) continue;
738 Glib::ustring upattern("*");
739 Glib::ustring extension = imod->get_extension();
740 fileDialogExtensionToPattern(upattern, extension);
742 Gtk::FileFilter filter;
743 Glib::ustring uname(_(imod->get_filetypename()));
744 filter.set_name(uname);
745 filter.add_pattern(upattern);
746 add_filter(filter);
747 extensionMap[uname] = imod;
749 //g_message("ext %s:%s '%s'\n", ioext->name, ioext->mimetype, upattern.c_str());
750 allInkscapeFilter.add_pattern(upattern);
751 if ( strncmp("image", imod->get_mimetype(), 5)==0 )
752 allImageFilter.add_pattern(upattern);
753 }
755 return;
756 }
758 /**
759 * Show this dialog modally. Return true if user hits [OK]
760 */
761 bool
762 FileOpenDialogImplGtk::show()
763 {
764 set_modal (TRUE); //Window
765 sp_transientize((GtkWidget *)gobj()); //Make transient
766 gint b = run(); //Dialog
767 svgPreview.showNoPreview();
768 hide();
770 if (b == Gtk::RESPONSE_OK)
771 {
772 //This is a hack, to avoid the warning messages that
773 //Gtk::FileChooser::get_filter() returns
774 //should be: Gtk::FileFilter *filter = get_filter();
775 GtkFileChooser *gtkFileChooser = Gtk::FileChooser::gobj();
776 GtkFileFilter *filter = gtk_file_chooser_get_filter(gtkFileChooser);
777 if (filter)
778 {
779 //Get which extension was chosen, if any
780 extension = extensionMap[gtk_file_filter_get_name(filter)];
781 }
782 myFilename = get_filename();
783 #ifdef WITH_GNOME_VFS
784 if (myFilename.empty() && gnome_vfs_initialized())
785 myFilename = get_uri();
786 #endif
787 cleanup( true );
788 return TRUE;
789 }
790 else
791 {
792 cleanup( false );
793 return FALSE;
794 }
795 }
800 /**
801 * Get the file extension type that was selected by the user. Valid after an [OK]
802 */
803 Inkscape::Extension::Extension *
804 FileOpenDialogImplGtk::getSelectionType()
805 {
806 return extension;
807 }
810 /**
811 * Get the file name chosen by the user. Valid after an [OK]
812 */
813 Glib::ustring
814 FileOpenDialogImplGtk::getFilename (void)
815 {
816 return myFilename;
817 }
820 /**
821 * To Get Multiple filenames selected at-once.
822 */
823 std::vector<Glib::ustring>FileOpenDialogImplGtk::getFilenames()
824 {
825 std::vector<Glib::ustring> result = get_filenames();
826 #ifdef WITH_GNOME_VFS
827 if (result.empty() && gnome_vfs_initialized())
828 result = get_uris();
829 #endif
830 return result;
831 }
833 Glib::ustring FileOpenDialogImplGtk::getCurrentDirectory()
834 {
835 return get_current_folder();
836 }
841 //########################################################################
842 //# F I L E S A V E
843 //########################################################################
845 /**
846 * Constructor
847 */
848 FileSaveDialogImplGtk::FileSaveDialogImplGtk( Gtk::Window &parentWindow,
849 const Glib::ustring &dir,
850 FileDialogType fileTypes,
851 const Glib::ustring &title,
852 const Glib::ustring &/*default_key*/ ) :
853 FileDialogBaseGtk(parentWindow, title, Gtk::FILE_CHOOSER_ACTION_SAVE, fileTypes, "dialogs.save_as")
854 {
855 /* One file at a time */
856 set_select_multiple(false);
858 #ifdef WITH_GNOME_VFS
859 if (gnome_vfs_initialized()) {
860 set_local_only(false);
861 }
862 #endif
864 /* Initalize to Autodetect */
865 extension = NULL;
866 /* No filename to start out with */
867 myFilename = "";
869 /* Set our dialog type (save, export, etc...)*/
870 _dialogType = fileTypes;
872 /* Set the pwd and/or the filename */
873 if (dir.size() > 0)
874 {
875 Glib::ustring udir(dir);
876 Glib::ustring::size_type len = udir.length();
877 // leaving a trailing backslash on the directory name leads to the infamous
878 // double-directory bug on win32
879 if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
880 myFilename = udir;
881 }
883 //###### Add the file types menu
884 //createFilterMenu();
886 //###### Do we want the .xxx extension automatically added?
887 fileTypeCheckbox.set_label(Glib::ustring(_("Append filename extension automatically")));
888 fileTypeCheckbox.set_active( (bool)prefs_get_int_attribute("dialogs.save_as",
889 "append_extension", 1) );
891 createFileTypeMenu();
892 fileTypeComboBox.set_size_request(200,40);
893 fileTypeComboBox.signal_changed().connect(
894 sigc::mem_fun(*this, &FileSaveDialogImplGtk::fileTypeChangedCallback) );
897 childBox.pack_start( checksBox );
898 childBox.pack_end( fileTypeComboBox );
899 checksBox.pack_start( fileTypeCheckbox );
900 checksBox.pack_start( previewCheckbox );
902 set_extra_widget( childBox );
904 //Let's do some customization
905 fileNameEntry = NULL;
906 Gtk::Container *cont = get_toplevel();
907 std::vector<Gtk::Entry *> entries;
908 findEntryWidgets(cont, entries);
909 //g_message("Found %d entry widgets\n", entries.size());
910 if (entries.size() >=1 )
911 {
912 //Catch when user hits [return] on the text field
913 fileNameEntry = entries[0];
914 fileNameEntry->signal_activate().connect(
915 sigc::mem_fun(*this, &FileSaveDialogImplGtk::fileNameEntryChangedCallback) );
916 }
918 //Let's do more customization
919 std::vector<Gtk::Expander *> expanders;
920 findExpanderWidgets(cont, expanders);
921 //g_message("Found %d expander widgets\n", expanders.size());
922 if (expanders.size() >=1 )
923 {
924 //Always show the file list
925 Gtk::Expander *expander = expanders[0];
926 expander->set_expanded(true);
927 }
929 // allow easy access to the user's own templates folder
930 gchar *templates = profile_path ("templates");
931 if ( Inkscape::IO::file_test(templates, G_FILE_TEST_EXISTS)
932 && Inkscape::IO::file_test(templates, G_FILE_TEST_IS_DIR)
933 && g_path_is_absolute(templates)
934 )
935 {
936 add_shortcut_folder(templates);
937 }
938 g_free (templates);
941 //if (extension == NULL)
942 // checkbox.set_sensitive(FALSE);
944 add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
945 set_default(*add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK));
947 show_all_children();
948 }
950 /**
951 * Destructor
952 */
953 FileSaveDialogImplGtk::~FileSaveDialogImplGtk()
954 {
955 }
957 /**
958 * Callback for fileNameEntry widget
959 */
960 void FileSaveDialogImplGtk::fileNameEntryChangedCallback()
961 {
962 if (!fileNameEntry)
963 return;
965 Glib::ustring fileName = fileNameEntry->get_text();
966 if (!Glib::get_charset()) //If we are not utf8
967 fileName = Glib::filename_to_utf8(fileName);
969 //g_message("User hit return. Text is '%s'\n", fileName.c_str());
971 if (!Glib::path_is_absolute(fileName)) {
972 //try appending to the current path
973 // not this way: fileName = get_current_folder() + "/" + fileName;
974 std::vector<Glib::ustring> pathSegments;
975 pathSegments.push_back( get_current_folder() );
976 pathSegments.push_back( fileName );
977 fileName = Glib::build_filename(pathSegments);
978 }
980 //g_message("path:'%s'\n", fileName.c_str());
982 if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
983 set_current_folder(fileName);
984 } else if (/*Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR)*/1) {
985 //dialog with either (1) select a regular file or (2) cd to dir
986 //simulate an 'OK'
987 set_filename(fileName);
988 response(Gtk::RESPONSE_OK);
989 }
990 }
994 /**
995 * Callback for fileNameEntry widget
996 */
997 void FileSaveDialogImplGtk::fileTypeChangedCallback()
998 {
999 int sel = fileTypeComboBox.get_active_row_number();
1000 if (sel<0 || sel >= (int)fileTypes.size())
1001 return;
1002 FileType type = fileTypes[sel];
1003 //g_message("selected: %s\n", type.name.c_str());
1005 extension = type.extension;
1006 Gtk::FileFilter filter;
1007 filter.add_pattern(type.pattern);
1008 set_filter(filter);
1010 updateNameAndExtension();
1011 }
1015 void FileSaveDialogImplGtk::createFileTypeMenu()
1016 {
1017 Inkscape::Extension::DB::OutputList extension_list;
1018 Inkscape::Extension::db.get_output_list(extension_list);
1019 knownExtensions.clear();
1021 for (Inkscape::Extension::DB::OutputList::iterator current_item = extension_list.begin();
1022 current_item != extension_list.end(); current_item++)
1023 {
1024 Inkscape::Extension::Output * omod = *current_item;
1026 // FIXME: would be nice to grey them out instead of not listing them
1027 if (omod->deactivated()) continue;
1029 FileType type;
1030 type.name = (_(omod->get_filetypename()));
1031 type.pattern = "*";
1032 Glib::ustring extension = omod->get_extension();
1033 knownExtensions.insert( extension.casefold() );
1034 fileDialogExtensionToPattern (type.pattern, extension);
1035 type.extension= omod;
1036 fileTypeComboBox.append_text(type.name);
1037 fileTypes.push_back(type);
1038 }
1040 //#Let user choose
1041 FileType guessType;
1042 guessType.name = _("Guess from extension");
1043 guessType.pattern = "*";
1044 guessType.extension = NULL;
1045 fileTypeComboBox.append_text(guessType.name);
1046 fileTypes.push_back(guessType);
1049 fileTypeComboBox.set_active(0);
1050 fileTypeChangedCallback(); //call at least once to set the filter
1051 }
1057 /**
1058 * Show this dialog modally. Return true if user hits [OK]
1059 */
1060 bool
1061 FileSaveDialogImplGtk::show()
1062 {
1063 change_path(myFilename);
1064 set_modal (TRUE); //Window
1065 sp_transientize((GtkWidget *)gobj()); //Make transient
1066 gint b = run(); //Dialog
1067 svgPreview.showNoPreview();
1068 set_preview_widget_active(false);
1069 hide();
1071 if (b == Gtk::RESPONSE_OK)
1072 {
1073 updateNameAndExtension();
1075 // Store changes of the "Append filename automatically" checkbox back to preferences.
1076 prefs_set_int_attribute("dialogs.save_as", "append_extension", fileTypeCheckbox.get_active());
1078 // Store the last used save-as filetype to preferences.
1079 prefs_set_string_attribute("dialogs.save_as", "default",
1080 ( extension != NULL ? extension->get_id() : "" ));
1082 cleanup( true );
1084 return TRUE;
1085 }
1086 else
1087 {
1088 cleanup( false );
1090 return FALSE;
1091 }
1092 }
1095 /**
1096 * Get the file extension type that was selected by the user. Valid after an [OK]
1097 */
1098 Inkscape::Extension::Extension *
1099 FileSaveDialogImplGtk::getSelectionType()
1100 {
1101 return extension;
1102 }
1104 void FileSaveDialogImplGtk::setSelectionType( Inkscape::Extension::Extension * key )
1105 {
1106 // If no pointer to extension is passed in, look up based on filename extension.
1107 if ( !key ) {
1108 // Not quite UTF-8 here.
1109 gchar *filenameLower = g_ascii_strdown(myFilename.c_str(), -1);
1110 for ( int i = 0; !key && (i < (int)fileTypes.size()); i++ ) {
1111 Inkscape::Extension::Output *ext = dynamic_cast<Inkscape::Extension::Output*>(fileTypes[i].extension);
1112 if ( ext && ext->get_extension() ) {
1113 gchar *extensionLower = g_ascii_strdown( ext->get_extension(), -1 );
1114 if ( g_str_has_suffix(filenameLower, extensionLower) ) {
1115 key = fileTypes[i].extension;
1116 }
1117 g_free(extensionLower);
1118 }
1119 }
1120 g_free(filenameLower);
1121 }
1123 // Ensure the proper entry in the combo box is selected.
1124 if ( key ) {
1125 extension = key;
1126 gchar const * extensionID = extension->get_id();
1127 if ( extensionID ) {
1128 for ( int i = 0; i < (int)fileTypes.size(); i++ ) {
1129 Inkscape::Extension::Extension *ext = fileTypes[i].extension;
1130 if ( ext ) {
1131 gchar const * id = ext->get_id();
1132 if ( id && ( strcmp(extensionID, id) == 0) ) {
1133 int oldSel = fileTypeComboBox.get_active_row_number();
1134 if ( i != oldSel ) {
1135 fileTypeComboBox.set_active(i);
1136 }
1137 break;
1138 }
1139 }
1140 }
1141 }
1142 }
1143 }
1145 Glib::ustring FileSaveDialogImplGtk::getCurrentDirectory()
1146 {
1147 return get_current_folder();
1148 }
1151 /*void
1152 FileSaveDialogImplGtk::change_title(const Glib::ustring& title)
1153 {
1154 set_title(title);
1155 }*/
1157 /**
1158 * Change the default save path location.
1159 */
1160 void
1161 FileSaveDialogImplGtk::change_path(const Glib::ustring& path)
1162 {
1163 myFilename = path;
1165 if (Glib::file_test(myFilename, Glib::FILE_TEST_IS_DIR)) {
1166 //fprintf(stderr,"set_current_folder(%s)\n",myFilename.c_str());
1167 set_current_folder(myFilename);
1168 } else {
1169 //fprintf(stderr,"set_filename(%s)\n",myFilename.c_str());
1170 if ( Glib::file_test( myFilename, Glib::FILE_TEST_EXISTS ) ) {
1171 set_filename(myFilename);
1172 } else {
1173 std::string dirName = Glib::path_get_dirname( myFilename );
1174 if ( dirName != get_current_folder() ) {
1175 set_current_folder(dirName);
1176 }
1177 }
1178 Glib::ustring basename = Glib::path_get_basename(myFilename);
1179 //fprintf(stderr,"set_current_name(%s)\n",basename.c_str());
1180 try {
1181 set_current_name( Glib::filename_to_utf8(basename) );
1182 } catch ( Glib::ConvertError& e ) {
1183 g_warning( "Error converting save filename to UTF-8." );
1184 // try a fallback.
1185 set_current_name( basename );
1186 }
1187 }
1188 }
1190 void FileSaveDialogImplGtk::updateNameAndExtension()
1191 {
1192 // Pick up any changes the user has typed in.
1193 Glib::ustring tmp = get_filename();
1194 #ifdef WITH_GNOME_VFS
1195 if ( tmp.empty() && gnome_vfs_initialized() ) {
1196 tmp = get_uri();
1197 }
1198 #endif
1199 if ( !tmp.empty() ) {
1200 myFilename = tmp;
1201 }
1203 Inkscape::Extension::Output* newOut = extension ? dynamic_cast<Inkscape::Extension::Output*>(extension) : 0;
1204 if ( fileTypeCheckbox.get_active() && newOut ) {
1205 // Append the file extension if it's not already present
1206 appendExtension(myFilename, newOut);
1207 }
1208 }
1211 //########################################################################
1212 //# F I L E E X P O R T
1213 //########################################################################
1215 /**
1216 * Callback for fileNameEntry widget
1217 */
1218 void FileExportDialogImpl::fileNameEntryChangedCallback()
1219 {
1220 if (!fileNameEntry)
1221 return;
1223 Glib::ustring fileName = fileNameEntry->get_text();
1224 if (!Glib::get_charset()) //If we are not utf8
1225 fileName = Glib::filename_to_utf8(fileName);
1227 //g_message("User hit return. Text is '%s'\n", fileName.c_str());
1229 if (!Glib::path_is_absolute(fileName)) {
1230 //try appending to the current path
1231 // not this way: fileName = get_current_folder() + "/" + fileName;
1232 std::vector<Glib::ustring> pathSegments;
1233 pathSegments.push_back( get_current_folder() );
1234 pathSegments.push_back( fileName );
1235 fileName = Glib::build_filename(pathSegments);
1236 }
1238 //g_message("path:'%s'\n", fileName.c_str());
1240 if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
1241 set_current_folder(fileName);
1242 } else if (/*Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR)*/1) {
1243 //dialog with either (1) select a regular file or (2) cd to dir
1244 //simulate an 'OK'
1245 set_filename(fileName);
1246 response(Gtk::RESPONSE_OK);
1247 }
1248 }
1252 /**
1253 * Callback for fileNameEntry widget
1254 */
1255 void FileExportDialogImpl::fileTypeChangedCallback()
1256 {
1257 int sel = fileTypeComboBox.get_active_row_number();
1258 if (sel<0 || sel >= (int)fileTypes.size())
1259 return;
1260 FileType type = fileTypes[sel];
1261 //g_message("selected: %s\n", type.name.c_str());
1262 Gtk::FileFilter filter;
1263 filter.add_pattern(type.pattern);
1264 set_filter(filter);
1265 }
1269 void FileExportDialogImpl::createFileTypeMenu()
1270 {
1271 Inkscape::Extension::DB::OutputList extension_list;
1272 Inkscape::Extension::db.get_output_list(extension_list);
1274 for (Inkscape::Extension::DB::OutputList::iterator current_item = extension_list.begin();
1275 current_item != extension_list.end(); current_item++)
1276 {
1277 Inkscape::Extension::Output * omod = *current_item;
1279 // FIXME: would be nice to grey them out instead of not listing them
1280 if (omod->deactivated()) continue;
1282 FileType type;
1283 type.name = (_(omod->get_filetypename()));
1284 type.pattern = "*";
1285 Glib::ustring extension = omod->get_extension();
1286 fileDialogExtensionToPattern (type.pattern, extension);
1287 type.extension= omod;
1288 fileTypeComboBox.append_text(type.name);
1289 fileTypes.push_back(type);
1290 }
1292 //#Let user choose
1293 FileType guessType;
1294 guessType.name = _("Guess from extension");
1295 guessType.pattern = "*";
1296 guessType.extension = NULL;
1297 fileTypeComboBox.append_text(guessType.name);
1298 fileTypes.push_back(guessType);
1301 fileTypeComboBox.set_active(0);
1302 fileTypeChangedCallback(); //call at least once to set the filter
1303 }
1306 /**
1307 * Constructor
1308 */
1309 FileExportDialogImpl::FileExportDialogImpl( Gtk::Window& parentWindow,
1310 const Glib::ustring &dir,
1311 FileDialogType fileTypes,
1312 const Glib::ustring &title,
1313 const Glib::ustring &/*default_key*/ ) :
1314 FileDialogBaseGtk(parentWindow, title, Gtk::FILE_CHOOSER_ACTION_SAVE, fileTypes, "dialogs.export"),
1315 sourceX0Spinner("X0", _("Left edge of source")),
1316 sourceY0Spinner("Y0", _("Top edge of source")),
1317 sourceX1Spinner("X1", _("Right edge of source")),
1318 sourceY1Spinner("Y1", _("Bottom edge of source")),
1319 sourceWidthSpinner("Width", _("Source width")),
1320 sourceHeightSpinner("Height", _("Source height")),
1321 destWidthSpinner("Width", _("Destination width")),
1322 destHeightSpinner("Height", _("Destination height")),
1323 destDPISpinner("DPI", _("Resolution (dots per inch)"))
1324 {
1325 append_extension = (bool)prefs_get_int_attribute("dialogs.save_as", "append_extension", 1);
1327 /* One file at a time */
1328 set_select_multiple(false);
1330 #ifdef WITH_GNOME_VFS
1331 if (gnome_vfs_initialized()) {
1332 set_local_only(false);
1333 }
1334 #endif
1336 /* Initalize to Autodetect */
1337 extension = NULL;
1338 /* No filename to start out with */
1339 myFilename = "";
1341 /* Set our dialog type (save, export, etc...)*/
1342 _dialogType = fileTypes;
1344 /* Set the pwd and/or the filename */
1345 if (dir.size()>0)
1346 {
1347 Glib::ustring udir(dir);
1348 Glib::ustring::size_type len = udir.length();
1349 // leaving a trailing backslash on the directory name leads to the infamous
1350 // double-directory bug on win32
1351 if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
1352 set_current_folder(udir.c_str());
1353 }
1355 //#########################################
1356 //## EXTRA WIDGET -- SOURCE SIDE
1357 //#########################################
1359 //##### Export options buttons/spinners, etc
1360 documentButton.set_label(_("Document"));
1361 scopeBox.pack_start(documentButton);
1362 scopeGroup = documentButton.get_group();
1364 pageButton.set_label(_("Page"));
1365 pageButton.set_group(scopeGroup);
1366 scopeBox.pack_start(pageButton);
1368 selectionButton.set_label(_("Selection"));
1369 selectionButton.set_group(scopeGroup);
1370 scopeBox.pack_start(selectionButton);
1372 customButton.set_label(_("Custom"));
1373 customButton.set_group(scopeGroup);
1374 scopeBox.pack_start(customButton);
1376 sourceBox.pack_start(scopeBox);
1380 //dimension buttons
1381 sourceTable.resize(3,3);
1382 sourceTable.attach(sourceX0Spinner, 0,1,0,1);
1383 sourceTable.attach(sourceY0Spinner, 1,2,0,1);
1384 sourceUnitsSpinner.setUnitType(UNIT_TYPE_LINEAR);
1385 sourceTable.attach(sourceUnitsSpinner, 2,3,0,1);
1386 sourceTable.attach(sourceX1Spinner, 0,1,1,2);
1387 sourceTable.attach(sourceY1Spinner, 1,2,1,2);
1388 sourceTable.attach(sourceWidthSpinner, 0,1,2,3);
1389 sourceTable.attach(sourceHeightSpinner, 1,2,2,3);
1391 sourceBox.pack_start(sourceTable);
1392 sourceFrame.set_label(_("Source"));
1393 sourceFrame.add(sourceBox);
1394 exportOptionsBox.pack_start(sourceFrame);
1397 //#########################################
1398 //## EXTRA WIDGET -- SOURCE SIDE
1399 //#########################################
1402 destTable.resize(3,3);
1403 destTable.attach(destWidthSpinner, 0,1,0,1);
1404 destTable.attach(destHeightSpinner, 1,2,0,1);
1405 destUnitsSpinner.setUnitType(UNIT_TYPE_LINEAR);
1406 destTable.attach(destUnitsSpinner, 2,3,0,1);
1407 destTable.attach(destDPISpinner, 0,1,1,2);
1409 destBox.pack_start(destTable);
1412 cairoButton.set_label(_("Cairo"));
1413 otherOptionBox.pack_start(cairoButton);
1415 antiAliasButton.set_label(_("Antialias"));
1416 otherOptionBox.pack_start(antiAliasButton);
1418 backgroundButton.set_label(_("Background"));
1419 otherOptionBox.pack_start(backgroundButton);
1421 destBox.pack_start(otherOptionBox);
1427 //###### File options
1428 //###### Do we want the .xxx extension automatically added?
1429 fileTypeCheckbox.set_label(Glib::ustring(_("Append filename extension automatically")));
1430 fileTypeCheckbox.set_active(append_extension);
1431 destBox.pack_start(fileTypeCheckbox);
1433 //###### File type menu
1434 createFileTypeMenu();
1435 fileTypeComboBox.set_size_request(200,40);
1436 fileTypeComboBox.signal_changed().connect(
1437 sigc::mem_fun(*this, &FileExportDialogImpl::fileTypeChangedCallback) );
1439 destBox.pack_start(fileTypeComboBox);
1441 destFrame.set_label(_("Destination"));
1442 destFrame.add(destBox);
1443 exportOptionsBox.pack_start(destFrame);
1445 //##### Put the two boxes and their parent onto the dialog
1446 exportOptionsBox.pack_start(sourceFrame);
1447 exportOptionsBox.pack_start(destFrame);
1449 set_extra_widget(exportOptionsBox);
1454 //Let's do some customization
1455 fileNameEntry = NULL;
1456 Gtk::Container *cont = get_toplevel();
1457 std::vector<Gtk::Entry *> entries;
1458 findEntryWidgets(cont, entries);
1459 //g_message("Found %d entry widgets\n", entries.size());
1460 if (entries.size() >=1 )
1461 {
1462 //Catch when user hits [return] on the text field
1463 fileNameEntry = entries[0];
1464 fileNameEntry->signal_activate().connect(
1465 sigc::mem_fun(*this, &FileExportDialogImpl::fileNameEntryChangedCallback) );
1466 }
1468 //Let's do more customization
1469 std::vector<Gtk::Expander *> expanders;
1470 findExpanderWidgets(cont, expanders);
1471 //g_message("Found %d expander widgets\n", expanders.size());
1472 if (expanders.size() >=1 )
1473 {
1474 //Always show the file list
1475 Gtk::Expander *expander = expanders[0];
1476 expander->set_expanded(true);
1477 }
1480 //if (extension == NULL)
1481 // checkbox.set_sensitive(FALSE);
1483 add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1484 set_default(*add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK));
1486 show_all_children();
1487 }
1489 /**
1490 * Destructor
1491 */
1492 FileExportDialogImpl::~FileExportDialogImpl()
1493 {
1494 }
1498 /**
1499 * Show this dialog modally. Return true if user hits [OK]
1500 */
1501 bool
1502 FileExportDialogImpl::show()
1503 {
1504 Glib::ustring s = Glib::filename_to_utf8 (get_current_folder());
1505 if (s.length() == 0)
1506 s = getcwd (NULL, 0);
1507 set_current_folder(Glib::filename_from_utf8(s)); //hack to force initial dir listing
1508 set_modal (TRUE); //Window
1509 sp_transientize((GtkWidget *)gobj()); //Make transient
1510 gint b = run(); //Dialog
1511 svgPreview.showNoPreview();
1512 hide();
1514 if (b == Gtk::RESPONSE_OK)
1515 {
1516 int sel = fileTypeComboBox.get_active_row_number ();
1517 if (sel>=0 && sel< (int)fileTypes.size())
1518 {
1519 FileType &type = fileTypes[sel];
1520 extension = type.extension;
1521 }
1522 myFilename = get_filename();
1523 #ifdef WITH_GNOME_VFS
1524 if ( myFilename.empty() && gnome_vfs_initialized() ) {
1525 myFilename = get_uri();
1526 }
1527 #endif
1529 /*
1531 // FIXME: Why do we have more code
1533 append_extension = checkbox.get_active();
1534 prefs_set_int_attribute("dialogs.save_as", "append_extension", append_extension);
1535 prefs_set_string_attribute("dialogs.save_as", "default",
1536 ( extension != NULL ? extension->get_id() : "" ));
1537 */
1538 return TRUE;
1539 }
1540 else
1541 {
1542 return FALSE;
1543 }
1544 }
1547 /**
1548 * Get the file extension type that was selected by the user. Valid after an [OK]
1549 */
1550 Inkscape::Extension::Extension *
1551 FileExportDialogImpl::getSelectionType()
1552 {
1553 return extension;
1554 }
1557 /**
1558 * Get the file name chosen by the user. Valid after an [OK]
1559 */
1560 Glib::ustring
1561 FileExportDialogImpl::getFilename()
1562 {
1563 return myFilename;
1564 }
1567 } //namespace Dialog
1568 } //namespace UI
1569 } //namespace Inkscape
1571 /*
1572 Local Variables:
1573 mode:c++
1574 c-file-style:"stroustrup"
1575 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1576 indent-tabs-mode:nil
1577 fill-column:99
1578 End:
1579 */
1580 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :