Re: [pygtk] pygtk - gtk Drawing Area - need help!

2001-10-08 Thread Tjabo Kloppenburg

hi martin!

  Is there sample code somewhere, or a geek who knows all this? :-)
 Maybe here's a geek who knows these things... :)
 ...

great - I'll try this out!


graçias!
  tk.

-- 
+ Tjabo Kloppenburg + taponet.de + de,en +
___
pygtk mailing list   [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk



Re: [pygtk] pygtk - gtk Drawing Area - need help!

2001-10-07 Thread Martin Grimme

 I need help on how to use a drawingarea - how to draw colored
 lines/circles, how to clear the area, and so on.

 Is there sample code somewhere, or a geek who knows all this? :-)

Maybe here's a geek who knows these things... :)

OK, after you created a GtkDrawingArea, you should connect the
expose-event to it. This event occurs every time parts of the drawing
area have to be repainted.

The callback function for the event gets 2 parameters:
 1. the GtkDrawingArea object
 2. an event structure (which tells you useful things about the event)

To actually draw on the drawing area, you need a GdkGC object.
A GC is a graphics context and encapsulates settings for drawing.
You would, for example, change the drawing color in the GC.
You should also know that each window has its own GC (so if you have 2
drawing areas, you would usually need 2 GCs).

There is a difference between GtkWindows and GdkWindows. While the
GtkWindow is a GtkWidget, many widgets do have their own GdkWindows.
To get the GC of the drawing area, you call the new_gc() method of its
GdkWindow. Luckily, the event structure provides you with this GdkWindow
(it is also possible to get the GdkWindow of a _realized_ widget by calling
get_window() on that widget).

OK, now that you have the GC, you want to set a drawing color.
Using the colormap of the GdkDrawingArea, creating a GdkColor is as simple
as this:

  new_color = drawingarea.get_colormap().alloc(r, g, b)

where r, g, b are the RGB values of the color (ranging from 0 to 65535).

You could also use X-color-names:

  new_color = drawingarea.get_colormap().alloc(green)


Finally, we can draw on the drawing area:

  drawingarea.draw_line(gc, x1, y1, x2, y2)


Here's tiny working example of all this stuff:

--

  from gtk import *

  def _on_expose(drawingarea, event):
  # get the GdkWindow
  window = event.window

  # get the GC
  gc = window.new_gc()

  # create a green color
  color = drawingarea.get_colormap().alloc(0x, 0x, 0x)

  # use this color for drawing
  gc.foreground = color

  # draw a line
  drawingarea.draw_line(gc, 0, 0, 200, 200)

  
  # the usual stuff
  win = GtkWindow()
  win.connect(destroy, mainquit)
  win.show()

  # create the drawing area
  drawingarea = GtkDrawingArea()
  drawingarea.connect(expose-event, _on_expose)
  drawingarea.show()
  win.add(drawingarea)

  # enter the mainloop
  mainloop()

---


This example always draws the complete line whenever a part has to be
redrawn. You could only redraw the parts which need to be redrawn by
looking at the area variable (a rectangle) of the event structure.

To clear the area, you draw a filled rectangle over it:
  drawingarea.draw_rectangle(gc, TRUE, 0, 0, window.width, window.height)

However, if you seriously want to make use of the drawing area, then
you should create an off-screen pixmap where to draw on. And the expose
handler just copies this pixmap into the drawing area.

If you need more description and examples, you may contact me. The
Gdk-API Reference is also useful for exploring the GC and drawing
primitives. Not everything is implemented in PyGTK, though :(


Bye, Martin Grimme --- http://www.pycage.de
___
pygtk mailing list   [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk