From 5b63e52be65dc1d9bae02fabb36590b06aa501b3 Mon Sep 17 00:00:00 2001
From: Daiki Ueno <ueno@unixuser.org>
Date: Fri, 10 Feb 2012 18:17:53 +0900
Subject: [PATCH] Allow user to preview fonts before applying changes.

---
 fontstweak/fontstools.ui |  283 +++++++++++++++++++++++++++++++++++++++++-----
 fontstweak/tool.py       |   24 ++++
 2 files changed, 278 insertions(+), 29 deletions(-)

diff --git a/fontstweak/fontstools.ui b/fontstweak/fontstools.ui
index 37afdb7..f6af58a 100644
--- a/fontstweak/fontstools.ui
+++ b/fontstweak/fontstools.ui
@@ -168,6 +168,7 @@
                             <property name="can_focus">False</property>
                             <property name="row_spacing">4</property>
                             <property name="column_spacing">4</property>
+                            <property name="n_rows">10</property>
                             <child>
                               <object class="GtkLabel" id="label4">
                                 <property name="visible">True</property>
@@ -182,27 +183,42 @@
                               </packing>
                             </child>
                             <child>
-                              <object class="GtkLabel" id="label5">
+                              <object class="GtkComboBox" id="sans_combobox">
                                 <property name="visible">True</property>
                                 <property name="can_focus">False</property>
-                                <property name="label" translatable="yes">Serif:</property>
+                                <property name="hexpand">True</property>
+                                <property name="model">sans_fonts_list</property>
                               </object>
                               <packing>
-                                <property name="left_attach">0</property>
-                                <property name="top_attach">1</property>
+                                <property name="left_attach">1</property>
+                                <property name="top_attach">0</property>
                                 <property name="width">1</property>
                                 <property name="height">1</property>
                               </packing>
                             </child>
                             <child>
-                              <object class="GtkLabel" id="label6">
+                              <object class="GtkLabel" id="label8">
                                 <property name="visible">True</property>
                                 <property name="can_focus">False</property>
-                                <property name="label" translatable="yes">Monospace:</property>
+                                <property name="label" translatable="yes">Fantasy:</property>
                               </object>
                               <packing>
                                 <property name="left_attach">0</property>
-                                <property name="top_attach">2</property>
+                                <property name="top_attach">8</property>
+                                <property name="width">1</property>
+                                <property name="height">1</property>
+                              </packing>
+                            </child>
+                            <child>
+                              <object class="GtkComboBox" id="fantasy_combobox">
+                                <property name="visible">True</property>
+                                <property name="can_focus">False</property>
+                                <property name="hexpand">True</property>
+                                <property name="model">fantasy_fonts_list</property>
+                              </object>
+                              <packing>
+                                <property name="left_attach">1</property>
+                                <property name="top_attach">8</property>
                                 <property name="width">1</property>
                                 <property name="height">1</property>
                               </packing>
@@ -215,16 +231,30 @@
                               </object>
                               <packing>
                                 <property name="left_attach">0</property>
-                                <property name="top_attach">3</property>
+                                <property name="top_attach">6</property>
                                 <property name="width">1</property>
                                 <property name="height">1</property>
                               </packing>
                             </child>
                             <child>
-                              <object class="GtkLabel" id="label8">
+                              <object class="GtkComboBox" id="cursive_combobox">
                                 <property name="visible">True</property>
                                 <property name="can_focus">False</property>
-                                <property name="label" translatable="yes">Fantasy:</property>
+                                <property name="hexpand">True</property>
+                                <property name="model">cursive_fonts_list</property>
+                              </object>
+                              <packing>
+                                <property name="left_attach">1</property>
+                                <property name="top_attach">6</property>
+                                <property name="width">1</property>
+                                <property name="height">1</property>
+                              </packing>
+                            </child>
+                            <child>
+                              <object class="GtkLabel" id="label6">
+                                <property name="visible">True</property>
+                                <property name="can_focus">False</property>
+                                <property name="label" translatable="yes">Monospace:</property>
                               </object>
                               <packing>
                                 <property name="left_attach">0</property>
@@ -234,15 +264,28 @@
                               </packing>
                             </child>
                             <child>
-                              <object class="GtkComboBox" id="sans_combobox">
+                              <object class="GtkComboBox" id="monospace_combobox">
                                 <property name="visible">True</property>
                                 <property name="can_focus">False</property>
                                 <property name="hexpand">True</property>
-                                <property name="model">sans_fonts_list</property>
+                                <property name="model">monospace_fonts_list</property>
                               </object>
                               <packing>
                                 <property name="left_attach">1</property>
-                                <property name="top_attach">0</property>
+                                <property name="top_attach">4</property>
+                                <property name="width">1</property>
+                                <property name="height">1</property>
+                              </packing>
+                            </child>
+                            <child>
+                              <object class="GtkLabel" id="label5">
+                                <property name="visible">True</property>
+                                <property name="can_focus">False</property>
+                                <property name="label" translatable="yes">Serif:</property>
+                              </object>
+                              <packing>
+                                <property name="left_attach">0</property>
+                                <property name="top_attach">2</property>
                                 <property name="width">1</property>
                                 <property name="height">1</property>
                               </packing>
@@ -256,31 +299,81 @@
                               </object>
                               <packing>
                                 <property name="left_attach">1</property>
-                                <property name="top_attach">1</property>
+                                <property name="top_attach">2</property>
                                 <property name="width">1</property>
                                 <property name="height">1</property>
                               </packing>
                             </child>
                             <child>
-                              <object class="GtkComboBox" id="monospace_combobox">
+                              <object class="GtkBox" id="box3">
                                 <property name="visible">True</property>
                                 <property name="can_focus">False</property>
                                 <property name="hexpand">True</property>
-                                <property name="model">monospace_fonts_list</property>
+                                <child>
+                                  <object class="GtkLabel" id="label3">
+                                    <property name="visible">True</property>
+                                    <property name="can_focus">False</property>
+                                    <property name="label" translatable="yes">&lt;span font_size="small"&gt;Sample: &lt;/span&gt;</property>
+                                    <property name="use_markup">True</property>
+                                  </object>
+                                  <packing>
+                                    <property name="expand">False</property>
+                                    <property name="fill">True</property>
+                                    <property name="position">0</property>
+                                  </packing>
+                                </child>
+                                <child>
+                                  <object class="GtkLabel" id="sans_label">
+                                    <property name="visible">True</property>
+                                    <property name="can_focus">False</property>
+                                    <property name="label" translatable="yes">&lt;span font_size="small" font_family="sans-serif"&gt;abcdefghijklmnopqrstuvwxyz&lt;/span&gt;</property>
+                                    <property name="use_markup">True</property>
+                                  </object>
+                                  <packing>
+                                    <property name="expand">False</property>
+                                    <property name="fill">True</property>
+                                    <property name="position">1</property>
+                                  </packing>
+                                </child>
                               </object>
                               <packing>
                                 <property name="left_attach">1</property>
-                                <property name="top_attach">2</property>
+                                <property name="top_attach">1</property>
                                 <property name="width">1</property>
                                 <property name="height">1</property>
                               </packing>
                             </child>
                             <child>
-                              <object class="GtkComboBox" id="cursive_combobox">
+                              <object class="GtkBox" id="box4">
                                 <property name="visible">True</property>
                                 <property name="can_focus">False</property>
                                 <property name="hexpand">True</property>
-                                <property name="model">cursive_fonts_list</property>
+                                <child>
+                                  <object class="GtkLabel" id="label11">
+                                    <property name="visible">True</property>
+                                    <property name="can_focus">False</property>
+                                    <property name="label" translatable="yes">&lt;span font_size="small"&gt;Sample: &lt;/span&gt;</property>
+                                    <property name="use_markup">True</property>
+                                  </object>
+                                  <packing>
+                                    <property name="expand">False</property>
+                                    <property name="fill">True</property>
+                                    <property name="position">0</property>
+                                  </packing>
+                                </child>
+                                <child>
+                                  <object class="GtkLabel" id="serif_label">
+                                    <property name="visible">True</property>
+                                    <property name="can_focus">False</property>
+                                    <property name="label" translatable="yes">&lt;span font_size="small" font_family="serif"&gt;abcdefghijklmnopqrstuvwxyz&lt;/span&gt;</property>
+                                    <property name="use_markup">True</property>
+                                  </object>
+                                  <packing>
+                                    <property name="expand">False</property>
+                                    <property name="fill">True</property>
+                                    <property name="position">1</property>
+                                  </packing>
+                                </child>
                               </object>
                               <packing>
                                 <property name="left_attach">1</property>
@@ -290,20 +383,159 @@
                               </packing>
                             </child>
                             <child>
-                              <object class="GtkComboBox" id="fantasy_combobox">
+                              <object class="GtkBox" id="box5">
                                 <property name="visible">True</property>
                                 <property name="can_focus">False</property>
                                 <property name="hexpand">True</property>
-                                <property name="model">fantasy_fonts_list</property>
+                                <child>
+                                  <object class="GtkLabel" id="label12">
+                                    <property name="visible">True</property>
+                                    <property name="can_focus">False</property>
+                                    <property name="label" translatable="yes">&lt;span font_size="small"&gt;Sample: &lt;/span&gt;</property>
+                                    <property name="use_markup">True</property>
+                                  </object>
+                                  <packing>
+                                    <property name="expand">False</property>
+                                    <property name="fill">True</property>
+                                    <property name="position">0</property>
+                                  </packing>
+                                </child>
+                                <child>
+                                  <object class="GtkLabel" id="monospace_label">
+                                    <property name="visible">True</property>
+                                    <property name="can_focus">False</property>
+                                    <property name="label" translatable="yes">&lt;span font_size="small" font_family="monospace"&gt;abcdefghijklmnopqrstuvwxyz&lt;/span&gt;</property>
+                                    <property name="use_markup">True</property>
+                                  </object>
+                                  <packing>
+                                    <property name="expand">False</property>
+                                    <property name="fill">True</property>
+                                    <property name="position">1</property>
+                                  </packing>
+                                </child>
                               </object>
                               <packing>
                                 <property name="left_attach">1</property>
-                                <property name="top_attach">4</property>
+                                <property name="top_attach">5</property>
                                 <property name="width">1</property>
                                 <property name="height">1</property>
                               </packing>
                             </child>
                             <child>
+                              <object class="GtkBox" id="box6">
+                                <property name="visible">True</property>
+                                <property name="can_focus">False</property>
+                                <property name="hexpand">True</property>
+                                <child>
+                                  <object class="GtkLabel" id="label13">
+                                    <property name="visible">True</property>
+                                    <property name="can_focus">False</property>
+                                    <property name="label" translatable="yes">&lt;span font_size="small"&gt;Sample: &lt;/span&gt;</property>
+                                    <property name="use_markup">True</property>
+                                  </object>
+                                  <packing>
+                                    <property name="expand">False</property>
+                                    <property name="fill">True</property>
+                                    <property name="position">0</property>
+                                  </packing>
+                                </child>
+                                <child>
+                                  <object class="GtkLabel" id="cursive_label">
+                                    <property name="visible">True</property>
+                                    <property name="can_focus">False</property>
+                                    <property name="label" translatable="yes">&lt;span font_size="small" font_family="cursive"&gt;abcdefghijklmnopqrstuvwxyz&lt;/span&gt;</property>
+                                    <property name="use_markup">True</property>
+                                  </object>
+                                  <packing>
+                                    <property name="expand">False</property>
+                                    <property name="fill">True</property>
+                                    <property name="position">1</property>
+                                  </packing>
+                                </child>
+                              </object>
+                              <packing>
+                                <property name="left_attach">1</property>
+                                <property name="top_attach">7</property>
+                                <property name="width">1</property>
+                                <property name="height">1</property>
+                              </packing>
+                            </child>
+                            <child>
+                              <object class="GtkBox" id="box7">
+                                <property name="visible">True</property>
+                                <property name="can_focus">False</property>
+                                <property name="hexpand">True</property>
+                                <child>
+                                  <object class="GtkLabel" id="label14">
+                                    <property name="visible">True</property>
+                                    <property name="can_focus">False</property>
+                                    <property name="label" translatable="yes">&lt;span font_size="small"&gt;Sample: &lt;/span&gt;</property>
+                                    <property name="use_markup">True</property>
+                                  </object>
+                                  <packing>
+                                    <property name="expand">False</property>
+                                    <property name="fill">True</property>
+                                    <property name="position">0</property>
+                                  </packing>
+                                </child>
+                                <child>
+                                  <object class="GtkLabel" id="fantasy_label">
+                                    <property name="visible">True</property>
+                                    <property name="can_focus">False</property>
+                                    <property name="label" translatable="yes">&lt;span font_size="small" font_family="fantasy"&gt;abcdefghijklmnopqrstuvwxyz&lt;/span&gt;</property>
+                                    <property name="use_markup">True</property>
+                                  </object>
+                                  <packing>
+                                    <property name="expand">False</property>
+                                    <property name="fill">True</property>
+                                    <property name="position">1</property>
+                                  </packing>
+                                </child>
+                              </object>
+                              <packing>
+                                <property name="left_attach">1</property>
+                                <property name="top_attach">9</property>
+                                <property name="width">1</property>
+                                <property name="height">1</property>
+                              </packing>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
                               <placeholder/>
                             </child>
                             <child>
@@ -514,12 +746,5 @@
   <object class="GtkSizeGroup" id="sizegroup1">
     <property name="mode">both</property>
     <property name="ignore_hidden">True</property>
-    <widgets>
-      <widget name="sans_combobox"/>
-      <widget name="serif_combobox"/>
-      <widget name="monospace_combobox"/>
-      <widget name="cursive_combobox"/>
-      <widget name="fantasy_combobox"/>
-    </widgets>
   </object>
 </interface>
diff --git a/fontstweak/tool.py b/fontstweak/tool.py
index 4abb572..f167102 100644
--- a/fontstweak/tool.py
+++ b/fontstweak/tool.py
@@ -27,12 +27,14 @@ from collections import OrderedDict
 from gi.repository import Gtk
 from gi.repository import GObject
 from gi.repository import Easyfc
+from xml.sax.saxutils import quoteattr
 
 __all__ = (
 	    "FontsTweakTool",
           )
 
 alias_names = ['sans-serif', 'serif', 'monospace', 'cursive', 'fantasy']
+sample_text = 'abcdefghijklmnopqrstuvwxyz'
 
 class LangList:
 
@@ -173,6 +175,7 @@ class FontsTweakTool:
                     self.config.add_alias(lang, a)
                 except gi._glib.GError:
                     pass
+                self.render_label(combobox)
 
     def closeClicked(self, *args):
 	Gtk.main_quit()
@@ -181,6 +184,17 @@ class FontsTweakTool:
         self.config.save()
         Gtk.main_quit()
 
+    def render_label(self, combobox):
+        model = combobox.get_model()
+        iter = combobox.get_active_iter()
+        if iter != None:
+            font = model.get(iter, 0)[0]
+            iter = model.get_iter_first()
+            alias = model.get(iter, 0)[0]
+            self.label[alias].set_markup(
+                "<span font_family=%s font_size=\"small\">%s</span>" % (
+                    quoteattr(font), sample_text))
+
     def render_combobox(self, lang, alias):
         if self.fontslist.has_key(lang) == False:
             self.fontslist[lang] = {}
@@ -195,6 +209,7 @@ class FontsTweakTool:
             if a.get_name() == alias:
                 fn = a.get_font()
                 break
+        family = alias
         if fn != None:
             model = self.combobox[alias].get_model()
             iter = model.get_iter_first()
@@ -202,10 +217,12 @@ class FontsTweakTool:
                 f = model.get(iter, 0)[0]
                 if f == unicode(fn, "utf8"):
                     self.combobox[alias].set_active_iter(iter)
+                    family = f
                     break
                 iter = model.iter_next(iter)
         else:
             self.combobox[alias].set_active(0)
+        self.render_label(self.combobox[alias])
 
     def __init__(self):
         self.__initialized = False
@@ -237,6 +254,13 @@ class FontsTweakTool:
 	self.combobox['cursive'] = builder.get_object("cursive_combobox")
 	self.combobox['fantasy'] = builder.get_object("fantasy_combobox")
 
+        self.label = {}
+	self.label['sans-serif'] = builder.get_object("sans_label")
+	self.label['serif'] = builder.get_object("serif_label")
+	self.label['monospace'] = builder.get_object("monospace_label")
+	self.label['cursive'] = builder.get_object("cursive_label")
+	self.label['fantasy'] = builder.get_object("fantasy_label")
+
         for f in alias_names:
             renderer_text = Gtk.CellRendererText()
             self.combobox[f].pack_start(renderer_text, True)
-- 
1.7.7.6

