2 # -*- coding: utf-8 -*-
4 # Project: J-Ben, Python front-end
5 # File: tab_prefskanjitest.py
7 # Created on: 28 Nov 2008
8 # Note: Original author of this was Alain Bertrand. I fixed some bugs,
9 # ported it to Python and rearranged some things.
12 from preferences
import options
15 class TabPrefsKanjiTest(gtk
.VBox
):
17 gtk
.VBox
.__init
__(self
, spacing
=5)
19 self
.chk_reading_onyomi
= gtk
.CheckButton(_("Show onyomi"))
20 self
.chk_reading_kunyomi
= gtk
.CheckButton(_("Show kunyomi"))
21 self
.chk_reading_english
= gtk
.CheckButton(_("Show English meaning"))
22 reading_vbox
= gtk
.VBox()
23 reading_vbox
.pack_start(self
.chk_reading_onyomi
, expand
=False)
24 reading_vbox
.pack_start(self
.chk_reading_kunyomi
, expand
=False)
25 reading_vbox
.pack_start(self
.chk_reading_english
, expand
=False)
26 reading_frame
= gtk
.Frame(_("Reading test"))
27 reading_frame
.add(reading_vbox
)
29 self
.chk_writing_onyomi
= gtk
.CheckButton(_("Show onyomi"))
30 self
.chk_writing_kunyomi
= gtk
.CheckButton(_("Show kunyomi"))
31 self
.chk_writing_english
= gtk
.CheckButton(_("Show English meaning"))
32 writing_vbox
= gtk
.VBox()
33 writing_vbox
.pack_start(self
.chk_writing_onyomi
, expand
=False)
34 writing_vbox
.pack_start(self
.chk_writing_kunyomi
, expand
=False)
35 writing_vbox
.pack_start(self
.chk_writing_english
, expand
=False)
36 writing_frame
= gtk
.Frame(_("Writing test"))
37 writing_frame
.add(writing_vbox
)
39 shortcut_table
= gtk
.Table(2,4)
41 ("keys.kanjitest.correct", _("Correct answer: ")),
42 ("keys.kanjitest.wrong", _("Wrong answer: ")),
43 ("keys.kanjitest.show", _("Show answer: ")),
44 ("keys.kanjitest.stop", _("Stop drill: "))
46 self
.shortcut_ctls
= []
47 for key
, label_str
in strings
:
48 label
= gtk
.Label(label_str
)
49 label
.set_alignment(0.0, 0.5)
52 keyval
= options
.get(key
)
55 uval
= gtk
.gdk
.keyval_to_unicode(keyval
)
56 s
= unichr(uval
).encode("utf-8")
58 entry
.set_max_length(1)
59 entry
.set_size_request(30, -1)
61 self
.shortcut_ctls
.append((key
, label
, entry
))
63 shortcut_table
.set_border_width(5)
64 shortcut_table
.set_row_spacings(5)
65 shortcut_table
.set_col_spacings(5)
66 shortcut_table
.set_col_spacing(1, 20)
68 for i
, (key
, label
, entry
) in enumerate(self
.shortcut_ctls
):
69 entry
.connect("key-press-event", self
.on_key_press_event
,
70 self
.shortcut_ctls
[i
])
73 shortcut_table
.attach(label
, x
, x
+1, y
, y
+1,
75 shortcut_table
.attach(entry
, x
+1, x
+2, y
, y
+1,
78 shortcut_frame
= gtk
.Frame(_("Keyboard shortcuts"))
79 shortcut_frame
.add(shortcut_table
)
81 self
.pack_start(reading_frame
, expand
=False)
82 self
.pack_start(writing_frame
, expand
=False)
83 self
.pack_start(shortcut_frame
, expand
=False)
85 def on_key_press_event(self
, widget
, event
, data
=None):
86 key
, label
, entry
= data
88 if event
.keyval
in (gtk
.keysyms
.Up
,
93 gtk
.keysyms
.ISO_Left_Tab
, # Shift-Tab...?
96 gtk
.keysyms
.BackSpace
,
100 ukeyval
= gtk
.gdk
.keyval_to_unicode(event
.keyval
)
101 strval
= unichr(ukeyval
).encode("utf-8")
102 widget
.set_text(strval
)
104 print _("Unhandled keypress captured: "
105 "keyval: 0x%X, hardware_keycode: 0x%X") \
106 % (event
.keyval
, event
.hardware_keycode
)
109 def update_prefs(self
):
110 for key
, label
, entry
in self
.shortcut_ctls
:
113 options
.pop(key
, None)
115 uval
= ord(s
.decode())
116 options
[key
] = str(gtk
.gdk
.unicode_to_keyval(uval
))