Debian Codegraph @

demos/gtk-demo/textwidget.cc

raw
1 /* textwidget.cc
2 *
3 * Copyright (C) 2001-2002 The gtkmm Development Team
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 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 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
17 */
18
19 #include <cstring>
20 #include "textwidget.h"
21 #include <pangomm/fontdescription.h>
22
23 using std::strstr;
24 using std::strncmp;
25 using std::strlen;
26
27 TextWidget::TextWidget(bool is_source)
28 {
29 set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
30 set_shadow_type (Gtk::SHADOW_IN);
31
32 m_refTextBuffer = Gtk::TextBuffer::create();
33 m_TextView.set_buffer(m_refTextBuffer);
34 m_TextView.set_editable(false);
35 m_TextView.set_cursor_visible(false);
36 add(m_TextView);
37
38 if (is_source)
39 {
40 m_TextView.set_wrap_mode (Gtk::WRAP_NONE);
41
42 auto refTag = m_refTextBuffer->create_tag("source");
43 refTag->property_font() = "monospace";
44
45 refTag = m_refTextBuffer->create_tag("comment");
46 refTag->property_foreground() = "DodgerBlue";
47
48 refTag = m_refTextBuffer->create_tag("type");
49 refTag->property_foreground() = "ForestGreen";
50
51 refTag = m_refTextBuffer->create_tag("string");
52 refTag->property_foreground() = "RosyBrown";
53 refTag->property_weight() = Pango::WEIGHT_BOLD;
54
55 refTag = m_refTextBuffer->create_tag("control");
56 refTag->property_foreground() = "purple";
57
58 refTag = m_refTextBuffer->create_tag("preprocessor");
59 refTag->property_style() = Pango::STYLE_OBLIQUE;
60 refTag->property_foreground() = "burlywood4";
61
62 refTag = m_refTextBuffer->create_tag("function");
63 refTag->property_weight() = Pango::WEIGHT_BOLD;
64 refTag->property_foreground() = "DarkGoldenrod4";
65 }
66 else
67 {
68 // Make it a bit nicer for text.
69 m_TextView.set_wrap_mode (Gtk::WRAP_WORD);
70 m_TextView.set_pixels_above_lines(2);
71 m_TextView.set_pixels_below_lines(2);
72
73 Glib::RefPtr<Gtk::TextBuffer::Tag> refTag = m_refTextBuffer->create_tag("title");
74 refTag->property_font() = "Sans 18";
75 }
76 }
77
78
79 TextWidget::~TextWidget()
80 {
81 }
82
83 Glib::RefPtr<Gtk::TextBuffer> TextWidget::get_buffer()
84 {
85 return m_refTextBuffer;
86 }
87
88 void TextWidget::wipe()
89 {
90 Gtk::TextBuffer::iterator start, end;
91 m_refTextBuffer->get_bounds(start, end);
92 m_refTextBuffer->erase(start, end);
93 }
94
95
96 /* Stupid syntax highlighting.
97 *
98 * No regex was used in the making of this highlighting.
99 * It should only work for simple cases. This is good, as
100 * that's all we should have in the demos.
101 */
102 /* This code should not be used elsewhere, except perhaps as an example of how
103 * to iterate through a text buffer.
104 */
105 enum enumStates
106 {
107 STATE_NORMAL,
108 STATE_IN_COMMENT
109 };
110
111 static const char* tokens[] =
112 {
113 "/*",
114 "\"",
115 nullptr
116 };
117
118 static const char* types[] =
119 {
120 "static",
121 "const ",
122 "void",
123 "gint",
124 "int ",
125 "char ",
126 "gchar ",
127 "gfloat",
128 "float",
129 "gint8",
130 "gint16",
131 "gint32",
132 "guint",
133 "guint8",
134 "guint16",
135 "guint32",
136 "guchar",
137 "glong",
138 "gboolean",
139 "bool",
140 "gshort",
141 "gushort",
142 "gulong",
143 "gdouble",
144 "gldouble",
145 "gpointer",
146 "NULL",
147 "nullptr",
148 "GList",
149 "GSList",
150 "FALSE",
151 "false",
152 "TRUE",
153 "true",
154 "FILE ",
155 "GtkColorSelection ",
156 "GtkWidget ",
157 "GtkButton ",
158 "GdkColor ",
159 "GdkRectangle ",
160 "GdkEventExpose ",
161 "GdkGC ",
162 "GdkPixbufLoader ",
163 "GdkPixbuf ",
164 "GError",
165 "size_t",
166 nullptr
167 };
168
169 static const char* control[] =
170 {
171 " if ",
172 " while ",
173 " else",
174 " do ",
175 " for ",
176 "?",
177 ":",
178 "return ",
179 "goto ",
180 nullptr
181 };
182
183 typedef const char* constpch;
184
185 void
186 parse_chars (constpch text,
187 constpch* end_ptr,
188 enumStates* state,
189 constpch* tag,
190 bool start)
191 {
192 int i = 0;
193 const char* next_token = nullptr;
194
195 /* Handle comments first */
196 if(*state == STATE_IN_COMMENT)
197 {
198 *end_ptr = strstr (text, "*/");
199 if (*end_ptr)
200 {
201 *end_ptr += 2;
202 *state = STATE_NORMAL;
203 *tag = "comment";
204 }
205
206 return;
207 }
208
209 *tag = nullptr;
210 *end_ptr = nullptr;
211
212 /* check for comment */
213 if (!strncmp (text, "/*", 2))
214 {
215 *end_ptr = strstr (text, "*/");
216
217 if (*end_ptr)
218 *end_ptr += 2;
219 else
220 *state = STATE_IN_COMMENT;
221 *tag = "comment";
222
223 return;
224 }
225
226 /* check for preprocessor defines */
227 if (*text == '#' && start)
228 {
229 *end_ptr = nullptr;
230 *tag = "preprocessor";
231 return;
232 }
233
234 /* functions */
235 if (start && * text != '\t' && *text != ' ' && *text != '{' && *text != '}')
236 {
237 if (strstr (text, "("))
238 {
239 *end_ptr = strstr (text, "(");
240 *tag = "function";
241 return;
242 }
243 }
244
245 /* check for types */
246 for (i = 0; types[i] != nullptr; i++)
247 if (!strncmp (text, types[i], strlen (types[i])))
248 {
249 *end_ptr = text + strlen (types[i]);
250 *tag = "type";
251 return;
252 }
253
254 /* check for control */
255 for (i = 0; control[i] != nullptr; i++)
256 if (!strncmp (text, control[i], strlen (control[i])))
257 {
258 *end_ptr = text + strlen (control[i]);
259 *tag = "control";
260 return;
261 }
262
263 /* check for string */
264 if (text[0] == '"')
265 {
266 int maybe_escape = false;
267
268 *end_ptr = text + 1;
269 *tag = "string";
270
271 while (**end_ptr != '\000')
272 {
273 if (**end_ptr == '\"' && !maybe_escape)
274 {
275 *end_ptr += 1;
276 return;
277 }
278
279 if (**end_ptr == '\\')
280 maybe_escape = true;
281 else
282 maybe_escape = false;
283 *end_ptr += 1;
284 }
285 return;
286 }
287
288 /* not at the start of a tag. Find the next one. */
289 for (i = 0; tokens[i] != nullptr; i++)
290 {
291 next_token = strstr (text, tokens[i]);
292 if (next_token)
293 {
294 if (*end_ptr)
295 *end_ptr = (*end_ptr<next_token)?*end_ptr:next_token;
296 else
297 *end_ptr = next_token;
298 }
299 }
300
301 for (i = 0; types[i] != nullptr; i++)
302 {
303 next_token = strstr (text, types[i]);
304 if (next_token)
305 {
306 if (*end_ptr)
307 *end_ptr = (*end_ptr<next_token)?*end_ptr:next_token;
308 else
309 *end_ptr = next_token;
310 }
311 }
312
313 for (i = 0; control[i] != nullptr; i++)
314 {
315 next_token = strstr (text, control[i]);
316 if (next_token)
317 {
318 if (*end_ptr)
319 *end_ptr = (*end_ptr<next_token)?*end_ptr:next_token;
320 else
321 *end_ptr = next_token;
322 }
323 }
324
325 }
326
327
328 /* While not as cool as c-mode, this will do as a quick attempt at highlighting */
329 void TextWidget::fontify()
330 {
331 enumStates state = STATE_NORMAL;
332
333 Gtk::TextBuffer::iterator iterStart;
334 Gtk::TextBuffer::iterator iterEnd;
335 m_refTextBuffer->get_bounds(iterStart, iterEnd);
336 m_refTextBuffer->apply_tag_by_name("source", iterStart, iterEnd);
337
338 iterStart = m_refTextBuffer->get_iter_at_offset(0);
339
340 Gtk::TextBuffer::iterator iterNext = iterStart;
341 while(iterNext.forward_line())
342 {
343 bool start = true;
344 const Glib::ustring& str = iterStart.get_text(iterNext);
345 const gchar* start_ptr = str.c_str();
346
347 const gchar* end_ptr = nullptr;
348 const gchar* tag = nullptr;
349
350 do
351 {
352 parse_chars (start_ptr, &end_ptr, &state, &tag, start);
353
354 start = false;
355 Gtk::TextBuffer::iterator iterTmp;
356 if(end_ptr)
357 {
358 iterTmp = iterStart;
359 iterTmp.forward_chars(end_ptr - start_ptr);
360 }
361 else
362 {
363 iterTmp = iterNext;
364 }
365
366 if(tag)
367 m_refTextBuffer->apply_tag_by_name(tag, iterStart, iterTmp);
368
369 iterStart = iterTmp;
370 start_ptr = end_ptr;
371 }
372 while(end_ptr);
373
374 iterStart = iterNext;
375 }
376 }