home *** CD-ROM | disk | FTP | other *** search
/ tusportal.tus.k12.pa.us / tusportal.tus.k12.pa.us.tar / tusportal.tus.k12.pa.us / Wyse / latest-image.raw / 0.img / usr / lib / pygtk / 2.0 / demos / textview.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2010-05-11  |  14KB  |  279 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. """Text Widget/TextView
  5.  
  6. The GtkTextView widget displays a GtkTextBuffer. One GtkTextBuffer can be displayed
  7. by multiple GtkTextViews. This demo has two views displaying a single buffer, and
  8. shows off the widget's text formatting features."""
  9. import os
  10. import sys
  11. import gobject
  12. import gtk
  13. gray50_width = 2
  14. gray50_height = 2
  15. gray50_bits = '\x02\x01'
  16. GTKLOGO_IMAGE = os.path.join(os.path.dirname(__file__), 'images', 'gtk-logo-rgb.gif')
  17. FLOPPYBUDDY_IMAGE = os.path.join(os.path.dirname(__file__), 'images', 'floppybuddy.gif')
  18.  
  19. class TextViewDemo(gtk.Window):
  20.     
  21.     def __init__(self, parent = None):
  22.         gtk.Window.__init__(self)
  23.         
  24.         try:
  25.             self.set_screen(parent.get_screen())
  26.         except AttributeError:
  27.             self.connect('destroy', (lambda : gtk.main_quit()))
  28.  
  29.         self.set_title(self.__class__.__name__)
  30.         self.set_default_size(450, 450)
  31.         self.set_border_width(0)
  32.         vpaned = gtk.VPaned()
  33.         vpaned.set_border_width(5)
  34.         self.add(vpaned)
  35.         view1 = gtk.TextView()
  36.         buffer_1 = view1.get_buffer()
  37.         view2 = gtk.TextView(buffer_1)
  38.         sw = gtk.ScrolledWindow()
  39.         sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
  40.         sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
  41.         vpaned.add1(sw)
  42.         sw.add(view1)
  43.         sw = gtk.ScrolledWindow()
  44.         sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
  45.         sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
  46.         vpaned.add2(sw)
  47.         sw.add(view2)
  48.         self.create_tags(buffer_1)
  49.         self.insert_text(buffer_1)
  50.         self.attach_widgets(view1)
  51.         self.attach_widgets(view2)
  52.         self.win = None
  53.         self.show_all()
  54.  
  55.     
  56.     def create_tags(self, text_buffer):
  57.         """
  58.         Create a bunch of tags. Note that it's also possible to
  59.         create tags with gtk.text_tag_new() then add them to the
  60.         tag table for the buffer, text_buffer.create_tag() is
  61.         just a convenience function. Also note that you don't have
  62.         to give tags a name; pass None for the name to create an
  63.         anonymous tag.
  64.  
  65.         In any real app, another useful optimization would be to create
  66.         a GtkTextTagTable in advance, and reuse the same tag table for
  67.         all the buffers with the same tag set, instead of creating
  68.         new copies of the same tags for every buffer.
  69.  
  70.         Tags are assigned default priorities in order of addition to the
  71.         tag table. That is, tags created later that affect the same text
  72.         property affected by an earlier tag will override the earlier
  73.         tag. You can modify tag priorities with
  74.         gtk.text_tag_set_priority().
  75.         """
  76.         import pango as pango
  77.         text_buffer.create_tag('heading', weight = pango.WEIGHT_BOLD, size = 15 * pango.SCALE)
  78.         text_buffer.create_tag('italic', style = pango.STYLE_ITALIC)
  79.         text_buffer.create_tag('bold', weight = pango.WEIGHT_BOLD)
  80.         text_buffer.create_tag('big', size = 20 * pango.SCALE)
  81.         text_buffer.create_tag('xx-small', scale = pango.SCALE_XX_SMALL)
  82.         text_buffer.create_tag('x-large', scale = pango.SCALE_X_LARGE)
  83.         text_buffer.create_tag('monospace', family = 'monospace')
  84.         text_buffer.create_tag('blue_foreground', foreground = 'blue')
  85.         text_buffer.create_tag('red_background', background = 'red')
  86.         stipple = gtk.gdk.bitmap_create_from_data(None, gray50_bits, gray50_width, gray50_height)
  87.         text_buffer.create_tag('background_stipple', background_stipple = stipple)
  88.         text_buffer.create_tag('foreground_stipple', foreground_stipple = stipple)
  89.         text_buffer.create_tag('big_gap_before_line', pixels_above_lines = 30)
  90.         text_buffer.create_tag('big_gap_after_line', pixels_below_lines = 30)
  91.         text_buffer.create_tag('double_spaced_line', pixels_inside_wrap = 10)
  92.         text_buffer.create_tag('not_editable', editable = False)
  93.         text_buffer.create_tag('word_wrap', wrap_mode = gtk.WRAP_WORD)
  94.         text_buffer.create_tag('char_wrap', wrap_mode = gtk.WRAP_CHAR)
  95.         text_buffer.create_tag('no_wrap', wrap_mode = gtk.WRAP_NONE)
  96.         text_buffer.create_tag('center', justification = gtk.JUSTIFY_CENTER)
  97.         text_buffer.create_tag('right_justify', justification = gtk.JUSTIFY_RIGHT)
  98.         text_buffer.create_tag('wide_margins', left_margin = 50, right_margin = 50)
  99.         text_buffer.create_tag('strikethrough', strikethrough = True)
  100.         text_buffer.create_tag('underline', underline = pango.UNDERLINE_SINGLE)
  101.         text_buffer.create_tag('double_underline', underline = pango.UNDERLINE_DOUBLE)
  102.         text_buffer.create_tag('superscript', rise = 10 * pango.SCALE, size = 8 * pango.SCALE)
  103.         text_buffer.create_tag('subscript', rise = -10 * pango.SCALE, size = 8 * pango.SCALE)
  104.         text_buffer.create_tag('rtl_quote', wrap_mode = gtk.WRAP_WORD, direction = gtk.TEXT_DIR_RTL, indent = 30, left_margin = 20, right_margin = 20)
  105.  
  106.     
  107.     def insert_text(self, text_buffer):
  108.         
  109.         try:
  110.             pixbuf = gtk.gdk.pixbuf_new_from_file(GTKLOGO_IMAGE)
  111.         except gobject.GError:
  112.             error = None
  113.             sys.exit('Failed to load image file gtk-logo-rgb.gif\n')
  114.  
  115.         scaled = pixbuf.scale_simple(32, 32, 'bilinear')
  116.         pixbuf = scaled
  117.         iter = text_buffer.get_iter_at_offset(0)
  118.         text_buffer.insert(iter, 'The text widget can display text with all kinds of nifty attributes. It also supports multiple views of the same buffer; this demo is showing the same buffer in two places.\n\n')
  119.         text_buffer.insert_with_tags_by_name(iter, 'Font styles. ', 'heading')
  120.         text_buffer.insert(iter, 'For example, you can have ')
  121.         text_buffer.insert_with_tags_by_name(iter, 'italic', 'italic')
  122.         text_buffer.insert(iter, ', ')
  123.         text_buffer.insert_with_tags_by_name(iter, 'bold', 'bold')
  124.         text_buffer.insert(iter, ', or ', -1)
  125.         text_buffer.insert_with_tags_by_name(iter, 'monospace(typewriter)', 'monospace')
  126.         text_buffer.insert(iter, ', or ')
  127.         text_buffer.insert_with_tags_by_name(iter, 'big', 'big')
  128.         text_buffer.insert(iter, ' text. ')
  129.         text_buffer.insert(iter, "It's best not to hardcode specific text sizes; you can use relative sizes as with CSS, such as ")
  130.         text_buffer.insert_with_tags_by_name(iter, 'xx-small', 'xx-small')
  131.         text_buffer.insert(iter, ' or ')
  132.         text_buffer.insert_with_tags_by_name(iter, 'x-large', 'x-large')
  133.         text_buffer.insert(iter, ' to ensure that your program properly adapts if the user changes the default font size.\n\n')
  134.         text_buffer.insert_with_tags_by_name(iter, 'Colors. ', 'heading')
  135.         text_buffer.insert(iter, 'Colors such as ')
  136.         text_buffer.insert_with_tags_by_name(iter, 'a blue foreground', 'blue_foreground')
  137.         text_buffer.insert(iter, ' or ')
  138.         text_buffer.insert_with_tags_by_name(iter, 'a red background', 'red_background')
  139.         text_buffer.insert(iter, ' or even ', -1)
  140.         text_buffer.insert_with_tags_by_name(iter, 'a stippled red background', 'red_background', 'background_stipple')
  141.         text_buffer.insert(iter, ' or ', -1)
  142.         text_buffer.insert_with_tags_by_name(iter, 'a stippled blue foreground on solid red background', 'blue_foreground', 'red_background', 'foreground_stipple')
  143.         text_buffer.insert(iter, '(select that to read it) can be used.\n\n', -1)
  144.         text_buffer.insert_with_tags_by_name(iter, 'Underline, strikethrough, and rise. ', 'heading')
  145.         text_buffer.insert_with_tags_by_name(iter, 'Strikethrough', 'strikethrough')
  146.         text_buffer.insert(iter, ', ', -1)
  147.         text_buffer.insert_with_tags_by_name(iter, 'underline', 'underline')
  148.         text_buffer.insert(iter, ', ', -1)
  149.         text_buffer.insert_with_tags_by_name(iter, 'double underline', 'double_underline')
  150.         text_buffer.insert(iter, ', ', -1)
  151.         text_buffer.insert_with_tags_by_name(iter, 'superscript', 'superscript')
  152.         text_buffer.insert(iter, ', and ', -1)
  153.         text_buffer.insert_with_tags_by_name(iter, 'subscript', 'subscript')
  154.         text_buffer.insert(iter, ' are all supported.\n\n', -1)
  155.         text_buffer.insert_with_tags_by_name(iter, 'Images. ', 'heading')
  156.         text_buffer.insert(iter, 'The buffer can have images in it: ', -1)
  157.         text_buffer.insert_pixbuf(iter, pixbuf)
  158.         text_buffer.insert_pixbuf(iter, pixbuf)
  159.         text_buffer.insert_pixbuf(iter, pixbuf)
  160.         text_buffer.insert(iter, ' for example.\n\n', -1)
  161.         text_buffer.insert_with_tags_by_name(iter, 'Spacing. ', 'heading')
  162.         text_buffer.insert(iter, 'You can adjust the amount of space before each line.\n', -1)
  163.         text_buffer.insert_with_tags_by_name(iter, 'This line has a whole lot of space before it.\n', 'big_gap_before_line', 'wide_margins')
  164.         text_buffer.insert_with_tags_by_name(iter, 'You can also adjust the amount of space after each line; this line has a whole lot of space after it.\n', 'big_gap_after_line', 'wide_margins')
  165.         text_buffer.insert_with_tags_by_name(iter, 'You can also adjust the amount of space between wrapped lines; this line has extra space between each wrapped line in the same paragraph. To show off wrapping, some filler text: the quick brown fox jumped over the lazy dog. Blah blah blah blah blah blah blah blah blah.\n', 'double_spaced_line', 'wide_margins')
  166.         text_buffer.insert(iter, 'Also note that those lines have extra-wide margins.\n\n', -1)
  167.         text_buffer.insert_with_tags_by_name(iter, 'Editability. ', 'heading')
  168.         text_buffer.insert_with_tags_by_name(iter, "This line is 'locked down' and can't be edited by the user - just try it! You can't delete this line.\n\n", 'not_editable')
  169.         text_buffer.insert_with_tags_by_name(iter, 'Wrapping. ', 'heading')
  170.         text_buffer.insert(iter, "This line(and most of the others in this buffer) is word-wrapped, using the proper Unicode algorithm. Word wrap should work in all scripts and languages that GTK+ supports. Let's make this a long paragraph to demonstrate: blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n\n", -1)
  171.         text_buffer.insert_with_tags_by_name(iter, "This line has character-based wrapping, and can wrap between any two character glyphs. Let's make this a long paragraph to demonstrate: blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n\n", 'char_wrap')
  172.         text_buffer.insert_with_tags_by_name(iter, 'This line has all wrapping turned off, so it makes the horizontal scrollbar appear.\n\n\n', 'no_wrap')
  173.         text_buffer.insert_with_tags_by_name(iter, 'Justification. ', 'heading')
  174.         text_buffer.insert_with_tags_by_name(iter, '\nThis line has center justification.\n', 'center')
  175.         text_buffer.insert_with_tags_by_name(iter, 'This line has right justification.\n', 'right_justify')
  176.         text_buffer.insert_with_tags_by_name(iter, '\nThis line has big wide margins. Text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text.\n', 'wide_margins')
  177.         text_buffer.insert_with_tags_by_name(iter, 'Internationalization. ', 'heading')
  178.         text_buffer.insert(iter, 'You can put all sorts of Unicode text in the buffer.\n\nGerman(Deutsch S\xc3\xbcd) Gr\xc3\xbc\xc3\x9f Gott\nGreek(\xce\x95\xce\xbb\xce\xbb\xce\xb7\xce\xbd\xce\xb9\xce\xba\xce\xac) \xce\x93\xce\xb5\xce\xb9\xce\xac \xcf\x83\xce\xb1\xcf\x82\nHebrew   \xd7\xa9\xd7\x9c\xd7\x95\xd7\x9d\nJapanese(\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e)\n\nThe widget properly handles bidirectional text, word wrapping, DOS/UNIX/Unicode paragraph separators, grapheme boundaries, and so on using the Pango internationalization framework.\n', -1)
  179.         text_buffer.insert(iter, "Here's a word-wrapped quote in a right-to-left language:\n", -1)
  180.         text_buffer.insert_with_tags_by_name(iter, '\xd9\x88\xd9\x82\xd8\xaf \xd8\xa8\xd8\xaf\xd8\xa3 \xd8\xab\xd9\x84\xd8\xa7\xd8\xab \xd9\x85\xd9\x86 \xd8\xa3\xd9\x83\xd8\xab\xd8\xb1 \xd8\xa7\xd9\x84\xd9\x85\xd8\xa4\xd8\xb3\xd8\xb3\xd8\xa7\xd8\xaa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b5\xd8\xb5 \xd9\x81\xd9\x8a \xd8\xae\xd8\xaf\xd9\x85\xd8\xa9 \xd9\x82\xd8\xb7\xd8\xa7\xd8\xb9 \xd8\xa7\xd9\x84\xd9\x85\xd8\xb4\xd8\xb1\xd9\x88\xd8\xb9\xd8\xa7\xd8\xaa \xd8\xa7\xd9\x84\xd8\xb5\xd8\xba\xd9\x8a\xd8\xb1\xd8\xa9. \xd9\x88\xd8\xa3\xd8\xad\xd8\xaf \xd8\xa3\xd9\x83\xd8\xab\xd8\xb1 \xd9\x87\xd8\xb0\xd9\x87 \xd8\xa7\xd9\x84\xd9\x85\xd8\xa4\xd8\xb3\xd8\xb3\xd8\xa7\xd8\xaa \xd9\x86\xd8\xac\xd8\xa7\xd8\xad\xd8\xa7 \xd9\x87\xd9\x88 \xc2\xbb\xd8\xa8\xd8\xa7\xd9\x86\xd9\x83\xd9\x88\xd8\xb3\xd9\x88\xd9\x84\xc2\xab \xd9\x81\xd9\x8a \xd8\xa8\xd9\x88\xd9\x84\xd9\x8a\xd9\x81\xd9\x8a\xd8\xa7.\n\n', 'rtl_quote')
  181.         text_buffer.insert(iter, "You can put widgets in the buffer: Here's a button: ", -1)
  182.         anchor = text_buffer.create_child_anchor(iter)
  183.         text_buffer.insert(iter, ' and a menu: ', -1)
  184.         anchor = text_buffer.create_child_anchor(iter)
  185.         text_buffer.insert(iter, ' and a scale: ', -1)
  186.         anchor = text_buffer.create_child_anchor(iter)
  187.         text_buffer.insert(iter, ' and an animation: ', -1)
  188.         anchor = text_buffer.create_child_anchor(iter)
  189.         text_buffer.insert(iter, ' finally a text entry: ', -1)
  190.         anchor = text_buffer.create_child_anchor(iter)
  191.         text_buffer.insert(iter, '.\n', -1)
  192.         text_buffer.insert(iter, "\n\nThis demo doesn't demonstrate all the GtkTextBuffer features; it leaves out, for example: invisible/hidden text(doesn't work in GTK 2, but planned), tab stops, application-drawn areas on the sides of the widget for displaying breakpoints and such...", -1)
  193.         (start, end) = text_buffer.get_bounds()
  194.         text_buffer.apply_tag_by_name('word_wrap', start, end)
  195.  
  196.     
  197.     def attach_widgets(self, text_view):
  198.         buffer = text_view.get_buffer()
  199.         iter = buffer.get_start_iter()
  200.         i = 0
  201.         while self.find_anchor(iter):
  202.             anchor = iter.get_child_anchor()
  203.             if i == 0:
  204.                 widget = gtk.Button('Click Me')
  205.                 widget.connect('clicked', self.easter_egg_callback)
  206.             elif i == 1:
  207.                 widget = gtk.combo_box_new_text()
  208.                 widget.append_text('Option 1')
  209.                 widget.append_text('Option 2')
  210.                 widget.append_text('Option 3')
  211.             elif i == 2:
  212.                 widget = gtk.HScale()
  213.                 widget.set_range(0, 100)
  214.                 widget.set_size_request(70, -1)
  215.             elif i == 3:
  216.                 widget = gtk.Image()
  217.                 widget.set_from_file(FLOPPYBUDDY_IMAGE)
  218.             elif i == 4:
  219.                 widget = gtk.Entry()
  220.             else:
  221.                 raise ValueError
  222.             (i == 0).add_child_at_anchor(widget, anchor)
  223.             widget.show_all()
  224.             i += 1
  225.  
  226.     
  227.     def find_anchor(self, iter):
  228.         while iter.forward_char():
  229.             if iter.get_child_anchor():
  230.                 return True
  231.             continue
  232.             iter.get_child_anchor()
  233.         return False
  234.  
  235.     
  236.     def easter_egg_callback(self, button):
  237.         if self.win:
  238.             self.win.present()
  239.             return None
  240.         buffer = gtk.TextBuffer()
  241.         iter = buffer.get_start_iter()
  242.         buffer.insert(iter, 'This buffer is shared by a set of nested text views.\n Nested view:\n')
  243.         anchor = buffer.create_child_anchor(iter)
  244.         buffer.insert(iter, "\nDon't do this in real applications, please.\n")
  245.         view = gtk.TextView(buffer)
  246.         self.recursive_attach_view(0, view, anchor)
  247.         self.win = gtk.Window()
  248.         sw = gtk.ScrolledWindow()
  249.         sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
  250.         self.win.add(sw)
  251.         sw.add(view)
  252.         self.win.set_default_size(300, 400)
  253.         self.win.show_all()
  254.  
  255.     
  256.     def recursive_attach_view(self, depth, view, anchor):
  257.         if depth > 4:
  258.             return None
  259.         child_view = gtk.TextView(view.get_buffer())
  260.         event_box = gtk.EventBox()
  261.         color = gtk.gdk.color_parse('black')
  262.         event_box.modify_bg(gtk.STATE_NORMAL, color)
  263.         align = gtk.Alignment(0.5, 0.5, 1, 1)
  264.         align.set_border_width(1)
  265.         event_box.add(align)
  266.         align.add(child_view)
  267.         view.add_child_at_anchor(event_box, anchor)
  268.         self.recursive_attach_view(depth + 1, child_view, anchor)
  269.  
  270.  
  271.  
  272. def main():
  273.     TextViewDemo()
  274.     gtk.main()
  275.  
  276. if __name__ == '__main__':
  277.     main()
  278.  
  279.