Rearrange Github pages-related Makefile targets
[pipeglade.git] / pipeglade.c
blob5d9f9e342a079ae6bcbb4490250ea853c93d1e89
1 /*
2 * Copyright (c) 2014-2017 Bert Burgemeister <trebbu@googlemail.com>
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #include <cairo-pdf.h>
25 #include <cairo-ps.h>
26 #include <cairo-svg.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <gtk/gtk.h>
30 #include <gtk/gtkunixprint.h>
31 #include <gtk/gtkx.h>
32 #include <inttypes.h>
33 #include <libxml/xpath.h>
34 #include <locale.h>
35 #include <math.h>
36 #include <pthread.h>
37 #include <stdio.h>
38 #include <stdarg.h>
39 #include <stdbool.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <sys/select.h>
43 #include <sys/stat.h>
44 #include <time.h>
45 #include <unistd.h>
47 #define VERSION "4.8.0"
48 #define BUFLEN 256
49 #define WHITESPACE " \t\n"
50 #define MAIN_WIN "main"
51 #define USAGE \
52 "usage: pipeglade [[-i in-fifo] " \
53 "[-o out-fifo] " \
54 "[-b] " \
55 "[-u glade-file.ui] " \
56 "[-e xid]\n" \
57 " [-l log-file] " \
58 "[-O err-file] " \
59 "[--display X-server]] | " \
60 "[-h |" \
61 "-G |" \
62 "-V]\n"
64 #define ABORT \
65 do { \
66 fprintf(stderr, \
67 "In %s (%s:%d): ", \
68 __func__, __FILE__, __LINE__); \
69 abort(); \
70 } while (0)
72 #define OOM_ABORT \
73 do { \
74 fprintf(stderr, \
75 "Out of memory in %s (%s:%d): ", \
76 __func__, __FILE__, __LINE__); \
77 abort(); \
78 } while (0)
82 * ============================================================
83 * Helper functions
84 * ============================================================
88 * Check if s1 and s2 are equal strings
90 static bool
91 eql(const char *s1, const char *s2)
93 return s1 != NULL && s2 != NULL && strcmp(s1, s2) == 0;
97 * Print a formatted message to stream s and give up with status
99 static void
100 bye(int status, FILE *s, const char *fmt, ...)
102 va_list ap;
104 va_start(ap, fmt);
105 vfprintf(s, fmt, ap);
106 va_end(ap);
107 exit(status);
110 static void
111 show_lib_versions(void)
113 bye(EXIT_SUCCESS, stdout,
114 "GTK+ v%d.%d.%d (running v%d.%d.%d)\n"
115 "cairo v%s (running v%s)\n",
116 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
117 gtk_get_major_version(), gtk_get_minor_version(),
118 gtk_get_micro_version(),
119 CAIRO_VERSION_STRING, cairo_version_string());
123 * XEmbed us if xid_s is given, or show a standalone window; give up
124 * on errors
126 static void
127 xembed_if(char *xid_s, GObject *main_window)
129 GtkWidget *plug, *body;
130 Window xid;
131 char xid_s2[BUFLEN];
133 if (xid_s == NULL) { /* standalone */
134 gtk_widget_show(GTK_WIDGET(main_window));
135 return;
137 /* We're being XEmbedded */
138 xid = strtoul(xid_s, NULL, 10);
139 snprintf(xid_s2, BUFLEN, "%lu", xid);
140 if (!eql(xid_s, xid_s2))
141 bye(EXIT_FAILURE, stderr,
142 "%s is not a valid XEmbed socket id\n", xid_s);
143 body = gtk_bin_get_child(GTK_BIN(main_window));
144 gtk_container_remove(GTK_CONTAINER(main_window), body);
145 plug = gtk_plug_new(xid);
146 if (!gtk_plug_get_embedded(GTK_PLUG(plug)))
147 bye(EXIT_FAILURE, stderr,
148 "unable to embed into XEmbed socket %s\n", xid_s);
149 gtk_container_add(GTK_CONTAINER(plug), body);
150 gtk_widget_show(plug);
154 * If requested, redirect stderr to file name
156 static void
157 redirect_stderr(const char *name)
159 if (name == NULL)
160 return;
161 if (freopen(name, "a", stderr) == NULL)
162 /* complaining on stdout since stderr is closed now */
163 bye(EXIT_FAILURE, stdout, "redirecting stderr to %s: %s\n",
164 name, strerror(errno));
165 if (fchmod(fileno(stderr), 0600) < 0)
166 bye(EXIT_FAILURE, stdout, "setting permissions of %s: %s\n",
167 name, strerror(errno));
168 setvbuf(stderr, NULL, _IOLBF, 0);
169 return;
173 * fork() if requested in bg; give up on errors
175 static void
176 go_bg_if(bool bg, FILE *in, FILE *out, char *err_file)
178 pid_t pid = 0;
180 if (!bg)
181 return;
182 if (in == stdin || out == stdout)
183 bye(EXIT_FAILURE, stderr,
184 "parameter -b requires both -i and -o\n");
185 pid = fork();
186 if (pid < 0)
187 bye(EXIT_FAILURE, stderr,
188 "going to background: %s\n", strerror(errno));
189 if (pid > 0)
190 bye(EXIT_SUCCESS, stdout, "%d\n", pid);
191 /* We're the child */
192 close(fileno(stdin)); /* making certain not-so-smart */
193 close(fileno(stdout)); /* system/run-shell commands happy */
194 if (err_file == NULL)
195 freopen("/dev/null", "w", stderr);
199 * Return the current locale and set it to "C". Should be free()d if
200 * done.
202 static char *
203 lc_numeric()
205 char *lc_orig;
206 char *lc = setlocale(LC_NUMERIC, NULL);
208 if ((lc_orig = malloc(strlen(lc) + 1)) == NULL)
209 OOM_ABORT;
210 strcpy(lc_orig, lc);
211 setlocale(LC_NUMERIC, "C");
212 return lc_orig;
216 * Set locale (back) to lc; free lc
218 static void
219 lc_numeric_free(char *lc)
221 setlocale(LC_NUMERIC, lc);
222 free(lc);
226 * Print a warning about a malformed command to stderr. Runs inside
227 * gtk_main().
229 static void
230 ign_cmd(GType type, const char *msg)
232 const char *name, *pad = " ";
234 if (type == G_TYPE_INVALID) {
235 name = "";
236 pad = "";
237 } else
238 name = g_type_name(type);
239 fprintf(stderr, "ignoring %s%scommand \"%s\"\n", name, pad, msg);
243 * Check if n is, or can be made, the name of a fifo, and put its
244 * struct stat into sb. Give up if n exists but is not a fifo.
246 static void
247 find_fifo(const char *n, struct stat *sb)
249 int fd;
251 if ((fd = open(n, O_RDONLY | O_NONBLOCK)) > -1) {
252 if (fstat(fd, sb) == 0 &&
253 S_ISFIFO(sb->st_mode) &&
254 fchmod(fd, 0600) == 0) {
255 fstat(fd, sb);
256 close(fd);
257 return;
259 bye(EXIT_FAILURE, stderr, "using pre-existing fifo %s: %s\n",
260 n, strerror(errno));
262 if (mkfifo(n, 0600) != 0)
263 bye(EXIT_FAILURE, stderr, "making fifo %s: %s\n",
264 n, strerror(errno));
265 find_fifo(n, sb);
268 static FILE *
269 open_fifo(const char *name, const char *fmode, FILE *fallback, int bmode)
271 FILE *s = NULL;
272 int fd;
273 struct stat sb1, sb2;
275 if (name == NULL)
276 s = fallback;
277 else {
278 find_fifo(name, &sb1);
279 /* TODO: O_RDWR on fifo is undefined in POSIX */
280 if (!((fd = open(name, O_RDWR)) > -1 &&
281 fstat(fd, &sb2) == 0 &&
282 sb1.st_mode == sb2.st_mode &&
283 sb1.st_ino == sb2.st_ino &&
284 sb1.st_dev == sb2.st_dev &&
285 (s = fdopen(fd, fmode)) != NULL))
286 bye(EXIT_FAILURE, stderr, "opening fifo %s (%s): %s\n",
287 name, fmode, strerror(errno));
289 setvbuf(s, NULL, bmode, 0);
290 return s;
294 * Create a log file if necessary, and open it. A name of "-"
295 * requests use of stderr.
297 static FILE *
298 open_log(const char *name)
300 FILE *s = NULL;
302 if (name == NULL)
303 return NULL;
304 if (eql(name, "-"))
305 return stderr;
306 if ((s = fopen(name, "a")) == NULL)
307 bye(EXIT_FAILURE, stderr, "opening log file %s: %s\n",
308 name, strerror(errno));
309 if (fchmod(fileno(s), 0600) < 0)
310 bye(EXIT_FAILURE, stderr, "setting permissions of %s: %s\n",
311 name, strerror(errno));
312 return s;
316 * Delete fifo fn if streams s and forbidden are distinct
318 static void
319 rm_unless(FILE *forbidden, FILE *s, char *fn)
321 if (s == forbidden)
322 return;
323 fclose(s);
324 remove(fn);
328 * Microseconds elapsed since start
330 static long int
331 usec_since(struct timespec *start)
333 struct timespec now;
335 clock_gettime(CLOCK_MONOTONIC, &now);
336 return (now.tv_sec - start->tv_sec) * 1e6 +
337 (now.tv_nsec - start->tv_nsec) / 1e3;
341 * Write string s to stream o, escaping newlines and backslashes
343 static void
344 fputs_escaped(const char *s, FILE *o)
346 size_t i = 0;
347 char c;
349 while ((c = s[i++]) != '\0')
350 switch (c) {
351 case '\\': fputs("\\\\", o); break;
352 case '\n': fputs("\\n", o); break;
353 default: putc(c, o); break;
358 * Write log file
360 static void
361 log_msg(FILE *l, char *msg)
363 static char *old_msg;
364 static struct timespec start;
366 if (l == NULL) /* no logging */
367 return;
368 if (msg == NULL && old_msg == NULL)
369 fprintf(l, "##########\t##### (New Pipeglade session) #####\n");
370 else if (msg == NULL && old_msg != NULL) {
371 /* command done; start idle */
372 fprintf(l, "%10ld\t", usec_since(&start));
373 fputs_escaped(old_msg, l);
374 putc('\n', l);
375 free(old_msg);
376 old_msg = NULL;
377 } else if (msg != NULL && old_msg == NULL) {
378 /* idle done; start command */
379 fprintf(l, "%10ld\t### (Idle) ###\n", usec_since(&start));
380 if ((old_msg = malloc(strlen(msg) + 1)) == NULL)
381 OOM_ABORT;
382 strcpy(old_msg, msg);
383 } else
384 ABORT;
385 clock_gettime(CLOCK_MONOTONIC, &start);
388 static bool
389 has_suffix(const char *s, const char *suffix)
391 int s_suf = strlen(s) - strlen(suffix);
393 if (s_suf < 0)
394 return false;
395 return eql(suffix, s + s_suf);
399 * Remove suffix from name; find the object named like this
401 static GObject *
402 obj_sans_suffix(GtkBuilder *builder, const char *suffix, const char *name)
404 char str[BUFLEN + 1] = {'\0'};
405 int str_l;
407 str_l = suffix - name;
408 strncpy(str, name, str_l < BUFLEN ? str_l : BUFLEN);
409 return gtk_builder_get_object(builder, str);
413 * Read UI definition from ui_file; give up on errors
415 static GtkBuilder *
416 builder_from_file(char *ui_file)
418 GError *error = NULL;
419 GtkBuilder *b;
421 b = gtk_builder_new();
422 if (gtk_builder_add_from_file(b, ui_file, &error) == 0)
423 bye(EXIT_FAILURE, stderr, "%s\n", error->message);
424 return b;
428 * Return the id attribute of widget
430 static const char *
431 widget_id(GtkBuildable *widget)
433 return gtk_buildable_get_name(widget);
437 * Get the main window; give up on errors
439 static GObject *
440 find_main_window(GtkBuilder *builder)
442 GObject *mw;
444 if (GTK_IS_WINDOW(mw = gtk_builder_get_object(builder, MAIN_WIN)))
445 return mw;
446 bye(EXIT_FAILURE, stderr, "no toplevel window with id \'" MAIN_WIN "\'\n");
447 return NULL; /* NOT REACHED */
451 * Store a line from stream s into buf, which should have been malloc'd
452 * to bufsize. Enlarge buf and bufsize if necessary.
454 static size_t
455 read_buf(FILE *s, char **buf, size_t *bufsize)
457 bool esc = false;
458 fd_set rfds;
459 int c;
460 int ifd = fileno(s);
461 size_t i = 0;
463 FD_ZERO(&rfds);
464 FD_SET(ifd, &rfds);
465 for (;;) {
466 select(ifd + 1, &rfds, NULL, NULL, NULL);
467 c = getc(s);
468 if (c == '\n' || feof(s))
469 break;
470 if (i >= *bufsize - 1)
471 if ((*buf = realloc(*buf, *bufsize *= 2)) == NULL)
472 OOM_ABORT;
473 if (esc) {
474 esc = false;
475 switch (c) {
476 case 'n': (*buf)[i++] = '\n'; break;
477 case 'r': (*buf)[i++] = '\r'; break;
478 default: (*buf)[i++] = c; break;
480 } else if (c == '\\')
481 esc = true;
482 else
483 (*buf)[i++] = c;
485 (*buf)[i] = '\0';
486 return i;
491 * ============================================================
492 * Receiving feedback from the GUI
493 * ============================================================
497 * Preclude triggering of GTK's default GtkLinkButton action which
498 * could otherwise interfere with pipeglade's own signal blocking
500 gboolean
501 gtk_show_uri(GdkScreen *s, const gchar *uri, guint32 ts, GError **e)
503 (void) s; (void) uri; (void) ts; (void) e;
504 return TRUE;
507 static void
508 send_msg_to(FILE* o, GtkBuildable *obj, const char *tag, va_list ap)
510 char *data;
511 const char *w_id = widget_id(obj);
512 fd_set wfds;
513 int ofd = fileno(o);
514 struct timeval timeout = {1, 0};
516 FD_ZERO(&wfds);
517 FD_SET(ofd, &wfds);
518 if (select(ofd + 1, NULL, &wfds, NULL, &timeout) == 1) {
519 fprintf(o, "%s:%s ", w_id, tag);
520 while ((data = va_arg(ap, char *)) != NULL)
521 fputs_escaped(data, o);
522 putc('\n', o);
523 } else
524 fprintf(stderr,
525 "send error; discarding feedback message %s:%s\n",
526 w_id, tag);
530 * Send GUI feedback to stream o. The message format is
531 * "<origin>:<tag> <data ...>". The variadic arguments are strings;
532 * last argument must be NULL.
534 static void
535 send_msg(FILE *o, GtkBuildable *obj, const char *tag, ...)
537 va_list ap;
539 va_start(ap, tag);
540 send_msg_to(o, obj, tag, ap);
541 va_end(ap);
545 * Send message from GUI to stream o. The message format is
546 * "<origin>:set <data ...>", which happens to be a legal command.
547 * The variadic arguments are strings; last argument must be NULL.
549 static void
550 send_msg_as_cmd(FILE *o, GtkBuildable *obj, const char *tag, ...)
552 va_list ap;
554 va_start(ap, tag);
555 send_msg_to(o, obj, "set", ap);
556 va_end(ap);
560 * Stuff to pass around
562 struct info {
563 FILE *fout; /* UI feedback messages */
564 FILE *fin; /* command input */
565 FILE *flog; /* logging output */
566 GtkBuilder *builder; /* to be read from .ui file */
567 GObject *obj;
568 GtkTreeModel *model;
569 char *txt;
570 char *data;
574 * Data to be passed to and from the GTK main loop
576 struct ui_data {
577 void (*fn)(struct ui_data *);
578 GObject *obj;
579 char *action;
580 char *data;
581 char *cmd;
582 char *cmd_tokens;
583 GType type;
584 struct info *args;
588 * Return pointer to a newly allocated struct info
590 struct info *
591 info_new_full(FILE *stream, GObject *obj, GtkTreeModel *model, char *txt)
593 struct info *ar;
595 if ((ar = malloc(sizeof(struct info))) == NULL)
596 OOM_ABORT;
597 ar->fout = stream;
598 ar->fin = NULL;
599 ar->flog = NULL;
600 ar->builder = NULL;
601 ar->obj = obj;
602 ar->model = model;
603 ar->txt = txt;
604 ar->data = NULL;
605 return ar;
608 struct info *
609 info_txt_new(FILE *stream, char *txt)
611 return info_new_full(stream, NULL, NULL, txt);
614 struct info *
615 info_obj_new(FILE *stream, GObject *obj, GtkTreeModel *model)
617 return info_new_full(stream, obj, model, NULL);
621 * Use msg_sender() to send a message describing a particular cell
623 static void
624 send_tree_cell_msg_by(void msg_sender(FILE *, GtkBuildable *, const char *, ...),
625 const char *path_s,
626 GtkTreeIter *iter, int col, struct info *ar)
628 GtkBuildable *obj = GTK_BUILDABLE(ar->obj);
629 GtkTreeModel *model = ar->model;
630 GType col_type;
631 GValue value = G_VALUE_INIT;
632 char str[BUFLEN], *lc = lc_numeric();
634 gtk_tree_model_get_value(model, iter, col, &value);
635 col_type = gtk_tree_model_get_column_type(model, col);
636 switch (col_type) {
637 case G_TYPE_INT:
638 snprintf(str, BUFLEN, " %d %d", col, g_value_get_int(&value));
639 msg_sender(ar->fout, obj, "gint", path_s, str, NULL);
640 break;
641 case G_TYPE_LONG:
642 snprintf(str, BUFLEN, " %d %ld", col, g_value_get_long(&value));
643 msg_sender(ar->fout, obj, "glong", path_s, str, NULL);
644 break;
645 case G_TYPE_INT64:
646 snprintf(str, BUFLEN, " %d %" PRId64, col, g_value_get_int64(&value));
647 msg_sender(ar->fout, obj, "gint64", path_s, str, NULL);
648 break;
649 case G_TYPE_UINT:
650 snprintf(str, BUFLEN, " %d %u", col, g_value_get_uint(&value));
651 msg_sender(ar->fout, obj, "guint", path_s, str, NULL);
652 break;
653 case G_TYPE_ULONG:
654 snprintf(str, BUFLEN, " %d %lu", col, g_value_get_ulong(&value));
655 msg_sender(ar->fout, obj, "gulong", path_s, str, NULL);
656 break;
657 case G_TYPE_UINT64:
658 snprintf(str, BUFLEN, " %d %" PRIu64, col, g_value_get_uint64(&value));
659 msg_sender(ar->fout, obj, "guint64", path_s, str, NULL);
660 break;
661 case G_TYPE_BOOLEAN:
662 snprintf(str, BUFLEN, " %d %d", col, g_value_get_boolean(&value));
663 msg_sender(ar->fout, obj, "gboolean", path_s, str, NULL);
664 break;
665 case G_TYPE_FLOAT:
666 snprintf(str, BUFLEN, " %d %f", col, g_value_get_float(&value));
667 msg_sender(ar->fout, obj, "gfloat", path_s, str, NULL);
668 break;
669 case G_TYPE_DOUBLE:
670 snprintf(str, BUFLEN, " %d %f", col, g_value_get_double(&value));
671 msg_sender(ar->fout, obj, "gdouble", path_s, str, NULL);
672 break;
673 case G_TYPE_STRING:
674 snprintf(str, BUFLEN, " %d ", col);
675 msg_sender(ar->fout, obj, "gchararray", path_s, str, g_value_get_string(&value), NULL);
676 break;
677 default:
678 fprintf(stderr, "column %d not implemented: %s\n", col, G_VALUE_TYPE_NAME(&value));
679 break;
681 g_value_unset(&value);
682 lc_numeric_free(lc);
686 * Use msg_sender() to send one message per column for a single row
688 static void
689 send_tree_row_msg_by(void msg_sender(FILE *, GtkBuildable *, const char *, ...),
690 char *path_s, GtkTreeIter *iter, struct info *ar)
692 int col;
694 for (col = 0; col < gtk_tree_model_get_n_columns(ar->model); col++)
695 send_tree_cell_msg_by(msg_sender, path_s, iter, col, ar);
699 * send_tree_row_msg serves as an argument for
700 * gtk_tree_selection_selected_foreach()
702 static gboolean
703 send_tree_row_msg(GtkTreeModel *model,
704 GtkTreePath *path, GtkTreeIter *iter, struct info *ar)
706 char *path_s = gtk_tree_path_to_string(path);
708 ar->model = model;
709 send_tree_row_msg_by(send_msg, path_s, iter, ar);
710 g_free(path_s);
711 return FALSE;
715 * save_tree_row_msg serves as an argument for
716 * gtk_tree_model_foreach().
717 * Send message from GUI to global stream "save".
719 static gboolean
720 save_tree_row_msg(GtkTreeModel *model,
721 GtkTreePath *path, GtkTreeIter *iter, struct info *ar)
723 char *path_s = gtk_tree_path_to_string(path);
725 ar->model = model;
726 send_tree_row_msg_by(send_msg_as_cmd, path_s, iter, ar);
727 g_free(path_s);
728 return FALSE;
731 static void
732 cb_calendar(GtkBuildable *obj, struct info *ar)
734 char str[BUFLEN];
735 unsigned int year = 0, month = 0, day = 0;
737 gtk_calendar_get_date(GTK_CALENDAR(obj), &year, &month, &day);
738 snprintf(str, BUFLEN, "%04u-%02u-%02u", year, ++month, day);
739 send_msg(ar->fout, obj, ar->txt, str, NULL);
742 static void
743 cb_color_button(GtkBuildable *obj, struct info *ar)
745 GdkRGBA color;
747 gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(obj), &color);
748 send_msg(ar->fout, obj, ar->txt, gdk_rgba_to_string(&color), NULL);
751 static void
752 cb_editable(GtkBuildable *obj, struct info *ar)
754 send_msg(ar->fout, obj, ar->txt, gtk_entry_get_text(GTK_ENTRY(obj)), NULL);
758 * Callback that sends a message about a pointer device button press
759 * in a GtkEventBox
761 static bool
762 cb_event_box_button(GtkBuildable *obj, GdkEvent *e, struct info *ar)
764 char data[BUFLEN], *lc = lc_numeric();
766 snprintf(data, BUFLEN, "%d %.1lf %.1lf",
767 e->button.button, e->button.x, e->button.y);
768 send_msg(ar->fout, obj, ar->txt, data, NULL);
769 lc_numeric_free(lc);
770 return true;
774 * Callback that sends in a message the name of the key pressed when
775 * a GtkEventBox is focused
777 static bool
778 cb_event_box_key(GtkBuildable *obj, GdkEvent *e, struct info *ar)
780 send_msg(ar->fout, obj, ar->txt, gdk_keyval_name(e->key.keyval), NULL);
781 return true;
785 * Callback that sends a message about pointer device motion in a
786 * GtkEventBox
788 static bool
789 cb_event_box_motion(GtkBuildable *obj, GdkEvent *e, struct info *ar)
791 char data[BUFLEN], *lc = lc_numeric();
793 snprintf(data, BUFLEN, "%.1lf %.1lf", e->button.x, e->button.y);
794 send_msg(ar->fout, obj, ar->txt, data, NULL);
795 lc_numeric_free(lc);
796 return true;
800 * Callback that only sends "name:tag" and returns false
802 static bool
803 cb_event_simple(GtkBuildable *obj, GdkEvent *e, struct info *ar)
805 (void) e;
806 send_msg(ar->fout, obj, ar->txt, NULL);
807 return false;
810 static void
811 cb_file_chooser_button(GtkBuildable *obj, struct info *ar)
813 send_msg(ar->fout, obj, ar->txt,
814 gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(obj)), NULL);
817 static void
818 cb_font_button(GtkBuildable *obj, struct info *ar)
820 send_msg(ar->fout, obj, ar->txt,
821 gtk_font_button_get_font_name(GTK_FONT_BUTTON(obj)), NULL);
824 static void
825 cb_menu_item(GtkBuildable *obj, struct info *ar)
827 send_msg(ar->fout, obj, ar->txt,
828 gtk_menu_item_get_label(GTK_MENU_ITEM(obj)), NULL);
831 static void
832 cb_range(GtkBuildable *obj, struct info *ar)
834 char str[BUFLEN], *lc = lc_numeric();
836 snprintf(str, BUFLEN, "%f", gtk_range_get_value(GTK_RANGE(obj)));
837 send_msg(ar->fout, obj, ar->txt, str, NULL);
838 lc_numeric_free(lc);
842 * Callback that sends user's selection from a file dialog
844 static void
845 cb_send_file_chooser_dialog_selection(struct info *ar)
847 send_msg(ar->fout, GTK_BUILDABLE(ar->obj), "file",
848 gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(ar->obj)),
849 NULL);
850 send_msg(ar->fout, GTK_BUILDABLE(ar->obj), "folder",
851 gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(ar->obj)),
852 NULL);
856 * Callback that sends in a message the content of the text buffer
857 * passed in user_data
859 static void
860 cb_send_text(GtkBuildable *obj, struct info *ar)
862 GtkTextIter a, b;
864 gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(ar->obj), &a, &b);
865 send_msg(ar->fout, obj, "text",
866 gtk_text_buffer_get_text(GTK_TEXT_BUFFER(ar->obj), &a, &b, TRUE),
867 NULL);
871 * Callback that sends in a message the highlighted text from the text
872 * buffer which was passed in user_data
874 static void
875 cb_send_text_selection(GtkBuildable *obj, struct info *ar)
877 GtkTextIter a, b;
879 gtk_text_buffer_get_selection_bounds(GTK_TEXT_BUFFER(ar->obj), &a, &b);
880 send_msg(ar->fout, obj, "text",
881 gtk_text_buffer_get_text(GTK_TEXT_BUFFER(ar->obj), &a, &b, TRUE),
882 NULL);
886 * Callback that only sends "name:tag data" and returns true
888 static bool
889 cb_simple(GtkBuildable *obj, struct info *ar)
891 send_msg(ar->fout, obj, ar->txt, ar->data, NULL);
892 return true;
895 static void
896 cb_spin_button(GtkBuildable *obj, struct info *ar)
898 char str[BUFLEN], *lc = lc_numeric();
900 snprintf(str, BUFLEN, "%f", gtk_spin_button_get_value(GTK_SPIN_BUTTON(obj)));
901 send_msg(ar->fout, obj, ar->txt, str, NULL);
902 lc_numeric_free(lc);
905 static void
906 cb_switch(GtkBuildable *obj, void *pspec, struct info *ar)
908 (void) pspec;
909 send_msg(ar->fout, obj,
910 gtk_switch_get_active(GTK_SWITCH(obj)) ? "1" : "0",
911 NULL);
914 static void
915 cb_toggle_button(GtkBuildable *obj, struct info *ar)
917 send_msg(ar->fout, obj,
918 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(obj)) ? "1" : "0",
919 NULL);
922 static void
923 cb_tree_selection(GtkBuildable *obj, struct info *ar)
925 GtkTreeSelection *sel = GTK_TREE_SELECTION(obj);
926 GtkTreeView *view = gtk_tree_selection_get_tree_view(sel);
928 ar->obj = G_OBJECT(view);
929 send_msg(ar->fout, GTK_BUILDABLE(view), ar->txt, NULL);
930 gtk_tree_selection_selected_foreach(
931 sel, (GtkTreeSelectionForeachFunc) send_tree_row_msg, ar);
936 * ============================================================
937 * cb_draw() maintains a drawing on a GtkDrawingArea; it needs a few
938 * helper functions
939 * ============================================================
943 * The set of supported drawing operations
945 enum cairo_fn {
946 ARC,
947 ARC_NEGATIVE,
948 CLOSE_PATH,
949 CURVE_TO,
950 FILL,
951 FILL_PRESERVE,
952 LINE_TO,
953 MOVE_TO,
954 RECTANGLE,
955 REL_CURVE_TO,
956 REL_LINE_TO,
957 REL_MOVE_TO,
958 REL_MOVE_FOR,
959 RESET_CTM,
960 SET_DASH,
961 SET_FONT_FACE,
962 SET_FONT_SIZE,
963 SET_LINE_CAP,
964 SET_LINE_JOIN,
965 SET_LINE_WIDTH,
966 SET_SOURCE_RGBA,
967 SHOW_TEXT,
968 STROKE,
969 STROKE_PRESERVE,
970 TRANSFORM,
974 * Text placement mode for rel_move_for()
976 enum ref_point {
988 enum draw_op_policy {
989 APPEND,
990 BEFORE,
991 REPLACE,
995 * One single element of a drawing
997 struct draw_op {
998 struct draw_op *next;
999 struct draw_op *prev;
1000 unsigned long long int id;
1001 unsigned long long int before;
1002 enum draw_op_policy policy;
1003 enum cairo_fn op;
1004 void *op_args;
1008 * Argument sets for the various drawing operations
1010 struct arc_args {
1011 double x;
1012 double y;
1013 double radius;
1014 double angle1;
1015 double angle2;
1018 struct curve_to_args {
1019 double x1;
1020 double y1;
1021 double x2;
1022 double y2;
1023 double x3;
1024 double y3;
1027 struct move_to_args {
1028 double x;
1029 double y;
1032 struct rectangle_args {
1033 double x;
1034 double y;
1035 double width;
1036 double height;
1039 struct rel_move_for_args {
1040 enum ref_point ref;
1041 int len;
1042 char text[];
1045 struct set_dash_args {
1046 int num_dashes;
1047 double dashes[];
1050 struct set_font_face_args {
1051 cairo_font_slant_t slant;
1052 cairo_font_weight_t weight;
1053 char family[];
1056 struct set_font_size_args {
1057 double size;
1060 struct set_line_cap_args {
1061 cairo_line_cap_t line_cap;
1064 struct set_line_join_args {
1065 cairo_line_join_t line_join;
1068 struct set_line_width_args {
1069 double width;
1072 struct set_source_rgba_args {
1073 GdkRGBA color;
1076 struct show_text_args {
1077 int len;
1078 char text[];
1081 struct transform_args {
1082 cairo_matrix_t matrix;
1085 static void
1086 draw(cairo_t *cr, enum cairo_fn op, void *op_args)
1088 switch (op) {
1089 case LINE_TO: {
1090 struct move_to_args *args = op_args;
1092 cairo_line_to(cr, args->x, args->y);
1093 break;
1095 case REL_LINE_TO: {
1096 struct move_to_args *args = op_args;
1098 cairo_rel_line_to(cr, args->x, args->y);
1099 break;
1101 case MOVE_TO: {
1102 struct move_to_args *args = op_args;
1104 cairo_move_to(cr, args->x, args->y);
1105 break;
1107 case REL_MOVE_TO: {
1108 struct move_to_args *args = op_args;
1110 cairo_rel_move_to(cr, args->x, args->y);
1111 break;
1113 case ARC: {
1114 struct arc_args *args = op_args;
1116 cairo_arc(cr, args->x, args->y, args->radius, args->angle1, args->angle2);
1117 break;
1119 case ARC_NEGATIVE: {
1120 struct arc_args *args = op_args;
1122 cairo_arc_negative(cr, args->x, args->y, args->radius, args->angle1, args->angle2);
1123 break;
1125 case CURVE_TO: {
1126 struct curve_to_args *args = op_args;
1128 cairo_curve_to(cr, args->x1, args->y1, args->x2, args->y2, args->x3, args->y3);
1129 break;
1131 case REL_CURVE_TO: {
1132 struct curve_to_args *args = op_args;
1134 cairo_curve_to(cr, args->x1, args->y1, args->x2, args->y2, args->x3, args->y3);
1135 break;
1137 case RECTANGLE: {
1138 struct rectangle_args *args = op_args;
1140 cairo_rectangle(cr, args->x, args->y, args->width, args->height);
1141 break;
1143 case CLOSE_PATH:
1144 cairo_close_path(cr);
1145 break;
1146 case SHOW_TEXT: {
1147 struct show_text_args *args = op_args;
1149 cairo_show_text(cr, args->text);
1150 break;
1152 case REL_MOVE_FOR: {
1153 cairo_text_extents_t e;
1154 double dx = 0.0, dy = 0.0;
1155 struct rel_move_for_args *args = op_args;
1157 cairo_text_extents(cr, args->text, &e);
1158 switch (args->ref) {
1159 case C: dx = -e.width / 2; dy = e.height / 2; break;
1160 case E: dx = -e.width; dy = e.height / 2; break;
1161 case N: dx = -e.width / 2; dy = e.height; break;
1162 case NE: dx = -e.width; dy = e.height; break;
1163 case NW: dy = e.height; break;
1164 case S: dx = -e.width / 2; break;
1165 case SE: dx = -e.width; break;
1166 case SW: break;
1167 case W: dy = e.height / 2; break;
1168 default: ABORT; break;
1170 cairo_rel_move_to(cr, dx, dy);
1171 break;
1173 case RESET_CTM:
1174 cairo_identity_matrix(cr);
1175 break;
1176 case STROKE:
1177 cairo_stroke(cr);
1178 break;
1179 case STROKE_PRESERVE:
1180 cairo_stroke_preserve(cr);
1181 break;
1182 case FILL:
1183 cairo_fill(cr);
1184 break;
1185 case FILL_PRESERVE:
1186 cairo_fill_preserve(cr);
1187 break;
1188 case SET_DASH: {
1189 struct set_dash_args *args = op_args;
1191 cairo_set_dash(cr, args->dashes, args->num_dashes, 0);
1192 break;
1194 case SET_FONT_FACE: {
1195 struct set_font_face_args *args = op_args;
1197 cairo_select_font_face(cr, args->family, args->slant, args->weight);
1198 break;
1200 case SET_FONT_SIZE: {
1201 struct set_font_size_args *args = op_args;
1203 cairo_set_font_size(cr, args->size);
1204 break;
1206 case SET_LINE_CAP: {
1207 struct set_line_cap_args *args = op_args;
1209 cairo_set_line_cap(cr, args->line_cap);
1210 break;
1212 case SET_LINE_JOIN: {
1213 struct set_line_join_args *args = op_args;
1215 cairo_set_line_join(cr, args->line_join);
1216 break;
1218 case SET_LINE_WIDTH: {
1219 struct set_line_width_args *args = op_args;
1221 cairo_set_line_width(cr, args->width);
1222 break;
1224 case SET_SOURCE_RGBA: {
1225 struct set_source_rgba_args *args = op_args;
1227 gdk_cairo_set_source_rgba(cr, &args->color);
1228 break;
1230 case TRANSFORM: {
1231 struct transform_args *args = op_args;
1233 cairo_transform(cr, &args->matrix);
1234 break;
1236 default:
1237 ABORT;
1238 break;
1243 * Callback that draws on a GtkDrawingArea
1245 static gboolean
1246 cb_draw(GtkWidget *widget, cairo_t *cr, gpointer data)
1248 struct draw_op *op;
1250 (void) data;
1251 for (op = g_object_get_data(G_OBJECT(widget), "draw_ops");
1252 op != NULL;
1253 op = op->next)
1254 draw(cr, op->op, op->op_args);
1255 return FALSE;
1260 * ============================================================
1261 * Manipulating the GUI
1262 * ============================================================
1266 * Generic actions that are applicable to most widgets
1270 * Simulate user activity on various widgets. Runs inside gtk_main().
1272 static void
1273 fake_ui_activity(struct ui_data *ud)
1275 char dummy;
1277 if (!GTK_IS_WIDGET(ud->obj) || sscanf(ud->data, " %c", &dummy) > 0)
1278 ign_cmd(ud->type, ud->cmd);
1279 else if (GTK_IS_SPIN_BUTTON(ud->obj)) {
1280 ud->args->txt = "text";
1281 cb_spin_button(GTK_BUILDABLE(ud->obj), ud->args); /* TODO: rename to "value" */
1282 } else if (GTK_IS_SCALE(ud->obj)) {
1283 ud->args->txt = "value";
1284 cb_range(GTK_BUILDABLE(ud->obj), ud->args);
1285 } else if (GTK_IS_ENTRY(ud->obj)) {
1286 ud->args->txt = "text";
1287 cb_editable(GTK_BUILDABLE(ud->obj), ud->args);
1288 } else if (GTK_IS_CALENDAR(ud->obj)) {
1289 ud->args->txt = "clicked";
1290 cb_calendar(GTK_BUILDABLE(ud->obj), ud->args);
1291 } else if (GTK_IS_FILE_CHOOSER_BUTTON(ud->obj)) {
1292 ud->args->txt = "file";
1293 cb_file_chooser_button(GTK_BUILDABLE(ud->obj), ud->args);
1294 } else if (!gtk_widget_activate(GTK_WIDGET(ud->obj)))
1295 ign_cmd(ud->type, ud->cmd);
1298 static void
1299 update_focus(struct ui_data *ud){
1300 char dummy;
1302 if (GTK_IS_WIDGET(ud->obj) &&
1303 sscanf(ud->data, " %c", &dummy) < 1 &&
1304 gtk_widget_get_can_focus(GTK_WIDGET(ud->obj)))
1305 gtk_widget_grab_focus(GTK_WIDGET(ud->obj));
1306 else
1307 ign_cmd(ud->type, ud->cmd);
1311 * Have the widget say "ping". Runs inside gtk_main().
1313 static void
1314 ping(struct ui_data *ud)
1316 if (!GTK_IS_WIDGET(ud->obj))
1317 ign_cmd(ud->type, ud->cmd);
1318 ud->args->txt = "ping";
1319 ud->args->data = ud->data;
1320 cb_simple(GTK_BUILDABLE(ud->obj), ud->args);
1324 * Write snapshot of widget in an appropriate format to file
1326 static void
1327 take_snapshot(struct ui_data *ud)
1329 cairo_surface_t *sur = NULL;
1330 cairo_t *cr = NULL;
1331 int height;
1332 int width;
1334 if (!GTK_IS_WIDGET(ud->obj) ||
1335 !gtk_widget_is_drawable(GTK_WIDGET(ud->obj))) {
1336 ign_cmd(ud->type, ud->cmd);
1337 return;
1339 height = gtk_widget_get_allocated_height(GTK_WIDGET(ud->obj));
1340 width = gtk_widget_get_allocated_width(GTK_WIDGET(ud->obj));
1341 if (has_suffix(ud->data, ".epsf") || has_suffix(ud->data, ".eps")) {
1342 sur = cairo_ps_surface_create(ud->data, width, height);
1343 cairo_ps_surface_set_eps(sur, TRUE);
1344 } else if (has_suffix(ud->data, ".pdf"))
1345 sur = cairo_pdf_surface_create(ud->data, width, height);
1346 else if (has_suffix(ud->data, ".ps"))
1347 sur = cairo_ps_surface_create(ud->data, width, height);
1348 else if (has_suffix(ud->data, ".svg"))
1349 sur = cairo_svg_surface_create(ud->data, width, height);
1350 else {
1351 ign_cmd(ud->type, ud->cmd);
1352 return;
1354 cr = cairo_create(sur);
1355 gtk_widget_draw(GTK_WIDGET(ud->obj), cr);
1356 cairo_destroy(cr);
1357 cairo_surface_destroy(sur);
1360 struct handler_id {
1361 unsigned int id; /* returned by g_signal_connect() and friends */
1362 bool blocked; /* we avoid multiple blocking/unblocking */
1363 struct handler_id *next;
1366 static void
1367 update_blocked(struct ui_data *ud)
1369 char dummy;
1370 struct handler_id *hid;
1371 unsigned long int val;
1373 if (sscanf(ud->data, "%lu %c", &val, &dummy) == 1 && val < 2) {
1374 for (hid = g_object_get_data(ud->obj, "signal-id");
1375 hid != NULL; hid = hid->next) {
1376 if (val == 0 && hid->blocked == true) {
1377 g_signal_handler_unblock(ud->obj, hid->id);
1378 hid->blocked = false;
1379 } else if (val == 1 && hid->blocked == false) {
1380 g_signal_handler_block(ud->obj, hid->id);
1381 hid->blocked = true;
1384 } else
1385 ign_cmd(ud->type, ud->cmd);
1388 static void
1389 update_sensitivity(struct ui_data *ud)
1391 char dummy;
1392 unsigned int val;
1394 if (GTK_IS_WIDGET(ud->obj) &&
1395 sscanf(ud->data, "%u %c", &val, &dummy) == 1 && val < 2)
1396 gtk_widget_set_sensitive(GTK_WIDGET(ud->obj), val);
1397 else
1398 ign_cmd(ud->type, ud->cmd);
1401 static void
1402 update_size_request(struct ui_data *ud)
1404 char dummy;
1405 int x, y;
1407 if (GTK_IS_WIDGET(ud->obj) &&
1408 sscanf(ud->data, "%d %d %c", &x, &y, &dummy) == 2)
1409 gtk_widget_set_size_request(GTK_WIDGET(ud->obj), x, y);
1410 else if (GTK_IS_WIDGET(ud->obj) &&
1411 sscanf(ud->data, " %c", &dummy) < 1)
1412 gtk_widget_set_size_request(GTK_WIDGET(ud->obj), -1, -1);
1413 else
1414 ign_cmd(ud->type, ud->cmd);
1417 static void
1418 update_tooltip_text(struct ui_data *ud)
1420 if (GTK_IS_WIDGET(ud->obj))
1421 gtk_widget_set_tooltip_text(GTK_WIDGET(ud->obj), ud->data);
1422 else
1423 ign_cmd(ud->type, ud->cmd);
1426 static void
1427 update_visibility(struct ui_data *ud)
1429 char dummy;
1430 unsigned int val;
1432 if (GTK_IS_WIDGET(ud->obj) &&
1433 sscanf(ud->data, "%u %c", &val, &dummy) == 1 && val < 2)
1434 gtk_widget_set_visible(GTK_WIDGET(ud->obj), val);
1435 else
1436 ign_cmd(ud->type, ud->cmd);
1440 * Change the style of the widget passed. Runs inside gtk_main().
1442 static void
1443 update_widget_style(struct ui_data *ud)
1445 GtkStyleContext *context;
1446 GtkStyleProvider *style_provider;
1447 char *style_decl;
1448 const char *prefix = "* {", *suffix = "}";
1449 size_t sz;
1451 if (!GTK_IS_WIDGET(ud->obj)) {
1452 ign_cmd(ud->type, ud->cmd);
1453 return;
1455 style_provider = g_object_get_data(ud->obj, "style_provider");
1456 sz = strlen(prefix) + strlen(suffix) + strlen(ud->data) + 1;
1457 context = gtk_widget_get_style_context(GTK_WIDGET(ud->obj));
1458 gtk_style_context_remove_provider(context, style_provider);
1459 if ((style_decl = malloc(sz)) == NULL)
1460 OOM_ABORT;
1461 strcpy(style_decl, prefix);
1462 strcat(style_decl, ud->data);
1463 strcat(style_decl, suffix);
1464 gtk_style_context_add_provider(context, style_provider,
1465 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
1466 gtk_css_provider_load_from_data(GTK_CSS_PROVIDER(style_provider),
1467 style_decl, -1, NULL);
1468 free(style_decl);
1472 * Check if one of the generic actions is requested; complain if none
1473 * of them is
1475 static void
1476 try_generic_cmds(struct ui_data *ud)
1478 if (eql(ud->action, "block"))
1479 update_blocked(ud);
1480 else if (eql(ud->action, "set_sensitive"))
1481 update_sensitivity(ud);
1482 else if (eql(ud->action, "set_visible"))
1483 update_visibility(ud);
1484 else if (eql(ud->action, "set_tooltip_text"))
1485 update_tooltip_text(ud);
1486 else if (eql(ud->action, "grab_focus"))
1487 update_focus(ud);
1488 else if (eql(ud->action, "set_size_request"))
1489 update_size_request(ud);
1490 else if (eql(ud->action, "style"))
1491 update_widget_style(ud);
1492 else if (eql(ud->action, "force"))
1493 fake_ui_activity(ud);
1494 else if (eql(ud->action, "ping"))
1495 ping(ud);
1496 else if (eql(ud->action, "snapshot"))
1497 take_snapshot(ud);
1498 else
1499 ign_cmd(ud->type, ud->cmd);
1503 * Manipulation of specific widgets
1506 static void
1507 update_button(struct ui_data *ud)
1509 if (eql(ud->action, "set_label"))
1510 gtk_button_set_label(GTK_BUTTON(ud->obj), ud->data);
1511 else
1512 try_generic_cmds(ud);
1515 static void
1516 update_calendar(struct ui_data *ud)
1518 GtkCalendar *calendar = GTK_CALENDAR(ud->obj);
1519 char dummy;
1520 int year = 0, month = 0, day = 0;
1522 if (eql(ud->action, "select_date") &&
1523 sscanf(ud->data, "%d-%d-%d %c", &year, &month, &day, &dummy) == 3) {
1524 if (month > -1 && month <= 11 && day > 0 && day <= 31) {
1525 gtk_calendar_select_month(calendar, --month, year);
1526 gtk_calendar_select_day(calendar, day);
1527 } else
1528 ign_cmd(ud->type, ud->cmd);
1529 } else if (eql(ud->action, "mark_day") &&
1530 sscanf(ud->data, "%d %c", &day, &dummy) == 1) {
1531 if (day > 0 && day <= 31)
1532 gtk_calendar_mark_day(calendar, day);
1533 else
1534 ign_cmd(ud->type, ud->cmd);
1535 } else if (eql(ud->action, "clear_marks") && sscanf(ud->data, " %c", &dummy) < 1)
1536 gtk_calendar_clear_marks(calendar);
1537 else
1538 try_generic_cmds(ud);
1542 * Common actions for various kinds of window. Return false if
1543 * command is ignored. Runs inside gtk_main().
1545 static bool
1546 update_class_window(struct ui_data *ud)
1548 GtkWindow *window = GTK_WINDOW(ud->obj);
1549 char dummy;
1550 int x, y;
1552 if (eql(ud->action, "set_title"))
1553 gtk_window_set_title(window, ud->data);
1554 else if (eql(ud->action, "fullscreen") && sscanf(ud->data, " %c", &dummy) < 1)
1555 gtk_window_fullscreen(window);
1556 else if (eql(ud->action, "unfullscreen") && sscanf(ud->data, " %c", &dummy) < 1)
1557 gtk_window_unfullscreen(window);
1558 else if (eql(ud->action, "resize") &&
1559 sscanf(ud->data, "%d %d %c", &x, &y, &dummy) == 2)
1560 gtk_window_resize(window, x, y);
1561 else if (eql(ud->action, "resize") && sscanf(ud->data, " %c", &dummy) < 1) {
1562 gtk_window_get_default_size(window, &x, &y);
1563 gtk_window_resize(window, x, y);
1564 } else if (eql(ud->action, "move") &&
1565 sscanf(ud->data, "%d %d %c", &x, &y, &dummy) == 2)
1566 gtk_window_move(window, x, y);
1567 else
1568 return false;
1569 return true;
1572 static void
1573 update_color_button(struct ui_data *ud)
1575 GdkRGBA color;
1577 if (eql(ud->action, "set_color")) {
1578 gdk_rgba_parse(&color, ud->data);
1579 gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(ud->obj), &color);
1580 } else
1581 try_generic_cmds(ud);
1584 static void
1585 update_combo_box_text(struct ui_data *ud)
1587 GtkComboBoxText *combobox = GTK_COMBO_BOX_TEXT(ud->obj);
1588 char dummy;
1589 int txt0, pos;
1591 if (eql(ud->action, "prepend_text"))
1592 gtk_combo_box_text_prepend_text(combobox, ud->data);
1593 else if (eql(ud->action, "append_text"))
1594 gtk_combo_box_text_append_text(combobox, ud->data);
1595 else if (eql(ud->action, "remove") &&
1596 sscanf(ud->data, "%d %c", &pos, &dummy) == 1)
1597 gtk_combo_box_text_remove(combobox, pos);
1598 else if (eql(ud->action, "insert_text") &&
1599 sscanf(ud->data, "%d %n", &pos, &txt0) == 1)
1600 gtk_combo_box_text_insert_text(combobox, pos, ud->data + txt0);
1601 else
1602 try_generic_cmds(ud);
1606 * update_drawing_area(), which runs inside gtk_main(), maintains a
1607 * list of drawing operations. It needs a few helper functions. It
1608 * is the responsibility of cb_draw() to actually execute the list.
1611 enum draw_op_stat {
1612 FAILURE,
1613 SUCCESS,
1614 NEED_REDRAW,
1618 * Fill structure *op with the drawing operation according to action
1619 * and with the appropriate set of arguments
1621 static enum draw_op_stat
1622 set_draw_op(struct draw_op *op, const char *action, const char *data)
1624 char dummy;
1625 const char *raw_args = data;
1626 enum draw_op_stat result = SUCCESS;
1627 int args_start = 0;
1629 if (sscanf(data, "=%llu %n", &op->id, &args_start) == 1) {
1630 op->policy = REPLACE;
1631 result = NEED_REDRAW;
1632 } else if (sscanf(data, "%llu<%llu %n", &op->id, &op->before, &args_start) == 2) {
1633 op->policy = BEFORE;
1634 result = NEED_REDRAW;
1635 } else if (sscanf(data, "%llu %n", &op->id, &args_start) == 1)
1636 op->policy = APPEND;
1637 else
1638 return FAILURE;
1639 raw_args += args_start;
1640 if (eql(action, "line_to")) {
1641 struct move_to_args *args;
1643 if ((args = malloc(sizeof(*args))) == NULL)
1644 OOM_ABORT;
1645 op->op = LINE_TO;
1646 op->op_args = args;
1647 if (sscanf(raw_args, "%lf %lf %c", &args->x, &args->y, &dummy) != 2)
1648 return FAILURE;
1649 } else if (eql(action, "rel_line_to")) {
1650 struct move_to_args *args;
1652 if ((args = malloc(sizeof(*args))) == NULL)
1653 OOM_ABORT;
1654 op->op = REL_LINE_TO;
1655 op->op_args = args;
1656 if (sscanf(raw_args, "%lf %lf %c", &args->x, &args->y, &dummy) != 2)
1657 return FAILURE;
1658 } else if (eql(action, "move_to")) {
1659 struct move_to_args *args;
1661 if ((args = malloc(sizeof(*args))) == NULL)
1662 OOM_ABORT;
1663 op->op = MOVE_TO;
1664 op->op_args = args;
1665 if (sscanf(raw_args, "%lf %lf %c", &args->x, &args->y, &dummy) != 2)
1666 return FAILURE;
1667 } else if (eql(action, "rel_move_to")) {
1668 struct move_to_args *args;
1670 if ((args = malloc(sizeof(*args))) == NULL)
1671 OOM_ABORT;
1672 op->op = REL_MOVE_TO;
1673 op->op_args = args;
1674 if (sscanf(raw_args, "%lf %lf %c", &args->x, &args->y, &dummy) != 2)
1675 return FAILURE;
1676 } else if (eql(action, "arc")) {
1677 struct arc_args *args;
1678 double deg1, deg2;
1680 if ((args = malloc(sizeof(*args))) == NULL)
1681 OOM_ABORT;
1682 op->op = ARC;
1683 op->op_args = args;
1684 if (sscanf(raw_args, "%lf %lf %lf %lf %lf %c",
1685 &args->x, &args->y, &args->radius, &deg1, &deg2, &dummy) != 5)
1686 return FAILURE;
1687 args->angle1 = deg1 * (M_PI / 180.L);
1688 args->angle2 = deg2 * (M_PI / 180.L);
1689 } else if (eql(action, "arc_negative")) {
1690 double deg1, deg2;
1691 struct arc_args *args;
1693 if ((args = malloc(sizeof(*args))) == NULL)
1694 OOM_ABORT;
1695 op->op = ARC_NEGATIVE;
1696 op->op_args = args;
1697 if (sscanf(raw_args, "%lf %lf %lf %lf %lf %c",
1698 &args->x, &args->y, &args->radius, &deg1, &deg2, &dummy) != 5)
1699 return FAILURE;
1700 args->angle1 = deg1 * (M_PI / 180.L);
1701 args->angle2 = deg2 * (M_PI / 180.L);
1702 } else if (eql(action, "curve_to")) {
1703 struct curve_to_args *args;
1705 if ((args = malloc(sizeof(*args))) == NULL)
1706 OOM_ABORT;
1707 op->op = CURVE_TO;
1708 op->op_args = args;
1709 if (sscanf(raw_args, "%lf %lf %lf %lf %lf %lf %c",
1710 &args->x1, &args->y1, &args->x2, &args->y2, &args->x3, &args->y3, &dummy) != 6)
1711 return FAILURE;
1712 } else if (eql(action, "rel_curve_to")) {
1713 struct curve_to_args *args;
1715 if ((args = malloc(sizeof(*args))) == NULL)
1716 OOM_ABORT;
1717 op->op = REL_CURVE_TO;
1718 op->op_args = args;
1719 if (sscanf(raw_args, "%lf %lf %lf %lf %lf %lf %c",
1720 &args->x1, &args->y1, &args->x2, &args->y2, &args->x3, &args->y3, &dummy) != 6)
1721 return FAILURE;
1722 } else if (eql(action, "rectangle")) {
1723 struct rectangle_args *args;
1725 if ((args = malloc(sizeof(*args))) == NULL)
1726 OOM_ABORT;
1727 op->op = RECTANGLE;
1728 op->op_args = args;
1729 if (sscanf(raw_args, "%lf %lf %lf %lf %c",
1730 &args->x, &args->y, &args->width, &args->height, &dummy) != 4)
1731 return FAILURE;
1732 } else if (eql(action, "close_path")) {
1733 op->op = CLOSE_PATH;
1734 if (sscanf(raw_args, " %c", &dummy) > 0)
1735 return FAILURE;
1736 op->op_args = NULL;
1737 } else if (eql(action, "show_text")) {
1738 struct show_text_args *args;
1739 int len;
1741 len = strlen(raw_args) + 1;
1742 if ((args = malloc(sizeof(*args) + len * sizeof(args->text[0]))) == NULL)
1743 OOM_ABORT;
1744 op->op = SHOW_TEXT;
1745 op->op_args = args;
1746 args->len = len; /* not used */
1747 strncpy(args->text, raw_args, len);
1748 result = NEED_REDRAW;
1749 } else if (eql(action, "rel_move_for")) {
1750 char ref_point[2 + 1];
1751 int start, len;
1752 struct rel_move_for_args *args;
1754 if (sscanf(raw_args, "%2s %n", ref_point, &start) < 1)
1755 return FAILURE;
1756 len = strlen(raw_args + start) + 1;
1757 if ((args = malloc(sizeof(*args) + len * sizeof(args->text[0]))) == NULL)
1758 OOM_ABORT;
1759 if (eql(ref_point, "c"))
1760 args->ref = C;
1761 else if (eql(ref_point, "e"))
1762 args->ref = E;
1763 else if (eql(ref_point, "n"))
1764 args->ref = N;
1765 else if (eql(ref_point, "ne"))
1766 args->ref = NE;
1767 else if (eql(ref_point, "nw"))
1768 args->ref = NW;
1769 else if (eql(ref_point, "s"))
1770 args->ref = S;
1771 else if (eql(ref_point, "se"))
1772 args->ref = SE;
1773 else if (eql(ref_point, "sw"))
1774 args->ref = SW;
1775 else if (eql(ref_point, "w"))
1776 args->ref = W;
1777 else
1778 return FAILURE;
1779 op->op = REL_MOVE_FOR;
1780 op->op_args = args;
1781 args->len = len; /* not used */
1782 strncpy(args->text, (raw_args + start), len);
1783 } else if (eql(action, "stroke")) {
1784 op->op = STROKE;
1785 if (sscanf(raw_args, " %c", &dummy) > 0)
1786 return FAILURE;
1787 op->op_args = NULL;
1788 result = NEED_REDRAW;
1789 } else if (eql(action, "stroke_preserve")) {
1790 op->op = STROKE_PRESERVE;
1791 if (sscanf(raw_args, " %c", &dummy) > 0)
1792 return FAILURE;
1793 op->op_args = NULL;
1794 result = NEED_REDRAW;
1795 } else if (eql(action, "fill")) {
1796 op->op = FILL;
1797 if (sscanf(raw_args, " %c", &dummy) > 0)
1798 return FAILURE;
1799 op->op_args = NULL;
1800 result = NEED_REDRAW;
1801 } else if (eql(action, "fill_preserve")) {
1802 op->op = FILL_PRESERVE;
1803 if (sscanf(raw_args, " %c", &dummy) > 0)
1804 return FAILURE;
1805 op->op_args = NULL;
1806 result = NEED_REDRAW;
1807 } else if (eql(action, "set_dash")) {
1808 char *next, *end;
1809 char data1[strlen(raw_args) + 1];
1810 int n, i;
1811 struct set_dash_args *args;
1813 strcpy(data1, raw_args);
1814 next = end = data1;
1815 n = -1;
1816 do {
1817 n++;
1818 next = end;
1819 strtod(next, &end);
1820 } while (next != end);
1821 if ((args = malloc(sizeof(*args) + n * sizeof(args->dashes[0]))) == NULL)
1822 OOM_ABORT;
1823 op->op = SET_DASH;
1824 op->op_args = args;
1825 args->num_dashes = n;
1826 for (i = 0, next = data1; i < n; i++, next = end) {
1827 args->dashes[i] = strtod(next, &end);
1829 } else if (eql(action, "set_font_face")) {
1830 char slant[7 + 1]; /* "oblique" */
1831 char weight[6 + 1]; /* "normal" */
1832 int family_start, family_len;
1833 struct set_font_face_args *args;
1835 if (sscanf(raw_args, "%7s %6s %n%*s", slant, weight, &family_start) != 2)
1836 return FAILURE;
1837 family_len = strlen(raw_args + family_start) + 1;
1838 if ((args = malloc(sizeof(*args) + family_len * sizeof(args->family[0]))) == NULL)
1839 OOM_ABORT;
1840 op->op = SET_FONT_FACE;
1841 op->op_args = args;
1842 strncpy(args->family, raw_args + family_start, family_len);
1843 if (eql(slant, "normal"))
1844 args->slant = CAIRO_FONT_SLANT_NORMAL;
1845 else if (eql(slant, "italic"))
1846 args->slant = CAIRO_FONT_SLANT_ITALIC;
1847 else if (eql(slant, "oblique"))
1848 args->slant = CAIRO_FONT_SLANT_OBLIQUE;
1849 else
1850 return FAILURE;
1851 if (eql(weight, "normal"))
1852 args->weight = CAIRO_FONT_WEIGHT_NORMAL;
1853 else if (eql(weight, "bold"))
1854 args->weight = CAIRO_FONT_WEIGHT_BOLD;
1855 else
1856 return FAILURE;
1857 } else if (eql(action, "set_font_size")) {
1858 struct set_font_size_args *args;
1860 if ((args = malloc(sizeof(*args))) == NULL)
1861 OOM_ABORT;
1862 op->op = SET_FONT_SIZE;
1863 op->op_args = args;
1864 if (sscanf(raw_args, "%lf %c", &args->size, &dummy) != 1)
1865 return FAILURE;
1866 } else if (eql(action, "set_line_cap")) {
1867 char str[6 + 1]; /* "square" */
1868 struct set_line_cap_args *args;
1870 if ((args = malloc(sizeof(*args))) == NULL)
1871 OOM_ABORT;
1872 op->op = SET_LINE_CAP;
1873 op->op_args = args;
1874 if (sscanf(raw_args, "%6s %c", str, &dummy) != 1)
1875 return FAILURE;
1876 if (eql(str, "butt"))
1877 args->line_cap = CAIRO_LINE_CAP_BUTT;
1878 else if (eql(str, "round"))
1879 args->line_cap = CAIRO_LINE_CAP_ROUND;
1880 else if (eql(str, "square"))
1881 args->line_cap = CAIRO_LINE_CAP_SQUARE;
1882 else
1883 return FAILURE;
1884 } else if (eql(action, "set_line_join")) {
1885 char str[5 + 1]; /* "miter" */
1886 struct set_line_join_args *args;
1888 if ((args = malloc(sizeof(*args))) == NULL)
1889 OOM_ABORT;
1890 op->op = SET_LINE_JOIN;
1891 op->op_args = args;
1892 if (sscanf(raw_args, "%5s %c", str, &dummy) != 1)
1893 return FAILURE;
1894 if (eql(str, "miter"))
1895 args->line_join = CAIRO_LINE_JOIN_MITER;
1896 else if (eql(str, "round"))
1897 args->line_join = CAIRO_LINE_JOIN_ROUND;
1898 else if (eql(str, "bevel"))
1899 args->line_join = CAIRO_LINE_JOIN_BEVEL;
1900 else
1901 return FAILURE;
1902 } else if (eql(action, "set_line_width")) {
1903 struct set_line_width_args *args;
1905 if ((args = malloc(sizeof(*args))) == NULL)
1906 OOM_ABORT;
1907 op->op = SET_LINE_WIDTH;
1908 op->op_args = args;
1909 if (sscanf(raw_args, "%lf %c", &args->width, &dummy) != 1)
1910 return FAILURE;
1911 } else if (eql(action, "set_source_rgba")) {
1912 struct set_source_rgba_args *args;
1914 if ((args = malloc(sizeof(*args))) == NULL)
1915 OOM_ABORT;
1916 op->op = SET_SOURCE_RGBA;
1917 op->op_args = args;
1918 gdk_rgba_parse(&args->color, raw_args);
1919 } else if (eql(action, "transform")) {
1920 char dummy;
1921 double xx, yx, xy, yy, x0, y0;
1923 if (sscanf(raw_args, "%lf %lf %lf %lf %lf %lf %c",
1924 &xx, &yx, &xy, &yy, &x0, &y0, &dummy) == 6) {
1925 struct transform_args *args;
1927 if ((args = malloc(sizeof(*args))) == NULL)
1928 OOM_ABORT;
1929 op->op_args = args;
1930 op->op = TRANSFORM;
1931 cairo_matrix_init(&args->matrix, xx, yx, xy, yy, x0, y0);
1932 } else if (sscanf(raw_args, " %c", &dummy) < 1) {
1933 op->op = RESET_CTM;
1934 op->op_args = NULL;
1935 } else
1936 return FAILURE;
1937 } else if (eql(action, "translate")) {
1938 double tx, ty;
1939 struct transform_args *args;
1941 if ((args = malloc(sizeof(*args))) == NULL)
1942 OOM_ABORT;
1943 op->op = TRANSFORM;
1944 op->op_args = args;
1945 if (sscanf(raw_args, "%lf %lf %c", &tx, &ty, &dummy) != 2)
1946 return FAILURE;
1947 cairo_matrix_init_translate(&args->matrix, tx, ty);
1948 } else if (eql(action, "scale")) {
1949 double sx, sy;
1950 struct transform_args *args;
1952 if ((args = malloc(sizeof(*args))) == NULL)
1953 OOM_ABORT;
1954 op->op = TRANSFORM;
1955 op->op_args = args;
1956 if (sscanf(raw_args, "%lf %lf %c", &sx, &sy, &dummy) != 2)
1957 return FAILURE;
1958 cairo_matrix_init_scale(&args->matrix, sx, sy);
1959 } else if (eql(action, "rotate")) {
1960 double angle;
1961 struct transform_args *args;
1963 if ((args = malloc(sizeof(*args))) == NULL)
1964 OOM_ABORT;
1965 op->op = TRANSFORM;
1966 op->op_args = args;
1967 if (sscanf(raw_args, "%lf %c", &angle, &dummy) != 1)
1968 return FAILURE;
1969 cairo_matrix_init_rotate(&args->matrix, angle * (M_PI / 180.L));
1970 } else
1971 return FAILURE;
1972 return result;
1976 * Add another element to widget's "draw_ops" list
1978 static enum draw_op_stat
1979 ins_draw_op(GObject *widget, const char *action, const char *data)
1981 enum draw_op_stat result;
1982 struct draw_op *new_op = NULL, *draw_ops = NULL, *prev_op = NULL;
1984 if ((new_op = malloc(sizeof(*new_op))) == NULL)
1985 OOM_ABORT;
1986 new_op->op_args = NULL;
1987 new_op->next = NULL;
1988 if ((result = set_draw_op(new_op, action, data)) == FAILURE) {
1989 free(new_op->op_args);
1990 free(new_op);
1991 return FAILURE;
1993 switch (new_op->policy) {
1994 case APPEND:
1995 if ((draw_ops = g_object_get_data(widget, "draw_ops")) == NULL)
1996 g_object_set_data(widget, "draw_ops", new_op);
1997 else {
1998 for (prev_op = draw_ops;
1999 prev_op->next != NULL;
2000 prev_op = prev_op->next);
2001 prev_op->next = new_op;
2003 break;
2004 case BEFORE:
2005 for (prev_op = NULL, draw_ops = g_object_get_data(widget, "draw_ops");
2006 draw_ops != NULL && draw_ops->id != new_op->before;
2007 prev_op = draw_ops, draw_ops = draw_ops->next);
2008 if (prev_op == NULL) { /* prepend a new first element */
2009 g_object_set_data(widget, "draw_ops", new_op);
2010 new_op->next = draw_ops;
2011 } else if (draw_ops == NULL) /* append */
2012 prev_op->next = new_op;
2013 else { /* insert */
2014 new_op->next = draw_ops;
2015 prev_op->next = new_op;
2017 break;
2018 case REPLACE:
2019 for (prev_op = NULL, draw_ops = g_object_get_data(widget, "draw_ops");
2020 draw_ops != NULL && draw_ops->id != new_op->id;
2021 prev_op = draw_ops, draw_ops = draw_ops->next);
2022 if (draw_ops == NULL && prev_op == NULL) /* start a new list */
2023 g_object_set_data(widget, "draw_ops", new_op);
2024 else if (prev_op == NULL) { /* replace the first element */
2025 g_object_set_data(widget, "draw_ops", new_op);
2026 new_op->next = draw_ops->next;
2027 free(draw_ops->op_args);
2028 free(draw_ops);
2029 } else if (draw_ops == NULL) /* append */
2030 prev_op->next = new_op;
2031 else { /* replace some other element */
2032 new_op->next = draw_ops->next;
2033 prev_op->next = new_op;
2034 free(draw_ops->op_args);
2035 free(draw_ops);
2037 break;
2038 default:
2039 ABORT;
2040 break;
2042 return result;
2046 * Remove all elements with the given id from widget's "draw_ops" list
2048 static enum draw_op_stat
2049 rem_draw_op(GObject *widget, const char *data)
2051 char dummy;
2052 struct draw_op *op, *next_op, *prev_op = NULL;
2053 unsigned long long int id;
2055 if (sscanf(data, "%llu %c", &id, &dummy) != 1)
2056 return FAILURE;
2057 op = g_object_get_data(widget, "draw_ops");
2058 while (op != NULL) {
2059 next_op = op->next;
2060 if (op->id == id) {
2061 if (prev_op == NULL) /* list head */
2062 g_object_set_data(widget, "draw_ops", op->next);
2063 else
2064 prev_op->next = op->next;
2065 free(op->op_args);
2066 free(op);
2067 } else
2068 prev_op = op;
2069 op = next_op;
2071 return NEED_REDRAW;
2074 static gboolean
2075 refresh_widget(GtkWidget *widget)
2077 gint height = gtk_widget_get_allocated_height(widget);
2078 gint width = gtk_widget_get_allocated_width(widget);
2080 gtk_widget_queue_draw_area(widget, 0, 0, width, height);
2081 return G_SOURCE_REMOVE;
2084 static void
2085 update_drawing_area(struct ui_data *ud)
2087 enum draw_op_stat dost;
2089 if (eql(ud->action, "remove"))
2090 dost = rem_draw_op(ud->obj, ud->data);
2091 else
2092 dost = ins_draw_op(ud->obj, ud->action, ud->data);
2093 switch (dost) {
2094 case NEED_REDRAW:
2095 gdk_threads_add_idle_full(G_PRIORITY_LOW,
2096 (GSourceFunc) refresh_widget,
2097 GTK_WIDGET(ud->obj), NULL);
2098 break;
2099 case FAILURE:
2100 try_generic_cmds(ud);
2101 break;
2102 case SUCCESS:
2103 break;
2104 default:
2105 ABORT;
2106 break;
2110 static void
2111 update_entry(struct ui_data *ud)
2113 GtkEntry *entry = GTK_ENTRY(ud->obj);
2115 if (eql(ud->action, "set_text"))
2116 gtk_entry_set_text(entry, ud->data);
2117 else if (eql(ud->action, "set_placeholder_text"))
2118 gtk_entry_set_placeholder_text(entry, ud->data);
2119 else
2120 try_generic_cmds(ud);
2123 static void
2124 update_expander(struct ui_data *ud)
2126 GtkExpander *expander = GTK_EXPANDER(ud->obj);
2127 char dummy;
2128 unsigned int val;
2130 if (eql(ud->action, "set_expanded") &&
2131 sscanf(ud->data, "%u %c", &val, &dummy) == 1 && val < 2)
2132 gtk_expander_set_expanded(expander, val);
2133 else if (eql(ud->action, "set_label"))
2134 gtk_expander_set_label(expander, ud->data);
2135 else
2136 try_generic_cmds(ud);
2139 static void
2140 update_file_chooser_button(struct ui_data *ud)
2142 if (eql(ud->action, "set_filename"))
2143 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(ud->obj), ud->data);
2144 else
2145 try_generic_cmds(ud);
2148 static void
2149 update_file_chooser_dialog(struct ui_data *ud)
2151 GtkFileChooser *chooser = GTK_FILE_CHOOSER(ud->obj);
2153 if (eql(ud->action, "set_filename"))
2154 gtk_file_chooser_set_filename(chooser, ud->data);
2155 else if (eql(ud->action, "set_current_name"))
2156 gtk_file_chooser_set_current_name(chooser, ud->data);
2157 else if (update_class_window(ud));
2158 else
2159 try_generic_cmds(ud);
2162 static void
2163 update_font_button(struct ui_data *ud){
2164 GtkFontButton *font_button = GTK_FONT_BUTTON(ud->obj);
2166 if (eql(ud->action, "set_font_name"))
2167 gtk_font_button_set_font_name(font_button, ud->data);
2168 else
2169 try_generic_cmds(ud);
2172 static void
2173 update_frame(struct ui_data *ud)
2175 if (eql(ud->action, "set_label"))
2176 gtk_frame_set_label(GTK_FRAME(ud->obj), ud->data);
2177 else
2178 try_generic_cmds(ud);
2181 static void
2182 update_image(struct ui_data *ud)
2184 GtkIconSize size;
2185 GtkImage *image = GTK_IMAGE(ud->obj);
2187 gtk_image_get_icon_name(image, NULL, &size);
2188 if (eql(ud->action, "set_from_file"))
2189 gtk_image_set_from_file(image, ud->data);
2190 else if (eql(ud->action, "set_from_icon_name"))
2191 gtk_image_set_from_icon_name(image, ud->data, size);
2192 else
2193 try_generic_cmds(ud);
2196 static void
2197 update_label(struct ui_data *ud)
2199 if (eql(ud->action, "set_text"))
2200 gtk_label_set_text(GTK_LABEL(ud->obj), ud->data);
2201 else
2202 try_generic_cmds(ud);
2205 static void
2206 update_link_button(struct ui_data *ud)
2208 char dummy;
2209 unsigned int val;
2211 if (eql(ud->action, "set_visited") &&
2212 sscanf(ud->data, "%u %c", &val, &dummy) == 1 && val < 2)
2213 gtk_link_button_set_visited(GTK_LINK_BUTTON(ud->obj), val);
2214 else
2215 update_button(ud);
2218 static void
2219 update_menu(struct ui_data *ud)
2221 char dummy;
2222 GtkMenu* menu = GTK_MENU(ud->obj);
2224 if (eql(ud->action, "popup") && sscanf(ud->data, " %c", &dummy) < 1)
2225 gtk_menu_popup(menu, NULL, NULL, NULL, NULL, 0,
2226 gtk_get_current_event_time());
2227 else if (eql(ud->action, "popdown") && sscanf(ud->data, " %c", &dummy) < 1)
2228 gtk_menu_popdown(menu);
2229 else
2230 try_generic_cmds(ud);
2233 static void
2234 update_menu_item(struct ui_data *ud)
2236 try_generic_cmds(ud);
2239 static void
2240 update_notebook(struct ui_data *ud)
2242 char dummy;
2243 int val, n_pages = gtk_notebook_get_n_pages(GTK_NOTEBOOK(ud->obj));
2245 if (eql(ud->action, "set_current_page") &&
2246 sscanf(ud->data, "%d %c", &val, &dummy) == 1 &&
2247 val >= 0 && val < n_pages)
2248 gtk_notebook_set_current_page(GTK_NOTEBOOK(ud->obj), val);
2249 else
2250 try_generic_cmds(ud);
2253 static void
2254 update_nothing(struct ui_data *ud)
2256 (void) ud;
2259 static void
2260 update_print_dialog(struct ui_data *ud)
2262 GtkPageSetup *page_setup;
2263 GtkPrintJob *job;
2264 GtkPrintSettings *settings;
2265 GtkPrintUnixDialog *dialog = GTK_PRINT_UNIX_DIALOG(ud->obj);
2266 GtkPrinter *printer;
2267 gint response_id;
2269 if (eql(ud->action, "print")) {
2270 response_id = gtk_dialog_run(GTK_DIALOG(dialog));
2271 switch (response_id) {
2272 case GTK_RESPONSE_OK:
2273 printer = gtk_print_unix_dialog_get_selected_printer(dialog);
2274 settings = gtk_print_unix_dialog_get_settings(dialog);
2275 page_setup = gtk_print_unix_dialog_get_page_setup(dialog);
2276 job = gtk_print_job_new(ud->data, printer, settings, page_setup);
2277 if (gtk_print_job_set_source_file(job, ud->data, NULL))
2278 gtk_print_job_send(job, NULL, NULL, NULL);
2279 else
2280 ign_cmd(ud->type, ud->cmd);
2281 g_clear_object(&settings);
2282 g_clear_object(&job);
2283 break;
2284 case GTK_RESPONSE_CANCEL:
2285 case GTK_RESPONSE_DELETE_EVENT:
2286 break;
2287 default:
2288 fprintf(stderr, "%s sent an unexpected response id (%d)\n",
2289 widget_id(GTK_BUILDABLE(dialog)), response_id);
2290 break;
2292 gtk_widget_hide(GTK_WIDGET(dialog));
2293 } else
2294 try_generic_cmds(ud);
2297 static void
2298 update_progress_bar(struct ui_data *ud)
2300 GtkProgressBar *progressbar = GTK_PROGRESS_BAR(ud->obj);
2301 char dummy;
2302 double frac;
2304 if (eql(ud->action, "set_text"))
2305 gtk_progress_bar_set_text(progressbar, *(ud->data) == '\0' ? NULL : ud->data);
2306 else if (eql(ud->action, "set_fraction") &&
2307 sscanf(ud->data, "%lf %c", &frac, &dummy) == 1)
2308 gtk_progress_bar_set_fraction(progressbar, frac);
2309 else
2310 try_generic_cmds(ud);
2313 static void
2314 update_scale(struct ui_data *ud)
2316 GtkRange *range = GTK_RANGE(ud->obj);
2317 char dummy;
2318 double val1, val2;
2320 if (eql(ud->action, "set_value") && sscanf(ud->data, "%lf %c", &val1, &dummy) == 1)
2321 gtk_range_set_value(range, val1);
2322 else if (eql(ud->action, "set_fill_level") &&
2323 sscanf(ud->data, "%lf %c", &val1, &dummy) == 1) {
2324 gtk_range_set_fill_level(range, val1);
2325 gtk_range_set_show_fill_level(range, TRUE);
2326 } else if (eql(ud->action, "set_fill_level") &&
2327 sscanf(ud->data, " %c", &dummy) < 1)
2328 gtk_range_set_show_fill_level(range, FALSE);
2329 else if (eql(ud->action, "set_range") &&
2330 sscanf(ud->data, "%lf %lf %c", &val1, &val2, &dummy) == 2)
2331 gtk_range_set_range(range, val1, val2);
2332 else if (eql(ud->action, "set_increments") &&
2333 sscanf(ud->data, "%lf %lf %c", &val1, &val2, &dummy) == 2)
2334 gtk_range_set_increments(range, val1, val2);
2335 else
2336 try_generic_cmds(ud);
2339 static void
2340 update_scrolled_window(struct ui_data *ud)
2342 GtkScrolledWindow *window = GTK_SCROLLED_WINDOW(ud->obj);
2343 GtkAdjustment *hadj = gtk_scrolled_window_get_hadjustment(window);
2344 GtkAdjustment *vadj = gtk_scrolled_window_get_vadjustment(window);
2345 char dummy;
2346 double d0, d1;
2348 if (eql(ud->action, "hscroll") && sscanf(ud->data, "%lf %c", &d0, &dummy) == 1)
2349 gtk_adjustment_set_value(hadj, d0);
2350 else if (eql(ud->action, "vscroll") && sscanf(ud->data, "%lf %c", &d0, &dummy) == 1)
2351 gtk_adjustment_set_value(vadj, d0);
2352 else if (eql(ud->action, "hscroll_to_range") &&
2353 sscanf(ud->data, "%lf %lf %c", &d0, &d1, &dummy) == 2)
2354 gtk_adjustment_clamp_page(hadj, d0, d1);
2355 else if (eql(ud->action, "vscroll_to_range") &&
2356 sscanf(ud->data, "%lf %lf %c", &d0, &d1, &dummy) == 2)
2357 gtk_adjustment_clamp_page(vadj, d0, d1);
2358 else
2359 try_generic_cmds(ud);
2362 static void
2363 update_socket(struct ui_data *ud)
2365 GtkSocket *socket = GTK_SOCKET(ud->obj);
2366 Window id;
2367 char str[BUFLEN], dummy;
2369 if (eql(ud->action, "id") && sscanf(ud->data, " %c", &dummy) < 1) {
2370 id = gtk_socket_get_id(socket);
2371 snprintf(str, BUFLEN, "%lu", id);
2372 send_msg(ud->args->fout, GTK_BUILDABLE(socket), "id", str, NULL);
2373 } else
2374 try_generic_cmds(ud);
2377 static void
2378 update_spin_button(struct ui_data *ud)
2380 GtkSpinButton *spinbutton = GTK_SPIN_BUTTON(ud->obj);
2381 char dummy;
2382 double val1, val2;
2384 if (eql(ud->action, "set_text") && /* TODO: rename to "set_value" */
2385 sscanf(ud->data, "%lf %c", &val1, &dummy) == 1)
2386 gtk_spin_button_set_value(spinbutton, val1);
2387 else if (eql(ud->action, "set_range") &&
2388 sscanf(ud->data, "%lf %lf %c", &val1, &val2, &dummy) == 2)
2389 gtk_spin_button_set_range(spinbutton, val1, val2);
2390 else if (eql(ud->action, "set_increments") &&
2391 sscanf(ud->data, "%lf %lf %c", &val1, &val2, &dummy) == 2)
2392 gtk_spin_button_set_increments(spinbutton, val1, val2);
2393 else
2394 try_generic_cmds(ud);
2397 static void
2398 update_spinner(struct ui_data *ud)
2400 GtkSpinner *spinner = GTK_SPINNER(ud->obj);
2401 char dummy;
2403 if (eql(ud->action, "start") && sscanf(ud->data, " %c", &dummy) < 1)
2404 gtk_spinner_start(spinner);
2405 else if (eql(ud->action, "stop") && sscanf(ud->data, " %c", &dummy) < 1)
2406 gtk_spinner_stop(spinner);
2407 else
2408 try_generic_cmds(ud);
2411 static void
2412 update_statusbar(struct ui_data *ud)
2414 GtkStatusbar *statusbar = GTK_STATUSBAR(ud->obj);
2415 char *ctx_msg, dummy;
2416 const char *status_msg;
2417 int ctx_len, t;
2419 /* TODO: remove "push", "pop", "remove_all"; rename "push_id" to "push", etc. */
2420 if ((ctx_msg = malloc(strlen(ud->data) + 1)) == NULL)
2421 OOM_ABORT;
2422 t = sscanf(ud->data, "%s %n%c", ctx_msg, &ctx_len, &dummy);
2423 status_msg = ud->data + ctx_len;
2424 if (eql(ud->action, "push"))
2425 gtk_statusbar_push(statusbar,
2426 gtk_statusbar_get_context_id(statusbar, "0"),
2427 ud->data);
2428 else if (eql(ud->action, "push_id") && t >= 1)
2429 gtk_statusbar_push(statusbar,
2430 gtk_statusbar_get_context_id(statusbar, ctx_msg),
2431 status_msg);
2432 else if (eql(ud->action, "pop") && t < 1)
2433 gtk_statusbar_pop(statusbar,
2434 gtk_statusbar_get_context_id(statusbar, "0"));
2435 else if (eql(ud->action, "pop_id") && t == 1)
2436 gtk_statusbar_pop(statusbar,
2437 gtk_statusbar_get_context_id(statusbar, ctx_msg));
2438 else if (eql(ud->action, "remove_all") && t < 1)
2439 gtk_statusbar_remove_all(statusbar,
2440 gtk_statusbar_get_context_id(statusbar, "0"));
2441 else if (eql(ud->action, "remove_all_id") && t == 1)
2442 gtk_statusbar_remove_all(statusbar,
2443 gtk_statusbar_get_context_id(statusbar, ctx_msg));
2444 else
2445 try_generic_cmds(ud);
2446 free(ctx_msg);
2449 static void
2450 update_switch(struct ui_data *ud)
2452 char dummy;
2453 unsigned int val;
2455 if (eql(ud->action, "set_active") &&
2456 sscanf(ud->data, "%u %c", &val, &dummy) == 1 && val < 2)
2457 gtk_switch_set_active(GTK_SWITCH(ud->obj), val);
2458 else
2459 try_generic_cmds(ud);
2462 static void
2463 update_text_view(struct ui_data *ud)
2465 FILE *sv;
2466 GtkTextView *view = GTK_TEXT_VIEW(ud->obj);
2467 GtkTextBuffer *textbuf = gtk_text_view_get_buffer(view);
2468 GtkTextIter a, b;
2469 char dummy;
2470 int val;
2472 if (eql(ud->action, "set_text"))
2473 gtk_text_buffer_set_text(textbuf, ud->data, -1);
2474 else if (eql(ud->action, "delete") && sscanf(ud->data, " %c", &dummy) < 1) {
2475 gtk_text_buffer_get_bounds(textbuf, &a, &b);
2476 gtk_text_buffer_delete(textbuf, &a, &b);
2477 } else if (eql(ud->action, "insert_at_cursor"))
2478 gtk_text_buffer_insert_at_cursor(textbuf, ud->data, -1);
2479 else if (eql(ud->action, "place_cursor") && eql(ud->data, "end")) {
2480 gtk_text_buffer_get_end_iter(textbuf, &a);
2481 gtk_text_buffer_place_cursor(textbuf, &a);
2482 } else if (eql(ud->action, "place_cursor") &&
2483 sscanf(ud->data, "%d %c", &val, &dummy) == 1) {
2484 gtk_text_buffer_get_iter_at_offset(textbuf, &a, val);
2485 gtk_text_buffer_place_cursor(textbuf, &a);
2486 } else if (eql(ud->action, "place_cursor_at_line") &&
2487 sscanf(ud->data, "%d %c", &val, &dummy) == 1) {
2488 gtk_text_buffer_get_iter_at_line(textbuf, &a, val);
2489 gtk_text_buffer_place_cursor(textbuf, &a);
2490 } else if (eql(ud->action, "scroll_to_cursor") &&
2491 sscanf(ud->data, " %c", &dummy) < 1)
2492 gtk_text_view_scroll_to_mark(view, gtk_text_buffer_get_insert(textbuf),
2493 0., 0, 0., 0.);
2494 else if (eql(ud->action, "save") && ud->data != NULL &&
2495 (sv = fopen(ud->data, "w")) != NULL) {
2496 gtk_text_buffer_get_bounds(textbuf, &a, &b);
2497 send_msg(sv, GTK_BUILDABLE(view), "insert_at_cursor",
2498 gtk_text_buffer_get_text(textbuf, &a, &b, TRUE), NULL);
2499 fclose(sv);
2500 } else
2501 try_generic_cmds(ud);
2504 static void
2505 update_toggle_button(struct ui_data *ud)
2507 char dummy;
2508 unsigned int val;
2510 if (eql(ud->action, "set_label"))
2511 gtk_button_set_label(GTK_BUTTON(ud->obj), ud->data);
2512 else if (eql(ud->action, "set_active") &&
2513 sscanf(ud->data, "%u %c", &val, &dummy) == 1 && val < 2)
2514 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ud->obj), val);
2515 else
2516 try_generic_cmds(ud);
2520 * update_tree_view(), which runs inside gtk_main(), needs a few
2521 * helper functions
2525 * Check if s is a valid string representation of a GtkTreePath
2527 static bool
2528 is_path_string(char *s)
2530 return s != NULL &&
2531 strlen(s) == strspn(s, ":0123456789") &&
2532 strstr(s, "::") == NULL &&
2533 strcspn(s, ":") > 0;
2536 static void
2537 tree_model_insert_before(GtkTreeModel *model, GtkTreeIter *iter,
2538 GtkTreeIter *parent, GtkTreeIter *sibling)
2540 if (GTK_IS_TREE_STORE(model))
2541 gtk_tree_store_insert_before(GTK_TREE_STORE(model),
2542 iter, parent, sibling);
2543 else if (GTK_IS_LIST_STORE(model))
2544 gtk_list_store_insert_before(GTK_LIST_STORE(model),
2545 iter, sibling);
2546 else
2547 ABORT;
2550 static void
2551 tree_model_insert_after(GtkTreeModel *model, GtkTreeIter *iter,
2552 GtkTreeIter *parent, GtkTreeIter *sibling)
2554 if (GTK_IS_TREE_STORE(model))
2555 gtk_tree_store_insert_after(GTK_TREE_STORE(model),
2556 iter, parent, sibling);
2557 else if (GTK_IS_LIST_STORE(model))
2558 gtk_list_store_insert_after(GTK_LIST_STORE(model),
2559 iter, sibling);
2560 else
2561 ABORT;
2564 static void
2565 tree_model_move_before(GtkTreeModel *model, GtkTreeIter *iter,
2566 GtkTreeIter *position)
2568 if (GTK_IS_TREE_STORE(model))
2569 gtk_tree_store_move_before(GTK_TREE_STORE(model), iter, position);
2570 else if (GTK_IS_LIST_STORE(model))
2571 gtk_list_store_move_before(GTK_LIST_STORE(model), iter, position);
2572 else
2573 ABORT;
2576 static void
2577 tree_model_remove(GtkTreeModel *model, GtkTreeIter *iter)
2579 if (GTK_IS_TREE_STORE(model))
2580 gtk_tree_store_remove(GTK_TREE_STORE(model), iter);
2581 else if (GTK_IS_LIST_STORE(model))
2582 gtk_list_store_remove(GTK_LIST_STORE(model), iter);
2583 else
2584 ABORT;
2587 static void
2588 tree_model_clear(GtkTreeModel *model)
2590 if (GTK_IS_TREE_STORE(model))
2591 gtk_tree_store_clear(GTK_TREE_STORE(model));
2592 else if (GTK_IS_LIST_STORE(model))
2593 gtk_list_store_clear(GTK_LIST_STORE(model));
2594 else
2595 ABORT;
2598 static void
2599 tree_model_set(GtkTreeModel *model, GtkTreeIter *iter, ...)
2601 va_list ap;
2603 va_start(ap, iter);
2604 if (GTK_IS_TREE_STORE(model))
2605 gtk_tree_store_set_valist(GTK_TREE_STORE(model), iter, ap);
2606 else if (GTK_IS_LIST_STORE(model))
2607 gtk_list_store_set_valist(GTK_LIST_STORE(model), iter, ap);
2608 else
2609 ABORT;
2610 va_end(ap);
2614 * Create an empty row at path if it doesn't yet exist. Create older
2615 * siblings and parents as necessary.
2617 static void
2618 create_subtree(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter)
2620 GtkTreeIter iter_1; /* iter's predecessor */
2621 GtkTreePath *path_1; /* path's predecessor */
2623 if (gtk_tree_path_get_depth(path) > 0 &&
2624 gtk_tree_model_get_iter(model, iter, path))
2625 return;
2626 path_1 = gtk_tree_path_copy(path);
2627 if (gtk_tree_path_prev(path_1)) { /* need an older sibling */
2628 create_subtree(model, path_1, iter);
2629 iter_1 = *iter;
2630 tree_model_insert_after(model, iter, NULL, &iter_1);
2631 } else if (gtk_tree_path_up(path_1)) { /* need a parent */
2632 create_subtree(model, path_1, iter);
2633 if (gtk_tree_path_get_depth(path_1) == 0)
2634 /* first toplevel row */
2635 tree_model_insert_after(model, iter, NULL, NULL);
2636 else { /* first row in a lower level */
2637 iter_1 = *iter;
2638 tree_model_insert_after(model, iter, &iter_1, NULL);
2640 } /* neither prev nor up mean we're at the root of an empty tree */
2641 gtk_tree_path_free(path_1);
2644 static bool
2645 set_tree_view_cell(GtkTreeModel *model, GtkTreeIter *iter,
2646 const char *path_s, int col, const char *new_text)
2648 GType col_type = gtk_tree_model_get_column_type(model, col);
2649 GtkTreePath *path;
2650 bool ok = false;
2651 char dummy;
2652 double d;
2653 long long int n;
2655 path = gtk_tree_path_new_from_string(path_s);
2656 switch (col_type) {
2657 case G_TYPE_BOOLEAN:
2658 case G_TYPE_INT:
2659 case G_TYPE_LONG:
2660 case G_TYPE_INT64:
2661 case G_TYPE_UINT:
2662 case G_TYPE_ULONG:
2663 case G_TYPE_UINT64:
2664 if (new_text != NULL &&
2665 sscanf(new_text, "%lld %c", &n, &dummy) == 1) {
2666 create_subtree(model, path, iter);
2667 tree_model_set(model, iter, col, n, -1);
2668 ok = true;
2670 break;
2671 case G_TYPE_FLOAT:
2672 case G_TYPE_DOUBLE:
2673 if (new_text != NULL &&
2674 sscanf(new_text, "%lf %c", &d, &dummy) == 1) {
2675 create_subtree(model, path, iter);
2676 tree_model_set(model, iter, col, d, -1);
2677 ok = true;
2679 break;
2680 case G_TYPE_STRING:
2681 create_subtree(model, path, iter);
2682 tree_model_set(model, iter, col, new_text, -1);
2683 ok = true;
2684 break;
2685 default:
2686 fprintf(stderr, "column %d: %s not implemented\n",
2687 col, g_type_name(col_type));
2688 ok = true;
2689 break;
2691 gtk_tree_path_free(path);
2692 return ok;
2695 static void
2696 tree_view_set_cursor(GtkTreeView *view, GtkTreePath *path, GtkTreeViewColumn *col)
2698 /* GTK+ 3.14 requires this. For 3.18, path = NULL */
2699 /* is just fine and this function need not exist. */
2700 if (path == NULL)
2701 path = gtk_tree_path_new();
2702 gtk_tree_view_set_cursor(view, path, col, false);
2705 static void
2706 update_tree_view(struct ui_data *ud)
2708 GtkTreeView *view = GTK_TREE_VIEW(ud->obj);
2709 GtkTreeIter iter0, iter1;
2710 GtkTreeModel *model = gtk_tree_view_get_model(view);
2711 GtkTreePath *path = NULL;
2712 GtkTreeSelection *sel = gtk_tree_view_get_selection(view);
2713 bool iter0_valid, iter1_valid;
2714 char *tokens, *arg0, *arg1, *arg2;
2715 int col = -1; /* invalid column number */
2716 struct info ar;
2718 if (!GTK_IS_LIST_STORE(model) && !GTK_IS_TREE_STORE(model))
2720 fprintf(stderr, "missing model/");
2721 ign_cmd(ud->type, ud->cmd);
2722 return;
2724 if ((tokens = malloc(strlen(ud->data) + 1)) == NULL)
2725 OOM_ABORT;
2726 strcpy(tokens, ud->data);
2727 arg0 = strtok(tokens, WHITESPACE);
2728 arg1 = strtok(NULL, WHITESPACE);
2729 arg2 = strtok(NULL, "");
2730 iter0_valid = is_path_string(arg0) &&
2731 gtk_tree_model_get_iter_from_string(model, &iter0, arg0);
2732 iter1_valid = is_path_string(arg1) &&
2733 gtk_tree_model_get_iter_from_string(model, &iter1, arg1);
2734 if (is_path_string(arg1))
2735 col = strtol(arg1, NULL, 10);
2736 if (eql(ud->action, "set") &&
2737 col > -1 &&
2738 col < gtk_tree_model_get_n_columns(model) &&
2739 is_path_string(arg0)) {
2740 if (set_tree_view_cell(model, &iter0, arg0, col, arg2) == false)
2741 ign_cmd(ud->type, ud->cmd);
2742 } else if (eql(ud->action, "scroll") && iter0_valid && iter1_valid &&
2743 arg2 == NULL) {
2744 path = gtk_tree_path_new_from_string(arg0);
2745 gtk_tree_view_scroll_to_cell (view,
2746 path,
2747 gtk_tree_view_get_column(view, col),
2748 0, 0., 0.);
2749 } else if (eql(ud->action, "expand") && iter0_valid && arg1 == NULL) {
2750 path = gtk_tree_path_new_from_string(arg0);
2751 gtk_tree_view_expand_row(view, path, false);
2752 } else if (eql(ud->action, "expand_all") && iter0_valid && arg1 == NULL) {
2753 path = gtk_tree_path_new_from_string(arg0);
2754 gtk_tree_view_expand_row(view, path, true);
2755 } else if (eql(ud->action, "expand_all") && arg0 == NULL)
2756 gtk_tree_view_expand_all(view);
2757 else if (eql(ud->action, "collapse") && iter0_valid && arg1 == NULL) {
2758 path = gtk_tree_path_new_from_string(arg0);
2759 gtk_tree_view_collapse_row(view, path);
2760 } else if (eql(ud->action, "collapse") && arg0 == NULL)
2761 gtk_tree_view_collapse_all(view);
2762 else if (eql(ud->action, "set_cursor") && iter0_valid && arg1 == NULL) {
2763 path = gtk_tree_path_new_from_string(arg0);
2764 tree_view_set_cursor(view, path, NULL);
2765 } else if (eql(ud->action, "set_cursor") && arg0 == NULL) {
2766 tree_view_set_cursor(view, NULL, NULL);
2767 gtk_tree_selection_unselect_all(sel);
2768 } else if (eql(ud->action, "insert_row") &&
2769 eql(arg0, "end") && arg1 == NULL)
2770 tree_model_insert_before(model, &iter1, NULL, NULL);
2771 else if (eql(ud->action, "insert_row") && iter0_valid &&
2772 eql(arg1, "as_child") && arg2 == NULL)
2773 tree_model_insert_after(model, &iter1, &iter0, NULL);
2774 else if (eql(ud->action, "insert_row") && iter0_valid && arg1 == NULL)
2775 tree_model_insert_before(model, &iter1, NULL, &iter0);
2776 else if (eql(ud->action, "move_row") && iter0_valid &&
2777 eql(arg1, "end") && arg2 == NULL)
2778 tree_model_move_before(model, &iter0, NULL);
2779 else if (eql(ud->action, "move_row") && iter0_valid && iter1_valid && arg2 == NULL)
2780 tree_model_move_before(model, &iter0, &iter1);
2781 else if (eql(ud->action, "remove_row") && iter0_valid && arg1 == NULL)
2782 tree_model_remove(model, &iter0);
2783 else if (eql(ud->action, "clear") && arg0 == NULL) {
2784 tree_view_set_cursor(view, NULL, NULL);
2785 gtk_tree_selection_unselect_all(sel);
2786 tree_model_clear(model);
2787 } else if (eql(ud->action, "block") && arg0 != NULL) {
2788 ud->obj=G_OBJECT(sel);
2789 update_blocked(ud);
2790 } else if (eql(ud->action, "save") && arg0 != NULL &&
2791 (ar.fout = fopen(arg0, "w")) != NULL) {
2792 ar.obj = ud->obj;
2793 gtk_tree_model_foreach(model,
2794 (GtkTreeModelForeachFunc) save_tree_row_msg,
2795 &ar);
2796 fclose(ar.fout);
2797 } else
2798 try_generic_cmds(ud);
2799 free(tokens);
2800 gtk_tree_path_free(path);
2803 static void
2804 update_window(struct ui_data *ud)
2806 if (!update_class_window(ud))
2807 try_generic_cmds(ud);
2811 * The final UI update. Runs inside gtk_main().
2813 static void
2814 main_quit(struct ui_data *ud)
2816 char dummy;
2818 if (sscanf(ud->data, " %c", &dummy) < 1)
2819 gtk_main_quit();
2820 else
2821 try_generic_cmds(ud);
2825 * Don't update anything; just complain from inside gtk_main()
2827 static void
2828 complain(struct ui_data *ud)
2830 ign_cmd(ud->type, ud->cmd);
2834 * Parse command pointed to by ud, and act on ui accordingly. Runs
2835 * once per command inside gtk_main().
2837 static gboolean
2838 update_ui(struct ui_data *ud)
2840 char *lc = lc_numeric();
2842 (ud->fn)(ud);
2843 free(ud->cmd_tokens);
2844 free(ud->cmd);
2845 free(ud);
2846 lc_numeric_free(lc);
2847 return G_SOURCE_REMOVE;
2851 * Keep track of loading files to avoid recursive loading of the same
2852 * file. If filename = NULL, forget the most recently remembered file.
2854 static bool
2855 remember_loading_file(char *filename)
2857 static char *filenames[BUFLEN];
2858 static size_t latest = 0;
2859 size_t i;
2861 if (filename == NULL) { /* pop */
2862 if (latest < 1)
2863 ABORT;
2864 latest--;
2865 return false;
2866 } else { /* push */
2867 for (i = 1; i <= latest; i++)
2868 if (eql(filename, filenames[i]))
2869 return false;
2870 if (latest > BUFLEN -2)
2871 return false;
2872 filenames[++latest] = filename;
2873 return true;
2878 * Read lines from stream cmd and perform appropriate actions on the
2879 * GUI. Runs inside receiver thread.
2881 static void *
2882 digest_cmd(struct info *ar)
2884 static int recursion = -1; /* > 0 means this is a recursive call */
2886 recursion++;
2887 for (;;) {
2888 FILE *cmd = ar->fin;
2889 struct ui_data *ud = NULL;
2890 char first_char = '\0';
2891 char *id; /* widget id */
2892 size_t msg_size = 32;
2893 int id_start = 0, id_end = 0;
2894 int action_start = 0, action_end = 0;
2895 int data_start = 0;
2897 if (feof(cmd))
2898 break;
2899 if ((ud = malloc(sizeof(*ud))) == NULL)
2900 OOM_ABORT;
2901 if ((ud->cmd = malloc(msg_size)) == NULL)
2902 OOM_ABORT;
2903 ud->args = ar;
2904 ud->type = G_TYPE_INVALID;
2905 pthread_testcancel();
2906 if (recursion == 0)
2907 log_msg(ar->flog, NULL);
2908 data_start = read_buf(cmd, &ud->cmd, &msg_size);
2909 if (recursion == 0)
2910 log_msg(ar->flog, ud->cmd);
2911 if ((ud->cmd_tokens = malloc(strlen(ud->cmd) + 1)) == NULL)
2912 OOM_ABORT;
2913 sscanf(ud->cmd, " %c", &first_char);
2914 if (data_start == 0 || /* empty line */
2915 first_char == '#') { /* comment */
2916 ud->fn = update_nothing;
2917 goto exec;
2919 strcpy(ud->cmd_tokens, ud->cmd);
2920 sscanf(ud->cmd_tokens,
2921 " %n%*[0-9a-zA-Z_-]%n:%n%*[0-9a-zA-Z_]%n%*1[ \t]%n",
2922 &id_start, &id_end, &action_start, &action_end, &data_start);
2923 ud->cmd_tokens[id_end] = ud->cmd_tokens[action_end] = '\0';
2924 id = ud->cmd_tokens + id_start;
2925 ud->action = ud->cmd_tokens + action_start;
2926 ud->data = ud->cmd_tokens + data_start;
2927 if (eql(ud->action, "main_quit")) {
2928 ud->fn = main_quit;
2929 goto exec;
2931 if (eql(ud->action, "load") && strlen(ud->data) > 0 &&
2932 remember_loading_file(ud->data)) {
2933 struct info a = *ar;
2935 if ((a.fin = fopen(ud->data, "r")) != NULL) {
2936 digest_cmd(&a);
2937 fclose(a.fin);
2938 ud->fn = update_nothing;
2939 } else
2940 ud->fn = complain;
2941 remember_loading_file(NULL);
2942 goto exec;
2944 if ((ud->obj = (gtk_builder_get_object(ar->builder, id))) == NULL) {
2945 ud->fn = complain;
2946 goto exec;
2948 ud->type = G_TYPE_FROM_INSTANCE(ud->obj);
2949 if (ud->type == GTK_TYPE_DRAWING_AREA)
2950 ud->fn = update_drawing_area;
2951 else if (ud->type == GTK_TYPE_TREE_VIEW)
2952 ud->fn = update_tree_view;
2953 else if (ud->type == GTK_TYPE_COMBO_BOX_TEXT)
2954 ud->fn = update_combo_box_text;
2955 else if (ud->type == GTK_TYPE_LABEL)
2956 ud->fn = update_label;
2957 else if (ud->type == GTK_TYPE_IMAGE)
2958 ud->fn = update_image;
2959 else if (ud->type == GTK_TYPE_TEXT_VIEW)
2960 ud->fn = update_text_view;
2961 else if (ud->type == GTK_TYPE_NOTEBOOK)
2962 ud->fn = update_notebook;
2963 else if (ud->type == GTK_TYPE_EXPANDER)
2964 ud->fn = update_expander;
2965 else if (ud->type == GTK_TYPE_FRAME ||
2966 ud->type == GTK_TYPE_ASPECT_FRAME)
2967 ud->fn = update_frame;
2968 else if (ud->type == GTK_TYPE_SCROLLED_WINDOW)
2969 ud->fn = update_scrolled_window;
2970 else if (ud->type == GTK_TYPE_LINK_BUTTON)
2971 ud->fn = update_link_button;
2972 else if (ud->type == GTK_TYPE_BUTTON)
2973 ud->fn = update_button;
2974 else if (ud->type == GTK_TYPE_MENU)
2975 ud->fn = update_menu;
2976 else if (ud->type == GTK_TYPE_MENU_ITEM)
2977 ud->fn = update_menu_item;
2978 else if (ud->type == GTK_TYPE_FILE_CHOOSER_DIALOG)
2979 ud->fn = update_file_chooser_dialog;
2980 else if (ud->type == GTK_TYPE_FILE_CHOOSER_BUTTON)
2981 ud->fn = update_file_chooser_button;
2982 else if (ud->type == GTK_TYPE_COLOR_BUTTON)
2983 ud->fn = update_color_button;
2984 else if (ud->type == GTK_TYPE_FONT_BUTTON)
2985 ud->fn = update_font_button;
2986 else if (ud->type == GTK_TYPE_PRINT_UNIX_DIALOG)
2987 ud->fn = update_print_dialog;
2988 else if (ud->type == GTK_TYPE_SWITCH)
2989 ud->fn = update_switch;
2990 else if (ud->type == GTK_TYPE_TOGGLE_BUTTON ||
2991 ud->type == GTK_TYPE_RADIO_BUTTON ||
2992 ud->type == GTK_TYPE_CHECK_BUTTON)
2993 ud->fn = update_toggle_button;
2994 else if (ud->type == GTK_TYPE_ENTRY)
2995 ud->fn = update_entry;
2996 else if (ud->type == GTK_TYPE_SPIN_BUTTON)
2997 ud->fn = update_spin_button;
2998 else if (ud->type == GTK_TYPE_SCALE)
2999 ud->fn = update_scale;
3000 else if (ud->type == GTK_TYPE_PROGRESS_BAR)
3001 ud->fn = update_progress_bar;
3002 else if (ud->type == GTK_TYPE_SPINNER)
3003 ud->fn = update_spinner;
3004 else if (ud->type == GTK_TYPE_STATUSBAR)
3005 ud->fn = update_statusbar;
3006 else if (ud->type == GTK_TYPE_CALENDAR)
3007 ud->fn = update_calendar;
3008 else if (ud->type == GTK_TYPE_SOCKET)
3009 ud->fn = update_socket;
3010 else if (ud->type == GTK_TYPE_WINDOW ||
3011 ud->type == GTK_TYPE_DIALOG)
3012 ud->fn = update_window;
3013 else
3014 ud->fn = try_generic_cmds;
3015 exec:
3016 pthread_testcancel();
3017 gdk_threads_add_timeout(0, (GSourceFunc) update_ui, ud);
3019 recursion--;
3020 return NULL;
3025 * ============================================================
3026 * Initialization
3027 * ============================================================
3031 * Return the first string xpath obtains from ui_file.
3032 * xmlFree(string) must be called when done
3034 static xmlChar *
3035 xpath1(xmlChar *xpath, const char *ui_file)
3037 xmlChar *r = NULL;
3038 xmlDocPtr doc = NULL;
3039 xmlNodeSetPtr nodes = NULL;
3040 xmlXPathContextPtr ctx = NULL;
3041 xmlXPathObjectPtr xpath_obj = NULL;
3043 if ((doc = xmlParseFile(ui_file)) == NULL)
3044 goto ret0;
3045 if ((ctx = xmlXPathNewContext(doc)) == NULL)
3046 goto ret1;
3047 if ((xpath_obj = xmlXPathEvalExpression(xpath, ctx)) == NULL)
3048 goto ret2;
3049 if ((nodes = xpath_obj->nodesetval) != NULL && nodes->nodeNr > 0)
3050 r = xmlNodeGetContent(nodes->nodeTab[0]);
3051 xmlXPathFreeObject(xpath_obj);
3052 ret2:
3053 xmlXPathFreeContext(ctx);
3054 ret1:
3055 xmlFreeDoc(doc);
3056 ret0:
3057 return r;
3061 * Attach key "col_number" to renderer. Associate "col_number" with
3062 * the corresponding column number in the underlying model.
3063 * Due to what looks like a gap in the GTK API, renderer id and column
3064 * number are taken directly from the XML .ui file.
3066 static bool
3067 tree_view_column_get_renderer_column(GtkBuilder *builder, const char *ui_file,
3068 GtkTreeViewColumn *t_col, int n,
3069 GtkCellRenderer **rnd)
3071 bool r = false;
3072 char *xp_bas1 = "//object[@class=\"GtkTreeViewColumn\" and @id=\"";
3073 char *xp_bas2 = "\"]/child[";
3074 char *xp_bas3 = "]/object[@class=\"GtkCellRendererText\""
3075 " or @class=\"GtkCellRendererToggle\"]/";
3076 char *xp_rnd_id = "@id";
3077 char *xp_text_col = "../attributes/attribute[@name=\"text\""
3078 " or @name=\"active\"]/text()";
3079 const char *tree_col_id = widget_id(GTK_BUILDABLE(t_col));
3080 size_t xp_rnd_nam_len, xp_mod_col_len;
3081 size_t xp_n_len = 3; /* Big Enough (TM) */
3082 xmlChar *xp_rnd_nam = NULL, *xp_mod_col = NULL;
3083 xmlChar *rnd_nam = NULL, *mod_col = NULL;
3085 /* find name of nth cell renderer under the GtkTreeViewColumn */
3086 /* tree_col_id */
3087 xp_rnd_nam_len = strlen(xp_bas1) + strlen(tree_col_id) +
3088 strlen(xp_bas2) + xp_n_len + strlen(xp_bas3) +
3089 strlen(xp_rnd_id) + sizeof('\0');
3090 if ((xp_rnd_nam = malloc(xp_rnd_nam_len)) == NULL)
3091 OOM_ABORT;
3092 snprintf((char *) xp_rnd_nam, xp_rnd_nam_len, "%s%s%s%d%s%s",
3093 xp_bas1, tree_col_id, xp_bas2, n,
3094 xp_bas3, xp_rnd_id);
3095 rnd_nam = xpath1(xp_rnd_nam, ui_file);
3096 /* find the model column that is attached to the nth cell */
3097 /* renderer under GtkTreeViewColumn tree_col_id */
3098 xp_mod_col_len = strlen(xp_bas1) + strlen(tree_col_id) +
3099 strlen(xp_bas2) + xp_n_len + strlen(xp_bas3) +
3100 strlen(xp_text_col) + sizeof('\0');
3101 if ((xp_mod_col = malloc(xp_mod_col_len)) == NULL)
3102 OOM_ABORT;
3103 snprintf((char *) xp_mod_col, xp_mod_col_len, "%s%s%s%d%s%s",
3104 xp_bas1, tree_col_id, xp_bas2, n, xp_bas3, xp_text_col);
3105 mod_col = xpath1(xp_mod_col, ui_file);
3106 if (rnd_nam) {
3107 *rnd = GTK_CELL_RENDERER(
3108 gtk_builder_get_object(builder, (char *) rnd_nam));
3109 if (mod_col) {
3110 g_object_set_data(G_OBJECT(*rnd), "col_number",
3111 GINT_TO_POINTER(strtol((char *) mod_col,
3112 NULL, 10)));
3113 r = true;
3116 free(xp_rnd_nam);
3117 free(xp_mod_col);
3118 xmlFree(rnd_nam);
3119 xmlFree(mod_col);
3120 return r;
3124 * Callbacks that forward a modification of a tree view cell to the
3125 * underlying model
3127 static void
3128 cb_tree_model_edit(GtkCellRenderer *renderer, const gchar *path_s,
3129 const gchar *new_text, struct info *ar)
3131 GtkTreeIter iter;
3132 int col = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(renderer),
3133 "col_number"));
3135 gtk_tree_model_get_iter_from_string(ar->model, &iter, path_s);
3136 set_tree_view_cell(ar->model, &iter, path_s, col,
3137 new_text);
3138 send_tree_cell_msg_by(send_msg, path_s, &iter, col, ar);
3141 static void
3142 cb_tree_model_toggle(GtkCellRenderer *renderer, gchar *path_s, struct info *ar)
3144 GtkTreeIter iter;
3145 bool toggle_state;
3146 int col = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(renderer),
3147 "col_number"));
3149 gtk_tree_model_get_iter_from_string(ar->model, &iter, path_s);
3150 gtk_tree_model_get(ar->model, &iter, col, &toggle_state, -1);
3151 set_tree_view_cell(ar->model, &iter, path_s, col,
3152 toggle_state? "0" : "1");
3156 * Add new element containing id to the list of callback-handler ids
3157 * stored in obj's field named "signal_id"
3159 static void
3160 push_handler_id(gpointer *obj, unsigned int id)
3162 struct handler_id *prev_hid, *hid;
3164 prev_hid = g_object_get_data(G_OBJECT(obj), "signal-id");
3165 if ((hid = malloc(sizeof(struct handler_id))) == NULL)
3166 OOM_ABORT;
3167 hid->next = prev_hid;
3168 hid->id = id;
3169 hid->blocked = false;
3170 g_object_set_data(G_OBJECT(obj), "signal-id", hid);
3174 * Connect function cb to obj's widget signal sig, remembering the
3175 * handler id in a list in obj's field named "signal-id"
3177 static void
3178 sig_conn(gpointer *obj, char *sig, GCallback cb, struct info *ar)
3180 unsigned int handler_id = g_signal_connect(obj, sig, cb, ar);
3182 push_handler_id(obj, handler_id);
3185 static void
3186 sig_conn_swapped(gpointer *obj, char *sig, GCallback cb, void *data)
3188 unsigned int handler_id = g_signal_connect_swapped(obj, sig, cb, data);
3190 push_handler_id(obj, handler_id);
3193 static void
3194 connect_widget_signals(gpointer *obj, struct info *ar)
3196 GObject *obj2;
3197 GType type = G_TYPE_INVALID;
3198 char *suffix = NULL;
3199 const char *w_id = NULL;
3200 FILE *o = ar->fout;
3202 type = G_TYPE_FROM_INSTANCE(obj);
3203 if (GTK_IS_BUILDABLE(obj))
3204 w_id = widget_id(GTK_BUILDABLE(obj));
3205 if (type == GTK_TYPE_TREE_VIEW_COLUMN) {
3206 GList *cells = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(obj));
3207 GtkTreeViewColumn *tv_col = GTK_TREE_VIEW_COLUMN(obj);
3208 GObject *view = G_OBJECT(
3209 gtk_tree_view_column_get_tree_view(tv_col));
3210 unsigned int i, n_cells = g_list_length(cells);
3212 g_list_free(cells);
3213 sig_conn(obj, "clicked", G_CALLBACK(cb_simple), info_txt_new(o, "clicked"));
3214 for (i = 1; i <= n_cells; i++) {
3215 GtkCellRenderer *renderer;
3216 gboolean editable = FALSE;
3218 if (!tree_view_column_get_renderer_column(ar->builder, ar->txt, tv_col,
3219 i, &renderer))
3220 continue;
3221 if (GTK_IS_CELL_RENDERER_TEXT(renderer)) {
3222 g_object_get(renderer, "editable", &editable, NULL);
3223 if (editable) {
3224 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
3226 g_signal_connect(renderer, "edited",
3227 G_CALLBACK(cb_tree_model_edit),
3228 info_obj_new(o, view, model));
3230 } else if (GTK_IS_CELL_RENDERER_TOGGLE(renderer)) {
3231 g_object_get(renderer, "activatable", &editable, NULL);
3232 if (editable) {
3233 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
3235 g_signal_connect(renderer, "toggled",
3236 G_CALLBACK(cb_tree_model_toggle),
3237 info_obj_new(o, NULL, model));
3241 } else if (type == GTK_TYPE_LINK_BUTTON)
3242 sig_conn(obj, "activate-link", G_CALLBACK(cb_simple), info_txt_new(o, "clicked"));
3243 else if (type == GTK_TYPE_BUTTON)
3244 /* Button associated with a GtkTextView. */
3245 if ((suffix = strstr(w_id, "_send_text")) != NULL &&
3246 GTK_IS_TEXT_VIEW(obj2 = obj_sans_suffix(ar->builder, suffix, w_id)))
3247 sig_conn(obj, "clicked", G_CALLBACK(cb_send_text),
3248 info_obj_new(o, G_OBJECT(gtk_text_view_get_buffer(GTK_TEXT_VIEW(obj2))), NULL));
3249 else if ((suffix = strstr(w_id, "_send_selection")) != NULL &&
3250 GTK_IS_TEXT_VIEW(obj2 = obj_sans_suffix(ar->builder, suffix, w_id)))
3251 sig_conn(obj, "clicked", G_CALLBACK(cb_send_text_selection),
3252 info_obj_new(o, G_OBJECT(gtk_text_view_get_buffer(GTK_TEXT_VIEW(obj2))), NULL));
3253 else {
3254 sig_conn(obj, "clicked", G_CALLBACK(cb_simple), info_txt_new(o, "clicked"));
3255 /* Buttons associated with (and part of) a GtkDialog.
3256 * (We shun response ids which could be returned from
3257 * gtk_dialog_run() because that would require the
3258 * user to define those response ids in Glade,
3259 * numerically */
3260 if ((suffix = strstr(w_id, "_cancel")) != NULL &&
3261 GTK_IS_DIALOG(obj2 = obj_sans_suffix(ar->builder, suffix, w_id)))
3262 if (eql(widget_id(GTK_BUILDABLE(obj2)), MAIN_WIN))
3263 sig_conn_swapped(obj, "clicked",
3264 G_CALLBACK(gtk_main_quit), NULL);
3265 else
3266 sig_conn_swapped(obj, "clicked",
3267 G_CALLBACK(gtk_widget_hide), obj2);
3268 else if ((suffix = strstr(w_id, "_ok")) != NULL &&
3269 GTK_IS_DIALOG(obj2 = obj_sans_suffix(ar->builder, suffix, w_id))) {
3270 if (GTK_IS_FILE_CHOOSER_DIALOG(obj2))
3271 sig_conn_swapped(obj, "clicked",
3272 G_CALLBACK(cb_send_file_chooser_dialog_selection),
3273 info_obj_new(o, obj2, NULL));
3274 if (eql(widget_id(GTK_BUILDABLE(obj2)), MAIN_WIN))
3275 sig_conn_swapped(obj, "clicked",
3276 G_CALLBACK(gtk_main_quit), NULL);
3277 else
3278 sig_conn_swapped(obj, "clicked",
3279 G_CALLBACK(gtk_widget_hide), obj2);
3280 } else if ((suffix = strstr(w_id, "_apply")) != NULL &&
3281 GTK_IS_FILE_CHOOSER_DIALOG(obj2 = obj_sans_suffix(ar->builder, suffix, w_id)))
3282 sig_conn_swapped(obj, "clicked",
3283 G_CALLBACK(cb_send_file_chooser_dialog_selection),
3284 info_obj_new(o, obj2, NULL));
3286 else if (GTK_IS_MENU_ITEM(obj))
3287 if ((suffix = strstr(w_id, "_invoke")) != NULL &&
3288 GTK_IS_DIALOG(obj2 = obj_sans_suffix(ar->builder, suffix, w_id)))
3289 sig_conn_swapped(obj, "activate",
3290 G_CALLBACK(gtk_widget_show), obj2);
3291 else
3292 sig_conn(obj, "activate",
3293 G_CALLBACK(cb_menu_item), info_txt_new(o, "active"));
3294 else if (GTK_IS_WINDOW(obj)) {
3295 sig_conn(obj, "delete-event",
3296 G_CALLBACK(cb_event_simple), info_txt_new(o, "closed"));
3297 if (eql(w_id, MAIN_WIN))
3298 sig_conn_swapped(obj, "delete-event",
3299 G_CALLBACK(gtk_main_quit), NULL);
3300 else
3301 sig_conn(obj, "delete-event",
3302 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
3303 } else if (type == GTK_TYPE_FILE_CHOOSER_BUTTON)
3304 sig_conn(obj, "file-set",
3305 G_CALLBACK(cb_file_chooser_button), info_txt_new(o, "file"));
3306 else if (type == GTK_TYPE_COLOR_BUTTON)
3307 sig_conn(obj, "color-set",
3308 G_CALLBACK(cb_color_button), info_txt_new(o, "color"));
3309 else if (type == GTK_TYPE_FONT_BUTTON)
3310 sig_conn(obj, "font-set",
3311 G_CALLBACK(cb_font_button), info_txt_new(o, "font"));
3312 else if (type == GTK_TYPE_SWITCH)
3313 sig_conn(obj, "notify::active",
3314 G_CALLBACK(cb_switch), info_txt_new(o, NULL));
3315 else if (type == GTK_TYPE_TOGGLE_BUTTON ||
3316 type == GTK_TYPE_RADIO_BUTTON ||
3317 type == GTK_TYPE_CHECK_BUTTON)
3318 sig_conn(obj, "toggled",
3319 G_CALLBACK(cb_toggle_button), info_txt_new(o, NULL));
3320 else if (type == GTK_TYPE_ENTRY)
3321 sig_conn(obj, "changed",
3322 G_CALLBACK(cb_editable), info_txt_new(o, "text"));
3323 else if (type == GTK_TYPE_SPIN_BUTTON)
3324 sig_conn(obj, "value_changed",
3325 G_CALLBACK(cb_spin_button), info_txt_new(o, "text")); /* TODO: rename to "value" */
3326 else if (type == GTK_TYPE_SCALE)
3327 sig_conn(obj, "value-changed",
3328 G_CALLBACK(cb_range), info_txt_new(o, "value"));
3329 else if (type == GTK_TYPE_CALENDAR) {
3330 sig_conn(obj, "day-selected-double-click",
3331 G_CALLBACK(cb_calendar), info_txt_new(o, "doubleclicked"));
3332 sig_conn(obj, "day-selected",
3333 G_CALLBACK(cb_calendar), info_txt_new(o, "clicked"));
3334 } else if (type == GTK_TYPE_TREE_SELECTION)
3335 sig_conn(obj, "changed",
3336 G_CALLBACK(cb_tree_selection), info_txt_new(o, "clicked"));
3337 else if (type == GTK_TYPE_SOCKET) {
3338 sig_conn(obj, "plug-added",
3339 G_CALLBACK(cb_simple), info_txt_new(o, "plug-added"));
3340 sig_conn(obj, "plug-removed",
3341 G_CALLBACK(cb_simple), info_txt_new(o, "plug-removed"));
3342 /* TODO: rename to plug_added, plug_removed */
3343 } else if (type == GTK_TYPE_DRAWING_AREA)
3344 sig_conn(obj, "draw", G_CALLBACK(cb_draw), NULL);
3345 else if (type == GTK_TYPE_EVENT_BOX) {
3346 gtk_widget_set_can_focus(GTK_WIDGET(obj), true);
3347 sig_conn(obj, "button-press-event",
3348 G_CALLBACK(cb_event_box_button),
3349 info_txt_new(o, "button_press"));
3350 sig_conn(obj, "button-release-event",
3351 G_CALLBACK(cb_event_box_button),
3352 info_txt_new(o, "button_release"));
3353 sig_conn(obj, "motion-notify-event",
3354 G_CALLBACK(cb_event_box_motion),
3355 info_txt_new(o, "motion"));
3356 sig_conn(obj, "key-press-event",
3357 G_CALLBACK(cb_event_box_key),
3358 info_txt_new(o, "key_press"));
3363 * We keep a style provider with each widget
3365 static void
3366 add_widget_style_provider(gpointer *obj, void *data)
3368 GtkCssProvider *style_provider;
3369 GtkStyleContext *context;
3371 (void) data;
3372 if (!GTK_IS_WIDGET(obj))
3373 return;
3374 style_provider = gtk_css_provider_new();
3375 context = gtk_widget_get_style_context(GTK_WIDGET(obj));
3376 gtk_style_context_add_provider(context,
3377 GTK_STYLE_PROVIDER(style_provider),
3378 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
3379 g_object_set_data(G_OBJECT(obj), "style_provider", style_provider);
3382 static void
3383 prepare_widgets(GtkBuilder *builder, char *ui_file, FILE *out)
3385 GSList *objects = NULL;
3386 struct info ar = {.builder = builder, .fout = out, .txt = ui_file};
3388 objects = gtk_builder_get_objects(builder);
3389 g_slist_foreach(objects, (GFunc) connect_widget_signals, &ar);
3390 g_slist_foreach(objects, (GFunc) add_widget_style_provider, NULL);
3391 g_slist_free(objects);
3395 main(int argc, char *argv[])
3397 GObject *main_window = NULL;
3398 bool bg = false;
3399 char *in_fifo = NULL, *out_fifo = NULL;
3400 char *ui_file = "pipeglade.ui", *log_file = NULL, *err_file = NULL;
3401 char *xid = NULL;
3402 char opt;
3403 pthread_t receiver;
3404 struct info ar;
3406 /* Disable runtime GLIB deprecation warnings: */
3407 setenv("G_ENABLE_DIAGNOSTIC", "0", 0);
3408 gtk_init(&argc, &argv);
3409 while ((opt = getopt(argc, argv, "bGhe:i:l:o:O:u:V")) != -1) {
3410 switch (opt) {
3411 case 'b': bg = true; break;
3412 case 'e': xid = optarg; break;
3413 case 'G': show_lib_versions(); break;
3414 case 'h': bye(EXIT_SUCCESS, stdout, USAGE); break;
3415 case 'i': in_fifo = optarg; break;
3416 case 'l': log_file = optarg; break;
3417 case 'o': out_fifo = optarg; break;
3418 case 'O': err_file = optarg; break;
3419 case 'u': ui_file = optarg; break;
3420 case 'V': bye(EXIT_SUCCESS, stdout, "%s\n", VERSION); break;
3421 case '?':
3422 default: bye(EXIT_FAILURE, stderr, USAGE); break;
3425 if (argv[optind] != NULL)
3426 bye(EXIT_FAILURE, stderr,
3427 "illegal parameter '%s'\n" USAGE, argv[optind]);
3428 redirect_stderr(err_file);
3429 ar.fin = open_fifo(in_fifo, "r", stdin, _IONBF);
3430 ar.fout = open_fifo(out_fifo, "w", stdout, _IOLBF);
3431 go_bg_if(bg, ar.fin, ar.fout, err_file);
3432 ar.builder = builder_from_file(ui_file);
3433 ar.flog = open_log(log_file);
3434 pthread_create(&receiver, NULL, (void *(*)(void *)) digest_cmd, &ar);
3435 main_window = find_main_window(ar.builder);
3436 xmlInitParser();
3437 LIBXML_TEST_VERSION;
3438 prepare_widgets(ar.builder, ui_file, ar.fout);
3439 xembed_if(xid, main_window);
3440 gtk_main();
3441 pthread_cancel(receiver);
3442 pthread_join(receiver, NULL);
3443 xmlCleanupParser();
3444 rm_unless(stdin, ar.fin, in_fifo);
3445 rm_unless(stdout, ar.fout, out_fifo);
3446 exit(EXIT_SUCCESS);