Features/GTK3/Porting: Difference between revisions
| Line 176: | Line 176: | ||
<pre> | <pre> | ||
# From activity.Activity, you inherit a canvas. Get its window. | |||
win = self.canvas.get_window() | |||
# Create a Cairo context from the window. | |||
cairo_context = win.cairo_create() | |||
# Create a Cairo surface from the context | |||
surface = cairo_context.get_target() | |||
# Create an XLib surface to be used for drawing | |||
xlib_surface = surface.create_similar(cairo.CONTENT_COLOR, | |||
gtk.gdk.screen_width(), | |||
gtk.gdk.screen_height()) | |||
# You'll need a Cairo context from which you'll build a GTK Cairo context | |||
cairo_context = cairo.Context(xlib_surface) | |||
cairo_context = gtk.gdk.CairoContext(cairo_context) | |||
# Use this context as you would a Drawable, substituting Cairo commands | |||
# for gtk commands, e.g., | |||
cairo_context.move_to(0, 0) | |||
cairo_context.line_to(100, 100) | |||
# Cairo uses floats from 0 to 1 for RGB values | |||
cairo_context.set_source_rgb(r, g, b) | |||
cairo_context.rectangle(x, y, w, h) | |||
cairo_context.fill() | |||
# To invalidate a region to force a refresh, use: | |||
self.canvas.queue_draw_area(x, y, w, h) | |||
</pre> | </pre> | ||
Pango is a bit different when used with Cairo: | Pango is a bit different when used with Cairo: | ||
<pre> | <pre> | ||
# Again, from the xlib_surface... | |||
cairo_context = cairo.Context(xlib_surface) | |||
# Create a PangoCairo context | |||
cairo_context = pangocairo.CairoContext(cairo_context) | |||
# The pango layout is created from the Cairo context | |||
pango_layout = cairo_context.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 | |||
pango_layout.set_font_description(fd) | |||
# Write text to your pango layout | |||
pango_layout.set_text('Hello world') | |||
# Position it within the Cairo context | |||
cairo_context.save() | |||
cairo_context.translate(x, y) | |||
cairo_context.rotate(pi / 3) # You can rotate text and images in Cairo | |||
cairo_context.set_source_rgb(1, 0, 0) | |||
# Finally, draw the text | |||
cairo_context.update_layout(pango_layout) | |||
cairo_context.show_layout(pango_layout) | |||
cairo_context.restore() | |||
</pre> | </pre> | ||
| Line 241: | Line 244: | ||
<pre> | <pre> | ||
# Again, from the xlib_surface... | |||
cairo_context = cairo.Context(xlib_surface) | |||
# Create a gtk context | |||
cairo_context = gtk.gdk.CairoContext(cairo_context) | |||
cairo_context.set_source_pixbuf(pixbuf, x, y) | |||
cairo_context.rectangle(x, y, w, h) | |||
cairo_context.fill() | |||
</pre> | </pre> | ||
| Line 254: | Line 257: | ||
<pre> | <pre> | ||
# Map the xlib surface onto a pixmap | |||
pixmap = gtk.gdk.Pixmap(None, w, h, 24) | |||
cairo_context = pixmap.cairo_create() | |||
cairo_context.set_source_surface(xlib_surface, 0, 0) | |||
cairo_context.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> | </pre> | ||
| Line 273: | Line 276: | ||
The Cairo/Pango interaction is a little different: | The Cairo/Pango interaction is a little different: | ||
<pre> | <pre> | ||
cairo_context = ... | |||
pango_layout = PangoCairo.create_layout(cairo_context) | |||
pango_layout.set_font_description(fd) | |||
pango_layout.set_text('Hello World', -1) | |||
cairo_context.set_source_rgb(1, 0, 0) | |||
PangoCairo.update_layout(cairo_context, pango_layout) | |||
PangoCairo.show_layout(cairo_context, pango_layout) | |||
</pre> | </pre> | ||