]> git.donarmstrong.com Git - xournal.git/blob - src/xo-image.c
add Polish translation
[xournal.git] / src / xo-image.c
1 /*
2  *  This program is free software; you can redistribute it and/or
3  *  modify it under the terms of the GNU General Public
4  *  License as published by the Free Software Foundation; either
5  *  version 2 of the License, or (at your option) any later version.
6  *
7  *  This software is distributed in the hope that it will be useful,
8  *  but WITHOUT ANY WARRANTY; without even the implied warranty of  
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  *  General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
14  */
15
16 #ifdef HAVE_CONFIG_H
17 #  include <config.h>
18 #endif
19
20 #include <math.h>
21 #include <string.h>
22 #include <gtk/gtk.h>
23
24 #include "xournal.h"
25 #include "xo-support.h"
26 #include "xo-image.h"
27
28 // create pixbuf from buffer, or return NULL on failure
29 GdkPixbuf *pixbuf_from_buffer(const gchar *buf, gsize buflen)
30 {
31   GdkPixbufLoader *loader;
32   GdkPixbuf *pixbuf;
33
34   loader = gdk_pixbuf_loader_new();
35   gdk_pixbuf_loader_write(loader, buf, buflen, NULL);
36   gdk_pixbuf_loader_close(loader, NULL);
37   pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
38   g_object_ref(pixbuf);
39   g_object_unref(loader);
40   return pixbuf;
41 }
42
43 void create_image_from_pixbuf(GdkPixbuf *pixbuf, double *pt)
44 {
45   double scale;
46   struct Item *item;
47
48   item = g_new(struct Item, 1);
49   item->type = ITEM_IMAGE;
50   item->canvas_item = NULL;
51   item->bbox.left = pt[0];
52   item->bbox.top = pt[1];
53   item->image = pixbuf;
54   item->image_png = NULL;
55   item->image_png_len = 0;
56
57   // Scale at native size, unless that won't fit, in which case we shrink it down.
58   scale = 1 / ui.zoom;
59   if ((scale * gdk_pixbuf_get_width(item->image)) > ui.cur_page->width - item->bbox.left)
60     scale = (ui.cur_page->width - item->bbox.left) / gdk_pixbuf_get_width(item->image);
61   if ((scale * gdk_pixbuf_get_height(item->image)) > ui.cur_page->height - item->bbox.top)
62     scale = (ui.cur_page->height - item->bbox.top) / gdk_pixbuf_get_height(item->image);
63
64   item->bbox.right = item->bbox.left + scale * gdk_pixbuf_get_width(item->image);
65   item->bbox.bottom = item->bbox.top + scale * gdk_pixbuf_get_height(item->image);
66   ui.cur_layer->items = g_list_append(ui.cur_layer->items, item);
67   ui.cur_layer->nitems++;
68   
69   make_canvas_item_one(ui.cur_layer->group, item);
70
71   // add undo information
72   prepare_new_undo();
73   undo->type = ITEM_IMAGE;
74   undo->item = item;
75   undo->layer = ui.cur_layer;
76   ui.cur_item = NULL;
77   ui.cur_item_type = ITEM_NONE;
78
79   // select image
80   reset_selection();
81   ui.selection = g_new0(struct Selection, 1);
82   ui.selection->type = ITEM_SELECTRECT;
83   ui.selection->layer = ui.cur_layer;
84   ui.selection->bbox = item->bbox;
85   ui.selection->items = g_list_append(ui.selection->items, item);
86   ui.selection->canvas_item = gnome_canvas_item_new(ui.cur_layer->group,
87       gnome_canvas_rect_get_type(), "width-pixels", 1,
88       "outline-color-rgba", 0x000000ff,
89       "fill-color-rgba", 0x80808040,
90       "x1", ui.selection->bbox.left, "x2", ui.selection->bbox.right, 
91       "y1", ui.selection->bbox.top, "y2", ui.selection->bbox.bottom, NULL);
92   make_dashed(ui.selection->canvas_item);
93   update_copy_paste_enabled();
94 }
95
96 void insert_image(GdkEvent *event)
97 {
98   GtkTextBuffer *buffer;
99   GnomeCanvasItem *canvas_item;
100   GdkColor color;
101   GtkWidget *dialog;
102   GtkFileFilter *filt_all;
103   GtkFileFilter *filt_gdkimage;
104   char *filename;
105   GdkPixbuf *pixbuf;
106   double scale=1;
107   double pt[2];
108   
109   dialog = gtk_file_chooser_dialog_new(_("Insert Image"), GTK_WINDOW (winMain),
110      GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
111      GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL);
112 #ifdef FILE_DIALOG_SIZE_BUGFIX
113   gtk_window_set_default_size(GTK_WINDOW(dialog), 500, 400);
114 #endif
115      
116   filt_all = gtk_file_filter_new();
117   gtk_file_filter_set_name(filt_all, _("All files"));
118   gtk_file_filter_add_pattern(filt_all, "*");
119   filt_gdkimage = gtk_file_filter_new();
120   gtk_file_filter_set_name(filt_gdkimage, _("Image files"));
121   gtk_file_filter_add_pixbuf_formats(filt_gdkimage);
122   gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (dialog), filt_gdkimage);
123   gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (dialog), filt_all);
124
125   if (ui.default_image != NULL) gtk_file_chooser_set_filename(GTK_FILE_CHOOSER (dialog), ui.default_image);
126
127   if (gtk_dialog_run(GTK_DIALOG(dialog)) != GTK_RESPONSE_OK) {
128     gtk_widget_destroy(dialog);
129     return;
130   }
131   filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER (dialog));
132   gtk_widget_destroy(dialog);
133
134   if (filename == NULL) return; /* nothing selected */
135
136   if (ui.default_image != NULL) g_free(ui.default_image);
137   ui.default_image = g_strdup(filename);
138   
139   set_cursor_busy(TRUE);
140   pixbuf=gdk_pixbuf_new_from_file(filename, NULL);
141   set_cursor_busy(FALSE);
142   
143   if(pixbuf==NULL) { /* open failed */
144     dialog = gtk_message_dialog_new(GTK_WINDOW (winMain), GTK_DIALOG_MODAL,
145       GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Error opening image '%s'"), filename);
146     gtk_dialog_run(GTK_DIALOG(dialog));
147     gtk_widget_destroy(dialog);
148     g_free(filename);
149     ui.cur_item = NULL;
150     ui.cur_item_type = ITEM_NONE;
151     return;
152   }
153
154   ui.cur_item_type = ITEM_IMAGE;
155
156   get_pointer_coords(event, pt);
157   set_current_page(pt);  
158
159   create_image_from_pixbuf(pixbuf, pt);
160 }
161
162 void rescale_images(void)
163 {
164   // nothing needed in this implementation
165 }
166