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