1/*
2* Copyright (c) 2018 (https://github.com/phase1geo/Minder)
3*
4* This program is free software; you can redistribute it and/or
5* modify it under the terms of the GNU General Public
6* License as published by the Free Software Foundation; either
7* version 2 of the License, or (at your option) any later version.
8*
9* This program is distributed in the hope that it will be useful,
10* but WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12* General Public License for more details.
13*
14* You should have received a copy of the GNU General Public
15* License along with this program; if not, write to the
16* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17* Boston, MA 02110-1301 USA
18*
19* Authored by: Trevor Williams <phase1geo@gmail.com>
20*/
21
22using Cairo;
23using Gdk;
24using Gtk;
25
26public class ExportImage : Export {
27
28  public ExportImage( string type, string label, string[] extensions ) {
29    base( type, label, extensions, true, false );
30  }
31
32  /* Default constructor */
33  public override bool export( string fname, DrawArea da ) {
34
35    /* Get the rectangle holding the entire document */
36    double x, y, w, h;
37    da.document_rectangle( out x, out y, out w, out h );
38
39    /* Create the drawing surface */
40    var surface = new ImageSurface( Format.RGB24, ((int)w + 20), ((int)h + 20) );
41    var context = new Context( surface );
42
43    /* Recreate the image */
44    da.get_style_context().render_background( context, 0, 0, ((int)w + 20), ((int)h + 20) );
45    context.translate( (10 - x), (10 - y) );
46    da.draw_all( context );
47
48    /* Write the pixbuf to the file */
49    var pixbuf = pixbuf_get_from_surface( surface, 0, 0, ((int)w + 20), ((int)h + 20) );
50
51    string[] option_keys   = {};
52    string[] option_values = {};
53
54    switch( name ) {
55      case "jpeg" :
56        var value = get_scale( "quality" );
57        option_keys += "quality";  option_values += value.to_string();
58        break;
59    }
60
61    try {
62      pixbuf.savev( fname, name, option_keys, option_values );
63    } catch( Error e ) {
64      stdout.printf( "Error writing %s: %s\n", name, e.message );
65      return( false );
66    }
67
68    return( true );
69
70  }
71
72  public override void add_settings( Grid grid ) {
73    switch( name ) {
74      case "jpeg" :  add_settings_jpeg( grid );  break;
75    }
76  }
77
78  private void add_settings_jpeg( Grid grid ) {
79    add_setting_scale( "quality", grid, _( "Quality" ), null, 0, 100, 1, 90 );
80  }
81
82  /* Save the settings */
83  public override void save_settings( Xml.Node* node ) {
84    switch( name ) {
85      case "jpeg" :  save_settings_jpeg( node );  break;
86    }
87  }
88
89  private void save_settings_jpeg( Xml.Node* node ) {
90    var value = get_scale( "quality" );
91    node->set_prop( "quality", value.to_string() );
92  }
93
94  /* Load the settings */
95  public override void load_settings( Xml.Node* node ) {
96    switch( name ) {
97      case "jpeg" :  load_settings_jpeg( node );  break;
98    }
99  }
100
101  private void load_settings_jpeg( Xml.Node* node ) {
102    var q = node->get_prop( "quality" );
103    if( q != null ) {
104      var value = int.parse( q );
105      set_scale( "quality", value );
106    }
107  }
108
109}
110