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.py < prev    next >
Text File  |  2010-05-11  |  21KB  |  499 lines

  1. #!/usr/bin/env python
  2. """Text Widget/TextView
  3.  
  4. The GtkTextView widget displays a GtkTextBuffer. One GtkTextBuffer can be displayed
  5. by multiple GtkTextViews. This demo has two views displaying a single buffer, and
  6. shows off the widget's text formatting features."""
  7. # pygtk version: Maik Hertha <maik.hertha@berlin.de>
  8.  
  9. import os
  10. import sys
  11.  
  12. import gobject
  13. import gtk
  14.  
  15. gray50_width  = 2
  16. gray50_height = 2
  17. gray50_bits   = '\x02\x01'
  18. GTKLOGO_IMAGE = os.path.join(os.path.dirname(__file__),
  19.                              'images', 'gtk-logo-rgb.gif')
  20. FLOPPYBUDDY_IMAGE = os.path.join(os.path.dirname(__file__),
  21.                                  'images', 'floppybuddy.gif')
  22.  
  23. class TextViewDemo(gtk.Window):
  24.     def __init__(self, parent=None):
  25.         # Create the toplevel window
  26.         gtk.Window.__init__(self)
  27.         try:
  28.             self.set_screen(parent.get_screen())
  29.         except AttributeError:
  30.             self.connect('destroy', lambda *w: gtk.main_quit())
  31.  
  32.         self.set_title(self.__class__.__name__)
  33.         self.set_default_size(450, 450)
  34.         self.set_border_width(0)
  35.  
  36.         vpaned = gtk.VPaned()
  37.         vpaned.set_border_width(5)
  38.         self.add(vpaned)
  39.  
  40.         # For convenience, we just use the autocreated buffer from
  41.         # the first text view; you could also create the buffer
  42.         # by itself with gtk.text_buffer_new(), then later create
  43.         # a view widget.
  44.  
  45.         view1 = gtk.TextView();
  46.         buffer_1 = view1.get_buffer()
  47.         view2 = gtk.TextView(buffer_1)
  48.  
  49.         sw = gtk.ScrolledWindow()
  50.         sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
  51.         sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
  52.  
  53.         vpaned.add1(sw)
  54.  
  55.         sw.add(view1)
  56.  
  57.         sw = gtk.ScrolledWindow()
  58.         sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
  59.         sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
  60.  
  61.         vpaned.add2(sw)
  62.  
  63.         sw.add(view2)
  64.  
  65.         self.create_tags(buffer_1)
  66.         self.insert_text(buffer_1)
  67.  
  68.         self.attach_widgets(view1)
  69.         self.attach_widgets(view2)
  70.         self.win = None
  71.         self.show_all()
  72.  
  73.     def create_tags(self, text_buffer):
  74.         '''
  75.         Create a bunch of tags. Note that it's also possible to
  76.         create tags with gtk.text_tag_new() then add them to the
  77.         tag table for the buffer, text_buffer.create_tag() is
  78.         just a convenience function. Also note that you don't have
  79.         to give tags a name; pass None for the name to create an
  80.         anonymous tag.
  81.  
  82.         In any real app, another useful optimization would be to create
  83.         a GtkTextTagTable in advance, and reuse the same tag table for
  84.         all the buffers with the same tag set, instead of creating
  85.         new copies of the same tags for every buffer.
  86.  
  87.         Tags are assigned default priorities in order of addition to the
  88.         tag table. That is, tags created later that affect the same text
  89.         property affected by an earlier tag will override the earlier
  90.         tag. You can modify tag priorities with
  91.         gtk.text_tag_set_priority().
  92.         '''
  93.  
  94.         import pango
  95.         text_buffer.create_tag("heading",
  96.                     weight=pango.WEIGHT_BOLD,
  97.                     size=15 * pango.SCALE)
  98.  
  99.         text_buffer.create_tag("italic", style=pango.STYLE_ITALIC)
  100.  
  101.         text_buffer.create_tag("bold", weight=pango.WEIGHT_BOLD)
  102.  
  103.                                 # points times the pango.SCALE factor
  104.         text_buffer.create_tag("big", size=20 * pango.SCALE)
  105.  
  106.         text_buffer.create_tag("xx-small", scale=pango.SCALE_XX_SMALL)
  107.  
  108.         text_buffer.create_tag("x-large", scale=pango.SCALE_X_LARGE)
  109.  
  110.         text_buffer.create_tag("monospace", family="monospace")
  111.  
  112.         text_buffer.create_tag("blue_foreground", foreground="blue")
  113.  
  114.         text_buffer.create_tag("red_background", background="red")
  115.  
  116.         stipple = gtk.gdk.bitmap_create_from_data(None,
  117.             gray50_bits, gray50_width, gray50_height)
  118.  
  119.         text_buffer.create_tag("background_stipple", background_stipple=stipple)
  120.  
  121.         text_buffer.create_tag("foreground_stipple", foreground_stipple=stipple)
  122.  
  123.         text_buffer.create_tag("big_gap_before_line", pixels_above_lines=30)
  124.  
  125.         text_buffer.create_tag("big_gap_after_line", pixels_below_lines=30)
  126.  
  127.         text_buffer.create_tag("double_spaced_line", pixels_inside_wrap=10)
  128.  
  129.         text_buffer.create_tag("not_editable", editable=False)
  130.  
  131.         text_buffer.create_tag("word_wrap", wrap_mode=gtk.WRAP_WORD)
  132.  
  133.         text_buffer.create_tag("char_wrap", wrap_mode=gtk.WRAP_CHAR)
  134.  
  135.         text_buffer.create_tag("no_wrap", wrap_mode=gtk.WRAP_NONE)
  136.  
  137.         text_buffer.create_tag("center", justification=gtk.JUSTIFY_CENTER)
  138.  
  139.         text_buffer.create_tag("right_justify", justification=gtk.JUSTIFY_RIGHT)
  140.  
  141.         text_buffer.create_tag("wide_margins",
  142.                     left_margin=50, right_margin=50)
  143.  
  144.         text_buffer.create_tag("strikethrough", strikethrough=True)
  145.  
  146.         text_buffer.create_tag("underline",
  147.                     underline=pango.UNDERLINE_SINGLE)
  148.  
  149.         text_buffer.create_tag("double_underline",
  150.                     underline=pango.UNDERLINE_DOUBLE)
  151.  
  152.         text_buffer.create_tag("superscript",
  153.                     rise=10 * pango.SCALE,      # 10 pixels
  154.                     size=8 * pango.SCALE)       #  8 points
  155.  
  156.         text_buffer.create_tag("subscript",
  157.                     rise=-10 * pango.SCALE,     # 10 pixels
  158.                     size=8 * pango.SCALE)       #  8 points
  159.  
  160.         text_buffer.create_tag("rtl_quote",
  161.                     wrap_mode=gtk.WRAP_WORD, direction=gtk.TEXT_DIR_RTL,
  162.                     indent=30, left_margin=20, right_margin=20)
  163.  
  164.     def insert_text(self, text_buffer):
  165.         # use the current directory for the file
  166.         try:
  167.             pixbuf = gtk.gdk.pixbuf_new_from_file(GTKLOGO_IMAGE)
  168.         except gobject.GError, error:
  169.             sys.exit("Failed to load image file gtk-logo-rgb.gif\n")
  170.  
  171.         scaled = pixbuf.scale_simple(32, 32, 'bilinear')
  172.         pixbuf = scaled
  173.  
  174.         # get start of buffer; each insertion will revalidate the
  175.         # iterator to point to just after the inserted text.
  176.         iter = text_buffer.get_iter_at_offset(0)
  177.  
  178.         text_buffer.insert(iter, "The text widget can display text with "
  179.             "all kinds of nifty attributes. It also supports multiple views "
  180.             "of the same buffer; this demo is showing the same buffer in "
  181.             "two places.\n\n")
  182.  
  183.         text_buffer.insert_with_tags_by_name(iter, "Font styles. ", "heading")
  184.  
  185.         text_buffer.insert(iter, "For example, you can have ")
  186.         text_buffer.insert_with_tags_by_name(iter,
  187.                             "italic", "italic")
  188.         text_buffer.insert(iter, ", ");
  189.         text_buffer.insert_with_tags_by_name(iter,
  190.                             "bold", "bold")
  191.         text_buffer.insert(iter, ", or ", -1)
  192.         text_buffer.insert_with_tags_by_name(iter,
  193.                             "monospace(typewriter)", "monospace")
  194.         text_buffer.insert(iter, ", or ")
  195.         text_buffer.insert_with_tags_by_name(iter,
  196.                             "big", "big")
  197.         text_buffer.insert(iter, " text. ")
  198.         text_buffer.insert(iter, "It's best not to hardcode specific text "
  199.             "sizes; you can use relative sizes as with CSS, such as ")
  200.         text_buffer.insert_with_tags_by_name(iter,
  201.                             "xx-small", "xx-small")
  202.         text_buffer.insert(iter, " or ")
  203.         text_buffer.insert_with_tags_by_name(iter,
  204.                             "x-large", "x-large")
  205.         text_buffer.insert(iter, " to ensure that your program properly "
  206.             "adapts if the user changes the default font size.\n\n")
  207.  
  208.         text_buffer.insert_with_tags_by_name(iter, "Colors. ", "heading")
  209.  
  210.         text_buffer.insert(iter, "Colors such as ");
  211.         text_buffer.insert_with_tags_by_name(iter,
  212.                             "a blue foreground", "blue_foreground")
  213.         text_buffer.insert(iter, " or ");
  214.         text_buffer.insert_with_tags_by_name(iter,
  215.                             "a red background",
  216.                             "red_background")
  217.         text_buffer.insert(iter, " or even ", -1);
  218.         text_buffer.insert_with_tags_by_name(iter,
  219.                             "a stippled red background",
  220.                             "red_background",
  221.                             "background_stipple")
  222.  
  223.         text_buffer.insert(iter, " or ", -1);
  224.         text_buffer.insert_with_tags_by_name(iter,
  225.                             "a stippled blue foreground on solid red background",
  226.                             "blue_foreground",
  227.                             "red_background",
  228.                             "foreground_stipple")
  229.         text_buffer.insert(iter, "(select that to read it) can be used.\n\n", -1);
  230.  
  231.         text_buffer.insert_with_tags_by_name(iter,
  232.             "Underline, strikethrough, and rise. ", "heading")
  233.  
  234.         text_buffer.insert_with_tags_by_name(iter,
  235.                             "Strikethrough",
  236.                             "strikethrough")
  237.         text_buffer.insert(iter, ", ", -1)
  238.         text_buffer.insert_with_tags_by_name(iter,
  239.                             "underline",
  240.                             "underline")
  241.         text_buffer.insert(iter, ", ", -1)
  242.         text_buffer.insert_with_tags_by_name(iter,
  243.                             "double underline",
  244.                             "double_underline")
  245.         text_buffer.insert(iter, ", ", -1)
  246.         text_buffer.insert_with_tags_by_name(iter,
  247.                             "superscript",
  248.                             "superscript")
  249.         text_buffer.insert(iter, ", and ", -1)
  250.         text_buffer.insert_with_tags_by_name(iter,
  251.                             "subscript",
  252.                             "subscript")
  253.         text_buffer.insert(iter, " are all supported.\n\n", -1)
  254.  
  255.         text_buffer.insert_with_tags_by_name(iter, "Images. ",
  256.                             "heading")
  257.  
  258.         text_buffer.insert(iter, "The buffer can have images in it: ", -1)
  259.         text_buffer.insert_pixbuf(iter, pixbuf)
  260.         text_buffer.insert_pixbuf(iter, pixbuf)
  261.         text_buffer.insert_pixbuf(iter, pixbuf)
  262.         text_buffer.insert(iter, " for example.\n\n", -1)
  263.  
  264.         text_buffer.insert_with_tags_by_name(iter, "Spacing. ",
  265.                             "heading")
  266.  
  267.         text_buffer.insert(iter,
  268.             "You can adjust the amount of space before each line.\n", -1)
  269.  
  270.         text_buffer.insert_with_tags_by_name(iter,
  271.             "This line has a whole lot of space before it.\n",
  272.             "big_gap_before_line", "wide_margins")
  273.         text_buffer.insert_with_tags_by_name(iter,
  274.             "You can also adjust the amount of space after each line; "
  275.             "this line has a whole lot of space after it.\n",
  276.             "big_gap_after_line", "wide_margins")
  277.  
  278.         text_buffer.insert_with_tags_by_name(iter,
  279.             "You can also adjust the amount of space between wrapped "
  280.             "lines; this line has extra space between each wrapped line "
  281.             "in the same paragraph. To show off wrapping, some filler "
  282.             "text: the quick brown fox jumped over the lazy dog. Blah "
  283.             "blah blah blah blah blah blah blah blah.\n",
  284.             "double_spaced_line", "wide_margins")
  285.  
  286.         text_buffer.insert(iter, "Also note that those lines have "
  287.             "extra-wide margins.\n\n", -1)
  288.  
  289.         text_buffer.insert_with_tags_by_name(iter, "Editability. ", "heading")
  290.  
  291.         text_buffer.insert_with_tags_by_name(iter,
  292.             "This line is 'locked down' and can't be edited by the "
  293.             "user - just try it! You can't delete this line.\n\n",
  294.             "not_editable")
  295.  
  296.         text_buffer.insert_with_tags_by_name(iter, "Wrapping. ", "heading")
  297.  
  298.         text_buffer.insert(iter,
  299.             "This line(and most of the others in this buffer) is "
  300.             "word-wrapped, using the proper Unicode algorithm. Word "
  301.             "wrap should work in all scripts and languages that GTK+ "
  302.             "supports. Let's make this a long paragraph to demonstrate: "
  303.             "blah blah blah blah blah blah blah blah blah blah blah "
  304.             "blah blah blah blah blah blah blah blah\n\n", -1);
  305.  
  306.         text_buffer.insert_with_tags_by_name(iter,
  307.             "This line has character-based wrapping, and can wrap "
  308.             "between any two character glyphs. Let's make this a long "
  309.             "paragraph to demonstrate: blah blah blah blah blah blah "
  310.             "blah blah blah blah blah blah blah blah blah blah blah "
  311.             "blah blah\n\n", "char_wrap")
  312.  
  313.         text_buffer.insert_with_tags_by_name(iter,
  314.             "This line has all wrapping turned off, so it makes the "
  315.             "horizontal scrollbar appear.\n\n\n", "no_wrap")
  316.  
  317.         text_buffer.insert_with_tags_by_name(iter, "Justification. ",
  318.                             "heading");
  319.  
  320.         text_buffer.insert_with_tags_by_name(iter,
  321.             "\nThis line has center justification.\n", "center")
  322.  
  323.         text_buffer.insert_with_tags_by_name(iter,
  324.             "This line has right justification.\n", "right_justify")
  325.  
  326.         text_buffer.insert_with_tags_by_name(iter,
  327.             "\nThis line has big wide margins. Text text text text "
  328.             "text text text text text text text text text text text "
  329.             "text text text text text text text text text text text "
  330.             "text text text text text text text text text text.\n",
  331.             "wide_margins");
  332.  
  333.         text_buffer.insert_with_tags_by_name(iter,
  334.             "Internationalization. ", "heading")
  335.  
  336.         text_buffer.insert(iter,
  337.             "You can put all sorts of Unicode text in the buffer.\n\n"
  338.             "German(Deutsch S\303\274d) Gr\303\274\303\237 Gott\nGreek"
  339.             "(\316\225\316\273\316\273\316\267\316\275\316\271\316\272"
  340.             "\316\254) \316\223\316\265\316\271\316\254 \317\203\316\261"
  341.             "\317\202\nHebrew   \327\251\327\234\327\225\327\235\n"
  342.             "Japanese(\346\227\245\346\234\254\350\252\236)\n\nThe "
  343.             "widget properly handles bidirectional text, word wrapping, "
  344.             "DOS/UNIX/Unicode paragraph separators, grapheme boundaries, "
  345.             "and so on using the Pango internationalization framework.\n", -1)
  346.  
  347.         text_buffer.insert(iter, "Here's a word-wrapped quote in a "
  348.             "right-to-left language:\n", -1)
  349.         text_buffer.insert_with_tags_by_name(iter,
  350.             "\331\210\331\202\330\257 \330\250\330\257\330\243 "
  351.             "\330\253\331\204\330\247\330\253 \331\205\331\206 "
  352.             "\330\243\331\203\330\253\330\261 \330\247\331\204\331"
  353.             "\205\330\244\330\263\330\263\330\247\330\252 \330\252"
  354.             "\331\202\330\257\331\205\330\247 \331\201\331\212 \330"
  355.             "\264\330\250\331\203\330\251 \330\247\331\203\330\263"
  356.             "\331\212\331\210\331\206 \330\250\330\261\330\247\331"
  357.             "\205\330\254\331\207\330\247 \331\203\331\205\331\206"
  358.             "\330\270\331\205\330\247\330\252 \331\204\330\247 \330"
  359.             "\252\330\263\330\271\331\211 \331\204\331\204\330\261"
  360.             "\330\250\330\255\330\214 \330\253\331\205 \330\252\330"
  361.             "\255\331\210\331\204\330\252 \331\201\331\212 \330\247"
  362.             "\331\204\330\263\331\206\331\210\330\247\330\252 \330"
  363.             "\247\331\204\330\256\331\205\330\263 \330\247\331\204"
  364.             "\331\205\330\247\330\266\331\212\330\251 \330\245\331"
  365.             "\204\331\211 \331\205\330\244\330\263\330\263\330\247"
  366.             "\330\252 \331\205\330\247\331\204\331\212\330\251 \331"
  367.             "\205\331\206\330\270\331\205\330\251\330\214 \331\210"
  368.             "\330\250\330\247\330\252\330\252 \330\254\330\262\330\241"
  369.             "\330\247 \331\205\331\206 \330\247\331\204\331\206\330\270"
  370.             "\330\247\331\205 \330\247\331\204\331\205\330\247\331\204"
  371.             "\331\212 \331\201\331\212 \330\250\331\204\330\257\330\247"
  372.             "\331\206\331\207\330\247\330\214 \331\210\331\204\331\203"
  373.             "\331\206\331\207\330\247 \330\252\330\252\330\256\330\265"
  374.             "\330\265 \331\201\331\212 \330\256\330\257\331\205\330\251 "
  375.             "\331\202\330\267\330\247\330\271 \330\247\331\204\331\205\330"
  376.             "\264\330\261\331\210\330\271\330\247\330\252 \330\247\331\204"
  377.             "\330\265\330\272\331\212\330\261\330\251. \331\210\330\243"
  378.             "\330\255\330\257 \330\243\331\203\330\253\330\261 \331\207"
  379.             "\330\260\331\207 \330\247\331\204\331\205\330\244\330\263"
  380.             "\330\263\330\247\330\252 \331\206\330\254\330\247\330\255"
  381.             "\330\247 \331\207\331\210 \302\273\330\250\330\247\331\206"
  382.             "\331\203\331\210\330\263\331\210\331\204\302\253 \331\201"
  383.             "\331\212 \330\250\331\210\331\204\331\212\331\201\331\212"
  384.             "\330\247.\n\n", "rtl_quote")
  385.  
  386.         text_buffer.insert(iter, "You can put widgets in the buffer: "
  387.             "Here's a button: ", -1)
  388.  
  389.         anchor = text_buffer.create_child_anchor(iter)
  390.         text_buffer.insert(iter, " and a menu: ", -1)
  391.         anchor = text_buffer.create_child_anchor(iter)
  392.         text_buffer.insert(iter, " and a scale: ", -1)
  393.         anchor = text_buffer.create_child_anchor(iter)
  394.         text_buffer.insert(iter, " and an animation: ", -1)
  395.         anchor = text_buffer.create_child_anchor(iter)
  396.         text_buffer.insert(iter, " finally a text entry: ", -1)
  397.         anchor = text_buffer.create_child_anchor(iter)
  398.         text_buffer.insert(iter, ".\n", -1)
  399.  
  400.         text_buffer.insert(iter, "\n\nThis demo doesn't demonstrate all "
  401.             "the GtkTextBuffer features; it leaves out, for example: "
  402.             "invisible/hidden text(doesn't work in GTK 2, but planned), "
  403.             "tab stops, application-drawn areas on the sides of the "
  404.             "widget for displaying breakpoints and such...", -1)
  405.  
  406.         # Apply word_wrap tag to whole buffer */
  407.         start, end = text_buffer.get_bounds()
  408.         text_buffer.apply_tag_by_name("word_wrap", start, end)
  409.  
  410.     def attach_widgets(self, text_view):
  411.         buffer = text_view.get_buffer()
  412.         iter = buffer.get_start_iter()
  413.         i = 0
  414.         while self.find_anchor(iter):
  415.             anchor = iter.get_child_anchor()
  416.             if i == 0:
  417.                 widget = gtk.Button("Click Me")
  418.                 widget.connect("clicked", self.easter_egg_callback)
  419.             elif i == 1:
  420.                 widget = gtk.combo_box_new_text()
  421.                 widget.append_text("Option 1")
  422.                 widget.append_text("Option 2")
  423.                 widget.append_text("Option 3")
  424.             elif i == 2:
  425.                 widget = gtk.HScale()
  426.                 widget.set_range(0, 100)
  427.                 widget.set_size_request(70, -1)
  428.             elif i == 3:
  429.                 widget = gtk.Image()
  430.                 widget.set_from_file(FLOPPYBUDDY_IMAGE)
  431.             elif i == 4:
  432.                 widget = gtk.Entry()
  433.             else:
  434.                 raise ValueError
  435.  
  436.             text_view.add_child_at_anchor(widget, anchor)
  437.             widget.show_all()
  438.             i += 1
  439.         return
  440.  
  441.     def find_anchor(self, iter):
  442.         while iter.forward_char():
  443.             if iter.get_child_anchor():
  444.                 return True
  445.         return False
  446.  
  447.     def easter_egg_callback(self, button):
  448.         if self.win:
  449.             self.win.present()
  450.             return
  451.  
  452.         buffer = gtk.TextBuffer()
  453.         iter = buffer.get_start_iter()
  454.         buffer.insert(iter,
  455.                       "This buffer is shared by a set of nested text views.\n Nested view:\n")
  456.         anchor = buffer.create_child_anchor(iter)
  457.         buffer.insert(iter,
  458.                       "\nDon't do this in real applications, please.\n")
  459.  
  460.         view = gtk.TextView(buffer)
  461.  
  462.         self.recursive_attach_view(0, view, anchor)
  463.  
  464.         self.win = gtk.Window()
  465.         sw = gtk.ScrolledWindow()
  466.         sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
  467.  
  468.         self.win.add(sw)
  469.         sw.add(view)
  470.         self.win.set_default_size(300, 400)
  471.         self.win.show_all()
  472.         return
  473.  
  474.     def recursive_attach_view(self, depth, view, anchor):
  475.         if depth > 4:
  476.             return
  477.         child_view = gtk.TextView(view.get_buffer())
  478.         # Event box is needed to add a black border around each child view
  479.         event_box = gtk.EventBox()
  480.         color = gtk.gdk.color_parse("black")
  481.         event_box.modify_bg(gtk.STATE_NORMAL, color)
  482.         align = gtk.Alignment(0.5, 0.5, 1.0, 1.0)
  483.         align.set_border_width(1)
  484.  
  485.         event_box.add(align)
  486.         align.add(child_view)
  487.  
  488.         view.add_child_at_anchor(event_box, anchor)
  489.  
  490.         self.recursive_attach_view(depth + 1, child_view, anchor)
  491.         return
  492.  
  493. def main():
  494.     TextViewDemo()
  495.     gtk.main()
  496.  
  497. if __name__ == '__main__':
  498.     main()
  499.