Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/extensions/cpsection/keyboard/view.py
blob: cd97733617f3087b75eac2ee03ce3ce25f1755ee (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# Copyright (C) 2009, OLPC
# Author: Sayamindu Dasgupta <sayamindu@laptop.org>
#
# This program 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 2 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

import gtk
import gobject
import pango
from gettext import gettext as _

from sugar.graphics import style
from sugar.graphics.icon import Icon

from jarabe.controlpanel.sectionview import SectionView

CLASS = 'Language'
ICON = 'module-keyboard'
TITLE = _('Keyboard')

_APPLY_TIMEOUT = 3000

class LayoutCombo(gtk.HBox):

    """
    Custom GTK widget with two comboboxes side by side, one for layout, and
    the other for variants for the selected layout.
    """

    __gsignals__ = {
        'selection-changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
                            (gobject.TYPE_STRING, gobject.TYPE_INT))
        }

    def __init__(self, xkb, n):
        gtk.HBox.__init__(self)
        self._xkb = xkb
        self._index = n

        self.set_border_width(style.DEFAULT_SPACING)
        self.set_spacing(style.DEFAULT_SPACING)

        label = gtk.Label(' <b>%s</b> ' % str(n+1))
        label.set_use_markup(True)
        label.modify_fg(gtk.STATE_NORMAL,
                    style.COLOR_SELECTION_GREY.get_gdk_color())
        label.set_alignment(0.5, 0.5)
        self.pack_start(label, expand=False)

        self._klang_store = gtk.ListStore(gobject.TYPE_STRING, \
                                                    gobject.TYPE_STRING)
        for description, name in self._xkb.get_languages():
            self._klang_store.append([name, description])

        self._klang_combo = gtk.ComboBox(model = self._klang_store)
        self._klang_combo_changed_id = \
            self._klang_combo.connect('changed', self._klang_combo_changed_cb)
        cell = gtk.CellRendererText()
        cell.props.ellipsize = pango.ELLIPSIZE_MIDDLE
        self._klang_combo.pack_start(cell)
        self._klang_combo.add_attribute(cell, 'text', 1)
        self.pack_start(self._klang_combo, expand=True, fill = True)

        self._kvariant_store = None
        self._kvariant_combo = gtk.ComboBox(model = None)
        self._kvariant_combo_changed_id = \
            self._kvariant_combo.connect('changed', \
                                self._kvariant_combo_changed_cb)
        cell = gtk.CellRendererText()
        cell.props.ellipsize = pango.ELLIPSIZE_MIDDLE
        self._kvariant_combo.pack_start(cell)
        self._kvariant_combo.add_attribute(cell, 'text', 1)
        self.pack_start(self._kvariant_combo, expand=True, fill = True)

        self._klang_combo.set_active(self._index)

    def select_layout(self, layout):
        """Select a given keyboard layout and show appropriate variants"""
        self._kvariant_combo.handler_block(self._kvariant_combo_changed_id)
        for i in range(0, len(self._klang_store)):
            self._klang_combo.set_active(i)
            for j in range(0, len(self._kvariant_store)):
                if self._kvariant_store[j][0] == layout:
                    self._kvariant_combo.set_active(j)
                    self._kvariant_combo.handler_unblock(\
                                self._kvariant_combo_changed_id)
                    return True

        self._kvariant_combo.handler_unblock(self._kvariant_combo_changed_id)
        self._klang_combo.set_active(0)
        return False

    def get_layout(self):
        """Gets the selected layout (with variant)"""
        iter = self._kvariant_combo.get_active_iter()
        model = self._kvariant_combo.get_model()
        return model.get(iter, 0)[0]

    def _set_kvariant_store(self, lang):
        self._kvariant_store = gtk.ListStore(gobject.TYPE_STRING, \
                                                    gobject.TYPE_STRING)
        for description, name in self._xkb.get_layouts_for_language(lang):
            self._kvariant_store.append([name, description])
        self._kvariant_combo.set_model(self._kvariant_store)
        self._kvariant_combo.set_active(0)

    def _klang_combo_changed_cb(self, combobox):
        it = combobox.get_active_iter()
        model = combobox.get_model()
        lang = model.get(it, 0)[0]
        self._set_kvariant_store(lang)
    
    def _kvariant_combo_changed_cb(self, combobox):
        it = combobox.get_active_iter()
        model = combobox.get_model()
        layout = model.get(it, 0)[0]
        self.emit('selection-changed', layout, self._index)


class Keyboard(SectionView):
    def __init__(self, model, alerts):
        SectionView.__init__(self)

        self._model = model

        self._kmodel = None
        self._selected_kmodel = None
        
        self._klayouts = []
        self._selected_klayouts = []
        
        self._group_switch_option = None
        self._selected_group_switch_option = None

        self.set_border_width(style.DEFAULT_SPACING * 2)
        self.set_spacing(style.DEFAULT_SPACING)
        
        self._layout_table = gtk.Table(rows = 4, columns = 2, \
                                                homogeneous = False)

        self._xkb = model.XKB(self.get_display())
        self._layout_combo_list = []
        self._layout_addremovebox_list = []

        scrollwindow = gtk.ScrolledWindow()
        scrollwindow.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
        self.pack_start(scrollwindow, expand=True)
        scrollwindow.show()

        self._vbox = gtk.VBox()
        scrollwindow.add_with_viewport(self._vbox)
        
        self.__kmodel_sid = None
        self.__layout_sid = None
        self.__group_switch_sid = None

        self._setup_kmodel()
        self._setup_layouts()
        self._setup_group_switch_option()
        
        self._vbox.show()

    def _setup_kmodel(self):
        """Adds the controls for changing the keyboard model"""
        separator_kmodel = gtk.HSeparator()
        self._vbox.pack_start(separator_kmodel, expand=False)
        separator_kmodel.show_all()

        label_kmodel = gtk.Label(_('Keyboard Model'))
        label_kmodel.set_alignment(0, 0)
        self._vbox.pack_start(label_kmodel, expand=False)
        label_kmodel.show_all()

        box_kmodel = gtk.VBox()
        box_kmodel.set_border_width(style.DEFAULT_SPACING * 2)
        box_kmodel.set_spacing(style.DEFAULT_SPACING)

        kmodel_store = gtk.ListStore(gobject.TYPE_STRING, \
                                                gobject.TYPE_STRING)
        for description, name in self._xkb.get_models():
            kmodel_store.append([name, description])

        kmodel_combo = gtk.ComboBox(model = kmodel_store)
        cell = gtk.CellRendererText()
        kmodel_combo.pack_start(cell)
        kmodel_combo.add_attribute(cell, 'text', 1)

        self._kmodel = self._xkb.get_current_model()
        for row in kmodel_store:
            if self._kmodel in row[0]:
                kmodel_combo.set_active_iter(row.iter)
                break

        box_kmodel.pack_start(kmodel_combo, expand = False)
        self._vbox.pack_start(box_kmodel, expand=False)
        box_kmodel.show_all()

        kmodel_combo.connect('changed', self.__kmodel_changed_cb)

    def __kmodel_changed_cb(self, combobox):
        if self.__kmodel_sid is not None:
            gobject.source_remove(self.__kmodel_sid)
        self.__kmodel_sid = gobject.timeout_add(_APPLY_TIMEOUT, 
            self.__kmodel_timeout_cb, combobox)

    def __kmodel_timeout_cb(self, combobox):
        it = combobox.get_active_iter()
        model = combobox.get_model()
        self._selected_kmodel = model.get(it, 0)[0]
        if self._selected_kmodel == self._xkb.get_current_model():
            return
        try:
            self._xkb.set_model(self._selected_kmodel)
        except:
            pass #TODO: Show error

        return False

    def _setup_group_switch_option(self):
        """Adds the controls for changing the group switch option of keyboard"""
        separator_grp_option = gtk.HSeparator()
        self._vbox.pack_start(separator_grp_option, expand=False)
        separator_grp_option.show_all()

        label_grp_option = gtk.Label(_('Key(s) to change layout'))
        label_grp_option.set_alignment(0, 0)
        self._vbox.pack_start(label_grp_option, expand=False)
        label_grp_option.show_all()

        box_grp_option = gtk.VBox()
        box_grp_option.set_border_width(style.DEFAULT_SPACING * 2)
        box_grp_option.set_spacing(style.DEFAULT_SPACING)

        grp_option_store = gtk.ListStore(gobject.TYPE_STRING, \
                                                    gobject.TYPE_STRING)
        for description, name in self._xkb.get_options_grp():
            grp_option_store.append([name, description])

        grp_option_combo = gtk.ComboBox(model = grp_option_store)
        cell = gtk.CellRendererText()
        grp_option_combo.pack_start(cell)
        grp_option_combo.add_attribute(cell, 'text', 1)

        self._group_switch_option = self._xkb.get_current_option_grp()
        if not self._group_switch_option:
            grp_option_combo.set_active(0)
        else:
            found = False
            for row in grp_option_store:
                if self._group_switch_option in row[0]:
                    grp_option_combo.set_active_iter(row.iter)
                    found = True
                    break
            if not found:
                grp_option_combo.set_active(0)

        box_grp_option.pack_start(grp_option_combo, expand = False)
        self._vbox.pack_start(box_grp_option, expand=False)
        box_grp_option.show_all()

        grp_option_combo.connect('changed', \
                            self.__group_switch_changed_cb)

    def __group_switch_changed_cb(self, combobox):
        if self.__group_switch_sid is not None:
            gobject.source_remove(self.__group_switch_sid)
        self.__group_switch_sid = gobject.timeout_add(_APPLY_TIMEOUT, 
            self.__group_switch_timeout_cb, combobox)

    def __group_switch_timeout_cb(self, combobox):
        it = combobox.get_active_iter()
        model = combobox.get_model()
        self._selected_group_switch_option = model.get(it, 0)[0]
        if self._selected_group_switch_option == \
                self._xkb.get_current_option_grp():
            return
        try:
            self._xkb.set_option_grp(self._selected_group_switch_option)
        except:
            pass #TODO: Show error

        return False

    def _setup_layouts(self):
        """Adds the controls for changing the keyboard layouts"""
        separator_klayout = gtk.HSeparator()
        self._vbox.pack_start(separator_klayout, expand=False)
        separator_klayout.show_all()

        label_klayout = gtk.Label(_('Keyboard Layout(s)'))
        label_klayout.set_alignment(0, 0)
        label_klayout.show_all()
        self._vbox.pack_start(label_klayout, expand=False)

        self._klayouts = self._xkb.get_current_layouts()
        for i in range(0, self._xkb.get_max_layouts()):
            add_remove_box = self.__create_add_remove_box()
            self._layout_addremovebox_list.append(add_remove_box)
            self._layout_table.attach(add_remove_box, 1, 2, i, i+1)

            layout_combo = LayoutCombo(self._xkb, i)
            layout_combo.connect('selection-changed', \
                self.__layout_combo_selection_changed_cb)
            self._layout_combo_list.append(layout_combo)
            self._layout_table.attach(layout_combo, 0, 1, i, i+1)

            if i < len(self._klayouts):
                layout_combo.show_all()
                layout_combo.select_layout(self._klayouts[i])
        
        self._vbox.pack_start(self._layout_table, expand=False)
        self._layout_table.set_size_request(self._vbox.size_request()[0], -1)
        self._layout_table.show()
        self._update_klayouts()

    def __determine_add_remove_box_visibility(self):
        i = 1
        for box in self._layout_addremovebox_list:
            if not i == len(self._selected_klayouts):
                box.props.visible = False
            else:
                box.show_all()
                if i == 1:
                    # First row - no need for showing remove btn
                    add, remove = box.get_children()
                    remove.props.visible = False
                if i == self._xkb.get_max_layouts():
                    # Last row - no need for showing add btn
                    add, remove = box.get_children()
                    add.props.visible = False
            i += 1

    def __create_add_remove_box(self):
        '''Creates gtk.Hbox with add/remove buttons'''
        add_icon =  Icon(icon_name='list-add')

        add_button = gtk.Button()
        add_button.set_image(add_icon)
        add_button.connect('clicked',
                            self.__add_button_clicked_cb)

        remove_icon =  Icon(icon_name='list-remove')
        remove_button = gtk.Button()
        remove_button.set_image(remove_icon)
        remove_button.connect('clicked',
                            self.__remove_button_clicked_cb)

        add_remove_box = gtk.HButtonBox()
        add_remove_box.set_layout(gtk.BUTTONBOX_START)
        add_remove_box.set_spacing(10)
        add_remove_box.pack_start(add_button)
        add_remove_box.pack_start(remove_button)

        return add_remove_box

    def __layout_combo_selection_changed_cb(self, combo, layout, index):
        self._update_klayouts()

    def __add_button_clicked_cb(self, button):
        self._layout_combo_list[len(self._selected_klayouts)].show_all()
        self._update_klayouts()
    
    def __remove_button_clicked_cb(self, button):
        self._layout_combo_list[len(self._selected_klayouts) - 1].hide()
        self._update_klayouts()

    def _update_klayouts(self):
        """Responds to any changes in the keyboard layout options"""
        self._selected_klayouts = []
        for combo in self._layout_combo_list:
            if combo.props.visible:
                self._selected_klayouts.append(combo.get_layout())

        self.__determine_add_remove_box_visibility()

        if self.__layout_sid is not None:
            gobject.source_remove(self.__layout_sid)
        self.__layout_sid = gobject.timeout_add(_APPLY_TIMEOUT, 
            self.__layout_timeout_cb)

    def __layout_timeout_cb(self):
        if self._selected_klayouts == self._xkb.get_current_layouts():
            return
        try:
            self._xkb.set_layouts(self._selected_klayouts)
        except:
            pass #TODO: Show error

        return False


    def undo(self):
        """Reverts back to the original keyboard configuration"""
        self._xkb.set_model(self._kmodel)
        self._xkb.set_layouts(self._klayouts)
        self._xkb.set_option_grp(self._group_switch_option)