Intermediate commit
[jben2_gui.git] / tab_prefskanjitest.py
blob032026f7faf1983421c14a12a2501b5a807903b6
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # Project: J-Ben, Python front-end
5 # File: tab_prefskanjitest.py
6 # Author: Paul Goins
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.
11 import gtk
12 from preferences import options
15 class TabPrefsKanjiTest(gtk.VBox):
16 def __init__(self):
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)
40 strings = [
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)
51 entry = gtk.Entry()
52 keyval = options.get(key)
53 if keyval:
54 keyval = int(keyval)
55 uval = gtk.gdk.keyval_to_unicode(keyval)
56 s = unichr(uval).encode("utf-8")
57 entry.set_text(s)
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])
71 x = (i % 2) * 2
72 y = i / 2
73 shortcut_table.attach(label, x, x+1, y, y+1,
74 gtk.FILL, gtk.FILL)
75 shortcut_table.attach(entry, x+1, x+2, y, y+1,
76 gtk.FILL, gtk.FILL)
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,
89 gtk.keysyms.Down,
90 gtk.keysyms.Left,
91 gtk.keysyms.Right,
92 gtk.keysyms.Tab,
93 gtk.keysyms.ISO_Left_Tab, # Shift-Tab...?
94 gtk.keysyms.Escape,
95 gtk.keysyms.Return,
96 gtk.keysyms.BackSpace,
97 gtk.keysyms.Delete):
98 return False
99 try:
100 ukeyval = gtk.gdk.keyval_to_unicode(event.keyval)
101 strval = unichr(ukeyval).encode("utf-8")
102 widget.set_text(strval)
103 except:
104 print _("Unhandled keypress captured: "
105 "keyval: 0x%X, hardware_keycode: 0x%X") \
106 % (event.keyval, event.hardware_keycode)
107 return True
109 def update_prefs(self):
110 for key, label, entry in self.shortcut_ctls:
111 s = entry.get_text()
112 if not s.strip():
113 options.pop(key, None)
114 else:
115 uval = ord(s.decode())
116 options[key] = str(gtk.gdk.unicode_to_keyval(uval))