From 2c07b0df01c7b8ca9e9d5ea1730b09ca3cf801de Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 11 Jan 2014 22:35:56 +0000 Subject: Merge GanvCanvasBase and GanvCanvas. git-svn-id: http://svn.drobilla.net/lad/trunk/ganv@5302 a436a847-0d15-0410-975c-d299462d15a1 --- ganv/canvas-base.h | 213 ------- ganv/canvas.h | 170 ++++- ganv/group.h | 4 + ganv/item.h | 56 +- src/Canvas.cpp | 1764 ++++++++++++++++++++++++++++++++++++++++++++++++++-- src/box.c | 12 +- src/canvas-base.c | 1728 -------------------------------------------------- src/circle.c | 9 +- src/edge.c | 31 +- src/ganv-private.h | 14 +- src/group.c | 2 +- src/item.c | 129 ++-- src/node.c | 5 +- src/text.c | 3 +- src/widget.c | 6 +- wscript | 1 - 16 files changed, 1988 insertions(+), 2159 deletions(-) delete mode 100644 ganv/canvas-base.h delete mode 100644 src/canvas-base.c diff --git a/ganv/canvas-base.h b/ganv/canvas-base.h deleted file mode 100644 index 8299132..0000000 --- a/ganv/canvas-base.h +++ /dev/null @@ -1,213 +0,0 @@ -/* This file is part of Ganv. - * Copyright 2007-2012 David Robillard - * - * Ganv 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 3 of the License, or any later version. - * - * Ganv 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 details. - * - * You should have received a copy of the GNU General Public License along - * with Ganv. If not, see . - */ - -/* Based on GnomeCanvas, by Federico Mena - * and Raph Levien - * Copyright 1997-2000 Free Software Foundation - */ - -#ifndef GANV_CANVAS_BASE_H -#define GANV_CANVAS_BASE_H - -#include - -#include -#include - -#include "ganv/item.h" - -G_BEGIN_DECLS - -typedef struct _GanvCanvasBase GanvCanvasBase; -typedef struct _GanvCanvasBaseClass GanvCanvasBaseClass; - -/* Update flags for items */ -enum { - GANV_CANVAS_BASE_UPDATE_REQUESTED = 1 << 0, - GANV_CANVAS_BASE_UPDATE_AFFINE = 1 << 1, - GANV_CANVAS_BASE_UPDATE_VISIBILITY = 1 << 2, -}; - -#define GANV_TYPE_CANVAS_BASE (ganv_canvas_base_get_type()) -#define GANV_CANVAS_BASE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GANV_TYPE_CANVAS_BASE, GanvCanvasBase)) -#define GANV_CANVAS_BASE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GANV_TYPE_CANVAS_BASE, GanvCanvasBaseClass)) -#define GANV_IS_CANVAS_BASE(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GANV_TYPE_CANVAS_BASE)) -#define GANV_IS_CANVAS_BASE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GANV_TYPE_CANVAS_BASE)) -#define GANV_CANVAS_BASE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GANV_TYPE_CANVAS_BASE, GanvCanvasBaseClass)) - -struct _GanvCanvasBase { - GtkLayout layout; - - /* Root canvas item */ - GanvItem* root; - - /* Region that needs redrawing (list of rectangles) */ - GSList* redraw_region; - - /* The item containing the mouse pointer, or NULL if none */ - GanvItem* current_item; - - /* Item that is about to become current (used to track deletions and such) */ - GanvItem* new_current_item; - - /* Item that holds a pointer grab, or NULL if none */ - GanvItem* grabbed_item; - - /* If non-NULL, the currently focused item */ - GanvItem* focused_item; - - /* GC for temporary draw pixmap */ - GdkGC* pixmap_gc; - - /* Event on which selection of current item is based */ - GdkEvent pick_event; - - /* Scrolling region */ - double scroll_x1, scroll_y1; - double scroll_x2, scroll_y2; - - /* Scaling factor to be used for display */ - double pixels_per_unit; - - /* Idle handler ID */ - guint idle_id; - - /* Signal handler ID for destruction of the root item */ - guint root_destroy_id; - - /* Area that is being redrawn. Contains (x1, y1) but not (x2, y2). - * Specified in canvas pixel coordinates. - */ - int redraw_x1, redraw_y1; - int redraw_x2, redraw_y2; - - /* Offsets of the temprary drawing pixmap */ - int draw_xofs, draw_yofs; - - /* Internal pixel offsets when zoomed out */ - int zoom_xofs, zoom_yofs; - - /* Last known modifier state, for deferred repick when a button is down */ - int state; - - /* Event mask specified when grabbing an item */ - guint grabbed_event_mask; - - /* Tolerance distance for picking items */ - int close_enough; - - /* Whether the canvas should center the scroll region in the middle of - * the window if the scroll region is smaller than the window. - */ - unsigned int center_scroll_region : 1; - - /* Whether items need update at next idle loop iteration */ - unsigned int need_update : 1; - - /* Whether the canvas needs redrawing at the next idle loop iteration */ - unsigned int need_redraw : 1; - - /* Whether current item will be repicked at next idle loop iteration */ - unsigned int need_repick : 1; - - /* For use by internal pick_current_item() function */ - unsigned int left_grabbed_item : 1; - - /* For use by internal pick_current_item() function */ - unsigned int in_repick : 1; -}; - -struct _GanvCanvasBaseClass { - GtkLayoutClass parent_class; - - /* Private Virtual methods for groping the canvas inside bonobo */ - void (* request_update)(GanvCanvasBase* canvas); - - /* Reserved for future expansion */ - gpointer spare_vmethods [4]; -}; - -GType ganv_canvas_base_get_type(void) G_GNUC_CONST; - -/* Creates a new canvas. You should check that the canvas is created with the - * proper visual and colormap. Any visual will do unless you intend to insert - * gdk_imlib images into it, in which case you should use the gdk_imlib visual. - * - * You should call ganv_canvas_base_set_scroll_region() soon after calling this - * function to set the desired scrolling limits for the canvas. - */ -GtkWidget* ganv_canvas_base_new(void); - -/* Returns the root item of the canvas */ -GanvItem* ganv_canvas_base_root(GanvCanvasBase* canvas); - -/* Sets the limits of the scrolling region, in world coordinates */ -void ganv_canvas_base_set_scroll_region(GanvCanvasBase* canvas, - double x1, double y1, double x2, double y2); - -/* Gets the limits of the scrolling region, in world coordinates */ -void ganv_canvas_base_get_scroll_region(GanvCanvasBase* canvas, - double* x1, double* y1, double* x2, double* y2); - -/* Whether the canvas centers the scroll region if it is smaller than the window */ -void ganv_canvas_base_set_center_scroll_region(GanvCanvasBase* canvas, gboolean center_scroll_region); - -/* Returns whether the canvas is set to center the scroll region if it is smaller than the window */ -gboolean ganv_canvas_base_get_center_scroll_region(GanvCanvasBase* canvas); - -/* Sets the number of pixels that correspond to one unit in world coordinates */ -void ganv_canvas_base_set_pixels_per_unit(GanvCanvasBase* canvas, double n); - -/* Scrolls the canvas to the specified offsets, given in canvas pixel coordinates */ -void ganv_canvas_base_scroll_to(GanvCanvasBase* canvas, int cx, int cy); - -/* Returns the scroll offsets of the canvas in canvas pixel coordinates. You - * can specify NULL for any of the values, in which case that value will not be - * queried. - */ -void ganv_canvas_base_get_scroll_offsets(GanvCanvasBase* canvas, int* cx, int* cy); - -/* Returns the item that is at the specified position in world coordinates, or - * NULL if no item is there. - */ -GanvItem* ganv_canvas_base_get_item_at(GanvCanvasBase* canvas, double x, double y); - -/* Gets the affine transform that converts world coordinates into canvas pixel - * coordinates. - */ -void ganv_canvas_base_w2c_affine(GanvCanvasBase* canvas, cairo_matrix_t* matrix); - -/* These functions convert from a coordinate system to another. "w" is world - * coordinates, "c" is canvas pixel coordinates (pixel coordinates that are - * (0,0) for the upper-left scrolling limit and something else for the - * lower-left scrolling limit). - */ -void ganv_canvas_base_w2c(GanvCanvasBase* canvas, double wx, double wy, int* cx, int* cy); -void ganv_canvas_base_w2c_d(GanvCanvasBase* canvas, double wx, double wy, double* cx, double* cy); -void ganv_canvas_base_c2w(GanvCanvasBase* canvas, int cx, int cy, double* wx, double* wy); - -/* This function takes in coordinates relative to the GTK_LAYOUT - * (canvas)->bin_window and converts them to world coordinates. - */ -void ganv_canvas_base_window_to_world(GanvCanvasBase* canvas, - double winx, double winy, double* worldx, double* worldy); - -/* This is the inverse of ganv_canvas_base_window_to_world() */ -void ganv_canvas_base_world_to_window(GanvCanvasBase* canvas, - double worldx, double worldy, double* winx, double* winy); - -G_END_DECLS - -#endif diff --git a/ganv/canvas.h b/ganv/canvas.h index 37fdc5d..d0a1eeb 100644 --- a/ganv/canvas.h +++ b/ganv/canvas.h @@ -16,9 +16,15 @@ #ifndef GANV_CANVAS_H #define GANV_CANVAS_H -#include "ganv/canvas-base.h" +#include + +#include +#include + +#include "ganv/canvas.h" #include "ganv/types.h" #include "ganv/edge.h" +#include "ganv/item.h" G_BEGIN_DECLS @@ -47,43 +53,159 @@ typedef enum { } GanvDirection; struct _GanvCanvas { - GanvCanvasBase canvas; + GtkLayout layout; + struct GanvCanvasImpl* impl; - GanvDirection direction; - double width; - double height; - gboolean locked; + + /* Root canvas item */ + GanvItem* root; + + /* Region that needs redrawing (list of rectangles) */ + GSList* redraw_region; + + /* The item containing the mouse pointer, or NULL if none */ + GanvItem* current_item; + + /* Item that is about to become current (used to track deletions and such) */ + GanvItem* new_current_item; + + /* Item that holds a pointer grab, or NULL if none */ + GanvItem* grabbed_item; + + /* If non-NULL, the currently focused item */ + GanvItem* focused_item; + + /* GC for temporary draw pixmap */ + GdkGC* pixmap_gc; + + /* Event on which selection of current item is based */ + GdkEvent pick_event; + + /* Scrolling region */ + double scroll_x1, scroll_y1; + double scroll_x2, scroll_y2; + + /* Scaling factor to be used for display */ + double pixels_per_unit; + + /* Idle handler ID */ + guint idle_id; + + /* Signal handler ID for destruction of the root item */ + guint root_destroy_id; + + /* Area that is being redrawn. Contains (x1, y1) but not (x2, y2). + * Specified in canvas pixel coordinates. + */ + int redraw_x1, redraw_y1; + int redraw_x2, redraw_y2; + + /* Offsets of the temprary drawing pixmap */ + int draw_xofs, draw_yofs; + + /* Internal pixel offsets when zoomed out */ + int zoom_xofs, zoom_yofs; + + /* Last known modifier state, for deferred repick when a button is down */ + int state; + + /* Event mask specified when grabbing an item */ + guint grabbed_event_mask; + + /* Tolerance distance for picking items */ + int close_enough; + + /* Whether the canvas should center the scroll region in the middle of + * the window if the scroll region is smaller than the window. + */ + unsigned int center_scroll_region : 1; + + /* Whether items need update at next idle loop iteration */ + unsigned int need_update : 1; + + /* Whether the canvas needs redrawing at the next idle loop iteration */ + unsigned int need_redraw : 1; + + /* Whether current item will be repicked at next idle loop iteration */ + unsigned int need_repick : 1; + + /* For use by internal pick_current_item() function */ + unsigned int left_grabbed_item : 1; + + /* For use by internal pick_current_item() function */ + unsigned int in_repick : 1; + + /* Flow direction */ + GanvDirection direction; + + /* Canvas width */ + double width; + + /* Canvas height */ + double height; + + /* Disable changes to canvas */ + gboolean locked; + #ifdef GANV_FDGL guint layout_idle_id; #endif }; struct _GanvCanvasClass { - GanvCanvasBaseClass parent_class; + GtkLayoutClass parent_class; + + /* Private Virtual methods for groping the canvas inside bonobo */ + void (* request_update)(GanvCanvas* canvas); + + /* Reserved for future expansion */ + gpointer spare_vmethods [4]; }; -GType ganv_canvas_get_type(void); +GType ganv_canvas_get_type(void) G_GNUC_CONST; GanvCanvas* ganv_canvas_new(double width, double height); -/** - * ganv_canvas_resize: - * Resize the canvas to the given dimensions. - */ +GanvItem* ganv_canvas_root(GanvCanvas* canvas); + +void ganv_canvas_set_scroll_region(GanvCanvas* canvas, + double x1, double y1, double x2, double y2); + +void ganv_canvas_get_scroll_region(GanvCanvas* canvas, + double* x1, double* y1, double* x2, double* y2); + +void ganv_canvas_set_center_scroll_region(GanvCanvas* canvas, gboolean center_scroll_region); + +gboolean ganv_canvas_get_center_scroll_region(GanvCanvas* canvas); + +void ganv_canvas_set_pixels_per_unit(GanvCanvas* canvas, double n); + +void ganv_canvas_scroll_to(GanvCanvas* canvas, int cx, int cy); + +void ganv_canvas_get_scroll_offsets(GanvCanvas* canvas, int* cx, int* cy); + +GanvItem* ganv_canvas_get_item_at(GanvCanvas* canvas, double x, double y); + +void ganv_canvas_w2c_affine(GanvCanvas* canvas, cairo_matrix_t* matrix); + +void ganv_canvas_w2c(GanvCanvas* canvas, double wx, double wy, int* cx, int* cy); + +void ganv_canvas_w2c_d(GanvCanvas* canvas, double wx, double wy, double* cx, double* cy); + +void ganv_canvas_c2w(GanvCanvas* canvas, int cx, int cy, double* wx, double* wy); + +void ganv_canvas_window_to_world(GanvCanvas* canvas, + double winx, double winy, double* worldx, double* worldy); + +void ganv_canvas_world_to_window(GanvCanvas* canvas, + double worldx, double worldy, double* winx, double* winy); + void ganv_canvas_resize(GanvCanvas* canvas, double width, double height); void ganv_canvas_contents_changed(GanvCanvas* canvas); -/** - * ganv_canvas_get_root: - * - * Return value: (transfer none): The root group of @canvas. - */ -GanvItem* -ganv_canvas_get_root(const GanvCanvas* canvas); - void ganv_canvas_add_node(GanvCanvas* canvas, GanvNode* node); @@ -92,12 +214,6 @@ void ganv_canvas_remove_node(GanvCanvas* canvas, GanvNode* node); -/** - * ganv_canvas_get_edge: - * Get the edge between two nodes, or NULL if none exists. - * - * Return value: (transfer none): The root group of @canvas. - */ GanvEdge* ganv_canvas_get_edge(GanvCanvas* canvas, GanvNode* tail, @@ -112,7 +228,6 @@ ganv_canvas_remove_edge_between(GanvCanvas* canvas, GanvNode* tail, GanvNode* head); -/** Get the default font size in points. */ double ganv_canvas_get_default_font_size(const GanvCanvas* canvas); @@ -125,7 +240,6 @@ ganv_canvas_clear_selection(GanvCanvas* canvas); void ganv_canvas_arrange(GanvCanvas* canvas); -/** Write a Graphviz DOT description of the canvas to @c filename. */ void ganv_canvas_export_dot(GanvCanvas* canvas, const char* filename); diff --git a/ganv/group.h b/ganv/group.h index 4bde530..678eacf 100644 --- a/ganv/group.h +++ b/ganv/group.h @@ -18,6 +18,8 @@ #include "ganv/item.h" +G_BEGIN_DECLS + /* Based on GnomeCanvasGroup, by Federico Mena * and Raph Levien * Copyright 1997-2000 Free Software Foundation @@ -47,4 +49,6 @@ struct _GanvGroupClass { GType ganv_group_get_type(void) G_GNUC_CONST; +G_END_DECLS + #endif /* GANV_GROUP_H */ diff --git a/ganv/item.h b/ganv/item.h index d75435a..6a77378 100644 --- a/ganv/item.h +++ b/ganv/item.h @@ -28,21 +28,6 @@ G_BEGIN_DECLS -/** - * GanvItem: Base class for all canvas items. - * - * All canvas items are derived from GanvItem. The only information a GanvItem - * contains is its parent canvas, its parent canvas item, its bounding box in - * world coordinates, and its current affine transformation. - * - * Items inside a canvas are organized in a tree, where leaves are items - * without any children. Each canvas has a single root item, which can be - * obtained with the ganv_canvas_base_get_root() function. - * - * The abstract GanvItem class does not have any configurable or queryable - * attributes. - */ - typedef struct _GanvItem GanvItem; typedef struct _GanvItemClass GanvItemClass; @@ -67,7 +52,7 @@ struct _GanvItem { GtkObject object; /* Parent canvas for this item */ - struct _GanvCanvasBase* canvas; + struct _GanvCanvas* canvas; /* Parent for this item */ GanvItem* parent; @@ -88,18 +73,13 @@ struct _GanvItem { struct _GanvItemClass { GtkObjectClass parent_class; - /** - * Add a child to this item (optional). - */ + /* Add a child to this item (optional). */ void (*add)(GanvItem* item, GanvItem* child); - /** - * Remove a child from this item (optional). - */ + /* Remove a child from this item (optional). */ void (*remove)(GanvItem* item, GanvItem* child); - /** - * Tell the item to update itself. + /* Tell the item to update itself. * * The flags are from the update flags defined above. The item should * update its internal state from its queued state, and recompute and @@ -108,28 +88,19 @@ struct _GanvItemClass { */ void (*update)(GanvItem* item, int flags); - /** - * Realize an item (create GCs, etc.). - */ + /* Realize an item (create GCs, etc.). */ void (*realize)(GanvItem* item); - /** - * Unrealize an item. - */ + /* Unrealize an item. */ void (*unrealize)(GanvItem* item); - /** - * Map an item - normally only need by items with their own GdkWindows. - */ + /* Map an item - normally only need by items with their own GdkWindows. */ void (*map)(GanvItem* item); - /** - * Unmap an item - */ + /* Unmap an item */ void (*unmap)(GanvItem* item); - /** - * Draw an item of this type. + /* Draw an item of this type. * * (x, y) are the upper-left canvas pixel coordinates of the drawable. * (width, height) are the dimensions of the drawable. @@ -141,8 +112,7 @@ struct _GanvItemClass { int width, int height); - /** - * Calculate the distance from an item to the specified point. + /* Calculate the distance from an item to the specified point. * * It also returns a canvas item which is actual item the point is within, * which may not be equal to @item if @item has children. (cx, cy) are the @@ -156,15 +126,13 @@ struct _GanvItemClass { int cy, GanvItem** actual_item); - /** - * Fetch the item's bounding box (need not be exactly tight). + /* Fetch the item's bounding box (need not be exactly tight). * * This should be in item-relative coordinates. */ void (*bounds)(GanvItem* item, double* x1, double* y1, double* x2, double* y2); - /** - * Signal: an event occurred for an item of this type. + /* Signal: an event occurred for an item of this type. * * The (x, y) coordinates are in the canvas world coordinate system. */ diff --git a/src/Canvas.cpp b/src/Canvas.cpp index 1fdcfb3..a461a6e 100644 --- a/src/Canvas.cpp +++ b/src/Canvas.cpp @@ -13,38 +13,50 @@ * with Ganv. If not, see . */ +/* Parts based on GnomeCanvas, by Federico Mena + * and Raph Levien + * Copyright 1997-2000 Free Software Foundation + */ + #define _POSIX_C_SOURCE 200809L // strdup +#include +#include +#include + #include #include #include #include #include #include +#include #include #include #include -#include +#include #include +#include #include - #include -#include "ganv_config.h" #include "ganv/Canvas.hpp" -#include "ganv/Edge.hpp" #include "ganv/Circle.hpp" +#include "ganv/Edge.hpp" #include "ganv/Module.hpp" #include "ganv/Port.hpp" #include "ganv/box.h" #include "ganv/canvas.h" #include "ganv/edge.h" +#include "ganv/group.h" #include "ganv/node.h" +#include "ganv_config.h" #include "./color.h" #include "./ganv-marshal.h" #include "./ganv-private.h" +#include "./gettext.h" #if defined(HAVE_AGRAPH_2_20) || defined(HAVE_AGRAPH_2_30) # include @@ -53,6 +65,49 @@ # include "./fdgl.hpp" #endif +/*** GanvCanvas ***/ + +#define CANVAS_IDLE_PRIORITY (GDK_PRIORITY_REDRAW - 5) + +typedef struct { + int x; + int y; + int width; + int height; +} IRect; + +extern "C" { +static void add_idle(GanvCanvas* canvas); +static void ganv_canvas_destroy(GtkObject* object); +static void ganv_canvas_map(GtkWidget* widget); +static void ganv_canvas_unmap(GtkWidget* widget); +static void ganv_canvas_realize(GtkWidget* widget); +static void ganv_canvas_unrealize(GtkWidget* widget); +static void ganv_canvas_size_allocate(GtkWidget* widget, + GtkAllocation* allocation); +static gint ganv_canvas_button(GtkWidget* widget, + GdkEventButton* event); +static gint ganv_canvas_motion(GtkWidget* widget, + GdkEventMotion* event); +static gint ganv_canvas_expose(GtkWidget* widget, + GdkEventExpose* event); +static gboolean ganv_canvas_key(GtkWidget* widget, + GdkEventKey* event); +static gboolean ganv_canvas_scroll(GtkWidget* widget, + GdkEventScroll* event); +static gint ganv_canvas_crossing(GtkWidget* widget, + GdkEventCrossing* event); +static gint ganv_canvas_focus_in(GtkWidget* widget, + GdkEventFocus* event); +static gint ganv_canvas_focus_out(GtkWidget* widget, + GdkEventFocus* event); +static void ganv_canvas_request_update_real(GanvCanvas* canvas); + +static GtkLayoutClass* canvas_parent_class; +} + +/*** end GanvCanvas ***/ + static guint signal_connect; static guint signal_disconnect; @@ -148,7 +203,7 @@ struct GanvCanvasImpl { _wrapper_key = g_quark_from_string("ganvmm"); _move_cursor = gdk_cursor_new(GDK_FLEUR); - g_signal_connect(G_OBJECT(ganv_canvas_base_root(GANV_CANVAS_BASE(_gcanvas))), + g_signal_connect(G_OBJECT(ganv_canvas_root(GANV_CANVAS(_gcanvas))), "event", G_CALLBACK(on_canvas_event), this); } @@ -180,7 +235,7 @@ struct GanvCanvasImpl { #endif GanvItem* root() { - return ganv_canvas_base_root(GANV_CANVAS_BASE(_gcanvas)); + return ganv_canvas_root(GANV_CANVAS(_gcanvas)); } void resize(double width, double height); @@ -1053,7 +1108,7 @@ GanvCanvasImpl::join_selection() GanvNode* GanvCanvasImpl::get_node_at(double x, double y) { - GanvItem* item = ganv_canvas_base_get_item_at(GANV_CANVAS_BASE(_gcanvas), x, y); + GanvItem* item = ganv_canvas_get_item_at(GANV_CANVAS(_gcanvas), x, y); while (item) { if (GANV_IS_NODE(item)) { return GANV_NODE(item); @@ -1075,7 +1130,7 @@ GanvCanvasImpl::on_event(GdkEvent* event) switch (event->type) { case GDK_KEY_PRESS: handled = true; - ganv_canvas_base_get_scroll_offsets(GANV_CANVAS_BASE(_gcanvas), &scroll_x, &scroll_y); + ganv_canvas_get_scroll_offsets(GANV_CANVAS(_gcanvas), &scroll_x, &scroll_y); switch (event->key.keyval) { case GDK_Up: scroll_y -= scroll_increment; @@ -1099,7 +1154,7 @@ GanvCanvasImpl::on_event(GdkEvent* event) handled = false; } if (handled) { - ganv_canvas_base_scroll_to(GANV_CANVAS_BASE(_gcanvas), scroll_x, scroll_y); + ganv_canvas_scroll_to(GANV_CANVAS(_gcanvas), scroll_x, scroll_y); return true; } break; @@ -1145,7 +1200,7 @@ GanvCanvasImpl::scroll_drag_handler(GdkEvent* event) GANV_ITEM(root()), GDK_POINTER_MOTION_MASK|GDK_BUTTON_RELEASE_MASK, NULL, event->button.time); - ganv_canvas_base_get_scroll_offsets(GANV_CANVAS_BASE(_gcanvas), &original_scroll_x, &original_scroll_y); + ganv_canvas_get_scroll_offsets(GANV_CANVAS(_gcanvas), &original_scroll_x, &original_scroll_y); scroll_offset_x = 0; scroll_offset_y = 0; origin_x = event->button.x_root; @@ -1166,7 +1221,7 @@ GanvCanvasImpl::scroll_drag_handler(GdkEvent* event) scroll_offset_x += x_offset; scroll_offset_y += y_offset; - ganv_canvas_base_scroll_to(GANV_CANVAS_BASE(_gcanvas), + ganv_canvas_scroll_to(GANV_CANVAS(_gcanvas), lrint(original_scroll_x + scroll_offset_x), lrint(original_scroll_y + scroll_offset_y)); last_x = x; @@ -1298,7 +1353,7 @@ GanvCanvasImpl::connect_drag_handler(GdkEvent* event) _drag_node = GANV_NODE( ganv_item_new( - GANV_ITEM(ganv_canvas_base_root(GANV_CANVAS_BASE(_gcanvas))), + GANV_ITEM(ganv_canvas_root(GANV_CANVAS(_gcanvas))), ganv_node_get_type(), "x", x, "y", y, @@ -1697,7 +1752,7 @@ GanvCanvasImpl::resize(double width, double height) if (width != _gcanvas->width || height != _gcanvas->height) { _gcanvas->width = width; _gcanvas->height = height; - ganv_canvas_base_set_scroll_region(GANV_CANVAS_BASE(_gcanvas), + ganv_canvas_set_scroll_region(GANV_CANVAS(_gcanvas), 0.0, 0.0, width, height); } } @@ -1728,7 +1783,7 @@ GanvCanvasImpl::set_zoom_and_font_size(double zoom, double points) if (zoom != _zoom) { _zoom = zoom; - ganv_canvas_base_set_pixels_per_unit(GANV_CANVAS_BASE(_gcanvas), zoom); + ganv_canvas_set_pixels_per_unit(GANV_CANVAS(_gcanvas), zoom); } _font_size = points; @@ -1836,7 +1891,7 @@ Canvas::for_each_selected_edge(GanvEdgeFunc f, void* data) GanvItem* Canvas::root() { - return ganv_canvas_base_root(GANV_CANVAS_BASE(impl()->_gcanvas)); + return ganv_canvas_root(GANV_CANVAS(impl()->_gcanvas)); } Gtk::Layout& @@ -1848,13 +1903,13 @@ Canvas::widget() void Canvas::get_scroll_offsets(int& cx, int& cy) const { - ganv_canvas_base_get_scroll_offsets(GANV_CANVAS_BASE(impl()->_gcanvas), &cx, &cy); + ganv_canvas_get_scroll_offsets(GANV_CANVAS(impl()->_gcanvas), &cx, &cy); } void Canvas::scroll_to(int x, int y) { - ganv_canvas_base_scroll_to(GANV_CANVAS_BASE(impl()->_gcanvas), x, y); + ganv_canvas_scroll_to(GANV_CANVAS(impl()->_gcanvas), x, y); } GQuark @@ -1879,16 +1934,13 @@ Canvas::gobj() const extern "C" { -#include "ganv/canvas-base.h" #include "ganv/canvas.h" #include "./boilerplate.h" #include "./color.h" #include "./gettext.h" -G_DEFINE_TYPE(GanvCanvas, ganv_canvas, GANV_TYPE_CANVAS_BASE) - -static GanvCanvasBaseClass* parent_class; +G_DEFINE_TYPE(GanvCanvas, ganv_canvas, GTK_TYPE_LAYOUT) enum { PROP_0, @@ -1896,12 +1948,59 @@ enum { PROP_HEIGHT, PROP_DIRECTION, PROP_FONT_SIZE, - PROP_LOCKED + PROP_LOCKED, + PROP_FOCUSED_ITEM }; +/* Callback used when the root item of a canvas is destroyed. The user should + * never ever do this, so we panic if this happens. + */ +static void +panic_root_destroyed(GtkObject* object, gpointer data) +{ + g_error("Eeeek, root item %p of canvas %p was destroyed!", (void*)object, data); +} + static void ganv_canvas_init(GanvCanvas* canvas) { + GTK_WIDGET_SET_FLAGS(canvas, GTK_CAN_FOCUS); + + canvas->need_update = FALSE; + canvas->need_redraw = FALSE; + canvas->redraw_region = NULL; + canvas->idle_id = 0; + + canvas->scroll_x1 = 0.0; + canvas->scroll_y1 = 0.0; + canvas->scroll_x2 = canvas->layout.width; + canvas->scroll_y2 = canvas->layout.height; + + canvas->pixels_per_unit = 1.0; + + canvas->pick_event.type = GDK_LEAVE_NOTIFY; + canvas->pick_event.crossing.x = 0; + canvas->pick_event.crossing.y = 0; + + canvas->center_scroll_region = TRUE; + + gtk_layout_set_hadjustment(GTK_LAYOUT(canvas), NULL); + gtk_layout_set_vadjustment(GTK_LAYOUT(canvas), NULL); + + /* Create the root item as a special case */ + canvas->root = GANV_ITEM(g_object_new(ganv_group_get_type(), NULL)); + canvas->root->canvas = canvas; + + g_object_ref_sink(canvas->root); + + canvas->root_destroy_id = g_signal_connect(canvas->root, "destroy", + G_CALLBACK(panic_root_destroyed), + canvas); + + canvas->need_repick = TRUE; + + /*************************/ + canvas->direction = GANV_DIRECTION_RIGHT; canvas->impl = new GanvCanvasImpl(canvas); canvas->impl->_font_size = ganv_canvas_get_default_font_size(canvas); @@ -1934,6 +2033,9 @@ ganv_canvas_set_property(GObject* object, case PROP_LOCKED: canvas->locked = g_value_get_boolean(value); break; + case PROP_FOCUSED_ITEM: + canvas->focused_item = GANV_ITEM(g_value_get_object(value)); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); break; @@ -1955,6 +2057,9 @@ ganv_canvas_get_property(GObject* object, GET_CASE(WIDTH, double, canvas->width) GET_CASE(HEIGHT, double, canvas->height) GET_CASE(LOCKED, boolean, canvas->locked); + case PROP_FOCUSED_ITEM: + g_value_set_object(value, GANV_CANVAS(object)->focused_item); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); break; @@ -1964,13 +2069,44 @@ ganv_canvas_get_property(GObject* object, static void ganv_canvas_class_init(GanvCanvasClass* klass) { - GObjectClass* gobject_class = (GObjectClass*)klass; + GObjectClass* gobject_class = (GObjectClass*)klass; + GtkObjectClass* object_class = (GtkObjectClass*)klass; + GtkWidgetClass* widget_class = (GtkWidgetClass*)klass; - parent_class = GANV_CANVAS_BASE_CLASS(g_type_class_peek_parent(klass)); + canvas_parent_class = GTK_LAYOUT_CLASS(g_type_class_peek_parent(klass)); gobject_class->set_property = ganv_canvas_set_property; gobject_class->get_property = ganv_canvas_get_property; + object_class->destroy = ganv_canvas_destroy; + + widget_class->map = ganv_canvas_map; + widget_class->unmap = ganv_canvas_unmap; + widget_class->realize = ganv_canvas_realize; + widget_class->unrealize = ganv_canvas_unrealize; + widget_class->size_allocate = ganv_canvas_size_allocate; + widget_class->button_press_event = ganv_canvas_button; + widget_class->button_release_event = ganv_canvas_button; + widget_class->motion_notify_event = ganv_canvas_motion; + widget_class->expose_event = ganv_canvas_expose; + widget_class->key_press_event = ganv_canvas_key; + widget_class->key_release_event = ganv_canvas_key; + widget_class->enter_notify_event = ganv_canvas_crossing; + widget_class->leave_notify_event = ganv_canvas_crossing; + widget_class->focus_in_event = ganv_canvas_focus_in; + widget_class->focus_out_event = ganv_canvas_focus_out; + widget_class->scroll_event = ganv_canvas_scroll; + + klass->request_update = ganv_canvas_request_update_real; + + g_object_class_install_property( + gobject_class, PROP_FOCUSED_ITEM, g_param_spec_object( + "focused-item", + NULL, + NULL, + GANV_TYPE_ITEM, + (GParamFlags)(G_PARAM_READABLE | G_PARAM_WRITABLE))); + g_object_class_install_property( gobject_class, PROP_WIDTH, g_param_spec_double( "width", @@ -2047,18 +2183,10 @@ ganv_canvas_class_init(GanvCanvasClass* klass) 0); } -GanvCanvas* -ganv_canvas_new(double width, double height) -{ - GanvCanvas* canvas = GANV_CANVAS(g_object_new(ganv_canvas_get_type(), - "width", width, - "height", height, - NULL)); - ganv_canvas_base_set_scroll_region(GANV_CANVAS_BASE(canvas), - 0.0, 0.0, width, height); - return canvas; -} - +/** + * ganv_canvas_resize: + * Resize the canvas to the given dimensions. + */ void ganv_canvas_resize(GanvCanvas* canvas, double width, double height) { @@ -2071,12 +2199,11 @@ ganv_canvas_contents_changed(GanvCanvas* canvas) canvas->impl->contents_changed(); } -GanvItem* -ganv_canvas_get_root(const GanvCanvas* canvas) -{ - return ganv_canvas_base_root(GANV_CANVAS_BASE(canvas->impl->_gcanvas)); -} - +/** + * ganv_canvas_get_default_font_size: + * + * Get the default font size in points. + */ double ganv_canvas_get_default_font_size(const GanvCanvas* canvas) { @@ -2117,7 +2244,7 @@ ganv_canvas_zoom_full(GanvCanvas* canvas) int win_width, win_height; GdkWindow* win = gtk_widget_get_window( - GTK_WIDGET(GANV_CANVAS_BASE(canvas->impl->_gcanvas))); + GTK_WIDGET(GANV_CANVAS(canvas->impl->_gcanvas))); gdk_window_get_size(win, &win_width, &win_height); // Box containing all canvas items @@ -2153,11 +2280,11 @@ ganv_canvas_zoom_full(GanvCanvas* canvas) ganv_canvas_set_zoom(canvas, new_zoom); int scroll_x, scroll_y; - ganv_canvas_base_w2c(GANV_CANVAS_BASE(canvas->impl->_gcanvas), + ganv_canvas_w2c(GANV_CANVAS(canvas->impl->_gcanvas), lrintf(left - pad), lrintf(bottom - pad), &scroll_x, &scroll_y); - ganv_canvas_base_scroll_to(GANV_CANVAS_BASE(canvas->impl->_gcanvas), + ganv_canvas_scroll_to(GANV_CANVAS(canvas->impl->_gcanvas), scroll_x, scroll_y); } @@ -2220,6 +2347,12 @@ ganv_canvas_add_node(GanvCanvas* canvas, canvas->impl->add_item(node); } +/** + * ganv_canvas_get_edge: + * Get the edge between two nodes, or NULL if none exists. + * + * Return value: (transfer none): The root group of @canvas. + */ GanvEdge* ganv_canvas_get_edge(GanvCanvas* canvas, GanvNode* tail, @@ -2413,7 +2546,7 @@ ganv_canvas_arrange(GanvCanvas* canvas) // Arrange to graphviz coordinates for (GVNodes::iterator i = nodes.begin(); i != nodes.end(); ++i) { - if (GANV_ITEM(i->first)->parent != GANV_ITEM(ganv_canvas_get_root(canvas))) { + if (GANV_ITEM(i->first)->parent != GANV_ITEM(ganv_canvas_root(canvas))) { continue; } const string pos = agget(i->second, (char*)"pos"); @@ -2469,7 +2602,7 @@ ganv_canvas_arrange(GanvCanvas* canvas) static const double border_width = canvas->impl->_font_size * 2.0; canvas->impl->move_contents_to_internal(border_width, border_width, least_x, least_y); - ganv_canvas_base_scroll_to(GANV_CANVAS_BASE(canvas->impl->_gcanvas), 0, 0); + ganv_canvas_scroll_to(GANV_CANVAS(canvas->impl->_gcanvas), 0, 0); FOREACH_ITEM(canvas->impl->_items, i) { const double x = GANV_ITEM(*i)->x; @@ -2479,6 +2612,11 @@ ganv_canvas_arrange(GanvCanvas* canvas) #endif } +/** + * ganv_canvas_export_dot: + * + * Write a Graphviz DOT description of the canvas to a file. + */ void ganv_canvas_export_dot(GanvCanvas* canvas, const char* filename) { @@ -2488,4 +2626,1536 @@ ganv_canvas_export_dot(GanvCanvas* canvas, const char* filename) #endif } + +/***************************************************************************/ + +/* Convenience function to remove the idle handler of a canvas */ +static void +remove_idle(GanvCanvas* canvas) +{ + if (canvas->idle_id == 0) { + return; + } + + g_source_remove(canvas->idle_id); + canvas->idle_id = 0; +} + +/* Removes the transient state of the canvas (idle handler, grabs). */ +static void +shutdown_transients(GanvCanvas* canvas) +{ + /* We turn off the need_redraw flag, since if the canvas is mapped again + * it will request a redraw anyways. We do not turn off the need_update + * flag, though, because updates are not queued when the canvas remaps + * itself. + */ + if (canvas->need_redraw) { + canvas->need_redraw = FALSE; + g_slist_foreach(canvas->redraw_region, (GFunc)g_free, NULL); + g_slist_free(canvas->redraw_region); + canvas->redraw_region = NULL; + canvas->redraw_x1 = 0; + canvas->redraw_y1 = 0; + canvas->redraw_x2 = 0; + canvas->redraw_y2 = 0; + } + + if (canvas->grabbed_item) { + canvas->grabbed_item = NULL; + gdk_pointer_ungrab(GDK_CURRENT_TIME); + } + + remove_idle(canvas); +} + +/* Destroy handler for GanvCanvas */ +static void +ganv_canvas_destroy(GtkObject* object) +{ + GanvCanvas* canvas; + + g_return_if_fail(GANV_IS_CANVAS(object)); + + /* remember, destroy can be run multiple times! */ + + canvas = GANV_CANVAS(object); + + if (canvas->root_destroy_id) { + g_signal_handler_disconnect(canvas->root, canvas->root_destroy_id); + canvas->root_destroy_id = 0; + } + if (canvas->root) { + gtk_object_destroy(GTK_OBJECT(canvas->root)); + g_object_unref(G_OBJECT(canvas->root)); + canvas->root = NULL; + } + + shutdown_transients(canvas); + + if (GTK_OBJECT_CLASS(canvas_parent_class)->destroy) { + (*GTK_OBJECT_CLASS(canvas_parent_class)->destroy)(object); + } +} + +/** + * ganv_canvas_new: + * + * Return value: A newly-created canvas. + */ +GanvCanvas* +ganv_canvas_new(double width, double height) +{ + GanvCanvas* canvas = GANV_CANVAS( + g_object_new(ganv_canvas_get_type(), + "width", width, + "height", height, + NULL)); + + ganv_canvas_set_scroll_region(GANV_CANVAS(canvas), + 0.0, 0.0, width, height); + + return canvas; +} + +/* Map handler for the canvas */ +static void +ganv_canvas_map(GtkWidget* widget) +{ + GanvCanvas* canvas; + + g_return_if_fail(GANV_IS_CANVAS(widget)); + + /* Normal widget mapping stuff */ + + if (GTK_WIDGET_CLASS(canvas_parent_class)->map) { + (*GTK_WIDGET_CLASS(canvas_parent_class)->map)(widget); + } + + canvas = GANV_CANVAS(widget); + + if (canvas->need_update) { + add_idle(canvas); + } + + /* Map items */ + + if (GANV_ITEM_GET_CLASS(canvas->root)->map) { + (*GANV_ITEM_GET_CLASS(canvas->root)->map)(canvas->root); + } +} + +/* Unmap handler for the canvas */ +static void +ganv_canvas_unmap(GtkWidget* widget) +{ + GanvCanvas* canvas; + + g_return_if_fail(GANV_IS_CANVAS(widget)); + + canvas = GANV_CANVAS(widget); + + shutdown_transients(canvas); + + /* Unmap items */ + + if (GANV_ITEM_GET_CLASS(canvas->root)->unmap) { + (*GANV_ITEM_GET_CLASS(canvas->root)->unmap)(canvas->root); + } + + /* Normal widget unmapping stuff */ + + if (GTK_WIDGET_CLASS(canvas_parent_class)->unmap) { + (*GTK_WIDGET_CLASS(canvas_parent_class)->unmap)(widget); + } +} + +/* Realize handler for the canvas */ +static void +ganv_canvas_realize(GtkWidget* widget) +{ + GanvCanvas* canvas; + + g_return_if_fail(GANV_IS_CANVAS(widget)); + + /* Normal widget realization stuff */ + + if (GTK_WIDGET_CLASS(canvas_parent_class)->realize) { + (*GTK_WIDGET_CLASS(canvas_parent_class)->realize)(widget); + } + + canvas = GANV_CANVAS(widget); + + gdk_window_set_events( + canvas->layout.bin_window, + (GdkEventMask)(gdk_window_get_events(canvas->layout.bin_window) + | GDK_EXPOSURE_MASK + | GDK_BUTTON_PRESS_MASK + | GDK_BUTTON_RELEASE_MASK + | GDK_POINTER_MOTION_MASK + | GDK_KEY_PRESS_MASK + | GDK_KEY_RELEASE_MASK + | GDK_ENTER_NOTIFY_MASK + | GDK_LEAVE_NOTIFY_MASK + | GDK_FOCUS_CHANGE_MASK)); + + /* Create our own temporary pixmap gc and realize all the items */ + + canvas->pixmap_gc = gdk_gc_new(canvas->layout.bin_window); + + (*GANV_ITEM_GET_CLASS(canvas->root)->realize)(canvas->root); +} + +/* Unrealize handler for the canvas */ +static void +ganv_canvas_unrealize(GtkWidget* widget) +{ + GanvCanvas* canvas; + + g_return_if_fail(GANV_IS_CANVAS(widget)); + + canvas = GANV_CANVAS(widget); + + shutdown_transients(canvas); + + /* Unrealize items and parent widget */ + + (*GANV_ITEM_GET_CLASS(canvas->root)->unrealize)(canvas->root); + + g_object_unref(canvas->pixmap_gc); + canvas->pixmap_gc = NULL; + + if (GTK_WIDGET_CLASS(canvas_parent_class)->unrealize) { + (*GTK_WIDGET_CLASS(canvas_parent_class)->unrealize)(widget); + } +} + +/* Handles scrolling of the canvas. Adjusts the scrolling and zooming offset to + * keep as much as possible of the canvas scrolling region in view. + */ +static void +scroll_to(GanvCanvas* canvas, int cx, int cy) +{ + int scroll_width, scroll_height; + int right_limit, bottom_limit; + int old_zoom_xofs, old_zoom_yofs; + int changed_x = FALSE, changed_y = FALSE; + int canvas_width, canvas_height; + + canvas_width = GTK_WIDGET(canvas)->allocation.width; + canvas_height = GTK_WIDGET(canvas)->allocation.height; + + scroll_width = floor((canvas->scroll_x2 - canvas->scroll_x1) * canvas->pixels_per_unit + + 0.5); + scroll_height = floor((canvas->scroll_y2 - canvas->scroll_y1) * canvas->pixels_per_unit + + 0.5); + + right_limit = scroll_width - canvas_width; + bottom_limit = scroll_height - canvas_height; + + old_zoom_xofs = canvas->zoom_xofs; + old_zoom_yofs = canvas->zoom_yofs; + + if (right_limit < 0) { + cx = 0; + + if (canvas->center_scroll_region) { + canvas->zoom_xofs = (canvas_width - scroll_width) / 2; + scroll_width = canvas_width; + } else { + canvas->zoom_xofs = 0; + } + } else if (cx < 0) { + cx = 0; + canvas->zoom_xofs = 0; + } else if (cx > right_limit) { + cx = right_limit; + canvas->zoom_xofs = 0; + } else { + canvas->zoom_xofs = 0; + } + + if (bottom_limit < 0) { + cy = 0; + + if (canvas->center_scroll_region) { + canvas->zoom_yofs = (canvas_height - scroll_height) / 2; + scroll_height = canvas_height; + } else { + canvas->zoom_yofs = 0; + } + } else if (cy < 0) { + cy = 0; + canvas->zoom_yofs = 0; + } else if (cy > bottom_limit) { + cy = bottom_limit; + canvas->zoom_yofs = 0; + } else { + canvas->zoom_yofs = 0; + } + + if ((canvas->zoom_xofs != old_zoom_xofs) || (canvas->zoom_yofs != old_zoom_yofs)) { + ganv_canvas_request_update(canvas); + gtk_widget_queue_draw(GTK_WIDGET(canvas)); + } + + if (canvas->layout.hadjustment && ( ((int)canvas->layout.hadjustment->value) != cx) ) { + canvas->layout.hadjustment->value = cx; + changed_x = TRUE; + } + + if (canvas->layout.vadjustment && ( ((int)canvas->layout.vadjustment->value) != cy) ) { + canvas->layout.vadjustment->value = cy; + changed_y = TRUE; + } + + if ((scroll_width != (int)canvas->layout.width) + || (scroll_height != (int)canvas->layout.height)) { + gtk_layout_set_size(GTK_LAYOUT(canvas), scroll_width, scroll_height); + } + + /* Signal GtkLayout that it should do a redraw. */ + + if (changed_x) { + g_signal_emit_by_name(canvas->layout.hadjustment, "value_changed"); + } + + if (changed_y) { + g_signal_emit_by_name(canvas->layout.vadjustment, "value_changed"); + } +} + +/* Size allocation handler for the canvas */ +static void +ganv_canvas_size_allocate(GtkWidget* widget, GtkAllocation* allocation) +{ + GanvCanvas* canvas; + + g_return_if_fail(GANV_IS_CANVAS(widget)); + g_return_if_fail(allocation != NULL); + + if (GTK_WIDGET_CLASS(canvas_parent_class)->size_allocate) { + (*GTK_WIDGET_CLASS(canvas_parent_class)->size_allocate)(widget, allocation); + } + + canvas = GANV_CANVAS(widget); + + /* Recenter the view, if appropriate */ + + canvas->layout.hadjustment->page_size = allocation->width; + canvas->layout.hadjustment->page_increment = allocation->width / 2; + + canvas->layout.vadjustment->page_size = allocation->height; + canvas->layout.vadjustment->page_increment = allocation->height / 2; + + scroll_to(canvas, + canvas->layout.hadjustment->value, + canvas->layout.vadjustment->value); + + g_signal_emit_by_name(canvas->layout.hadjustment, "changed"); + g_signal_emit_by_name(canvas->layout.vadjustment, "changed"); +} + +/* Returns whether the item is an inferior of or is equal to the parent. */ +static gboolean +is_descendant(GanvItem* item, GanvItem* parent) +{ + for (; item; item = item->parent) { + if (item == parent) { + return TRUE; + } + } + + return FALSE; +} + + +/* Emits an event for an item in the canvas, be it the current item, grabbed + * item, or focused item, as appropriate. + */ +int +ganv_canvas_emit_event(GanvCanvas* canvas, GdkEvent* event) +{ + GdkEvent* ev; + gint finished; + GanvItem* item; + GanvItem* parent; + guint mask; + + /* Perform checks for grabbed items */ + + if (canvas->grabbed_item + && !is_descendant(canvas->current_item, canvas->grabbed_item)) { + /* I think this warning is annoying and I don't know what it's for + * so I'll disable it for now. + */ + /* g_warning ("emit_event() returning FALSE!\n");*/ + return FALSE; + } + + if (canvas->grabbed_item) { + switch (event->type) { + case GDK_ENTER_NOTIFY: + mask = GDK_ENTER_NOTIFY_MASK; + break; + + case GDK_LEAVE_NOTIFY: + mask = GDK_LEAVE_NOTIFY_MASK; + break; + + case GDK_MOTION_NOTIFY: + mask = GDK_POINTER_MOTION_MASK; + break; + + case GDK_BUTTON_PRESS: + case GDK_2BUTTON_PRESS: + case GDK_3BUTTON_PRESS: + mask = GDK_BUTTON_PRESS_MASK; + break; + + case GDK_BUTTON_RELEASE: + mask = GDK_BUTTON_RELEASE_MASK; + break; + + case GDK_KEY_PRESS: + mask = GDK_KEY_PRESS_MASK; + break; + + case GDK_KEY_RELEASE: + mask = GDK_KEY_RELEASE_MASK; + break; + + case GDK_SCROLL: + mask = GDK_SCROLL_MASK; + break; + + default: + mask = 0; + break; + } + + if (!(mask & canvas->grabbed_event_mask)) { + return FALSE; + } + } + + /* Convert to world coordinates -- we have two cases because of diferent + * offsets of the fields in the event structures. + */ + + ev = gdk_event_copy(event); + + switch (ev->type) { + case GDK_ENTER_NOTIFY: + case GDK_LEAVE_NOTIFY: + ganv_canvas_window_to_world(canvas, + ev->crossing.x, ev->crossing.y, + &ev->crossing.x, &ev->crossing.y); + break; + + case GDK_MOTION_NOTIFY: + case GDK_BUTTON_PRESS: + case GDK_2BUTTON_PRESS: + case GDK_3BUTTON_PRESS: + case GDK_BUTTON_RELEASE: + ganv_canvas_window_to_world(canvas, + ev->motion.x, ev->motion.y, + &ev->motion.x, &ev->motion.y); + break; + + default: + break; + } + + /* Choose where we send the event */ + + item = canvas->current_item; + + if (canvas->focused_item + && ((event->type == GDK_KEY_PRESS) + || (event->type == GDK_KEY_RELEASE) + || (event->type == GDK_FOCUS_CHANGE))) { + item = canvas->focused_item; + } + + /* The event is propagated up the hierarchy (for if someone connected to + * a group instead of a leaf event), and emission is stopped if a + * handler returns TRUE, just like for GtkWidget events. + */ + + finished = FALSE; + + while (item && !finished) { + g_object_ref(G_OBJECT(item)); + + ganv_item_emit_event(item, ev, &finished); + + parent = item->parent; + g_object_unref(G_OBJECT(item)); + + item = parent; + } + + gdk_event_free(ev); + + return finished; +} + +/* Re-picks the current item in the canvas, based on the event's coordinates. + * Also emits enter/leave events for items as appropriate. + */ +static int +pick_current_item(GanvCanvas* canvas, GdkEvent* event) +{ + int button_down; + double x, y; + int cx, cy; + int retval; + + retval = FALSE; + + /* If a button is down, we'll perform enter and leave events on the + * current item, but not enter on any other item. This is more or less + * like X pointer grabbing for canvas items. + */ + button_down = canvas->state & (GDK_BUTTON1_MASK + | GDK_BUTTON2_MASK + | GDK_BUTTON3_MASK + | GDK_BUTTON4_MASK + | GDK_BUTTON5_MASK); + if (!button_down) { + canvas->left_grabbed_item = FALSE; + } + + /* Save the event in the canvas. This is used to synthesize enter and + * leave events in case the current item changes. It is also used to + * re-pick the current item if the current one gets deleted. Also, + * synthesize an enter event. + */ + if (event != &canvas->pick_event) { + if ((event->type == GDK_MOTION_NOTIFY) || (event->type == GDK_BUTTON_RELEASE)) { + /* these fields have the same offsets in both types of events */ + + canvas->pick_event.crossing.type = GDK_ENTER_NOTIFY; + canvas->pick_event.crossing.window = event->motion.window; + canvas->pick_event.crossing.send_event = event->motion.send_event; + canvas->pick_event.crossing.subwindow = NULL; + canvas->pick_event.crossing.x = event->motion.x; + canvas->pick_event.crossing.y = event->motion.y; + canvas->pick_event.crossing.mode = GDK_CROSSING_NORMAL; + canvas->pick_event.crossing.detail = GDK_NOTIFY_NONLINEAR; + canvas->pick_event.crossing.focus = FALSE; + canvas->pick_event.crossing.state = event->motion.state; + + /* these fields don't have the same offsets in both types of events */ + + if (event->type == GDK_MOTION_NOTIFY) { + canvas->pick_event.crossing.x_root = event->motion.x_root; + canvas->pick_event.crossing.y_root = event->motion.y_root; + } else { + canvas->pick_event.crossing.x_root = event->button.x_root; + canvas->pick_event.crossing.y_root = event->button.y_root; + } + } else { + canvas->pick_event = *event; + } + } + + /* Don't do anything else if this is a recursive call */ + + if (canvas->in_repick) { + return retval; + } + + /* LeaveNotify means that there is no current item, so we don't look for one */ + + if (canvas->pick_event.type != GDK_LEAVE_NOTIFY) { + /* these fields don't have the same offsets in both types of events */ + + if (canvas->pick_event.type == GDK_ENTER_NOTIFY) { + x = canvas->pick_event.crossing.x - canvas->zoom_xofs; + y = canvas->pick_event.crossing.y - canvas->zoom_yofs; + } else { + x = canvas->pick_event.motion.x - canvas->zoom_xofs; + y = canvas->pick_event.motion.y - canvas->zoom_yofs; + } + + /* canvas pixel coords */ + + cx = (int)(x + 0.5); + cy = (int)(y + 0.5); + + /* world coords */ + + x = canvas->scroll_x1 + x / canvas->pixels_per_unit; + y = canvas->scroll_y1 + y / canvas->pixels_per_unit; + + /* find the closest item */ + + if (canvas->root->object.flags & GANV_ITEM_VISIBLE) { + GANV_ITEM_GET_CLASS(canvas->root)->point( + canvas->root, + x - canvas->root->x, y - canvas->root->y, + cx, cy, + &canvas->new_current_item); + } else { + canvas->new_current_item = NULL; + } + } else { + canvas->new_current_item = NULL; + } + + if ((canvas->new_current_item == canvas->current_item) && !canvas->left_grabbed_item) { + return retval; /* current item did not change */ + + } + /* Synthesize events for old and new current items */ + + if ((canvas->new_current_item != canvas->current_item) + && (canvas->current_item != NULL) + && !canvas->left_grabbed_item) { + GdkEvent new_event; + + new_event = canvas->pick_event; + new_event.type = GDK_LEAVE_NOTIFY; + + new_event.crossing.detail = GDK_NOTIFY_ANCESTOR; + new_event.crossing.subwindow = NULL; + canvas->in_repick = TRUE; + retval = ganv_canvas_emit_event(canvas, &new_event); + canvas->in_repick = FALSE; + } + + /* new_current_item may have been set to NULL during the call to ganv_canvas_emit_event() above */ + + if ((canvas->new_current_item != canvas->current_item) && button_down) { + canvas->left_grabbed_item = TRUE; + return retval; + } + + /* Handle the rest of cases */ + + canvas->left_grabbed_item = FALSE; + canvas->current_item = canvas->new_current_item; + + if (canvas->current_item != NULL) { + GdkEvent new_event; + + new_event = canvas->pick_event; + new_event.type = GDK_ENTER_NOTIFY; + new_event.crossing.detail = GDK_NOTIFY_ANCESTOR; + new_event.crossing.subwindow = NULL; + retval = ganv_canvas_emit_event(canvas, &new_event); + } + + return retval; +} + +/* Button event handler for the canvas */ +static gint +ganv_canvas_button(GtkWidget* widget, GdkEventButton* event) +{ + GanvCanvas* canvas; + int mask; + int retval; + + g_return_val_if_fail(GANV_IS_CANVAS(widget), FALSE); + g_return_val_if_fail(event != NULL, FALSE); + + retval = FALSE; + + canvas = GANV_CANVAS(widget); + + /* + * dispatch normally regardless of the event's window if an item has + * has a pointer grab in effect + */ + if (!canvas->grabbed_item && ( event->window != canvas->layout.bin_window) ) { + return retval; + } + + switch (event->button) { + case 1: + mask = GDK_BUTTON1_MASK; + break; + case 2: + mask = GDK_BUTTON2_MASK; + break; + case 3: + mask = GDK_BUTTON3_MASK; + break; + case 4: + mask = GDK_BUTTON4_MASK; + break; + case 5: + mask = GDK_BUTTON5_MASK; + break; + default: + mask = 0; + } + + switch (event->type) { + case GDK_BUTTON_PRESS: + case GDK_2BUTTON_PRESS: + case GDK_3BUTTON_PRESS: + /* Pick the current item as if the button were not pressed, and + * then process the event. + */ + canvas->state = event->state; + pick_current_item(canvas, (GdkEvent*)event); + canvas->state ^= mask; + retval = ganv_canvas_emit_event(canvas, (GdkEvent*)event); + break; + + case GDK_BUTTON_RELEASE: + /* Process the event as if the button were pressed, then repick + * after the button has been released + */ + canvas->state = event->state; + retval = ganv_canvas_emit_event(canvas, (GdkEvent*)event); + event->state ^= mask; + canvas->state = event->state; + pick_current_item(canvas, (GdkEvent*)event); + event->state ^= mask; + break; + + default: + g_assert_not_reached(); + } + + return retval; +} + +/* Motion event handler for the canvas */ +static gint +ganv_canvas_motion(GtkWidget* widget, GdkEventMotion* event) +{ + GanvCanvas* canvas; + + g_return_val_if_fail(GANV_IS_CANVAS(widget), FALSE); + g_return_val_if_fail(event != NULL, FALSE); + + canvas = GANV_CANVAS(widget); + + if (event->window != canvas->layout.bin_window) { + return FALSE; + } + + canvas->state = event->state; + pick_current_item(canvas, (GdkEvent*)event); + return ganv_canvas_emit_event(canvas, (GdkEvent*)event); +} + +static gboolean +ganv_canvas_scroll(GtkWidget* widget, GdkEventScroll* event) +{ + GanvCanvas* canvas; + + g_return_val_if_fail(GANV_IS_CANVAS(widget), FALSE); + g_return_val_if_fail(event != NULL, FALSE); + + canvas = GANV_CANVAS(widget); + + if (event->window != canvas->layout.bin_window) { + return FALSE; + } + + canvas->state = event->state; + pick_current_item(canvas, (GdkEvent*)event); + return ganv_canvas_emit_event(canvas, (GdkEvent*)event); +} + +/* Key event handler for the canvas */ +static gboolean +ganv_canvas_key(GtkWidget* widget, GdkEventKey* event) +{ + GanvCanvas* canvas; + + g_return_val_if_fail(GANV_IS_CANVAS(widget), FALSE); + g_return_val_if_fail(event != NULL, FALSE); + + canvas = GANV_CANVAS(widget); + + if (!ganv_canvas_emit_event(canvas, (GdkEvent*)event)) { + GtkWidgetClass* widget_class; + + widget_class = GTK_WIDGET_CLASS(canvas_parent_class); + + if (event->type == GDK_KEY_PRESS) { + if (widget_class->key_press_event) { + return (*widget_class->key_press_event)(widget, event); + } + } else if (event->type == GDK_KEY_RELEASE) { + if (widget_class->key_release_event) { + return (*widget_class->key_release_event)(widget, event); + } + } else { + g_assert_not_reached(); + } + + return FALSE; + } else { + return TRUE; + } +} + +/* Crossing event handler for the canvas */ +static gint +ganv_canvas_crossing(GtkWidget* widget, GdkEventCrossing* event) +{ + GanvCanvas* canvas; + + g_return_val_if_fail(GANV_IS_CANVAS(widget), FALSE); + g_return_val_if_fail(event != NULL, FALSE); + + canvas = GANV_CANVAS(widget); + + if (event->window != canvas->layout.bin_window) { + return FALSE; + } + + canvas->state = event->state; + return pick_current_item(canvas, (GdkEvent*)event); +} + +/* Focus in handler for the canvas */ +static gint +ganv_canvas_focus_in(GtkWidget* widget, GdkEventFocus* event) +{ + GanvCanvas* canvas; + + GTK_WIDGET_SET_FLAGS(widget, GTK_HAS_FOCUS); + + canvas = GANV_CANVAS(widget); + + if (canvas->focused_item) { + return ganv_canvas_emit_event(canvas, (GdkEvent*)event); + } else { + return FALSE; + } +} + +/* Focus out handler for the canvas */ +static gint +ganv_canvas_focus_out(GtkWidget* widget, GdkEventFocus* event) +{ + GanvCanvas* canvas; + + GTK_WIDGET_UNSET_FLAGS(widget, GTK_HAS_FOCUS); + + canvas = GANV_CANVAS(widget); + + if (canvas->focused_item) { + return ganv_canvas_emit_event(canvas, (GdkEvent*)event); + } else { + return FALSE; + } +} + +#define REDRAW_QUANTUM_SIZE 512 + +static void +ganv_canvas_paint_rect(GanvCanvas* canvas, gint x0, gint y0, gint x1, gint y1) +{ + gint draw_x1, draw_y1; + gint draw_x2, draw_y2; + gint draw_width, draw_height; + + g_return_if_fail(!canvas->need_update); + + draw_x1 = MAX(x0, canvas->layout.hadjustment->value - canvas->zoom_xofs); + draw_y1 = MAX(y0, canvas->layout.vadjustment->value - canvas->zoom_yofs); + draw_x2 = MIN(draw_x1 + GTK_WIDGET(canvas)->allocation.width, x1); + draw_y2 = MIN(draw_y1 + GTK_WIDGET(canvas)->allocation.height, y1); + + draw_width = draw_x2 - draw_x1; + draw_height = draw_y2 - draw_y1; + + if ((draw_width < 1) || (draw_height < 1)) { + return; + } + + canvas->redraw_x1 = draw_x1; + canvas->redraw_y1 = draw_y1; + canvas->redraw_x2 = draw_x2; + canvas->redraw_y2 = draw_y2; + canvas->draw_xofs = draw_x1; + canvas->draw_yofs = draw_y1; + + cairo_t* cr = gdk_cairo_create(canvas->layout.bin_window); + + double wx, wy; + ganv_canvas_window_to_world(canvas, 0, 0, &wx, &wy); + cairo_translate(cr, -wx, -wy); + + if (canvas->root->object.flags & GANV_ITEM_VISIBLE) { + (*GANV_ITEM_GET_CLASS(canvas->root)->draw)( + canvas->root, cr, + draw_x1, draw_y1, + draw_width, draw_height); + } + + cairo_destroy(cr); +} + +/* Expose handler for the canvas */ +static gint +ganv_canvas_expose(GtkWidget* widget, GdkEventExpose* event) +{ + GanvCanvas* canvas = GANV_CANVAS(widget); + if (!GTK_WIDGET_DRAWABLE(widget) || + (event->window != canvas->layout.bin_window)) { + return FALSE; + } + + /* Find a single bounding rectangle for all rectangles in the region. + Since drawing the root group is O(n) and thus very expensive for large + canvases, it's much faster to do a single paint than many, even though + more area may be painted that way. With a better group implementation, + it would likely be better to paint each changed rectangle separately. */ + GdkRectangle clip; + gdk_region_get_clipbox(event->region, &clip); + + const int x2 = clip.x + clip.width; + const int y2 = clip.y + clip.height; + + if (canvas->need_update || canvas->need_redraw) { + /* Update or drawing is scheduled, so just mark exposed area as dirty */ + ganv_canvas_request_redraw(canvas, clip.x, clip.y, x2, y2); + } else { + /* No pending updates, draw exposed area immediately */ + ganv_canvas_paint_rect(canvas, clip.x, clip.y, x2, y2); + + /* And call expose on parent container class */ + if (GTK_WIDGET_CLASS(canvas_parent_class)->expose_event) { + (*GTK_WIDGET_CLASS(canvas_parent_class)->expose_event)( + widget, event); + } + } + + return FALSE; +} + +/* Repaints the areas in the canvas that need it */ +static void +paint(GanvCanvas* canvas) +{ + for (GSList* l = canvas->redraw_region; l; l = l->next) { + IRect* rect = (IRect*)l->data; + + const GdkRectangle gdkrect = { + rect->x + canvas->zoom_xofs, + rect->y + canvas->zoom_yofs, + rect->width, + rect->height + }; + + gdk_window_invalidate_rect(canvas->layout.bin_window, &gdkrect, FALSE); + g_free(rect); + } + + g_slist_free(canvas->redraw_region); + canvas->redraw_region = NULL; + canvas->need_redraw = FALSE; + + canvas->redraw_x1 = 0; + canvas->redraw_y1 = 0; + canvas->redraw_x2 = 0; + canvas->redraw_y2 = 0; +} + +static void +do_update(GanvCanvas* canvas) +{ + /* Cause the update if necessary */ + +update_again: + if (canvas->need_update) { + ganv_item_invoke_update(canvas->root, 0); + + canvas->need_update = FALSE; + } + + /* Pick new current item */ + + while (canvas->need_repick) { + canvas->need_repick = FALSE; + pick_current_item(canvas, &canvas->pick_event); + } + + /* it is possible that during picking we emitted an event in which + the user then called some function which then requested update + of something. Without this we'd be left in a state where + need_update would have been left TRUE and the canvas would have + been left unpainted. */ + if (canvas->need_update) { + goto update_again; + } + + /* Paint if able to */ + + if (GTK_WIDGET_DRAWABLE(canvas) && canvas->need_redraw) { + paint(canvas); + } +} + +/* Idle handler for the canvas. It deals with pending updates and redraws. */ +static gboolean +idle_handler(gpointer data) +{ + GanvCanvas* canvas; + + GDK_THREADS_ENTER(); + + canvas = GANV_CANVAS(data); + + do_update(canvas); + + /* Reset idle id */ + canvas->idle_id = 0; + + GDK_THREADS_LEAVE(); + + return FALSE; +} + +/* Convenience function to add an idle handler to a canvas */ +static void +add_idle(GanvCanvas* canvas) +{ + g_assert(canvas->need_update || canvas->need_redraw); + + if (!canvas->idle_id) { + canvas->idle_id = g_idle_add_full(CANVAS_IDLE_PRIORITY, + idle_handler, + canvas, + NULL); + } + + /* canvas->idle_id = gtk_idle_add (idle_handler, canvas); */ +} + +/** + * ganv_canvas_root: + * @canvas: A canvas. + * + * Return value: (transfer none): The root group of the canvas. + */ +GanvItem* +ganv_canvas_root(GanvCanvas* canvas) +{ + g_return_val_if_fail(GANV_IS_CANVAS(canvas), NULL); + + return canvas->root; +} + +/** + * ganv_canvas_set_scroll_region: + * @canvas: A canvas. + * @x1: Leftmost limit of the scrolling region. + * @y1: Upper limit of the scrolling region. + * @x2: Rightmost limit of the scrolling region. + * @y2: Lower limit of the scrolling region. + * + * Sets the scrolling region of a canvas to the specified rectangle. The canvas + * will then be able to scroll only within this region. The view of the canvas + * is adjusted as appropriate to display as much of the new region as possible. + **/ +void +ganv_canvas_set_scroll_region(GanvCanvas* canvas, double x1, double y1, double x2, + double y2) +{ + double wxofs, wyofs; + int xofs, yofs; + + g_return_if_fail(GANV_IS_CANVAS(canvas)); + + /* + * Set the new scrolling region. If possible, do not move the visible contents of the + * canvas. + */ + + ganv_canvas_c2w(canvas, + GTK_LAYOUT(canvas)->hadjustment->value + canvas->zoom_xofs, + GTK_LAYOUT(canvas)->vadjustment->value + canvas->zoom_yofs, + /*canvas->zoom_xofs, + canvas->zoom_yofs,*/ + &wxofs, &wyofs); + + canvas->scroll_x1 = x1; + canvas->scroll_y1 = y1; + canvas->scroll_x2 = x2; + canvas->scroll_y2 = y2; + + ganv_canvas_w2c(canvas, wxofs, wyofs, &xofs, &yofs); + + scroll_to(canvas, xofs, yofs); + + canvas->need_repick = TRUE; +#if 0 + /* todo: should be requesting update */ + (*GANV_ITEM_CLASS(canvas->root->object.klass)->update)( + canvas->root, NULL, NULL, 0); +#endif +} + +/** + * ganv_canvas_get_scroll_region: + * @canvas: A canvas. + * @x1: Leftmost limit of the scrolling region (return value). + * @y1: Upper limit of the scrolling region (return value). + * @x2: Rightmost limit of the scrolling region (return value). + * @y2: Lower limit of the scrolling region (return value). + * + * Queries the scrolling region of a canvas. + **/ +void +ganv_canvas_get_scroll_region(GanvCanvas* canvas, double* x1, double* y1, double* x2, + double* y2) +{ + g_return_if_fail(GANV_IS_CANVAS(canvas)); + + if (x1) { + *x1 = canvas->scroll_x1; + } + + if (y1) { + *y1 = canvas->scroll_y1; + } + + if (x2) { + *x2 = canvas->scroll_x2; + } + + if (y2) { + *y2 = canvas->scroll_y2; + } +} + +/** + * ganv_canvas_set_center_scroll_region: + * @canvas: A canvas. + * @center_scroll_region: Whether to center the scrolling region in the canvas + * window when it is smaller than the canvas' allocation. + * + * When the scrolling region of the canvas is smaller than the canvas window, + * e.g. the allocation of the canvas, it can be either centered on the window + * or simply made to be on the upper-left corner on the window. This function + * lets you configure this property. + **/ +void +ganv_canvas_set_center_scroll_region(GanvCanvas* canvas, gboolean center_scroll_region) +{ + g_return_if_fail(GANV_IS_CANVAS(canvas)); + + canvas->center_scroll_region = center_scroll_region != 0; + + scroll_to(canvas, + canvas->layout.hadjustment->value, + canvas->layout.vadjustment->value); +} + +/** + * ganv_canvas_get_center_scroll_region: + * @canvas: A canvas. + * + * Returns whether the canvas is set to center the scrolling region in the window + * if the former is smaller than the canvas' allocation. + * + * Return value: Whether the scroll region is being centered in the canvas window. + **/ +gboolean +ganv_canvas_get_center_scroll_region(GanvCanvas* canvas) +{ + g_return_val_if_fail(GANV_IS_CANVAS(canvas), FALSE); + + return canvas->center_scroll_region ? TRUE : FALSE; +} + +/** + * ganv_canvas_set_pixels_per_unit: + * @canvas: A canvas. + * @n: The number of pixels that correspond to one canvas unit. + * + * Sets the zooming factor of a canvas by specifying the number of pixels that + * correspond to one canvas unit. + * + * The anchor point for zooming, i.e. the point that stays fixed and all others + * zoom inwards or outwards from it, depends on whether the canvas is set to + * center the scrolling region or not. You can control this using the + * ganv_canvas_set_center_scroll_region() function. If the canvas is set to + * center the scroll region, then the center of the canvas window is used as the + * anchor point for zooming. Otherwise, the upper-left corner of the canvas + * window is used as the anchor point. + **/ +void +ganv_canvas_set_pixels_per_unit(GanvCanvas* canvas, double n) +{ + double ax, ay; + int x1, y1; + int anchor_x, anchor_y; + + g_return_if_fail(GANV_IS_CANVAS(canvas)); + g_return_if_fail(n > 1e-10); + + if (canvas->center_scroll_region) { + anchor_x = GTK_WIDGET(canvas)->allocation.width / 2; + anchor_y = GTK_WIDGET(canvas)->allocation.height / 2; + } else { + anchor_x = anchor_y = 0; + } + + /* Find the coordinates of the anchor point in units. */ + if (canvas->layout.hadjustment) { + ax + = (canvas->layout.hadjustment->value + + anchor_x) / canvas->pixels_per_unit + canvas->scroll_x1 + canvas->zoom_xofs; + } else { + ax = (0.0 + anchor_x) / canvas->pixels_per_unit + canvas->scroll_x1 + canvas->zoom_xofs; + } + if (canvas->layout.hadjustment) { + ay + = (canvas->layout.vadjustment->value + + anchor_y) / canvas->pixels_per_unit + canvas->scroll_y1 + canvas->zoom_yofs; + } else { + ay = (0.0 + anchor_y) / canvas->pixels_per_unit + canvas->scroll_y1 + canvas->zoom_yofs; + } + + /* Now calculate the new offset of the upper left corner. */ + x1 = ((ax - canvas->scroll_x1) * n) - anchor_x; + y1 = ((ay - canvas->scroll_y1) * n) - anchor_y; + + canvas->pixels_per_unit = n; + + scroll_to(canvas, x1, y1); + + ganv_canvas_request_update(canvas); + canvas->need_repick = TRUE; +} + +/** + * ganv_canvas_scroll_to: + * @canvas: A canvas. + * @cx: Horizontal scrolling offset in canvas pixel units. + * @cy: Vertical scrolling offset in canvas pixel units. + * + * Makes a canvas scroll to the specified offsets, given in canvas pixel units. + * The canvas will adjust the view so that it is not outside the scrolling + * region. This function is typically not used, as it is better to hook + * scrollbars to the canvas layout's scrolling adjusments. + **/ +void +ganv_canvas_scroll_to(GanvCanvas* canvas, int cx, int cy) +{ + g_return_if_fail(GANV_IS_CANVAS(canvas)); + + scroll_to(canvas, cx, cy); +} + +/** + * ganv_canvas_get_scroll_offsets: + * @canvas: A canvas. + * @cx: Horizontal scrolling offset (return value). + * @cy: Vertical scrolling offset (return value). + * + * Queries the scrolling offsets of a canvas. The values are returned in canvas + * pixel units. + **/ +void +ganv_canvas_get_scroll_offsets(GanvCanvas* canvas, int* cx, int* cy) +{ + g_return_if_fail(GANV_IS_CANVAS(canvas)); + + if (cx) { + *cx = canvas->layout.hadjustment->value; + } + + if (cy) { + *cy = canvas->layout.vadjustment->value; + } +} + +/** + * ganv_canvas_get_item_at: + * @canvas: A canvas. + * @x: X position in world coordinates. + * @y: Y position in world coordinates. + * + * Looks for the item that is under the specified position, which must be + * specified in world coordinates. + * + * Return value: (transfer none): The sought item, or NULL if no item is at the + * specified coordinates. + **/ +GanvItem* +ganv_canvas_get_item_at(GanvCanvas* canvas, double x, double y) +{ + GanvItem* item; + double dist; + int cx, cy; + + g_return_val_if_fail(GANV_IS_CANVAS(canvas), NULL); + + ganv_canvas_w2c(canvas, x, y, &cx, &cy); + + dist = GANV_ITEM_GET_CLASS(canvas->root)->point( + canvas->root, + x - canvas->root->x, y - canvas->root->y, + cx, cy, + &item); + if ((int)(dist * canvas->pixels_per_unit + 0.5) <= canvas->close_enough) { + return item; + } else { + return NULL; + } +} + +/* Queues an update of the canvas */ +void +ganv_canvas_request_update(GanvCanvas* canvas) +{ + GANV_CANVAS_GET_CLASS(canvas)->request_update(canvas); +} + +static void +ganv_canvas_request_update_real(GanvCanvas* canvas) +{ + if (canvas->need_update) { + return; + } + + canvas->need_update = TRUE; + if (GTK_WIDGET_MAPPED((GtkWidget*)canvas)) { + add_idle(canvas); + } +} + +static inline gboolean +rect_overlaps(const IRect* a, const IRect* b) +{ + if ((a->x > b->x + b->width) || + (a->y > b->y + b->height) || + (a->x + a->width < b->x) || + (a->y + a->height < b->y)) { + return FALSE; + } + return TRUE; +} + +static inline gboolean +rect_is_visible(GanvCanvas* canvas, const IRect* r) +{ + const IRect rect = { + (int)(canvas->layout.hadjustment->value - canvas->zoom_xofs), + (int)(canvas->layout.vadjustment->value - canvas->zoom_yofs), + GTK_WIDGET(canvas)->allocation.width, + GTK_WIDGET(canvas)->allocation.height + }; + + return rect_overlaps(&rect, r); +} + +/** + * ganv_canvas_request_redraw: + * @canvas: A canvas. + * @x1: Leftmost coordinate of the rectangle to be redrawn. + * @y1: Upper coordinate of the rectangle to be redrawn. + * @x2: Rightmost coordinate of the rectangle to be redrawn, plus 1. + * @y2: Lower coordinate of the rectangle to be redrawn, plus 1. + * + * Informs a canvas that the specified area needs to be repainted. To be used + * only by item implementations. + **/ +void +ganv_canvas_request_redraw(GanvCanvas* canvas, int x1, int y1, int x2, int y2) +{ + g_return_if_fail(GANV_IS_CANVAS(canvas)); + + if (!GTK_WIDGET_DRAWABLE(canvas) || (x1 >= x2) || (y1 >= y2)) { + return; + } + + const IRect rect = { x1, y1, x2 - x1, y2 - y1 }; + + if (!rect_is_visible(canvas, &rect)) { + return; + } + + IRect* r = (IRect*)g_malloc(sizeof(IRect)); + *r = rect; + + canvas->redraw_region = g_slist_prepend(canvas->redraw_region, r); + canvas->need_redraw = TRUE; + + if (canvas->idle_id == 0) { + add_idle(canvas); + } +} + +/** + * ganv_canvas_w2c_affine: + * @canvas: A canvas. + * @matrix: An affine transformation matrix (return value). + * + * Gets the affine transform that converts from world coordinates to canvas + * pixel coordinates. + **/ +void +ganv_canvas_w2c_affine(GanvCanvas* canvas, cairo_matrix_t* matrix) +{ + g_return_if_fail(GANV_IS_CANVAS(canvas)); + g_return_if_fail(matrix != NULL); + + cairo_matrix_init_translate(matrix, + -canvas->scroll_x1, + -canvas->scroll_y1); + + cairo_matrix_scale(matrix, + canvas->pixels_per_unit, + canvas->pixels_per_unit); +} + +/** + * ganv_canvas_w2c: + * @canvas: A canvas. + * @wx: World X coordinate. + * @wy: World Y coordinate. + * @cx: X pixel coordinate (return value). + * @cy: Y pixel coordinate (return value). + * + * Converts world coordinates into canvas pixel coordinates. + **/ +void +ganv_canvas_w2c(GanvCanvas* canvas, double wx, double wy, int* cx, int* cy) +{ + g_return_if_fail(GANV_IS_CANVAS(canvas)); + + cairo_matrix_t matrix; + ganv_canvas_w2c_affine(canvas, &matrix); + + cairo_matrix_transform_point(&matrix, &wx, &wy); + + if (cx) { + *cx = floor(wx + 0.5); + } + if (cy) { + *cy = floor(wy + 0.5); + } +} + +/** + * ganv_canvas_w2c_d: + * @canvas: A canvas. + * @wx: World X coordinate. + * @wy: World Y coordinate. + * @cx: X pixel coordinate (return value). + * @cy: Y pixel coordinate (return value). + * + * Converts world coordinates into canvas pixel coordinates. This + * version returns coordinates in floating point coordinates, for + * greater precision. + **/ +void +ganv_canvas_w2c_d(GanvCanvas* canvas, double wx, double wy, double* cx, double* cy) +{ + g_return_if_fail(GANV_IS_CANVAS(canvas)); + + cairo_matrix_t matrix; + ganv_canvas_w2c_affine(canvas, &matrix); + + cairo_matrix_transform_point(&matrix, &wx, &wy); + + if (cx) { + *cx = wx; + } + if (cy) { + *cy = wy; + } +} + +/** + * ganv_canvas_c2w: + * @canvas: A canvas. + * @cx: Canvas pixel X coordinate. + * @cy: Canvas pixel Y coordinate. + * @wx: X world coordinate (return value). + * @wy: Y world coordinate (return value). + * + * Converts canvas pixel coordinates to world coordinates. + **/ +void +ganv_canvas_c2w(GanvCanvas* canvas, int cx, int cy, double* wx, double* wy) +{ + g_return_if_fail(GANV_IS_CANVAS(canvas)); + + cairo_matrix_t matrix; + ganv_canvas_w2c_affine(canvas, &matrix); + cairo_matrix_invert(&matrix); + + double x = cx; + double y = cy; + cairo_matrix_transform_point(&matrix, &x, &y); + + if (wx) { + *wx = x; + } + if (wy) { + *wy = y; + } +} + +/** + * ganv_canvas_window_to_world: + * @canvas: A canvas. + * @winx: Window-relative X coordinate. + * @winy: Window-relative Y coordinate. + * @worldx: X world coordinate (return value). + * @worldy: Y world coordinate (return value). + * + * Converts window-relative coordinates into world coordinates. You can use + * this when you need to convert mouse coordinates into world coordinates, for + * example. + **/ +void +ganv_canvas_window_to_world(GanvCanvas* canvas, double winx, double winy, + double* worldx, double* worldy) +{ + g_return_if_fail(GANV_IS_CANVAS(canvas)); + + if (worldx) { + *worldx = canvas->scroll_x1 + ((winx - canvas->zoom_xofs) + / canvas->pixels_per_unit); + } + + if (worldy) { + *worldy = canvas->scroll_y1 + ((winy - canvas->zoom_yofs) + / canvas->pixels_per_unit); + } +} + +/** + * ganv_canvas_world_to_window: + * @canvas: A canvas. + * @worldx: World X coordinate. + * @worldy: World Y coordinate. + * @winx: X window-relative coordinate. + * @winy: Y window-relative coordinate. + * + * Converts world coordinates into window-relative coordinates. + **/ +void +ganv_canvas_world_to_window(GanvCanvas* canvas, double worldx, double worldy, + double* winx, double* winy) +{ + g_return_if_fail(GANV_IS_CANVAS(canvas)); + + if (winx) { + *winx = (canvas->pixels_per_unit) * (worldx - canvas->scroll_x1) + canvas->zoom_xofs; + } + + if (winy) { + *winy = (canvas->pixels_per_unit) * (worldy - canvas->scroll_y1) + canvas->zoom_yofs; + } +} + } // extern "C" diff --git a/src/box.c b/src/box.c index 8847066..381eb9c 100644 --- a/src/box.c +++ b/src/box.c @@ -153,7 +153,7 @@ ganv_box_request_redraw(GanvItem* item, ganv_item_i2w_pair(item, &x1, &y1, &x2, &y2); } - ganv_canvas_base_request_redraw(item->canvas, x1, y1, x2, y2); + ganv_canvas_request_redraw(item->canvas, x1, y1, x2, y2); } static void @@ -198,8 +198,8 @@ ganv_box_update(GanvItem* item, int flags) ganv_item_i2w_pair(item, &x1, &y1, &x2, &y2); // Update item canvas coordinates - ganv_canvas_base_w2c_d(GANV_CANVAS_BASE(item->canvas), x1, y1, &item->x1, &item->y1); - ganv_canvas_base_w2c_d(GANV_CANVAS_BASE(item->canvas), x2, y2, &item->x2, &item->y2); + ganv_canvas_w2c_d(item->canvas, x1, y1, &item->x1, &item->y1); + ganv_canvas_w2c_d(item->canvas, x2, y2, &item->x2, &item->y2); // Request redraw of new location ganv_box_request_redraw(item, &impl->coords, FALSE); @@ -337,9 +337,9 @@ ganv_box_is_within(const GanvNode* self, ganv_item_i2w_pair(GANV_ITEM(self), &bx1, &by1, &bx2, &by2); return ( bx1 >= x1 - && by2 >= y1 - && bx2 <= x2 - && by2 <= y2); + && by2 >= y1 + && bx2 <= x2 + && by2 <= y2); } static void diff --git a/src/canvas-base.c b/src/canvas-base.c deleted file mode 100644 index 9691bff..0000000 --- a/src/canvas-base.c +++ /dev/null @@ -1,1728 +0,0 @@ -/* This file is part of Ganv. - * Copyright 2007-2013 David Robillard - * - * Ganv 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 3 of the License, or any later version. - * - * Ganv 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 details. - * - * You should have received a copy of the GNU General Public License along - * with Ganv. If not, see . - */ - -/* Based on GnomeCanvas, by Federico Mena - * and Raph Levien - * Copyright 1997-2000 Free Software Foundation - */ - -#include -#include -#include - -#include -#include - -#include "ganv/canvas-base.h" -#include "ganv/group.h" - -#include "./ganv-marshal.h" -#include "./ganv-private.h" -#include "./gettext.h" - -/* We must run our idle update handler *before* GDK wants to redraw. */ -#define CANVAS_IDLE_PRIORITY (GDK_PRIORITY_REDRAW - 5) - -static void add_idle(GanvCanvasBase* canvas); - -typedef struct { - int x; - int y; - int width; - int height; -} IRect; - -/*** GanvCanvasBase ***/ - -static void ganv_canvas_base_destroy(GtkObject* object); -static void ganv_canvas_base_map(GtkWidget* widget); -static void ganv_canvas_base_unmap(GtkWidget* widget); -static void ganv_canvas_base_realize(GtkWidget* widget); -static void ganv_canvas_base_unrealize(GtkWidget* widget); -static void ganv_canvas_base_size_allocate(GtkWidget* widget, - GtkAllocation* allocation); -static gint ganv_canvas_base_button(GtkWidget* widget, - GdkEventButton* event); -static gint ganv_canvas_base_motion(GtkWidget* widget, - GdkEventMotion* event); -static gint ganv_canvas_base_expose(GtkWidget* widget, - GdkEventExpose* event); -static gboolean ganv_canvas_base_key(GtkWidget* widget, - GdkEventKey* event); -static gboolean ganv_canvas_base_scroll(GtkWidget* widget, - GdkEventScroll* event); -static gint ganv_canvas_base_crossing(GtkWidget* widget, - GdkEventCrossing* event); -static gint ganv_canvas_base_focus_in(GtkWidget* widget, - GdkEventFocus* event); -static gint ganv_canvas_base_focus_out(GtkWidget* widget, - GdkEventFocus* event); -static void ganv_canvas_base_request_update_real(GanvCanvasBase* canvas); - -static GtkLayoutClass* canvas_parent_class; - -enum { - PROP_FOCUSED_ITEM = 1 -}; - -G_DEFINE_TYPE(GanvCanvasBase, ganv_canvas_base, GTK_TYPE_LAYOUT) - -static void -ganv_canvas_base_get_property(GObject* object, - guint prop_id, - GValue* value, - GParamSpec* pspec) -{ - switch (prop_id) { - case PROP_FOCUSED_ITEM: - g_value_set_object(value, GANV_CANVAS_BASE(object)->focused_item); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); - break; - } -} - -static void -ganv_canvas_base_set_property(GObject* object, - guint prop_id, - const GValue* value, - GParamSpec* pspec) -{ - switch (prop_id) { - case PROP_FOCUSED_ITEM: - GANV_CANVAS_BASE(object)->focused_item = g_value_get_object(value); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); - break; - } -} - -/* Class initialization function for GanvCanvasBaseClass */ -static void -ganv_canvas_base_class_init(GanvCanvasBaseClass* klass) -{ - GObjectClass* gobject_class; - GtkObjectClass* object_class; - GtkWidgetClass* widget_class; - - gobject_class = (GObjectClass*)klass; - object_class = (GtkObjectClass*)klass; - widget_class = (GtkWidgetClass*)klass; - - canvas_parent_class = g_type_class_peek_parent(klass); - - gobject_class->set_property = ganv_canvas_base_set_property; - gobject_class->get_property = ganv_canvas_base_get_property; - - object_class->destroy = ganv_canvas_base_destroy; - - widget_class->map = ganv_canvas_base_map; - widget_class->unmap = ganv_canvas_base_unmap; - widget_class->realize = ganv_canvas_base_realize; - widget_class->unrealize = ganv_canvas_base_unrealize; - widget_class->size_allocate = ganv_canvas_base_size_allocate; - widget_class->button_press_event = ganv_canvas_base_button; - widget_class->button_release_event = ganv_canvas_base_button; - widget_class->motion_notify_event = ganv_canvas_base_motion; - widget_class->expose_event = ganv_canvas_base_expose; - widget_class->key_press_event = ganv_canvas_base_key; - widget_class->key_release_event = ganv_canvas_base_key; - widget_class->enter_notify_event = ganv_canvas_base_crossing; - widget_class->leave_notify_event = ganv_canvas_base_crossing; - widget_class->focus_in_event = ganv_canvas_base_focus_in; - widget_class->focus_out_event = ganv_canvas_base_focus_out; - widget_class->scroll_event = ganv_canvas_base_scroll; - - klass->request_update = ganv_canvas_base_request_update_real; - - g_object_class_install_property(gobject_class, PROP_FOCUSED_ITEM, - g_param_spec_object("focused_item", NULL, NULL, - GANV_TYPE_ITEM, - (G_PARAM_READABLE | G_PARAM_WRITABLE))); -} - -/* Callback used when the root item of a canvas is destroyed. The user should - * never ever do this, so we panic if this happens. - */ -static void -panic_root_destroyed(GtkObject* object, gpointer data) -{ - g_error("Eeeek, root item %p of canvas %p was destroyed!", (void*)object, data); -} - -/* Object initialization function for GanvCanvasBase */ -static void -ganv_canvas_base_init(GanvCanvasBase* canvas) -{ - GTK_WIDGET_SET_FLAGS(canvas, GTK_CAN_FOCUS); - - canvas->need_update = FALSE; - canvas->need_redraw = FALSE; - canvas->redraw_region = NULL; - canvas->idle_id = 0; - - canvas->scroll_x1 = 0.0; - canvas->scroll_y1 = 0.0; - canvas->scroll_x2 = canvas->layout.width; - canvas->scroll_y2 = canvas->layout.height; - - canvas->pixels_per_unit = 1.0; - - canvas->pick_event.type = GDK_LEAVE_NOTIFY; - canvas->pick_event.crossing.x = 0; - canvas->pick_event.crossing.y = 0; - - canvas->center_scroll_region = TRUE; - - gtk_layout_set_hadjustment(GTK_LAYOUT(canvas), NULL); - gtk_layout_set_vadjustment(GTK_LAYOUT(canvas), NULL); - - /* Create the root item as a special case */ - canvas->root = GANV_ITEM(g_object_new(ganv_group_get_type(), NULL)); - canvas->root->canvas = canvas; - - g_object_ref_sink(canvas->root); - - canvas->root_destroy_id = g_signal_connect(canvas->root, "destroy", - G_CALLBACK(panic_root_destroyed), - canvas); - - canvas->need_repick = TRUE; -} - -/* Convenience function to remove the idle handler of a canvas */ -static void -remove_idle(GanvCanvasBase* canvas) -{ - if (canvas->idle_id == 0) { - return; - } - - g_source_remove(canvas->idle_id); - canvas->idle_id = 0; -} - -/* Removes the transient state of the canvas (idle handler, grabs). */ -static void -shutdown_transients(GanvCanvasBase* canvas) -{ - /* We turn off the need_redraw flag, since if the canvas is mapped again - * it will request a redraw anyways. We do not turn off the need_update - * flag, though, because updates are not queued when the canvas remaps - * itself. - */ - if (canvas->need_redraw) { - canvas->need_redraw = FALSE; - g_slist_foreach(canvas->redraw_region, (GFunc)g_free, NULL); - g_slist_free(canvas->redraw_region); - canvas->redraw_region = NULL; - canvas->redraw_x1 = 0; - canvas->redraw_y1 = 0; - canvas->redraw_x2 = 0; - canvas->redraw_y2 = 0; - } - - if (canvas->grabbed_item) { - canvas->grabbed_item = NULL; - gdk_pointer_ungrab(GDK_CURRENT_TIME); - } - - remove_idle(canvas); -} - -/* Destroy handler for GanvCanvasBase */ -static void -ganv_canvas_base_destroy(GtkObject* object) -{ - GanvCanvasBase* canvas; - - g_return_if_fail(GANV_IS_CANVAS_BASE(object)); - - /* remember, destroy can be run multiple times! */ - - canvas = GANV_CANVAS_BASE(object); - - if (canvas->root_destroy_id) { - g_signal_handler_disconnect(canvas->root, canvas->root_destroy_id); - canvas->root_destroy_id = 0; - } - if (canvas->root) { - gtk_object_destroy(GTK_OBJECT(canvas->root)); - g_object_unref(G_OBJECT(canvas->root)); - canvas->root = NULL; - } - - shutdown_transients(canvas); - - if (GTK_OBJECT_CLASS(canvas_parent_class)->destroy) { - (*GTK_OBJECT_CLASS(canvas_parent_class)->destroy)(object); - } -} - -/** - * ganv_canvas_base_new: - * - * Creates a new empty canvas in non-antialiased mode. - * - * Return value: A newly-created canvas. - **/ -GtkWidget* -ganv_canvas_base_new(void) -{ - return GTK_WIDGET(g_object_new(ganv_canvas_base_get_type(), NULL)); -} - -/* Map handler for the canvas */ -static void -ganv_canvas_base_map(GtkWidget* widget) -{ - GanvCanvasBase* canvas; - - g_return_if_fail(GANV_IS_CANVAS_BASE(widget)); - - /* Normal widget mapping stuff */ - - if (GTK_WIDGET_CLASS(canvas_parent_class)->map) { - (*GTK_WIDGET_CLASS(canvas_parent_class)->map)(widget); - } - - canvas = GANV_CANVAS_BASE(widget); - - if (canvas->need_update) { - add_idle(canvas); - } - - /* Map items */ - - if (GANV_ITEM_GET_CLASS(canvas->root)->map) { - (*GANV_ITEM_GET_CLASS(canvas->root)->map)(canvas->root); - } -} - -/* Unmap handler for the canvas */ -static void -ganv_canvas_base_unmap(GtkWidget* widget) -{ - GanvCanvasBase* canvas; - - g_return_if_fail(GANV_IS_CANVAS_BASE(widget)); - - canvas = GANV_CANVAS_BASE(widget); - - shutdown_transients(canvas); - - /* Unmap items */ - - if (GANV_ITEM_GET_CLASS(canvas->root)->unmap) { - (*GANV_ITEM_GET_CLASS(canvas->root)->unmap)(canvas->root); - } - - /* Normal widget unmapping stuff */ - - if (GTK_WIDGET_CLASS(canvas_parent_class)->unmap) { - (*GTK_WIDGET_CLASS(canvas_parent_class)->unmap)(widget); - } -} - -/* Realize handler for the canvas */ -static void -ganv_canvas_base_realize(GtkWidget* widget) -{ - GanvCanvasBase* canvas; - - g_return_if_fail(GANV_IS_CANVAS_BASE(widget)); - - /* Normal widget realization stuff */ - - if (GTK_WIDGET_CLASS(canvas_parent_class)->realize) { - (*GTK_WIDGET_CLASS(canvas_parent_class)->realize)(widget); - } - - canvas = GANV_CANVAS_BASE(widget); - - gdk_window_set_events(canvas->layout.bin_window, - (gdk_window_get_events(canvas->layout.bin_window) - | GDK_EXPOSURE_MASK - | GDK_BUTTON_PRESS_MASK - | GDK_BUTTON_RELEASE_MASK - | GDK_POINTER_MOTION_MASK - | GDK_KEY_PRESS_MASK - | GDK_KEY_RELEASE_MASK - | GDK_ENTER_NOTIFY_MASK - | GDK_LEAVE_NOTIFY_MASK - | GDK_FOCUS_CHANGE_MASK)); - - /* Create our own temporary pixmap gc and realize all the items */ - - canvas->pixmap_gc = gdk_gc_new(canvas->layout.bin_window); - - (*GANV_ITEM_GET_CLASS(canvas->root)->realize)(canvas->root); -} - -/* Unrealize handler for the canvas */ -static void -ganv_canvas_base_unrealize(GtkWidget* widget) -{ - GanvCanvasBase* canvas; - - g_return_if_fail(GANV_IS_CANVAS_BASE(widget)); - - canvas = GANV_CANVAS_BASE(widget); - - shutdown_transients(canvas); - - /* Unrealize items and parent widget */ - - (*GANV_ITEM_GET_CLASS(canvas->root)->unrealize)(canvas->root); - - g_object_unref(canvas->pixmap_gc); - canvas->pixmap_gc = NULL; - - if (GTK_WIDGET_CLASS(canvas_parent_class)->unrealize) { - (*GTK_WIDGET_CLASS(canvas_parent_class)->unrealize)(widget); - } -} - -/* Handles scrolling of the canvas. Adjusts the scrolling and zooming offset to - * keep as much as possible of the canvas scrolling region in view. - */ -static void -scroll_to(GanvCanvasBase* canvas, int cx, int cy) -{ - int scroll_width, scroll_height; - int right_limit, bottom_limit; - int old_zoom_xofs, old_zoom_yofs; - int changed_x = FALSE, changed_y = FALSE; - int canvas_width, canvas_height; - - canvas_width = GTK_WIDGET(canvas)->allocation.width; - canvas_height = GTK_WIDGET(canvas)->allocation.height; - - scroll_width = floor((canvas->scroll_x2 - canvas->scroll_x1) * canvas->pixels_per_unit - + 0.5); - scroll_height = floor((canvas->scroll_y2 - canvas->scroll_y1) * canvas->pixels_per_unit - + 0.5); - - right_limit = scroll_width - canvas_width; - bottom_limit = scroll_height - canvas_height; - - old_zoom_xofs = canvas->zoom_xofs; - old_zoom_yofs = canvas->zoom_yofs; - - if (right_limit < 0) { - cx = 0; - - if (canvas->center_scroll_region) { - canvas->zoom_xofs = (canvas_width - scroll_width) / 2; - scroll_width = canvas_width; - } else { - canvas->zoom_xofs = 0; - } - } else if (cx < 0) { - cx = 0; - canvas->zoom_xofs = 0; - } else if (cx > right_limit) { - cx = right_limit; - canvas->zoom_xofs = 0; - } else { - canvas->zoom_xofs = 0; - } - - if (bottom_limit < 0) { - cy = 0; - - if (canvas->center_scroll_region) { - canvas->zoom_yofs = (canvas_height - scroll_height) / 2; - scroll_height = canvas_height; - } else { - canvas->zoom_yofs = 0; - } - } else if (cy < 0) { - cy = 0; - canvas->zoom_yofs = 0; - } else if (cy > bottom_limit) { - cy = bottom_limit; - canvas->zoom_yofs = 0; - } else { - canvas->zoom_yofs = 0; - } - - if ((canvas->zoom_xofs != old_zoom_xofs) || (canvas->zoom_yofs != old_zoom_yofs)) { - ganv_canvas_base_request_update(canvas); - gtk_widget_queue_draw(GTK_WIDGET(canvas)); - } - - if (canvas->layout.hadjustment && ( ((int)canvas->layout.hadjustment->value) != cx) ) { - canvas->layout.hadjustment->value = cx; - changed_x = TRUE; - } - - if (canvas->layout.vadjustment && ( ((int)canvas->layout.vadjustment->value) != cy) ) { - canvas->layout.vadjustment->value = cy; - changed_y = TRUE; - } - - if ((scroll_width != (int)canvas->layout.width) - || (scroll_height != (int)canvas->layout.height)) { - gtk_layout_set_size(GTK_LAYOUT(canvas), scroll_width, scroll_height); - } - - /* Signal GtkLayout that it should do a redraw. */ - - if (changed_x) { - g_signal_emit_by_name(canvas->layout.hadjustment, "value_changed"); - } - - if (changed_y) { - g_signal_emit_by_name(canvas->layout.vadjustment, "value_changed"); - } -} - -/* Size allocation handler for the canvas */ -static void -ganv_canvas_base_size_allocate(GtkWidget* widget, GtkAllocation* allocation) -{ - GanvCanvasBase* canvas; - - g_return_if_fail(GANV_IS_CANVAS_BASE(widget)); - g_return_if_fail(allocation != NULL); - - if (GTK_WIDGET_CLASS(canvas_parent_class)->size_allocate) { - (*GTK_WIDGET_CLASS(canvas_parent_class)->size_allocate)(widget, allocation); - } - - canvas = GANV_CANVAS_BASE(widget); - - /* Recenter the view, if appropriate */ - - canvas->layout.hadjustment->page_size = allocation->width; - canvas->layout.hadjustment->page_increment = allocation->width / 2; - - canvas->layout.vadjustment->page_size = allocation->height; - canvas->layout.vadjustment->page_increment = allocation->height / 2; - - scroll_to(canvas, - canvas->layout.hadjustment->value, - canvas->layout.vadjustment->value); - - g_signal_emit_by_name(canvas->layout.hadjustment, "changed"); - g_signal_emit_by_name(canvas->layout.vadjustment, "changed"); -} - -/* Returns whether the item is an inferior of or is equal to the parent. */ -static gboolean -is_descendant(GanvItem* item, GanvItem* parent) -{ - for (; item; item = item->parent) { - if (item == parent) { - return TRUE; - } - } - - return FALSE; -} - - -/* Emits an event for an item in the canvas, be it the current item, grabbed - * item, or focused item, as appropriate. - */ -int -ganv_canvas_base_emit_event(GanvCanvasBase* canvas, GdkEvent* event) -{ - GdkEvent* ev; - gint finished; - GanvItem* item; - GanvItem* parent; - guint mask; - - /* Perform checks for grabbed items */ - - if (canvas->grabbed_item - && !is_descendant(canvas->current_item, canvas->grabbed_item)) { - /* I think this warning is annoying and I don't know what it's for - * so I'll disable it for now. - */ - /* g_warning ("emit_event() returning FALSE!\n");*/ - return FALSE; - } - - if (canvas->grabbed_item) { - switch (event->type) { - case GDK_ENTER_NOTIFY: - mask = GDK_ENTER_NOTIFY_MASK; - break; - - case GDK_LEAVE_NOTIFY: - mask = GDK_LEAVE_NOTIFY_MASK; - break; - - case GDK_MOTION_NOTIFY: - mask = GDK_POINTER_MOTION_MASK; - break; - - case GDK_BUTTON_PRESS: - case GDK_2BUTTON_PRESS: - case GDK_3BUTTON_PRESS: - mask = GDK_BUTTON_PRESS_MASK; - break; - - case GDK_BUTTON_RELEASE: - mask = GDK_BUTTON_RELEASE_MASK; - break; - - case GDK_KEY_PRESS: - mask = GDK_KEY_PRESS_MASK; - break; - - case GDK_KEY_RELEASE: - mask = GDK_KEY_RELEASE_MASK; - break; - - case GDK_SCROLL: - mask = GDK_SCROLL_MASK; - break; - - default: - mask = 0; - break; - } - - if (!(mask & canvas->grabbed_event_mask)) { - return FALSE; - } - } - - /* Convert to world coordinates -- we have two cases because of diferent - * offsets of the fields in the event structures. - */ - - ev = gdk_event_copy(event); - - switch (ev->type) { - case GDK_ENTER_NOTIFY: - case GDK_LEAVE_NOTIFY: - ganv_canvas_base_window_to_world(canvas, - ev->crossing.x, ev->crossing.y, - &ev->crossing.x, &ev->crossing.y); - break; - - case GDK_MOTION_NOTIFY: - case GDK_BUTTON_PRESS: - case GDK_2BUTTON_PRESS: - case GDK_3BUTTON_PRESS: - case GDK_BUTTON_RELEASE: - ganv_canvas_base_window_to_world(canvas, - ev->motion.x, ev->motion.y, - &ev->motion.x, &ev->motion.y); - break; - - default: - break; - } - - /* Choose where we send the event */ - - item = canvas->current_item; - - if (canvas->focused_item - && ((event->type == GDK_KEY_PRESS) - || (event->type == GDK_KEY_RELEASE) - || (event->type == GDK_FOCUS_CHANGE))) { - item = canvas->focused_item; - } - - /* The event is propagated up the hierarchy (for if someone connected to - * a group instead of a leaf event), and emission is stopped if a - * handler returns TRUE, just like for GtkWidget events. - */ - - finished = FALSE; - - while (item && !finished) { - g_object_ref(G_OBJECT(item)); - - ganv_item_emit_event(item, ev, &finished); - - parent = item->parent; - g_object_unref(G_OBJECT(item)); - - item = parent; - } - - gdk_event_free(ev); - - return finished; -} - -/* Re-picks the current item in the canvas, based on the event's coordinates. - * Also emits enter/leave events for items as appropriate. - */ -static int -pick_current_item(GanvCanvasBase* canvas, GdkEvent* event) -{ - int button_down; - double x, y; - int cx, cy; - int retval; - - retval = FALSE; - - /* If a button is down, we'll perform enter and leave events on the - * current item, but not enter on any other item. This is more or less - * like X pointer grabbing for canvas items. - */ - button_down = canvas->state & (GDK_BUTTON1_MASK - | GDK_BUTTON2_MASK - | GDK_BUTTON3_MASK - | GDK_BUTTON4_MASK - | GDK_BUTTON5_MASK); - if (!button_down) { - canvas->left_grabbed_item = FALSE; - } - - /* Save the event in the canvas. This is used to synthesize enter and - * leave events in case the current item changes. It is also used to - * re-pick the current item if the current one gets deleted. Also, - * synthesize an enter event. - */ - if (event != &canvas->pick_event) { - if ((event->type == GDK_MOTION_NOTIFY) || (event->type == GDK_BUTTON_RELEASE)) { - /* these fields have the same offsets in both types of events */ - - canvas->pick_event.crossing.type = GDK_ENTER_NOTIFY; - canvas->pick_event.crossing.window = event->motion.window; - canvas->pick_event.crossing.send_event = event->motion.send_event; - canvas->pick_event.crossing.subwindow = NULL; - canvas->pick_event.crossing.x = event->motion.x; - canvas->pick_event.crossing.y = event->motion.y; - canvas->pick_event.crossing.mode = GDK_CROSSING_NORMAL; - canvas->pick_event.crossing.detail = GDK_NOTIFY_NONLINEAR; - canvas->pick_event.crossing.focus = FALSE; - canvas->pick_event.crossing.state = event->motion.state; - - /* these fields don't have the same offsets in both types of events */ - - if (event->type == GDK_MOTION_NOTIFY) { - canvas->pick_event.crossing.x_root = event->motion.x_root; - canvas->pick_event.crossing.y_root = event->motion.y_root; - } else { - canvas->pick_event.crossing.x_root = event->button.x_root; - canvas->pick_event.crossing.y_root = event->button.y_root; - } - } else { - canvas->pick_event = *event; - } - } - - /* Don't do anything else if this is a recursive call */ - - if (canvas->in_repick) { - return retval; - } - - /* LeaveNotify means that there is no current item, so we don't look for one */ - - if (canvas->pick_event.type != GDK_LEAVE_NOTIFY) { - /* these fields don't have the same offsets in both types of events */ - - if (canvas->pick_event.type == GDK_ENTER_NOTIFY) { - x = canvas->pick_event.crossing.x - canvas->zoom_xofs; - y = canvas->pick_event.crossing.y - canvas->zoom_yofs; - } else { - x = canvas->pick_event.motion.x - canvas->zoom_xofs; - y = canvas->pick_event.motion.y - canvas->zoom_yofs; - } - - /* canvas pixel coords */ - - cx = (int)(x + 0.5); - cy = (int)(y + 0.5); - - /* world coords */ - - x = canvas->scroll_x1 + x / canvas->pixels_per_unit; - y = canvas->scroll_y1 + y / canvas->pixels_per_unit; - - /* find the closest item */ - - if (canvas->root->object.flags & GANV_ITEM_VISIBLE) { - GANV_ITEM_GET_CLASS(canvas->root)->point( - canvas->root, - x - canvas->root->x, y - canvas->root->y, - cx, cy, - &canvas->new_current_item); - } else { - canvas->new_current_item = NULL; - } - } else { - canvas->new_current_item = NULL; - } - - if ((canvas->new_current_item == canvas->current_item) && !canvas->left_grabbed_item) { - return retval; /* current item did not change */ - - } - /* Synthesize events for old and new current items */ - - if ((canvas->new_current_item != canvas->current_item) - && (canvas->current_item != NULL) - && !canvas->left_grabbed_item) { - GdkEvent new_event; - - new_event = canvas->pick_event; - new_event.type = GDK_LEAVE_NOTIFY; - - new_event.crossing.detail = GDK_NOTIFY_ANCESTOR; - new_event.crossing.subwindow = NULL; - canvas->in_repick = TRUE; - retval = ganv_canvas_base_emit_event(canvas, &new_event); - canvas->in_repick = FALSE; - } - - /* new_current_item may have been set to NULL during the call to ganv_canvas_base_emit_event() above */ - - if ((canvas->new_current_item != canvas->current_item) && button_down) { - canvas->left_grabbed_item = TRUE; - return retval; - } - - /* Handle the rest of cases */ - - canvas->left_grabbed_item = FALSE; - canvas->current_item = canvas->new_current_item; - - if (canvas->current_item != NULL) { - GdkEvent new_event; - - new_event = canvas->pick_event; - new_event.type = GDK_ENTER_NOTIFY; - new_event.crossing.detail = GDK_NOTIFY_ANCESTOR; - new_event.crossing.subwindow = NULL; - retval = ganv_canvas_base_emit_event(canvas, &new_event); - } - - return retval; -} - -/* Button event handler for the canvas */ -static gint -ganv_canvas_base_button(GtkWidget* widget, GdkEventButton* event) -{ - GanvCanvasBase* canvas; - int mask; - int retval; - - g_return_val_if_fail(GANV_IS_CANVAS_BASE(widget), FALSE); - g_return_val_if_fail(event != NULL, FALSE); - - retval = FALSE; - - canvas = GANV_CANVAS_BASE(widget); - - /* - * dispatch normally regardless of the event's window if an item has - * has a pointer grab in effect - */ - if (!canvas->grabbed_item && ( event->window != canvas->layout.bin_window) ) { - return retval; - } - - switch (event->button) { - case 1: - mask = GDK_BUTTON1_MASK; - break; - case 2: - mask = GDK_BUTTON2_MASK; - break; - case 3: - mask = GDK_BUTTON3_MASK; - break; - case 4: - mask = GDK_BUTTON4_MASK; - break; - case 5: - mask = GDK_BUTTON5_MASK; - break; - default: - mask = 0; - } - - switch (event->type) { - case GDK_BUTTON_PRESS: - case GDK_2BUTTON_PRESS: - case GDK_3BUTTON_PRESS: - /* Pick the current item as if the button were not pressed, and - * then process the event. - */ - canvas->state = event->state; - pick_current_item(canvas, (GdkEvent*)event); - canvas->state ^= mask; - retval = ganv_canvas_base_emit_event(canvas, (GdkEvent*)event); - break; - - case GDK_BUTTON_RELEASE: - /* Process the event as if the button were pressed, then repick - * after the button has been released - */ - canvas->state = event->state; - retval = ganv_canvas_base_emit_event(canvas, (GdkEvent*)event); - event->state ^= mask; - canvas->state = event->state; - pick_current_item(canvas, (GdkEvent*)event); - event->state ^= mask; - break; - - default: - g_assert_not_reached(); - } - - return retval; -} - -/* Motion event handler for the canvas */ -static gint -ganv_canvas_base_motion(GtkWidget* widget, GdkEventMotion* event) -{ - GanvCanvasBase* canvas; - - g_return_val_if_fail(GANV_IS_CANVAS_BASE(widget), FALSE); - g_return_val_if_fail(event != NULL, FALSE); - - canvas = GANV_CANVAS_BASE(widget); - - if (event->window != canvas->layout.bin_window) { - return FALSE; - } - - canvas->state = event->state; - pick_current_item(canvas, (GdkEvent*)event); - return ganv_canvas_base_emit_event(canvas, (GdkEvent*)event); -} - -static gboolean -ganv_canvas_base_scroll(GtkWidget* widget, GdkEventScroll* event) -{ - GanvCanvasBase* canvas; - - g_return_val_if_fail(GANV_IS_CANVAS_BASE(widget), FALSE); - g_return_val_if_fail(event != NULL, FALSE); - - canvas = GANV_CANVAS_BASE(widget); - - if (event->window != canvas->layout.bin_window) { - return FALSE; - } - - canvas->state = event->state; - pick_current_item(canvas, (GdkEvent*)event); - return ganv_canvas_base_emit_event(canvas, (GdkEvent*)event); -} - -/* Key event handler for the canvas */ -static gboolean -ganv_canvas_base_key(GtkWidget* widget, GdkEventKey* event) -{ - GanvCanvasBase* canvas; - - g_return_val_if_fail(GANV_IS_CANVAS_BASE(widget), FALSE); - g_return_val_if_fail(event != NULL, FALSE); - - canvas = GANV_CANVAS_BASE(widget); - - if (!ganv_canvas_base_emit_event(canvas, (GdkEvent*)event)) { - GtkWidgetClass* widget_class; - - widget_class = GTK_WIDGET_CLASS(canvas_parent_class); - - if (event->type == GDK_KEY_PRESS) { - if (widget_class->key_press_event) { - return (*widget_class->key_press_event)(widget, event); - } - } else if (event->type == GDK_KEY_RELEASE) { - if (widget_class->key_release_event) { - return (*widget_class->key_release_event)(widget, event); - } - } else { - g_assert_not_reached(); - } - - return FALSE; - } else { - return TRUE; - } -} - -/* Crossing event handler for the canvas */ -static gint -ganv_canvas_base_crossing(GtkWidget* widget, GdkEventCrossing* event) -{ - GanvCanvasBase* canvas; - - g_return_val_if_fail(GANV_IS_CANVAS_BASE(widget), FALSE); - g_return_val_if_fail(event != NULL, FALSE); - - canvas = GANV_CANVAS_BASE(widget); - - if (event->window != canvas->layout.bin_window) { - return FALSE; - } - - canvas->state = event->state; - return pick_current_item(canvas, (GdkEvent*)event); -} - -/* Focus in handler for the canvas */ -static gint -ganv_canvas_base_focus_in(GtkWidget* widget, GdkEventFocus* event) -{ - GanvCanvasBase* canvas; - - GTK_WIDGET_SET_FLAGS(widget, GTK_HAS_FOCUS); - - canvas = GANV_CANVAS_BASE(widget); - - if (canvas->focused_item) { - return ganv_canvas_base_emit_event(canvas, (GdkEvent*)event); - } else { - return FALSE; - } -} - -/* Focus out handler for the canvas */ -static gint -ganv_canvas_base_focus_out(GtkWidget* widget, GdkEventFocus* event) -{ - GanvCanvasBase* canvas; - - GTK_WIDGET_UNSET_FLAGS(widget, GTK_HAS_FOCUS); - - canvas = GANV_CANVAS_BASE(widget); - - if (canvas->focused_item) { - return ganv_canvas_base_emit_event(canvas, (GdkEvent*)event); - } else { - return FALSE; - } -} - -#define REDRAW_QUANTUM_SIZE 512 - -static void -ganv_canvas_base_paint_rect(GanvCanvasBase* canvas, gint x0, gint y0, gint x1, gint y1) -{ - gint draw_x1, draw_y1; - gint draw_x2, draw_y2; - gint draw_width, draw_height; - - g_return_if_fail(!canvas->need_update); - - draw_x1 = MAX(x0, canvas->layout.hadjustment->value - canvas->zoom_xofs); - draw_y1 = MAX(y0, canvas->layout.vadjustment->value - canvas->zoom_yofs); - draw_x2 = MIN(draw_x1 + GTK_WIDGET(canvas)->allocation.width, x1); - draw_y2 = MIN(draw_y1 + GTK_WIDGET(canvas)->allocation.height, y1); - - draw_width = draw_x2 - draw_x1; - draw_height = draw_y2 - draw_y1; - - if ((draw_width < 1) || (draw_height < 1)) { - return; - } - - canvas->redraw_x1 = draw_x1; - canvas->redraw_y1 = draw_y1; - canvas->redraw_x2 = draw_x2; - canvas->redraw_y2 = draw_y2; - canvas->draw_xofs = draw_x1; - canvas->draw_yofs = draw_y1; - - cairo_t* cr = gdk_cairo_create(canvas->layout.bin_window); - - double wx, wy; - ganv_canvas_base_window_to_world(canvas, 0, 0, &wx, &wy); - cairo_translate(cr, -wx, -wy); - - if (canvas->root->object.flags & GANV_ITEM_VISIBLE) { - (*GANV_ITEM_GET_CLASS(canvas->root)->draw)( - canvas->root, cr, - draw_x1, draw_y1, - draw_width, draw_height); - } - - cairo_destroy(cr); -} - -/* Expose handler for the canvas */ -static gint -ganv_canvas_base_expose(GtkWidget* widget, GdkEventExpose* event) -{ - GanvCanvasBase* canvas = GANV_CANVAS_BASE(widget); - if (!GTK_WIDGET_DRAWABLE(widget) || - (event->window != canvas->layout.bin_window)) { - return FALSE; - } - - /* Find a single bounding rectangle for all rectangles in the region. - Since drawing the root group is O(n) and thus very expensive for large - canvases, it's much faster to do a single paint than many, even though - more area may be painted that way. With a better group implementation, - it would likely be better to paint each changed rectangle separately. */ - GdkRectangle clip; - gdk_region_get_clipbox(event->region, &clip); - - const int x2 = clip.x + clip.width; - const int y2 = clip.y + clip.height; - - if (canvas->need_update || canvas->need_redraw) { - /* Update or drawing is scheduled, so just mark exposed area as dirty */ - ganv_canvas_base_request_redraw(canvas, clip.x, clip.y, x2, y2); - } else { - /* No pending updates, draw exposed area immediately */ - ganv_canvas_base_paint_rect(canvas, clip.x, clip.y, x2, y2); - - /* And call expose on parent container class */ - if (GTK_WIDGET_CLASS(canvas_parent_class)->expose_event) { - (*GTK_WIDGET_CLASS(canvas_parent_class)->expose_event)( - widget, event); - } - } - - return FALSE; -} - -/* Repaints the areas in the canvas that need it */ -static void -paint(GanvCanvasBase* canvas) -{ - for (GSList* l = canvas->redraw_region; l; l = l->next) { - IRect* rect = (IRect*)l->data; - - const GdkRectangle gdkrect = { - rect->x + canvas->zoom_xofs, - rect->y + canvas->zoom_yofs, - rect->width, - rect->height - }; - - gdk_window_invalidate_rect(canvas->layout.bin_window, &gdkrect, FALSE); - g_free(rect); - } - - g_slist_free(canvas->redraw_region); - canvas->redraw_region = NULL; - canvas->need_redraw = FALSE; - - canvas->redraw_x1 = 0; - canvas->redraw_y1 = 0; - canvas->redraw_x2 = 0; - canvas->redraw_y2 = 0; -} - -static void -do_update(GanvCanvasBase* canvas) -{ - /* Cause the update if necessary */ - -update_again: - if (canvas->need_update) { - ganv_item_invoke_update(canvas->root, 0); - - canvas->need_update = FALSE; - } - - /* Pick new current item */ - - while (canvas->need_repick) { - canvas->need_repick = FALSE; - pick_current_item(canvas, &canvas->pick_event); - } - - /* it is possible that during picking we emitted an event in which - the user then called some function which then requested update - of something. Without this we'd be left in a state where - need_update would have been left TRUE and the canvas would have - been left unpainted. */ - if (canvas->need_update) { - goto update_again; - } - - /* Paint if able to */ - - if (GTK_WIDGET_DRAWABLE(canvas) && canvas->need_redraw) { - paint(canvas); - } -} - -/* Idle handler for the canvas. It deals with pending updates and redraws. */ -static gboolean -idle_handler(gpointer data) -{ - GanvCanvasBase* canvas; - - GDK_THREADS_ENTER(); - - canvas = GANV_CANVAS_BASE(data); - - do_update(canvas); - - /* Reset idle id */ - canvas->idle_id = 0; - - GDK_THREADS_LEAVE(); - - return FALSE; -} - -/* Convenience function to add an idle handler to a canvas */ -static void -add_idle(GanvCanvasBase* canvas) -{ - g_assert(canvas->need_update || canvas->need_redraw); - - if (!canvas->idle_id) { - canvas->idle_id = g_idle_add_full(CANVAS_IDLE_PRIORITY, - idle_handler, - canvas, - NULL); - } - - /* canvas->idle_id = gtk_idle_add (idle_handler, canvas); */ -} - -/** - * ganv_canvas_base_root: - * @canvas: A canvas. - * - * Queries the root group of a canvas. - * - * Return value: (transfer none): The root group of the specified canvas. - **/ -GanvItem* -ganv_canvas_base_root(GanvCanvasBase* canvas) -{ - g_return_val_if_fail(GANV_IS_CANVAS_BASE(canvas), NULL); - - return canvas->root; -} - -/** - * ganv_canvas_base_set_scroll_region: - * @canvas: A canvas. - * @x1: Leftmost limit of the scrolling region. - * @y1: Upper limit of the scrolling region. - * @x2: Rightmost limit of the scrolling region. - * @y2: Lower limit of the scrolling region. - * - * Sets the scrolling region of a canvas to the specified rectangle. The canvas - * will then be able to scroll only within this region. The view of the canvas - * is adjusted as appropriate to display as much of the new region as possible. - **/ -void -ganv_canvas_base_set_scroll_region(GanvCanvasBase* canvas, double x1, double y1, double x2, - double y2) -{ - double wxofs, wyofs; - int xofs, yofs; - - g_return_if_fail(GANV_IS_CANVAS_BASE(canvas)); - - /* - * Set the new scrolling region. If possible, do not move the visible contents of the - * canvas. - */ - - ganv_canvas_base_c2w(canvas, - GTK_LAYOUT(canvas)->hadjustment->value + canvas->zoom_xofs, - GTK_LAYOUT(canvas)->vadjustment->value + canvas->zoom_yofs, - /*canvas->zoom_xofs, - canvas->zoom_yofs,*/ - &wxofs, &wyofs); - - canvas->scroll_x1 = x1; - canvas->scroll_y1 = y1; - canvas->scroll_x2 = x2; - canvas->scroll_y2 = y2; - - ganv_canvas_base_w2c(canvas, wxofs, wyofs, &xofs, &yofs); - - scroll_to(canvas, xofs, yofs); - - canvas->need_repick = TRUE; -#if 0 - /* todo: should be requesting update */ - (*GANV_ITEM_CLASS(canvas->root->object.klass)->update)( - canvas->root, NULL, NULL, 0); -#endif -} - -/** - * ganv_canvas_base_get_scroll_region: - * @canvas: A canvas. - * @x1: Leftmost limit of the scrolling region (return value). - * @y1: Upper limit of the scrolling region (return value). - * @x2: Rightmost limit of the scrolling region (return value). - * @y2: Lower limit of the scrolling region (return value). - * - * Queries the scrolling region of a canvas. - **/ -void -ganv_canvas_base_get_scroll_region(GanvCanvasBase* canvas, double* x1, double* y1, double* x2, - double* y2) -{ - g_return_if_fail(GANV_IS_CANVAS_BASE(canvas)); - - if (x1) { - *x1 = canvas->scroll_x1; - } - - if (y1) { - *y1 = canvas->scroll_y1; - } - - if (x2) { - *x2 = canvas->scroll_x2; - } - - if (y2) { - *y2 = canvas->scroll_y2; - } -} - -/** - * ganv_canvas_base_set_center_scroll_region: - * @canvas: A canvas. - * @center_scroll_region: Whether to center the scrolling region in the canvas - * window when it is smaller than the canvas' allocation. - * - * When the scrolling region of the canvas is smaller than the canvas window, - * e.g. the allocation of the canvas, it can be either centered on the window - * or simply made to be on the upper-left corner on the window. This function - * lets you configure this property. - **/ -void -ganv_canvas_base_set_center_scroll_region(GanvCanvasBase* canvas, gboolean center_scroll_region) -{ - g_return_if_fail(GANV_IS_CANVAS_BASE(canvas)); - - canvas->center_scroll_region = center_scroll_region != 0; - - scroll_to(canvas, - canvas->layout.hadjustment->value, - canvas->layout.vadjustment->value); -} - -/** - * ganv_canvas_base_get_center_scroll_region: - * @canvas: A canvas. - * - * Returns whether the canvas is set to center the scrolling region in the window - * if the former is smaller than the canvas' allocation. - * - * Return value: Whether the scroll region is being centered in the canvas window. - **/ -gboolean -ganv_canvas_base_get_center_scroll_region(GanvCanvasBase* canvas) -{ - g_return_val_if_fail(GANV_IS_CANVAS_BASE(canvas), FALSE); - - return canvas->center_scroll_region ? TRUE : FALSE; -} - -/** - * ganv_canvas_base_set_pixels_per_unit: - * @canvas: A canvas. - * @n: The number of pixels that correspond to one canvas unit. - * - * Sets the zooming factor of a canvas by specifying the number of pixels that - * correspond to one canvas unit. - * - * The anchor point for zooming, i.e. the point that stays fixed and all others - * zoom inwards or outwards from it, depends on whether the canvas is set to - * center the scrolling region or not. You can control this using the - * ganv_canvas_base_set_center_scroll_region() function. If the canvas is set to - * center the scroll region, then the center of the canvas window is used as the - * anchor point for zooming. Otherwise, the upper-left corner of the canvas - * window is used as the anchor point. - **/ -void -ganv_canvas_base_set_pixels_per_unit(GanvCanvasBase* canvas, double n) -{ - double ax, ay; - int x1, y1; - int anchor_x, anchor_y; - - g_return_if_fail(GANV_IS_CANVAS_BASE(canvas)); - g_return_if_fail(n > 1e-10); - - if (canvas->center_scroll_region) { - anchor_x = GTK_WIDGET(canvas)->allocation.width / 2; - anchor_y = GTK_WIDGET(canvas)->allocation.height / 2; - } else { - anchor_x = anchor_y = 0; - } - - /* Find the coordinates of the anchor point in units. */ - if (canvas->layout.hadjustment) { - ax - = (canvas->layout.hadjustment->value - + anchor_x) / canvas->pixels_per_unit + canvas->scroll_x1 + canvas->zoom_xofs; - } else { - ax = (0.0 + anchor_x) / canvas->pixels_per_unit + canvas->scroll_x1 + canvas->zoom_xofs; - } - if (canvas->layout.hadjustment) { - ay - = (canvas->layout.vadjustment->value - + anchor_y) / canvas->pixels_per_unit + canvas->scroll_y1 + canvas->zoom_yofs; - } else { - ay = (0.0 + anchor_y) / canvas->pixels_per_unit + canvas->scroll_y1 + canvas->zoom_yofs; - } - - /* Now calculate the new offset of the upper left corner. */ - x1 = ((ax - canvas->scroll_x1) * n) - anchor_x; - y1 = ((ay - canvas->scroll_y1) * n) - anchor_y; - - canvas->pixels_per_unit = n; - - scroll_to(canvas, x1, y1); - - ganv_canvas_base_request_update(canvas); - canvas->need_repick = TRUE; -} - -/** - * ganv_canvas_base_scroll_to: - * @canvas: A canvas. - * @cx: Horizontal scrolling offset in canvas pixel units. - * @cy: Vertical scrolling offset in canvas pixel units. - * - * Makes a canvas scroll to the specified offsets, given in canvas pixel units. - * The canvas will adjust the view so that it is not outside the scrolling - * region. This function is typically not used, as it is better to hook - * scrollbars to the canvas layout's scrolling adjusments. - **/ -void -ganv_canvas_base_scroll_to(GanvCanvasBase* canvas, int cx, int cy) -{ - g_return_if_fail(GANV_IS_CANVAS_BASE(canvas)); - - scroll_to(canvas, cx, cy); -} - -/** - * ganv_canvas_base_get_scroll_offsets: - * @canvas: A canvas. - * @cx: Horizontal scrolling offset (return value). - * @cy: Vertical scrolling offset (return value). - * - * Queries the scrolling offsets of a canvas. The values are returned in canvas - * pixel units. - **/ -void -ganv_canvas_base_get_scroll_offsets(GanvCanvasBase* canvas, int* cx, int* cy) -{ - g_return_if_fail(GANV_IS_CANVAS_BASE(canvas)); - - if (cx) { - *cx = canvas->layout.hadjustment->value; - } - - if (cy) { - *cy = canvas->layout.vadjustment->value; - } -} - -/** - * ganv_canvas_base_get_item_at: - * @canvas: A canvas. - * @x: X position in world coordinates. - * @y: Y position in world coordinates. - * - * Looks for the item that is under the specified position, which must be - * specified in world coordinates. - * - * Return value: (transfer none): The sought item, or NULL if no item is at the - * specified coordinates. - **/ -GanvItem* -ganv_canvas_base_get_item_at(GanvCanvasBase* canvas, double x, double y) -{ - GanvItem* item; - double dist; - int cx, cy; - - g_return_val_if_fail(GANV_IS_CANVAS_BASE(canvas), NULL); - - ganv_canvas_base_w2c(canvas, x, y, &cx, &cy); - - dist = GANV_ITEM_GET_CLASS(canvas->root)->point( - canvas->root, - x - canvas->root->x, y - canvas->root->y, - cx, cy, - &item); - if ((int)(dist * canvas->pixels_per_unit + 0.5) <= canvas->close_enough) { - return item; - } else { - return NULL; - } -} - -/* Queues an update of the canvas */ -void -ganv_canvas_base_request_update(GanvCanvasBase* canvas) -{ - GANV_CANVAS_BASE_GET_CLASS(canvas)->request_update(canvas); -} - -static void -ganv_canvas_base_request_update_real(GanvCanvasBase* canvas) -{ - if (canvas->need_update) { - return; - } - - canvas->need_update = TRUE; - if (GTK_WIDGET_MAPPED((GtkWidget*)canvas)) { - add_idle(canvas); - } -} - -static inline gboolean -rect_overlaps(const IRect* a, const IRect* b) -{ - if ((a->x > b->x + b->width) || - (a->y > b->y + b->height) || - (a->x + a->width < b->x) || - (a->y + a->height < b->y)) { - return FALSE; - } - return TRUE; -} - -static inline gboolean -rect_is_visible(GanvCanvasBase* canvas, const IRect* r) -{ - const IRect rect = { - canvas->layout.hadjustment->value - canvas->zoom_xofs, - canvas->layout.vadjustment->value - canvas->zoom_yofs, - GTK_WIDGET(canvas)->allocation.width, - GTK_WIDGET(canvas)->allocation.height - }; - - return rect_overlaps(&rect, r); -} - -/** - * ganv_canvas_base_request_redraw: - * @canvas: A canvas. - * @x1: Leftmost coordinate of the rectangle to be redrawn. - * @y1: Upper coordinate of the rectangle to be redrawn. - * @x2: Rightmost coordinate of the rectangle to be redrawn, plus 1. - * @y2: Lower coordinate of the rectangle to be redrawn, plus 1. - * - * Informs a canvas that the specified area needs to be repainted. To be used - * only by item implementations. - **/ -void -ganv_canvas_base_request_redraw(GanvCanvasBase* canvas, int x1, int y1, int x2, int y2) -{ - g_return_if_fail(GANV_IS_CANVAS_BASE(canvas)); - - if (!GTK_WIDGET_DRAWABLE(canvas) || (x1 >= x2) || (y1 >= y2)) { - return; - } - - const IRect rect = { x1, y1, x2 - x1, y2 - y1 }; - - if (!rect_is_visible(canvas, &rect)) { - return; - } - - IRect* r = (IRect*)g_malloc(sizeof(IRect)); - *r = rect; - - canvas->redraw_region = g_slist_prepend(canvas->redraw_region, r); - canvas->need_redraw = TRUE; - - if (canvas->idle_id == 0) { - add_idle(canvas); - } -} - -/** - * ganv_canvas_base_w2c_affine: - * @canvas: A canvas. - * @matrix: An affine transformation matrix (return value). - * - * Gets the affine transform that converts from world coordinates to canvas - * pixel coordinates. - **/ -void -ganv_canvas_base_w2c_affine(GanvCanvasBase* canvas, cairo_matrix_t* matrix) -{ - g_return_if_fail(GANV_IS_CANVAS_BASE(canvas)); - g_return_if_fail(matrix != NULL); - - cairo_matrix_init_translate(matrix, - -canvas->scroll_x1, - -canvas->scroll_y1); - - cairo_matrix_scale(matrix, - canvas->pixels_per_unit, - canvas->pixels_per_unit); -} - -/** - * ganv_canvas_base_w2c: - * @canvas: A canvas. - * @wx: World X coordinate. - * @wy: World Y coordinate. - * @cx: X pixel coordinate (return value). - * @cy: Y pixel coordinate (return value). - * - * Converts world coordinates into canvas pixel coordinates. - **/ -void -ganv_canvas_base_w2c(GanvCanvasBase* canvas, double wx, double wy, int* cx, int* cy) -{ - g_return_if_fail(GANV_IS_CANVAS_BASE(canvas)); - - cairo_matrix_t matrix; - ganv_canvas_base_w2c_affine(canvas, &matrix); - - cairo_matrix_transform_point(&matrix, &wx, &wy); - - if (cx) { - *cx = floor(wx + 0.5); - } - if (cy) { - *cy = floor(wy + 0.5); - } -} - -/** - * ganv_canvas_base_w2c_d: - * @canvas: A canvas. - * @wx: World X coordinate. - * @wy: World Y coordinate. - * @cx: X pixel coordinate (return value). - * @cy: Y pixel coordinate (return value). - * - * Converts world coordinates into canvas pixel coordinates. This - * version returns coordinates in floating point coordinates, for - * greater precision. - **/ -void -ganv_canvas_base_w2c_d(GanvCanvasBase* canvas, double wx, double wy, double* cx, double* cy) -{ - g_return_if_fail(GANV_IS_CANVAS_BASE(canvas)); - - cairo_matrix_t matrix; - ganv_canvas_base_w2c_affine(canvas, &matrix); - - cairo_matrix_transform_point(&matrix, &wx, &wy); - - if (cx) { - *cx = wx; - } - if (cy) { - *cy = wy; - } -} - -/** - * ganv_canvas_base_c2w: - * @canvas: A canvas. - * @cx: Canvas pixel X coordinate. - * @cy: Canvas pixel Y coordinate. - * @wx: X world coordinate (return value). - * @wy: Y world coordinate (return value). - * - * Converts canvas pixel coordinates to world coordinates. - **/ -void -ganv_canvas_base_c2w(GanvCanvasBase* canvas, int cx, int cy, double* wx, double* wy) -{ - g_return_if_fail(GANV_IS_CANVAS_BASE(canvas)); - - cairo_matrix_t matrix; - ganv_canvas_base_w2c_affine(canvas, &matrix); - cairo_matrix_invert(&matrix); - - double x = cx; - double y = cy; - cairo_matrix_transform_point(&matrix, &x, &y); - - if (wx) { - *wx = x; - } - if (wy) { - *wy = y; - } -} - -/** - * ganv_canvas_base_window_to_world: - * @canvas: A canvas. - * @winx: Window-relative X coordinate. - * @winy: Window-relative Y coordinate. - * @worldx: X world coordinate (return value). - * @worldy: Y world coordinate (return value). - * - * Converts window-relative coordinates into world coordinates. You can use - * this when you need to convert mouse coordinates into world coordinates, for - * example. - **/ -void -ganv_canvas_base_window_to_world(GanvCanvasBase* canvas, double winx, double winy, - double* worldx, double* worldy) -{ - g_return_if_fail(GANV_IS_CANVAS_BASE(canvas)); - - if (worldx) { - *worldx = canvas->scroll_x1 + ((winx - canvas->zoom_xofs) - / canvas->pixels_per_unit); - } - - if (worldy) { - *worldy = canvas->scroll_y1 + ((winy - canvas->zoom_yofs) - / canvas->pixels_per_unit); - } -} - -/** - * ganv_canvas_base_world_to_window: - * @canvas: A canvas. - * @worldx: World X coordinate. - * @worldy: World Y coordinate. - * @winx: X window-relative coordinate. - * @winy: Y window-relative coordinate. - * - * Converts world coordinates into window-relative coordinates. - **/ -void -ganv_canvas_base_world_to_window(GanvCanvasBase* canvas, double worldx, double worldy, - double* winx, double* winy) -{ - g_return_if_fail(GANV_IS_CANVAS_BASE(canvas)); - - if (winx) { - *winx = (canvas->pixels_per_unit) * (worldx - canvas->scroll_x1) + canvas->zoom_xofs; - } - - if (winy) { - *winy = (canvas->pixels_per_unit) * (worldy - canvas->scroll_y1) + canvas->zoom_yofs; - } -} diff --git a/src/circle.c b/src/circle.c index 0e0d096..c37d78c 100644 --- a/src/circle.c +++ b/src/circle.c @@ -17,7 +17,6 @@ #include #include "ganv/canvas.h" -#include "ganv/canvas-base.h" #include "ganv/circle.h" #include "./color.h" @@ -233,8 +232,8 @@ request_redraw(GanvItem* item, ganv_item_i2w_pair(item, &x1, &y1, &x2, &y2); } - ganv_canvas_base_request_redraw(item->canvas, x1, y1, x2, y2); - ganv_canvas_base_request_redraw(item->canvas, 0, 0, 10000, 10000); + ganv_canvas_request_redraw(item->canvas, x1, y1, x2, y2); + ganv_canvas_request_redraw(item->canvas, 0, 0, 10000, 10000); } static void @@ -290,8 +289,8 @@ ganv_circle_update(GanvItem* item, int flags) ganv_item_i2w_pair(item, &x1, &y1, &x2, &y2); // Update item canvas coordinates - ganv_canvas_base_w2c_d(GANV_CANVAS_BASE(item->canvas), x1, y1, &item->x1, &item->y1); - ganv_canvas_base_w2c_d(GANV_CANVAS_BASE(item->canvas), x2, y2, &item->x2, &item->y2); + ganv_canvas_w2c_d(item->canvas, x1, y1, &item->x1, &item->y1); + ganv_canvas_w2c_d(item->canvas, x2, y2, &item->x2, &item->y2); // Request redraw of new location request_redraw(item, &impl->coords, FALSE); diff --git a/src/edge.c b/src/edge.c index 2743100..8f557b7 100644 --- a/src/edge.c +++ b/src/edge.c @@ -18,7 +18,6 @@ #include -#include "ganv/canvas-base.h" #include "ganv/canvas.h" #include "ganv/edge.h" #include "ganv/node.h" @@ -164,7 +163,7 @@ ganv_edge_get_property(GObject* object, } void -ganv_edge_request_redraw(GanvCanvasBase* canvas, +ganv_edge_request_redraw(GanvCanvas* canvas, const GanvEdgeCoords* coords) { const double w = coords->width; @@ -184,17 +183,17 @@ ganv_edge_request_redraw(GanvCanvasBase* canvas, const double r1y1 = MIN(MIN(src_y, join_y), src_y1); const double r1x2 = MAX(MAX(src_x, join_x), src_x1); const double r1y2 = MAX(MAX(src_y, join_y), src_y1); - ganv_canvas_base_request_redraw(canvas, - r1x1 - w, r1y1 - w, - r1x2 + w, r1y2 + w); + ganv_canvas_request_redraw(canvas, + r1x1 - w, r1y1 - w, + r1x2 + w, r1y2 + w); const double r2x1 = MIN(MIN(dst_x, join_x), dst_x1); const double r2y1 = MIN(MIN(dst_y, join_y), dst_y1); const double r2x2 = MAX(MAX(dst_x, join_x), dst_x1); const double r2y2 = MAX(MAX(dst_y, join_y), dst_y1); - ganv_canvas_base_request_redraw(canvas, - r2x1 - w, r2y1 - w, - r2x2 + w, r2y2 + w); + ganv_canvas_request_redraw(canvas, + r2x1 - w, r2y1 - w, + r2x2 + w, r2y2 + w); } else { const double x1 = MIN(coords->x1, coords->x2); @@ -202,13 +201,13 @@ ganv_edge_request_redraw(GanvCanvasBase* canvas, const double x2 = MAX(coords->x1, coords->x2); const double y2 = MAX(coords->y1, coords->y2); - ganv_canvas_base_request_redraw(canvas, - x1 - w, y1 - w, - x2 + w, y2 + w); + ganv_canvas_request_redraw(canvas, + x1 - w, y1 - w, + x2 + w, y2 + w); } if (coords->handle_radius > 0.0) { - ganv_canvas_base_request_redraw( + ganv_canvas_request_redraw( canvas, coords->handle_x - coords->handle_radius - w, coords->handle_y - coords->handle_radius - w, @@ -217,7 +216,7 @@ ganv_edge_request_redraw(GanvCanvasBase* canvas, } if (coords->arrowhead) { - ganv_canvas_base_request_redraw( + ganv_canvas_request_redraw( canvas, coords->x2 - ARROW_DEPTH, coords->y2 - ARROW_BREADTH, @@ -308,8 +307,8 @@ ganv_edge_update(GanvItem* item, int flags) } // Update item canvas coordinates - ganv_canvas_base_w2c_d(GANV_CANVAS_BASE(item->canvas), x1, y1, &item->x1, &item->y1); - ganv_canvas_base_w2c_d(GANV_CANVAS_BASE(item->canvas), x2, y2, &item->x2, &item->y2); + ganv_canvas_w2c_d(item->canvas, x1, y1, &item->x1, &item->y1); + ganv_canvas_w2c_d(item->canvas, x2, y2, &item->x2, &item->y2); // Request redraw of new location ganv_edge_request_redraw(item->canvas, &impl->coords); @@ -621,7 +620,7 @@ ganv_edge_new(GanvCanvas* canvas, va_list args; va_start(args, first_prop_name); ganv_item_construct(&edge->item, - GANV_ITEM(ganv_canvas_get_root(canvas)), + GANV_ITEM(ganv_canvas_root(canvas)), first_prop_name, args); va_end(args); diff --git a/src/ganv-private.h b/src/ganv-private.h index 227c46b..368e056 100644 --- a/src/ganv-private.h +++ b/src/ganv-private.h @@ -22,7 +22,7 @@ extern "C" { #include -#include "ganv/canvas-base.h" +#include "ganv/canvas.h" #include "ganv/text.h" #include "ganv/types.h" @@ -210,21 +210,19 @@ ganv_item_invoke_update(GanvItem* item, int flags); void ganv_item_emit_event(GanvItem* item, GdkEvent* event, gint* finished); -/* CanvasBase */ - void -ganv_canvas_base_request_update(GanvCanvasBase* canvas); +ganv_canvas_request_update(GanvCanvas* canvas); int -ganv_canvas_base_emit_event(GanvCanvasBase* canvas, GdkEvent* event); +ganv_canvas_emit_event(GanvCanvas* canvas, GdkEvent* event); /* For use only by item type implementations. Request that the canvas * eventually redraw the specified region, specified in canvas pixel * coordinates. The region contains (x1, y1) but not (x2, y2). */ void -ganv_canvas_base_request_redraw(GanvCanvasBase* canvas, - int x1, int y1, int x2, int y2); +ganv_canvas_request_redraw(GanvCanvas* canvas, + int x1, int y1, int x2, int y2); /* Edge */ @@ -232,7 +230,7 @@ void ganv_edge_get_coords(const GanvEdge* edge, GanvEdgeCoords* coords); void -ganv_edge_request_redraw(GanvCanvasBase* canvas, +ganv_edge_request_redraw(GanvCanvas* canvas, const GanvEdgeCoords* coords); /* Box */ diff --git a/src/group.c b/src/group.c index 2a05e5e..58f5007 100644 --- a/src/group.c +++ b/src/group.c @@ -20,7 +20,7 @@ #include -#include "ganv/canvas-base.h" +#include "ganv/canvas.h" #include "ganv/group.h" #include "./gettext.h" diff --git a/src/item.c b/src/item.c index 05295bb..d3f9678 100644 --- a/src/item.c +++ b/src/item.c @@ -19,7 +19,6 @@ */ #include "ganv/canvas.h" -#include "ganv/canvas-base.h" #include "ganv/node.h" #include "./boilerplate.h" @@ -27,9 +26,31 @@ #include "./ganv-private.h" #include "./gettext.h" -#define GCI_UPDATE_MASK (GANV_CANVAS_BASE_UPDATE_REQUESTED \ - | GANV_CANVAS_BASE_UPDATE_AFFINE \ - | GANV_CANVAS_BASE_UPDATE_VISIBILITY) +/** + * #GanvItem: Base class for all canvas items. + * + * All canvas items are derived from GanvItem. The only information a GanvItem + * contains is its parent canvas, its parent canvas item, its bounding box in + * world coordinates, and its current affine transformation. + * + * Items inside a canvas are organized in a tree, where leaves are items + * without any children. Each canvas has a single root item, which can be + * obtained with the ganv_canvas_base_get_root() function. + * + * The abstract GanvItem class does not have any configurable or queryable + * attributes. + */ + +/* Update flags for items */ +enum { + GANV_CANVAS_UPDATE_REQUESTED = 1 << 0, + GANV_CANVAS_UPDATE_AFFINE = 1 << 1, + GANV_CANVAS_UPDATE_VISIBILITY = 1 << 2, +}; + +#define GCI_UPDATE_MASK (GANV_CANVAS_UPDATE_REQUESTED \ + | GANV_CANVAS_UPDATE_AFFINE \ + | GANV_CANVAS_UPDATE_VISIBILITY) enum { ITEM_PROP_0, @@ -48,7 +69,7 @@ static guint item_signals[ITEM_LAST_SIGNAL]; G_DEFINE_TYPE(GanvItem, ganv_item, GTK_TYPE_OBJECT) -static GtkObjectClass * item_parent_class; +static GtkObjectClass* item_parent_class; /* Object initialization function for GanvItem */ static void @@ -70,7 +91,7 @@ ganv_item_init(GanvItem* item) * Creates a new canvas item with @parent as its parent group. The item is * created at the top of its parent's stack, and starts up as visible. The item * is of the specified @type, for example, it can be - * ganv_canvas_base_rect_get_type(). The list of object arguments/value pairs is + * ganv_canvas_rect_get_type(). The list of object arguments/value pairs is * used to configure the item. If you need to pass construct time parameters, you * should use g_object_new() to pass the parameters and * ganv_item_construct() to set up the canvas item. @@ -108,9 +129,9 @@ item_post_create_setup(GanvItem* item) g_warning("item added to non-parent item\n"); } } - ganv_canvas_base_request_redraw(item->canvas, - item->x1, item->y1, - item->x2 + 1, item->y2 + 1); + ganv_canvas_request_redraw(item->canvas, + item->x1, item->y1, + item->x2 + 1, item->y2 + 1); item->canvas->need_repick = TRUE; } @@ -212,8 +233,8 @@ static void redraw_if_visible(GanvItem* item) { if (item->object.flags & GANV_ITEM_VISIBLE) { - ganv_canvas_base_request_redraw(item->canvas, item->x1, item->y1, item->x2 + 1, - item->y2 + 1); + ganv_canvas_request_redraw(item->canvas, item->x1, item->y1, item->x2 + 1, + item->y2 + 1); } } @@ -332,14 +353,14 @@ ganv_item_invoke_update(GanvItem* item, int flags) /* apply object flags to child flags */ - child_flags &= ~GANV_CANVAS_BASE_UPDATE_REQUESTED; + child_flags &= ~GANV_CANVAS_UPDATE_REQUESTED; if (item->object.flags & GANV_ITEM_NEED_UPDATE) { - child_flags |= GANV_CANVAS_BASE_UPDATE_REQUESTED; + child_flags |= GANV_CANVAS_UPDATE_REQUESTED; } if (item->object.flags & GANV_ITEM_NEED_VIS) { - child_flags |= GANV_CANVAS_BASE_UPDATE_VISIBILITY; + child_flags |= GANV_CANVAS_UPDATE_VISIBILITY; } if (child_flags & GCI_UPDATE_MASK) { @@ -432,8 +453,8 @@ ganv_item_show(GanvItem* item) if (!(item->object.flags & GANV_ITEM_VISIBLE)) { item->object.flags |= GANV_ITEM_VISIBLE; - ganv_canvas_base_request_redraw(item->canvas, item->x1, item->y1, item->x2 + 1, - item->y2 + 1); + ganv_canvas_request_redraw(item->canvas, item->x1, item->y1, item->x2 + 1, + item->y2 + 1); item->canvas->need_repick = TRUE; } } @@ -452,8 +473,8 @@ ganv_item_hide(GanvItem* item) if (item->object.flags & GANV_ITEM_VISIBLE) { item->object.flags &= ~GANV_ITEM_VISIBLE; - ganv_canvas_base_request_redraw(item->canvas, item->x1, item->y1, item->x2 + 1, - item->y2 + 1); + ganv_canvas_request_redraw(item->canvas, item->x1, item->y1, item->x2 + 1, + item->y2 + 1); item->canvas->need_repick = TRUE; } } @@ -537,7 +558,7 @@ ganv_item_i2w_offset(GanvItem* item, double* px, double* py) { double x = 0.0; double y = 0.0; - while (item) { + while (item) { x += item->x; y += item->y; item = item->parent; @@ -559,8 +580,8 @@ void ganv_item_i2w(GanvItem* item, double* x, double* y) { /*g_return_if_fail(GANV_IS_ITEM(item)); - g_return_if_fail(x != NULL); - g_return_if_fail(y != NULL);*/ + g_return_if_fail(x != NULL); + g_return_if_fail(y != NULL);*/ double off_x; double off_y; @@ -628,7 +649,7 @@ ganv_item_grab_focus(GanvItem* item) ev.focus_change.send_event = FALSE; ev.focus_change.in = FALSE; - ganv_canvas_base_emit_event(item->canvas, &ev); + ganv_canvas_emit_event(item->canvas, &ev); } item->canvas->focused_item = item; @@ -640,7 +661,7 @@ ganv_item_grab_focus(GanvItem* item) ev.focus_change.send_event = FALSE; ev.focus_change.in = TRUE; - ganv_canvas_base_emit_event(item->canvas, &ev); + ganv_canvas_emit_event(item->canvas, &ev); } } @@ -702,7 +723,7 @@ ganv_item_request_update(GanvItem* item) ganv_item_request_update(item->parent); } else { /* Have reached the top of the tree, make sure the update call gets scheduled. */ - ganv_canvas_base_request_update(item->canvas); + ganv_canvas_request_update(item->canvas); } } @@ -736,43 +757,43 @@ ganv_item_class_init(GanvItemClass* klass) gobject_class->get_property = ganv_item_get_property; g_object_class_install_property - (gobject_class, ITEM_PROP_PARENT, - g_param_spec_object("parent", NULL, NULL, - GANV_TYPE_ITEM, - (G_PARAM_READABLE | G_PARAM_WRITABLE))); + (gobject_class, ITEM_PROP_PARENT, + g_param_spec_object("parent", NULL, NULL, + GANV_TYPE_ITEM, + (G_PARAM_READABLE | G_PARAM_WRITABLE))); g_object_class_install_property - (gobject_class, ITEM_PROP_X, - g_param_spec_double("x", - _("X"), - _("X"), - -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, - (G_PARAM_READABLE | G_PARAM_WRITABLE))); + (gobject_class, ITEM_PROP_X, + g_param_spec_double("x", + _("X"), + _("X"), + -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, + (G_PARAM_READABLE | G_PARAM_WRITABLE))); g_object_class_install_property - (gobject_class, ITEM_PROP_Y, - g_param_spec_double("y", - _("Y"), - _("Y"), - -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, - (G_PARAM_READABLE | G_PARAM_WRITABLE))); + (gobject_class, ITEM_PROP_Y, + g_param_spec_double("y", + _("Y"), + _("Y"), + -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, + (G_PARAM_READABLE | G_PARAM_WRITABLE))); g_object_class_install_property - (gobject_class, ITEM_PROP_MANAGED, - g_param_spec_boolean("managed", - _("Managed"), - _("Whether the item is managed by its parent"), - 0, - (G_PARAM_READABLE | G_PARAM_WRITABLE))); + (gobject_class, ITEM_PROP_MANAGED, + g_param_spec_boolean("managed", + _("Managed"), + _("Whether the item is managed by its parent"), + 0, + (G_PARAM_READABLE | G_PARAM_WRITABLE))); item_signals[ITEM_EVENT] - = g_signal_new("event", - G_TYPE_FROM_CLASS(klass), - G_SIGNAL_RUN_LAST, - G_STRUCT_OFFSET(GanvItemClass, event), - boolean_handled_accumulator, NULL, - ganv_marshal_BOOLEAN__BOXED, - G_TYPE_BOOLEAN, 1, - GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE); + = g_signal_new("event", + G_TYPE_FROM_CLASS(klass), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET(GanvItemClass, event), + boolean_handled_accumulator, NULL, + ganv_marshal_BOOLEAN__BOXED, + G_TYPE_BOOLEAN, 1, + GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE); gobject_class->dispose = ganv_item_dispose; diff --git a/src/node.c b/src/node.c index a728cb0..8e8e0ad 100644 --- a/src/node.c +++ b/src/node.c @@ -14,7 +14,6 @@ */ #include "ganv/canvas.h" -#include "ganv/canvas-base.h" #include "ganv/node.h" #include "./boilerplate.h" @@ -169,8 +168,8 @@ ganv_node_set_property(GObject* object, break; case PROP_CANVAS: if (!GANV_ITEM(object)->parent) { - GanvCanvasBase* canvas = GANV_CANVAS_BASE(g_value_get_object(value)); - g_object_set(object, "parent", ganv_canvas_base_root(canvas), NULL); + GanvCanvas* canvas = GANV_CANVAS(g_value_get_object(value)); + g_object_set(object, "parent", ganv_canvas_root(canvas), NULL); ganv_canvas_add_node(GANV_CANVAS(canvas), node); } else { g_warning("Cannot change `canvas' property after construction"); diff --git a/src/text.c b/src/text.c index 137bfa5..3d3f938 100644 --- a/src/text.c +++ b/src/text.c @@ -20,7 +20,6 @@ #include -#include "ganv/canvas-base.h" #include "ganv/canvas.h" #include "ganv/text.h" @@ -246,7 +245,7 @@ ganv_text_update(GanvItem* item, int flags) parent_class->update(item, flags); - ganv_canvas_base_request_redraw(item->canvas, x1, y1, x2, y2); + ganv_canvas_request_redraw(item->canvas, x1, y1, x2, y2); } static double diff --git a/src/widget.c b/src/widget.c index afbf56c..63c1dfd 100644 --- a/src/widget.c +++ b/src/widget.c @@ -21,7 +21,7 @@ #include -#include "ganv/canvas-base.h" +#include "ganv/canvas.h" #include "ganv/widget.h" #include "./gettext.h" @@ -85,7 +85,7 @@ recalc_bounds(GanvWidget* witem) /* Get canvas pixel coordinates */ - ganv_canvas_base_w2c(item->canvas, wx, wy, &witem->cx, &witem->cy); + ganv_canvas_w2c(item->canvas, wx, wy, &witem->cx, &witem->cy); /* Anchor widget item */ @@ -330,7 +330,7 @@ ganv_widget_point(GanvItem* item, double x, double y, *actual_item = item; double x1, y1; - ganv_canvas_base_c2w(item->canvas, witem->cx, witem->cy, &x1, &y1); + ganv_canvas_c2w(item->canvas, witem->cx, witem->cy, &x1, &y1); double x2 = x1 + (witem->cwidth - 1) / item->canvas->pixels_per_unit; double y2 = y1 + (witem->cheight - 1) / item->canvas->pixels_per_unit; diff --git a/wscript b/wscript index fca4701..08c6956 100644 --- a/wscript +++ b/wscript @@ -83,7 +83,6 @@ ganv_source = [ 'src/Canvas.cpp', 'src/Port.cpp', 'src/box.c', - 'src/canvas-base.c', 'src/circle.c', 'src/edge.c', 'src/ganv-marshal.c', -- cgit v1.2.1