]> git.donarmstrong.com Git - xournal.git/blobdiff - src/xo-paint.c
Image patch
[xournal.git] / src / xo-paint.c
index 47fd6137b2c074b597677a628929a93789324f5f..33576f8a21cca3fc5ccbef072e631fb5d9f4e361 100644 (file)
@@ -1,3 +1,18 @@
+/*
+ *  This program is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2 of the License, or (at your option) any later version.
+ *
+ *  This software is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
 #ifdef HAVE_CONFIG_H
 #  include <config.h>
 #endif
 #include "xo-misc.h"
 #include "xo-paint.h"
 
+/***** Win32 fix for gdk_cursor_new_from_pixmap() by Dirk Gerrits ****/
+
+#ifdef WIN32
+gboolean colors_too_similar(const GdkColor *colora, const GdkColor *colorb)
+{
+  return (abs(colora->red - colorb->red) < 256 &&
+          abs(colora->green - colorb->green) < 256 &&
+          abs(colora->blue - colorb->blue) < 256);
+}
+
+/* gdk_cursor_new_from_pixmap is broken on Windows.
+   this is a workaround using gdk_cursor_new_from_pixbuf. */
+GdkCursor* fixed_gdk_cursor_new_from_pixmap(GdkPixmap *source, GdkPixmap *mask,
+                                           const GdkColor *fg, const GdkColor *bg,
+                                           gint x, gint y)
+{
+  GdkPixmap *rgb_pixmap;
+  GdkGC *gc;
+  GdkPixbuf *rgb_pixbuf, *rgba_pixbuf;
+  GdkCursor *cursor;
+  int width, height;
+
+  /* HACK!  It seems impossible to work with RGBA pixmaps directly in
+     GDK-Win32.  Instead we pick some third color, different from fg
+     and bg, and use that as the 'transparent color'.  We do this using
+     colors_too_similar (see above) because two colors could be
+     unequal in GdkColor's 16-bit/sample, but equal in GdkPixbuf's
+     8-bit/sample. */
+  GdkColor candidates[3] = {{0,65535,0,0}, {0,0,65535,0}, {0,0,0,65535}};
+  GdkColor *trans = &candidates[0];
+  if (colors_too_similar(trans, fg) || colors_too_similar(trans, bg)) {
+    trans = &candidates[1];
+    if (colors_too_similar(trans, fg) || colors_too_similar(trans, bg)) {
+      trans = &candidates[2];
+    }
+  } /* trans is now guaranteed to be unique from fg and bg */
+
+  /* create an empty pixmap to hold the cursor image */
+  gdk_drawable_get_size(source, &width, &height);
+  rgb_pixmap = gdk_pixmap_new(NULL, width, height, 24);
+
+  /* blit the bitmaps defining the cursor onto a transparent background */
+  gc = gdk_gc_new(rgb_pixmap);
+  gdk_gc_set_fill(gc, GDK_SOLID);
+  gdk_gc_set_rgb_fg_color(gc, trans);
+  gdk_draw_rectangle(rgb_pixmap, gc, TRUE, 0, 0, width, height);
+  gdk_gc_set_fill(gc, GDK_OPAQUE_STIPPLED);
+  gdk_gc_set_stipple(gc, source);
+  gdk_gc_set_clip_mask(gc, mask);
+  gdk_gc_set_rgb_fg_color(gc, fg);
+  gdk_gc_set_rgb_bg_color(gc, bg);
+  gdk_draw_rectangle(rgb_pixmap, gc, TRUE, 0, 0, width, height);
+  gdk_gc_unref(gc);
+
+  /* create a cursor out of the created pixmap */
+  rgb_pixbuf = gdk_pixbuf_get_from_drawable(
+    NULL, rgb_pixmap, gdk_colormap_get_system(), 0, 0, 0, 0, width, height);
+  gdk_pixmap_unref(rgb_pixmap);
+  rgba_pixbuf = gdk_pixbuf_add_alpha(
+    rgb_pixbuf, TRUE, trans->red, trans->green, trans->blue);
+  gdk_pixbuf_unref(rgb_pixbuf);
+  cursor = gdk_cursor_new_from_pixbuf(gdk_display_get_default(), rgba_pixbuf, x, y);
+  gdk_pixbuf_unref(rgba_pixbuf);
+
+  return cursor;
+}
+#define gdk_cursor_new_from_pixmap fixed_gdk_cursor_new_from_pixmap
+#endif
+
+
 /************** drawing nice cursors *********/
 
 static char cursor_pen_bits[] = {
@@ -879,209 +964,9 @@ void selection_delete(void)
      the forward direction */
 }
 
-void callback_clipboard_get(GtkClipboard *clipboard,
-                            GtkSelectionData *selection_data,
-                            guint info, gpointer user_data)
-{
-  int length;
-  
-  g_memmove(&length, user_data, sizeof(int));
-  gtk_selection_data_set(selection_data,
-     gdk_atom_intern("_XOURNAL", FALSE), 8, user_data, length);
-}
-
-void callback_clipboard_clear(GtkClipboard *clipboard, gpointer user_data)
-{
-  g_free(user_data);
-}
-
-void selection_to_clip(void)
-{
-  int bufsz, nitems, val;
-  char *buf, *p;
-  GList *list;
-  struct Item *item;
-  GtkTargetEntry target;
-  
-  if (ui.selection == NULL) return;
-  bufsz = 2*sizeof(int) // bufsz, nitems
-        + sizeof(struct BBox); // bbox
-  nitems = 0;
-  for (list = ui.selection->items; list != NULL; list = list->next) {
-    item = (struct Item *)list->data;
-    nitems++;
-    if (item->type == ITEM_STROKE) {
-      bufsz+= sizeof(int) // type
-            + sizeof(struct Brush) // brush
-            + sizeof(int) // num_points
-            + 2*item->path->num_points*sizeof(double); // the points
-      if (item->brush.variable_width)
-        bufsz += (item->path->num_points-1)*sizeof(double); // the widths
-    }
-    else if (item->type == ITEM_TEXT) {
-      bufsz+= sizeof(int) // type
-            + sizeof(struct Brush) // brush
-            + 2*sizeof(double) // bbox upper-left
-            + sizeof(int) // text len
-            + strlen(item->text)+1 // text
-            + sizeof(int) // font_name len
-            + strlen(item->font_name)+1 // font_name
-            + sizeof(double); // font_size
-    }
-    else bufsz+= sizeof(int); // type
-  }
-  p = buf = g_malloc(bufsz);
-  g_memmove(p, &bufsz, sizeof(int)); p+= sizeof(int);
-  g_memmove(p, &nitems, sizeof(int)); p+= sizeof(int);
-  g_memmove(p, &ui.selection->bbox, sizeof(struct BBox)); p+= sizeof(struct BBox);
-  for (list = ui.selection->items; list != NULL; list = list->next) {
-    item = (struct Item *)list->data;
-    g_memmove(p, &item->type, sizeof(int)); p+= sizeof(int);
-    if (item->type == ITEM_STROKE) {
-      g_memmove(p, &item->brush, sizeof(struct Brush)); p+= sizeof(struct Brush);
-      g_memmove(p, &item->path->num_points, sizeof(int)); p+= sizeof(int);
-      g_memmove(p, item->path->coords, 2*item->path->num_points*sizeof(double));
-      p+= 2*item->path->num_points*sizeof(double);
-      if (item->brush.variable_width) {
-        g_memmove(p, item->widths, (item->path->num_points-1)*sizeof(double));
-        p+= (item->path->num_points-1)*sizeof(double);
-      }
-    }
-    if (item->type == ITEM_TEXT) {
-      g_memmove(p, &item->brush, sizeof(struct Brush)); p+= sizeof(struct Brush);
-      g_memmove(p, &item->bbox.left, sizeof(double)); p+= sizeof(double);
-      g_memmove(p, &item->bbox.top, sizeof(double)); p+= sizeof(double);
-      val = strlen(item->text);
-      g_memmove(p, &val, sizeof(int)); p+= sizeof(int);
-      g_memmove(p, item->text, val+1); p+= val+1;
-      val = strlen(item->font_name);
-      g_memmove(p, &val, sizeof(int)); p+= sizeof(int);
-      g_memmove(p, item->font_name, val+1); p+= val+1;
-      g_memmove(p, &item->font_size, sizeof(double)); p+= sizeof(double);
-    }
-  }
-  
-  target.target = "_XOURNAL";
-  target.flags = 0;
-  target.info = 0;
-  
-  gtk_clipboard_set_with_data(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), 
-       &target, 1,
-       callback_clipboard_get, callback_clipboard_clear, buf);
-}
-
-
-void clipboard_paste(void)
-{
-  GtkSelectionData *sel_data;
-  unsigned char *p;
-  int nitems, npts, i, len;
-  struct Item *item;
-  double hoffset, voffset, cx, cy;
-  double *pf;
-  int sx, sy, wx, wy;
-  
-  if (ui.cur_layer == NULL) return;
-  
-  ui.cur_item_type = ITEM_PASTE;
-  sel_data = gtk_clipboard_wait_for_contents(
-      gtk_clipboard_get(GDK_SELECTION_CLIPBOARD),
-      gdk_atom_intern("_XOURNAL", FALSE));
-  ui.cur_item_type = ITEM_NONE;
-  if (sel_data == NULL) return; // paste failed
-  
-  reset_selection();
-  
-  ui.selection = g_new(struct Selection, 1);
-  p = sel_data->data + sizeof(int);
-  g_memmove(&nitems, p, sizeof(int)); p+= sizeof(int);
-  ui.selection->type = ITEM_SELECTRECT;
-  ui.selection->layer = ui.cur_layer;
-  g_memmove(&ui.selection->bbox, p, sizeof(struct BBox)); p+= sizeof(struct BBox);
-  ui.selection->items = NULL;
-  
-  // find by how much we translate the pasted selection
-  gnome_canvas_get_scroll_offsets(canvas, &sx, &sy);
-  gdk_window_get_geometry(GTK_WIDGET(canvas)->window, NULL, NULL, &wx, &wy, NULL);
-  gnome_canvas_window_to_world(canvas, sx + wx/2, sy + wy/2, &cx, &cy);
-  cx -= ui.cur_page->hoffset;
-  cy -= ui.cur_page->voffset;
-  if (cx + (ui.selection->bbox.right-ui.selection->bbox.left)/2 > ui.cur_page->width)
-    cx = ui.cur_page->width - (ui.selection->bbox.right-ui.selection->bbox.left)/2;
-  if (cx - (ui.selection->bbox.right-ui.selection->bbox.left)/2 < 0)
-    cx = (ui.selection->bbox.right-ui.selection->bbox.left)/2;
-  if (cy + (ui.selection->bbox.bottom-ui.selection->bbox.top)/2 > ui.cur_page->height)
-    cy = ui.cur_page->height - (ui.selection->bbox.bottom-ui.selection->bbox.top)/2;
-  if (cy - (ui.selection->bbox.bottom-ui.selection->bbox.top)/2 < 0)
-    cy = (ui.selection->bbox.bottom-ui.selection->bbox.top)/2;
-  hoffset = cx - (ui.selection->bbox.right+ui.selection->bbox.left)/2;
-  voffset = cy - (ui.selection->bbox.top+ui.selection->bbox.bottom)/2;
-  ui.selection->bbox.left += hoffset;
-  ui.selection->bbox.right += hoffset;
-  ui.selection->bbox.top += voffset;
-  ui.selection->bbox.bottom += voffset;
-
-  ui.selection->canvas_item = gnome_canvas_item_new(ui.cur_layer->group,
-      gnome_canvas_rect_get_type(), "width-pixels", 1,
-      "outline-color-rgba", 0x000000ff,
-      "fill-color-rgba", 0x80808040,
-      "x1", ui.selection->bbox.left, "x2", ui.selection->bbox.right, 
-      "y1", ui.selection->bbox.top, "y2", ui.selection->bbox.bottom, NULL);
-  make_dashed(ui.selection->canvas_item);
-
-  while (nitems-- > 0) {
-    item = g_new(struct Item, 1);
-    ui.selection->items = g_list_append(ui.selection->items, item);
-    ui.cur_layer->items = g_list_append(ui.cur_layer->items, item);
-    ui.cur_layer->nitems++;
-    g_memmove(&item->type, p, sizeof(int)); p+= sizeof(int);
-    if (item->type == ITEM_STROKE) {
-      g_memmove(&item->brush, p, sizeof(struct Brush)); p+= sizeof(struct Brush);
-      g_memmove(&npts, p, sizeof(int)); p+= sizeof(int);
-      item->path = gnome_canvas_points_new(npts);
-      pf = (double *)p;
-      for (i=0; i<npts; i++) {
-        item->path->coords[2*i] = pf[2*i] + hoffset;
-        item->path->coords[2*i+1] = pf[2*i+1] + voffset;
-      }
-      p+= 2*item->path->num_points*sizeof(double);
-      if (item->brush.variable_width) {
-        item->widths = g_memdup(p, (item->path->num_points-1)*sizeof(double));
-        p+= (item->path->num_points-1)*sizeof(double);
-      }
-      else item->widths = NULL;
-      update_item_bbox(item);
-      make_canvas_item_one(ui.cur_layer->group, item);
-    }
-    if (item->type == ITEM_TEXT) {
-      g_memmove(&item->brush, p, sizeof(struct Brush)); p+= sizeof(struct Brush);
-      g_memmove(&item->bbox.left, p, sizeof(double)); p+= sizeof(double);
-      g_memmove(&item->bbox.top, p, sizeof(double)); p+= sizeof(double);
-      item->bbox.left += hoffset;
-      item->bbox.top += voffset;
-      g_memmove(&len, p, sizeof(int)); p+= sizeof(int);
-      item->text = g_malloc(len+1);
-      g_memmove(item->text, p, len+1); p+= len+1;
-      g_memmove(&len, p, sizeof(int)); p+= sizeof(int);
-      item->font_name = g_malloc(len+1);
-      g_memmove(item->font_name, p, len+1); p+= len+1;
-      g_memmove(&item->font_size, p, sizeof(double)); p+= sizeof(double);
-      make_canvas_item_one(ui.cur_layer->group, item);
-    }
-  }
-
-  prepare_new_undo();
-  undo->type = ITEM_PASTE;
-  undo->layer = ui.cur_layer;
-  undo->itemlist = g_list_copy(ui.selection->items);  
-  
-  gtk_selection_data_free(sel_data);
-  update_copy_paste_enabled();
-}
-
 // modify the color or thickness of pen strokes in a selection
 
-void recolor_selection(int color)
+void recolor_selection(int color_no, guint color_rgba)
 {
   GList *itemlist;
   struct Item *item;
@@ -1103,8 +988,8 @@ void recolor_selection(int color)
     g_memmove(brush, &(item->brush), sizeof(struct Brush));
     undo->auxlist = g_list_append(undo->auxlist, brush);
     // repaint the stroke
-    item->brush.color_no = color;
-    item->brush.color_rgba = predef_colors_rgba[color];
+    item->brush.color_no = color_no;
+    item->brush.color_rgba = color_rgba | 0xff; // no alpha
     if (item->canvas_item!=NULL) {
       if (!item->brush.variable_width)
         gnome_canvas_item_set(item->canvas_item, 
@@ -1206,8 +1091,9 @@ void start_text(GdkEvent *event, struct Item *item)
   GnomeCanvasItem *canvas_item;
   PangoFontDescription *font_desc;
   GdkColor color;
-  
+
   get_pointer_coords(event, pt);
+
   ui.cur_item_type = ITEM_TEXT;
 
   if (item==NULL) {
@@ -1254,13 +1140,13 @@ void start_text(GdkEvent *event, struct Item *item)
   item->canvas_item = canvas_item;
 
   gtk_widget_show(item->widget);
-  gtk_widget_grab_focus(item->widget);
   ui.resize_signal_handler = 
     g_signal_connect((gpointer) winMain, "check_resize",
        G_CALLBACK(resize_textview), NULL);
   update_font_button();
   gtk_widget_set_sensitive(GET_COMPONENT("editPaste"), FALSE);
   gtk_widget_set_sensitive(GET_COMPONENT("buttonPaste"), FALSE);
+  gtk_widget_grab_focus(item->widget); 
 }
 
 void end_text(void)
@@ -1272,7 +1158,7 @@ void end_text(void)
   GnomeCanvasItem *tmpitem;
 
   if (ui.cur_item_type!=ITEM_TEXT) return; // nothing for us to do!
-  
+
   // finalize the text that's been edited... 
   buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(ui.cur_item->widget));
   gtk_text_buffer_get_bounds(buffer, &start, &end);
@@ -1405,7 +1291,6 @@ void process_font_sel(gchar *str)
     else *p=0;
   }
   else size=0.;
-  reset_focus();
   g_free(ui.font_name);
   ui.font_name = str;  
   if (size>0.) ui.font_size = size;