diff options
author | David Robillard <d@drobilla.net> | 2011-12-09 05:48:35 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2011-12-09 05:48:35 +0000 |
commit | 985d9b8babb2faac0de62cb684fc14c7910e9909 (patch) | |
tree | e59ebd3ae5144f503795aadbcbd4fb570c22377e /src | |
parent | 00df069cdf55e115ea734119c0ba97b78192cfcc (diff) | |
download | ganv-985d9b8babb2faac0de62cb684fc14c7910e9909.tar.gz ganv-985d9b8babb2faac0de62cb684fc14c7910e9909.tar.bz2 ganv-985d9b8babb2faac0de62cb684fc14c7910e9909.zip |
Remove GnomeCanvas dependency.
git-svn-id: http://svn.drobilla.net/lad/trunk/ganv@3842 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'src')
-rw-r--r-- | src/Canvas.cpp | 169 | ||||
-rw-r--r-- | src/boilerplate.h | 4 | ||||
-rw-r--r-- | src/box.c | 88 | ||||
-rw-r--r-- | src/canvas-base.c | 3820 | ||||
-rw-r--r-- | src/circle.c | 65 | ||||
-rw-r--r-- | src/edge.c | 70 | ||||
-rw-r--r-- | src/ganv-marshal.list | 2 | ||||
-rw-r--r-- | src/ganv-private.h | 20 | ||||
-rw-r--r-- | src/ganv_test.c | 7 | ||||
-rwxr-xr-x | src/ganv_test.py | 7 | ||||
-rw-r--r-- | src/module.c | 111 | ||||
-rw-r--r-- | src/node.c | 81 | ||||
-rw-r--r-- | src/port.c | 42 | ||||
-rw-r--r-- | src/text.c | 62 | ||||
-rw-r--r-- | src/widget.c | 508 |
15 files changed, 4682 insertions, 374 deletions
diff --git a/src/Canvas.cpp b/src/Canvas.cpp index f81c269..9de1a43 100644 --- a/src/Canvas.cpp +++ b/src/Canvas.cpp @@ -31,8 +31,6 @@ #include <gtkmm/widget.h> -#include <libgnomecanvas/gnome-canvas-pixbuf.h> - #include "ganv-config.h" #include "ganv/Canvas.hpp" #include "ganv/Edge.hpp" @@ -133,14 +131,14 @@ struct GanvCanvasImpl { , _layout(GTK_LAYOUT(_gcanvas)) , _connect_port(NULL) , _last_selected_port(NULL) - , _base_rect(gnome_canvas_item_new( - gnome_canvas_root(GNOME_CANVAS(_gcanvas)), - gnome_canvas_rect_get_type(), + , _base_rect(ganv_item_new( + ganv_canvas_base_root(GANV_CANVAS_BASE(_gcanvas)), + ganv_box_get_type(), "x1", 0.0, "y1", 0.0, "x2", 1.0, "y2", 1.0, - "fill-color-rgba", 0x000000FF, + "fill-color", 0x000000FF, NULL)) , _select_rect(NULL) , _zoom(1.0) @@ -150,7 +148,7 @@ struct GanvCanvasImpl { _wrapper_key = g_quark_from_string("ganvmm"); _move_cursor = gdk_cursor_new(GDK_FLEUR); - g_signal_connect(G_OBJECT(gnome_canvas_root(GNOME_CANVAS(_gcanvas))), + g_signal_connect(G_OBJECT(ganv_canvas_base_root(GANV_CANVAS_BASE(_gcanvas))), "event", G_CALLBACK(on_canvas_event), this); } @@ -161,15 +159,15 @@ struct GanvCanvasImpl { } static gboolean - on_canvas_event(GnomeCanvasItem* canvasitem, - GdkEvent* ev, - void* impl) + on_canvas_event(GanvItem* canvasitem, + GdkEvent* ev, + void* impl) { return ((GanvCanvasImpl*)impl)->on_event(ev); } - GnomeCanvasGroup* root() { - return gnome_canvas_root(GNOME_CANVAS(_gcanvas)); + GanvGroup* root() { + return ganv_canvas_base_root(GANV_CANVAS_BASE(_gcanvas)); } void resize(double width, double height); @@ -248,8 +246,8 @@ struct GanvCanvasImpl { GanvPort* _connect_port; ///< Port for which a edge is being made GanvPort* _last_selected_port; - GnomeCanvasItem* _base_rect; ///< Background - GanvBox* _select_rect; ///< Rectangle for drag selection + GanvItem* _base_rect; ///< Background + GanvBox* _select_rect; ///< Rectangle for drag selection double _zoom; ///< Current zoom level double _font_size; ///< Current font size in points @@ -262,9 +260,9 @@ struct GanvCanvasImpl { }; typedef struct { - GnomeCanvasItem item; - GanvEdgeImpl* impl; - GanvEdgeImpl impl_data; + GanvItem item; + GanvEdgeImpl* impl; + GanvEdgeImpl impl_data; } GanvEdgeKey; static void @@ -297,14 +295,14 @@ GanvCanvasImpl::first_edge_to(const GanvNode* head) void GanvCanvasImpl::select_edge(GanvEdge* edge) { - gnome_canvas_item_set(GNOME_CANVAS_ITEM(edge), "selected", TRUE, NULL); + ganv_item_set(GANV_ITEM(edge), "selected", TRUE, NULL); _selected_edges.insert(edge); } void GanvCanvasImpl::unselect_edge(GanvEdge* edge) { - gnome_canvas_item_set(GNOME_CANVAS_ITEM(edge), "selected", FALSE, NULL); + ganv_item_set(GANV_ITEM(edge), "selected", FALSE, NULL); _selected_edges.erase(edge); } @@ -374,7 +372,8 @@ select_edges(GanvPort* port, void* data) void GanvCanvasImpl::add_item(GanvNode* n) { - if (GNOME_CANVAS_ITEM(n)->parent == GNOME_CANVAS_ITEM(root())) { + GanvItem* item = GANV_ITEM(n); + if (item != _base_rect && item->parent == GANV_ITEM(root())) { _items.insert(n); } } @@ -656,7 +655,7 @@ GanvCanvasImpl::select_port_toggle(GanvPort* port, int mod_state) // Pivot around _last_selected_port in a single pass over module ports each click GanvPort* old_last_selected = _last_selected_port; GanvPort* first = NULL; - bool done = false; + bool done = false; for (size_t i = 0; i < ganv_module_num_ports(m); ++i) { GanvPort* const p = ganv_module_get_port(m, i); if (!first && !done && (p == _last_selected_port || p == port)) { @@ -739,7 +738,7 @@ GanvCanvasImpl::join_selection() GanvNode* GanvCanvasImpl::get_node_at(double x, double y) { - GnomeCanvasItem* item = gnome_canvas_get_item_at(GNOME_CANVAS(_gcanvas), x, y); + GanvItem* item = ganv_canvas_base_get_item_at(GANV_CANVAS_BASE(_gcanvas), x, y); while (item) { if (GANV_IS_NODE(item)) { return GANV_NODE(item); @@ -761,7 +760,7 @@ GanvCanvasImpl::on_event(GdkEvent* event) switch (event->type) { case GDK_KEY_PRESS: handled = true; - gnome_canvas_get_scroll_offsets(GNOME_CANVAS(_gcanvas), &scroll_x, &scroll_y); + ganv_canvas_base_get_scroll_offsets(GANV_CANVAS_BASE(_gcanvas), &scroll_x, &scroll_y); switch (event->key.keyval) { case GDK_Up: scroll_y -= scroll_increment; @@ -785,7 +784,7 @@ GanvCanvasImpl::on_event(GdkEvent* event) handled = false; } if (handled) { - gnome_canvas_scroll_to(GNOME_CANVAS(_gcanvas), scroll_x, scroll_y); + ganv_canvas_base_scroll_to(GANV_CANVAS_BASE(_gcanvas), scroll_x, scroll_y); return true; } break; @@ -827,11 +826,11 @@ GanvCanvasImpl::scroll_drag_handler(GdkEvent* event) static double last_y = 0; if (event->type == GDK_BUTTON_PRESS && event->button.button == 2) { - gnome_canvas_item_grab( - GNOME_CANVAS_ITEM(_base_rect), + ganv_item_grab( + GANV_ITEM(_base_rect), GDK_POINTER_MOTION_MASK|GDK_BUTTON_RELEASE_MASK, NULL, event->button.time); - gnome_canvas_get_scroll_offsets(GNOME_CANVAS(_gcanvas), &original_scroll_x, &original_scroll_y); + ganv_canvas_base_get_scroll_offsets(GANV_CANVAS_BASE(_gcanvas), &original_scroll_x, &original_scroll_y); scroll_offset_x = 0; scroll_offset_y = 0; origin_x = event->button.x_root; @@ -852,13 +851,13 @@ GanvCanvasImpl::scroll_drag_handler(GdkEvent* event) scroll_offset_x += x_offset; scroll_offset_y += y_offset; - gnome_canvas_scroll_to(GNOME_CANVAS(_gcanvas), + ganv_canvas_base_scroll_to(GANV_CANVAS_BASE(_gcanvas), lrint(original_scroll_x + scroll_offset_x), lrint(original_scroll_y + scroll_offset_y)); last_x = x; last_y = y; } else if (event->type == GDK_BUTTON_RELEASE && _drag_state == SCROLL) { - gnome_canvas_item_ungrab(GNOME_CANVAS_ITEM(_base_rect), event->button.time); + ganv_item_ungrab(GANV_ITEM(_base_rect), event->button.time); _drag_state = NOT_DRAGGING; } else { handled = false; @@ -892,7 +891,7 @@ GanvCanvasImpl::select_drag_handler(GdkEvent* event) if ( !(event->button.state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) ) clear_selection(); _select_rect = GANV_BOX( - gnome_canvas_item_new( + ganv_item_new( root(), ganv_box_get_type(), "x1", event->button.x, @@ -902,20 +901,20 @@ GanvCanvasImpl::select_drag_handler(GdkEvent* event) "fill-color", SELECT_RECT_FILL_COLOUR, "border-color", SELECT_RECT_BORDER_COLOUR, NULL)); - gnome_canvas_item_lower_to_bottom(GNOME_CANVAS_ITEM(_select_rect)); - gnome_canvas_item_lower_to_bottom(GNOME_CANVAS_ITEM(_base_rect)); - gnome_canvas_item_grab( - GNOME_CANVAS_ITEM(_base_rect), GDK_POINTER_MOTION_MASK|GDK_BUTTON_RELEASE_MASK, + ganv_item_lower_to_bottom(GANV_ITEM(_select_rect)); + ganv_item_lower_to_bottom(GANV_ITEM(_base_rect)); + ganv_item_grab( + GANV_ITEM(_base_rect), GDK_POINTER_MOTION_MASK|GDK_BUTTON_RELEASE_MASK, NULL, event->button.time); return true; } else if (event->type == GDK_MOTION_NOTIFY && _drag_state == SELECT) { assert(_select_rect); double x, y; get_motion_coords(&event->motion, &x, &y); - gnome_canvas_item_set(GNOME_CANVAS_ITEM(_select_rect), - "x2", x, - "y2", y, - NULL); + ganv_item_set(GANV_ITEM(_select_rect), + "x2", x, + "y2", y, + NULL); return true; } else if (event->type == GDK_BUTTON_RELEASE && _drag_state == SELECT) { // Normalize select rect @@ -952,7 +951,7 @@ GanvCanvasImpl::select_drag_handler(GdkEvent* event) } } - gnome_canvas_item_ungrab(GNOME_CANVAS_ITEM(_base_rect), event->button.time); + ganv_item_ungrab(GANV_ITEM(_base_rect), event->button.time); gtk_object_destroy(GTK_OBJECT(_select_rect)); _select_rect = NULL; @@ -976,7 +975,7 @@ GanvCanvasImpl::connect_drag_handler(GdkEvent* event) if (event->type == GDK_MOTION_NOTIFY) { double x, y; get_motion_coords(&event->motion, &x, &y); - gnome_canvas_item_i2w(GNOME_CANVAS_ITEM(_base_rect), &x, &y); + ganv_item_i2w(GANV_ITEM(_base_rect), &x, &y); if (!drag_edge) { // Create drag edge @@ -984,7 +983,7 @@ GanvCanvasImpl::connect_drag_handler(GdkEvent* event) assert(_connect_port); drag_node = GANV_NODE( - gnome_canvas_item_new(gnome_canvas_root(GNOME_CANVAS(_gcanvas)), + ganv_item_new(ganv_canvas_base_root(GANV_CANVAS_BASE(_gcanvas)), ganv_node_get_type(), "x", x, "y", y, @@ -1004,30 +1003,30 @@ GanvCanvasImpl::connect_drag_handler(GdkEvent* event) if (joinee && ganv_node_can_head(joinee) && joinee != drag_node) { // Snap to item snapped = true; - gnome_canvas_item_set(&drag_edge->item, - "head", joinee, - NULL); + ganv_item_set(&drag_edge->item, + "head", joinee, + NULL); } else if (snapped) { // Unsnap from item snapped = false; - gnome_canvas_item_set(&drag_edge->item, - "head", drag_node, - NULL); + ganv_item_set(&drag_edge->item, + "head", drag_node, + NULL); } // Update drag edge for pointer position ganv_node_move_to(drag_node, x, y); - gnome_canvas_item_request_update(&drag_node->group.item); - gnome_canvas_item_request_update(&drag_edge->item); + ganv_item_request_update(&drag_node->group.item); + ganv_item_request_update(&drag_edge->item); return true; } else if (event->type == GDK_BUTTON_RELEASE) { - gnome_canvas_item_ungrab(GNOME_CANVAS_ITEM(_base_rect), event->button.time); + ganv_item_ungrab(GANV_ITEM(_base_rect), event->button.time); double x = event->button.x; double y = event->button.y; - gnome_canvas_item_i2w(GNOME_CANVAS_ITEM(_base_rect), &x, &y); + ganv_item_i2w(GANV_ITEM(_base_rect), &x, &y); GanvNode* joinee = get_node_at(x, y); @@ -1174,8 +1173,8 @@ switch (event->type) { _drag_state = GanvCanvasImpl::EDGE; _connect_port = port; port_dragging = false; - gnome_canvas_item_grab( - GNOME_CANVAS_ITEM(_base_rect), + ganv_item_grab( + GANV_ITEM(_base_rect), GDK_BUTTON_PRESS_MASK|GDK_POINTER_MOTION_MASK|GDK_BUTTON_RELEASE_MASK, NULL, event->crossing.time); return true; @@ -1314,11 +1313,11 @@ GanvCanvasImpl::clear_selection() unselect_ports(); FOREACH_ITEM(_selected_items, i) { - gnome_canvas_item_set(GNOME_CANVAS_ITEM(*i), "selected", FALSE, NULL); + ganv_item_set(GANV_ITEM(*i), "selected", FALSE, NULL); } FOREACH_SELECTED_EDGE(_selected_edges, c) { - gnome_canvas_item_set(GNOME_CANVAS_ITEM(*c), "selected", FALSE, NULL); + ganv_item_set(GANV_ITEM(*c), "selected", FALSE, NULL); } _selected_items.clear(); @@ -1329,14 +1328,11 @@ void GanvCanvasImpl::resize(double width, double height) { if (width != _gcanvas->width || height != _gcanvas->height) { - gnome_canvas_item_set( - GNOME_CANVAS_ITEM(_base_rect), - "x2", width, - "y2", height, - NULL); + ganv_box_set_width(GANV_BOX(_base_rect), width); + ganv_box_set_height(GANV_BOX(_base_rect), height); _gcanvas->width = width; _gcanvas->height = height; - gnome_canvas_set_scroll_region(GNOME_CANVAS(_gcanvas), + ganv_canvas_base_set_scroll_region(GANV_CANVAS_BASE(_gcanvas), 0.0, 0.0, width, height); } } @@ -1344,9 +1340,9 @@ GanvCanvasImpl::resize(double width, double height) namespace Ganv { static gboolean -on_event_after(GnomeCanvasItem* canvasitem, - GdkEvent* ev, - void* canvas) +on_event_after(GanvItem* canvasitem, + GdkEvent* ev, + void* canvas) { return ((Canvas*)canvas)->signal_event.emit(ev); } @@ -1410,7 +1406,7 @@ Canvas::set_zoom_and_font_size(double zoom, double points) if (zoom != impl()->_zoom) { impl()->_zoom = zoom; - gnome_canvas_set_pixels_per_unit(GNOME_CANVAS(impl()->_gcanvas), zoom); + ganv_canvas_base_set_pixels_per_unit(GANV_CANVAS_BASE(impl()->_gcanvas), zoom); } impl()->_font_size = points; @@ -1430,7 +1426,7 @@ Canvas::zoom_full() return; int win_width, win_height; - Glib::RefPtr<Gdk::Window> win = GNOME_CANVAS(impl()->_gcanvas)->get_window(); + Glib::RefPtr<Gdk::Window> win = GANV_CANVAS_BASE(impl()->_gcanvas)->get_window(); win->get_size(win_width, win_height); // Box containing all canvas items @@ -1459,9 +1455,9 @@ Canvas::zoom_full() set_zoom(new_zoom); int scroll_x, scroll_y; - GNOME_CANVAS(impl()->_gcanvas)->w2c(lrintf(left - pad), lrintf(bottom - pad), scroll_x, scroll_y); + GANV_CANVAS_BASE(impl()->_gcanvas)->w2c(lrintf(left - pad), lrintf(bottom - pad), scroll_x, scroll_y); - GNOME_CANVAS(impl()->_gcanvas)->scroll_to(scroll_x, scroll_y); + GANV_CANVAS_BASE(impl()->_gcanvas)->scroll_to(scroll_x, scroll_y); #endif } @@ -1567,7 +1563,7 @@ Canvas::arrange(bool use_length_hints) // Arrange to graphviz coordinates for (GVNodes::iterator i = nodes.begin(); i != nodes.end(); ++i) { - if (GNOME_CANVAS_ITEM(i->first)->parent != GNOME_CANVAS_ITEM(root())) { + if (GANV_ITEM(i->first)->parent != GANV_ITEM(root())) { continue; } const string pos = agget(i->second, (char*)"pos"); @@ -1612,7 +1608,7 @@ Canvas::arrange(bool use_length_hints) static const double border_width = impl()->_font_size; impl()->move_contents_to_internal(border_width, border_width, least_x, least_y); - gnome_canvas_scroll_to(GNOME_CANVAS(impl()->_gcanvas), 0, 0); + ganv_canvas_base_scroll_to(GANV_CANVAS_BASE(impl()->_gcanvas), 0, 0); FOREACH_ITEM(impl()->_items, i) { double x, y; @@ -1673,10 +1669,10 @@ Canvas::for_each_selected_edge(EdgePtrFunction f, void* data) } } -GnomeCanvasGroup* +GanvGroup* Canvas::root() { - return gnome_canvas_root(GNOME_CANVAS(impl()->_gcanvas)); + return ganv_canvas_base_root(GANV_CANVAS_BASE(impl()->_gcanvas)); } Gtk::Layout& @@ -1688,13 +1684,13 @@ Canvas::widget() void Canvas::get_scroll_offsets(int& cx, int& cy) const { - gnome_canvas_get_scroll_offsets(GNOME_CANVAS(impl()->_gcanvas), &cx, &cy); + ganv_canvas_base_get_scroll_offsets(GANV_CANVAS_BASE(impl()->_gcanvas), &cx, &cy); } void Canvas::scroll_to(int x, int y) { - gnome_canvas_scroll_to(GNOME_CANVAS(impl()->_gcanvas), x, y); + ganv_canvas_base_scroll_to(GANV_CANVAS_BASE(impl()->_gcanvas), x, y); } GQuark @@ -1726,17 +1722,16 @@ Canvas::gobj() const extern "C" { -#include <libgnomecanvas/libgnomecanvas.h> - +#include "ganv/canvas-base.h" #include "ganv/canvas.h" #include "./boilerplate.h" #include "./color.h" #include "./gettext.h" -G_DEFINE_TYPE(GanvCanvas, ganv_canvas, GNOME_TYPE_CANVAS) +G_DEFINE_TYPE(GanvCanvas, ganv_canvas, GANV_TYPE_CANVAS_BASE) -static GnomeCanvasClass* parent_class; +static GanvCanvasBaseClass* parent_class; enum { PROP_0, @@ -1775,12 +1770,12 @@ ganv_canvas_set_property(GObject* object, case PROP_LOCKED: { const gboolean tmp = g_value_get_boolean(value); if (canvas->locked != tmp) { - GnomeCanvasItem* base = GNOME_CANVAS_ITEM(canvas->impl->_base_rect); - gnome_canvas_item_set(base, - "fill-color", tmp ? 0x131415FF : 0x000000FF, - NULL); + GanvItem* base = GANV_ITEM(canvas->impl->_base_rect); + ganv_item_set(base, + "fill-color", tmp ? 0x131415FF : 0x000000FF, + NULL); canvas->locked = tmp; - gnome_canvas_item_request_update(base); + ganv_item_request_update(base); } break; } @@ -1816,7 +1811,7 @@ ganv_canvas_class_init(GanvCanvasClass* klass) { GObjectClass* gobject_class = (GObjectClass*)klass; - parent_class = GNOME_CANVAS_CLASS(g_type_class_peek_parent(klass)); + parent_class = GANV_CANVAS_BASE_CLASS(g_type_class_peek_parent(klass)); gobject_class->set_property = ganv_canvas_set_property; gobject_class->get_property = ganv_canvas_get_property; @@ -1893,7 +1888,7 @@ ganv_canvas_new(double width, double height) "width", width, "height", height, NULL)); - gnome_canvas_set_scroll_region(GNOME_CANVAS(canvas), + ganv_canvas_base_set_scroll_region(GANV_CANVAS_BASE(canvas), 0.0, 0.0, width, height); return canvas; } @@ -1904,10 +1899,10 @@ ganv_canvas_resize(GanvCanvas* canvas, double width, double height) canvas->impl->resize(width, height); } -GnomeCanvasGroup* +GanvGroup* ganv_canvas_get_root(const GanvCanvas* canvas) { - return gnome_canvas_root(GNOME_CANVAS(canvas->impl->_gcanvas)); + return ganv_canvas_base_root(GANV_CANVAS_BASE(canvas->impl->_gcanvas)); } double diff --git a/src/boilerplate.h b/src/boilerplate.h index a893eb1..7b68a7f 100644 --- a/src/boilerplate.h +++ b/src/boilerplate.h @@ -26,9 +26,9 @@ typedef gpointer gobject; const g##type tmp = g_value_get_##type(value); \ if (field != tmp) { \ field = tmp; \ - GnomeCanvasItem* item = GNOME_CANVAS_ITEM(object); \ + GanvItem* item = GANV_ITEM(object); \ if (item->canvas) { \ - gnome_canvas_item_request_update(item); \ + ganv_item_request_update(item); \ } \ } \ break; \ @@ -147,7 +147,7 @@ ganv_box_bounds_item(const GanvBoxCoords* coords, static void -request_redraw(GnomeCanvasItem* item, +request_redraw(GanvItem* item, const GanvBoxCoords* coords, gboolean world) { @@ -156,37 +156,37 @@ request_redraw(GnomeCanvasItem* item, if (!world) { // Convert from parent-relative coordinates to world coordinates - gnome_canvas_item_i2w(item, &x1, &y1); - gnome_canvas_item_i2w(item, &x2, &y2); + ganv_item_i2w(item, &x1, &y1); + ganv_item_i2w(item, &x2, &y2); } - gnome_canvas_request_redraw(item->canvas, x1, y1, x2, y2); + ganv_canvas_base_request_redraw(item->canvas, x1, y1, x2, y2); } static void -coords_i2w(GnomeCanvasItem* item, GanvBoxCoords* coords) +coords_i2w(GanvItem* item, GanvBoxCoords* coords) { - gnome_canvas_item_i2w(item, &coords->x1, &coords->y1); - gnome_canvas_item_i2w(item, &coords->x2, &coords->y2); + ganv_item_i2w(item, &coords->x1, &coords->y1); + ganv_item_i2w(item, &coords->x2, &coords->y2); } static void -ganv_box_bounds(GnomeCanvasItem* item, +ganv_box_bounds(GanvItem* item, double* x1, double* y1, double* x2, double* y2) { // Note this will not be correct if children are outside the box bounds GanvBox* box = GANV_BOX(item); ganv_box_bounds_item(&box->impl->coords, x1, y1, x2, y2); - gnome_canvas_item_i2w(item, x1, y1); - gnome_canvas_item_i2w(item, x2, y2); + ganv_item_i2w(item, x1, y1); + ganv_item_i2w(item, x2, y2); } static void -ganv_box_update(GnomeCanvasItem* item, - double* affine, - ArtSVP* clip_path, - int flags) +ganv_box_update(GanvItem* item, + double* affine, + ArtSVP* clip_path, + int flags) { GanvBox* box = GANV_BOX(item); GanvBoxImpl* impl = box->impl; @@ -195,7 +195,7 @@ ganv_box_update(GnomeCanvasItem* item, // Request redraw of old location request_redraw(item, &impl->old_coords, TRUE); - GnomeCanvasItemClass* item_class = GNOME_CANVAS_ITEM_CLASS(parent_class); + GanvItemClass* item_class = GANV_ITEM_CLASS(parent_class); item_class->update(item, affine, clip_path, flags); // Store old coordinates in world relative coordinates in case the @@ -208,8 +208,8 @@ ganv_box_update(GnomeCanvasItem* item, ganv_box_bounds(item, &x1, &y1, &x2, &y2); // Update item canvas coordinates - gnome_canvas_w2c_d(GNOME_CANVAS(item->canvas), x1, y1, &item->x1, &item->y1); - gnome_canvas_w2c_d(GNOME_CANVAS(item->canvas), x2, y2, &item->x2, &item->y2); + 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); // Request redraw of new location request_redraw(item, &impl->coords, FALSE); @@ -217,14 +217,7 @@ ganv_box_update(GnomeCanvasItem* item, } static void -ganv_box_render(GnomeCanvasItem* item, - GnomeCanvasBuf* buf) -{ - // Not implemented -} - -static void -ganv_box_draw(GnomeCanvasItem* item, +ganv_box_draw(GanvItem* item, GdkDrawable* drawable, int cx, int cy, int width, int height) @@ -237,8 +230,8 @@ ganv_box_draw(GnomeCanvasItem* item, double y1 = impl->coords.y1; double x2 = impl->coords.x2; double y2 = impl->coords.y2; - gnome_canvas_item_i2w(item, &x1, &y1); - gnome_canvas_item_i2w(item, &x2, &y2); + ganv_item_i2w(item, &x1, &y1); + ganv_item_i2w(item, &x2, &y2); double dash_length, border_color, fill_color; ganv_node_get_draw_properties( @@ -292,17 +285,17 @@ ganv_box_draw(GnomeCanvasItem* item, cairo_stroke(cr); } - GnomeCanvasItemClass* item_class = GNOME_CANVAS_ITEM_CLASS(parent_class); + GanvItemClass* item_class = GANV_ITEM_CLASS(parent_class); item_class->draw(item, drawable, cx, cy, width, height); cairo_destroy(cr); } static double -ganv_box_point(GnomeCanvasItem* item, +ganv_box_point(GanvItem* item, double x, double y, int cx, int cy, - GnomeCanvasItem** actual_item) + GanvItem** actual_item) { GanvBox* box = GANV_BOX(item); GanvBoxImpl* impl = box->impl; @@ -314,7 +307,7 @@ ganv_box_point(GnomeCanvasItem* item, // Point is inside the box (distance 0) if ((x >= x1) && (y >= y1) && (x <= x2) && (y <= y2)) { - GnomeCanvasItemClass* item_class = GNOME_CANVAS_ITEM_CLASS(parent_class); + GanvItemClass* item_class = GANV_ITEM_CLASS(parent_class); double d = item_class->point(item, x, y, cx, cy, actual_item); if (*actual_item) { return d; @@ -363,40 +356,40 @@ ganv_box_is_within(const GanvNode* self, "y2", &by2, NULL); - gnome_canvas_item_i2w(GNOME_CANVAS_ITEM(self), &bx1, &by1); - gnome_canvas_item_i2w(GNOME_CANVAS_ITEM(self), &bx2, &by2); + ganv_item_i2w(GANV_ITEM(self), &bx1, &by1); + ganv_item_i2w(GANV_ITEM(self), &bx2, &by2); return ( bx1 >= x1 - && by2 >= y1 - && bx2 <= x2 - && by2 <= y2); + && by2 >= y1 + && bx2 <= x2 + && by2 <= y2); } static void ganv_box_default_set_width(GanvBox* box, double width) { - gnome_canvas_item_set(GNOME_CANVAS_ITEM(box), - "x2", ganv_box_get_x1(box) + width, - NULL); + ganv_item_set(GANV_ITEM(box), + "x2", ganv_box_get_x1(box) + width, + NULL); } static void ganv_box_default_set_height(GanvBox* box, - double height) + double height) { - gnome_canvas_item_set(GNOME_CANVAS_ITEM(box), - "y2", ganv_box_get_y1(box) + height, - NULL); + ganv_item_set(GANV_ITEM(box), + "y2", ganv_box_get_y1(box) + height, + NULL); } static void ganv_box_class_init(GanvBoxClass* class) { - GObjectClass* gobject_class = (GObjectClass*)class; - GtkObjectClass* object_class = (GtkObjectClass*)class; - GnomeCanvasItemClass* item_class = (GnomeCanvasItemClass*)class; - GanvNodeClass* node_class = (GanvNodeClass*)class; + GObjectClass* gobject_class = (GObjectClass*)class; + GtkObjectClass* object_class = (GtkObjectClass*)class; + GanvItemClass* item_class = (GanvItemClass*)class; + GanvNodeClass* node_class = (GanvNodeClass*)class; parent_class = GANV_NODE_CLASS(g_type_class_peek_parent(class)); @@ -490,7 +483,6 @@ ganv_box_class_init(GanvBoxClass* class) item_class->update = ganv_box_update; item_class->bounds = ganv_box_bounds; item_class->point = ganv_box_point; - item_class->render = ganv_box_render; item_class->draw = ganv_box_draw; node_class->is_within = ganv_box_is_within; diff --git a/src/canvas-base.c b/src/canvas-base.c new file mode 100644 index 0000000..2cff987 --- /dev/null +++ b/src/canvas-base.c @@ -0,0 +1,3820 @@ +/* This file is part of Ganv. + * Copyright 2007-2011 David Robillard <http://drobilla.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +/* Based on GnomeCanvas, by Federico Mena <federico@nuclecu.unam.mx> + * and Raph Levien <raph@gimp.org> + * Copyright 1997-2000 Free Software Foundation + */ + +#include <math.h> +#include <stdio.h> +#include <string.h> + +#include <gdk/gdkprivate.h> +#include <gtk/gtk.h> +#include <cairo.h> + +#include "libart_lgpl/art_rect.h" +#include "libart_lgpl/art_rect_uta.h" +#include "libart_lgpl/art_uta_rect.h" +#include "libart_lgpl/art_uta_ops.h" + +#include "ganv/canvas-base.h" + +#include "./gettext.h" +#include "./ganv-marshal.h" + +/* We must run our idle update handler *before* GDK wants to redraw. */ +#define CANVAS_IDLE_PRIORITY (GDK_PRIORITY_REDRAW - 5) + +static void ganv_canvas_base_request_update(GanvCanvasBase* canvas); +static void group_add(GanvGroup* group, + GanvItem* item); +static void group_remove(GanvGroup* group, + GanvItem* item); +static void add_idle(GanvCanvasBase* canvas); + + +/*** GanvItem ***/ + +/* Some convenience stuff */ +#define GCI_UPDATE_MASK (GANV_CANVAS_BASE_UPDATE_REQUESTED | GANV_CANVAS_BASE_UPDATE_AFFINE \ + | GANV_CANVAS_BASE_UPDATE_CLIP | GANV_CANVAS_BASE_UPDATE_VISIBILITY) +#define GCI_EPSILON 1e-18 +#define GCI_PRINT_MATRIX(s, a) g_print("%s %g %g %g %g %g %g\n", s, (a)[0], (a)[1], (a)[2], (a)[3], \ + (a)[4], (a)[5]) + +enum { + ITEM_PROP_0, + ITEM_PROP_PARENT +}; + +enum { + ITEM_EVENT, + ITEM_LAST_SIGNAL +}; + +static void ganv_item_class_init(GanvItemClass* class); +static void ganv_item_init(GanvItem* item); +static int emit_event(GanvCanvasBase* canvas, GdkEvent* event); + +static guint item_signals[ITEM_LAST_SIGNAL]; + +G_DEFINE_TYPE(GanvItem, ganv_item, GTK_TYPE_OBJECT) + +static GtkObjectClass * item_parent_class; + +/* Object initialization function for GanvItem */ +static void +ganv_item_init(GanvItem* item) +{ + item->object.flags |= GANV_ITEM_VISIBLE; +} + +/** + * ganv_item_new: + * @parent: The parent group for the new item. + * @type: The object type of the item. + * @first_arg_name: A list of object argument name/value pairs, NULL-terminated, + * used to configure the item. For example, "fill_color", "black", + * "width_units", 5.0, NULL. + * @Varargs: + * + * 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 + * 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. + * + * Return value: The newly-created item. + **/ +GanvItem* +ganv_item_new(GanvGroup* parent, GType type, const gchar* first_arg_name, ...) +{ + GanvItem* item; + va_list args; + + g_return_val_if_fail(GANV_IS_GROUP(parent), NULL); + g_return_val_if_fail(g_type_is_a(type, ganv_item_get_type()), NULL); + + item = GANV_ITEM(g_object_new(type, NULL)); + + va_start(args, first_arg_name); + ganv_item_construct(item, parent, first_arg_name, args); + va_end(args); + + return item; +} + +/* Performs post-creation operations on a canvas item (adding it to its parent + * group, etc.) + */ +static void +item_post_create_setup(GanvItem* item) +{ + group_add(GANV_GROUP(item->parent), item); + + ganv_canvas_base_request_redraw(item->canvas, item->x1, item->y1, item->x2 + 1, item->y2 + 1); + item->canvas->need_repick = TRUE; +} + +/* Set_property handler for canvas items */ +static void +ganv_item_set_property(GObject* gobject, guint param_id, + const GValue* value, GParamSpec* pspec) +{ + GanvItem* item; + + g_return_if_fail(GANV_IS_ITEM(gobject)); + + item = GANV_ITEM(gobject); + + switch (param_id) { + case ITEM_PROP_PARENT: + if (item->parent != NULL) { + g_warning("Cannot set `parent' argument after item has " + "already been constructed."); + } else if (g_value_get_object(value)) { + item->parent = GANV_ITEM(g_value_get_object(value)); + item->canvas = item->parent->canvas; + item_post_create_setup(item); + } + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, param_id, pspec); + break; + } +} + +/* Get_property handler for canvas items */ +static void +ganv_item_get_property(GObject* gobject, guint param_id, + GValue* value, GParamSpec* pspec) +{ + GanvItem* item; + + g_return_if_fail(GANV_IS_ITEM(gobject)); + + item = GANV_ITEM(gobject); + + switch (param_id) { + case ITEM_PROP_PARENT: + g_value_set_object(value, item->parent); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, param_id, pspec); + break; + } +} + +/** + * ganv_item_construct: + * @item: An unconstructed canvas item. + * @parent: The parent group for the item. + * @first_arg_name: The name of the first argument for configuring the item. + * @args: The list of arguments used to configure the item. + * + * Constructs a canvas item; meant for use only by item implementations. + **/ +void +ganv_item_construct(GanvItem* item, GanvGroup* parent, + const gchar* first_arg_name, va_list args) +{ + g_return_if_fail(GANV_IS_GROUP(parent)); + g_return_if_fail(GANV_IS_ITEM(item)); + + item->parent = GANV_ITEM(parent); + item->canvas = item->parent->canvas; + + g_object_set_valist(G_OBJECT(item), first_arg_name, args); + + item_post_create_setup(item); +} + +/* If the item is visible, requests a redraw of it. */ +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); + } +} + +/* Standard object dispose function for canvas items */ +static void +ganv_item_dispose(GObject* object) +{ + GanvItem* item; + + g_return_if_fail(GANV_IS_ITEM(object)); + + item = GANV_ITEM(object); + + if (item->canvas) { + redraw_if_visible(item); + } + + /* Make the canvas forget about us */ + + if (item->canvas && ( item == item->canvas->current_item) ) { + item->canvas->current_item = NULL; + item->canvas->need_repick = TRUE; + } + + if (item->canvas && ( item == item->canvas->new_current_item) ) { + item->canvas->new_current_item = NULL; + item->canvas->need_repick = TRUE; + } + + if (item->canvas && ( item == item->canvas->grabbed_item) ) { + item->canvas->grabbed_item = NULL; + gdk_pointer_ungrab(GDK_CURRENT_TIME); + } + + if (item->canvas && ( item == item->canvas->focused_item) ) { + item->canvas->focused_item = NULL; + } + + /* Normal destroy stuff */ + + if (item->object.flags & GANV_ITEM_MAPPED) { + (*GANV_ITEM_GET_CLASS(item)->unmap)(item); + } + + if (item->object.flags & GANV_ITEM_REALIZED) { + (*GANV_ITEM_GET_CLASS(item)->unrealize)(item); + } + + if (item->parent) { + group_remove(GANV_GROUP(item->parent), item); + } + + g_free(item->xform); + item->xform = NULL; + + G_OBJECT_CLASS(item_parent_class)->dispose(object); + /* items should remove any reference to item->canvas after the + first ::destroy */ + item->canvas = NULL; +} + +/* Realize handler for canvas items */ +static void +ganv_item_realize(GanvItem* item) +{ + GTK_OBJECT_SET_FLAGS(item, GANV_ITEM_REALIZED); + + ganv_item_request_update(item); +} + +/* Unrealize handler for canvas items */ +static void +ganv_item_unrealize(GanvItem* item) +{ + GTK_OBJECT_UNSET_FLAGS(item, GANV_ITEM_REALIZED); +} + +/* Map handler for canvas items */ +static void +ganv_item_map(GanvItem* item) +{ + GTK_OBJECT_SET_FLAGS(item, GANV_ITEM_MAPPED); +} + +/* Unmap handler for canvas items */ +static void +ganv_item_unmap(GanvItem* item) +{ + GTK_OBJECT_UNSET_FLAGS(item, GANV_ITEM_MAPPED); +} + +/* Update handler for canvas items */ +static void +ganv_item_update(GanvItem* item, double* affine, ArtSVP* clip_path, int flags) +{ + GTK_OBJECT_UNSET_FLAGS(item, GANV_ITEM_NEED_UPDATE); + GTK_OBJECT_UNSET_FLAGS(item, GANV_ITEM_NEED_AFFINE); + GTK_OBJECT_UNSET_FLAGS(item, GANV_ITEM_NEED_CLIP); + GTK_OBJECT_UNSET_FLAGS(item, GANV_ITEM_NEED_VIS); +} + +#define noHACKISH_AFFINE + +/* + * This routine invokes the update method of the item + * Please notice, that we take parent to canvas pixel matrix as argument + * unlike virtual method ::update, whose argument is item 2 canvas pixel + * matrix + * + * I will try to force somewhat meaningful naming for affines (Lauris) + * General naming rule is FROM2TO, where FROM and TO are abbreviations + * So p2cpx is Parent2CanvasPixel and i2cpx is Item2CanvasPixel + * I hope that this helps to keep track of what really happens + * + */ + +static void +ganv_item_invoke_update(GanvItem* item, double* p2cpx, ArtSVP* clip_path, int flags) +{ + int child_flags; + gdouble i2cpx[6]; + +#ifdef HACKISH_AFFINE + double i2w[6], w2c[6], i2c[6]; +#endif + + child_flags = flags; + if (!(item->object.flags & GANV_ITEM_VISIBLE)) { + child_flags &= ~GANV_CANVAS_BASE_UPDATE_IS_VISIBLE; + } + + /* Calculate actual item transformation matrix */ + + if (item->xform) { + if (item->object.flags & GANV_ITEM_AFFINE_FULL) { + /* Item has full affine */ + art_affine_multiply(i2cpx, item->xform, p2cpx); + } else { + /* Item has only translation */ + memcpy(i2cpx, p2cpx, 4 * sizeof(gdouble)); + i2cpx[4] = item->xform[0] * p2cpx[0] + item->xform[1] * p2cpx[2] + p2cpx[4]; + i2cpx[5] = item->xform[0] * p2cpx[1] + item->xform[1] * p2cpx[3] + p2cpx[5]; + } + } else { + /* Item has no matrix (i.e. identity) */ + memcpy(i2cpx, p2cpx, 6 * sizeof(gdouble)); + } + +#ifdef HACKISH_AFFINE + ganv_item_i2w_affine(item, i2w); + ganv_canvas_base_w2c_affine(item->canvas, w2c); + art_affine_multiply(i2c, i2w, w2c); + /* invariant (doesn't hold now): child_affine == i2c */ + child_affine = i2c; +#endif + + /* apply object flags to child flags */ + + child_flags &= ~GANV_CANVAS_BASE_UPDATE_REQUESTED; + + if (item->object.flags & GANV_ITEM_NEED_UPDATE) { + child_flags |= GANV_CANVAS_BASE_UPDATE_REQUESTED; + } + + if (item->object.flags & GANV_ITEM_NEED_AFFINE) { + child_flags |= GANV_CANVAS_BASE_UPDATE_AFFINE; + } + + if (item->object.flags & GANV_ITEM_NEED_CLIP) { + child_flags |= GANV_CANVAS_BASE_UPDATE_CLIP; + } + + if (item->object.flags & GANV_ITEM_NEED_VIS) { + child_flags |= GANV_CANVAS_BASE_UPDATE_VISIBILITY; + } + + if (child_flags & GCI_UPDATE_MASK) { + if (GANV_ITEM_GET_CLASS(item)->update) { + GANV_ITEM_GET_CLASS(item)->update(item, i2cpx, clip_path, child_flags); + } + } +} + +/* + * This routine invokes the point method of the item. + * The arguments x, y should be in the parent item local coordinates. + * + * This is potentially evil, as we are relying on matrix inversion (Lauris) + */ + +static double +ganv_item_invoke_point(GanvItem* item, double x, double y, int cx, int cy, + GanvItem** actual_item) +{ + /* Calculate x & y in item local coordinates */ + + if (item->xform) { + if (item->object.flags & GANV_ITEM_AFFINE_FULL) { + gdouble p2i[6], t; + /* Item has full affine */ + art_affine_invert(p2i, item->xform); + t = x * p2i[0] + y * p2i[2] + p2i[4]; + y = x * p2i[1] + y * p2i[3] + p2i[5]; + x = t; + } else { + /* Item has only translation */ + x -= item->xform[0]; + y -= item->xform[1]; + } + } + +#ifdef HACKISH_AFFINE + double i2w[6], w2c[6], i2c[6], c2i[6]; + ArtPoint c, i; +#endif + +#ifdef HACKISH_AFFINE + ganv_item_i2w_affine(item, i2w); + ganv_canvas_base_w2c_affine(item->canvas, w2c); + art_affine_multiply(i2c, i2w, w2c); + art_affine_invert(c2i, i2c); + c.x = cx; + c.y = cy; + art_affine_point(&i, &c, c2i); + x = i.x; + y = i.y; +#endif + + if (GANV_ITEM_GET_CLASS(item)->point) { + return GANV_ITEM_GET_CLASS(item)->point(item, x, y, cx, cy, actual_item); + } + + return 1e18; +} + +/** + * ganv_item_set: + * @item: A canvas item. + * @first_arg_name: The list of object argument name/value pairs used to configure the item. + * @Varargs: + * + * Configures a canvas item. The arguments in the item are set to the specified + * values, and the item is repainted as appropriate. + **/ +void +ganv_item_set(GanvItem* item, const gchar* first_arg_name, ...) +{ + va_list args; + + va_start(args, first_arg_name); + ganv_item_set_valist(item, first_arg_name, args); + va_end(args); +} + +/** + * ganv_item_set_valist: + * @item: A canvas item. + * @first_arg_name: The name of the first argument used to configure the item. + * @args: The list of object argument name/value pairs used to configure the item. + * + * Configures a canvas item. The arguments in the item are set to the specified + * values, and the item is repainted as appropriate. + **/ +void +ganv_item_set_valist(GanvItem* item, const gchar* first_arg_name, va_list args) +{ + g_return_if_fail(GANV_IS_ITEM(item)); + + g_object_set_valist(G_OBJECT(item), first_arg_name, args); + + item->canvas->need_repick = TRUE; +} + +/** + * ganv_item_affine_relative: + * @item: A canvas item. + * @affine: An affine transformation matrix. + * + * Combines the specified affine transformation matrix with the item's current + * transformation. NULL affine is not allowed. + **/ +#define GCIAR_EPSILON 1e-6 +void +ganv_item_affine_relative(GanvItem* item, const double affine[6]) +{ + gdouble i2p[6]; + + g_return_if_fail(item != NULL); + g_return_if_fail(GANV_IS_ITEM(item)); + g_return_if_fail(affine != NULL); + + /* Calculate actual item transformation matrix */ + + if (item->xform) { + if (item->object.flags & GANV_ITEM_AFFINE_FULL) { + /* Item has full affine */ + art_affine_multiply(i2p, affine, item->xform); + } else { + /* Item has only translation */ + memcpy(i2p, affine, 6 * sizeof(gdouble)); + i2p[4] += item->xform[0]; + i2p[5] += item->xform[1]; + } + } else { + /* Item has no matrix (i.e. identity) */ + memcpy(i2p, affine, 6 * sizeof(gdouble)); + } + + ganv_item_affine_absolute(item, i2p); +} + +/** + * ganv_item_affine_absolute: + * @item: A canvas item. + * @affine: An affine transformation matrix. + * + * Makes the item's affine transformation matrix be equal to the specified + * matrix. NULL affine is treated as identity. + **/ +void +ganv_item_affine_absolute(GanvItem* item, const double i2p[6]) +{ + g_return_if_fail(item != NULL); + g_return_if_fail(GANV_IS_ITEM(item)); + + if (i2p + && (fabs(i2p[0] - 1.0) < GCI_EPSILON) + && (fabs(i2p[1] - 0.0) < GCI_EPSILON) + && (fabs(i2p[2] - 0.0) < GCI_EPSILON) + && (fabs(i2p[3] - 1.0) < GCI_EPSILON) + && (fabs(i2p[4] - 0.0) < GCI_EPSILON) + && (fabs(i2p[5] - 0.0) < GCI_EPSILON)) { + /* We are identity */ + i2p = NULL; + } + + if (i2p) { + if (item->xform && !(item->object.flags & GANV_ITEM_AFFINE_FULL)) { + /* We do not want to deal with translation-only affines */ + g_free(item->xform); + item->xform = NULL; + } + if (!item->xform) { + item->xform = g_new(gdouble, 6); + } + memcpy(item->xform, i2p, 6 * sizeof(gdouble)); + item->object.flags |= GANV_ITEM_AFFINE_FULL; + } else { + if (item->xform) { + g_free(item->xform); + item->xform = NULL; + } + } + + if (!(item->object.flags & GANV_ITEM_NEED_AFFINE)) { + /* Request update */ + item->object.flags |= GANV_ITEM_NEED_AFFINE; + ganv_item_request_update(item); + } + + item->canvas->need_repick = TRUE; +} + +/** + * ganv_item_move: + * @item: A canvas item. + * @dx: Horizontal offset. + * @dy: Vertical offset. + * + * Moves a canvas item by creating an affine transformation matrix for + * translation by using the specified values. This happens in item + * local coordinate system, so if you have nontrivial transform, it + * most probably does not do, what you want. + **/ +void +ganv_item_move(GanvItem* item, double dx, double dy) +{ + double translate[6]; + + g_return_if_fail(item != NULL); + g_return_if_fail(GANV_IS_ITEM(item)); + + art_affine_translate(translate, dx, dy); + + ganv_item_affine_relative(item, translate); +} + +/* Convenience function to reorder items in a group's child list. This puts the + * specified link after the "before" link. Returns TRUE if the list was changed. + */ +static gboolean +put_item_after(GList* link, GList* before) +{ + GanvGroup* parent; + GList* old_before, * old_after; + GList* after; + + parent = GANV_GROUP(GANV_ITEM(link->data)->parent); + + if (before) { + after = before->next; + } else { + after = parent->item_list; + } + + if (( before == link) || ( after == link) ) { + return FALSE; + } + + /* Unlink */ + + old_before = link->prev; + old_after = link->next; + + if (old_before) { + old_before->next = old_after; + } else { + parent->item_list = old_after; + } + + if (old_after) { + old_after->prev = old_before; + } else { + parent->item_list_end = old_before; + } + + /* Relink */ + + link->prev = before; + if (before) { + before->next = link; + } else { + parent->item_list = link; + } + + link->next = after; + if (after) { + after->prev = link; + } else { + parent->item_list_end = link; + } + + return TRUE; +} + +/** + * ganv_item_raise: + * @item: A canvas item. + * @positions: Number of steps to raise the item. + * + * Raises the item in its parent's stack by the specified number of positions. + * If the number of positions is greater than the distance to the top of the + * stack, then the item is put at the top. + **/ +void +ganv_item_raise(GanvItem* item, int positions) +{ + GList* link, * before; + GanvGroup* parent; + + g_return_if_fail(GANV_IS_ITEM(item)); + g_return_if_fail(positions >= 0); + + if (!item->parent || ( positions == 0) ) { + return; + } + + parent = GANV_GROUP(item->parent); + link = g_list_find(parent->item_list, item); + g_assert(link != NULL); + + for (before = link; positions && before; positions--) { + before = before->next; + } + + if (!before) { + before = parent->item_list_end; + } + + if (put_item_after(link, before)) { + redraw_if_visible(item); + item->canvas->need_repick = TRUE; + } +} + +/** + * ganv_item_lower: + * @item: A canvas item. + * @positions: Number of steps to lower the item. + * + * Lowers the item in its parent's stack by the specified number of positions. + * If the number of positions is greater than the distance to the bottom of the + * stack, then the item is put at the bottom. + **/ +void +ganv_item_lower(GanvItem* item, int positions) +{ + GList* link, * before; + GanvGroup* parent; + + g_return_if_fail(GANV_IS_ITEM(item)); + g_return_if_fail(positions >= 1); + + if (!item->parent || ( positions == 0) ) { + return; + } + + parent = GANV_GROUP(item->parent); + link = g_list_find(parent->item_list, item); + g_assert(link != NULL); + + if (link->prev) { + for (before = link->prev; positions && before; positions--) { + before = before->prev; + } + } else { + before = NULL; + } + + if (put_item_after(link, before)) { + redraw_if_visible(item); + item->canvas->need_repick = TRUE; + } +} + +/** + * ganv_item_raise_to_top: + * @item: A canvas item. + * + * Raises an item to the top of its parent's stack. + **/ +void +ganv_item_raise_to_top(GanvItem* item) +{ + GList* link; + GanvGroup* parent; + + g_return_if_fail(GANV_IS_ITEM(item)); + + if (!item->parent) { + return; + } + + parent = GANV_GROUP(item->parent); + link = g_list_find(parent->item_list, item); + g_assert(link != NULL); + + if (put_item_after(link, parent->item_list_end)) { + redraw_if_visible(item); + item->canvas->need_repick = TRUE; + } +} + +/** + * ganv_item_lower_to_bottom: + * @item: A canvas item. + * + * Lowers an item to the bottom of its parent's stack. + **/ +void +ganv_item_lower_to_bottom(GanvItem* item) +{ + GList* link; + GanvGroup* parent; + + g_return_if_fail(GANV_IS_ITEM(item)); + + if (!item->parent) { + return; + } + + parent = GANV_GROUP(item->parent); + link = g_list_find(parent->item_list, item); + g_assert(link != NULL); + + if (put_item_after(link, NULL)) { + redraw_if_visible(item); + item->canvas->need_repick = TRUE; + } +} + +/** + * ganv_item_show: + * @item: A canvas item. + * + * Shows a canvas item. If the item was already shown, then no action is taken. + **/ +void +ganv_item_show(GanvItem* item) +{ + g_return_if_fail(GANV_IS_ITEM(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); + item->canvas->need_repick = TRUE; + } +} + +/** + * ganv_item_hide: + * @item: A canvas item. + * + * Hides a canvas item. If the item was already hidden, then no action is + * taken. + **/ +void +ganv_item_hide(GanvItem* item) +{ + g_return_if_fail(GANV_IS_ITEM(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); + item->canvas->need_repick = TRUE; + } +} + +/** + * ganv_item_grab: + * @item: A canvas item. + * @event_mask: Mask of events that will be sent to this item. + * @cursor: If non-NULL, the cursor that will be used while the grab is active. + * @etime: The timestamp required for grabbing the mouse, or GDK_CURRENT_TIME. + * + * Specifies that all events that match the specified event mask should be sent + * to the specified item, and also grabs the mouse by calling + * gdk_pointer_grab(). The event mask is also used when grabbing the pointer. + * If @cursor is not NULL, then that cursor is used while the grab is active. + * The @etime parameter is the timestamp required for grabbing the mouse. + * + * Return value: If an item was already grabbed, it returns %GDK_GRAB_ALREADY_GRABBED. If + * the specified item was hidden by calling ganv_item_hide(), then it + * returns %GDK_GRAB_NOT_VIEWABLE. Else, it returns the result of calling + * gdk_pointer_grab(). + **/ +int +ganv_item_grab(GanvItem* item, guint event_mask, GdkCursor* cursor, guint32 etime) +{ + int retval; + + g_return_val_if_fail(GANV_IS_ITEM(item), GDK_GRAB_NOT_VIEWABLE); + g_return_val_if_fail(GTK_WIDGET_MAPPED(item->canvas), GDK_GRAB_NOT_VIEWABLE); + + if (item->canvas->grabbed_item) { + return GDK_GRAB_ALREADY_GRABBED; + } + + if (!(item->object.flags & GANV_ITEM_VISIBLE)) { + return GDK_GRAB_NOT_VIEWABLE; + } + + retval = gdk_pointer_grab(item->canvas->layout.bin_window, + FALSE, + event_mask, + NULL, + cursor, + etime); + + if (retval != GDK_GRAB_SUCCESS) { + return retval; + } + + item->canvas->grabbed_item = item; + item->canvas->grabbed_event_mask = event_mask; + item->canvas->current_item = item; /* So that events go to the grabbed item */ + + return retval; +} + +/** + * ganv_item_ungrab: + * @item: A canvas item that holds a grab. + * @etime: The timestamp for ungrabbing the mouse. + * + * Ungrabs the item, which must have been grabbed in the canvas, and ungrabs the + * mouse. + **/ +void +ganv_item_ungrab(GanvItem* item, guint32 etime) +{ + g_return_if_fail(GANV_IS_ITEM(item)); + + if (item->canvas->grabbed_item != item) { + return; + } + + item->canvas->grabbed_item = NULL; + + gdk_pointer_ungrab(etime); +} + +/** + * ganv_item_i2w_affine: + * @item: A canvas item + * @affine: An affine transformation matrix (return value). + * + * Gets the affine transform that converts from the item's coordinate system to + * world coordinates. + **/ +void +ganv_item_i2w_affine(GanvItem* item, double affine[6]) +{ + g_return_if_fail(GANV_IS_ITEM(item)); + g_return_if_fail(affine != NULL); + + art_affine_identity(affine); + + while (item) { + if (item->xform != NULL) { + if (item->object.flags & GANV_ITEM_AFFINE_FULL) { + art_affine_multiply(affine, affine, item->xform); + } else { + affine[4] += item->xform[0]; + affine[5] += item->xform[1]; + } + } + + item = item->parent; + } +} + +/** + * ganv_item_w2i: + * @item: A canvas item. + * @x: X coordinate to convert (input/output value). + * @y: Y coordinate to convert (input/output value). + * + * Converts a coordinate pair from world coordinates to item-relative + * coordinates. + **/ +void +ganv_item_w2i(GanvItem* item, double* x, double* y) +{ + double affine[6], inv[6]; + ArtPoint w, i; + + g_return_if_fail(GANV_IS_ITEM(item)); + g_return_if_fail(x != NULL); + g_return_if_fail(y != NULL); + + ganv_item_i2w_affine(item, affine); + art_affine_invert(inv, affine); + w.x = *x; + w.y = *y; + art_affine_point(&i, &w, inv); + *x = i.x; + *y = i.y; +} + +/** + * ganv_item_i2w: + * @item: A canvas item. + * @x: X coordinate to convert (input/output value). + * @y: Y coordinate to convert (input/output value). + * + * Converts a coordinate pair from item-relative coordinates to world + * coordinates. + **/ +void +ganv_item_i2w(GanvItem* item, double* x, double* y) +{ + double affine[6]; + ArtPoint w, i; + + g_return_if_fail(GANV_IS_ITEM(item)); + g_return_if_fail(x != NULL); + g_return_if_fail(y != NULL); + + ganv_item_i2w_affine(item, affine); + i.x = *x; + i.y = *y; + art_affine_point(&w, &i, affine); + *x = w.x; + *y = w.y; +} + +/** + * ganv_item_i2c_affine: + * @item: A canvas item. + * @affine: An affine transformation matrix (return value). + * + * Gets the affine transform that converts from item-relative coordinates to + * canvas pixel coordinates. + **/ +void +ganv_item_i2c_affine(GanvItem* item, double affine[6]) +{ + double i2w[6], w2c[6]; + + ganv_item_i2w_affine(item, i2w); + ganv_canvas_base_w2c_affine(item->canvas, w2c); + art_affine_multiply(affine, i2w, w2c); +} + +/* Returns whether the item is an inferior of or is equal to the parent. */ +static int +is_descendant(GanvItem* item, GanvItem* parent) +{ + for (; item; item = item->parent) { + if (item == parent) { + return TRUE; + } + } + + return FALSE; +} + +/** + * ganv_item_reparent: + * @item: A canvas item. + * @new_group: A canvas group. + * + * Changes the parent of the specified item to be the new group. The item keeps + * its group-relative coordinates as for its old parent, so the item may change + * its absolute position within the canvas. + **/ +void +ganv_item_reparent(GanvItem* item, GanvGroup* new_group) +{ + g_return_if_fail(GANV_IS_ITEM(item)); + g_return_if_fail(GANV_IS_GROUP(new_group)); + + /* Both items need to be in the same canvas */ + g_return_if_fail(item->canvas == GANV_ITEM(new_group)->canvas); + + /* The group cannot be an inferior of the item or be the item itself -- + * this also takes care of the case where the item is the root item of + * the canvas. */ + g_return_if_fail(!is_descendant(GANV_ITEM(new_group), item)); + + /* Everything is ok, now actually reparent the item */ + + g_object_ref(G_OBJECT(item)); /* protect it from the unref in group_remove */ + + redraw_if_visible(item); + + group_remove(GANV_GROUP(item->parent), item); + item->parent = GANV_ITEM(new_group); + group_add(new_group, item); + + /* Redraw and repick */ + + redraw_if_visible(item); + item->canvas->need_repick = TRUE; + + g_object_unref(G_OBJECT(item)); +} + +/** + * ganv_item_grab_focus: + * @item: A canvas item. + * + * Makes the specified item take the keyboard focus, so all keyboard events will + * be sent to it. If the canvas widget itself did not have the focus, it grabs + * it as well. + **/ +void +ganv_item_grab_focus(GanvItem* item) +{ + GanvItem* focused_item; + GdkEvent ev; + + g_return_if_fail(GANV_IS_ITEM(item)); + g_return_if_fail(GTK_WIDGET_CAN_FOCUS(GTK_WIDGET(item->canvas))); + + focused_item = item->canvas->focused_item; + + if (focused_item) { + ev.focus_change.type = GDK_FOCUS_CHANGE; + ev.focus_change.window = GTK_LAYOUT(item->canvas)->bin_window; + ev.focus_change.send_event = FALSE; + ev.focus_change.in = FALSE; + + emit_event(item->canvas, &ev); + } + + item->canvas->focused_item = item; + gtk_widget_grab_focus(GTK_WIDGET(item->canvas)); + + if (focused_item) { + ev.focus_change.type = GDK_FOCUS_CHANGE; + ev.focus_change.window = GTK_LAYOUT(item->canvas)->bin_window; + ev.focus_change.send_event = FALSE; + ev.focus_change.in = TRUE; + + emit_event(item->canvas, &ev); + } +} + +/** + * ganv_item_get_bounds: + * @item: A canvas item. + * @x1: Leftmost edge of the bounding box (return value). + * @y1: Upper edge of the bounding box (return value). + * @x2: Rightmost edge of the bounding box (return value). + * @y2: Lower edge of the bounding box (return value). + * + * Queries the bounding box of a canvas item. The bounds are returned in the + * coordinate system of the item's parent. + **/ +void +ganv_item_get_bounds(GanvItem* item, double* x1, double* y1, double* x2, double* y2) +{ + double tx1, ty1, tx2, ty2; + ArtPoint p1, p2, p3, p4; + ArtPoint q1, q2, q3, q4; + double min_x1, min_y1, min_x2, min_y2; + double max_x1, max_y1, max_x2, max_y2; + + g_return_if_fail(GANV_IS_ITEM(item)); + + tx1 = ty1 = tx2 = ty2 = 0.0; + + /* Get the item's bounds in its coordinate system */ + + if (GANV_ITEM_GET_CLASS(item)->bounds) { + (*GANV_ITEM_GET_CLASS(item)->bounds)(item, &tx1, &ty1, &tx2, &ty2); + } + + /* Make the bounds relative to the item's parent coordinate system */ + + if (item->xform && (item->object.flags & GANV_ITEM_AFFINE_FULL)) { + p1.x = p2.x = tx1; + p1.y = p4.y = ty1; + p3.x = p4.x = tx2; + p2.y = p3.y = ty2; + + art_affine_point(&q1, &p1, item->xform); + art_affine_point(&q2, &p2, item->xform); + art_affine_point(&q3, &p3, item->xform); + art_affine_point(&q4, &p4, item->xform); + + if (q1.x < q2.x) { + min_x1 = q1.x; + max_x1 = q2.x; + } else { + min_x1 = q2.x; + max_x1 = q1.x; + } + + if (q1.y < q2.y) { + min_y1 = q1.y; + max_y1 = q2.y; + } else { + min_y1 = q2.y; + max_y1 = q1.y; + } + + if (q3.x < q4.x) { + min_x2 = q3.x; + max_x2 = q4.x; + } else { + min_x2 = q4.x; + max_x2 = q3.x; + } + + if (q3.y < q4.y) { + min_y2 = q3.y; + max_y2 = q4.y; + } else { + min_y2 = q4.y; + max_y2 = q3.y; + } + + tx1 = MIN(min_x1, min_x2); + ty1 = MIN(min_y1, min_y2); + tx2 = MAX(max_x1, max_x2); + ty2 = MAX(max_y1, max_y2); + } else if (item->xform) { + tx1 += item->xform[0]; + ty1 += item->xform[1]; + tx2 += item->xform[0]; + ty2 += item->xform[1]; + } + + /* Return the values */ + + if (x1) { + *x1 = tx1; + } + + if (y1) { + *y1 = ty1; + } + + if (x2) { + *x2 = tx2; + } + + if (y2) { + *y2 = ty2; + } +} + +/** + * ganv_item_request_update + * @item: A canvas item. + * + * To be used only by item implementations. Requests that the canvas queue an + * update for the specified item. + **/ +void +ganv_item_request_update(GanvItem* item) +{ + if (item->object.flags & GANV_ITEM_NEED_UPDATE) { + return; + } + + item->object.flags |= GANV_ITEM_NEED_UPDATE; + + if (item->parent != NULL) { + /* Recurse up the tree */ + 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); + } +} + +/*** GanvGroup ***/ + + +enum { + GROUP_PROP_0, + GROUP_PROP_X, + GROUP_PROP_Y +}; + + +static void ganv_group_class_init(GanvGroupClass* class); +static void ganv_group_init(GanvGroup* group); +static void ganv_group_set_property(GObject* object, + guint param_id, + const GValue* value, + GParamSpec* pspec); +static void ganv_group_get_property(GObject* object, + guint param_id, + GValue* value, + GParamSpec* pspec); + +static void ganv_group_destroy(GtkObject* object); + +static void ganv_group_update(GanvItem* item, double* affine, + ArtSVP* clip_path, int flags); +static void ganv_group_realize(GanvItem* item); +static void ganv_group_unrealize(GanvItem* item); +static void ganv_group_map(GanvItem* item); +static void ganv_group_unmap(GanvItem* item); +static void ganv_group_draw(GanvItem* item, GdkDrawable* drawable, + int x, int y, int width, int height); +static double ganv_group_point(GanvItem* item, double x, double y, + int cx, int cy, + GanvItem** actual_item); +static void ganv_group_bounds(GanvItem* item, double* x1, double* y1, + double* x2, double* y2); + + +static GanvItemClass* group_parent_class; + +G_DEFINE_TYPE(GanvGroup, ganv_group, GANV_TYPE_ITEM) + +/* Class initialization function for GanvGroupClass */ +static void +ganv_group_class_init(GanvGroupClass* class) +{ + GObjectClass* gobject_class; + GtkObjectClass* object_class; + GanvItemClass* item_class; + + gobject_class = (GObjectClass*)class; + object_class = (GtkObjectClass*)class; + item_class = (GanvItemClass*)class; + + group_parent_class = g_type_class_peek_parent(class); + + gobject_class->set_property = ganv_group_set_property; + gobject_class->get_property = ganv_group_get_property; + + g_object_class_install_property + (gobject_class, GROUP_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, GROUP_PROP_Y, + g_param_spec_double("y", + _("Y"), + _("Y"), + -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, + (G_PARAM_READABLE | G_PARAM_WRITABLE))); + + object_class->destroy = ganv_group_destroy; + + item_class->update = ganv_group_update; + item_class->realize = ganv_group_realize; + item_class->unrealize = ganv_group_unrealize; + item_class->map = ganv_group_map; + item_class->unmap = ganv_group_unmap; + item_class->draw = ganv_group_draw; + item_class->point = ganv_group_point; + item_class->bounds = ganv_group_bounds; +} + +/* Object initialization function for GanvGroup */ +static void +ganv_group_init(GanvGroup* group) +{ +#if 0 + group->xpos = 0.0; + group->ypos = 0.0; +#endif +} + +/* Translate handler for canvas groups */ +static double* +ganv_canvas_base_ensure_translate(GanvItem* item) +{ + if (item->xform == NULL) { + GTK_OBJECT_UNSET_FLAGS(item, GANV_ITEM_AFFINE_FULL); + item->xform = g_new(double, 2); + item->xform[0] = 0.0; + item->xform[1] = 0.0; + return item->xform; + } else if (item->object.flags & GANV_ITEM_AFFINE_FULL) { + return item->xform + 4; + } else { + return item->xform; + } +} + +/* Set_property handler for canvas groups */ +static void +ganv_group_set_property(GObject* gobject, guint param_id, + const GValue* value, GParamSpec* pspec) +{ + GanvItem* item; + double* xlat; + + g_return_if_fail(GANV_IS_GROUP(gobject)); + + item = GANV_ITEM(gobject); + + switch (param_id) { + case GROUP_PROP_X: + xlat = ganv_canvas_base_ensure_translate(item); + xlat[0] = g_value_get_double(value); + break; + + case GROUP_PROP_Y: + xlat = ganv_canvas_base_ensure_translate(item); + xlat[1] = g_value_get_double(value); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, param_id, pspec); + break; + } +} + +/* Get_property handler for canvas groups */ +static void +ganv_group_get_property(GObject* gobject, guint param_id, + GValue* value, GParamSpec* pspec) +{ + GanvItem* item; + + g_return_if_fail(GANV_IS_GROUP(gobject)); + + item = GANV_ITEM(gobject); + + switch (param_id) { + case GROUP_PROP_X: + if (item->xform == NULL) { + g_value_set_double(value, 0); + } else if (GTK_OBJECT(gobject)->flags & GANV_ITEM_AFFINE_FULL) { + g_value_set_double(value, item->xform[4]); + } else { + g_value_set_double(value, item->xform[0]); + } + break; + + case GROUP_PROP_Y: + if (item->xform == NULL) { + g_value_set_double(value, 0); + } else if (GTK_OBJECT(gobject)->flags & GANV_ITEM_AFFINE_FULL) { + g_value_set_double(value, item->xform[5]); + } else { + g_value_set_double(value, item->xform[1]); + } + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, param_id, pspec); + break; + } +} + +/* Destroy handler for canvas groups */ +static void +ganv_group_destroy(GtkObject* object) +{ + GanvGroup* group; + + g_return_if_fail(GANV_IS_GROUP(object)); + + group = GANV_GROUP(object); + + while (group->item_list) { + // child is unref'ed by the child's group_remove(). + gtk_object_destroy(GTK_OBJECT(group->item_list->data)); + } + if (GTK_OBJECT_CLASS(group_parent_class)->destroy) { + (*GTK_OBJECT_CLASS(group_parent_class)->destroy)(object); + } +} + +/* Update handler for canvas groups */ +static void +ganv_group_update(GanvItem* item, double* affine, ArtSVP* clip_path, int flags) +{ + GanvGroup* group; + GList* list; + GanvItem* i; + ArtDRect bbox, child_bbox; + + group = GANV_GROUP(item); + + (*group_parent_class->update)(item, affine, clip_path, flags); + + bbox.x0 = 0; + bbox.y0 = 0; + bbox.x1 = 0; + bbox.y1 = 0; + + for (list = group->item_list; list; list = list->next) { + i = list->data; + + ganv_item_invoke_update(i, affine, clip_path, flags); + + child_bbox.x0 = i->x1; + child_bbox.y0 = i->y1; + child_bbox.x1 = i->x2; + child_bbox.y1 = i->y2; + art_drect_union(&bbox, &bbox, &child_bbox); + } + item->x1 = bbox.x0; + item->y1 = bbox.y0; + item->x2 = bbox.x1; + item->y2 = bbox.y1; +} + +/* Realize handler for canvas groups */ +static void +ganv_group_realize(GanvItem* item) +{ + GanvGroup* group; + GList* list; + GanvItem* i; + + group = GANV_GROUP(item); + + for (list = group->item_list; list; list = list->next) { + i = list->data; + + if (!(i->object.flags & GANV_ITEM_REALIZED)) { + (*GANV_ITEM_GET_CLASS(i)->realize)(i); + } + } + + (*group_parent_class->realize)(item); +} + +/* Unrealize handler for canvas groups */ +static void +ganv_group_unrealize(GanvItem* item) +{ + GanvGroup* group; + GList* list; + GanvItem* i; + + group = GANV_GROUP(item); + + for (list = group->item_list; list; list = list->next) { + i = list->data; + + if (i->object.flags & GANV_ITEM_REALIZED) { + (*GANV_ITEM_GET_CLASS(i)->unrealize)(i); + } + } + + (*group_parent_class->unrealize)(item); +} + +/* Map handler for canvas groups */ +static void +ganv_group_map(GanvItem* item) +{ + GanvGroup* group; + GList* list; + GanvItem* i; + + group = GANV_GROUP(item); + + for (list = group->item_list; list; list = list->next) { + i = list->data; + + if (!(i->object.flags & GANV_ITEM_MAPPED)) { + (*GANV_ITEM_GET_CLASS(i)->map)(i); + } + } + + (*group_parent_class->map)(item); +} + +/* Unmap handler for canvas groups */ +static void +ganv_group_unmap(GanvItem* item) +{ + GanvGroup* group; + GList* list; + GanvItem* i; + + group = GANV_GROUP(item); + + for (list = group->item_list; list; list = list->next) { + i = list->data; + + if (i->object.flags & GANV_ITEM_MAPPED) { + (*GANV_ITEM_GET_CLASS(i)->unmap)(i); + } + } + + (*group_parent_class->unmap)(item); +} + +/* Draw handler for canvas groups */ +static void +ganv_group_draw(GanvItem* item, GdkDrawable* drawable, + int x, int y, int width, int height) +{ + GanvGroup* group; + GList* list; + GanvItem* child = NULL; + + group = GANV_GROUP(item); + + for (list = group->item_list; list; list = list->next) { + child = list->data; + + if (((child->object.flags & GANV_ITEM_VISIBLE) + && ((child->x1 < (x + width)) + && (child->y1 < (y + height)) + && (child->x2 > x) + && (child->y2 > y))) + || ((GTK_OBJECT_FLAGS(child) & GANV_ITEM_ALWAYS_REDRAW) + && (child->x1 < child->canvas->redraw_x2) + && (child->y1 < child->canvas->redraw_y2) + && (child->x2 > child->canvas->redraw_x1) + && (child->y2 > child->canvas->redraw_y2))) { + if (GANV_ITEM_GET_CLASS(child)->draw) { + (*GANV_ITEM_GET_CLASS(child)->draw)( + child, drawable, x, y, width, height); + } + } + } +} + +/* Point handler for canvas groups */ +static double +ganv_group_point(GanvItem* item, double x, double y, int cx, int cy, + GanvItem** actual_item) +{ + GanvGroup* group; + GList* list; + GanvItem* child, * point_item; + int x1, y1, x2, y2; + double gx, gy; + double dist, best; + int has_point; + + group = GANV_GROUP(item); + + x1 = cx - item->canvas->close_enough; + y1 = cy - item->canvas->close_enough; + x2 = cx + item->canvas->close_enough; + y2 = cy + item->canvas->close_enough; + + best = 0.0; + *actual_item = NULL; + + gx = x; + gy = y; + + dist = 0.0; /* keep gcc happy */ + + for (list = group->item_list; list; list = list->next) { + child = list->data; + + if ((child->x1 > x2) || (child->y1 > y2) || (child->x2 < x1) || (child->y2 < y1)) { + continue; + } + + point_item = NULL; /* cater for incomplete item implementations */ + + if ((child->object.flags & GANV_ITEM_VISIBLE) + && GANV_ITEM_GET_CLASS(child)->point) { + dist = ganv_item_invoke_point(child, gx, gy, cx, cy, &point_item); + has_point = TRUE; + } else { + has_point = FALSE; + } + + if (has_point + && point_item + && ((int)(dist * item->canvas->pixels_per_unit + 0.5) + <= item->canvas->close_enough)) { + best = dist; + *actual_item = point_item; + } + } + + return best; +} + +/* Bounds handler for canvas groups */ +static void +ganv_group_bounds(GanvItem* item, double* x1, double* y1, double* x2, double* y2) +{ + GanvGroup* group; + GanvItem* child; + GList* list; + double tx1, ty1, tx2, ty2; + double minx, miny, maxx, maxy; + int set; + + group = GANV_GROUP(item); + + /* Get the bounds of the first visible item */ + + child = NULL; /* Unnecessary but eliminates a warning. */ + + set = FALSE; + + for (list = group->item_list; list; list = list->next) { + child = list->data; + + if (child->object.flags & GANV_ITEM_VISIBLE) { + set = TRUE; + ganv_item_get_bounds(child, &minx, &miny, &maxx, &maxy); + break; + } + } + + /* If there were no visible items, return an empty bounding box */ + + if (!set) { + *x1 = *y1 = *x2 = *y2 = 0.0; + return; + } + + /* Now we can grow the bounds using the rest of the items */ + + list = list->next; + + for (; list; list = list->next) { + child = list->data; + + if (!(child->object.flags & GANV_ITEM_VISIBLE)) { + continue; + } + + ganv_item_get_bounds(child, &tx1, &ty1, &tx2, &ty2); + + if (tx1 < minx) { + minx = tx1; + } + + if (ty1 < miny) { + miny = ty1; + } + + if (tx2 > maxx) { + maxx = tx2; + } + + if (ty2 > maxy) { + maxy = ty2; + } + } + + *x1 = minx; + *y1 = miny; + *x2 = maxx; + *y2 = maxy; +} + +/* Adds an item to a group */ +static void +group_add(GanvGroup* group, GanvItem* item) +{ + g_object_ref_sink(G_OBJECT(item)); + + if (!group->item_list) { + group->item_list = g_list_append(group->item_list, item); + group->item_list_end = group->item_list; + } else { + group->item_list_end = g_list_append(group->item_list_end, item)->next; + } + + if (group->item.object.flags & GANV_ITEM_REALIZED) { + (*GANV_ITEM_GET_CLASS(item)->realize)(item); + } + + if (group->item.object.flags & GANV_ITEM_MAPPED) { + (*GANV_ITEM_GET_CLASS(item)->map)(item); + } + + g_object_notify(G_OBJECT(item), "parent"); +} + +/* Removes an item from a group */ +static void +group_remove(GanvGroup* group, GanvItem* item) +{ + GList* children; + + g_return_if_fail(GANV_IS_GROUP(group)); + g_return_if_fail(GANV_IS_ITEM(item)); + + for (children = group->item_list; children; children = children->next) { + if (children->data == item) { + if (item->object.flags & GANV_ITEM_MAPPED) { + (*GANV_ITEM_GET_CLASS(item)->unmap)(item); + } + + if (item->object.flags & GANV_ITEM_REALIZED) { + (*GANV_ITEM_GET_CLASS(item)->unrealize)(item); + } + + /* Unparent the child */ + + item->parent = NULL; + g_object_unref(G_OBJECT(item)); + + /* Remove it from the list */ + + if (children == group->item_list_end) { + group->item_list_end = children->prev; + } + + group->item_list = g_list_remove_link(group->item_list, children); + g_list_free(children); + break; + } + } +} + +/*** GanvCanvasBase ***/ + + +enum { + DRAW_BACKGROUND, + LAST_SIGNAL +}; + +static void ganv_canvas_base_class_init(GanvCanvasBaseClass* class); +static void ganv_canvas_base_init(GanvCanvasBase* canvas); +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 void ganv_canvas_base_draw_background(GanvCanvasBase* canvas, + GdkDrawable* drawable, + int x, + int y, + int width, + int height); + + +static GtkLayoutClass* canvas_parent_class; + +static guint canvas_signals[LAST_SIGNAL]; + +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->draw_background = ganv_canvas_base_draw_background; + 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))); + + canvas_signals[DRAW_BACKGROUND] + = g_signal_new("draw_background", + G_TYPE_FROM_CLASS(object_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET(GanvCanvasBaseClass, draw_background), + NULL, NULL, + ganv_marshal_VOID__OBJECT_INT_INT_INT_INT, + G_TYPE_NONE, 5, GDK_TYPE_DRAWABLE, + G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT); +} + +/* 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_area = 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); + + /* Disable the gtk+ double buffering since the canvas uses it's own. */ + gtk_widget_set_double_buffered(GTK_WIDGET(canvas), FALSE); + + /* 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; + art_uta_free(canvas->redraw_area); + canvas->redraw_area = 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)) { + /* This can only occur, if either canvas size or widget size changes */ + /* So I think we can request full redraw here */ + /* The reason is, that coverage UTA will be invalidated by offset change */ + /* fixme: Strictly this is not correct - we have to remove our own idle (Lauris) */ + /* More stuff - we have to mark root as needing fresh affine (Lauris) */ + if (!(canvas->root->object.flags & GANV_ITEM_NEED_AFFINE)) { + canvas->root->object.flags |= GANV_ITEM_NEED_AFFINE; + 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"); +} + +/* Emits an event for an item in the canvas, be it the current item, grabbed + * item, or focused item, as appropriate. + */ + +static int +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)); + + g_signal_emit(item, item_signals[ITEM_EVENT], 0, + 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_invoke_point(canvas->root, x, 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 = emit_event(canvas, &new_event); + canvas->in_repick = FALSE; + } + + /* new_current_item may have been set to NULL during the call to 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 = 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 = 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 = 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 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 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 (!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 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 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) +{ + GtkWidget* widget; + gint draw_x1, draw_y1; + gint draw_x2, draw_y2; + gint draw_width, draw_height; + + g_return_if_fail(!canvas->need_update); + + widget = GTK_WIDGET(canvas); + + 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; + + GdkPixmap* pixmap; + + pixmap = gdk_pixmap_new(canvas->layout.bin_window, + draw_width, draw_height, + gtk_widget_get_visual(widget)->depth); + + g_signal_emit(G_OBJECT(canvas), canvas_signals[DRAW_BACKGROUND], 0, pixmap, + draw_x1, draw_y1, draw_width, draw_height); + + if (canvas->root->object.flags & GANV_ITEM_VISIBLE) { + (*GANV_ITEM_GET_CLASS(canvas->root)->draw)( + canvas->root, pixmap, + draw_x1, draw_y1, + draw_width, draw_height); + } + + /* Copy the pixmap to the window and clean up */ + + gdk_draw_drawable(canvas->layout.bin_window, + canvas->pixmap_gc, + pixmap, + 0, 0, + draw_x1 + canvas->zoom_xofs, + draw_y1 + canvas->zoom_yofs, + draw_width, draw_height); + + g_object_unref(pixmap); +} + +/* Expose handler for the canvas */ +static gint +ganv_canvas_base_expose(GtkWidget* widget, GdkEventExpose* event) +{ + GanvCanvasBase* canvas; + GdkRectangle* rects; + gint n_rects; + int i; + + canvas = GANV_CANVAS_BASE(widget); + + if (!GTK_WIDGET_DRAWABLE(widget) || (event->window != canvas->layout.bin_window)) { + return FALSE; + } + +#ifdef VERBOSE + g_print("Expose\n"); +#endif + + gdk_region_get_rectangles(event->region, &rects, &n_rects); + + for (i = 0; i < n_rects; i++) { + ArtIRect rect; + + rect.x0 = rects[i].x - canvas->zoom_xofs; + rect.y0 = rects[i].y - canvas->zoom_yofs; + rect.x1 = rects[i].x + rects[i].width - canvas->zoom_xofs; + rect.y1 = rects[i].y + rects[i].height - canvas->zoom_yofs; + + if (canvas->need_update || canvas->need_redraw) { + ArtUta* uta; + + /* Update or drawing is scheduled, so just mark exposed area as dirty */ + uta = art_uta_from_irect(&rect); + ganv_canvas_base_request_redraw_uta(canvas, uta); + } else { + /* No pending updates, draw exposed area immediately */ + ganv_canvas_base_paint_rect(canvas, rect.x0, rect.y0, rect.x1, rect.y1); + + /* 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); + } + } + } + + g_free(rects); + + return FALSE; +} + +/* Repaints the areas in the canvas that need it */ +static void +paint(GanvCanvasBase* canvas) +{ + ArtIRect* rects; + gint n_rects, i; + ArtIRect visible_rect; + GdkRegion* region; + + /* Extract big rectangles from the microtile array */ + + rects = art_rect_list_from_uta(canvas->redraw_area, + REDRAW_QUANTUM_SIZE, REDRAW_QUANTUM_SIZE, + &n_rects); + + art_uta_free(canvas->redraw_area); + canvas->redraw_area = NULL; + canvas->need_redraw = FALSE; + + /* Turn those rectangles into a GdkRegion for exposing */ + + visible_rect.x0 = canvas->layout.hadjustment->value - canvas->zoom_xofs; + visible_rect.y0 = canvas->layout.vadjustment->value - canvas->zoom_yofs; + visible_rect.x1 = visible_rect.x0 + GTK_WIDGET(canvas)->allocation.width; + visible_rect.y1 = visible_rect.y0 + GTK_WIDGET(canvas)->allocation.height; + + region = gdk_region_new(); + + for (i = 0; i < n_rects; i++) { + ArtIRect clipped; + + art_irect_intersect(&clipped, &visible_rect, rects + i); + if (!art_irect_empty(&clipped)) { + GdkRectangle gdkrect; + + gdkrect.x = clipped.x0 + canvas->zoom_xofs; + gdkrect.y = clipped.y0 + canvas->zoom_yofs; + gdkrect.width = clipped.x1 - clipped.x0; + gdkrect.height = clipped.y1 - clipped.y0; + + region = gdk_region_rectangle(&gdkrect); + gdk_window_invalidate_region(canvas->layout.bin_window, region, FALSE); + gdk_region_destroy(region); + } + } + + art_free(rects); + + canvas->redraw_x1 = 0; + canvas->redraw_y1 = 0; + canvas->redraw_x2 = 0; + canvas->redraw_y2 = 0; +} + +static void +ganv_canvas_base_draw_background(GanvCanvasBase* canvas, GdkDrawable* drawable, + int x, int y, int width, int height) +{ + /* By default, we use the style background. */ + gdk_gc_set_foreground(canvas->pixmap_gc, + >K_WIDGET(canvas)->style->bg[GTK_STATE_NORMAL]); + gdk_draw_rectangle(drawable, + canvas->pixmap_gc, + TRUE, + 0, 0, + width, height); +} + +static void +do_update(GanvCanvasBase* canvas) +{ + /* Cause the update if necessary */ + +update_again: + if (canvas->need_update) { + gdouble w2cpx[6]; + + /* We start updating root with w2cpx affine */ + w2cpx[0] = canvas->pixels_per_unit; + w2cpx[1] = 0.0; + w2cpx[2] = 0.0; + w2cpx[3] = canvas->pixels_per_unit; + w2cpx[4] = -canvas->scroll_x1 * canvas->pixels_per_unit; + w2cpx[5] = -canvas->scroll_y1 * canvas->pixels_per_unit; + + ganv_item_invoke_update(canvas->root, w2cpx, NULL, 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: The root group of the specified canvas. + **/ +GanvGroup* +ganv_canvas_base_root(GanvCanvasBase* canvas) +{ + g_return_val_if_fail(GANV_IS_CANVAS_BASE(canvas), NULL); + + return GANV_GROUP(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 > GANV_CANVAS_BASE_EPSILON); + + 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); + + if (!(canvas->root->object.flags & GANV_ITEM_NEED_AFFINE)) { + canvas->root->object.flags |= GANV_ITEM_NEED_AFFINE; + 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_update_now: + * @canvas: A canvas. + * + * Forces an immediate update and redraw of a canvas. If the canvas does not + * have any pending update or redraw requests, then no action is taken. This is + * typically only used by applications that need explicit control of when the + * display is updated, like games. It is not needed by normal applications. + */ +void +ganv_canvas_base_update_now(GanvCanvasBase* canvas) +{ + g_return_if_fail(GANV_IS_CANVAS_BASE(canvas)); + + if (!(canvas->need_update || canvas->need_redraw)) { + g_assert(canvas->idle_id == 0); + g_assert(canvas->redraw_area == NULL); + return; + } + + remove_idle(canvas); + do_update(canvas); +} + +/** + * 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: 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_invoke_point(canvas->root, x, 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 */ +static 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); + } +} + +/* Computes the union of two microtile arrays while clipping the result to the + * specified rectangle. Any of the specified utas can be NULL, in which case it + * is taken to be an empty region. + */ +static ArtUta* +uta_union_clip(ArtUta* uta1, ArtUta* uta2, ArtIRect* clip) +{ + ArtUta* uta; + ArtUtaBbox* utiles; + int clip_x1, clip_y1, clip_x2, clip_y2; + int union_x1, union_y1, union_x2, union_y2; + int new_x1, new_y1, new_x2, new_y2; + int x, y; + int ofs, ofs1, ofs2; + + g_assert(clip != NULL); + + /* Compute the tile indices for the clipping rectangle */ + + clip_x1 = clip->x0 >> ART_UTILE_SHIFT; + clip_y1 = clip->y0 >> ART_UTILE_SHIFT; + clip_x2 = (clip->x1 >> ART_UTILE_SHIFT) + 1; + clip_y2 = (clip->y1 >> ART_UTILE_SHIFT) + 1; + + /* Get the union of the bounds of both utas */ + + if (!uta1) { + if (!uta2) { + return art_uta_new(clip_x1, clip_y1, clip_x1 + 1, clip_y1 + 1); + } + + union_x1 = uta2->x0; + union_y1 = uta2->y0; + union_x2 = uta2->x0 + uta2->width; + union_y2 = uta2->y0 + uta2->height; + } else { + if (!uta2) { + union_x1 = uta1->x0; + union_y1 = uta1->y0; + union_x2 = uta1->x0 + uta1->width; + union_y2 = uta1->y0 + uta1->height; + } else { + union_x1 = MIN(uta1->x0, uta2->x0); + union_y1 = MIN(uta1->y0, uta2->y0); + union_x2 = MAX(uta1->x0 + uta1->width, uta2->x0 + uta2->width); + union_y2 = MAX(uta1->y0 + uta1->height, uta2->y0 + uta2->height); + } + } + + /* Clip the union of the bounds */ + + new_x1 = MAX(clip_x1, union_x1); + new_y1 = MAX(clip_y1, union_y1); + new_x2 = MIN(clip_x2, union_x2); + new_y2 = MIN(clip_y2, union_y2); + + if (( new_x1 >= new_x2) || ( new_y1 >= new_y2) ) { + return art_uta_new(clip_x1, clip_y1, clip_x1 + 1, clip_y1 + 1); + } + + /* Make the new clipped union */ + + uta = art_new(ArtUta, 1); + uta->x0 = new_x1; + uta->y0 = new_y1; + uta->width = new_x2 - new_x1; + uta->height = new_y2 - new_y1; + uta->utiles = utiles = art_new(ArtUtaBbox, uta->width * uta->height); + + ofs = 0; + ofs1 = ofs2 = 0; + + for (y = new_y1; y < new_y2; y++) { + if (uta1) { + ofs1 = (y - uta1->y0) * uta1->width + new_x1 - uta1->x0; + } + + if (uta2) { + ofs2 = (y - uta2->y0) * uta2->width + new_x1 - uta2->x0; + } + + for (x = new_x1; x < new_x2; x++) { + ArtUtaBbox bb1, bb2, bb; + + if (!uta1 + || ( x < uta1->x0) || ( y < uta1->y0) + || ( x >= uta1->x0 + uta1->width) || ( y >= uta1->y0 + uta1->height) ) { + bb1 = 0; + } else { + bb1 = uta1->utiles[ofs1]; + } + + if (!uta2 + || ( x < uta2->x0) || ( y < uta2->y0) + || ( x >= uta2->x0 + uta2->width) || ( y >= uta2->y0 + uta2->height) ) { + bb2 = 0; + } else { + bb2 = uta2->utiles[ofs2]; + } + + if (bb1 == 0) { + bb = bb2; + } else if (bb2 == 0) { + bb = bb1; + } else { + bb = ART_UTA_BBOX_CONS(MIN(ART_UTA_BBOX_X0(bb1), + ART_UTA_BBOX_X0(bb2)), + MIN(ART_UTA_BBOX_Y0(bb1), + ART_UTA_BBOX_Y0(bb2)), + MAX(ART_UTA_BBOX_X1(bb1), + ART_UTA_BBOX_X1(bb2)), + MAX(ART_UTA_BBOX_Y1(bb1), + ART_UTA_BBOX_Y1(bb2))); + } + + utiles[ofs] = bb; + + ofs++; + ofs1++; + ofs2++; + } + } + + return uta; +} + +static inline void +get_visible_region(GanvCanvasBase* canvas, ArtIRect* visible) +{ + visible->x0 = canvas->layout.hadjustment->value - canvas->zoom_xofs; + visible->y0 = canvas->layout.vadjustment->value - canvas->zoom_yofs; + visible->x1 = visible->x0 + GTK_WIDGET(canvas)->allocation.width; + visible->y1 = visible->y0 + GTK_WIDGET(canvas)->allocation.height; +} + +/** + * ganv_canvas_base_request_redraw_uta: + * @canvas: A canvas. + * @uta: Microtile array that specifies the area to be redrawn. It will + * be freed by this function, so the argument you pass will be invalid + * after you call this function. + * + * Informs a canvas that the specified area, given as a microtile array, needs + * to be repainted. To be used only by item implementations. + **/ +void +ganv_canvas_base_request_redraw_uta(GanvCanvasBase* canvas, + ArtUta* uta) +{ + ArtIRect visible; + + g_return_if_fail(GANV_IS_CANVAS_BASE(canvas)); + g_return_if_fail(uta != NULL); + + if (!GTK_WIDGET_DRAWABLE(canvas)) { + art_uta_free(uta); + return; + } + + get_visible_region(canvas, &visible); + + if (canvas->need_redraw) { + ArtUta* new_uta; + + g_assert(canvas->redraw_area != NULL); + /* ALEX: This can fail if e.g. redraw_uta is called by an item + update function and we're called from update_now -> do_update + because update_now sets idle_id == 0. There is also some way + to get it from the expose handler (see bug #102811). + g_assert (canvas->idle_id != 0); */ + + new_uta = uta_union_clip(canvas->redraw_area, uta, &visible); + art_uta_free(canvas->redraw_area); + art_uta_free(uta); + canvas->redraw_area = new_uta; + if (canvas->idle_id == 0) { + add_idle(canvas); + } + } else { + ArtUta* new_uta; + + g_assert(canvas->redraw_area == NULL); + + new_uta = uta_union_clip(uta, NULL, &visible); + art_uta_free(uta); + canvas->redraw_area = new_uta; + + canvas->need_redraw = TRUE; + add_idle(canvas); + } +} + +/** + * 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. + * + * Convenience function that informs a canvas that the specified rectangle needs + * to be repainted. This function converts the rectangle to a microtile array + * and feeds it to ganv_canvas_base_request_redraw_uta(). The rectangle includes + * @x1 and @y1, but not @x2 and @y2. To be used only by item implementations. + **/ +void +ganv_canvas_base_request_redraw(GanvCanvasBase* canvas, int x1, int y1, int x2, int y2) +{ + ArtUta* uta; + ArtIRect bbox; + ArtIRect visible; + ArtIRect clip; + + g_return_if_fail(GANV_IS_CANVAS_BASE(canvas)); + + if (!GTK_WIDGET_DRAWABLE(canvas) || (x1 >= x2) || (y1 >= y2)) { + return; + } + + bbox.x0 = x1; + bbox.y0 = y1; + bbox.x1 = x2; + bbox.y1 = y2; + + get_visible_region(canvas, &visible); + + art_irect_intersect(&clip, &bbox, &visible); + + if (!art_irect_empty(&clip)) { + uta = art_uta_from_irect(&clip); + ganv_canvas_base_request_redraw_uta(canvas, uta); + } +} + +/** + * ganv_canvas_base_w2c_affine: + * @canvas: A canvas. + * @affine: 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, double affine[6]) +{ + double zooom; + + g_return_if_fail(GANV_IS_CANVAS_BASE(canvas)); + g_return_if_fail(affine != NULL); + + zooom = canvas->pixels_per_unit; + + affine[0] = zooom; + affine[1] = 0; + affine[2] = 0; + affine[3] = zooom; + affine[4] = -canvas->scroll_x1 * zooom; + affine[5] = -canvas->scroll_y1 * zooom; +} + +/** + * 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) +{ + double affine[6]; + ArtPoint w, c; + + g_return_if_fail(GANV_IS_CANVAS_BASE(canvas)); + + ganv_canvas_base_w2c_affine(canvas, affine); + w.x = wx; + w.y = wy; + art_affine_point(&c, &w, affine); + if (cx) { + *cx = floor(c.x + 0.5); + } + if (cy) { + *cy = floor(c.y + 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) +{ + double affine[6]; + ArtPoint w, c; + + g_return_if_fail(GANV_IS_CANVAS_BASE(canvas)); + + ganv_canvas_base_w2c_affine(canvas, affine); + w.x = wx; + w.y = wy; + art_affine_point(&c, &w, affine); + if (cx) { + *cx = c.x; + } + if (cy) { + *cy = c.y; + } +} + +/** + * 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) +{ + double affine[6], inv[6]; + ArtPoint w, c; + + g_return_if_fail(GANV_IS_CANVAS_BASE(canvas)); + + ganv_canvas_base_w2c_affine(canvas, affine); + art_affine_invert(inv, affine); + c.x = cx; + c.y = cy; + art_affine_point(&w, &c, inv); + if (wx) { + *wx = w.x; + } + if (wy) { + *wy = w.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; + } +} + +static gboolean +boolean_handled_accumulator(GSignalInvocationHint* ihint, + GValue* return_accu, + const GValue* handler_return, + gpointer dummy) +{ + gboolean continue_emission; + gboolean signal_handled; + + signal_handled = g_value_get_boolean(handler_return); + g_value_set_boolean(return_accu, signal_handled); + continue_emission = !signal_handled; + + return continue_emission; +} + +/* Class initialization function for GanvItemClass */ +static void +ganv_item_class_init(GanvItemClass* class) +{ + GObjectClass* gobject_class; + + gobject_class = (GObjectClass*)class; + + item_parent_class = g_type_class_peek_parent(class); + + gobject_class->set_property = ganv_item_set_property; + 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))); + + item_signals[ITEM_EVENT] + = g_signal_new("event", + G_TYPE_FROM_CLASS(class), + 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; + + class->realize = ganv_item_realize; + class->unrealize = ganv_item_unrealize; + class->map = ganv_item_map; + class->unmap = ganv_item_unmap; + class->update = ganv_item_update; +} diff --git a/src/circle.c b/src/circle.c index b27c822..4f4675d 100644 --- a/src/circle.c +++ b/src/circle.c @@ -16,6 +16,7 @@ #include <math.h> #include <string.h> +#include "ganv/canvas-base.h" #include "ganv/circle.h" #include "./color.h" @@ -118,7 +119,7 @@ ganv_circle_tail_vector(const GanvNode* self, double* dy) { g_object_get(G_OBJECT(self), "x", x, "y", y, NULL); - gnome_canvas_item_i2w(GNOME_CANVAS_ITEM(self)->parent, x, y); + ganv_item_i2w(GANV_ITEM(self)->parent, x, y); *dx = 0.0; *dy = 0.0; } @@ -157,11 +158,11 @@ ganv_circle_head_vector(const GanvNode* self, *dx = 0.0; *dy = 0.0; - gnome_canvas_item_i2w(GNOME_CANVAS_ITEM(self)->parent, x, y); + ganv_item_i2w(GANV_ITEM(self)->parent, x, y); } static void -request_redraw(GnomeCanvasItem* item, +request_redraw(GanvItem* item, const GanvCircleCoords* coords, gboolean world) { @@ -174,22 +175,22 @@ request_redraw(GnomeCanvasItem* item, if (!world) { // Convert from parent-relative coordinates to world coordinates - gnome_canvas_item_i2w(item, &x1, &y1); - gnome_canvas_item_i2w(item, &x2, &y2); + ganv_item_i2w(item, &x1, &y1); + ganv_item_i2w(item, &x2, &y2); } - gnome_canvas_request_redraw(item->canvas, x1, y1, x2, y2); - gnome_canvas_request_redraw(item->canvas, 0, 0, 10000, 10000); + ganv_canvas_base_request_redraw(item->canvas, x1, y1, x2, y2); + ganv_canvas_base_request_redraw(item->canvas, 0, 0, 10000, 10000); } static void -coords_i2w(GnomeCanvasItem* item, GanvCircleCoords* coords) +coords_i2w(GanvItem* item, GanvCircleCoords* coords) { - gnome_canvas_item_i2w(item, &coords->x, &coords->y); + ganv_item_i2w(item, &coords->x, &coords->y); } static void -ganv_circle_bounds_item(GnomeCanvasItem* item, +ganv_circle_bounds_item(GanvItem* item, double* x1, double* y1, double* x2, double* y2) { @@ -202,25 +203,25 @@ ganv_circle_bounds_item(GnomeCanvasItem* item, } static void -ganv_circle_bounds(GnomeCanvasItem* item, +ganv_circle_bounds(GanvItem* item, double* x1, double* y1, double* x2, double* y2) { ganv_circle_bounds_item(item, x1, y1, x2, y2); - gnome_canvas_item_i2w(item, x1, y1); - gnome_canvas_item_i2w(item, x2, y2); + ganv_item_i2w(item, x1, y1); + ganv_item_i2w(item, x2, y2); } static void -ganv_circle_update(GnomeCanvasItem* item, - double* affine, - ArtSVP* clip_path, - int flags) +ganv_circle_update(GanvItem* item, + double* affine, + ArtSVP* clip_path, + int flags) { GanvCircle* circle = GANV_CIRCLE(item); GanvCircleImpl* impl = circle->impl; - GnomeCanvasItemClass* item_class = GNOME_CANVAS_ITEM_CLASS(parent_class); + GanvItemClass* item_class = GANV_ITEM_CLASS(parent_class); if (item_class->update) { (*item_class->update)(item, affine, clip_path, flags); } @@ -238,22 +239,15 @@ ganv_circle_update(GnomeCanvasItem* item, ganv_circle_bounds(item, &x1, &y1, &x2, &y2); // Update item canvas coordinates - gnome_canvas_w2c_d(GNOME_CANVAS(item->canvas), x1, y1, &item->x1, &item->y1); - gnome_canvas_w2c_d(GNOME_CANVAS(item->canvas), x2, y2, &item->x2, &item->y2); + 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); // Request redraw of new location request_redraw(item, &impl->coords, FALSE); } static void -ganv_circle_render(GnomeCanvasItem* item, - GnomeCanvasBuf* buf) -{ - // Not implemented -} - -static void -ganv_circle_draw(GnomeCanvasItem* item, +ganv_circle_draw(GanvItem* item, GdkDrawable* drawable, int x, int y, int width, int height) @@ -266,7 +260,7 @@ ganv_circle_draw(GnomeCanvasItem* item, double cx = impl->coords.x; double cy = impl->coords.y; - gnome_canvas_item_i2w(item, &cx, &cy); + ganv_item_i2w(item, &cx, &cy); double dash_length, border_color, fill_color; ganv_node_get_draw_properties( @@ -296,10 +290,10 @@ ganv_circle_draw(GnomeCanvasItem* item, } static double -ganv_circle_point(GnomeCanvasItem* item, +ganv_circle_point(GanvItem* item, double x, double y, int cx, int cy, - GnomeCanvasItem** actual_item) + GanvItem** actual_item) { const GanvCircle* circle = GANV_CIRCLE(item); const GanvCircleCoords* coords = &circle->impl->coords; @@ -322,10 +316,10 @@ ganv_circle_point(GnomeCanvasItem* item, static void ganv_circle_class_init(GanvCircleClass* class) { - GObjectClass* gobject_class = (GObjectClass*)class; - GtkObjectClass* object_class = (GtkObjectClass*)class; - GnomeCanvasItemClass* item_class = (GnomeCanvasItemClass*)class; - GanvNodeClass* node_class = (GanvNodeClass*)class; + GObjectClass* gobject_class = (GObjectClass*)class; + GtkObjectClass* object_class = (GtkObjectClass*)class; + GanvItemClass* item_class = (GanvItemClass*)class; + GanvNodeClass* node_class = (GanvNodeClass*)class; parent_class = GANV_NODE_CLASS(g_type_class_peek_parent(class)); @@ -352,6 +346,5 @@ ganv_circle_class_init(GanvCircleClass* class) item_class->update = ganv_circle_update; item_class->bounds = ganv_circle_bounds; item_class->point = ganv_circle_point; - item_class->render = ganv_circle_render; item_class->draw = ganv_circle_draw; } @@ -17,8 +17,8 @@ #include <string.h> #include <cairo.h> -#include <libgnomecanvas/libgnomecanvas.h> +#include "ganv/canvas-base.h" #include "ganv/canvas.h" #include "ganv/edge.h" #include "ganv/node.h" @@ -52,9 +52,9 @@ enum { PROP_GHOST }; -G_DEFINE_TYPE(GanvEdge, ganv_edge, GNOME_TYPE_CANVAS_ITEM) +G_DEFINE_TYPE(GanvEdge, ganv_edge, GANV_TYPE_ITEM) -static GnomeCanvasItemClass* parent_class; +static GanvItemClass* parent_class; static void ganv_edge_init(GanvEdge* edge) @@ -162,7 +162,7 @@ ganv_edge_get_property(GObject* object, } static void -request_redraw(GnomeCanvas* canvas, +request_redraw(GanvCanvasBase* canvas, const GanvEdgeCoords* coords) { const double w = coords->width; @@ -182,7 +182,7 @@ request_redraw(GnomeCanvas* 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); - gnome_canvas_request_redraw(canvas, + ganv_canvas_base_request_redraw(canvas, r1x1 - w, r1y1 - w, r1x2 + w, r1y2 + w); @@ -190,7 +190,7 @@ request_redraw(GnomeCanvas* canvas, 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); - gnome_canvas_request_redraw(canvas, + ganv_canvas_base_request_redraw(canvas, r2x1 - w, r2y1 - w, r2x2 + w, r2y2 + w); @@ -200,19 +200,19 @@ request_redraw(GnomeCanvas* canvas, const double x2 = MAX(coords->x1, coords->x2); const double y2 = MAX(coords->y1, coords->y2); - gnome_canvas_request_redraw(canvas, + ganv_canvas_base_request_redraw(canvas, x1 - w, y1 - w, x2 + w, y2 + w); } - gnome_canvas_request_redraw(canvas, + ganv_canvas_base_request_redraw(canvas, coords->handle_x - coords->handle_radius, coords->handle_y - coords->handle_radius, coords->handle_x + coords->handle_radius, coords->handle_y + coords->handle_radius); if (coords->arrowhead) { - gnome_canvas_request_redraw( + ganv_canvas_base_request_redraw( canvas, coords->x2 - ARROW_DEPTH, coords->y2 - ARROW_BREADTH, @@ -222,7 +222,7 @@ request_redraw(GnomeCanvas* canvas, } static void -ganv_edge_bounds(GnomeCanvasItem* item, +ganv_edge_bounds(GanvItem* item, double* x1, double* y1, double* x2, double* y2) { @@ -237,10 +237,10 @@ ganv_edge_bounds(GnomeCanvasItem* item, } static void -ganv_edge_update(GnomeCanvasItem* item, - double* affine, - ArtSVP* clip_path, - int flags) +ganv_edge_update(GanvItem* item, + double* affine, + ArtSVP* clip_path, + int flags) { GanvEdge* edge = GANV_EDGE(item); GanvEdgeImpl* impl = edge->impl; @@ -287,22 +287,15 @@ ganv_edge_update(GnomeCanvasItem* item, } // Update item canvas coordinates - gnome_canvas_w2c_d(GNOME_CANVAS(item->canvas), x1, y1, &item->x1, &item->y1); - gnome_canvas_w2c_d(GNOME_CANVAS(item->canvas), x2, y2, &item->x2, &item->y2); + 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); // Request redraw of new location request_redraw(item->canvas, &impl->coords); } static void -ganv_edge_render(GnomeCanvasItem* item, - GnomeCanvasBuf* buf) -{ - // Not implemented -} - -static void -ganv_edge_draw(GnomeCanvasItem* item, +ganv_edge_draw(GanvItem* item, GdkDrawable* drawable, int x, int y, int width, int height) @@ -420,10 +413,10 @@ ganv_edge_draw(GnomeCanvasItem* item, } static double -ganv_edge_point(GnomeCanvasItem* item, +ganv_edge_point(GanvItem* item, double x, double y, int cx, int cy, - GnomeCanvasItem** actual_item) + GanvItem** actual_item) { const GanvEdge* edge = GANV_EDGE(item); const GanvEdgeCoords* coords = &edge->impl->coords; @@ -462,11 +455,11 @@ ganv_edge_is_within(const GanvEdge* edge, static void ganv_edge_class_init(GanvEdgeClass* class) { - GObjectClass* gobject_class = (GObjectClass*)class; - GtkObjectClass* object_class = (GtkObjectClass*)class; - GnomeCanvasItemClass* item_class = (GnomeCanvasItemClass*)class; + GObjectClass* gobject_class = (GObjectClass*)class; + GtkObjectClass* object_class = (GtkObjectClass*)class; + GanvItemClass* item_class = (GanvItemClass*)class; - parent_class = GNOME_CANVAS_ITEM_CLASS(g_type_class_peek_parent(class)); + parent_class = GANV_ITEM_CLASS(g_type_class_peek_parent(class)); g_type_class_add_private(class, sizeof(GanvEdgeImpl)); @@ -582,7 +575,6 @@ ganv_edge_class_init(GanvEdgeClass* class) item_class->update = ganv_edge_update; item_class->bounds = ganv_edge_bounds; item_class->point = ganv_edge_point; - item_class->render = ganv_edge_render; item_class->draw = ganv_edge_draw; } @@ -597,7 +589,7 @@ ganv_edge_new(GanvCanvas* canvas, va_list args; va_start(args, first_prop_name); - gnome_canvas_item_construct(&edge->item, + ganv_item_construct(&edge->item, ganv_canvas_get_root(canvas), first_prop_name, args); va_end(args); @@ -614,7 +606,7 @@ ganv_edge_new(GanvCanvas* canvas, void ganv_edge_update_location(GanvEdge* edge) { - gnome_canvas_item_request_update(GNOME_CANVAS_ITEM(edge)); + ganv_item_request_update(GANV_ITEM(edge)); } void @@ -635,24 +627,24 @@ void ganv_edge_highlight(GanvEdge* edge) { edge->impl->highlighted = TRUE; - gnome_canvas_item_raise_to_top(GNOME_CANVAS_ITEM(edge)); - gnome_canvas_item_request_update(GNOME_CANVAS_ITEM(edge)); + ganv_item_raise_to_top(GANV_ITEM(edge)); + ganv_item_request_update(GANV_ITEM(edge)); } void ganv_edge_unhighlight(GanvEdge* edge) { edge->impl->highlighted = FALSE; - gnome_canvas_item_request_update(GNOME_CANVAS_ITEM(edge)); + ganv_item_request_update(GANV_ITEM(edge)); } void ganv_edge_tick(GanvEdge* edge, double seconds) { - gnome_canvas_item_set(GNOME_CANVAS_ITEM(edge), - "dash-offset", seconds * 8.0, - NULL); + ganv_item_set(GANV_ITEM(edge), + "dash-offset", seconds * 8.0, + NULL); } void diff --git a/src/ganv-marshal.list b/src/ganv-marshal.list new file mode 100644 index 0000000..5ad61bf --- /dev/null +++ b/src/ganv-marshal.list @@ -0,0 +1,2 @@ +VOID:OBJECT,INT,INT,INT,INT +BOOLEAN:BOXED diff --git a/src/ganv-private.h b/src/ganv-private.h index 2f5b90b..8e9af59 100644 --- a/src/ganv-private.h +++ b/src/ganv-private.h @@ -84,16 +84,16 @@ struct _GanvEdgeImpl struct _GanvModuleImpl { - GPtrArray* ports; - GnomeCanvasItem* icon_box; - GnomeCanvasItem* embed_item; - int embed_width; - int embed_height; - double widest_input; - double widest_output; - gboolean show_port_labels; - gboolean must_resize; - gboolean port_size_changed; + GPtrArray* ports; + GanvItem* icon_box; + GanvItem* embed_item; + int embed_width; + int embed_height; + double widest_input; + double widest_output; + gboolean show_port_labels; + gboolean must_resize; + gboolean port_size_changed; }; /* Node */ diff --git a/src/ganv_test.c b/src/ganv_test.c index 4c1fc40..a1fba2a 100644 --- a/src/ganv_test.c +++ b/src/ganv_test.c @@ -49,14 +49,17 @@ main(int argc, char** argv) NULL); ganv_port_show_control(cport); + //GtkWidget* entry = gtk_entry_new(); + //ganv_module_embed(module, entry); + GanvPort* tport = ganv_port_new(module, TRUE, "label", "Toggle", NULL); ganv_port_show_control(tport); ganv_port_set_control_is_toggle(tport, TRUE); - gnome_canvas_item_show(GNOME_CANVAS_ITEM(module)); - gnome_canvas_item_raise_to_top(GNOME_CANVAS_ITEM(module)); + ganv_item_show(GANV_ITEM(module)); + ganv_item_raise_to_top(GANV_ITEM(module)); gtk_widget_show_all(GTK_WIDGET(win)); gtk_window_present(win); diff --git a/src/ganv_test.py b/src/ganv_test.py index 8ba784d..1488224 100755 --- a/src/ganv_test.py +++ b/src/ganv_test.py @@ -9,6 +9,13 @@ win.connect("destroy", lambda obj: Gtk.main_quit()) canvas = Ganv.Canvas.new(1024, 768) module = Ganv.Module(canvas=canvas, label="Test") +iport = Ganv.Port(module=module, + is_input=True, + label="In") +oport = Ganv.Port(module=module, + is_input=False, + label="In") + win.add(canvas) win.show_all() win.present() diff --git a/src/module.c b/src/module.c index 21fca0a..e509615 100644 --- a/src/module.c +++ b/src/module.c @@ -18,6 +18,7 @@ #include "ganv/canvas.h" #include "ganv/module.h" #include "ganv/port.h" +#include "ganv/widget.h" #include "./color.h" #include "./boilerplate.h" @@ -112,7 +113,7 @@ ganv_module_set_property(GObject* object, FOREACH_PORT_CONST(gobj()->ports, p) { (*p)->show_label(b); }*/ - gnome_canvas_item_request_update(GNOME_CANVAS_ITEM(object)); + ganv_item_request_update(GANV_ITEM(object)); } break; } @@ -172,7 +173,7 @@ measure(GanvModule* module, Metrics* m) double title_w, title_h; title_size(module, &title_w, &title_h); - GanvCanvas* canvas = GANV_CANVAS(GNOME_CANVAS_ITEM(module)->canvas); + GanvCanvas* canvas = GANV_CANVAS(GANV_ITEM(module)->canvas); GanvText* canvas_title = GANV_NODE(module)->impl->label; GanvModuleImpl* impl = module->impl; @@ -268,28 +269,28 @@ place_title(GanvModule* module, GanvDirection dir) return; } else if (dir == GANV_DIRECTION_RIGHT) { if (impl->icon_box) { - gnome_canvas_item_set(GNOME_CANVAS_ITEM(canvas_title), - "x", MODULE_ICON_SIZE + 1.0, - NULL); + ganv_item_set(GANV_ITEM(canvas_title), + "x", MODULE_ICON_SIZE + 1.0, + NULL); } else { - gnome_canvas_item_set(GNOME_CANVAS_ITEM(canvas_title), - "x", ((ganv_box_get_width(box) / 2.0) - - (title_w / 2.0)), - NULL); + ganv_item_set(GANV_ITEM(canvas_title), + "x", ((ganv_box_get_width(box) / 2.0) + - (title_w / 2.0)), + NULL); } } else { - gnome_canvas_item_set(GNOME_CANVAS_ITEM(canvas_title), - "x", ((ganv_box_get_width(box) / 2.0) - - (title_w / 2.0)), - "y", ganv_module_get_empty_port_depth(module) + 2.0, - NULL); + ganv_item_set(GANV_ITEM(canvas_title), + "x", ((ganv_box_get_width(box) / 2.0) + - (title_w / 2.0)), + "y", ganv_module_get_empty_port_depth(module) + 2.0, + NULL); } } static void resize_horiz(GanvModule* module) { - GanvCanvas* canvas = GANV_CANVAS(GNOME_CANVAS_ITEM(module)->canvas); + GanvCanvas* canvas = GANV_CANVAS(GANV_ITEM(module)->canvas); GanvModuleImpl* impl = module->impl; Metrics m; @@ -304,9 +305,9 @@ resize_horiz(GanvModule* module) double height = header_height; if (impl->embed_item) { - gnome_canvas_item_set(impl->embed_item, - "x", (double)m.embed_x, - NULL); + ganv_item_set(impl->embed_item, + "x", (double)m.embed_x, + NULL); } // Actually set width and height @@ -369,7 +370,7 @@ resize_horiz(GanvModule* module) static void resize_vert(GanvModule* module) { - GanvCanvas* canvas = GANV_CANVAS(GNOME_CANVAS_ITEM(module)->canvas); + GanvCanvas* canvas = GANV_CANVAS(GANV_ITEM(module)->canvas); GanvModuleImpl* impl = module->impl; Metrics m; @@ -384,10 +385,10 @@ resize_vert(GanvModule* module) const double port_breadth = ganv_module_get_empty_port_breadth(module); if (impl->embed_item) { - gnome_canvas_item_set(impl->embed_item, - "x", (double)m.embed_x, - "y", port_depth + title_h, - NULL); + ganv_item_set(impl->embed_item, + "x", (double)m.embed_x, + "y", port_depth + title_h, + NULL); } const double height = PAD + title_h @@ -460,7 +461,7 @@ layout(GanvNode* self) GanvModule* module = GANV_MODULE(self); GanvModuleImpl* impl = module->impl; GanvNode* node = GANV_NODE(self); - GanvCanvas* canvas = GANV_CANVAS(GNOME_CANVAS_ITEM(module)->canvas); + GanvCanvas* canvas = GANV_CANVAS(GANV_ITEM(module)->canvas); double label_w = 0.0; double label_h = 0.0; @@ -506,10 +507,10 @@ ganv_module_resize(GanvNode* self) } static void -ganv_module_update(GnomeCanvasItem* item, - double* affine, - ArtSVP* clip_path, - int flags) +ganv_module_update(GanvItem* item, + double* affine, + ArtSVP* clip_path, + int flags) { GanvNode* node = GANV_NODE(item); GanvModule* module = GANV_MODULE(item); @@ -517,7 +518,7 @@ ganv_module_update(GnomeCanvasItem* item, layout(node); } - GnomeCanvasItemClass* item_class = GNOME_CANVAS_ITEM_CLASS(parent_class); + GanvItemClass* item_class = GANV_ITEM_CLASS(parent_class); item_class->update(item, affine, clip_path, flags); } @@ -548,10 +549,10 @@ ganv_module_move(GanvNode* node, static void ganv_module_class_init(GanvModuleClass* class) { - GObjectClass* gobject_class = (GObjectClass*)class; - GtkObjectClass* object_class = (GtkObjectClass*)class; - GnomeCanvasItemClass* item_class = (GnomeCanvasItemClass*)class; - GanvNodeClass* node_class = (GanvNodeClass*)class; + GObjectClass* gobject_class = (GObjectClass*)class; + GtkObjectClass* object_class = (GtkObjectClass*)class; + GanvItemClass* item_class = (GanvItemClass*)class; + GanvNodeClass* node_class = (GanvNodeClass*)class; parent_class = GANV_BOX_CLASS(g_type_class_peek_parent(class)); @@ -662,10 +663,10 @@ ganv_module_add_port(GanvModule* module, //} GanvCanvas* canvas = GANV_CANVAS( - GNOME_CANVAS_ITEM(module)->canvas); + GANV_ITEM(module)->canvas); place_title(module, canvas->direction); - gnome_canvas_item_request_update(GNOME_CANVAS_ITEM(module)); + ganv_item_request_update(GANV_ITEM(module)); } void @@ -696,7 +697,7 @@ ganv_module_remove_port(GanvModule* module, } _must_resize = true; - gnome_canvas_item_request_update(GNOME_CANVAS_ITEM(_gobj)); + ganv_item_request_update(GANV_ITEM(_gobj)); } else { std::cerr << "Failed to find port to remove" << std::endl; } @@ -713,7 +714,7 @@ double ganv_module_get_empty_port_depth(const GanvModule* module) { GanvCanvas* canvas = GANV_CANVAS( - GNOME_CANVAS_ITEM(module)->canvas); + GANV_ITEM(module)->canvas); return ganv_canvas_get_font_size(canvas); } @@ -722,6 +723,9 @@ void ganv_module_set_icon(GanvModule* module, GdkPixbuf* icon) { + fprintf(stderr, "FIXME: icon\n"); + return; +#if 0 GanvModuleImpl* impl = module->impl; if (impl->icon_box) { @@ -730,9 +734,9 @@ ganv_module_set_icon(GanvModule* module, } if (icon) { - impl->icon_box = gnome_canvas_item_new( - GNOME_CANVAS_GROUP(module), - gnome_canvas_pixbuf_get_type(), + impl->icon_box = ganv_item_new( + GANV_GROUP(module), + ganv_canvas_base_pixbuf_get_type(), "x", 8.0, "y", 10.0, "pixbuf", icon, @@ -747,11 +751,12 @@ ganv_module_set_icon(GanvModule* module, scale, 0.0, 0.0 }; - gnome_canvas_item_affine_relative(impl->icon_box, scale_trans); - gnome_canvas_item_raise_to_top(impl->icon_box); - gnome_canvas_item_show(impl->icon_box); + ganv_item_affine_relative(impl->icon_box, scale_trans); + ganv_item_raise_to_top(impl->icon_box); + ganv_item_show(impl->icon_box); } impl->must_resize = TRUE; +#endif } static void @@ -775,10 +780,10 @@ on_embed_size_request(GtkWidget* widget, allocation.height = r->width; gtk_widget_size_allocate(widget, &allocation); - gnome_canvas_item_set(impl->embed_item, - "width", (double)r->width, - "height", (double)r->height, - NULL); + ganv_item_set(impl->embed_item, + "width", (double)r->width, + "height", (double)r->height, + NULL); } void @@ -803,9 +808,9 @@ ganv_module_embed(GanvModule* module, title_size(module, &title_w, &title_h); const double y = 4.0 + title_h; - impl->embed_item = gnome_canvas_item_new( - GNOME_CANVAS_GROUP(module), - gnome_canvas_widget_get_type(), + impl->embed_item = ganv_item_new( + GANV_GROUP(module), + ganv_widget_get_type(), "x", 2.0, "y", y, "widget", widget, @@ -817,14 +822,14 @@ ganv_module_embed(GanvModule* module, gtk_widget_size_request(widget, &r); on_embed_size_request(widget, &r, module); - gnome_canvas_item_show(impl->embed_item); - gnome_canvas_item_raise_to_top(impl->embed_item); + ganv_item_show(impl->embed_item); + ganv_item_raise_to_top(impl->embed_item); g_signal_connect(widget, "size-request", G_CALLBACK(on_embed_size_request), module); layout(GANV_NODE(module)); - gnome_canvas_item_request_update(GNOME_CANVAS_ITEM(module)); + ganv_item_request_update(GANV_ITEM(module)); } void @@ -13,9 +13,8 @@ * with Ganv. If not, see <http://www.gnu.org/licenses/>. */ -#include <libgnomecanvas/libgnomecanvas.h> - #include "ganv/canvas.h" +#include "ganv/canvas-base.h" #include "ganv/node.h" #include "./boilerplate.h" @@ -25,9 +24,9 @@ guint signal_moved; -G_DEFINE_TYPE(GanvNode, ganv_node, GNOME_TYPE_CANVAS_GROUP) +G_DEFINE_TYPE(GanvNode, ganv_node, GANV_TYPE_GROUP) -static GnomeCanvasGroupClass* parent_class; +static GanvGroupClass* parent_class; enum { PROP_0, @@ -78,9 +77,9 @@ ganv_node_init(GanvNode* node) } static void -ganv_node_realize(GnomeCanvasItem* item) +ganv_node_realize(GanvItem* item) { - GNOME_CANVAS_ITEM_CLASS(parent_class)->realize(item); + GANV_ITEM_CLASS(parent_class)->realize(item); ganv_canvas_add_node(GANV_CANVAS(item->canvas), GANV_NODE(item)); } @@ -98,7 +97,7 @@ ganv_node_destroy(GtkObject* object) impl->label = NULL; } - GnomeCanvasItem* item = GNOME_CANVAS_ITEM(object); + GanvItem* item = GANV_ITEM(object); ganv_node_disconnect(node); if (item->canvas) { ganv_canvas_remove_node(GANV_CANVAS(item->canvas), node); @@ -123,7 +122,7 @@ ganv_node_set_property(GObject* object, GanvNode* node = GANV_NODE(object); GanvNodeImpl* impl = node->impl; - GnomeCanvasItem* item = GNOME_CANVAS_ITEM(object); + GanvItem* item = GANV_ITEM(object); switch (prop_id) { SET_CASE(PARTNER, object, impl->partner); @@ -139,8 +138,8 @@ ganv_node_set_property(GObject* object, SET_CASE(DRAGGABLE, boolean, impl->draggable); case PROP_CANVAS: if (!item->parent) { - GnomeCanvas* canvas = GNOME_CANVAS(g_value_get_object(value)); - g_object_set(object, "parent", gnome_canvas_root(canvas), NULL); + GanvCanvasBase* canvas = GANV_CANVAS_BASE(g_value_get_object(value)); + g_object_set(object, "parent", ganv_canvas_base_root(canvas), NULL); ganv_canvas_add_node(GANV_CANVAS(canvas), node); } else { g_warning("Cannot change `canvas' property after construction"); @@ -148,7 +147,7 @@ ganv_node_set_property(GObject* object, break; case PROP_LABEL: ganv_node_set_label(node, g_value_get_string(value)); - gnome_canvas_item_request_update(GNOME_CANVAS_ITEM(object)); + ganv_item_request_update(GANV_ITEM(object)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); @@ -167,7 +166,7 @@ ganv_node_get_property(GObject* object, GanvNode* node = GANV_NODE(object); GanvNodeImpl* impl = node->impl; - GnomeCanvasItem* item = GNOME_CANVAS_ITEM(object); + GanvItem* item = GANV_ITEM(object); typedef char* gstring; @@ -208,7 +207,7 @@ ganv_node_default_tail_vector(const GanvNode* self, *dx = 1.0; *dy = 0.0; - gnome_canvas_item_i2w(GNOME_CANVAS_ITEM(self)->parent, x, y); + ganv_item_i2w(GANV_ITEM(self)->parent, x, y); } static void @@ -226,7 +225,7 @@ ganv_node_default_head_vector(const GanvNode* self, *dx = -1.0; *dy = 0.0; - gnome_canvas_item_i2w(GNOME_CANVAS_ITEM(self)->parent, x, y); + ganv_item_i2w(GANV_ITEM(self)->parent, x, y); } void @@ -262,12 +261,12 @@ ganv_node_set_label(GanvNode* node, const char* str) impl->label = NULL; } } else if (impl->label) { - gnome_canvas_item_set(GNOME_CANVAS_ITEM(impl->label), - "text", str, - NULL); + ganv_item_set(GANV_ITEM(impl->label), + "text", str, + NULL); } else { - impl->label = GANV_TEXT(gnome_canvas_item_new( - GNOME_CANVAS_GROUP(node), + impl->label = GANV_TEXT(ganv_item_new( + GANV_GROUP(node), ganv_text_get_type(), "text", str, "color", 0xFFFFFFFF, @@ -286,13 +285,13 @@ ganv_node_default_tick(GanvNode* self, { GanvNode* node = GANV_NODE(self); node->impl->dash_offset = seconds * 8.0; - gnome_canvas_item_request_update(GNOME_CANVAS_ITEM(self)); + ganv_item_request_update(GANV_ITEM(self)); } static void ganv_node_default_disconnect(GanvNode* node) { - GanvCanvas* canvas = GANV_CANVAS(GNOME_CANVAS_ITEM(node)->canvas); + GanvCanvas* canvas = GANV_CANVAS(GANV_ITEM(node)->canvas); if (canvas) { ganv_canvas_for_each_edge_on(canvas, node, ganv_edge_remove); } @@ -303,8 +302,8 @@ ganv_node_default_move(GanvNode* node, double dx, double dy) { - GanvCanvas* canvas = GANV_CANVAS(GNOME_CANVAS_ITEM(node)->canvas); - gnome_canvas_item_move(GNOME_CANVAS_ITEM(node), dx, dy); + GanvCanvas* canvas = GANV_CANVAS(GANV_ITEM(node)->canvas); + ganv_item_move(GANV_ITEM(node), dx, dy); ganv_canvas_for_each_edge_on(canvas, node, ganv_edge_update_location); @@ -315,11 +314,11 @@ ganv_node_default_move_to(GanvNode* node, double x, double y) { - GanvCanvas* canvas = GANV_CANVAS(GNOME_CANVAS_ITEM(node)->canvas); - gnome_canvas_item_set(GNOME_CANVAS_ITEM(node), - "x", x, - "y", y, - NULL); + GanvCanvas* canvas = GANV_CANVAS(GANV_ITEM(node)->canvas); + ganv_item_set(GANV_ITEM(node), + "x", x, + "y", y, + NULL); if (node->impl->can_tail) { ganv_canvas_for_each_edge_from( canvas, node, ganv_edge_update_location); @@ -333,7 +332,7 @@ static gboolean ganv_node_default_on_event(GanvNode* node, GdkEvent* event) { - GanvCanvas* canvas = GANV_CANVAS(GNOME_CANVAS_ITEM(node)->canvas); + GanvCanvas* canvas = GANV_CANVAS(GANV_ITEM(node)->canvas); // FIXME: put these somewhere better static double last_x, last_y; @@ -342,19 +341,19 @@ ganv_node_default_on_event(GanvNode* node, /* double click_x, - gnome_canvas_item_w2i(GNOME_CANVAS_ITEM(GNOME_CANVAS_ITEM(node)->parent), + ganv_item_w2i(GANV_ITEM(GANV_ITEM(node)->parent), &click_x, &click_y); */ switch (event->type) { case GDK_ENTER_NOTIFY: - gnome_canvas_item_set(GNOME_CANVAS_ITEM(node), - "highlighted", TRUE, NULL); + ganv_item_set(GANV_ITEM(node), + "highlighted", TRUE, NULL); return TRUE; case GDK_LEAVE_NOTIFY: - gnome_canvas_item_set(GNOME_CANVAS_ITEM(node), - "highlighted", FALSE, NULL); + ganv_item_set(GANV_ITEM(node), + "highlighted", FALSE, NULL); return TRUE; case GDK_BUTTON_PRESS: @@ -363,8 +362,8 @@ ganv_node_default_on_event(GanvNode* node, last_x = event->button.x; last_y = event->button.y; if (!canvas->locked && node->impl->draggable && event->button.button == 1) { - gnome_canvas_item_grab( - GNOME_CANVAS_ITEM(node), + ganv_item_grab( + GANV_ITEM(node), GDK_POINTER_MOTION_MASK|GDK_BUTTON_RELEASE_MASK|GDK_BUTTON_PRESS_MASK, ganv_canvas_get_move_cursor(canvas), event->button.time); @@ -377,7 +376,7 @@ ganv_node_default_on_event(GanvNode* node, if (dragging) { gboolean selected; g_object_get(G_OBJECT(node), "selected", &selected, NULL); - gnome_canvas_item_ungrab(GNOME_CANVAS_ITEM(node), event->button.time); + ganv_item_ungrab(GANV_ITEM(node), event->button.time); dragging = FALSE; if (event->button.x != drag_start_x || event->button.y != drag_start_y) { if (selected) { @@ -443,11 +442,11 @@ ganv_node_default_on_event(GanvNode* node, static void ganv_node_class_init(GanvNodeClass* class) { - GObjectClass* gobject_class = (GObjectClass*)class; - GtkObjectClass* object_class = (GtkObjectClass*)class; - GnomeCanvasItemClass* item_class = (GnomeCanvasItemClass*)class; + GObjectClass* gobject_class = (GObjectClass*)class; + GtkObjectClass* object_class = (GtkObjectClass*)class; + GanvItemClass* item_class = (GanvItemClass*)class; - parent_class = GNOME_CANVAS_GROUP_CLASS(g_type_class_peek_parent(class)); + parent_class = GANV_GROUP_CLASS(g_type_class_peek_parent(class)); g_type_class_add_private(class, sizeof(GanvNodeImpl)); @@ -50,9 +50,9 @@ ganv_port_destroy(GtkObject* object) g_return_if_fail(object != NULL); g_return_if_fail(GANV_IS_PORT(object)); - GnomeCanvasItem* item = GNOME_CANVAS_ITEM(object); - GanvPort* port = GANV_PORT(object); - GanvCanvas* canvas = GANV_CANVAS(item->canvas); + GanvItem* item = GANV_ITEM(object); + GanvPort* port = GANV_PORT(object); + GanvCanvas* canvas = GANV_CANVAS(item->canvas); if (canvas) { if (port->impl->is_input) { ganv_canvas_for_each_edge_to(canvas, @@ -127,7 +127,7 @@ ganv_port_tail_vector(const GanvNode* self, *dx = 1.0; *dy = 0.0; - gnome_canvas_item_i2w(GNOME_CANVAS_ITEM(self)->parent, x, y); + ganv_item_i2w(GANV_ITEM(self)->parent, x, y); } static void @@ -148,7 +148,7 @@ ganv_port_head_vector(const GanvNode* self, *dx = -1.0; *dy = 0.0; - gnome_canvas_item_i2w(GNOME_CANVAS_ITEM(self)->parent, x, y); + ganv_item_i2w(GANV_ITEM(self)->parent, x, y); } static void @@ -166,10 +166,10 @@ ganv_port_resize(GanvNode* self) ganv_box_set_width(&port->box, label_w + (PORT_LABEL_HPAD * 2.0)); ganv_box_set_height(&port->box, label_h + (PORT_LABEL_VPAD * 2.0)); - gnome_canvas_item_set(GNOME_CANVAS_ITEM(node->impl->label), - "x", PORT_LABEL_HPAD, - "y", PORT_LABEL_VPAD, - NULL); + ganv_item_set(GANV_ITEM(node->impl->label), + "x", PORT_LABEL_HPAD, + "y", PORT_LABEL_VPAD, + NULL); if (parent_class->parent_class.resize) { parent_class->parent_class.resize(self); @@ -196,17 +196,17 @@ ganv_port_set_height(GanvBox* box, if (port->impl->control) { double control_y1; g_object_get(port->impl->control->rect, "y1", &control_y1, NULL); - gnome_canvas_item_set(GNOME_CANVAS_ITEM(port->impl->control->rect), - "y2", control_y1 + height, - NULL); + ganv_item_set(GANV_ITEM(port->impl->control->rect), + "y2", control_y1 + height, + NULL); } } static gboolean on_event(GanvNode* node, GdkEvent* event) { - GnomeCanvasItem* item = GNOME_CANVAS_ITEM(node); - GanvCanvas* canvas = GANV_CANVAS(item->canvas); + GanvItem* item = GANV_ITEM(node); + GanvCanvas* canvas = GANV_CANVAS(item->canvas); return ganv_canvas_port_event(canvas, GANV_PORT(node), event); } @@ -253,11 +253,11 @@ ganv_port_new(GanvModule* module, GanvPort* port = GANV_PORT( g_object_new(ganv_port_get_type(), NULL)); - GnomeCanvasItem* item = GNOME_CANVAS_ITEM(port); + GanvItem* item = GANV_ITEM(port); va_list args; va_start(args, first_prop_name); - gnome_canvas_item_construct(item, - GNOME_CANVAS_GROUP(module), + ganv_item_construct(item, + GANV_GROUP(module), first_prop_name, args); va_end(args); @@ -301,8 +301,8 @@ ganv_port_show_control(GanvPort* port) control->min = 0.0f; control->max = 0.0f; control->is_toggle = FALSE; - control->rect = GANV_BOX(gnome_canvas_item_new( - GNOME_CANVAS_GROUP(port), + control->rect = GANV_BOX(ganv_item_new( + GANV_GROUP(port), ganv_box_get_type(), "x1", 0.0, "y1", 0.0, @@ -311,7 +311,7 @@ ganv_port_show_control(GanvPort* port) "fill-color", 0xFFFFFF80, "border-width", 0.0, NULL)); - gnome_canvas_item_show(GNOME_CANVAS_ITEM(control->rect)); + ganv_item_show(GANV_ITEM(control->rect)); } void @@ -425,7 +425,7 @@ ganv_port_get_natural_width(const GanvPort* port) GanvModule* ganv_port_get_module(const GanvPort* port) { - return GANV_MODULE(GNOME_CANVAS_ITEM(port)->parent); + return GANV_MODULE(GANV_ITEM(port)->parent); } float @@ -18,8 +18,8 @@ #include <math.h> #include <gtk/gtkstyle.h> -#include <libgnomecanvas/libgnomecanvas.h> +#include "ganv/canvas-base.h" #include "ganv/canvas.h" #include "ganv/text.h" @@ -28,9 +28,9 @@ #include "./gettext.h" #include "./ganv-private.h" -G_DEFINE_TYPE(GanvText, ganv_text, GNOME_TYPE_CANVAS_ITEM) +G_DEFINE_TYPE(GanvText, ganv_text, GANV_TYPE_ITEM) -static GnomeCanvasItemClass* parent_class; +static GanvItemClass* parent_class; enum { PROP_0, @@ -88,12 +88,12 @@ ganv_text_destroy(GtkObject* object) static void ganv_text_layout(GanvText* text) { - GanvTextImpl* impl = text->impl; - GnomeCanvasItem* item = GNOME_CANVAS_ITEM(text); - GanvCanvas* canvas = GANV_CANVAS(item->canvas); - GtkWidget* widget = GTK_WIDGET(canvas); - double font_size = ganv_canvas_get_font_size(canvas); - guint color = 0xFFFFFFFF; + GanvTextImpl* impl = text->impl; + GanvItem* item = GANV_ITEM(text); + GanvCanvas* canvas = GANV_CANVAS(item->canvas); + GtkWidget* widget = GTK_WIDGET(canvas); + double font_size = ganv_canvas_get_font_size(canvas); + guint color = 0xFFFFFFFF; GtkStyle* style = gtk_rc_get_style(widget); PangoFontDescription* font = pango_font_description_copy(style->font_desc); @@ -139,7 +139,7 @@ ganv_text_layout(GanvText* text) pango_font_description_free(font); impl->needs_layout = FALSE; - gnome_canvas_item_request_update(GNOME_CANVAS_ITEM(text)); + ganv_item_request_update(GANV_ITEM(text)); } static void @@ -202,7 +202,7 @@ ganv_text_get_property(GObject* object, } static void -ganv_text_bounds_item(GnomeCanvasItem* item, +ganv_text_bounds_item(GanvItem* item, double* x1, double* y1, double* x2, double* y2) { @@ -220,24 +220,24 @@ ganv_text_bounds_item(GnomeCanvasItem* item, } static void -ganv_text_bounds(GnomeCanvasItem* item, +ganv_text_bounds(GanvItem* item, double* x1, double* y1, double* x2, double* y2) { ganv_text_bounds_item(item, x1, y1, x2, y2); - gnome_canvas_item_i2w(item->parent, x1, y1); - gnome_canvas_item_i2w(item->parent, x2, y2); + ganv_item_i2w(item->parent, x1, y1); + ganv_item_i2w(item->parent, x2, y2); } static void -ganv_text_update(GnomeCanvasItem* item, - double* affine, - ArtSVP* clip_path, - int flags) +ganv_text_update(GanvItem* item, + double* affine, + ArtSVP* clip_path, + int flags) { double x1, y1, x2, y2; ganv_text_bounds(item, &x1, &y1, &x2, &y2); - gnome_canvas_request_redraw(item->canvas, x1, y1, x2, y2); + ganv_canvas_base_request_redraw(item->canvas, x1, y1, x2, y2); // I have no idea why this is necessary item->x1 = x1; @@ -249,10 +249,10 @@ ganv_text_update(GnomeCanvasItem* item, } static double -ganv_text_point(GnomeCanvasItem* item, +ganv_text_point(GanvItem* item, double x, double y, int cx, int cy, - GnomeCanvasItem** actual_item) + GanvItem** actual_item) { *actual_item = NULL; @@ -287,14 +287,7 @@ ganv_text_point(GnomeCanvasItem* item, } static void -ganv_text_render(GnomeCanvasItem* item, - GnomeCanvasBuf* buf) -{ - // Not implemented -} - -static void -ganv_text_draw(GnomeCanvasItem* item, +ganv_text_draw(GanvItem* item, GdkDrawable* drawable, int x, int y, int width, int height) @@ -305,7 +298,7 @@ ganv_text_draw(GnomeCanvasItem* item, double wx = impl->coords.x; double wy = impl->coords.y; - gnome_canvas_item_i2w(item, &wx, &wy); + ganv_item_i2w(item, &wx, &wy); // Round to the nearest pixel so text isn't blurry wx = lrint(wx - x); @@ -320,11 +313,11 @@ ganv_text_draw(GnomeCanvasItem* item, static void ganv_text_class_init(GanvTextClass* class) { - GObjectClass* gobject_class = (GObjectClass*)class; - GtkObjectClass* object_class = (GtkObjectClass*)class; - GnomeCanvasItemClass* item_class = (GnomeCanvasItemClass*)class; + GObjectClass* gobject_class = (GObjectClass*)class; + GtkObjectClass* object_class = (GtkObjectClass*)class; + GanvItemClass* item_class = (GanvItemClass*)class; - parent_class = GNOME_CANVAS_ITEM_CLASS(g_type_class_peek_parent(class)); + parent_class = GANV_ITEM_CLASS(g_type_class_peek_parent(class)); g_type_class_add_private(class, sizeof(GanvTextImpl)); @@ -389,6 +382,5 @@ ganv_text_class_init(GanvTextClass* class) item_class->update = ganv_text_update; item_class->bounds = ganv_text_bounds; item_class->point = ganv_text_point; - item_class->render = ganv_text_render; item_class->draw = ganv_text_draw; } diff --git a/src/widget.c b/src/widget.c new file mode 100644 index 0000000..d11a2e6 --- /dev/null +++ b/src/widget.c @@ -0,0 +1,508 @@ +/* This file is part of Ganv. + * Copyright 2007-2011 David Robillard <http://drobilla.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +/* Based on GnomeCanvasWidget, by Federico Mena <federico@nuclecu.unam.mx> + * Copyright 1997-2000 Free Software Foundation + */ + +#include <math.h> + +#include <gtk/gtksignal.h> + +#include "ganv/widget.h" + +#include "./gettext.h" + +G_DEFINE_TYPE(GanvWidget, ganv_widget, GANV_TYPE_ITEM) + +static GanvItemClass* parent_class; + +enum { + PROP_0, + PROP_WIDGET, + PROP_X, + PROP_Y, + PROP_WIDTH, + PROP_HEIGHT, + PROP_ANCHOR, + PROP_SIZE_PIXELS +}; + +static void +ganv_widget_init(GanvWidget* witem) +{ + witem->x = 0.0; + witem->y = 0.0; + witem->width = 0.0; + witem->height = 0.0; + witem->anchor = GTK_ANCHOR_NW; + witem->size_pixels = FALSE; +} + +static void +ganv_widget_destroy(GtkObject* object) +{ + GanvWidget* witem; + + g_return_if_fail(object != NULL); + g_return_if_fail(GANV_IS_WIDGET(object)); + + witem = GANV_WIDGET(object); + + if (witem->widget && !witem->in_destroy) { + g_signal_handler_disconnect(witem->widget, witem->destroy_id); + gtk_widget_destroy(witem->widget); + witem->widget = NULL; + } + + if (GTK_OBJECT_CLASS(parent_class)->destroy) { + (*GTK_OBJECT_CLASS(parent_class)->destroy)(object); + } +} + +static void +recalc_bounds(GanvWidget* witem) +{ + GanvItem* item; + double wx, wy; + + item = GANV_ITEM(witem); + + /* Get world coordinates */ + + wx = witem->x; + wy = witem->y; + ganv_item_i2w(item, &wx, &wy); + + /* Get canvas pixel coordinates */ + + ganv_canvas_base_w2c(item->canvas, wx, wy, &witem->cx, &witem->cy); + + /* Anchor widget item */ + + switch (witem->anchor) { + case GTK_ANCHOR_N: + case GTK_ANCHOR_CENTER: + case GTK_ANCHOR_S: + witem->cx -= witem->cwidth / 2; + break; + + case GTK_ANCHOR_NE: + case GTK_ANCHOR_E: + case GTK_ANCHOR_SE: + witem->cx -= witem->cwidth; + break; + + default: + break; + } + + switch (witem->anchor) { + case GTK_ANCHOR_W: + case GTK_ANCHOR_CENTER: + case GTK_ANCHOR_E: + witem->cy -= witem->cheight / 2; + break; + + case GTK_ANCHOR_SW: + case GTK_ANCHOR_S: + case GTK_ANCHOR_SE: + witem->cy -= witem->cheight; + break; + + default: + break; + } + + /* Bounds */ + + item->x1 = witem->cx; + item->y1 = witem->cy; + item->x2 = witem->cx + witem->cwidth; + item->y2 = witem->cy + witem->cheight; + + if (witem->widget) { + gtk_layout_move(GTK_LAYOUT(item->canvas), witem->widget, + witem->cx + item->canvas->zoom_xofs, + witem->cy + item->canvas->zoom_yofs); + } +} + +static void +do_destroy(GtkObject* object, gpointer data) +{ + GanvWidget* witem = GANV_WIDGET(data); + + witem->in_destroy = TRUE; + gtk_object_destroy(data); +} + +static void +ganv_widget_set_property(GObject* object, + guint param_id, + const GValue* value, + GParamSpec* pspec) +{ + GanvItem* item = GANV_ITEM(object); + GanvWidget* witem = GANV_WIDGET(object); + int update = FALSE; + int calc_bounds = FALSE; + GObject* obj; + + switch (param_id) { + case PROP_WIDGET: + if (witem->widget) { + g_signal_handler_disconnect(witem->widget, witem->destroy_id); + gtk_container_remove(GTK_CONTAINER(item->canvas), witem->widget); + } + + obj = g_value_get_object(value); + if (obj) { + witem->widget = GTK_WIDGET(obj); + witem->destroy_id = g_signal_connect(obj, "destroy", + G_CALLBACK(do_destroy), + witem); + gtk_layout_put(GTK_LAYOUT(item->canvas), witem->widget, + witem->cx + item->canvas->zoom_xofs, + witem->cy + item->canvas->zoom_yofs); + } + + update = TRUE; + break; + + case PROP_X: + if (witem->x != g_value_get_double(value)) { + witem->x = g_value_get_double(value); + calc_bounds = TRUE; + } + break; + + case PROP_Y: + if (witem->y != g_value_get_double(value)) { + witem->y = g_value_get_double(value); + calc_bounds = TRUE; + } + break; + + case PROP_WIDTH: + if (witem->width != fabs(g_value_get_double(value))) { + witem->width = fabs(g_value_get_double(value)); + update = TRUE; + } + break; + + case PROP_HEIGHT: + if (witem->height != fabs(g_value_get_double(value))) { + witem->height = fabs(g_value_get_double(value)); + update = TRUE; + } + break; + + case PROP_ANCHOR: + if (witem->anchor != (GtkAnchorType)g_value_get_enum(value)) { + witem->anchor = g_value_get_enum(value); + update = TRUE; + } + break; + + case PROP_SIZE_PIXELS: + if (witem->size_pixels != g_value_get_boolean(value)) { + witem->size_pixels = g_value_get_boolean(value); + update = TRUE; + } + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec); + break; + } + + if (update) { + (*GANV_ITEM_GET_CLASS(item)->update)(item, NULL, NULL, 0); + } + + if (calc_bounds) { + recalc_bounds(witem); + } +} + +static void +ganv_widget_get_property(GObject* object, + guint param_id, + GValue* value, + GParamSpec* pspec) +{ + GanvWidget* witem; + + g_return_if_fail(object != NULL); + g_return_if_fail(GANV_IS_WIDGET(object)); + + witem = GANV_WIDGET(object); + + switch (param_id) { + case PROP_WIDGET: + g_value_set_object(value, (GObject*)witem->widget); + break; + + case PROP_X: + g_value_set_double(value, witem->x); + break; + + case PROP_Y: + g_value_set_double(value, witem->y); + break; + + case PROP_WIDTH: + g_value_set_double(value, witem->width); + break; + + case PROP_HEIGHT: + g_value_set_double(value, witem->height); + break; + + case PROP_ANCHOR: + g_value_set_enum(value, witem->anchor); + break; + + case PROP_SIZE_PIXELS: + g_value_set_boolean(value, witem->size_pixels); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec); + break; + } +} + +static void +ganv_widget_update(GanvItem* item, double* affine, ArtSVP* clip_path, int flags) +{ + GanvWidget* witem; + + witem = GANV_WIDGET(item); + + if (parent_class->update) { + (*parent_class->update)(item, affine, clip_path, flags); + } + + if (witem->widget) { + if (witem->size_pixels) { + witem->cwidth = (int)(witem->width + 0.5); + witem->cheight = (int)(witem->height + 0.5); + } else { + witem->cwidth = (int)(witem->width * item->canvas->pixels_per_unit + 0.5); + witem->cheight = (int)(witem->height * item->canvas->pixels_per_unit + 0.5); + } + + gtk_widget_set_size_request(witem->widget, witem->cwidth, witem->cheight); + } else { + witem->cwidth = 0.0; + witem->cheight = 0.0; + } + + recalc_bounds(witem); +} + +static void +ganv_widget_draw(GanvItem* item, + GdkDrawable* drawable, + int x, int y, + int width, int height) +{ +#if 0 + GanvWidget* witem; + + witem = GANV_WIDGET(item); + + if (witem->widget) { + gtk_widget_queue_draw(witem->widget); + } +#endif +} + +static double +ganv_widget_point(GanvItem* item, double x, double y, + int cx, int cy, GanvItem** actual_item) +{ + GanvWidget* witem; + double x1, y1, x2, y2; + double dx, dy; + + witem = GANV_WIDGET(item); + + *actual_item = item; + + ganv_canvas_base_c2w(item->canvas, witem->cx, witem->cy, &x1, &y1); + + x2 = x1 + (witem->cwidth - 1) / item->canvas->pixels_per_unit; + y2 = y1 + (witem->cheight - 1) / item->canvas->pixels_per_unit; + + /* Is point inside widget bounds? */ + + if ((x >= x1) && (y >= y1) && (x <= x2) && (y <= y2)) { + return 0.0; + } + + /* Point is outside widget bounds */ + + if (x < x1) { + dx = x1 - x; + } else if (x > x2) { + dx = x - x2; + } else { + dx = 0.0; + } + + if (y < y1) { + dy = y1 - y; + } else if (y > y2) { + dy = y - y2; + } else { + dy = 0.0; + } + + return sqrt(dx * dx + dy * dy); +} + +static void +ganv_widget_bounds(GanvItem* item, double* x1, double* y1, double* x2, double* y2) +{ + GanvWidget* witem; + + witem = GANV_WIDGET(item); + + *x1 = witem->x; + *y1 = witem->y; + + switch (witem->anchor) { + case GTK_ANCHOR_NW: + case GTK_ANCHOR_W: + case GTK_ANCHOR_SW: + break; + + case GTK_ANCHOR_N: + case GTK_ANCHOR_CENTER: + case GTK_ANCHOR_S: + *x1 -= witem->width / 2.0; + break; + + case GTK_ANCHOR_NE: + case GTK_ANCHOR_E: + case GTK_ANCHOR_SE: + *x1 -= witem->width; + break; + + default: + break; + } + + switch (witem->anchor) { + case GTK_ANCHOR_NW: + case GTK_ANCHOR_N: + case GTK_ANCHOR_NE: + break; + + case GTK_ANCHOR_W: + case GTK_ANCHOR_CENTER: + case GTK_ANCHOR_E: + *y1 -= witem->height / 2.0; + break; + + case GTK_ANCHOR_SW: + case GTK_ANCHOR_S: + case GTK_ANCHOR_SE: + *y1 -= witem->height; + break; + + default: + break; + } + + *x2 = *x1 + witem->width; + *y2 = *y1 + witem->height; +} + +static void +ganv_widget_class_init(GanvWidgetClass* class) +{ + GObjectClass* gobject_class = (GObjectClass*)class; + GtkObjectClass* object_class = (GtkObjectClass*)class; + GanvItemClass* item_class = (GanvItemClass*)class; + + parent_class = g_type_class_peek_parent(class); + + gobject_class->set_property = ganv_widget_set_property; + gobject_class->get_property = ganv_widget_get_property; + + g_object_class_install_property( + gobject_class, PROP_WIDGET, g_param_spec_object( + "widget", _("Widget"), + _("The widget to embed in this item."), + GTK_TYPE_WIDGET, + (G_PARAM_READABLE | G_PARAM_WRITABLE))); + + g_object_class_install_property( + gobject_class, PROP_X, g_param_spec_double( + "x", _("x"), + _("The x coordinate of the anchor"), + -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, + G_PARAM_READWRITE)); + + g_object_class_install_property( + gobject_class, PROP_Y, g_param_spec_double( + "y", _("y"), + _("The x coordinate of the anchor"), + -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, + G_PARAM_READWRITE)); + + g_object_class_install_property( + gobject_class, PROP_WIDTH, g_param_spec_double( + "width", _("Width"), + _("The width of the widget."), + -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, + G_PARAM_READWRITE)); + + g_object_class_install_property( + gobject_class, PROP_HEIGHT, g_param_spec_double( + "height", _("Height"), + _("The height of the widget."), + -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, + G_PARAM_READWRITE)); + + g_object_class_install_property( + gobject_class, PROP_ANCHOR, g_param_spec_enum( + "anchor", _("Anchor"), + _("The anchor point of the widget."), + GTK_TYPE_ANCHOR_TYPE, + GTK_ANCHOR_NW, + G_PARAM_READWRITE)); + + g_object_class_install_property( + gobject_class, PROP_SIZE_PIXELS, g_param_spec_boolean( + "size-pixels", ("Size is in pixels"), + _("Specifies whether the widget size is specified in pixels or" + " canvas units. If it is in pixels, then the widget will not" + " be scaled when the canvas zoom factor changes. Otherwise," + " it will be scaled."), + FALSE, + G_PARAM_READWRITE)); + + object_class->destroy = ganv_widget_destroy; + + item_class->update = ganv_widget_update; + item_class->point = ganv_widget_point; + item_class->bounds = ganv_widget_bounds; + item_class->draw = ganv_widget_draw; +} |