Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugarvte/vteview.py
blob: 3a789c17da9959f7cc188c52046ca5178532079f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Copyright (C) 2012 Daniel Francis <francis@sugarlabs.org>
#
# This is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

#Python translation of Vte.
# http://git.gnome.org/browse/vte?h=vte-next

import logging
logger = logging.getLogger('SugarVteView')

from gi.repository import GObject
from gi.repository import Gtk

class WordCharRange:
    start = None
    end = None

class View(Gtk.Widget, Gtk.Scrollable):
    char_width = 1
    char_height = 2
    char_ascent = 3
    char_descent = 4
    line_thickness = 1
    underline_position = 1
    strikethrough_position = 1
    scroll_on_keystroke = True

    def __init__(self):
        GObject.GObject.__init__(self)
        self.set_can_focus(True)
        self.set_app_paintable(True)
        self.set_redraw_on_allocate(False)
        self.set_word_chars(None)

    vadjustment = GObject.property(type=Gtk.Adjustment)
    hadjustment = GObject.property(type=Gtk.Adjustment)
    hscroll_policy = GObject.property(type=Gtk.ScrollablePolicy, default=Gtk.ScrollablePolicy.NATURAL)
    vscroll_policy = GObject.property(type=Gtk.ScrollablePolicy, default=Gtk.ScrollablePolicy.NATURAL)

    def set_word_chars(self, spec=None):
        """
@spec: a specification
When the user double-clicks to start selection, the terminal will extend
the selection on word boundaries.  It will treat characters included in @spec
as parts of words, and all other characters as word separators.  Ranges of
characters can be specified by separating them with a hyphen.

As a special case, if @spec is %NULL or the empty string, the terminal will
treat all graphic non-punctuation non-space characters as word characters.
"""
        range = WordCharRange()
        self.word_chars = []
        if spec == None or spec[0] == '\0':
            self.notify('word-chars')
        ubuf = unicode(spec)
        i = -1
        while i < len(ubuf):
            i += 1
            if ubuf[i] == '-':
                range.start = ubuf[i]
                range.end = ubuf[i]
                self.word_chars.append(range)
                logger.debug('Word charset includes hypen')
                continue
            if ubuf[i] != '-' and ubuf[i + 1] != '-':
                range.start = ubuf[i]
                range.end = ubuf[i]
                self.word_chars.append(range)
                continue
            if ubuf[i] != '-' and\
                    ubuf[i + 1] == u'-' and\
                    ubuf[i + 2] != u'-' and\
                    ubuf[i + 2] != unichr(0):
                range.start = ubuf[i]
                range.end = ubuf[i + 1]
                self.word_chars.append(range)
                continue
        self.notify('word-chars')


if __name__ == "__main__":
    view = View()
    w = Gtk.Window()
    w.connect('destroy', Gtk.main_quit)
    w.add(view)
    view.show()
    w.show()
    Gtk.main()