Exemple #1
0
    def draw(self, cairocontext):
        alloc = self._widget.get_allocation()
        padding = 5
        x = self._area_x - alloc.x + padding
        y = self._area_y - alloc.y + padding
        width = max(0, self._area_width - 2 * padding)
        height = max(0, self._area_height - 2 * padding)

        if width == 0 or height == 0:
            return

        stylecontext = self._widget.get_style_context()
        state = stylecontext.get_state()
        if self._widget.is_focus():
            state |= Gtk.StateFlags.SELECTED
        if self._prelit:
            state |= Gtk.StateFlags.PRELIGHT

        if Gtk.cairo_should_draw_window(cairocontext, self._window):
            stylecontext.save()
            stylecontext.set_state(state)
            stylecontext.add_class(Gtk.STYLE_CLASS_PANE_SEPARATOR)
            color = stylecontext.get_background_color(state)
            if color.alpha > 0.0:
                Gtk.render_handle(stylecontext, cairocontext, x, y, width,
                                  height)
            else:
                xcenter = x + width / 2.0
                Gtk.render_line(stylecontext, cairocontext, xcenter, y,
                                xcenter, y + height)
            stylecontext.restore()
Exemple #2
0
    def draw(self, cairocontext):
        alloc = self._widget.get_allocation()
        padding = 5
        x = self._area_x - alloc.x + padding
        y = self._area_y - alloc.y + padding
        width = max(0, self._area_width - 2 * padding)
        height = max(0, self._area_height - 2 * padding)

        if width == 0 or height == 0:
            return

        stylecontext = self._widget.get_style_context()
        state = self._widget.get_state_flags()
        if self._widget.is_focus():
            state |= Gtk.StateFlags.SELECTED
        if self._prelit:
            state |= Gtk.StateFlags.PRELIGHT

        if Gtk.cairo_should_draw_window(cairocontext, self._window):
            stylecontext.save()
            stylecontext.set_state(state)
            stylecontext.add_class(Gtk.STYLE_CLASS_PANE_SEPARATOR)
            color = stylecontext.get_background_color(state)
            if color.alpha > 0.0:
                Gtk.render_handle(stylecontext, cairocontext,
                                  x, y, width, height)
            else:
                xcenter = x + width / 2.0
                Gtk.render_line(stylecontext, cairocontext,
                                xcenter, y, xcenter, y + height)
            stylecontext.restore()
Exemple #3
0
 def draw_rule(self, cr):
     cr.save()
     
     horizontal = self.orientation == Gtk.Orientation.HORIZONTAL
     
     sc = self.get_style_context()
     alloc = self.get_allocation()
     
     orient, subdivs = self.orientation, self.subdivisions
     
     ruler_fg = sc.get_color(Gtk.StateFlags.NORMAL)
     Gdk.cairo_set_source_rgba(cr, ruler_fg)
     
     cr.set_line_width(1.0)
     
     if horizontal:
         span = alloc.width
     else:
         span = alloc.height
     
     # prevents a bit of code duplication
     flip_if_vertical = lambda x, y: (x, y) if horizontal else (y, x)
     
     for tick in range(0, span + 1, self.major_ticks_spacing):
         tick_start = flip_if_vertical(tick + 0.5, self.size)
         tick_end = flip_if_vertical(0, -(7 * self.size) // 9 - 0.5)
         
         cr.move_to(*tick_start)
         cr.rel_line_to(*tick_end)
         
         for subtick in range(1, subdivs):
             spacing = (subtick * self.major_ticks_spacing) // subdivs
             
             if subdivs % 2 == 0 and subtick == subdivs // 2:
                 subtick_size = self.size // 2
             else:
                 subtick_size = self.size // 4
             
             subtick_start = flip_if_vertical(tick + spacing + 0.5, self.size)
             subtick_end = flip_if_vertical(0, -subtick_size - 0.5)
             
             cr.move_to(*subtick_start)
             cr.rel_line_to(*subtick_end)
         
     cr.stroke()
     
     end = alloc.width if horizontal else alloc.height
     coords_start = flip_if_vertical(0, self.size - 1)
     coords_end = flip_if_vertical(end, self.size - 1)
     
     Gtk.render_line(sc, cr, *(coords_start + coords_end))
     
     cr.restore()
Exemple #4
0
    def do_render(self, cr, widget, background_area, cell_area, flags):
        x_offset, y_offset, width, height = self.do_get_size(widget, cell_area)

        # Center the label
        y_offset = cell_area.height / 2 - height / 2

        # Draws label
        context = widget.get_style_context()
        Gtk.render_layout(context, cr, cell_area.x + x_offset,
                          cell_area.y + y_offset, self._label_layout)
        if not self._details_callback:
            return

        Gtk.render_line(context, cr, cell_area.x, cell_area.y,
                        cell_area.x + cell_area.width,
                        cell_area.y + cell_area.height - 1)
Exemple #5
0
    def create_multi_row_drag_icon(self, paths, max_rows):

        if not paths:
            return

        if len(paths) == 1:
            return self.oTreeviev.create_row_drag_icon(paths[0])

        # create_row_drag_icon can return None
        icons = [self.oTreeviev.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

        # Gives (x, y, width, height) for a pixbuf or a surface, scale independent
        sizes = []

        for oIcon in icons:

            if isinstance(oIcon, GdkPixbuf.Pixbuf):

                sizes.append((0, 0, oIcon.get_width(), oIcon.get_height()))

            else:

                ctx = cairo.Context(oIcon)
                x1, y1, x2, y2 = ctx.clip_extents()
                x1 = int(math.floor(x1))
                y1 = int(math.floor(y1))
                x2 = int(math.ceil(x2))
                y2 = int(math.ceil(y2))
                x2 -= x1
                y2 -= y1

                sizes.append((x1, y1, x2, y2))
        #~Gives (x, y, width, height) for a pixbuf or a surface, scale independent

        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 = _('and {rowcount} more').format(rowcount = len(paths) - max_rows)
            more = "<b><i>%s</i></b>" % more
            layout = self.oTreeviev.create_pango_layout("")
            layout.set_markup(more)
            layout.set_alignment(Pango.Alignment.LEFT)
            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.oTreeviev.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
Exemple #6
0
    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 = [get_surface_extents(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
    def do_draw(self, cr):
        cr.save()

        window = self.get_window()
        w = window.get_width()
        h = window.get_height()

        points_per_set = (len(self.data_array) / self.num_sets)
        pixels_per_point = (float(w) /
                            (float((points_per_set - 1) or 1)))

        widget = self
        ctx = widget.get_style_context()

        # This draws the light gray backing rectangle
        Gtk.render_background(ctx, cr, 0, 0, w - 1, h - 1)

        # This draws the marker ticks
        max_ticks = 4
        for index in range(1, max_ticks):
            Gtk.render_line(ctx, cr, 1,
                            (h / max_ticks) * index,
                            w - 2,
                            (h / max_ticks) * index)

        # Foreground-color graphics context
        # This draws the black border
        Gtk.render_frame(ctx, cr, 0, 0, w - 1, h - 1)

        # Draw the actual sparkline
        def get_y(dataset, index):
            baseline_y = h

            n = dataset * points_per_set
            if self.reversed:
                n += (points_per_set - index - 1)
            else:
                n += index

            val = self.data_array[n]
            return baseline_y - ((h - 1) * val)

        cr.set_line_width(2)

        for dataset in range(0, self.num_sets):
            if len(self.rgb) == (self.num_sets * 3):
                cr.set_source_rgb(self.rgb[(dataset * 3)],
                                        self.rgb[(dataset * 3) + 1],
                                        self.rgb[(dataset * 1) + 2])
            points = []
            for index in range(0, points_per_set):
                x = index * pixels_per_point
                y = get_y(dataset, index)

                points.append((int(x), int(y)))


            if self.num_sets == 1:
                pass

            draw_line(cr, 0, 0, w, h, points)
            if self.filled:
                # Fixes a fully filled graph from having an oddly
                # tapered in end (bug 560913). Need to figure out
                # what's really going on.
                points = [(0, h)] + points
                draw_fill(cr, 0, 0, w, h, points, taper=True)

        cr.restore()

        return 0
Exemple #8
0
    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 = [_get_surface_size(s) for s in icons]
        if None in sizes:
            return
        width = max([s[0] for s in sizes])
        height = sum([s[1] for s in sizes])

        layout = None
        if len(paths) > max_rows:
            more = _("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))
            lw, lh = layout.get_pixel_size()
            height += lh
            height += 6  # padding

        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 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, (icon_width, icon_height) in zip(icons, sizes):
            ctx.save()
            ctx.set_source_surface(icon, 2, count_y + 2)
            ctx.rectangle(2, count_y + 2, icon_width - 4, icon_height - 4)
            ctx.clip()
            ctx.paint()
            ctx.restore()
            count_y += icon_height

        if layout:
            Gtk.render_layout(style_ctx, ctx, 1, 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
Exemple #9
0
    def do_draw(self, cr):
        cr.save()

        window = self.get_window()
        w = window.get_width()
        h = window.get_height()

        points_per_set = (len(self.data_array) // self.num_sets)
        pixels_per_point = (float(w) / (float((points_per_set - 1) or 1)))

        widget = self
        ctx = widget.get_style_context()

        # This draws the light gray backing rectangle
        Gtk.render_background(ctx, cr, 0, 0, w - 1, h - 1)

        # This draws the marker ticks
        max_ticks = 4
        for index in range(1, max_ticks):
            Gtk.render_line(ctx, cr, 1, (h // max_ticks) * index, w - 2,
                            (h // max_ticks) * index)

        # Foreground-color graphics context
        # This draws the black border
        Gtk.render_frame(ctx, cr, 0, 0, w - 1, h - 1)

        # Draw the actual sparkline
        def get_y(dataset, index):
            baseline_y = h

            n = dataset * points_per_set
            if self.reversed:
                n += (points_per_set - index - 1)
            else:
                n += index

            val = self.data_array[n]
            return baseline_y - ((h - 1) * val)

        cr.set_line_width(2)

        for dataset in range(0, self.num_sets):
            if len(self.rgb) == (self.num_sets * 3):
                cr.set_source_rgb(self.rgb[(dataset * 3)],
                                  self.rgb[(dataset * 3) + 1],
                                  self.rgb[(dataset * 1) + 2])
            points = []
            for index in range(0, points_per_set):
                x = index * pixels_per_point
                y = get_y(dataset, index)

                points.append((int(x), int(y)))

            if self.num_sets == 1:
                pass

            draw_line(cr, 0, 0, w, h, points)
            if self.filled:
                # Fixes a fully filled graph from having an oddly
                # tapered in end (bug 560913). Need to figure out
                # what's really going on.
                points = [(0, h)] + points
                draw_fill(cr, 0, 0, w, h, points, taper=True)

        cr.restore()

        return 0