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.
27 #include <gtk/gtkunixprint.h>
30 #include <libxml/xpath.h>
39 #include <sys/select.h>
44 #define VERSION "4.7.0"
46 #define WHITESPACE " \t\n"
47 #define MAIN_WIN "main"
49 "usage: pipeglade [[-i in-fifo] " \
51 "[-u glade-file.ui] " \
54 "[--display X-server]] | " \
63 __func__, __FILE__, __LINE__); \
70 "Out of memory in %s (%s:%d): ", \
71 __func__, __FILE__, __LINE__); \
75 static FILE *out
; /* UI feedback messages */
76 static FILE *save
; /* saving user data */
77 static FILE *log_out
; /* logging output */
78 static GtkBuilder
*builder
; /* to be read from .ui file */
81 * ============================================================
83 * ============================================================
87 * Check if s1 and s2 are equal strings
90 eql(const char *s1
, const char *s2
)
92 return s1
!= NULL
&& s2
!= NULL
&& strcmp(s1
, s2
) == 0;
96 * Print a formatted message to stream s and give up with status
99 bye(int status
, FILE *s
, const char *fmt
, ...)
104 vfprintf(s
, fmt
, ap
);
110 * Print a warning about a malformed command to stderr
113 ign_cmd(GType type
, const char *msg
)
115 const char *name
, *pad
= " ";
117 if (type
== G_TYPE_INVALID
) {
122 name
= g_type_name(type
);
123 fprintf(stderr
, "ignoring %s%scommand \"%s\"\n", name
, pad
, msg
);
127 * Create a fifo if necessary, and open it. Give up if the file
128 * exists but is not a fifo
131 fifo(const char *name
, const char *mode
)
140 if (S_ISFIFO(sb
.st_mode
)) {
141 if (chmod(name
, 0600) != 0)
142 bye(EXIT_FAILURE
, stderr
,
143 "using pre-existing fifo %s: %s\n",
144 name
, strerror(errno
));
145 } else if (mkfifo(name
, 0600) != 0)
146 bye(EXIT_FAILURE
, stderr
,
147 "making fifo %s: %s\n", name
, strerror(errno
));
155 if ((fd
= open(name
, O_RDWR
| O_NONBLOCK
)) < 0)
156 bye(EXIT_FAILURE
, stderr
,
157 "opening fifo %s (%s): %s\n",
158 name
, mode
, strerror(errno
));
167 s
= fopen(name
, "w+");
174 bye(EXIT_FAILURE
, stderr
, "opening fifo %s (%s): %s\n",
175 name
, mode
, strerror(errno
));
177 setvbuf(s
, NULL
, bufmode
, 0);
182 * Create a log file if necessary, and open it. A name of "-"
183 * requests use of stderr.
186 log_file(const char *name
)
192 else if (name
&& (s
= fopen(name
, "a")) == NULL
)
193 bye(EXIT_FAILURE
, stderr
,
194 "opening log file %s: %s\n", name
, strerror(errno
));
199 * Microseconds elapsed since start
202 usec_since(struct timespec
*start
)
206 clock_gettime(CLOCK_MONOTONIC
, &now
);
207 return (now
.tv_sec
- start
->tv_sec
) * 1e6
+
208 (now
.tv_nsec
- start
->tv_nsec
) / 1e3
;
217 static struct timespec start
;
218 static char *old_msg
;
220 if (log_out
== NULL
) /* no logging */
222 if (msg
== NULL
&& old_msg
== NULL
)
224 "##########\t##### (New Pipeglade session) #####\n");
225 else if (msg
== NULL
&& old_msg
!= NULL
) { /* command done; start idle */
227 "%10ld\t%s\n", usec_since(&start
), old_msg
);
230 } else if (msg
!= NULL
&& old_msg
== NULL
) { /* idle done; start command */
232 "%10ld\t### (Idle) ###\n", usec_since(&start
));
233 if ((old_msg
= malloc(strlen(msg
) + 1)) == NULL
)
235 strcpy(old_msg
, msg
);
238 clock_gettime(CLOCK_MONOTONIC
, &start
);
242 * Remove suffix from name; find the object named like this
245 obj_sans_suffix(const char *suffix
, const char *name
)
248 char str
[BUFLEN
+ 1] = {'\0'};
250 str_l
= suffix
- name
;
251 strncpy(str
, name
, str_l
< BUFLEN
? str_l
: BUFLEN
);
252 return gtk_builder_get_object(builder
, str
);
256 widget_name(GtkBuildable
*obj
)
258 return gtk_buildable_get_name(obj
);
262 * Store a line from stream s into buf, which should have been malloc'd
263 * to bufsize. Enlarge buf and bufsize if necessary.
266 read_buf(FILE *s
, char **buf
, size_t *bufsize
)
277 select(ifd
+ 1, &rfds
, NULL
, NULL
, NULL
);
279 if (c
== '\n' || feof(s
))
281 if (i
>= *bufsize
- 1)
282 if ((*buf
= realloc(*buf
, *bufsize
= *bufsize
* 2)) == NULL
)
287 case 'n': (*buf
)[i
++] = '\n'; break;
288 case 'r': (*buf
)[i
++] = '\r'; break;
289 default: (*buf
)[i
++] = c
; break;
291 } else if (c
== '\\')
301 * ============================================================
302 * Receiving feedback from the GUI
303 * ============================================================
307 send_msg_to(FILE* o
, GtkBuildable
*obj
, const char *tag
, va_list ap
)
310 const char *w_name
= widget_name(obj
);
313 struct timeval timeout
= {1, 0};
317 if (select(ofd
+ 1, NULL
, &wfds
, NULL
, &timeout
) == 1) {
318 fprintf(o
, "%s:%s ", w_name
, tag
);
319 while ((data
= va_arg(ap
, char *)) != NULL
) {
323 while ((c
= data
[i
++]) != '\0')
334 "send error; discarding feedback message %s:%s\n",
339 * Send GUI feedback to global stream "out". The message format is
340 * "<origin>:<tag> <data ...>". The variadic arguments are strings;
341 * last argument must be NULL.
344 send_msg(GtkBuildable
*obj
, const char *tag
, ...)
349 send_msg_to(out
, obj
, tag
, ap
);
354 * Send message from GUI to global stream "save". The message format
355 * is "<origin>:<tag> <data ...>". The variadic arguments are strings;
356 * last argument must be NULL.
359 save_msg(GtkBuildable
*obj
, const char *tag
, ...)
364 send_msg_to(save
, obj
, tag
, ap
);
369 * Send message from GUI to global stream "save". The message format
370 * is "<origin>:set <data ...>". The variadic arguments are strings;
371 * last argument must be NULL.
374 save_action_set_msg(GtkBuildable
*obj
, const char *tag
, ...)
379 send_msg_to(save
, obj
, "set", ap
);
384 * Use msg_sender() to send a message describing a particular cell
387 send_tree_cell_msg_by(void msg_sender(GtkBuildable
*, const char *, ...),
388 GtkTreeModel
*model
, const char *path_s
,
389 GtkTreeIter
*iter
, int col
, GtkBuildable
*obj
)
391 GValue value
= G_VALUE_INIT
;
395 gtk_tree_model_get_value(model
, iter
, col
, &value
);
396 col_type
= gtk_tree_model_get_column_type(model
, col
);
399 snprintf(str
, BUFLEN
, " %d %d", col
, g_value_get_int(&value
));
400 msg_sender(obj
, "gint", path_s
, str
, NULL
);
403 snprintf(str
, BUFLEN
, " %d %ld", col
, g_value_get_long(&value
));
404 msg_sender(obj
, "glong", path_s
, str
, NULL
);
407 snprintf(str
, BUFLEN
, " %d %" PRId64
, col
, g_value_get_int64(&value
));
408 msg_sender(obj
, "gint64", path_s
, str
, NULL
);
411 snprintf(str
, BUFLEN
, " %d %u", col
, g_value_get_uint(&value
));
412 msg_sender(obj
, "guint", path_s
, str
, NULL
);
415 snprintf(str
, BUFLEN
, " %d %lu", col
, g_value_get_ulong(&value
));
416 msg_sender(obj
, "gulong", path_s
, str
, NULL
);
419 snprintf(str
, BUFLEN
, " %d %" PRIu64
, col
, g_value_get_uint64(&value
));
420 msg_sender(obj
, "guint64", path_s
, str
, NULL
);
423 snprintf(str
, BUFLEN
, " %d %d", col
, g_value_get_boolean(&value
));
424 msg_sender(obj
, "gboolean", path_s
, str
, NULL
);
427 snprintf(str
, BUFLEN
, " %d %f", col
, g_value_get_float(&value
));
428 msg_sender(obj
, "gfloat", path_s
, str
, NULL
);
431 snprintf(str
, BUFLEN
, " %d %f", col
, g_value_get_double(&value
));
432 msg_sender(obj
, "gdouble", path_s
, str
, NULL
);
435 snprintf(str
, BUFLEN
, " %d ", col
);
436 msg_sender(obj
, "gchararray", path_s
, str
, g_value_get_string(&value
), NULL
);
439 fprintf(stderr
, "column %d not implemented: %s\n", col
, G_VALUE_TYPE_NAME(&value
));
442 g_value_unset(&value
);
446 * Use msg_sender() to send one message per column for a single row
449 send_tree_row_msg_by(void msg_sender(GtkBuildable
*, const char *, ...),
450 GtkTreeModel
*model
, char *path_s
,
451 GtkTreeIter
*iter
, GtkBuildable
*obj
)
455 for (col
= 0; col
< gtk_tree_model_get_n_columns(model
); col
++)
456 send_tree_cell_msg_by(msg_sender
, model
, path_s
, iter
, col
, obj
);
460 * send_tree_row_msg serves as an argument for
461 * gtk_tree_selection_selected_foreach()
464 send_tree_row_msg(GtkTreeModel
*model
,
465 GtkTreePath
*path
, GtkTreeIter
*iter
, GtkBuildable
*obj
)
467 char *path_s
= gtk_tree_path_to_string(path
);
469 send_tree_row_msg_by(send_msg
, model
, path_s
, iter
, obj
);
475 * save_tree_row_msg serves as an argument for
476 * gtk_tree_model_foreach().
477 * Send message from GUI to global stream "save".
480 save_tree_row_msg(GtkTreeModel
*model
,
481 GtkTreePath
*path
, GtkTreeIter
*iter
, GtkBuildable
*obj
)
483 char *path_s
= gtk_tree_path_to_string(path
);
486 send_tree_row_msg_by(save_action_set_msg
, model
, path_s
, iter
, obj
);
492 cb_calendar(GtkBuildable
*obj
, const char *tag
)
495 unsigned int year
= 0, month
= 0, day
= 0;
497 gtk_calendar_get_date(GTK_CALENDAR(obj
), &year
, &month
, &day
);
498 snprintf(str
, BUFLEN
, "%04u-%02u-%02u", year
, ++month
, day
);
499 send_msg(obj
, tag
, str
, NULL
);
503 cb_color_button(GtkBuildable
*obj
, const char *tag
)
507 gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(obj
), &color
);
508 send_msg(obj
, tag
, gdk_rgba_to_string(&color
), NULL
);
512 cb_editable(GtkBuildable
*obj
, const char *tag
)
514 send_msg(obj
, tag
, gtk_entry_get_text(GTK_ENTRY(obj
)), NULL
);
518 * Callback that sends a message about a pointer device button press
522 cb_event_box_button(GtkBuildable
*obj
, GdkEvent
*e
, gpointer user_data
)
526 snprintf(data
, BUFLEN
, "%d %.1lf %.1lf",
527 e
->button
.button
, e
->button
.x
, e
->button
.y
);
528 send_msg(obj
, user_data
, data
, NULL
);
533 * Callback that sends in a message the name of the key pressed when
534 * a GtkEventBox is focused
537 cb_event_box_key(GtkBuildable
*obj
, GdkEvent
*e
, gpointer user_data
)
539 send_msg(obj
, user_data
, gdk_keyval_name(e
->key
.keyval
), NULL
);
544 * Callback that sends a message about pointer device motion in a
548 cb_event_box_motion(GtkBuildable
*obj
, GdkEvent
*e
, gpointer user_data
)
552 snprintf(data
, BUFLEN
, "%.1lf %.1lf", e
->button
.x
, e
->button
.y
);
553 send_msg(obj
, user_data
, data
, NULL
);
558 * Callback that only sends "name:tag" and returns false
561 cb_event_simple(GtkBuildable
*obj
, GdkEvent
*e
, const char *tag
)
564 send_msg(obj
, tag
, NULL
);
569 cb_file_chooser_button(GtkBuildable
*obj
, const char *tag
)
571 send_msg(obj
, tag
, gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(obj
)), NULL
);
575 cb_font_button(GtkBuildable
*obj
, const char *tag
)
577 send_msg(obj
, tag
, gtk_font_button_get_font_name(GTK_FONT_BUTTON(obj
)), NULL
);
581 cb_menu_item(GtkBuildable
*obj
, const char *tag
)
583 send_msg(obj
, tag
, gtk_menu_item_get_label(GTK_MENU_ITEM(obj
)), NULL
);
587 cb_range(GtkBuildable
*obj
, const char *tag
)
591 snprintf(str
, BUFLEN
, "%f", gtk_range_get_value(GTK_RANGE(obj
)));
592 send_msg(obj
, tag
, str
, NULL
);
596 * Callback that sends user's selection from a file dialog
599 cb_send_file_chooser_dialog_selection(gpointer user_data
)
601 send_msg(user_data
, "file",
602 gtk_file_chooser_get_filename(user_data
), NULL
);
603 send_msg(user_data
, "folder",
604 gtk_file_chooser_get_current_folder(user_data
), NULL
);
608 * Callback that sends in a message the content of the text buffer
609 * passed in user_data
612 cb_send_text(GtkBuildable
*obj
, gpointer user_data
)
616 gtk_text_buffer_get_bounds(user_data
, &a
, &b
);
617 send_msg(obj
, "text", gtk_text_buffer_get_text(user_data
, &a
, &b
, TRUE
), NULL
);
621 * Callback that sends in a message the highlighted text from the text
622 * buffer which was passed in user_data
625 cb_send_text_selection(GtkBuildable
*obj
, gpointer user_data
)
629 gtk_text_buffer_get_selection_bounds(user_data
, &a
, &b
);
630 send_msg(obj
, "text", gtk_text_buffer_get_text(user_data
, &a
, &b
, TRUE
), NULL
);
634 * Callback that only sends "name:tag" and returns true
637 cb_simple(GtkBuildable
*obj
, const char *tag
)
639 send_msg(obj
, tag
, NULL
);
644 cb_switch(GtkBuildable
*obj
, void *pspec
, void *user_data
)
648 send_msg(obj
, gtk_switch_get_active(GTK_SWITCH(obj
)) ? "1" : "0", NULL
);
652 cb_toggle_button(GtkBuildable
*obj
, const char *tag
)
655 send_msg(obj
, gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(obj
)) ? "1" : "0", NULL
);
659 cb_tree_selection(GtkBuildable
*obj
, const char *tag
)
663 view
= gtk_tree_selection_get_tree_view(GTK_TREE_SELECTION(obj
));
664 send_msg(GTK_BUILDABLE(view
), tag
, NULL
);
665 gtk_tree_selection_selected_foreach(GTK_TREE_SELECTION(obj
),
666 (GtkTreeSelectionForeachFunc
) send_tree_row_msg
,
671 * ============================================================
672 * cb_draw() maintains a drawing on a GtkDrawingArea; it needs a few
674 * ============================================================
678 * The set of supported drawing operations
705 * One single element of a drawing
708 struct draw_op
*next
;
709 struct draw_op
*prev
;
716 * Argument sets for the various drawing operations
726 struct curve_to_args
{
735 struct move_to_args
{
740 struct rectangle_args
{
747 struct set_dash_args
{
752 struct set_font_size_args
{
756 struct set_line_cap_args
{
757 cairo_line_cap_t line_cap
;
760 struct set_line_join_args
{
761 cairo_line_join_t line_join
;
764 struct set_line_width_args
{
768 struct set_source_rgba_args
{
772 struct show_text_args
{
778 draw(cairo_t
*cr
, enum cairo_fn op
, void *op_args
)
782 struct move_to_args
*args
= op_args
;
784 cairo_line_to(cr
, args
->x
, args
->y
);
788 struct move_to_args
*args
= op_args
;
790 cairo_rel_line_to(cr
, args
->x
, args
->y
);
794 struct move_to_args
*args
= op_args
;
796 cairo_move_to(cr
, args
->x
, args
->y
);
800 struct move_to_args
*args
= op_args
;
802 cairo_rel_move_to(cr
, args
->x
, args
->y
);
806 struct arc_args
*args
= op_args
;
808 cairo_arc(cr
, args
->x
, args
->y
, args
->radius
, args
->angle1
, args
->angle2
);
812 struct arc_args
*args
= op_args
;
814 cairo_arc_negative(cr
, args
->x
, args
->y
, args
->radius
, args
->angle1
, args
->angle2
);
818 struct curve_to_args
*args
= op_args
;
820 cairo_curve_to(cr
, args
->x1
, args
->y1
, args
->x2
, args
->y2
, args
->x3
, args
->y3
);
824 struct curve_to_args
*args
= op_args
;
826 cairo_curve_to(cr
, args
->x1
, args
->y1
, args
->x2
, args
->y2
, args
->x3
, args
->y3
);
830 struct rectangle_args
*args
= op_args
;
832 cairo_rectangle(cr
, args
->x
, args
->y
, args
->width
, args
->height
);
836 cairo_close_path(cr
);
839 struct show_text_args
*args
= op_args
;
841 cairo_show_text(cr
, args
->text
);
847 case STROKE_PRESERVE
:
848 cairo_stroke_preserve(cr
);
854 cairo_fill_preserve(cr
);
857 struct set_dash_args
*args
= op_args
;
859 cairo_set_dash(cr
, args
->dashes
, args
->num_dashes
, 0);
862 case SET_FONT_SIZE
: {
863 struct set_font_size_args
*args
= op_args
;
865 cairo_set_font_size(cr
, args
->size
);
869 struct set_line_cap_args
*args
= op_args
;
871 cairo_set_line_cap(cr
, args
->line_cap
);
874 case SET_LINE_JOIN
: {
875 struct set_line_join_args
*args
= op_args
;
877 cairo_set_line_join(cr
, args
->line_join
);
880 case SET_LINE_WIDTH
: {
881 struct set_line_width_args
*args
= op_args
;
883 cairo_set_line_width(cr
, args
->width
);
886 case SET_SOURCE_RGBA
: {
887 struct set_source_rgba_args
*args
= op_args
;
889 gdk_cairo_set_source_rgba(cr
, &args
->color
);
899 * Callback that draws on a GtkDrawingArea
902 cb_draw(GtkWidget
*widget
, cairo_t
*cr
, gpointer data
)
907 for (op
= g_object_get_data(G_OBJECT(widget
), "draw_ops");
910 draw(cr
, op
->op
, op
->op_args
);
915 * ============================================================
916 * Manipulating the GUI
917 * ============================================================
921 update_button(GObject
*obj
, const char *action
,
922 const char *data
, const char *whole_msg
, GType type
)
924 if (eql(action
, "set_label"))
925 gtk_button_set_label(GTK_BUTTON(obj
), data
);
927 ign_cmd(type
, whole_msg
);
931 update_calendar(GObject
*obj
, const char *action
,
932 const char *data
, const char *whole_msg
, GType type
)
934 GtkCalendar
*calendar
= GTK_CALENDAR(obj
);
935 int year
= 0, month
= 0, day
= 0;
937 if (eql(action
, "select_date")) {
938 sscanf(data
, "%d-%d-%d", &year
, &month
, &day
);
939 if (month
> -1 && month
<= 11 && day
> 0 && day
<= 31) {
940 gtk_calendar_select_month(calendar
, --month
, year
);
941 gtk_calendar_select_day(calendar
, day
);
943 ign_cmd(type
, whole_msg
);
944 } else if (eql(action
, "mark_day")) {
945 day
= strtol(data
, NULL
, 10);
946 if (day
> 0 && day
<= 31)
947 gtk_calendar_mark_day(calendar
, day
);
949 ign_cmd(type
, whole_msg
);
950 } else if (eql(action
, "clear_marks"))
951 gtk_calendar_clear_marks(calendar
);
953 ign_cmd(type
, whole_msg
);
957 * Common actions for various kinds of window. Return false if
961 update_class_window(GObject
*obj
, const char *action
,
962 const char *data
, const char *whole_msg
, GType type
)
964 GtkWindow
*window
= GTK_WINDOW(obj
);
968 if (eql(action
, "set_title"))
969 gtk_window_set_title(window
, data
);
970 else if (eql(action
, "fullscreen"))
971 gtk_window_fullscreen(window
);
972 else if (eql(action
, "unfullscreen"))
973 gtk_window_unfullscreen(window
);
974 else if (eql(action
, "resize")) {
975 if (sscanf(data
, "%d %d", &x
, &y
) != 2)
976 gtk_window_get_default_size(window
, &x
, &y
);
977 gtk_window_resize(window
, x
, y
);
978 } else if (eql(action
, "move")) {
979 if (sscanf(data
, "%d %d", &x
, &y
) == 2)
980 gtk_window_move(window
, x
, y
);
982 ign_cmd(type
, whole_msg
);
989 update_color_button(GObject
*obj
, const char *action
,
990 const char *data
, const char *whole_msg
, GType type
)
994 if (eql(action
, "set_color")) {
995 gdk_rgba_parse(&color
, data
);
996 gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(obj
), &color
);
998 ign_cmd(type
, whole_msg
);
1002 update_combo_box_text(GObject
*obj
, const char *action
,
1003 const char *data
, const char *whole_msg
, GType type
)
1005 GtkComboBoxText
*combobox
= GTK_COMBO_BOX_TEXT(obj
);
1006 char data1
[strlen(data
) + 1];
1008 strcpy(data1
, data
);
1009 if (eql(action
, "prepend_text"))
1010 gtk_combo_box_text_prepend_text(combobox
, data1
);
1011 else if (eql(action
, "append_text"))
1012 gtk_combo_box_text_append_text(combobox
, data1
);
1013 else if (eql(action
, "remove"))
1014 gtk_combo_box_text_remove(combobox
, strtol(data1
, NULL
, 10));
1015 else if (eql(action
, "insert_text")) {
1016 char *position
= strtok(data1
, WHITESPACE
);
1017 char *text
= strtok(NULL
, WHITESPACE
);
1018 gtk_combo_box_text_insert_text(combobox
,
1019 strtol(position
, NULL
, 10), text
);
1021 ign_cmd(type
, whole_msg
);
1025 * Maintaining a list of drawing operations. It is the responsibility
1026 * of cb_draw() to actually draw them. update_drawing_area() needs a
1027 * few helper functions.
1031 * Fill structure *op with the drawing operation according to action
1032 * and with the appropriate set of arguments
1035 set_draw_op(struct draw_op
*op
, const char *action
, const char *data
)
1037 if (eql(action
, "line_to")) {
1038 struct move_to_args
*args
;
1040 if ((args
= malloc(sizeof(*args
))) == NULL
)
1044 if (sscanf(data
, "%u %lf %lf", &op
->id
, &args
->x
, &args
->y
) != 3)
1046 } else if (eql(action
, "rel_line_to")) {
1047 struct move_to_args
*args
;
1049 if ((args
= malloc(sizeof(*args
))) == NULL
)
1051 op
->op
= REL_LINE_TO
;
1053 if (sscanf(data
, "%u %lf %lf", &op
->id
, &args
->x
, &args
->y
) != 3)
1055 } else if (eql(action
, "move_to")) {
1056 struct move_to_args
*args
;
1058 if ((args
= malloc(sizeof(*args
))) == NULL
)
1062 if (sscanf(data
, "%u %lf %lf", &op
->id
, &args
->x
, &args
->y
) != 3)
1064 } else if (eql(action
, "rel_move_to")) {
1065 struct move_to_args
*args
;
1067 if ((args
= malloc(sizeof(*args
))) == NULL
)
1069 op
->op
= REL_MOVE_TO
;
1071 if (sscanf(data
, "%u %lf %lf", &op
->id
, &args
->x
, &args
->y
) != 3)
1073 } else if (eql(action
, "arc")) {
1074 struct arc_args
*args
;
1077 if ((args
= malloc(sizeof(*args
))) == NULL
)
1081 if (sscanf(data
, "%u %lf %lf %lf %lf %lf", &op
->id
,
1082 &args
->x
, &args
->y
, &args
->radius
, °1
, °2
) != 6)
1084 args
->angle1
= deg1
* (M_PI
/ 180.);
1085 args
->angle2
= deg2
* (M_PI
/ 180.);
1086 } else if (eql(action
, "arc_negative")) {
1087 struct arc_args
*args
;
1090 if ((args
= malloc(sizeof(*args
))) == NULL
)
1092 op
->op
= ARC_NEGATIVE
;
1094 if (sscanf(data
, "%u %lf %lf %lf %lf %lf", &op
->id
,
1095 &args
->x
, &args
->y
, &args
->radius
, °1
, °2
) != 6)
1097 args
->angle1
= deg1
* (M_PI
/ 180.);
1098 args
->angle2
= deg2
* (M_PI
/ 180.);
1099 } else if (eql(action
, "curve_to")) {
1100 struct curve_to_args
*args
;
1102 if ((args
= malloc(sizeof(*args
))) == NULL
)
1106 if (sscanf(data
, "%u %lf %lf %lf %lf %lf %lf", &op
->id
,
1107 &args
->x1
, &args
->y1
, &args
->x2
, &args
->y2
, &args
->x3
, &args
->y3
) != 7)
1109 } else if (eql(action
, "rel_curve_to")) {
1110 struct curve_to_args
*args
;
1112 if ((args
= malloc(sizeof(*args
))) == NULL
)
1114 op
->op
= REL_CURVE_TO
;
1116 if (sscanf(data
, "%u %lf %lf %lf %lf %lf %lf", &op
->id
,
1117 &args
->x1
, &args
->y1
, &args
->x2
, &args
->y2
, &args
->x3
, &args
->y3
) != 7)
1119 } else if (eql(action
, "rectangle")) {
1120 struct rectangle_args
*args
;
1122 if ((args
= malloc(sizeof(*args
))) == NULL
)
1126 if (sscanf(data
, "%u %lf %lf %lf %lf", &op
->id
,
1127 &args
->x
, &args
->y
, &args
->width
, &args
->height
) != 5)
1129 } else if (eql(action
, "close_path")) {
1130 op
->op
= CLOSE_PATH
;
1131 if (sscanf(data
, "%u", &op
->id
) != 1)
1134 } else if (eql(action
, "show_text")) {
1135 struct show_text_args
*args
;
1138 if (sscanf(data
, "%u %n", &op
->id
, &start
) < 1)
1140 len
= strlen(data
+ start
) + 1;
1141 if ((args
= malloc(sizeof(*args
) + len
* sizeof(args
->text
[0]))) == NULL
)
1145 args
->len
= len
; /* not used */
1146 strncpy(args
->text
, (data
+ start
), len
);
1147 } else if (eql(action
, "stroke")) {
1149 if (sscanf(data
, "%u", &op
->id
) != 1)
1152 } else if (eql(action
, "stroke_preserve")) {
1153 op
->op
= STROKE_PRESERVE
;
1154 if (sscanf(data
, "%u", &op
->id
) != 1)
1157 } else if (eql(action
, "fill")) {
1159 if (sscanf(data
, "%u", &op
->id
) != 1)
1162 } else if (eql(action
, "fill_preserve")) {
1163 op
->op
= FILL_PRESERVE
;
1164 if (sscanf(data
, "%u", &op
->id
) != 1)
1167 } else if (eql(action
, "set_dash")) {
1168 struct set_dash_args
*args
;
1170 char data1
[strlen(data
) + 1];
1173 strcpy(data1
, data
);
1174 if (sscanf(data1
, "%u %n", &op
->id
, &d_start
) < 1)
1176 next
= end
= data1
+ d_start
;
1182 } while (next
!= end
);
1183 if ((args
= malloc(sizeof(*args
) + n
* sizeof(args
->dashes
[0]))) == NULL
)
1187 args
->num_dashes
= n
;
1188 for (i
= 0, next
= data1
+ d_start
; i
< n
; i
++, next
= end
) {
1189 args
->dashes
[i
] = strtod(next
, &end
);
1191 } else if (eql(action
, "set_font_size")) {
1192 struct set_font_size_args
*args
;
1194 if ((args
= malloc(sizeof(*args
))) == NULL
)
1196 op
->op
= SET_FONT_SIZE
;
1198 if (sscanf(data
, "%u %lf", &op
->id
, &args
->size
) != 2)
1200 } else if (eql(action
, "set_line_cap")) {
1201 struct set_line_cap_args
*args
;
1204 if ((args
= malloc(sizeof(*args
))) == NULL
)
1206 op
->op
= SET_LINE_CAP
;
1208 if (sscanf(data
, "%u %6s", &op
->id
, str
) != 2)
1210 if (eql(str
, "butt"))
1211 args
->line_cap
= CAIRO_LINE_CAP_BUTT
;
1212 else if (eql(str
, "round"))
1213 args
->line_cap
= CAIRO_LINE_CAP_ROUND
;
1214 else if (eql(str
, "square"))
1215 args
->line_cap
= CAIRO_LINE_CAP_SQUARE
;
1218 } else if (eql(action
, "set_line_join")) {
1219 struct set_line_join_args
*args
;
1222 if ((args
= malloc(sizeof(*args
))) == NULL
)
1224 op
->op
= SET_LINE_JOIN
;
1226 if (sscanf(data
, "%u %5s", &op
->id
, str
) != 2)
1228 if (eql(str
, "miter"))
1229 args
->line_join
= CAIRO_LINE_JOIN_MITER
;
1230 else if (eql(str
, "round"))
1231 args
->line_join
= CAIRO_LINE_JOIN_ROUND
;
1232 else if (eql(str
, "bevel"))
1233 args
->line_join
= CAIRO_LINE_JOIN_BEVEL
;
1236 } else if (eql(action
, "set_line_width")) {
1237 struct set_line_width_args
*args
;
1239 if ((args
= malloc(sizeof(*args
))) == NULL
)
1241 op
->op
= SET_LINE_WIDTH
;
1243 if (sscanf(data
, "%u %lf", &op
->id
, &args
->width
) != 2)
1245 } else if (eql(action
, "set_source_rgba")) {
1246 struct set_source_rgba_args
*args
;
1249 if ((args
= malloc(sizeof(*args
))) == NULL
)
1251 op
->op
= SET_SOURCE_RGBA
;
1253 if ((sscanf(data
, "%u %n", &op
->id
, &c_start
) < 1))
1255 gdk_rgba_parse(&args
->color
, data
+ c_start
);
1262 * Append another element to widget's "draw_ops" list
1265 ins_draw_op(GObject
*widget
, const char *action
, const char *data
)
1267 struct draw_op
*op
, *draw_ops
, *last_op
;
1269 if ((op
= malloc(sizeof(*op
))) == NULL
)
1273 if (!set_draw_op(op
, action
, data
)) {
1278 if ((draw_ops
= g_object_get_data(widget
, "draw_ops")) == NULL
)
1279 g_object_set_data(widget
, "draw_ops", op
);
1281 for (last_op
= draw_ops
;
1282 last_op
->next
!= NULL
;
1283 last_op
= last_op
->next
);
1290 * Remove all elements with the given id from widget's "draw_ops" list
1293 rem_draw_op(GObject
*widget
, const char *data
)
1295 struct draw_op
*op
, *next_op
, *prev_op
= NULL
;
1298 if (sscanf(data
, "%u", &id
) != 1)
1300 op
= g_object_get_data(widget
, "draw_ops");
1301 while (op
!= NULL
) {
1304 if (prev_op
== NULL
) /* list head */
1305 g_object_set_data(widget
, "draw_ops", op
->next
);
1307 prev_op
->next
= op
->next
;
1318 update_drawing_area(GObject
*obj
, const char *action
,
1319 const char *data
, const char *whole_msg
, GType type
)
1321 if (eql(action
, "remove")) {
1322 if (!rem_draw_op(obj
, data
))
1323 ign_cmd(type
, whole_msg
);
1324 } else if (eql(action
, "refresh")) {
1325 gint width
= gtk_widget_get_allocated_width(GTK_WIDGET(obj
));
1326 gint height
= gtk_widget_get_allocated_height(GTK_WIDGET(obj
));
1328 gtk_widget_queue_draw_area(GTK_WIDGET(obj
), 0, 0, width
, height
);
1329 } else if (ins_draw_op(obj
, action
, data
));
1331 ign_cmd(type
, whole_msg
);
1335 update_entry(GObject
*obj
, const char *action
,
1336 const char *data
, const char *whole_msg
, GType type
)
1338 GtkEntry
*entry
= GTK_ENTRY(obj
);
1340 if (eql(action
, "set_text"))
1341 gtk_entry_set_text(entry
, data
);
1342 else if (eql(action
, "set_placeholder_text"))
1343 gtk_entry_set_placeholder_text(entry
, data
);
1345 ign_cmd(type
, whole_msg
);
1349 update_expander(GObject
*obj
, const char *action
,
1350 const char *data
, const char *whole_msg
, GType type
)
1352 GtkExpander
*expander
= GTK_EXPANDER(obj
);
1354 if (eql(action
, "set_expanded"))
1355 gtk_expander_set_expanded(expander
, strtol(data
, NULL
, 10));
1356 else if (eql(action
, "set_label"))
1357 gtk_expander_set_label(expander
, data
);
1359 ign_cmd(type
, whole_msg
);
1363 update_file_chooser_button(GObject
*obj
, const char *action
,
1364 const char *data
, const char *whole_msg
, GType type
)
1366 if (eql(action
, "set_filename"))
1367 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(obj
), data
);
1369 ign_cmd(type
, whole_msg
);
1373 update_file_chooser_dialog(GObject
*obj
, const char *action
,
1374 const char *data
, const char *whole_msg
, GType type
)
1376 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(obj
);
1378 if (eql(action
, "set_filename"))
1379 gtk_file_chooser_set_filename(chooser
, data
);
1380 else if (eql(action
, "set_current_name"))
1381 gtk_file_chooser_set_current_name(chooser
, data
);
1382 else if (update_class_window(obj
, action
, data
, whole_msg
, type
));
1384 ign_cmd(type
, whole_msg
);
1388 update_focus(GObject
*obj
, const char *action
,
1389 const char *data
, const char *whole_msg
, GType type
)
1393 if (gtk_widget_get_can_focus(GTK_WIDGET(obj
)))
1394 gtk_widget_grab_focus(GTK_WIDGET(obj
));
1396 ign_cmd(type
, whole_msg
);
1400 update_font_button(GObject
*obj
, const char *action
,
1401 const char *data
, const char *whole_msg
, GType type
)
1403 GtkFontButton
*font_button
= GTK_FONT_BUTTON(obj
);
1405 if (eql(action
, "set_font_name"))
1406 gtk_font_button_set_font_name(font_button
, data
);
1408 ign_cmd(type
, whole_msg
);
1412 update_frame(GObject
*obj
, const char *action
,
1413 const char *data
, const char *whole_msg
, GType type
)
1415 if (eql(action
, "set_label"))
1416 gtk_frame_set_label(GTK_FRAME(obj
), data
);
1418 ign_cmd(type
, whole_msg
);
1422 update_image(GObject
*obj
, const char *action
,
1423 const char *data
, const char *whole_msg
, GType type
)
1425 GtkImage
*image
= GTK_IMAGE(obj
);
1428 gtk_image_get_icon_name(image
, NULL
, &size
);
1429 if (eql(action
, "set_from_file"))
1430 gtk_image_set_from_file(image
, data
);
1431 else if (eql(action
, "set_from_icon_name"))
1432 gtk_image_set_from_icon_name(image
, data
, size
);
1434 ign_cmd(type
, whole_msg
);
1438 update_label(GObject
*obj
, const char *action
,
1439 const char *data
, const char *whole_msg
, GType type
)
1441 if (eql(action
, "set_text"))
1442 gtk_label_set_text(GTK_LABEL(obj
), data
);
1444 ign_cmd(type
, whole_msg
);
1448 update_notebook(GObject
*obj
, const char *action
,
1449 const char *data
, const char *whole_msg
, GType type
)
1451 if (eql(action
, "set_current_page"))
1452 gtk_notebook_set_current_page(GTK_NOTEBOOK(obj
), strtol(data
, NULL
, 10));
1454 ign_cmd(type
, whole_msg
);
1458 update_nothing(GObject
*obj
, const char *action
,
1459 const char *data
, const char *whole_msg
, GType type
)
1469 update_print_dialog(GObject
*obj
, const char *action
,
1470 const char *data
, const char *whole_msg
, GType type
)
1472 GtkPrintUnixDialog
*dialog
= GTK_PRINT_UNIX_DIALOG(obj
);
1474 GtkPrinter
*printer
;
1475 GtkPrintSettings
*settings
;
1476 GtkPageSetup
*page_setup
;
1479 if (eql(action
, "print")) {
1480 response_id
= gtk_dialog_run(GTK_DIALOG(dialog
));
1481 switch (response_id
) {
1482 case GTK_RESPONSE_OK
:
1483 printer
= gtk_print_unix_dialog_get_selected_printer(dialog
);
1484 settings
= gtk_print_unix_dialog_get_settings(dialog
);
1485 page_setup
= gtk_print_unix_dialog_get_page_setup(dialog
);
1486 job
= gtk_print_job_new(data
, printer
, settings
, page_setup
);
1487 if (gtk_print_job_set_source_file(job
, data
, NULL
))
1488 gtk_print_job_send(job
, NULL
, NULL
, NULL
);
1490 ign_cmd(type
, whole_msg
);
1491 g_clear_object(&settings
);
1492 g_clear_object(&job
);
1494 case GTK_RESPONSE_CANCEL
:
1495 case GTK_RESPONSE_DELETE_EVENT
:
1498 fprintf(stderr
, "%s sent an unexpected response id (%d)\n",
1499 widget_name(GTK_BUILDABLE(dialog
)), response_id
);
1502 gtk_widget_hide(GTK_WIDGET(dialog
));
1504 ign_cmd(type
, whole_msg
);
1508 update_progress_bar(GObject
*obj
, const char *action
,
1509 const char *data
, const char *whole_msg
, GType type
)
1511 GtkProgressBar
*progressbar
= GTK_PROGRESS_BAR(obj
);
1513 if (eql(action
, "set_text"))
1514 gtk_progress_bar_set_text(progressbar
, *data
== '\0' ? NULL
: data
);
1515 else if (eql(action
, "set_fraction"))
1516 gtk_progress_bar_set_fraction(progressbar
, strtod(data
, NULL
));
1518 ign_cmd(type
, whole_msg
);
1522 update_scale(GObject
*obj
, const char *action
,
1523 const char *data
, const char *whole_msg
, GType type
)
1525 if (eql(action
, "set_value"))
1526 gtk_range_set_value(GTK_RANGE(obj
), strtod(data
, NULL
));
1528 ign_cmd(type
, whole_msg
);
1532 update_scrolled_window(GObject
*obj
, const char *action
,
1533 const char *data
, const char *whole_msg
, GType type
)
1535 GtkScrolledWindow
*window
= GTK_SCROLLED_WINDOW(obj
);
1536 GtkAdjustment
*hadj
= gtk_scrolled_window_get_hadjustment(window
);
1537 GtkAdjustment
*vadj
= gtk_scrolled_window_get_vadjustment(window
);
1540 if (eql(action
, "hscroll") && sscanf(data
, "%lf", &d0
) == 1)
1541 gtk_adjustment_set_value(hadj
, d0
);
1542 else if (eql(action
, "vscroll") && sscanf(data
, "%lf", &d0
) == 1)
1543 gtk_adjustment_set_value(vadj
, d0
);
1544 else if (eql(action
, "hscroll_to_range") &&
1545 sscanf(data
, "%lf %lf", &d0
, &d1
) == 2)
1546 gtk_adjustment_clamp_page(hadj
, d0
, d1
);
1547 else if (eql(action
, "vscroll_to_range") &&
1548 sscanf(data
, "%lf %lf", &d0
, &d1
) == 2)
1549 gtk_adjustment_clamp_page(vadj
, d0
, d1
);
1551 ign_cmd(type
, whole_msg
);
1555 update_sensitivity(GObject
*obj
, const char *action
,
1556 const char *data
, const char *whole_msg
, GType type
)
1561 gtk_widget_set_sensitive(GTK_WIDGET(obj
), strtol(data
, NULL
, 10));
1565 update_size_request(GObject
*obj
, const char *action
,
1566 const char *data
, const char *whole_msg
, GType type
)
1573 if (sscanf(data
, "%d %d", &x
, &y
) == 2)
1574 gtk_widget_set_size_request(GTK_WIDGET(obj
), x
, y
);
1576 gtk_widget_set_size_request(GTK_WIDGET(obj
), -1, -1);
1580 update_socket(GObject
*obj
, const char *action
,
1581 const char *data
, const char *whole_msg
, GType type
)
1583 GtkSocket
*socket
= GTK_SOCKET(obj
);
1588 if (eql(action
, "id")) {
1589 id
= gtk_socket_get_id(socket
);
1590 snprintf(str
, BUFLEN
, "%lu", id
);
1591 send_msg(GTK_BUILDABLE(socket
), "id", str
, NULL
);
1593 ign_cmd(type
, whole_msg
);
1597 update_spinner(GObject
*obj
, const char *action
,
1598 const char *data
, const char *whole_msg
, GType type
)
1600 GtkSpinner
*spinner
= GTK_SPINNER(obj
);
1603 if (eql(action
, "start"))
1604 gtk_spinner_start(spinner
);
1605 else if (eql(action
, "stop"))
1606 gtk_spinner_stop(spinner
);
1608 ign_cmd(type
, whole_msg
);
1612 update_statusbar(GObject
*obj
, const char *action
,
1613 const char *data
, const char *whole_msg
, GType type
)
1615 GtkStatusbar
*statusbar
= GTK_STATUSBAR(obj
);
1616 char *ctx_msg
, *msg
;
1619 if ((ctx_msg
= malloc(strlen(data
) + 1)) == NULL
)
1621 strcpy(ctx_msg
, data
);
1622 ctx_len
= strcspn(ctx_msg
, WHITESPACE
);
1624 ctx_msg
[ctx_len
] = '\0';
1625 msg
= ctx_msg
+ ctx_len
+ 1;
1627 msg
= ctx_msg
+ strlen(ctx_msg
);
1628 if (eql(action
, "push"))
1629 gtk_statusbar_push(statusbar
,
1630 gtk_statusbar_get_context_id(statusbar
, "0"),
1632 else if (eql(action
, "push_id"))
1633 gtk_statusbar_push(statusbar
,
1634 gtk_statusbar_get_context_id(statusbar
, ctx_msg
),
1636 else if (eql(action
, "pop"))
1637 gtk_statusbar_pop(statusbar
,
1638 gtk_statusbar_get_context_id(statusbar
, "0"));
1639 else if (eql(action
, "pop_id"))
1640 gtk_statusbar_pop(statusbar
,
1641 gtk_statusbar_get_context_id(statusbar
, ctx_msg
));
1642 else if (eql(action
, "remove_all"))
1643 gtk_statusbar_remove_all(statusbar
,
1644 gtk_statusbar_get_context_id(statusbar
, "0"));
1645 else if (eql(action
, "remove_all_id"))
1646 gtk_statusbar_remove_all(statusbar
,
1647 gtk_statusbar_get_context_id(statusbar
, ctx_msg
));
1649 ign_cmd(type
, whole_msg
);
1654 update_switch(GObject
*obj
, const char *action
,
1655 const char *data
, const char *whole_msg
, GType type
)
1657 if (eql(action
, "set_active"))
1658 gtk_switch_set_active(GTK_SWITCH(obj
), strtol(data
, NULL
, 10));
1660 ign_cmd(type
, whole_msg
);
1664 update_text_view(GObject
*obj
, const char *action
,
1665 const char *data
, const char *whole_msg
, GType type
)
1667 GtkTextView
*view
= GTK_TEXT_VIEW(obj
);
1668 GtkTextBuffer
*textbuf
= gtk_text_view_get_buffer(view
);
1671 if (eql(action
, "set_text"))
1672 gtk_text_buffer_set_text(textbuf
, data
, -1);
1673 else if (eql(action
, "delete")) {
1674 gtk_text_buffer_get_bounds(textbuf
, &a
, &b
);
1675 gtk_text_buffer_delete(textbuf
, &a
, &b
);
1676 } else if (eql(action
, "insert_at_cursor"))
1677 gtk_text_buffer_insert_at_cursor(textbuf
, data
, -1);
1678 else if (eql(action
, "place_cursor")) {
1679 if (eql(data
, "end"))
1680 gtk_text_buffer_get_end_iter(textbuf
, &a
);
1681 else /* numeric offset */
1682 gtk_text_buffer_get_iter_at_offset(textbuf
, &a
,
1683 strtol(data
, NULL
, 10));
1684 gtk_text_buffer_place_cursor(textbuf
, &a
);
1685 } else if (eql(action
, "place_cursor_at_line")) {
1686 gtk_text_buffer_get_iter_at_line(textbuf
, &a
, strtol(data
, NULL
, 10));
1687 gtk_text_buffer_place_cursor(textbuf
, &a
);
1688 } else if (eql(action
, "scroll_to_cursor"))
1689 gtk_text_view_scroll_to_mark(view
, gtk_text_buffer_get_insert(textbuf
),
1691 else if (eql(action
, "save") && data
!= NULL
&&
1692 (save
= fopen(data
, "w")) != NULL
) {
1693 gtk_text_buffer_get_bounds(textbuf
, &a
, &b
);
1694 save_msg(GTK_BUILDABLE(view
), "insert_at_cursor",
1695 gtk_text_buffer_get_text(textbuf
, &a
, &b
, TRUE
), NULL
);
1698 ign_cmd(type
, whole_msg
);
1702 update_toggle_button(GObject
*obj
, const char *action
,
1703 const char *data
, const char *whole_msg
, GType type
)
1705 if (eql(action
, "set_label"))
1706 gtk_button_set_label(GTK_BUTTON(obj
), data
);
1707 else if (eql(action
, "set_active"))
1708 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(obj
), strtol(data
, NULL
, 10));
1710 ign_cmd(type
, whole_msg
);
1714 update_tooltip_text(GObject
*obj
, const char *action
,
1715 const char *data
, const char *whole_msg
, GType type
)
1720 gtk_widget_set_tooltip_text(GTK_WIDGET(obj
), data
);
1724 * update_tree_view() needs a few helper functions
1728 * Check if s is a valid string representation of a GtkTreePath
1731 is_path_string(char *s
)
1734 strlen(s
) == strspn(s
, ":0123456789") &&
1735 strstr(s
, "::") == NULL
&&
1736 strcspn(s
, ":") > 0;
1740 tree_model_insert_before(GtkTreeModel
*model
, GtkTreeIter
*iter
,
1741 GtkTreeIter
*parent
, GtkTreeIter
*sibling
)
1743 if (GTK_IS_TREE_STORE(model
))
1744 gtk_tree_store_insert_before(GTK_TREE_STORE(model
),
1745 iter
, parent
, sibling
);
1746 else if (GTK_IS_LIST_STORE(model
))
1747 gtk_list_store_insert_before(GTK_LIST_STORE(model
),
1754 tree_model_insert_after(GtkTreeModel
*model
, GtkTreeIter
*iter
,
1755 GtkTreeIter
*parent
, GtkTreeIter
*sibling
)
1757 if (GTK_IS_TREE_STORE(model
))
1758 gtk_tree_store_insert_after(GTK_TREE_STORE(model
),
1759 iter
, parent
, sibling
);
1760 else if (GTK_IS_LIST_STORE(model
))
1761 gtk_list_store_insert_after(GTK_LIST_STORE(model
),
1768 tree_model_move_before(GtkTreeModel
*model
, GtkTreeIter
*iter
,
1769 GtkTreeIter
*position
)
1771 if (GTK_IS_TREE_STORE(model
))
1772 gtk_tree_store_move_before(GTK_TREE_STORE(model
), iter
, position
);
1773 else if (GTK_IS_LIST_STORE(model
))
1774 gtk_list_store_move_before(GTK_LIST_STORE(model
), iter
, position
);
1780 tree_model_remove(GtkTreeModel
*model
, GtkTreeIter
*iter
)
1782 if (GTK_IS_TREE_STORE(model
))
1783 gtk_tree_store_remove(GTK_TREE_STORE(model
), iter
);
1784 else if (GTK_IS_LIST_STORE(model
))
1785 gtk_list_store_remove(GTK_LIST_STORE(model
), iter
);
1791 tree_model_clear(GtkTreeModel
*model
)
1793 if (GTK_IS_TREE_STORE(model
))
1794 gtk_tree_store_clear(GTK_TREE_STORE(model
));
1795 else if (GTK_IS_LIST_STORE(model
))
1796 gtk_list_store_clear(GTK_LIST_STORE(model
));
1802 tree_model_set(GtkTreeModel
*model
, GtkTreeIter
*iter
, ...)
1807 if (GTK_IS_TREE_STORE(model
))
1808 gtk_tree_store_set_valist(GTK_TREE_STORE(model
), iter
, ap
);
1809 else if (GTK_IS_LIST_STORE(model
))
1810 gtk_list_store_set_valist(GTK_LIST_STORE(model
), iter
, ap
);
1817 * Create an empty row at path if it doesn't yet exist. Create older
1818 * siblings and parents as necessary.
1821 create_subtree(GtkTreeModel
*model
, GtkTreePath
*path
, GtkTreeIter
*iter
)
1823 GtkTreePath
*path_1
; /* path's predecessor */
1824 GtkTreeIter iter_1
; /* iter's predecessor */
1826 if (gtk_tree_model_get_iter(model
, iter
, path
))
1828 path_1
= gtk_tree_path_copy(path
);
1829 if (gtk_tree_path_prev(path_1
)) { /* need an older sibling */
1830 create_subtree(model
, path_1
, iter
);
1832 tree_model_insert_after(model
, iter
, NULL
, &iter_1
);
1833 } else if (gtk_tree_path_up(path_1
)) { /* need a parent */
1834 create_subtree(model
, path_1
, iter
);
1835 if (gtk_tree_path_get_depth(path_1
) == 0)
1836 /* first toplevel row */
1837 tree_model_insert_after(model
, iter
, NULL
, NULL
);
1838 else { /* first row in a lower level */
1840 tree_model_insert_after(model
, iter
, &iter_1
, NULL
);
1842 } /* neither prev nor up mean we're at the root of an empty tree */
1843 gtk_tree_path_free(path_1
);
1847 set_tree_view_cell(GtkTreeModel
*model
, GtkTreeIter
*iter
,
1848 const char *path_s
, int col
, const char *new_text
)
1850 GType col_type
= gtk_tree_model_get_column_type(model
, col
);
1857 path
= gtk_tree_path_new_from_string(path_s
);
1858 create_subtree(model
, path
, iter
);
1859 gtk_tree_path_free(path
);
1861 case G_TYPE_BOOLEAN
:
1870 n
= strtoll(new_text
, &endptr
, 10);
1871 if (!errno
&& endptr
!= new_text
) {
1872 tree_model_set(model
, iter
, col
, n
, -1);
1880 d
= strtod(new_text
, &endptr
);
1881 if (!errno
&& endptr
!= new_text
) {
1882 tree_model_set(model
, iter
, col
, d
, -1);
1887 tree_model_set(model
, iter
, col
, new_text
, -1);
1891 fprintf(stderr
, "column %d: %s not implemented\n",
1892 col
, g_type_name(col_type
));
1900 tree_view_set_cursor(GtkTreeView
*view
, GtkTreePath
*path
, GtkTreeViewColumn
*col
)
1902 /* GTK+ 3.14 requires this. For 3.18, path = NULL */
1903 /* is just fine and this function need not exist. */
1905 path
= gtk_tree_path_new();
1906 gtk_tree_view_set_cursor(view
, path
, col
, false);
1910 update_tree_view(GObject
*obj
, const char *action
,
1911 const char *data
, const char *whole_msg
, GType type
)
1913 GtkTreeView
*view
= GTK_TREE_VIEW(obj
);
1914 GtkTreeModel
*model
= gtk_tree_view_get_model(view
);
1915 GtkTreeIter iter0
, iter1
;
1916 GtkTreePath
*path
= NULL
;
1917 bool iter0_valid
, iter1_valid
;
1918 char *tokens
, *arg0
, *arg1
, *arg2
;
1919 int col
= -1; /* invalid column number */
1921 if (!GTK_IS_LIST_STORE(model
) && !GTK_IS_TREE_STORE(model
))
1923 fprintf(stderr
, "missing model/");
1924 ign_cmd(type
, whole_msg
);
1927 if ((tokens
= malloc(strlen(data
) + 1)) == NULL
)
1929 strcpy(tokens
, data
);
1930 arg0
= strtok(tokens
, WHITESPACE
);
1931 arg1
= strtok(NULL
, WHITESPACE
);
1932 arg2
= strtok(NULL
, "");
1933 iter0_valid
= is_path_string(arg0
) &&
1934 gtk_tree_model_get_iter_from_string(model
, &iter0
, arg0
);
1935 iter1_valid
= is_path_string(arg1
) &&
1936 gtk_tree_model_get_iter_from_string(model
, &iter1
, arg1
);
1937 if (is_path_string(arg1
))
1938 col
= strtol(arg1
, NULL
, 10);
1939 if (eql(action
, "set") &&
1940 col
> -1 && col
< gtk_tree_model_get_n_columns(model
) &&
1941 is_path_string(arg0
)) {
1942 if (set_tree_view_cell(model
, &iter0
, arg0
, col
, arg2
) == false)
1943 ign_cmd(type
, whole_msg
);
1944 } else if (eql(action
, "scroll") && iter0_valid
&& iter1_valid
) {
1945 path
= gtk_tree_path_new_from_string(arg0
);
1946 gtk_tree_view_scroll_to_cell (view
,
1948 gtk_tree_view_get_column(view
, col
),
1950 } else if (eql(action
, "expand") && iter0_valid
) {
1951 path
= gtk_tree_path_new_from_string(arg0
);
1952 gtk_tree_view_expand_row(view
, path
, false);
1953 } else if (eql(action
, "expand_all") && iter0_valid
) {
1954 path
= gtk_tree_path_new_from_string(arg0
);
1955 gtk_tree_view_expand_row(view
, path
, true);
1956 } else if (eql(action
, "expand_all") && arg0
== NULL
)
1957 gtk_tree_view_expand_all(view
);
1958 else if (eql(action
, "collapse") && iter0_valid
) {
1959 path
= gtk_tree_path_new_from_string(arg0
);
1960 gtk_tree_view_collapse_row(view
, path
);
1961 } else if (eql(action
, "collapse") && arg0
== NULL
)
1962 gtk_tree_view_collapse_all(view
);
1963 else if (eql(action
, "set_cursor") && iter0_valid
) {
1964 path
= gtk_tree_path_new_from_string(arg0
);
1965 tree_view_set_cursor(view
, path
, NULL
);
1966 } else if (eql(action
, "set_cursor") && arg0
== NULL
) {
1967 tree_view_set_cursor(view
, NULL
, NULL
);
1968 gtk_tree_selection_unselect_all(gtk_tree_view_get_selection(view
));
1969 } else if (eql(action
, "insert_row") && eql(arg0
, "end"))
1970 tree_model_insert_before(model
, &iter1
, NULL
, NULL
);
1971 else if (eql(action
, "insert_row") && iter0_valid
&& eql(arg1
, "as_child"))
1972 tree_model_insert_after(model
, &iter1
, &iter0
, NULL
);
1973 else if (eql(action
, "insert_row") && iter0_valid
)
1974 tree_model_insert_before(model
, &iter1
, NULL
, &iter0
);
1975 else if (eql(action
, "move_row") && iter0_valid
&& eql(arg1
, "end"))
1976 tree_model_move_before(model
, &iter0
, NULL
);
1977 else if (eql(action
, "move_row") && iter0_valid
&& iter1_valid
)
1978 tree_model_move_before(model
, &iter0
, &iter1
);
1979 else if (eql(action
, "remove_row") && iter0_valid
)
1980 tree_model_remove(model
, &iter0
);
1981 else if (eql(action
, "clear") && arg0
== NULL
) {
1982 tree_view_set_cursor(view
, NULL
, NULL
);
1983 gtk_tree_selection_unselect_all(gtk_tree_view_get_selection(view
));
1984 tree_model_clear(model
);
1985 } else if (eql(action
, "save") && arg0
!= NULL
&&
1986 (save
= fopen(arg0
, "w")) != NULL
) {
1987 gtk_tree_model_foreach(model
, (GtkTreeModelForeachFunc
) save_tree_row_msg
, view
);
1990 ign_cmd(type
, whole_msg
);
1992 gtk_tree_path_free(path
);
1996 update_visibility(GObject
*obj
, const char *action
,
1997 const char *data
, const char *whole_msg
, GType type
)
2002 gtk_widget_set_visible(GTK_WIDGET(obj
), strtol(data
, NULL
, 10));
2006 * Change the style of the widget passed
2009 update_widget_style(GObject
*obj
, const char *name
,
2010 const char *data
, const char *whole_msg
, GType type
)
2012 GtkStyleContext
*context
;
2013 GtkStyleProvider
*style_provider
;
2015 const char *prefix
= "* {", *suffix
= "}";
2021 style_provider
= g_object_get_data(obj
, "style_provider");
2022 sz
= strlen(prefix
) + strlen(suffix
) + strlen(data
) + 1;
2023 context
= gtk_widget_get_style_context(GTK_WIDGET(obj
));
2024 gtk_style_context_remove_provider(context
, style_provider
);
2025 if ((style_decl
= malloc(sz
)) == NULL
)
2027 strcpy(style_decl
, prefix
);
2028 strcat(style_decl
, data
);
2029 strcat(style_decl
, suffix
);
2030 gtk_style_context_add_provider(context
, style_provider
,
2031 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
);
2032 gtk_css_provider_load_from_data(GTK_CSS_PROVIDER(style_provider
),
2033 style_decl
, -1, NULL
);
2038 update_window(GObject
*obj
, const char *action
,
2039 const char *data
, const char *whole_msg
, GType type
)
2041 if (!update_class_window(obj
, action
, data
, whole_msg
, type
))
2042 ign_cmd(type
, whole_msg
);
2046 * Simulate user activity on various widgets
2049 fake_ui_activity(GObject
*obj
, const char *action
,
2050 const char *data
, const char *whole_msg
, GType type
)
2054 if (!GTK_IS_WIDGET(obj
))
2055 ign_cmd(type
, whole_msg
);
2056 else if (GTK_IS_ENTRY(obj
) || GTK_IS_SPIN_BUTTON(obj
))
2057 cb_editable(GTK_BUILDABLE(obj
), "text");
2058 else if (GTK_IS_SCALE(obj
))
2059 cb_range(GTK_BUILDABLE(obj
), "value");
2060 else if (GTK_IS_CALENDAR(obj
))
2061 cb_calendar(GTK_BUILDABLE(obj
), "clicked");
2062 else if (GTK_IS_FILE_CHOOSER_BUTTON(obj
))
2063 cb_file_chooser_button(GTK_BUILDABLE(obj
), "file");
2064 else if (!gtk_widget_activate(GTK_WIDGET(obj
)))
2065 ign_cmd(type
, whole_msg
);
2069 * The final UI update
2072 main_quit(GObject
*obj
, const char *action
,
2073 const char *data
, const char *whole_msg
, GType type
)
2084 * Don't update anything; just complain
2087 complain(GObject
*obj
, const char *action
,
2088 const char *data
, const char *whole_msg
, GType type
)
2093 ign_cmd(type
, whole_msg
);
2097 * Data to be passed to and from the GTK main loop
2100 void (*fn
)(GObject
*, const char *action
,
2101 const char *data
, const char *msg
, GType type
);
2111 * Parse command pointed to by ud, and act on ui accordingly; post
2112 * semaphore ud.msg_digested if done. Runs once per command inside
2116 update_ui(struct ui_data
*ud
)
2118 (ud
->fn
)(ud
->obj
, ud
->action
, ud
->data
, ud
->msg
, ud
->type
);
2119 free(ud
->msg_tokens
);
2122 return G_SOURCE_REMOVE
;
2126 * Keep track of loading files to avoid recursive loading of the same
2127 * file. If filename = NULL, forget the most recently remembered file.
2130 remember_loading_file(char *filename
)
2132 static char *filenames
[BUFLEN
];
2133 static size_t latest
= 0;
2136 if (filename
== NULL
) { /* pop */
2142 for (i
= 1; i
<= latest
; i
++)
2143 if (eql(filename
, filenames
[i
]))
2145 if (latest
> BUFLEN
-2)
2147 filenames
[++latest
] = filename
;
2153 * Read lines from stream cmd and perform appropriate actions on the
2157 digest_msg(FILE *cmd
)
2159 FILE *load
; /* restoring user data */
2161 static int recursion
= -1; /* > 0 means this is a recursive call */
2166 char first_char
= '\0';
2167 size_t msg_size
= 32;
2168 int name_start
= 0, name_end
= 0;
2169 int action_start
= 0, action_end
= 0;
2174 if ((ud
= malloc(sizeof(*ud
))) == NULL
)
2176 if ((ud
->msg
= malloc(msg_size
)) == NULL
)
2178 ud
->type
= G_TYPE_INVALID
;
2179 pthread_testcancel();
2182 read_buf(cmd
, &ud
->msg
, &msg_size
);
2185 data_start
= strlen(ud
->msg
);
2186 if ((ud
->msg_tokens
= malloc(strlen(ud
->msg
) + 1)) == NULL
)
2188 strcpy(ud
->msg_tokens
, ud
->msg
);
2189 sscanf(ud
->msg
, " %c", &first_char
);
2190 if (strlen(ud
->msg
) == 0 || first_char
== '#') { /* comment */
2191 ud
->fn
= update_nothing
;
2194 sscanf(ud
->msg_tokens
,
2195 " %n%*[0-9a-zA-Z_]%n:%n%*[0-9a-zA-Z_]%n%*1[ \t]%n",
2196 &name_start
, &name_end
, &action_start
, &action_end
, &data_start
);
2197 ud
->msg_tokens
[name_end
] = ud
->msg_tokens
[action_end
] = '\0';
2198 name
= ud
->msg_tokens
+ name_start
;
2199 ud
->action
= ud
->msg_tokens
+ action_start
;
2200 if (eql(ud
->action
, "main_quit")) {
2204 ud
->data
= ud
->msg_tokens
+ data_start
;
2205 if (eql(ud
->action
, "load") && strlen(ud
->data
) > 0 &&
2206 (load
= fopen(ud
->data
, "r")) != NULL
&&
2207 remember_loading_file(ud
->data
)) {
2210 remember_loading_file(NULL
);
2211 ud
->fn
= update_nothing
;
2214 if ((ud
->obj
= (gtk_builder_get_object(builder
, name
))) == NULL
) {
2218 ud
->type
= G_TYPE_FROM_INSTANCE(ud
->obj
);
2219 if (eql(ud
->action
, "force"))
2220 ud
->fn
= fake_ui_activity
;
2221 else if (eql(ud
->action
, "set_sensitive"))
2222 ud
->fn
= update_sensitivity
;
2223 else if (eql(ud
->action
, "set_visible"))
2224 ud
->fn
= update_visibility
;
2225 else if (eql(ud
->action
, "set_size_request"))
2226 ud
->fn
= update_size_request
;
2227 else if (eql(ud
->action
, "set_tooltip_text"))
2228 ud
->fn
= update_tooltip_text
;
2229 else if (eql(ud
->action
, "grab_focus"))
2230 ud
->fn
= update_focus
;
2231 else if (eql(ud
->action
, "style")) {
2233 ud
->fn
= update_widget_style
;
2234 } else if (ud
->type
== GTK_TYPE_DRAWING_AREA
)
2235 ud
->fn
= update_drawing_area
;
2236 else if (ud
->type
== GTK_TYPE_TREE_VIEW
)
2237 ud
->fn
= update_tree_view
;
2238 else if (ud
->type
== GTK_TYPE_COMBO_BOX_TEXT
)
2239 ud
->fn
= update_combo_box_text
;
2240 else if (ud
->type
== GTK_TYPE_LABEL
)
2241 ud
->fn
= update_label
;
2242 else if (ud
->type
== GTK_TYPE_IMAGE
)
2243 ud
->fn
= update_image
;
2244 else if (ud
->type
== GTK_TYPE_TEXT_VIEW
)
2245 ud
->fn
= update_text_view
;
2246 else if (ud
->type
== GTK_TYPE_NOTEBOOK
)
2247 ud
->fn
= update_notebook
;
2248 else if (ud
->type
== GTK_TYPE_EXPANDER
)
2249 ud
->fn
= update_expander
;
2250 else if (ud
->type
== GTK_TYPE_FRAME
)
2251 ud
->fn
= update_frame
;
2252 else if (ud
->type
== GTK_TYPE_SCROLLED_WINDOW
)
2253 ud
->fn
= update_scrolled_window
;
2254 else if (ud
->type
== GTK_TYPE_BUTTON
)
2255 ud
->fn
= update_button
;
2256 else if (ud
->type
== GTK_TYPE_FILE_CHOOSER_DIALOG
)
2257 ud
->fn
= update_file_chooser_dialog
;
2258 else if (ud
->type
== GTK_TYPE_FILE_CHOOSER_BUTTON
)
2259 ud
->fn
= update_file_chooser_button
;
2260 else if (ud
->type
== GTK_TYPE_COLOR_BUTTON
)
2261 ud
->fn
= update_color_button
;
2262 else if (ud
->type
== GTK_TYPE_FONT_BUTTON
)
2263 ud
->fn
= update_font_button
;
2264 else if (ud
->type
== GTK_TYPE_PRINT_UNIX_DIALOG
)
2265 ud
->fn
= update_print_dialog
;
2266 else if (ud
->type
== GTK_TYPE_SWITCH
)
2267 ud
->fn
= update_switch
;
2268 else if (ud
->type
== GTK_TYPE_TOGGLE_BUTTON
||
2269 ud
->type
== GTK_TYPE_RADIO_BUTTON
||
2270 ud
->type
== GTK_TYPE_CHECK_BUTTON
)
2271 ud
->fn
= update_toggle_button
;
2272 else if (ud
->type
== GTK_TYPE_SPIN_BUTTON
||
2273 ud
->type
== GTK_TYPE_ENTRY
)
2274 ud
->fn
= update_entry
;
2275 else if (ud
->type
== GTK_TYPE_SCALE
)
2276 ud
->fn
= update_scale
;
2277 else if (ud
->type
== GTK_TYPE_PROGRESS_BAR
)
2278 ud
->fn
= update_progress_bar
;
2279 else if (ud
->type
== GTK_TYPE_SPINNER
)
2280 ud
->fn
= update_spinner
;
2281 else if (ud
->type
== GTK_TYPE_STATUSBAR
)
2282 ud
->fn
= update_statusbar
;
2283 else if (ud
->type
== GTK_TYPE_CALENDAR
)
2284 ud
->fn
= update_calendar
;
2285 else if (ud
->type
== GTK_TYPE_SOCKET
)
2286 ud
->fn
= update_socket
;
2287 else if (ud
->type
== GTK_TYPE_WINDOW
||
2288 ud
->type
== GTK_TYPE_DIALOG
)
2289 ud
->fn
= update_window
;
2293 pthread_testcancel();
2294 gdk_threads_add_timeout(0, (GSourceFunc
) update_ui
, ud
);
2301 * ============================================================
2303 * ============================================================
2307 * Callbacks that forward a modification of a tree view cell to the
2311 cb_tree_model_edit(GtkCellRenderer
*renderer
, const gchar
*path_s
,
2312 const gchar
*new_text
, gpointer model
)
2318 gtk_tree_model_get_iter_from_string(model
, &iter
, path_s
);
2319 view
= g_object_get_data(G_OBJECT(renderer
), "tree_view");
2320 col
= g_object_get_data(G_OBJECT(renderer
), "col_number");
2321 set_tree_view_cell(model
, &iter
, path_s
, GPOINTER_TO_INT(col
),
2323 send_tree_cell_msg_by(send_msg
, model
, path_s
, &iter
, GPOINTER_TO_INT(col
),
2324 GTK_BUILDABLE(view
));
2328 cb_tree_model_toggle(GtkCellRenderer
*renderer
, gchar
*path_s
, gpointer model
)
2334 gtk_tree_model_get_iter_from_string(model
, &iter
, path_s
);
2335 col
= g_object_get_data(G_OBJECT(renderer
), "col_number");
2336 gtk_tree_model_get(model
, &iter
, col
, &toggle_state
, -1);
2337 set_tree_view_cell(model
, &iter
, path_s
, GPOINTER_TO_INT(col
),
2338 toggle_state
? "0" : "1");
2342 * Attach to renderer key "col_number". Associate "col_number" with
2343 * the corresponding column number in the underlying model.
2344 * Due to what looks like a gap in the GTK API, renderer id and column
2345 * number are taken directly from the XML .ui file.
2348 tree_view_column_get_renderer_column(const char *ui_file
, GtkTreeViewColumn
*t_col
,
2349 int n
, GtkCellRenderer
**renderer
)
2352 xmlXPathContextPtr xpath_ctx
;
2353 xmlXPathObjectPtr xpath_obj
;
2354 xmlNodeSetPtr nodes
;
2357 xmlChar
*xpath
, *renderer_name
= NULL
, *m_col_s
= NULL
;
2358 char *xpath_base1
= "//object[@class=\"GtkTreeViewColumn\" and @id=\"";
2359 const char *xpath_id
= widget_name(GTK_BUILDABLE(t_col
));
2360 char *xpath_base2
= "\"]/child[";
2361 size_t xpath_n_len
= 3; /* Big Enough (TM) */
2362 char *xpath_base3
= "]/object[@class=\"GtkCellRendererText\""
2363 " or @class=\"GtkCellRendererToggle\"]/";
2364 char *xpath_text_col
= "../attributes/attribute[@name=\"text\""
2365 " or @name=\"active\"]";
2366 char *xpath_renderer_id
= "/@id";
2370 if ((doc
= xmlParseFile(ui_file
)) == NULL
)
2372 if ((xpath_ctx
= xmlXPathNewContext(doc
)) == NULL
) {
2376 xpath_len
= 2 * (strlen(xpath_base1
) + strlen(xpath_id
) +
2377 strlen(xpath_base2
) + xpath_n_len
+
2378 strlen(xpath_base3
))
2380 + strlen(xpath_text_col
) + strlen(xpath_renderer_id
)
2382 if ((xpath
= malloc(xpath_len
)) == NULL
) {
2386 snprintf((char *) xpath
, xpath_len
, "%s%s%s%d%s%s|%s%s%s%d%s%s",
2387 xpath_base1
, xpath_id
, xpath_base2
, n
, xpath_base3
, xpath_text_col
,
2388 xpath_base1
, xpath_id
, xpath_base2
, n
, xpath_base3
, xpath_renderer_id
);
2389 if ((xpath_obj
= xmlXPathEvalExpression(xpath
, xpath_ctx
)) == NULL
) {
2390 xmlXPathFreeContext(xpath_ctx
);
2395 if ((nodes
= xpath_obj
->nodesetval
) != NULL
) {
2396 for (i
= 0; i
< nodes
->nodeNr
; ++i
) {
2397 if (nodes
->nodeTab
[i
]->type
== XML_ELEMENT_NODE
) {
2398 cur
= nodes
->nodeTab
[i
];
2399 m_col_s
= xmlNodeGetContent(cur
);
2401 cur
= nodes
->nodeTab
[i
];
2402 renderer_name
= xmlNodeGetContent(cur
);
2406 if (renderer_name
) {
2407 *renderer
= GTK_CELL_RENDERER(
2408 gtk_builder_get_object(builder
, (char *) renderer_name
));
2410 g_object_set_data(G_OBJECT(*renderer
), "col_number",
2411 GINT_TO_POINTER(strtol((char *) m_col_s
,
2416 xmlFree(renderer_name
);
2418 xmlXPathFreeObject(xpath_obj
);
2419 xmlXPathFreeContext(xpath_ctx
);
2426 connect_widget_signals(gpointer
*obj
, char *ui_file
)
2428 const char *name
= NULL
;
2429 char *suffix
= NULL
;
2431 GType type
= G_TYPE_INVALID
;
2433 type
= G_TYPE_FROM_INSTANCE(obj
);
2434 if (GTK_IS_BUILDABLE(obj
))
2435 name
= widget_name(GTK_BUILDABLE(obj
));
2436 if (type
== GTK_TYPE_TREE_VIEW_COLUMN
) {
2437 gboolean editable
= FALSE
;
2439 GtkTreeModel
*model
;
2440 GtkCellRenderer
*renderer
;
2443 g_signal_connect(obj
, "clicked", G_CALLBACK(cb_simple
), "clicked");
2444 view
= GTK_TREE_VIEW(gtk_tree_view_column_get_tree_view(GTK_TREE_VIEW_COLUMN(obj
)));
2445 model
= gtk_tree_view_get_model(view
);
2447 if (!tree_view_column_get_renderer_column(ui_file
, GTK_TREE_VIEW_COLUMN(obj
), i
, &renderer
))
2449 g_object_set_data(G_OBJECT(renderer
), "tree_view", view
);
2450 if (GTK_IS_CELL_RENDERER_TEXT(renderer
)) {
2451 g_object_get(renderer
, "editable", &editable
, NULL
);
2453 g_signal_connect(renderer
, "edited", G_CALLBACK(cb_tree_model_edit
), model
);
2454 } else if (GTK_IS_CELL_RENDERER_TOGGLE(renderer
)) {
2455 g_object_get(renderer
, "activatable", &editable
, NULL
);
2457 g_signal_connect(renderer
, "toggled", G_CALLBACK(cb_tree_model_toggle
), model
);
2461 else if (type
== GTK_TYPE_BUTTON
) {
2462 /* Button associated with a GtkTextView. */
2463 if ((suffix
= strstr(name
, "_send_text")) != NULL
&&
2464 GTK_IS_TEXT_VIEW(obj2
= obj_sans_suffix(suffix
, name
)))
2465 g_signal_connect(obj
, "clicked", G_CALLBACK(cb_send_text
),
2466 gtk_text_view_get_buffer(GTK_TEXT_VIEW(obj2
)));
2467 else if ((suffix
= strstr(name
, "_send_selection")) != NULL
&&
2468 GTK_IS_TEXT_VIEW(obj2
= obj_sans_suffix(suffix
, name
)))
2469 g_signal_connect(obj
, "clicked", G_CALLBACK(cb_send_text_selection
),
2470 gtk_text_view_get_buffer(GTK_TEXT_VIEW(obj2
)));
2472 g_signal_connect(obj
, "clicked", G_CALLBACK(cb_simple
), "clicked");
2473 /* Buttons associated with (and part of) a GtkDialog.
2474 * (We shun response ids which could be returned from
2475 * gtk_dialog_run() because that would require the
2476 * user to define those response ids in Glade,
2478 if ((suffix
= strstr(name
, "_cancel")) != NULL
&&
2479 GTK_IS_DIALOG(obj2
= obj_sans_suffix(suffix
, name
)))
2480 if (eql(widget_name(GTK_BUILDABLE(obj2
)), MAIN_WIN
))
2481 g_signal_connect_swapped(obj
, "clicked", G_CALLBACK(gtk_main_quit
), NULL
);
2483 g_signal_connect_swapped(obj
, "clicked", G_CALLBACK(gtk_widget_hide
), obj2
);
2484 else if ((suffix
= strstr(name
, "_ok")) != NULL
&&
2485 GTK_IS_DIALOG(obj2
= obj_sans_suffix(suffix
, name
))) {
2486 if (GTK_IS_FILE_CHOOSER_DIALOG(obj2
))
2487 g_signal_connect_swapped(obj
, "clicked", G_CALLBACK(cb_send_file_chooser_dialog_selection
), GTK_FILE_CHOOSER(obj2
));
2488 if (eql(widget_name(GTK_BUILDABLE(obj2
)), MAIN_WIN
))
2489 g_signal_connect_swapped(obj
, "clicked", G_CALLBACK(gtk_main_quit
), NULL
);
2491 g_signal_connect_swapped(obj
, "clicked", G_CALLBACK(gtk_widget_hide
), obj2
);
2492 } else if ((suffix
= strstr(name
, "_apply")) != NULL
&&
2493 GTK_IS_FILE_CHOOSER_DIALOG(obj2
= obj_sans_suffix(suffix
, name
)))
2494 g_signal_connect_swapped(obj
, "clicked", G_CALLBACK(cb_send_file_chooser_dialog_selection
), obj2
);
2496 } else if (GTK_IS_MENU_ITEM(obj
))
2497 if ((suffix
= strstr(name
, "_invoke")) != NULL
&&
2498 GTK_IS_DIALOG(obj2
= obj_sans_suffix(suffix
, name
)))
2499 g_signal_connect_swapped(obj
, "activate", G_CALLBACK(gtk_widget_show
), obj2
);
2501 g_signal_connect(obj
, "activate", G_CALLBACK(cb_menu_item
), "active");
2502 else if (GTK_IS_WINDOW(obj
)) {
2503 g_signal_connect(obj
, "delete-event", G_CALLBACK(cb_event_simple
), "closed");
2504 if (eql(name
, MAIN_WIN
))
2505 g_signal_connect_swapped(obj
, "delete-event", G_CALLBACK(gtk_main_quit
), NULL
);
2507 g_signal_connect(obj
, "delete-event", G_CALLBACK(gtk_widget_hide_on_delete
), NULL
);
2508 } else if (type
== GTK_TYPE_FILE_CHOOSER_BUTTON
)
2509 g_signal_connect(obj
, "file-set", G_CALLBACK(cb_file_chooser_button
), "file");
2510 else if (type
== GTK_TYPE_COLOR_BUTTON
)
2511 g_signal_connect(obj
, "color-set", G_CALLBACK(cb_color_button
), "color");
2512 else if (type
== GTK_TYPE_FONT_BUTTON
)
2513 g_signal_connect(obj
, "font-set", G_CALLBACK(cb_font_button
), "font");
2514 else if (type
== GTK_TYPE_SWITCH
)
2515 g_signal_connect(obj
, "notify::active", G_CALLBACK(cb_switch
), NULL
);
2516 else if (type
== GTK_TYPE_TOGGLE_BUTTON
|| type
== GTK_TYPE_RADIO_BUTTON
|| type
== GTK_TYPE_CHECK_BUTTON
)
2517 g_signal_connect(obj
, "toggled", G_CALLBACK(cb_toggle_button
), NULL
);
2518 else if (type
== GTK_TYPE_SPIN_BUTTON
|| type
== GTK_TYPE_ENTRY
)
2519 g_signal_connect(obj
, "changed", G_CALLBACK(cb_editable
), "text");
2520 else if (type
== GTK_TYPE_SCALE
)
2521 g_signal_connect(obj
, "value-changed", G_CALLBACK(cb_range
), "value");
2522 else if (type
== GTK_TYPE_CALENDAR
) {
2523 g_signal_connect(obj
, "day-selected-double-click", G_CALLBACK(cb_calendar
), "doubleclicked");
2524 g_signal_connect(obj
, "day-selected", G_CALLBACK(cb_calendar
), "clicked");
2525 } else if (type
== GTK_TYPE_TREE_SELECTION
)
2526 g_signal_connect(obj
, "changed", G_CALLBACK(cb_tree_selection
), "clicked");
2527 else if (type
== GTK_TYPE_SOCKET
) {
2528 g_signal_connect(obj
, "plug-added", G_CALLBACK(cb_simple
), "plug-added");
2529 g_signal_connect(obj
, "plug-removed", G_CALLBACK(cb_simple
), "plug-removed");
2530 } else if (type
== GTK_TYPE_DRAWING_AREA
)
2531 g_signal_connect(obj
, "draw", G_CALLBACK(cb_draw
), NULL
);
2532 else if (type
== GTK_TYPE_EVENT_BOX
) {
2533 gtk_widget_set_can_focus(GTK_WIDGET(obj
), true);
2534 g_signal_connect(obj
, "button-press-event", G_CALLBACK(cb_event_box_button
), "button_press");
2535 g_signal_connect(obj
, "button-release-event", G_CALLBACK(cb_event_box_button
), "button_release");
2536 g_signal_connect(obj
, "motion-notify-event", G_CALLBACK(cb_event_box_motion
), "motion");
2537 g_signal_connect(obj
, "key-press-event", G_CALLBACK(cb_event_box_key
), "key_press");
2542 * We keep a style provider with each widget
2545 add_widget_style_provider(gpointer
*obj
, void *data
)
2547 GtkStyleContext
*context
;
2548 GtkCssProvider
*style_provider
;
2551 if (!GTK_IS_WIDGET(obj
))
2553 style_provider
= gtk_css_provider_new();
2554 context
= gtk_widget_get_style_context(GTK_WIDGET(obj
));
2555 gtk_style_context_add_provider(context
,
2556 GTK_STYLE_PROVIDER(style_provider
),
2557 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
);
2558 g_object_set_data(G_OBJECT(obj
), "style_provider", style_provider
);
2562 prepare_widgets(char *ui_file
)
2564 GSList
*objects
= NULL
;
2566 objects
= gtk_builder_get_objects(builder
);
2567 g_slist_foreach(objects
, (GFunc
) connect_widget_signals
, ui_file
);
2568 g_slist_foreach(objects
, (GFunc
) add_widget_style_provider
, NULL
);
2569 g_slist_free(objects
);
2573 main(int argc
, char *argv
[])
2576 char *in_fifo
= NULL
, *out_fifo
= NULL
, *ui_file
= NULL
;
2577 char *log_name
= NULL
;
2578 char *xid_s
= NULL
, xid_s2
[BUFLEN
];
2580 GtkWidget
*plug
, *body
;
2582 GError
*error
= NULL
;
2583 GObject
*main_window
= NULL
;
2584 FILE *in
= NULL
; /* command input */
2586 /* Disable runtime GLIB deprecation warnings: */
2587 setenv("G_ENABLE_DIAGNOSTIC", "0", 0);
2591 gtk_init(&argc
, &argv
);
2592 while ((opt
= getopt(argc
, argv
, "he:i:l:o:u:GV")) != -1) {
2594 case 'e': xid_s
= optarg
; break;
2595 case 'i': in_fifo
= optarg
; break;
2596 case 'l': log_name
= optarg
; break;
2597 case 'o': out_fifo
= optarg
; break;
2598 case 'u': ui_file
= optarg
; break;
2599 case 'G': bye(EXIT_SUCCESS
, stdout
,
2600 "GTK+ v%d.%d.%d (running v%d.%d.%d)\n"
2601 "cairo v%s (running v%s)\n",
2602 GTK_MAJOR_VERSION
, GTK_MINOR_VERSION
, GTK_MICRO_VERSION
,
2603 gtk_get_major_version(), gtk_get_minor_version(),
2604 gtk_get_micro_version(),
2605 CAIRO_VERSION_STRING
, cairo_version_string());
2607 case 'V': bye(EXIT_SUCCESS
, stdout
, "%s\n", VERSION
); break;
2608 case 'h': bye(EXIT_SUCCESS
, stdout
, USAGE
); break;
2610 default: bye(EXIT_FAILURE
, stderr
, USAGE
); break;
2613 if (argv
[optind
] != NULL
)
2614 bye(EXIT_FAILURE
, stderr
,
2615 "illegal parameter '%s'\n" USAGE
, argv
[optind
]);
2616 in
= fifo(in_fifo
, "r");
2617 out
= fifo(out_fifo
, "w");
2618 if (ui_file
== NULL
)
2619 ui_file
= "pipeglade.ui";
2620 builder
= gtk_builder_new();
2621 if (gtk_builder_add_from_file(builder
, ui_file
, &error
) == 0)
2622 bye(EXIT_FAILURE
, stderr
, "%s\n", error
->message
);
2623 log_out
= log_file(log_name
);
2624 pthread_create(&receiver
, NULL
, (void *(*)(void *)) digest_msg
, in
);
2625 main_window
= gtk_builder_get_object(builder
, MAIN_WIN
);
2626 if (!GTK_IS_WINDOW(main_window
))
2627 bye(EXIT_FAILURE
, stderr
,
2628 "no toplevel window named \'" MAIN_WIN
"\'\n");
2630 LIBXML_TEST_VERSION
;
2631 prepare_widgets(ui_file
);
2632 if (xid_s
== NULL
) /* standalone */
2633 gtk_widget_show(GTK_WIDGET(main_window
));
2634 else { /* We're being XEmbedded */
2635 xid
= strtoul(xid_s
, NULL
, 10);
2636 snprintf(xid_s2
, BUFLEN
, "%lu", xid
);
2637 if (!eql(xid_s
, xid_s2
))
2638 bye(EXIT_FAILURE
, stderr
,
2639 "%s is not a valid XEmbed socket id\n", xid_s
);
2640 body
= gtk_bin_get_child(GTK_BIN(main_window
));
2641 gtk_container_remove(GTK_CONTAINER(main_window
), body
);
2642 plug
= gtk_plug_new(xid
);
2643 if (!gtk_plug_get_embedded(GTK_PLUG(plug
)))
2644 bye(EXIT_FAILURE
, stderr
,
2645 "unable to embed into XEmbed socket %s\n", xid_s
);
2646 gtk_container_add(GTK_CONTAINER(plug
), body
);
2647 gtk_widget_show(plug
);
2654 if (out
!= stdout
) {
2658 pthread_cancel(receiver
);
2659 pthread_join(receiver
, NULL
);