| 1 |
# vim:set et sts=4 sw=4: |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# |
| 4 |
# ibus - The Input Bus |
| 5 |
# |
| 6 |
# Copyright (c) 2015 Peng Huang <[email protected]> |
| 7 |
# Copyright (c) 2015-2023 Takao Fujiwara <[email protected]> |
| 8 |
# Copyright (c) 2013-2015 Red Hat, Inc. |
| 9 |
# |
| 10 |
# This program is free software; you can redistribute it and/or |
| 11 |
# modify it under the terms of the GNU General Public License as |
| 12 |
# published by the Free Software Foundation; either version 2 of the |
| 13 |
# License, or (at your option) any later version. |
| 14 |
# |
| 15 |
# This program is distributed in the hope that it will be useful, but |
| 16 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 |
# General Public License for more details. |
| 19 |
# |
| 20 |
# You should have received a copy of the GNU General Public License |
| 21 |
# along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 22 |
|
| 23 |
# This file is ported from |
| 24 |
# gnome-control-center/panels/region/cc-input-chooser.c |
| 25 |
|
| 26 |
from gi.repository import Gtk |
| 27 |
from gi.repository import GLib |
| 28 |
from gi.repository import IBus |
| 29 |
|
| 30 |
import functools |
| 31 |
import gettext |
| 32 |
import i18n |
| 33 |
import locale |
| 34 |
|
| 35 |
from icon import load_icon |
| 36 |
from i18n import _, N_ |
| 37 |
|
| 38 |
ROW_TRAVEL_DIRECTION_NONE, \ |
| 39 |
ROW_TRAVEL_DIRECTION_FORWARD, \ |
| 40 |
ROW_TRAVEL_DIRECTION_BACKWARD = list(range(3)) |
| 41 |
|
| 42 |
class EngineDialog(Gtk.Dialog): |
| 43 |
__gtype_name__ = 'EngineDialog' |
| 44 |
__initial_languages = [ IBus.get_language_name('en_US'), |
| 45 |
IBus.get_language_name('en_GB'), |
| 46 |
IBus.get_language_name('de_DE'), |
| 47 |
IBus.get_language_name('fr_FR'), |
| 48 |
IBus.get_language_name('es_ES'), |
| 49 |
IBus.get_language_name('zh_CN'), |
| 50 |
IBus.get_language_name('ja_JP'), |
| 51 |
IBus.get_language_name('ru_RU'), |
| 52 |
IBus.get_language_name('ar_EG') ] |
| 53 |
|
| 54 |
|
| 55 |
def __init__(self, transient_for = None): |
| 56 |
super(EngineDialog, self).__init__( |
| 57 |
title = _("Select an input method"), |
| 58 |
transient_for = transient_for, |
| 59 |
resizable = True) |
| 60 |
buttons = (_("_Cancel"), Gtk.ResponseType.CANCEL, |
| 61 |
_("_Add"), Gtk.ResponseType.APPLY) |
| 62 |
self.add_buttons(*buttons) |
| 63 |
self.set_response_sensitive(Gtk.ResponseType.APPLY, False) |
| 64 |
|
| 65 |
self.__engines_for_lang = {} |
| 66 |
self.__untrans_for_lang = {} |
| 67 |
self.__langs = {} |
| 68 |
|
| 69 |
self.__scrolled = Gtk.ScrolledWindow( |
| 70 |
hscrollbar_policy = Gtk.PolicyType.NEVER, |
| 71 |
vscrollbar_policy = Gtk.PolicyType.NEVER, |
| 72 |
shadow_type = Gtk.ShadowType.IN, |
| 73 |
margin_start = 6, |
| 74 |
margin_end = 6, |
| 75 |
margin_top = 6, |
| 76 |
margin_bottom = 6) |
| 77 |
self.vbox.add(self.__scrolled) |
| 78 |
viewport = Gtk.Viewport() |
| 79 |
self.__scrolled.add(viewport) |
| 80 |
self.__list = Gtk.ListBox(vexpand = True, |
| 81 |
halign = Gtk.Align.FILL, |
| 82 |
valign = Gtk.Align.FILL) |
| 83 |
viewport.add(self.__list) |
| 84 |
|
| 85 |
self.__adjustment = self.__scrolled.get_vadjustment() |
| 86 |
self.__list.set_adjustment(self.__adjustment) |
| 87 |
self.__list.set_filter_func(self.__list_filter, None) |
| 88 |
self.__list.connect('row-activated', self.__row_activated) |
| 89 |
self.__list.connect('row-selected', self.__row_selected) |
| 90 |
|
| 91 |
self.__showing_extra = False |
| 92 |
self.__more_row = self.__more_row_new() |
| 93 |
|
| 94 |
self.__filter_timeout_id = 0 |
| 95 |
self.__filter_word = None |
| 96 |
self.__filter_entry = Gtk.SearchEntry(hexpand = True, |
| 97 |
margin_start = 6, |
| 98 |
margin_end = 6, |
| 99 |
margin_top = 6, |
| 100 |
margin_bottom = 6) |
| 101 |
self.__filter_entry.set_no_show_all(True) |
| 102 |
self.__filter_entry.connect('search-changed', self.__filter_changed) |
| 103 |
self.vbox.add(self.__filter_entry) |
| 104 |
|
| 105 |
self.show_all() |
| 106 |
|
| 107 |
|
| 108 |
def __list_filter(self, row, data): |
| 109 |
if row == self.__more_row: |
| 110 |
return not self.__showing_extra |
| 111 |
if not self.__showing_extra and row.is_extra: |
| 112 |
return False |
| 113 |
if self.__filter_word == None: |
| 114 |
return True |
| 115 |
if row.back: |
| 116 |
return True |
| 117 |
|
| 118 |
words = self.__filter_word.lower().strip().split() |
| 119 |
if all(word in row.name.lower() for word in words): |
| 120 |
return True |
| 121 |
if all(word in row.untrans.lower() for word in words): |
| 122 |
return True |
| 123 |
# Search engine name in language list |
| 124 |
if row.lang_info: |
| 125 |
if row.name in self.__engines_for_lang.keys(): |
| 126 |
for row_l in self.__engines_for_lang[row.name]: |
| 127 |
if all(word in row_l.name.lower() for word in words): |
| 128 |
return True |
| 129 |
if all(word in row_l.untrans.lower() for word in words): |
| 130 |
return True |
| 131 |
# Search language name in engine list |
| 132 |
if not row.lang_info: |
| 133 |
for l in self.__engines_for_lang.keys(): |
| 134 |
if all(word in l.lower() for word in words): |
| 135 |
for row_l in self.__engines_for_lang[l]: |
| 136 |
if row.name == row_l.name: |
| 137 |
return True |
| 138 |
for (trans, untrans) in self.__untrans_for_lang.items(): |
| 139 |
if all(word in untrans.lower() for word in words): |
| 140 |
for row_l in self.__engines_for_lang[trans]: |
| 141 |
if row.name == row_l.name: |
| 142 |
return True |
| 143 |
return False |
| 144 |
|
| 145 |
|
| 146 |
def __row_activated(self, box, row): |
| 147 |
if row == self.__more_row: |
| 148 |
# Undo sensitive Gtk.ResponseType.APPLY button |
| 149 |
self.__list.unselect_row(row) |
| 150 |
self.__show_more() |
| 151 |
return |
| 152 |
if row.back: |
| 153 |
self.__show_lang_rows() |
| 154 |
return |
| 155 |
if row.lang_info: |
| 156 |
self.__show_engines_for_lang(row) |
| 157 |
return |
| 158 |
|
| 159 |
|
| 160 |
def __row_selected(self, box, row): |
| 161 |
self.set_response_sensitive(Gtk.ResponseType.APPLY, row != None) |
| 162 |
|
| 163 |
|
| 164 |
def __padded_label_new(self, text, icon, alignment, direction): |
| 165 |
hbox = Gtk.Box(orientation = Gtk.Orientation.HORIZONTAL) |
| 166 |
|
| 167 |
if direction == ROW_TRAVEL_DIRECTION_BACKWARD: |
| 168 |
rtl = (Gtk.Widget.get_default_direction() == \ |
| 169 |
Gtk.TextDirection.RTL) |
| 170 |
if rtl: |
| 171 |
arrow = Gtk.Image.new_from_icon_name( |
| 172 |
'go-previous-rtl-symbolic', Gtk.IconSize.MENU) |
| 173 |
else: |
| 174 |
arrow = Gtk.Image.new_from_icon_name( |
| 175 |
'go-previous-symbolic', Gtk.IconSize.MENU) |
| 176 |
hbox.pack_start(arrow, False, True, 0) |
| 177 |
|
| 178 |
if icon != None: |
| 179 |
pixbuf = load_icon(icon, Gtk.IconSize.LARGE_TOOLBAR) |
| 180 |
image = Gtk.Image(pixbuf = pixbuf) |
| 181 |
hbox.pack_start(image, False, True, 0) |
| 182 |
|
| 183 |
label = Gtk.Label(label = text) |
| 184 |
label.set_halign(alignment) |
| 185 |
label.set_valign(Gtk.Align.CENTER) |
| 186 |
label.set_margin_start(20) |
| 187 |
label.set_margin_end(20) |
| 188 |
label.set_margin_top(6) |
| 189 |
label.set_margin_bottom(6) |
| 190 |
hbox.pack_start(label, True, True, 0) |
| 191 |
return hbox |
| 192 |
|
| 193 |
|
| 194 |
def __list_box_row_new(self, text): |
| 195 |
row = Gtk.ListBoxRow() |
| 196 |
row.name = text |
| 197 |
row.is_extra = False |
| 198 |
row.lang_info = False |
| 199 |
row.back = False |
| 200 |
row.untrans = '' |
| 201 |
row.engine = None |
| 202 |
return row |
| 203 |
|
| 204 |
|
| 205 |
def __lang_row_new(self, text): |
| 206 |
row = self.__list_box_row_new(text) |
| 207 |
row.lang_info = True |
| 208 |
if len(self.__untrans_for_lang) != 0: |
| 209 |
row.untrans = self.__untrans_for_lang[text] |
| 210 |
if not self.__showing_extra and text not in self.__initial_languages: |
| 211 |
row.is_extra = True |
| 212 |
widget = self.__padded_label_new(text, |
| 213 |
None, |
| 214 |
Gtk.Align.CENTER, |
| 215 |
ROW_TRAVEL_DIRECTION_NONE) |
| 216 |
row.add(widget) |
| 217 |
return row |
| 218 |
|
| 219 |
|
| 220 |
def __more_row_new(self): |
| 221 |
row = Gtk.ListBoxRow() |
| 222 |
hbox = Gtk.Box(orientation = Gtk.Orientation.HORIZONTAL) |
| 223 |
row.add(hbox) |
| 224 |
row.set_tooltip_text(_("Moreā¦")) |
| 225 |
arrow = Gtk.Image.new_from_icon_name('view-more-symbolic', |
| 226 |
Gtk.IconSize.MENU) |
| 227 |
arrow.set_margin_start(20) |
| 228 |
arrow.set_margin_end(20) |
| 229 |
arrow.set_margin_top(6) |
| 230 |
arrow.set_margin_bottom(6) |
| 231 |
arrow.set_halign(Gtk.Align.CENTER) |
| 232 |
arrow.set_valign(Gtk.Align.CENTER) |
| 233 |
hbox.pack_start(arrow, True, True, 0) |
| 234 |
return row |
| 235 |
|
| 236 |
|
| 237 |
def __back_row_new(self, text): |
| 238 |
row = self.__list_box_row_new(text) |
| 239 |
row.lang_info = True |
| 240 |
row.back = True |
| 241 |
widget = self.__padded_label_new(text, |
| 242 |
None, |
| 243 |
Gtk.Align.CENTER, |
| 244 |
ROW_TRAVEL_DIRECTION_BACKWARD) |
| 245 |
row.add(widget) |
| 246 |
return row |
| 247 |
|
| 248 |
|
| 249 |
def __engine_row_new(self, engine): |
| 250 |
longname = i18n.gettext_engine_longname(engine) |
| 251 |
description = i18n.gettext_engine_description(engine) |
| 252 |
row = self.__list_box_row_new(longname) |
| 253 |
row.untrans = engine.get_longname() |
| 254 |
row.rank = engine.get_rank() |
| 255 |
row.set_tooltip_text(description) |
| 256 |
row.engine = engine |
| 257 |
widget = self.__padded_label_new(longname, |
| 258 |
engine.get_icon(), |
| 259 |
Gtk.Align.START, |
| 260 |
ROW_TRAVEL_DIRECTION_NONE) |
| 261 |
row.add(widget) |
| 262 |
return row |
| 263 |
|
| 264 |
|
| 265 |
def __set_fixed_size(self): |
| 266 |
if self.__scrolled.get_policy()[0] == Gtk.PolicyType.AUTOMATIC: |
| 267 |
return |
| 268 |
(width, height) = self.get_size() |
| 269 |
self.set_size_request(width, height) |
| 270 |
self.__scrolled.set_policy(Gtk.PolicyType.AUTOMATIC, |
| 271 |
Gtk.PolicyType.AUTOMATIC) |
| 272 |
|
| 273 |
|
| 274 |
def __remove_all_children(self): |
| 275 |
for l in self.__list.get_children(): |
| 276 |
self.__list.remove(l) |
| 277 |
|
| 278 |
|
| 279 |
def __add_engine_rows_for_lang(self, row): |
| 280 |
lang = row.name |
| 281 |
|
| 282 |
def cmp_engine(a, b): |
| 283 |
if a.rank == b.rank: |
| 284 |
return locale.strcoll(a.name, b.name) |
| 285 |
return int(b.rank - a.rank) |
| 286 |
|
| 287 |
self.__engines_for_lang[lang].sort( |
| 288 |
key = functools.cmp_to_key(cmp_engine)) |
| 289 |
for row in self.__engines_for_lang[lang]: |
| 290 |
self.__list.add(row) |
| 291 |
|
| 292 |
|
| 293 |
def __show_lang_rows(self): |
| 294 |
self.__remove_all_children() |
| 295 |
for lang in self.__langs: |
| 296 |
row = self.__lang_row_new(lang) |
| 297 |
self.__list.add(row) |
| 298 |
self.__list.add(self.__more_row) |
| 299 |
self.__list.show_all() |
| 300 |
self.__adjustment.set_value(self.__adjustment.get_lower()) |
| 301 |
self.__list.invalidate_filter() |
| 302 |
self.__list.set_selection_mode(Gtk.SelectionMode.SINGLE) |
| 303 |
|
| 304 |
|
| 305 |
def __show_more(self): |
| 306 |
self.__set_fixed_size() |
| 307 |
self.__filter_entry.show() |
| 308 |
# Get focus after clicking on the view-more row: |
| 309 |
self.__filter_entry.grab_focus_without_selecting() |
| 310 |
self.__showing_extra = True |
| 311 |
self.__list.invalidate_filter() |
| 312 |
|
| 313 |
|
| 314 |
def __show_engines_for_lang(self, row): |
| 315 |
text = row.name |
| 316 |
self.__set_fixed_size() |
| 317 |
self.__remove_all_children() |
| 318 |
row = self.__back_row_new(text) |
| 319 |
self.__list.add(row) |
| 320 |
self.__add_engine_rows_for_lang(row) |
| 321 |
self.__list.show_all() |
| 322 |
self.__adjustment.set_value(self.__adjustment.get_lower()) |
| 323 |
|
| 324 |
|
| 325 |
def __do_filter(self): |
| 326 |
text = self.__filter_entry.get_text() |
| 327 |
if text == '': |
| 328 |
self.__filter_word = None |
| 329 |
else: |
| 330 |
self.__filter_word = text |
| 331 |
self.__list.invalidate_filter() |
| 332 |
self.__filter_timeout_id = 0 |
| 333 |
return False |
| 334 |
|
| 335 |
|
| 336 |
def __filter_changed(self, entry): |
| 337 |
if self.__filter_timeout_id == 0: |
| 338 |
self.__filter_timeout_id = GLib.timeout_add(150, self.__do_filter) |
| 339 |
|
| 340 |
|
| 341 |
def set_engines(self, engines): |
| 342 |
self.__engines_for_lang = {} |
| 343 |
self.__untrans_for_lang = {} |
| 344 |
for e in engines: |
| 345 |
l = IBus.get_language_name(e.get_language()) |
| 346 |
if l == None: |
| 347 |
l = '' |
| 348 |
if l not in self.__engines_for_lang: |
| 349 |
self.__engines_for_lang[l] = [] |
| 350 |
i18n.init_textdomain(e.get_textdomain()) |
| 351 |
row = self.__engine_row_new(e) |
| 352 |
self.__engines_for_lang[l].append(row) |
| 353 |
|
| 354 |
# Retrieve Untranslated language names. |
| 355 |
untrans = IBus.get_untranslated_language_name(e.get_language()) |
| 356 |
if untrans == None: |
| 357 |
untrans = '' |
| 358 |
self.__untrans_for_lang[l] = untrans |
| 359 |
|
| 360 |
keys = list(self.__engines_for_lang.keys()) |
| 361 |
keys.sort(key=functools.cmp_to_key(locale.strcoll)) |
| 362 |
loc = locale.getlocale()[0] |
| 363 |
# None on C locale |
| 364 |
if loc == None or loc == 'C': |
| 365 |
loc = 'en_US' |
| 366 |
current_lang = IBus.get_language_name(loc) |
| 367 |
# move current language to the first place |
| 368 |
if current_lang in keys: |
| 369 |
keys.remove(current_lang) |
| 370 |
keys.insert(0, current_lang) |
| 371 |
|
| 372 |
# move English to the second place |
| 373 |
en_lang = IBus.get_language_name('en_US') |
| 374 |
if en_lang != current_lang and en_lang in keys: |
| 375 |
keys.remove(en_lang) |
| 376 |
keys.insert(1, en_lang) |
| 377 |
|
| 378 |
#add 'Others' to the end of the combo box |
| 379 |
if IBus.get_language_name('Other') in keys: |
| 380 |
keys.remove(IBus.get_language_name('Other')) |
| 381 |
keys += [IBus.get_language_name('Other')] |
| 382 |
|
| 383 |
self.__langs = keys |
| 384 |
self.__show_lang_rows() |
| 385 |
|
| 386 |
|
| 387 |
def get_selected_engine(self): |
| 388 |
row = self.__list.get_selected_row() |
| 389 |
if row == None: |
| 390 |
return None |
| 391 |
return row.engine |