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 * Preclude triggering of GTK's default GtkLinkButton action which
498 * could otherwise interfere with pipeglade's own signal blocking
501 gtk_show_uri(GdkScreen
*s
, const gchar
*uri
, guint32 ts
, GError
**e
)
503 (void) s
; (void) uri
; (void) ts
; (void) e
;
508 send_msg_to(FILE* o
, GtkBuildable
*obj
, const char *tag
, va_list ap
)
511 const char *w_id
= widget_id(obj
);
514 struct timeval timeout
= {1, 0};
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
);
525 "send error; discarding feedback message %s:%s\n",
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.
535 send_msg(FILE *o
, GtkBuildable
*obj
, const char *tag
, ...)
540 send_msg_to(o
, obj
, tag
, 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.
550 send_msg_as_cmd(FILE *o
, GtkBuildable
*obj
, const char *tag
, ...)
555 send_msg_to(o
, obj
, "set", ap
);
560 * Stuff to pass around
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 */
574 * Data to be passed to and from the GTK main loop
577 void (*fn
)(struct ui_data
*);
588 * Return pointer to a newly allocated struct info
591 info_new_full(FILE *stream
, GObject
*obj
, GtkTreeModel
*model
, char *txt
)
595 if ((ar
= malloc(sizeof(struct info
))) == NULL
)
608 info_txt_new(FILE *stream
, char *txt
)
610 return info_new_full(stream
, NULL
, NULL
, txt
);
614 info_obj_new(FILE *stream
, GObject
*obj
, GtkTreeModel
*model
)
616 return info_new_full(stream
, obj
, model
, NULL
);
620 * Use msg_sender() to send a message describing a particular cell
623 send_tree_cell_msg_by(void msg_sender(FILE *, GtkBuildable
*, const char *, ...),
625 GtkTreeIter
*iter
, int col
, struct info
*ar
)
627 GtkBuildable
*obj
= GTK_BUILDABLE(ar
->obj
);
628 GtkTreeModel
*model
= ar
->model
;
630 GValue value
= G_VALUE_INIT
;
631 char str
[BUFLEN
], *lc
= lc_numeric();
633 gtk_tree_model_get_value(model
, iter
, col
, &value
);
634 col_type
= gtk_tree_model_get_column_type(model
, col
);
637 snprintf(str
, BUFLEN
, " %d %d", col
, g_value_get_int(&value
));
638 msg_sender(ar
->fout
, obj
, "gint", path_s
, str
, NULL
);
641 snprintf(str
, BUFLEN
, " %d %ld", col
, g_value_get_long(&value
));
642 msg_sender(ar
->fout
, obj
, "glong", path_s
, str
, NULL
);
645 snprintf(str
, BUFLEN
, " %d %" PRId64
, col
, g_value_get_int64(&value
));
646 msg_sender(ar
->fout
, obj
, "gint64", path_s
, str
, NULL
);
649 snprintf(str
, BUFLEN
, " %d %u", col
, g_value_get_uint(&value
));
650 msg_sender(ar
->fout
, obj
, "guint", path_s
, str
, NULL
);
653 snprintf(str
, BUFLEN
, " %d %lu", col
, g_value_get_ulong(&value
));
654 msg_sender(ar
->fout
, obj
, "gulong", path_s
, str
, NULL
);
657 snprintf(str
, BUFLEN
, " %d %" PRIu64
, col
, g_value_get_uint64(&value
));
658 msg_sender(ar
->fout
, obj
, "guint64", path_s
, str
, NULL
);
661 snprintf(str
, BUFLEN
, " %d %d", col
, g_value_get_boolean(&value
));
662 msg_sender(ar
->fout
, obj
, "gboolean", path_s
, str
, NULL
);
665 snprintf(str
, BUFLEN
, " %d %f", col
, g_value_get_float(&value
));
666 msg_sender(ar
->fout
, obj
, "gfloat", path_s
, str
, NULL
);
669 snprintf(str
, BUFLEN
, " %d %f", col
, g_value_get_double(&value
));
670 msg_sender(ar
->fout
, obj
, "gdouble", path_s
, str
, NULL
);
673 snprintf(str
, BUFLEN
, " %d ", col
);
674 msg_sender(ar
->fout
, obj
, "gchararray", path_s
, str
, g_value_get_string(&value
), NULL
);
677 fprintf(stderr
, "column %d not implemented: %s\n", col
, G_VALUE_TYPE_NAME(&value
));
680 g_value_unset(&value
);
685 * Use msg_sender() to send one message per column for a single row
688 send_tree_row_msg_by(void msg_sender(FILE *, GtkBuildable
*, const char *, ...),
689 char *path_s
, GtkTreeIter
*iter
, struct info
*ar
)
693 for (col
= 0; col
< gtk_tree_model_get_n_columns(ar
->model
); col
++)
694 send_tree_cell_msg_by(msg_sender
, path_s
, iter
, col
, ar
);
698 * send_tree_row_msg serves as an argument for
699 * gtk_tree_selection_selected_foreach()
702 send_tree_row_msg(GtkTreeModel
*model
,
703 GtkTreePath
*path
, GtkTreeIter
*iter
, struct info
*ar
)
705 char *path_s
= gtk_tree_path_to_string(path
);
708 send_tree_row_msg_by(send_msg
, path_s
, iter
, ar
);
714 * save_tree_row_msg serves as an argument for
715 * gtk_tree_model_foreach().
716 * Send message from GUI to global stream "save".
719 save_tree_row_msg(GtkTreeModel
*model
,
720 GtkTreePath
*path
, GtkTreeIter
*iter
, struct info
*ar
)
722 char *path_s
= gtk_tree_path_to_string(path
);
725 send_tree_row_msg_by(send_msg_as_cmd
, path_s
, iter
, ar
);
731 cb_calendar(GtkBuildable
*obj
, struct info
*ar
)
734 unsigned int year
= 0, month
= 0, day
= 0;
736 gtk_calendar_get_date(GTK_CALENDAR(obj
), &year
, &month
, &day
);
737 snprintf(str
, BUFLEN
, "%04u-%02u-%02u", year
, ++month
, day
);
738 send_msg(ar
->fout
, obj
, ar
->txt
, str
, NULL
);
742 cb_color_button(GtkBuildable
*obj
, struct info
*ar
)
746 gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(obj
), &color
);
747 send_msg(ar
->fout
, obj
, ar
->txt
, gdk_rgba_to_string(&color
), NULL
);
751 cb_editable(GtkBuildable
*obj
, struct info
*ar
)
753 send_msg(ar
->fout
, obj
, ar
->txt
, gtk_entry_get_text(GTK_ENTRY(obj
)), NULL
);
757 * Callback that sends a message about a pointer device button press
761 cb_event_box_button(GtkBuildable
*obj
, GdkEvent
*e
, struct info
*ar
)
763 char data
[BUFLEN
], *lc
= lc_numeric();
765 snprintf(data
, BUFLEN
, "%d %.1lf %.1lf",
766 e
->button
.button
, e
->button
.x
, e
->button
.y
);
767 send_msg(ar
->fout
, obj
, ar
->txt
, data
, NULL
);
773 * Callback that sends in a message the name of the key pressed when
774 * a GtkEventBox is focused
777 cb_event_box_key(GtkBuildable
*obj
, GdkEvent
*e
, struct info
*ar
)
779 send_msg(ar
->fout
, obj
, ar
->txt
, gdk_keyval_name(e
->key
.keyval
), NULL
);
784 * Callback that sends a message about pointer device motion in a
788 cb_event_box_motion(GtkBuildable
*obj
, GdkEvent
*e
, struct info
*ar
)
790 char data
[BUFLEN
], *lc
= lc_numeric();
792 snprintf(data
, BUFLEN
, "%.1lf %.1lf", e
->button
.x
, e
->button
.y
);
793 send_msg(ar
->fout
, obj
, ar
->txt
, data
, NULL
);
799 * Callback that only sends "name:tag" and returns false
802 cb_event_simple(GtkBuildable
*obj
, GdkEvent
*e
, struct info
*ar
)
805 send_msg(ar
->fout
, obj
, ar
->txt
, NULL
);
810 cb_file_chooser_button(GtkBuildable
*obj
, struct info
*ar
)
812 send_msg(ar
->fout
, obj
, ar
->txt
,
813 gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(obj
)), NULL
);
817 cb_font_button(GtkBuildable
*obj
, struct info
*ar
)
819 send_msg(ar
->fout
, obj
, ar
->txt
,
820 gtk_font_button_get_font_name(GTK_FONT_BUTTON(obj
)), NULL
);
824 cb_menu_item(GtkBuildable
*obj
, struct info
*ar
)
826 send_msg(ar
->fout
, obj
, ar
->txt
,
827 gtk_menu_item_get_label(GTK_MENU_ITEM(obj
)), NULL
);
831 cb_range(GtkBuildable
*obj
, struct info
*ar
)
833 char str
[BUFLEN
], *lc
= lc_numeric();
835 snprintf(str
, BUFLEN
, "%f", gtk_range_get_value(GTK_RANGE(obj
)));
836 send_msg(ar
->fout
, obj
, ar
->txt
, str
, NULL
);
841 * Callback that sends user's selection from a file dialog
844 cb_send_file_chooser_dialog_selection(struct info
*ar
)
846 send_msg(ar
->fout
, GTK_BUILDABLE(ar
->obj
), "file",
847 gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(ar
->obj
)),
849 send_msg(ar
->fout
, GTK_BUILDABLE(ar
->obj
), "folder",
850 gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(ar
->obj
)),
855 * Callback that sends in a message the content of the text buffer
856 * passed in user_data
859 cb_send_text(GtkBuildable
*obj
, struct info
*ar
)
863 gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(ar
->obj
), &a
, &b
);
864 send_msg(ar
->fout
, obj
, "text",
865 gtk_text_buffer_get_text(GTK_TEXT_BUFFER(ar
->obj
), &a
, &b
, TRUE
),
870 * Callback that sends in a message the highlighted text from the text
871 * buffer which was passed in user_data
874 cb_send_text_selection(GtkBuildable
*obj
, struct info
*ar
)
878 gtk_text_buffer_get_selection_bounds(GTK_TEXT_BUFFER(ar
->obj
), &a
, &b
);
879 send_msg(ar
->fout
, obj
, "text",
880 gtk_text_buffer_get_text(GTK_TEXT_BUFFER(ar
->obj
), &a
, &b
, TRUE
),
885 * Callback that only sends "name:tag data" and returns true
888 cb_simple(GtkBuildable
*obj
, struct info
*ar
)
890 send_msg(ar
->fout
, obj
, ar
->txt
, ar
->data
, NULL
);
895 cb_spin_button(GtkBuildable
*obj
, struct info
*ar
)
897 char str
[BUFLEN
], *lc
= lc_numeric();
899 snprintf(str
, BUFLEN
, "%f", gtk_spin_button_get_value(GTK_SPIN_BUTTON(obj
)));
900 send_msg(ar
->fout
, obj
, ar
->txt
, str
, NULL
);
905 cb_switch(GtkBuildable
*obj
, void *pspec
, struct info
*ar
)
908 send_msg(ar
->fout
, obj
,
909 gtk_switch_get_active(GTK_SWITCH(obj
)) ? "1" : "0",
914 cb_toggle_button(GtkBuildable
*obj
, struct info
*ar
)
916 send_msg(ar
->fout
, obj
,
917 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(obj
)) ? "1" : "0",
922 cb_tree_selection(GtkBuildable
*obj
, struct info
*ar
)
924 GtkTreeSelection
*sel
= GTK_TREE_SELECTION(obj
);
925 GtkTreeView
*view
= gtk_tree_selection_get_tree_view(sel
);
927 ar
->obj
= G_OBJECT(view
);
928 send_msg(ar
->fout
, GTK_BUILDABLE(view
), ar
->txt
, NULL
);
929 gtk_tree_selection_selected_foreach(
930 sel
, (GtkTreeSelectionForeachFunc
) send_tree_row_msg
, ar
);
935 * ============================================================
936 * cb_draw() maintains a drawing on a GtkDrawingArea; it needs a few
938 * ============================================================
942 * The set of supported drawing operations
973 * Text placement mode for rel_move_for()
987 enum draw_op_policy
{
994 * One single element of a drawing
997 struct draw_op
*next
;
998 struct draw_op
*prev
;
999 unsigned long long int id
;
1000 unsigned long long int before
;
1001 enum draw_op_policy policy
;
1007 * Argument sets for the various drawing operations
1017 struct curve_to_args
{
1026 struct move_to_args
{
1031 struct rectangle_args
{
1038 struct rel_move_for_args
{
1044 struct set_dash_args
{
1049 struct set_font_face_args
{
1050 cairo_font_slant_t slant
;
1051 cairo_font_weight_t weight
;
1055 struct set_font_size_args
{
1059 struct set_line_cap_args
{
1060 cairo_line_cap_t line_cap
;
1063 struct set_line_join_args
{
1064 cairo_line_join_t line_join
;
1067 struct set_line_width_args
{
1071 struct set_source_rgba_args
{
1075 struct show_text_args
{
1080 struct transform_args
{
1081 cairo_matrix_t matrix
;
1085 draw(cairo_t
*cr
, enum cairo_fn op
, void *op_args
)
1089 struct move_to_args
*args
= op_args
;
1091 cairo_line_to(cr
, args
->x
, args
->y
);
1095 struct move_to_args
*args
= op_args
;
1097 cairo_rel_line_to(cr
, args
->x
, args
->y
);
1101 struct move_to_args
*args
= op_args
;
1103 cairo_move_to(cr
, args
->x
, args
->y
);
1107 struct move_to_args
*args
= op_args
;
1109 cairo_rel_move_to(cr
, args
->x
, args
->y
);
1113 struct arc_args
*args
= op_args
;
1115 cairo_arc(cr
, args
->x
, args
->y
, args
->radius
, args
->angle1
, args
->angle2
);
1118 case ARC_NEGATIVE
: {
1119 struct arc_args
*args
= op_args
;
1121 cairo_arc_negative(cr
, args
->x
, args
->y
, args
->radius
, args
->angle1
, args
->angle2
);
1125 struct curve_to_args
*args
= op_args
;
1127 cairo_curve_to(cr
, args
->x1
, args
->y1
, args
->x2
, args
->y2
, args
->x3
, args
->y3
);
1130 case REL_CURVE_TO
: {
1131 struct curve_to_args
*args
= op_args
;
1133 cairo_curve_to(cr
, args
->x1
, args
->y1
, args
->x2
, args
->y2
, args
->x3
, args
->y3
);
1137 struct rectangle_args
*args
= op_args
;
1139 cairo_rectangle(cr
, args
->x
, args
->y
, args
->width
, args
->height
);
1143 cairo_close_path(cr
);
1146 struct show_text_args
*args
= op_args
;
1148 cairo_show_text(cr
, args
->text
);
1151 case REL_MOVE_FOR
: {
1152 cairo_text_extents_t e
;
1153 double dx
= 0.0, dy
= 0.0;
1154 struct rel_move_for_args
*args
= op_args
;
1156 cairo_text_extents(cr
, args
->text
, &e
);
1157 switch (args
->ref
) {
1158 case C
: dx
= -e
.width
/ 2; dy
= e
.height
/ 2; break;
1159 case E
: dx
= -e
.width
; dy
= e
.height
/ 2; break;
1160 case N
: dx
= -e
.width
/ 2; dy
= e
.height
; break;
1161 case NE
: dx
= -e
.width
; dy
= e
.height
; break;
1162 case NW
: dy
= e
.height
; break;
1163 case S
: dx
= -e
.width
/ 2; break;
1164 case SE
: dx
= -e
.width
; break;
1166 case W
: dy
= e
.height
/ 2; break;
1167 default: ABORT
; break;
1169 cairo_rel_move_to(cr
, dx
, dy
);
1173 cairo_identity_matrix(cr
);
1178 case STROKE_PRESERVE
:
1179 cairo_stroke_preserve(cr
);
1185 cairo_fill_preserve(cr
);
1188 struct set_dash_args
*args
= op_args
;
1190 cairo_set_dash(cr
, args
->dashes
, args
->num_dashes
, 0);
1193 case SET_FONT_FACE
: {
1194 struct set_font_face_args
*args
= op_args
;
1196 cairo_select_font_face(cr
, args
->family
, args
->slant
, args
->weight
);
1199 case SET_FONT_SIZE
: {
1200 struct set_font_size_args
*args
= op_args
;
1202 cairo_set_font_size(cr
, args
->size
);
1205 case SET_LINE_CAP
: {
1206 struct set_line_cap_args
*args
= op_args
;
1208 cairo_set_line_cap(cr
, args
->line_cap
);
1211 case SET_LINE_JOIN
: {
1212 struct set_line_join_args
*args
= op_args
;
1214 cairo_set_line_join(cr
, args
->line_join
);
1217 case SET_LINE_WIDTH
: {
1218 struct set_line_width_args
*args
= op_args
;
1220 cairo_set_line_width(cr
, args
->width
);
1223 case SET_SOURCE_RGBA
: {
1224 struct set_source_rgba_args
*args
= op_args
;
1226 gdk_cairo_set_source_rgba(cr
, &args
->color
);
1230 struct transform_args
*args
= op_args
;
1232 cairo_transform(cr
, &args
->matrix
);
1242 * Callback that draws on a GtkDrawingArea
1245 cb_draw(GtkWidget
*widget
, cairo_t
*cr
, gpointer data
)
1250 for (op
= g_object_get_data(G_OBJECT(widget
), "draw_ops");
1253 draw(cr
, op
->op
, op
->op_args
);
1259 * ============================================================
1260 * Manipulating the GUI
1261 * ============================================================
1265 * Generic actions that are applicable to most widgets
1269 * Simulate user activity on various widgets. Runs inside gtk_main().
1272 fake_ui_activity(struct ui_data
*ud
)
1276 if (!GTK_IS_WIDGET(ud
->obj
) || sscanf(ud
->data
, " %c", &dummy
) > 0)
1277 ign_cmd(ud
->type
, ud
->cmd
);
1278 else if (GTK_IS_SPIN_BUTTON(ud
->obj
)) {
1279 ud
->args
->txt
= "text";
1280 cb_spin_button(GTK_BUILDABLE(ud
->obj
), ud
->args
); /* TODO: rename to "value" */
1281 } else if (GTK_IS_SCALE(ud
->obj
)) {
1282 ud
->args
->txt
= "value";
1283 cb_range(GTK_BUILDABLE(ud
->obj
), ud
->args
);
1284 } else if (GTK_IS_ENTRY(ud
->obj
)) {
1285 ud
->args
->txt
= "text";
1286 cb_editable(GTK_BUILDABLE(ud
->obj
), ud
->args
);
1287 } else if (GTK_IS_CALENDAR(ud
->obj
)) {
1288 ud
->args
->txt
= "clicked";
1289 cb_calendar(GTK_BUILDABLE(ud
->obj
), ud
->args
);
1290 } else if (GTK_IS_FILE_CHOOSER_BUTTON(ud
->obj
)) {
1291 ud
->args
->txt
= "file";
1292 cb_file_chooser_button(GTK_BUILDABLE(ud
->obj
), ud
->args
);
1293 } else if (!gtk_widget_activate(GTK_WIDGET(ud
->obj
)))
1294 ign_cmd(ud
->type
, ud
->cmd
);
1298 update_focus(struct ui_data
*ud
){
1301 if (GTK_IS_WIDGET(ud
->obj
) &&
1302 sscanf(ud
->data
, " %c", &dummy
) < 1 &&
1303 gtk_widget_get_can_focus(GTK_WIDGET(ud
->obj
)))
1304 gtk_widget_grab_focus(GTK_WIDGET(ud
->obj
));
1306 ign_cmd(ud
->type
, ud
->cmd
);
1310 * Have the widget say "ping". Runs inside gtk_main().
1313 ping(struct ui_data
*ud
)
1315 if (!GTK_IS_WIDGET(ud
->obj
))
1316 ign_cmd(ud
->type
, ud
->cmd
);
1317 ud
->args
->txt
= "ping";
1318 ud
->args
->data
= ud
->data
;
1319 cb_simple(GTK_BUILDABLE(ud
->obj
), ud
->args
);
1323 * Write snapshot of widget in an appropriate format to file
1326 take_snapshot(struct ui_data
*ud
)
1328 cairo_surface_t
*sur
= NULL
;
1333 if (!GTK_IS_WIDGET(ud
->obj
) ||
1334 !gtk_widget_is_drawable(GTK_WIDGET(ud
->obj
))) {
1335 ign_cmd(ud
->type
, ud
->cmd
);
1338 height
= gtk_widget_get_allocated_height(GTK_WIDGET(ud
->obj
));
1339 width
= gtk_widget_get_allocated_width(GTK_WIDGET(ud
->obj
));
1340 if (has_suffix(ud
->data
, ".epsf") || has_suffix(ud
->data
, ".eps")) {
1341 sur
= cairo_ps_surface_create(ud
->data
, width
, height
);
1342 cairo_ps_surface_set_eps(sur
, TRUE
);
1343 } else if (has_suffix(ud
->data
, ".pdf"))
1344 sur
= cairo_pdf_surface_create(ud
->data
, width
, height
);
1345 else if (has_suffix(ud
->data
, ".ps"))
1346 sur
= cairo_ps_surface_create(ud
->data
, width
, height
);
1347 else if (has_suffix(ud
->data
, ".svg"))
1348 sur
= cairo_svg_surface_create(ud
->data
, width
, height
);
1350 ign_cmd(ud
->type
, ud
->cmd
);
1353 cr
= cairo_create(sur
);
1354 gtk_widget_draw(GTK_WIDGET(ud
->obj
), cr
);
1356 cairo_surface_destroy(sur
);
1360 unsigned int id
; /* returned by g_signal_connect() and friends */
1361 bool blocked
; /* we avoid multiple blocking/unblocking */
1362 struct handler_id
*next
;
1366 update_blocked(struct ui_data
*ud
)
1369 struct handler_id
*hid
;
1370 unsigned long int val
;
1372 if (sscanf(ud
->data
, "%lu %c", &val
, &dummy
) == 1 && val
< 2) {
1373 for (hid
= g_object_get_data(ud
->obj
, "signal-id");
1374 hid
!= NULL
; hid
= hid
->next
) {
1375 if (val
== 0 && hid
->blocked
== true) {
1376 g_signal_handler_unblock(ud
->obj
, hid
->id
);
1377 hid
->blocked
= false;
1378 } else if (val
== 1 && hid
->blocked
== false) {
1379 g_signal_handler_block(ud
->obj
, hid
->id
);
1380 hid
->blocked
= true;
1384 ign_cmd(ud
->type
, ud
->cmd
);
1388 update_sensitivity(struct ui_data
*ud
)
1393 if (GTK_IS_WIDGET(ud
->obj
) &&
1394 sscanf(ud
->data
, "%u %c", &val
, &dummy
) == 1 && val
< 2)
1395 gtk_widget_set_sensitive(GTK_WIDGET(ud
->obj
), val
);
1397 ign_cmd(ud
->type
, ud
->cmd
);
1401 update_size_request(struct ui_data
*ud
)
1406 if (GTK_IS_WIDGET(ud
->obj
) &&
1407 sscanf(ud
->data
, "%d %d %c", &x
, &y
, &dummy
) == 2)
1408 gtk_widget_set_size_request(GTK_WIDGET(ud
->obj
), x
, y
);
1409 else if (GTK_IS_WIDGET(ud
->obj
) &&
1410 sscanf(ud
->data
, " %c", &dummy
) < 1)
1411 gtk_widget_set_size_request(GTK_WIDGET(ud
->obj
), -1, -1);
1413 ign_cmd(ud
->type
, ud
->cmd
);
1417 update_tooltip_text(struct ui_data
*ud
)
1419 if (GTK_IS_WIDGET(ud
->obj
))
1420 gtk_widget_set_tooltip_text(GTK_WIDGET(ud
->obj
), ud
->data
);
1422 ign_cmd(ud
->type
, ud
->cmd
);
1426 update_visibility(struct ui_data
*ud
)
1431 if (GTK_IS_WIDGET(ud
->obj
) &&
1432 sscanf(ud
->data
, "%u %c", &val
, &dummy
) == 1 && val
< 2)
1433 gtk_widget_set_visible(GTK_WIDGET(ud
->obj
), val
);
1435 ign_cmd(ud
->type
, ud
->cmd
);
1439 * Change the style of the widget passed. Runs inside gtk_main().
1442 update_widget_style(struct ui_data
*ud
)
1444 GtkStyleContext
*context
;
1445 GtkStyleProvider
*style_provider
;
1447 const char *prefix
= "* {", *suffix
= "}";
1450 if (!GTK_IS_WIDGET(ud
->obj
)) {
1451 ign_cmd(ud
->type
, ud
->cmd
);
1454 style_provider
= g_object_get_data(ud
->obj
, "style_provider");
1455 sz
= strlen(prefix
) + strlen(suffix
) + strlen(ud
->data
) + 1;
1456 context
= gtk_widget_get_style_context(GTK_WIDGET(ud
->obj
));
1457 gtk_style_context_remove_provider(context
, style_provider
);
1458 if ((style_decl
= malloc(sz
)) == NULL
)
1460 strcpy(style_decl
, prefix
);
1461 strcat(style_decl
, ud
->data
);
1462 strcat(style_decl
, suffix
);
1463 gtk_style_context_add_provider(context
, style_provider
,
1464 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
);
1465 gtk_css_provider_load_from_data(GTK_CSS_PROVIDER(style_provider
),
1466 style_decl
, -1, NULL
);
1471 * Check if one of the generic actions is requested; complain if none
1475 try_generic_cmds(struct ui_data
*ud
)
1477 if (eql(ud
->action
, "block"))
1479 else if (eql(ud
->action
, "set_sensitive"))
1480 update_sensitivity(ud
);
1481 else if (eql(ud
->action
, "set_visible"))
1482 update_visibility(ud
);
1483 else if (eql(ud
->action
, "set_tooltip_text"))
1484 update_tooltip_text(ud
);
1485 else if (eql(ud
->action
, "grab_focus"))
1487 else if (eql(ud
->action
, "set_size_request"))
1488 update_size_request(ud
);
1489 else if (eql(ud
->action
, "style"))
1490 update_widget_style(ud
);
1491 else if (eql(ud
->action
, "force"))
1492 fake_ui_activity(ud
);
1493 else if (eql(ud
->action
, "ping"))
1495 else if (eql(ud
->action
, "snapshot"))
1498 ign_cmd(ud
->type
, ud
->cmd
);
1502 * Manipulation of specific widgets
1506 update_button(struct ui_data
*ud
)
1508 if (eql(ud
->action
, "set_label"))
1509 gtk_button_set_label(GTK_BUTTON(ud
->obj
), ud
->data
);
1511 try_generic_cmds(ud
);
1515 update_calendar(struct ui_data
*ud
)
1517 GtkCalendar
*calendar
= GTK_CALENDAR(ud
->obj
);
1519 int year
= 0, month
= 0, day
= 0;
1521 if (eql(ud
->action
, "select_date") &&
1522 sscanf(ud
->data
, "%d-%d-%d %c", &year
, &month
, &day
, &dummy
) == 3) {
1523 if (month
> -1 && month
<= 11 && day
> 0 && day
<= 31) {
1524 gtk_calendar_select_month(calendar
, --month
, year
);
1525 gtk_calendar_select_day(calendar
, day
);
1527 ign_cmd(ud
->type
, ud
->cmd
);
1528 } else if (eql(ud
->action
, "mark_day") &&
1529 sscanf(ud
->data
, "%d %c", &day
, &dummy
) == 1) {
1530 if (day
> 0 && day
<= 31)
1531 gtk_calendar_mark_day(calendar
, day
);
1533 ign_cmd(ud
->type
, ud
->cmd
);
1534 } else if (eql(ud
->action
, "clear_marks") && sscanf(ud
->data
, " %c", &dummy
) < 1)
1535 gtk_calendar_clear_marks(calendar
);
1537 try_generic_cmds(ud
);
1541 * Common actions for various kinds of window. Return false if
1542 * command is ignored. Runs inside gtk_main().
1545 update_class_window(struct ui_data
*ud
)
1547 GtkWindow
*window
= GTK_WINDOW(ud
->obj
);
1551 if (eql(ud
->action
, "set_title"))
1552 gtk_window_set_title(window
, ud
->data
);
1553 else if (eql(ud
->action
, "fullscreen") && sscanf(ud
->data
, " %c", &dummy
) < 1)
1554 gtk_window_fullscreen(window
);
1555 else if (eql(ud
->action
, "unfullscreen") && sscanf(ud
->data
, " %c", &dummy
) < 1)
1556 gtk_window_unfullscreen(window
);
1557 else if (eql(ud
->action
, "resize") &&
1558 sscanf(ud
->data
, "%d %d %c", &x
, &y
, &dummy
) == 2)
1559 gtk_window_resize(window
, x
, y
);
1560 else if (eql(ud
->action
, "resize") && sscanf(ud
->data
, " %c", &dummy
) < 1) {
1561 gtk_window_get_default_size(window
, &x
, &y
);
1562 gtk_window_resize(window
, x
, y
);
1563 } else if (eql(ud
->action
, "move") &&
1564 sscanf(ud
->data
, "%d %d %c", &x
, &y
, &dummy
) == 2)
1565 gtk_window_move(window
, x
, y
);
1572 update_color_button(struct ui_data
*ud
)
1576 if (eql(ud
->action
, "set_color")) {
1577 gdk_rgba_parse(&color
, ud
->data
);
1578 gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(ud
->obj
), &color
);
1580 try_generic_cmds(ud
);
1584 update_combo_box_text(struct ui_data
*ud
)
1586 GtkComboBoxText
*combobox
= GTK_COMBO_BOX_TEXT(ud
->obj
);
1590 if (eql(ud
->action
, "prepend_text"))
1591 gtk_combo_box_text_prepend_text(combobox
, ud
->data
);
1592 else if (eql(ud
->action
, "append_text"))
1593 gtk_combo_box_text_append_text(combobox
, ud
->data
);
1594 else if (eql(ud
->action
, "remove") &&
1595 sscanf(ud
->data
, "%d %c", &pos
, &dummy
) == 1)
1596 gtk_combo_box_text_remove(combobox
, pos
);
1597 else if (eql(ud
->action
, "insert_text") &&
1598 sscanf(ud
->data
, "%d %n", &pos
, &txt0
) == 1)
1599 gtk_combo_box_text_insert_text(combobox
, pos
, ud
->data
+ txt0
);
1601 try_generic_cmds(ud
);
1605 * update_drawing_area(), which runs inside gtk_main(), maintains a
1606 * list of drawing operations. It needs a few helper functions. It
1607 * is the responsibility of cb_draw() to actually execute the list.
1617 * Fill structure *op with the drawing operation according to action
1618 * and with the appropriate set of arguments
1620 static enum draw_op_stat
1621 set_draw_op(struct draw_op
*op
, const char *action
, const char *data
)
1624 const char *raw_args
= data
;
1625 enum draw_op_stat result
= SUCCESS
;
1628 if (sscanf(data
, "=%llu %n", &op
->id
, &args_start
) == 1) {
1629 op
->policy
= REPLACE
;
1630 result
= NEED_REDRAW
;
1631 } else if (sscanf(data
, "%llu<%llu %n", &op
->id
, &op
->before
, &args_start
) == 2) {
1632 op
->policy
= BEFORE
;
1633 result
= NEED_REDRAW
;
1634 } else if (sscanf(data
, "%llu %n", &op
->id
, &args_start
) == 1)
1635 op
->policy
= APPEND
;
1638 raw_args
+= args_start
;
1639 if (eql(action
, "line_to")) {
1640 struct move_to_args
*args
;
1642 if ((args
= malloc(sizeof(*args
))) == NULL
)
1646 if (sscanf(raw_args
, "%lf %lf %c", &args
->x
, &args
->y
, &dummy
) != 2)
1648 } else if (eql(action
, "rel_line_to")) {
1649 struct move_to_args
*args
;
1651 if ((args
= malloc(sizeof(*args
))) == NULL
)
1653 op
->op
= REL_LINE_TO
;
1655 if (sscanf(raw_args
, "%lf %lf %c", &args
->x
, &args
->y
, &dummy
) != 2)
1657 } else if (eql(action
, "move_to")) {
1658 struct move_to_args
*args
;
1660 if ((args
= malloc(sizeof(*args
))) == NULL
)
1664 if (sscanf(raw_args
, "%lf %lf %c", &args
->x
, &args
->y
, &dummy
) != 2)
1666 } else if (eql(action
, "rel_move_to")) {
1667 struct move_to_args
*args
;
1669 if ((args
= malloc(sizeof(*args
))) == NULL
)
1671 op
->op
= REL_MOVE_TO
;
1673 if (sscanf(raw_args
, "%lf %lf %c", &args
->x
, &args
->y
, &dummy
) != 2)
1675 } else if (eql(action
, "arc")) {
1676 struct arc_args
*args
;
1679 if ((args
= malloc(sizeof(*args
))) == NULL
)
1683 if (sscanf(raw_args
, "%lf %lf %lf %lf %lf %c",
1684 &args
->x
, &args
->y
, &args
->radius
, °1
, °2
, &dummy
) != 5)
1686 args
->angle1
= deg1
* (M_PI
/ 180.L
);
1687 args
->angle2
= deg2
* (M_PI
/ 180.L
);
1688 } else if (eql(action
, "arc_negative")) {
1690 struct arc_args
*args
;
1692 if ((args
= malloc(sizeof(*args
))) == NULL
)
1694 op
->op
= ARC_NEGATIVE
;
1696 if (sscanf(raw_args
, "%lf %lf %lf %lf %lf %c",
1697 &args
->x
, &args
->y
, &args
->radius
, °1
, °2
, &dummy
) != 5)
1699 args
->angle1
= deg1
* (M_PI
/ 180.L
);
1700 args
->angle2
= deg2
* (M_PI
/ 180.L
);
1701 } else if (eql(action
, "curve_to")) {
1702 struct curve_to_args
*args
;
1704 if ((args
= malloc(sizeof(*args
))) == NULL
)
1708 if (sscanf(raw_args
, "%lf %lf %lf %lf %lf %lf %c",
1709 &args
->x1
, &args
->y1
, &args
->x2
, &args
->y2
, &args
->x3
, &args
->y3
, &dummy
) != 6)
1711 } else if (eql(action
, "rel_curve_to")) {
1712 struct curve_to_args
*args
;
1714 if ((args
= malloc(sizeof(*args
))) == NULL
)
1716 op
->op
= REL_CURVE_TO
;
1718 if (sscanf(raw_args
, "%lf %lf %lf %lf %lf %lf %c",
1719 &args
->x1
, &args
->y1
, &args
->x2
, &args
->y2
, &args
->x3
, &args
->y3
, &dummy
) != 6)
1721 } else if (eql(action
, "rectangle")) {
1722 struct rectangle_args
*args
;
1724 if ((args
= malloc(sizeof(*args
))) == NULL
)
1728 if (sscanf(raw_args
, "%lf %lf %lf %lf %c",
1729 &args
->x
, &args
->y
, &args
->width
, &args
->height
, &dummy
) != 4)
1731 } else if (eql(action
, "close_path")) {
1732 op
->op
= CLOSE_PATH
;
1733 if (sscanf(raw_args
, " %c", &dummy
) > 0)
1736 } else if (eql(action
, "show_text")) {
1737 struct show_text_args
*args
;
1740 len
= strlen(raw_args
) + 1;
1741 if ((args
= malloc(sizeof(*args
) + len
* sizeof(args
->text
[0]))) == NULL
)
1745 args
->len
= len
; /* not used */
1746 strncpy(args
->text
, raw_args
, len
);
1747 result
= NEED_REDRAW
;
1748 } else if (eql(action
, "rel_move_for")) {
1749 char ref_point
[2 + 1];
1751 struct rel_move_for_args
*args
;
1753 if (sscanf(raw_args
, "%2s %n", ref_point
, &start
) < 1)
1755 len
= strlen(raw_args
+ start
) + 1;
1756 if ((args
= malloc(sizeof(*args
) + len
* sizeof(args
->text
[0]))) == NULL
)
1758 if (eql(ref_point
, "c"))
1760 else if (eql(ref_point
, "e"))
1762 else if (eql(ref_point
, "n"))
1764 else if (eql(ref_point
, "ne"))
1766 else if (eql(ref_point
, "nw"))
1768 else if (eql(ref_point
, "s"))
1770 else if (eql(ref_point
, "se"))
1772 else if (eql(ref_point
, "sw"))
1774 else if (eql(ref_point
, "w"))
1778 op
->op
= REL_MOVE_FOR
;
1780 args
->len
= len
; /* not used */
1781 strncpy(args
->text
, (raw_args
+ start
), len
);
1782 } else if (eql(action
, "stroke")) {
1784 if (sscanf(raw_args
, " %c", &dummy
) > 0)
1787 result
= NEED_REDRAW
;
1788 } else if (eql(action
, "stroke_preserve")) {
1789 op
->op
= STROKE_PRESERVE
;
1790 if (sscanf(raw_args
, " %c", &dummy
) > 0)
1793 result
= NEED_REDRAW
;
1794 } else if (eql(action
, "fill")) {
1796 if (sscanf(raw_args
, " %c", &dummy
) > 0)
1799 result
= NEED_REDRAW
;
1800 } else if (eql(action
, "fill_preserve")) {
1801 op
->op
= FILL_PRESERVE
;
1802 if (sscanf(raw_args
, " %c", &dummy
) > 0)
1805 result
= NEED_REDRAW
;
1806 } else if (eql(action
, "set_dash")) {
1808 char data1
[strlen(raw_args
) + 1];
1810 struct set_dash_args
*args
;
1812 strcpy(data1
, raw_args
);
1819 } while (next
!= end
);
1820 if ((args
= malloc(sizeof(*args
) + n
* sizeof(args
->dashes
[0]))) == NULL
)
1824 args
->num_dashes
= n
;
1825 for (i
= 0, next
= data1
; i
< n
; i
++, next
= end
) {
1826 args
->dashes
[i
] = strtod(next
, &end
);
1828 } else if (eql(action
, "set_font_face")) {
1829 char slant
[7 + 1]; /* "oblique" */
1830 char weight
[6 + 1]; /* "normal" */
1831 int family_start
, family_len
;
1832 struct set_font_face_args
*args
;
1834 if (sscanf(raw_args
, "%7s %6s %n%*s", slant
, weight
, &family_start
) != 2)
1836 family_len
= strlen(raw_args
+ family_start
) + 1;
1837 if ((args
= malloc(sizeof(*args
) + family_len
* sizeof(args
->family
[0]))) == NULL
)
1839 op
->op
= SET_FONT_FACE
;
1841 strncpy(args
->family
, raw_args
+ family_start
, family_len
);
1842 if (eql(slant
, "normal"))
1843 args
->slant
= CAIRO_FONT_SLANT_NORMAL
;
1844 else if (eql(slant
, "italic"))
1845 args
->slant
= CAIRO_FONT_SLANT_ITALIC
;
1846 else if (eql(slant
, "oblique"))
1847 args
->slant
= CAIRO_FONT_SLANT_OBLIQUE
;
1850 if (eql(weight
, "normal"))
1851 args
->weight
= CAIRO_FONT_WEIGHT_NORMAL
;
1852 else if (eql(weight
, "bold"))
1853 args
->weight
= CAIRO_FONT_WEIGHT_BOLD
;
1856 } else if (eql(action
, "set_font_size")) {
1857 struct set_font_size_args
*args
;
1859 if ((args
= malloc(sizeof(*args
))) == NULL
)
1861 op
->op
= SET_FONT_SIZE
;
1863 if (sscanf(raw_args
, "%lf %c", &args
->size
, &dummy
) != 1)
1865 } else if (eql(action
, "set_line_cap")) {
1866 char str
[6 + 1]; /* "square" */
1867 struct set_line_cap_args
*args
;
1869 if ((args
= malloc(sizeof(*args
))) == NULL
)
1871 op
->op
= SET_LINE_CAP
;
1873 if (sscanf(raw_args
, "%6s %c", str
, &dummy
) != 1)
1875 if (eql(str
, "butt"))
1876 args
->line_cap
= CAIRO_LINE_CAP_BUTT
;
1877 else if (eql(str
, "round"))
1878 args
->line_cap
= CAIRO_LINE_CAP_ROUND
;
1879 else if (eql(str
, "square"))
1880 args
->line_cap
= CAIRO_LINE_CAP_SQUARE
;
1883 } else if (eql(action
, "set_line_join")) {
1884 char str
[5 + 1]; /* "miter" */
1885 struct set_line_join_args
*args
;
1887 if ((args
= malloc(sizeof(*args
))) == NULL
)
1889 op
->op
= SET_LINE_JOIN
;
1891 if (sscanf(raw_args
, "%5s %c", str
, &dummy
) != 1)
1893 if (eql(str
, "miter"))
1894 args
->line_join
= CAIRO_LINE_JOIN_MITER
;
1895 else if (eql(str
, "round"))
1896 args
->line_join
= CAIRO_LINE_JOIN_ROUND
;
1897 else if (eql(str
, "bevel"))
1898 args
->line_join
= CAIRO_LINE_JOIN_BEVEL
;
1901 } else if (eql(action
, "set_line_width")) {
1902 struct set_line_width_args
*args
;
1904 if ((args
= malloc(sizeof(*args
))) == NULL
)
1906 op
->op
= SET_LINE_WIDTH
;
1908 if (sscanf(raw_args
, "%lf %c", &args
->width
, &dummy
) != 1)
1910 } else if (eql(action
, "set_source_rgba")) {
1911 struct set_source_rgba_args
*args
;
1913 if ((args
= malloc(sizeof(*args
))) == NULL
)
1915 op
->op
= SET_SOURCE_RGBA
;
1917 gdk_rgba_parse(&args
->color
, raw_args
);
1918 } else if (eql(action
, "transform")) {
1920 double xx
, yx
, xy
, yy
, x0
, y0
;
1922 if (sscanf(raw_args
, "%lf %lf %lf %lf %lf %lf %c",
1923 &xx
, &yx
, &xy
, &yy
, &x0
, &y0
, &dummy
) == 6) {
1924 struct transform_args
*args
;
1926 if ((args
= malloc(sizeof(*args
))) == NULL
)
1930 cairo_matrix_init(&args
->matrix
, xx
, yx
, xy
, yy
, x0
, y0
);
1931 } else if (sscanf(raw_args
, " %c", &dummy
) < 1) {
1936 } else if (eql(action
, "translate")) {
1938 struct transform_args
*args
;
1940 if ((args
= malloc(sizeof(*args
))) == NULL
)
1944 if (sscanf(raw_args
, "%lf %lf %c", &tx
, &ty
, &dummy
) != 2)
1946 cairo_matrix_init_translate(&args
->matrix
, tx
, ty
);
1947 } else if (eql(action
, "scale")) {
1949 struct transform_args
*args
;
1951 if ((args
= malloc(sizeof(*args
))) == NULL
)
1955 if (sscanf(raw_args
, "%lf %lf %c", &sx
, &sy
, &dummy
) != 2)
1957 cairo_matrix_init_scale(&args
->matrix
, sx
, sy
);
1958 } else if (eql(action
, "rotate")) {
1960 struct transform_args
*args
;
1962 if ((args
= malloc(sizeof(*args
))) == NULL
)
1966 if (sscanf(raw_args
, "%lf %c", &angle
, &dummy
) != 1)
1968 cairo_matrix_init_rotate(&args
->matrix
, angle
* (M_PI
/ 180.L
));
1975 * Add another element to widget's "draw_ops" list
1977 static enum draw_op_stat
1978 ins_draw_op(GObject
*widget
, const char *action
, const char *data
)
1980 enum draw_op_stat result
;
1981 struct draw_op
*new_op
= NULL
, *draw_ops
= NULL
, *prev_op
= NULL
;
1983 if ((new_op
= malloc(sizeof(*new_op
))) == NULL
)
1985 new_op
->op_args
= NULL
;
1986 new_op
->next
= NULL
;
1987 if ((result
= set_draw_op(new_op
, action
, data
)) == FAILURE
) {
1988 free(new_op
->op_args
);
1992 switch (new_op
->policy
) {
1994 if ((draw_ops
= g_object_get_data(widget
, "draw_ops")) == NULL
)
1995 g_object_set_data(widget
, "draw_ops", new_op
);
1997 for (prev_op
= draw_ops
;
1998 prev_op
->next
!= NULL
;
1999 prev_op
= prev_op
->next
);
2000 prev_op
->next
= new_op
;
2004 for (prev_op
= NULL
, draw_ops
= g_object_get_data(widget
, "draw_ops");
2005 draw_ops
!= NULL
&& draw_ops
->id
!= new_op
->before
;
2006 prev_op
= draw_ops
, draw_ops
= draw_ops
->next
);
2007 if (prev_op
== NULL
) { /* prepend a new first element */
2008 g_object_set_data(widget
, "draw_ops", new_op
);
2009 new_op
->next
= draw_ops
;
2010 } else if (draw_ops
== NULL
) /* append */
2011 prev_op
->next
= new_op
;
2013 new_op
->next
= draw_ops
;
2014 prev_op
->next
= new_op
;
2018 for (prev_op
= NULL
, draw_ops
= g_object_get_data(widget
, "draw_ops");
2019 draw_ops
!= NULL
&& draw_ops
->id
!= new_op
->id
;
2020 prev_op
= draw_ops
, draw_ops
= draw_ops
->next
);
2021 if (draw_ops
== NULL
&& prev_op
== NULL
) /* start a new list */
2022 g_object_set_data(widget
, "draw_ops", new_op
);
2023 else if (prev_op
== NULL
) { /* replace the first element */
2024 g_object_set_data(widget
, "draw_ops", new_op
);
2025 new_op
->next
= draw_ops
->next
;
2026 free(draw_ops
->op_args
);
2028 } else if (draw_ops
== NULL
) /* append */
2029 prev_op
->next
= new_op
;
2030 else { /* replace some other element */
2031 new_op
->next
= draw_ops
->next
;
2032 prev_op
->next
= new_op
;
2033 free(draw_ops
->op_args
);
2045 * Remove all elements with the given id from widget's "draw_ops" list
2047 static enum draw_op_stat
2048 rem_draw_op(GObject
*widget
, const char *data
)
2051 struct draw_op
*op
, *next_op
, *prev_op
= NULL
;
2052 unsigned long long int id
;
2054 if (sscanf(data
, "%llu %c", &id
, &dummy
) != 1)
2056 op
= g_object_get_data(widget
, "draw_ops");
2057 while (op
!= NULL
) {
2060 if (prev_op
== NULL
) /* list head */
2061 g_object_set_data(widget
, "draw_ops", op
->next
);
2063 prev_op
->next
= op
->next
;
2074 refresh_widget(GtkWidget
*widget
)
2076 gint height
= gtk_widget_get_allocated_height(widget
);
2077 gint width
= gtk_widget_get_allocated_width(widget
);
2079 gtk_widget_queue_draw_area(widget
, 0, 0, width
, height
);
2080 return G_SOURCE_REMOVE
;
2084 update_drawing_area(struct ui_data
*ud
)
2086 enum draw_op_stat dost
;
2088 if (eql(ud
->action
, "remove"))
2089 dost
= rem_draw_op(ud
->obj
, ud
->data
);
2091 dost
= ins_draw_op(ud
->obj
, ud
->action
, ud
->data
);
2094 gdk_threads_add_idle_full(G_PRIORITY_LOW
,
2095 (GSourceFunc
) refresh_widget
,
2096 GTK_WIDGET(ud
->obj
), NULL
);
2099 try_generic_cmds(ud
);
2110 update_entry(struct ui_data
*ud
)
2112 GtkEntry
*entry
= GTK_ENTRY(ud
->obj
);
2114 if (eql(ud
->action
, "set_text"))
2115 gtk_entry_set_text(entry
, ud
->data
);
2116 else if (eql(ud
->action
, "set_placeholder_text"))
2117 gtk_entry_set_placeholder_text(entry
, ud
->data
);
2119 try_generic_cmds(ud
);
2123 update_expander(struct ui_data
*ud
)
2125 GtkExpander
*expander
= GTK_EXPANDER(ud
->obj
);
2129 if (eql(ud
->action
, "set_expanded") &&
2130 sscanf(ud
->data
, "%u %c", &val
, &dummy
) == 1 && val
< 2)
2131 gtk_expander_set_expanded(expander
, val
);
2132 else if (eql(ud
->action
, "set_label"))
2133 gtk_expander_set_label(expander
, ud
->data
);
2135 try_generic_cmds(ud
);
2139 update_file_chooser_button(struct ui_data
*ud
)
2141 if (eql(ud
->action
, "set_filename"))
2142 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(ud
->obj
), ud
->data
);
2144 try_generic_cmds(ud
);
2148 update_file_chooser_dialog(struct ui_data
*ud
)
2150 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(ud
->obj
);
2152 if (eql(ud
->action
, "set_filename"))
2153 gtk_file_chooser_set_filename(chooser
, ud
->data
);
2154 else if (eql(ud
->action
, "set_current_name"))
2155 gtk_file_chooser_set_current_name(chooser
, ud
->data
);
2156 else if (update_class_window(ud
));
2158 try_generic_cmds(ud
);
2162 update_font_button(struct ui_data
*ud
){
2163 GtkFontButton
*font_button
= GTK_FONT_BUTTON(ud
->obj
);
2165 if (eql(ud
->action
, "set_font_name"))
2166 gtk_font_button_set_font_name(font_button
, ud
->data
);
2168 try_generic_cmds(ud
);
2172 update_frame(struct ui_data
*ud
)
2174 if (eql(ud
->action
, "set_label"))
2175 gtk_frame_set_label(GTK_FRAME(ud
->obj
), ud
->data
);
2177 try_generic_cmds(ud
);
2181 update_image(struct ui_data
*ud
)
2184 GtkImage
*image
= GTK_IMAGE(ud
->obj
);
2186 gtk_image_get_icon_name(image
, NULL
, &size
);
2187 if (eql(ud
->action
, "set_from_file"))
2188 gtk_image_set_from_file(image
, ud
->data
);
2189 else if (eql(ud
->action
, "set_from_icon_name"))
2190 gtk_image_set_from_icon_name(image
, ud
->data
, size
);
2192 try_generic_cmds(ud
);
2196 update_label(struct ui_data
*ud
)
2198 if (eql(ud
->action
, "set_text"))
2199 gtk_label_set_text(GTK_LABEL(ud
->obj
), ud
->data
);
2201 try_generic_cmds(ud
);
2205 update_link_button(struct ui_data
*ud
)
2210 if (eql(ud
->action
, "set_visited") &&
2211 sscanf(ud
->data
, "%u %c", &val
, &dummy
) == 1 && val
< 2)
2212 gtk_link_button_set_visited(GTK_LINK_BUTTON(ud
->obj
), val
);
2218 update_menu(struct ui_data
*ud
)
2221 GtkMenu
* menu
= GTK_MENU(ud
->obj
);
2223 if (eql(ud
->action
, "popup") && sscanf(ud
->data
, " %c", &dummy
) < 1)
2224 gtk_menu_popup(menu
, NULL
, NULL
, NULL
, NULL
, 0,
2225 gtk_get_current_event_time());
2226 else if (eql(ud
->action
, "popdown") && sscanf(ud
->data
, " %c", &dummy
) < 1)
2227 gtk_menu_popdown(menu
);
2229 try_generic_cmds(ud
);
2233 update_menu_item(struct ui_data
*ud
)
2235 try_generic_cmds(ud
);
2239 update_notebook(struct ui_data
*ud
)
2242 int val
, n_pages
= gtk_notebook_get_n_pages(GTK_NOTEBOOK(ud
->obj
));
2244 if (eql(ud
->action
, "set_current_page") &&
2245 sscanf(ud
->data
, "%d %c", &val
, &dummy
) == 1 &&
2246 val
>= 0 && val
< n_pages
)
2247 gtk_notebook_set_current_page(GTK_NOTEBOOK(ud
->obj
), val
);
2249 try_generic_cmds(ud
);
2253 update_nothing(struct ui_data
*ud
)
2259 update_print_dialog(struct ui_data
*ud
)
2261 GtkPageSetup
*page_setup
;
2263 GtkPrintSettings
*settings
;
2264 GtkPrintUnixDialog
*dialog
= GTK_PRINT_UNIX_DIALOG(ud
->obj
);
2265 GtkPrinter
*printer
;
2268 if (eql(ud
->action
, "print")) {
2269 response_id
= gtk_dialog_run(GTK_DIALOG(dialog
));
2270 switch (response_id
) {
2271 case GTK_RESPONSE_OK
:
2272 printer
= gtk_print_unix_dialog_get_selected_printer(dialog
);
2273 settings
= gtk_print_unix_dialog_get_settings(dialog
);
2274 page_setup
= gtk_print_unix_dialog_get_page_setup(dialog
);
2275 job
= gtk_print_job_new(ud
->data
, printer
, settings
, page_setup
);
2276 if (gtk_print_job_set_source_file(job
, ud
->data
, NULL
))
2277 gtk_print_job_send(job
, NULL
, NULL
, NULL
);
2279 ign_cmd(ud
->type
, ud
->cmd
);
2280 g_clear_object(&settings
);
2281 g_clear_object(&job
);
2283 case GTK_RESPONSE_CANCEL
:
2284 case GTK_RESPONSE_DELETE_EVENT
:
2287 fprintf(stderr
, "%s sent an unexpected response id (%d)\n",
2288 widget_id(GTK_BUILDABLE(dialog
)), response_id
);
2291 gtk_widget_hide(GTK_WIDGET(dialog
));
2293 try_generic_cmds(ud
);
2297 update_progress_bar(struct ui_data
*ud
)
2299 GtkProgressBar
*progressbar
= GTK_PROGRESS_BAR(ud
->obj
);
2303 if (eql(ud
->action
, "set_text"))
2304 gtk_progress_bar_set_text(progressbar
, *(ud
->data
) == '\0' ? NULL
: ud
->data
);
2305 else if (eql(ud
->action
, "set_fraction") &&
2306 sscanf(ud
->data
, "%lf %c", &frac
, &dummy
) == 1)
2307 gtk_progress_bar_set_fraction(progressbar
, frac
);
2309 try_generic_cmds(ud
);
2313 update_scale(struct ui_data
*ud
)
2315 GtkRange
*range
= GTK_RANGE(ud
->obj
);
2319 if (eql(ud
->action
, "set_value") && sscanf(ud
->data
, "%lf %c", &val1
, &dummy
) == 1)
2320 gtk_range_set_value(range
, val1
);
2321 else if (eql(ud
->action
, "set_fill_level") &&
2322 sscanf(ud
->data
, "%lf %c", &val1
, &dummy
) == 1) {
2323 gtk_range_set_fill_level(range
, val1
);
2324 gtk_range_set_show_fill_level(range
, TRUE
);
2325 } else if (eql(ud
->action
, "set_fill_level") &&
2326 sscanf(ud
->data
, " %c", &dummy
) < 1)
2327 gtk_range_set_show_fill_level(range
, FALSE
);
2328 else if (eql(ud
->action
, "set_range") &&
2329 sscanf(ud
->data
, "%lf %lf %c", &val1
, &val2
, &dummy
) == 2)
2330 gtk_range_set_range(range
, val1
, val2
);
2331 else if (eql(ud
->action
, "set_increments") &&
2332 sscanf(ud
->data
, "%lf %lf %c", &val1
, &val2
, &dummy
) == 2)
2333 gtk_range_set_increments(range
, val1
, val2
);
2335 try_generic_cmds(ud
);
2339 update_scrolled_window(struct ui_data
*ud
)
2341 GtkScrolledWindow
*window
= GTK_SCROLLED_WINDOW(ud
->obj
);
2342 GtkAdjustment
*hadj
= gtk_scrolled_window_get_hadjustment(window
);
2343 GtkAdjustment
*vadj
= gtk_scrolled_window_get_vadjustment(window
);
2347 if (eql(ud
->action
, "hscroll") && sscanf(ud
->data
, "%lf %c", &d0
, &dummy
) == 1)
2348 gtk_adjustment_set_value(hadj
, d0
);
2349 else if (eql(ud
->action
, "vscroll") && sscanf(ud
->data
, "%lf %c", &d0
, &dummy
) == 1)
2350 gtk_adjustment_set_value(vadj
, d0
);
2351 else if (eql(ud
->action
, "hscroll_to_range") &&
2352 sscanf(ud
->data
, "%lf %lf %c", &d0
, &d1
, &dummy
) == 2)
2353 gtk_adjustment_clamp_page(hadj
, d0
, d1
);
2354 else if (eql(ud
->action
, "vscroll_to_range") &&
2355 sscanf(ud
->data
, "%lf %lf %c", &d0
, &d1
, &dummy
) == 2)
2356 gtk_adjustment_clamp_page(vadj
, d0
, d1
);
2358 try_generic_cmds(ud
);
2362 update_socket(struct ui_data
*ud
)
2364 GtkSocket
*socket
= GTK_SOCKET(ud
->obj
);
2366 char str
[BUFLEN
], dummy
;
2368 if (eql(ud
->action
, "id") && sscanf(ud
->data
, " %c", &dummy
) < 1) {
2369 id
= gtk_socket_get_id(socket
);
2370 snprintf(str
, BUFLEN
, "%lu", id
);
2371 send_msg(ud
->args
->fout
, GTK_BUILDABLE(socket
), "id", str
, NULL
);
2373 try_generic_cmds(ud
);
2377 update_spin_button(struct ui_data
*ud
)
2379 GtkSpinButton
*spinbutton
= GTK_SPIN_BUTTON(ud
->obj
);
2383 if (eql(ud
->action
, "set_text") && /* TODO: rename to "set_value" */
2384 sscanf(ud
->data
, "%lf %c", &val1
, &dummy
) == 1)
2385 gtk_spin_button_set_value(spinbutton
, val1
);
2386 else if (eql(ud
->action
, "set_range") &&
2387 sscanf(ud
->data
, "%lf %lf %c", &val1
, &val2
, &dummy
) == 2)
2388 gtk_spin_button_set_range(spinbutton
, val1
, val2
);
2389 else if (eql(ud
->action
, "set_increments") &&
2390 sscanf(ud
->data
, "%lf %lf %c", &val1
, &val2
, &dummy
) == 2)
2391 gtk_spin_button_set_increments(spinbutton
, val1
, val2
);
2393 try_generic_cmds(ud
);
2397 update_spinner(struct ui_data
*ud
)
2399 GtkSpinner
*spinner
= GTK_SPINNER(ud
->obj
);
2402 if (eql(ud
->action
, "start") && sscanf(ud
->data
, " %c", &dummy
) < 1)
2403 gtk_spinner_start(spinner
);
2404 else if (eql(ud
->action
, "stop") && sscanf(ud
->data
, " %c", &dummy
) < 1)
2405 gtk_spinner_stop(spinner
);
2407 try_generic_cmds(ud
);
2411 update_statusbar(struct ui_data
*ud
)
2413 GtkStatusbar
*statusbar
= GTK_STATUSBAR(ud
->obj
);
2414 char *ctx_msg
, dummy
;
2415 const char *status_msg
;
2418 /* TODO: remove "push", "pop", "remove_all"; rename "push_id" to "push", etc. */
2419 if ((ctx_msg
= malloc(strlen(ud
->data
) + 1)) == NULL
)
2421 t
= sscanf(ud
->data
, "%s %n%c", ctx_msg
, &ctx_len
, &dummy
);
2422 status_msg
= ud
->data
+ ctx_len
;
2423 if (eql(ud
->action
, "push"))
2424 gtk_statusbar_push(statusbar
,
2425 gtk_statusbar_get_context_id(statusbar
, "0"),
2427 else if (eql(ud
->action
, "push_id") && t
>= 1)
2428 gtk_statusbar_push(statusbar
,
2429 gtk_statusbar_get_context_id(statusbar
, ctx_msg
),
2431 else if (eql(ud
->action
, "pop") && t
< 1)
2432 gtk_statusbar_pop(statusbar
,
2433 gtk_statusbar_get_context_id(statusbar
, "0"));
2434 else if (eql(ud
->action
, "pop_id") && t
== 1)
2435 gtk_statusbar_pop(statusbar
,
2436 gtk_statusbar_get_context_id(statusbar
, ctx_msg
));
2437 else if (eql(ud
->action
, "remove_all") && t
< 1)
2438 gtk_statusbar_remove_all(statusbar
,
2439 gtk_statusbar_get_context_id(statusbar
, "0"));
2440 else if (eql(ud
->action
, "remove_all_id") && t
== 1)
2441 gtk_statusbar_remove_all(statusbar
,
2442 gtk_statusbar_get_context_id(statusbar
, ctx_msg
));
2444 try_generic_cmds(ud
);
2449 update_switch(struct ui_data
*ud
)
2454 if (eql(ud
->action
, "set_active") &&
2455 sscanf(ud
->data
, "%u %c", &val
, &dummy
) == 1 && val
< 2)
2456 gtk_switch_set_active(GTK_SWITCH(ud
->obj
), val
);
2458 try_generic_cmds(ud
);
2462 update_text_view(struct ui_data
*ud
)
2465 GtkTextView
*view
= GTK_TEXT_VIEW(ud
->obj
);
2466 GtkTextBuffer
*textbuf
= gtk_text_view_get_buffer(view
);
2471 if (eql(ud
->action
, "set_text"))
2472 gtk_text_buffer_set_text(textbuf
, ud
->data
, -1);
2473 else if (eql(ud
->action
, "delete") && sscanf(ud
->data
, " %c", &dummy
) < 1) {
2474 gtk_text_buffer_get_bounds(textbuf
, &a
, &b
);
2475 gtk_text_buffer_delete(textbuf
, &a
, &b
);
2476 } else if (eql(ud
->action
, "insert_at_cursor"))
2477 gtk_text_buffer_insert_at_cursor(textbuf
, ud
->data
, -1);
2478 else if (eql(ud
->action
, "place_cursor") && eql(ud
->data
, "end")) {
2479 gtk_text_buffer_get_end_iter(textbuf
, &a
);
2480 gtk_text_buffer_place_cursor(textbuf
, &a
);
2481 } else if (eql(ud
->action
, "place_cursor") &&
2482 sscanf(ud
->data
, "%d %c", &val
, &dummy
) == 1) {
2483 gtk_text_buffer_get_iter_at_offset(textbuf
, &a
, val
);
2484 gtk_text_buffer_place_cursor(textbuf
, &a
);
2485 } else if (eql(ud
->action
, "place_cursor_at_line") &&
2486 sscanf(ud
->data
, "%d %c", &val
, &dummy
) == 1) {
2487 gtk_text_buffer_get_iter_at_line(textbuf
, &a
, val
);
2488 gtk_text_buffer_place_cursor(textbuf
, &a
);
2489 } else if (eql(ud
->action
, "scroll_to_cursor") &&
2490 sscanf(ud
->data
, " %c", &dummy
) < 1)
2491 gtk_text_view_scroll_to_mark(view
, gtk_text_buffer_get_insert(textbuf
),
2493 else if (eql(ud
->action
, "save") && ud
->data
!= NULL
&&
2494 (sv
= fopen(ud
->data
, "w")) != NULL
) {
2495 gtk_text_buffer_get_bounds(textbuf
, &a
, &b
);
2496 send_msg(sv
, GTK_BUILDABLE(view
), "insert_at_cursor",
2497 gtk_text_buffer_get_text(textbuf
, &a
, &b
, TRUE
), NULL
);
2500 try_generic_cmds(ud
);
2504 update_toggle_button(struct ui_data
*ud
)
2509 if (eql(ud
->action
, "set_label"))
2510 gtk_button_set_label(GTK_BUTTON(ud
->obj
), ud
->data
);
2511 else if (eql(ud
->action
, "set_active") &&
2512 sscanf(ud
->data
, "%u %c", &val
, &dummy
) == 1 && val
< 2)
2513 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ud
->obj
), val
);
2515 try_generic_cmds(ud
);
2519 * update_tree_view(), which runs inside gtk_main(), needs a few
2524 * Check if s is a valid string representation of a GtkTreePath
2527 is_path_string(char *s
)
2530 strlen(s
) == strspn(s
, ":0123456789") &&
2531 strstr(s
, "::") == NULL
&&
2532 strcspn(s
, ":") > 0;
2536 tree_model_insert_before(GtkTreeModel
*model
, GtkTreeIter
*iter
,
2537 GtkTreeIter
*parent
, GtkTreeIter
*sibling
)
2539 if (GTK_IS_TREE_STORE(model
))
2540 gtk_tree_store_insert_before(GTK_TREE_STORE(model
),
2541 iter
, parent
, sibling
);
2542 else if (GTK_IS_LIST_STORE(model
))
2543 gtk_list_store_insert_before(GTK_LIST_STORE(model
),
2550 tree_model_insert_after(GtkTreeModel
*model
, GtkTreeIter
*iter
,
2551 GtkTreeIter
*parent
, GtkTreeIter
*sibling
)
2553 if (GTK_IS_TREE_STORE(model
))
2554 gtk_tree_store_insert_after(GTK_TREE_STORE(model
),
2555 iter
, parent
, sibling
);
2556 else if (GTK_IS_LIST_STORE(model
))
2557 gtk_list_store_insert_after(GTK_LIST_STORE(model
),
2564 tree_model_move_before(GtkTreeModel
*model
, GtkTreeIter
*iter
,
2565 GtkTreeIter
*position
)
2567 if (GTK_IS_TREE_STORE(model
))
2568 gtk_tree_store_move_before(GTK_TREE_STORE(model
), iter
, position
);
2569 else if (GTK_IS_LIST_STORE(model
))
2570 gtk_list_store_move_before(GTK_LIST_STORE(model
), iter
, position
);
2576 tree_model_remove(GtkTreeModel
*model
, GtkTreeIter
*iter
)
2578 if (GTK_IS_TREE_STORE(model
))
2579 gtk_tree_store_remove(GTK_TREE_STORE(model
), iter
);
2580 else if (GTK_IS_LIST_STORE(model
))
2581 gtk_list_store_remove(GTK_LIST_STORE(model
), iter
);
2587 tree_model_clear(GtkTreeModel
*model
)
2589 if (GTK_IS_TREE_STORE(model
))
2590 gtk_tree_store_clear(GTK_TREE_STORE(model
));
2591 else if (GTK_IS_LIST_STORE(model
))
2592 gtk_list_store_clear(GTK_LIST_STORE(model
));
2598 tree_model_set(GtkTreeModel
*model
, GtkTreeIter
*iter
, ...)
2603 if (GTK_IS_TREE_STORE(model
))
2604 gtk_tree_store_set_valist(GTK_TREE_STORE(model
), iter
, ap
);
2605 else if (GTK_IS_LIST_STORE(model
))
2606 gtk_list_store_set_valist(GTK_LIST_STORE(model
), iter
, ap
);
2613 * Create an empty row at path if it doesn't yet exist. Create older
2614 * siblings and parents as necessary.
2617 create_subtree(GtkTreeModel
*model
, GtkTreePath
*path
, GtkTreeIter
*iter
)
2619 GtkTreeIter iter_1
; /* iter's predecessor */
2620 GtkTreePath
*path_1
; /* path's predecessor */
2622 if (gtk_tree_path_get_depth(path
) > 0 &&
2623 gtk_tree_model_get_iter(model
, iter
, path
))
2625 path_1
= gtk_tree_path_copy(path
);
2626 if (gtk_tree_path_prev(path_1
)) { /* need an older sibling */
2627 create_subtree(model
, path_1
, iter
);
2629 tree_model_insert_after(model
, iter
, NULL
, &iter_1
);
2630 } else if (gtk_tree_path_up(path_1
)) { /* need a parent */
2631 create_subtree(model
, path_1
, iter
);
2632 if (gtk_tree_path_get_depth(path_1
) == 0)
2633 /* first toplevel row */
2634 tree_model_insert_after(model
, iter
, NULL
, NULL
);
2635 else { /* first row in a lower level */
2637 tree_model_insert_after(model
, iter
, &iter_1
, NULL
);
2639 } /* neither prev nor up mean we're at the root of an empty tree */
2640 gtk_tree_path_free(path_1
);
2644 set_tree_view_cell(GtkTreeModel
*model
, GtkTreeIter
*iter
,
2645 const char *path_s
, int col
, const char *new_text
)
2647 GType col_type
= gtk_tree_model_get_column_type(model
, col
);
2654 path
= gtk_tree_path_new_from_string(path_s
);
2656 case G_TYPE_BOOLEAN
:
2663 if (new_text
!= NULL
&&
2664 sscanf(new_text
, "%lld %c", &n
, &dummy
) == 1) {
2665 create_subtree(model
, path
, iter
);
2666 tree_model_set(model
, iter
, col
, n
, -1);
2672 if (new_text
!= NULL
&&
2673 sscanf(new_text
, "%lf %c", &d
, &dummy
) == 1) {
2674 create_subtree(model
, path
, iter
);
2675 tree_model_set(model
, iter
, col
, d
, -1);
2680 create_subtree(model
, path
, iter
);
2681 tree_model_set(model
, iter
, col
, new_text
, -1);
2685 fprintf(stderr
, "column %d: %s not implemented\n",
2686 col
, g_type_name(col_type
));
2690 gtk_tree_path_free(path
);
2695 tree_view_set_cursor(GtkTreeView
*view
, GtkTreePath
*path
, GtkTreeViewColumn
*col
)
2697 /* GTK+ 3.14 requires this. For 3.18, path = NULL */
2698 /* is just fine and this function need not exist. */
2700 path
= gtk_tree_path_new();
2701 gtk_tree_view_set_cursor(view
, path
, col
, false);
2705 update_tree_view(struct ui_data
*ud
)
2707 GtkTreeView
*view
= GTK_TREE_VIEW(ud
->obj
);
2708 GtkTreeIter iter0
, iter1
;
2709 GtkTreeModel
*model
= gtk_tree_view_get_model(view
);
2710 GtkTreePath
*path
= NULL
;
2711 GtkTreeSelection
*sel
= gtk_tree_view_get_selection(view
);
2712 bool iter0_valid
, iter1_valid
;
2713 char *tokens
, *arg0
, *arg1
, *arg2
;
2714 int col
= -1; /* invalid column number */
2717 if (!GTK_IS_LIST_STORE(model
) && !GTK_IS_TREE_STORE(model
))
2719 fprintf(stderr
, "missing model/");
2720 ign_cmd(ud
->type
, ud
->cmd
);
2723 if ((tokens
= malloc(strlen(ud
->data
) + 1)) == NULL
)
2725 strcpy(tokens
, ud
->data
);
2726 arg0
= strtok(tokens
, WHITESPACE
);
2727 arg1
= strtok(NULL
, WHITESPACE
);
2728 arg2
= strtok(NULL
, "");
2729 iter0_valid
= is_path_string(arg0
) &&
2730 gtk_tree_model_get_iter_from_string(model
, &iter0
, arg0
);
2731 iter1_valid
= is_path_string(arg1
) &&
2732 gtk_tree_model_get_iter_from_string(model
, &iter1
, arg1
);
2733 if (is_path_string(arg1
))
2734 col
= strtol(arg1
, NULL
, 10);
2735 if (eql(ud
->action
, "set") &&
2737 col
< gtk_tree_model_get_n_columns(model
) &&
2738 is_path_string(arg0
)) {
2739 if (set_tree_view_cell(model
, &iter0
, arg0
, col
, arg2
) == false)
2740 ign_cmd(ud
->type
, ud
->cmd
);
2741 } else if (eql(ud
->action
, "scroll") && iter0_valid
&& iter1_valid
&&
2743 path
= gtk_tree_path_new_from_string(arg0
);
2744 gtk_tree_view_scroll_to_cell (view
,
2746 gtk_tree_view_get_column(view
, col
),
2748 } else if (eql(ud
->action
, "expand") && iter0_valid
&& arg1
== NULL
) {
2749 path
= gtk_tree_path_new_from_string(arg0
);
2750 gtk_tree_view_expand_row(view
, path
, false);
2751 } else if (eql(ud
->action
, "expand_all") && iter0_valid
&& arg1
== NULL
) {
2752 path
= gtk_tree_path_new_from_string(arg0
);
2753 gtk_tree_view_expand_row(view
, path
, true);
2754 } else if (eql(ud
->action
, "expand_all") && arg0
== NULL
)
2755 gtk_tree_view_expand_all(view
);
2756 else if (eql(ud
->action
, "collapse") && iter0_valid
&& arg1
== NULL
) {
2757 path
= gtk_tree_path_new_from_string(arg0
);
2758 gtk_tree_view_collapse_row(view
, path
);
2759 } else if (eql(ud
->action
, "collapse") && arg0
== NULL
)
2760 gtk_tree_view_collapse_all(view
);
2761 else if (eql(ud
->action
, "set_cursor") && iter0_valid
&& arg1
== NULL
) {
2762 path
= gtk_tree_path_new_from_string(arg0
);
2763 tree_view_set_cursor(view
, path
, NULL
);
2764 } else if (eql(ud
->action
, "set_cursor") && arg0
== NULL
) {
2765 tree_view_set_cursor(view
, NULL
, NULL
);
2766 gtk_tree_selection_unselect_all(sel
);
2767 } else if (eql(ud
->action
, "insert_row") &&
2768 eql(arg0
, "end") && arg1
== NULL
)
2769 tree_model_insert_before(model
, &iter1
, NULL
, NULL
);
2770 else if (eql(ud
->action
, "insert_row") && iter0_valid
&&
2771 eql(arg1
, "as_child") && arg2
== NULL
)
2772 tree_model_insert_after(model
, &iter1
, &iter0
, NULL
);
2773 else if (eql(ud
->action
, "insert_row") && iter0_valid
&& arg1
== NULL
)
2774 tree_model_insert_before(model
, &iter1
, NULL
, &iter0
);
2775 else if (eql(ud
->action
, "move_row") && iter0_valid
&&
2776 eql(arg1
, "end") && arg2
== NULL
)
2777 tree_model_move_before(model
, &iter0
, NULL
);
2778 else if (eql(ud
->action
, "move_row") && iter0_valid
&& iter1_valid
&& arg2
== NULL
)
2779 tree_model_move_before(model
, &iter0
, &iter1
);
2780 else if (eql(ud
->action
, "remove_row") && iter0_valid
&& arg1
== NULL
)
2781 tree_model_remove(model
, &iter0
);
2782 else if (eql(ud
->action
, "clear") && arg0
== NULL
) {
2783 tree_view_set_cursor(view
, NULL
, NULL
);
2784 gtk_tree_selection_unselect_all(sel
);
2785 tree_model_clear(model
);
2786 } else if (eql(ud
->action
, "block") && arg0
!= NULL
) {
2787 ud
->obj
=G_OBJECT(sel
);
2789 } else if (eql(ud
->action
, "save") && arg0
!= NULL
&&
2790 (ar
.fout
= fopen(arg0
, "w")) != NULL
) {
2792 gtk_tree_model_foreach(model
,
2793 (GtkTreeModelForeachFunc
) save_tree_row_msg
,
2797 try_generic_cmds(ud
);
2799 gtk_tree_path_free(path
);
2803 update_window(struct ui_data
*ud
)
2805 if (!update_class_window(ud
))
2806 try_generic_cmds(ud
);
2810 * The final UI update. Runs inside gtk_main().
2813 main_quit(struct ui_data
*ud
)
2817 if (sscanf(ud
->data
, " %c", &dummy
) < 1)
2820 try_generic_cmds(ud
);
2824 * Don't update anything; just complain from inside gtk_main()
2827 complain(struct ui_data
*ud
)
2829 ign_cmd(ud
->type
, ud
->cmd
);
2833 * Parse command pointed to by ud, and act on ui accordingly. Runs
2834 * once per command inside gtk_main().
2837 update_ui(struct ui_data
*ud
)
2839 char *lc
= lc_numeric();
2842 free(ud
->cmd_tokens
);
2845 lc_numeric_free(lc
);
2846 return G_SOURCE_REMOVE
;
2850 * Keep track of loading files to avoid recursive loading of the same
2851 * file. If filename = NULL, forget the most recently remembered file.
2854 remember_loading_file(char *filename
)
2856 static char *filenames
[BUFLEN
];
2857 static size_t latest
= 0;
2860 if (filename
== NULL
) { /* pop */
2866 for (i
= 1; i
<= latest
; i
++)
2867 if (eql(filename
, filenames
[i
]))
2869 if (latest
> BUFLEN
-2)
2871 filenames
[++latest
] = filename
;
2877 * Read lines from stream cmd and perform appropriate actions on the
2878 * GUI. Runs inside receiver thread.
2881 digest_cmd(struct info
*ar
)
2883 static int recursion
= -1; /* > 0 means this is a recursive call */
2887 FILE *cmd
= ar
->fin
;
2888 struct ui_data
*ud
= NULL
;
2889 char first_char
= '\0';
2890 char *id
; /* widget id */
2891 size_t msg_size
= 32;
2892 int id_start
= 0, id_end
= 0;
2893 int action_start
= 0, action_end
= 0;
2898 if ((ud
= malloc(sizeof(*ud
))) == NULL
)
2900 if ((ud
->cmd
= malloc(msg_size
)) == NULL
)
2903 ud
->type
= G_TYPE_INVALID
;
2904 pthread_testcancel();
2906 log_msg(ar
->flog
, NULL
);
2907 data_start
= read_buf(cmd
, &ud
->cmd
, &msg_size
);
2909 log_msg(ar
->flog
, ud
->cmd
);
2910 if ((ud
->cmd_tokens
= malloc(strlen(ud
->cmd
) + 1)) == NULL
)
2912 sscanf(ud
->cmd
, " %c", &first_char
);
2913 if (data_start
== 0 || /* empty line */
2914 first_char
== '#') { /* comment */
2915 ud
->fn
= update_nothing
;
2918 strcpy(ud
->cmd_tokens
, ud
->cmd
);
2919 sscanf(ud
->cmd_tokens
,
2920 " %n%*[0-9a-zA-Z_-]%n:%n%*[0-9a-zA-Z_]%n%*1[ \t]%n",
2921 &id_start
, &id_end
, &action_start
, &action_end
, &data_start
);
2922 ud
->cmd_tokens
[id_end
] = ud
->cmd_tokens
[action_end
] = '\0';
2923 id
= ud
->cmd_tokens
+ id_start
;
2924 ud
->action
= ud
->cmd_tokens
+ action_start
;
2925 ud
->data
= ud
->cmd_tokens
+ data_start
;
2926 if (eql(ud
->action
, "main_quit")) {
2930 if (eql(ud
->action
, "load") && strlen(ud
->data
) > 0 &&
2931 remember_loading_file(ud
->data
)) {
2932 struct info a
= *ar
;
2934 if ((a
.fin
= fopen(ud
->data
, "r")) != NULL
) {
2937 ud
->fn
= update_nothing
;
2940 remember_loading_file(NULL
);
2943 if ((ud
->obj
= (gtk_builder_get_object(ar
->builder
, id
))) == NULL
) {
2947 ud
->type
= G_TYPE_FROM_INSTANCE(ud
->obj
);
2948 if (ud
->type
== GTK_TYPE_DRAWING_AREA
)
2949 ud
->fn
= update_drawing_area
;
2950 else if (ud
->type
== GTK_TYPE_TREE_VIEW
)
2951 ud
->fn
= update_tree_view
;
2952 else if (ud
->type
== GTK_TYPE_COMBO_BOX_TEXT
)
2953 ud
->fn
= update_combo_box_text
;
2954 else if (ud
->type
== GTK_TYPE_LABEL
)
2955 ud
->fn
= update_label
;
2956 else if (ud
->type
== GTK_TYPE_IMAGE
)
2957 ud
->fn
= update_image
;
2958 else if (ud
->type
== GTK_TYPE_TEXT_VIEW
)
2959 ud
->fn
= update_text_view
;
2960 else if (ud
->type
== GTK_TYPE_NOTEBOOK
)
2961 ud
->fn
= update_notebook
;
2962 else if (ud
->type
== GTK_TYPE_EXPANDER
)
2963 ud
->fn
= update_expander
;
2964 else if (ud
->type
== GTK_TYPE_FRAME
||
2965 ud
->type
== GTK_TYPE_ASPECT_FRAME
)
2966 ud
->fn
= update_frame
;
2967 else if (ud
->type
== GTK_TYPE_SCROLLED_WINDOW
)
2968 ud
->fn
= update_scrolled_window
;
2969 else if (ud
->type
== GTK_TYPE_LINK_BUTTON
)
2970 ud
->fn
= update_link_button
;
2971 else if (ud
->type
== GTK_TYPE_BUTTON
)
2972 ud
->fn
= update_button
;
2973 else if (ud
->type
== GTK_TYPE_MENU
)
2974 ud
->fn
= update_menu
;
2975 else if (ud
->type
== GTK_TYPE_MENU_ITEM
)
2976 ud
->fn
= update_menu_item
;
2977 else if (ud
->type
== GTK_TYPE_FILE_CHOOSER_DIALOG
)
2978 ud
->fn
= update_file_chooser_dialog
;
2979 else if (ud
->type
== GTK_TYPE_FILE_CHOOSER_BUTTON
)
2980 ud
->fn
= update_file_chooser_button
;
2981 else if (ud
->type
== GTK_TYPE_COLOR_BUTTON
)
2982 ud
->fn
= update_color_button
;
2983 else if (ud
->type
== GTK_TYPE_FONT_BUTTON
)
2984 ud
->fn
= update_font_button
;
2985 else if (ud
->type
== GTK_TYPE_PRINT_UNIX_DIALOG
)
2986 ud
->fn
= update_print_dialog
;
2987 else if (ud
->type
== GTK_TYPE_SWITCH
)
2988 ud
->fn
= update_switch
;
2989 else if (ud
->type
== GTK_TYPE_TOGGLE_BUTTON
||
2990 ud
->type
== GTK_TYPE_RADIO_BUTTON
||
2991 ud
->type
== GTK_TYPE_CHECK_BUTTON
)
2992 ud
->fn
= update_toggle_button
;
2993 else if (ud
->type
== GTK_TYPE_ENTRY
)
2994 ud
->fn
= update_entry
;
2995 else if (ud
->type
== GTK_TYPE_SPIN_BUTTON
)
2996 ud
->fn
= update_spin_button
;
2997 else if (ud
->type
== GTK_TYPE_SCALE
)
2998 ud
->fn
= update_scale
;
2999 else if (ud
->type
== GTK_TYPE_PROGRESS_BAR
)
3000 ud
->fn
= update_progress_bar
;
3001 else if (ud
->type
== GTK_TYPE_SPINNER
)
3002 ud
->fn
= update_spinner
;
3003 else if (ud
->type
== GTK_TYPE_STATUSBAR
)
3004 ud
->fn
= update_statusbar
;
3005 else if (ud
->type
== GTK_TYPE_CALENDAR
)
3006 ud
->fn
= update_calendar
;
3007 else if (ud
->type
== GTK_TYPE_SOCKET
)
3008 ud
->fn
= update_socket
;
3009 else if (ud
->type
== GTK_TYPE_WINDOW
||
3010 ud
->type
== GTK_TYPE_DIALOG
)
3011 ud
->fn
= update_window
;
3013 ud
->fn
= try_generic_cmds
;
3015 pthread_testcancel();
3016 gdk_threads_add_timeout(0, (GSourceFunc
) update_ui
, ud
);
3024 * ============================================================
3026 * ============================================================
3030 * Return the first string xpath obtains from ui_file.
3031 * xmlFree(string) must be called when done
3034 xpath1(xmlChar
*xpath
, const char *ui_file
)
3037 xmlDocPtr doc
= NULL
;
3038 xmlNodeSetPtr nodes
= NULL
;
3039 xmlXPathContextPtr ctx
= NULL
;
3040 xmlXPathObjectPtr xpath_obj
= NULL
;
3042 if ((doc
= xmlParseFile(ui_file
)) == NULL
)
3044 if ((ctx
= xmlXPathNewContext(doc
)) == NULL
)
3046 if ((xpath_obj
= xmlXPathEvalExpression(xpath
, ctx
)) == NULL
)
3048 if ((nodes
= xpath_obj
->nodesetval
) != NULL
&& nodes
->nodeNr
> 0)
3049 r
= xmlNodeGetContent(nodes
->nodeTab
[0]);
3050 xmlXPathFreeObject(xpath_obj
);
3052 xmlXPathFreeContext(ctx
);
3060 * Attach key "col_number" to renderer. Associate "col_number" with
3061 * the corresponding column number in the underlying model.
3062 * Due to what looks like a gap in the GTK API, renderer id and column
3063 * number are taken directly from the XML .ui file.
3066 tree_view_column_get_renderer_column(GtkBuilder
*builder
, const char *ui_file
,
3067 GtkTreeViewColumn
*t_col
, int n
,
3068 GtkCellRenderer
**rnd
)
3071 char *xp_bas1
= "//object[@class=\"GtkTreeViewColumn\" and @id=\"";
3072 char *xp_bas2
= "\"]/child[";
3073 char *xp_bas3
= "]/object[@class=\"GtkCellRendererText\""
3074 " or @class=\"GtkCellRendererToggle\"]/";
3075 char *xp_rnd_id
= "@id";
3076 char *xp_text_col
= "../attributes/attribute[@name=\"text\""
3077 " or @name=\"active\"]/text()";
3078 const char *tree_col_id
= widget_id(GTK_BUILDABLE(t_col
));
3079 size_t xp_rnd_nam_len
, xp_mod_col_len
;
3080 size_t xp_n_len
= 3; /* Big Enough (TM) */
3081 xmlChar
*xp_rnd_nam
= NULL
, *xp_mod_col
= NULL
;
3082 xmlChar
*rnd_nam
= NULL
, *mod_col
= NULL
;
3084 /* find name of nth cell renderer under the GtkTreeViewColumn */
3086 xp_rnd_nam_len
= strlen(xp_bas1
) + strlen(tree_col_id
) +
3087 strlen(xp_bas2
) + xp_n_len
+ strlen(xp_bas3
) +
3088 strlen(xp_rnd_id
) + sizeof('\0');
3089 if ((xp_rnd_nam
= malloc(xp_rnd_nam_len
)) == NULL
)
3091 snprintf((char *) xp_rnd_nam
, xp_rnd_nam_len
, "%s%s%s%d%s%s",
3092 xp_bas1
, tree_col_id
, xp_bas2
, n
,
3093 xp_bas3
, xp_rnd_id
);
3094 rnd_nam
= xpath1(xp_rnd_nam
, ui_file
);
3095 /* find the model column that is attached to the nth cell */
3096 /* renderer under GtkTreeViewColumn tree_col_id */
3097 xp_mod_col_len
= strlen(xp_bas1
) + strlen(tree_col_id
) +
3098 strlen(xp_bas2
) + xp_n_len
+ strlen(xp_bas3
) +
3099 strlen(xp_text_col
) + sizeof('\0');
3100 if ((xp_mod_col
= malloc(xp_mod_col_len
)) == NULL
)
3102 snprintf((char *) xp_mod_col
, xp_mod_col_len
, "%s%s%s%d%s%s",
3103 xp_bas1
, tree_col_id
, xp_bas2
, n
, xp_bas3
, xp_text_col
);
3104 mod_col
= xpath1(xp_mod_col
, ui_file
);
3106 *rnd
= GTK_CELL_RENDERER(
3107 gtk_builder_get_object(builder
, (char *) rnd_nam
));
3109 g_object_set_data(G_OBJECT(*rnd
), "col_number",
3110 GINT_TO_POINTER(strtol((char *) mod_col
,
3123 * Callbacks that forward a modification of a tree view cell to the
3127 cb_tree_model_edit(GtkCellRenderer
*renderer
, const gchar
*path_s
,
3128 const gchar
*new_text
, struct info
*ar
)
3131 int col
= GPOINTER_TO_INT(g_object_get_data(G_OBJECT(renderer
),
3134 gtk_tree_model_get_iter_from_string(ar
->model
, &iter
, path_s
);
3135 set_tree_view_cell(ar
->model
, &iter
, path_s
, col
,
3137 send_tree_cell_msg_by(send_msg
, path_s
, &iter
, col
, ar
);
3141 cb_tree_model_toggle(GtkCellRenderer
*renderer
, gchar
*path_s
, struct info
*ar
)
3145 int col
= GPOINTER_TO_INT(g_object_get_data(G_OBJECT(renderer
),
3148 gtk_tree_model_get_iter_from_string(ar
->model
, &iter
, path_s
);
3149 gtk_tree_model_get(ar
->model
, &iter
, col
, &toggle_state
, -1);
3150 set_tree_view_cell(ar
->model
, &iter
, path_s
, col
,
3151 toggle_state
? "0" : "1");
3155 * Add new element containing id to the list of callback-handler ids
3156 * stored in obj's field named "signal_id"
3159 push_handler_id(gpointer
*obj
, unsigned int id
)
3161 struct handler_id
*prev_hid
, *hid
;
3163 prev_hid
= g_object_get_data(G_OBJECT(obj
), "signal-id");
3164 if ((hid
= malloc(sizeof(struct handler_id
))) == NULL
)
3166 hid
->next
= prev_hid
;
3168 hid
->blocked
= false;
3169 g_object_set_data(G_OBJECT(obj
), "signal-id", hid
);
3173 * Connect function cb to obj's widget signal sig, remembering the
3174 * handler id in a list in obj's field named "signal-id"
3177 sig_conn(gpointer
*obj
, char *sig
, GCallback cb
, struct info
*ar
)
3179 unsigned int handler_id
= g_signal_connect(obj
, sig
, cb
, ar
);
3181 push_handler_id(obj
, handler_id
);
3185 sig_conn_swapped(gpointer
*obj
, char *sig
, GCallback cb
, void *data
)
3187 unsigned int handler_id
= g_signal_connect_swapped(obj
, sig
, cb
, data
);
3189 push_handler_id(obj
, handler_id
);
3193 connect_widget_signals(gpointer
*obj
, struct info
*ar
)
3196 GType type
= G_TYPE_INVALID
;
3197 char *suffix
= NULL
;
3198 const char *w_id
= NULL
;
3201 type
= G_TYPE_FROM_INSTANCE(obj
);
3202 if (GTK_IS_BUILDABLE(obj
))
3203 w_id
= widget_id(GTK_BUILDABLE(obj
));
3204 if (type
== GTK_TYPE_TREE_VIEW_COLUMN
) {
3205 GList
*cells
= gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(obj
));
3206 GtkTreeViewColumn
*tv_col
= GTK_TREE_VIEW_COLUMN(obj
);
3207 GObject
*view
= G_OBJECT(
3208 gtk_tree_view_column_get_tree_view(tv_col
));
3209 unsigned int i
, n_cells
= g_list_length(cells
);
3212 sig_conn(obj
, "clicked", G_CALLBACK(cb_simple
), info_txt_new(o
, "clicked"));
3213 for (i
= 1; i
<= n_cells
; i
++) {
3214 GtkCellRenderer
*renderer
;
3215 gboolean editable
= FALSE
;
3217 if (!tree_view_column_get_renderer_column(ar
->builder
, ar
->txt
, tv_col
,
3220 if (GTK_IS_CELL_RENDERER_TEXT(renderer
)) {
3221 g_object_get(renderer
, "editable", &editable
, NULL
);
3223 GtkTreeModel
*model
= gtk_tree_view_get_model(GTK_TREE_VIEW(view
));
3225 g_signal_connect(renderer
, "edited",
3226 G_CALLBACK(cb_tree_model_edit
),
3227 info_obj_new(o
, view
, model
));
3229 } else if (GTK_IS_CELL_RENDERER_TOGGLE(renderer
)) {
3230 g_object_get(renderer
, "activatable", &editable
, NULL
);
3232 GtkTreeModel
*model
= gtk_tree_view_get_model(GTK_TREE_VIEW(view
));
3234 g_signal_connect(renderer
, "toggled",
3235 G_CALLBACK(cb_tree_model_toggle
),
3236 info_obj_new(o
, NULL
, model
));
3240 } else if (type
== GTK_TYPE_LINK_BUTTON
)
3241 sig_conn(obj
, "activate-link", G_CALLBACK(cb_simple
), info_txt_new(o
, "clicked"));
3242 else if (type
== GTK_TYPE_BUTTON
)
3243 /* Button associated with a GtkTextView. */
3244 if ((suffix
= strstr(w_id
, "_send_text")) != NULL
&&
3245 GTK_IS_TEXT_VIEW(obj2
= obj_sans_suffix(ar
->builder
, suffix
, w_id
)))
3246 sig_conn(obj
, "clicked", G_CALLBACK(cb_send_text
),
3247 info_obj_new(o
, G_OBJECT(gtk_text_view_get_buffer(GTK_TEXT_VIEW(obj2
))), NULL
));
3248 else if ((suffix
= strstr(w_id
, "_send_selection")) != NULL
&&
3249 GTK_IS_TEXT_VIEW(obj2
= obj_sans_suffix(ar
->builder
, suffix
, w_id
)))
3250 sig_conn(obj
, "clicked", G_CALLBACK(cb_send_text_selection
),
3251 info_obj_new(o
, G_OBJECT(gtk_text_view_get_buffer(GTK_TEXT_VIEW(obj2
))), NULL
));
3253 sig_conn(obj
, "clicked", G_CALLBACK(cb_simple
), info_txt_new(o
, "clicked"));
3254 /* Buttons associated with (and part of) a GtkDialog.
3255 * (We shun response ids which could be returned from
3256 * gtk_dialog_run() because that would require the
3257 * user to define those response ids in Glade,
3259 if ((suffix
= strstr(w_id
, "_cancel")) != NULL
&&
3260 GTK_IS_DIALOG(obj2
= obj_sans_suffix(ar
->builder
, suffix
, w_id
)))
3261 if (eql(widget_id(GTK_BUILDABLE(obj2
)), MAIN_WIN
))
3262 sig_conn_swapped(obj
, "clicked",
3263 G_CALLBACK(gtk_main_quit
), NULL
);
3265 sig_conn_swapped(obj
, "clicked",
3266 G_CALLBACK(gtk_widget_hide
), obj2
);
3267 else if ((suffix
= strstr(w_id
, "_ok")) != NULL
&&
3268 GTK_IS_DIALOG(obj2
= obj_sans_suffix(ar
->builder
, suffix
, w_id
))) {
3269 if (GTK_IS_FILE_CHOOSER_DIALOG(obj2
))
3270 sig_conn_swapped(obj
, "clicked",
3271 G_CALLBACK(cb_send_file_chooser_dialog_selection
),
3272 info_obj_new(o
, obj2
, NULL
));
3273 if (eql(widget_id(GTK_BUILDABLE(obj2
)), MAIN_WIN
))
3274 sig_conn_swapped(obj
, "clicked",
3275 G_CALLBACK(gtk_main_quit
), NULL
);
3277 sig_conn_swapped(obj
, "clicked",
3278 G_CALLBACK(gtk_widget_hide
), obj2
);
3279 } else if ((suffix
= strstr(w_id
, "_apply")) != NULL
&&
3280 GTK_IS_FILE_CHOOSER_DIALOG(obj2
= obj_sans_suffix(ar
->builder
, suffix
, w_id
)))
3281 sig_conn_swapped(obj
, "clicked",
3282 G_CALLBACK(cb_send_file_chooser_dialog_selection
),
3283 info_obj_new(o
, obj2
, NULL
));
3285 else if (GTK_IS_MENU_ITEM(obj
))
3286 if ((suffix
= strstr(w_id
, "_invoke")) != NULL
&&
3287 GTK_IS_DIALOG(obj2
= obj_sans_suffix(ar
->builder
, suffix
, w_id
)))
3288 sig_conn_swapped(obj
, "activate",
3289 G_CALLBACK(gtk_widget_show
), obj2
);
3291 sig_conn(obj
, "activate",
3292 G_CALLBACK(cb_menu_item
), info_txt_new(o
, "active"));
3293 else if (GTK_IS_WINDOW(obj
)) {
3294 sig_conn(obj
, "delete-event",
3295 G_CALLBACK(cb_event_simple
), info_txt_new(o
, "closed"));
3296 if (eql(w_id
, MAIN_WIN
))
3297 sig_conn_swapped(obj
, "delete-event",
3298 G_CALLBACK(gtk_main_quit
), NULL
);
3300 sig_conn(obj
, "delete-event",
3301 G_CALLBACK(gtk_widget_hide_on_delete
), NULL
);
3302 } else if (type
== GTK_TYPE_FILE_CHOOSER_BUTTON
)
3303 sig_conn(obj
, "file-set",
3304 G_CALLBACK(cb_file_chooser_button
), info_txt_new(o
, "file"));
3305 else if (type
== GTK_TYPE_COLOR_BUTTON
)
3306 sig_conn(obj
, "color-set",
3307 G_CALLBACK(cb_color_button
), info_txt_new(o
, "color"));
3308 else if (type
== GTK_TYPE_FONT_BUTTON
)
3309 sig_conn(obj
, "font-set",
3310 G_CALLBACK(cb_font_button
), info_txt_new(o
, "font"));
3311 else if (type
== GTK_TYPE_SWITCH
)
3312 sig_conn(obj
, "notify::active",
3313 G_CALLBACK(cb_switch
), info_txt_new(o
, NULL
));
3314 else if (type
== GTK_TYPE_TOGGLE_BUTTON
||
3315 type
== GTK_TYPE_RADIO_BUTTON
||
3316 type
== GTK_TYPE_CHECK_BUTTON
)
3317 sig_conn(obj
, "toggled",
3318 G_CALLBACK(cb_toggle_button
), info_txt_new(o
, NULL
));
3319 else if (type
== GTK_TYPE_ENTRY
)
3320 sig_conn(obj
, "changed",
3321 G_CALLBACK(cb_editable
), info_txt_new(o
, "text"));
3322 else if (type
== GTK_TYPE_SPIN_BUTTON
)
3323 sig_conn(obj
, "value_changed",
3324 G_CALLBACK(cb_spin_button
), info_txt_new(o
, "text")); /* TODO: rename to "value" */
3325 else if (type
== GTK_TYPE_SCALE
)
3326 sig_conn(obj
, "value-changed",
3327 G_CALLBACK(cb_range
), info_txt_new(o
, "value"));
3328 else if (type
== GTK_TYPE_CALENDAR
) {
3329 sig_conn(obj
, "day-selected-double-click",
3330 G_CALLBACK(cb_calendar
), info_txt_new(o
, "doubleclicked"));
3331 sig_conn(obj
, "day-selected",
3332 G_CALLBACK(cb_calendar
), info_txt_new(o
, "clicked"));
3333 } else if (type
== GTK_TYPE_TREE_SELECTION
)
3334 sig_conn(obj
, "changed",
3335 G_CALLBACK(cb_tree_selection
), info_txt_new(o
, "clicked"));
3336 else if (type
== GTK_TYPE_SOCKET
) {
3337 sig_conn(obj
, "plug-added",
3338 G_CALLBACK(cb_simple
), info_txt_new(o
, "plug-added"));
3339 sig_conn(obj
, "plug-removed",
3340 G_CALLBACK(cb_simple
), info_txt_new(o
, "plug-removed"));
3341 /* TODO: rename to plug_added, plug_removed */
3342 } else if (type
== GTK_TYPE_DRAWING_AREA
)
3343 sig_conn(obj
, "draw", G_CALLBACK(cb_draw
), NULL
);
3344 else if (type
== GTK_TYPE_EVENT_BOX
) {
3345 gtk_widget_set_can_focus(GTK_WIDGET(obj
), true);
3346 sig_conn(obj
, "button-press-event",
3347 G_CALLBACK(cb_event_box_button
),
3348 info_txt_new(o
, "button_press"));
3349 sig_conn(obj
, "button-release-event",
3350 G_CALLBACK(cb_event_box_button
),
3351 info_txt_new(o
, "button_release"));
3352 sig_conn(obj
, "motion-notify-event",
3353 G_CALLBACK(cb_event_box_motion
),
3354 info_txt_new(o
, "motion"));
3355 sig_conn(obj
, "key-press-event",
3356 G_CALLBACK(cb_event_box_key
),
3357 info_txt_new(o
, "key_press"));
3362 * We keep a style provider with each widget
3365 add_widget_style_provider(gpointer
*obj
, void *data
)
3367 GtkCssProvider
*style_provider
;
3368 GtkStyleContext
*context
;
3371 if (!GTK_IS_WIDGET(obj
))
3373 style_provider
= gtk_css_provider_new();
3374 context
= gtk_widget_get_style_context(GTK_WIDGET(obj
));
3375 gtk_style_context_add_provider(context
,
3376 GTK_STYLE_PROVIDER(style_provider
),
3377 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
);
3378 g_object_set_data(G_OBJECT(obj
), "style_provider", style_provider
);
3382 prepare_widgets(GtkBuilder
*builder
, char *ui_file
, FILE *out
)
3384 GSList
*objects
= NULL
;
3385 struct info ar
= {.builder
= builder
, .fout
= out
, .txt
= ui_file
};
3387 objects
= gtk_builder_get_objects(builder
);
3388 g_slist_foreach(objects
, (GFunc
) connect_widget_signals
, &ar
);
3389 g_slist_foreach(objects
, (GFunc
) add_widget_style_provider
, NULL
);
3390 g_slist_free(objects
);
3394 main(int argc
, char *argv
[])
3396 GObject
*main_window
= NULL
;
3398 char *in_fifo
= NULL
, *out_fifo
= NULL
;
3399 char *ui_file
= "pipeglade.ui", *log_file
= NULL
, *err_file
= NULL
;
3405 /* Disable runtime GLIB deprecation warnings: */
3406 setenv("G_ENABLE_DIAGNOSTIC", "0", 0);
3407 gtk_init(&argc
, &argv
);
3408 while ((opt
= getopt(argc
, argv
, "bGhe:i:l:o:O:u:V")) != -1) {
3410 case 'b': bg
= true; break;
3411 case 'e': xid
= optarg
; break;
3412 case 'G': show_lib_versions(); break;
3413 case 'h': bye(EXIT_SUCCESS
, stdout
, USAGE
); break;
3414 case 'i': in_fifo
= optarg
; break;
3415 case 'l': log_file
= optarg
; break;
3416 case 'o': out_fifo
= optarg
; break;
3417 case 'O': err_file
= optarg
; break;
3418 case 'u': ui_file
= optarg
; break;
3419 case 'V': bye(EXIT_SUCCESS
, stdout
, "%s\n", VERSION
); break;
3421 default: bye(EXIT_FAILURE
, stderr
, USAGE
); break;
3424 if (argv
[optind
] != NULL
)
3425 bye(EXIT_FAILURE
, stderr
,
3426 "illegal parameter '%s'\n" USAGE
, argv
[optind
]);
3427 redirect_stderr(err_file
);
3428 ar
.fin
= open_fifo(in_fifo
, "r", stdin
, _IONBF
);
3429 ar
.fout
= open_fifo(out_fifo
, "w", stdout
, _IOLBF
);
3430 go_bg_if(bg
, ar
.fin
, ar
.fout
, err_file
);
3431 ar
.builder
= builder_from_file(ui_file
);
3432 ar
.flog
= open_log(log_file
);
3433 pthread_create(&receiver
, NULL
, (void *(*)(void *)) digest_cmd
, &ar
);
3434 main_window
= find_main_window(ar
.builder
);
3436 LIBXML_TEST_VERSION
;
3437 prepare_widgets(ar
.builder
, ui_file
, ar
.fout
);
3438 xembed_if(xid
, main_window
);
3440 pthread_cancel(receiver
);
3441 pthread_join(receiver
, NULL
);
3443 rm_unless(stdin
, ar
.fin
, in_fifo
);
3444 rm_unless(stdout
, ar
.fout
, out_fifo
);