def test_pbosf_get_rect(self): sf = cairo.ImageSurface(cairo.FORMAT_RGB24, 10, 11) self.assertEqual(pbosf_get_rect(sf), (0, 0, 10, 11)) pixbuf = GdkPixbuf.Pixbuf.new(GdkPixbuf.Colorspace.RGB, True, 8, 4, 5) self.assertEqual(pbosf_get_rect(pixbuf), (0, 0, 4, 5))
def create_multi_row_drag_icon(self, paths, max_rows): """Similar to create_row_drag_icon() but creates a drag icon for multiple paths or None. The resulting surface will draw max_rows rows and point out if there are more rows selected. """ if not paths: return if len(paths) == 1: return self.create_row_drag_icon(paths[0]) # create_row_drag_icon can return None icons = [self.create_row_drag_icon(p) for p in paths[:max_rows]] icons = [i for i in icons if i is not None] if not icons: return sizes = [pbosf_get_rect(s) for s in icons] if None in sizes: return width = max([s[2] for s in sizes]) height = sum([s[3] for s in sizes]) # this is the border width we see in the gtk provided surface, not # much we can do besides hardcoding it here bw = 1 layout = None if len(paths) > max_rows: more = _(u"and %d more…") % (len(paths) - max_rows) more = "<i>%s</i>" % more layout = self.create_pango_layout("") layout.set_markup(more) layout.set_alignment(Pango.Alignment.CENTER) layout.set_width(Pango.SCALE * (width - 2 * bw)) lw, lh = layout.get_pixel_size() height += lh height += 6 # padding surface = icons[0].create_similar( cairo.CONTENT_COLOR_ALPHA, width, height) ctx = cairo.Context(surface) # render background style_ctx = self.get_style_context() Gtk.render_background(style_ctx, ctx, 0, 0, width, height) # render rows count_y = 0 for icon, (x, y, icon_width, icon_height) in zip(icons, sizes): ctx.save() ctx.set_source_surface(icon, -x, count_y + -y) ctx.rectangle(bw, count_y + bw, icon_width - 2 * bw, icon_height - 2 * bw) ctx.clip() ctx.paint() ctx.restore() count_y += icon_height if layout: Gtk.render_layout(style_ctx, ctx, bw, count_y, layout) # render border Gtk.render_line(style_ctx, ctx, 0, 0, 0, height - 1) Gtk.render_line(style_ctx, ctx, 0, height - 1, width - 1, height - 1) Gtk.render_line(style_ctx, ctx, width - 1, height - 1, width - 1, 0) Gtk.render_line(style_ctx, ctx, width - 1, 0, 0, 0) return surface