]> git.donarmstrong.com Git - xournal.git/blob - src/xo-paint.c
generate cursors from pixbufs (fixes a Win32 bug)
[xournal.git] / src / xo-paint.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 #include <libgnomecanvas/libgnomecanvas.h>
24
25 #include "xournal.h"
26 #include "xo-callbacks.h"
27 #include "xo-interface.h"
28 #include "xo-support.h"
29 #include "xo-misc.h"
30 #include "xo-paint.h"
31
32 /************** drawing nice cursors *********/
33
34 void set_cursor_busy(gboolean busy)
35 {
36   GdkCursor *cursor;
37   
38   if (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);
43   }
44   else {
45     gdk_window_set_cursor(GTK_WIDGET(winMain)->window, NULL);
46     update_cursor();
47   }
48   gdk_display_sync(gdk_display_get_default());
49 }
50
51 #define PEN_CURSOR_RADIUS 1
52 #define HILITER_CURSOR_RADIUS 3
53 #define HILITER_BORDER_RADIUS 4
54
55 GdkCursor *make_pen_cursor(guint color_rgba)
56 {
57   int rowstride, x, y;
58   guchar col[4], *pixels;
59   
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
64   }
65   rowstride = gdk_pixbuf_get_rowstride(ui.pen_cursor_pix);
66   pixels = gdk_pixbuf_get_pixels(ui.pen_cursor_pix);
67
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);
75   
76   return gdk_cursor_new_from_pixbuf(gdk_display_get_default(), ui.pen_cursor_pix, 7, 7);
77 }
78
79 GdkCursor *make_hiliter_cursor(guint color_rgba)
80 {
81   int rowstride, x, y;
82   guchar col[4], *pixels;
83   
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
88   }
89   rowstride = gdk_pixbuf_get_rowstride(ui.hiliter_cursor_pix);
90   pixels = gdk_pixbuf_get_pixels(ui.hiliter_cursor_pix);
91
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);
97
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);
105   
106   return gdk_cursor_new_from_pixbuf(gdk_display_get_default(), ui.hiliter_cursor_pix, 7, 7);
107 }
108
109 void update_cursor(void)
110 {
111   GdkPixmap *source, *mask;
112   GdkColor fg = {0, 0, 0, 0}, bg = {0, 65535, 65535, 65535};
113
114   ui.is_sel_cursor = FALSE;
115   if (GTK_WIDGET(canvas)->window == NULL) return;
116   
117   if (ui.cursor!=NULL) { 
118     gdk_cursor_unref(ui.cursor);
119     ui.cursor = NULL;
120   }
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);
127   }
128   else if (ui.toolno[ui.cur_mapping] == TOOL_ERASER) {
129     ui.cursor = make_hiliter_cursor(0xffffffff);
130   }
131   else if (ui.toolno[ui.cur_mapping] == TOOL_HIGHLIGHTER) {
132     ui.cursor = make_hiliter_cursor(ui.cur_brush->color_rgba);
133   }
134   else if (ui.cur_item_type == ITEM_SELECTRECT) {
135     ui.cursor = gdk_cursor_new(GDK_TCROSS);
136   }
137   else if (ui.toolno[ui.cur_mapping] == TOOL_HAND) {
138     ui.cursor = gdk_cursor_new(GDK_HAND1);
139   }
140   else if (ui.toolno[ui.cur_mapping] == TOOL_TEXT) {
141     ui.cursor = gdk_cursor_new(GDK_XTERM);
142   }
143   
144   gdk_window_set_cursor(GTK_WIDGET(canvas)->window, ui.cursor);
145 }
146
147 /* adjust the cursor shape if it hovers near a selection box */
148
149 void update_cursor_for_resize(double *pt)
150 {
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;
155
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)
160   {
161     if (ui.is_sel_cursor) update_cursor();
162     return;
163   }
164
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);
170
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;
175   }
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;
180   }
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;
184
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);
189 }
190
191 /************** painting strokes *************/
192
193 #define SUBDIVIDE_MAXDIST 5.0
194
195 void subdivide_cur_path(null)
196 {
197   int n, pieces, k;
198   double *p;
199   double x0, y0, x1, y1;
200
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);
203     if (pieces>1) {
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;
211       n += (pieces-1);
212       for (k=1; k<pieces; k++) {
213         p+=2;
214         p[0] = x0 + k*(x1-x0)/pieces;
215         p[1] = y0 + k*(y1-y0)/pieces;
216       } 
217     }
218   }
219 }
220
221 void create_new_stroke(GdkEvent *event)
222 {
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;
228   realloc_cur_path(2);
229   ui.cur_path.num_points = 1;
230   get_pointer_coords(event, ui.cur_path.coords);
231   
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;
239   } else
240     ui.cur_item->canvas_item = gnome_canvas_item_new(
241       ui.cur_layer->group, gnome_canvas_group_get_type(), NULL);
242 }
243
244 void continue_stroke(GdkEvent *event)
245 {
246   GnomeCanvasPoints seg;
247   double *pt, current_width;
248
249   if (ui.cur_brush->ruler) {
250     pt = ui.cur_path.coords;
251   } else {
252     realloc_cur_path(ui.cur_path.num_points+1);
253     pt = ui.cur_path.coords + 2*(ui.cur_path.num_points-1);
254   } 
255   
256   get_pointer_coords(event, pt+2);
257   
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;
262   }
263   else current_width = ui.cur_item->brush.thickness;
264   
265   if (ui.cur_brush->ruler)
266     ui.cur_path.num_points = 2;
267   else {
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++;
271   }
272
273   seg.coords = pt; 
274   seg.num_points = 2;
275   seg.ref_count = 1;
276   
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 */
280
281   if (ui.cur_brush->ruler)
282     gnome_canvas_item_set(ui.cur_item->canvas_item, "points", &seg, NULL);
283   else
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);
289 }
290
291 void finalize_stroke(void)
292 {
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;
298   }
299   
300   if (!ui.cur_item->brush.variable_width)
301     subdivide_cur_path(); // split the segment so eraser will work
302
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;
312
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);
318   }
319
320   // add undo information
321   prepare_new_undo();
322   undo->type = ITEM_STROKE;
323   undo->item = ui.cur_item;
324   undo->layer = ui.cur_layer;
325
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++;
329   ui.cur_item = NULL;
330   ui.cur_item_type = ITEM_NONE;
331 }
332
333 /************** eraser tool *************/
334
335 void erase_stroke_portions(struct Item *item, double x, double y, double radius,
336                    gboolean whole_strokes, struct UndoErasureData *erasure)
337 {
338   int i;
339   double *pt;
340   struct Item *newhead, *newtail;
341   gboolean need_recalc = FALSE;
342
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);
355         erasure->nrepl = 0;
356         erasure->replacement_items = NULL;
357       }
358       // split the stroke
359       newhead = newtail = NULL;
360       if (!whole_strokes) {
361         if (i>=2) {
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;
370         }
371         while (++i < item->path->num_points) {
372           pt+=2;
373           if (hypot(pt[0]-x, pt[1]-y) > radius) break;
374         }
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;
387         }
388       }
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));
395         erasure->nrepl--;
396         erasure->replacement_items = g_list_remove(erasure->replacement_items, item);
397         g_free(item);
398       }
399       // add the new head
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);
406         erasure->nrepl++;
407         // prepending ensures it won't get processed twice
408       }
409       // recurse into the new tail
410       need_recalc = (newtail!=NULL);
411       if (newtail == NULL) break;
412       item = newtail;
413       erasure->replacement_items = g_list_prepend(erasure->replacement_items, newtail);
414       erasure->nrepl++;
415       i=0; pt=item->path->coords;
416     }
417   }
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);
424 }
425
426
427 void do_eraser(GdkEvent *event, double radius, gboolean whole_strokes)
428 {
429   struct Item *item, *repl;
430   GList *itemlist, *repllist;
431   double pos[2];
432   struct BBox eraserbox;
433   
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);
452       }
453     }
454   }
455 }
456
457 void finalize_erasure(void)
458 {
459   GList *itemlist, *partlist;
460   struct Item *item;
461   
462   prepare_new_undo();
463   undo->type = ITEM_ERASURE;
464   undo->layer = ui.cur_layer;
465   undo->erasurelist = NULL;
466   
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;
478     }
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;
485   }
486     
487   ui.cur_item = NULL;
488   ui.cur_item_type = ITEM_NONE;
489   
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 */
494 }
495
496
497 gboolean do_hand_scrollto(gpointer data)
498 {
499   ui.hand_scrollto_pending = FALSE;
500   gnome_canvas_scroll_to(canvas, ui.hand_scrollto_cx, ui.hand_scrollto_cy);
501   return FALSE;
502 }
503
504 void do_hand(GdkEvent *event)
505 {
506   double pt[2];
507   int cx, cy;
508   
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;
517 }
518
519 /************ TEXT FUNCTIONS **************/
520
521 // to make it easier to copy/paste at end of text box
522 #define WIDGET_RIGHT_MARGIN 10
523
524 void resize_textview(gpointer *toplevel, gpointer *data)
525 {
526   GtkTextView *w;
527   int width, height;
528   
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;
533   height = w->height;
534   gnome_canvas_item_set(ui.cur_item->canvas_item, 
535     "size-pixels", TRUE, 
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;
539 }
540
541 void start_text(GdkEvent *event, struct Item *item)
542 {
543   double pt[2];
544   GtkTextBuffer *buffer;
545   GnomeCanvasItem *canvas_item;
546   PangoFontDescription *font_desc;
547   GdkColor color;
548
549   get_pointer_coords(event, pt);
550
551   ui.cur_item_type = ITEM_TEXT;
552
553   if (item==NULL) {
554     item = g_new(struct Item, 1);
555     item->text = NULL;
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++;
566   }
567   
568   item->type = ITEM_TEMP_TEXT;
569   ui.cur_item = item;
570   
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);
582
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));
593   }
594   item->canvas_item = canvas_item;
595
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); 
604 }
605
606 void end_text(void)
607 {
608   GtkTextBuffer *buffer;
609   GtkTextIter start, end;
610   gchar *new_text;
611   struct UndoErasureData *erasure;
612   GnomeCanvasItem *tmpitem;
613
614   if (ui.cur_item_type!=ITEM_TEXT) return; // nothing for us to do!
615
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);
624   
625   if (strlen(new_text)==0) { // erase object and cancel
626     g_free(new_text);
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
633       prepare_new_undo();
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);
639       erasure->nrepl = 0;
640       erasure->replacement_items = NULL;
641       undo->erasurelist = g_list_append(NULL, erasure);
642     }
643     ui.cur_layer->items = g_list_remove(ui.cur_layer->items, ui.cur_item);
644     ui.cur_layer->nitems--;
645     ui.cur_item = NULL;
646     return;
647   }
648
649   // store undo data
650   if (ui.cur_item->text==NULL || strcmp(ui.cur_item->text, new_text)) {
651     prepare_new_undo();
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;
657   }
658   else g_free(ui.cur_item->text);
659
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));
668 }
669
670 /* update the items in the canvas so they're of the right font size */
671
672 void update_text_item_displayfont(struct Item *item)
673 {
674   PangoFontDescription *font_desc;
675
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);
683   else {
684     gnome_canvas_item_set(item->canvas_item, "font-desc", font_desc, NULL);
685     update_item_bbox(item);
686   }
687   pango_font_description_free(font_desc);
688 }
689
690 void rescale_text_items(void)
691 {
692   GList *pagelist, *layerlist, *itemlist;
693   
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);
698 }
699
700 struct Item *click_is_in_text(struct Layer *layer, double x, double y)
701 {
702   GList *itemlist;
703   struct Item *item, *val;
704   
705   val = NULL;
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;
711     val = item;
712   }
713   return val;
714 }
715
716 struct Item *click_is_in_text_or_image(struct Layer *layer, double x, double y)
717 {
718   GList *itemlist;
719   struct Item *item, *val;
720   
721   val = NULL;
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;
727     val = item;
728   }
729   return val;
730 }
731
732 void refont_text_item(struct Item *item, gchar *font_name, double font_size)
733 {
734   if (!strcmp(font_name, item->font_name) && font_size==item->font_size) return;
735   if (item->text!=NULL) {
736     prepare_new_undo();
737     undo->type = ITEM_TEXT_ATTRIB;
738     undo->item = item;
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));
742   }
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);
747 }
748
749 void process_font_sel(gchar *str)
750 {
751   gchar *p, *q;
752   struct Item *it;
753   gdouble size;
754   GList *list;
755   gboolean undo_cont;
756
757   p = strrchr(str, ' ');
758   if (p!=NULL) { 
759     size = g_strtod(p+1, &q);
760     if (*q!=0 || size<1.) size=0.;
761     else *p=0;
762   }
763   else size=0.;
764   g_free(ui.font_name);
765   ui.font_name = str;  
766   if (size>0.) ui.font_size = size;
767   undo_cont = FALSE;   
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);   
772   }
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;
781         undo_cont = TRUE;
782       }
783     }  
784   update_font_button();
785 }