Root/
Source at commit d4c4031b9afeb0a1509767bcbfec8a5248cb9866 created 13 years 9 months ago. By werner, Added support for reordering frames in the GUI. | |
---|---|
1 | /* |
2 | * gui_status.c - GUI, status area |
3 | * |
4 | * Written 2009, 2010 by Werner Almesberger |
5 | * Copyright 2009, 2010 by Werner Almesberger |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by |
9 | * the Free Software Foundation; either version 2 of the License, or |
10 | * (at your option) any later version. |
11 | */ |
12 | |
13 | |
14 | #include <stdarg.h> |
15 | #include <stdlib.h> |
16 | #include <stdio.h> |
17 | #include <string.h> |
18 | #include <assert.h> |
19 | #include <gtk/gtk.h> |
20 | #include <gdk/gdkkeysyms.h> |
21 | |
22 | #include "util.h" |
23 | #include "coord.h" |
24 | #include "error.h" |
25 | #include "unparse.h" |
26 | #include "obj.h" |
27 | #include "gui_util.h" |
28 | #include "gui_style.h" |
29 | #include "gui_canvas.h" |
30 | #include "gui.h" |
31 | #include "gui_status.h" |
32 | |
33 | |
34 | enum edit_status { |
35 | es_unchanged, |
36 | es_good, |
37 | es_bad, |
38 | }; |
39 | |
40 | |
41 | struct edit_ops { |
42 | char *(*retrieve)(void *ctx); |
43 | enum edit_status (*status)(const char *s, void *ctx); |
44 | void (*store)(const char *s, void *ctx); |
45 | }; |
46 | |
47 | |
48 | enum curr_unit curr_unit = curr_unit_mm; |
49 | |
50 | |
51 | static GtkWidget *open_edits = NULL; |
52 | static GtkWidget *last_edit = NULL; |
53 | |
54 | |
55 | /* ----- setter functions -------------------------------------------------- */ |
56 | |
57 | |
58 | static GtkWidget *status_icon; |
59 | static GtkWidget *status_name, *status_entry; |
60 | static GtkWidget *status_type_x, *status_type_y, *status_type_entry; |
61 | static GtkWidget *status_box_x, *status_entry_y; |
62 | static GtkWidget *status_x, *status_y; |
63 | static GtkWidget *status_r, *status_angle; |
64 | static GtkWidget *status_sys_x, *status_sys_y; |
65 | static GtkWidget *status_user_x, *status_user_y; |
66 | static GtkWidget *status_zoom, *status_grid, *status_unit; |
67 | static GtkWidget *status_msg; |
68 | |
69 | /* The x entry area serves multiple purposes */ |
70 | |
71 | static GtkWidget *status_entry_x; |
72 | |
73 | |
74 | static void set_label(GtkWidget *label, const char *tooltip, |
75 | const char *fmt, va_list ap) |
76 | { |
77 | char *s; |
78 | |
79 | s = stralloc_vprintf(fmt, ap); |
80 | gtk_label_set_text(GTK_LABEL(label), s); |
81 | gtk_widget_set_tooltip_markup(label, tooltip); |
82 | free(s); |
83 | } |
84 | |
85 | |
86 | #define SETTER(name) \ |
87 | void status_set_##name(const char *tooltip, const char *fmt, ...)\ |
88 | { \ |
89 | va_list ap; \ |
90 | \ |
91 | va_start(ap, fmt); \ |
92 | set_label(status_##name, tooltip, fmt, ap); \ |
93 | va_end(ap); \ |
94 | } |
95 | |
96 | SETTER(type_x) |
97 | SETTER(type_y) |
98 | SETTER(type_entry) |
99 | SETTER(name) |
100 | SETTER(x) |
101 | SETTER(y) |
102 | SETTER(r) |
103 | SETTER(angle) |
104 | SETTER(sys_x) |
105 | SETTER(sys_y) |
106 | SETTER(user_x) |
107 | SETTER(user_y) |
108 | SETTER(zoom) |
109 | SETTER(grid) |
110 | SETTER(unit) |
111 | |
112 | |
113 | /* ----- set things with units --------------------------------------------- */ |
114 | |
115 | |
116 | void set_with_units(void (*set)(const char *tooltip, const char *fmt, ...), |
117 | const char *prefix, unit_type u, const char *tooltip) |
118 | { |
119 | double n; |
120 | int mm; |
121 | |
122 | switch (curr_unit) { |
123 | case curr_unit_mm: |
124 | n = units_to_mm(u); |
125 | mm = 1; |
126 | break; |
127 | case curr_unit_mil: |
128 | n = units_to_mil(u); |
129 | mm = 0; |
130 | break; |
131 | case curr_unit_auto: |
132 | n = units_to_best(u, &mm); |
133 | break; |
134 | default: |
135 | abort(); |
136 | } |
137 | if (mm) { |
138 | /* -NNN.NNN mm */ |
139 | set(tooltip, "%s" MM_FORMAT_FIXED " mm", prefix, n); |
140 | } else { |
141 | /* -NNNN.N mil */ |
142 | set(tooltip, "%s" MIL_FORMAT_FIXED " mil", prefix, n); |
143 | } |
144 | } |
145 | |
146 | |
147 | /* ----- complex status updates -------------------------------------------- */ |
148 | |
149 | |
150 | void status_set_icon(GtkWidget *image) |
151 | { |
152 | vacate_widget(status_icon); |
153 | if (image) |
154 | gtk_container_add(GTK_CONTAINER(status_icon), image); |
155 | gtk_widget_show_all(status_icon); |
156 | } |
157 | |
158 | |
159 | void status_set_xy(struct coord coord) |
160 | { |
161 | /* do dX/dY etc. stuff later */ |
162 | status_set_type_x(NULL, "X ="); |
163 | status_set_type_y(NULL, "Y ="); |
164 | |
165 | set_with_units(status_set_x, "", coord.x, "Width"); |
166 | set_with_units(status_set_y, "", coord.y, "Height"); |
167 | } |
168 | |
169 | |
170 | void status_set_angle_xy(const char *tooltip, struct coord v) |
171 | { |
172 | if (!v.x && !v.y) |
173 | status_set_angle(tooltip, "a = 0 deg"); |
174 | else |
175 | status_set_angle(tooltip, "a = %3.1f deg", theta_vec(v)); |
176 | |
177 | } |
178 | |
179 | |
180 | static void entry_color(GtkWidget *widget, const char *color) |
181 | { |
182 | GdkColor col; |
183 | |
184 | col = get_color(color); |
185 | gtk_widget_modify_base(widget, GTK_STATE_NORMAL, &col); |
186 | } |
187 | |
188 | |
189 | /* ----- pad type display and change --------------------------------------- */ |
190 | |
191 | |
192 | static enum pad_type *curr_pad_type; |
193 | static GtkWidget *pad_type; |
194 | |
195 | |
196 | static void show_pad_type(void) |
197 | { |
198 | const char *s; |
199 | |
200 | switch (*curr_pad_type) { |
201 | case pt_normal: |
202 | s = "normal"; |
203 | break; |
204 | case pt_bare: |
205 | s = "bare"; |
206 | break; |
207 | case pt_paste: |
208 | s = "paste"; |
209 | break; |
210 | case pt_mask: |
211 | s = "mask"; |
212 | break; |
213 | default: |
214 | abort(); |
215 | } |
216 | gtk_label_set_text(GTK_LABEL(pad_type), s); |
217 | } |
218 | |
219 | |
220 | static gboolean pad_type_button_press_event(GtkWidget *widget, |
221 | GdkEventButton *event, gpointer data) |
222 | { |
223 | switch (event->button) { |
224 | case 1: |
225 | *curr_pad_type = (*curr_pad_type+1) % pt_n; |
226 | show_pad_type(); |
227 | break; |
228 | } |
229 | /* |
230 | * We can't just redraw() here, because changing the pad type may also |
231 | * affect the visual stacking. So we change the world and hope we end |
232 | * up selecting the same pad afterwards. |
233 | */ |
234 | change_world_reselect(); |
235 | return TRUE; |
236 | } |
237 | |
238 | |
239 | void edit_pad_type(enum pad_type *type) |
240 | { |
241 | vacate_widget(status_box_x); |
242 | curr_pad_type = type; |
243 | pad_type = label_in_box_new(NULL, "Pad type. Click to cycle."); |
244 | gtk_container_add(GTK_CONTAINER(status_box_x), box_of_label(pad_type)); |
245 | label_in_box_bg(pad_type, COLOR_SELECTOR); |
246 | g_signal_connect(G_OBJECT(box_of_label(pad_type)), |
247 | "button_press_event", G_CALLBACK(pad_type_button_press_event), |
248 | NULL); |
249 | show_pad_type(); |
250 | gtk_widget_show_all(status_box_x); |
251 | } |
252 | |
253 | |
254 | /* ----- edit helper functions --------------------------------------------- */ |
255 | |
256 | |
257 | static void reset_edit(GtkWidget *widget) |
258 | { |
259 | struct edit_ops *ops; |
260 | void *ctx; |
261 | char *s; |
262 | |
263 | ops = gtk_object_get_data(GTK_OBJECT(widget), "edit-ops"); |
264 | ctx = gtk_object_get_data(GTK_OBJECT(widget), "edit-ctx"); |
265 | assert(ops); |
266 | s = ops->retrieve(ctx); |
267 | gtk_object_set_data(GTK_OBJECT(widget), "edit-ops", NULL); |
268 | gtk_entry_set_text(GTK_ENTRY(widget), s); |
269 | free(s); |
270 | entry_color(widget, COLOR_EDIT_ASIS); |
271 | gtk_object_set_data(GTK_OBJECT(widget), "edit-ops", ops); |
272 | } |
273 | |
274 | |
275 | static void reset_edits(void) |
276 | { |
277 | GtkWidget *edit; |
278 | |
279 | for (edit = open_edits; edit; |
280 | edit = gtk_object_get_data(GTK_OBJECT(edit), "edit-next")) |
281 | reset_edit(edit); |
282 | gtk_widget_grab_focus(GTK_WIDGET(open_edits)); |
283 | } |
284 | |
285 | |
286 | static gboolean edit_key_press_event(GtkWidget *widget, GdkEventKey *event, |
287 | gpointer data) |
288 | { |
289 | GtkWidget *next = gtk_object_get_data(GTK_OBJECT(widget), "edit-next"); |
290 | |
291 | switch (event->keyval) { |
292 | case GDK_Tab: |
293 | gtk_widget_grab_focus(GTK_WIDGET(next ? next : open_edits)); |
294 | return TRUE; |
295 | case GDK_Escape: |
296 | reset_edits(); |
297 | return TRUE; |
298 | default: |
299 | return FALSE; |
300 | } |
301 | } |
302 | |
303 | |
304 | static void setup_edit(GtkWidget *widget, struct edit_ops *ops, void *ctx, |
305 | const char *tooltip) |
306 | { |
307 | gtk_object_set_data(GTK_OBJECT(widget), "edit-ops", ops); |
308 | gtk_object_set_data(GTK_OBJECT(widget), "edit-ctx", ctx); |
309 | gtk_object_set_data(GTK_OBJECT(widget), "edit-next", NULL); |
310 | |
311 | reset_edit(widget); |
312 | |
313 | if (!open_edits) |
314 | gtk_widget_grab_focus(GTK_WIDGET(widget)); |
315 | gtk_widget_show(widget); |
316 | |
317 | g_signal_connect(G_OBJECT(widget), "key_press_event", |
318 | G_CALLBACK(edit_key_press_event), open_edits); |
319 | |
320 | if (last_edit) |
321 | gtk_object_set_data(GTK_OBJECT(last_edit), "edit-next", widget); |
322 | else |
323 | open_edits = widget; |
324 | last_edit = widget; |
325 | gtk_widget_set_tooltip_markup(widget, tooltip); |
326 | } |
327 | |
328 | |
329 | /* ----- identifier fields ------------------------------------------------- */ |
330 | |
331 | |
332 | struct edit_unique_ctx { |
333 | const char **s; |
334 | int (*validate)(const char *s, void *ctx); |
335 | void *ctx; |
336 | }; |
337 | |
338 | |
339 | /* |
340 | * Handle NULL so that we can also use it for unique_null |
341 | */ |
342 | |
343 | static char *unique_retrieve(void *ctx) |
344 | { |
345 | struct edit_unique_ctx *unique_ctx = ctx; |
346 | |
347 | return stralloc(*unique_ctx->s ? *unique_ctx->s : ""); |
348 | } |
349 | |
350 | |
351 | static enum edit_status unique_status(const char *s, void *ctx) |
352 | { |
353 | const struct edit_unique_ctx *unique_ctx = ctx; |
354 | |
355 | if (!strcmp(s, *unique_ctx->s)) |
356 | return es_unchanged; |
357 | return !unique_ctx->validate || |
358 | unique_ctx->validate(s, unique_ctx->ctx) ? es_good : es_bad; |
359 | } |
360 | |
361 | |
362 | static void unique_store(const char *s, void *ctx) |
363 | { |
364 | const struct edit_unique_ctx *unique_ctx = ctx; |
365 | |
366 | *unique_ctx->s = unique(s); |
367 | } |
368 | |
369 | |
370 | static struct edit_ops edit_ops_unique = { |
371 | .retrieve = unique_retrieve, |
372 | .status = unique_status, |
373 | .store = unique_store, |
374 | }; |
375 | |
376 | |
377 | void edit_unique(const char **s, int (*validate)(const char *s, void *ctx), |
378 | void *ctx, const char *tooltip) |
379 | { |
380 | static struct edit_unique_ctx unique_ctx; |
381 | |
382 | unique_ctx.s = s; |
383 | unique_ctx.validate = validate; |
384 | unique_ctx.ctx = ctx; |
385 | setup_edit(status_entry, &edit_ops_unique, &unique_ctx, tooltip); |
386 | } |
387 | |
388 | |
389 | /* ----- identifier fields with NULL --------------------------------------- */ |
390 | |
391 | |
392 | static enum edit_status unique_null_status(const char *s, void *ctx) |
393 | { |
394 | const struct edit_unique_ctx *unique_ctx = ctx; |
395 | |
396 | if (!strcmp(s, *unique_ctx->s ? *unique_ctx->s : "")) |
397 | return es_unchanged; |
398 | if (!*s) |
399 | return es_good; |
400 | return !unique_ctx->validate || |
401 | unique_ctx->validate(s, unique_ctx->ctx) ? es_good : es_bad; |
402 | } |
403 | |
404 | |
405 | static void unique_null_store(const char *s, void *ctx) |
406 | { |
407 | const struct edit_unique_ctx *unique_ctx = ctx; |
408 | |
409 | if (!*s) |
410 | *unique_ctx->s = NULL; |
411 | else |
412 | *unique_ctx->s = unique(s); |
413 | } |
414 | |
415 | |
416 | static struct edit_ops edit_ops_null_unique = { |
417 | .retrieve = unique_retrieve, |
418 | .status = unique_null_status, |
419 | .store = unique_null_store, |
420 | }; |
421 | |
422 | |
423 | void edit_unique_null(const char **s, |
424 | int (*validate)(const char *s, void *ctx), void *ctx, const char *tooltip) |
425 | { |
426 | static struct edit_unique_ctx unique_ctx; |
427 | |
428 | unique_ctx.s = s; |
429 | unique_ctx.validate = validate; |
430 | unique_ctx.ctx = ctx; |
431 | setup_edit(status_entry, &edit_ops_null_unique, &unique_ctx, tooltip); |
432 | } |
433 | |
434 | |
435 | /* ----- unique field (variable) optionally followed by values ------------- */ |
436 | |
437 | |
438 | struct edit_unique_with_values_ctx { |
439 | const char **s; |
440 | int (*validate)(const char *s, void *ctx); |
441 | void *ctx; |
442 | void (*set_values)(void *user, const struct value *values, |
443 | int n_values); |
444 | void *user; |
445 | int max_values; |
446 | }; |
447 | |
448 | |
449 | static enum edit_status unique_with_values_status(const char *s, void *ctx) |
450 | { |
451 | const struct edit_unique_with_values_ctx *unique_ctx = ctx; |
452 | const char *id; |
453 | struct value *values; |
454 | int n; |
455 | |
456 | if (!strcmp(s, *unique_ctx->s)) |
457 | return es_unchanged; |
458 | status_begin_reporting(); |
459 | n = parse_var(s, &id, &values, unique_ctx->max_values); |
460 | if (n < 0) |
461 | return es_bad; |
462 | free_values(values, 0); |
463 | return !unique_ctx->validate || |
464 | unique_ctx->validate(id, unique_ctx->ctx) ? es_good : es_bad; |
465 | } |
466 | |
467 | |
468 | static void unique_with_values_store(const char *s, void *ctx) |
469 | { |
470 | const struct edit_unique_with_values_ctx *unique_ctx = ctx; |
471 | struct value *values; |
472 | int n; |
473 | |
474 | status_begin_reporting(); |
475 | n = parse_var(s, unique_ctx->s, &values, unique_ctx->max_values); |
476 | if (!n) |
477 | return; |
478 | assert(n >= 0); |
479 | assert(unique_ctx->max_values == -1 || n <= unique_ctx->max_values); |
480 | unique_ctx->set_values(unique_ctx->user, values, n); |
481 | free_values(values, 1); |
482 | } |
483 | |
484 | |
485 | static struct edit_ops edit_ops_unique_with_values = { |
486 | .retrieve = unique_retrieve, |
487 | .status = unique_with_values_status, |
488 | .store = unique_with_values_store, |
489 | }; |
490 | |
491 | |
492 | void edit_unique_with_values(const char **s, |
493 | int (*validate)(const char *s, void *ctx), void *ctx, |
494 | void (*set_values)(void *user, const struct value *values, int n_values), |
495 | void *user, int max_values, |
496 | const char *tooltip) |
497 | { |
498 | static struct edit_unique_with_values_ctx unique_ctx; |
499 | |
500 | unique_ctx.s = s; |
501 | unique_ctx.validate = validate; |
502 | unique_ctx.ctx = ctx; |
503 | unique_ctx.set_values = set_values; |
504 | unique_ctx.user = user; |
505 | unique_ctx.max_values = max_values; |
506 | setup_edit(status_entry, &edit_ops_unique_with_values, &unique_ctx, |
507 | tooltip); |
508 | } |
509 | |
510 | |
511 | /* ----- string fields ----------------------------------------------------- */ |
512 | |
513 | |
514 | struct edit_name_ctx { |
515 | char **s; |
516 | int (*validate)(const char *s, void *ctx); |
517 | void *ctx; |
518 | }; |
519 | |
520 | |
521 | static char *name_retrieve(void *ctx) |
522 | { |
523 | struct edit_name_ctx *name_ctx = ctx; |
524 | |
525 | return stralloc(*name_ctx->s ? *name_ctx->s : ""); |
526 | } |
527 | |
528 | |
529 | static enum edit_status name_status(const char *s, void *ctx) |
530 | { |
531 | const struct edit_name_ctx *name_ctx = ctx; |
532 | |
533 | if (!strcmp(s, *name_ctx->s)) |
534 | return es_unchanged; |
535 | return !name_ctx->validate || name_ctx->validate(s, name_ctx->ctx) ? |
536 | es_good : es_bad; |
537 | } |
538 | |
539 | |
540 | static void name_store(const char *s, void *ctx) |
541 | { |
542 | const struct edit_name_ctx *name_ctx = ctx; |
543 | |
544 | free(*name_ctx->s); |
545 | *name_ctx->s = stralloc(s); |
546 | } |
547 | |
548 | |
549 | static struct edit_ops edit_ops_name = { |
550 | .retrieve = name_retrieve, |
551 | .status = name_status, |
552 | .store = name_store, |
553 | }; |
554 | |
555 | |
556 | void edit_name(char **s, int (*validate)(const char *s, void *ctx), void *ctx, |
557 | const char *tooltip) |
558 | { |
559 | static struct edit_name_ctx name_ctx; |
560 | |
561 | name_ctx.s = s; |
562 | name_ctx.validate = validate; |
563 | name_ctx.ctx = ctx; |
564 | setup_edit(status_entry, &edit_ops_name, &name_ctx, tooltip); |
565 | } |
566 | |
567 | |
568 | /* ----- expression fields ------------------------------------------------- */ |
569 | |
570 | |
571 | static struct expr *try_parse_expr(const char *s) |
572 | { |
573 | status_begin_reporting(); |
574 | return parse_expr(s); |
575 | } |
576 | |
577 | |
578 | static char *expr_retrieve(void *ctx) |
579 | { |
580 | struct expr **expr = ctx; |
581 | |
582 | return unparse(*expr); |
583 | } |
584 | |
585 | |
586 | static enum edit_status expr_status(const char *s, void *ctx) |
587 | { |
588 | struct expr *expr; |
589 | |
590 | expr = try_parse_expr(s); |
591 | if (!expr) |
592 | return es_bad; |
593 | free_expr(expr); |
594 | return es_good; |
595 | } |
596 | |
597 | |
598 | static void expr_store(const char *s, void *ctx) |
599 | { |
600 | struct expr **anchor = ctx; |
601 | struct expr *expr; |
602 | |
603 | expr = try_parse_expr(s); |
604 | assert(expr); |
605 | if (*anchor) |
606 | free_expr(*anchor); |
607 | *anchor = expr; |
608 | } |
609 | |
610 | |
611 | static struct edit_ops edit_ops_expr = { |
612 | .retrieve = expr_retrieve, |
613 | .status = expr_status, |
614 | .store = expr_store, |
615 | }; |
616 | |
617 | |
618 | void edit_expr(struct expr **expr, const char *tooltip) |
619 | { |
620 | setup_edit(status_entry, &edit_ops_expr, expr, tooltip); |
621 | } |
622 | |
623 | |
624 | /* ----- distance expressions ---------------------------------------------- */ |
625 | |
626 | |
627 | static void dist_expr_store(const char *s, void *ctx) |
628 | { |
629 | struct expr **anchor = ctx; |
630 | struct expr *expr; |
631 | |
632 | expr_store(s, ctx); |
633 | expr = *anchor; |
634 | if (expr->op == op_num && !expr->u.num.exponent && !expr->u.num.n) |
635 | expr->u.num.exponent = 1; |
636 | } |
637 | |
638 | |
639 | static struct edit_ops edit_ops_dist_expr = { |
640 | .retrieve = expr_retrieve, |
641 | .status = expr_status, |
642 | .store = dist_expr_store, |
643 | }; |
644 | |
645 | |
646 | static void edit_any_dist_expr(GtkWidget *widget, struct expr **expr, |
647 | const char *tooltip) |
648 | { |
649 | setup_edit(widget, &edit_ops_dist_expr, expr, tooltip); |
650 | } |
651 | |
652 | |
653 | void edit_dist_expr(struct expr **expr, const char *tooltip) |
654 | { |
655 | edit_any_dist_expr(status_entry, expr, tooltip); |
656 | } |
657 | |
658 | |
659 | void edit_x(struct expr **expr, const char *tooltip) |
660 | { |
661 | vacate_widget(status_box_x); |
662 | gtk_container_add(GTK_CONTAINER(status_box_x), status_entry_x); |
663 | gtk_widget_show(status_box_x); |
664 | edit_any_dist_expr(status_entry_x, expr, tooltip); |
665 | } |
666 | |
667 | |
668 | void edit_y(struct expr **expr, const char *tooltip) |
669 | { |
670 | edit_any_dist_expr(status_entry_y, expr, tooltip); |
671 | } |
672 | |
673 | |
674 | /* ----- expression list --------------------------------------------------- */ |
675 | |
676 | |
677 | struct edit_expr_list_ctx { |
678 | struct expr *expr; |
679 | void (*set_values)(void *user, const struct value *values, |
680 | int n_values); |
681 | void *user; |
682 | }; |
683 | |
684 | |
685 | static char *expr_list_retrieve(void *ctx) |
686 | { |
687 | struct edit_expr_list_ctx *expr_list_ctx = ctx; |
688 | |
689 | return unparse(expr_list_ctx->expr); |
690 | } |
691 | |
692 | |
693 | static enum edit_status expr_list_status(const char *s, void *ctx) |
694 | { |
695 | struct value *values; |
696 | int n; |
697 | |
698 | status_begin_reporting(); |
699 | n = parse_values(s, &values); |
700 | if (n < 0) |
701 | return es_bad; |
702 | free_values(values, 0); |
703 | return es_good; |
704 | } |
705 | |
706 | |
707 | static void expr_list_store(const char *s, void *ctx) |
708 | { |
709 | struct edit_expr_list_ctx *expr_list_ctx = ctx; |
710 | struct value *values; |
711 | int n; |
712 | |
713 | status_begin_reporting(); |
714 | n = parse_values(s, &values); |
715 | assert(n >= 0); |
716 | expr_list_ctx->set_values(expr_list_ctx->user, values, n); |
717 | free_values(values, 1); |
718 | } |
719 | |
720 | |
721 | static struct edit_ops edit_ops_expr_list = { |
722 | .retrieve = expr_list_retrieve, |
723 | .status = expr_list_status, |
724 | .store = expr_list_store, |
725 | }; |
726 | |
727 | |
728 | void edit_expr_list(struct expr *expr, |
729 | void (*set_values)(void *user, const struct value *values, int n_values), |
730 | void *user, const char *tooltip) |
731 | { |
732 | static struct edit_expr_list_ctx expr_list_ctx; |
733 | |
734 | expr_list_ctx.expr = expr; |
735 | expr_list_ctx.set_values = set_values; |
736 | expr_list_ctx.user = user; |
737 | setup_edit(status_entry, &edit_ops_expr_list, &expr_list_ctx, tooltip); |
738 | } |
739 | |
740 | |
741 | /* ----- text entry -------------------------------------------------------- */ |
742 | |
743 | |
744 | static enum edit_status get_status(GtkWidget *widget) |
745 | { |
746 | struct edit_ops *ops; |
747 | void *ctx; |
748 | const char *s; |
749 | |
750 | ops = gtk_object_get_data(GTK_OBJECT(widget), "edit-ops"); |
751 | if (!ops) |
752 | return es_unchanged; |
753 | ctx = gtk_object_get_data(GTK_OBJECT(widget), "edit-ctx"); |
754 | s = gtk_entry_get_text(GTK_ENTRY(widget)); |
755 | return ops->status(s, ctx); |
756 | } |
757 | |
758 | |
759 | static void set_edit(GtkWidget *widget) |
760 | { |
761 | struct edit_ops *ops; |
762 | void *ctx; |
763 | const char *s; |
764 | |
765 | ops = gtk_object_get_data(GTK_OBJECT(widget), "edit-ops"); |
766 | if (!ops) |
767 | return; |
768 | ctx = gtk_object_get_data(GTK_OBJECT(widget), "edit-ctx"); |
769 | s = gtk_entry_get_text(GTK_ENTRY(widget)); |
770 | if (ops->store) |
771 | ops->store(s, ctx); |
772 | } |
773 | |
774 | |
775 | static gboolean changed(GtkWidget *widget, GdkEventMotion *event, |
776 | gpointer data) |
777 | { |
778 | switch (get_status(widget)) { |
779 | case es_unchanged: |
780 | entry_color(widget, COLOR_EDIT_ASIS); |
781 | break; |
782 | case es_good: |
783 | entry_color(widget, COLOR_EDIT_GOOD); |
784 | break; |
785 | case es_bad: |
786 | entry_color(widget, COLOR_EDIT_BAD); |
787 | break; |
788 | default: |
789 | abort(); |
790 | } |
791 | return TRUE; |
792 | } |
793 | |
794 | |
795 | static gboolean activate(GtkWidget *widget, GdkEventMotion *event, |
796 | gpointer data) |
797 | { |
798 | GtkWidget *edit; |
799 | enum edit_status status; |
800 | int unchanged = 1; |
801 | |
802 | for (edit = open_edits; edit; |
803 | edit = gtk_object_get_data(GTK_OBJECT(edit), "edit-next")) { |
804 | status = get_status(edit); |
805 | if (status == es_bad) |
806 | return TRUE; |
807 | if (status == es_good) |
808 | unchanged = 0; |
809 | } |
810 | if (unchanged) { |
811 | inst_deselect(); |
812 | return TRUE; |
813 | } |
814 | for (edit = open_edits; edit; |
815 | edit = gtk_object_get_data(GTK_OBJECT(edit), "edit-next")) |
816 | if (get_status(edit) == es_good) { |
817 | entry_color(edit, COLOR_EDIT_ASIS); |
818 | set_edit(edit); |
819 | } |
820 | inst_deselect(); |
821 | change_world(); |
822 | return TRUE; |
823 | } |
824 | |
825 | |
826 | void edit_nothing(void) |
827 | { |
828 | gtk_widget_hide(status_entry); |
829 | gtk_widget_hide(status_box_x); |
830 | gtk_widget_hide(status_entry_y); |
831 | open_edits = NULL; |
832 | last_edit = NULL; |
833 | } |
834 | |
835 | |
836 | /* ----- status reports ---------------------------------------------------- */ |
837 | |
838 | |
839 | static gint context_id; |
840 | static int have_msg = 0; |
841 | |
842 | |
843 | static void clear_status_msg(void) |
844 | { |
845 | if (have_msg) { |
846 | gtk_statusbar_pop(GTK_STATUSBAR(status_msg), context_id); |
847 | have_msg = 0; |
848 | } |
849 | } |
850 | |
851 | |
852 | static void report_to_gui(const char *s) |
853 | { |
854 | if (!have_msg) |
855 | gtk_statusbar_push(GTK_STATUSBAR(status_msg), context_id, s); |
856 | have_msg = 1; |
857 | } |
858 | |
859 | |
860 | void status_begin_reporting(void) |
861 | { |
862 | clear_status_msg(); |
863 | reporter = report_to_gui; |
864 | } |
865 | |
866 | |
867 | /* ----- unit selection ---------------------------------------------------- */ |
868 | |
869 | |
870 | static void show_curr_unit(void) |
871 | { |
872 | static const char *tip = "Display unit. Click to cycle."; |
873 | |
874 | switch (curr_unit) { |
875 | case curr_unit_mm: |
876 | status_set_unit(tip, "mm"); |
877 | break; |
878 | case curr_unit_mil: |
879 | status_set_unit(tip, "mil"); |
880 | break; |
881 | case curr_unit_auto: |
882 | status_set_unit(tip, "auto"); |
883 | break; |
884 | default: |
885 | abort(); |
886 | } |
887 | } |
888 | |
889 | |
890 | static gboolean unit_button_press_event(GtkWidget *widget, |
891 | GdkEventButton *event, gpointer data) |
892 | { |
893 | switch (event->button) { |
894 | case 1: |
895 | curr_unit = (curr_unit+1) % curr_unit_n; |
896 | show_curr_unit(); |
897 | break; |
898 | } |
899 | refresh_pos(); |
900 | change_world(); |
901 | return TRUE; |
902 | } |
903 | |
904 | |
905 | /* ----- setup ------------------------------------------------------------- */ |
906 | |
907 | |
908 | static GtkWidget *add_vbox(GtkWidget *tab, int col, int row) |
909 | { |
910 | GtkWidget *vbox; |
911 | |
912 | vbox = gtk_vbox_new(FALSE, 0); |
913 | gtk_table_attach_defaults(GTK_TABLE(tab), vbox, |
914 | col, col+1, row, row+1); |
915 | return vbox; |
916 | } |
917 | |
918 | |
919 | static GtkWidget *add_label_basic(GtkWidget *tab, int col, int row) |
920 | { |
921 | GtkWidget *label; |
922 | |
923 | label = label_in_box_new(NULL, NULL); |
924 | gtk_table_attach(GTK_TABLE(tab), box_of_label(label), |
925 | col, col+1, row, row+1, |
926 | GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 1); |
927 | /* 0 , 1 - padding */ |
928 | return label; |
929 | } |
930 | |
931 | |
932 | static GtkWidget *add_label(GtkWidget *tab, int col, int row) |
933 | { |
934 | GtkWidget *label; |
935 | |
936 | label = add_label_basic(tab, col, row); |
937 | gtk_misc_set_alignment(GTK_MISC(label), 0, 0); |
938 | gtk_label_set_selectable(GTK_LABEL(label), TRUE); |
939 | return label; |
940 | } |
941 | |
942 | |
943 | static GtkWidget *make_entry(void) |
944 | { |
945 | GtkWidget *entry; |
946 | |
947 | entry = gtk_entry_new(); |
948 | gtk_entry_set_has_frame(GTK_ENTRY(entry), FALSE); |
949 | |
950 | g_signal_connect(G_OBJECT(entry), "changed", |
951 | G_CALLBACK(changed), entry); |
952 | g_signal_connect(G_OBJECT(entry), "activate", |
953 | G_CALLBACK(activate), entry); |
954 | |
955 | return entry; |
956 | } |
957 | |
958 | |
959 | static GtkWidget *add_entry(GtkWidget *tab, int col, int row) |
960 | { |
961 | GtkWidget *entry; |
962 | |
963 | entry = make_entry(); |
964 | gtk_table_attach_defaults(GTK_TABLE(tab), entry, |
965 | col, col+1, row, row+1); |
966 | return entry; |
967 | } |
968 | |
969 | |
970 | void make_status_area(GtkWidget *vbox) |
971 | { |
972 | GtkWidget *tab, *sep; |
973 | GtkWidget *hbox, *vbox2; |
974 | |
975 | hbox = gtk_hbox_new(FALSE, 0); |
976 | gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0); |
977 | |
978 | vbox2 = gtk_vbox_new(FALSE, 0); |
979 | gtk_box_pack_start(GTK_BOX(hbox), vbox2, FALSE, FALSE, 1); |
980 | |
981 | status_icon = gtk_event_box_new(); |
982 | gtk_box_pack_start(GTK_BOX(vbox2), status_icon, FALSE, FALSE, 0); |
983 | |
984 | tab = gtk_table_new(7, 3, FALSE); |
985 | gtk_box_pack_start(GTK_BOX(hbox), tab, TRUE, TRUE, 0); |
986 | |
987 | /* types */ |
988 | |
989 | status_type_x = add_label(tab, 0, 0); |
990 | status_type_y = add_label(tab, 0, 1); |
991 | status_type_entry = add_label(tab, 0, 2); |
992 | |
993 | /* x / y */ |
994 | |
995 | status_x = add_label(tab, 1, 0); |
996 | status_box_x = add_vbox(tab, 2, 0); |
997 | status_y = add_label(tab, 1, 1); |
998 | status_entry_y = add_entry(tab, 2, 1); |
999 | |
1000 | status_entry_x = gtk_widget_ref(make_entry()); |
1001 | |
1002 | /* name and input */ |
1003 | |
1004 | status_name = add_label(tab, 1, 2); |
1005 | status_entry = add_entry(tab, 2, 2); |
1006 | |
1007 | /* separator */ |
1008 | |
1009 | sep = gtk_vseparator_new(); |
1010 | gtk_table_attach_defaults(GTK_TABLE(tab), sep, 3, 4, 0, 3); |
1011 | |
1012 | /* sys / user pos */ |
1013 | |
1014 | status_sys_x = add_label(tab, 4, 0); |
1015 | status_sys_y = add_label(tab, 4, 1); |
1016 | status_user_x = add_label(tab, 5, 0); |
1017 | status_user_y = add_label(tab, 5, 1); |
1018 | |
1019 | /* r / angle */ |
1020 | |
1021 | status_r = add_label(tab, 4, 2); |
1022 | status_angle = add_label(tab, 5, 2); |
1023 | |
1024 | /* zoom / grid / unit */ |
1025 | |
1026 | status_zoom = add_label(tab, 6, 0); |
1027 | status_grid = add_label(tab, 6, 1); |
1028 | status_unit = add_label_basic(tab, 6, 2); |
1029 | |
1030 | /* unit selection */ |
1031 | |
1032 | label_in_box_bg(status_unit, COLOR_SELECTOR); |
1033 | show_curr_unit(); |
1034 | g_signal_connect(G_OBJECT(box_of_label(status_unit)), |
1035 | "button_press_event", G_CALLBACK(unit_button_press_event), NULL); |
1036 | |
1037 | /* message bar */ |
1038 | |
1039 | status_msg = gtk_statusbar_new(); |
1040 | gtk_box_pack_start(GTK_BOX(vbox), status_msg, FALSE, FALSE, 0); |
1041 | |
1042 | context_id = gtk_statusbar_get_context_id(GTK_STATUSBAR(status_msg), |
1043 | "messages"); |
1044 | } |
1045 | |
1046 | |
1047 | void cleanup_status_area(void) |
1048 | { |
1049 | gtk_widget_unref(status_entry_x); |
1050 | } |
1051 |
Branches:
master