/
sheet.c
1510 lines (1239 loc) · 41.9 KB
/
sheet.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* sheet.c
*
*
* Authors:
* Richard Hult <rhult@hem.passagen.se>
* Ricardo Markiewicz <rmarkie@fi.uba.ar>
* Andres de Barbara <adebarbara@fi.uba.ar>
* Marc Lorber <lorber.marc@wanadoo.fr>
* Bernhard Schuster <bernhard@ahoi.io>
* Guido Trentalancia <guido@trentalancia.com>
*
* Web page: https://ahoi.io/project/oregano
*
* Copyright (C) 1999-2001 Richard Hult
* Copyright (C) 2003,2006 Ricardo Markiewicz
* Copyright (C) 2009-2012 Marc Lorber
* Copyright (C) 2013-2019 Bernhard Schuster
* Copyright (C) 2017 Guido Trentalancia
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <gdk/gdk.h>
#include <gdk/gdkkeysyms.h>
#include <math.h>
#include <goocanvas.h>
#include <goocanvasutils.h>
#include "sheet-private.h"
#include "sheet-item.h"
#include "node-store.h"
#include "node-item.h"
#include "wire-item.h"
#include "part-item.h"
#include "grid.h"
#include "sheet-item-factory.h"
#include "schematic-view.h"
#include "options.h"
#include "rubberband.h"
#include "create-wire.h"
static void sheet_class_init (SheetClass *klass);
static void sheet_init (Sheet *sheet);
static void sheet_set_property (GObject *object, guint prop_id, const GValue *value,
GParamSpec *spec);
static void sheet_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *spec);
static void sheet_set_zoom (Sheet *sheet, const double zoom);
static GList *sheet_preserve_selection (Sheet *sheet);
static void rotate_items (Sheet *sheet, GList *items, gint angle);
static void move_items (Sheet *sheet, GList *items, const Coords *delta);
static void flip_items (Sheet *sheet, GList *items, IDFlip direction);
static void node_dot_added_callback (Schematic *schematic, Coords *pos, Sheet *sheet);
static void node_dot_removed_callback (Schematic *schematic, Coords *pos, Sheet *sheet);
static void sheet_finalize (GObject *object);
static int dot_equal (gconstpointer a, gconstpointer b);
static guint dot_hash (gconstpointer key);
#define ZOOM_MIN 0.35
#define ZOOM_MAX 3
#include "debug.h"
enum { SELECTION_CHANGED, BUTTON_PRESS, CONTEXT_CLICK, CANCEL, LAST_SIGNAL };
static guint signals[LAST_SIGNAL] = {0};
enum { ARG_0, ARG_ZOOM };
G_DEFINE_TYPE (Sheet, sheet, GOO_TYPE_CANVAS)
static void sheet_class_init (SheetClass *sheet_class)
{
GObjectClass *object_class;
object_class = G_OBJECT_CLASS (sheet_class);
object_class->set_property = sheet_set_property;
object_class->get_property = sheet_get_property;
object_class->finalize = sheet_finalize;
sheet_parent_class = g_type_class_peek (GOO_TYPE_CANVAS);
g_object_class_install_property (object_class, ARG_ZOOM,
g_param_spec_double ("zoom", "Sheet::zoom", "the zoom factor",
0.01f, 10.0f, 1.0f, G_PARAM_READWRITE));
// Signals.
signals[SELECTION_CHANGED] =
g_signal_new ("selection_changed", G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (SheetClass, selection_changed), NULL, NULL,
g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
signals[BUTTON_PRESS] =
g_signal_new ("button_press", G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (SheetClass, button_press), NULL, NULL,
g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, GDK_TYPE_EVENT);
signals[CONTEXT_CLICK] = g_signal_new (
"context_click", G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (SheetClass, context_click), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_POINTER);
signals[CANCEL] =
g_signal_new ("cancel", G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (SheetClass, cancel), NULL, NULL,
g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}
static void sheet_init (Sheet *sheet)
{
sheet->priv = g_new0 (SheetPriv, 1);
sheet->priv->zoom = 1.0;
sheet->priv->selected_group = NULL;
sheet->priv->floating_group = NULL;
sheet->priv->floating_objects = NULL;
sheet->priv->selected_objects = NULL;
sheet->priv->wire_handler_id = 0;
sheet->priv->float_handler_id = 0;
sheet->priv->items = NULL;
sheet->priv->rubberband_info = NULL;
sheet->priv->create_wire_info = NULL;
sheet->priv->preserve_selection_items = NULL;
sheet->priv->sheet_parent_class = g_type_class_ref (GOO_TYPE_CANVAS);
sheet->priv->voltmeter_nodes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
sheet->state = SHEET_STATE_NONE;
// track these to cancel wires if the cursor leaves the window
gtk_widget_add_events(GTK_WIDGET(sheet), GDK_LEAVE_NOTIFY | GDK_ENTER_NOTIFY);
}
static void sheet_finalize (GObject *object)
{
Sheet *sheet = SHEET (object);
if (sheet->priv) {
g_list_free (sheet->priv->selected_objects);
g_list_free (sheet->priv->floating_objects);
g_list_free (sheet->priv->items);
g_list_free (sheet->priv->preserve_selection_items);
if (sheet->priv->voltmeter_nodes)
g_hash_table_destroy (sheet->priv->voltmeter_nodes);
if (sheet->priv->node_dots)
g_hash_table_destroy (sheet->priv->node_dots);
g_free (sheet->priv->rubberband_info);
g_free (sheet->priv->create_wire_info);
g_free (sheet->priv);
}
if (sheet->grid)
g_object_unref (G_OBJECT (sheet->grid));
if (G_OBJECT_CLASS (sheet_parent_class)->finalize)
(*G_OBJECT_CLASS (sheet_parent_class)->finalize)(object);
}
/**
* position within the sheet in pixel coordinates
*
* coordinates are clamped to grid if grid is enabled
* see snap_to_grid
* zero point : top left corner of the window (not widget!)
*
* @param x horizontal, left to right
* @param y vertical, top to bottom
* @returns wether the position could be detected properly
*
* @attention never call in event handlers!
*/
gboolean sheet_get_pointer_pixel (Sheet *sheet, gdouble *x, gdouble *y)
{
GtkAdjustment *hadj = NULL, *vadj = NULL;
gdouble x1, y1;
gint _x, _y;
GdkDevice *device_pointer;
GdkRectangle allocation;
GdkDisplay *display;
GdkWindow *window;
#if GTK_CHECK_VERSION (3,20,0)
GdkSeat *seat;
#else
GdkDeviceManager *device_manager;
#endif
// deprecated gtk_widget_get_pointer (GTK_WIDGET (sheet), &_x, &_y);
// replaced by a code copied from evince
if (G_UNLIKELY (!sheet || !gtk_widget_get_realized (GTK_WIDGET (sheet)))) {
NG_DEBUG ("Widget is not realized.");
return FALSE;
}
display = gtk_widget_get_display (GTK_WIDGET (sheet));
#if GTK_CHECK_VERSION (3,20,0)
seat = gdk_display_get_default_seat (display);
device_pointer = gdk_seat_get_pointer (seat);
#else
device_manager = gdk_display_get_device_manager (display);
// gdk_device_manager_get_client_pointer
// shall not be used within events
device_pointer = gdk_device_manager_get_client_pointer (device_manager);
#endif
window = gtk_widget_get_window (GTK_WIDGET (sheet));
if (!window) {
NG_DEBUG ("Window is not realized.");
return FALSE;
}
// even though above is all defined the below will always return NUL for
// unknown reason and _x and _y are populated as expected
gdk_window_get_device_position (window, device_pointer, &_x, &_y, NULL);
#if 0
if (!window) { //fails always
NG_DEBUG ("Window does not seem to be realized yet?");
return FALSE;
}
#else
#if GTK_CHECK_VERSION (3,20,0)
NG_DEBUG ("\n%p %p %p %p %i %i\n\n", display, seat, device_pointer, window, _x, _y);
#else
NG_DEBUG ("\n%p %p %p %p %i %i\n\n", display, device_manager, device_pointer, window, _x, _y);
#endif
#endif
gtk_widget_get_allocation (GTK_WIDGET (sheet), &allocation);
_x -= allocation.x;
_y -= allocation.y;
x1 = (gdouble)_x;
y1 = (gdouble)_y;
if (!sheet_get_adjustments (sheet, &hadj, &vadj)) {
return FALSE;
}
x1 += gtk_adjustment_get_value (hadj);
y1 += gtk_adjustment_get_value (vadj);
*x = x1;
*y = y1;
return TRUE;
}
/**
* get the pointer position in goocanvas coordinates
*
* @attention shall not be called in event callbacks,
* except for GDK_MOTION_... where it is useless since
* the event itself contains the cursor position
*/
gboolean sheet_get_pointer (Sheet *sheet, gdouble *x, gdouble *y)
{
if (!sheet_get_pointer_pixel (sheet, x, y)) {
return FALSE;
}
goo_canvas_convert_from_pixels (GOO_CANVAS (sheet), x, y);
return TRUE;
}
gboolean sheet_get_pointer_snapped (Sheet *sheet, gdouble *x, gdouble *y)
{
if (!sheet_get_pointer_pixel (sheet, x, y)) {
return FALSE;
}
goo_canvas_convert_from_pixels (GOO_CANVAS (sheet), x, y);
snap_to_grid (sheet->grid, x, y);
return TRUE;
}
void sheet_get_zoom (const Sheet *sheet, gdouble *zoom)
{
g_return_if_fail (sheet);
g_return_if_fail (IS_SHEET (sheet));
*zoom = sheet->priv->zoom;
}
static void sheet_set_zoom (Sheet *sheet, const double zoom)
{
g_return_if_fail (sheet);
g_return_if_fail (IS_SHEET (sheet));
sheet->priv->zoom = zoom;
}
/*
* \brief gets the sheets parent adjustments
*
* @returns TRUE on success
*/
gboolean sheet_get_adjustments (const Sheet *sheet, GtkAdjustment **hadj, GtkAdjustment **vadj)
{
GtkWidget *parent;
GtkScrolledWindow *scrolled;
if (G_UNLIKELY (!sheet))
return FALSE;
if (G_UNLIKELY (!vadj || !hadj))
return FALSE;
parent = gtk_widget_get_parent (GTK_WIDGET (sheet));
if (G_UNLIKELY (!parent || !GTK_IS_SCROLLED_WINDOW (parent)))
return FALSE;
scrolled = GTK_SCROLLED_WINDOW (parent);
*hadj = gtk_scrolled_window_get_hadjustment (scrolled);
if (G_UNLIKELY (!*hadj || !GTK_IS_ADJUSTMENT (*hadj)))
return FALSE;
*vadj = gtk_scrolled_window_get_vadjustment (scrolled);
if (G_UNLIKELY (!*vadj || !GTK_IS_ADJUSTMENT (*vadj)))
return FALSE;
return TRUE;
}
/**
* \brief change the zoom by factor (zoom step)
*
* zoom origin when zooming in is the cursor
* zoom origin when zooming out is the center of the current viewport
*
* @param sheet
* @param factor values should be in the range of [0.5 .. 2]
*/
void sheet_zoom_step (Sheet *sheet, const gdouble factor)
{
double zoom;
g_return_if_fail (sheet);
g_return_if_fail (IS_SHEET (sheet));
sheet_get_zoom (sheet, &zoom);
sheet_set_zoom (sheet, zoom * factor);
Coords adju, r, pointer, delta, center, pagesize;
GtkAdjustment *hadj = NULL, *vadj = NULL;
GooCanvas *canvas;
gboolean b = FALSE;
canvas = GOO_CANVAS (sheet);
// if we scroll out, just use the center as focus
// mouse curser centered scroll out "feels" awkward
if (factor < 1.) {
goo_canvas_set_scale (canvas, factor * goo_canvas_get_scale (canvas));
return;
}
// get pointer position in pixels
// just skip the correction if we can not get the pointer
if (!sheet_get_pointer_pixel (sheet, &pointer.x, &pointer.y)) {
goo_canvas_set_scale (canvas, factor * goo_canvas_get_scale (canvas));
g_warning ("Failed to get cursor position.");
return;
}
// top left corner in pixels
b = sheet_get_adjustments (sheet, &hadj, &vadj);
if (b) {
adju.x = gtk_adjustment_get_value (hadj);
adju.y = gtk_adjustment_get_value (vadj);
// get the page size in pixels
pagesize.x = gtk_adjustment_get_page_size (hadj);
pagesize.y = gtk_adjustment_get_page_size (vadj);
} else {
// FIXME untested codepath, check for variable space conversion
// FIXME Pixel vs GooUnits
gdouble left, right, top, bottom;
goo_canvas_get_bounds (canvas, &left, &top, &right, &bottom);
pagesize.x = bottom - top;
pagesize.y = right - left;
adju.x = adju.y = 0.;
}
// calculate the center of the widget in pixels
center.x = adju.x + pagesize.x / 2.;
center.y = adju.y + pagesize.y / 2.;
// calculate the delta between the center and the pointer in pixels
// this is required as the center is the zoom target
delta.x = pointer.x - center.x;
delta.y = pointer.y - center.y;
// increase the top left position in pixels by our calculated delta
adju.x += delta.x;
adju.y += delta.y;
// convert to canvas coords
goo_canvas_convert_from_pixels (canvas, &adju.x, &adju.y);
// the center of the canvas is now our cursor position
goo_canvas_scroll_to (canvas, adju.x, adju.y);
// calculate a correction term
// for the case that we can not scroll the pane far enough to
// compensate the whole off-due-to-wrong-center-error
if (b) {
r.x = gtk_adjustment_get_value (hadj);
r.y = gtk_adjustment_get_value (vadj);
goo_canvas_convert_from_pixels (canvas, &r.x, &r.y);
// the correction term in goo coordinates, to be subtracted from the
// backscroll distance
r.x -= adju.x;
r.y -= adju.y;
} else {
r.x = r.y = 0.;
}
// no the center is our cursor position and we can safely call scale
goo_canvas_set_scale (canvas, factor * goo_canvas_get_scale (canvas));
// top left corner in pixels after scaling
if (b) {
adju.x = gtk_adjustment_get_value (hadj);
adju.y = gtk_adjustment_get_value (vadj);
} else {
adju.x = adju.y = 0.;
}
// gtk_adjustment_get_page_size is constant before and after scale
adju.x -= (delta.x) / sheet->priv->zoom;
adju.y -= (delta.y) / sheet->priv->zoom;
goo_canvas_convert_from_pixels (canvas, &adju.x, &adju.y);
goo_canvas_scroll_to (canvas, adju.x - r.x, adju.y - r.y);
gtk_widget_queue_draw (GTK_WIDGET (canvas));
}
/**
* \brief defines the drawing widget on which the actual schematic will be drawn
*
* @param width width of the content area
* @param height height of the content area
*/
GtkWidget *sheet_new (const gdouble width, const gdouble height)
{
GooCanvas *sheet_canvas;
GooCanvasGroup *sheet_group;
GooCanvasPoints *points;
Sheet *sheet;
GtkWidget *sheet_widget;
GooCanvasItem *root;
// Creation of the Canvas
sheet = SHEET (g_object_new (TYPE_SHEET, NULL));
sheet_canvas = GOO_CANVAS (sheet);
g_object_set (G_OBJECT (sheet_canvas), "bounds-from-origin", FALSE, "bounds-padding", 4.0,
"background-color-rgb", 0xFFFFFF, NULL);
root = goo_canvas_get_root_item (sheet_canvas);
sheet_group = GOO_CANVAS_GROUP (goo_canvas_group_new (root, NULL));
sheet_widget = GTK_WIDGET (sheet);
goo_canvas_set_bounds (GOO_CANVAS (sheet_canvas), 0., 0., width + 20., height + 20.);
sheet->priv->width = width;
sheet->priv->height = height;
// Create the dot grid.
sheet->grid = grid_new (GOO_CANVAS_ITEM (sheet_group), width, height);
// Everything outside the sheet should be gray.
// top //
goo_canvas_rect_new (GOO_CANVAS_ITEM (sheet_group), 0.0, 0.0, width + 20.0, 20.0, "fill_color",
"gray", "line-width", 0.0, NULL);
goo_canvas_rect_new (GOO_CANVAS_ITEM (sheet_group), 0.0, height, width + 20.0, height + 20.0,
"fill_color", "gray", "line-width", 0.0, NULL);
// right //
goo_canvas_rect_new (GOO_CANVAS_ITEM (sheet_group), 0.0, 0.0, 20.0, height + 20.0, "fill_color",
"gray", "line-width", 0.0, NULL);
goo_canvas_rect_new (GOO_CANVAS_ITEM (sheet_group), width, 0.0, width + 20.0, height + 20.0,
"fill_color", "gray", "line-width", 0.0, NULL);
if (oregano_options_debug_directions ()) {
goo_canvas_polyline_new_line (GOO_CANVAS_ITEM (sheet_group), 10.0, 10.0, 50.0, 10.0,
"stroke-color", "green", "line-width", 2.0, "end-arrow", TRUE,
NULL);
goo_canvas_text_new (GOO_CANVAS_ITEM (sheet_group), "x", 90.0, 10.0, -1.0,
GOO_CANVAS_ANCHOR_WEST, "fill-color", "green", NULL);
goo_canvas_polyline_new_line (GOO_CANVAS_ITEM (sheet_group), 10.0, 10.0, 10.0, 50.0,
"stroke-color", "red", "line-width", 2.0, "end-arrow", TRUE,
NULL);
goo_canvas_text_new (GOO_CANVAS_ITEM (sheet_group), "y", 10.0, 90.0, -1.0,
GOO_CANVAS_ANCHOR_CENTER, "fill-color", "red", NULL);
}
// Draw a thin black border around the sheet.
points = goo_canvas_points_new (5);
points->coords[0] = 20.0;
points->coords[1] = 20.0;
points->coords[2] = width;
points->coords[3] = 20.0;
points->coords[4] = width;
points->coords[5] = height;
points->coords[6] = 20.0;
points->coords[7] = height;
points->coords[8] = 20.0;
points->coords[9] = 20.0;
goo_canvas_polyline_new (GOO_CANVAS_ITEM (sheet_group), FALSE, 0, "line-width", 1.0, "points",
points, NULL);
goo_canvas_points_unref (points);
// Finally, create the object group that holds all objects.
sheet->object_group = GOO_CANVAS_GROUP (goo_canvas_group_new (root, "x", 0.0, "y", 0.0, NULL));
NG_DEBUG ("root group %p", sheet->object_group);
sheet->priv->selected_group = GOO_CANVAS_GROUP (
goo_canvas_group_new (GOO_CANVAS_ITEM (sheet->object_group), "x", 0.0, "y", 0.0, NULL));
NG_DEBUG ("selected group %p", sheet->priv->selected_group);
sheet->priv->floating_group = GOO_CANVAS_GROUP (
goo_canvas_group_new (GOO_CANVAS_ITEM (sheet->object_group), "x", 0.0, "y", 0.0, NULL));
NG_DEBUG ("floating group %p", sheet->priv->floating_group);
// Hash table that maps coordinates to a specific dot.
sheet->priv->node_dots = g_hash_table_new_full (dot_hash, dot_equal, g_free, NULL);
// this requires object_group to be setup properly
sheet->priv->rubberband_info = rubberband_info_new (sheet);
sheet->priv->create_wire_info = create_wire_info_new (sheet);
return sheet_widget;
}
/*
* Replace the current sheet with a new one (eventually
* with a different size, i.e. with a different width
* and/or height).
*/
gboolean sheet_replace (SchematicView *sv)
{
g_return_val_if_fail (sv != NULL, FALSE);
g_return_val_if_fail (IS_SCHEMATIC_VIEW (sv), FALSE);
Schematic *sm = schematic_view_get_schematic (sv);
Sheet *old_sheet = schematic_view_get_sheet (sv);
Sheet *sheet;
GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (old_sheet));
gdouble zoom;
g_return_val_if_fail (old_sheet != NULL, FALSE);
g_return_val_if_fail (IS_SHEET (old_sheet), FALSE);
sheet_get_zoom (old_sheet, &zoom);
sheet = SHEET (sheet_new ((double) schematic_get_width (sm) + SHEET_BORDER, (double) schematic_get_height (sm) + SHEET_BORDER));
if (!sheet)
return FALSE;
sheet_set_zoom (sheet, zoom);
goo_canvas_set_scale (GOO_CANVAS (sheet), zoom);
g_signal_connect (G_OBJECT (sheet), "event", G_CALLBACK (sheet_event_callback),
sheet);
schematic_view_set_sheet (sv, sheet);
rubberband_info_destroy (old_sheet->priv->rubberband_info);
old_sheet->priv->rubberband_info = NULL;
create_wire_info_destroy (old_sheet->priv->create_wire_info);
old_sheet->priv->create_wire_info = NULL;
gtk_widget_destroy (GTK_WIDGET (old_sheet));
gtk_container_add (GTK_CONTAINER (parent), GTK_WIDGET (sheet));
gtk_widget_grab_focus (GTK_WIDGET (sheet));
return TRUE;
}
static void sheet_set_property (GObject *object, guint prop_id, const GValue *value,
GParamSpec *spec)
{
Sheet *sheet = SHEET (object);
switch (prop_id) {
case ARG_ZOOM:
sheet_set_zoom (sheet, (const double) g_value_get_double (value));
break;
}
}
static void sheet_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *spec)
{
const Sheet *sheet = SHEET (object);
switch (prop_id) {
case ARG_ZOOM:
g_value_set_double (value, sheet->priv->zoom);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (sheet, prop_id, spec);
break;
}
}
/*
* scroll to <dx,dy> in pixels relative to the current coords
* note that pixels are _not_ affected by zoom
*/
void sheet_scroll_pixel (const Sheet *sheet, int delta_x, int delta_y)
{
GtkAdjustment *hadj = NULL, *vadj = NULL;
GtkAllocation allocation;
gfloat vnew, hnew;
gfloat hmax, vmax;
gfloat x1, y1;
const SheetPriv *priv = sheet->priv;
if (sheet_get_adjustments (sheet, &hadj, &vadj)) {
x1 = gtk_adjustment_get_value (hadj);
y1 = gtk_adjustment_get_value (vadj);
} else {
x1 = y1 = 0.f;
}
gtk_widget_get_allocation (GTK_WIDGET (sheet), &allocation);
if (priv->width > allocation.width)
hmax = (gfloat)(priv->width - allocation.width);
else
hmax = 0.f;
if (priv->height > allocation.height)
vmax = (gfloat)(priv->height - allocation.height);
else
vmax = 0.f;
hnew = CLAMP (x1 + (gfloat)delta_x, 0.f, hmax);
vnew = CLAMP (y1 + (gfloat)delta_y, 0.f, vmax);
if (hadj && hnew != x1) {
gtk_adjustment_set_value (hadj, hnew);
g_signal_emit_by_name (G_OBJECT (hadj), "value_changed");
}
if (vadj && vnew != y1) {
gtk_adjustment_set_value (vadj, vnew);
g_signal_emit_by_name (G_OBJECT (vadj), "value_changed");
}
}
void sheet_get_size_pixels (const Sheet *sheet, guint *width, guint *height)
{
*width = sheet->priv->width * sheet->priv->zoom;
*height = sheet->priv->height * sheet->priv->zoom;
}
void sheet_remove_selected_object (const Sheet *sheet, SheetItem *item)
{
sheet->priv->selected_objects = g_list_remove (sheet->priv->selected_objects, item);
}
void sheet_prepend_selected_object (Sheet *sheet, SheetItem *item)
{
sheet->priv->selected_objects = g_list_prepend (sheet->priv->selected_objects, item);
}
void sheet_remove_floating_object (const Sheet *sheet, SheetItem *item)
{
sheet->priv->floating_objects = g_list_remove (sheet->priv->floating_objects, item);
}
void sheet_prepend_floating_object (Sheet *sheet, SheetItem *item)
{
sheet->priv->floating_objects = g_list_prepend (sheet->priv->floating_objects, item);
}
void sheet_connect_part_item_to_floating_group (Sheet *sheet, gpointer *sv)
{
g_return_if_fail (sheet);
g_return_if_fail (IS_SHEET (sheet));
g_return_if_fail (sv);
g_return_if_fail (IS_SCHEMATIC_VIEW (sv));
sheet->state = SHEET_STATE_FLOAT_START;
if (sheet->priv->float_handler_id != 0)
return;
sheet->priv->float_handler_id =
g_signal_connect (G_OBJECT (sheet), "event", G_CALLBACK (sheet_item_floating_event), sv);
}
void sheet_show_node_labels (Sheet *sheet, gboolean show)
{
g_return_if_fail (sheet != NULL);
g_return_if_fail (IS_SHEET (sheet));
GList *item = NULL;
for (item = sheet->priv->items; item; item = item->next) {
if (IS_PART_ITEM (item->data)) {
if (part_get_num_pins (PART (sheet_item_get_data (SHEET_ITEM (item->data)))) == 1) {
part_item_show_node_labels (PART_ITEM (item->data), show);
}
}
}
}
void sheet_add_item (Sheet *sheet, SheetItem *item)
{
g_return_if_fail (sheet != NULL);
g_return_if_fail (IS_SHEET (sheet));
g_return_if_fail (item != NULL);
g_return_if_fail (IS_SHEET_ITEM (item));
sheet->priv->items = g_list_prepend (sheet->priv->items, item);
}
/**
* save the selection
* @attention not stackable
*/
GList *sheet_preserve_selection (Sheet *sheet)
{
g_return_val_if_fail (sheet != NULL, FALSE);
g_return_val_if_fail (IS_SHEET (sheet), FALSE);
GList *list = NULL;
for (list = sheet->priv->selected_objects; list; list = list->next) {
sheet_item_set_preserve_selection (SHEET_ITEM (list->data), TRUE);
}
// Return the list so that we can remove the preserve_selection
// flags later.
return sheet->priv->selected_objects;
}
int sheet_event_callback (GtkWidget *widget, GdkEvent *event, Sheet *sheet)
{
GtkWidgetClass *wklass = GTK_WIDGET_CLASS (sheet->priv->sheet_parent_class);
switch (event->type) {
case GDK_3BUTTON_PRESS:
// We do not care about triple clicks on the sheet.
return FALSE;
case GDK_2BUTTON_PRESS:
// The sheet does not care about double clicks, but invoke the
// canvas event handler and see if an item picks up the event.
return wklass->button_press_event (widget, (GdkEventButton *)event);
case GDK_BUTTON_PRESS:
// If we are in the middle of something else, don't interfere
// with that.
if (sheet->state != SHEET_STATE_NONE) {
return FALSE;
}
if (wklass->button_press_event (widget, (GdkEventButton *)event)) {
return TRUE;
}
if (event->button.button == 3) {
run_context_menu (schematic_view_get_schematicview_from_sheet (sheet),
(GdkEventButton *)event);
return TRUE;
}
if (event->button.button == 1) {
if (!(event->button.state & GDK_SHIFT_MASK)) {
sheet_select_all (sheet, FALSE);
}
rubberband_start (sheet, event);
return TRUE;
}
break;
case GDK_BUTTON_RELEASE:
if (event->button.button == 4 || event->button.button == 5) {
return TRUE;
}
if (event->button.button == 1 && sheet->priv->rubberband_info->state == RUBBERBAND_ACTIVE) {
rubberband_finish (sheet, event);
return TRUE;
}
if (wklass->button_release_event != NULL) {
return wklass->button_release_event (widget, (GdkEventButton *)event);
}
break;
case GDK_SCROLL: {
GdkEventScroll *scr_event = (GdkEventScroll *)event;
if (scr_event->state & GDK_SHIFT_MASK) {
// Scroll horizontally
if (scr_event->direction == GDK_SCROLL_UP) {
sheet_scroll_pixel (sheet, -30, 0);
}
else if (scr_event->direction == GDK_SCROLL_DOWN) {
sheet_scroll_pixel (sheet, 30, 0);
}
} else if (scr_event->state & GDK_CONTROL_MASK) {
// Scroll vertically
if (scr_event->direction == GDK_SCROLL_UP) {
sheet_scroll_pixel (sheet, 0, -30);
}
else if (scr_event->direction == GDK_SCROLL_DOWN) {
sheet_scroll_pixel (sheet, 0, 30);
}
} else {
// Zoom
if (scr_event->direction == GDK_SCROLL_UP) {
double zoom;
sheet_get_zoom (sheet, &zoom);
if (zoom < ZOOM_MAX)
sheet_zoom_step (sheet, 1.1);
} else if (scr_event->direction == GDK_SCROLL_DOWN) {
double zoom;
sheet_get_zoom (sheet, &zoom);
if (zoom > ZOOM_MIN)
sheet_zoom_step (sheet, 0.9);
}
}
} break;
case GDK_MOTION_NOTIFY:
if (sheet->priv->rubberband_info->state == RUBBERBAND_ACTIVE) {
rubberband_update (sheet, event);
return TRUE;
}
if (wklass->motion_notify_event != NULL) {
return wklass->motion_notify_event (widget, (GdkEventMotion *)event);
}
break;
case GDK_ENTER_NOTIFY:
return wklass->enter_notify_event (widget, (GdkEventCrossing *)event);
case GDK_LEAVE_NOTIFY:
return wklass->leave_notify_event (widget, (GdkEventCrossing *)event);
case GDK_KEY_PRESS:
switch (event->key.keyval) {
case GDK_KEY_R:
case GDK_KEY_r:
if (sheet->state == SHEET_STATE_NONE)
sheet_rotate_selection (sheet, 90);
break;
case GDK_KEY_L:
case GDK_KEY_l:
if (sheet->state == SHEET_STATE_NONE)
sheet_rotate_selection (sheet, -90);
break;
case GDK_KEY_Home:
case GDK_KEY_End:
break;
case GDK_KEY_Left:
if (event->key.state & GDK_MOD1_MASK) {
sheet_scroll_pixel (sheet, -20, 0);
} else {
sheet_move_selection (sheet, -20., 0.);
}
break;
case GDK_KEY_Up:
if (event->key.state & GDK_MOD1_MASK) {
sheet_scroll_pixel (sheet, 0, -20);
} else {
sheet_move_selection (sheet, 0., -20.);
}
break;
case GDK_KEY_Right:
if (event->key.state & GDK_MOD1_MASK) {
sheet_scroll_pixel (sheet, 20, 0);
} else {
sheet_move_selection (sheet, +20., 0.);
}
break;
case GDK_KEY_Down:
if (event->key.state & GDK_MOD1_MASK) {
sheet_scroll_pixel (sheet, 0, 20);
} else {
sheet_move_selection (sheet, 0., +20.);
}
break;
case GDK_KEY_Escape:
g_signal_emit_by_name (G_OBJECT (sheet), "cancel");
break;
case GDK_KEY_Delete:
sheet_delete_selection (sheet);
break;
default:
return FALSE;
}
default:
return FALSE;
}
return TRUE;
}
/**
* select all items on the sheet
*/
void sheet_select_all (Sheet *sheet, gboolean select)
{
GList *list;
g_return_if_fail (sheet != NULL);
g_return_if_fail (IS_SHEET (sheet));
for (list = sheet->priv->items; list; list = list->next)
sheet_item_select (SHEET_ITEM (list->data), select);
if (!select)
sheet_release_selected_objects (sheet);
}
/**
* rotate the currently selected on the sheet
*/
void sheet_rotate_selection (Sheet *sheet, gint angle)
{
g_return_if_fail (sheet != NULL);
g_return_if_fail (IS_SHEET (sheet));
if (sheet->priv->selected_objects != NULL)
rotate_items (sheet, sheet->priv->selected_objects, angle);
}
/**
* move the currently selected on the sheet
*/
void sheet_move_selection (Sheet *sheet, gdouble x, gdouble y)
{
g_return_if_fail (sheet != NULL);
g_return_if_fail (IS_SHEET (sheet));
const Coords delta = {x, y};
if (sheet->priv->selected_objects != NULL)
move_items (sheet, sheet->priv->selected_objects, &delta);
}
/**
* rotate floating items
*/
void sheet_rotate_ghosts (Sheet *sheet)
{
g_return_if_fail (sheet != NULL);
g_return_if_fail (IS_SHEET (sheet));
if (sheet->priv->floating_objects != NULL)
rotate_items (sheet, sheet->priv->floating_objects, 90);
}
static void rotate_items (Sheet *sheet, GList *items, gint angle)
{
GList *list, *item_data_list;
Coords center, b1, b2;
item_data_list = NULL;
for (list = items; list; list = list->next) {
item_data_list = g_list_prepend (item_data_list, sheet_item_get_data (list->data));
}
item_data_list_get_absolute_bbox (item_data_list, &b1, &b2);
center = coords_average (&b1, &b2);
snap_to_grid (sheet->grid, ¢er.x, ¢er.y);
for (list = item_data_list; list; list = list->next) {
ItemData *item_data = list->data;
if (item_data == NULL)
continue;