Code

2a37aed2b62e37ae9a36bdb8c7b34053cd3228cb
[inkscape.git] / src / ui / dialog / filedialogimpl-gtkmm.h
1 /** @file
2  * @brief Implementation of the file dialog interfaces defined in filedialogimpl.h
3  */
4 /* Authors:
5  *   Bob Jamison
6  *   Johan Engelen <johan@shouraizou.nl>
7  *   Joel Holdsworth
8  *   Bruno Dilly
9  *   Other dudes from The Inkscape Organization
10  *
11  * Copyright (C) 2004-2008 Authors
12  * Copyright (C) 2004-2007 The Inkscape Organization
13  *
14  * Released under GNU GPL, read the file 'COPYING' for more information
15  */
17 #ifndef __FILE_DIALOGIMPL_H__
18 #define __FILE_DIALOGIMPL_H__
20 #include "filedialog.h"
22 //General includes
23 #include <unistd.h>
24 #include <sys/stat.h>
25 #include <errno.h>
26 #include <libxml/parser.h>
27 #include <libxml/tree.h>
30 //Gtk includes
31 #include <glibmm/i18n.h>
32 #include <glib/gstdio.h>
34 //Temporary ugly hack
35 //Remove this after the get_filter() calls in
36 //show() on both classes are fixed
37 #include <gtk/gtkfilechooser.h>
39 //Another hack
40 #include <gtk/gtkentry.h>
41 #include <gtk/gtkexpander.h>
43 //Inkscape includes
44 #include "extension/input.h"
45 #include "extension/output.h"
46 #include "extension/db.h"
47 #include "inkscape.h"
48 #include "svg-view-widget.h"
50 //For export dialog
51 #include "ui/widget/scalar-unit.h"
53 namespace Inkscape
54 {
55 namespace UI
56 {
57 namespace Dialog
58 {
60 /*#########################################################################
61 ### Utility
62 #########################################################################*/
63 void
64 fileDialogExtensionToPattern(Glib::ustring &pattern,
65                       Glib::ustring &extension);
67 void
68 findEntryWidgets(Gtk::Container *parent,
69                  std::vector<Gtk::Entry *> &result);
71 void
72 findExpanderWidgets(Gtk::Container *parent,
73                     std::vector<Gtk::Expander *> &result);
75 /*#########################################################################
76 ### SVG Preview Widget
77 #########################################################################*/
79 class FileType
80 {
81     public:
82     FileType() {}
83     ~FileType() {}
84     Glib::ustring name;
85     Glib::ustring pattern;
86     Inkscape::Extension::Extension *extension;
87 };
89 /**
90  * Simple class for displaying an SVG file in the "preview widget."
91  * Currently, this is just a wrapper of the sp_svg_view Gtk widget.
92  * Hopefully we will eventually replace with a pure Gtkmm widget.
93  */
94 class SVGPreview : public Gtk::VBox
95 {
96 public:
98     SVGPreview();
100     ~SVGPreview();
102     bool setDocument(SPDocument *doc);
104     bool setFileName(Glib::ustring &fileName);
106     bool setFromMem(char const *xmlBuffer);
108     bool set(Glib::ustring &fileName, int dialogType);
110     bool setURI(URI &uri);
112     /**
113      * Show image embedded in SVG
114      */
115     void showImage(Glib::ustring &fileName);
117     /**
118      * Show the "No preview" image
119      */
120     void showNoPreview();
122     /**
123      * Show the "Too large" image
124      */
125     void showTooLarge(long fileLength);
127 private:
128     /**
129      * The svg document we are currently showing
130      */
131     SPDocument *document;
133     /**
134      * The sp_svg_view widget
135      */
136     GtkWidget *viewerGtk;
138     /**
139      * are we currently showing the "no preview" image?
140      */
141     bool showingNoPreview;
143 };
145 /*#########################################################################
146 ### F I L E     D I A L O G    B A S E    C L A S S
147 #########################################################################*/
149 /**
150  * This class is the base implementation for the others.  This
151  * reduces redundancies and bugs.
152  */
153 class FileDialogBaseGtk : public Gtk::FileChooserDialog
155 public:
157     /**
158      *
159      */
160     FileDialogBaseGtk(Gtk::Window& parentWindow, const Glib::ustring &title,
161                 Gtk::FileChooserAction dialogType, FileDialogType type, gchar const* preferenceBase) :
162         Gtk::FileChooserDialog(parentWindow, title, dialogType),
163         preferenceBase(preferenceBase ? preferenceBase : "unknown"),
164         _dialogType(type)
165     {
166         internalSetup();
167     }
169     /**
170      *
171      */
172     FileDialogBaseGtk(Gtk::Window& parentWindow, const char *title,
173                    Gtk::FileChooserAction dialogType, FileDialogType type, gchar const* preferenceBase) :
174         Gtk::FileChooserDialog(parentWindow, title, dialogType),
175         preferenceBase(preferenceBase ? preferenceBase : "unknown"),
176         _dialogType(type)
177     {
178         internalSetup();
179     }
181     /**
182      *
183      */
184     virtual ~FileDialogBaseGtk()
185         {}
187 protected:
188     void cleanup( bool showConfirmed );
190     Glib::ustring const preferenceBase;
191     /**
192      * What type of 'open' are we? (open, import, place, etc)
193      */
194     FileDialogType _dialogType;
196     /**
197      * Our svg preview widget
198      */
199     SVGPreview svgPreview;
201     /**
202          * Child widgets
203          */
204     Gtk::CheckButton previewCheckbox;
206 private:
207     void internalSetup();
209     /**
210      * Callback for user changing preview checkbox
211      */
212     void _previewEnabledCB();
214     /**
215      * Callback for seeing if the preview needs to be drawn
216      */
217     void _updatePreviewCallback();
218 };
223 /*#########################################################################
224 ### F I L E    O P E N
225 #########################################################################*/
227 /**
228  * Our implementation class for the FileOpenDialog interface..
229  */
230 class FileOpenDialogImplGtk : public FileOpenDialog, public FileDialogBaseGtk
232 public:
234     FileOpenDialogImplGtk(Gtk::Window& parentWindow,
235                        const Glib::ustring &dir,
236                        FileDialogType fileTypes,
237                        const Glib::ustring &title);
239     virtual ~FileOpenDialogImplGtk();
241     bool show();
243     Inkscape::Extension::Extension *getSelectionType();
245     Glib::ustring getFilename();
247     std::vector<Glib::ustring> getFilenames();
249         Glib::ustring getCurrentDirectory();
251 private:
253     /**
254      *  Create a filter menu for this type of dialog
255      */
256     void createFilterMenu();
258     /**
259      * Filter name->extension lookup
260      */
261     std::map<Glib::ustring, Inkscape::Extension::Extension *> extensionMap;
263     /**
264      * The extension to use to write this file
265      */
266     Inkscape::Extension::Extension *extension;
268 };
272 //########################################################################
273 //# F I L E    S A V E
274 //########################################################################
276 /**
277  * Our implementation of the FileSaveDialog interface.
278  */
279 class FileSaveDialogImplGtk : public FileSaveDialog, public FileDialogBaseGtk
282 public:
283     FileSaveDialogImplGtk(Gtk::Window &parentWindow,
284                           const Glib::ustring &dir,
285                           FileDialogType fileTypes,
286                           const Glib::ustring &title,
287                           const Glib::ustring &default_key,
288                           const gchar* docTitle,
289                           const bool save_copy);
291     virtual ~FileSaveDialogImplGtk();
293     bool show();
295     Inkscape::Extension::Extension *getSelectionType();
296     virtual void setSelectionType( Inkscape::Extension::Extension * key );
298         Glib::ustring getCurrentDirectory();
300 private:
301     //void change_title(const Glib::ustring& title);
302     void change_path(const Glib::ustring& path);
303     void updateNameAndExtension();
305     /**
306      * Whether the dialog was invoked by "Save as ..." or "Save a copy ..."
307      */
308     bool is_copy;
310     /**
311      * Fix to allow the user to type the file name
312      */
313     Gtk::Entry *fileNameEntry;
316     /**
317      * Allow the specification of the output file type
318      */
319     Gtk::ComboBoxText fileTypeComboBox;
322     /**
323      *  Data mirror of the combo box
324      */
325     std::vector<FileType> fileTypes;
327     //# Child widgets
328     Gtk::HBox childBox;
329     Gtk::VBox checksBox;
331     Gtk::CheckButton fileTypeCheckbox;
333     /**
334      * Callback for user input into fileNameEntry
335      */
336     void fileTypeChangedCallback();
338     /**
339      *  Create a filter menu for this type of dialog
340      */
341     void createFileTypeMenu();
344     /**
345      * The extension to use to write this file
346      */
347     Inkscape::Extension::Extension *extension;
349     /**
350      * Callback for user input into fileNameEntry
351      */
352     void fileNameEntryChangedCallback();
353 };
358 //########################################################################
359 //# F I L E     E X P O R T
360 //########################################################################
362 /**
363  * Our implementation of the FileExportDialog interface.
364  */
365 class FileExportDialogImpl : public FileExportDialog, public FileDialogBaseGtk
368 public:
369     FileExportDialogImpl(Gtk::Window& parentWindow,
370             const Glib::ustring &dir,
371             FileDialogType fileTypes,
372             const Glib::ustring &title,
373             const Glib::ustring &default_key);
375     virtual ~FileExportDialogImpl();
377     bool show();
379     Inkscape::Extension::Extension *getSelectionType();
381     Glib::ustring getFilename();
384     /**
385      * Return the scope of the export.  One of the enumerated types
386      * in ScopeType
387      */
388     ScopeType getScope()
389         {
390         if (pageButton.get_active())
391             return SCOPE_PAGE;
392         else if (selectionButton.get_active())
393             return SCOPE_SELECTION;
394         else if (customButton.get_active())
395             return SCOPE_CUSTOM;
396         else
397             return SCOPE_DOCUMENT;
399         }
401     /**
402      * Return left side of the exported region
403      */
404     double getSourceX()
405         { return sourceX0Spinner.getValue(); }
407     /**
408      * Return the top of the exported region
409      */
410     double getSourceY()
411         { return sourceY1Spinner.getValue(); }
413     /**
414      * Return the width of the exported region
415      */
416     double getSourceWidth()
417         { return sourceWidthSpinner.getValue(); }
419     /**
420      * Return the height of the exported region
421      */
422     double getSourceHeight()
423         { return sourceHeightSpinner.getValue(); }
425     /**
426      * Return the units of the coordinates of exported region
427      */
428     Glib::ustring getSourceUnits()
429         { return sourceUnitsSpinner.getUnitAbbr(); }
431     /**
432      * Return the width of the destination document
433      */
434     double getDestinationWidth()
435         { return destWidthSpinner.getValue(); }
437     /**
438      * Return the height of the destination document
439      */
440     double getDestinationHeight()
441         { return destHeightSpinner.getValue(); }
443     /**
444      * Return the height of the exported region
445      */
446     Glib::ustring getDestinationUnits()
447         { return destUnitsSpinner.getUnitAbbr(); }
449     /**
450      * Return the destination DPI image resulution, if bitmap
451      */
452     double getDestinationDPI()
453         { return destDPISpinner.getValue(); }
455     /**
456      * Return whether we should use Cairo for rendering
457      */
458     bool getUseCairo()
459         { return cairoButton.get_active(); }
461     /**
462      * Return whether we should use antialiasing
463      */
464     bool getUseAntialias()
465         { return antiAliasButton.get_active(); }
467     /**
468      * Return the background color for exporting
469      */
470     unsigned long getBackground()
471         { return backgroundButton.get_color().get_pixel(); }
473 private:
475     /**
476      * Fix to allow the user to type the file name
477      */
478     Gtk::Entry *fileNameEntry;
480     //##########################################
481     //# EXTRA WIDGET -- SOURCE SIDE
482     //##########################################
484     Gtk::Frame            sourceFrame;
485     Gtk::VBox             sourceBox;
487     Gtk::HBox             scopeBox;
488     Gtk::RadioButtonGroup scopeGroup;
489     Gtk::RadioButton      documentButton;
490     Gtk::RadioButton      pageButton;
491     Gtk::RadioButton      selectionButton;
492     Gtk::RadioButton      customButton;
494     Gtk::Table                      sourceTable;
495     Inkscape::UI::Widget::Scalar    sourceX0Spinner;
496     Inkscape::UI::Widget::Scalar    sourceY0Spinner;
497     Inkscape::UI::Widget::Scalar    sourceX1Spinner;
498     Inkscape::UI::Widget::Scalar    sourceY1Spinner;
499     Inkscape::UI::Widget::Scalar    sourceWidthSpinner;
500     Inkscape::UI::Widget::Scalar    sourceHeightSpinner;
501     Inkscape::UI::Widget::UnitMenu  sourceUnitsSpinner;
504     //##########################################
505     //# EXTRA WIDGET -- DESTINATION SIDE
506     //##########################################
508     Gtk::Frame       destFrame;
509     Gtk::VBox        destBox;
511     Gtk::Table                      destTable;
512     Inkscape::UI::Widget::Scalar    destWidthSpinner;
513     Inkscape::UI::Widget::Scalar    destHeightSpinner;
514     Inkscape::UI::Widget::Scalar    destDPISpinner;
515     Inkscape::UI::Widget::UnitMenu  destUnitsSpinner;
517     Gtk::HBox        otherOptionBox;
518     Gtk::CheckButton cairoButton;
519     Gtk::CheckButton antiAliasButton;
520     Gtk::ColorButton backgroundButton;
523     /**
524      * 'Extra' widget that holds two boxes above
525      */
526     Gtk::HBox exportOptionsBox;
529     //# Child widgets
530     Gtk::CheckButton fileTypeCheckbox;
532     /**
533      * Allow the specification of the output file type
534      */
535     Gtk::ComboBoxText fileTypeComboBox;
538     /**
539      *  Data mirror of the combo box
540      */
541     std::vector<FileType> fileTypes;
545     /**
546      * Callback for user input into fileNameEntry
547      */
548     void fileTypeChangedCallback();
550     /**
551      *  Create a filter menu for this type of dialog
552      */
553     void createFileTypeMenu();
556     bool append_extension;
558     /**
559      * The extension to use to write this file
560      */
561     Inkscape::Extension::Extension *extension;
563     /**
564      * Callback for user input into fileNameEntry
565      */
566     void fileNameEntryChangedCallback();
568     /**
569      * Filename that was given
570      */
571     Glib::ustring myFilename;
572 };
575 } // namespace Dialog
576 } // namespace UI
577 } // namespace Inkscape
579 #endif /*__FILE_DIALOGIMPL_H__*/
581 /*
582   Local Variables:
583   mode:c++
584   c-file-style:"stroustrup"
585   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
586   indent-tabs-mode:nil
587   fill-column:99
588   End:
589 */
590 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :