[docs] Updated italian translations
[adg.git] / demo / adg-demo.c
blob0f9192c468b108db034db5a622f7e031939ecfac
1 /* Needed for localization support */
2 #include <adg/adg-internal.h>
4 /* Force the reinclusion of adg.h */
5 #undef __ADG_H__
7 #include "demo.h"
9 #include <adg-gtk.h>
10 #include <string.h>
11 #include <math.h>
13 #define SQRT3 1.732050808
14 #define CHAMFER 0.3
17 typedef struct _AdgPart AdgPart;
19 struct _AdgPart {
20 /* Raw data */
21 gdouble A, B, C;
22 gdouble DHOLE, LHOLE;
23 gdouble D1, D2, D3, D4, D5, D6, D7;
24 gdouble RD34, RD56;
25 gdouble LD2, LD3, LD5, LD6, LD7;
27 gboolean GROOVE;
28 gdouble ZGROOVE, DGROOVE, LGROOVE;
30 /* User interface widgets */
31 AdgGtkArea *area;
32 GHashTable *widgets;
33 GtkButton *apply, *reset;
35 /* Models */
36 AdgPath *shape, *hatch, *hatch_edge;
37 AdgEdges *edges;
41 static void
42 _adg_version(void)
44 g_print("adg-demo " PACKAGE_VERSION "\n");
45 exit(0);
48 static void
49 _adg_parse_args(gint *p_argc, gchar **p_argv[], gboolean *show_extents)
51 GError *error = NULL;
52 GOptionEntry entries[] = {
53 {"version", 'V', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
54 _adg_version, _("Display version information"), NULL},
55 {"show-extents", 'E', 0, G_OPTION_ARG_NONE,
56 NULL, _("Show the boundary boxes of every entity"), NULL},
57 {NULL}
60 entries[1].arg_data = show_extents;
61 *show_extents = FALSE;
62 gtk_init_with_args(p_argc, p_argv, _("- ADG demonstration program"),
63 entries, GETTEXT_PACKAGE, &error);
65 if (error != NULL) {
66 gint error_code = error->code;
68 g_printerr("%s\n", error->message);
70 g_error_free(error);
71 exit(error_code);
76 /**
77 * _adg_error:
78 * @message: a custom error message
79 * @parent_window: the parent window or %NULL
81 * Convenient function that presents an error dialog and waits the user
82 * to close this modal dialog.
83 **/
84 void
85 _adg_error(const gchar *message, GtkWindow *parent_window)
87 GtkWidget *dialog = gtk_message_dialog_new(parent_window,
88 GTK_DIALOG_DESTROY_WITH_PARENT,
89 GTK_MESSAGE_ERROR,
90 GTK_BUTTONS_CLOSE,
91 "%s", message);
92 gtk_window_set_title(GTK_WINDOW(dialog), _("Error from adg-demo"));
93 gtk_dialog_run(GTK_DIALOG(dialog));
94 gtk_widget_destroy(dialog);
97 static void
98 _adg_path_add_hole(AdgPath *path, const AdgPart *part)
100 AdgModel *model;
101 AdgPair pair;
103 model = ADG_MODEL(path);
104 pair.x = part->LHOLE;
105 pair.y = 0;
107 adg_path_move_to(path, &pair);
108 adg_model_set_named_pair(model, "LHOLE", &pair);
110 pair.y = part->DHOLE / 2;
111 pair.x -= pair.y / SQRT3;
112 adg_path_line_to(path, &pair);
114 pair.x = 0;
115 adg_path_line_to(path, &pair);
116 adg_model_set_named_pair(model, "DHOLE", &pair);
119 static void
120 _adg_path_add_rail(AdgPath *path, const AdgPart *part, gdouble to_x)
122 AdgModel *model;
123 AdgPair pair;
125 model = ADG_MODEL(path);
126 pair.x = 0;
127 pair.y = part->D1 / 2;
129 adg_path_line_to(path, &pair);
130 adg_model_set_named_pair(model, "D1I", &pair);
132 if (part->GROOVE && to_x > part->ZGROOVE) {
133 pair.x = part->ZGROOVE;
134 adg_path_line_to(path, &pair);
135 adg_model_set_named_pair(model, "DGROOVEI_X", &pair);
137 pair.y = part->D3 / 2;
138 adg_model_set_named_pair(model, "DGROOVEY_POS", &pair);
140 pair.y = part->DGROOVE / 2;
141 adg_path_line_to(path, &pair);
142 adg_model_set_named_pair(model, "DGROOVEI_Y", &pair);
144 pair.x += part->LGROOVE;
145 adg_path_line_to(path, &pair);
147 pair.y = part->D3 / 2;
148 adg_model_set_named_pair(model, "DGROOVEX_POS", &pair);
150 pair.y = part->D1 / 2;
151 adg_path_line_to(path, &pair);
152 adg_model_set_named_pair(model, "DGROOVEF_X", &pair);
155 pair.x = to_x;
156 adg_path_line_to(path, &pair);
157 adg_model_set_named_pair(model, "D1F", &pair);
160 static void
161 _adg_part_define_hatch(AdgPart *part)
163 AdgPath *path;
164 AdgPair pair;
166 path = part->hatch;
167 pair.x = 0;
168 pair.y = (part->D1 + part->DHOLE) / 4;
170 _adg_path_add_hole(path, part);
171 adg_path_line_to(path, &pair);
172 adg_path_curve_to_explicit(path,
173 part->LHOLE / 2, part->DHOLE / 2,
174 part->LHOLE + 2, part->D1 / 2,
175 part->LHOLE + 2, 0);
176 adg_path_reflect(path, NULL);
177 adg_path_close(path);
179 path = part->hatch_edge;
180 adg_path_move_to(path, &pair);
181 adg_path_curve_to_explicit(path,
182 part->LHOLE / 2, part->DHOLE / 2,
183 part->LHOLE + 2, part->D1 / 2,
184 part->LHOLE + 2, 0);
185 adg_path_reflect(path, NULL);
188 static void
189 _adg_part_define_shape(AdgPart *part)
191 AdgModel *model;
192 AdgPath *path;
193 AdgPair pair, tmp;
194 const AdgPrimitive *primitive;
196 path = part->shape;
197 model = ADG_MODEL(path);
198 pair.x = part->A - part->B - part->LD2;
199 pair.y = part->D3 / 2;
200 adg_model_set_named_pair(model, "D2_POS", &pair);
202 _adg_path_add_hole(path, part);
203 _adg_path_add_rail(path, part, pair.x);
205 pair.x += (part->D1 - part->D2) / 2;
206 pair.y = part->D2 / 2;
207 adg_path_line_to(path, &pair);
208 adg_model_set_named_pair(model, "D2I", &pair);
210 pair.x = part->A - part->B;
211 adg_path_line_to(path, &pair);
212 adg_path_fillet(path, 0.4);
214 pair.x = part->A - part->B;
215 pair.y = part->D3 / 2;
216 adg_path_line_to(path, &pair);
217 adg_model_set_named_pair(model, "D3I", &pair);
219 pair.x = part->A;
220 adg_model_set_named_pair(model, "East", &pair);
222 pair.x = 0;
223 adg_model_set_named_pair(model, "West", &pair);
225 adg_path_chamfer(path, CHAMFER, CHAMFER);
227 pair.x = part->A - part->B + part->LD3;
228 pair.y = part->D3 / 2;
229 adg_path_line_to(path, &pair);
231 primitive = adg_path_over_primitive(path);
232 cpml_pair_from_cairo(&tmp, cpml_primitive_get_point(primitive, 0));
233 adg_model_set_named_pair(model, "D3I_X", &tmp);
235 cpml_pair_from_cairo(&tmp, cpml_primitive_get_point(primitive, -1));
236 adg_model_set_named_pair(model, "D3I_Y", &tmp);
238 adg_path_chamfer(path, CHAMFER, CHAMFER);
240 pair.y = part->D4 / 2;
241 adg_path_line_to(path, &pair);
243 primitive = adg_path_over_primitive(path);
244 cpml_pair_from_cairo(&tmp, cpml_primitive_get_point(primitive, 0));
245 adg_model_set_named_pair(model, "D3F_Y", &tmp);
246 cpml_pair_from_cairo(&tmp, cpml_primitive_get_point(primitive, -1));
247 adg_model_set_named_pair(model, "D3F_X", &tmp);
249 adg_path_fillet(path, part->RD34);
251 pair.x += part->RD34;
252 adg_model_set_named_pair(model, "D4I", &pair);
254 pair.x = part->A - part->C - part->LD5;
255 adg_path_line_to(path, &pair);
256 adg_model_set_named_pair(model, "D4F", &pair);
258 pair.y = part->D3 / 2;
259 adg_model_set_named_pair(model, "D4_POS", &pair);
261 primitive = adg_path_over_primitive(path);
262 cpml_pair_from_cairo(&tmp, cpml_primitive_get_point(primitive, 0));
263 tmp.x += part->RD34;
264 adg_model_set_named_pair(model, "RD34", &tmp);
266 tmp.x -= cos(G_PI_4) * part->RD34,
267 tmp.y -= sin(G_PI_4) * part->RD34,
268 adg_model_set_named_pair(model, "RD34_R", &tmp);
270 tmp.x += part->RD34,
271 tmp.y += part->RD34,
272 adg_model_set_named_pair(model, "RD34_XY", &tmp);
274 pair.x += (part->D4 - part->D5) / 2;
275 pair.y = part->D5 / 2;
276 adg_path_line_to(path, &pair);
277 adg_model_set_named_pair(model, "D5I", &pair);
279 pair.x = part->A - part->C;
280 adg_path_line_to(path, &pair);
282 adg_path_fillet(path, 0.2);
284 pair.y = part->D6 / 2;
285 adg_path_line_to(path, &pair);
287 primitive = adg_path_over_primitive(path);
288 cpml_pair_from_cairo(&tmp, cpml_primitive_get_point(primitive, 0));
289 adg_model_set_named_pair(model, "D5F", &tmp);
291 adg_path_fillet(path, 0.1);
293 pair.x += part->LD6;
294 adg_path_line_to(path, &pair);
295 adg_model_set_named_pair(model, "D6F", &pair);
297 primitive = adg_path_over_primitive(path);
298 cpml_pair_from_cairo(&tmp, cpml_primitive_get_point(primitive, 0));
299 adg_model_set_named_pair(model, "D6I_X", &tmp);
301 primitive = adg_path_over_primitive(path);
302 cpml_pair_from_cairo(&tmp, cpml_primitive_get_point(primitive, -1));
303 adg_model_set_named_pair(model, "D6I_Y", &tmp);
305 pair.x = part->A - part->LD7;
306 pair.y -= (part->C - part->LD7 - part->LD6) / SQRT3;
307 adg_path_line_to(path, &pair);
308 adg_model_set_named_pair(model, "D67", &pair);
310 pair.y = part->D7 / 2;
311 adg_path_line_to(path, &pair);
313 pair.x = part->A;
314 adg_path_line_to(path, &pair);
315 adg_model_set_named_pair(model, "D7F", &pair);
317 adg_path_reflect(path, NULL);
318 adg_path_close(path);
321 static void
322 _adg_part_lock(AdgPart *part)
324 gtk_widget_set_sensitive(GTK_WIDGET(part->apply), FALSE);
325 gtk_widget_set_sensitive(GTK_WIDGET(part->reset), FALSE);
328 static void
329 _adg_part_unlock(AdgPart *part)
331 gtk_widget_set_sensitive(GTK_WIDGET(part->apply), TRUE);
332 gtk_widget_set_sensitive(GTK_WIDGET(part->reset), TRUE);
335 static void
336 _adg_part_link(AdgPart *part, gpointer data, GObject *widget)
338 const gchar *edit_signal;
340 g_assert(GTK_IS_WIDGET(widget));
341 g_object_ref(widget);
342 g_hash_table_insert(part->widgets, data, widget);
344 if (GTK_IS_SPIN_BUTTON(widget)) {
345 GtkSpinButton *spin_button = GTK_SPIN_BUTTON(widget);
346 gtk_adjustment_value_changed(gtk_spin_button_get_adjustment(spin_button));
349 edit_signal = GTK_IS_TOGGLE_BUTTON(widget) ? "toggled" : "changed";
350 g_signal_connect_swapped(widget, edit_signal,
351 G_CALLBACK(_adg_part_unlock), part);
354 static void
355 _adg_part_ui_to_boolean(AdgPart *part, gboolean *data)
357 GtkWidget *widget = g_hash_table_lookup(part->widgets, data);
359 g_assert(GTK_IS_TOGGLE_BUTTON(widget));
361 *data = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
364 static void
365 _adg_part_ui_to_double(AdgPart *part, gdouble *data)
367 GtkWidget *widget = g_hash_table_lookup(part->widgets, data);
369 g_assert(GTK_IS_SPIN_BUTTON(widget));
371 *data = gtk_spin_button_get_value(GTK_SPIN_BUTTON(widget));
374 static void
375 _adg_part_boolean_to_ui(AdgPart *part, gboolean *data)
377 GtkWidget *widget = g_hash_table_lookup(part->widgets, data);
379 g_assert(GTK_IS_TOGGLE_BUTTON(widget));
381 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), *data);
384 static void
385 _adg_part_double_to_ui(AdgPart *part, gdouble *data)
387 GtkWidget *widget = g_hash_table_lookup(part->widgets, data);
389 g_assert(GTK_IS_WIDGET(widget));
391 gtk_spin_button_set_value(GTK_SPIN_BUTTON(widget), *data);
394 static void
395 _adg_demo_canvas_add_sheet(AdgCanvas *canvas)
397 AdgTitleBlock *title_block = adg_title_block_new();
399 g_object_set(title_block,
400 "title", _("SAMPLE DRAWING"),
401 "author", "NtD",
402 "date", NULL,
403 "drawing", "TEST123",
404 "logo", adg_logo_new(),
405 "projection", adg_projection_new(ADG_PROJECTION_FIRST_ANGLE),
406 "scale", "---",
407 "size", "A4",
408 NULL);
410 adg_canvas_set_title_block(canvas, title_block);
413 static void
414 _adg_demo_canvas_add_dimensions(AdgCanvas *canvas, AdgModel *model)
416 AdgLDim *ldim;
417 AdgADim *adim;
418 AdgRDim *rdim;
420 /* NORTH */
421 ldim = adg_ldim_new_full_from_model(model, "-D3I_X", "-D3F_X", "-D3F_Y",
422 ADG_DIR_UP);
423 adg_dim_set_outside(ADG_DIM(ldim), ADG_THREE_STATE_OFF);
424 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
426 ldim = adg_ldim_new_full_from_model(model, "-D6I_X", "-D67", "-East",
427 ADG_DIR_UP);
428 adg_dim_set_level(ADG_DIM(ldim), 0);
429 adg_ldim_switch_extension1(ldim, FALSE);
430 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
432 ldim = adg_ldim_new_full_from_model(model, "-D6I_X", "-D7F", "-East",
433 ADG_DIR_UP);
434 adg_dim_set_limits(ADG_DIM(ldim), "-0.06", NULL);
435 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
437 adim = adg_adim_new_full_from_model(model, "-D6I_Y", "-D6F",
438 "-D6F", "-D67", "-D6F");
439 adg_dim_set_level(ADG_DIM(adim), 2);
440 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(adim));
442 rdim = adg_rdim_new_full_from_model(model, "-RD34", "-RD34_R", "-RD34_XY");
443 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(rdim));
445 ldim = adg_ldim_new_full_from_model(model, "-DGROOVEI_X", "-DGROOVEF_X",
446 "-DGROOVEX_POS", ADG_DIR_UP);
447 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
449 ldim = adg_ldim_new_full_from_model(model, "D2I", "-D2I", "-D2_POS",
450 ADG_DIR_LEFT);
451 adg_dim_set_limits(ADG_DIM(ldim), "-0.1", NULL);
452 adg_dim_set_outside(ADG_DIM(ldim), ADG_THREE_STATE_OFF);
453 adg_dim_set_value(ADG_DIM(ldim), ADG_UTF8_DIAMETER "<>");
454 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
456 ldim = adg_ldim_new_full_from_model(model, "DGROOVEI_Y", "-DGROOVEI_Y",
457 "-DGROOVEY_POS", ADG_DIR_LEFT);
458 adg_dim_set_limits(ADG_DIM(ldim), "-0.1", NULL);
459 adg_dim_set_outside(ADG_DIM(ldim), ADG_THREE_STATE_OFF);
460 adg_dim_set_value(ADG_DIM(ldim), ADG_UTF8_DIAMETER "<>");
461 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
463 /* SOUTH */
464 adim = adg_adim_new_full_from_model(model, "D1F", "D1I", "D2I", "D1F", "D1F");
465 adg_dim_set_level(ADG_DIM(adim), 2);
466 adg_adim_switch_extension2(adim, FALSE);
467 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(adim));
469 ldim = adg_ldim_new_full_from_model(model, "D1I", "LHOLE", "West",
470 ADG_DIR_DOWN);
471 adg_ldim_switch_extension1(ldim, FALSE);
472 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
474 ldim = adg_ldim_new_full_from_model(model, "D1I", "DGROOVEI_X", "West",
475 ADG_DIR_DOWN);
476 adg_ldim_switch_extension1(ldim, FALSE);
477 adg_dim_set_level(ADG_DIM(ldim), 2);
478 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
480 ldim = adg_ldim_new_full_from_model(model, "D4F", "D6I_X", "D4_POS",
481 ADG_DIR_DOWN);
482 adg_dim_set_limits(ADG_DIM(ldim), NULL, "+0.2");
483 adg_dim_set_outside(ADG_DIM(ldim), ADG_THREE_STATE_OFF);
484 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
486 ldim = adg_ldim_new_full_from_model(model, "D1F", "D3I_X", "D2_POS",
487 ADG_DIR_DOWN);
488 adg_dim_set_level(ADG_DIM(ldim), 2);
489 adg_ldim_switch_extension2(ldim, FALSE);
490 adg_dim_set_outside(ADG_DIM(ldim), ADG_THREE_STATE_OFF);
491 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
493 ldim = adg_ldim_new_full_from_model(model, "D3I_X", "D7F", "East",
494 ADG_DIR_DOWN);
495 adg_dim_set_limits(ADG_DIM(ldim), NULL, "+0.1");
496 adg_dim_set_level(ADG_DIM(ldim), 2);
497 adg_dim_set_outside(ADG_DIM(ldim), ADG_THREE_STATE_OFF);
498 adg_ldim_switch_extension2(ldim, FALSE);
499 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
501 ldim = adg_ldim_new_full_from_model(model, "D1I", "D7F", "D3F_Y",
502 ADG_DIR_DOWN);
503 adg_dim_set_limits(ADG_DIM(ldim), "-0.05", "+0.05");
504 adg_dim_set_level(ADG_DIM(ldim), 3);
505 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
507 adim = adg_adim_new_full_from_model(model, "D4F", "D4I",
508 "D5I", "D4F", "D4F");
509 adg_dim_set_level(ADG_DIM(adim), 1.5);
510 adg_adim_switch_extension2(adim, FALSE);
511 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(adim));
513 /* EAST */
514 ldim = adg_ldim_new_full_from_model(model, "D6F", "-D6F", "East",
515 ADG_DIR_RIGHT);
516 adg_dim_set_limits(ADG_DIM(ldim), "-0.1", NULL);
517 adg_dim_set_level(ADG_DIM(ldim), 4);
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, "D4F", "-D4F", "East",
522 ADG_DIR_RIGHT);
523 adg_dim_set_level(ADG_DIM(ldim), 3);
524 adg_dim_set_value(ADG_DIM(ldim), ADG_UTF8_DIAMETER "<>");
525 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
527 ldim = adg_ldim_new_full_from_model(model, "D5F", "-D5F", "East",
528 ADG_DIR_RIGHT);
529 adg_dim_set_limits(ADG_DIM(ldim), "-0.1", NULL);
530 adg_dim_set_level(ADG_DIM(ldim), 2);
531 adg_dim_set_value(ADG_DIM(ldim), ADG_UTF8_DIAMETER "<>");
532 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
534 ldim = adg_ldim_new_full_from_model(model, "D7F", "-D7F", "East",
535 ADG_DIR_RIGHT);
536 adg_dim_set_value(ADG_DIM(ldim), ADG_UTF8_DIAMETER "<>");
537 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
539 /* WEST */
540 ldim = adg_ldim_new_full_from_model(model, "DHOLE", "-DHOLE", "-West",
541 ADG_DIR_LEFT);
542 adg_dim_set_value(ADG_DIM(ldim), ADG_UTF8_DIAMETER "<>");
543 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
545 ldim = adg_ldim_new_full_from_model(model, "D1I", "-D1I", "-West",
546 ADG_DIR_LEFT);
547 adg_dim_set_limits(ADG_DIM(ldim), "-0.05", "+0.05");
548 adg_dim_set_level(ADG_DIM(ldim), 2);
549 adg_dim_set_value(ADG_DIM(ldim), ADG_UTF8_DIAMETER "<>");
550 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
552 ldim = adg_ldim_new_full_from_model(model, "D3I_Y", "-D3I_Y", "-West",
553 ADG_DIR_LEFT);
554 adg_dim_set_limits(ADG_DIM(ldim), "-0.25", NULL);
555 adg_dim_set_level(ADG_DIM(ldim), 3);
556 adg_dim_set_value(ADG_DIM(ldim), ADG_UTF8_DIAMETER "<>");
557 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(ldim));
560 static void
561 _adg_demo_canvas_add_stuff(AdgCanvas *canvas, AdgModel *model)
563 AdgToyText *toy_text;
564 AdgMatrix map;
566 toy_text = adg_toy_text_new(_("Rotate the mouse wheel to zoom in and out"));
567 adg_entity_set_local_method(ADG_ENTITY(toy_text), ADG_MIX_DISABLED);
568 cairo_matrix_init_translate(&map, 15, 490);
569 adg_entity_set_global_map(ADG_ENTITY(toy_text), &map);
570 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(toy_text));
572 toy_text = adg_toy_text_new(_("Drag with the wheel pressed to pan"));
573 adg_entity_set_local_method(ADG_ENTITY(toy_text), ADG_MIX_DISABLED);
574 cairo_matrix_init_translate(&map, 15, 505);
575 adg_entity_set_global_map(ADG_ENTITY(toy_text), &map);
576 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(toy_text));
578 toy_text = adg_toy_text_new(_("Keep the shift pressed to zoom or pan in global space"));
579 adg_entity_set_local_method(ADG_ENTITY(toy_text), ADG_MIX_DISABLED);
580 cairo_matrix_init_translate(&map, 15, 520);
581 adg_entity_set_global_map(ADG_ENTITY(toy_text), &map);
582 adg_container_add(ADG_CONTAINER(canvas), ADG_ENTITY(toy_text));
585 static AdgCanvas *
586 _adg_canvas_init(AdgCanvas *canvas, AdgPart *part)
588 AdgContainer *container;
589 AdgEntity *entity;
590 AdgMatrix map;
592 container = (AdgContainer *) canvas;
594 adg_canvas_set_paper(canvas, GTK_PAPER_NAME_A4,
595 GTK_PAGE_ORIENTATION_LANDSCAPE);
597 entity = ADG_ENTITY(adg_stroke_new(ADG_TRAIL(part->shape)));
598 adg_container_add(container, entity);
600 entity = ADG_ENTITY(adg_hatch_new(ADG_TRAIL(part->hatch)));
601 adg_container_add(container, entity);
603 entity = ADG_ENTITY(adg_stroke_new(ADG_TRAIL(part->hatch_edge)));
604 adg_container_add(container, entity);
606 entity = ADG_ENTITY(adg_stroke_new(ADG_TRAIL(part->edges)));
607 adg_container_add(container, entity);
609 _adg_demo_canvas_add_sheet(canvas);
610 _adg_demo_canvas_add_dimensions(canvas, ADG_MODEL(part->shape));
611 _adg_demo_canvas_add_stuff(canvas, ADG_MODEL(part->shape));
613 cairo_matrix_init_translate(&map, 140, 180);
614 cairo_matrix_scale(&map, 8, 8);
615 adg_entity_set_local_map(ADG_ENTITY(container), &map);
617 return canvas;
620 static GtkRadioButton *
621 _adg_group_get_active(GtkRadioButton *radio_group)
623 GSList *list = gtk_radio_button_get_group(radio_group);
625 while (list) {
626 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(list->data)))
627 return list->data;
629 list = list->next;
632 return NULL;
635 static void
636 _adg_do_edit(AdgPart *part)
638 _adg_part_ui_to_double(part, &part->A);
639 _adg_part_ui_to_double(part, &part->B);
640 _adg_part_ui_to_double(part, &part->C);
641 _adg_part_ui_to_double(part, &part->DHOLE);
642 _adg_part_ui_to_double(part, &part->LHOLE);
643 _adg_part_ui_to_double(part, &part->D1);
644 _adg_part_ui_to_double(part, &part->D2);
645 _adg_part_ui_to_double(part, &part->LD2);
646 _adg_part_ui_to_double(part, &part->D3);
647 _adg_part_ui_to_double(part, &part->LD3);
648 _adg_part_ui_to_double(part, &part->D4);
649 //_adg_part_ui_to_double(part, &part->D5);
650 _adg_part_ui_to_double(part, &part->D6);
651 //_adg_part_ui_to_double(part, &part->LD6);
652 _adg_part_ui_to_double(part, &part->D7);
653 //_adg_part_ui_to_double(part, &part->LD7);
655 _adg_part_ui_to_boolean(part, &part->GROOVE);
656 _adg_part_ui_to_double(part, &part->ZGROOVE);
657 _adg_part_ui_to_double(part, &part->DGROOVE);
658 _adg_part_ui_to_double(part, &part->LGROOVE);
660 _adg_part_lock(part);
662 adg_model_reset(ADG_MODEL(part->shape));
663 adg_model_reset(ADG_MODEL(part->hatch));
664 adg_model_reset(ADG_MODEL(part->hatch_edge));
665 adg_model_reset(ADG_MODEL(part->edges));
667 _adg_part_define_shape(part);
668 _adg_part_define_hatch(part);
670 adg_model_changed(ADG_MODEL(part->shape));
671 adg_model_changed(ADG_MODEL(part->hatch));
672 adg_model_changed(ADG_MODEL(part->hatch_edge));
673 adg_model_changed(ADG_MODEL(part->edges));
675 gtk_widget_queue_draw(GTK_WIDGET(part->area));
678 static void
679 _adg_do_reset(AdgPart *part)
681 _adg_part_double_to_ui(part, &part->A);
682 _adg_part_double_to_ui(part, &part->B);
683 _adg_part_double_to_ui(part, &part->C);
684 _adg_part_double_to_ui(part, &part->DHOLE);
685 _adg_part_double_to_ui(part, &part->LHOLE);
686 _adg_part_double_to_ui(part, &part->D1);
687 _adg_part_double_to_ui(part, &part->D2);
688 _adg_part_double_to_ui(part, &part->LD2);
689 _adg_part_double_to_ui(part, &part->D3);
690 _adg_part_double_to_ui(part, &part->LD3);
691 _adg_part_double_to_ui(part, &part->D4);
692 //_adg_part_double_to_ui(part, &part->D5);
693 _adg_part_double_to_ui(part, &part->D6);
694 //_adg_part_double_to_ui(part, &part->LD6);
695 _adg_part_double_to_ui(part, &part->D7);
696 //_adg_part_double_to_ui(part, &part->LD7);
698 _adg_part_boolean_to_ui(part, &part->GROOVE);
699 _adg_part_double_to_ui(part, &part->ZGROOVE);
700 _adg_part_double_to_ui(part, &part->DGROOVE);
701 _adg_part_double_to_ui(part, &part->LGROOVE);
703 _adg_part_lock(part);
706 static void
707 _adg_do_save_as(GtkWindow *window, GtkResponseType response, AdgCanvas *canvas)
709 GtkRadioButton *type_radio;
710 gchar *file, *suffix;
711 cairo_surface_t *surface;
713 if (response != GTK_RESPONSE_OK) {
714 gtk_widget_hide(GTK_WIDGET(window));
715 return;
718 file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(window));
719 if (file == NULL)
720 return;
722 type_radio = _adg_group_get_active(g_object_get_data(G_OBJECT(window),
723 "type-group"));
724 g_assert(GTK_IS_RADIO_BUTTON(type_radio));
725 suffix = gtk_widget_get_tooltip_markup(GTK_WIDGET(type_radio));
726 g_assert(suffix != NULL);
728 if (!g_str_has_suffix(file, suffix)) {
729 gchar *tmp = file;
730 file = g_strconcat(file, suffix, NULL);
731 g_free(tmp);
734 #ifdef CAIRO_HAS_PNG_FUNCTIONS
735 if (strcmp(suffix, ".png") == 0) {
736 surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, 800, 600);
737 } else
738 #endif
739 #ifdef CAIRO_HAS_SVG_SURFACE
740 if (strcmp(suffix, ".svg") == 0) {
741 #include <cairo-svg.h>
742 surface = cairo_svg_surface_create(file, 841, 595);
743 } else
744 #endif
745 #ifdef CAIRO_HAS_PDF_SURFACE
746 if (strcmp(suffix, ".pdf") == 0) {
747 #include <cairo-pdf.h>
748 surface = cairo_pdf_surface_create(file, 841, 595);
749 } else
750 #endif
751 #ifdef CAIRO_HAS_PS_SURFACE
752 if (strcmp(suffix, ".ps") == 0) {
753 #include <cairo-ps.h>
754 surface = cairo_ps_surface_create(file, 841, 595);
755 cairo_ps_surface_dsc_comment(surface, "%%Title: " PACKAGE_STRING);
756 cairo_ps_surface_dsc_comment(surface, "%%Copyright: Copyleft (C) 2006-2010 Fontana Nicola");
757 cairo_ps_surface_dsc_comment(surface, "%%Orientation: Landscape");
758 cairo_ps_surface_dsc_begin_setup(surface);
759 cairo_ps_surface_dsc_begin_page_setup(surface);
760 cairo_ps_surface_dsc_comment(surface, "%%IncludeFeature: *PageSize A4");
761 } else
762 #endif
764 _adg_error(_("Requested format not supported"), window);
765 surface = NULL;
768 if (surface) {
769 cairo_t *cr;
770 cairo_status_t status;
772 cr = cairo_create(surface);
773 cairo_surface_destroy(surface);
774 adg_entity_render(ADG_ENTITY(canvas), cr);
776 if (cairo_surface_get_type(surface) == CAIRO_SURFACE_TYPE_IMAGE) {
777 status = cairo_surface_write_to_png(surface, file);
778 } else {
779 cairo_show_page(cr);
780 status = cairo_status(cr);
783 cairo_destroy(cr);
785 if (status != CAIRO_STATUS_SUCCESS)
786 _adg_error(cairo_status_to_string(status), window);
789 g_free(file);
790 g_free(suffix);
791 gtk_widget_hide(GTK_WIDGET(window));
794 static void
795 _adg_print_prepare(GtkPrintOperation *operation, GtkPrintContext *context)
797 gtk_print_operation_set_n_pages(operation, 1);
800 static void
801 _adg_print_page(GtkPrintOperation *operation, GtkPrintContext *context,
802 gint page_nr, AdgEntity *canvas)
804 cairo_t *cr = gtk_print_context_get_cairo_context(context);
805 AdgMatrix old_map;
807 adg_matrix_copy(&old_map, adg_entity_get_global_map(canvas));;
809 adg_entity_set_global_map(canvas, adg_matrix_identity());
810 adg_entity_render(canvas, cr);
812 adg_entity_set_global_map(canvas, &old_map);
815 static void
816 _adg_do_print(GtkWidget *button, AdgCanvas *canvas)
818 static GtkPrintSettings *settings = NULL;
819 GtkWindow *window;
820 GtkPrintOperation *operation;
821 GtkPageSetup *page_setup;
822 GError *error;
824 window = (GtkWindow *) gtk_widget_get_toplevel(button);
825 operation = gtk_print_operation_new();
826 page_setup = g_object_get_data(G_OBJECT(canvas), "_adg_page_setup");
827 error = NULL;
829 if (settings)
830 gtk_print_operation_set_print_settings(operation, settings);
831 if (page_setup)
832 gtk_print_operation_set_default_page_setup(operation, page_setup);
834 g_signal_connect(operation, "begin-print",
835 G_CALLBACK(_adg_print_prepare), NULL);
836 g_signal_connect(operation, "draw-page",
837 G_CALLBACK(_adg_print_page), canvas);
839 gtk_print_operation_set_use_full_page(operation, FALSE);
840 gtk_print_operation_set_unit(operation, GTK_UNIT_POINTS);
841 #if GTK_CHECK_VERSION(2, 18, 0)
842 gtk_print_operation_set_embed_page_setup(operation, TRUE);
843 #endif
845 switch (gtk_print_operation_run(operation,
846 GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
847 window, &error)) {
849 case GTK_PRINT_OPERATION_RESULT_APPLY:
850 if (settings)
851 g_object_unref(settings);
852 settings = gtk_print_operation_get_print_settings(operation);
853 if (settings)
854 g_object_ref(settings);
855 break;
857 default:
858 break;
861 g_object_unref(operation);
863 if (error)
864 _adg_error(error->message, window);
867 static AdgPart *
868 _adg_part_new(GtkBuilder *builder)
870 AdgPart *part;
871 GObject *object, *toggle_object;
873 part = g_new0(AdgPart, 1);
874 part->widgets = g_hash_table_new_full(g_direct_hash, g_direct_equal,
875 NULL, g_object_unref);
876 part->area = (AdgGtkArea *) gtk_builder_get_object(builder, "mainCanvas");
877 part->apply = (GtkButton *) gtk_builder_get_object(builder, "editApply");
878 part->reset = (GtkButton *) gtk_builder_get_object(builder, "editReset");
879 part->shape = adg_path_new();
880 part->hatch = adg_path_new();
881 part->hatch_edge = adg_path_new();
882 part->edges = adg_edges_new_with_source(ADG_TRAIL(part->shape));
884 g_assert(ADG_GTK_IS_AREA(part->area));
885 g_assert(GTK_IS_BUTTON(part->apply));
886 g_assert(GTK_IS_BUTTON(part->reset));
888 _adg_part_link(part, &part->A, gtk_builder_get_object(builder, "editA"));
889 _adg_part_link(part, &part->B, gtk_builder_get_object(builder, "editB"));
890 _adg_part_link(part, &part->C, gtk_builder_get_object(builder, "editC"));
891 _adg_part_link(part, &part->D1, gtk_builder_get_object(builder, "editD1"));
892 _adg_part_link(part, &part->D2, gtk_builder_get_object(builder, "editD2"));
893 _adg_part_link(part, &part->LD2, gtk_builder_get_object(builder, "editLD2"));
894 _adg_part_link(part, &part->D3, gtk_builder_get_object(builder, "editD3"));
895 _adg_part_link(part, &part->LD3, gtk_builder_get_object(builder, "editLD3"));
896 _adg_part_link(part, &part->D4, gtk_builder_get_object(builder, "editD4"));
897 //_adg_part_link(part, &part->D5, gtk_builder_get_object(builder, "editD5"));
898 _adg_part_link(part, &part->D6, gtk_builder_get_object(builder, "editD6"));
899 //_adg_part_link(part, &part->LD6, gtk_builder_get_object(builder, "editLD6"));
900 _adg_part_link(part, &part->D7, gtk_builder_get_object(builder, "editD7"));
901 //_adg_part_link(part, &part->LD7, gtk_builder_get_object(builder, "editLD7"));
902 _adg_part_link(part, &part->DHOLE, gtk_builder_get_object(builder, "editDHOLE"));
903 _adg_part_link(part, &part->LHOLE, gtk_builder_get_object(builder, "editLHOLE"));
905 toggle_object = gtk_builder_get_object(builder, "GROOVE");
906 _adg_part_link(part, &part->GROOVE, toggle_object);
908 object = gtk_builder_get_object(builder, "editZGROOVE");
909 _adg_part_link(part, &part->ZGROOVE, object);
910 g_signal_connect(toggle_object, "toggled",
911 G_CALLBACK(adg_gtk_toggle_button_sensitivize), object);
912 object = gtk_builder_get_object(builder, "editZGROOVELabel");
913 g_signal_connect(toggle_object, "toggled",
914 G_CALLBACK(adg_gtk_toggle_button_sensitivize), object);
916 object = gtk_builder_get_object(builder, "editDGROOVE");
917 _adg_part_link(part, &part->DGROOVE, object);
918 g_signal_connect(toggle_object, "toggled",
919 G_CALLBACK(adg_gtk_toggle_button_sensitivize), object);
920 object = gtk_builder_get_object(builder, "editDGROOVELabel");
921 g_signal_connect(toggle_object, "toggled",
922 G_CALLBACK(adg_gtk_toggle_button_sensitivize), object);
924 object = gtk_builder_get_object(builder, "editLGROOVE");
925 _adg_part_link(part, &part->LGROOVE, object);
926 g_signal_connect(toggle_object, "toggled",
927 G_CALLBACK(adg_gtk_toggle_button_sensitivize), object);
928 object = gtk_builder_get_object(builder, "editLGROOVELabel");
929 g_signal_connect(toggle_object, "toggled",
930 G_CALLBACK(adg_gtk_toggle_button_sensitivize), object);
932 part->D5 = 4.5;
933 part->RD34 = 1;
934 part->LD5 = 5;
935 part->LD6 = 1;
936 part->LD7 = 0.5;
938 _adg_do_edit(part);
940 return part;
943 static void
944 _adg_part_destroy(AdgPart *part)
946 g_hash_table_destroy(part->widgets);
947 g_free(part);
950 static GtkWidget *
951 _adg_about_window(GtkBuilder *builder)
953 GtkWidget *window;
955 window = (GtkWidget *) gtk_builder_get_object(builder, "wndAbout");
956 g_assert(GTK_IS_ABOUT_DIALOG(window));
958 g_signal_connect(window, "response", G_CALLBACK(gtk_widget_hide), NULL);
960 return window;
963 static GtkWidget *
964 _adg_edit_window(GtkBuilder *builder, AdgPart *part)
966 GtkWidget *window;
968 window = (GtkWidget *) gtk_builder_get_object(builder, "wndEdit");
969 g_assert(GTK_IS_DIALOG(window));
971 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_MOUSE);
973 g_signal_connect_swapped(part->apply, "clicked",
974 G_CALLBACK(_adg_do_edit), part);
975 g_signal_connect_swapped(part->reset, "clicked",
976 G_CALLBACK(_adg_do_reset), part);
977 g_signal_connect(window, "response",
978 G_CALLBACK(adg_gtk_window_hide_here), NULL);
980 return window;
983 static GtkWidget *
984 _adg_save_as_window(GtkBuilder *builder, AdgCanvas *canvas)
986 GtkWidget *window;
987 GtkWidget *type_group;
989 window = (GtkWidget *) gtk_builder_get_object(builder, "wndSaveAs");
990 g_assert(GTK_IS_WINDOW(window));
991 type_group = (GtkWidget *) gtk_builder_get_object(builder, "saveAsPng");
992 g_assert(GTK_IS_RADIO_BUTTON(type_group));
994 g_object_set_data(G_OBJECT(window), "type-group", type_group);
996 /* Set the default destination file */
997 if (GTK_IS_FILE_CHOOSER(window)) {
998 GtkFileChooser *file_chooser;
999 const gchar *dir;
1001 file_chooser = (GtkFileChooser *) window;
1003 #if GLIB_CHECK_VERSION(2, 14, 0)
1004 dir = g_get_user_special_dir(G_USER_DIRECTORY_DOCUMENTS);
1005 #else
1006 dir = NULL;
1007 #endif
1009 if (dir == NULL)
1010 dir = g_get_home_dir();
1012 gtk_file_chooser_set_current_folder(file_chooser, dir);
1013 gtk_file_chooser_set_current_name(file_chooser, "adg-demo");
1016 g_signal_connect(window, "response", G_CALLBACK(_adg_do_save_as), canvas);
1018 return window;
1021 static GtkWidget *
1022 _adg_main_window(GtkBuilder *builder)
1024 GtkWidget *window;
1025 AdgPart *part;
1026 AdgCanvas *canvas;
1027 GtkWidget *button_edit, *button_save_as, *button_print;
1028 GtkWidget *button_about, *button_quit;
1030 window = (GtkWidget *) gtk_builder_get_object(builder, "wndMain");
1031 part = _adg_part_new(builder);
1032 canvas = adg_canvas_new();
1034 _adg_canvas_init(canvas, part);
1035 adg_gtk_area_set_canvas(part->area, canvas);
1037 g_assert(GTK_IS_WINDOW(window));
1038 button_edit = (GtkWidget *) gtk_builder_get_object(builder, "mainEdit");
1039 g_assert(GTK_IS_BUTTON(button_edit));
1040 button_save_as = (GtkWidget *) gtk_builder_get_object(builder, "mainSaveAs");
1041 g_assert(GTK_IS_BUTTON(button_save_as));
1042 button_print = (GtkWidget *) gtk_builder_get_object(builder, "mainPrint");
1043 g_assert(GTK_IS_BUTTON(button_print));
1044 button_about = (GtkWidget *) gtk_builder_get_object(builder, "mainAbout");
1045 g_assert(GTK_IS_BUTTON(button_about));
1046 button_quit = (GtkWidget *) gtk_builder_get_object(builder, "mainQuit");
1047 g_assert(GTK_IS_BUTTON(button_quit));
1049 g_signal_connect_swapped(button_about, "clicked",
1050 G_CALLBACK(gtk_dialog_run),
1051 _adg_about_window(builder));
1052 g_signal_connect_swapped(button_edit, "clicked",
1053 G_CALLBACK(gtk_dialog_run),
1054 _adg_edit_window(builder, part));
1055 g_signal_connect_swapped(button_save_as, "clicked",
1056 G_CALLBACK(gtk_dialog_run),
1057 _adg_save_as_window(builder, canvas));
1058 g_signal_connect(button_print, "clicked",
1059 G_CALLBACK(_adg_do_print), canvas);
1060 g_signal_connect(button_quit, "clicked", G_CALLBACK(gtk_main_quit), NULL);
1061 g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL);
1062 g_signal_connect_swapped(window, "destroy",
1063 G_CALLBACK(_adg_part_destroy), part);
1065 return window;
1070 main(gint argc, gchar **argv)
1072 gboolean show_extents;
1073 gchar *path;
1074 GtkBuilder *builder;
1075 GError *error;
1076 GtkWidget *main_window;
1078 _adg_parse_args(&argc, &argv, &show_extents);
1079 adg_switch_extents(show_extents);
1081 path = demo_find_data_file("adg-demo.ui", argv[0]);
1082 if (path == NULL) {
1083 g_print(_("adg-demo.ui not found!\n"));
1084 return 1;
1087 builder = gtk_builder_new();
1088 error = NULL;
1090 gtk_builder_set_translation_domain(builder, GETTEXT_PACKAGE);
1091 gtk_builder_add_from_file(builder, path, &error);
1093 if (error != NULL) {
1094 g_critical("%s", error->message);
1095 g_error_free(error);
1096 return 2;
1099 main_window = _adg_main_window(builder);
1100 g_object_unref(builder);
1102 gtk_widget_show_all(main_window);
1103 gtk_main();
1105 return 0;