1
# Copyright (c) 2012 - The IBus Cangjie authors
3
# This file is part of ibus-cangjie, the IBus Cangjie input method engine.
5
# ibus-cangjie is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU Lesser General Public License as published by
7
# the Free Software Foundation, either version 3 of the License, or
8
# (at your option) any later version.
10
# ibus-cangjie is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU Lesser General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with ibus-cangjie. If not, see <http://www.gnu.org/licenses/>.
20
# FIXME: Find a way to de-hardcode the gettext package
21
_ = lambda x: gettext.dgettext("ibus-cangjie", x)
23
from gi.repository import Gdk
24
from gi.repository import GLib
25
from gi.repository import Gtk
27
from ibus_cangjie.config import options, Config
30
titles = {"cangjie": _("Cangjie Preferences"),
31
"quick": _("Quick Preferences")}
35
def __init__ (self, bus, engine, datadir, gettext_package):
36
self.__config = Config(bus, engine, self.on_value_changed)
38
ui_file = GLib.build_filenamev([datadir, "setup.ui"])
39
self.__builder = Gtk.Builder()
40
self.__builder.set_translation_domain(gettext_package)
41
self.__builder.add_from_file(ui_file)
43
for option in options:
44
prepare_func = getattr(self, "prepare_%s" % option["widget"])
47
self.__window = self.__builder.get_object("setup_dialog")
48
self.__window.set_title(titles[engine])
51
# Pretend to be a GNOME Control Center dialog if appropriate
52
self.gnome_cc_xid = int(GLib.getenv('GNOME_CONTROL_CENTER_XID'))
53
self.__window.set_wmclass('gnome-control-center',
54
'Gnome-control-center')
55
self.__window.set_modal(True)
56
self.__window.connect('notify::window', self.set_transient)
57
self.__window.set_type_hint(Gdk.WindowTypeHint.DIALOG)
60
# No problem here, we're just a normal dialog
65
def set_transient(self, obj, pspec):
66
from gi.repository import GdkX11
67
window = self.__window.get_window()
69
parent = GdkX11.X11Window.foreign_new_for_display(
70
Gdk.Display.get_default(), self.gnome_cc_xid)
73
window.set_transient_for(parent)
75
def prepare_switch(self, option):
76
"""Prepare a Gtk.Switch
78
Set the switch named `option["name"]` (in)active based on the current
83
switch = self.__builder.get_object(name)
85
v = self.__config.read(name)
86
switch.set_active(v.unpack())
87
switch.connect("notify::active", self.on_switch_toggled,
90
setattr(self, name, switch)
92
def prepare_combo(self, option):
93
"""Prepare a Gtk.ComboBox
95
Set the combobox named `option['name']` to the current engine config
99
current_value = self.__config.read(name).unpack()
101
combo = self.__builder.get_object(name)
103
store = combo.get_model()
104
active_index = self.__get_active_index(store, current_value)
105
combo.set_active(active_index)
107
combo.connect("changed", self.on_combo_changed, name, option)
109
setattr(self, name, combo)
111
def __get_active_index(self, store, value):
112
for i, n in enumerate(store):
113
v = store.get_value(store.get_iter(i), 0)
118
res = self.__window.run()
119
self.__window.destroy()
121
def on_value_changed(self, config, section, name, value, data):
122
"""Callback when the value of a widget is changed.
124
We need to react, in case the value was changed from somewhere else,
125
for example from another setup UI.
127
if section != self.__config.config_section:
131
widget = getattr(self, name)
132
except AttributeError:
135
value = value.unpack()
137
if isinstance(widget, Gtk.ComboBox):
138
store = widget.get_model()
139
v = store.get_value(store.get_iter(widget.get_active()), 0)
141
widget.set_active(self.__get_active_index(widget.get_model(),
145
if widget.get_active() != value:
146
widget.set_active(value)
148
def on_switch_toggled(self, widget, active, setting_name, variant_type):
149
self.__config.write(setting_name, GLib.Variant(variant_type, widget.get_active()))
151
def on_combo_changed(self, widget, setting_name, option):
152
store = widget.get_model()
153
value = store.get_value(store.get_iter(widget.get_active()), 0)
154
self.__config.write(setting_name, GLib.Variant(option["type"], value))