Exemple #1
0
def add_selection_to_layout(pl, colours, doc, selection, offset, size):
    yoffset, xoffset = offset
    chars, rows = size
    selection = selection.get_normalised()

    sel_start = doc.offset_to_cursor_pos(selection.start)
    sel_end = doc.offset_to_cursor_pos(selection.end)

    # print selection.start, selection.end, sel_start, sel_end

    scr_start = offset
    lastline_num = min(yoffset + rows - 1, doc.num_lines - 1)
    scr_end = (lastline_num, chars)

    # we're going to add attributes to this
    attrs = pl.get_attributes()

    # if the selection starts before the last position displayed and
    # it ends after the first position displayed, then the selection is
    # on screen
    if not (min(scr_end, sel_start) == sel_start and min(scr_start, sel_end) == scr_start):
        # short-circuit so that we don't set all the attrs for nothing
        return

    start = max(sel_start, scr_start)
    end = min(sel_end, scr_end)

    endline_num = end[0]

    # work out the character positions to actual screen positions
    # by converting characters to spaces
    sy, sx = start
    sline = doc.get_line(sy)
    sx = char_pos_to_tab_pos(sline, sx, doc.tab_size)
    ey, ex = end
    eline = doc.get_line(ey)
    ex = char_pos_to_tab_pos(eline, ex, doc.tab_size)

    # cap the positions so that it is only the bit that's on screen
    start_pos = (cap(sy - yoffset, 0, rows), cap(sx - xoffset, 0, chars))
    end_pos = (cap(ey - yoffset, 0, rows), cap(ex - xoffset, 0, chars))

    sy, sx = start_pos
    ey, ex = end_pos
    if endline_num != sel_end[0]:
        # if this isn't the last line of the selection, then the
        # selection has to go to the end of the screen
        ex = chars

    # convert the (y, x) coordinates to indexes into the layout text
    start_index = sy * (chars + 1) + sx  # 1 for line ending
    end_index = ey * (chars + 1) + ex

    # add the selection attribute
    bg = colours["sel"]["pango"]
    attr = pango.AttrBackground(red=bg.red, green=bg.green, blue=bg.blue, start_index=start_index, end_index=end_index)
    attrs.insert(attr)

    pl.set_attributes(attrs)
Exemple #2
0
    def draw_brackets(self):
        view = self.view
        doc = view.document

        if view.brackets == None:
            view.calculate_brackets()

        if not view.brackets:
            return

        draw_target = self.pixmap

        xoffset = int(self.hadjustment.value)
        yoffset = int(self.vadjustment.value)

        chars = int(self.hadjustment.page_size)
        rows = int(self.vadjustment.page_size)

        clip_off, clip_size = self.textbox_text_clip
        clip_yoff, clip_xoff = clip_off
        clip_width, clip_height = clip_size

        cursor_pos = view.cursor_pos

        gc = draw_target.new_gc()
        gc.copy(view.colours['plain']['gc'])
        #gc.set_function(gtk.gdk.INVERT)

        screen_start = (xoffset, yoffset)
        lastline_num = min(doc.num_lines-1, yoffset+rows-1)
        screen_end = (chars, lastline_num)

        for bracket_offset in view.brackets:
            bracket_pos = doc.offset_to_cursor_pos(bracket_offset)
            if min(bracket_pos, screen_start) == screen_start and\
            min(bracket_pos, screen_end) == bracket_pos:
                y, x = bracket_pos
                line = doc.get_line(y)
                x = char_pos_to_tab_pos(line, x, doc.tab_size)
                x -= xoffset
                y -= yoffset

                if x >= 0 and x < chars and y >= 0 and y < rows:
                    xpos = clip_xoff+x*self.char_width
                    ypos = y*self.char_height
                    draw_target.draw_rectangle(gc,
                                               False,
                                               xpos,
                                               ypos,
                                               self.char_width,
                                               self.char_height)
            
            # c for cursor, b for bracket
            cy, cx = cursor_pos
            by, bx = bracket_pos
            
            if cy != by:
                # s for start, b for end
                if cy < by:
                    sy = cy
                    ey = by
                else:
                    sy = by
                    ey = cy

                # cap them (c for capped)
                csy = cap(sy, yoffset, yoffset+rows-1)
                cey = cap(ey, yoffset, yoffset+rows-1)

                # d for draw
                dsy = csy - yoffset
                dey = cey - yoffset

                gc = view.colours['gutter_line']['gc']

                # l for line
                lx = self.char_width/2-2
                if sy == csy:
                    ly = dsy*self.char_height+self.char_height/2-2
                    # start arrow
                    draw_target.draw_rectangle(gc, True, lx, ly, 8, 4)
                    lh = (dey-dsy)*self.char_height
                else:
                    ly = dsy*self.char_height
                    if ey == cey:
                        lh = (dey-dsy)*self.char_height+self.char_height/2-2
                    else:
                        lh = (dey-dsy)*self.char_height

                # vertical line
                draw_target.draw_rectangle(gc, True, lx, ly, 4, lh)

                if ey == cey:
                    # end angle
                    y = dey*self.char_height+self.char_height/2-2
                    draw_target.draw_rectangle(gc, True, lx, y, 8, 4)
Exemple #3
0
def test_cap_too_big():
    #cap(value, minimum, maximum)
    assert cap(4, 1, 3) == 3
Exemple #4
0
def test_cap_ok():
    #cap(value, minimum, maximum)
    assert cap(2, 1, 3) == 2
Exemple #5
0
def test_cap_too_small():
    #cap(value, minimum, maximum)
    assert cap(1, 2, 3) == 2