def write_text(self): if not self.text: # Nothing useful to draw, return now! return if self.resized( ) or self.text != self.prev_text or self.text_overlay is None: # Need to recreate the cache drawing self.text_overlay = ImageSurface(FORMAT_ARGB32, self.width, self.height) ctx = Context(self.text_overlay) pg = CairoContext(ctx) pl = pg.create_layout() pl.set_font_description(self.text_font) pl.set_width(-1) # No text wrapping pl.set_text(self.text) plsize = pl.get_size() text_width = plsize[0] // SCALE text_height = plsize[1] // SCALE area_width_without_text = self.width - text_width area_height_without_text = self.height - text_height ctx.move_to(area_width_without_text // 2, area_height_without_text // 2) ctx.set_source_rgb(1, 1, 1) pg.update_layout(pl) pg.show_layout(pl) self.cr.set_source_surface(self.text_overlay) self.cr.paint()
def on_render(self, window, widget, background_area, cell_area, expose_area, flags): cr = window.cairo_create() # Check if a state icon should be rendered. stock = self.get_property("stock-id") if stock: # ICON_SIZE_MENU is the default size specified in the GTK sources. pixbuf = widget.render_icon(stock, gtk.ICON_SIZE_MENU) width, height = pixbuf.get_width(), pixbuf.get_height() cr.set_source_pixbuf( pixbuf, expose_area.x + round((expose_area.width - width + 0.5) / 2), expose_area.y + round((expose_area.height - height + 0.5) / 2)) cr.rectangle(*expose_area) cr.fill() else: # Render active resp. inactive rows. layout = self.__get_layout(widget) width, height = layout.get_pixel_size() layout.set_width((expose_area.width + expose_area.x) * pango.SCALE) layout.set_ellipsize(pango.ELLIPSIZE_END) layout.set_font_description(widget.get_style().font_desc) style = widget.get_style() if (flags == (gtk.CELL_RENDERER_SELECTED | gtk.CELL_RENDERER_PRELIT) or flags == gtk.CELL_RENDERER_SELECTED): color = style.text[gtk.STATE_SELECTED] else: color = style.text[gtk.STATE_NORMAL] cr.set_source_color(color) pc_context = CairoContext(cr) if width < expose_area.width: x = (expose_area.x + round((expose_area.width - width + 0.5) / 2)) else: x = expose_area.x pc_context.move_to(x, expose_area.y + round((expose_area.height - height + 0.5) / 2)) pc_context.show_layout(layout)
def renderText(self, surface, text, y_offset, size, shade, w=(2,3), wrap=True): if len(text) < 1: return def setdesc(l, size): l.set_font_description( FontDescription( self.font + " " + str(size))) origin = Context(surface) origin.translate(self.w * (w[1] - w[0]) / (w[1] * 2), y_offset) box = CairoContext(origin) # Start laying out text layout = box.create_layout() setdesc(layout, size) width = self.w * w[0] / w[1] if wrap: layout.set_width(width * pango.SCALE) else: layout.set_width(-1) layout.set_alignment(pango.ALIGN_CENTER) layout.set_text(text) # Resize text to make sure it doesn't exceed width. wi, n = layout.get_pixel_size() if wi > width: s = size * width / wi setdesc(layout, s) layout.set_width(width * pango.SCALE) # Draw a transparent pane behind the text origin.set_source_rgba(1, 1, 1, 0.7) origin.rectangle(*layout.get_pixel_extents()[1]) origin.fill() # Draw text origin.set_source_rgb(shade, shade, shade) box.update_layout(layout) box.show_layout(layout)