[pygtk] glib.py

2001-10-07 Thread Christian Robottom Reis


Johan has proposed we have a separate module that wraps just the glib
functions (though they still go through _gtkmodule.o). This speeds up
importing them quite a bit, but is only useful for people that want a
mainloop for something -- which is our case, for o-p.

James, what would the proper approach to this be?

Take care,
--
Christian Reis, Senior Engineer, Async Open Source, Brazil.
http://async.com.br/~kiko/ | [+55 16] 272 3330 | NMFL



import _gtk

TRUE = 1
FALSE = 0
def mainloop ():
_gtk.gtk_main ()

def mainquit (*args):
_gtk.gtk_main_quit ()

def mainiteration (block=TRUE):
return _gtk.gtk_main_iteration (block)

def events_pending ():
return _gtk.gtk_events_pending ()

def idle_add (callback, *args):
return _gtk.gtk_idle_add (callback, args)

def idle_add_priority (priority, callback):
return _gtk.gtk_idle_add_priority (priority, callback)

def idle_remove (tag):
_gtk.gtk_idle_remove (tag)

def quit_add (mainlevel, callback, *args):
return _gtk.gtk_quit_add (mainlevel, callback, args)

def quit_add_destroy (mainlevel, object):
_gtk.gtk_quit_add_destroy (mainlevel, object._o)

def quit_remove (tag):
_gtk.gtk_quit_remove (tag)

def timeout_add (timeout, callback, *args):
return _gtk.gtk_timeout_add (timeout, callback, args)

def timeout_remove (tag):
_gtk.gtk_timeout_remove (tag)

def input_add (source, condition, callback):
if hasattr (source, 'fileno'):
# handle python file handles
def wrapper (source, condition, real_s=source,real_cb=callback):
real_cb (real_s, condition)
return _gtk.gtk_input_add (source.fileno (), condition, wrapper)
else:
return _gtk.gtk_input_add (source, condition, callback)

def input_remove (tag):
_gtk.gtk_input_remove (tag)




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

2001-10-07 Thread Tom Cato Amundsen

Can someone port this do pygtk2? Is enough functions wrapped
yet to do this?

> --
> 
>   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()
> 
___
pygtk mailing list   [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk



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

2001-10-07 Thread Tjabo Kloppenburg

hi, 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? :-)

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