2 * This file is part of DisOrder
3 * Copyright (C) 2007 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 /** @file disobedience/help.c
24 #include "disobedience.h"
28 VECTOR_TYPE(markstack, GtkTextMark *, xrealloc);
30 /** @brief Known tag type */
32 /** @brief HTML tag name */
35 /** @brief Called to set up the tag */
36 void (*init)(GtkTextTag *tag);
40 /** @brief Initialize the bold tag
42 * This doesn't seem to work on OS X though the italic and monospace tags are
43 * fine, and bold is OK on Linux, even connecting to the Apple X swerver.
45 static void init_bold(GtkTextTag *tag) {
46 g_object_set(G_OBJECT(tag), "weight", PANGO_WEIGHT_BOLD, (char *)0);
49 /** @brief Initialize the italic tag */
50 static void init_italic(GtkTextTag *tag) {
51 g_object_set(G_OBJECT(tag), "style", PANGO_STYLE_ITALIC, (char *)0);
54 /** @brief Initialize the pre tag */
55 static void init_pre(GtkTextTag *tag) {
56 g_object_set(G_OBJECT(tag), "family", "monospace", (char *)0);
59 /** @brief Table of known tags
61 * Keep in alphabetical order
63 static const struct tag tags[] = {
69 /** @brief Number of known tags */
70 #define NTAGS (sizeof tags / sizeof *tags)
72 /** @brief State structure for insert_html() */
74 /** @brief The buffer to insert into */
75 GtkTextBuffer *buffer;
77 /** @brief The buffer's tag table */
78 GtkTextTagTable *tagtable;
80 /** @brief True if we are inside <body> */
83 /** @brief True if inside <pre> */
86 /** @brief True if a space is required before any non-space */
89 /** @brief Stack of marks corresponding to tags */
90 struct markstack marks[1];
94 /** @brief Called for an open tag */
95 static void html_open(const char *tag,
96 hash attribute((unused)) *attrs,
98 struct state *const s = u;
101 if(!strcmp(tag, "body"))
103 else if(!strcmp(tag, "pre"))
107 /* push a mark for the start of the region */
108 gtk_text_buffer_get_iter_at_mark(s->buffer, iter,
109 gtk_text_buffer_get_insert(s->buffer));
110 markstack_append(s->marks,
111 gtk_text_buffer_create_mark(s->buffer,
114 TRUE/*left_gravity*/));
117 /** @brief Called for a close tag */
118 static void html_close(const char *tag,
120 struct state *const s = u;
121 GtkTextIter start[1], end[1];
124 if(!strcmp(tag, "body"))
126 else if(!strcmp(tag, "pre")) {
132 /* see if this is a known tag */
133 texttag = gtk_text_tag_table_lookup(s->tagtable, tag);
136 /* pop the mark at the start of the region */
137 assert(s->marks->nvec > 0);
138 gtk_text_buffer_get_iter_at_mark(s->buffer, start,
139 s->marks->vec[--s->marks->nvec]);
140 gtk_text_buffer_get_iter_at_mark(s->buffer, end,
141 gtk_text_buffer_get_insert(s->buffer));
143 gtk_text_buffer_apply_tag(s->buffer, texttag, start, end);
144 /* don't need the start mark any more */
145 gtk_text_buffer_delete_mark(s->buffer, s->marks->vec[s->marks->nvec]);
148 /** @brief Called for text */
149 static void html_text(const char *text,
151 struct state *const s = u;
157 char *formatted = xmalloc(strlen(text) + 1), *t = formatted;
158 /* normalize spacing */
160 if(isspace((unsigned char)*text)) {
174 gtk_text_buffer_insert_at_cursor(s->buffer, text, strlen(text));
177 /** @brief Callbacks for insert_html() */
178 static const struct html_parser_callbacks insert_html_callbacks = {
184 /** @brief Insert @p html into @p buffer at cursor */
185 static void insert_html(GtkTextBuffer *buffer,
190 memset(s, 0, sizeof *s);
192 markstack_init(s->marks);
193 /* initialize tags */
194 s->tagtable = gtk_text_buffer_get_tag_table(s->buffer);
195 for(n = 0; n < NTAGS; ++n) {
196 GtkTextTag *const tag = gtk_text_tag_new(tags[n].name);
198 gtk_text_tag_table_add(s->tagtable, tag);
200 /* convert the input */
201 html_parse(&insert_html_callbacks, html, s);
204 /** @brief Create a GtkTextBuffer with @p html rendered into it */
205 static GtkTextBuffer *html_buffer(const char *html) {
206 GtkTextBuffer *buffer = gtk_text_buffer_new(NULL);
208 insert_html(buffer, html);
212 /** @brief The manual page window */
213 static GtkWidget *help_window;
215 /** @brief Pop up the manual page in a window */
216 void popup_help(void) {
220 gtk_window_present(GTK_WINDOW(help_window));
223 help_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
224 g_signal_connect(help_window, "destroy",
225 G_CALLBACK(gtk_widget_destroyed), &help_window);
226 gtk_window_set_title(GTK_WINDOW(help_window), "Disobedience Manual Page");
227 view = gtk_text_view_new_with_buffer(html_buffer(manual));
228 gtk_text_view_set_editable(GTK_TEXT_VIEW(view), FALSE);
229 gtk_container_add(GTK_CONTAINER(help_window), scroll_widget(view));
230 gtk_window_set_default_size(GTK_WINDOW(help_window), 512, 512);
231 gtk_widget_show_all(help_window);