Web · Wiki · Activities · Blog · Lists · Chat · Meeting · Bugs · Git · Translate · Archive · People · Donate

Commit c1074be1ce91ecfe2c709c81fe20ff688960cc83

Text view tests
  
1import pango
2import gtk
3
4
5class TestText():
6
7 def __init__(self):
8 win = gtk.Window()
9 win.connect('destroy', gtk.main_quit)
10
11 self.main_panel = gtk.VBox()
12
13 self.botonera = gtk.HBox()
14
15 self.bold_bt = gtk.ToggleButton("Bold")
16 self.italic_bt = gtk.ToggleButton("Italic")
17 #self.subscribe_bt = gtk.ToggleButton("Subscribe")
18
19 self._fonts = []
20 pango_context = self.main_panel.get_pango_context()
21 pango_context.set_language(pango.Language("en"))
22 for family in pango_context.list_families():
23 self._fonts.append(family.get_name())
24 self._fonts.sort()
25
26 self._font_combo = gtk.combo_box_new_text()
27 self._fonts_changed_id = self._font_combo.connect('changed',
28 self.__font_changed_cb)
29 for f in self._fonts:
30 self._font_combo.append_text(f)
31
32 self._font_size_combo = gtk.combo_box_new_text()
33 self._font_sizes = ['8', '10', '12', '14', '16', '20', '22', '24',
34 '26', '28', '36', '48', '72']
35 self._font_size_changed_id = self._font_size_combo.connect('changed',
36 self.__font_size_changed_cb)
37 for s in self._font_sizes:
38 self._font_size_combo.append_text(s)
39
40 self.bold_bt.connect('clicked', self.__bold_bt_cb)
41 self.italic_bt.connect('clicked', self.__italic_bt_cb)
42 #self.subscribe_bt.connect('clicked',self.__subscribe_bt_clicked)
43
44 self.botonera.pack_start(self.bold_bt, expand=False)
45 self.botonera.pack_start(self.italic_bt, expand=False)
46 #self.botonera.pack_start(self.subscribe_bt, expand=False)
47 self.botonera.pack_start(self._font_combo, expand=False)
48 self.botonera.pack_start(self._font_size_combo, expand=False)
49
50 self.value = gtk.TextView()
51 self.value.get_buffer().set_text('Texto de prueba')
52
53 self.valuefont = pango.FontDescription()
54 self.valuefont.set_family('monospace')
55 self.valuefont.set_absolute_size(20 * pango.SCALE)
56
57 self.value.modify_text(gtk.STATE_NORMAL,
58 gtk.gdk.color_parse("#FF0078"))
59 self.value.modify_base(gtk.STATE_NORMAL,
60 gtk.gdk.color_parse("#001078"))
61 self.value.modify_font(self.valuefont)
62
63 self.main_panel.pack_start(self.botonera, expand=False)
64 self.main_panel.pack_start(self.value, expand=False)
65
66 win.add(self.main_panel)
67 win.show_all()
68 gtk.main()
69
70 def __bold_bt_cb(self, button):
71 if button.get_active():
72 self.valuefont.set_weight(pango.WEIGHT_BOLD)
73 else:
74 self.valuefont.set_weight(pango.WEIGHT_NORMAL)
75 self.value.modify_font(self.valuefont)
76
77 def __italic_bt_cb(self, button):
78 if button.get_active():
79 self.valuefont.set_style(pango.STYLE_ITALIC)
80 else:
81 self.valuefont.set_style(pango.STYLE_NORMAL)
82 self.value.modify_font(self.valuefont)
83
84 """
85 def __subscribe_bt_clicked(self, button):
86 self.valuefont.set_weight(pango.WEIGHT_BOLD)
87 self.value.modify_font(self.valuefont)
88 """
89
90 def __font_size_changed_cb(self, combo):
91 value = self.get_active_text(combo)
92 self.valuefont.set_size(int(value) * pango.SCALE)
93 self.value.modify_font(self.valuefont)
94
95 def __font_changed_cb(self, combo):
96 value = self.get_active_text(combo)
97 self.valuefont.set_family(value)
98 self.value.modify_font(self.valuefont)
99
100 def get_active_text(self, combobox):
101 model = combobox.get_model()
102 active = combobox.get_active()
103 if active < 0:
104 return None
105 return model[active][0]
106
107if __name__ == '__main__':
108 test = TestText()
  
1#!/usr/bin/env python
2
3import gtk, pango
4
5class TextViewAdvanced:
6 def __init__(self):
7 window = gtk.Window()
8 window.set_default_size(-1, 350)
9
10 vbox = gtk.VBox(False, 5)
11 scrolledwindow = gtk.ScrolledWindow()
12 scrolledwindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
13 hbox = gtk.HBox(True, 5)
14
15 texttagtable = gtk.TextTagTable()
16 self.textbuffer = gtk.TextBuffer(texttagtable)
17 self.textview = gtk.TextView(self.textbuffer)
18 self.textview.set_wrap_mode(gtk.WRAP_WORD)
19
20 button_bold = gtk.Button("Bold", gtk.STOCK_BOLD)
21 button_italic = gtk.Button("Italic", gtk.STOCK_ITALIC)
22 button_underline = gtk.Button("Underline", gtk.STOCK_UNDERLINE)
23
24 self.texttag_bold = gtk.TextTag("bold")
25 self.texttag_bold.set_property("weight", pango.WEIGHT_BOLD)
26 texttagtable.add(self.texttag_bold)
27 self.texttag_italic = gtk.TextTag("italic")
28 self.texttag_italic.set_property("style", pango.STYLE_ITALIC)
29 texttagtable.add(self.texttag_italic)
30 self.texttag_underline = gtk.TextTag("underline")
31 self.texttag_underline.set_property("underline", pango.UNDERLINE_SINGLE)
32 texttagtable.add(self.texttag_underline)
33
34 hbox.pack_start(button_bold)
35 hbox.pack_start(button_italic)
36 hbox.pack_start(button_underline)
37
38 window.connect("destroy", lambda w: gtk.main_quit())
39 button_bold.connect("clicked", self.bold_text)
40 button_italic.connect("clicked", self.italic_text)
41 button_underline.connect("clicked", self.underline_text)
42
43 window.add(vbox)
44 vbox.pack_start(scrolledwindow, True, True, 0)
45 vbox.pack_start(hbox, False, False, 0)
46 scrolledwindow.add(self.textview)
47
48 file = open("gtk.txt", "r")
49 self.textbuffer.set_text(file.read())
50 file.close()
51
52 window.show_all()
53
54 def bold_text(self, widget):
55 if self.textbuffer.get_selection_bounds() != ():
56 start, end = self.textbuffer.get_selection_bounds()
57 self.textbuffer.apply_tag(self.texttag_bold, start, end)
58
59 def italic_text(self, widget):
60 if self.textbuffer.get_selection_bounds() != ():
61 start, end = self.textbuffer.get_selection_bounds()
62 self.textbuffer.apply_tag(self.texttag_italic, start, end)
63
64 def underline_text(self, widget):
65 if self.textbuffer.get_selection_bounds() != ():
66 start, end = self.textbuffer.get_selection_bounds()
67 self.textbuffer.apply_tag(self.texttag_underline, start, end)
68
69TextViewAdvanced()
70gtk.main()