1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
------------------------------------------------------------------------------
-- GtkAda - Ada95 binding for the Gimp Toolkit --
-- --
-- Copyright (C) 2003-2018, AdaCore --
-- --
-- This library is free software; you can redistribute it and/or modify it --
-- under terms of the GNU General Public License as published by the Free --
-- Software Foundation; either version 3, or (at your option) any later --
-- version. This library is distributed in the hope that it will be useful, --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. --
-- --
-- --
-- --
-- --
-- --
-- You should have received a copy of the GNU General Public License and --
-- a copy of the GCC Runtime Library Exception along with this program; --
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
-- <http://www.gnu.org/licenses/>. --
-- --
------------------------------------------------------------------------------
with Glib; use Glib;
with Gdk.RGBA; use Gdk.RGBA;
with Gtk; use Gtk;
with Gtk.Enums; use Gtk.Enums;
with Gtk.Scrolled_Window; use Gtk.Scrolled_Window;
with Gtk.Text_Tag_Table; use Gtk.Text_Tag_Table;
with Gtk.Text_Tag; use Gtk.Text_Tag;
with Gtk.Text_Iter; use Gtk.Text_Iter;
with Gtk.Text_Buffer; use Gtk.Text_Buffer;
with Gtk.Text_View; use Gtk.Text_View;
with Gtk.Text_Mark; use Gtk.Text_Mark;
with Gtk.Frame; use Gtk.Frame;
with Gtk.Widget; use Gtk.Widget;
with Pango.Font; use Pango.Font;
package body Create_Text_View is
procedure Insert_With_Tag
(Buffer : access Gtk_Text_Buffer_Record'Class;
Tag : String;
Text : String);
-- Insert some text with some special highlighting in the buffer.
-- Note: in a real application, one would pass a Gtk_Tag instead of a tag
-- name, to avoid a lookup in the tags table
----------
-- Help --
----------
function Help return String is
begin
return "A @bGtk_Text_View@B widget is a widget used to display any"
& " text graphically. It provides support for changing fonts, colors,"
& " background colors, as well as inserting pixmaps in the text."
& ASCII.LF
& "It is based on the model-view paradigm: the text itself is stored"
& " in a non-graphical object, a @bGtk_Text_Buffer@B, which has"
& " support for traversing the text through @bGtk_Text_Iter@B objects;"
& " This buffer is then associated with one or many @bGtk_Text_View@B"
& " which automatically reflect any change in the buffer."
& ASCII.LF
& "The text is fully editable";
end Help;
---------------------
-- Insert_With_Tag --
---------------------
procedure Insert_With_Tag
(Buffer : access Gtk_Text_Buffer_Record'Class;
Tag : String;
Text : String)
is
T : Gtk_Text_Tag;
Iter, Start_Iter : Gtk_Text_Iter;
Table : Gtk_Text_Tag_Table;
Result : Boolean;
pragma Warnings (Off, Result);
begin
Get_End_Iter (Buffer, Iter);
if Tag = "" then
Insert (Buffer, Iter, Text & ASCII.LF);
else
Table := Get_Tag_Table (Buffer);
T := Lookup (Table, Tag);
Insert (Buffer, Iter, Text & ASCII.LF);
Start_Iter := Iter;
Backward_Chars (Start_Iter, Text'Length + 1, Result);
Apply_Tag (Buffer, T, Start_Iter, Iter);
end if;
end Insert_With_Tag;
---------
-- Run --
---------
procedure Run (Frame : access Gtk.Frame.Gtk_Frame_Record'Class) is
Buffer : Gtk_Text_Buffer;
Tags : Gtk_Text_Tag_Table;
Tag : Gtk_Text_Tag;
Color : Gdk_RGBA;
View : Gtk_Text_View;
Scrolled : Gtk_Scrolled_Window;
Success : Boolean;
begin
Gtk_New (Tags);
-- Create the tags that will be used to change the rendering of the
-- text.
Parse (Color, "red", Success);
Gtk_New (Tag, "red_tag");
Add (Tags, Tag);
Set_Property (Tag, Foreground_Rgba_Property, Color);
Gtk_New (Tag, "courier_tag");
Add (Tags, Tag);
Set_Property (Tag, Font_Desc_Property, From_String ("Courier bold"));
Parse (Color, "green", Success);
Gtk_New (Tag, "green_tag");
Add (Tags, Tag);
Set_Property (Tag, Foreground_Rgba_Property, Color);
-- Create the buffer and the views as appropriate
Gtk_New (Buffer, Tags);
Set_Label (Frame, "Text View");
Gtk_New (View, Buffer);
-- Insert some random text
for Lien in 1 .. 2 loop
for Count in 1 .. 10 loop
Insert_With_Tag
(Buffer, "", "A normal line with no special highlight");
end loop;
Insert_With_Tag
(Buffer, "red_tag", "Some text in red, notice the use of tags");
Insert_With_Tag
(Buffer, "courier_tag", "The font can also be changed");
end loop;
-- Insert the view in the frame
Gtk_New (Scrolled);
Set_Policy (Scrolled, Policy_Always, Policy_Always);
Add (Scrolled, View);
Show_All (Scrolled);
Add (Frame, Scrolled);
declare
Iter, Begin_Result, End_Result : Gtk_Text_Iter;
Success : Boolean := False;
Mark : Gtk_Text_Mark;
begin
-- Create a mark referencing the first line in courier
Gtk_New (Mark, "My mark", True);
Get_Iter_At_Line (Buffer, Iter, 12);
Add_Mark (Buffer, Mark, Iter);
-- Replace the five first occurrences of the word "placeholder"
for J in 1 .. 5 loop
Get_Iter_At_Offset (Buffer, Iter, Char_Offset => 0);
-- Use forward search to find the next occurrence
Forward_Search (Iter, "special", Case_Insensitive,
Match_Start => Begin_Result,
Match_End => End_Result,
Result => Success);
-- Replace "special" with "particular"
if Success then
Delete (Buffer, Begin_Result, End_Result);
Insert (Buffer, Begin_Result, "particular");
end if;
end loop;
Forward_Line (Begin_Result, Success);
-- Delete the next five lines
for J in 1 .. 5 loop
if Success then
End_Result := Begin_Result;
Forward_To_Line_End (End_Result, Success);
Forward_Char (End_Result, Success);
if Success then
Delete (Buffer, Begin_Result, End_Result);
end if;
end if;
end loop;
-- See that the mark moved with the deletions
-- By modifying and coloring the line where the mark was set at
Get_Iter_At_Mark (Buffer, Iter, Mark);
Forward_Search (Iter, "normal", Case_Insensitive,
Match_Start => Begin_Result,
Match_End => End_Result,
Result => Success);
Forward_To_Line_End (End_Result, Success);
if Success then
Delete (Buffer, Begin_Result, End_Result);
Insert (Buffer, Begin_Result, "green line");
end if;
-- Color the whole modified line in green
Tag := Lookup (Tags, "green_tag");
Get_Iter_At_Mark (Buffer, Begin_Result, Mark);
End_Result := Begin_Result;
Forward_To_Line_End (End_Result, Success);
Apply_Tag (Buffer, Tag, Begin_Result, End_Result);
-- Remove the mark, we no longer need it
Delete_Mark (Buffer, Mark);
end;
end Run;
end Create_Text_View;
|