Font selection added. Preference saving/loading improved.
[jben2_gui.git] / tab_prefsfonts.py
blobfdbd4df90c794b9df5f789dc4e82ec88062e7e16
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # Project: J-Ben, Python front-end
5 # File: tab_prefsfonts.py
6 # Author: Paul Goins
7 # Created on: 28 Nov 2008
9 import gtk
10 import os
11 from preferences import options
14 class TabPrefsFonts(gtk.VBox):
16 def __init__(self):
18 gtk.VBox.__init__(self, spacing = 5)
20 # We're loading 4 rows of GUI controls, all following the same
21 # order: 0 = Japanese Normal, 1 = Japanese Large,
22 # 2 = English Normal, 3 = English Small.
24 params = [("font.ja", _("Japanese Font, Normal")),
25 ("font.ja.large", _("Japanese Font, Large")),
26 ("font.native", _("English Font, Normal")),
27 ("font.native.small", _("English Font, Small"))]
29 self.controls = []
30 table = gtk.Table(4, 3)
31 for k, s in params:
32 self.controls.append(
33 (k, gtk.Label(s), gtk.TextView(), gtk.Button(_("Change..."))))
35 for i, (key, label, textview, button) in enumerate(self.controls):
37 label.set_alignment(1.0, 0.5)
39 textview.set_accepts_tab(False)
40 textview.set_editable(False)
42 # Tacking the font_name onto the textview for convenience
43 textview.font_name = self.get_font_name(key)
45 self.update_font_control(self.controls[i], textview.font_name)
47 button.connect("clicked", self.on_font_change, self.controls[i])
49 frame = gtk.Frame()
50 frame.add(textview)
51 frame.set_shadow_type(gtk.SHADOW_IN)
52 bbox = gtk.HButtonBox()
53 bbox.pack_start(button)
55 table.attach(label, 0, 1, i, i + 1,
56 gtk.FILL, gtk.SHRINK, 5, 5)
57 table.attach(frame, 1, 2, i, i + 1,
58 gtk.FILL | gtk.EXPAND, gtk.SHRINK, 5, 5)
59 table.attach(bbox, 2, 3, i, i + 1,
60 gtk.SHRINK, gtk.SHRINK, 5, 5)
62 self.pack_start(table, expand = False);
64 def get_font_name(self, key):
65 font_name = options.get(key)
66 if not font_name:
67 if os.name == "nt":
68 if key == "font.native": return "Tahoma 12"
69 elif key == "font.native.small": return "Tahoma 8"
70 elif key == "font.ja": return "MS Mincho 16"
71 elif key == "font.ja.large": return "MS Mincho 32"
72 else:
73 if key == "font.native": return "sans 12"
74 elif key == "font.native.small": return "sans 8"
75 elif key == "font.ja": return "serif 16"
76 elif key == "font.ja.large": return "serif 32"
77 return font_name
79 def update_font_control(self, ctrls, font_name):
80 key, label, textview, button = ctrls
81 print u"update_font_control: set %08X to font %s" \
82 % (id(textview), font_name)
83 buf = textview.get_buffer()
84 buf.set_text("")
86 table = buf.get_tag_table()
87 tag = table.lookup("font")
88 if tag:
89 table.remove(tag)
91 tag = gtk.TextTag("font")
92 tag.set_property("font", font_name)
93 table.add(tag)
94 start = buf.get_start_iter()
95 buf.insert_with_tags(start, font_name, tag)
97 def on_font_change(self, widget, ctrls):
98 print("TabPrefsFonts.on_font_change for key %s" % ctrls[0])
99 key, label, textview, button = ctrls
100 fd = gtk.FontSelectionDialog(_("Choose Font"))
101 if "font.ja" in key:
102 fd.set_preview_text(_("ROMAJI romaji 日本語 にほんご ニホンゴ"))
103 fd.set_font_name(textview.font_name)
104 result = fd.run()
105 fd.hide()
106 if result == gtk.RESPONSE_OK:
107 textview.font_name = fd.get_font_name();
108 self.update_font_control(ctrls, textview.font_name)
110 def update_prefs(self):
111 for key, label, textview, button in self.controls:
112 buf = textview.get_buffer()
113 s = buf.get_start_iter()
114 e = buf.get_end_iter()
115 text = textview.get_buffer().get_text(s, e, False)
116 options[key] = text
117 print "Setting '%s' to '%s'" % (key, text)