tests: update AdgPath to latest changes
[adg.git] / demo / adg-demo.c
blob3b11f204ee19787c2bfe79134c63cdfdcccc3fc5
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2015 Nicola Fontana <ntd at entidi.it>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 #include "demo.h"
21 #include <adg.h>
22 #include <string.h>
23 #include <math.h>
25 #define SQRT3 1.732050808
26 #define CHAMFER 0.3
29 /* Whether render the boxes to highlight the extents of every entity */
30 static gboolean show_extents = FALSE;
33 typedef struct _DemoPart DemoPart;
35 struct _DemoPart {
36 /* Dimensions */
37 gdouble A;
38 gdouble B;
39 gdouble C;
40 gdouble DHOLE;
41 gdouble LHOLE;
42 gdouble D1;
43 gdouble D2;
44 gdouble D3;
45 gdouble D4;
46 gdouble D5;
47 gdouble D6;
48 gdouble D7;
49 gdouble RD34;
50 gdouble RD56;
51 gdouble LD2;
52 gdouble LD3;
53 gdouble LD5;
54 gdouble LD6;
55 gdouble LD7;
56 gboolean GROOVE;
57 gdouble ZGROOVE;
58 gdouble DGROOVE;
59 gdouble LGROOVE;
61 /* Metadata */
62 gchar *TITLE;
63 gchar *DRAWING;
64 gchar *AUTHOR;
65 gchar *DATE;
67 /* User interface widgets */
68 AdgGtkArea *area;
69 GHashTable *widgets;
70 GtkButton *apply;
71 GtkButton *reset;
73 /* Data models */
74 AdgPath *body;
75 AdgPath *hole;
76 AdgPath *axis;
78 /* Special entities */
79 AdgTitleBlock *title_block;
80 AdgEdges *edges;
84 static void
85 _adg_version(void)
87 g_print("adg-demo " PACKAGE_VERSION "\n");
88 exit(0);
91 static void
92 parse_args(gint *p_argc, gchar **p_argv[])
94 GOptionEntry entries[] = {
95 {"version", 'V', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
96 (gpointer) _adg_version, _("Display version information"), NULL},
97 {"show-extents", 'E', 0, G_OPTION_ARG_NONE,
98 &show_extents, _("Show the boundary boxes of every entity"), NULL},
99 {NULL}
101 GError *error;
103 error = NULL;
104 gtk_init_with_args(p_argc, p_argv, _("- ADG demonstration program"),
105 entries, GETTEXT_PACKAGE, &error);
106 if (error != NULL) {
107 g_error("%s", error->message);
108 g_assert_not_reached();
113 * _adg_error:
114 * @message: a custom error message
115 * @parent_window: the parent window or %NULL
117 * Convenient function that presents an error dialog and waits the user
118 * to close this modal dialog.
120 void
121 _adg_error(const gchar *message, GtkWindow *parent_window)
123 GtkWidget *dialog = gtk_message_dialog_new(parent_window,
124 GTK_DIALOG_DESTROY_WITH_PARENT,
125 GTK_MESSAGE_ERROR,
126 GTK_BUTTONS_CLOSE,
127 "%s", message);
128 gtk_window_set_title(GTK_WINDOW(dialog), _("Error from adg-demo"));
129 gtk_dialog_run(GTK_DIALOG(dialog));
130 gtk_widget_destroy(dialog);
133 static void
134 _adg_path_add_groove(AdgPath *path, const DemoPart *part)
136 AdgModel *model;
137 CpmlPair pair;
139 model = ADG_MODEL(path);
141 pair.x = part->ZGROOVE;
142 pair.y = part->D1 / 2;
143 adg_path_line_to(path, &pair);
144 adg_model_set_named_pair(model, "DGROOVEI_X", &pair);
146 pair.y = part->D3 / 2;
147 adg_model_set_named_pair(model, "DGROOVEY_POS", &pair);
149 pair.y = part->DGROOVE / 2;
150 adg_path_line_to(path, &pair);
151 adg_model_set_named_pair(model, "DGROOVEI_Y", &pair);
153 pair.x += part->LGROOVE;
154 adg_path_line_to(path, &pair);
156 pair.y = part->D3 / 2;
157 adg_model_set_named_pair(model, "DGROOVEX_POS", &pair);
159 pair.y = part->D1 / 2;
160 adg_path_line_to(path, &pair);
161 adg_model_set_named_pair(model, "DGROOVEF_X", &pair);
165 static void
166 _adg_part_define_title_block(DemoPart *part)
168 g_object_set(part->title_block,
169 "title", part->TITLE,
170 "author", part->AUTHOR,
171 "date", part->DATE,
172 "drawing", part->DRAWING,
173 "logo", adg_logo_new(),
174 "projection", adg_projection_new(ADG_PROJECTION_SCHEME_FIRST_ANGLE),
175 "size", "A4",
176 NULL);
179 static void
180 _adg_part_define_hole(DemoPart *part)
182 AdgPath *path;
183 AdgModel *model;
184 CpmlPair pair, edge;
186 path = part->hole;
187 model = ADG_MODEL(path);
189 pair.x = part->LHOLE;
190 pair.y = 0;
192 adg_path_move_to(path, &pair);
193 adg_model_set_named_pair(model, "LHOLE", &pair);
195 pair.y = part->DHOLE / 2;
196 pair.x -= pair.y / SQRT3;
197 adg_path_line_to(path, &pair);
198 cpml_pair_copy(&edge, &pair);
200 pair.x = 0;
201 adg_path_line_to(path, &pair);
202 adg_model_set_named_pair(model, "DHOLE", &pair);
204 pair.y = (part->D1 + part->DHOLE) / 4;
205 adg_path_line_to(path, &pair);
207 adg_path_curve_to_explicit(path,
208 part->LHOLE / 2, part->DHOLE / 2,
209 part->LHOLE + 2, part->D1 / 2,
210 part->LHOLE + 2, 0);
211 adg_path_reflect(path, NULL);
212 adg_path_close(path);
214 /* No need to incomodate an AdgEdge model for two reasons:
215 * it is only a single line and it is always needed */
216 adg_path_move_to(path, &edge);
217 edge.y = -edge.y;
218 adg_path_line_to(path, &edge);
221 static void
222 _adg_part_define_body(DemoPart *part)
224 AdgModel *model;
225 AdgPath *path;
226 CpmlPair pair, tmp;
227 const CpmlPrimitive *primitive;
229 path = part->body;
230 model = ADG_MODEL(path);
232 pair.x = 0;
233 pair.y = part->D1 / 2;
234 adg_path_move_to(path, &pair);
235 adg_model_set_named_pair(model, "D1I", &pair);
237 if (part->GROOVE) {
238 _adg_path_add_groove(path, part);
241 pair.x = part->A - part->B - part->LD2;
242 adg_path_line_to(path, &pair);
243 adg_model_set_named_pair(model, "D1F", &pair);
245 pair.y = part->D3 / 2;
246 adg_model_set_named_pair(model, "D2_POS", &pair);
248 pair.x += (part->D1 - part->D2) / 2;
249 pair.y = part->D2 / 2;
250 adg_path_line_to(path, &pair);
251 adg_model_set_named_pair(model, "D2I", &pair);
253 pair.x = part->A - part->B;
254 adg_path_line_to(path, &pair);
255 adg_path_fillet(path, 0.4);
257 pair.x = part->A - part->B;
258 pair.y = part->D3 / 2;
259 adg_path_line_to(path, &pair);
260 adg_model_set_named_pair(model, "D3I", &pair);
262 pair.x = part->A;
263 adg_model_set_named_pair(model, "East", &pair);
265 pair.x = 0;
266 adg_model_set_named_pair(model, "West", &pair);
268 adg_path_chamfer(path, CHAMFER, CHAMFER);
270 pair.x = part->A - part->B + part->LD3;
271 pair.y = part->D3 / 2;
272 adg_path_line_to(path, &pair);
274 primitive = adg_path_over_primitive(path);
275 cpml_primitive_put_point(primitive, 0, &tmp);
276 adg_model_set_named_pair(model, "D3I_X", &tmp);
278 cpml_primitive_put_point(primitive, -1, &tmp);
279 adg_model_set_named_pair(model, "D3I_Y", &tmp);
281 adg_path_chamfer(path, CHAMFER, CHAMFER);
283 pair.y = part->D4 / 2;
284 adg_path_line_to(path, &pair);
286 primitive = adg_path_over_primitive(path);
287 cpml_primitive_put_point(primitive, 0, &tmp);
288 adg_model_set_named_pair(model, "D3F_Y", &tmp);
289 cpml_primitive_put_point(primitive, -1, &tmp);
290 adg_model_set_named_pair(model, "D3F_X", &tmp);
292 adg_path_fillet(path, part->RD34);
294 pair.x += part->RD34;
295 adg_model_set_named_pair(model, "D4I", &pair);
297 pair.x = part->A - part->C - part->LD5;
298 adg_path_line_to(path, &pair);
299 adg_model_set_named_pair(model, "D4F", &pair);
301 pair.y = part->D3 / 2;
302 adg_model_set_named_pair(model, "D4_POS", &pair);
304 primitive = adg_path_over_primitive(path);
305 cpml_primitive_put_point(primitive, 0, &tmp);
306 tmp.x += part->RD34;
307 adg_model_set_named_pair(model, "RD34", &tmp);
309 tmp.x -= cos(G_PI_4) * part->RD34,
310 tmp.y -= sin(G_PI_4) * part->RD34,
311 adg_model_set_named_pair(model, "RD34_R", &tmp);
313 tmp.x += part->RD34,
314 tmp.y += part->RD34,
315 adg_model_set_named_pair(model, "RD34_XY", &tmp);
317 pair.x += (part->D4 - part->D5) / 2;
318 pair.y = part->D5 / 2;
319 adg_path_line_to(path, &pair);
320 adg_model_set_named_pair(model, "D5I", &pair);
322 pair.x = part->A - part->C;
323 adg_path_line_to(path, &pair);
325 adg_path_fillet(path, 0.2);
327 pair.y = part->D6 / 2;
328 adg_path_line_to(path, &pair);
330 primitive = adg_path_over_primitive(path);
331 cpml_primitive_put_point(primitive, 0, &tmp);
332 adg_model_set_named_pair(model, "D5F", &tmp);
334 adg_path_fillet(path, 0.1);
336 pair.x += part->LD6;
337 adg_path_line_to(path, &pair);
338 adg_model_set_named_pair(model, "D6F", &pair);
340 primitive = adg_path_over_primitive(path);
341 cpml_primitive_put_point(primitive, 0, &tmp);
342 adg_model_set_named_pair(model, "D6I_X", &tmp);
344 primitive = adg_path_over_primitive(path);
345 cpml_primitive_put_point(primitive, -1, &tmp);
346 adg_model_set_named_pair(model, "D6I_Y", &tmp);
348 pair.x = part->A - part->LD7;
349 pair.y -= (part->C - part->LD7 - part->LD6) / SQRT3;
350 adg_path_line_to(path, &pair);
351 adg_model_set_named_pair(model, "D67", &pair);
353 pair.y = part->D7 / 2;
354 adg_path_line_to(path, &pair);
356 pair.x = part->A;
357 adg_path_line_to(path, &pair);
358 adg_model_set_named_pair(model, "D7F", &pair);
360 adg_path_reflect(path, NULL);
361 adg_path_close(path);
364 static void
365 _adg_part_define_axis(DemoPart *part)
367 AdgPath *path;
369 path = part->axis;
371 /* XXX: actually the end points can extend outside the body
372 * only in local space. The proper extension values should be
373 * expressed in global space but actually is impossible to
374 * combine local and global space in the AdgPath API.
376 adg_path_move_to_explicit(path, -1, 0);
377 adg_path_line_to_explicit(path, part->A + 1, 0);
380 static void
381 _adg_part_lock(DemoPart *part)
383 gtk_widget_set_sensitive(GTK_WIDGET(part->apply), FALSE);
384 gtk_widget_set_sensitive(GTK_WIDGET(part->reset), FALSE);
387 static void
388 _adg_part_unlock(DemoPart *part)
390 gtk_widget_set_sensitive(GTK_WIDGET(part->apply), TRUE);
391 gtk_widget_set_sensitive(GTK_WIDGET(part->reset), TRUE);
394 static void
395 _adg_part_link(DemoPart *part, gpointer data, GObject *widget)
397 const gchar *edit_signal;
399 g_assert(GTK_IS_WIDGET(widget));
400 g_object_ref(widget);
401 g_hash_table_insert(part->widgets, data, widget);
403 if (GTK_IS_SPIN_BUTTON(widget)) {
404 GtkSpinButton *spin_button = GTK_SPIN_BUTTON(widget);
405 gtk_adjustment_value_changed(gtk_spin_button_get_adjustment(spin_button));
408 edit_signal = GTK_IS_TOGGLE_BUTTON(widget) ? "toggled" : "changed";
409 g_signal_connect_swapped(widget, edit_signal,
410 G_CALLBACK(_adg_part_unlock), part);
413 static void
414 _adg_part_ui_to_boolean(DemoPart *part, gboolean *data)
416 GtkWidget *widget = g_hash_table_lookup(part->widgets, data);
418 g_assert(GTK_IS_TOGGLE_BUTTON(widget));
420 *data = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
423 static void
424 _adg_part_ui_to_double(DemoPart *part, gdouble *data)
426 GtkWidget *widget;
427 GtkSpinButton *spin_button;
429 widget = g_hash_table_lookup(part->widgets, data);
430 g_assert(GTK_IS_SPIN_BUTTON(widget));
432 spin_button = GTK_SPIN_BUTTON(widget);
433 gtk_spin_button_update(spin_button);
434 *data = gtk_spin_button_get_value(spin_button);
437 static void
438 _adg_part_ui_to_string(DemoPart *part, gchar **data)
440 GtkWidget *widget = g_hash_table_lookup(part->widgets, data);
442 g_assert(GTK_IS_ENTRY(widget));
444 g_free(*data);
445 *data = g_strdup(gtk_entry_get_text(GTK_ENTRY(widget)));
448 static void
449 _adg_part_boolean_to_ui(DemoPart *part, gboolean *data)
451 GtkWidget *widget = g_hash_table_lookup(part->widgets, data);
453 g_assert(GTK_IS_TOGGLE_BUTTON(widget));
455 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), *data);
458 static void
459 _adg_part_double_to_ui(DemoPart *part, gdouble *data)
461 GtkWidget *widget = g_hash_table_lookup(part->widgets, data);
463 g_assert(GTK_IS_SPIN_BUTTON(widget));
465 gtk_spin_button_set_value(GTK_SPIN_BUTTON(widget), *data);
468 static void
469 _adg_part_string_to_ui(DemoPart *part, gchar **data)
471 GtkWidget *widget = g_hash_table_lookup(part->widgets, data);
473 g_assert(GTK_IS_ENTRY(widget));
475 gtk_entry_set_text(GTK_ENTRY(widget), *data);
478 static void
479 _adg_demo_canvas_add_dimensions(AdgCanvas *canvas, AdgModel *model)
481 AdgLDim *ldim;
482 AdgADim *adim;
483 AdgRDim *rdim;
485 /* NORTH */
486 ldim = adg_ldim_new_full_from_model(model, "-D3I_X", "-D3F_X", "-D3F_Y",
487 ADG_DIR_UP);
488 adg_dim_set_outside(ADG_DIM(ldim), ADG_THREE_STATE_OFF);
489 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
491 ldim = adg_ldim_new_full_from_model(model, "-D6I_X", "-D67", "-East",
492 ADG_DIR_UP);
493 adg_dim_set_level(ADG_DIM(ldim), 0);
494 adg_ldim_switch_extension1(ldim, FALSE);
495 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
497 ldim = adg_ldim_new_full_from_model(model, "-D6I_X", "-D7F", "-East",
498 ADG_DIR_UP);
499 adg_dim_set_limits(ADG_DIM(ldim), "-0.06", NULL);
500 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
502 adim = adg_adim_new_full_from_model(model, "-D6I_Y", "-D6F",
503 "-D6F", "-D67", "-D6F");
504 adg_dim_set_level(ADG_DIM(adim), 2);
505 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(adim));
507 rdim = adg_rdim_new_full_from_model(model, "-RD34", "-RD34_R", "-RD34_XY");
508 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(rdim));
510 ldim = adg_ldim_new_full_from_model(model, "-DGROOVEI_X", "-DGROOVEF_X",
511 "-DGROOVEX_POS", ADG_DIR_UP);
512 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
514 ldim = adg_ldim_new_full_from_model(model, "D2I", "-D2I", "-D2_POS",
515 ADG_DIR_LEFT);
516 adg_dim_set_limits(ADG_DIM(ldim), "-0.1", NULL);
517 adg_dim_set_outside(ADG_DIM(ldim), ADG_THREE_STATE_OFF);
518 adg_dim_set_value(ADG_DIM(ldim), ADG_UTF8_DIAMETER "<>");
519 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
521 ldim = adg_ldim_new_full_from_model(model, "DGROOVEI_Y", "-DGROOVEI_Y",
522 "-DGROOVEY_POS", ADG_DIR_LEFT);
523 adg_dim_set_limits(ADG_DIM(ldim), "-0.1", NULL);
524 adg_dim_set_outside(ADG_DIM(ldim), ADG_THREE_STATE_OFF);
525 adg_dim_set_value(ADG_DIM(ldim), ADG_UTF8_DIAMETER "<>");
526 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
528 /* SOUTH */
529 adim = adg_adim_new_full_from_model(model, "D1F", "D1I", "D2I", "D1F", "D1F");
530 adg_dim_set_level(ADG_DIM(adim), 2);
531 adg_adim_switch_extension2(adim, FALSE);
532 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(adim));
534 ldim = adg_ldim_new_full_from_model(model, "D1I", "LHOLE", "West",
535 ADG_DIR_DOWN);
536 adg_ldim_switch_extension1(ldim, FALSE);
537 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
539 ldim = adg_ldim_new_full_from_model(model, "D1I", "DGROOVEI_X", "West",
540 ADG_DIR_DOWN);
541 adg_ldim_switch_extension1(ldim, FALSE);
542 adg_dim_set_level(ADG_DIM(ldim), 2);
543 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
545 ldim = adg_ldim_new_full_from_model(model, "D4F", "D6I_X", "D4_POS",
546 ADG_DIR_DOWN);
547 adg_dim_set_limits(ADG_DIM(ldim), NULL, "+0.2");
548 adg_dim_set_outside(ADG_DIM(ldim), ADG_THREE_STATE_OFF);
549 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
551 ldim = adg_ldim_new_full_from_model(model, "D1F", "D3I_X", "D2_POS",
552 ADG_DIR_DOWN);
553 adg_dim_set_level(ADG_DIM(ldim), 2);
554 adg_ldim_switch_extension2(ldim, FALSE);
555 adg_dim_set_outside(ADG_DIM(ldim), ADG_THREE_STATE_OFF);
556 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
558 ldim = adg_ldim_new_full_from_model(model, "D3I_X", "D7F", "East",
559 ADG_DIR_DOWN);
560 adg_dim_set_limits(ADG_DIM(ldim), NULL, "+0.1");
561 adg_dim_set_level(ADG_DIM(ldim), 2);
562 adg_dim_set_outside(ADG_DIM(ldim), ADG_THREE_STATE_OFF);
563 adg_ldim_switch_extension2(ldim, FALSE);
564 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
566 ldim = adg_ldim_new_full_from_model(model, "D1I", "D7F", "D3F_Y",
567 ADG_DIR_DOWN);
568 adg_dim_set_limits(ADG_DIM(ldim), "-0.05", "+0.05");
569 adg_dim_set_level(ADG_DIM(ldim), 3);
570 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
572 adim = adg_adim_new_full_from_model(model, "D4F", "D4I",
573 "D5I", "D4F", "D4F");
574 adg_dim_set_level(ADG_DIM(adim), 1.5);
575 adg_adim_switch_extension2(adim, FALSE);
576 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(adim));
578 /* EAST */
579 ldim = adg_ldim_new_full_from_model(model, "D6F", "-D6F", "East",
580 ADG_DIR_RIGHT);
581 adg_dim_set_limits(ADG_DIM(ldim), "-0.1", NULL);
582 adg_dim_set_level(ADG_DIM(ldim), 4);
583 adg_dim_set_value(ADG_DIM(ldim), ADG_UTF8_DIAMETER "<>");
584 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
586 ldim = adg_ldim_new_full_from_model(model, "D4F", "-D4F", "East",
587 ADG_DIR_RIGHT);
588 adg_dim_set_level(ADG_DIM(ldim), 3);
589 adg_dim_set_value(ADG_DIM(ldim), ADG_UTF8_DIAMETER "<>");
590 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
592 ldim = adg_ldim_new_full_from_model(model, "D5F", "-D5F", "East",
593 ADG_DIR_RIGHT);
594 adg_dim_set_limits(ADG_DIM(ldim), "-0.1", NULL);
595 adg_dim_set_level(ADG_DIM(ldim), 2);
596 adg_dim_set_value(ADG_DIM(ldim), ADG_UTF8_DIAMETER "<>");
597 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
599 ldim = adg_ldim_new_full_from_model(model, "D7F", "-D7F", "East",
600 ADG_DIR_RIGHT);
601 adg_dim_set_value(ADG_DIM(ldim), ADG_UTF8_DIAMETER "<>");
602 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
604 /* WEST */
605 ldim = adg_ldim_new_full_from_model(model, "DHOLE", "-DHOLE", "-West",
606 ADG_DIR_LEFT);
607 adg_dim_set_value(ADG_DIM(ldim), ADG_UTF8_DIAMETER "<>");
608 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
610 ldim = adg_ldim_new_full_from_model(model, "D1I", "-D1I", "-West",
611 ADG_DIR_LEFT);
612 adg_dim_set_limits(ADG_DIM(ldim), "-0.05", "+0.05");
613 adg_dim_set_level(ADG_DIM(ldim), 2);
614 adg_dim_set_value(ADG_DIM(ldim), ADG_UTF8_DIAMETER "<>");
615 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
617 ldim = adg_ldim_new_full_from_model(model, "D3I_Y", "-D3I_Y", "-West",
618 ADG_DIR_LEFT);
619 adg_dim_set_limits(ADG_DIM(ldim), "-0.25", NULL);
620 adg_dim_set_level(ADG_DIM(ldim), 3);
621 adg_dim_set_value(ADG_DIM(ldim), ADG_UTF8_DIAMETER "<>");
622 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
625 static void
626 _adg_demo_canvas_add_axis(AdgCanvas *canvas, AdgTrail *trail)
628 AdgStroke *stroke = adg_stroke_new(trail);
629 adg_stroke_set_line_dress(stroke, ADG_DRESS_LINE_AXIS);
630 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(stroke));
633 static AdgCanvas *
634 _adg_canvas_init(AdgCanvas *canvas, DemoPart *part)
636 AdgContainer *container;
637 AdgEntity *entity;
639 container = (AdgContainer *) canvas;
641 adg_canvas_set_paper(canvas, GTK_PAPER_NAME_A4,
642 GTK_PAGE_ORIENTATION_LANDSCAPE);
643 adg_canvas_set_title_block(canvas, part->title_block);
645 entity = ADG_ENTITY(adg_stroke_new(ADG_TRAIL(part->body)));
646 adg_container_add(container, entity);
648 entity = ADG_ENTITY(adg_hatch_new(ADG_TRAIL(part->hole)));
649 adg_container_add(container, entity);
651 entity = ADG_ENTITY(adg_stroke_new(ADG_TRAIL(part->hole)));
652 adg_container_add(container, entity);
654 entity = ADG_ENTITY(adg_stroke_new(ADG_TRAIL(part->edges)));
655 adg_container_add(container, entity);
657 _adg_demo_canvas_add_dimensions(canvas, ADG_MODEL(part->body));
659 _adg_demo_canvas_add_axis(canvas, ADG_TRAIL(part->axis));
661 return canvas;
664 static GtkRadioButton *
665 _adg_group_get_active(GtkRadioButton *radio_group)
667 GSList *list = gtk_radio_button_get_group(radio_group);
669 while (list) {
670 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(list->data)))
671 return list->data;
673 list = list->next;
676 return NULL;
679 static void
680 _adg_do_edit(DemoPart *part)
682 _adg_part_ui_to_double(part, &part->A);
683 _adg_part_ui_to_double(part, &part->B);
684 _adg_part_ui_to_double(part, &part->C);
685 _adg_part_ui_to_double(part, &part->DHOLE);
686 _adg_part_ui_to_double(part, &part->LHOLE);
687 _adg_part_ui_to_double(part, &part->D1);
688 _adg_part_ui_to_double(part, &part->D2);
689 _adg_part_ui_to_double(part, &part->LD2);
690 _adg_part_ui_to_double(part, &part->D3);
691 _adg_part_ui_to_double(part, &part->LD3);
692 _adg_part_ui_to_double(part, &part->D4);
693 //_adg_part_ui_to_double(part, &part->D5);
694 _adg_part_ui_to_double(part, &part->D6);
695 //_adg_part_ui_to_double(part, &part->LD6);
696 _adg_part_ui_to_double(part, &part->D7);
697 //_adg_part_ui_to_double(part, &part->LD7);
699 _adg_part_ui_to_boolean(part, &part->GROOVE);
700 _adg_part_ui_to_double(part, &part->ZGROOVE);
701 _adg_part_ui_to_double(part, &part->DGROOVE);
702 _adg_part_ui_to_double(part, &part->LGROOVE);
704 _adg_part_ui_to_string(part, &part->TITLE);
705 _adg_part_ui_to_string(part, &part->DRAWING);
706 _adg_part_ui_to_string(part, &part->AUTHOR);
707 _adg_part_ui_to_string(part, &part->DATE);
709 _adg_part_lock(part);
711 adg_model_reset(ADG_MODEL(part->body));
712 adg_model_reset(ADG_MODEL(part->hole));
713 adg_model_reset(ADG_MODEL(part->axis));
714 adg_model_reset(ADG_MODEL(part->edges));
716 _adg_part_define_title_block(part);
717 _adg_part_define_body(part);
718 _adg_part_define_hole(part);
719 _adg_part_define_axis(part);
721 adg_model_changed(ADG_MODEL(part->body));
722 adg_model_changed(ADG_MODEL(part->hole));
723 adg_model_changed(ADG_MODEL(part->axis));
724 adg_model_changed(ADG_MODEL(part->edges));
726 gtk_widget_queue_draw(GTK_WIDGET(part->area));
729 static void
730 _adg_do_reset(DemoPart *part)
732 _adg_part_double_to_ui(part, &part->A);
733 _adg_part_double_to_ui(part, &part->B);
734 _adg_part_double_to_ui(part, &part->C);
735 _adg_part_double_to_ui(part, &part->DHOLE);
736 _adg_part_double_to_ui(part, &part->LHOLE);
737 _adg_part_double_to_ui(part, &part->D1);
738 _adg_part_double_to_ui(part, &part->D2);
739 _adg_part_double_to_ui(part, &part->LD2);
740 _adg_part_double_to_ui(part, &part->D3);
741 _adg_part_double_to_ui(part, &part->LD3);
742 _adg_part_double_to_ui(part, &part->D4);
743 //_adg_part_double_to_ui(part, &part->D5);
744 _adg_part_double_to_ui(part, &part->D6);
745 //_adg_part_double_to_ui(part, &part->LD6);
746 _adg_part_double_to_ui(part, &part->D7);
747 //_adg_part_double_to_ui(part, &part->LD7);
749 _adg_part_boolean_to_ui(part, &part->GROOVE);
750 _adg_part_double_to_ui(part, &part->ZGROOVE);
751 _adg_part_double_to_ui(part, &part->DGROOVE);
752 _adg_part_double_to_ui(part, &part->LGROOVE);
754 _adg_part_string_to_ui(part, &part->TITLE);
755 _adg_part_string_to_ui(part, &part->DRAWING);
756 _adg_part_string_to_ui(part, &part->AUTHOR);
757 _adg_part_string_to_ui(part, &part->DATE);
759 _adg_part_lock(part);
762 static void
763 _adg_do_save_as(GtkWindow *window, GtkResponseType response, AdgCanvas *canvas)
765 GtkRadioButton *type_radio;
766 gchar *file, *suffix;
767 cairo_surface_type_t type;
768 GError *error;
770 if (response != GTK_RESPONSE_OK) {
771 gtk_widget_hide(GTK_WIDGET(window));
772 return;
775 file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(window));
776 if (file == NULL)
777 return;
779 type_radio = _adg_group_get_active(g_object_get_data(G_OBJECT(window),
780 "type-group"));
781 g_assert(GTK_IS_RADIO_BUTTON(type_radio));
783 suffix = gtk_widget_get_tooltip_markup(GTK_WIDGET(type_radio));
784 if (suffix != NULL) {
785 /* Avoid double extension on the filename */
786 if (! g_str_has_suffix(file, suffix)) {
787 gchar *tmp = file;
788 file = g_strconcat(file, suffix, NULL);
789 g_free(tmp);
791 g_free(suffix);
794 error = NULL;
795 type = adg_type_from_filename(file);
797 if (type == CAIRO_SURFACE_TYPE_XLIB) {
798 g_set_error(&error, ADG_CANVAS_ERROR, ADG_CANVAS_ERROR_SURFACE, "%s",
799 _("Requested format not supported"));
800 } else {
801 adg_canvas_export(canvas, type, file, &error);
804 g_free(file);
805 if (error != NULL) {
806 _adg_error(error->message, window);
807 g_error_free(error);
810 gtk_widget_hide(GTK_WIDGET(window));
813 static void
814 _adg_print_prepare(GtkPrintOperation *operation, GtkPrintContext *context)
816 gtk_print_operation_set_n_pages(operation, 1);
819 static void
820 _adg_print_page(GtkPrintOperation *operation, GtkPrintContext *context,
821 gint page_nr, AdgEntity *canvas)
823 cairo_t *cr = gtk_print_context_get_cairo_context(context);
824 cairo_matrix_t old_map;
826 adg_matrix_copy(&old_map, adg_entity_get_global_map(canvas));;
828 adg_entity_set_global_map(canvas, adg_matrix_identity());
829 adg_entity_render(canvas, cr);
831 adg_entity_set_global_map(canvas, &old_map);
834 static void
835 _adg_do_print(GtkWidget *button, AdgCanvas *canvas)
837 static GtkPrintSettings *settings = NULL;
838 GtkWindow *window;
839 GtkPrintOperation *operation;
840 GtkPageSetup *page_setup;
841 GError *error;
843 window = (GtkWindow *) gtk_widget_get_toplevel(button);
844 operation = gtk_print_operation_new();
845 page_setup = adg_canvas_get_page_setup(canvas);
846 error = NULL;
848 if (settings)
849 gtk_print_operation_set_print_settings(operation, settings);
850 if (page_setup)
851 gtk_print_operation_set_default_page_setup(operation, page_setup);
853 g_signal_connect(operation, "begin-print",
854 G_CALLBACK(_adg_print_prepare), NULL);
855 g_signal_connect(operation, "draw-page",
856 G_CALLBACK(_adg_print_page), canvas);
858 gtk_print_operation_set_use_full_page(operation, FALSE);
859 gtk_print_operation_set_unit(operation, GTK_UNIT_POINTS);
860 #if GTK_CHECK_VERSION(2, 18, 0)
861 gtk_print_operation_set_embed_page_setup(operation, TRUE);
862 #endif
864 switch (gtk_print_operation_run(operation,
865 GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
866 window, &error)) {
868 case GTK_PRINT_OPERATION_RESULT_APPLY:
869 if (settings)
870 g_object_unref(settings);
871 settings = gtk_print_operation_get_print_settings(operation);
872 if (settings)
873 g_object_ref(settings);
874 break;
876 default:
877 break;
880 g_object_unref(operation);
882 if (error)
883 _adg_error(error->message, window);
886 static gboolean
887 _adg_button_press(AdgGtkArea *area, GdkEventButton *event)
889 AdgCanvas *canvas;
891 if (event->button != 1 && event->button != 3)
892 return FALSE;
894 canvas = adg_gtk_area_get_canvas(area);
895 g_return_val_if_fail(ADG_IS_CANVAS(canvas), FALSE);
897 if (event->button == 1) {
898 /* Restore the original zoom */
899 adg_gtk_area_reset(area);
900 } else if (event->button == 3) {
901 adg_canvas_autoscale(canvas);
902 gtk_widget_queue_draw((GtkWidget *) area);
905 return FALSE;
908 static DemoPart *
909 _adg_part_new(GtkBuilder *builder)
911 DemoPart *part;
912 GObject *object, *toggle_object;
914 part = g_new0(DemoPart, 1);
915 part->widgets = g_hash_table_new_full(g_direct_hash, g_direct_equal,
916 NULL, g_object_unref);
917 part->area = (AdgGtkArea *) gtk_builder_get_object(builder, "mainCanvas");
918 part->apply = (GtkButton *) gtk_builder_get_object(builder, "btnApply");
919 part->reset = (GtkButton *) gtk_builder_get_object(builder, "btnReset");
920 part->body = adg_path_new();
921 part->hole = adg_path_new();
922 part->axis = adg_path_new();
923 part->title_block = adg_title_block_new();
924 part->edges = adg_edges_new_with_source(ADG_TRAIL(part->body));
926 g_assert(ADG_GTK_IS_AREA(part->area));
927 g_assert(GTK_IS_BUTTON(part->apply));
928 g_assert(GTK_IS_BUTTON(part->reset));
930 _adg_part_link(part, &part->A, gtk_builder_get_object(builder, "editA"));
931 _adg_part_link(part, &part->B, gtk_builder_get_object(builder, "editB"));
932 _adg_part_link(part, &part->C, gtk_builder_get_object(builder, "editC"));
933 _adg_part_link(part, &part->D1, gtk_builder_get_object(builder, "editD1"));
934 _adg_part_link(part, &part->D2, gtk_builder_get_object(builder, "editD2"));
935 _adg_part_link(part, &part->LD2, gtk_builder_get_object(builder, "editLD2"));
936 _adg_part_link(part, &part->D3, gtk_builder_get_object(builder, "editD3"));
937 _adg_part_link(part, &part->LD3, gtk_builder_get_object(builder, "editLD3"));
938 _adg_part_link(part, &part->D4, gtk_builder_get_object(builder, "editD4"));
939 //_adg_part_link(part, &part->D5, gtk_builder_get_object(builder, "editD5"));
940 _adg_part_link(part, &part->D6, gtk_builder_get_object(builder, "editD6"));
941 //_adg_part_link(part, &part->LD6, gtk_builder_get_object(builder, "editLD6"));
942 _adg_part_link(part, &part->D7, gtk_builder_get_object(builder, "editD7"));
943 //_adg_part_link(part, &part->LD7, gtk_builder_get_object(builder, "editLD7"));
944 _adg_part_link(part, &part->DHOLE, gtk_builder_get_object(builder, "editDHOLE"));
945 _adg_part_link(part, &part->LHOLE, gtk_builder_get_object(builder, "editLHOLE"));
947 g_signal_connect(part->area, "button-press-event",
948 G_CALLBACK(_adg_button_press), NULL);
950 toggle_object = gtk_builder_get_object(builder, "editGROOVE");
951 _adg_part_link(part, &part->GROOVE, toggle_object);
953 object = gtk_builder_get_object(builder, "editZGROOVE");
954 _adg_part_link(part, &part->ZGROOVE, object);
955 g_signal_connect(toggle_object, "toggled",
956 G_CALLBACK(adg_gtk_toggle_button_sensitivize), object);
957 object = gtk_builder_get_object(builder, "editZGROOVELabel");
958 g_signal_connect(toggle_object, "toggled",
959 G_CALLBACK(adg_gtk_toggle_button_sensitivize), object);
961 object = gtk_builder_get_object(builder, "editDGROOVE");
962 _adg_part_link(part, &part->DGROOVE, object);
963 g_signal_connect(toggle_object, "toggled",
964 G_CALLBACK(adg_gtk_toggle_button_sensitivize), object);
965 object = gtk_builder_get_object(builder, "editDGROOVELabel");
966 g_signal_connect(toggle_object, "toggled",
967 G_CALLBACK(adg_gtk_toggle_button_sensitivize), object);
969 object = gtk_builder_get_object(builder, "editLGROOVE");
970 _adg_part_link(part, &part->LGROOVE, object);
971 g_signal_connect(toggle_object, "toggled",
972 G_CALLBACK(adg_gtk_toggle_button_sensitivize), object);
973 object = gtk_builder_get_object(builder, "editLGROOVELabel");
974 g_signal_connect(toggle_object, "toggled",
975 G_CALLBACK(adg_gtk_toggle_button_sensitivize), object);
977 _adg_part_link(part, &part->TITLE, gtk_builder_get_object(builder, "editTITLE"));
978 _adg_part_link(part, &part->DRAWING, gtk_builder_get_object(builder, "editDRAWING"));
979 _adg_part_link(part, &part->AUTHOR, gtk_builder_get_object(builder, "editAUTHOR"));
980 _adg_part_link(part, &part->DATE, gtk_builder_get_object(builder, "editDATE"));
982 part->D5 = 4.5;
983 part->RD34 = 1;
984 part->LD5 = 5;
985 part->LD6 = 1;
986 part->LD7 = 0.5;
988 _adg_do_edit(part);
990 return part;
993 static void
994 _adg_part_destroy(DemoPart *part)
996 g_hash_table_destroy(part->widgets);
997 g_free(part);
1000 static GtkWidget *
1001 _adg_help_window(GtkBuilder *builder)
1003 GtkWidget *window;
1005 window = (GtkWidget *) gtk_builder_get_object(builder, "wndHelp");
1006 g_assert(GTK_IS_MESSAGE_DIALOG(window));
1007 g_signal_connect(window, "response", G_CALLBACK(gtk_widget_hide), NULL);
1009 return window;
1012 static GtkWidget *
1013 _adg_about_window(GtkBuilder *builder)
1015 GtkWidget *window;
1016 GList *icon_list, *last_node;
1018 window = (GtkWidget *) gtk_builder_get_object(builder, "wndAbout");
1019 g_assert(GTK_IS_ABOUT_DIALOG(window));
1020 g_signal_connect(window, "response", G_CALLBACK(gtk_widget_hide), NULL);
1022 icon_list = gtk_window_get_default_icon_list();
1023 last_node = g_list_last(icon_list);
1024 if (last_node != NULL) {
1025 /* The last icon is supposed to be the largest one:
1026 * check adg_gtk_use_default_icons() implementation. */
1027 GdkPixbuf *last_icon = last_node->data;
1028 gtk_about_dialog_set_logo(GTK_ABOUT_DIALOG(window), last_icon);
1030 g_list_free(icon_list);
1032 return window;
1035 static GtkWidget *
1036 _adg_edit_window(GtkBuilder *builder, DemoPart *part)
1038 GtkWidget *window;
1040 window = (GtkWidget *) gtk_builder_get_object(builder, "wndEdit");
1041 g_assert(GTK_IS_DIALOG(window));
1043 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_MOUSE);
1045 g_signal_connect_swapped(part->apply, "clicked",
1046 G_CALLBACK(_adg_do_edit), part);
1047 g_signal_connect_swapped(part->reset, "clicked",
1048 G_CALLBACK(_adg_do_reset), part);
1049 g_signal_connect(window, "response",
1050 G_CALLBACK(adg_gtk_window_hide_here), NULL);
1052 return window;
1055 static GtkWidget *
1056 _adg_save_as_window(GtkBuilder *builder, AdgCanvas *canvas)
1058 GtkWidget *window;
1059 GtkWidget *type_group;
1061 window = (GtkWidget *) gtk_builder_get_object(builder, "wndSaveAs");
1062 g_assert(GTK_IS_WINDOW(window));
1063 type_group = (GtkWidget *) gtk_builder_get_object(builder, "saveAsPng");
1064 g_assert(GTK_IS_RADIO_BUTTON(type_group));
1066 g_object_set_data(G_OBJECT(window), "type-group", type_group);
1068 /* Set the default destination file */
1069 if (GTK_IS_FILE_CHOOSER(window)) {
1070 GtkFileChooser *file_chooser;
1071 const gchar *dir;
1073 file_chooser = (GtkFileChooser *) window;
1075 #if GLIB_CHECK_VERSION(2, 14, 0)
1076 dir = g_get_user_special_dir(G_USER_DIRECTORY_DOCUMENTS);
1077 #else
1078 dir = NULL;
1079 #endif
1081 if (dir == NULL)
1082 dir = g_get_home_dir();
1084 gtk_file_chooser_set_current_folder(file_chooser, dir);
1085 gtk_file_chooser_set_current_name(file_chooser, "adg-demo");
1088 g_signal_connect(window, "response", G_CALLBACK(_adg_do_save_as), canvas);
1090 return window;
1093 static GtkWidget *
1094 _adg_main_window(GtkBuilder *builder)
1096 GtkWidget *window;
1097 DemoPart *part;
1098 AdgCanvas *canvas;
1099 GtkWidget *button_edit, *button_save_as, *button_print;
1100 GtkWidget *button_help, *button_about, *button_quit;
1102 if (is_installed) {
1103 #ifdef G_OS_WIN32
1104 gchar *icondir = g_build_filename(basedir, PKGDATADIR, NULL);
1105 adg_gtk_use_default_icons(icondir);
1106 g_free(icondir);
1107 #else
1108 adg_gtk_use_default_icons(PKGDATADIR);
1109 #endif
1110 } else {
1111 adg_gtk_use_default_icons(SRCDIR);
1114 window = (GtkWidget *) gtk_builder_get_object(builder, "wndMain");
1115 part = _adg_part_new(builder);
1116 canvas = adg_canvas_new();
1118 _adg_canvas_init(canvas, part);
1119 adg_gtk_area_set_canvas(part->area, canvas);
1120 adg_canvas_autoscale(canvas);
1122 button_help = (GtkWidget *) gtk_builder_get_object(builder, "mainHelp");
1123 g_assert(GTK_IS_BUTTON(button_help));
1124 button_about = (GtkWidget *) gtk_builder_get_object(builder, "mainAbout");
1125 g_assert(GTK_IS_BUTTON(button_about));
1126 g_assert(GTK_IS_WINDOW(window));
1127 button_edit = (GtkWidget *) gtk_builder_get_object(builder, "mainEdit");
1128 g_assert(GTK_IS_BUTTON(button_edit));
1129 button_save_as = (GtkWidget *) gtk_builder_get_object(builder, "mainSaveAs");
1130 g_assert(GTK_IS_BUTTON(button_save_as));
1131 button_print = (GtkWidget *) gtk_builder_get_object(builder, "mainPrint");
1132 g_assert(GTK_IS_BUTTON(button_print));
1133 button_quit = (GtkWidget *) gtk_builder_get_object(builder, "mainQuit");
1134 g_assert(GTK_IS_BUTTON(button_quit));
1136 g_signal_connect_swapped(button_help, "clicked",
1137 G_CALLBACK(gtk_dialog_run),
1138 _adg_help_window(builder));
1139 g_signal_connect_swapped(button_about, "clicked",
1140 G_CALLBACK(gtk_dialog_run),
1141 _adg_about_window(builder));
1142 g_signal_connect_swapped(button_edit, "clicked",
1143 G_CALLBACK(gtk_dialog_run),
1144 _adg_edit_window(builder, part));
1145 g_signal_connect_swapped(button_save_as, "clicked",
1146 G_CALLBACK(gtk_dialog_run),
1147 _adg_save_as_window(builder, canvas));
1148 g_signal_connect(button_print, "clicked",
1149 G_CALLBACK(_adg_do_print), canvas);
1150 g_signal_connect(button_quit, "clicked", G_CALLBACK(gtk_main_quit), NULL);
1151 g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL);
1152 g_signal_connect_swapped(window, "destroy",
1153 G_CALLBACK(_adg_part_destroy), part);
1155 return window;
1160 main(gint argc, gchar **argv)
1162 gchar *path;
1163 GtkBuilder *builder;
1164 GError *error;
1165 GtkWidget *main_window;
1167 _demo_init(argc, argv);
1168 parse_args(&argc, &argv);
1169 adg_switch_extents(show_extents);
1171 path = _demo_file("adg-demo.ui");
1172 if (path == NULL) {
1173 g_printerr(_("adg-demo.ui not found!\n"));
1174 return 1;
1177 builder = gtk_builder_new();
1178 error = NULL;
1180 gtk_builder_set_translation_domain(builder, GETTEXT_PACKAGE);
1181 gtk_builder_add_from_file(builder, path, &error);
1182 g_free(path);
1184 if (error != NULL) {
1185 g_critical("%s", error->message);
1186 g_error_free(error);
1187 return 2;
1190 main_window = _adg_main_window(builder);
1191 g_object_unref(builder);
1193 gtk_widget_show_all(main_window);
1194 gtk_main();
1196 return 0;