2 * Copyright (c) 2014-2016 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>
26 #include <cairo-svg.h>
30 #include <gtk/gtkunixprint.h>
33 #include <libxml/xpath.h>
42 #include <sys/select.h>
47 #define VERSION "4.7.0"
49 #define WHITESPACE " \t\n"
50 #define MAIN_WIN "main"
52 "usage: pipeglade [[-i in-fifo] " \
55 "[-u glade-file.ui] " \
59 "[--display X-server]] | " \
68 __func__, __FILE__, __LINE__); \
75 "Out of memory in %s (%s:%d): ", \
76 __func__, __FILE__, __LINE__); \
82 * ============================================================
84 * ============================================================
88 * Check if s1 and s2 are equal strings
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
100 bye(int status
, FILE *s
, const char *fmt
, ...)
105 vfprintf(s
, fmt
, ap
);
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
127 xembed_if(char *xid_s
, GObject
*main_window
)
129 GtkWidget
*plug
, *body
;
133 if (xid_s
== NULL
) { /* standalone */
134 gtk_widget_show(GTK_WIDGET(main_window
));
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
157 redirect_stderr(const char *name
)
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);
173 * fork() if requested in bg; give up on errors
176 go_bg_if(bool bg
, FILE *in
, FILE *out
, char *err_file
)
182 if (in
== stdin
|| out
== stdout
)
183 bye(EXIT_FAILURE
, stderr
,
184 "parameter -b requires both -i and -o\n");
187 bye(EXIT_FAILURE
, stderr
,
188 "going to background: %s\n", strerror(errno
));
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
206 char *lc
= setlocale(LC_NUMERIC
, NULL
);
208 if ((lc_orig
= malloc(strlen(lc
) + 1)) == NULL
)
211 setlocale(LC_NUMERIC
, "C");
216 * Set locale (back) to lc; free lc
219 lc_numeric_free(char *lc
)
221 setlocale(LC_NUMERIC
, lc
);
226 * Print a warning about a malformed command to stderr. Runs inside
230 ign_cmd(GType type
, const char *msg
)
232 const char *name
, *pad
= " ";
234 if (type
== G_TYPE_INVALID
) {
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.
247 find_fifo(const char *n
, struct stat
*sb
)
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) {
259 bye(EXIT_FAILURE
, stderr
, "using pre-existing fifo %s: %s\n",
262 if (mkfifo(n
, 0600) != 0)
263 bye(EXIT_FAILURE
, stderr
, "making fifo %s: %s\n",
269 open_fifo(const char *name
, const char *fmode
, FILE *fallback
, int bmode
)
273 struct stat sb1
, sb2
;
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);
294 * Create a log file if necessary, and open it. A name of "-"
295 * requests use of stderr.
298 open_log(const char *name
)
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
));
316 * Delete fifo fn if streams s and forbidden are distinct
319 rm_unless(FILE *forbidden
, FILE *s
, char *fn
)
328 * Microseconds elapsed since start
331 usec_since(struct timespec
*start
)
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
344 fputs_escaped(const char *s
, FILE *o
)
349 while ((c
= s
[i
++]) != '\0')
351 case '\\': fputs("\\\\", o
); break;
352 case '\n': fputs("\\n", o
); break;
353 default: putc(c
, o
); break;
361 log_msg(FILE *l
, char *msg
)
363 static char *old_msg
;
364 static struct timespec start
;
366 if (l
== NULL
) /* no logging */
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
);
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
)
382 strcpy(old_msg
, msg
);
385 clock_gettime(CLOCK_MONOTONIC
, &start
);
389 has_suffix(const char *s
, const char *suffix
)
391 int s_suf
= strlen(s
) - strlen(suffix
);
395 return eql(suffix
, s
+ s_suf
);
399 * Remove suffix from name; find the object named like this
402 obj_sans_suffix(GtkBuilder
*builder
, const char *suffix
, const char *name
)
404 char str
[BUFLEN
+ 1] = {'\0'};
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
416 builder_from_file(char *ui_file
)
418 GError
*error
= NULL
;
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
);
428 * Return the id attribute of widget
431 widget_id(GtkBuildable
*widget
)
433 return gtk_buildable_get_name(widget
);
437 * Get the main window; give up on errors
440 find_main_window(GtkBuilder
*builder
)
444 if (GTK_IS_WINDOW(mw
= gtk_builder_get_object(builder
, MAIN_WIN
)))
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.
455 read_buf(FILE *s
, char **buf
, size_t *bufsize
)
466 select(ifd
+ 1, &rfds
, NULL
, NULL
, NULL
);
468 if (c
== '\n' || feof(s
))
470 if (i
>= *bufsize
- 1)
471 if ((*buf
= realloc(*buf
, *bufsize
*= 2)) == NULL
)
476 case 'n': (*buf
)[i
++] = '\n'; break;
477 case 'r': (*buf
)[i
++] = '\r'; break;
478 default: (*buf
)[i
++] = c
; break;
480 } else if (c
== '\\')
491 * ============================================================
492 * Receiving feedback from the GUI
493 * ============================================================
497 send_msg_to(FILE* o
, GtkBuildable
*obj
, const char *tag
, va_list ap
)
500 const char *w_id
= widget_id(obj
);
503 struct timeval timeout
= {1, 0};
507 if (select(ofd
+ 1, NULL
, &wfds
, NULL
, &timeout
) == 1) {
508 fprintf(o
, "%s:%s ", w_id
, tag
);
509 while ((data
= va_arg(ap
, char *)) != NULL
)
510 fputs_escaped(data
, o
);
514 "send error; discarding feedback message %s:%s\n",
519 * Send GUI feedback to stream o. The message format is
520 * "<origin>:<tag> <data ...>". The variadic arguments are strings;
521 * last argument must be NULL.
524 send_msg(FILE *o
, GtkBuildable
*obj
, const char *tag
, ...)
529 send_msg_to(o
, obj
, tag
, ap
);
534 * Send message from GUI to stream o. The message format is
535 * "<origin>:set <data ...>", which happens to be a legal command.
536 * The variadic arguments are strings; last argument must be NULL.
539 send_msg_as_cmd(FILE *o
, GtkBuildable
*obj
, const char *tag
, ...)
544 send_msg_to(o
, obj
, "set", ap
);
549 * Stuff to pass around
552 FILE *fout
; /* UI feedback messages */
553 FILE *fin
; /* command input */
554 FILE *flog
; /* logging output */
555 GtkBuilder
*builder
; /* to be read from .ui file */
562 * Data to be passed to and from the GTK main loop
565 void (*fn
)(struct ui_data
*);
576 * Return pointer to a newly allocated struct info
579 info_new_full(FILE *stream
, GObject
*obj
, GtkTreeModel
*model
, char *txt
)
583 if ((ar
= malloc(sizeof(struct info
))) == NULL
)
596 info_txt_new(FILE *stream
, char *txt
)
598 return info_new_full(stream
, NULL
, NULL
, txt
);
602 info_obj_new(FILE *stream
, GObject
*obj
, GtkTreeModel
*model
)
604 return info_new_full(stream
, obj
, model
, NULL
);
608 * Use msg_sender() to send a message describing a particular cell
611 send_tree_cell_msg_by(void msg_sender(FILE *, GtkBuildable
*, const char *, ...),
613 GtkTreeIter
*iter
, int col
, struct info
*ar
)
615 GtkBuildable
*obj
= GTK_BUILDABLE(ar
->obj
);
616 GtkTreeModel
*model
= ar
->model
;
618 GValue value
= G_VALUE_INIT
;
619 char str
[BUFLEN
], *lc
= lc_numeric();
621 gtk_tree_model_get_value(model
, iter
, col
, &value
);
622 col_type
= gtk_tree_model_get_column_type(model
, col
);
625 snprintf(str
, BUFLEN
, " %d %d", col
, g_value_get_int(&value
));
626 msg_sender(ar
->fout
, obj
, "gint", path_s
, str
, NULL
);
629 snprintf(str
, BUFLEN
, " %d %ld", col
, g_value_get_long(&value
));
630 msg_sender(ar
->fout
, obj
, "glong", path_s
, str
, NULL
);
633 snprintf(str
, BUFLEN
, " %d %" PRId64
, col
, g_value_get_int64(&value
));
634 msg_sender(ar
->fout
, obj
, "gint64", path_s
, str
, NULL
);
637 snprintf(str
, BUFLEN
, " %d %u", col
, g_value_get_uint(&value
));
638 msg_sender(ar
->fout
, obj
, "guint", path_s
, str
, NULL
);
641 snprintf(str
, BUFLEN
, " %d %lu", col
, g_value_get_ulong(&value
));
642 msg_sender(ar
->fout
, obj
, "gulong", path_s
, str
, NULL
);
645 snprintf(str
, BUFLEN
, " %d %" PRIu64
, col
, g_value_get_uint64(&value
));
646 msg_sender(ar
->fout
, obj
, "guint64", path_s
, str
, NULL
);
649 snprintf(str
, BUFLEN
, " %d %d", col
, g_value_get_boolean(&value
));
650 msg_sender(ar
->fout
, obj
, "gboolean", path_s
, str
, NULL
);
653 snprintf(str
, BUFLEN
, " %d %f", col
, g_value_get_float(&value
));
654 msg_sender(ar
->fout
, obj
, "gfloat", path_s
, str
, NULL
);
657 snprintf(str
, BUFLEN
, " %d %f", col
, g_value_get_double(&value
));
658 msg_sender(ar
->fout
, obj
, "gdouble", path_s
, str
, NULL
);
661 snprintf(str
, BUFLEN
, " %d ", col
);
662 msg_sender(ar
->fout
, obj
, "gchararray", path_s
, str
, g_value_get_string(&value
), NULL
);
665 fprintf(stderr
, "column %d not implemented: %s\n", col
, G_VALUE_TYPE_NAME(&value
));
668 g_value_unset(&value
);
673 * Use msg_sender() to send one message per column for a single row
676 send_tree_row_msg_by(void msg_sender(FILE *, GtkBuildable
*, const char *, ...),
677 char *path_s
, GtkTreeIter
*iter
, struct info
*ar
)
681 for (col
= 0; col
< gtk_tree_model_get_n_columns(ar
->model
); col
++)
682 send_tree_cell_msg_by(msg_sender
, path_s
, iter
, col
, ar
);
686 * send_tree_row_msg serves as an argument for
687 * gtk_tree_selection_selected_foreach()
690 send_tree_row_msg(GtkTreeModel
*model
,
691 GtkTreePath
*path
, GtkTreeIter
*iter
, struct info
*ar
)
693 char *path_s
= gtk_tree_path_to_string(path
);
696 send_tree_row_msg_by(send_msg
, path_s
, iter
, ar
);
702 * save_tree_row_msg serves as an argument for
703 * gtk_tree_model_foreach().
704 * Send message from GUI to global stream "save".
707 save_tree_row_msg(GtkTreeModel
*model
,
708 GtkTreePath
*path
, GtkTreeIter
*iter
, struct info
*ar
)
710 char *path_s
= gtk_tree_path_to_string(path
);
713 send_tree_row_msg_by(send_msg_as_cmd
, path_s
, iter
, ar
);
719 cb_calendar(GtkBuildable
*obj
, struct info
*ar
)
722 unsigned int year
= 0, month
= 0, day
= 0;
724 gtk_calendar_get_date(GTK_CALENDAR(obj
), &year
, &month
, &day
);
725 snprintf(str
, BUFLEN
, "%04u-%02u-%02u", year
, ++month
, day
);
726 send_msg(ar
->fout
, obj
, ar
->txt
, str
, NULL
);
730 cb_color_button(GtkBuildable
*obj
, struct info
*ar
)
734 gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(obj
), &color
);
735 send_msg(ar
->fout
, obj
, ar
->txt
, gdk_rgba_to_string(&color
), NULL
);
739 cb_editable(GtkBuildable
*obj
, struct info
*ar
)
741 send_msg(ar
->fout
, obj
, ar
->txt
, gtk_entry_get_text(GTK_ENTRY(obj
)), NULL
);
745 * Callback that sends a message about a pointer device button press
749 cb_event_box_button(GtkBuildable
*obj
, GdkEvent
*e
, struct info
*ar
)
751 char data
[BUFLEN
], *lc
= lc_numeric();
753 snprintf(data
, BUFLEN
, "%d %.1lf %.1lf",
754 e
->button
.button
, e
->button
.x
, e
->button
.y
);
755 send_msg(ar
->fout
, obj
, ar
->txt
, data
, NULL
);
761 * Callback that sends in a message the name of the key pressed when
762 * a GtkEventBox is focused
765 cb_event_box_key(GtkBuildable
*obj
, GdkEvent
*e
, struct info
*ar
)
767 send_msg(ar
->fout
, obj
, ar
->txt
, gdk_keyval_name(e
->key
.keyval
), NULL
);
772 * Callback that sends a message about pointer device motion in a
776 cb_event_box_motion(GtkBuildable
*obj
, GdkEvent
*e
, struct info
*ar
)
778 char data
[BUFLEN
], *lc
= lc_numeric();
780 snprintf(data
, BUFLEN
, "%.1lf %.1lf", e
->button
.x
, e
->button
.y
);
781 send_msg(ar
->fout
, obj
, ar
->txt
, data
, NULL
);
787 * Callback that only sends "name:tag" and returns false
790 cb_event_simple(GtkBuildable
*obj
, GdkEvent
*e
, struct info
*ar
)
793 send_msg(ar
->fout
, obj
, ar
->txt
, NULL
);
798 cb_file_chooser_button(GtkBuildable
*obj
, struct info
*ar
)
800 send_msg(ar
->fout
, obj
, ar
->txt
,
801 gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(obj
)), NULL
);
805 cb_font_button(GtkBuildable
*obj
, struct info
*ar
)
807 send_msg(ar
->fout
, obj
, ar
->txt
,
808 gtk_font_button_get_font_name(GTK_FONT_BUTTON(obj
)), NULL
);
812 cb_menu_item(GtkBuildable
*obj
, struct info
*ar
)
814 send_msg(ar
->fout
, obj
, ar
->txt
,
815 gtk_menu_item_get_label(GTK_MENU_ITEM(obj
)), NULL
);
819 cb_range(GtkBuildable
*obj
, struct info
*ar
)
821 char str
[BUFLEN
], *lc
= lc_numeric();
823 snprintf(str
, BUFLEN
, "%f", gtk_range_get_value(GTK_RANGE(obj
)));
824 send_msg(ar
->fout
, obj
, ar
->txt
, str
, NULL
);
829 * Callback that sends user's selection from a file dialog
832 cb_send_file_chooser_dialog_selection(struct info
*ar
)
834 send_msg(ar
->fout
, GTK_BUILDABLE(ar
->obj
), "file",
835 gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(ar
->obj
)),
837 send_msg(ar
->fout
, GTK_BUILDABLE(ar
->obj
), "folder",
838 gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(ar
->obj
)),
843 * Callback that sends in a message the content of the text buffer
844 * passed in user_data
847 cb_send_text(GtkBuildable
*obj
, struct info
*ar
)
851 gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(ar
->obj
), &a
, &b
);
852 send_msg(ar
->fout
, obj
, "text",
853 gtk_text_buffer_get_text(GTK_TEXT_BUFFER(ar
->obj
), &a
, &b
, TRUE
),
858 * Callback that sends in a message the highlighted text from the text
859 * buffer which was passed in user_data
862 cb_send_text_selection(GtkBuildable
*obj
, struct info
*ar
)
866 gtk_text_buffer_get_selection_bounds(GTK_TEXT_BUFFER(ar
->obj
), &a
, &b
);
867 send_msg(ar
->fout
, obj
, "text",
868 gtk_text_buffer_get_text(GTK_TEXT_BUFFER(ar
->obj
), &a
, &b
, TRUE
),
873 * Callback that only sends "name:tag" and returns true
876 cb_simple(GtkBuildable
*obj
, struct info
*ar
)
878 send_msg(ar
->fout
, obj
, ar
->txt
, NULL
);
883 cb_spin_button(GtkBuildable
*obj
, struct info
*ar
)
885 char str
[BUFLEN
], *lc
= lc_numeric();
887 snprintf(str
, BUFLEN
, "%f", gtk_spin_button_get_value(GTK_SPIN_BUTTON(obj
)));
888 send_msg(ar
->fout
, obj
, ar
->txt
, str
, NULL
);
893 cb_switch(GtkBuildable
*obj
, void *pspec
, struct info
*ar
)
896 send_msg(ar
->fout
, obj
,
897 gtk_switch_get_active(GTK_SWITCH(obj
)) ? "1" : "0",
902 cb_toggle_button(GtkBuildable
*obj
, struct info
*ar
)
904 send_msg(ar
->fout
, obj
,
905 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(obj
)) ? "1" : "0",
910 cb_tree_selection(GtkBuildable
*obj
, struct info
*ar
)
912 GtkTreeSelection
*sel
= GTK_TREE_SELECTION(obj
);
913 GtkTreeView
*view
= gtk_tree_selection_get_tree_view(sel
);
915 ar
->obj
= G_OBJECT(view
);
916 send_msg(ar
->fout
, GTK_BUILDABLE(view
), ar
->txt
, NULL
);
917 gtk_tree_selection_selected_foreach(
918 sel
, (GtkTreeSelectionForeachFunc
) send_tree_row_msg
, ar
);
923 * ============================================================
924 * cb_draw() maintains a drawing on a GtkDrawingArea; it needs a few
926 * ============================================================
930 * The set of supported drawing operations
961 * Text placement mode for rel_move_for()
975 enum draw_op_policy
{
982 * One single element of a drawing
985 struct draw_op
*next
;
986 struct draw_op
*prev
;
987 unsigned long long int id
;
988 unsigned long long int before
;
989 enum draw_op_policy policy
;
995 * Argument sets for the various drawing operations
1005 struct curve_to_args
{
1014 struct move_to_args
{
1019 struct rectangle_args
{
1026 struct rel_move_for_args
{
1032 struct set_dash_args
{
1037 struct set_font_face_args
{
1038 cairo_font_slant_t slant
;
1039 cairo_font_weight_t weight
;
1043 struct set_font_size_args
{
1047 struct set_line_cap_args
{
1048 cairo_line_cap_t line_cap
;
1051 struct set_line_join_args
{
1052 cairo_line_join_t line_join
;
1055 struct set_line_width_args
{
1059 struct set_source_rgba_args
{
1063 struct show_text_args
{
1068 struct transform_args
{
1069 cairo_matrix_t matrix
;
1073 draw(cairo_t
*cr
, enum cairo_fn op
, void *op_args
)
1077 struct move_to_args
*args
= op_args
;
1079 cairo_line_to(cr
, args
->x
, args
->y
);
1083 struct move_to_args
*args
= op_args
;
1085 cairo_rel_line_to(cr
, args
->x
, args
->y
);
1089 struct move_to_args
*args
= op_args
;
1091 cairo_move_to(cr
, args
->x
, args
->y
);
1095 struct move_to_args
*args
= op_args
;
1097 cairo_rel_move_to(cr
, args
->x
, args
->y
);
1101 struct arc_args
*args
= op_args
;
1103 cairo_arc(cr
, args
->x
, args
->y
, args
->radius
, args
->angle1
, args
->angle2
);
1106 case ARC_NEGATIVE
: {
1107 struct arc_args
*args
= op_args
;
1109 cairo_arc_negative(cr
, args
->x
, args
->y
, args
->radius
, args
->angle1
, args
->angle2
);
1113 struct curve_to_args
*args
= op_args
;
1115 cairo_curve_to(cr
, args
->x1
, args
->y1
, args
->x2
, args
->y2
, args
->x3
, args
->y3
);
1118 case REL_CURVE_TO
: {
1119 struct curve_to_args
*args
= op_args
;
1121 cairo_curve_to(cr
, args
->x1
, args
->y1
, args
->x2
, args
->y2
, args
->x3
, args
->y3
);
1125 struct rectangle_args
*args
= op_args
;
1127 cairo_rectangle(cr
, args
->x
, args
->y
, args
->width
, args
->height
);
1131 cairo_close_path(cr
);
1134 struct show_text_args
*args
= op_args
;
1136 cairo_show_text(cr
, args
->text
);
1139 case REL_MOVE_FOR
: {
1140 cairo_text_extents_t e
;
1141 double dx
= 0.0, dy
= 0.0;
1142 struct rel_move_for_args
*args
= op_args
;
1144 cairo_text_extents(cr
, args
->text
, &e
);
1145 switch (args
->ref
) {
1146 case C
: dx
= -e
.width
/ 2; dy
= e
.height
/ 2; break;
1147 case E
: dx
= -e
.width
; dy
= e
.height
/ 2; break;
1148 case N
: dx
= -e
.width
/ 2; dy
= e
.height
; break;
1149 case NE
: dx
= -e
.width
; dy
= e
.height
; break;
1150 case NW
: dy
= e
.height
; break;
1151 case S
: dx
= -e
.width
/ 2; break;
1152 case SE
: dx
= -e
.width
; break;
1154 case W
: dy
= e
.height
/ 2; break;
1155 default: ABORT
; break;
1157 cairo_rel_move_to(cr
, dx
, dy
);
1161 cairo_identity_matrix(cr
);
1166 case STROKE_PRESERVE
:
1167 cairo_stroke_preserve(cr
);
1173 cairo_fill_preserve(cr
);
1176 struct set_dash_args
*args
= op_args
;
1178 cairo_set_dash(cr
, args
->dashes
, args
->num_dashes
, 0);
1181 case SET_FONT_FACE
: {
1182 struct set_font_face_args
*args
= op_args
;
1184 cairo_select_font_face(cr
, args
->family
, args
->slant
, args
->weight
);
1187 case SET_FONT_SIZE
: {
1188 struct set_font_size_args
*args
= op_args
;
1190 cairo_set_font_size(cr
, args
->size
);
1193 case SET_LINE_CAP
: {
1194 struct set_line_cap_args
*args
= op_args
;
1196 cairo_set_line_cap(cr
, args
->line_cap
);
1199 case SET_LINE_JOIN
: {
1200 struct set_line_join_args
*args
= op_args
;
1202 cairo_set_line_join(cr
, args
->line_join
);
1205 case SET_LINE_WIDTH
: {
1206 struct set_line_width_args
*args
= op_args
;
1208 cairo_set_line_width(cr
, args
->width
);
1211 case SET_SOURCE_RGBA
: {
1212 struct set_source_rgba_args
*args
= op_args
;
1214 gdk_cairo_set_source_rgba(cr
, &args
->color
);
1218 struct transform_args
*args
= op_args
;
1220 cairo_transform(cr
, &args
->matrix
);
1230 * Callback that draws on a GtkDrawingArea
1233 cb_draw(GtkWidget
*widget
, cairo_t
*cr
, gpointer data
)
1238 for (op
= g_object_get_data(G_OBJECT(widget
), "draw_ops");
1241 draw(cr
, op
->op
, op
->op_args
);
1247 * ============================================================
1248 * Manipulating the GUI
1249 * ============================================================
1253 update_button(struct ui_data
*ud
)
1255 if (eql(ud
->action
, "set_label"))
1256 gtk_button_set_label(GTK_BUTTON(ud
->obj
), ud
->data
);
1258 ign_cmd(ud
->type
, ud
->cmd
);
1262 update_calendar(struct ui_data
*ud
)
1264 GtkCalendar
*calendar
= GTK_CALENDAR(ud
->obj
);
1266 int year
= 0, month
= 0, day
= 0;
1268 if (eql(ud
->action
, "select_date") &&
1269 sscanf(ud
->data
, "%d-%d-%d %c", &year
, &month
, &day
, &dummy
) == 3) {
1270 if (month
> -1 && month
<= 11 && day
> 0 && day
<= 31) {
1271 gtk_calendar_select_month(calendar
, --month
, year
);
1272 gtk_calendar_select_day(calendar
, day
);
1274 ign_cmd(ud
->type
, ud
->cmd
);
1275 } else if (eql(ud
->action
, "mark_day") &&
1276 sscanf(ud
->data
, "%d %c", &day
, &dummy
) == 1) {
1277 if (day
> 0 && day
<= 31)
1278 gtk_calendar_mark_day(calendar
, day
);
1280 ign_cmd(ud
->type
, ud
->cmd
);
1281 } else if (eql(ud
->action
, "clear_marks") && sscanf(ud
->data
, " %c", &dummy
) < 1)
1282 gtk_calendar_clear_marks(calendar
);
1284 ign_cmd(ud
->type
, ud
->cmd
);
1288 * Common actions for various kinds of window. Return false if
1289 * command is ignored. Runs inside gtk_main().
1292 update_class_window(struct ui_data
*ud
)
1294 GtkWindow
*window
= GTK_WINDOW(ud
->obj
);
1298 if (eql(ud
->action
, "set_title"))
1299 gtk_window_set_title(window
, ud
->data
);
1300 else if (eql(ud
->action
, "fullscreen") && sscanf(ud
->data
, " %c", &dummy
) < 1)
1301 gtk_window_fullscreen(window
);
1302 else if (eql(ud
->action
, "unfullscreen") && sscanf(ud
->data
, " %c", &dummy
) < 1)
1303 gtk_window_unfullscreen(window
);
1304 else if (eql(ud
->action
, "resize") &&
1305 sscanf(ud
->data
, "%d %d %c", &x
, &y
, &dummy
) == 2)
1306 gtk_window_resize(window
, x
, y
);
1307 else if (eql(ud
->action
, "resize") && sscanf(ud
->data
, " %c", &dummy
) < 1) {
1308 gtk_window_get_default_size(window
, &x
, &y
);
1309 gtk_window_resize(window
, x
, y
);
1310 } else if (eql(ud
->action
, "move") &&
1311 sscanf(ud
->data
, "%d %d %c", &x
, &y
, &dummy
) == 2)
1312 gtk_window_move(window
, x
, y
);
1319 update_color_button(struct ui_data
*ud
)
1323 if (eql(ud
->action
, "set_color")) {
1324 gdk_rgba_parse(&color
, ud
->data
);
1325 gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(ud
->obj
), &color
);
1327 ign_cmd(ud
->type
, ud
->cmd
);
1331 update_combo_box_text(struct ui_data
*ud
)
1333 GtkComboBoxText
*combobox
= GTK_COMBO_BOX_TEXT(ud
->obj
);
1337 if (eql(ud
->action
, "prepend_text"))
1338 gtk_combo_box_text_prepend_text(combobox
, ud
->data
);
1339 else if (eql(ud
->action
, "append_text"))
1340 gtk_combo_box_text_append_text(combobox
, ud
->data
);
1341 else if (eql(ud
->action
, "remove") &&
1342 sscanf(ud
->data
, "%d %c", &pos
, &dummy
) == 1)
1343 gtk_combo_box_text_remove(combobox
, pos
);
1344 else if (eql(ud
->action
, "insert_text") &&
1345 sscanf(ud
->data
, "%d %n", &pos
, &txt0
) == 1)
1346 gtk_combo_box_text_insert_text(combobox
, pos
, ud
->data
+ txt0
);
1348 ign_cmd(ud
->type
, ud
->cmd
);
1352 * update_drawing_area(), which runs inside gtk_main(), maintains a
1353 * list of drawing operations. It needs a few helper functions. It
1354 * is the responsibility of cb_draw() to actually execute the list.
1364 * Fill structure *op with the drawing operation according to action
1365 * and with the appropriate set of arguments
1367 static enum draw_op_stat
1368 set_draw_op(struct draw_op
*op
, const char *action
, const char *data
)
1371 const char *raw_args
= data
;
1372 enum draw_op_stat result
= SUCCESS
;
1375 if (sscanf(data
, "=%llu %n", &op
->id
, &args_start
) == 1) {
1376 op
->policy
= REPLACE
;
1377 result
= NEED_REDRAW
;
1378 } else if (sscanf(data
, "%llu<%llu %n", &op
->id
, &op
->before
, &args_start
) == 2) {
1379 op
->policy
= BEFORE
;
1380 result
= NEED_REDRAW
;
1381 } else if (sscanf(data
, "%llu %n", &op
->id
, &args_start
) == 1)
1382 op
->policy
= APPEND
;
1385 raw_args
+= args_start
;
1386 if (eql(action
, "line_to")) {
1387 struct move_to_args
*args
;
1389 if ((args
= malloc(sizeof(*args
))) == NULL
)
1393 if (sscanf(raw_args
, "%lf %lf %c", &args
->x
, &args
->y
, &dummy
) != 2)
1395 } else if (eql(action
, "rel_line_to")) {
1396 struct move_to_args
*args
;
1398 if ((args
= malloc(sizeof(*args
))) == NULL
)
1400 op
->op
= REL_LINE_TO
;
1402 if (sscanf(raw_args
, "%lf %lf %c", &args
->x
, &args
->y
, &dummy
) != 2)
1404 } else if (eql(action
, "move_to")) {
1405 struct move_to_args
*args
;
1407 if ((args
= malloc(sizeof(*args
))) == NULL
)
1411 if (sscanf(raw_args
, "%lf %lf %c", &args
->x
, &args
->y
, &dummy
) != 2)
1413 } else if (eql(action
, "rel_move_to")) {
1414 struct move_to_args
*args
;
1416 if ((args
= malloc(sizeof(*args
))) == NULL
)
1418 op
->op
= REL_MOVE_TO
;
1420 if (sscanf(raw_args
, "%lf %lf %c", &args
->x
, &args
->y
, &dummy
) != 2)
1422 } else if (eql(action
, "arc")) {
1423 struct arc_args
*args
;
1426 if ((args
= malloc(sizeof(*args
))) == NULL
)
1430 if (sscanf(raw_args
, "%lf %lf %lf %lf %lf %c",
1431 &args
->x
, &args
->y
, &args
->radius
, °1
, °2
, &dummy
) != 5)
1433 args
->angle1
= deg1
* (M_PI
/ 180.L
);
1434 args
->angle2
= deg2
* (M_PI
/ 180.L
);
1435 } else if (eql(action
, "arc_negative")) {
1437 struct arc_args
*args
;
1439 if ((args
= malloc(sizeof(*args
))) == NULL
)
1441 op
->op
= ARC_NEGATIVE
;
1443 if (sscanf(raw_args
, "%lf %lf %lf %lf %lf %c",
1444 &args
->x
, &args
->y
, &args
->radius
, °1
, °2
, &dummy
) != 5)
1446 args
->angle1
= deg1
* (M_PI
/ 180.L
);
1447 args
->angle2
= deg2
* (M_PI
/ 180.L
);
1448 } else if (eql(action
, "curve_to")) {
1449 struct curve_to_args
*args
;
1451 if ((args
= malloc(sizeof(*args
))) == NULL
)
1455 if (sscanf(raw_args
, "%lf %lf %lf %lf %lf %lf %c",
1456 &args
->x1
, &args
->y1
, &args
->x2
, &args
->y2
, &args
->x3
, &args
->y3
, &dummy
) != 6)
1458 } else if (eql(action
, "rel_curve_to")) {
1459 struct curve_to_args
*args
;
1461 if ((args
= malloc(sizeof(*args
))) == NULL
)
1463 op
->op
= REL_CURVE_TO
;
1465 if (sscanf(raw_args
, "%lf %lf %lf %lf %lf %lf %c",
1466 &args
->x1
, &args
->y1
, &args
->x2
, &args
->y2
, &args
->x3
, &args
->y3
, &dummy
) != 6)
1468 } else if (eql(action
, "rectangle")) {
1469 struct rectangle_args
*args
;
1471 if ((args
= malloc(sizeof(*args
))) == NULL
)
1475 if (sscanf(raw_args
, "%lf %lf %lf %lf %c",
1476 &args
->x
, &args
->y
, &args
->width
, &args
->height
, &dummy
) != 4)
1478 } else if (eql(action
, "close_path")) {
1479 op
->op
= CLOSE_PATH
;
1480 if (sscanf(raw_args
, " %c", &dummy
) > 0)
1483 } else if (eql(action
, "show_text")) {
1484 struct show_text_args
*args
;
1487 len
= strlen(raw_args
) + 1;
1488 if ((args
= malloc(sizeof(*args
) + len
* sizeof(args
->text
[0]))) == NULL
)
1492 args
->len
= len
; /* not used */
1493 strncpy(args
->text
, raw_args
, len
);
1494 result
= NEED_REDRAW
;
1495 } else if (eql(action
, "rel_move_for")) {
1496 char ref_point
[2 + 1];
1498 struct rel_move_for_args
*args
;
1500 if (sscanf(raw_args
, "%2s %n", ref_point
, &start
) < 1)
1502 len
= strlen(raw_args
+ start
) + 1;
1503 if ((args
= malloc(sizeof(*args
) + len
* sizeof(args
->text
[0]))) == NULL
)
1505 if (eql(ref_point
, "c"))
1507 else if (eql(ref_point
, "e"))
1509 else if (eql(ref_point
, "n"))
1511 else if (eql(ref_point
, "ne"))
1513 else if (eql(ref_point
, "nw"))
1515 else if (eql(ref_point
, "s"))
1517 else if (eql(ref_point
, "se"))
1519 else if (eql(ref_point
, "sw"))
1521 else if (eql(ref_point
, "w"))
1525 op
->op
= REL_MOVE_FOR
;
1527 args
->len
= len
; /* not used */
1528 strncpy(args
->text
, (raw_args
+ start
), len
);
1529 } else if (eql(action
, "stroke")) {
1531 if (sscanf(raw_args
, " %c", &dummy
) > 0)
1534 result
= NEED_REDRAW
;
1535 } else if (eql(action
, "stroke_preserve")) {
1536 op
->op
= STROKE_PRESERVE
;
1537 if (sscanf(raw_args
, " %c", &dummy
) > 0)
1540 result
= NEED_REDRAW
;
1541 } else if (eql(action
, "fill")) {
1543 if (sscanf(raw_args
, " %c", &dummy
) > 0)
1546 result
= NEED_REDRAW
;
1547 } else if (eql(action
, "fill_preserve")) {
1548 op
->op
= FILL_PRESERVE
;
1549 if (sscanf(raw_args
, " %c", &dummy
) > 0)
1552 result
= NEED_REDRAW
;
1553 } else if (eql(action
, "set_dash")) {
1555 char data1
[strlen(raw_args
) + 1];
1557 struct set_dash_args
*args
;
1559 strcpy(data1
, raw_args
);
1566 } while (next
!= end
);
1567 if ((args
= malloc(sizeof(*args
) + n
* sizeof(args
->dashes
[0]))) == NULL
)
1571 args
->num_dashes
= n
;
1572 for (i
= 0, next
= data1
; i
< n
; i
++, next
= end
) {
1573 args
->dashes
[i
] = strtod(next
, &end
);
1575 } else if (eql(action
, "set_font_face")) {
1576 char slant
[7 + 1]; /* "oblique" */
1577 char weight
[6 + 1]; /* "normal" */
1578 int family_start
, family_len
;
1579 struct set_font_face_args
*args
;
1581 if (sscanf(raw_args
, "%7s %6s %n%*s", slant
, weight
, &family_start
) != 2)
1583 family_len
= strlen(raw_args
+ family_start
) + 1;
1584 if ((args
= malloc(sizeof(*args
) + family_len
* sizeof(args
->family
[0]))) == NULL
)
1586 op
->op
= SET_FONT_FACE
;
1588 strncpy(args
->family
, raw_args
+ family_start
, family_len
);
1589 if (eql(slant
, "normal"))
1590 args
->slant
= CAIRO_FONT_SLANT_NORMAL
;
1591 else if (eql(slant
, "italic"))
1592 args
->slant
= CAIRO_FONT_SLANT_ITALIC
;
1593 else if (eql(slant
, "oblique"))
1594 args
->slant
= CAIRO_FONT_SLANT_OBLIQUE
;
1597 if (eql(weight
, "normal"))
1598 args
->weight
= CAIRO_FONT_WEIGHT_NORMAL
;
1599 else if (eql(weight
, "bold"))
1600 args
->weight
= CAIRO_FONT_WEIGHT_BOLD
;
1603 } else if (eql(action
, "set_font_size")) {
1604 struct set_font_size_args
*args
;
1606 if ((args
= malloc(sizeof(*args
))) == NULL
)
1608 op
->op
= SET_FONT_SIZE
;
1610 if (sscanf(raw_args
, "%lf %c", &args
->size
, &dummy
) != 1)
1612 } else if (eql(action
, "set_line_cap")) {
1613 char str
[6 + 1]; /* "square" */
1614 struct set_line_cap_args
*args
;
1616 if ((args
= malloc(sizeof(*args
))) == NULL
)
1618 op
->op
= SET_LINE_CAP
;
1620 if (sscanf(raw_args
, "%6s %c", str
, &dummy
) != 1)
1622 if (eql(str
, "butt"))
1623 args
->line_cap
= CAIRO_LINE_CAP_BUTT
;
1624 else if (eql(str
, "round"))
1625 args
->line_cap
= CAIRO_LINE_CAP_ROUND
;
1626 else if (eql(str
, "square"))
1627 args
->line_cap
= CAIRO_LINE_CAP_SQUARE
;
1630 } else if (eql(action
, "set_line_join")) {
1631 char str
[5 + 1]; /* "miter" */
1632 struct set_line_join_args
*args
;
1634 if ((args
= malloc(sizeof(*args
))) == NULL
)
1636 op
->op
= SET_LINE_JOIN
;
1638 if (sscanf(raw_args
, "%5s %c", str
, &dummy
) != 1)
1640 if (eql(str
, "miter"))
1641 args
->line_join
= CAIRO_LINE_JOIN_MITER
;
1642 else if (eql(str
, "round"))
1643 args
->line_join
= CAIRO_LINE_JOIN_ROUND
;
1644 else if (eql(str
, "bevel"))
1645 args
->line_join
= CAIRO_LINE_JOIN_BEVEL
;
1648 } else if (eql(action
, "set_line_width")) {
1649 struct set_line_width_args
*args
;
1651 if ((args
= malloc(sizeof(*args
))) == NULL
)
1653 op
->op
= SET_LINE_WIDTH
;
1655 if (sscanf(raw_args
, "%lf %c", &args
->width
, &dummy
) != 1)
1657 } else if (eql(action
, "set_source_rgba")) {
1658 struct set_source_rgba_args
*args
;
1660 if ((args
= malloc(sizeof(*args
))) == NULL
)
1662 op
->op
= SET_SOURCE_RGBA
;
1664 gdk_rgba_parse(&args
->color
, raw_args
);
1665 } else if (eql(action
, "transform")) {
1667 double xx
, yx
, xy
, yy
, x0
, y0
;
1669 if (sscanf(raw_args
, "%lf %lf %lf %lf %lf %lf %c",
1670 &xx
, &yx
, &xy
, &yy
, &x0
, &y0
, &dummy
) == 6) {
1671 struct transform_args
*args
;
1673 if ((args
= malloc(sizeof(*args
))) == NULL
)
1677 cairo_matrix_init(&args
->matrix
, xx
, yx
, xy
, yy
, x0
, y0
);
1678 } else if (sscanf(raw_args
, " %c", &dummy
) < 1) {
1683 } else if (eql(action
, "translate")) {
1685 struct transform_args
*args
;
1687 if ((args
= malloc(sizeof(*args
))) == NULL
)
1691 if (sscanf(raw_args
, "%lf %lf %c", &tx
, &ty
, &dummy
) != 2)
1693 cairo_matrix_init_translate(&args
->matrix
, tx
, ty
);
1694 } else if (eql(action
, "scale")) {
1696 struct transform_args
*args
;
1698 if ((args
= malloc(sizeof(*args
))) == NULL
)
1702 if (sscanf(raw_args
, "%lf %lf %c", &sx
, &sy
, &dummy
) != 2)
1704 cairo_matrix_init_scale(&args
->matrix
, sx
, sy
);
1705 } else if (eql(action
, "rotate")) {
1707 struct transform_args
*args
;
1709 if ((args
= malloc(sizeof(*args
))) == NULL
)
1713 if (sscanf(raw_args
, "%lf %c", &angle
, &dummy
) != 1)
1715 cairo_matrix_init_rotate(&args
->matrix
, angle
* (M_PI
/ 180.L
));
1722 * Add another element to widget's "draw_ops" list
1724 static enum draw_op_stat
1725 ins_draw_op(GObject
*widget
, const char *action
, const char *data
)
1727 enum draw_op_stat result
;
1728 struct draw_op
*new_op
= NULL
, *draw_ops
= NULL
, *prev_op
= NULL
;
1730 if ((new_op
= malloc(sizeof(*new_op
))) == NULL
)
1732 new_op
->op_args
= NULL
;
1733 new_op
->next
= NULL
;
1734 if ((result
= set_draw_op(new_op
, action
, data
)) == FAILURE
) {
1735 free(new_op
->op_args
);
1739 switch (new_op
->policy
) {
1741 if ((draw_ops
= g_object_get_data(widget
, "draw_ops")) == NULL
)
1742 g_object_set_data(widget
, "draw_ops", new_op
);
1744 for (prev_op
= draw_ops
;
1745 prev_op
->next
!= NULL
;
1746 prev_op
= prev_op
->next
);
1747 prev_op
->next
= new_op
;
1751 for (prev_op
= NULL
, draw_ops
= g_object_get_data(widget
, "draw_ops");
1752 draw_ops
!= NULL
&& draw_ops
->id
!= new_op
->before
;
1753 prev_op
= draw_ops
, draw_ops
= draw_ops
->next
);
1754 if (prev_op
== NULL
) { /* prepend a new first element */
1755 g_object_set_data(widget
, "draw_ops", new_op
);
1756 new_op
->next
= draw_ops
;
1757 } else if (draw_ops
== NULL
) /* append */
1758 prev_op
->next
= new_op
;
1760 new_op
->next
= draw_ops
;
1761 prev_op
->next
= new_op
;
1765 for (prev_op
= NULL
, draw_ops
= g_object_get_data(widget
, "draw_ops");
1766 draw_ops
!= NULL
&& draw_ops
->id
!= new_op
->id
;
1767 prev_op
= draw_ops
, draw_ops
= draw_ops
->next
);
1768 if (draw_ops
== NULL
&& prev_op
== NULL
) /* start a new list */
1769 g_object_set_data(widget
, "draw_ops", new_op
);
1770 else if (prev_op
== NULL
) { /* replace the first element */
1771 g_object_set_data(widget
, "draw_ops", new_op
);
1772 new_op
->next
= draw_ops
->next
;
1773 free(draw_ops
->op_args
);
1775 } else if (draw_ops
== NULL
) /* append */
1776 prev_op
->next
= new_op
;
1777 else { /* replace some other element */
1778 new_op
->next
= draw_ops
->next
;
1779 prev_op
->next
= new_op
;
1780 free(draw_ops
->op_args
);
1792 * Remove all elements with the given id from widget's "draw_ops" list
1794 static enum draw_op_stat
1795 rem_draw_op(GObject
*widget
, const char *data
)
1798 struct draw_op
*op
, *next_op
, *prev_op
= NULL
;
1799 unsigned long long int id
;
1801 if (sscanf(data
, "%llu %c", &id
, &dummy
) != 1)
1803 op
= g_object_get_data(widget
, "draw_ops");
1804 while (op
!= NULL
) {
1807 if (prev_op
== NULL
) /* list head */
1808 g_object_set_data(widget
, "draw_ops", op
->next
);
1810 prev_op
->next
= op
->next
;
1821 refresh_widget(GtkWidget
*widget
)
1823 gint height
= gtk_widget_get_allocated_height(widget
);
1824 gint width
= gtk_widget_get_allocated_width(widget
);
1826 gtk_widget_queue_draw_area(widget
, 0, 0, width
, height
);
1827 return G_SOURCE_REMOVE
;
1831 update_drawing_area(struct ui_data
*ud
)
1833 enum draw_op_stat dost
;
1835 if (eql(ud
->action
, "remove"))
1836 dost
= rem_draw_op(ud
->obj
, ud
->data
);
1838 dost
= ins_draw_op(ud
->obj
, ud
->action
, ud
->data
);
1841 gdk_threads_add_idle_full(G_PRIORITY_LOW
,
1842 (GSourceFunc
) refresh_widget
,
1843 GTK_WIDGET(ud
->obj
), NULL
);
1846 ign_cmd(ud
->type
, ud
->cmd
);
1857 update_entry(struct ui_data
*ud
)
1859 GtkEntry
*entry
= GTK_ENTRY(ud
->obj
);
1861 if (eql(ud
->action
, "set_text"))
1862 gtk_entry_set_text(entry
, ud
->data
);
1863 else if (eql(ud
->action
, "set_placeholder_text"))
1864 gtk_entry_set_placeholder_text(entry
, ud
->data
);
1866 ign_cmd(ud
->type
, ud
->cmd
);
1870 update_expander(struct ui_data
*ud
)
1872 GtkExpander
*expander
= GTK_EXPANDER(ud
->obj
);
1876 if (eql(ud
->action
, "set_expanded") &&
1877 sscanf(ud
->data
, "%u %c", &val
, &dummy
) == 1 && val
< 2)
1878 gtk_expander_set_expanded(expander
, val
);
1879 else if (eql(ud
->action
, "set_label"))
1880 gtk_expander_set_label(expander
, ud
->data
);
1882 ign_cmd(ud
->type
, ud
->cmd
);
1886 update_file_chooser_button(struct ui_data
*ud
)
1888 if (eql(ud
->action
, "set_filename"))
1889 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(ud
->obj
), ud
->data
);
1891 ign_cmd(ud
->type
, ud
->cmd
);
1895 update_file_chooser_dialog(struct ui_data
*ud
)
1897 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(ud
->obj
);
1899 if (eql(ud
->action
, "set_filename"))
1900 gtk_file_chooser_set_filename(chooser
, ud
->data
);
1901 else if (eql(ud
->action
, "set_current_name"))
1902 gtk_file_chooser_set_current_name(chooser
, ud
->data
);
1903 else if (update_class_window(ud
));
1905 ign_cmd(ud
->type
, ud
->cmd
);
1909 update_focus(struct ui_data
*ud
){
1912 if (GTK_IS_WIDGET(ud
->obj
) &&
1913 sscanf(ud
->data
, " %c", &dummy
) < 1 &&
1914 gtk_widget_get_can_focus(GTK_WIDGET(ud
->obj
)))
1915 gtk_widget_grab_focus(GTK_WIDGET(ud
->obj
));
1917 ign_cmd(ud
->type
, ud
->cmd
);
1921 update_font_button(struct ui_data
*ud
){
1922 GtkFontButton
*font_button
= GTK_FONT_BUTTON(ud
->obj
);
1924 if (eql(ud
->action
, "set_font_name"))
1925 gtk_font_button_set_font_name(font_button
, ud
->data
);
1927 ign_cmd(ud
->type
, ud
->cmd
);
1931 update_frame(struct ui_data
*ud
)
1933 if (eql(ud
->action
, "set_label"))
1934 gtk_frame_set_label(GTK_FRAME(ud
->obj
), ud
->data
);
1936 ign_cmd(ud
->type
, ud
->cmd
);
1940 update_image(struct ui_data
*ud
)
1943 GtkImage
*image
= GTK_IMAGE(ud
->obj
);
1945 gtk_image_get_icon_name(image
, NULL
, &size
);
1946 if (eql(ud
->action
, "set_from_file"))
1947 gtk_image_set_from_file(image
, ud
->data
);
1948 else if (eql(ud
->action
, "set_from_icon_name"))
1949 gtk_image_set_from_icon_name(image
, ud
->data
, size
);
1951 ign_cmd(ud
->type
, ud
->cmd
);
1955 update_label(struct ui_data
*ud
)
1957 if (eql(ud
->action
, "set_text"))
1958 gtk_label_set_text(GTK_LABEL(ud
->obj
), ud
->data
);
1960 ign_cmd(ud
->type
, ud
->cmd
);
1964 unsigned int id
; /* returned by g_signal_connect() and friends */
1965 bool blocked
; /* we avoid multiple blocking/unblocking */
1966 struct handler_id
*next
;
1970 update_blocked(struct ui_data
*ud
)
1973 struct handler_id
*hid
;
1974 unsigned long int val
;
1976 if (sscanf(ud
->data
, "%lu %c", &val
, &dummy
) == 1 && val
< 2) {
1977 for (hid
= g_object_get_data(ud
->obj
, "signal-id");
1978 hid
!= NULL
; hid
= hid
->next
) {
1979 if (val
== 0 && hid
->blocked
== true) {
1980 g_signal_handler_unblock(ud
->obj
, hid
->id
);
1981 hid
->blocked
= false;
1982 } else if (val
== 1 && hid
->blocked
== false) {
1983 g_signal_handler_block(ud
->obj
, hid
->id
);
1984 hid
->blocked
= true;
1988 ign_cmd(ud
->type
, ud
->cmd
);
1992 update_notebook(struct ui_data
*ud
)
1995 int val
, n_pages
= gtk_notebook_get_n_pages(GTK_NOTEBOOK(ud
->obj
));
1997 if (eql(ud
->action
, "set_current_page") &&
1998 sscanf(ud
->data
, "%d %c", &val
, &dummy
) == 1 &&
1999 val
>= 0 && val
< n_pages
)
2000 gtk_notebook_set_current_page(GTK_NOTEBOOK(ud
->obj
), val
);
2002 ign_cmd(ud
->type
, ud
->cmd
);
2006 update_nothing(struct ui_data
*ud
)
2012 update_print_dialog(struct ui_data
*ud
)
2014 GtkPageSetup
*page_setup
;
2016 GtkPrintSettings
*settings
;
2017 GtkPrintUnixDialog
*dialog
= GTK_PRINT_UNIX_DIALOG(ud
->obj
);
2018 GtkPrinter
*printer
;
2021 if (eql(ud
->action
, "print")) {
2022 response_id
= gtk_dialog_run(GTK_DIALOG(dialog
));
2023 switch (response_id
) {
2024 case GTK_RESPONSE_OK
:
2025 printer
= gtk_print_unix_dialog_get_selected_printer(dialog
);
2026 settings
= gtk_print_unix_dialog_get_settings(dialog
);
2027 page_setup
= gtk_print_unix_dialog_get_page_setup(dialog
);
2028 job
= gtk_print_job_new(ud
->data
, printer
, settings
, page_setup
);
2029 if (gtk_print_job_set_source_file(job
, ud
->data
, NULL
))
2030 gtk_print_job_send(job
, NULL
, NULL
, NULL
);
2032 ign_cmd(ud
->type
, ud
->cmd
);
2033 g_clear_object(&settings
);
2034 g_clear_object(&job
);
2036 case GTK_RESPONSE_CANCEL
:
2037 case GTK_RESPONSE_DELETE_EVENT
:
2040 fprintf(stderr
, "%s sent an unexpected response id (%d)\n",
2041 widget_id(GTK_BUILDABLE(dialog
)), response_id
);
2044 gtk_widget_hide(GTK_WIDGET(dialog
));
2046 ign_cmd(ud
->type
, ud
->cmd
);
2050 update_progress_bar(struct ui_data
*ud
)
2052 GtkProgressBar
*progressbar
= GTK_PROGRESS_BAR(ud
->obj
);
2056 if (eql(ud
->action
, "set_text"))
2057 gtk_progress_bar_set_text(progressbar
, *(ud
->data
) == '\0' ? NULL
: ud
->data
);
2058 else if (eql(ud
->action
, "set_fraction") &&
2059 sscanf(ud
->data
, "%lf %c", &frac
, &dummy
) == 1)
2060 gtk_progress_bar_set_fraction(progressbar
, frac
);
2062 ign_cmd(ud
->type
, ud
->cmd
);
2066 update_scale(struct ui_data
*ud
)
2068 GtkRange
*range
= GTK_RANGE(ud
->obj
);
2072 if (eql(ud
->action
, "set_value") && sscanf(ud
->data
, "%lf %c", &val1
, &dummy
) == 1)
2073 gtk_range_set_value(range
, val1
);
2074 else if (eql(ud
->action
, "set_fill_level") &&
2075 sscanf(ud
->data
, "%lf %c", &val1
, &dummy
) == 1) {
2076 gtk_range_set_fill_level(range
, val1
);
2077 gtk_range_set_show_fill_level(range
, TRUE
);
2078 } else if (eql(ud
->action
, "set_fill_level") &&
2079 sscanf(ud
->data
, " %c", &dummy
) < 1)
2080 gtk_range_set_show_fill_level(range
, FALSE
);
2081 else if (eql(ud
->action
, "set_range") &&
2082 sscanf(ud
->data
, "%lf %lf %c", &val1
, &val2
, &dummy
) == 2)
2083 gtk_range_set_range(range
, val1
, val2
);
2084 else if (eql(ud
->action
, "set_increments") &&
2085 sscanf(ud
->data
, "%lf %lf %c", &val1
, &val2
, &dummy
) == 2)
2086 gtk_range_set_increments(range
, val1
, val2
);
2088 ign_cmd(ud
->type
, ud
->cmd
);
2092 update_scrolled_window(struct ui_data
*ud
)
2094 GtkScrolledWindow
*window
= GTK_SCROLLED_WINDOW(ud
->obj
);
2095 GtkAdjustment
*hadj
= gtk_scrolled_window_get_hadjustment(window
);
2096 GtkAdjustment
*vadj
= gtk_scrolled_window_get_vadjustment(window
);
2100 if (eql(ud
->action
, "hscroll") && sscanf(ud
->data
, "%lf %c", &d0
, &dummy
) == 1)
2101 gtk_adjustment_set_value(hadj
, d0
);
2102 else if (eql(ud
->action
, "vscroll") && sscanf(ud
->data
, "%lf %c", &d0
, &dummy
) == 1)
2103 gtk_adjustment_set_value(vadj
, d0
);
2104 else if (eql(ud
->action
, "hscroll_to_range") &&
2105 sscanf(ud
->data
, "%lf %lf %c", &d0
, &d1
, &dummy
) == 2)
2106 gtk_adjustment_clamp_page(hadj
, d0
, d1
);
2107 else if (eql(ud
->action
, "vscroll_to_range") &&
2108 sscanf(ud
->data
, "%lf %lf %c", &d0
, &d1
, &dummy
) == 2)
2109 gtk_adjustment_clamp_page(vadj
, d0
, d1
);
2111 ign_cmd(ud
->type
, ud
->cmd
);
2115 update_sensitivity(struct ui_data
*ud
)
2120 if (GTK_IS_WIDGET(ud
->obj
) &&
2121 sscanf(ud
->data
, "%u %c", &val
, &dummy
) == 1 && val
< 2)
2122 gtk_widget_set_sensitive(GTK_WIDGET(ud
->obj
), val
);
2124 ign_cmd(ud
->type
, ud
->cmd
);
2128 update_size_request(struct ui_data
*ud
)
2133 if (GTK_IS_WIDGET(ud
->obj
) &&
2134 sscanf(ud
->data
, "%d %d %c", &x
, &y
, &dummy
) == 2)
2135 gtk_widget_set_size_request(GTK_WIDGET(ud
->obj
), x
, y
);
2136 else if (GTK_IS_WIDGET(ud
->obj
) &&
2137 sscanf(ud
->data
, " %c", &dummy
) < 1)
2138 gtk_widget_set_size_request(GTK_WIDGET(ud
->obj
), -1, -1);
2140 ign_cmd(ud
->type
, ud
->cmd
);
2144 update_socket(struct ui_data
*ud
)
2146 GtkSocket
*socket
= GTK_SOCKET(ud
->obj
);
2148 char str
[BUFLEN
], dummy
;
2150 if (eql(ud
->action
, "id") && sscanf(ud
->data
, " %c", &dummy
) < 1) {
2151 id
= gtk_socket_get_id(socket
);
2152 snprintf(str
, BUFLEN
, "%lu", id
);
2153 send_msg(ud
->args
->fout
, GTK_BUILDABLE(socket
), "id", str
, NULL
);
2155 ign_cmd(ud
->type
, ud
->cmd
);
2159 update_spin_button(struct ui_data
*ud
)
2161 GtkSpinButton
*spinbutton
= GTK_SPIN_BUTTON(ud
->obj
);
2165 if (eql(ud
->action
, "set_text") && /* TODO: rename to "set_value" */
2166 sscanf(ud
->data
, "%lf %c", &val1
, &dummy
) == 1)
2167 gtk_spin_button_set_value(spinbutton
, val1
);
2168 else if (eql(ud
->action
, "set_range") &&
2169 sscanf(ud
->data
, "%lf %lf %c", &val1
, &val2
, &dummy
) == 2)
2170 gtk_spin_button_set_range(spinbutton
, val1
, val2
);
2171 else if (eql(ud
->action
, "set_increments") &&
2172 sscanf(ud
->data
, "%lf %lf %c", &val1
, &val2
, &dummy
) == 2)
2173 gtk_spin_button_set_increments(spinbutton
, val1
, val2
);
2175 ign_cmd(ud
->type
, ud
->cmd
);
2179 update_spinner(struct ui_data
*ud
)
2181 GtkSpinner
*spinner
= GTK_SPINNER(ud
->obj
);
2184 if (eql(ud
->action
, "start") && sscanf(ud
->data
, " %c", &dummy
) < 1)
2185 gtk_spinner_start(spinner
);
2186 else if (eql(ud
->action
, "stop") && sscanf(ud
->data
, " %c", &dummy
) < 1)
2187 gtk_spinner_stop(spinner
);
2189 ign_cmd(ud
->type
, ud
->cmd
);
2193 update_statusbar(struct ui_data
*ud
)
2195 GtkStatusbar
*statusbar
= GTK_STATUSBAR(ud
->obj
);
2196 char *ctx_msg
, dummy
;
2197 const char *status_msg
;
2200 /* TODO: remove "push", "pop", "remove_all"; rename "push_id" to "push", etc. */
2201 if ((ctx_msg
= malloc(strlen(ud
->data
) + 1)) == NULL
)
2203 t
= sscanf(ud
->data
, "%s %n%c", ctx_msg
, &ctx_len
, &dummy
);
2204 status_msg
= ud
->data
+ ctx_len
;
2205 if (eql(ud
->action
, "push"))
2206 gtk_statusbar_push(statusbar
,
2207 gtk_statusbar_get_context_id(statusbar
, "0"),
2209 else if (eql(ud
->action
, "push_id") && t
>= 1)
2210 gtk_statusbar_push(statusbar
,
2211 gtk_statusbar_get_context_id(statusbar
, ctx_msg
),
2213 else if (eql(ud
->action
, "pop") && t
< 1)
2214 gtk_statusbar_pop(statusbar
,
2215 gtk_statusbar_get_context_id(statusbar
, "0"));
2216 else if (eql(ud
->action
, "pop_id") && t
== 1)
2217 gtk_statusbar_pop(statusbar
,
2218 gtk_statusbar_get_context_id(statusbar
, ctx_msg
));
2219 else if (eql(ud
->action
, "remove_all") && t
< 1)
2220 gtk_statusbar_remove_all(statusbar
,
2221 gtk_statusbar_get_context_id(statusbar
, "0"));
2222 else if (eql(ud
->action
, "remove_all_id") && t
== 1)
2223 gtk_statusbar_remove_all(statusbar
,
2224 gtk_statusbar_get_context_id(statusbar
, ctx_msg
));
2226 ign_cmd(ud
->type
, ud
->cmd
);
2231 update_switch(struct ui_data
*ud
)
2236 if (eql(ud
->action
, "set_active") &&
2237 sscanf(ud
->data
, "%u %c", &val
, &dummy
) == 1 && val
< 2)
2238 gtk_switch_set_active(GTK_SWITCH(ud
->obj
), val
);
2240 ign_cmd(ud
->type
, ud
->cmd
);
2244 update_text_view(struct ui_data
*ud
)
2247 GtkTextView
*view
= GTK_TEXT_VIEW(ud
->obj
);
2248 GtkTextBuffer
*textbuf
= gtk_text_view_get_buffer(view
);
2253 if (eql(ud
->action
, "set_text"))
2254 gtk_text_buffer_set_text(textbuf
, ud
->data
, -1);
2255 else if (eql(ud
->action
, "delete") && sscanf(ud
->data
, " %c", &dummy
) < 1) {
2256 gtk_text_buffer_get_bounds(textbuf
, &a
, &b
);
2257 gtk_text_buffer_delete(textbuf
, &a
, &b
);
2258 } else if (eql(ud
->action
, "insert_at_cursor"))
2259 gtk_text_buffer_insert_at_cursor(textbuf
, ud
->data
, -1);
2260 else if (eql(ud
->action
, "place_cursor") && eql(ud
->data
, "end")) {
2261 gtk_text_buffer_get_end_iter(textbuf
, &a
);
2262 gtk_text_buffer_place_cursor(textbuf
, &a
);
2263 } else if (eql(ud
->action
, "place_cursor") &&
2264 sscanf(ud
->data
, "%d %c", &val
, &dummy
) == 1) {
2265 gtk_text_buffer_get_iter_at_offset(textbuf
, &a
, val
);
2266 gtk_text_buffer_place_cursor(textbuf
, &a
);
2267 } else if (eql(ud
->action
, "place_cursor_at_line") &&
2268 sscanf(ud
->data
, "%d %c", &val
, &dummy
) == 1) {
2269 gtk_text_buffer_get_iter_at_line(textbuf
, &a
, val
);
2270 gtk_text_buffer_place_cursor(textbuf
, &a
);
2271 } else if (eql(ud
->action
, "scroll_to_cursor") &&
2272 sscanf(ud
->data
, " %c", &dummy
) < 1)
2273 gtk_text_view_scroll_to_mark(view
, gtk_text_buffer_get_insert(textbuf
),
2275 else if (eql(ud
->action
, "save") && ud
->data
!= NULL
&&
2276 (sv
= fopen(ud
->data
, "w")) != NULL
) {
2277 gtk_text_buffer_get_bounds(textbuf
, &a
, &b
);
2278 send_msg(sv
, GTK_BUILDABLE(view
), "insert_at_cursor",
2279 gtk_text_buffer_get_text(textbuf
, &a
, &b
, TRUE
), NULL
);
2282 ign_cmd(ud
->type
, ud
->cmd
);
2286 update_toggle_button(struct ui_data
*ud
)
2291 if (eql(ud
->action
, "set_label"))
2292 gtk_button_set_label(GTK_BUTTON(ud
->obj
), ud
->data
);
2293 else if (eql(ud
->action
, "set_active") &&
2294 sscanf(ud
->data
, "%u %c", &val
, &dummy
) == 1 && val
< 2)
2295 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ud
->obj
), val
);
2297 ign_cmd(ud
->type
, ud
->cmd
);
2301 update_tooltip_text(struct ui_data
*ud
)
2303 if (GTK_IS_WIDGET(ud
->obj
))
2304 gtk_widget_set_tooltip_text(GTK_WIDGET(ud
->obj
), ud
->data
);
2306 ign_cmd(ud
->type
, ud
->cmd
);
2310 * update_tree_view(), which runs inside gtk_main(), needs a few
2315 * Check if s is a valid string representation of a GtkTreePath
2318 is_path_string(char *s
)
2321 strlen(s
) == strspn(s
, ":0123456789") &&
2322 strstr(s
, "::") == NULL
&&
2323 strcspn(s
, ":") > 0;
2327 tree_model_insert_before(GtkTreeModel
*model
, GtkTreeIter
*iter
,
2328 GtkTreeIter
*parent
, GtkTreeIter
*sibling
)
2330 if (GTK_IS_TREE_STORE(model
))
2331 gtk_tree_store_insert_before(GTK_TREE_STORE(model
),
2332 iter
, parent
, sibling
);
2333 else if (GTK_IS_LIST_STORE(model
))
2334 gtk_list_store_insert_before(GTK_LIST_STORE(model
),
2341 tree_model_insert_after(GtkTreeModel
*model
, GtkTreeIter
*iter
,
2342 GtkTreeIter
*parent
, GtkTreeIter
*sibling
)
2344 if (GTK_IS_TREE_STORE(model
))
2345 gtk_tree_store_insert_after(GTK_TREE_STORE(model
),
2346 iter
, parent
, sibling
);
2347 else if (GTK_IS_LIST_STORE(model
))
2348 gtk_list_store_insert_after(GTK_LIST_STORE(model
),
2355 tree_model_move_before(GtkTreeModel
*model
, GtkTreeIter
*iter
,
2356 GtkTreeIter
*position
)
2358 if (GTK_IS_TREE_STORE(model
))
2359 gtk_tree_store_move_before(GTK_TREE_STORE(model
), iter
, position
);
2360 else if (GTK_IS_LIST_STORE(model
))
2361 gtk_list_store_move_before(GTK_LIST_STORE(model
), iter
, position
);
2367 tree_model_remove(GtkTreeModel
*model
, GtkTreeIter
*iter
)
2369 if (GTK_IS_TREE_STORE(model
))
2370 gtk_tree_store_remove(GTK_TREE_STORE(model
), iter
);
2371 else if (GTK_IS_LIST_STORE(model
))
2372 gtk_list_store_remove(GTK_LIST_STORE(model
), iter
);
2378 tree_model_clear(GtkTreeModel
*model
)
2380 if (GTK_IS_TREE_STORE(model
))
2381 gtk_tree_store_clear(GTK_TREE_STORE(model
));
2382 else if (GTK_IS_LIST_STORE(model
))
2383 gtk_list_store_clear(GTK_LIST_STORE(model
));
2389 tree_model_set(GtkTreeModel
*model
, GtkTreeIter
*iter
, ...)
2394 if (GTK_IS_TREE_STORE(model
))
2395 gtk_tree_store_set_valist(GTK_TREE_STORE(model
), iter
, ap
);
2396 else if (GTK_IS_LIST_STORE(model
))
2397 gtk_list_store_set_valist(GTK_LIST_STORE(model
), iter
, ap
);
2404 * Create an empty row at path if it doesn't yet exist. Create older
2405 * siblings and parents as necessary.
2408 create_subtree(GtkTreeModel
*model
, GtkTreePath
*path
, GtkTreeIter
*iter
)
2410 GtkTreeIter iter_1
; /* iter's predecessor */
2411 GtkTreePath
*path_1
; /* path's predecessor */
2413 if (gtk_tree_path_get_depth(path
) > 0 &&
2414 gtk_tree_model_get_iter(model
, iter
, path
))
2416 path_1
= gtk_tree_path_copy(path
);
2417 if (gtk_tree_path_prev(path_1
)) { /* need an older sibling */
2418 create_subtree(model
, path_1
, iter
);
2420 tree_model_insert_after(model
, iter
, NULL
, &iter_1
);
2421 } else if (gtk_tree_path_up(path_1
)) { /* need a parent */
2422 create_subtree(model
, path_1
, iter
);
2423 if (gtk_tree_path_get_depth(path_1
) == 0)
2424 /* first toplevel row */
2425 tree_model_insert_after(model
, iter
, NULL
, NULL
);
2426 else { /* first row in a lower level */
2428 tree_model_insert_after(model
, iter
, &iter_1
, NULL
);
2430 } /* neither prev nor up mean we're at the root of an empty tree */
2431 gtk_tree_path_free(path_1
);
2435 set_tree_view_cell(GtkTreeModel
*model
, GtkTreeIter
*iter
,
2436 const char *path_s
, int col
, const char *new_text
)
2438 GType col_type
= gtk_tree_model_get_column_type(model
, col
);
2445 path
= gtk_tree_path_new_from_string(path_s
);
2447 case G_TYPE_BOOLEAN
:
2454 if (new_text
!= NULL
&&
2455 sscanf(new_text
, "%lld %c", &n
, &dummy
) == 1) {
2456 create_subtree(model
, path
, iter
);
2457 tree_model_set(model
, iter
, col
, n
, -1);
2463 if (new_text
!= NULL
&&
2464 sscanf(new_text
, "%lf %c", &d
, &dummy
) == 1) {
2465 create_subtree(model
, path
, iter
);
2466 tree_model_set(model
, iter
, col
, d
, -1);
2471 create_subtree(model
, path
, iter
);
2472 tree_model_set(model
, iter
, col
, new_text
, -1);
2476 fprintf(stderr
, "column %d: %s not implemented\n",
2477 col
, g_type_name(col_type
));
2481 gtk_tree_path_free(path
);
2486 tree_view_set_cursor(GtkTreeView
*view
, GtkTreePath
*path
, GtkTreeViewColumn
*col
)
2488 /* GTK+ 3.14 requires this. For 3.18, path = NULL */
2489 /* is just fine and this function need not exist. */
2491 path
= gtk_tree_path_new();
2492 gtk_tree_view_set_cursor(view
, path
, col
, false);
2496 update_tree_view(struct ui_data
*ud
)
2498 GtkTreeView
*view
= GTK_TREE_VIEW(ud
->obj
);
2499 GtkTreeIter iter0
, iter1
;
2500 GtkTreeModel
*model
= gtk_tree_view_get_model(view
);
2501 GtkTreePath
*path
= NULL
;
2502 bool iter0_valid
, iter1_valid
;
2503 char *tokens
, *arg0
, *arg1
, *arg2
;
2504 int col
= -1; /* invalid column number */
2507 if (!GTK_IS_LIST_STORE(model
) && !GTK_IS_TREE_STORE(model
))
2509 fprintf(stderr
, "missing model/");
2510 ign_cmd(ud
->type
, ud
->cmd
);
2513 if ((tokens
= malloc(strlen(ud
->data
) + 1)) == NULL
)
2515 strcpy(tokens
, ud
->data
);
2516 arg0
= strtok(tokens
, WHITESPACE
);
2517 arg1
= strtok(NULL
, WHITESPACE
);
2518 arg2
= strtok(NULL
, "");
2519 iter0_valid
= is_path_string(arg0
) &&
2520 gtk_tree_model_get_iter_from_string(model
, &iter0
, arg0
);
2521 iter1_valid
= is_path_string(arg1
) &&
2522 gtk_tree_model_get_iter_from_string(model
, &iter1
, arg1
);
2523 if (is_path_string(arg1
))
2524 col
= strtol(arg1
, NULL
, 10);
2525 if (eql(ud
->action
, "set") &&
2527 col
< gtk_tree_model_get_n_columns(model
) &&
2528 is_path_string(arg0
)) {
2529 if (set_tree_view_cell(model
, &iter0
, arg0
, col
, arg2
) == false)
2530 ign_cmd(ud
->type
, ud
->cmd
);
2531 } else if (eql(ud
->action
, "scroll") && iter0_valid
&& iter1_valid
&&
2533 path
= gtk_tree_path_new_from_string(arg0
);
2534 gtk_tree_view_scroll_to_cell (view
,
2536 gtk_tree_view_get_column(view
, col
),
2538 } else if (eql(ud
->action
, "expand") && iter0_valid
&& arg1
== NULL
) {
2539 path
= gtk_tree_path_new_from_string(arg0
);
2540 gtk_tree_view_expand_row(view
, path
, false);
2541 } else if (eql(ud
->action
, "expand_all") && iter0_valid
&& arg1
== NULL
) {
2542 path
= gtk_tree_path_new_from_string(arg0
);
2543 gtk_tree_view_expand_row(view
, path
, true);
2544 } else if (eql(ud
->action
, "expand_all") && arg0
== NULL
)
2545 gtk_tree_view_expand_all(view
);
2546 else if (eql(ud
->action
, "collapse") && iter0_valid
&& arg1
== NULL
) {
2547 path
= gtk_tree_path_new_from_string(arg0
);
2548 gtk_tree_view_collapse_row(view
, path
);
2549 } else if (eql(ud
->action
, "collapse") && arg0
== NULL
)
2550 gtk_tree_view_collapse_all(view
);
2551 else if (eql(ud
->action
, "set_cursor") && iter0_valid
&& arg1
== NULL
) {
2552 path
= gtk_tree_path_new_from_string(arg0
);
2553 tree_view_set_cursor(view
, path
, NULL
);
2554 } else if (eql(ud
->action
, "set_cursor") && arg0
== NULL
) {
2555 tree_view_set_cursor(view
, NULL
, NULL
);
2556 gtk_tree_selection_unselect_all(gtk_tree_view_get_selection(view
));
2557 } else if (eql(ud
->action
, "insert_row") &&
2558 eql(arg0
, "end") && arg1
== NULL
)
2559 tree_model_insert_before(model
, &iter1
, NULL
, NULL
);
2560 else if (eql(ud
->action
, "insert_row") && iter0_valid
&&
2561 eql(arg1
, "as_child") && arg2
== NULL
)
2562 tree_model_insert_after(model
, &iter1
, &iter0
, NULL
);
2563 else if (eql(ud
->action
, "insert_row") && iter0_valid
&& arg1
== NULL
)
2564 tree_model_insert_before(model
, &iter1
, NULL
, &iter0
);
2565 else if (eql(ud
->action
, "move_row") && iter0_valid
&&
2566 eql(arg1
, "end") && arg2
== NULL
)
2567 tree_model_move_before(model
, &iter0
, NULL
);
2568 else if (eql(ud
->action
, "move_row") && iter0_valid
&& iter1_valid
&& arg2
== NULL
)
2569 tree_model_move_before(model
, &iter0
, &iter1
);
2570 else if (eql(ud
->action
, "remove_row") && iter0_valid
&& arg1
== NULL
)
2571 tree_model_remove(model
, &iter0
);
2572 else if (eql(ud
->action
, "clear") && arg0
== NULL
) {
2573 tree_view_set_cursor(view
, NULL
, NULL
);
2574 gtk_tree_selection_unselect_all(gtk_tree_view_get_selection(view
));
2575 tree_model_clear(model
);
2576 } else if (eql(ud
->action
, "save") && arg0
!= NULL
&&
2577 (ar
.fout
= fopen(arg0
, "w")) != NULL
) {
2579 gtk_tree_model_foreach(model
,
2580 (GtkTreeModelForeachFunc
) save_tree_row_msg
,
2584 ign_cmd(ud
->type
, ud
->cmd
);
2586 gtk_tree_path_free(path
);
2590 update_visibility(struct ui_data
*ud
)
2595 if (GTK_IS_WIDGET(ud
->obj
) &&
2596 sscanf(ud
->data
, "%u %c", &val
, &dummy
) == 1 && val
< 2)
2597 gtk_widget_set_visible(GTK_WIDGET(ud
->obj
), val
);
2599 ign_cmd(ud
->type
, ud
->cmd
);
2603 * Change the style of the widget passed. Runs inside gtk_main().
2606 update_widget_style(struct ui_data
*ud
)
2608 GtkStyleContext
*context
;
2609 GtkStyleProvider
*style_provider
;
2611 const char *prefix
= "* {", *suffix
= "}";
2614 if (!GTK_IS_WIDGET(ud
->obj
)) {
2615 ign_cmd(ud
->type
, ud
->cmd
);
2618 style_provider
= g_object_get_data(ud
->obj
, "style_provider");
2619 sz
= strlen(prefix
) + strlen(suffix
) + strlen(ud
->data
) + 1;
2620 context
= gtk_widget_get_style_context(GTK_WIDGET(ud
->obj
));
2621 gtk_style_context_remove_provider(context
, style_provider
);
2622 if ((style_decl
= malloc(sz
)) == NULL
)
2624 strcpy(style_decl
, prefix
);
2625 strcat(style_decl
, ud
->data
);
2626 strcat(style_decl
, suffix
);
2627 gtk_style_context_add_provider(context
, style_provider
,
2628 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
);
2629 gtk_css_provider_load_from_data(GTK_CSS_PROVIDER(style_provider
),
2630 style_decl
, -1, NULL
);
2635 update_window(struct ui_data
*ud
)
2637 if (!update_class_window(ud
))
2638 ign_cmd(ud
->type
, ud
->cmd
);
2642 * Have the widget say "ping". Runs inside gtk_main().
2645 ping(struct ui_data
*ud
)
2649 if (!GTK_IS_WIDGET(ud
->obj
) || sscanf(ud
->data
, " %c", &dummy
) > 0)
2650 ign_cmd(ud
->type
, ud
->cmd
);
2651 ud
->args
->txt
= "ping";
2652 cb_simple(GTK_BUILDABLE(ud
->obj
), ud
->args
);
2656 * Simulate user activity on various widgets. Runs inside gtk_main().
2659 fake_ui_activity(struct ui_data
*ud
)
2663 if (!GTK_IS_WIDGET(ud
->obj
) || sscanf(ud
->data
, " %c", &dummy
) > 0)
2664 ign_cmd(ud
->type
, ud
->cmd
);
2665 else if (GTK_IS_SPIN_BUTTON(ud
->obj
)) {
2666 ud
->args
->txt
= "text";
2667 cb_spin_button(GTK_BUILDABLE(ud
->obj
), ud
->args
); /* TODO: rename to "value" */
2668 } else if (GTK_IS_SCALE(ud
->obj
)) {
2669 ud
->args
->txt
= "value";
2670 cb_range(GTK_BUILDABLE(ud
->obj
), ud
->args
);
2671 } else if (GTK_IS_ENTRY(ud
->obj
)) {
2672 ud
->args
->txt
= "text";
2673 cb_editable(GTK_BUILDABLE(ud
->obj
), ud
->args
);
2674 } else if (GTK_IS_CALENDAR(ud
->obj
)) {
2675 ud
->args
->txt
= "clicked";
2676 cb_calendar(GTK_BUILDABLE(ud
->obj
), ud
->args
);
2677 } else if (GTK_IS_FILE_CHOOSER_BUTTON(ud
->obj
)) {
2678 ud
->args
->txt
= "file";
2679 cb_file_chooser_button(GTK_BUILDABLE(ud
->obj
), ud
->args
);
2680 } else if (!gtk_widget_activate(GTK_WIDGET(ud
->obj
)))
2681 ign_cmd(ud
->type
, ud
->cmd
);
2685 * The final UI update. Runs inside gtk_main().
2688 main_quit(struct ui_data
*ud
)
2692 if (sscanf(ud
->data
, " %c", &dummy
) < 1)
2695 ign_cmd(ud
->type
, ud
->cmd
);
2699 * Write snapshot of widget in an appropriate format to file
2702 take_snapshot(struct ui_data
*ud
)
2704 cairo_surface_t
*sur
= NULL
;
2709 if (!GTK_IS_WIDGET(ud
->obj
)) {
2710 ign_cmd(ud
->type
, ud
->cmd
);
2713 height
= gtk_widget_get_allocated_height(GTK_WIDGET(ud
->obj
));
2714 width
= gtk_widget_get_allocated_width(GTK_WIDGET(ud
->obj
));
2715 if (has_suffix(ud
->data
, ".epsf") || has_suffix(ud
->data
, ".eps")) {
2716 sur
= cairo_ps_surface_create(ud
->data
, width
, height
);
2717 cairo_ps_surface_set_eps(sur
, TRUE
);
2718 } else if (has_suffix(ud
->data
, ".pdf"))
2719 sur
= cairo_pdf_surface_create(ud
->data
, width
, height
);
2720 else if (has_suffix(ud
->data
, ".ps"))
2721 sur
= cairo_ps_surface_create(ud
->data
, width
, height
);
2722 else if (has_suffix(ud
->data
, ".svg"))
2723 sur
= cairo_svg_surface_create(ud
->data
, width
, height
);
2725 ign_cmd(ud
->type
, ud
->cmd
);
2726 cr
= cairo_create(sur
);
2727 gtk_widget_draw(GTK_WIDGET(ud
->obj
), cr
);
2729 cairo_surface_destroy(sur
);
2733 * Don't update anything; just complain from inside gtk_main()
2736 complain(struct ui_data
*ud
)
2738 ign_cmd(ud
->type
, ud
->cmd
);
2742 * Parse command pointed to by ud, and act on ui accordingly. Runs
2743 * once per command inside gtk_main().
2746 update_ui(struct ui_data
*ud
)
2748 char *lc
= lc_numeric();
2751 free(ud
->cmd_tokens
);
2754 lc_numeric_free(lc
);
2755 return G_SOURCE_REMOVE
;
2759 * Keep track of loading files to avoid recursive loading of the same
2760 * file. If filename = NULL, forget the most recently remembered file.
2763 remember_loading_file(char *filename
)
2765 static char *filenames
[BUFLEN
];
2766 static size_t latest
= 0;
2769 if (filename
== NULL
) { /* pop */
2775 for (i
= 1; i
<= latest
; i
++)
2776 if (eql(filename
, filenames
[i
]))
2778 if (latest
> BUFLEN
-2)
2780 filenames
[++latest
] = filename
;
2786 * Read lines from stream cmd and perform appropriate actions on the
2787 * GUI. Runs inside receiver thread.
2790 digest_cmd(struct info
*ar
)
2792 static int recursion
= -1; /* > 0 means this is a recursive call */
2796 FILE *cmd
= ar
->fin
;
2797 struct ui_data
*ud
= NULL
;
2798 char first_char
= '\0';
2799 char *id
; /* widget id */
2800 size_t msg_size
= 32;
2801 int id_start
= 0, id_end
= 0;
2802 int action_start
= 0, action_end
= 0;
2807 if ((ud
= malloc(sizeof(*ud
))) == NULL
)
2809 if ((ud
->cmd
= malloc(msg_size
)) == NULL
)
2812 ud
->type
= G_TYPE_INVALID
;
2813 pthread_testcancel();
2815 log_msg(ar
->flog
, NULL
);
2816 data_start
= read_buf(cmd
, &ud
->cmd
, &msg_size
);
2818 log_msg(ar
->flog
, ud
->cmd
);
2819 if ((ud
->cmd_tokens
= malloc(strlen(ud
->cmd
) + 1)) == NULL
)
2821 sscanf(ud
->cmd
, " %c", &first_char
);
2822 if (data_start
== 0 || /* empty line */
2823 first_char
== '#') { /* comment */
2824 ud
->fn
= update_nothing
;
2827 strcpy(ud
->cmd_tokens
, ud
->cmd
);
2828 sscanf(ud
->cmd_tokens
,
2829 " %n%*[0-9a-zA-Z_-]%n:%n%*[0-9a-zA-Z_]%n%*1[ \t]%n",
2830 &id_start
, &id_end
, &action_start
, &action_end
, &data_start
);
2831 ud
->cmd_tokens
[id_end
] = ud
->cmd_tokens
[action_end
] = '\0';
2832 id
= ud
->cmd_tokens
+ id_start
;
2833 ud
->action
= ud
->cmd_tokens
+ action_start
;
2834 ud
->data
= ud
->cmd_tokens
+ data_start
;
2835 if (eql(ud
->action
, "main_quit")) {
2839 if (eql(ud
->action
, "load") && strlen(ud
->data
) > 0 &&
2840 remember_loading_file(ud
->data
)) {
2841 struct info a
= *ar
;
2843 if ((a
.fin
= fopen(ud
->data
, "r")) != NULL
) {
2846 ud
->fn
= update_nothing
;
2849 remember_loading_file(NULL
);
2852 if ((ud
->obj
= (gtk_builder_get_object(ar
->builder
, id
))) == NULL
) {
2856 ud
->type
= G_TYPE_FROM_INSTANCE(ud
->obj
);
2857 if (eql(ud
->action
, "force"))
2858 ud
->fn
= fake_ui_activity
;
2859 /* next line intentionally mangled to exclude it from */
2860 /* auto-generated list of commands */
2861 else if (eql(ud
->action
, /* undocumented */ "ping"))
2863 else if (eql(ud
->action
, "snapshot"))
2864 ud
->fn
= take_snapshot
;
2865 else if (eql(ud
->action
, "block"))
2866 ud
->fn
= update_blocked
;
2867 else if (eql(ud
->action
, "set_sensitive"))
2868 ud
->fn
= update_sensitivity
;
2869 else if (eql(ud
->action
, "set_visible"))
2870 ud
->fn
= update_visibility
;
2871 else if (eql(ud
->action
, "set_size_request"))
2872 ud
->fn
= update_size_request
;
2873 else if (eql(ud
->action
, "set_tooltip_text"))
2874 ud
->fn
= update_tooltip_text
;
2875 else if (eql(ud
->action
, "grab_focus"))
2876 ud
->fn
= update_focus
;
2877 else if (eql(ud
->action
, "style")) {
2879 ud
->fn
= update_widget_style
;
2880 } else if (ud
->type
== GTK_TYPE_DRAWING_AREA
)
2881 ud
->fn
= update_drawing_area
;
2882 else if (ud
->type
== GTK_TYPE_TREE_VIEW
)
2883 ud
->fn
= update_tree_view
;
2884 else if (ud
->type
== GTK_TYPE_COMBO_BOX_TEXT
)
2885 ud
->fn
= update_combo_box_text
;
2886 else if (ud
->type
== GTK_TYPE_LABEL
)
2887 ud
->fn
= update_label
;
2888 else if (ud
->type
== GTK_TYPE_IMAGE
)
2889 ud
->fn
= update_image
;
2890 else if (ud
->type
== GTK_TYPE_TEXT_VIEW
)
2891 ud
->fn
= update_text_view
;
2892 else if (ud
->type
== GTK_TYPE_NOTEBOOK
)
2893 ud
->fn
= update_notebook
;
2894 else if (ud
->type
== GTK_TYPE_EXPANDER
)
2895 ud
->fn
= update_expander
;
2896 else if (ud
->type
== GTK_TYPE_FRAME
)
2897 ud
->fn
= update_frame
;
2898 else if (ud
->type
== GTK_TYPE_SCROLLED_WINDOW
)
2899 ud
->fn
= update_scrolled_window
;
2900 else if (ud
->type
== GTK_TYPE_BUTTON
)
2901 ud
->fn
= update_button
;
2902 else if (ud
->type
== GTK_TYPE_FILE_CHOOSER_DIALOG
)
2903 ud
->fn
= update_file_chooser_dialog
;
2904 else if (ud
->type
== GTK_TYPE_FILE_CHOOSER_BUTTON
)
2905 ud
->fn
= update_file_chooser_button
;
2906 else if (ud
->type
== GTK_TYPE_COLOR_BUTTON
)
2907 ud
->fn
= update_color_button
;
2908 else if (ud
->type
== GTK_TYPE_FONT_BUTTON
)
2909 ud
->fn
= update_font_button
;
2910 else if (ud
->type
== GTK_TYPE_PRINT_UNIX_DIALOG
)
2911 ud
->fn
= update_print_dialog
;
2912 else if (ud
->type
== GTK_TYPE_SWITCH
)
2913 ud
->fn
= update_switch
;
2914 else if (ud
->type
== GTK_TYPE_TOGGLE_BUTTON
||
2915 ud
->type
== GTK_TYPE_RADIO_BUTTON
||
2916 ud
->type
== GTK_TYPE_CHECK_BUTTON
)
2917 ud
->fn
= update_toggle_button
;
2918 else if (ud
->type
== GTK_TYPE_ENTRY
)
2919 ud
->fn
= update_entry
;
2920 else if (ud
->type
== GTK_TYPE_SPIN_BUTTON
)
2921 ud
->fn
= update_spin_button
;
2922 else if (ud
->type
== GTK_TYPE_SCALE
)
2923 ud
->fn
= update_scale
;
2924 else if (ud
->type
== GTK_TYPE_PROGRESS_BAR
)
2925 ud
->fn
= update_progress_bar
;
2926 else if (ud
->type
== GTK_TYPE_SPINNER
)
2927 ud
->fn
= update_spinner
;
2928 else if (ud
->type
== GTK_TYPE_STATUSBAR
)
2929 ud
->fn
= update_statusbar
;
2930 else if (ud
->type
== GTK_TYPE_CALENDAR
)
2931 ud
->fn
= update_calendar
;
2932 else if (ud
->type
== GTK_TYPE_SOCKET
)
2933 ud
->fn
= update_socket
;
2934 else if (ud
->type
== GTK_TYPE_WINDOW
||
2935 ud
->type
== GTK_TYPE_DIALOG
)
2936 ud
->fn
= update_window
;
2940 pthread_testcancel();
2941 gdk_threads_add_timeout(0, (GSourceFunc
) update_ui
, ud
);
2949 * ============================================================
2951 * ============================================================
2955 * Return the first string xpath obtains from ui_file.
2956 * xmlFree(string) must be called when done
2959 xpath1(xmlChar
*xpath
, const char *ui_file
)
2962 xmlDocPtr doc
= NULL
;
2963 xmlNodeSetPtr nodes
= NULL
;
2964 xmlXPathContextPtr ctx
= NULL
;
2965 xmlXPathObjectPtr xpath_obj
= NULL
;
2967 if ((doc
= xmlParseFile(ui_file
)) == NULL
)
2969 if ((ctx
= xmlXPathNewContext(doc
)) == NULL
)
2971 if ((xpath_obj
= xmlXPathEvalExpression(xpath
, ctx
)) == NULL
)
2973 if ((nodes
= xpath_obj
->nodesetval
) != NULL
&& nodes
->nodeNr
> 0)
2974 r
= xmlNodeGetContent(nodes
->nodeTab
[0]);
2975 xmlXPathFreeObject(xpath_obj
);
2977 xmlXPathFreeContext(ctx
);
2985 * Attach key "col_number" to renderer. Associate "col_number" with
2986 * the corresponding column number in the underlying model.
2987 * Due to what looks like a gap in the GTK API, renderer id and column
2988 * number are taken directly from the XML .ui file.
2991 tree_view_column_get_renderer_column(GtkBuilder
*builder
, const char *ui_file
,
2992 GtkTreeViewColumn
*t_col
, int n
,
2993 GtkCellRenderer
**rnd
)
2996 char *xp_bas1
= "//object[@class=\"GtkTreeViewColumn\" and @id=\"";
2997 char *xp_bas2
= "\"]/child[";
2998 char *xp_bas3
= "]/object[@class=\"GtkCellRendererText\""
2999 " or @class=\"GtkCellRendererToggle\"]/";
3000 char *xp_rnd_id
= "@id";
3001 char *xp_text_col
= "../attributes/attribute[@name=\"text\""
3002 " or @name=\"active\"]/text()";
3003 const char *tree_col_id
= widget_id(GTK_BUILDABLE(t_col
));
3004 size_t xp_rnd_nam_len
, xp_mod_col_len
;
3005 size_t xp_n_len
= 3; /* Big Enough (TM) */
3006 xmlChar
*xp_rnd_nam
= NULL
, *xp_mod_col
= NULL
;
3007 xmlChar
*rnd_nam
= NULL
, *mod_col
= NULL
;
3009 /* find name of nth cell renderer under the GtkTreeViewColumn */
3011 xp_rnd_nam_len
= strlen(xp_bas1
) + strlen(tree_col_id
) +
3012 strlen(xp_bas2
) + xp_n_len
+ strlen(xp_bas3
) +
3013 strlen(xp_rnd_id
) + sizeof('\0');
3014 if ((xp_rnd_nam
= malloc(xp_rnd_nam_len
)) == NULL
)
3016 snprintf((char *) xp_rnd_nam
, xp_rnd_nam_len
, "%s%s%s%d%s%s",
3017 xp_bas1
, tree_col_id
, xp_bas2
, n
,
3018 xp_bas3
, xp_rnd_id
);
3019 rnd_nam
= xpath1(xp_rnd_nam
, ui_file
);
3020 /* find the model column that is attached to the nth cell */
3021 /* renderer under GtkTreeViewColumn tree_col_id */
3022 xp_mod_col_len
= strlen(xp_bas1
) + strlen(tree_col_id
) +
3023 strlen(xp_bas2
) + xp_n_len
+ strlen(xp_bas3
) +
3024 strlen(xp_text_col
) + sizeof('\0');
3025 if ((xp_mod_col
= malloc(xp_mod_col_len
)) == NULL
)
3027 snprintf((char *) xp_mod_col
, xp_mod_col_len
, "%s%s%s%d%s%s",
3028 xp_bas1
, tree_col_id
, xp_bas2
, n
, xp_bas3
, xp_text_col
);
3029 mod_col
= xpath1(xp_mod_col
, ui_file
);
3031 *rnd
= GTK_CELL_RENDERER(
3032 gtk_builder_get_object(builder
, (char *) rnd_nam
));
3034 g_object_set_data(G_OBJECT(*rnd
), "col_number",
3035 GINT_TO_POINTER(strtol((char *) mod_col
,
3048 * Callbacks that forward a modification of a tree view cell to the
3052 cb_tree_model_edit(GtkCellRenderer
*renderer
, const gchar
*path_s
,
3053 const gchar
*new_text
, struct info
*ar
)
3056 int col
= GPOINTER_TO_INT(g_object_get_data(G_OBJECT(renderer
),
3059 gtk_tree_model_get_iter_from_string(ar
->model
, &iter
, path_s
);
3060 set_tree_view_cell(ar
->model
, &iter
, path_s
, col
,
3062 send_tree_cell_msg_by(send_msg
, path_s
, &iter
, col
, ar
);
3066 cb_tree_model_toggle(GtkCellRenderer
*renderer
, gchar
*path_s
, struct info
*ar
)
3070 int col
= GPOINTER_TO_INT(g_object_get_data(G_OBJECT(renderer
),
3073 gtk_tree_model_get_iter_from_string(ar
->model
, &iter
, path_s
);
3074 gtk_tree_model_get(ar
->model
, &iter
, col
, &toggle_state
, -1);
3075 set_tree_view_cell(ar
->model
, &iter
, path_s
, col
,
3076 toggle_state
? "0" : "1");
3080 * Add new element containing id to the list of callback-handler ids
3081 * stored in obj's field named "signal_id"
3084 push_handler_id(gpointer
*obj
, unsigned int id
)
3086 struct handler_id
*prev_hid
, *hid
;
3088 prev_hid
= g_object_get_data(G_OBJECT(obj
), "signal-id");
3089 if ((hid
= malloc(sizeof(struct handler_id
))) == NULL
)
3091 hid
->next
= prev_hid
;
3093 hid
->blocked
= false;
3094 g_object_set_data(G_OBJECT(obj
), "signal-id", hid
);
3098 * Connect function cb to obj's widget signal sig, remembering the
3099 * handler id in a list in obj's field named "signal-id"
3102 sig_conn(gpointer
*obj
, char *sig
, GCallback cb
, struct info
*ar
)
3104 unsigned int handler_id
= g_signal_connect(obj
, sig
, cb
, ar
);
3106 push_handler_id(obj
, handler_id
);
3110 sig_conn_swapped(gpointer
*obj
, char *sig
, GCallback cb
, void *data
)
3112 unsigned int handler_id
= g_signal_connect_swapped(obj
, sig
, cb
, data
);
3114 push_handler_id(obj
, handler_id
);
3118 connect_widget_signals(gpointer
*obj
, struct info
*ar
)
3121 GType type
= G_TYPE_INVALID
;
3122 char *suffix
= NULL
;
3123 const char *w_id
= NULL
;
3126 type
= G_TYPE_FROM_INSTANCE(obj
);
3127 if (GTK_IS_BUILDABLE(obj
))
3128 w_id
= widget_id(GTK_BUILDABLE(obj
));
3129 if (type
== GTK_TYPE_TREE_VIEW_COLUMN
) {
3130 GList
*cells
= gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(obj
));
3131 GtkTreeViewColumn
*tv_col
= GTK_TREE_VIEW_COLUMN(obj
);
3132 GObject
*view
= G_OBJECT(
3133 gtk_tree_view_column_get_tree_view(tv_col
));
3134 unsigned int i
, n_cells
= g_list_length(cells
);
3137 sig_conn(obj
, "clicked", G_CALLBACK(cb_simple
), info_txt_new(o
, "clicked"));
3138 for (i
= 1; i
<= n_cells
; i
++) {
3139 GtkCellRenderer
*renderer
;
3140 gboolean editable
= FALSE
;
3142 if (!tree_view_column_get_renderer_column(ar
->builder
, ar
->txt
, tv_col
,
3145 if (GTK_IS_CELL_RENDERER_TEXT(renderer
)) {
3146 g_object_get(renderer
, "editable", &editable
, NULL
);
3148 GtkTreeModel
*model
= gtk_tree_view_get_model(GTK_TREE_VIEW(view
));
3150 g_signal_connect(renderer
, "edited",
3151 G_CALLBACK(cb_tree_model_edit
),
3152 info_obj_new(o
, view
, model
));
3154 } else if (GTK_IS_CELL_RENDERER_TOGGLE(renderer
)) {
3155 g_object_get(renderer
, "activatable", &editable
, NULL
);
3157 GtkTreeModel
*model
= gtk_tree_view_get_model(GTK_TREE_VIEW(view
));
3159 g_signal_connect(renderer
, "toggled",
3160 G_CALLBACK(cb_tree_model_toggle
),
3161 info_obj_new(o
, NULL
, model
));
3165 } else if (type
== GTK_TYPE_BUTTON
)
3166 /* Button associated with a GtkTextView. */
3167 if ((suffix
= strstr(w_id
, "_send_text")) != NULL
&&
3168 GTK_IS_TEXT_VIEW(obj2
= obj_sans_suffix(ar
->builder
, suffix
, w_id
)))
3169 sig_conn(obj
, "clicked", G_CALLBACK(cb_send_text
),
3170 info_obj_new(o
, G_OBJECT(gtk_text_view_get_buffer(GTK_TEXT_VIEW(obj2
))), NULL
));
3171 else if ((suffix
= strstr(w_id
, "_send_selection")) != NULL
&&
3172 GTK_IS_TEXT_VIEW(obj2
= obj_sans_suffix(ar
->builder
, suffix
, w_id
)))
3173 sig_conn(obj
, "clicked", G_CALLBACK(cb_send_text_selection
),
3174 info_obj_new(o
, G_OBJECT(gtk_text_view_get_buffer(GTK_TEXT_VIEW(obj2
))), NULL
));
3176 sig_conn(obj
, "clicked", G_CALLBACK(cb_simple
), info_txt_new(o
, "clicked"));
3177 /* Buttons associated with (and part of) a GtkDialog.
3178 * (We shun response ids which could be returned from
3179 * gtk_dialog_run() because that would require the
3180 * user to define those response ids in Glade,
3182 if ((suffix
= strstr(w_id
, "_cancel")) != NULL
&&
3183 GTK_IS_DIALOG(obj2
= obj_sans_suffix(ar
->builder
, suffix
, w_id
)))
3184 if (eql(widget_id(GTK_BUILDABLE(obj2
)), MAIN_WIN
))
3185 sig_conn_swapped(obj
, "clicked",
3186 G_CALLBACK(gtk_main_quit
), NULL
);
3188 sig_conn_swapped(obj
, "clicked",
3189 G_CALLBACK(gtk_widget_hide
), obj2
);
3190 else if ((suffix
= strstr(w_id
, "_ok")) != NULL
&&
3191 GTK_IS_DIALOG(obj2
= obj_sans_suffix(ar
->builder
, suffix
, w_id
))) {
3192 if (GTK_IS_FILE_CHOOSER_DIALOG(obj2
))
3193 sig_conn_swapped(obj
, "clicked",
3194 G_CALLBACK(cb_send_file_chooser_dialog_selection
),
3195 info_obj_new(o
, obj2
, NULL
));
3196 if (eql(widget_id(GTK_BUILDABLE(obj2
)), MAIN_WIN
))
3197 sig_conn_swapped(obj
, "clicked",
3198 G_CALLBACK(gtk_main_quit
), NULL
);
3200 sig_conn_swapped(obj
, "clicked",
3201 G_CALLBACK(gtk_widget_hide
), obj2
);
3202 } else if ((suffix
= strstr(w_id
, "_apply")) != NULL
&&
3203 GTK_IS_FILE_CHOOSER_DIALOG(obj2
= obj_sans_suffix(ar
->builder
, suffix
, w_id
)))
3204 sig_conn_swapped(obj
, "clicked",
3205 G_CALLBACK(cb_send_file_chooser_dialog_selection
),
3206 info_obj_new(o
, obj2
, NULL
));
3208 else if (GTK_IS_MENU_ITEM(obj
))
3209 if ((suffix
= strstr(w_id
, "_invoke")) != NULL
&&
3210 GTK_IS_DIALOG(obj2
= obj_sans_suffix(ar
->builder
, suffix
, w_id
)))
3211 sig_conn_swapped(obj
, "activate",
3212 G_CALLBACK(gtk_widget_show
), obj2
);
3214 sig_conn(obj
, "activate",
3215 G_CALLBACK(cb_menu_item
), info_txt_new(o
, "active"));
3216 else if (GTK_IS_WINDOW(obj
)) {
3217 sig_conn(obj
, "delete-event",
3218 G_CALLBACK(cb_event_simple
), info_txt_new(o
, "closed"));
3219 if (eql(w_id
, MAIN_WIN
))
3220 sig_conn_swapped(obj
, "delete-event",
3221 G_CALLBACK(gtk_main_quit
), NULL
);
3223 sig_conn(obj
, "delete-event",
3224 G_CALLBACK(gtk_widget_hide_on_delete
), NULL
);
3225 } else if (type
== GTK_TYPE_FILE_CHOOSER_BUTTON
)
3226 sig_conn(obj
, "file-set",
3227 G_CALLBACK(cb_file_chooser_button
), info_txt_new(o
, "file"));
3228 else if (type
== GTK_TYPE_COLOR_BUTTON
)
3229 sig_conn(obj
, "color-set",
3230 G_CALLBACK(cb_color_button
), info_txt_new(o
, "color"));
3231 else if (type
== GTK_TYPE_FONT_BUTTON
)
3232 sig_conn(obj
, "font-set",
3233 G_CALLBACK(cb_font_button
), info_txt_new(o
, "font"));
3234 else if (type
== GTK_TYPE_SWITCH
)
3235 sig_conn(obj
, "notify::active",
3236 G_CALLBACK(cb_switch
), info_txt_new(o
, NULL
));
3237 else if (type
== GTK_TYPE_TOGGLE_BUTTON
||
3238 type
== GTK_TYPE_RADIO_BUTTON
||
3239 type
== GTK_TYPE_CHECK_BUTTON
)
3240 sig_conn(obj
, "toggled",
3241 G_CALLBACK(cb_toggle_button
), info_txt_new(o
, NULL
));
3242 else if (type
== GTK_TYPE_ENTRY
)
3243 sig_conn(obj
, "changed",
3244 G_CALLBACK(cb_editable
), info_txt_new(o
, "text"));
3245 else if (type
== GTK_TYPE_SPIN_BUTTON
)
3246 sig_conn(obj
, "value_changed",
3247 G_CALLBACK(cb_spin_button
), info_txt_new(o
, "text")); /* TODO: rename to "value" */
3248 else if (type
== GTK_TYPE_SCALE
)
3249 sig_conn(obj
, "value-changed",
3250 G_CALLBACK(cb_range
), info_txt_new(o
, "value"));
3251 else if (type
== GTK_TYPE_CALENDAR
) {
3252 sig_conn(obj
, "day-selected-double-click",
3253 G_CALLBACK(cb_calendar
), info_txt_new(o
, "doubleclicked"));
3254 sig_conn(obj
, "day-selected",
3255 G_CALLBACK(cb_calendar
), info_txt_new(o
, "clicked"));
3256 } else if (type
== GTK_TYPE_TREE_SELECTION
)
3257 sig_conn(obj
, "changed",
3258 G_CALLBACK(cb_tree_selection
), info_txt_new(o
, "clicked"));
3259 else if (type
== GTK_TYPE_SOCKET
) {
3260 sig_conn(obj
, "plug-added",
3261 G_CALLBACK(cb_simple
), info_txt_new(o
, "plug-added"));
3262 sig_conn(obj
, "plug-removed",
3263 G_CALLBACK(cb_simple
), info_txt_new(o
, "plug-removed"));
3264 /* TODO: rename to plug_added, plug_removed */
3265 } else if (type
== GTK_TYPE_DRAWING_AREA
)
3266 sig_conn(obj
, "draw", G_CALLBACK(cb_draw
), NULL
);
3267 else if (type
== GTK_TYPE_EVENT_BOX
) {
3268 gtk_widget_set_can_focus(GTK_WIDGET(obj
), true);
3269 sig_conn(obj
, "button-press-event",
3270 G_CALLBACK(cb_event_box_button
),
3271 info_txt_new(o
, "button_press"));
3272 sig_conn(obj
, "button-release-event",
3273 G_CALLBACK(cb_event_box_button
),
3274 info_txt_new(o
, "button_release"));
3275 sig_conn(obj
, "motion-notify-event",
3276 G_CALLBACK(cb_event_box_motion
),
3277 info_txt_new(o
, "motion"));
3278 sig_conn(obj
, "key-press-event",
3279 G_CALLBACK(cb_event_box_key
),
3280 info_txt_new(o
, "key_press"));
3285 * We keep a style provider with each widget
3288 add_widget_style_provider(gpointer
*obj
, void *data
)
3290 GtkCssProvider
*style_provider
;
3291 GtkStyleContext
*context
;
3294 if (!GTK_IS_WIDGET(obj
))
3296 style_provider
= gtk_css_provider_new();
3297 context
= gtk_widget_get_style_context(GTK_WIDGET(obj
));
3298 gtk_style_context_add_provider(context
,
3299 GTK_STYLE_PROVIDER(style_provider
),
3300 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
);
3301 g_object_set_data(G_OBJECT(obj
), "style_provider", style_provider
);
3305 prepare_widgets(GtkBuilder
*builder
, char *ui_file
, FILE *out
)
3307 GSList
*objects
= NULL
;
3308 struct info ar
= {.builder
= builder
, .fout
= out
, .txt
= ui_file
};
3310 objects
= gtk_builder_get_objects(builder
);
3311 g_slist_foreach(objects
, (GFunc
) connect_widget_signals
, &ar
);
3312 g_slist_foreach(objects
, (GFunc
) add_widget_style_provider
, NULL
);
3313 g_slist_free(objects
);
3317 main(int argc
, char *argv
[])
3319 GObject
*main_window
= NULL
;
3321 char *in_fifo
= NULL
, *out_fifo
= NULL
;
3322 char *ui_file
= "pipeglade.ui", *log_file
= NULL
, *err_file
= NULL
;
3328 /* Disable runtime GLIB deprecation warnings: */
3329 setenv("G_ENABLE_DIAGNOSTIC", "0", 0);
3330 gtk_init(&argc
, &argv
);
3331 while ((opt
= getopt(argc
, argv
, "bGhe:i:l:o:O:u:V")) != -1) {
3333 case 'b': bg
= true; break;
3334 case 'e': xid
= optarg
; break;
3335 case 'G': show_lib_versions(); break;
3336 case 'h': bye(EXIT_SUCCESS
, stdout
, USAGE
); break;
3337 case 'i': in_fifo
= optarg
; break;
3338 case 'l': log_file
= optarg
; break;
3339 case 'o': out_fifo
= optarg
; break;
3340 case 'O': err_file
= optarg
; break;
3341 case 'u': ui_file
= optarg
; break;
3342 case 'V': bye(EXIT_SUCCESS
, stdout
, "%s\n", VERSION
); break;
3344 default: bye(EXIT_FAILURE
, stderr
, USAGE
); break;
3347 if (argv
[optind
] != NULL
)
3348 bye(EXIT_FAILURE
, stderr
,
3349 "illegal parameter '%s'\n" USAGE
, argv
[optind
]);
3350 redirect_stderr(err_file
);
3351 ar
.fin
= open_fifo(in_fifo
, "r", stdin
, _IONBF
);
3352 ar
.fout
= open_fifo(out_fifo
, "w", stdout
, _IOLBF
);
3353 go_bg_if(bg
, ar
.fin
, ar
.fout
, err_file
);
3354 ar
.builder
= builder_from_file(ui_file
);
3355 ar
.flog
= open_log(log_file
);
3356 pthread_create(&receiver
, NULL
, (void *(*)(void *)) digest_cmd
, &ar
);
3357 main_window
= find_main_window(ar
.builder
);
3359 LIBXML_TEST_VERSION
;
3360 prepare_widgets(ar
.builder
, ui_file
, ar
.fout
);
3361 xembed_if(xid
, main_window
);
3363 pthread_cancel(receiver
);
3364 pthread_join(receiver
, NULL
);
3366 rm_unless(stdin
, ar
.fin
, in_fifo
);
3367 rm_unless(stdout
, ar
.fout
, out_fifo
);