Proposal: how about a gtk_canvas widget?

One thing I've always liked about Tk is the canvas widget.

For those who haven't used it, it's a widget which allows you to add
simple graphical elements (lines, arcs, polygons, text etc.) to it, as
well as widgets.  Each element on the canvas has a unique ID, and
elements' attributes (geometry, colour, line width...) can be updated
by their ID.  It's also possible to tag a whole bunch of elements and
update elements by tag.  Each element can also respond to various
events - mouse & keyboard events, mainly.

I've been toying with the idea of constructing a Gtk version of this
canvas widget for a little while now.  If you couple the above features
with some of the nice ideas from Qt's QPainter, like co-ordinate
transformations, you could end up with a fairly flexible and powerful
object-based drawing surface.

I haven't done any "serious" Gtk programming yet (but I think it's
about time I tried :-), and I'm still totally uncertain about all the
implementation issues, but I'd be keen to hear ideas if anyone thinks
it's worth a go...

Some initial thoughts on the API:

  GtkWidget *gtk_canvas_new(); 
  gint gtk_canvas_create(GtkCanvas *canvas, GtkCanvasItemType type, 
                         gint x, gint y, gint width, gint height, ...);
  void gtk_canvas_item_config(GtkCanvas *canvas, gint item, ...);
  void gtk_canvas_tag_config(GtkCanvas *canvas, gint tag, ...);

The argument list here might be best expressed as a list of key/value
pairs, otherwise you'd end up with a proliferation of routines to
create & modify items.  E.g.:

  GdkColor red; /* initialise this somewhere */

  canvas = gtk_canvas_new();
  gtk_widget_set_usize(canvas, 200, 200);
  line = gtk_canvas_create(canvas, GTK_CANVAS_ITEM_LINE, 50, 50, 50, 50,
  gtk_canvas_item_config(canvas, line, GTK_CANVAS_ITEM_X, 70);

Of course, this leads to a proliferation of (rather long-winded)
constants.  Hmm.  Maybe the constants should just be strings, like
signal names.

There are of course many many other useful routines which could be
implemented - just see the Tcl/Tk canvas man page.

Thoughts, comments?

Des Herriott

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]