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 //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 const gchar* docTitle) :
854 FileDialogBaseGtk(parentWindow, title, Gtk::FILE_CHOOSER_ACTION_SAVE, fileTypes, "/dialogs/save_as")
855 {
856 FileSaveDialog::myDocTitle = docTitle;
858 /* One file at a time */
859 set_select_multiple(false);
861 #ifdef WITH_GNOME_VFS
862 if (gnome_vfs_initialized()) {
863 set_local_only(false);
864 }
865 #endif
867 /* Initalize to Autodetect */
868 extension = NULL;
869 /* No filename to start out with */
870 myFilename = "";
872 /* Set our dialog type (save, export, etc...)*/
873 _dialogType = fileTypes;
875 /* Set the pwd and/or the filename */
876 if (dir.size() > 0)
877 {
878 Glib::ustring udir(dir);
879 Glib::ustring::size_type len = udir.length();
880 // leaving a trailing backslash on the directory name leads to the infamous
881 // double-directory bug on win32
882 if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
883 myFilename = udir;
884 }
886 //###### Add the file types menu
887 //createFilterMenu();
889 //###### Do we want the .xxx extension automatically added?
890 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
891 fileTypeCheckbox.set_label(Glib::ustring(_("Append filename extension automatically")));
892 fileTypeCheckbox.set_active(prefs->getBool("/dialogs/save_as/append_extension", true));
894 createFileTypeMenu();
895 fileTypeComboBox.set_size_request(200,40);
896 fileTypeComboBox.signal_changed().connect(
897 sigc::mem_fun(*this, &FileSaveDialogImplGtk::fileTypeChangedCallback) );
900 childBox.pack_start( checksBox );
901 childBox.pack_end( fileTypeComboBox );
902 checksBox.pack_start( fileTypeCheckbox );
903 checksBox.pack_start( previewCheckbox );
905 set_extra_widget( childBox );
907 //Let's do some customization
908 fileNameEntry = NULL;
909 Gtk::Container *cont = get_toplevel();
910 std::vector<Gtk::Entry *> entries;
911 findEntryWidgets(cont, entries);
912 //g_message("Found %d entry widgets\n", entries.size());
913 if (entries.size() >=1 )
914 {
915 //Catch when user hits [return] on the text field
916 fileNameEntry = entries[0];
917 fileNameEntry->signal_activate().connect(
918 sigc::mem_fun(*this, &FileSaveDialogImplGtk::fileNameEntryChangedCallback) );
919 }
921 //Let's do more customization
922 std::vector<Gtk::Expander *> expanders;
923 findExpanderWidgets(cont, expanders);
924 //g_message("Found %d expander widgets\n", expanders.size());
925 if (expanders.size() >=1 )
926 {
927 //Always show the file list
928 Gtk::Expander *expander = expanders[0];
929 expander->set_expanded(true);
930 }
932 // allow easy access to the user's own templates folder
933 gchar *templates = profile_path ("templates");
934 if ( Inkscape::IO::file_test(templates, G_FILE_TEST_EXISTS)
935 && Inkscape::IO::file_test(templates, G_FILE_TEST_IS_DIR)
936 && g_path_is_absolute(templates)
937 )
938 {
939 add_shortcut_folder(templates);
940 }
941 g_free (templates);
944 //if (extension == NULL)
945 // checkbox.set_sensitive(FALSE);
947 add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
948 set_default(*add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK));
950 show_all_children();
951 }
953 /**
954 * Destructor
955 */
956 FileSaveDialogImplGtk::~FileSaveDialogImplGtk()
957 {
958 }
960 /**
961 * Callback for fileNameEntry widget
962 */
963 void FileSaveDialogImplGtk::fileNameEntryChangedCallback()
964 {
965 if (!fileNameEntry)
966 return;
968 Glib::ustring fileName = fileNameEntry->get_text();
969 if (!Glib::get_charset()) //If we are not utf8
970 fileName = Glib::filename_to_utf8(fileName);
972 //g_message("User hit return. Text is '%s'\n", fileName.c_str());
974 if (!Glib::path_is_absolute(fileName)) {
975 //try appending to the current path
976 // not this way: fileName = get_current_folder() + "/" + fileName;
977 std::vector<Glib::ustring> pathSegments;
978 pathSegments.push_back( get_current_folder() );
979 pathSegments.push_back( fileName );
980 fileName = Glib::build_filename(pathSegments);
981 }
983 //g_message("path:'%s'\n", fileName.c_str());
985 if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
986 set_current_folder(fileName);
987 } else if (/*Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR)*/1) {
988 //dialog with either (1) select a regular file or (2) cd to dir
989 //simulate an 'OK'
990 set_filename(fileName);
991 response(Gtk::RESPONSE_OK);
992 }
993 }
997 /**
998 * Callback for fileNameEntry widget
999 */
1000 void FileSaveDialogImplGtk::fileTypeChangedCallback()
1001 {
1002 int sel = fileTypeComboBox.get_active_row_number();
1003 if (sel<0 || sel >= (int)fileTypes.size())
1004 return;
1005 FileType type = fileTypes[sel];
1006 //g_message("selected: %s\n", type.name.c_str());
1008 extension = type.extension;
1009 Gtk::FileFilter filter;
1010 filter.add_pattern(type.pattern);
1011 set_filter(filter);
1013 updateNameAndExtension();
1014 }
1018 void FileSaveDialogImplGtk::createFileTypeMenu()
1019 {
1020 Inkscape::Extension::DB::OutputList extension_list;
1021 Inkscape::Extension::db.get_output_list(extension_list);
1022 knownExtensions.clear();
1024 for (Inkscape::Extension::DB::OutputList::iterator current_item = extension_list.begin();
1025 current_item != extension_list.end(); current_item++)
1026 {
1027 Inkscape::Extension::Output * omod = *current_item;
1029 // FIXME: would be nice to grey them out instead of not listing them
1030 if (omod->deactivated()) continue;
1032 FileType type;
1033 type.name = (_(omod->get_filetypename()));
1034 type.pattern = "*";
1035 Glib::ustring extension = omod->get_extension();
1036 knownExtensions.insert( extension.casefold() );
1037 fileDialogExtensionToPattern (type.pattern, extension);
1038 type.extension= omod;
1039 fileTypeComboBox.append_text(type.name);
1040 fileTypes.push_back(type);
1041 }
1043 //#Let user choose
1044 FileType guessType;
1045 guessType.name = _("Guess from extension");
1046 guessType.pattern = "*";
1047 guessType.extension = NULL;
1048 fileTypeComboBox.append_text(guessType.name);
1049 fileTypes.push_back(guessType);
1052 fileTypeComboBox.set_active(0);
1053 fileTypeChangedCallback(); //call at least once to set the filter
1054 }
1060 /**
1061 * Show this dialog modally. Return true if user hits [OK]
1062 */
1063 bool
1064 FileSaveDialogImplGtk::show()
1065 {
1066 change_path(myFilename);
1067 set_modal (TRUE); //Window
1068 sp_transientize((GtkWidget *)gobj()); //Make transient
1069 gint b = run(); //Dialog
1070 svgPreview.showNoPreview();
1071 set_preview_widget_active(false);
1072 hide();
1074 if (b == Gtk::RESPONSE_OK) {
1075 updateNameAndExtension();
1076 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1078 // Store changes of the "Append filename automatically" checkbox back to preferences.
1079 prefs->setBool("/dialogs/save_as/append_extension", fileTypeCheckbox.get_active());
1081 // Store the last used save-as filetype to preferences.
1082 prefs->setString("/dialogs/save_as/default", ( extension != NULL ? extension->get_id() : "" ));
1084 cleanup( true );
1086 return TRUE;
1087 } else {
1088 cleanup( false );
1089 return FALSE;
1090 }
1091 }
1094 /**
1095 * Get the file extension type that was selected by the user. Valid after an [OK]
1096 */
1097 Inkscape::Extension::Extension *
1098 FileSaveDialogImplGtk::getSelectionType()
1099 {
1100 return extension;
1101 }
1103 void FileSaveDialogImplGtk::setSelectionType( Inkscape::Extension::Extension * key )
1104 {
1105 // If no pointer to extension is passed in, look up based on filename extension.
1106 if ( !key ) {
1107 // Not quite UTF-8 here.
1108 gchar *filenameLower = g_ascii_strdown(myFilename.c_str(), -1);
1109 for ( int i = 0; !key && (i < (int)fileTypes.size()); i++ ) {
1110 Inkscape::Extension::Output *ext = dynamic_cast<Inkscape::Extension::Output*>(fileTypes[i].extension);
1111 if ( ext && ext->get_extension() ) {
1112 gchar *extensionLower = g_ascii_strdown( ext->get_extension(), -1 );
1113 if ( g_str_has_suffix(filenameLower, extensionLower) ) {
1114 key = fileTypes[i].extension;
1115 }
1116 g_free(extensionLower);
1117 }
1118 }
1119 g_free(filenameLower);
1120 }
1122 // Ensure the proper entry in the combo box is selected.
1123 if ( key ) {
1124 extension = key;
1125 gchar const * extensionID = extension->get_id();
1126 if ( extensionID ) {
1127 for ( int i = 0; i < (int)fileTypes.size(); i++ ) {
1128 Inkscape::Extension::Extension *ext = fileTypes[i].extension;
1129 if ( ext ) {
1130 gchar const * id = ext->get_id();
1131 if ( id && ( strcmp(extensionID, id) == 0) ) {
1132 int oldSel = fileTypeComboBox.get_active_row_number();
1133 if ( i != oldSel ) {
1134 fileTypeComboBox.set_active(i);
1135 }
1136 break;
1137 }
1138 }
1139 }
1140 }
1141 }
1142 }
1144 Glib::ustring FileSaveDialogImplGtk::getCurrentDirectory()
1145 {
1146 return get_current_folder();
1147 }
1150 /*void
1151 FileSaveDialogImplGtk::change_title(const Glib::ustring& title)
1152 {
1153 set_title(title);
1154 }*/
1156 /**
1157 * Change the default save path location.
1158 */
1159 void
1160 FileSaveDialogImplGtk::change_path(const Glib::ustring& path)
1161 {
1162 myFilename = path;
1164 if (Glib::file_test(myFilename, Glib::FILE_TEST_IS_DIR)) {
1165 //fprintf(stderr,"set_current_folder(%s)\n",myFilename.c_str());
1166 set_current_folder(myFilename);
1167 } else {
1168 //fprintf(stderr,"set_filename(%s)\n",myFilename.c_str());
1169 if ( Glib::file_test( myFilename, Glib::FILE_TEST_EXISTS ) ) {
1170 set_filename(myFilename);
1171 } else {
1172 std::string dirName = Glib::path_get_dirname( myFilename );
1173 if ( dirName != get_current_folder() ) {
1174 set_current_folder(dirName);
1175 }
1176 }
1177 Glib::ustring basename = Glib::path_get_basename(myFilename);
1178 //fprintf(stderr,"set_current_name(%s)\n",basename.c_str());
1179 try {
1180 set_current_name( Glib::filename_to_utf8(basename) );
1181 } catch ( Glib::ConvertError& e ) {
1182 g_warning( "Error converting save filename to UTF-8." );
1183 // try a fallback.
1184 set_current_name( basename );
1185 }
1186 }
1187 }
1189 void FileSaveDialogImplGtk::updateNameAndExtension()
1190 {
1191 // Pick up any changes the user has typed in.
1192 Glib::ustring tmp = get_filename();
1193 #ifdef WITH_GNOME_VFS
1194 if ( tmp.empty() && gnome_vfs_initialized() ) {
1195 tmp = get_uri();
1196 }
1197 #endif
1198 if ( !tmp.empty() ) {
1199 myFilename = tmp;
1200 }
1202 Inkscape::Extension::Output* newOut = extension ? dynamic_cast<Inkscape::Extension::Output*>(extension) : 0;
1203 if ( fileTypeCheckbox.get_active() && newOut ) {
1204 // Append the file extension if it's not already present
1205 appendExtension(myFilename, newOut);
1206 }
1207 }
1210 //########################################################################
1211 //# F I L E E X P O R T
1212 //########################################################################
1214 /**
1215 * Callback for fileNameEntry widget
1216 */
1217 void FileExportDialogImpl::fileNameEntryChangedCallback()
1218 {
1219 if (!fileNameEntry)
1220 return;
1222 Glib::ustring fileName = fileNameEntry->get_text();
1223 if (!Glib::get_charset()) //If we are not utf8
1224 fileName = Glib::filename_to_utf8(fileName);
1226 //g_message("User hit return. Text is '%s'\n", fileName.c_str());
1228 if (!Glib::path_is_absolute(fileName)) {
1229 //try appending to the current path
1230 // not this way: fileName = get_current_folder() + "/" + fileName;
1231 std::vector<Glib::ustring> pathSegments;
1232 pathSegments.push_back( get_current_folder() );
1233 pathSegments.push_back( fileName );
1234 fileName = Glib::build_filename(pathSegments);
1235 }
1237 //g_message("path:'%s'\n", fileName.c_str());
1239 if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
1240 set_current_folder(fileName);
1241 } else if (/*Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR)*/1) {
1242 //dialog with either (1) select a regular file or (2) cd to dir
1243 //simulate an 'OK'
1244 set_filename(fileName);
1245 response(Gtk::RESPONSE_OK);
1246 }
1247 }
1251 /**
1252 * Callback for fileNameEntry widget
1253 */
1254 void FileExportDialogImpl::fileTypeChangedCallback()
1255 {
1256 int sel = fileTypeComboBox.get_active_row_number();
1257 if (sel<0 || sel >= (int)fileTypes.size())
1258 return;
1259 FileType type = fileTypes[sel];
1260 //g_message("selected: %s\n", type.name.c_str());
1261 Gtk::FileFilter filter;
1262 filter.add_pattern(type.pattern);
1263 set_filter(filter);
1264 }
1268 void FileExportDialogImpl::createFileTypeMenu()
1269 {
1270 Inkscape::Extension::DB::OutputList extension_list;
1271 Inkscape::Extension::db.get_output_list(extension_list);
1273 for (Inkscape::Extension::DB::OutputList::iterator current_item = extension_list.begin();
1274 current_item != extension_list.end(); current_item++)
1275 {
1276 Inkscape::Extension::Output * omod = *current_item;
1278 // FIXME: would be nice to grey them out instead of not listing them
1279 if (omod->deactivated()) continue;
1281 FileType type;
1282 type.name = (_(omod->get_filetypename()));
1283 type.pattern = "*";
1284 Glib::ustring extension = omod->get_extension();
1285 fileDialogExtensionToPattern (type.pattern, extension);
1286 type.extension= omod;
1287 fileTypeComboBox.append_text(type.name);
1288 fileTypes.push_back(type);
1289 }
1291 //#Let user choose
1292 FileType guessType;
1293 guessType.name = _("Guess from extension");
1294 guessType.pattern = "*";
1295 guessType.extension = NULL;
1296 fileTypeComboBox.append_text(guessType.name);
1297 fileTypes.push_back(guessType);
1300 fileTypeComboBox.set_active(0);
1301 fileTypeChangedCallback(); //call at least once to set the filter
1302 }
1305 /**
1306 * Constructor
1307 */
1308 FileExportDialogImpl::FileExportDialogImpl( Gtk::Window& parentWindow,
1309 const Glib::ustring &dir,
1310 FileDialogType fileTypes,
1311 const Glib::ustring &title,
1312 const Glib::ustring &/*default_key*/ ) :
1313 FileDialogBaseGtk(parentWindow, title, Gtk::FILE_CHOOSER_ACTION_SAVE, fileTypes, "/dialogs/export"),
1314 sourceX0Spinner("X0", _("Left edge of source")),
1315 sourceY0Spinner("Y0", _("Top edge of source")),
1316 sourceX1Spinner("X1", _("Right edge of source")),
1317 sourceY1Spinner("Y1", _("Bottom edge of source")),
1318 sourceWidthSpinner("Width", _("Source width")),
1319 sourceHeightSpinner("Height", _("Source height")),
1320 destWidthSpinner("Width", _("Destination width")),
1321 destHeightSpinner("Height", _("Destination height")),
1322 destDPISpinner("DPI", _("Resolution (dots per inch)"))
1323 {
1324 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1325 append_extension = prefs->getBool("/dialogs/save_as/append_extension", true);
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 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1535 prefs->setBool("/dialogs/save_as/append_extension", append_extension);
1536 prefs->setBool("/dialogs/save_as/default", ( 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:encoding=utf-8:textwidth=99 :