Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGonzalo Odiard <godiard@gmail.com>2011-08-09 03:33:48 (GMT)
committer Gonzalo Odiard <godiard@gmail.com>2011-08-09 03:33:48 (GMT)
commitc1074be1ce91ecfe2c709c81fe20ff688960cc83 (patch)
tree139df229c62278d34bd28cc6689847164982178a
parent7fc8d0650ba63323966ef347f212fdf8e0984032 (diff)
Text view tests
-rw-r--r--font-change-test.py108
-rw-r--r--textview-test.py70
2 files changed, 178 insertions, 0 deletions
diff --git a/font-change-test.py b/font-change-test.py
new file mode 100644
index 0000000..bd1d64b
--- /dev/null
+++ b/font-change-test.py
@@ -0,0 +1,108 @@
+import pango
+import gtk
+
+
+class TestText():
+
+ def __init__(self):
+ win = gtk.Window()
+ win.connect('destroy', gtk.main_quit)
+
+ self.main_panel = gtk.VBox()
+
+ self.botonera = gtk.HBox()
+
+ self.bold_bt = gtk.ToggleButton("Bold")
+ self.italic_bt = gtk.ToggleButton("Italic")
+ #self.subscribe_bt = gtk.ToggleButton("Subscribe")
+
+ self._fonts = []
+ pango_context = self.main_panel.get_pango_context()
+ pango_context.set_language(pango.Language("en"))
+ for family in pango_context.list_families():
+ self._fonts.append(family.get_name())
+ self._fonts.sort()
+
+ self._font_combo = gtk.combo_box_new_text()
+ self._fonts_changed_id = self._font_combo.connect('changed',
+ self.__font_changed_cb)
+ for f in self._fonts:
+ self._font_combo.append_text(f)
+
+ self._font_size_combo = gtk.combo_box_new_text()
+ self._font_sizes = ['8', '10', '12', '14', '16', '20', '22', '24',
+ '26', '28', '36', '48', '72']
+ self._font_size_changed_id = self._font_size_combo.connect('changed',
+ self.__font_size_changed_cb)
+ for s in self._font_sizes:
+ self._font_size_combo.append_text(s)
+
+ self.bold_bt.connect('clicked', self.__bold_bt_cb)
+ self.italic_bt.connect('clicked', self.__italic_bt_cb)
+ #self.subscribe_bt.connect('clicked',self.__subscribe_bt_clicked)
+
+ self.botonera.pack_start(self.bold_bt, expand=False)
+ self.botonera.pack_start(self.italic_bt, expand=False)
+ #self.botonera.pack_start(self.subscribe_bt, expand=False)
+ self.botonera.pack_start(self._font_combo, expand=False)
+ self.botonera.pack_start(self._font_size_combo, expand=False)
+
+ self.value = gtk.TextView()
+ self.value.get_buffer().set_text('Texto de prueba')
+
+ self.valuefont = pango.FontDescription()
+ self.valuefont.set_family('monospace')
+ self.valuefont.set_absolute_size(20 * pango.SCALE)
+
+ self.value.modify_text(gtk.STATE_NORMAL,
+ gtk.gdk.color_parse("#FF0078"))
+ self.value.modify_base(gtk.STATE_NORMAL,
+ gtk.gdk.color_parse("#001078"))
+ self.value.modify_font(self.valuefont)
+
+ self.main_panel.pack_start(self.botonera, expand=False)
+ self.main_panel.pack_start(self.value, expand=False)
+
+ win.add(self.main_panel)
+ win.show_all()
+ gtk.main()
+
+ def __bold_bt_cb(self, button):
+ if button.get_active():
+ self.valuefont.set_weight(pango.WEIGHT_BOLD)
+ else:
+ self.valuefont.set_weight(pango.WEIGHT_NORMAL)
+ self.value.modify_font(self.valuefont)
+
+ def __italic_bt_cb(self, button):
+ if button.get_active():
+ self.valuefont.set_style(pango.STYLE_ITALIC)
+ else:
+ self.valuefont.set_style(pango.STYLE_NORMAL)
+ self.value.modify_font(self.valuefont)
+
+ """
+ def __subscribe_bt_clicked(self, button):
+ self.valuefont.set_weight(pango.WEIGHT_BOLD)
+ self.value.modify_font(self.valuefont)
+ """
+
+ def __font_size_changed_cb(self, combo):
+ value = self.get_active_text(combo)
+ self.valuefont.set_size(int(value) * pango.SCALE)
+ self.value.modify_font(self.valuefont)
+
+ def __font_changed_cb(self, combo):
+ value = self.get_active_text(combo)
+ self.valuefont.set_family(value)
+ self.value.modify_font(self.valuefont)
+
+ def get_active_text(self, combobox):
+ model = combobox.get_model()
+ active = combobox.get_active()
+ if active < 0:
+ return None
+ return model[active][0]
+
+if __name__ == '__main__':
+ test = TestText()
diff --git a/textview-test.py b/textview-test.py
new file mode 100644
index 0000000..77a1317
--- /dev/null
+++ b/textview-test.py
@@ -0,0 +1,70 @@
+#!/usr/bin/env python
+
+import gtk, pango
+
+class TextViewAdvanced:
+ def __init__(self):
+ window = gtk.Window()
+ window.set_default_size(-1, 350)
+
+ vbox = gtk.VBox(False, 5)
+ scrolledwindow = gtk.ScrolledWindow()
+ scrolledwindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
+ hbox = gtk.HBox(True, 5)
+
+ texttagtable = gtk.TextTagTable()
+ self.textbuffer = gtk.TextBuffer(texttagtable)
+ self.textview = gtk.TextView(self.textbuffer)
+ self.textview.set_wrap_mode(gtk.WRAP_WORD)
+
+ button_bold = gtk.Button("Bold", gtk.STOCK_BOLD)
+ button_italic = gtk.Button("Italic", gtk.STOCK_ITALIC)
+ button_underline = gtk.Button("Underline", gtk.STOCK_UNDERLINE)
+
+ self.texttag_bold = gtk.TextTag("bold")
+ self.texttag_bold.set_property("weight", pango.WEIGHT_BOLD)
+ texttagtable.add(self.texttag_bold)
+ self.texttag_italic = gtk.TextTag("italic")
+ self.texttag_italic.set_property("style", pango.STYLE_ITALIC)
+ texttagtable.add(self.texttag_italic)
+ self.texttag_underline = gtk.TextTag("underline")
+ self.texttag_underline.set_property("underline", pango.UNDERLINE_SINGLE)
+ texttagtable.add(self.texttag_underline)
+
+ hbox.pack_start(button_bold)
+ hbox.pack_start(button_italic)
+ hbox.pack_start(button_underline)
+
+ window.connect("destroy", lambda w: gtk.main_quit())
+ button_bold.connect("clicked", self.bold_text)
+ button_italic.connect("clicked", self.italic_text)
+ button_underline.connect("clicked", self.underline_text)
+
+ window.add(vbox)
+ vbox.pack_start(scrolledwindow, True, True, 0)
+ vbox.pack_start(hbox, False, False, 0)
+ scrolledwindow.add(self.textview)
+
+ file = open("gtk.txt", "r")
+ self.textbuffer.set_text(file.read())
+ file.close()
+
+ window.show_all()
+
+ def bold_text(self, widget):
+ if self.textbuffer.get_selection_bounds() != ():
+ start, end = self.textbuffer.get_selection_bounds()
+ self.textbuffer.apply_tag(self.texttag_bold, start, end)
+
+ def italic_text(self, widget):
+ if self.textbuffer.get_selection_bounds() != ():
+ start, end = self.textbuffer.get_selection_bounds()
+ self.textbuffer.apply_tag(self.texttag_italic, start, end)
+
+ def underline_text(self, widget):
+ if self.textbuffer.get_selection_bounds() != ():
+ start, end = self.textbuffer.get_selection_bounds()
+ self.textbuffer.apply_tag(self.texttag_underline, start, end)
+
+TextViewAdvanced()
+gtk.main()