def prepare_stringed (self, rows, cols): # We use a Pixmap as offscreen drawing canvas cm = Gdk.colormap_get_system() pm = Gdk.Pixmap(None, self.width, self.height, cm.get_visual().depth) #pangolayout = pm.create_pango_layout("") font_size = int(self.width / cols / 4) l = Gtk.Label() pangolayout = Pango.Layout(l.create_pango_context()) pangolayout.set_font_description(Pango.FontDescription("sans bold %i" % font_size)) gc = pm.new_gc() gc.set_colormap(Gdk.colormap_get_system()) color = cm.alloc_color('white') gc.set_foreground(color) pm.draw_rectangle(gc, True, 0, 0, self.width, self.height) color = cm.alloc_color('black') gc.set_foreground(color) sw, sh = (self.width / cols), (self.height / rows) item = iter(self.tlist) for r in range(rows): for c in range(cols): px = sw * c py = sh * r #if c > 0 and r > 0: # pm.draw_line(gc, px, 0, px, self.height-1) # pm.draw_line(gc, 0, py, self.width-1, py) pangolayout.set_text(str(item.next())) pe = pangolayout.get_pixel_extents() pe = pe[1][2]/2, pe[1][3]/2 pm.draw_layout(gc, px + (sw / 2) - pe[0], py + (sh / 2) - pe[1], pangolayout) self.get_from_drawable(pm, cm, 0, 0, 0, 0, -1, -1)
def __init__(self): self.statusicon = Gtk.StatusIcon() self.counter = 0 self.statusicon.connect("activate", self.quit_cb) self.statusicon.connect("popup-menu", self.quit_cb) self.statusicon.set_tooltip_text("StatusIcon Example") #generate tray image: s = 64 w, h = s * 2, s * 2 pixmap = Gdk.Pixmap(Gdk.get_default_root_window(), w, h) cr = pixmap.cairo_create() cr.set_operator(cairo.OPERATOR_CLEAR) cr.fill() cr.set_operator(cairo.OPERATOR_SOURCE) for i, color in enumerate([(1, 0, 0, 1), (0, 1, 0, 1), (0, 0, 1, 1)]): cr.set_source_rgba(*color) cr.new_path() x = (i % 2) * s y = (i / 2) * s cr.move_to(x, y) cr.line_to(x + s, y) cr.line_to(x + s, y + s) cr.line_to(x, y + s) cr.close_path() cr.fill() pixbuf = GdkPixbuf(Gdk.COLORSPACE_RGB, True, 8, w, h) pixbuf.get_from_drawable(pixmap, pixmap.get_colormap(), 0, 0, 0, 0, w, h) self.statusicon.set_from_pixbuf(pixbuf)
def do_size_allocate(self, allocation): """ Applies the non-rectangular shape """ width, height = allocation.width, allocation.height mask = Gdk.Pixmap(None, width, height, 1) context = mask.cairo_create() context.set_source_rgb(0, 0, 0) context.set_operator(cairo.OPERATOR_CLEAR) context.paint() radius = self.__options['border_radius'] inner = (radius, radius, width - radius, height - radius) context.set_source_rgb(1, 1, 1) context.set_operator(cairo.OPERATOR_SOURCE) # Top left corner context.arc(inner.x, inner.y, radius, 1.0 * pi, 1.5 * pi) # Top right corner context.arc(inner.width, inner.y, radius, 1.5 * pi, 2.0 * pi) # Bottom right corner context.arc(inner.width, inner.height, radius, 0.0 * pi, 0.5 * pi) # Bottom left corner context.arc(inner.x, inner.height, radius, 0.5 * pi, 1.0 * pi) context.fill() self.shape_combine_mask(mask, 0, 0) Gtk.Window.do_size_allocate(self, allocation)
def colored_cursor(size=64, x=32, y=32): #create a custom cursor: #first get a colormap and then allocate the colors #black and white for drawing on the bitmaps: pm = Gdk.Pixmap(None, size, size, 1) mask = Gdk.Pixmap(None, size, size, 1) colormap = Gdk.colormap_get_system() black = colormap.alloc_color('black') white = colormap.alloc_color('white') # Create two GCs - one each for black and white: bgc = pm.new_gc(foreground=black) wgc = pm.new_gc(foreground=white) # Use the black gc to clear the pixmap and mask: mask.draw_rectangle(bgc,True,0,0,size,size) pm.draw_rectangle(bgc,True,0,0,size,size) # Use the white gc to set the bits in the pixmap and mask: pm.draw_arc(wgc,True,0,2,size,size/2,0,360*64) mask.draw_rectangle(wgc,True,0,0,size,size) # Then create and set the cursor using unallocated colors: green = Gdk.color_parse('green') red = Gdk.color_parse('red') return Gdk.Cursor(pm,mask,green,red, x, y)
def on_realize(self, entry): layout = self.get_layout().copy() layout.set_markup("<span foreground='dark gray'>%s</span>" % self.default_text) extents = layout.get_pixel_extents()[1] drawable = Gdk.Pixmap(self.get_parent_window(), extents[2], extents[3]) gc = Gdk.GC(drawable) gc2 = entry.props.style.base_gc[0] drawable.draw_rectangle(gc2, True, *extents) drawable.draw_layout(gc, 0, 0, layout) pixbuf = GdkPixbuf.Pixbuf(Gdk.COLORSPACE_RGB, True, 8, extents[2], extents[3]) pixbuf.get_from_drawable(drawable, drawable.get_colormap(), 0, 0, *extents) self.empty_pixbuf = pixbuf if not Gtk.Entry.get_text(self): self.props.primary_icon_pixbuf = pixbuf
def __init__(self, width, height): gdk_root = Gdk.get_default_root_window() self._gdk_pixmap = Gdk.Pixmap(gdk_root, width, height)