Beispiel #1
0
def pbosf_render(style_context, cairo_context, pbosf, x, y):
    """Draws the PixbufOrSurface to the cairo context at (x, y)"""

    if isinstance(pbosf, GdkPixbuf.Pixbuf):
        Gtk.render_icon(style_context, cairo_context, pbosf, x, y)
    else:
        Gtk.render_icon_surface(style_context, cairo_context, pbosf, x, y)
Beispiel #2
0
    def do_draw(self, context, background_area, cell_area, start, end, state):
        GtkSource.GutterRendererPixbuf.do_draw(
            self, context, background_area, cell_area, start, end, state)
        if self.is_action:
            if Gtk.get_minor_version() < 20:
                style_context = get_style(None, "GtkButton.flat.image-button")
                style_context.add_class(Gtk.STYLE_CLASS_BUTTON)
                style_context.add_class(Gtk.STYLE_CLASS_FLAT)
            else:
                # TODO: Fix padding and min-height in CSS and use
                # draw_style_common
                style_context = get_style(None, "button.flat.image-button")
            style_context.set_state(renderer_to_gtk_state(state))

            x = background_area.x + 1
            y = background_area.y + 1
            width = background_area.width - 2
            height = background_area.height - 2

            Gtk.render_background(style_context, context, x, y, width, height)
            Gtk.render_frame(style_context, context, x, y, width, height)

            pixbuf = self.props.pixbuf
            pix_width, pix_height = pixbuf.props.width, pixbuf.props.height
            Gtk.render_icon(
                style_context, context, pixbuf,
                x + (width - pix_width) // 2,
                y + (height - pix_height) // 2)

        self.draw_chunks(
            context, background_area, cell_area, start, end, state)
Beispiel #3
0
    def calculate_positions(self, ctx, widget, x_base=0, y_base=0, width=0):
        '''calculate x,y coord of each element'''
        current_line = 1

        #base coord
        x_coord = x_base
        y_coord = y_base

        context = widget.get_style_context()

        for w in self.update_markup():
            #calculate remaining space in current line
            avariable_width = width - x_coord

            if isinstance(w, basestring):
                lines = w.split("\n")
                lines_count = len(lines)
                for i in range(0, lines_count):
                    lbl = Gtk.Label()
                    lbl.set_markup(lines[i])
                    layout = lbl.get_layout()
                    lblwidth = lbl.get_preferred_width()[1]

                    # only render if we have enought space
                    # otherwise only do the calculation for coords
                    if avariable_width > 0:
                        # if can can't render the label entirely, elipside it.
                        if avariable_width < lblwidth:
                            layout.set_width(avariable_width * Pango.SCALE)
                            layout.set_ellipsize(Pango.ELLIPSIZE_END)
                        Gtk.render_layout(context, ctx, x_coord, y_coord, layout)

                    #if we aren't in last line then update coords
                    if lines_count > 1 and i != lines_count - 1:
                        x_coord = x_base
                        y_coord += self._lines_height[current_line]
                        #avoid moving on last line, since we can have pixbuf in it
                        current_line += i
                    else:
                        #update only x coord because we can have smileys in this line
                        x_coord += lblwidth
            elif isinstance(w, GdkPixbuf.Pixbuf):
                #scale image to the text height
                size = min(self._lines_height[current_line], w.get_width())
                pix = w.scale_simple(size, size, GdkPixbuf.InterpType.BILINEAR)
                # only render the image if it's totally into the visible area
                if avariable_width > size:
                    Gtk.render_icon(context, ctx, pix, x_coord, y_coord)
                x_coord += pix.get_width()
            else:
                log.error("unhandled type %s" % type(i))
Beispiel #4
0
    def calculate_positions(self, ctx, widget, x_base=0, y_base=0):
        '''calculate x,y coord of each element'''
        current_line = 1
        lines_height = self.calculate_lines_height()

        #base coord
        x_coord = x_base
        y_coord = y_base

        context = widget.get_style_context()

        for w in self.update_markup():
            if isinstance(w, basestring):
                lines = w.split("\n")
                lines_count = len(lines)
                for i in range(0, lines_count):
                    lbl = Gtk.Label()
                    lbl.set_markup(lines[i])
                    layout = lbl.get_layout()
                    Gtk.render_layout(context, ctx, x_coord, y_coord, layout)

                    #if we aren't in last line then update coords
                    if lines_count > 1 and i != lines_count - 1:
                        x_coord = x_base
                        y_coord += lines_height[current_line]
                        #avoid moving on last line, since we can have pixbuf in it
                        current_line += i
                    else:
                        #update only x coord because we can have smileys in this line
                        x_coord += lbl.get_preferred_width()[1]
            elif isinstance(w, GdkPixbuf.Pixbuf):
                #scale image to the text height
                size = min(lines_height[current_line], w.get_width())
                pix = w.scale_simple(size, size, GdkPixbuf.InterpType.BILINEAR)
                Gtk.render_icon(context, ctx, pix, x_coord, y_coord)
                x_coord += pix.get_width()
            else:
                log.error("unhandled type %s" % type(i))
Beispiel #5
0
    def do_draw(self, context, background_area, cell_area, start, end, state):
        GtkSource.GutterRendererPixbuf.do_draw(
            self, context, background_area, cell_area, start, end, state)
        if self.is_action:
            # TODO: Fix padding and min-height in CSS and use
            # draw_style_common
            style_context = get_style(None, "button.flat.image-button")
            style_context.set_state(renderer_to_gtk_state(state))

            x = background_area.x + 1
            y = background_area.y + 1
            width = background_area.width - 2
            height = background_area.height - 2

            Gtk.render_background(style_context, context, x, y, width, height)
            Gtk.render_frame(style_context, context, x, y, width, height)

            pixbuf = self.props.pixbuf
            pix_width, pix_height = pixbuf.props.width, pixbuf.props.height

            xalign, yalign = self.get_alignment()
            align_mode = self.get_alignment_mode()
            if align_mode == GtkSource.GutterRendererAlignmentMode.CELL:
                icon_x = x + (width - pix_width) // 2
                icon_y = y + (height - pix_height) // 2
            else:
                line_iter = start if align_mode == ALIGN_MODE_FIRST else end
                textview = self.get_view()
                loc = textview.get_iter_location(line_iter)
                line_x, line_y = textview.buffer_to_window_coords(
                    self.get_window_type(), loc.x, loc.y)
                icon_x = cell_area.x + (cell_area.width - pix_width) * xalign
                icon_y = line_y + (loc.height - pix_height) * yalign

            Gtk.render_icon(style_context, context, pixbuf, icon_x, icon_y)

        self.draw_chunks(
            context, background_area, cell_area, start, end, state)
Beispiel #6
0
    def do_draw(self, context):
        view = self.source_view
        if not view or not view.get_realized():
            return

        self.buttons = []

        width = self.get_allocated_width()
        height = self.get_allocated_height()

        style_context = self.get_style_context()
        Gtk.render_background(style_context, context, 0, 0, width, height)

        buf = view.get_buffer()

        context.save()
        context.set_line_width(1.0)

        # Get our linked view's visible offset, get our vertical offset
        # against our view (e.g., for info bars at the top of the view)
        # and translate our context to match.
        view_y_start = view.get_visible_rect().y
        view_y_offset = view.translate_coordinates(self, 0, 0)[1]
        gutter_y_translate = view_y_offset - view_y_start
        context.translate(0, gutter_y_translate)

        button_x = 1
        button_width = width - 2

        for chunk in self.get_chunk_range(view_y_start, view_y_start + height):

            change_type, start_line, end_line, *_unused = chunk

            rect_y = view.get_y_for_line_num(start_line)
            rect_height = max(
                0, view.get_y_for_line_num(end_line) - rect_y - 1)

            # Draw our rectangle outside x bounds, so we don't get
            # vertical lines. Fill first, over-fill with a highlight
            # if in the focused chunk, and then stroke the border.
            context.rectangle(-0.5, rect_y + 0.5, width + 1, rect_height)
            if start_line != end_line:
                context.set_source_rgba(*self.fill_colors[change_type])
                context.fill_preserve()
                if view.current_chunk_check(chunk):
                    highlight = self.fill_colors['current-chunk-highlight']
                    context.set_source_rgba(*highlight)
                    context.fill_preserve()
            context.set_source_rgba(*self.line_colors[change_type])
            context.stroke()

            # Button rendering and tracking
            action = self._classify_change_actions(chunk)
            if action is None:
                continue

            it = buf.get_iter_at_line(start_line)
            button_y, button_height = view.get_line_yrange(it)
            button_y += 1
            button_height -= 2

            button_style_context = get_style(None, 'button.flat.image-button')
            if chunk == self.pointer_chunk:
                button_style_context.set_state(Gtk.StateFlags.PRELIGHT)

            Gtk.render_background(
                button_style_context, context, button_x, button_y,
                button_width, button_height)
            Gtk.render_frame(
                button_style_context, context, button_x, button_y,
                button_width, button_height)

            # TODO: Ideally we'd do this in a pre-render step of some
            # kind, but I'm having trouble figuring out what that would
            # look like.
            self.buttons.append(
                (
                    button_x,
                    button_y + gutter_y_translate,
                    button_x + button_width,
                    button_y + gutter_y_translate + button_height,
                    chunk,
                )
            )

            pixbuf = self.action_map.get(action)
            icon_x = button_x + (button_width - pixbuf.props.width) // 2
            icon_y = button_y + (button_height - pixbuf.props.height) // 2
            Gtk.render_icon(
                button_style_context, context, pixbuf, icon_x, icon_y)

        context.restore()