1 | /* simple.c |
2 | * Copyright (C) 2017 Red Hat, Inc |
3 | * Author: Benjamin Otte |
4 | * |
5 | * This library is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU Library General Public |
7 | * License as published by the Free Software Foundation; either |
8 | * version 2 of the License, or (at your option) any later version. |
9 | * |
10 | * This library is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | * Library General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU Library General Public |
16 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. |
17 | */ |
18 | #include <gtk/gtk.h> |
19 | |
20 | static GtkWidget *_margin; |
21 | static GtkWidget *_align; |
22 | static GtkWidget *_xalign; |
23 | static GtkWidget *_yalign; |
24 | |
25 | static void |
26 | highlight_at_mark (GtkTextBuffer *buffer, |
27 | GtkTextMark *mark, |
28 | gboolean on) |
29 | { |
30 | GtkTextIter iter, iter2; |
31 | |
32 | gtk_text_buffer_get_iter_at_mark (buffer, iter: &iter, mark); |
33 | iter2 = iter; |
34 | gtk_text_iter_forward_line (iter: &iter2); |
35 | |
36 | if (on) |
37 | gtk_text_buffer_apply_tag_by_name (buffer, name: "hihi" , start: &iter, end: &iter2); |
38 | else |
39 | gtk_text_buffer_remove_tag_by_name (buffer, name: "hihi" , start: &iter, end: &iter2); |
40 | } |
41 | |
42 | static void |
43 | go_forward_or_back (GtkButton *button, |
44 | GtkTextView *tv, |
45 | gboolean forward) |
46 | { |
47 | GtkTextBuffer *buffer; |
48 | GtkTextMark *mark; |
49 | GtkTextIter iter; |
50 | gboolean found; |
51 | |
52 | buffer = gtk_text_view_get_buffer (text_view: tv); |
53 | mark = gtk_text_buffer_get_mark (buffer, name: "mimi" ); |
54 | highlight_at_mark (buffer, mark, FALSE); |
55 | |
56 | gtk_text_buffer_get_iter_at_mark (buffer, iter: &iter, mark); |
57 | if (forward) |
58 | found = gtk_text_iter_forward_search (iter: &iter, str: "\n-----" , flags: 0, match_start: &iter, NULL, NULL); |
59 | else |
60 | found = gtk_text_iter_backward_search (iter: &iter, str: "\n-----" , flags: 0, match_start: &iter, NULL, NULL); |
61 | if (found) |
62 | { |
63 | double margin; |
64 | gboolean use_align; |
65 | double xalign, yalign; |
66 | |
67 | gtk_text_iter_forward_char (iter: &iter); |
68 | gtk_text_buffer_move_mark (buffer, mark, where: &iter); |
69 | highlight_at_mark (buffer, mark, TRUE); |
70 | |
71 | margin = gtk_spin_button_get_value (GTK_SPIN_BUTTON (_margin)); |
72 | use_align = gtk_check_button_get_active (GTK_CHECK_BUTTON (_align)); |
73 | xalign = gtk_spin_button_get_value (GTK_SPIN_BUTTON (_xalign)); |
74 | yalign = gtk_spin_button_get_value (GTK_SPIN_BUTTON (_yalign)); |
75 | |
76 | gtk_text_view_scroll_to_mark (text_view: tv, mark, within_margin: margin, use_align, xalign, yalign); |
77 | } |
78 | else |
79 | { |
80 | if (forward) |
81 | gtk_text_buffer_get_end_iter (buffer, iter: &iter); |
82 | else |
83 | gtk_text_buffer_get_start_iter (buffer, iter: &iter); |
84 | |
85 | gtk_text_buffer_move_mark (buffer, mark, where: &iter); |
86 | |
87 | gtk_widget_error_bell (GTK_WIDGET (button)); |
88 | } |
89 | } |
90 | |
91 | static void |
92 | go_forward (GtkButton *button, |
93 | GtkTextView *tv) |
94 | { |
95 | go_forward_or_back (button, tv, TRUE); |
96 | } |
97 | |
98 | static void |
99 | go_back (GtkButton *button, |
100 | GtkTextView *tv) |
101 | { |
102 | go_forward_or_back (button, tv, FALSE); |
103 | } |
104 | |
105 | int |
106 | main (int argc, char *argv[]) |
107 | { |
108 | GtkWidget *window, *box; |
109 | GtkWidget *sw, *tv; |
110 | GtkWidget *button, *box2; |
111 | GtkTextBuffer *buffer; |
112 | GtkTextIter iter; |
113 | GtkTextTag *tag; |
114 | GdkRGBA bg; |
115 | |
116 | gtk_init (); |
117 | |
118 | window = gtk_window_new (); |
119 | gtk_window_set_default_size (GTK_WINDOW (window), width: 400, height: 600); |
120 | |
121 | box = gtk_box_new (orientation: GTK_ORIENTATION_VERTICAL, spacing: 10); |
122 | |
123 | tv = gtk_text_view_new (); |
124 | gtk_text_view_set_left_margin (GTK_TEXT_VIEW (tv), left_margin: 10); |
125 | gtk_text_view_set_right_margin (GTK_TEXT_VIEW (tv), right_margin: 10); |
126 | gtk_text_view_set_top_margin (GTK_TEXT_VIEW (tv), top_margin: 10); |
127 | gtk_text_view_set_bottom_margin (GTK_TEXT_VIEW (tv), bottom_margin: 10); |
128 | |
129 | buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv)); |
130 | |
131 | if (argc > 1) |
132 | { |
133 | char *contents; |
134 | gsize size; |
135 | |
136 | if (g_file_get_contents (filename: argv[1], contents: &contents, length: &size, NULL)) |
137 | gtk_text_buffer_set_text (buffer, text: contents, len: size); |
138 | } |
139 | |
140 | gtk_text_buffer_get_start_iter (buffer, iter: &iter); |
141 | gtk_text_buffer_create_mark (buffer, mark_name: "mimi" , where: &iter, TRUE); |
142 | |
143 | tag = gtk_text_tag_new (name: "hihi" ); |
144 | bg.red = 0; |
145 | bg.green = 0; |
146 | bg.blue = 1; |
147 | bg.alpha = 0.3; |
148 | g_object_set (object: tag, first_property_name: "background-rgba" , &bg, NULL); |
149 | gtk_text_tag_table_add (table: gtk_text_buffer_get_tag_table (buffer), tag); |
150 | |
151 | sw = gtk_scrolled_window_new (); |
152 | gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), child: tv); |
153 | |
154 | gtk_widget_set_hexpand (widget: sw, TRUE); |
155 | gtk_widget_set_vexpand (widget: sw, TRUE); |
156 | gtk_box_append (GTK_BOX (box), child: sw); |
157 | |
158 | box2 = gtk_box_new (orientation: GTK_ORIENTATION_HORIZONTAL, spacing: 10); |
159 | gtk_box_append (GTK_BOX (box), child: box2); |
160 | |
161 | button = gtk_button_new_with_label (label: "Forward" ); |
162 | g_signal_connect (button, "clicked" , G_CALLBACK (go_forward), tv); |
163 | gtk_box_append (GTK_BOX (box2), child: button); |
164 | |
165 | button = gtk_button_new_with_label (label: "Back" ); |
166 | g_signal_connect (button, "clicked" , G_CALLBACK (go_back), tv); |
167 | gtk_box_append (GTK_BOX (box2), child: button); |
168 | |
169 | box2 = gtk_box_new (orientation: GTK_ORIENTATION_HORIZONTAL, spacing: 10); |
170 | gtk_box_append (GTK_BOX (box), child: box2); |
171 | gtk_box_append (GTK_BOX (box2), child: gtk_label_new (str: "Margin:" )); |
172 | _margin = gtk_spin_button_new_with_range (min: 0, max: 0.5, step: 0.1); |
173 | gtk_box_append (GTK_BOX (box2), child: _margin); |
174 | |
175 | box2 = gtk_box_new (orientation: GTK_ORIENTATION_HORIZONTAL, spacing: 10); |
176 | gtk_box_append (GTK_BOX (box), child: box2); |
177 | gtk_box_append (GTK_BOX (box2), child: gtk_label_new (str: "Align:" )); |
178 | _align = gtk_check_button_new (); |
179 | gtk_box_append (GTK_BOX (box2), child: _align); |
180 | _xalign = gtk_spin_button_new_with_range (min: 0, max: 1, step: 0.1); |
181 | gtk_box_append (GTK_BOX (box2), child: _xalign); |
182 | _yalign = gtk_spin_button_new_with_range (min: 0, max: 1, step: 0.1); |
183 | gtk_box_append (GTK_BOX (box2), child: _yalign); |
184 | |
185 | gtk_window_set_child (GTK_WINDOW (window), child: box); |
186 | |
187 | gtk_widget_show (widget: window); |
188 | |
189 | while (1) |
190 | g_main_context_iteration (NULL, TRUE); |
191 | |
192 | return 0; |
193 | } |
194 | |