/src/wrappers/gtk/examples/text/text_demo.e

http://github.com/tybor/Liberty · Specman e · 195 lines · 34 code · 36 blank · 125 comment · 2 complexity · 1b5ad7ff52e599e9aecd548fbd516910 MD5 · raw file

  1. indexing
  2. description: "."
  3. copyright: "[
  4. Copyright (C) 2006 Paolo Redaelli, GTK+ team
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation; either version 2.1 of
  8. the License, or (at your option) any later version.
  9. This library is distributed in the hopeOA that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA
  17. ]"
  18. class TEXT_DEMO
  19. -- Overview of GtkTextBuffer, GtkTextView, and friends
  20. -- Conceptual Overview: GTK+ has an extremely powerful framework
  21. -- for multiline text editing. The primary objects involved in the
  22. -- process are GtkTextBuffer, which represents the text being
  23. -- edited, and GtkTextView, a widget which can display a
  24. -- GtkTextBuffer. Each buffer can be displayed by any number of
  25. -- views.
  26. -- One of the important things to remember about text in GTK+ is
  27. -- that it's in the UTF-8 encoding. This means that one character
  28. -- can be encoded as multiple bytes. Character counts are usually
  29. -- referred to as offsets, while byte counts are called indexes. If
  30. -- you confuse these two, things will work fine with ASCII, but as
  31. -- soon as your buffer contains multibyte characters, bad things
  32. -- will happen.
  33. -- Text in a buffer can be marked with tags. A tag is an attribute
  34. -- that can be applied to some range of text. For example, a tag
  35. -- might be called "bold" and make the text inside the tag
  36. -- bold. However, the tag concept is more general than that; tags
  37. -- don't have to affect appearance. They can instead affect the
  38. -- behavior of mouse and key presses, "lock" a range of text so the
  39. -- user can't edit it, or countless other things. A tag is
  40. -- represented by a GtkTextTag object. One GtkTextTag can be
  41. -- applied to any number of text ranges in any number of buffers.
  42. -- Each tag is stored in a GtkTextTagTable. A tag table defines a
  43. -- set of tags that can be used together. Each buffer has one tag
  44. -- table associated with it; only tags from that tag table can be
  45. -- used with the buffer. A single tag table can be shared between
  46. -- multiple buffers, however.
  47. -- Tags can have names, which is convenient sometimes (for example,
  48. -- you can name your tag that makes things bold "bold"), but they
  49. -- can also be anonymous (which is convenient if you're creating
  50. -- tags on-the-fly).
  51. -- Most text manipulation is accomplished with iterators,
  52. -- represented by a GtkTextIter. An iterator represents a position
  53. -- between two characters in the text buffer. GtkTextIter is a
  54. -- struct designed to be allocated on the stack; it's guaranteed to
  55. -- be copiable by value and never contain any heap-allocated
  56. -- data. Iterators are not valid indefinitely; whenever the buffer
  57. -- is modified in a way that affects the number of characters in
  58. -- the buffer, all outstanding iterators become invalid. (Note that
  59. -- deleting 5 characters and then reinserting 5 still invalidates
  60. -- iterators, though you end up with the same number of characters
  61. -- you pass through a state with a different number).
  62. -- Because of this, iterators can't be used to preserve positions
  63. -- across buffer modifications. To preserve a position, the
  64. -- GtkTextMark object is ideal. You can think of a mark as an
  65. -- invisible cursor or insertion point; it floats in the buffer,
  66. -- saving a position. If the text surrounding the mark is deleted,
  67. -- the mark remains in the position the text once occupied; if text
  68. -- is inserted at the mark, the mark ends up either to the left or
  69. -- to the right of the new text, depending on its gravity. The
  70. -- standard text cursor in left-to-right languages is a mark with
  71. -- right gravity, because it stays to the right of inserted text.
  72. -- Like tags, marks can be either named or anonymous. There are two
  73. -- marks built-in to GtkTextBuffer; these are named "insert" and
  74. -- "selection_bound" and refer to the insertion point and the
  75. -- boundary of the selection which is not the insertion point,
  76. -- respectively. If no text is selected, these two marks will be in
  77. -- the same position. You can manipulate what is selected and where
  78. -- the cursor appears by moving these marks around. ^[2]
  79. -- Text buffers always contain at least one line, but may be empty
  80. -- (that is, buffers can contain zero characters). The last line in
  81. -- the text buffer never ends in a line separator (such as
  82. -- newline); the other lines in the buffer always end in a line
  83. -- separator. Line separators count as characters when computing
  84. -- character counts and character offsets. Note that some Unicode
  85. -- line separators are represented with multiple bytes in UTF-8,
  86. -- and the two-character sequence "\r\n" is also considered a line
  87. -- separator.
  88. -- Simple Example
  89. -- The simplest usage of GtkTextView might look like this:
  90. -- GtkWidget *view;
  91. -- GtkTextBuffer *buffer;
  92. -- view = gtk_text_view_new ();
  93. -- buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
  94. -- gtk_text_buffer_set_text (buffer, "Hello, this is some text", -1);
  95. -- /* Now you might put the view in a container and display it on the
  96. -- * screen; when the user edits the text, signals on the buffer
  97. -- * will be emitted, such as "changed", "insert_text", and so on.
  98. -- */
  99. -- In many cases it's also convenient to first create the buffer
  100. -- with gtk_text_buffer_new(), then create a widget for that buffer
  101. -- with gtk_text_view_new_with_buffer(). Or you can change the
  102. -- buffer the widget displays after the widget is created with
  103. -- gtk_text_view_set_buffer().
  104. --Example of Changing Text Attributes: There are two ways to affect
  105. --text attributes in GtkTextView. You can change the default
  106. --attributes for a given GtkTextView, and you can apply tags that
  107. --change the attributes for a region of text. For text features
  108. --that come from the theme - such as font and foreground color -
  109. --use standard GtkWidget functions such as gtk_widget_modify_font()
  110. --or gtk_widget_modify_text(). For other attributes there are
  111. --dedicated methods on GtkTextView such as
  112. --gtk_text_view_set_tabs().
  113. -- GtkWidget *view;
  114. -- GtkTextBuffer *buffer;
  115. -- GtkTextIter start, end;
  116. -- PangoFontDescription *font_desc;
  117. -- GdkColor color;
  118. -- GtkTextTag *tag;
  119. -- view = gtk_text_view_new ();
  120. -- buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
  121. -- gtk_text_buffer_set_text (buffer, "Hello, this is some text", -1);
  122. -- /* Change default font throughout the widget */
  123. -- font_desc = pango_font_description_from_string ("Serif 15");
  124. -- gtk_widget_modify_font (view, font_desc);
  125. -- pango_font_description_free (font_desc);
  126. -- /* Change default color throughout the widget */
  127. -- gdk_color_parse ("green", &color);
  128. -- gtk_widget_modify_text (view, GTK_STATE_NORMAL, &color);
  129. -- /* Change left margin throughout the widget */
  130. -- gtk_text_view_set_left_margin (GTK_TEXT_VIEW (view), 30);
  131. -- /* Use a tag to change the color for just one part of the widget */
  132. -- tag = gtk_text_buffer_create_tag (buffer, "blue_foreground",
  133. -- "foreground", "blue", NULL);
  134. -- gtk_text_buffer_get_iter_at_offset (buffer, &start, 7);
  135. -- gtk_text_buffer_get_iter_at_offset (buffer, &end, 12);
  136. -- gtk_text_buffer_apply_tag (buffer, tag, &start, &end);
  137. -- The gtk-demo application that comes with GTK+ contains more example code
  138. -- for GtkTextView.
  139. -- ^[2] If you want to place the cursor in response to a user
  140. -- action, be sure to use gtk_text_buffer_place_cursor(), which
  141. -- moves both at once without causing a temporary selection (moving
  142. -- one then the other temporarily selects the range in between the
  143. -- old and new positions).
  144. inherit
  145. GTK
  146. ANY
  147. creation make
  148. feature
  149. make is
  150. do
  151. gtk.initialize -- instead of "initialize_gtk"
  152. create window.make
  153. window.show_all
  154. start := window.text.buffer.iter_at_offset(0)
  155. gtk.run_main_loop -- instead of "gtk_main"
  156. end
  157. window: TEXT_VIEW_WINDOW
  158. start: GTK_TEXT_ITER
  159. end