Blame platform-demos/ca/textview.py.page

Packit 1470ea
Packit 1470ea
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" xmlns:xi="http://www.w3.org/2001/XInclude" type="guide" style="task" id="textview.py" xml:lang="ca">
Packit 1470ea
  <info>
Packit 1470ea
    <title type="text">TextView (Python)</title>
Packit 1470ea
    <link type="guide" xref="beginner.py#multiline"/>
Packit 1470ea
    <link type="seealso" xref="strings.py"/>
Packit 1470ea
    <link type="seealso" xref="scrolledwindow.py"/>
Packit 1470ea
    <link type="next" xref="dialog.py"/>
Packit 1470ea
    <revision version="0.2" date="2012-06-19" status="draft"/>
Packit 1470ea
Packit 1470ea
    <credit type="author copyright">
Packit 1470ea
      <name>Sebastian Pölsterl</name>
Packit 1470ea
      <email its:translate="no">sebp@k-d-w.org</email>
Packit 1470ea
      <years>2011</years>
Packit 1470ea
    </credit>
Packit 1470ea
Packit 1470ea
    <credit type="author copyright editor">
Packit 1470ea
      <name>Marta Maria Casetti</name>
Packit 1470ea
      <email its:translate="no">mmcasetti@gmail.com</email>
Packit 1470ea
      <years>2012</years>
Packit 1470ea
    </credit>
Packit 1470ea
Packit 1470ea
    <desc>Widget that displays a GtkTextBuffer</desc>
Packit 1470ea
  </info>
Packit 1470ea
Packit 1470ea
  <title>TextView</title>
Packit 1470ea
Packit 1470ea
  <note style="sidebar">

This is an example of Gtk.TextView.

Packit 1470ea
  

If we press "enter", we have a new line.

Packit 1470ea
  

But we can also have a new line if we write a long sentence (the text will wrap breaking lines between words).

Packit 1470ea
  

If we have a loooooooooooooooooooooooooooooooooooong

Packit 1470ea
  

(that was long)

Packit 1470ea
  

word, an horizontal scrollbar will appear.

</note>
Packit 1470ea
Packit 1470ea
  <media type="image" mime="image/png" src="media/textview.png"/>
Packit 1470ea
Packit 1470ea
  <links type="section"/>
Packit 1470ea
Packit 1470ea
  <section id="code">
Packit 1470ea
  <title>Code used to generate this example</title>
Packit 1470ea
Packit 1470ea
  from gi.repository import Gtk
Packit 1470ea
import sys
Packit 1470ea
Packit 1470ea
Packit 1470ea
class MyWindow(Gtk.ApplicationWindow):
Packit 1470ea
Packit 1470ea
    def __init__(self, app):
Packit 1470ea
        Gtk.Window.__init__(self, title="TextView Example", application=app)
Packit 1470ea
        self.set_default_size(300, 450)
Packit 1470ea
Packit 1470ea
        # a scrollbar for the child widget (that is going to be the textview)
Packit 1470ea
        scrolled_window = Gtk.ScrolledWindow()
Packit 1470ea
        scrolled_window.set_border_width(5)
Packit 1470ea
        # we scroll only if needed
Packit 1470ea
        scrolled_window.set_policy(
Packit 1470ea
            Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
Packit 1470ea
Packit 1470ea
        # a text buffer (stores text)
Packit 1470ea
        buffer1 = Gtk.TextBuffer()
Packit 1470ea
Packit 1470ea
        # a textview (displays the buffer)
Packit 1470ea
        textview = Gtk.TextView(buffer=buffer1)
Packit 1470ea
        # wrap the text, if needed, breaking lines in between words
Packit 1470ea
        textview.set_wrap_mode(Gtk.WrapMode.WORD)
Packit 1470ea
Packit 1470ea
        # textview is scrolled
Packit 1470ea
        scrolled_window.add(textview)
Packit 1470ea
Packit 1470ea
        self.add(scrolled_window)
Packit 1470ea
Packit 1470ea
Packit 1470ea
class MyApplication(Gtk.Application):
Packit 1470ea
Packit 1470ea
    def __init__(self):
Packit 1470ea
        Gtk.Application.__init__(self)
Packit 1470ea
Packit 1470ea
    def do_activate(self):
Packit 1470ea
        win = MyWindow(self)
Packit 1470ea
        win.show_all()
Packit 1470ea
Packit 1470ea
    def do_startup(self):
Packit 1470ea
        Gtk.Application.do_startup(self)
Packit 1470ea
Packit 1470ea
app = MyApplication()
Packit 1470ea
exit_status = app.run(sys.argv)
Packit 1470ea
sys.exit(exit_status)
Packit 1470ea
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="methods">
Packit 1470ea
  <title>Useful methods for a TextView widget</title>
Packit 1470ea
  

A Gtk.TextView displays the text stored in a Gtk.TextBuffer. However, most text manipulation is accomplished with iterators, represented by a Gtk.TextIter - a position between two characters in the text buffer. Iterators are not valid indefinitely; whenever the buffer is modified in a way that affects the contents of the buffer, all outstanding iterators become invalid. Because of this, iterators can’t be used to preserve positions across buffer modifications. To preserve a position, we use a Gtk.TextMark, that can be set visible with visible(True). A text buffer contains two built-in marks; an "insert" mark (the position of the cursor) and the "selection_bound" mark.

Packit 1470ea
  

Methods for a TextView widget:

Packit 1470ea
  <list>
Packit 1470ea
    <item>

The TextView widget is by default editable. If you prefer otherwise, use set_editable(False). If the buffer has no editable text, it might be a good idea to use set_cursor_visible(False) as well.

</item>
Packit 1470ea
    <item>

The justification of the text is set with set_justification(Gtk.Justification.JUSTIFICATION) where JUSTIFICATION is one of LEFT, RIGHT, CENTER, FILL.

</item>
Packit 1470ea
    <item>

The line wrapping of the text is set with set_wrap_mode(Gtk.WrapMode.WRAP) where WRAP is one of NONE (the text area is made wider), CHAR (break lines anywhere the cursor can appear), WORD (break lines between words), WORD_CHAR (break lines between words, but if that is not enough between characters).

</item>
Packit 1470ea
  </list>
Packit 1470ea
  

Methods for a TextBuffer widget:

Packit 1470ea
  <list>
Packit 1470ea
    <item>

get_insert() returns the Gtk.TextMark that represents the cursor, that is the insertion point.

</item>
Packit 1470ea
    <item>

get_selection_bound() returns the Gtk.TextMark that represents the selection bound.

</item>
Packit 1470ea
    <item>

set_text("some text", length) where length is a positive integer or -1, sets the content of the buffer as the first length characters of the "some text" text. If length is omitted or -1, the text is inserted completely. The content of the buffer, if there is any, is destroyed.

</item>
Packit 1470ea
    <item>

insert(iter, "some text", length) where iter is a text iterator and length is a positive integer or -1, inserts in the buffer at iter the first length characters of the "some text" text. If length is omitted or -1, the text is inserted completely.

</item>
Packit 1470ea
    <item>

insert_at_cursor("some text", length) does the same as insert(iter, "some text", length), with the current cursor taken as iter.

</item>
Packit 1470ea
    <item>

create_mark("mark_name", iter, left_gravity) where iter is a Gtk.TextIter and left_gravity is a boolean, creates a Gtk.TextMark at the position of iter. If "mark_name" is None, the mark is anonymous; otherwise, the mark can be retrieved by name using get_mark(). If a mark has left gravity, and text is inserted at the mark’s current location, the mark will be moved to the left of the newly-inserted text. If left_gravity is omitted, it defaults to False.

Packit 1470ea

</item>
Packit 1470ea
    <item>

To specify that some text in the buffer should have specific formatting, you must define a tag to hold that formatting information, and then apply that tag to the region of text using create_tag("tag name", property) and apply_tag(tag, start_iter, end_iter) as in, for instance:

Packit 1470ea
      
Packit 1470ea
tag = textbuffer.create_tag("orange_bg", background="orange")
Packit 1470ea
textbuffer.apply_tag(tag, start_iter, end_iter)
Packit 1470ea
     

The following are some of the common styles applied to text:

Packit 1470ea
      <list>
Packit 1470ea
        <item>

Background colour ("background" property)

</item>
Packit 1470ea
        <item>

Foreground colour ("foreground" property)

</item>
Packit 1470ea
        <item>

Underline ("underline" property)

</item>
Packit 1470ea
        <item>

Bold ("weight" property)

</item>
Packit 1470ea
        <item>

Italics ("style" property)

</item>
Packit 1470ea
        <item>

Strikethrough ("strikethrough" property)

</item>
Packit 1470ea
        <item>

Justification ("justification" property)

</item>
Packit 1470ea
        <item>

Size ("size" and "size-points" properties)

</item>
Packit 1470ea
        <item>

Text wrapping ("wrap-mode" property)

</item>
Packit 1470ea
      </list>
Packit 1470ea
    

You can also delete particular tags later using remove_tag() or delete all tags in a given region by calling remove_all_tags().

</item>
Packit 1470ea
  </list>
Packit 1470ea
  

Methods for a TextIter widget

Packit 1470ea
  <list>
Packit 1470ea
    <item>

forward_search(needle, flags, limit) searches forward for needle. The search will not continue past the Gtk.TextIter limit. The flags can be set to one of the following, or any combination of it by concatenating them with the bitwise-OR operator |: 0 (the match must be exact); Gtk.TextSearchFlags.VISIBLE_ONLY (the match may have invisible text interspersed in needle); Gtk.TextSearchFlags.TEXT_ONLY (the match may have pixbufs or child widgets mixed inside the matched range); Gtk.TextSearchFlags.CASE_INSENSITIVE (the text will be matched regardless of what case it is in). The method returns a tuple containing a Gtk.TextIter pointing to the start and to the first character after the match; if no match is found, None is returned.

</item>
Packit 1470ea
    <item>

backward_search(needle, flags, limit) does the same as forward_search(), but moving backwards.

</item>
Packit 1470ea
  </list>
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="references">
Packit 1470ea
  <title>API References</title>
Packit 1470ea
  

In this sample we used the following:

Packit 1470ea
  <list>
Packit 1470ea
    <item>

<link href="http://developer.gnome.org/gtk3/unstable/GtkTextView.html">GtkTextView</link>

</item>
Packit 1470ea
    <item>

<link href="http://developer.gnome.org/gtk3/unstable/GtkTextBuffer.html">GtkTextBuffer</link>

</item>
Packit 1470ea
    <item>

<link href="http://developer.gnome.org/gtk3/unstable/GtkTextTag.html">GtkTextTag</link>

</item>
Packit 1470ea
    <item>

<link href="http://developer.gnome.org/gtk3/unstable/GtkScrolledWindow.html">GtkScrolledWindow</link>

</item>
Packit 1470ea
    <item>

<link href="http://developer.gnome.org/gtk3/unstable/gtk3-Standard-Enumerations.html">Standard Enumerations</link>

</item>
Packit 1470ea
  </list>
Packit 1470ea
  </section>
Packit 1470ea
</page>