1 /*
2 * Implementation of the file dialog interfaces defined in filedialog.h
3 *
4 * Authors:
5 * Bob Jamison
6 * Other dudes from The Inkscape Organization
7 *
8 * Copyright (C) 2004-2006 The Inkscape Organization
9 *
10 * Released under GNU GPL, read the file 'COPYING' for more information
11 */
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
19 //Temporary ugly hack
20 //Remove these after the get_filter() calls in
21 //show() on both classes are fixed
22 #include <gtk/gtkfilechooser.h>
24 //Another hack
25 #include <gtk/gtkentry.h>
26 #include <gtk/gtkexpander.h>
28 #include <unistd.h>
29 #include <sys/stat.h>
30 #include <glibmm/i18n.h>
31 #include <gtkmm/box.h>
32 #include <gtkmm/colorbutton.h>
33 #include <gtkmm/frame.h>
34 #include <gtkmm/filechooserdialog.h>
35 #include <gtkmm/menubar.h>
36 #include <gtkmm/menu.h>
37 #include <gtkmm/entry.h>
38 #include <gtkmm/expander.h>
39 #include <gtkmm/comboboxtext.h>
40 #include <gtkmm/stock.h>
41 #include <gdkmm/pixbuf.h>
43 #include "prefs-utils.h"
44 #include <dialogs/dialog-events.h>
45 #include <extension/input.h>
46 #include <extension/output.h>
47 #include <extension/db.h>
48 #include "inkscape.h"
49 #include "svg-view-widget.h"
50 #include "filedialog.h"
51 #include "gc-core.h"
53 //For export dialog
54 #include "ui/widget/scalar-unit.h"
57 #undef INK_DUMP_FILENAME_CONV
59 #ifdef INK_DUMP_FILENAME_CONV
60 void dump_str( const gchar* str, const gchar* prefix );
61 void dump_ustr( const Glib::ustring& ustr );
62 #endif
64 namespace Inkscape
65 {
66 namespace UI
67 {
68 namespace Dialog
69 {
75 //########################################################################
76 //### U T I L I T Y
77 //########################################################################
79 /**
80 \brief A quick function to turn a standard extension into a searchable
81 pattern for the file dialogs
82 \param pattern The patter that the extension should be written to
83 \param in_file_extension The C string that represents the extension
85 This function just goes through the string, and takes all characters
86 and puts a [<upper><lower>] so that both are searched and shown in
87 the file dialog. This function edits the pattern string to make
88 this happen.
89 */
90 static void
91 fileDialogExtensionToPattern(Glib::ustring &pattern,
92 Glib::ustring &extension)
93 {
94 for (unsigned int i = 0; i < extension.length(); i++ )
95 {
96 Glib::ustring::value_type ch = extension[i];
97 if ( Glib::Unicode::isalpha(ch) )
98 {
99 pattern += '[';
100 pattern += Glib::Unicode::toupper(ch);
101 pattern += Glib::Unicode::tolower(ch);
102 pattern += ']';
103 }
104 else
105 {
106 pattern += ch;
107 }
108 }
109 }
112 /**
113 * Hack: Find all entry widgets in a container
114 */
115 static void
116 findEntryWidgets(Gtk::Container *parent,
117 std::vector<Gtk::Entry *> &result)
118 {
119 if (!parent)
120 return;
121 std::vector<Gtk::Widget *> children = parent->get_children();
122 for (unsigned int i=0; i<children.size() ; i++)
123 {
124 Gtk::Widget *child = children[i];
125 GtkWidget *wid = child->gobj();
126 if (GTK_IS_ENTRY(wid))
127 result.push_back((Gtk::Entry *)child);
128 else if (GTK_IS_CONTAINER(wid))
129 findEntryWidgets((Gtk::Container *)child, result);
130 }
132 }
137 /**
138 * Hack: Find all expander widgets in a container
139 */
140 static void
141 findExpanderWidgets(Gtk::Container *parent,
142 std::vector<Gtk::Expander *> &result)
143 {
144 if (!parent)
145 return;
146 std::vector<Gtk::Widget *> children = parent->get_children();
147 for (unsigned int i=0; i<children.size() ; i++)
148 {
149 Gtk::Widget *child = children[i];
150 GtkWidget *wid = child->gobj();
151 if (GTK_IS_EXPANDER(wid))
152 result.push_back((Gtk::Expander *)child);
153 else if (GTK_IS_CONTAINER(wid))
154 findExpanderWidgets((Gtk::Container *)child, result);
155 }
157 }
160 /*#########################################################################
161 ### SVG Preview Widget
162 #########################################################################*/
164 /**
165 * Simple class for displaying an SVG file in the "preview widget."
166 * Currently, this is just a wrapper of the sp_svg_view Gtk widget.
167 * Hopefully we will eventually replace with a pure Gtkmm widget.
168 */
169 class SVGPreview : public Gtk::VBox
170 {
171 public:
173 SVGPreview();
175 ~SVGPreview();
177 bool setDocument(SPDocument *doc);
179 bool setFileName(Glib::ustring &fileName);
181 bool setFromMem(char const *xmlBuffer);
183 bool set(Glib::ustring &fileName, int dialogType);
185 bool setURI(URI &uri);
187 /**
188 * Show image embedded in SVG
189 */
190 void showImage(Glib::ustring &fileName);
192 /**
193 * Show the "No preview" image
194 */
195 void showNoPreview();
197 /**
198 * Show the "Too large" image
199 */
200 void showTooLarge(long fileLength);
202 private:
203 /**
204 * The svg document we are currently showing
205 */
206 SPDocument *document;
208 /**
209 * The sp_svg_view widget
210 */
211 GtkWidget *viewerGtk;
213 /**
214 * are we currently showing the "no preview" image?
215 */
216 bool showingNoPreview;
218 };
221 bool SVGPreview::setDocument(SPDocument *doc)
222 {
223 if (document)
224 sp_document_unref(document);
226 sp_document_ref(doc);
227 document = doc;
229 //This should remove it from the box, and free resources
230 if (viewerGtk)
231 gtk_widget_destroy(viewerGtk);
233 viewerGtk = sp_svg_view_widget_new(doc);
234 GtkWidget *vbox = (GtkWidget *)gobj();
235 gtk_box_pack_start(GTK_BOX(vbox), viewerGtk, TRUE, TRUE, 0);
236 gtk_widget_show(viewerGtk);
238 return true;
239 }
241 bool SVGPreview::setFileName(Glib::ustring &theFileName)
242 {
243 Glib::ustring fileName = theFileName;
245 fileName = Glib::filename_to_utf8(fileName);
247 SPDocument *doc = sp_document_new (fileName.c_str(), 0);
248 if (!doc) {
249 g_warning("SVGView: error loading document '%s'\n", fileName.c_str());
250 return false;
251 }
253 setDocument(doc);
255 sp_document_unref(doc);
257 return true;
258 }
262 bool SVGPreview::setFromMem(char const *xmlBuffer)
263 {
264 if (!xmlBuffer)
265 return false;
267 gint len = (gint)strlen(xmlBuffer);
268 SPDocument *doc = sp_document_new_from_mem(xmlBuffer, len, 0);
269 if (!doc) {
270 g_warning("SVGView: error loading buffer '%s'\n",xmlBuffer);
271 return false;
272 }
274 setDocument(doc);
276 sp_document_unref(doc);
278 Inkscape::GC::request_early_collection();
280 return true;
281 }
285 void SVGPreview::showImage(Glib::ustring &theFileName)
286 {
287 Glib::ustring fileName = theFileName;
290 /*#####################################
291 # LET'S HAVE SOME FUN WITH SVG!
292 # Instead of just loading an image, why
293 # don't we make a lovely little svg and
294 # display it nicely?
295 #####################################*/
297 //Arbitrary size of svg doc -- rather 'portrait' shaped
298 gint previewWidth = 400;
299 gint previewHeight = 600;
301 //Get some image info. Smart pointer does not need to be deleted
302 Glib::RefPtr<Gdk::Pixbuf> img = Gdk::Pixbuf::create_from_file(fileName);
303 gint imgWidth = img->get_width();
304 gint imgHeight = img->get_height();
306 //Find the minimum scale to fit the image inside the preview area
307 double scaleFactorX = (0.9 *(double)previewWidth) / ((double)imgWidth);
308 double scaleFactorY = (0.9 *(double)previewHeight) / ((double)imgHeight);
309 double scaleFactor = scaleFactorX;
310 if (scaleFactorX > scaleFactorY)
311 scaleFactor = scaleFactorY;
313 //Now get the resized values
314 gint scaledImgWidth = (int) (scaleFactor * (double)imgWidth);
315 gint scaledImgHeight = (int) (scaleFactor * (double)imgHeight);
317 //center the image on the area
318 gint imgX = (previewWidth - scaledImgWidth) / 2;
319 gint imgY = (previewHeight - scaledImgHeight) / 2;
321 //wrap a rectangle around the image
322 gint rectX = imgX-1;
323 gint rectY = imgY-1;
324 gint rectWidth = scaledImgWidth +2;
325 gint rectHeight = scaledImgHeight+2;
327 //Our template. Modify to taste
328 gchar const *xformat =
329 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
330 "<svg\n"
331 "xmlns=\"http://www.w3.org/2000/svg\"\n"
332 "xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
333 "width=\"%d\" height=\"%d\">\n"
334 "<rect\n"
335 " style=\"fill:#eeeeee;stroke:none\"\n"
336 " x=\"-100\" y=\"-100\" width=\"4000\" height=\"4000\"/>\n"
337 "<image x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\"\n"
338 "xlink:href=\"%s\"/>\n"
339 "<rect\n"
340 " style=\"fill:none;"
341 " stroke:#000000;stroke-width:1.0;"
342 " stroke-linejoin:miter;stroke-opacity:1.0000000;"
343 " stroke-miterlimit:4.0000000;stroke-dasharray:none\"\n"
344 " x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\"/>\n"
345 "<text\n"
346 " style=\"font-size:24.000000;font-style:normal;font-weight:normal;"
347 " fill:#000000;fill-opacity:1.0000000;stroke:none;"
348 " font-family:Bitstream Vera Sans\"\n"
349 " x=\"10\" y=\"26\">%d x %d</text>\n"
350 "</svg>\n\n";
352 //if (!Glib::get_charset()) //If we are not utf8
353 fileName = Glib::filename_to_utf8(fileName);
355 //Fill in the template
356 /* FIXME: Do proper XML quoting for fileName. */
357 gchar *xmlBuffer = g_strdup_printf(xformat,
358 previewWidth, previewHeight,
359 imgX, imgY, scaledImgWidth, scaledImgHeight,
360 fileName.c_str(),
361 rectX, rectY, rectWidth, rectHeight,
362 imgWidth, imgHeight);
364 //g_message("%s\n", xmlBuffer);
366 //now show it!
367 setFromMem(xmlBuffer);
368 g_free(xmlBuffer);
369 }
373 void SVGPreview::showNoPreview()
374 {
375 //Are we already showing it?
376 if (showingNoPreview)
377 return;
379 //Arbitrary size of svg doc -- rather 'portrait' shaped
380 gint previewWidth = 300;
381 gint previewHeight = 600;
383 //Our template. Modify to taste
384 gchar const *xformat =
385 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
386 "<svg\n"
387 "xmlns=\"http://www.w3.org/2000/svg\"\n"
388 "xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
389 "width=\"%d\" height=\"%d\">\n"
390 "<g transform=\"translate(-190,24.27184)\" style=\"opacity:0.12\">\n"
391 "<path\n"
392 "style=\"font-size:12;fill:#ffffff;fill-rule:evenodd;stroke:#000000;stroke-width:0.936193pt\"\n"
393 "d=\"M 397.64309 320.25301 L 280.39197 282.517 L 250.74227 124.83447 L 345.08225 "
394 "29.146783 L 393.59996 46.667064 L 483.89679 135.61619 L 397.64309 320.25301 z \"\n"
395 "id=\"whiteSpace\" />\n"
396 "<path\n"
397 "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
398 "d=\"M 476.95792 339.17168 C 495.78197 342.93607 499.54842 356.11361 495.78197 359.87802 "
399 "C 492.01856 363.6434 482.6065 367.40781 475.07663 361.76014 C 467.54478 "
400 "356.11361 467.54478 342.93607 476.95792 339.17168 z \"\n"
401 "id=\"droplet01\" />\n"
402 "<path\n"
403 "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
404 "d=\"M 286.46194 340.42914 C 284.6277 340.91835 269.30405 327.71337 257.16909 333.8338 "
405 "C 245.03722 339.95336 236.89276 353.65666 248.22676 359.27982 C 259.56184 364.90298 "
406 "267.66433 358.41867 277.60113 351.44119 C 287.53903 344.46477 "
407 "287.18046 343.1206 286.46194 340.42914 z \"\n"
408 "id=\"droplet02\" />\n"
409 "<path\n"
410 "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
411 "d=\"M 510.35756 306.92856 C 520.59494 304.36879 544.24333 306.92856 540.47688 321.98634 "
412 "C 536.71354 337.04806 504.71297 331.39827 484.00371 323.87156 C 482.12141 "
413 "308.81083 505.53237 308.13423 510.35756 306.92856 z \"\n"
414 "id=\"droplet03\" />\n"
415 "<path\n"
416 "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
417 "d=\"M 359.2403 21.362537 C 347.92693 21.362537 336.6347 25.683095 327.96556 34.35223 "
418 "L 173.87387 188.41466 C 165.37697 196.9114 161.1116 207.95813 160.94269 219.04577 "
419 "L 160.88418 219.04577 C 160.88418 219.08524 160.94076 219.12322 160.94269 219.16279 "
420 "C 160.94033 219.34888 160.88418 219.53256 160.88418 219.71865 L 161.14748 219.71865 "
421 "C 164.0966 230.93917 240.29699 245.24198 248.79866 253.74346 C 261.63771 266.58263 "
422 "199.5652 276.01151 212.4041 288.85074 C 225.24316 301.68979 289.99433 313.6933 302.8346 "
423 "326.53254 C 315.67368 339.37161 276.5961 353.04289 289.43532 365.88196 C 302.27439 "
424 "378.72118 345.40201 362.67257 337.5908 396.16198 C 354.92909 413.50026 391.10302 "
425 "405.2208 415.32417 387.88252 C 428.16323 375.04345 390.6948 376.17577 403.53397 "
426 "363.33668 C 416.37304 350.49745 448.78128 350.4282 476.08902 319.71589 C 465.09739 "
427 "302.62116 429.10801 295.34136 441.94719 282.50217 C 454.78625 269.66311 479.74708 "
428 "276.18423 533.60644 251.72479 C 559.89837 239.78398 557.72636 230.71459 557.62567 "
429 "219.71865 C 557.62356 219.48727 557.62567 219.27892 557.62567 219.04577 L 557.56716 "
430 "219.04577 C 557.3983 207.95812 553.10345 196.9114 544.60673 188.41466 L 390.54428 "
431 "34.35223 C 381.87515 25.683095 370.55366 21.362537 359.2403 21.362537 z M 357.92378 "
432 "41.402939 C 362.95327 41.533963 367.01541 45.368018 374.98006 50.530832 L 447.76915 "
433 "104.50827 C 448.56596 105.02498 449.32484 105.564 450.02187 106.11735 C 450.7189 106.67062 "
434 "451.3556 107.25745 451.95277 107.84347 C 452.54997 108.42842 453.09281 109.01553 453.59111 "
435 "109.62808 C 454.08837 110.24052 454.53956 110.86661 454.93688 111.50048 C 455.33532 112.13538 "
436 "455.69164 112.78029 455.9901 113.43137 C 456.28877 114.08363 456.52291 114.75639 456.7215 "
437 "115.42078 C 456.92126 116.08419 457.08982 116.73973 457.18961 117.41019 C 457.28949 "
438 "118.08184 457.33588 118.75535 457.33588 119.42886 L 414.21245 98.598549 L 409.9118 "
439 "131.16055 L 386.18512 120.04324 L 349.55654 144.50131 L 335.54288 96.1703 L 317.4919 "
440 "138.4453 L 267.08369 143.47735 L 267.63956 121.03795 C 267.63956 115.64823 296.69685 "
441 "77.915899 314.39075 68.932902 L 346.77721 45.674327 C 351.55594 42.576634 354.90608 "
442 "41.324327 357.92378 41.402939 z M 290.92738 261.61333 C 313.87149 267.56365 339.40299 "
443 "275.37038 359.88393 275.50997 L 360.76161 284.72563 C 343.2235 282.91785 306.11346 "
444 "274.45012 297.36372 269.98057 L 290.92738 261.61333 z \"\n"
445 "id=\"mountainDroplet\" />\n"
446 "</g> <g transform=\"translate(-20,0)\">\n"
447 "<text xml:space=\"preserve\"\n"
448 "style=\"font-size:32.000000;font-style:normal;font-variant:normal;font-weight:bold;"
449 "font-stretch:normal;fill:#000000;fill-opacity:1.0000000;stroke:none;stroke-width:1.0000000pt;"
450 "stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1.0000000;"
451 "font-family:Bitstream Vera Sans;text-anchor:middle;writing-mode:lr\"\n"
452 "x=\"190\" y=\"240\">%s</text></g>\n"
453 "</svg>\n\n";
455 //Fill in the template
456 gchar *xmlBuffer = g_strdup_printf(xformat,
457 previewWidth, previewHeight, _("No preview"));
459 //g_message("%s\n", xmlBuffer);
461 //now show it!
462 setFromMem(xmlBuffer);
463 g_free(xmlBuffer);
464 showingNoPreview = true;
466 }
468 void SVGPreview::showTooLarge(long fileLength)
469 {
471 //Arbitrary size of svg doc -- rather 'portrait' shaped
472 gint previewWidth = 300;
473 gint previewHeight = 600;
475 //Our template. Modify to taste
476 gchar const *xformat =
477 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
478 "<svg\n"
479 "xmlns=\"http://www.w3.org/2000/svg\"\n"
480 "xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
481 "width=\"%d\" height=\"%d\">\n"
482 "<g transform=\"translate(-170,24.27184)\" style=\"opacity:0.12\">\n"
483 "<path\n"
484 "style=\"font-size:12;fill:#ffffff;fill-rule:evenodd;stroke:#000000;stroke-width:0.936193pt\"\n"
485 "d=\"M 397.64309 320.25301 L 280.39197 282.517 L 250.74227 124.83447 L 345.08225 "
486 "29.146783 L 393.59996 46.667064 L 483.89679 135.61619 L 397.64309 320.25301 z \"\n"
487 "id=\"whiteSpace\" />\n"
488 "<path\n"
489 "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
490 "d=\"M 476.95792 339.17168 C 495.78197 342.93607 499.54842 356.11361 495.78197 359.87802 "
491 "C 492.01856 363.6434 482.6065 367.40781 475.07663 361.76014 C 467.54478 "
492 "356.11361 467.54478 342.93607 476.95792 339.17168 z \"\n"
493 "id=\"droplet01\" />\n"
494 "<path\n"
495 "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
496 "d=\"M 286.46194 340.42914 C 284.6277 340.91835 269.30405 327.71337 257.16909 333.8338 "
497 "C 245.03722 339.95336 236.89276 353.65666 248.22676 359.27982 C 259.56184 364.90298 "
498 "267.66433 358.41867 277.60113 351.44119 C 287.53903 344.46477 "
499 "287.18046 343.1206 286.46194 340.42914 z \"\n"
500 "id=\"droplet02\" />\n"
501 "<path\n"
502 "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
503 "d=\"M 510.35756 306.92856 C 520.59494 304.36879 544.24333 306.92856 540.47688 321.98634 "
504 "C 536.71354 337.04806 504.71297 331.39827 484.00371 323.87156 C 482.12141 "
505 "308.81083 505.53237 308.13423 510.35756 306.92856 z \"\n"
506 "id=\"droplet03\" />\n"
507 "<path\n"
508 "style=\"font-size:12;fill-rule:evenodd;stroke-width:1pt;fill:#000000;fill-opacity:1\"\n"
509 "d=\"M 359.2403 21.362537 C 347.92693 21.362537 336.6347 25.683095 327.96556 34.35223 "
510 "L 173.87387 188.41466 C 165.37697 196.9114 161.1116 207.95813 160.94269 219.04577 "
511 "L 160.88418 219.04577 C 160.88418 219.08524 160.94076 219.12322 160.94269 219.16279 "
512 "C 160.94033 219.34888 160.88418 219.53256 160.88418 219.71865 L 161.14748 219.71865 "
513 "C 164.0966 230.93917 240.29699 245.24198 248.79866 253.74346 C 261.63771 266.58263 "
514 "199.5652 276.01151 212.4041 288.85074 C 225.24316 301.68979 289.99433 313.6933 302.8346 "
515 "326.53254 C 315.67368 339.37161 276.5961 353.04289 289.43532 365.88196 C 302.27439 "
516 "378.72118 345.40201 362.67257 337.5908 396.16198 C 354.92909 413.50026 391.10302 "
517 "405.2208 415.32417 387.88252 C 428.16323 375.04345 390.6948 376.17577 403.53397 "
518 "363.33668 C 416.37304 350.49745 448.78128 350.4282 476.08902 319.71589 C 465.09739 "
519 "302.62116 429.10801 295.34136 441.94719 282.50217 C 454.78625 269.66311 479.74708 "
520 "276.18423 533.60644 251.72479 C 559.89837 239.78398 557.72636 230.71459 557.62567 "
521 "219.71865 C 557.62356 219.48727 557.62567 219.27892 557.62567 219.04577 L 557.56716 "
522 "219.04577 C 557.3983 207.95812 553.10345 196.9114 544.60673 188.41466 L 390.54428 "
523 "34.35223 C 381.87515 25.683095 370.55366 21.362537 359.2403 21.362537 z M 357.92378 "
524 "41.402939 C 362.95327 41.533963 367.01541 45.368018 374.98006 50.530832 L 447.76915 "
525 "104.50827 C 448.56596 105.02498 449.32484 105.564 450.02187 106.11735 C 450.7189 106.67062 "
526 "451.3556 107.25745 451.95277 107.84347 C 452.54997 108.42842 453.09281 109.01553 453.59111 "
527 "109.62808 C 454.08837 110.24052 454.53956 110.86661 454.93688 111.50048 C 455.33532 112.13538 "
528 "455.69164 112.78029 455.9901 113.43137 C 456.28877 114.08363 456.52291 114.75639 456.7215 "
529 "115.42078 C 456.92126 116.08419 457.08982 116.73973 457.18961 117.41019 C 457.28949 "
530 "118.08184 457.33588 118.75535 457.33588 119.42886 L 414.21245 98.598549 L 409.9118 "
531 "131.16055 L 386.18512 120.04324 L 349.55654 144.50131 L 335.54288 96.1703 L 317.4919 "
532 "138.4453 L 267.08369 143.47735 L 267.63956 121.03795 C 267.63956 115.64823 296.69685 "
533 "77.915899 314.39075 68.932902 L 346.77721 45.674327 C 351.55594 42.576634 354.90608 "
534 "41.324327 357.92378 41.402939 z M 290.92738 261.61333 C 313.87149 267.56365 339.40299 "
535 "275.37038 359.88393 275.50997 L 360.76161 284.72563 C 343.2235 282.91785 306.11346 "
536 "274.45012 297.36372 269.98057 L 290.92738 261.61333 z \"\n"
537 "id=\"mountainDroplet\" />\n"
538 "</g>\n"
539 "<text xml:space=\"preserve\"\n"
540 "style=\"font-size:32.000000;font-style:normal;font-variant:normal;font-weight:bold;"
541 "font-stretch:normal;fill:#000000;fill-opacity:1.0000000;stroke:none;stroke-width:1.0000000pt;"
542 "stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1.0000000;"
543 "font-family:Bitstream Vera Sans;text-anchor:middle;writing-mode:lr\"\n"
544 "x=\"170\" y=\"215\">%5.1f MB</text>\n"
545 "<text xml:space=\"preserve\"\n"
546 "style=\"font-size:24.000000;font-style:normal;font-variant:normal;font-weight:bold;"
547 "font-stretch:normal;fill:#000000;fill-opacity:1.0000000;stroke:none;stroke-width:1.0000000pt;"
548 "stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1.0000000;"
549 "font-family:Bitstream Vera Sans;text-anchor:middle;writing-mode:lr\"\n"
550 "x=\"180\" y=\"245\">%s</text>\n"
551 "</svg>\n\n";
553 //Fill in the template
554 double floatFileLength = ((double)fileLength) / 1048576.0;
555 //printf("%ld %f\n", fileLength, floatFileLength);
556 gchar *xmlBuffer = g_strdup_printf(xformat,
557 previewWidth, previewHeight, floatFileLength,
558 _("too large for preview"));
560 //g_message("%s\n", xmlBuffer);
562 //now show it!
563 setFromMem(xmlBuffer);
564 g_free(xmlBuffer);
566 }
569 /**
570 * Return true if the string ends with the given suffix
571 */
572 static bool
573 hasSuffix(Glib::ustring &str, Glib::ustring &ext)
574 {
575 int strLen = str.length();
576 int extLen = ext.length();
577 if (extLen > strLen)
578 return false;
579 int strpos = strLen-1;
580 for (int extpos = extLen-1 ; extpos>=0 ; extpos--, strpos--)
581 {
582 Glib::ustring::value_type ch = str[strpos];
583 if (ch != ext[extpos])
584 {
585 if ( ((ch & 0xff80) != 0) ||
586 static_cast<Glib::ustring::value_type>( g_ascii_tolower( static_cast<gchar>(0x07f & ch) ) ) != ext[extpos] )
587 {
588 return false;
589 }
590 }
591 }
592 return true;
593 }
596 /**
597 * Return true if the image is loadable by Gdk, else false
598 */
599 static bool
600 isValidImageFile(Glib::ustring &fileName)
601 {
602 std::vector<Gdk::PixbufFormat>formats = Gdk::Pixbuf::get_formats();
603 for (unsigned int i=0; i<formats.size(); i++)
604 {
605 Gdk::PixbufFormat format = formats[i];
606 std::vector<Glib::ustring>extensions = format.get_extensions();
607 for (unsigned int j=0; j<extensions.size(); j++)
608 {
609 Glib::ustring ext = extensions[j];
610 if (hasSuffix(fileName, ext))
611 return true;
612 }
613 }
614 return false;
615 }
617 bool SVGPreview::set(Glib::ustring &fileName, int dialogType)
618 {
620 if (!Glib::file_test(fileName, Glib::FILE_TEST_EXISTS))
621 return false;
623 gchar *fName = (gchar *)fileName.c_str();
624 //g_message("fname:%s\n", fName);
627 if (Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR))
628 {
629 struct stat info;
630 if (stat(fName, &info))
631 {
632 return FALSE;
633 }
634 long fileLen = info.st_size;
635 if (fileLen > 0x150000L)
636 {
637 showingNoPreview = false;
638 showTooLarge(fileLen);
639 return FALSE;
640 }
641 }
643 Glib::ustring svg = ".svg";
644 Glib::ustring svgz = ".svgz";
646 if ((dialogType == SVG_TYPES || dialogType == IMPORT_TYPES) &&
647 (hasSuffix(fileName, svg) || hasSuffix(fileName, svgz) )
648 )
649 {
650 bool retval = setFileName(fileName);
651 showingNoPreview = false;
652 return retval;
653 }
654 else if (isValidImageFile(fileName))
655 {
656 showImage(fileName);
657 showingNoPreview = false;
658 return true;
659 }
660 else
661 {
662 showNoPreview();
663 return false;
664 }
665 }
668 SVGPreview::SVGPreview()
669 {
670 if (!INKSCAPE)
671 inkscape_application_init("",false);
672 document = NULL;
673 viewerGtk = NULL;
674 set_size_request(150,150);
675 showingNoPreview = false;
676 }
678 SVGPreview::~SVGPreview()
679 {
681 }
687 /*#########################################################################
688 ### F I L E D I A L O G B A S E C L A S S
689 #########################################################################*/
691 /**
692 * This class is the base implementation for the others. This
693 * reduces redundancies and bugs.
694 */
695 class FileDialogBase : public Gtk::FileChooserDialog
696 {
697 public:
699 /**
700 *
701 */
702 FileDialogBase(const Glib::ustring &title) :
703 Gtk::FileChooserDialog(title)
704 {
705 }
707 /**
708 *
709 */
710 FileDialogBase(const Glib::ustring &title,
711 Gtk::FileChooserAction dialogType) :
712 Gtk::FileChooserDialog(title, dialogType)
713 {
714 }
716 /**
717 *
718 */
719 virtual ~FileDialogBase()
720 {}
722 };
726 /*#########################################################################
727 ### F I L E O P E N
728 #########################################################################*/
730 /**
731 * Our implementation class for the FileOpenDialog interface..
732 */
733 class FileOpenDialogImpl : public FileOpenDialog, public FileDialogBase
734 {
735 public:
737 FileOpenDialogImpl(const Glib::ustring &dir,
738 FileDialogType fileTypes,
739 const Glib::ustring &title);
741 virtual ~FileOpenDialogImpl();
743 bool show();
745 Inkscape::Extension::Extension *getSelectionType();
747 Glib::ustring getFilename();
749 std::vector<Glib::ustring> getFilenames ();
751 protected:
755 private:
758 /**
759 * What type of 'open' are we? (open, import, place, etc)
760 */
761 FileDialogType dialogType;
763 /**
764 * Our svg preview widget
765 */
766 SVGPreview svgPreview;
768 /**
769 * Callback for seeing if the preview needs to be drawn
770 */
771 void updatePreviewCallback();
773 /**
774 * Fix to allow the user to type the file name
775 */
776 Gtk::Entry fileNameEntry;
778 /**
779 * Create a filter menu for this type of dialog
780 */
781 void createFilterMenu();
783 /**
784 * Callback for user input into fileNameEntry
785 */
786 void fileNameEntryChangedCallback();
788 /**
789 * Callback for user changing which item is selected on the list
790 */
791 void fileSelectedCallback();
794 /**
795 * Filter name->extension lookup
796 */
797 std::map<Glib::ustring, Inkscape::Extension::Extension *> extensionMap;
799 /**
800 * The extension to use to write this file
801 */
802 Inkscape::Extension::Extension *extension;
804 /**
805 * Filename that was given
806 */
807 Glib::ustring myFilename;
809 };
815 /**
816 * Callback for checking if the preview needs to be redrawn
817 */
818 void FileOpenDialogImpl::updatePreviewCallback()
819 {
820 Glib::ustring fileName = get_preview_filename();
821 if (fileName.length() < 1)
822 return;
823 svgPreview.set(fileName, dialogType);
824 }
830 /**
831 * Callback for fileNameEntry widget
832 */
833 void FileOpenDialogImpl::fileNameEntryChangedCallback()
834 {
835 Glib::ustring rawFileName = fileNameEntry.get_text();
837 Glib::ustring fileName = Glib::filename_from_utf8(rawFileName);
839 //g_message("User hit return. Text is '%s'\n", fileName.c_str());
841 if (!Glib::path_is_absolute(fileName)) {
842 //try appending to the current path
843 // not this way: fileName = get_current_folder() + "/" + fName;
844 std::vector<Glib::ustring> pathSegments;
845 pathSegments.push_back( get_current_folder() );
846 pathSegments.push_back( fileName );
847 fileName = Glib::build_filename(pathSegments);
848 }
850 //g_message("path:'%s'\n", fName.c_str());
852 if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
853 set_current_folder(fileName);
854 } else if (Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR)) {
855 //dialog with either (1) select a regular file or (2) cd to dir
856 //simulate an 'OK'
857 set_filename(fileName);
858 response(Gtk::RESPONSE_OK);
859 }
860 }
866 /**
867 * Callback for fileNameEntry widget
868 */
869 void FileOpenDialogImpl::fileSelectedCallback()
870 {
871 Glib::ustring fileName = get_filename();
872 if (!Glib::get_charset()) //If we are not utf8
873 fileName = Glib::filename_to_utf8(fileName);
874 //g_message("User selected '%s'\n",
875 // filename().c_str());
877 #ifdef INK_DUMP_FILENAME_CONV
878 ::dump_ustr( get_filename() );
879 #endif
880 fileNameEntry.set_text(fileName);
881 }
886 void FileOpenDialogImpl::createFilterMenu()
887 {
888 //patterns added dynamically below
889 Gtk::FileFilter allImageFilter;
890 allImageFilter.set_name(_("All Images"));
891 extensionMap[Glib::ustring(_("All Images"))]=NULL;
892 add_filter(allImageFilter);
894 Gtk::FileFilter allFilter;
895 allFilter.set_name(_("All Files"));
896 extensionMap[Glib::ustring(_("All Files"))]=NULL;
897 allFilter.add_pattern("*");
898 add_filter(allFilter);
900 //patterns added dynamically below
901 Gtk::FileFilter allInkscapeFilter;
902 allInkscapeFilter.set_name(_("All Inkscape Files"));
903 extensionMap[Glib::ustring(_("All Inkscape Files"))]=NULL;
904 add_filter(allInkscapeFilter);
906 Inkscape::Extension::DB::InputList extension_list;
907 Inkscape::Extension::db.get_input_list(extension_list);
909 for (Inkscape::Extension::DB::InputList::iterator current_item = extension_list.begin();
910 current_item != extension_list.end(); current_item++)
911 {
912 Inkscape::Extension::Input * imod = *current_item;
914 // FIXME: would be nice to grey them out instead of not listing them
915 if (imod->deactivated()) continue;
917 Glib::ustring upattern("*");
918 Glib::ustring extension = imod->get_extension();
919 fileDialogExtensionToPattern(upattern, extension);
921 Gtk::FileFilter filter;
922 Glib::ustring uname(_(imod->get_filetypename()));
923 filter.set_name(uname);
924 filter.add_pattern(upattern);
925 add_filter(filter);
926 extensionMap[uname] = imod;
928 //g_message("ext %s:%s '%s'\n", ioext->name, ioext->mimetype, upattern.c_str());
929 allInkscapeFilter.add_pattern(upattern);
930 if ( strncmp("image", imod->get_mimetype(), 5)==0 )
931 allImageFilter.add_pattern(upattern);
932 }
934 return;
935 }
939 /**
940 * Constructor. Not called directly. Use the factory.
941 */
942 FileOpenDialogImpl::FileOpenDialogImpl(const Glib::ustring &dir,
943 FileDialogType fileTypes,
944 const Glib::ustring &title) :
945 FileDialogBase(title)
946 {
949 /* One file at a time */
950 /* And also Multiple Files */
951 set_select_multiple(true);
953 /* Initalize to Autodetect */
954 extension = NULL;
955 /* No filename to start out with */
956 myFilename = "";
958 /* Set our dialog type (open, import, etc...)*/
959 dialogType = fileTypes;
962 /* Set the pwd and/or the filename */
963 if (dir.size() > 0)
964 {
965 Glib::ustring udir(dir);
966 Glib::ustring::size_type len = udir.length();
967 // leaving a trailing backslash on the directory name leads to the infamous
968 // double-directory bug on win32
969 if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
970 set_current_folder(udir.c_str());
971 }
973 //###### Add the file types menu
974 createFilterMenu();
976 //###### Add a preview widget
977 set_preview_widget(svgPreview);
978 set_preview_widget_active(true);
979 set_use_preview_label (false);
981 //Catch selection-changed events, so we can adjust the text widget
982 signal_update_preview().connect(
983 sigc::mem_fun(*this, &FileOpenDialogImpl::updatePreviewCallback) );
986 //###### Add a text entry bar, and tie it to file chooser events
987 fileNameEntry.set_text(get_current_folder());
988 set_extra_widget(fileNameEntry);
989 fileNameEntry.grab_focus();
991 //Catch when user hits [return] on the text field
992 fileNameEntry.signal_activate().connect(
993 sigc::mem_fun(*this, &FileOpenDialogImpl::fileNameEntryChangedCallback) );
995 //Catch selection-changed events, so we can adjust the text widget
996 signal_selection_changed().connect(
997 sigc::mem_fun(*this, &FileOpenDialogImpl::fileSelectedCallback) );
999 add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1000 add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
1002 }
1008 /**
1009 * Public factory. Called by file.cpp, among others.
1010 */
1011 FileOpenDialog *FileOpenDialog::create(const Glib::ustring &path,
1012 FileDialogType fileTypes,
1013 const Glib::ustring &title)
1014 {
1015 FileOpenDialog *dialog = new FileOpenDialogImpl(path, fileTypes, title);
1016 return dialog;
1017 }
1022 /**
1023 * Destructor
1024 */
1025 FileOpenDialogImpl::~FileOpenDialogImpl()
1026 {
1028 }
1031 /**
1032 * Show this dialog modally. Return true if user hits [OK]
1033 */
1034 bool
1035 FileOpenDialogImpl::show()
1036 {
1037 Glib::ustring s = Glib::filename_to_utf8 (get_current_folder());
1038 if (s.length() == 0)
1039 s = getcwd (NULL, 0);
1040 set_current_folder(Glib::filename_from_utf8(s)); //hack to force initial dir listing
1041 set_modal (TRUE); //Window
1042 sp_transientize((GtkWidget *)gobj()); //Make transient
1043 gint b = run(); //Dialog
1044 svgPreview.showNoPreview();
1045 hide();
1047 if (b == Gtk::RESPONSE_OK)
1048 {
1049 //This is a hack, to avoid the warning messages that
1050 //Gtk::FileChooser::get_filter() returns
1051 //should be: Gtk::FileFilter *filter = get_filter();
1052 GtkFileChooser *gtkFileChooser = Gtk::FileChooser::gobj();
1053 GtkFileFilter *filter = gtk_file_chooser_get_filter(gtkFileChooser);
1054 if (filter)
1055 {
1056 //Get which extension was chosen, if any
1057 extension = extensionMap[gtk_file_filter_get_name(filter)];
1058 }
1059 myFilename = get_filename();
1060 return TRUE;
1061 }
1062 else
1063 {
1064 return FALSE;
1065 }
1066 }
1071 /**
1072 * Get the file extension type that was selected by the user. Valid after an [OK]
1073 */
1074 Inkscape::Extension::Extension *
1075 FileOpenDialogImpl::getSelectionType()
1076 {
1077 return extension;
1078 }
1081 /**
1082 * Get the file name chosen by the user. Valid after an [OK]
1083 */
1084 Glib::ustring
1085 FileOpenDialogImpl::getFilename (void)
1086 {
1087 return g_strdup(myFilename.c_str());
1088 }
1091 /**
1092 * To Get Multiple filenames selected at-once.
1093 */
1094 std::vector<Glib::ustring>FileOpenDialogImpl::getFilenames()
1095 {
1096 std::vector<Glib::ustring> result = get_filenames();
1097 return result;
1098 }
1105 //########################################################################
1106 //# F I L E S A V E
1107 //########################################################################
1109 class FileType
1110 {
1111 public:
1112 FileType() {}
1113 ~FileType() {}
1114 Glib::ustring name;
1115 Glib::ustring pattern;
1116 Inkscape::Extension::Extension *extension;
1117 };
1119 /**
1120 * Our implementation of the FileSaveDialog interface.
1121 */
1122 class FileSaveDialogImpl : public FileSaveDialog, public FileDialogBase
1123 {
1125 public:
1126 FileSaveDialogImpl(const Glib::ustring &dir,
1127 FileDialogType fileTypes,
1128 const Glib::ustring &title,
1129 const Glib::ustring &default_key);
1131 virtual ~FileSaveDialogImpl();
1133 bool show();
1135 Inkscape::Extension::Extension *getSelectionType();
1137 Glib::ustring getFilename();
1140 private:
1142 /**
1143 * What type of 'open' are we? (save, export, etc)
1144 */
1145 FileDialogType dialogType;
1147 /**
1148 * Our svg preview widget
1149 */
1150 SVGPreview svgPreview;
1152 /**
1153 * Fix to allow the user to type the file name
1154 */
1155 Gtk::Entry *fileNameEntry;
1157 /**
1158 * Callback for seeing if the preview needs to be drawn
1159 */
1160 void updatePreviewCallback();
1164 /**
1165 * Allow the specification of the output file type
1166 */
1167 Gtk::HBox fileTypeBox;
1169 /**
1170 * Allow the specification of the output file type
1171 */
1172 Gtk::ComboBoxText fileTypeComboBox;
1175 /**
1176 * Data mirror of the combo box
1177 */
1178 std::vector<FileType> fileTypes;
1180 //# Child widgets
1181 Gtk::CheckButton fileTypeCheckbox;
1184 /**
1185 * Callback for user input into fileNameEntry
1186 */
1187 void fileTypeChangedCallback();
1189 /**
1190 * Create a filter menu for this type of dialog
1191 */
1192 void createFileTypeMenu();
1195 bool append_extension;
1197 /**
1198 * The extension to use to write this file
1199 */
1200 Inkscape::Extension::Extension *extension;
1202 /**
1203 * Callback for user input into fileNameEntry
1204 */
1205 void fileNameEntryChangedCallback();
1207 /**
1208 * Filename that was given
1209 */
1210 Glib::ustring myFilename;
1211 };
1218 /**
1219 * Callback for checking if the preview needs to be redrawn
1220 */
1221 void FileSaveDialogImpl::updatePreviewCallback()
1222 {
1223 Glib::ustring fileName = get_preview_filename();
1224 if (!fileName.c_str())
1225 return;
1226 bool retval = svgPreview.set(fileName, dialogType);
1227 set_preview_widget_active(retval);
1228 }
1232 /**
1233 * Callback for fileNameEntry widget
1234 */
1235 void FileSaveDialogImpl::fileNameEntryChangedCallback()
1236 {
1237 if (!fileNameEntry)
1238 return;
1240 Glib::ustring fileName = fileNameEntry->get_text();
1241 if (!Glib::get_charset()) //If we are not utf8
1242 fileName = Glib::filename_to_utf8(fileName);
1244 //g_message("User hit return. Text is '%s'\n", fileName.c_str());
1246 if (!Glib::path_is_absolute(fileName)) {
1247 //try appending to the current path
1248 // not this way: fileName = get_current_folder() + "/" + fileName;
1249 std::vector<Glib::ustring> pathSegments;
1250 pathSegments.push_back( get_current_folder() );
1251 pathSegments.push_back( fileName );
1252 fileName = Glib::build_filename(pathSegments);
1253 }
1255 //g_message("path:'%s'\n", fileName.c_str());
1257 if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
1258 set_current_folder(fileName);
1259 } else if (/*Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR)*/1) {
1260 //dialog with either (1) select a regular file or (2) cd to dir
1261 //simulate an 'OK'
1262 set_filename(fileName);
1263 response(Gtk::RESPONSE_OK);
1264 }
1265 }
1269 /**
1270 * Callback for fileNameEntry widget
1271 */
1272 void FileSaveDialogImpl::fileTypeChangedCallback()
1273 {
1274 int sel = fileTypeComboBox.get_active_row_number();
1275 if (sel<0 || sel >= (int)fileTypes.size())
1276 return;
1277 FileType type = fileTypes[sel];
1278 //g_message("selected: %s\n", type.name.c_str());
1279 Gtk::FileFilter filter;
1280 filter.add_pattern(type.pattern);
1281 set_filter(filter);
1282 }
1286 void FileSaveDialogImpl::createFileTypeMenu()
1287 {
1288 Inkscape::Extension::DB::OutputList extension_list;
1289 Inkscape::Extension::db.get_output_list(extension_list);
1291 for (Inkscape::Extension::DB::OutputList::iterator current_item = extension_list.begin();
1292 current_item != extension_list.end(); current_item++)
1293 {
1294 Inkscape::Extension::Output * omod = *current_item;
1296 // FIXME: would be nice to grey them out instead of not listing them
1297 if (omod->deactivated()) continue;
1299 FileType type;
1300 type.name = (_(omod->get_filetypename()));
1301 type.pattern = "*";
1302 Glib::ustring extension = omod->get_extension();
1303 fileDialogExtensionToPattern (type.pattern, extension);
1304 type.extension= omod;
1305 fileTypeComboBox.append_text(type.name);
1306 fileTypes.push_back(type);
1307 }
1309 //#Let user choose
1310 FileType guessType;
1311 guessType.name = _("Guess from extension");
1312 guessType.pattern = "*";
1313 guessType.extension = NULL;
1314 fileTypeComboBox.append_text(guessType.name);
1315 fileTypes.push_back(guessType);
1318 fileTypeComboBox.set_active(0);
1319 fileTypeChangedCallback(); //call at least once to set the filter
1320 }
1324 /**
1325 * Constructor
1326 */
1327 FileSaveDialogImpl::FileSaveDialogImpl(const Glib::ustring &dir,
1328 FileDialogType fileTypes,
1329 const Glib::ustring &title,
1330 const Glib::ustring &default_key) :
1331 FileDialogBase(title, Gtk::FILE_CHOOSER_ACTION_SAVE)
1332 {
1333 append_extension = (bool)prefs_get_int_attribute("dialogs.save_as",
1334 "append_extension", 1);
1336 /* One file at a time */
1337 set_select_multiple(false);
1339 /* Initalize to Autodetect */
1340 extension = NULL;
1341 /* No filename to start out with */
1342 myFilename = "";
1344 /* Set our dialog type (save, export, etc...)*/
1345 dialogType = fileTypes;
1347 /* Set the pwd and/or the filename */
1348 if (dir.size() > 0)
1349 {
1350 Glib::ustring udir(dir);
1351 Glib::ustring::size_type len = udir.length();
1352 // leaving a trailing backslash on the directory name leads to the infamous
1353 // double-directory bug on win32
1354 if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
1355 set_current_folder(udir.c_str());
1356 }
1358 //###### Add the file types menu
1359 //createFilterMenu();
1361 //###### Do we want the .xxx extension automatically added?
1362 fileTypeCheckbox.set_label(Glib::ustring(_("Append filename extension automatically")));
1363 fileTypeCheckbox.set_active(append_extension);
1365 fileTypeBox.pack_start(fileTypeCheckbox);
1366 createFileTypeMenu();
1367 fileTypeComboBox.set_size_request(200,40);
1368 fileTypeComboBox.signal_changed().connect(
1369 sigc::mem_fun(*this, &FileSaveDialogImpl::fileTypeChangedCallback) );
1371 fileTypeBox.pack_start(fileTypeComboBox);
1373 set_extra_widget(fileTypeBox);
1374 //get_vbox()->pack_start(fileTypeBox, false, false, 0);
1375 //get_vbox()->reorder_child(fileTypeBox, 2);
1377 //###### Add a preview widget
1378 set_preview_widget(svgPreview);
1379 set_preview_widget_active(true);
1380 set_use_preview_label (false);
1382 //Catch selection-changed events, so we can adjust the text widget
1383 signal_update_preview().connect(
1384 sigc::mem_fun(*this, &FileSaveDialogImpl::updatePreviewCallback) );
1387 //Let's do some customization
1388 fileNameEntry = NULL;
1389 Gtk::Container *cont = get_toplevel();
1390 std::vector<Gtk::Entry *> entries;
1391 findEntryWidgets(cont, entries);
1392 //g_message("Found %d entry widgets\n", entries.size());
1393 if (entries.size() >=1 )
1394 {
1395 //Catch when user hits [return] on the text field
1396 fileNameEntry = entries[0];
1397 fileNameEntry->signal_activate().connect(
1398 sigc::mem_fun(*this, &FileSaveDialogImpl::fileNameEntryChangedCallback) );
1399 }
1401 //Let's do more customization
1402 std::vector<Gtk::Expander *> expanders;
1403 findExpanderWidgets(cont, expanders);
1404 //g_message("Found %d expander widgets\n", expanders.size());
1405 if (expanders.size() >=1 )
1406 {
1407 //Always show the file list
1408 Gtk::Expander *expander = expanders[0];
1409 expander->set_expanded(true);
1410 }
1413 //if (extension == NULL)
1414 // checkbox.set_sensitive(FALSE);
1416 add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1417 add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK);
1419 show_all_children();
1420 }
1424 /**
1425 * Public factory method. Used in file.cpp
1426 */
1427 FileSaveDialog *FileSaveDialog::create(const Glib::ustring &path,
1428 FileDialogType fileTypes,
1429 const Glib::ustring &title,
1430 const Glib::ustring &default_key)
1431 {
1432 FileSaveDialog *dialog = new FileSaveDialogImpl(path, fileTypes, title, default_key);
1433 return dialog;
1434 }
1440 /**
1441 * Destructor
1442 */
1443 FileSaveDialogImpl::~FileSaveDialogImpl()
1444 {
1445 }
1449 /**
1450 * Show this dialog modally. Return true if user hits [OK]
1451 */
1452 bool
1453 FileSaveDialogImpl::show()
1454 {
1455 Glib::ustring s = Glib::filename_to_utf8 (get_current_folder());
1456 if (s.length() == 0)
1457 s = getcwd (NULL, 0);
1458 set_current_folder(Glib::filename_from_utf8(s)); //hack to force initial dir listing
1459 set_modal (TRUE); //Window
1460 sp_transientize((GtkWidget *)gobj()); //Make transient
1461 gint b = run(); //Dialog
1462 svgPreview.showNoPreview();
1463 hide();
1465 if (b == Gtk::RESPONSE_OK)
1466 {
1467 int sel = fileTypeComboBox.get_active_row_number ();
1468 if (sel>=0 && sel< (int)fileTypes.size())
1469 {
1470 FileType &type = fileTypes[sel];
1471 extension = type.extension;
1472 }
1473 myFilename = get_filename();
1475 /*
1477 // FIXME: Why do we have more code
1479 append_extension = checkbox.get_active();
1480 prefs_set_int_attribute("dialogs.save_as", "append_extension", append_extension);
1481 prefs_set_string_attribute("dialogs.save_as", "default",
1482 ( extension != NULL ? extension->get_id() : "" ));
1483 */
1484 return TRUE;
1485 }
1486 else
1487 {
1488 return FALSE;
1489 }
1490 }
1493 /**
1494 * Get the file extension type that was selected by the user. Valid after an [OK]
1495 */
1496 Inkscape::Extension::Extension *
1497 FileSaveDialogImpl::getSelectionType()
1498 {
1499 return extension;
1500 }
1503 /**
1504 * Get the file name chosen by the user. Valid after an [OK]
1505 */
1506 Glib::ustring
1507 FileSaveDialogImpl::getFilename()
1508 {
1509 return myFilename;
1510 }
1519 //########################################################################
1520 //# F I L E E X P O R T
1521 //########################################################################
1524 /**
1525 * Our implementation of the FileExportDialog interface.
1526 */
1527 class FileExportDialogImpl : public FileExportDialog, public FileDialogBase
1528 {
1530 public:
1531 FileExportDialogImpl(const Glib::ustring &dir,
1532 FileDialogType fileTypes,
1533 const Glib::ustring &title,
1534 const Glib::ustring &default_key);
1536 virtual ~FileExportDialogImpl();
1538 bool show();
1540 Inkscape::Extension::Extension *getSelectionType();
1542 Glib::ustring getFilename();
1545 /**
1546 * Return the scope of the export. One of the enumerated types
1547 * in ScopeType
1548 */
1549 ScopeType getScope()
1550 {
1551 if (pageButton.get_active())
1552 return SCOPE_PAGE;
1553 else if (selectionButton.get_active())
1554 return SCOPE_SELECTION;
1555 else if (customButton.get_active())
1556 return SCOPE_CUSTOM;
1557 else
1558 return SCOPE_DOCUMENT;
1560 }
1562 /**
1563 * Return left side of the exported region
1564 */
1565 double getSourceX()
1566 { return sourceX0Spinner.getValue(); }
1568 /**
1569 * Return the top of the exported region
1570 */
1571 double getSourceY()
1572 { return sourceY1Spinner.getValue(); }
1574 /**
1575 * Return the width of the exported region
1576 */
1577 double getSourceWidth()
1578 { return sourceWidthSpinner.getValue(); }
1580 /**
1581 * Return the height of the exported region
1582 */
1583 double getSourceHeight()
1584 { return sourceHeightSpinner.getValue(); }
1586 /**
1587 * Return the units of the coordinates of exported region
1588 */
1589 Glib::ustring getSourceUnits()
1590 { return sourceUnitsSpinner.getUnitAbbr(); }
1592 /**
1593 * Return the width of the destination document
1594 */
1595 double getDestinationWidth()
1596 { return destWidthSpinner.getValue(); }
1598 /**
1599 * Return the height of the destination document
1600 */
1601 double getDestinationHeight()
1602 { return destHeightSpinner.getValue(); }
1604 /**
1605 * Return the height of the exported region
1606 */
1607 Glib::ustring getDestinationUnits()
1608 { return destUnitsSpinner.getUnitAbbr(); }
1610 /**
1611 * Return the destination DPI image resulution, if bitmap
1612 */
1613 double getDestinationDPI()
1614 { return destDPISpinner.getValue(); }
1616 /**
1617 * Return whether we should use Cairo for rendering
1618 */
1619 bool getUseCairo()
1620 { return cairoButton.get_active(); }
1622 /**
1623 * Return whether we should use antialiasing
1624 */
1625 bool getUseAntialias()
1626 { return antiAliasButton.get_active(); }
1628 /**
1629 * Return the background color for exporting
1630 */
1631 unsigned long getBackground()
1632 { return backgroundButton.get_color().get_pixel(); }
1634 private:
1636 /**
1637 * What type of 'open' are we? (save, export, etc)
1638 */
1639 FileDialogType dialogType;
1641 /**
1642 * Our svg preview widget
1643 */
1644 SVGPreview svgPreview;
1646 /**
1647 * Fix to allow the user to type the file name
1648 */
1649 Gtk::Entry *fileNameEntry;
1651 /**
1652 * Callback for seeing if the preview needs to be drawn
1653 */
1654 void updatePreviewCallback();
1656 //##########################################
1657 //# EXTRA WIDGET -- SOURCE SIDE
1658 //##########################################
1660 Gtk::Frame sourceFrame;
1661 Gtk::VBox sourceBox;
1663 Gtk::HBox scopeBox;
1664 Gtk::RadioButtonGroup scopeGroup;
1665 Gtk::RadioButton documentButton;
1666 Gtk::RadioButton pageButton;
1667 Gtk::RadioButton selectionButton;
1668 Gtk::RadioButton customButton;
1670 Gtk::Table sourceTable;
1671 Inkscape::UI::Widget::Scalar sourceX0Spinner;
1672 Inkscape::UI::Widget::Scalar sourceY0Spinner;
1673 Inkscape::UI::Widget::Scalar sourceX1Spinner;
1674 Inkscape::UI::Widget::Scalar sourceY1Spinner;
1675 Inkscape::UI::Widget::Scalar sourceWidthSpinner;
1676 Inkscape::UI::Widget::Scalar sourceHeightSpinner;
1677 Inkscape::UI::Widget::UnitMenu sourceUnitsSpinner;
1680 //##########################################
1681 //# EXTRA WIDGET -- DESTINATION SIDE
1682 //##########################################
1684 Gtk::Frame destFrame;
1685 Gtk::VBox destBox;
1687 Gtk::Table destTable;
1688 Inkscape::UI::Widget::Scalar destWidthSpinner;
1689 Inkscape::UI::Widget::Scalar destHeightSpinner;
1690 Inkscape::UI::Widget::Scalar destDPISpinner;
1691 Inkscape::UI::Widget::UnitMenu destUnitsSpinner;
1693 Gtk::HBox otherOptionBox;
1694 Gtk::CheckButton cairoButton;
1695 Gtk::CheckButton antiAliasButton;
1696 Gtk::ColorButton backgroundButton;
1699 /**
1700 * 'Extra' widget that holds two boxes above
1701 */
1702 Gtk::HBox exportOptionsBox;
1705 //# Child widgets
1706 Gtk::CheckButton fileTypeCheckbox;
1708 /**
1709 * Allow the specification of the output file type
1710 */
1711 Gtk::ComboBoxText fileTypeComboBox;
1714 /**
1715 * Data mirror of the combo box
1716 */
1717 std::vector<FileType> fileTypes;
1721 /**
1722 * Callback for user input into fileNameEntry
1723 */
1724 void fileTypeChangedCallback();
1726 /**
1727 * Create a filter menu for this type of dialog
1728 */
1729 void createFileTypeMenu();
1732 bool append_extension;
1734 /**
1735 * The extension to use to write this file
1736 */
1737 Inkscape::Extension::Extension *extension;
1739 /**
1740 * Callback for user input into fileNameEntry
1741 */
1742 void fileNameEntryChangedCallback();
1744 /**
1745 * Filename that was given
1746 */
1747 Glib::ustring myFilename;
1748 };
1755 /**
1756 * Callback for checking if the preview needs to be redrawn
1757 */
1758 void FileExportDialogImpl::updatePreviewCallback()
1759 {
1760 Glib::ustring fileName = get_preview_filename();
1761 if (!fileName.c_str())
1762 return;
1763 bool retval = svgPreview.set(fileName, dialogType);
1764 set_preview_widget_active(retval);
1765 }
1769 /**
1770 * Callback for fileNameEntry widget
1771 */
1772 void FileExportDialogImpl::fileNameEntryChangedCallback()
1773 {
1774 if (!fileNameEntry)
1775 return;
1777 Glib::ustring fileName = fileNameEntry->get_text();
1778 if (!Glib::get_charset()) //If we are not utf8
1779 fileName = Glib::filename_to_utf8(fileName);
1781 //g_message("User hit return. Text is '%s'\n", fileName.c_str());
1783 if (!Glib::path_is_absolute(fileName)) {
1784 //try appending to the current path
1785 // not this way: fileName = get_current_folder() + "/" + fileName;
1786 std::vector<Glib::ustring> pathSegments;
1787 pathSegments.push_back( get_current_folder() );
1788 pathSegments.push_back( fileName );
1789 fileName = Glib::build_filename(pathSegments);
1790 }
1792 //g_message("path:'%s'\n", fileName.c_str());
1794 if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
1795 set_current_folder(fileName);
1796 } else if (/*Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR)*/1) {
1797 //dialog with either (1) select a regular file or (2) cd to dir
1798 //simulate an 'OK'
1799 set_filename(fileName);
1800 response(Gtk::RESPONSE_OK);
1801 }
1802 }
1806 /**
1807 * Callback for fileNameEntry widget
1808 */
1809 void FileExportDialogImpl::fileTypeChangedCallback()
1810 {
1811 int sel = fileTypeComboBox.get_active_row_number();
1812 if (sel<0 || sel >= (int)fileTypes.size())
1813 return;
1814 FileType type = fileTypes[sel];
1815 //g_message("selected: %s\n", type.name.c_str());
1816 Gtk::FileFilter filter;
1817 filter.add_pattern(type.pattern);
1818 set_filter(filter);
1819 }
1823 void FileExportDialogImpl::createFileTypeMenu()
1824 {
1825 Inkscape::Extension::DB::OutputList extension_list;
1826 Inkscape::Extension::db.get_output_list(extension_list);
1828 for (Inkscape::Extension::DB::OutputList::iterator current_item = extension_list.begin();
1829 current_item != extension_list.end(); current_item++)
1830 {
1831 Inkscape::Extension::Output * omod = *current_item;
1833 // FIXME: would be nice to grey them out instead of not listing them
1834 if (omod->deactivated()) continue;
1836 FileType type;
1837 type.name = (_(omod->get_filetypename()));
1838 type.pattern = "*";
1839 Glib::ustring extension = omod->get_extension();
1840 fileDialogExtensionToPattern (type.pattern, extension);
1841 type.extension= omod;
1842 fileTypeComboBox.append_text(type.name);
1843 fileTypes.push_back(type);
1844 }
1846 //#Let user choose
1847 FileType guessType;
1848 guessType.name = _("Guess from extension");
1849 guessType.pattern = "*";
1850 guessType.extension = NULL;
1851 fileTypeComboBox.append_text(guessType.name);
1852 fileTypes.push_back(guessType);
1855 fileTypeComboBox.set_active(0);
1856 fileTypeChangedCallback(); //call at least once to set the filter
1857 }
1860 /**
1861 * Constructor
1862 */
1863 FileExportDialogImpl::FileExportDialogImpl(const Glib::ustring &dir,
1864 FileDialogType fileTypes,
1865 const Glib::ustring &title,
1866 const Glib::ustring &default_key) :
1867 FileDialogBase(title, Gtk::FILE_CHOOSER_ACTION_SAVE),
1868 sourceX0Spinner("X0", _("Source left bound")),
1869 sourceY0Spinner("Y0", _("Source top bound")),
1870 sourceX1Spinner("X1", _("Source right bound")),
1871 sourceY1Spinner("Y1", _("Source bottom bound")),
1872 sourceWidthSpinner("Width", _("Source width")),
1873 sourceHeightSpinner("Height", _("Source height")),
1874 destWidthSpinner("Width", _("Destination width")),
1875 destHeightSpinner("Height", _("Destination height")),
1876 destDPISpinner("DPI", _("Dots per inch resolution"))
1877 {
1878 append_extension = (bool)prefs_get_int_attribute("dialogs.save_as", "append_extension", 1);
1880 /* One file at a time */
1881 set_select_multiple(false);
1883 /* Initalize to Autodetect */
1884 extension = NULL;
1885 /* No filename to start out with */
1886 myFilename = "";
1888 /* Set our dialog type (save, export, etc...)*/
1889 dialogType = fileTypes;
1891 /* Set the pwd and/or the filename */
1892 if (dir.size()>0)
1893 {
1894 Glib::ustring udir(dir);
1895 Glib::ustring::size_type len = udir.length();
1896 // leaving a trailing backslash on the directory name leads to the infamous
1897 // double-directory bug on win32
1898 if (len != 0 && udir[len - 1] == '\\') udir.erase(len - 1);
1899 set_current_folder(udir.c_str());
1900 }
1902 //#########################################
1903 //## EXTRA WIDGET -- SOURCE SIDE
1904 //#########################################
1906 //##### Export options buttons/spinners, etc
1907 documentButton.set_label(_("Document"));
1908 scopeBox.pack_start(documentButton);
1909 scopeGroup = documentButton.get_group();
1911 pageButton.set_label(_("Page"));
1912 pageButton.set_group(scopeGroup);
1913 scopeBox.pack_start(pageButton);
1915 selectionButton.set_label(_("Selection"));
1916 selectionButton.set_group(scopeGroup);
1917 scopeBox.pack_start(selectionButton);
1919 customButton.set_label(_("Custom"));
1920 customButton.set_group(scopeGroup);
1921 scopeBox.pack_start(customButton);
1923 sourceBox.pack_start(scopeBox);
1927 //dimension buttons
1928 sourceTable.resize(3,3);
1929 sourceTable.attach(sourceX0Spinner, 0,1,0,1);
1930 sourceTable.attach(sourceY0Spinner, 1,2,0,1);
1931 sourceUnitsSpinner.setUnitType(UNIT_TYPE_LINEAR);
1932 sourceTable.attach(sourceUnitsSpinner, 2,3,0,1);
1933 sourceTable.attach(sourceX1Spinner, 0,1,1,2);
1934 sourceTable.attach(sourceY1Spinner, 1,2,1,2);
1935 sourceTable.attach(sourceWidthSpinner, 0,1,2,3);
1936 sourceTable.attach(sourceHeightSpinner, 1,2,2,3);
1938 sourceBox.pack_start(sourceTable);
1939 sourceFrame.set_label(_("Source"));
1940 sourceFrame.add(sourceBox);
1941 exportOptionsBox.pack_start(sourceFrame);
1944 //#########################################
1945 //## EXTRA WIDGET -- SOURCE SIDE
1946 //#########################################
1949 destTable.resize(3,3);
1950 destTable.attach(destWidthSpinner, 0,1,0,1);
1951 destTable.attach(destHeightSpinner, 1,2,0,1);
1952 destUnitsSpinner.setUnitType(UNIT_TYPE_LINEAR);
1953 destTable.attach(destUnitsSpinner, 2,3,0,1);
1954 destTable.attach(destDPISpinner, 0,1,1,2);
1956 destBox.pack_start(destTable);
1959 cairoButton.set_label(_("Cairo"));
1960 otherOptionBox.pack_start(cairoButton);
1962 antiAliasButton.set_label(_("Antialias"));
1963 otherOptionBox.pack_start(antiAliasButton);
1965 backgroundButton.set_label(_("Background"));
1966 otherOptionBox.pack_start(backgroundButton);
1968 destBox.pack_start(otherOptionBox);
1974 //###### File options
1975 //###### Do we want the .xxx extension automatically added?
1976 fileTypeCheckbox.set_label(Glib::ustring(_("Append filename extension automatically")));
1977 fileTypeCheckbox.set_active(append_extension);
1978 destBox.pack_start(fileTypeCheckbox);
1980 //###### File type menu
1981 createFileTypeMenu();
1982 fileTypeComboBox.set_size_request(200,40);
1983 fileTypeComboBox.signal_changed().connect(
1984 sigc::mem_fun(*this, &FileExportDialogImpl::fileTypeChangedCallback) );
1986 destBox.pack_start(fileTypeComboBox);
1988 destFrame.set_label(_("Destination"));
1989 destFrame.add(destBox);
1990 exportOptionsBox.pack_start(destFrame);
1992 //##### Put the two boxes and their parent onto the dialog
1993 exportOptionsBox.pack_start(sourceFrame);
1994 exportOptionsBox.pack_start(destFrame);
1996 set_extra_widget(exportOptionsBox);
2001 //###### PREVIEW WIDGET
2002 set_preview_widget(svgPreview);
2003 set_preview_widget_active(true);
2004 set_use_preview_label (false);
2006 //Catch selection-changed events, so we can adjust the text widget
2007 signal_update_preview().connect(
2008 sigc::mem_fun(*this, &FileExportDialogImpl::updatePreviewCallback) );
2011 //Let's do some customization
2012 fileNameEntry = NULL;
2013 Gtk::Container *cont = get_toplevel();
2014 std::vector<Gtk::Entry *> entries;
2015 findEntryWidgets(cont, entries);
2016 //g_message("Found %d entry widgets\n", entries.size());
2017 if (entries.size() >=1 )
2018 {
2019 //Catch when user hits [return] on the text field
2020 fileNameEntry = entries[0];
2021 fileNameEntry->signal_activate().connect(
2022 sigc::mem_fun(*this, &FileExportDialogImpl::fileNameEntryChangedCallback) );
2023 }
2025 //Let's do more customization
2026 std::vector<Gtk::Expander *> expanders;
2027 findExpanderWidgets(cont, expanders);
2028 //g_message("Found %d expander widgets\n", expanders.size());
2029 if (expanders.size() >=1 )
2030 {
2031 //Always show the file list
2032 Gtk::Expander *expander = expanders[0];
2033 expander->set_expanded(true);
2034 }
2037 //if (extension == NULL)
2038 // checkbox.set_sensitive(FALSE);
2040 add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
2041 add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK);
2043 show_all_children();
2044 }
2048 /**
2049 * Public factory method. Used in file.cpp
2050 */
2051 FileExportDialog *FileExportDialog::create(const Glib::ustring &path,
2052 FileDialogType fileTypes,
2053 const Glib::ustring &title,
2054 const Glib::ustring &default_key)
2055 {
2056 FileExportDialog *dialog = new FileExportDialogImpl(path, fileTypes, title, default_key);
2057 return dialog;
2058 }
2064 /**
2065 * Destructor
2066 */
2067 FileExportDialogImpl::~FileExportDialogImpl()
2068 {
2069 }
2073 /**
2074 * Show this dialog modally. Return true if user hits [OK]
2075 */
2076 bool
2077 FileExportDialogImpl::show()
2078 {
2079 Glib::ustring s = Glib::filename_to_utf8 (get_current_folder());
2080 if (s.length() == 0)
2081 s = getcwd (NULL, 0);
2082 set_current_folder(Glib::filename_from_utf8(s)); //hack to force initial dir listing
2083 set_modal (TRUE); //Window
2084 sp_transientize((GtkWidget *)gobj()); //Make transient
2085 gint b = run(); //Dialog
2086 svgPreview.showNoPreview();
2087 hide();
2089 if (b == Gtk::RESPONSE_OK)
2090 {
2091 int sel = fileTypeComboBox.get_active_row_number ();
2092 if (sel>=0 && sel< (int)fileTypes.size())
2093 {
2094 FileType &type = fileTypes[sel];
2095 extension = type.extension;
2096 }
2097 myFilename = get_filename();
2099 /*
2101 // FIXME: Why do we have more code
2103 append_extension = checkbox.get_active();
2104 prefs_set_int_attribute("dialogs.save_as", "append_extension", append_extension);
2105 prefs_set_string_attribute("dialogs.save_as", "default",
2106 ( extension != NULL ? extension->get_id() : "" ));
2107 */
2108 return TRUE;
2109 }
2110 else
2111 {
2112 return FALSE;
2113 }
2114 }
2117 /**
2118 * Get the file extension type that was selected by the user. Valid after an [OK]
2119 */
2120 Inkscape::Extension::Extension *
2121 FileExportDialogImpl::getSelectionType()
2122 {
2123 return extension;
2124 }
2127 /**
2128 * Get the file name chosen by the user. Valid after an [OK]
2129 */
2130 Glib::ustring
2131 FileExportDialogImpl::getFilename()
2132 {
2133 return myFilename;
2134 }
2139 } //namespace Dialog
2140 } //namespace UI
2141 } //namespace Inkscape
2144 /*
2145 Local Variables:
2146 mode:c++
2147 c-file-style:"stroustrup"
2148 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2149 indent-tabs-mode:nil
2150 fill-column:99
2151 End:
2152 */
2153 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :