File: textwidget.cc

package info (click to toggle)
gtkmm3.0 3.24.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 22,680 kB
  • sloc: xml: 121,333; cpp: 8,531; sh: 4,250; makefile: 262; perl: 236
file content (377 lines) | stat: -rw-r--r-- 7,934 bytes parent folder | download
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/* textwidget.cc
 *
 * Copyright (C) 2001-2002 The gtkmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, 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
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <cstring>
#include "textwidget.h"
#include <pangomm/fontdescription.h>

using std::strstr;
using std::strncmp;
using std::strlen;


TextWidget::TextWidget(bool is_source)
{
  set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
  set_shadow_type (Gtk::SHADOW_IN);

  m_refTextBuffer = Gtk::TextBuffer::create();
  m_TextView.set_buffer(m_refTextBuffer);
  m_TextView.set_editable(false);
  m_TextView.set_cursor_visible(false);
  add(m_TextView);

  if (is_source)
  {
    m_TextView.set_wrap_mode (Gtk::WRAP_NONE);

    Glib::RefPtr<Gtk::TextBuffer::Tag> refTag = m_refTextBuffer->create_tag("source");
    refTag->property_font() = "Courier 12";

    refTag = m_refTextBuffer->create_tag("comment");
    refTag->property_foreground() = "red";

    refTag = m_refTextBuffer->create_tag("type");
    refTag->property_foreground() = "ForestGreen";

    refTag = m_refTextBuffer->create_tag("string");

    refTag->property_foreground() = "RosyBrown";
    refTag->property_weight() = Pango::WEIGHT_BOLD;


    refTag = m_refTextBuffer->create_tag("control");
    refTag->property_foreground() = "purple";

    refTag = m_refTextBuffer->create_tag("preprocessor");
    refTag->property_style() = Pango::STYLE_OBLIQUE;
    refTag->property_foreground() = "burlywood4";

    refTag = m_refTextBuffer->create_tag("function");
    refTag->property_weight() = Pango::WEIGHT_BOLD;
    refTag->property_foreground() = "DarkGoldenrod4";
  }
  else
  {
    // Make it a bit nicer for text.
    m_TextView.set_wrap_mode (Gtk::WRAP_WORD);
    m_TextView.set_pixels_above_lines(2);
    m_TextView.set_pixels_below_lines(2);

    Glib::RefPtr<Gtk::TextBuffer::Tag> refTag = m_refTextBuffer->create_tag("title");
    refTag->property_font() = "Sans 18";
  }
}


TextWidget::~TextWidget()
{
}

Glib::RefPtr<Gtk::TextBuffer> TextWidget::get_buffer()
{
   return m_refTextBuffer;
}

void TextWidget::wipe()
{
  Gtk::TextBuffer::iterator start, end;
  m_refTextBuffer->get_bounds(start, end);
  m_refTextBuffer->erase(start, end);
}


/* Stupid syntax highlighting.
 *
 * No regex was used in the making of this highlighting.
 * It should only work for simple cases.  This is good, as
 * that's all we should have in the demos.
 */
/* This code should not be used elsewhere, except perhaps as an example of how
 * to iterate through a text buffer.
 */
enum enumStates
{
  STATE_NORMAL,
  STATE_IN_COMMENT
};

static const char* tokens[] =
{
  "/*",
  "\"",
  nullptr
};

static const char* types[] =
{
  "static",
  "const ",
  "void",
  "gint",
  "int ",
  "char ",
  "gchar ",
  "gfloat",
  "float",
  "gint8",
  "gint16",
  "gint32",
  "guint",
  "guint8",
  "guint16",
  "guint32",
  "guchar",
  "glong",
  "gboolean" ,
  "gshort",
  "gushort",
  "gulong",
  "gdouble",
  "gldouble",
  "gpointer",
  "NULL",
  "GList",
  "GSList",
  "FALSE",
  "TRUE",
  "FILE ",
  "GtkObject ",
  "GtkColorSelection ",
  "GtkWidget ",
  "GtkButton ",
  "GdkColor ",
  "GdkRectangle ",
  "GdkEventExpose ",
  "GdkGC ",
  "GdkPixbufLoader ",
  "GdkPixbuf ",
  "GError",
  "size_t",
  nullptr
};

static const char* control[] =
{
  " if ",
  " while ",
  " else",
  " do ",
  " for ",
  "?",
  ":",
  "return ",
  "goto ",
  nullptr
};

typedef const char* constpch;

void
parse_chars (constpch text,
       constpch* end_ptr,
       enumStates* state,
       constpch* tag,
       bool   start)
{
  int i = 0;
  const char* next_token = nullptr;

  /* Handle comments first */
  if(*state == STATE_IN_COMMENT)
  {
    *end_ptr = strstr (text, "*/");
    if (*end_ptr)
    {
      *end_ptr += 2;
      *state = STATE_NORMAL;
      *tag = "comment";
    }

    return;
  }

  *tag = nullptr;
  *end_ptr = nullptr;

  /* check for comment */
  if (!strncmp (text, "/*", 2))
  {
    *end_ptr = strstr (text, "*/");

    if (*end_ptr)
      *end_ptr += 2;
    else
      *state = STATE_IN_COMMENT;
    *tag = "comment";

    return;
  }

  /* check for preprocessor defines */
  if (*text == '#' && start)
  {
    *end_ptr = nullptr;
    *tag = "preprocessor";
    return;
  }

  /* functions */
  if (start && * text != '\t' && *text != ' ' && *text != '{' && *text != '}')
  {
    if (strstr (text, "("))
    {
      *end_ptr = strstr (text, "(");
      *tag = "function";
      return;
    }
  }

  /* check for types */
  for (i = 0; types[i] != nullptr; i++)
    if (!strncmp (text, types[i], strlen (types[i])))
    {
      *end_ptr = text + strlen (types[i]);
      *tag = "type";
      return;
    }

  /* check for control */
  for (i = 0; control[i] != nullptr; i++)
    if (!strncmp (text, control[i], strlen (control[i])))
    {
      *end_ptr = text + strlen (control[i]);
      *tag = "control";
      return;
    }

  /* check for string */
  if (text[0] == '"')
  {
    int maybe_escape = false;

    *end_ptr = text + 1;
    *tag = "string";

    while (**end_ptr != '\000')
    {
      if (**end_ptr == '\"' && !maybe_escape)
      {
        *end_ptr += 1;
        return;
      }

      if (**end_ptr == '\\')
        maybe_escape = true;
      else
        maybe_escape = false;
      *end_ptr += 1;
    }
    return;
  }

  /* not at the start of a tag.  Find the next one. */
  for (i = 0; tokens[i] != nullptr; i++)
  {
    next_token = strstr (text, tokens[i]);
    if (next_token)
    {
      if (*end_ptr)
        *end_ptr = (*end_ptr<next_token)?*end_ptr:next_token;
      else
        *end_ptr = next_token;
    }
  }

  for (i = 0; types[i] != nullptr; i++)
  {
    next_token = strstr (text, types[i]);
    if (next_token)
    {
      if (*end_ptr)
        *end_ptr = (*end_ptr<next_token)?*end_ptr:next_token;
      else
        *end_ptr = next_token;
    }
  }

  for (i = 0; control[i] != nullptr; i++)
  {
    next_token = strstr (text, control[i]);
    if (next_token)
    {
      if (*end_ptr)
        *end_ptr = (*end_ptr<next_token)?*end_ptr:next_token;
      else
        *end_ptr = next_token;
    }
  }

}


/* While not as cool as c-mode, this will do as a quick attempt at highlighting */
void TextWidget::fontify()
{
  enumStates state = STATE_NORMAL;

  Gtk::TextBuffer::iterator iterStart;
  Gtk::TextBuffer::iterator iterEnd;
  m_refTextBuffer->get_bounds(iterStart, iterEnd);
  m_refTextBuffer->apply_tag_by_name("source", iterStart, iterEnd);

  iterStart = m_refTextBuffer->get_iter_at_offset(0);

  Gtk::TextBuffer::iterator iterNext = iterStart;
  while(iterNext.forward_line())
  {
    bool start = true;
    const Glib::ustring& str = iterStart.get_text(iterNext);
    const gchar* start_ptr = str.c_str();

    const gchar* end_ptr = nullptr;
    const gchar* tag = nullptr;

    do
    {
      parse_chars (start_ptr, &end_ptr, &state, &tag, start);

      start = false;
      Gtk::TextBuffer::iterator iterTmp;
      if(end_ptr)
      {
        iterTmp = iterStart;
        iterTmp.forward_chars(end_ptr - start_ptr);
      }
      else
      {
        iterTmp = iterNext;
      }

      if(tag)
        m_refTextBuffer->apply_tag_by_name(tag, iterStart, iterTmp);

      iterStart = iterTmp;
      start_ptr = end_ptr;
    }
    while(end_ptr);

    iterStart = iterNext;
  }
}