Features/GTK3/Porting: Difference between revisions

Line 175: Line 175:
GTK-3 does not support gtk Drawable objects, so the first step is to get your activity running under Cairo.
GTK-3 does not support gtk Drawable objects, so the first step is to get your activity running under Cairo.


<pre>
  # From activity.Activity, you inherit a canvas. Get its window.
  # From activity.Activity, you inherit a canvas. Get its window.
  win = self.canvas.get_window()
  win = self.canvas.get_window()
Line 199: Line 200:
  cr.fill()
  cr.fill()


  # To invalidate a region to forse a refresh, use:
  # To invalidate a region to force a refresh, use:
  self.canvas.queue_draw_area(x, y, w, h)
  self.canvas.queue_draw_area(x, y, w, h)
</pre>
Pango is a bit different when used with Cairo:
<pre>
# Again, from the xlib_surface...
cr = cairo.Context(xlib_surface)
# Create a PangoCairo context
cr = pangocairo.CairoContext(cr)
# The pango layout is created from the Cairo context
pl = cr.create_layout()
# You still use pango to set up font descriptions.
fd = pango.FontDescription('Sans')
fd.set_size(12 * pango.SCALE)


# Tell your pango layout about your font description
pl.set_font_description(fd)


   
  # Write text to your pango layout
pl.set_text('Hello world')
 
# Position it within the Cairo context
cr.save()
cr.translate(x, y)
cr.rotate(pi / 3)  # You can rotate text and images in Cairo
cr.set_source_rgb(1, 0, 0)
 
# Finally, draw the text
cr.update_layout(pl)
cr.show_layout(pl)
cr.restore()
</pre>
 
To draw a bitmap...
 
<pre>
# Again, from the xlib_surface...
cr = cairo.Context(xlib_surface)
 
# Create a gtk context
cr = gtk.gdk.CairoContext(cr)
cr.set_source_pixbuf(pixbuf, x, y)
cr.rectangle(x, y, w, h)
cr.fill()
</pre>
 
To read a pixel...
 
<pre>
# Map the xlib surface onto a pixmap
pixmap = gtk.gdk.Pixmap(None, w, h, 24)
cr = pixmap.cairo_create()
cr.set_source_surface(xlib_surface, 0, 0)
cr.paint()
 
# Read the pixel
pixel = pixmap.get_image(x, y, 1, 1).get_pixel(0, 0)
return(int((pixel & 0xFF0000) >> 16),  # red
        int((pixel & 0x00FF00) >> 8),  # green
        int((pixel & 0x0000FF) >> 0), 0)  # blue
</pre>


:* Example: abacus-cairo
===Going from Cairo in GTK-2 to Cairo in GTK-3===
:* Example: abacus-gtk3
* The conversion script (here) leaves a few things to be done by hand:
:* Notes from conversion from abacus-cairo to abacus-gtk3


* Notes from the TurtleArt conversion
Not much changes, but...
* Removing Hippo


===Taking a screenshot and making a thumbnail===
===Taking a screenshot and making a thumbnail===