~ubuntu-branches/ubuntu/raring/gnome-photo-printer/raring

« back to all changes in this revision

Viewing changes to src/gpp_functions.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Schlueter
  • Date: 2004-04-20 22:18:13 UTC
  • Revision ID: james.westby@ubuntu.com-20040420221813-x5mf0l08zlgqq96x
Tags: upstream-0.6.3
ImportĀ upstreamĀ versionĀ 0.6.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
This file is part of Gnome Photo Printer.
 
3
 
 
4
    Gnome Photo Printer is free software; you can redistribute it and/or modify
 
5
    it under the terms of the GNU General Public License as published by
 
6
    the Free Software Foundation; either version 2 of the License, or
 
7
    (at your option) any later version.
 
8
 
 
9
    Gnome Photo Printer 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
 
12
    GNU General Public License for more details.
 
13
 
 
14
    You should have received a copy of the GNU General Public License
 
15
    along with Gnome Photo Printer; if not, write to the Free Software
 
16
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
*/
 
18
 
 
19
#include <stdio.h>
 
20
#include <stdlib.h>
 
21
#include <gnome.h>
 
22
#include <libgnome/gnome-i18n.h>
 
23
#include <glade/glade.h>
 
24
#include <gdk-pixbuf/gdk-pixbuf.h>
 
25
#include <libgnomevfs/gnome-vfs.h>
 
26
#include <libgnomeprint/gnome-print.h>
 
27
#include <libgnomeprint/gnome-print-job.h>
 
28
#include <libgnomeprintui/gnome-print-dialog.h>
 
29
#include "gpp_print.h"
 
30
#include "gpp_functions.h"
 
31
#include "gpp_widget.h"
 
32
#include "gpp_callbacks.h"
 
33
#include "gpp.h"
 
34
 
 
35
/* 
 
36
        Get the made Settings
 
37
        return: true on success
 
38
*/
 
39
gboolean
 
40
gpp_get_settings_from_libgnomeprint ()
 
41
{
 
42
        gdouble width, height;
 
43
        gboolean to_file;
 
44
 
 
45
        if (gnome_print_job_get_page_size (gpp_job, &width, &height) != TRUE)
 
46
        {
 
47
                g_printf ("Error, couldn't get page size!\n");
 
48
                return FALSE;
 
49
        }
 
50
 
 
51
        gpp_definition->paper_size_x = width;
 
52
        gpp_definition->paper_size_y = height;
 
53
 
 
54
 
 
55
        /* This truely needs to be fixed!!! Its a bug inside libgnomeprint!!!
 
56
         * if (gnome_print_config_get_boolean
 
57
         * (gpp_config, "PrintToFile", &to_file) != TRUE)
 
58
         * {
 
59
         * g_printf ("Error, couldn't get output definition!\n");
 
60
         * return FALSE;
 
61
         * }
 
62
         * else
 
63
         * {
 
64
         * gpp_definition->to_file = to_file;
 
65
         * }
 
66
         */
 
67
 
 
68
        /* Needs to be fixed when libgnomeprintui gets fixed! 
 
69
         * if (gnome_print_config_get
 
70
         * (gpp_config, "Settings.Transport.Backend.FileName") == NULL)
 
71
         * {
 
72
         * g_printf ("Error, couldn't get Name of Output File!\n");
 
73
         * return FALSE;
 
74
         * }
 
75
         * else
 
76
         * {
 
77
         * gpp_definition->output_file_name =
 
78
         * g_strdup (gnome_print_config_get
 
79
         * (gpp_config,
 
80
         * "Settings.Transport.Backend.FileName"));
 
81
         * }
 
82
         */
 
83
 
 
84
        /* Doing it this way right now! */
 
85
        gpp_definition->output_file_name =
 
86
                g_strdup (gnome_print_config_get
 
87
                          (gpp_config,
 
88
                           "Settings.Transport.Backend.FileName"));
 
89
 
 
90
        return TRUE;
 
91
}
 
92
 
 
93
/* 
 
94
        Show the settings 
 
95
        return: is TRUE on success
 
96
*/
 
97
gboolean
 
98
gpp_show_settings_from_libgnomeprint ()
 
99
{
 
100
        GtkTreeIter iter;
 
101
        GtkTreeModel *tree;
 
102
        gint i;
 
103
        gboolean valid;
 
104
 
 
105
        g_printf ("Print to file: %d\n", gpp_definition->to_file);
 
106
        g_printf ("File Name: %s\n", gpp_definition->output_file_name);
 
107
        g_printf ("Width: %f\n", gpp_definition->paper_size_x);
 
108
        g_printf ("Height: %f\n", gpp_definition->paper_size_y);
 
109
        g_printf ("Layout Type: %d\n", gpp_definition->layout);
 
110
        g_printf ("Layout X: %f\n", gpp_definition->layout_x);
 
111
        g_printf ("Layout Y: %f\n", gpp_definition->layout_y);
 
112
        g_printf ("Rotate: %d\n", gpp_calculate_rotate_pictures ());
 
113
        g_printf ("Number of Files: %d\n", gpp_definition->number_of_files);
 
114
 
 
115
        tree = GTK_TREE_MODEL (gpp_definition->file_names);
 
116
 
 
117
        /* Get the first iter in the list */
 
118
        valid = gtk_tree_model_get_iter_first (tree, &iter);
 
119
 
 
120
 
 
121
        while (valid)
 
122
        {
 
123
                /* Walk through the list, reading each row */
 
124
                gchar *file_name;
 
125
 
 
126
                gtk_tree_model_get (tree, &iter, FILE_COLUMN, &file_name, -1);
 
127
 
 
128
                /* Do something with the data */
 
129
                g_print ("File Names %s\n", file_name);
 
130
                g_free (file_name);
 
131
 
 
132
                valid = gtk_tree_model_iter_next (tree, &iter);
 
133
        }
 
134
 
 
135
        return TRUE;
 
136
}
 
137
 
 
138
/* 
 
139
        Calculate the pictures sizes (in metric) 
 
140
        size: is in cm 
 
141
        return: is in dots
 
142
*/
 
143
gdouble
 
144
gpp_calculate_size_metric_to_dpi (gdouble size)
 
145
{
 
146
        gdouble value;
 
147
        value = (size / 2.54) * PRINTER_DPI;
 
148
        return value;
 
149
}
 
150
 
 
151
/*
 
152
        Put as many pictures on one page as possibly!
 
153
        With or without rotating the pictures?
 
154
        Also updates the gpp_definition->layout information.
 
155
        return: true or false
 
156
*/
 
157
 
 
158
gboolean
 
159
gpp_calculate_rotate_pictures ()
 
160
{
 
161
        gdouble help, ax, ay, bx, by;
 
162
        gint cx, cy, dx, dy;
 
163
 
 
164
        ax = gpp_definition->paper_size_x;
 
165
        ay = gpp_definition->paper_size_y;
 
166
 
 
167
        bx = gpp_definition->layout_x;
 
168
        by = gpp_definition->layout_y;
 
169
 
 
170
        cx = gpp_calculate_fit_to_page (ax, bx);
 
171
        cy = gpp_calculate_fit_to_page (ay, by);
 
172
 
 
173
        dx = gpp_calculate_fit_to_page (ax, by);
 
174
        dy = gpp_calculate_fit_to_page (ay, bx);
 
175
 
 
176
        if ((cx * cy) <= (dx * dy))
 
177
        {
 
178
                help = gpp_definition->layout_x;
 
179
                gpp_definition->layout_x = gpp_definition->layout_y;
 
180
                gpp_definition->layout_y = help;
 
181
                return TRUE;
 
182
        }
 
183
 
 
184
        return FALSE;
 
185
}
 
186
 
 
187
gint
 
188
gpp_calculate_fit_to_page (gdouble page, gdouble pic)
 
189
{
 
190
        return page / pic;
 
191
}
 
192
 
 
193
/*
 
194
        Returns the number of pages
 
195
        return: number of pages
 
196
*/
 
197
gint
 
198
gpp_calculate_number_of_sites ()
 
199
{
 
200
        gint sites;
 
201
 
 
202
        sites = gpp_definition->number_of_files /
 
203
                gpp_calculate_pics_on_page ();
 
204
        if ((gpp_definition->number_of_files %
 
205
             gpp_calculate_pics_on_page ()) > 0)
 
206
                sites++;
 
207
 
 
208
        return sites;
 
209
}
 
210
 
 
211
/* 
 
212
        Calculates the number of pictures that fit on one page.
 
213
        return: number of pictures
 
214
*/
 
215
gint
 
216
gpp_calculate_pics_on_page ()
 
217
{
 
218
        gdouble ax, ay, bx, by;
 
219
        gint cx, cy;
 
220
 
 
221
        ax = gpp_definition->paper_size_x;
 
222
        ay = gpp_definition->paper_size_y;
 
223
 
 
224
        bx = gpp_definition->layout_x;
 
225
        by = gpp_definition->layout_y;
 
226
 
 
227
        cx = gpp_calculate_fit_to_page (ax, bx);
 
228
        cy = gpp_calculate_fit_to_page (ay, by);
 
229
 
 
230
        return cx * cy;
 
231
 
 
232
}
 
233
 
 
234
/*
 
235
        Calculates the space between each picture
 
236
        return: space between pictures
 
237
*/
 
238
gdouble
 
239
gpp_calculate_space_between_pics (gdouble page, gdouble pic)
 
240
{
 
241
        gdouble spaces;
 
242
 
 
243
        spaces = (page -
 
244
                  (gpp_calculate_fit_to_page (page, pic) * pic)) /
 
245
                (gpp_calculate_fit_to_page (page, pic) + 1);
 
246
 
 
247
        return spaces;
 
248
}
 
249
 
 
250
/* 
 
251
        Rotates one picture at 90 degree
 
252
        input: pixbuf
 
253
        return: pixbuf
 
254
*/
 
255
GdkPixbuf *
 
256
gpp_gdk_rotate_pixbuf_90 (GdkPixbuf * pixbuf)
 
257
{
 
258
        GdkPixbuf *pixbuf_new;
 
259
        GdkColorspace colorspace;
 
260
        guchar *raw_image;
 
261
        gboolean has_alpha;
 
262
        gint x, y, rowstride, height, width, bits_per_sample;
 
263
 
 
264
        raw_image = gdk_pixbuf_get_pixels (pixbuf);
 
265
        has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
 
266
        rowstride = gdk_pixbuf_get_rowstride (pixbuf);
 
267
        height = gdk_pixbuf_get_height (pixbuf);
 
268
        width = gdk_pixbuf_get_width (pixbuf);
 
269
        bits_per_sample = gdk_pixbuf_get_bits_per_sample (pixbuf);
 
270
        colorspace = gdk_pixbuf_get_colorspace (pixbuf);
 
271
 
 
272
        pixbuf_new =
 
273
                gdk_pixbuf_new (colorspace, has_alpha, bits_per_sample,
 
274
                                height, width);
 
275
 
 
276
        for (x = 0; x < width; x++)
 
277
        {
 
278
                for (y = 0; y < height; y++)
 
279
                {
 
280
                        gdk_pixbuf_copy_area (pixbuf, x, y, 1, 1, pixbuf_new,
 
281
                                              height - y - 1, x);
 
282
                };
 
283
        };
 
284
 
 
285
        gdk_pixbuf_unref (pixbuf);
 
286
 
 
287
        return pixbuf_new;
 
288
}
 
289
 
 
290
/*
 
291
        Checks if a file_name is an Image
 
292
        input: file_name
 
293
        output: TRUE or FALSE
 
294
*/
 
295
gboolean
 
296
gpp_check_for_image (gchar * file_name)
 
297
{
 
298
        GnomeVFSResult result;
 
299
        GnomeVFSFileInfo *file_info;
 
300
 
 
301
        file_info = gnome_vfs_file_info_new ();
 
302
        result = gnome_vfs_get_file_info (file_name, file_info,
 
303
                                          GNOME_VFS_FILE_INFO_GET_MIME_TYPE);
 
304
 
 
305
        if (file_info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_TYPE)
 
306
                if (file_info->type == GNOME_VFS_FILE_TYPE_REGULAR)
 
307
                        if (file_info->
 
308
                            valid_fields &
 
309
                            GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE)
 
310
                                if (g_strrstr (file_info->mime_type, "image/")
 
311
                                    != NULL)
 
312
                                        return TRUE;
 
313
 
 
314
        return FALSE;
 
315
}