/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to diff.py

  • Committer: Szilveszter Farkas
  • Date: 2009-05-26 09:57:29 UTC
  • mto: (635.2.6 trunk)
  • mto: This revision was merged to the branch mainline in revision 639.
  • Revision ID: szilveszter.farkas@gmail.com-20090526095729-qjx9lpjm8yo5r658
Support gedit color schemes for gtksourceview2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
67
67
            slm = gtksourceview2.LanguageManager()
68
68
            gsl = slm.guess_language(content_type="text/x-patch")
69
69
            if have_gconf:
70
 
                self.apply_gedit_colors(gsl)
 
70
                self.apply_gedit_colors(self.buffer)
71
71
            self.apply_colordiff_colors(gsl)
72
72
            self.buffer.set_language(gsl)
73
 
            self.buffer.set_highlight(True)
 
73
            self.buffer.set_highlight_syntax(True)
74
74
 
75
75
            self.sourceview = gtksourceview2.View(self.buffer)
76
76
        else:
83
83
        self.sourceview.show()
84
84
 
85
85
    @staticmethod
86
 
    def apply_gedit_colors(lang):
87
 
        """Set style for lang to that specified in gedit configuration.
 
86
    def apply_gedit_colors(buf):
 
87
        """Set style to that specified in gedit configuration.
88
88
 
89
89
        This method needs the gconf module.
90
90
 
91
 
        :param lang: a gtksourceview2.Language object.
 
91
        :param buf: a gtksourceview2.Buffer object.
92
92
        """
93
 
        GEDIT_SYNTAX_PATH = '/apps/gedit-2/preferences/syntax_highlighting'
94
 
        GEDIT_LANG_PATH = GEDIT_SYNTAX_PATH + '/' + lang.get_id()
 
93
        GEDIT_SCHEME_PATH = '/apps/gedit-2/preferences/editor/colors/scheme'
95
94
 
96
95
        client = gconf.client_get_default()
97
 
        client.add_dir(GEDIT_LANG_PATH, gconf.CLIENT_PRELOAD_NONE)
98
 
 
99
 
        for tag in lang.get_tags():
100
 
            tag_id = tag.get_id()
101
 
            gconf_key = GEDIT_LANG_PATH + '/' + tag_id
102
 
            style_string = client.get_string(gconf_key)
103
 
 
104
 
            if style_string is None:
105
 
                continue
106
 
 
107
 
            # function to get a bool from a string that's either '0' or '1'
108
 
            string_bool = lambda x: bool(int(x))
109
 
 
110
 
            # style_string is a string like "2/#FFCCAA/#000000/0/1/0/0"
111
 
            # values are: mask, fg, bg, italic, bold, underline, strike
112
 
            # this packs them into (str_value, attr_name, conv_func) tuples
113
 
            items = zip(style_string.split('/'), ['mask', 'foreground',
114
 
                'background', 'italic', 'bold', 'underline', 'strikethrough' ],
115
 
                [ int, gtk.gdk.color_parse, gtk.gdk.color_parse, string_bool,
116
 
                    string_bool, string_bool, string_bool ]
117
 
            )
118
 
 
119
 
            style = gtksourceview2.Style()
120
 
 
121
 
            # XXX The mask attribute controls whether the present values of
122
 
            # foreground and background color should in fact be used. Ideally
123
 
            # (and that's what gedit does), one could set all three attributes,
124
 
            # and let the TagStyle object figure out which colors to use.
125
 
            # However, in the GtkSourceview python bindings, the mask attribute
126
 
            # is read-only, and it's derived instead from the colors being
127
 
            # set or not. This means that we have to sometimes refrain from
128
 
            # setting fg or bg colors, depending on the value of the mask.
129
 
            # This code could go away if mask were writable.
130
 
            mask = int(items[0][0])
131
 
            if not (mask & 1): # GTK_SOURCE_TAG_STYLE_USE_BACKGROUND
132
 
                items[2:3] = []
133
 
            if not (mask & 2): # GTK_SOURCE_TAG_STYLE_USE_FOREGROUND
134
 
                items[1:2] = []
135
 
            items[0:1] = [] # skip the mask unconditionally
136
 
 
137
 
            for value, attr, func in items:
138
 
                try:
139
 
                    value = func(value)
140
 
                except ValueError:
141
 
                    warning('gconf key %s contains an invalid value: %s'
142
 
                            % gconf_key, value)
143
 
                else:
144
 
                    setattr(style, attr, value)
145
 
 
146
 
            lang.set_tag_style(tag_id, style)
 
96
        style_scheme_name = client.get_string(GEDIT_SCHEME_PATH)
 
97
        style_scheme = gtksourceview2.StyleSchemeManager().get_scheme(style_scheme_name)
 
98
        
 
99
        buf.set_style_scheme(style_scheme)
147
100
 
148
101
    @classmethod
149
102
    def apply_colordiff_colors(klass, lang):