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.
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.
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/>.
25 #include "xo-support.h"
28 // create pixbuf from buffer, or return NULL on failure
29 GdkPixbuf *pixbuf_from_buffer(const gchar *buf, gsize buflen)
31 GdkPixbufLoader *loader;
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);
39 g_object_unref(loader);
43 void create_image_from_pixbuf(GdkPixbuf *pixbuf, double *pt)
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];
54 item->image_png = NULL;
55 item->image_png_len = 0;
57 // Scale at native size, unless that won't fit, in which case we shrink it down.
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);
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++;
69 make_canvas_item_one(ui.cur_layer->group, item);
71 // add undo information
73 undo->type = ITEM_IMAGE;
75 undo->layer = ui.cur_layer;
77 ui.cur_item_type = ITEM_NONE;
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();
96 void insert_image(GdkEvent *event)
98 GtkTextBuffer *buffer;
99 GnomeCanvasItem *canvas_item;
102 GtkFileFilter *filt_all;
103 GtkFileFilter *filt_gdkimage;
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);
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);
125 if (ui.default_image != NULL) gtk_file_chooser_set_filename(GTK_FILE_CHOOSER (dialog), ui.default_image);
127 if (gtk_dialog_run(GTK_DIALOG(dialog)) != GTK_RESPONSE_OK) {
128 gtk_widget_destroy(dialog);
131 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER (dialog));
132 gtk_widget_destroy(dialog);
134 if (filename == NULL) return; /* nothing selected */
136 if (ui.default_image != NULL) g_free(ui.default_image);
137 ui.default_image = g_strdup(filename);
139 set_cursor_busy(TRUE);
140 pixbuf=gdk_pixbuf_new_from_file(filename, NULL);
141 set_cursor_busy(FALSE);
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);
150 ui.cur_item_type = ITEM_NONE;
154 ui.cur_item_type = ITEM_IMAGE;
156 get_pointer_coords(event, pt);
157 set_current_page(pt);
159 create_image_from_pixbuf(pixbuf, pt);
162 void rescale_images(void)
164 // nothing needed in this implementation