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/>.
23 #include <libgnomecanvas/libgnomecanvas.h>
26 #include "xo-callbacks.h"
27 #include "xo-interface.h"
28 #include "xo-support.h"
32 /************** drawing nice cursors *********/
34 void set_cursor_busy(gboolean busy)
39 cursor = gdk_cursor_new(GDK_WATCH);
40 gdk_window_set_cursor(GTK_WIDGET(winMain)->window, cursor);
41 gdk_window_set_cursor(GTK_WIDGET(canvas)->window, cursor);
42 gdk_cursor_unref(cursor);
45 gdk_window_set_cursor(GTK_WIDGET(winMain)->window, NULL);
48 gdk_display_sync(gdk_display_get_default());
51 #define PEN_CURSOR_RADIUS 1
52 #define HILITER_CURSOR_RADIUS 3
53 #define HILITER_BORDER_RADIUS 4
55 GdkCursor *make_pen_cursor(guint color_rgba)
58 guchar col[4], *pixels;
60 if (ui.pen_cursor_pix == NULL) {
61 ui.pen_cursor_pix = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 16, 16);
62 if (ui.pen_cursor_pix == NULL) return NULL; // couldn't create pixbuf
63 gdk_pixbuf_fill(ui.pen_cursor_pix, 0xffffff00); // transparent white
65 rowstride = gdk_pixbuf_get_rowstride(ui.pen_cursor_pix);
66 pixels = gdk_pixbuf_get_pixels(ui.pen_cursor_pix);
68 col[0] = (color_rgba >> 24) & 0xff;
69 col[1] = (color_rgba >> 16) & 0xff;
70 col[2] = (color_rgba >> 8) & 0xff;
71 col[3] = 0xff; // solid
72 for (x = 8-PEN_CURSOR_RADIUS; x <= 8+PEN_CURSOR_RADIUS; x++)
73 for (y = 8-PEN_CURSOR_RADIUS; y <= 8+PEN_CURSOR_RADIUS; y++)
74 g_memmove(pixels + y*rowstride + x*4, col, 4);
76 return gdk_cursor_new_from_pixbuf(gdk_display_get_default(), ui.pen_cursor_pix, 7, 7);
79 GdkCursor *make_hiliter_cursor(guint color_rgba)
82 guchar col[4], *pixels;
84 if (ui.hiliter_cursor_pix == NULL) {
85 ui.hiliter_cursor_pix = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 16, 16);
86 if (ui.hiliter_cursor_pix == NULL) return NULL; // couldn't create pixbuf
87 gdk_pixbuf_fill(ui.hiliter_cursor_pix, 0xffffff00); // transparent white
89 rowstride = gdk_pixbuf_get_rowstride(ui.hiliter_cursor_pix);
90 pixels = gdk_pixbuf_get_pixels(ui.hiliter_cursor_pix);
92 col[0] = col[1] = col[2] = 0; // black
93 col[3] = 0xff; // solid
94 for (x = 8-HILITER_BORDER_RADIUS; x <= 8+HILITER_BORDER_RADIUS; x++)
95 for (y = 8-HILITER_BORDER_RADIUS; y <= 8+HILITER_BORDER_RADIUS; y++)
96 g_memmove(pixels + y*rowstride + x*4, col, 4);
98 col[0] = (color_rgba >> 24) & 0xff;
99 col[1] = (color_rgba >> 16) & 0xff;
100 col[2] = (color_rgba >> 8) & 0xff;
101 col[3] = 0xff; // solid
102 for (x = 8-HILITER_CURSOR_RADIUS; x <= 8+HILITER_CURSOR_RADIUS; x++)
103 for (y = 8-HILITER_CURSOR_RADIUS; y <= 8+HILITER_CURSOR_RADIUS; y++)
104 g_memmove(pixels + y*rowstride + x*4, col, 4);
106 return gdk_cursor_new_from_pixbuf(gdk_display_get_default(), ui.hiliter_cursor_pix, 7, 7);
109 void update_cursor(void)
111 GdkPixmap *source, *mask;
112 GdkColor fg = {0, 0, 0, 0}, bg = {0, 65535, 65535, 65535};
114 ui.is_sel_cursor = FALSE;
115 if (GTK_WIDGET(canvas)->window == NULL) return;
117 if (ui.cursor!=NULL) {
118 gdk_cursor_unref(ui.cursor);
121 if (ui.cur_item_type == ITEM_MOVESEL_VERT)
122 ui.cursor = gdk_cursor_new(GDK_SB_V_DOUBLE_ARROW);
123 else if (ui.cur_item_type == ITEM_MOVESEL)
124 ui.cursor = gdk_cursor_new(GDK_FLEUR);
125 else if (ui.toolno[ui.cur_mapping] == TOOL_PEN) {
126 ui.cursor = make_pen_cursor(ui.cur_brush->color_rgba);
128 else if (ui.toolno[ui.cur_mapping] == TOOL_ERASER) {
129 ui.cursor = make_hiliter_cursor(0xffffffff);
131 else if (ui.toolno[ui.cur_mapping] == TOOL_HIGHLIGHTER) {
132 ui.cursor = make_hiliter_cursor(ui.cur_brush->color_rgba);
134 else if (ui.cur_item_type == ITEM_SELECTRECT) {
135 ui.cursor = gdk_cursor_new(GDK_TCROSS);
137 else if (ui.toolno[ui.cur_mapping] == TOOL_HAND) {
138 ui.cursor = gdk_cursor_new(GDK_HAND1);
140 else if (ui.toolno[ui.cur_mapping] == TOOL_TEXT) {
141 ui.cursor = gdk_cursor_new(GDK_XTERM);
144 gdk_window_set_cursor(GTK_WIDGET(canvas)->window, ui.cursor);
147 /* adjust the cursor shape if it hovers near a selection box */
149 void update_cursor_for_resize(double *pt)
151 gboolean in_range_x, in_range_y;
152 gboolean can_resize_left, can_resize_right, can_resize_bottom, can_resize_top;
153 gdouble resize_margin;
154 GdkCursorType newcursor;
156 // if we're not even close to the box in some direction, return immediately
157 resize_margin = RESIZE_MARGIN / ui.zoom;
158 if (pt[0]<ui.selection->bbox.left-resize_margin || pt[0]>ui.selection->bbox.right+resize_margin
159 || pt[1]<ui.selection->bbox.top-resize_margin || pt[1]>ui.selection->bbox.bottom+resize_margin)
161 if (ui.is_sel_cursor) update_cursor();
165 ui.is_sel_cursor = TRUE;
166 can_resize_left = (pt[0] < ui.selection->bbox.left+resize_margin);
167 can_resize_right = (pt[0] > ui.selection->bbox.right-resize_margin);
168 can_resize_top = (pt[1] < ui.selection->bbox.top+resize_margin);
169 can_resize_bottom = (pt[1] > ui.selection->bbox.bottom-resize_margin);
171 if (can_resize_left) {
172 if (can_resize_top) newcursor = GDK_TOP_LEFT_CORNER;
173 else if (can_resize_bottom) newcursor = GDK_BOTTOM_LEFT_CORNER;
174 else newcursor = GDK_LEFT_SIDE;
176 else if (can_resize_right) {
177 if (can_resize_top) newcursor = GDK_TOP_RIGHT_CORNER;
178 else if (can_resize_bottom) newcursor = GDK_BOTTOM_RIGHT_CORNER;
179 else newcursor = GDK_RIGHT_SIDE;
181 else if (can_resize_top) newcursor = GDK_TOP_SIDE;
182 else if (can_resize_bottom) newcursor = GDK_BOTTOM_SIDE;
183 else newcursor = GDK_FLEUR;
185 if (ui.cursor!=NULL && ui.cursor->type == newcursor) return;
186 if (ui.cursor!=NULL) gdk_cursor_unref(ui.cursor);
187 ui.cursor = gdk_cursor_new(newcursor);
188 gdk_window_set_cursor(GTK_WIDGET(canvas)->window, ui.cursor);
191 /************** painting strokes *************/
193 #define SUBDIVIDE_MAXDIST 5.0
195 void subdivide_cur_path(null)
199 double x0, y0, x1, y1;
201 for (n=0, p=ui.cur_path.coords; n<ui.cur_path.num_points-1; n++, p+=2) {
202 pieces = (int)floor(hypot(p[2]-p[0], p[3]-p[1])/SUBDIVIDE_MAXDIST);
204 x0 = p[0]; y0 = p[1];
205 x1 = p[2]; y1 = p[3];
206 realloc_cur_path(ui.cur_path.num_points+pieces-1);
207 g_memmove(ui.cur_path.coords+2*(n+pieces), ui.cur_path.coords+2*(n+1),
208 2*(ui.cur_path.num_points-n-1)*sizeof(double));
209 p = ui.cur_path.coords+2*n;
210 ui.cur_path.num_points += pieces-1;
212 for (k=1; k<pieces; k++) {
214 p[0] = x0 + k*(x1-x0)/pieces;
215 p[1] = y0 + k*(y1-y0)/pieces;
221 void create_new_stroke(GdkEvent *event)
223 ui.cur_item_type = ITEM_STROKE;
224 ui.cur_item = g_new(struct Item, 1);
225 ui.cur_item->type = ITEM_STROKE;
226 g_memmove(&(ui.cur_item->brush), ui.cur_brush, sizeof(struct Brush));
227 ui.cur_item->path = &ui.cur_path;
229 ui.cur_path.num_points = 1;
230 get_pointer_coords(event, ui.cur_path.coords);
232 if (ui.cur_brush->ruler) {
233 ui.cur_item->canvas_item = gnome_canvas_item_new(ui.cur_layer->group,
234 gnome_canvas_line_get_type(),
235 "cap-style", GDK_CAP_ROUND, "join-style", GDK_JOIN_ROUND,
236 "fill-color-rgba", ui.cur_item->brush.color_rgba,
237 "width-units", ui.cur_item->brush.thickness, NULL);
238 ui.cur_item->brush.variable_width = FALSE;
240 ui.cur_item->canvas_item = gnome_canvas_item_new(
241 ui.cur_layer->group, gnome_canvas_group_get_type(), NULL);
244 void continue_stroke(GdkEvent *event)
246 GnomeCanvasPoints seg;
247 double *pt, current_width;
249 if (ui.cur_brush->ruler) {
250 pt = ui.cur_path.coords;
252 realloc_cur_path(ui.cur_path.num_points+1);
253 pt = ui.cur_path.coords + 2*(ui.cur_path.num_points-1);
256 get_pointer_coords(event, pt+2);
258 if (ui.cur_item->brush.variable_width) {
259 realloc_cur_widths(ui.cur_path.num_points);
260 current_width = ui.cur_item->brush.thickness*get_pressure_multiplier(event);
261 ui.cur_widths[ui.cur_path.num_points-1] = current_width;
263 else current_width = ui.cur_item->brush.thickness;
265 if (ui.cur_brush->ruler)
266 ui.cur_path.num_points = 2;
268 if (hypot(pt[0]-pt[2], pt[1]-pt[3]) < PIXEL_MOTION_THRESHOLD/ui.zoom)
269 return; // not a meaningful motion
270 ui.cur_path.num_points++;
277 /* note: we're using a piece of the cur_path array. This is ok because
278 upon creation the line just copies the contents of the GnomeCanvasPoints
279 into an internal structure */
281 if (ui.cur_brush->ruler)
282 gnome_canvas_item_set(ui.cur_item->canvas_item, "points", &seg, NULL);
284 gnome_canvas_item_new((GnomeCanvasGroup *)ui.cur_item->canvas_item,
285 gnome_canvas_line_get_type(), "points", &seg,
286 "cap-style", GDK_CAP_ROUND, "join-style", GDK_JOIN_ROUND,
287 "fill-color-rgba", ui.cur_item->brush.color_rgba,
288 "width-units", current_width, NULL);
291 void finalize_stroke(void)
293 if (ui.cur_path.num_points == 1) { // GnomeCanvas doesn't like num_points=1
294 ui.cur_path.coords[2] = ui.cur_path.coords[0]+0.1;
295 ui.cur_path.coords[3] = ui.cur_path.coords[1];
296 ui.cur_path.num_points = 2;
297 ui.cur_item->brush.variable_width = FALSE;
300 if (!ui.cur_item->brush.variable_width)
301 subdivide_cur_path(); // split the segment so eraser will work
303 ui.cur_item->path = gnome_canvas_points_new(ui.cur_path.num_points);
304 g_memmove(ui.cur_item->path->coords, ui.cur_path.coords,
305 2*ui.cur_path.num_points*sizeof(double));
306 if (ui.cur_item->brush.variable_width)
307 ui.cur_item->widths = (gdouble *)g_memdup(ui.cur_widths,
308 (ui.cur_path.num_points-1)*sizeof(gdouble));
309 else ui.cur_item->widths = NULL;
310 update_item_bbox(ui.cur_item);
311 ui.cur_path.num_points = 0;
313 if (!ui.cur_item->brush.variable_width) {
314 // destroy the entire group of temporary line segments
315 gtk_object_destroy(GTK_OBJECT(ui.cur_item->canvas_item));
316 // make a new line item to replace it
317 make_canvas_item_one(ui.cur_layer->group, ui.cur_item);
320 // add undo information
322 undo->type = ITEM_STROKE;
323 undo->item = ui.cur_item;
324 undo->layer = ui.cur_layer;
326 // store the item on top of the layer stack
327 ui.cur_layer->items = g_list_append(ui.cur_layer->items, ui.cur_item);
328 ui.cur_layer->nitems++;
330 ui.cur_item_type = ITEM_NONE;
333 /************** eraser tool *************/
335 void erase_stroke_portions(struct Item *item, double x, double y, double radius,
336 gboolean whole_strokes, struct UndoErasureData *erasure)
340 struct Item *newhead, *newtail;
341 gboolean need_recalc = FALSE;
343 for (i=0, pt=item->path->coords; i<item->path->num_points; i++, pt+=2) {
344 if (hypot(pt[0]-x, pt[1]-y) <= radius) { // found an intersection
345 // FIXME: need to test if line SEGMENT hits the circle
346 // hide the canvas item, and create erasure data if needed
347 if (erasure == NULL) {
348 item->type = ITEM_TEMP_STROKE;
349 gnome_canvas_item_hide(item->canvas_item);
350 /* we'll use this hidden item as an insertion point later */
351 erasure = (struct UndoErasureData *)g_malloc(sizeof(struct UndoErasureData));
352 item->erasure = erasure;
353 erasure->item = item;
354 erasure->npos = g_list_index(ui.cur_layer->items, item);
356 erasure->replacement_items = NULL;
359 newhead = newtail = NULL;
360 if (!whole_strokes) {
362 newhead = (struct Item *)g_malloc(sizeof(struct Item));
363 newhead->type = ITEM_STROKE;
364 g_memmove(&newhead->brush, &item->brush, sizeof(struct Brush));
365 newhead->path = gnome_canvas_points_new(i);
366 g_memmove(newhead->path->coords, item->path->coords, 2*i*sizeof(double));
367 if (newhead->brush.variable_width)
368 newhead->widths = (gdouble *)g_memdup(item->widths, (i-1)*sizeof(gdouble));
369 else newhead->widths = NULL;
371 while (++i < item->path->num_points) {
373 if (hypot(pt[0]-x, pt[1]-y) > radius) break;
375 if (i<item->path->num_points-1) {
376 newtail = (struct Item *)g_malloc(sizeof(struct Item));
377 newtail->type = ITEM_STROKE;
378 g_memmove(&newtail->brush, &item->brush, sizeof(struct Brush));
379 newtail->path = gnome_canvas_points_new(item->path->num_points-i);
380 g_memmove(newtail->path->coords, item->path->coords+2*i,
381 2*(item->path->num_points-i)*sizeof(double));
382 if (newtail->brush.variable_width)
383 newtail->widths = (gdouble *)g_memdup(item->widths+i,
384 (item->path->num_points-i-1)*sizeof(gdouble));
385 else newtail->widths = NULL;
386 newtail->canvas_item = NULL;
389 if (item->type == ITEM_STROKE) {
390 // it's inside an erasure list - we destroy it
391 gnome_canvas_points_free(item->path);
392 if (item->brush.variable_width) g_free(item->widths);
393 if (item->canvas_item != NULL)
394 gtk_object_destroy(GTK_OBJECT(item->canvas_item));
396 erasure->replacement_items = g_list_remove(erasure->replacement_items, item);
400 if (newhead != NULL) {
401 update_item_bbox(newhead);
402 make_canvas_item_one(ui.cur_layer->group, newhead);
403 lower_canvas_item_to(ui.cur_layer->group,
404 newhead->canvas_item, erasure->item->canvas_item);
405 erasure->replacement_items = g_list_prepend(erasure->replacement_items, newhead);
407 // prepending ensures it won't get processed twice
409 // recurse into the new tail
410 need_recalc = (newtail!=NULL);
411 if (newtail == NULL) break;
413 erasure->replacement_items = g_list_prepend(erasure->replacement_items, newtail);
415 i=0; pt=item->path->coords;
418 // add the tail if needed
419 if (!need_recalc) return;
420 update_item_bbox(item);
421 make_canvas_item_one(ui.cur_layer->group, item);
422 lower_canvas_item_to(ui.cur_layer->group, item->canvas_item,
423 erasure->item->canvas_item);
427 void do_eraser(GdkEvent *event, double radius, gboolean whole_strokes)
429 struct Item *item, *repl;
430 GList *itemlist, *repllist;
432 struct BBox eraserbox;
434 get_pointer_coords(event, pos);
435 eraserbox.left = pos[0]-radius;
436 eraserbox.right = pos[0]+radius;
437 eraserbox.top = pos[1]-radius;
438 eraserbox.bottom = pos[1]+radius;
439 for (itemlist = ui.cur_layer->items; itemlist!=NULL; itemlist = itemlist->next) {
440 item = (struct Item *)itemlist->data;
441 if (item->type == ITEM_STROKE) {
442 if (!have_intersect(&(item->bbox), &eraserbox)) continue;
443 erase_stroke_portions(item, pos[0], pos[1], radius, whole_strokes, NULL);
444 } else if (item->type == ITEM_TEMP_STROKE) {
445 repllist = item->erasure->replacement_items;
446 while (repllist!=NULL) {
447 repl = (struct Item *)repllist->data;
448 // we may delete the item soon! so advance now in the list
449 repllist = repllist->next;
450 if (have_intersect(&(repl->bbox), &eraserbox))
451 erase_stroke_portions(repl, pos[0], pos[1], radius, whole_strokes, item->erasure);
457 void finalize_erasure(void)
459 GList *itemlist, *partlist;
463 undo->type = ITEM_ERASURE;
464 undo->layer = ui.cur_layer;
465 undo->erasurelist = NULL;
467 itemlist = ui.cur_layer->items;
468 while (itemlist!=NULL) {
469 item = (struct Item *)itemlist->data;
470 itemlist = itemlist->next;
471 if (item->type != ITEM_TEMP_STROKE) continue;
472 item->type = ITEM_STROKE;
473 ui.cur_layer->items = g_list_remove(ui.cur_layer->items, item);
474 // the item has an invisible canvas item, which used to act as anchor
475 if (item->canvas_item!=NULL) {
476 gtk_object_destroy(GTK_OBJECT(item->canvas_item));
477 item->canvas_item = NULL;
479 undo->erasurelist = g_list_append(undo->erasurelist, item->erasure);
480 // add the new strokes into the current layer
481 for (partlist = item->erasure->replacement_items; partlist!=NULL; partlist = partlist->next)
482 ui.cur_layer->items = g_list_insert_before(
483 ui.cur_layer->items, itemlist, partlist->data);
484 ui.cur_layer->nitems += item->erasure->nrepl-1;
488 ui.cur_item_type = ITEM_NONE;
490 /* NOTE: the list of erasures goes in the depth order of the layer;
491 this guarantees that, upon undo, the erasure->npos fields give the
492 correct position where each item should be reinserted as the list
493 is traversed in the forward direction */
497 gboolean do_hand_scrollto(gpointer data)
499 ui.hand_scrollto_pending = FALSE;
500 gnome_canvas_scroll_to(canvas, ui.hand_scrollto_cx, ui.hand_scrollto_cy);
504 void do_hand(GdkEvent *event)
509 get_pointer_coords(event, pt);
510 pt[0] += ui.cur_page->hoffset;
511 pt[1] += ui.cur_page->voffset;
512 gnome_canvas_get_scroll_offsets(canvas, &cx, &cy);
513 ui.hand_scrollto_cx = cx - (pt[0]-ui.hand_refpt[0])*ui.zoom;
514 ui.hand_scrollto_cy = cy - (pt[1]-ui.hand_refpt[1])*ui.zoom;
515 if (!ui.hand_scrollto_pending) g_idle_add(do_hand_scrollto, NULL);
516 ui.hand_scrollto_pending = TRUE;
519 /************ TEXT FUNCTIONS **************/
521 // to make it easier to copy/paste at end of text box
522 #define WIDGET_RIGHT_MARGIN 10
524 void resize_textview(gpointer *toplevel, gpointer *data)
529 /* when the text changes, resize the GtkTextView accordingly */
530 if (ui.cur_item_type!=ITEM_TEXT) return;
531 w = GTK_TEXT_VIEW(ui.cur_item->widget);
532 width = w->width + WIDGET_RIGHT_MARGIN;
534 gnome_canvas_item_set(ui.cur_item->canvas_item,
536 "width", (gdouble)width, "height", (gdouble)height, NULL);
537 ui.cur_item->bbox.right = ui.cur_item->bbox.left + width/ui.zoom;
538 ui.cur_item->bbox.bottom = ui.cur_item->bbox.top + height/ui.zoom;
541 void start_text(GdkEvent *event, struct Item *item)
544 GtkTextBuffer *buffer;
545 GnomeCanvasItem *canvas_item;
546 PangoFontDescription *font_desc;
549 get_pointer_coords(event, pt);
551 ui.cur_item_type = ITEM_TEXT;
554 item = g_new(struct Item, 1);
556 item->canvas_item = NULL;
557 item->bbox.left = pt[0];
558 item->bbox.top = pt[1];
559 item->bbox.right = ui.cur_page->width;
560 item->bbox.bottom = pt[1]+100.;
561 item->font_name = g_strdup(ui.font_name);
562 item->font_size = ui.font_size;
563 g_memmove(&(item->brush), ui.cur_brush, sizeof(struct Brush));
564 ui.cur_layer->items = g_list_append(ui.cur_layer->items, item);
565 ui.cur_layer->nitems++;
568 item->type = ITEM_TEMP_TEXT;
571 font_desc = pango_font_description_from_string(item->font_name);
572 pango_font_description_set_absolute_size(font_desc,
573 item->font_size*ui.zoom*PANGO_SCALE);
574 item->widget = gtk_text_view_new();
575 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(item->widget));
576 if (item->text!=NULL)
577 gtk_text_buffer_set_text(buffer, item->text, -1);
578 gtk_widget_modify_font(item->widget, font_desc);
579 rgb_to_gdkcolor(item->brush.color_rgba, &color);
580 gtk_widget_modify_text(item->widget, GTK_STATE_NORMAL, &color);
581 pango_font_description_free(font_desc);
583 canvas_item = gnome_canvas_item_new(ui.cur_layer->group,
584 gnome_canvas_widget_get_type(),
585 "x", item->bbox.left, "y", item->bbox.top,
586 "width", item->bbox.right-item->bbox.left,
587 "height", item->bbox.bottom-item->bbox.top,
588 "widget", item->widget, NULL);
589 // TODO: width/height?
590 if (item->canvas_item!=NULL) {
591 lower_canvas_item_to(ui.cur_layer->group, canvas_item, item->canvas_item);
592 gtk_object_destroy(GTK_OBJECT(item->canvas_item));
594 item->canvas_item = canvas_item;
596 gtk_widget_show(item->widget);
597 ui.resize_signal_handler =
598 g_signal_connect((gpointer) winMain, "check_resize",
599 G_CALLBACK(resize_textview), NULL);
600 update_font_button();
601 gtk_widget_set_sensitive(GET_COMPONENT("editPaste"), FALSE);
602 gtk_widget_set_sensitive(GET_COMPONENT("buttonPaste"), FALSE);
603 gtk_widget_grab_focus(item->widget);
608 GtkTextBuffer *buffer;
609 GtkTextIter start, end;
611 struct UndoErasureData *erasure;
612 GnomeCanvasItem *tmpitem;
614 if (ui.cur_item_type!=ITEM_TEXT) return; // nothing for us to do!
616 // finalize the text that's been edited...
617 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(ui.cur_item->widget));
618 gtk_text_buffer_get_bounds(buffer, &start, &end);
619 ui.cur_item->type = ITEM_TEXT;
620 new_text = gtk_text_buffer_get_text(buffer, &start, &end, TRUE);
621 ui.cur_item_type = ITEM_NONE;
622 gtk_widget_set_sensitive(GET_COMPONENT("editPaste"), TRUE);
623 gtk_widget_set_sensitive(GET_COMPONENT("buttonPaste"), TRUE);
625 if (strlen(new_text)==0) { // erase object and cancel
627 g_signal_handler_disconnect(winMain, ui.resize_signal_handler);
628 gtk_object_destroy(GTK_OBJECT(ui.cur_item->canvas_item));
629 ui.cur_item->canvas_item = NULL;
630 if (ui.cur_item->text == NULL) // nothing happened
631 g_free(ui.cur_item->font_name);
632 else { // treat this as an erasure
634 undo->type = ITEM_ERASURE;
635 undo->layer = ui.cur_layer;
636 erasure = (struct UndoErasureData *)g_malloc(sizeof(struct UndoErasureData));
637 erasure->item = ui.cur_item;
638 erasure->npos = g_list_index(ui.cur_layer->items, ui.cur_item);
640 erasure->replacement_items = NULL;
641 undo->erasurelist = g_list_append(NULL, erasure);
643 ui.cur_layer->items = g_list_remove(ui.cur_layer->items, ui.cur_item);
644 ui.cur_layer->nitems--;
650 if (ui.cur_item->text==NULL || strcmp(ui.cur_item->text, new_text)) {
652 if (ui.cur_item->text == NULL) undo->type = ITEM_TEXT;
653 else undo->type = ITEM_TEXT_EDIT;
654 undo->layer = ui.cur_layer;
655 undo->item = ui.cur_item;
656 undo->str = ui.cur_item->text;
658 else g_free(ui.cur_item->text);
660 ui.cur_item->text = new_text;
661 ui.cur_item->widget = NULL;
662 // replace the canvas item
663 tmpitem = ui.cur_item->canvas_item;
664 make_canvas_item_one(ui.cur_layer->group, ui.cur_item);
665 update_item_bbox(ui.cur_item);
666 lower_canvas_item_to(ui.cur_layer->group, ui.cur_item->canvas_item, tmpitem);
667 gtk_object_destroy(GTK_OBJECT(tmpitem));
670 /* update the items in the canvas so they're of the right font size */
672 void update_text_item_displayfont(struct Item *item)
674 PangoFontDescription *font_desc;
676 if (item->type != ITEM_TEXT && item->type != ITEM_TEMP_TEXT) return;
677 if (item->canvas_item==NULL) return;
678 font_desc = pango_font_description_from_string(item->font_name);
679 pango_font_description_set_absolute_size(font_desc,
680 item->font_size*ui.zoom*PANGO_SCALE);
681 if (item->type == ITEM_TEMP_TEXT)
682 gtk_widget_modify_font(item->widget, font_desc);
684 gnome_canvas_item_set(item->canvas_item, "font-desc", font_desc, NULL);
685 update_item_bbox(item);
687 pango_font_description_free(font_desc);
690 void rescale_text_items(void)
692 GList *pagelist, *layerlist, *itemlist;
694 for (pagelist = journal.pages; pagelist!=NULL; pagelist = pagelist->next)
695 for (layerlist = ((struct Page *)pagelist->data)->layers; layerlist!=NULL; layerlist = layerlist->next)
696 for (itemlist = ((struct Layer *)layerlist->data)->items; itemlist!=NULL; itemlist = itemlist->next)
697 update_text_item_displayfont((struct Item *)itemlist->data);
700 struct Item *click_is_in_text(struct Layer *layer, double x, double y)
703 struct Item *item, *val;
706 for (itemlist = layer->items; itemlist!=NULL; itemlist = itemlist->next) {
707 item = (struct Item *)itemlist->data;
708 if (item->type != ITEM_TEXT) continue;
709 if (x<item->bbox.left || x>item->bbox.right) continue;
710 if (y<item->bbox.top || y>item->bbox.bottom) continue;
716 struct Item *click_is_in_text_or_image(struct Layer *layer, double x, double y)
719 struct Item *item, *val;
722 for (itemlist = layer->items; itemlist!=NULL; itemlist = itemlist->next) {
723 item = (struct Item *)itemlist->data;
724 if (item->type != ITEM_TEXT && item->type != ITEM_IMAGE) continue;
725 if (x<item->bbox.left || x>item->bbox.right) continue;
726 if (y<item->bbox.top || y>item->bbox.bottom) continue;
732 void refont_text_item(struct Item *item, gchar *font_name, double font_size)
734 if (!strcmp(font_name, item->font_name) && font_size==item->font_size) return;
735 if (item->text!=NULL) {
737 undo->type = ITEM_TEXT_ATTRIB;
739 undo->str = item->font_name;
740 undo->val_x = item->font_size;
741 undo->brush = (struct Brush *)g_memdup(&(item->brush), sizeof(struct Brush));
743 else g_free(item->font_name);
744 item->font_name = g_strdup(font_name);
745 if (font_size>0.) item->font_size = font_size;
746 update_text_item_displayfont(item);
749 void process_font_sel(gchar *str)
757 p = strrchr(str, ' ');
759 size = g_strtod(p+1, &q);
760 if (*q!=0 || size<1.) size=0.;
764 g_free(ui.font_name);
766 if (size>0.) ui.font_size = size;
768 // if there's a current text item, re-font it
769 if (ui.cur_item_type == ITEM_TEXT) {
770 refont_text_item(ui.cur_item, str, size);
771 undo_cont = (ui.cur_item->text!=NULL);
773 // if there's a current selection, re-font it
774 if (ui.selection!=NULL)
775 for (list=ui.selection->items; list!=NULL; list=list->next) {
776 it = (struct Item *)list->data;
777 if (it->type == ITEM_TEXT) {
778 if (undo_cont) undo->multiop |= MULTIOP_CONT_REDO;
779 refont_text_item(it, str, size);
780 if (undo_cont) undo->multiop |= MULTIOP_CONT_UNDO;
784 update_font_button();