-
Notifications
You must be signed in to change notification settings - Fork 24
/
events.c
6822 lines (5885 loc) · 199 KB
/
events.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
/*-------------------------------------------------------------------------*/
/* events.c --- xcircuit routines handling Xevents and Callbacks */
/* Copyright (c) 2002 Tim Edwards, Johns Hopkins University */
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/* written by Tim Edwards, 8/13/93 */
/*-------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#ifndef XC_WIN32
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#define XK_MISCELLANY
#define XK_LATIN1
#include <X11/keysymdef.h>
#else
#ifdef TCL_WRAPPER
#define XK_MISCELLANY
#define XK_LATIN1
#include <X11/keysymdef.h>
#endif
#endif
#ifdef HAVE_CAIRO
#include <cairo/cairo-xlib.h>
#endif
/*-------------------------------------------------------------------------*/
/* Local includes */
/*-------------------------------------------------------------------------*/
#ifdef TCL_WRAPPER
#include <tk.h>
#endif
#include "xcircuit.h"
#include "colordefs.h"
#define HOLD_MASK (Mod4Mask << 16)
/*----------------------------------------------------------------------*/
/* Function prototype declarations */
/*----------------------------------------------------------------------*/
#include "prototypes.h"
/*-------------------------------------------------------------------------*/
/* Global Variable definitions */
/*-------------------------------------------------------------------------*/
extern XtAppContext app;
extern Display *dpy;
extern Cursor appcursors[NUM_CURSORS];
extern Globaldata xobjs;
extern XCWindowData *areawin;
extern ApplicationData appdata;
extern colorindex *colorlist;
extern short popups;
extern int pressmode;
extern xcWidget message2, top;
extern char _STR[150], _STR2[250];
extern short beeper;
extern double saveratio;
extern u_char texttype;
extern aliasptr aliastop;
#ifdef TCL_WRAPPER
extern Tcl_Interp *xcinterp;
#else
extern short help_up;
#endif
/* double buffer */
#if !defined(HAVE_CAIRO)
Pixmap dbuf = (Pixmap)NULL;
#endif
Boolean was_preselected;
/*----------------------------------------------------------------------------*/
/* Edit Object pushing and popping. */
/*----------------------------------------------------------------------------*/
Boolean recursefind(objectptr parent, objectptr suspect)
{
genericptr *shell;
if (parent == suspect) return True;
for (shell = parent->plist; shell < parent->plist + parent->parts; shell++)
if (IS_OBJINST(*shell))
if (recursefind(TOOBJINST(shell)->thisobject, suspect)) return True;
return False;
}
/*--------------------------------------------------------------*/
/* Transfer objects in the select list to the current object */
/* (but disallow infinitely recursive loops!) */
/*--------------------------------------------------------------*/
/* IMPORTANT: delete_for_xfer() MUST be executed prior to */
/* calling transferselects(), so that the deleted elements are */
/* in an object saved in areawin->editstack. */
/*--------------------------------------------------------------*/
void transferselects()
{
short locselects;
objinstptr tobj;
XPoint newpos;
if (areawin->editstack->parts == 0) return;
if (eventmode == MOVE_MODE || eventmode == COPY_MODE ||
eventmode == UNDO_MODE || eventmode == CATMOVE_MODE) {
short ps = topobject->parts;
freeselects();
locselects = areawin->editstack->parts;
areawin->selectlist = xc_undelete(areawin->topinstance,
areawin->editstack, (short)NORMAL, (short *)NULL);
areawin->selects = locselects;
/* Move all selected items to the cursor position */
newpos = UGetCursor();
drag((int)newpos.x, (int)newpos.y);
/* check to make sure this object is not the current object */
/* or one of its direct ancestors, else an infinite loop results. */
for (ps = 0; ps < topobject->parts; ps++) {
if (IS_OBJINST(*(topobject->plist + ps))) {
tobj = TOOBJINST(topobject->plist + ps);
if (recursefind(tobj->thisobject, topobject)) {
Wprintf("Attempt to place object inside of itself");
delete_noundo(NORMAL);
break;
}
}
}
}
}
/*-------------------------------------------------------------------*/
/* Make a new matrix corresponding to the current position and scale */
/*-------------------------------------------------------------------*/
void newmatrix()
{
if (DCTM == NULL) {
DCTM = (Matrixptr)malloc(sizeof(Matrix));
DCTM->nextmatrix = NULL;
}
UResetCTM(DCTM);
UMakeWCTM(DCTM);
}
/*-------------------------------------------------------*/
/* set the viewscale variable to the proper address */
/*-------------------------------------------------------*/
void setpage(Boolean killselects)
{
areawin->vscale = topobject->viewscale;
areawin->pcorner = topobject->pcorner;
newmatrix();
if (killselects) clearselects();
#ifdef TCL_WRAPPER
if (xobjs.suspend < 0)
XcInternalTagCall(xcinterp, 2, "page", "goto");
#endif
}
/*-------------------------------------------------------*/
/* switch to a new page */
/*-------------------------------------------------------*/
int changepage(short pagenumber)
{
short npage;
objectptr pageobj;
u_char undo_type;
/* to add to existing number of top level pages. . . */
if (pagenumber == 255) {
if (xobjs.pages == 255) {
Wprintf("Out of available pages!");
return -1;
}
else pagenumber = xobjs.pages;
}
if (pagenumber >= xobjs.pages) {
xobjs.pagelist = (Pagedata **)realloc(xobjs.pagelist, (pagenumber + 1)
* sizeof(Pagedata *));
xobjs.pagelist[pagenumber] = (Pagedata *)malloc(sizeof(Pagedata));
xobjs.pagelist[pagenumber]->filename = NULL;
xobjs.pagelist[pagenumber]->background.name = NULL;
xobjs.pagelist[pagenumber]->pageinst = NULL;
/* If we skipped ahead to pagenumber, fill in the pages in between */
for (npage = xobjs.pages; npage < pagenumber; npage++) {
xobjs.pagelist[npage] = (Pagedata *)malloc(sizeof(Pagedata));
xobjs.pagelist[npage]->pageinst = NULL;
}
xobjs.pages = pagenumber + 1;
makepagebutton();
}
if (eventmode == MOVE_MODE || eventmode == COPY_MODE || eventmode == UNDO_MODE) {
delete_for_xfer(NORMAL, areawin->selectlist, areawin->selects);
undo_type = UNDO_MORE;
}
else {
clearselects();
undo_type = UNDO_DONE;
}
if (areawin->page != pagenumber)
register_for_undo(XCF_Page, undo_type, areawin->topinstance,
areawin->page, pagenumber);
if (eventmode != ASSOC_MODE) {
areawin->page = pagenumber;
free_stack(&areawin->stack);
}
if (xobjs.pagelist[pagenumber]->pageinst == NULL) {
/* initialize a new page */
pageobj = (objectptr) malloc (sizeof(object));
initmem(pageobj);
sprintf(pageobj->name, "Page %d", pagenumber + 1);
xobjs.pagelist[pagenumber]->pageinst = newpageinst(pageobj);
xobjs.pagelist[pagenumber]->filename = NULL;
xobjs.pagelist[pagenumber]->background.name = NULL;
pagereset(pagenumber);
}
/* Write back the current view parameters */
if (areawin->topinstance != NULL) {
topobject->viewscale = areawin->vscale;
topobject->pcorner = areawin->pcorner;
}
areawin->topinstance = xobjs.pagelist[pagenumber]->pageinst;
setpage(TRUE);
return 0;
}
/*-------------------------------------------------------*/
/* switch to a new page and redisplay */
/*-------------------------------------------------------*/
void newpage(short pagenumber)
{
switch (eventmode) {
case CATALOG_MODE:
eventmode = NORMAL_MODE;
catreturn();
break;
case NORMAL_MODE: case COPY_MODE: case MOVE_MODE: case UNDO_MODE:
if (changepage(pagenumber) >= 0) {
transferselects();
renderbackground();
refresh(NULL, NULL, NULL);
togglegrid((u_short)xobjs.pagelist[areawin->page]->coordstyle);
setsymschem();
}
break;
default:
Wprintf("Cannot switch pages from this mode");
break;
}
}
/*---------------------------------------*/
/* Stack structure push and pop routines */
/*---------------------------------------*/
void push_stack(pushlistptr *stackroot, objinstptr thisinst, char *clientdata)
{
pushlistptr newpush;
newpush = (pushlistptr)malloc(sizeof(pushlist));
newpush->next = *stackroot;
newpush->clientdata = clientdata;
newpush->thisinst = thisinst;
*stackroot = newpush;
}
/*----------------------------------------------------------*/
void pop_stack(pushlistptr *stackroot)
{
pushlistptr lastpush;
if (!(*stackroot)) {
Fprintf(stderr, "pop_genstack() Error: NULL instance stack!\n");
return;
}
lastpush = (*stackroot)->next;
free(*stackroot);
*stackroot = lastpush;
}
/*----------------------------------------------------------*/
void free_stack(pushlistptr *stackroot)
{
while ((*stackroot) != NULL)
pop_stack(stackroot);
}
/*------------------------------------------*/
/* Push object onto hierarchy stack to edit */
/*------------------------------------------*/
void pushobject(objinstptr thisinst)
{
short *selectobj, *savelist;
int saves;
u_char undo_type = UNDO_DONE;
objinstptr pushinst = thisinst;
savelist = NULL;
saves = 0;
if (eventmode == MOVE_MODE || eventmode == COPY_MODE) {
savelist = areawin->selectlist;
saves = areawin->selects;
areawin->selectlist = NULL;
areawin->selects = 0;
undo_type = UNDO_MORE;
}
if (pushinst == NULL) {
selectobj = areawin->selectlist;
if (areawin->selects == 0) {
disable_selects(topobject, savelist, saves);
selectobj = select_element(OBJINST);
enable_selects(topobject, savelist, saves);
}
if (areawin->selects == 0) {
Wprintf("No objects selected.");
return;
}
else if (areawin->selects > 1) {
Wprintf("Choose only one object.");
return;
}
else if (SELECTTYPE(selectobj) != OBJINST) {
Wprintf("Element to push must be an object.");
return;
}
else pushinst = SELTOOBJINST(selectobj);
}
if (savelist != NULL) {
delete_for_xfer(NORMAL, savelist, saves);
free(savelist);
}
register_for_undo(XCF_Push, undo_type, areawin->topinstance, pushinst);
/* save the address of the current object to the push stack */
push_stack(&areawin->stack, areawin->topinstance, NULL);
topobject->viewscale = areawin->vscale;
topobject->pcorner = areawin->pcorner;
areawin->topinstance = pushinst;
/* move selected items to the new object */
setpage(TRUE);
transferselects();
refresh(NULL, NULL, NULL);
setsymschem();
}
/*--------------------------*/
/* Pop edit hierarchy stack */
/*--------------------------*/
void popobject(xcWidget w, pointertype no_undo, caddr_t calldata)
{
u_char undo_type = UNDO_DONE;
UNUSED(w); UNUSED(calldata);
if (areawin->stack == NULL || (eventmode != NORMAL_MODE && eventmode != MOVE_MODE
&& eventmode != COPY_MODE && eventmode != FONTCAT_MODE &&
eventmode != ASSOC_MODE && eventmode != UNDO_MODE &&
eventmode != EFONTCAT_MODE)) return;
if ((eventmode == MOVE_MODE || eventmode == COPY_MODE || eventmode == UNDO_MODE)
&& ((areawin->stack->thisinst == xobjs.libtop[LIBRARY]) ||
(areawin->stack->thisinst == xobjs.libtop[USERLIB]))) return;
/* remove any selected items from the current object */
if (eventmode == MOVE_MODE || eventmode == COPY_MODE || eventmode == UNDO_MODE) {
undo_type = UNDO_MORE;
delete_for_xfer(NORMAL, areawin->selectlist, areawin->selects);
}
else if (eventmode != FONTCAT_MODE && eventmode != EFONTCAT_MODE)
unselect_all();
/* If coming from the library, don't register an undo action, because */
/* it has already been registered as type XCF_Library_Pop. */
if (no_undo == (pointertype)0)
register_for_undo(XCF_Pop, undo_type, areawin->topinstance);
topobject->viewscale = areawin->vscale;
topobject->pcorner = areawin->pcorner;
areawin->topinstance = areawin->stack->thisinst;
pop_stack(&areawin->stack);
/* if new object is a library or PAGELIB, put back into CATALOG_MODE */
if (is_library(topobject) >= 0) eventmode = CATALOG_MODE;
/* move selected items to the new object */
if (eventmode == FONTCAT_MODE || eventmode == EFONTCAT_MODE)
setpage(False);
else {
setpage(True);
setsymschem();
if (eventmode != ASSOC_MODE)
transferselects();
}
refresh(NULL, NULL, NULL);
}
/*-------------------------------------------------------------------------*/
/* Destructive reset of entire object */
/*-------------------------------------------------------------------------*/
void resetbutton(xcWidget button, pointertype pageno, caddr_t calldata)
{
short page;
objectptr pageobj;
objinstptr pageinst;
UNUSED(button); UNUSED(calldata);
if (eventmode != NORMAL_MODE) return;
page = (pageno == (pointertype)0) ? areawin->page : (short)(pageno - 1);
pageinst = xobjs.pagelist[page]->pageinst;
if (pageinst == NULL) return; /* page already cleared */
pageobj = pageinst->thisobject;
/* Make sure this is a real top-level page */
if (is_page(topobject) < 0) {
if (pageno == (pointertype)0) {
Wprintf("Can only clear top-level pages!");
return;
}
else {
/* Make sure that we're not in the hierarchy of the page being deleted */
pushlistptr slist;
for (slist = areawin->stack; slist != NULL; slist = slist->next)
if (slist->thisinst->thisobject == pageobj) {
Wprintf("Can't delete the page while you're in its hierarchy!");
return;
}
}
}
/* Watch for pages which are linked by schematic/symbol. */
if (pageobj->symschem != NULL) {
Wprintf("Schematic association to object %s", pageobj->symschem->name);
return;
}
sprintf(pageobj->name, "Page %d", page + 1);
xobjs.pagelist[page]->filename = (char *)realloc(xobjs.pagelist[page]->filename,
(strlen(pageobj->name) + 1) * sizeof(char));
strcpy(xobjs.pagelist[page]->filename, pageobj->name);
reset(pageobj, NORMAL);
flush_undo_stack();
if (page == areawin->page) {
areawin->redraw_needed = True;
drawarea(areawin->area, NULL, NULL);
printname(pageobj);
renamepage(page);
Wprintf("Page cleared.");
}
}
/*------------------------------------------------------*/
/* Redraw the horizontal scrollbar */
/*------------------------------------------------------*/
void drawhbar(xcWidget bar, caddr_t clientdata, caddr_t calldata)
{
Window bwin;
float frac;
long rleft, rright, rmid;
int sbarsize;
char *scale;
UNUSED(clientdata); UNUSED(calldata);
if (!xcIsRealized(bar)) return;
if (xobjs.suspend >= 0) return;
#ifdef TCL_WRAPPER
scale = (char *)Tcl_GetVar2(xcinterp, "XCOps", "scale", TCL_GLOBAL_ONLY);
sbarsize = SBARSIZE * atoi(scale);
#else
sbarsize = SBARSIZE;
#endif
bwin = xcWindow(bar);
if (topobject->bbox.width > 0) {
frac = (float) areawin->width / (float) topobject->bbox.width;
rleft = (long)(frac * (float)(areawin->pcorner.x
- topobject->bbox.lowerleft.x));
rright = rleft + (long)(frac * (float)areawin->width / areawin->vscale);
}
else {
rleft = 0L;
rright = (long)areawin->width;
}
rmid = (rright + rleft) >> 1;
if (rleft < 0) rleft = 0;
if (rright > areawin->width) rright = areawin->width;
XSetFunction(dpy, areawin->gc, GXcopy);
XSetForeground(dpy, areawin->gc, colorlist[BARCOLOR].color.pixel);
if (rmid > 0 && rleft > 0)
XClearArea(dpy, bwin, 0, 0, (int)rleft, sbarsize, FALSE);
XFillRectangle(dpy, bwin, areawin->gc, (int)rleft + 1, 1,
(int)(rright - rleft), sbarsize - 1);
if (rright > rmid)
XClearArea(dpy, bwin, (int)rright + 1, 0, areawin->width
- (int)rright, sbarsize, FALSE);
XClearArea(dpy, bwin, (int)rmid - 1, 1, 3, sbarsize, FALSE);
XSetForeground(dpy, areawin->gc, colorlist[areawin->gccolor].color.pixel);
}
/*------------------------------------------------------*/
/* Redraw the vertical scrollbar */
/*------------------------------------------------------*/
void drawvbar(xcWidget bar, caddr_t clientdata, caddr_t calldata)
{
Window bwin = xcWindow(bar);
float frac;
char *scale;
int sbarsize;
long rtop, rbot, rmid;
UNUSED(clientdata); UNUSED(calldata);
#ifdef TCL_WRAPPER
scale = (char *)Tcl_GetVar2(xcinterp, "XCOps", "scale", TCL_GLOBAL_ONLY);
sbarsize = SBARSIZE * atoi(scale);
#else
sbarsize = SBARSIZE;
#endif
if (!xcIsRealized(bar)) return;
if (xobjs.suspend >= 0) return;
if (topobject->bbox.height > 0) {
frac = (float)areawin->height / (float)topobject->bbox.height;
rbot = (long)(frac * (float)(topobject->bbox.lowerleft.y
- areawin->pcorner.y + topobject->bbox.height));
rtop = rbot - (long)(frac * (float)areawin->height / areawin->vscale);
}
else {
rbot = areawin->height;
rtop = 0;
}
rmid = (rtop + rbot) >> 1;
if (rtop < 0) rtop = 0;
if (rbot > areawin->height) rbot = areawin->height;
XSetFunction(dpy, areawin->gc, GXcopy);
XSetForeground(dpy, areawin->gc, colorlist[BARCOLOR].color.pixel);
if (rmid > 0 && rtop > 0)
XClearArea(dpy, bwin, 0, 0, sbarsize, (int)rtop, FALSE);
XFillRectangle(dpy, bwin, areawin->gc, 0, (int)rtop + 2, sbarsize,
(int)(rbot - rtop));
if (rbot > rmid)
XClearArea(dpy, bwin, 0, (int)rbot + 1, sbarsize, areawin->height
- (int)rbot, FALSE);
XClearArea(dpy, bwin, 0, (int)rmid - 1, sbarsize, 3, FALSE);
XSetForeground(dpy, areawin->gc, colorlist[areawin->gccolor].color.pixel);
}
/*------------------------------------------------------*/
/* Simultaneously scroll the screen and horizontal */
/* bar when dragging the mouse in the scrollbar area */
/*------------------------------------------------------*/
void panhbar(xcWidget bar, caddr_t clientdata, XButtonEvent *event)
{
long newx, newpx;
short savex = areawin->pcorner.x;
UNUSED(clientdata);
if (eventmode == SELAREA_MODE) return;
newx = (long)(event->x * ((float)topobject->bbox.width /
areawin->width) + topobject->bbox.lowerleft.x - 0.5 *
((float)areawin->width / areawin->vscale));
areawin->pcorner.x = (short)newx;
drawhbar(bar, NULL, NULL);
areawin->pcorner.x = savex;
if ((newpx = (long)(newx - savex) * areawin->vscale) == 0)
return;
areawin->panx = -newpx;
drawarea(NULL, NULL, NULL);
}
/*------------------------------------------------------*/
/* End the horizontal scroll and refresh entire screen */
/*------------------------------------------------------*/
void endhbar(xcWidget bar, caddr_t clientdata, XButtonEvent *event)
{
long newx;
short savex = areawin->pcorner.x;
UNUSED(clientdata);
areawin->panx = 0;
newx = (long)(event->x * ((float)topobject->bbox.width /
areawin->width) + topobject->bbox.lowerleft.x - 0.5 *
((float)areawin->width / areawin->vscale));
areawin->pcorner.x = (short)newx;
if ((newx << 1) != (long)((short)(newx << 1)) || checkbounds() == -1) {
areawin->pcorner.x = savex;
Wprintf("Reached boundary: cannot pan further");
}
else
W3printf(" ");
areawin->redraw_needed = True;
areawin->lastbackground = NULL;
renderbackground();
drawhbar(bar, NULL, NULL);
drawarea(bar, NULL, NULL);
}
/*------------------------------------------------------*/
/* Simultaneously scroll the screen and vertical */
/* bar when dragging the mouse in the scrollbar area */
/*------------------------------------------------------*/
void panvbar(xcWidget bar, caddr_t clientdata, XButtonEvent *event)
{
long newy, newpy;
short savey = areawin->pcorner.y;
UNUSED(clientdata);
if (eventmode == SELAREA_MODE) return;
newy = (int)((areawin->height - event->y) *
((float)topobject->bbox.height / areawin->height) +
topobject->bbox.lowerleft.y - 0.5 * ((float)areawin->height /
areawin->vscale));
areawin->pcorner.y = (short)newy;
drawvbar(bar, NULL, NULL);
areawin->pcorner.y = savey;
if ((newpy = (long)(newy - savey) * areawin->vscale) == 0)
return;
areawin->pany = newpy;
drawarea(NULL, NULL, NULL);
}
/*------------------------------------------------------*/
/* Pan the screen to follow the cursor position */
/*------------------------------------------------------*/
void trackpan(int x, int y)
{
XPoint newpos;
short savey = areawin->pcorner.y;
short savex = areawin->pcorner.x;
newpos.x = areawin->origin.x - x;
newpos.y = y - areawin->origin.y;
areawin->pcorner.x += newpos.x / areawin->vscale;
areawin->pcorner.y += newpos.y / areawin->vscale;
drawhbar(areawin->scrollbarh, NULL, NULL);
drawvbar(areawin->scrollbarv, NULL, NULL);
areawin->panx = -newpos.x;
areawin->pany = newpos.y;
drawarea(NULL, NULL, NULL);
areawin->pcorner.x = savex;
areawin->pcorner.y = savey;
}
/*------------------------------------------------------*/
/* End the vertical scroll and refresh entire screen */
/*------------------------------------------------------*/
void endvbar(xcWidget bar, caddr_t clientdata, XButtonEvent *event)
{
long newy;
short savey = areawin->pcorner.y;
UNUSED(clientdata);
areawin->pany = 0;
newy = (int)((areawin->height - event->y) *
((float)topobject->bbox.height / areawin->height) +
topobject->bbox.lowerleft.y - 0.5 * ((float)areawin->height /
areawin->vscale));
areawin->pcorner.y = (short)newy;
if ((newy << 1) != (long)((short)(newy << 1)) || checkbounds() == -1) {
areawin->pcorner.y = savey;
Wprintf("Reached boundary: cannot pan further");
}
else
W3printf(" ");
areawin->redraw_needed = True;
areawin->lastbackground = NULL;
renderbackground();
drawvbar(bar, NULL, NULL);
drawarea(bar, NULL, NULL);
}
/*--------------------------------------------------------------------*/
/* Zoom functions-- zoom box, zoom in, zoom out, and pan. */
/*--------------------------------------------------------------------*/
void postzoom()
{
W3printf(" ");
areawin->lastbackground = NULL;
renderbackground();
newmatrix();
}
/*--------------------------------------------------------------------*/
void zoominbox(int x, int y)
{
float savescale;
float delxscale, delyscale;
XPoint savell; /* ucenter, ncenter, (jdk)*/
UNUSED(x); UNUSED(y);
savescale = areawin->vscale;
savell.x = areawin->pcorner.x;
savell.y = areawin->pcorner.y;
/* zoom-box function: corners are in areawin->save and areawin->origin */
/* select box has lower-left corner in .origin, upper-right in .save */
/* ignore if zoom box is size zero */
if (areawin->save.x == areawin->origin.x || areawin->save.y == areawin->origin.y) {
Wprintf("Zoom box of size zero: Ignoring.");
eventmode = NORMAL_MODE;
return;
}
/* determine whether x or y is limiting factor in zoom */
delxscale = (areawin->width / areawin->vscale) /
abs(areawin->save.x - areawin->origin.x);
delyscale = (areawin->height / areawin->vscale) /
abs(areawin->save.y - areawin->origin.y);
areawin->vscale *= min(delxscale, delyscale);
areawin->pcorner.x = min(areawin->origin.x, areawin->save.x) -
(areawin->width / areawin->vscale -
abs(areawin->save.x - areawin->origin.x)) / 2;
areawin->pcorner.y = min(areawin->origin.y, areawin->save.y) -
(areawin->height / areawin->vscale -
abs(areawin->save.y - areawin->origin.y)) / 2;
eventmode = NORMAL_MODE;
/* check for minimum scale */
if (checkbounds() == -1) {
areawin->pcorner.x = savell.x;
areawin->pcorner.y = savell.y;
areawin->vscale = savescale;
Wprintf("At minimum scale: cannot scale further");
/* this is a rare case where an object gets out-of-bounds */
if (checkbounds() == -1) {
if (beeper) XBell(dpy, 100);
Wprintf("Unable to scale: Delete out-of-bounds object!");
}
return;
}
postzoom();
}
/*--------------------------------------------------------------------*/
void zoomin(int x, int y)
{
float savescale;
XPoint ucenter, ncenter, savell;
savescale = areawin->vscale;
savell.x = areawin->pcorner.x;
savell.y = areawin->pcorner.y;
window_to_user(areawin->width / 2, areawin->height / 2, &ucenter);
areawin->vscale *= areawin->zoomfactor;
window_to_user(areawin->width / 2, areawin->height / 2, &ncenter);
areawin->pcorner.x += (ucenter.x - ncenter.x);
areawin->pcorner.y += (ucenter.y - ncenter.y);
/* check for minimum scale */
if (checkbounds() == -1) {
areawin->pcorner.x = savell.x;
areawin->pcorner.y = savell.y;
areawin->vscale = savescale;
Wprintf("At minimum scale: cannot scale further");
/* this is a rare case where an object gets out-of-bounds */
if (checkbounds() == -1) {
if (beeper) XBell(dpy, 100);
Wprintf("Unable to scale: Delete out-of-bounds object!");
}
return;
}
else if (eventmode == MOVE_MODE || eventmode == COPY_MODE ||
eventmode == CATMOVE_MODE)
drag(x, y);
postzoom();
}
/*--------------------------------------------------------------------*/
void zoominrefresh(int x, int y)
{
if (eventmode == SELAREA_MODE)
zoominbox(x, y);
else
zoomin(x, y);
refresh(NULL, NULL, NULL);
}
/*--------------------------------------------------------------------*/
void zoomoutbox(int x, int y)
{
float savescale;
float delxscale, delyscale, scalefac;
XPoint savell; /* ucenter, ncenter, (jdk)*/
XlPoint newll;
UNUSED(x); UNUSED(y);
savescale = areawin->vscale;
savell.x = areawin->pcorner.x;
savell.y = areawin->pcorner.y;
/* zoom-box function, analogous to that for zoom-in */
/* ignore if zoom box is size zero */
if (areawin->save.x == areawin->origin.x || areawin->save.y == areawin->origin.y) {
Wprintf("Zoom box of size zero: Ignoring.");
eventmode = NORMAL_MODE;
return;
}
/* determine whether x or y is limiting factor in zoom */
delxscale = abs(areawin->save.x - areawin->origin.x) /
(areawin->width / areawin->vscale);
delyscale = abs(areawin->save.y - areawin->origin.y) /
(areawin->height / areawin->vscale);
scalefac = min(delxscale, delyscale);
areawin->vscale *= scalefac;
/* compute lower-left corner of (reshaped) select box */
if (delxscale < delyscale) {
newll.y = min(areawin->save.y, areawin->origin.y);
newll.x = (areawin->save.x + areawin->origin.x
- (abs(areawin->save.y - areawin->origin.y) *
areawin->width / areawin->height)) / 2;
}
else {
newll.x = min(areawin->save.x, areawin->origin.x);
newll.y = (areawin->save.y + areawin->origin.y
- (abs(areawin->save.x - areawin->origin.x) *
areawin->height / areawin->width)) / 2;
}
/* extrapolate to find new lower-left corner of screen */
newll.x = areawin->pcorner.x - (int)((float)(newll.x -
areawin->pcorner.x) / scalefac);
newll.y = areawin->pcorner.y - (int)((float)(newll.y -
areawin->pcorner.y) / scalefac);
eventmode = NORMAL_MODE;
areawin->pcorner.x = (short)newll.x;
areawin->pcorner.y = (short)newll.y;
if ((newll.x << 1) != (long)(areawin->pcorner.x << 1) || (newll.y << 1)
!= (long)(areawin->pcorner.y << 1) || checkbounds() == -1) {
areawin->vscale = savescale;
areawin->pcorner.x = savell.x;
areawin->pcorner.y = savell.y;
Wprintf("At maximum scale: cannot scale further.");
return;
}
postzoom();
}
/*--------------------------------------------------------------------*/
void zoomout(int x, int y)
{
float savescale;
XPoint ucenter, ncenter, savell;
XlPoint newll;
savescale = areawin->vscale;
savell.x = areawin->pcorner.x;
savell.y = areawin->pcorner.y;
window_to_user(areawin->width / 2, areawin->height / 2, &ucenter);
areawin->vscale /= areawin->zoomfactor;
window_to_user(areawin->width / 2, areawin->height / 2, &ncenter);
newll.x = (long)areawin->pcorner.x + (long)(ucenter.x - ncenter.x);
newll.y = (long)areawin->pcorner.y + (long)(ucenter.y - ncenter.y);
areawin->pcorner.x = (short)newll.x;
areawin->pcorner.y = (short)newll.y;
if ((newll.x << 1) != (long)(areawin->pcorner.x << 1) || (newll.y << 1)
!= (long)(areawin->pcorner.y << 1) || checkbounds() == -1) {
areawin->vscale = savescale;
areawin->pcorner.x = savell.x;
areawin->pcorner.y = savell.y;
Wprintf("At maximum scale: cannot scale further.");
return;
}
else if (eventmode == MOVE_MODE || eventmode == COPY_MODE ||
eventmode == CATMOVE_MODE)
drag(x, y);
postzoom();
}
/*--------------------------------------------------------------------*/
void zoomoutrefresh(int x, int y)
{
if (eventmode == SELAREA_MODE)
zoomoutbox(x, y);
else
zoomout(x, y);
refresh(NULL, NULL, NULL);