Root/
Source at commit 0fb83ed27446be7bc114945ca6e74895ac4e65b6 created 13 years 5 months ago. By Xiangfu Liu, use usual name for orig tarball top-level directory | |
---|---|
1 | /* |
2 | * gui_status.c - GUI, status area |
3 | * |
4 | * Written 2009-2011 by Werner Almesberger |
5 | * Copyright 2009-2011 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_trace: |
208 | s = "trace"; |
209 | break; |
210 | case pt_paste: |
211 | s = "paste"; |
212 | break; |
213 | case pt_mask: |
214 | s = "mask"; |
215 | break; |
216 | default: |
217 | abort(); |
218 | } |
219 | gtk_label_set_text(GTK_LABEL(pad_type), s); |
220 | } |
221 | |
222 | |
223 | static gboolean pad_type_button_press_event(GtkWidget *widget, |
224 | GdkEventButton *event, gpointer data) |
225 | { |
226 | switch (event->button) { |
227 | case 1: |
228 | *curr_pad_type = (*curr_pad_type+1) % pt_n; |
229 | show_pad_type(); |
230 | break; |
231 | } |
232 | /* |
233 | * We can't just redraw() here, because changing the pad type may also |
234 | * affect the visual stacking. So we change the world and hope we end |
235 | * up selecting the same pad afterwards. |
236 | */ |
237 | change_world_reselect(); |
238 | return TRUE; |
239 | } |
240 | |
241 | |
242 | void edit_pad_type(enum pad_type *type) |
243 | { |
244 | vacate_widget(status_box_x); |
245 | curr_pad_type = type; |
246 | pad_type = label_in_box_new(NULL, "Pad type. Click to cycle."); |
247 | gtk_container_add(GTK_CONTAINER(status_box_x), box_of_label(pad_type)); |
248 | label_in_box_bg(pad_type, COLOR_SELECTOR); |
249 | g_signal_connect(G_OBJECT(box_of_label(pad_type)), |
250 | "button_press_event", G_CALLBACK(pad_type_button_press_event), |
251 | NULL); |
252 | show_pad_type(); |
253 | gtk_widget_show_all(status_box_x); |
254 | } |
255 | |
256 | |
257 | /* ----- edit helper functions --------------------------------------------- */ |
258 | |
259 | |
260 | static void reset_edit(GtkWidget *widget) |
261 | { |
262 | struct edit_ops *ops; |
263 | void *ctx; |
264 | char *s; |
265 | |
266 | ops = gtk_object_get_data(GTK_OBJECT(widget), "edit-ops"); |
267 | ctx = gtk_object_get_data(GTK_OBJECT(widget), "edit-ctx"); |
268 | assert(ops); |
269 | s = ops->retrieve(ctx); |
270 | gtk_object_set_data(GTK_OBJECT(widget), "edit-ops", NULL); |
271 | gtk_entry_set_text(GTK_ENTRY(widget), s); |
272 | free(s); |
273 | entry_color(widget, COLOR_EDIT_ASIS); |
274 | gtk_object_set_data(GTK_OBJECT(widget), "edit-ops", ops); |
275 | } |
276 | |
277 | |
278 | static void reset_edits(void) |
279 | { |
280 | GtkWidget *edit; |
281 | |
282 | for (edit = open_edits; edit; |
283 | edit = gtk_object_get_data(GTK_OBJECT(edit), "edit-next")) |
284 | reset_edit(edit); |
285 | gtk_widget_grab_focus(GTK_WIDGET(open_edits)); |
286 | } |
287 | |
288 | |
289 | static gboolean edit_key_press_event(GtkWidget *widget, GdkEventKey *event, |
290 | gpointer data) |
291 | { |
292 | GtkWidget *next = gtk_object_get_data(GTK_OBJECT(widget), "edit-next"); |
293 | |
294 | switch (event->keyval) { |
295 | case GDK_Tab: |
296 | gtk_widget_grab_focus(GTK_WIDGET(next ? next : open_edits)); |
297 | return TRUE; |
298 | case GDK_Escape: |
299 | reset_edits(); |
300 | return TRUE; |
301 | default: |
302 | return FALSE; |
303 | } |
304 | } |
305 | |
306 | |
307 | static void setup_edit(GtkWidget *widget, struct edit_ops *ops, void *ctx, |
308 | const char *tooltip) |
309 | { |
310 | gtk_object_set_data(GTK_OBJECT(widget), "edit-ops", ops); |
311 | gtk_object_set_data(GTK_OBJECT(widget), "edit-ctx", ctx); |
312 | gtk_object_set_data(GTK_OBJECT(widget), "edit-next", NULL); |
313 | |
314 | reset_edit(widget); |
315 | |
316 | if (!open_edits) |
317 | gtk_widget_grab_focus(GTK_WIDGET(widget)); |
318 | gtk_widget_show(widget); |
319 | |
320 | g_signal_connect(G_OBJECT(widget), "key_press_event", |
321 | G_CALLBACK(edit_key_press_event), open_edits); |
322 | |
323 | if (last_edit) |
324 | gtk_object_set_data(GTK_OBJECT(last_edit), "edit-next", widget); |
325 | else |
326 | open_edits = widget; |
327 | last_edit = widget; |
328 | gtk_widget_set_tooltip_markup(widget, tooltip); |
329 | } |
330 | |
331 | |
332 | /* ----- identifier fields ------------------------------------------------- */ |
333 | |
334 | |
335 | struct edit_unique_ctx { |
336 | const char **s; |
337 | int (*validate)(const char *s, void *ctx); |
338 | void *ctx; |
339 | }; |
340 | |
341 | |
342 | /* |
343 | * Handle NULL so that we can also use it for unique_null |
344 | */ |
345 | |
346 | static char *unique_retrieve(void *ctx) |
347 | { |
348 | struct edit_unique_ctx *unique_ctx = ctx; |
349 | |
350 | return stralloc(*unique_ctx->s ? *unique_ctx->s : ""); |
351 | } |
352 | |
353 | |
354 | static enum edit_status unique_status(const char *s, void *ctx) |
355 | { |
356 | const struct edit_unique_ctx *unique_ctx = ctx; |
357 | |
358 | if (!strcmp(s, *unique_ctx->s)) |
359 | return es_unchanged; |
360 | return !unique_ctx->validate || |
361 | unique_ctx->validate(s, unique_ctx->ctx) ? es_good : es_bad; |
362 | } |
363 | |
364 | |
365 | static void unique_store(const char *s, void *ctx) |
366 | { |
367 | const struct edit_unique_ctx *unique_ctx = ctx; |
368 | |
369 | *unique_ctx->s = unique(s); |
370 | } |
371 | |
372 | |
373 | static struct edit_ops edit_ops_unique = { |
374 | .retrieve = unique_retrieve, |
375 | .status = unique_status, |
376 | .store = unique_store, |
377 | }; |
378 | |
379 | |
380 | void edit_unique(const char **s, int (*validate)(const char *s, void *ctx), |
381 | void *ctx, const char *tooltip) |
382 | { |
383 | static struct edit_unique_ctx unique_ctx; |
384 | |
385 | unique_ctx.s = s; |
386 | unique_ctx.validate = validate; |
387 | unique_ctx.ctx = ctx; |
388 | setup_edit(status_entry, &edit_ops_unique, &unique_ctx, tooltip); |
389 | } |
390 | |
391 | |
392 | /* ----- identifier fields with NULL --------------------------------------- */ |
393 | |
394 | |
395 | static enum edit_status unique_null_status(const char *s, void *ctx) |
396 | { |
397 | const struct edit_unique_ctx *unique_ctx = ctx; |
398 | |
399 | if (!strcmp(s, *unique_ctx->s ? *unique_ctx->s : "")) |
400 | return es_unchanged; |
401 | if (!*s) |
402 | return es_good; |
403 | return !unique_ctx->validate || |
404 | unique_ctx->validate(s, unique_ctx->ctx) ? es_good : es_bad; |
405 | } |
406 | |
407 | |
408 | static void unique_null_store(const char *s, void *ctx) |
409 | { |
410 | const struct edit_unique_ctx *unique_ctx = ctx; |
411 | |
412 | if (!*s) |
413 | *unique_ctx->s = NULL; |
414 | else |
415 | *unique_ctx->s = unique(s); |
416 | } |
417 | |
418 | |
419 | static struct edit_ops edit_ops_null_unique = { |
420 | .retrieve = unique_retrieve, |
421 | .status = unique_null_status, |
422 | .store = unique_null_store, |
423 | }; |
424 | |
425 | |
426 | void edit_unique_null(const char **s, |
427 | int (*validate)(const char *s, void *ctx), void *ctx, const char *tooltip) |
428 | { |
429 | static struct edit_unique_ctx unique_ctx; |
430 | |
431 | unique_ctx.s = s; |
432 | unique_ctx.validate = validate; |
433 | unique_ctx.ctx = ctx; |
434 | setup_edit(status_entry, &edit_ops_null_unique, &unique_ctx, tooltip); |
435 | } |
436 | |
437 | |
438 | /* ----- unique field (variable) optionally followed by values ------------- */ |
439 | |
440 | |
441 | struct edit_unique_with_values_ctx { |
442 | const char **s; |
443 | int (*validate)(const char *s, void *ctx); |
444 | void *ctx; |
445 | void (*set_values)(void *user, const struct value *values, |
446 | int n_values); |
447 | void *user; |
448 | int max_values; |
449 | }; |
450 | |
451 | |
452 | static enum edit_status unique_with_values_status(const char *s, void *ctx) |
453 | { |
454 | const struct edit_unique_with_values_ctx *unique_ctx = ctx; |
455 | const char *id; |
456 | struct value *values; |
457 | int n; |
458 | |
459 | if (!strcmp(s, *unique_ctx->s)) |
460 | return es_unchanged; |
461 | status_begin_reporting(); |
462 | n = parse_var(s, &id, &values, unique_ctx->max_values); |
463 | if (n < 0) |
464 | return es_bad; |
465 | free_values(values, 0); |
466 | return !unique_ctx->validate || |
467 | unique_ctx->validate(id, unique_ctx->ctx) ? es_good : es_bad; |
468 | } |
469 | |
470 | |
471 | static void unique_with_values_store(const char *s, void *ctx) |
472 | { |
473 | const struct edit_unique_with_values_ctx *unique_ctx = ctx; |
474 | struct value *values; |
475 | int n; |
476 | |
477 | status_begin_reporting(); |
478 | n = parse_var(s, unique_ctx->s, &values, unique_ctx->max_values); |
479 | if (!n) |
480 | return; |
481 | assert(n >= 0); |
482 | assert(unique_ctx->max_values == -1 || n <= unique_ctx->max_values); |
483 | unique_ctx->set_values(unique_ctx->user, values, n); |
484 | free_values(values, 1); |
485 | } |
486 | |
487 | |
488 | static struct edit_ops edit_ops_unique_with_values = { |
489 | .retrieve = unique_retrieve, |
490 | .status = unique_with_values_status, |
491 | .store = unique_with_values_store, |
492 | }; |
493 | |
494 | |
495 | void edit_unique_with_values(const char **s, |
496 | int (*validate)(const char *s, void *ctx), void *ctx, |
497 | void (*set_values)(void *user, const struct value *values, int n_values), |
498 | void *user, int max_values, |
499 | const char *tooltip) |
500 | { |
501 | static struct edit_unique_with_values_ctx unique_ctx; |
502 | |
503 | unique_ctx.s = s; |
504 | unique_ctx.validate = validate; |
505 | unique_ctx.ctx = ctx; |
506 | unique_ctx.set_values = set_values; |
507 | unique_ctx.user = user; |
508 | unique_ctx.max_values = max_values; |
509 | setup_edit(status_entry, &edit_ops_unique_with_values, &unique_ctx, |
510 | tooltip); |
511 | } |
512 | |
513 | |
514 | /* ----- string fields ----------------------------------------------------- */ |
515 | |
516 | |
517 | struct edit_name_ctx { |
518 | char **s; |
519 | int (*validate)(const char *s, void *ctx); |
520 | void *ctx; |
521 | }; |
522 | |
523 | |
524 | static char *name_retrieve(void *ctx) |
525 | { |
526 | struct edit_name_ctx *name_ctx = ctx; |
527 | |
528 | return stralloc(*name_ctx->s ? *name_ctx->s : ""); |
529 | } |
530 | |
531 | |
532 | static enum edit_status name_status(const char *s, void *ctx) |
533 | { |
534 | const struct edit_name_ctx *name_ctx = ctx; |
535 | |
536 | if (!strcmp(s, *name_ctx->s)) |
537 | return es_unchanged; |
538 | return !name_ctx->validate || name_ctx->validate(s, name_ctx->ctx) ? |
539 | es_good : es_bad; |
540 | } |
541 | |
542 | |
543 | static void name_store(const char *s, void *ctx) |
544 | { |
545 | const struct edit_name_ctx *name_ctx = ctx; |
546 | |
547 | free(*name_ctx->s); |
548 | *name_ctx->s = stralloc(s); |
549 | } |
550 | |
551 | |
552 | static struct edit_ops edit_ops_name = { |
553 | .retrieve = name_retrieve, |
554 | .status = name_status, |
555 | .store = name_store, |
556 | }; |
557 | |
558 | |
559 | void edit_name(char **s, int (*validate)(const char *s, void *ctx), void *ctx, |
560 | const char *tooltip) |
561 | { |
562 | static struct edit_name_ctx name_ctx; |
563 | |
564 | name_ctx.s = s; |
565 | name_ctx.validate = validate; |
566 | name_ctx.ctx = ctx; |
567 | setup_edit(status_entry, &edit_ops_name, &name_ctx, tooltip); |
568 | } |
569 | |
570 | |
571 | /* ----- expression fields ------------------------------------------------- */ |
572 | |
573 | |
574 | static struct expr *try_parse_expr(const char *s) |
575 | { |
576 | status_begin_reporting(); |
577 | return parse_expr(s); |
578 | } |
579 | |
580 | |
581 | static char *expr_retrieve(void *ctx) |
582 | { |
583 | struct expr **expr = ctx; |
584 | |
585 | return unparse(*expr); |
586 | } |
587 | |
588 | |
589 | static enum edit_status expr_status(const char *s, void *ctx) |
590 | { |
591 | struct expr *expr; |
592 | |
593 | expr = try_parse_expr(s); |
594 | if (!expr) |
595 | return es_bad; |
596 | free_expr(expr); |
597 | return es_good; |
598 | } |
599 | |
600 | |
601 | static void expr_store(const char *s, void *ctx) |
602 | { |
603 | struct expr **anchor = ctx; |
604 | struct expr *expr; |
605 | |
606 | expr = try_parse_expr(s); |
607 | assert(expr); |
608 | if (*anchor) |
609 | free_expr(*anchor); |
610 | *anchor = expr; |
611 | } |
612 | |
613 | |
614 | static struct edit_ops edit_ops_expr = { |
615 | .retrieve = expr_retrieve, |
616 | .status = expr_status, |
617 | .store = expr_store, |
618 | }; |
619 | |
620 | |
621 | void edit_expr(struct expr **expr, const char *tooltip) |
622 | { |
623 | setup_edit(status_entry, &edit_ops_expr, expr, tooltip); |
624 | } |
625 | |
626 | |
627 | /* ----- distance expressions ---------------------------------------------- */ |
628 | |
629 | |
630 | static void dist_expr_store(const char *s, void *ctx) |
631 | { |
632 | struct expr **anchor = ctx; |
633 | struct expr *expr; |
634 | |
635 | expr_store(s, ctx); |
636 | expr = *anchor; |
637 | if (expr->op == op_num && !expr->u.num.exponent && !expr->u.num.n) |
638 | expr->u.num.exponent = 1; |
639 | } |
640 | |
641 | |
642 | static struct edit_ops edit_ops_dist_expr = { |
643 | .retrieve = expr_retrieve, |
644 | .status = expr_status, |
645 | .store = dist_expr_store, |
646 | }; |
647 | |
648 | |
649 | static void edit_any_dist_expr(GtkWidget *widget, struct expr **expr, |
650 | const char *tooltip) |
651 | { |
652 | setup_edit(widget, &edit_ops_dist_expr, expr, tooltip); |
653 | } |
654 | |
655 | |
656 | void edit_dist_expr(struct expr **expr, const char *tooltip) |
657 | { |
658 | edit_any_dist_expr(status_entry, expr, tooltip); |
659 | } |
660 | |
661 | |
662 | void edit_x(struct expr **expr, const char *tooltip) |
663 | { |
664 | vacate_widget(status_box_x); |
665 | gtk_container_add(GTK_CONTAINER(status_box_x), status_entry_x); |
666 | gtk_widget_show(status_box_x); |
667 | edit_any_dist_expr(status_entry_x, expr, tooltip); |
668 | } |
669 | |
670 | |
671 | void edit_y(struct expr **expr, const char *tooltip) |
672 | { |
673 | edit_any_dist_expr(status_entry_y, expr, tooltip); |
674 | } |
675 | |
676 | |
677 | /* ----- expression list --------------------------------------------------- */ |
678 | |
679 | |
680 | struct edit_expr_list_ctx { |
681 | struct expr *expr; |
682 | void (*set_values)(void *user, const struct value *values, |
683 | int n_values); |
684 | void *user; |
685 | }; |
686 | |
687 | |
688 | static char *expr_list_retrieve(void *ctx) |
689 | { |
690 | struct edit_expr_list_ctx *expr_list_ctx = ctx; |
691 | |
692 | return unparse(expr_list_ctx->expr); |
693 | } |
694 | |
695 | |
696 | static enum edit_status expr_list_status(const char *s, void *ctx) |
697 | { |
698 | struct value *values; |
699 | int n; |
700 | |
701 | status_begin_reporting(); |
702 | n = parse_values(s, &values); |
703 | if (n < 0) |
704 | return es_bad; |
705 | free_values(values, 0); |
706 | return es_good; |
707 | } |
708 | |
709 | |
710 | static void expr_list_store(const char *s, void *ctx) |
711 | { |
712 | struct edit_expr_list_ctx *expr_list_ctx = ctx; |
713 | struct value *values; |
714 | int n; |
715 | |
716 | status_begin_reporting(); |
717 | n = parse_values(s, &values); |
718 | assert(n >= 0); |
719 | expr_list_ctx->set_values(expr_list_ctx->user, values, n); |
720 | free_values(values, 1); |
721 | } |
722 | |
723 | |
724 | static struct edit_ops edit_ops_expr_list = { |
725 | .retrieve = expr_list_retrieve, |
726 | .status = expr_list_status, |
727 | .store = expr_list_store, |
728 | }; |
729 | |
730 | |
731 | void edit_expr_list(struct expr *expr, |
732 | void (*set_values)(void *user, const struct value *values, int n_values), |
733 | void *user, const char *tooltip) |
734 | { |
735 | static struct edit_expr_list_ctx expr_list_ctx; |
736 | |
737 | expr_list_ctx.expr = expr; |
738 | expr_list_ctx.set_values = set_values; |
739 | expr_list_ctx.user = user; |
740 | setup_edit(status_entry, &edit_ops_expr_list, &expr_list_ctx, tooltip); |
741 | } |
742 | |
743 | |
744 | /* ----- text entry -------------------------------------------------------- */ |
745 | |
746 | |
747 | static enum edit_status get_status(GtkWidget *widget) |
748 | { |
749 | struct edit_ops *ops; |
750 | void *ctx; |
751 | const char *s; |
752 | |
753 | ops = gtk_object_get_data(GTK_OBJECT(widget), "edit-ops"); |
754 | if (!ops) |
755 | return es_unchanged; |
756 | ctx = gtk_object_get_data(GTK_OBJECT(widget), "edit-ctx"); |
757 | s = gtk_entry_get_text(GTK_ENTRY(widget)); |
758 | return ops->status(s, ctx); |
759 | } |
760 | |
761 | |
762 | static void set_edit(GtkWidget *widget) |
763 | { |
764 | struct edit_ops *ops; |
765 | void *ctx; |
766 | const char *s; |
767 | |
768 | ops = gtk_object_get_data(GTK_OBJECT(widget), "edit-ops"); |
769 | if (!ops) |
770 | return; |
771 | ctx = gtk_object_get_data(GTK_OBJECT(widget), "edit-ctx"); |
772 | s = gtk_entry_get_text(GTK_ENTRY(widget)); |
773 | if (ops->store) |
774 | ops->store(s, ctx); |
775 | } |
776 | |
777 | |
778 | static gboolean changed(GtkWidget *widget, GdkEventMotion *event, |
779 | gpointer data) |
780 | { |
781 | switch (get_status(widget)) { |
782 | case es_unchanged: |
783 | entry_color(widget, COLOR_EDIT_ASIS); |
784 | break; |
785 | case es_good: |
786 | entry_color(widget, COLOR_EDIT_GOOD); |
787 | break; |
788 | case es_bad: |
789 | entry_color(widget, COLOR_EDIT_BAD); |
790 | break; |
791 | default: |
792 | abort(); |
793 | } |
794 | return TRUE; |
795 | } |
796 | |
797 | |
798 | static gboolean activate(GtkWidget *widget, GdkEventMotion *event, |
799 | gpointer data) |
800 | { |
801 | GtkWidget *edit; |
802 | enum edit_status status; |
803 | int unchanged = 1; |
804 | |
805 | for (edit = open_edits; edit; |
806 | edit = gtk_object_get_data(GTK_OBJECT(edit), "edit-next")) { |
807 | status = get_status(edit); |
808 | if (status == es_bad) |
809 | return TRUE; |
810 | if (status == es_good) |
811 | unchanged = 0; |
812 | } |
813 | if (unchanged) { |
814 | inst_deselect(); |
815 | return TRUE; |
816 | } |
817 | for (edit = open_edits; edit; |
818 | edit = gtk_object_get_data(GTK_OBJECT(edit), "edit-next")) |
819 | if (get_status(edit) == es_good) { |
820 | entry_color(edit, COLOR_EDIT_ASIS); |
821 | set_edit(edit); |
822 | } |
823 | inst_deselect(); |
824 | change_world(); |
825 | return TRUE; |
826 | } |
827 | |
828 | |
829 | void edit_nothing(void) |
830 | { |
831 | gtk_widget_hide(status_entry); |
832 | gtk_widget_hide(status_box_x); |
833 | gtk_widget_hide(status_entry_y); |
834 | open_edits = NULL; |
835 | last_edit = NULL; |
836 | } |
837 | |
838 | |
839 | /* ----- status reports ---------------------------------------------------- */ |
840 | |
841 | |
842 | static gint context_id; |
843 | static int have_msg = 0; |
844 | |
845 | |
846 | static void clear_status_msg(void) |
847 | { |
848 | if (have_msg) { |
849 | gtk_statusbar_pop(GTK_STATUSBAR(status_msg), context_id); |
850 | have_msg = 0; |
851 | } |
852 | } |
853 | |
854 | |
855 | static void report_to_gui(const char *s) |
856 | { |
857 | if (!have_msg) |
858 | gtk_statusbar_push(GTK_STATUSBAR(status_msg), context_id, s); |
859 | have_msg = 1; |
860 | } |
861 | |
862 | |
863 | void status_begin_reporting(void) |
864 | { |
865 | clear_status_msg(); |
866 | reporter = report_to_gui; |
867 | } |
868 | |
869 | |
870 | /* ----- unit selection ---------------------------------------------------- */ |
871 | |
872 | |
873 | static void show_curr_unit(void) |
874 | { |
875 | static const char *tip = "Display unit. Click to cycle."; |
876 | |
877 | switch (curr_unit) { |
878 | case curr_unit_mm: |
879 | status_set_unit(tip, "mm"); |
880 | break; |
881 | case curr_unit_mil: |
882 | status_set_unit(tip, "mil"); |
883 | break; |
884 | case curr_unit_auto: |
885 | status_set_unit(tip, "auto"); |
886 | break; |
887 | default: |
888 | abort(); |
889 | } |
890 | } |
891 | |
892 | |
893 | static gboolean unit_button_press_event(GtkWidget *widget, |
894 | GdkEventButton *event, gpointer data) |
895 | { |
896 | switch (event->button) { |
897 | case 1: |
898 | curr_unit = (curr_unit+1) % curr_unit_n; |
899 | show_curr_unit(); |
900 | break; |
901 | } |
902 | refresh_pos(); |
903 | change_world(); |
904 | return TRUE; |
905 | } |
906 | |
907 | |
908 | /* ----- setup ------------------------------------------------------------- */ |
909 | |
910 | |
911 | static GtkWidget *add_vbox(GtkWidget *tab, int col, int row) |
912 | { |
913 | GtkWidget *vbox; |
914 | |
915 | vbox = gtk_vbox_new(FALSE, 0); |
916 | gtk_table_attach_defaults(GTK_TABLE(tab), vbox, |
917 | col, col+1, row, row+1); |
918 | return vbox; |
919 | } |
920 | |
921 | |
922 | static GtkWidget *add_label_basic(GtkWidget *tab, int col, int row) |
923 | { |
924 | GtkWidget *label; |
925 | |
926 | label = label_in_box_new(NULL, NULL); |
927 | gtk_table_attach(GTK_TABLE(tab), box_of_label(label), |
928 | col, col+1, row, row+1, |
929 | GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 1); |
930 | /* 0 , 1 - padding */ |
931 | return label; |
932 | } |
933 | |
934 | |
935 | static GtkWidget *add_label(GtkWidget *tab, int col, int row) |
936 | { |
937 | GtkWidget *label; |
938 | |
939 | label = add_label_basic(tab, col, row); |
940 | gtk_misc_set_alignment(GTK_MISC(label), 0, 0); |
941 | gtk_label_set_selectable(GTK_LABEL(label), TRUE); |
942 | return label; |
943 | } |
944 | |
945 | |
946 | static GtkWidget *make_entry(void) |
947 | { |
948 | GtkWidget *entry; |
949 | |
950 | entry = gtk_entry_new(); |
951 | gtk_entry_set_has_frame(GTK_ENTRY(entry), FALSE); |
952 | |
953 | g_signal_connect(G_OBJECT(entry), "changed", |
954 | G_CALLBACK(changed), entry); |
955 | g_signal_connect(G_OBJECT(entry), "activate", |
956 | G_CALLBACK(activate), entry); |
957 | |
958 | return entry; |
959 | } |
960 | |
961 | |
962 | static GtkWidget *add_entry(GtkWidget *tab, int col, int row) |
963 | { |
964 | GtkWidget *entry; |
965 | |
966 | entry = make_entry(); |
967 | gtk_table_attach_defaults(GTK_TABLE(tab), entry, |
968 | col, col+1, row, row+1); |
969 | return entry; |
970 | } |
971 | |
972 | |
973 | void make_status_area(GtkWidget *vbox) |
974 | { |
975 | GtkWidget *tab, *sep; |
976 | GtkWidget *hbox, *vbox2; |
977 | |
978 | hbox = gtk_hbox_new(FALSE, 0); |
979 | gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0); |
980 | |
981 | vbox2 = gtk_vbox_new(FALSE, 0); |
982 | gtk_box_pack_start(GTK_BOX(hbox), vbox2, FALSE, FALSE, 1); |
983 | |
984 | status_icon = gtk_event_box_new(); |
985 | gtk_box_pack_start(GTK_BOX(vbox2), status_icon, FALSE, FALSE, 0); |
986 | |
987 | tab = gtk_table_new(7, 3, FALSE); |
988 | gtk_box_pack_start(GTK_BOX(hbox), tab, TRUE, TRUE, 0); |
989 | |
990 | /* types */ |
991 | |
992 | status_type_x = add_label(tab, 0, 0); |
993 | status_type_y = add_label(tab, 0, 1); |
994 | status_type_entry = add_label(tab, 0, 2); |
995 | |
996 | /* x / y */ |
997 | |
998 | status_x = add_label(tab, 1, 0); |
999 | status_box_x = add_vbox(tab, 2, 0); |
1000 | status_y = add_label(tab, 1, 1); |
1001 | status_entry_y = add_entry(tab, 2, 1); |
1002 | |
1003 | status_entry_x = gtk_widget_ref(make_entry()); |
1004 | |
1005 | /* name and input */ |
1006 | |
1007 | status_name = add_label(tab, 1, 2); |
1008 | status_entry = add_entry(tab, 2, 2); |
1009 | |
1010 | /* separator */ |
1011 | |
1012 | sep = gtk_vseparator_new(); |
1013 | gtk_table_attach_defaults(GTK_TABLE(tab), sep, 3, 4, 0, 3); |
1014 | |
1015 | /* sys / user pos */ |
1016 | |
1017 | status_sys_x = add_label(tab, 4, 0); |
1018 | status_sys_y = add_label(tab, 4, 1); |
1019 | status_user_x = add_label(tab, 5, 0); |
1020 | status_user_y = add_label(tab, 5, 1); |
1021 | |
1022 | /* r / angle */ |
1023 | |
1024 | status_r = add_label(tab, 4, 2); |
1025 | status_angle = add_label(tab, 5, 2); |
1026 | |
1027 | /* zoom / grid / unit */ |
1028 | |
1029 | status_zoom = add_label(tab, 6, 0); |
1030 | status_grid = add_label(tab, 6, 1); |
1031 | status_unit = add_label_basic(tab, 6, 2); |
1032 | |
1033 | /* unit selection */ |
1034 | |
1035 | label_in_box_bg(status_unit, COLOR_SELECTOR); |
1036 | show_curr_unit(); |
1037 | g_signal_connect(G_OBJECT(box_of_label(status_unit)), |
1038 | "button_press_event", G_CALLBACK(unit_button_press_event), NULL); |
1039 | |
1040 | /* message bar */ |
1041 | |
1042 | status_msg = gtk_statusbar_new(); |
1043 | gtk_box_pack_start(GTK_BOX(vbox), status_msg, FALSE, FALSE, 0); |
1044 | |
1045 | context_id = gtk_statusbar_get_context_id(GTK_STATUSBAR(status_msg), |
1046 | "messages"); |
1047 | } |
1048 | |
1049 | |
1050 | void cleanup_status_area(void) |
1051 | { |
1052 | gtk_widget_unref(status_entry_x); |
1053 | } |
1054 |
Branches:
master