aboutsummaryrefslogtreecommitdiff
path: root/src/graph/view2D/process2d.c
blob: 276948efccc0a75d8828e0aa7473fdec5c95b60c (plain)
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
/*
  Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
  All rights reserved.
  Copyright (C) 2007-2011, Gabriel Dos Reis.
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are
  met:

      - Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.

      - Redistributions in binary form must reproduce the above copyright
        notice, this list of conditions and the following disclaimer in
        the documentation and/or other materials provided with the
        distribution.

      - Neither the name of The Numerical Algorithms Group Ltd. nor the
        names of its contributors may be used to endorse or promote products
        derived from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#define _PROCESS2D_C
#include "openaxiom-c-macros.h"

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#ifdef RIOSplatform
#include <sys/select.h>
#endif

#include "cfuns.h"
#include "header2.h"
#include "all_2d.H1"
#include "util.H1"
#include "Gfun.H1"
#include "XSpadFill.h"

using namespace OpenAxiom;


static int doit=0;  /* globish variable for picking/dropping/clearing - all sorts of
                2 button sequence events (command & graph #). */


void 
doPick (int i,int bKey)
{
  int vCommand=pick2D;
  
  picking = no;
  /* reset indicator so that we're not in pick/drop/query mode anymore */
  doit = 0;
  if (graphArray[currentGraph].key) {
    check(write(Socket,&vCommand,intSize));
    check(write(Socket,&(graphArray[currentGraph].key),intSize));
    check(write(Socket,&(graphStateArray[currentGraph]),
                sizeof(graphStateStruct)));
    sprintf(control->message,"%s%d","Picked up graph ",currentGraph+1);
  } else
    sprintf(control->message,"%s","This graph is empty!");
  writeControlMessage();
  
  
}


void 
doDrop (int i,int bKey)
{
  int vCommand=drop2D;
  int viewGoAhead;
  
  dropping = no;
  /* reset indicator so that we're not in pick/drop/query mode anymore */
  doit = 0;
  check(write(Socket,&(vCommand),intSize));
  readViewman(&viewGoAhead,intSize);
  if (viewGoAhead < 0) {
    sprintf(control->message,"%s%d","Couldn't drop to graph ",currentGraph+1);
    writeControlMessage();
  } else {
    sprintf(control->message,"%s%d","Dropped onto graph ",currentGraph+1);
    writeControlMessage();
    freeGraph(currentGraph);
    readViewman(&(graphArray[currentGraph].key),intSize);
    getGraphFromViewman(currentGraph);
    /* simulate a button press to turn display number on and select on */
    /* need !yes since it will be inverted */
    graphStateArray[currentGraph].selected = no;
    graphStateArray[currentGraph].showing =
      !(graphStateArray[currentGraph].showing);
    clickedOnGraph(currentGraph,currentGraph+graphStart);
    clickedOnGraphSelect(currentGraph,currentGraph+graphSelectStart);
  }
}

void 
clickedOnGraphSelect (int i,int bKey)
{
  int strlength;
  
  switch (doit) {
  case pick2D:
    currentGraph = i;
    doPick(i,bKey);
    break;
  case drop2D:
    currentGraph = i;
    doDrop(i,bKey);
    break;
  case query2D:
    queriedGraph = i;
    makeMessageFromData(queriedGraph);
    writeControlMessage();
    /* reset indicator so that we're not in pick/drop/query mode anymore */
    doit = 0;
    break;
  default:
    graphStateArray[i].selected = !(graphStateArray[i].selected);
    if (graphStateArray[i].selected) {
      GSetForeground(graphGC,(float)graphBarSelectColor,Xoption);
      strcpy(control->buttonQueue[bKey].text,"^");
      strlength = strlen(control->buttonQueue[bKey].text);
    } else {
      GSetForeground(graphGC,(float)graphBarNotSelectColor,Xoption);
      strcpy(control->buttonQueue[bKey].text,"-");
      strlength = strlen(control->buttonQueue[bKey].text);
    }
    /* just to make sure the background is reset from clickedOnGraph */
    if (mono) {
      GSetForeground(graphGC,(float)foregroundColor,Xoption);
      GSetBackground(graphGC,(float)backgroundColor,Xoption);
    }
    
    GDrawImageString(graphGC,control->controlWindow,
                     control->buttonQueue[bKey].buttonX +
                     centerX(graphGC,
                             control->buttonQueue[bKey].text,strlength,
                             control->buttonQueue[bKey].buttonWidth),
                     control->buttonQueue[bKey].buttonY +
                     centerY(graphGC,
                             control->buttonQueue[bKey].buttonHeight),
                     control->buttonQueue[bKey].text,strlength,Xoption);
    GSetForeground(trashGC,(float)foregroundColor,Xoption);
    GSetLineAttributes(trashGC,2,LineSolid,CapButt,JoinMiter,Xoption);
    GDrawLine(trashGC,control->controlWindow,0,286,controlWidth,286,Xoption);
    break;
  } /* switch doit */
  
}


static void
drawControlPushButton(int isOn, int index)
{
  GDrawPushButton(dsply, processGC, processGC, processGC,
                  control->controlWindow,
                  (control->buttonQueue[index]).buttonX,
                  (control->buttonQueue[index]).buttonY,
                  (control->buttonQueue[index]).buttonWidth,
                  (control->buttonQueue[index]).buttonHeight,
                  isOn,
                  (control->buttonQueue[index]).text,
                  buttonColor,
                  monoColor((control->buttonQueue[index]).textColor), Xoption);
  XSync(dsply,False);
}



void 
buttonAction (int bKey)
{
  int i;
  
  switch (bKey) {
    
  case pick2D:
    if (viewAloned) {
      sprintf(control->message,"%s","Cannot pick without OpenAxiom!");
      writeControlMessage();
      XSync(dsply,False);
    }
    else {
      doit = pick2D;
      sprintf(control->message,"%s","Click on the graph to pick");
      writeControlMessage();
      XSync(dsply,False);
    }
    break;
    
  case drop2D:
    if (viewAloned) {
      sprintf(control->message,"%s","Cannot drop without OpenAxiom!");
      writeControlMessage();
      XSync(dsply,False);
    } else {
      doit = drop2D;
      sprintf(control->message,"%s","Click on the graph to drop");
      writeControlMessage();
      XSync(dsply,False);
    }
    break;
    
  case query2D:
    doit = query2D;
    sprintf(control->message,"%s","Click on the graph to query");
    writeControlMessage();
    XSync(dsply,False);
    break;
    
    
  case zoom2Dx:
    if (zoomXON)
      strcpy((control->buttonQueue[zoom2Dx]).text ,
             "X Off");
    else
      strcpy((control->buttonQueue[zoom2Dx]).text , 
             "X On ");
    zoomXON = !zoomXON;
    
    drawControlPushButton(zoomXON, zoom2Dx);
    XSync(dsply,False);
    break;
    
  case zoom2Dy:
    if (zoomYON) strcpy((control->buttonQueue[zoom2Dy]).text,
                        "Y Off");
    else strcpy((control->buttonQueue[zoom2Dy]).text ,
                "Y On ");
    zoomYON = !zoomYON;
    drawControlPushButton(zoomYON, zoom2Dy);
    XSync(dsply,False);
    break;
    
    
  case translate2Dx:
    if (transXON) strcpy((control->buttonQueue[translate2Dx]).text,
                         "X Off");
    else strcpy( (control->buttonQueue[translate2Dx]).text,
                 "X On ");
    transXON = !transXON;
    drawControlPushButton(transXON, translate2Dx);
    XSync(dsply,False);
    break;
    
  case translate2Dy:
    if (transYON) strcpy((control->buttonQueue[translate2Dy]).text,
                         "Y Off");
    else strcpy( (control->buttonQueue[translate2Dy]).text,
                 "Y On");
    transYON = !transYON;
    drawControlPushButton(transYON, translate2Dy);
    XSync(dsply,False);
    break;
    
  case pointsOnOff:
    if (pointsON) strcpy((control->buttonQueue[pointsOnOff]).text,
                         "Pts Off");
    else strcpy( (control->buttonQueue[pointsOnOff]).text,
                 "Pts On ");
    pointsON = !pointsON;
    for (i=0; i<maxGraphs; i++)
      if (graphStateArray[i].showing && graphStateArray[i].selected)
        graphStateArray[i].pointsOn = pointsON;
    
    drawControlPushButton(pointsON, pointsOnOff);
    drawViewport(Xoption);
    break;
    
  case connectOnOff:
    if (connectON) strcpy((control->buttonQueue[connectOnOff]).text,
                          "Lines Off");
    else strcpy( (control->buttonQueue[connectOnOff]).text,
                 "Lines On ");
    connectON = !connectON;
    for (i=0; i<maxGraphs; i++)
      if (graphStateArray[i].showing && graphStateArray[i].selected)
        graphStateArray[i].connectOn = connectON;
    
    drawControlPushButton(connectON, connectOnOff);
    drawViewport(Xoption);
    break;
    
  case spline2D:
    if (splineON) strcpy((control->buttonQueue[spline2D]).text,
                         "Box Off");
    else strcpy ((control->buttonQueue[spline2D]).text ,
                 "Box On ");
    splineON = !splineON;
    for (i=0; i<maxGraphs; i++)
      if (graphStateArray[i].showing && graphStateArray[i].selected)
        graphStateArray[i].splineOn = splineON;
    
    drawControlPushButton(splineON, spline2D);
    drawViewport(Xoption);
    break;
    
  case axesOnOff2D:
    if (axesON)
      strcpy ((control->buttonQueue[axesOnOff2D]).text,
              "Axes Off");
    else
      strcpy ((control->buttonQueue[axesOnOff2D]).text ,
              "Axes On ");
    axesON = !axesON;
    for (i=0; i<maxGraphs; i++)
      if (graphStateArray[i].showing && graphStateArray[i].selected)
        graphStateArray[i].axesOn = axesON;
    
    drawControlPushButton(axesON, axesOnOff2D);
    drawViewport(Xoption);
    break;
    
  case unitsOnOff2D:
    if (unitsON)
      strcpy( (control->buttonQueue[unitsOnOff2D]).text,
              "Units Off");
    else
      strcpy ((control->buttonQueue[unitsOnOff2D]).text,
              "Units On ");
    unitsON = !unitsON;
    for (i=0; i<maxGraphs; i++)
      if (graphStateArray[i].showing && graphStateArray[i].selected)
        graphStateArray[i].unitsOn = unitsON;
    
    drawControlPushButton(unitsON, unitsOnOff2D);
    drawViewport(Xoption);
    break;
    
  case ps2D:
    strcpy(control->message,"Creating postscript now ...");
    writeControlMessage();
    drawControlPushButton(1, ps2D);
    
    if (PSInit(viewport->viewWindow, viewport->titleWindow) == psError) {
      strcpy(control->message,"Aborted: PSInit error.");
      writeControlMessage();
      drawControlPushButton(0, ps2D);
      XSync(dsply,False);
      return;   /* make new temp name for new file */
    }
    
    drawViewport(PSoption);     /* draw picture in PS; create ps script file */
    
    if (PSCreateFile(viewBorderWidth, 
                     viewport->viewWindow,
                     viewport->titleWindow, 
                     viewport->title) == psError) {
      strcpy(control->message,"Aborted: PSCreateFile error.");
      writeControlMessage();
      drawControlPushButton(0, ps2D);
      XSync(dsply,False);
      return;
    }
    
    clearControlMessage();
    strcpy(control->message,PSfilename);
    strcat(control->message," in working dir ");
    writeControlMessage();
    drawControlPushButton(0, ps2D);
    XSync(dsply,False);
    break;
    
  case hideControl2D:
    if (viewport->haveControl) {
      viewport->haveControl = no;
      XUnmapWindow(dsply,control->controlWindow);
      XSync(dsply,False);
    }
    break;
    
  case reset2D:
    /* reset view */
    for (i=0; i<maxGraphs; i++)
      if (graphStateArray[i].showing && graphStateArray[i].selected)
        graphStateArray[i] = graphStateBackupArray[i];
    
    unitsON = no;
    strcpy( (control->buttonQueue[unitsOnOff2D]).text,s  = "Units Off");
    for (i=0; i<maxGraphs; i++)
      if (graphStateArray[i].showing && graphStateArray[i].selected)
        graphStateArray[i].unitsOn = no;
    drawControlPushButton(unitsON, unitsOnOff2D);
    
    pointsON = yes;
    strcpy ((control->buttonQueue[pointsOnOff]).text ,"Pts On ");
    for (i=0; i<maxGraphs; i++)
      if (graphStateArray[i].showing && graphStateArray[i].selected)
        graphStateArray[i].pointsOn = yes;
    drawControlPushButton(pointsON, pointsOnOff);
    
    axesON = yes;
    strcpy ((control->buttonQueue[axesOnOff2D]).text,"Axes On ");
    for (i=0; i<maxGraphs; i++)
      if (graphStateArray[i].showing && graphStateArray[i].selected)
        graphStateArray[i].axesOn = yes;
    drawControlPushButton(axesON, axesOnOff2D);
    
    connectON = yes;
    strcpy((control->buttonQueue[connectOnOff]).text,"Lines On ");
    for (i=0; i<maxGraphs; i++)
      if (graphStateArray[i].showing && graphStateArray[i].selected)
        graphStateArray[i].connectOn = yes;
    drawControlPushButton(connectON, connectOnOff);
    
    splineON = no;
    strcpy( (control->buttonQueue[connectOnOff]).text ,"Box Off");
    for (i=0; i<maxGraphs; i++)
      if (graphStateArray[i].showing && graphStateArray[i].selected)
        graphStateArray[i].splineOn = splineON;
    drawControlPushButton(splineON, spline2D);
    drawViewport(Xoption);
    
    break;
    
  case closeAll2D:
    strcpy(control->message,"       Click again to confirm       ");
    writeControlMessage();
    drawControlPushButton(1, closeAll2D);
    XSync(dsply,False);
    viewport->closing = yes;
    break;
    
  case clear2D:
    for (i=0; i<maxGraphs; i++) graphStateArray[i].selected = 1;
    clickedOnGraphSelect(0,graphSelect1);
    clickedOnGraphSelect(1,graphSelect2);
    clickedOnGraphSelect(2,graphSelect3);
    clickedOnGraphSelect(3,graphSelect4);
    clickedOnGraphSelect(4,graphSelect5);
    clickedOnGraphSelect(5,graphSelect6);
    clickedOnGraphSelect(6,graphSelect7);
    clickedOnGraphSelect(7,graphSelect8);
    clickedOnGraphSelect(8,graphSelect9);
    XSync(dsply,False);
    break;
    
  case graph1:
  case graph2:
  case graph3:
  case graph4:
  case graph5:
  case graph6:
  case graph7:
  case graph8:
  case graph9:
    clickedOnGraph(bKey-graphStart,bKey);
    XSync(dsply,False);
    break;
    
  case graphSelect1:
  case graphSelect2:
  case graphSelect3:
  case graphSelect4:
  case graphSelect5:
  case graphSelect6:
  case graphSelect7:
  case graphSelect8:
  case graphSelect9:
    clickedOnGraphSelect(bKey-graphSelectStart,bKey);
    XSync(dsply,False);
    break;
    
  } /* switch (action) */
}

/*********************** X Event Processing ***************************/
void 
processEvents(void)
{
  
  XEvent                  *event,
    tempEvent;
  Window                  whichWindow;
  XWindowAttributes       graphWindowAttrib;
  buttonStruct            *controlButton;
  mouseCoord              mouseXY;
  int                     i,
    someInt,
    mouseW4,
    mouseH4,
    toggleReady, 
    gotToggle = no,
    checkButton = no,
    firstTime = yes,
    gotEvent = 0,
    buttonTablePtr,
    Xcon,
    len,
    externalControl;
  fd_set                  rd;
  
  
  externalControl=0;
  Xcon = ConnectionNumber(dsply);
  
  
  if (!(event = (XEvent *)malloc(sizeof(XEvent)))) {
    fprintf(stderr,"Ran out of memory initializing event processing.\n");
    exitWithAck(RootWindow(dsply,scrn),Window,-1);
  }
  
  controlButton = control->buttonQueue;
  
  while(1) {
    
    len=0;
    while(len<=0) {
      FD_ZERO(&rd);
      if (externalControl==0) FD_SET(0, &rd);
      FD_SET(Xcon,&rd);
      
      if (XEventsQueued(dsply, QueuedAlready)) {
        len=1;
        break;
      }
      if (!followMouse) 
        len=select(FD_SETSIZE, &rd,0,0,0); 
      else 
        len=1;
    }
    if (FD_ISSET(Xcon,&rd)|| 
        XEventsQueued(dsply, QueuedAfterFlush) || 
        followMouse) {
      
      if (followMouse) {
        if (XPending(dsply)) 
          XNextEvent(dsply,event);
        gotEvent++;
      } else {
        XNextEvent(dsply,event);
        gotEvent++;
      }
      
      if (gotToggle || !followMouse) 
        checkButton = no;
      
      if (gotEvent) {
        whichWindow = ((XButtonEvent *)event)->window;
        firstTime = no;
        
        switch(((XEvent *)event)->type) {
          
        case ClientMessage:
          if (event->xclient.data.l[0] == wm_delete_window) {
                  goodbye(-1);
          }
          else {
            fprintf(stderr,"Unknown Client Message ...\n");
          }
          break;
        case Expose:
          if (whichWindow == viewport->titleWindow) {
            /* get rid of redundant events */
            XCheckWindowEvent(dsply,
                              viewport->titleWindow,
                              ExposureMask,
                              &tempEvent);
            writeTitle();
            XGetWindowAttributes(dsply,
                                 whichWindow,
                                 &graphWindowAttrib);
            XResizeWindow(dsply,
                          viewport->viewWindow,
                          graphWindowAttrib.width,
                          graphWindowAttrib.height-titleHeight);
            XSync(dsply,False);
            break;
          } else if (whichWindow == viewport->viewWindow) {
            XCheckWindowEvent(dsply,
                              viewport->viewWindow,
                              ExposureMask,
                              &tempEvent);
            XGetWindowAttributes(dsply,
                                 viewport->titleWindow,
                                 &graphWindowAttrib);
            XResizeWindow(dsply,
                          viewport->viewWindow,
                          graphWindowAttrib.width,
                          graphWindowAttrib.height-titleHeight);
            drawViewport(Xoption);
            XMapWindow(dsply,whichWindow);
            XSync(dsply,False);
            break;
          } else {      /* it's gotta be the control panel */
            XGetWindowAttributes(dsply,
                                 control->controlWindow,
                                 &graphWindowAttrib);
            /* do not allow resizing of control panel */
            if ((graphWindowAttrib.width != controlWidth) ||
                (graphWindowAttrib.height != controlHeight)) {
              XResizeWindow(dsply,
                            control->controlWindow,
                            controlWidth,
                            controlHeight);
            }
            drawControlPanel();
            XSync(dsply,False);
            break;
          }
          break;
          
        case MotionNotify:
          if (followMouse) {
            while (XCheckMaskEvent(dsply,
                                   ButtonMotionMask,
                                   event));
            mouseXY = getPotValue(((XButtonEvent *)event)->x,
                                  ((XButtonEvent *)event)->y,
                                  controlButton->xHalf,
                                  controlButton->yHalf);
          }
          if (controlButton->pot) {
            gotToggle = no;
            checkButton = yes;
          }
          break;
          
        case ButtonRelease:
          if (followMouse==yes) {
            followMouse = no;
            toggleReady = yes;
            checkButton = no;
            drawViewport(Xoption);
          } else {
            followMouse = no;
            toggleReady = yes;
            checkButton = no;
          }
          break;
          
        case LeaveNotify:
          /*
            We still follow the mouse when we leave the pots.
            */
          /*      
                  followMouse = no;
                  toggleReady = yes; 
                  checkButton = no; 
                  */

          break;
          
        case ButtonPress:
          if (whichWindow == viewport->viewWindow) {
            
            /* mouse clicked on viewport */

            switch (((XButtonEvent *)event)->button) {
            case Button3:

              /* print out (x,y) object-space coordinates in message area */

              XGetWindowAttributes(dsply,whichWindow,&graphWindowAttrib);
              sprintf(viewport->controlPanel->message,
                      "       >%d<: [%6.2f,%6.2f]       ", 
                      queriedGraph+1,
                      projX((((XButtonEvent *)event)->x),
                            graphWindowAttrib.width,queriedGraph),
                      projY((((XButtonEvent *)event)->y),
                            graphWindowAttrib.height,queriedGraph));
              writeControlMessage();
              XFlush(dsply);
              break;
            default:

              /* Find where mouse is on the viewport => where to put the CP */

              XGetWindowAttributes(dsply,
                                   whichWindow,
                                   &graphWindowAttrib);
              mouseW4 = graphWindowAttrib.width/4;
              if (((XButtonEvent *)event)->x >
                  (graphWindowAttrib.width - mouseW4))
                someInt = 1;
              else {
                mouseH4 = graphWindowAttrib.height/4;
                if (((XButtonEvent *)event)->y >
                    (graphWindowAttrib.height - mouseH4)) 
                  someInt = 2;
                else if (((XButtonEvent *)event)->x < mouseW4) 
                  someInt = 3;
                else if (((XButtonEvent *)event)->y < mouseH4) 
                  someInt = 4;
                else someInt = 0;
              }
              if (viewport->haveControl) {
                XUnmapWindow(dsply,control->controlWindow);
              }
              putControlPanelSomewhere(someInt);
              XMapWindow(dsply,control->controlWindow);
              XSync(dsply,False);
              break;
            } /* switch on mouse button */
          } else if (whichWindow == control->colormapWindow) {

            /* mouse clicked on colormap */

            followMouse = yes;
            gotToggle   = no;
            checkButton = yes;
            firstTime   = yes;
          } else if (whichWindow != control->controlWindow) {

            /* mouse clicked on control window (not colormap) */

            if (controlButton->self != whichWindow) {
              buttonTablePtr = *((int *)XLookUpAssoc(dsply,table,whichWindow));
              controlButton = &(control->buttonQueue[buttonTablePtr]);
            }
            
            if (controlButton->pot) {
              /* figure out [x,y] for this button in the range [-1..1,-1..1] */
              mouseXY = getPotValue(((XButtonEvent *)event)->x,
                                    ((XButtonEvent *)event)->y,
                                    controlButton->xHalf,
                                    controlButton->yHalf);
              followMouse = yes;
              gotToggle = no;
            } else {
              followMouse = no;
              gotToggle = yes;  /* auto-repeat on toggle buttons not allowed */
              if (toggleReady) {
                toggleReady = no;
              }
            }
            checkButton = yes;
            firstTime   = yes;
          }
          break;
          
        } /* switch */
        gotEvent--;
      } /* if gotEvent */
      
      
      /* Allow repeat polling when mouse button clicked on a potentiometer. */

      if (followMouse && !firstTime && (followMouse++ > mouseWait)) {
        followMouse = yes;   /* reset for next timing loop */
        checkButton = yes;
      }
      if (checkButton) {
        if (viewport->closing && (controlButton->buttonKey == closeAll2D)) {
          goodbye(-1);
        } else {
          clearControlMessage();
          viewport->closing = no;
          drawControlPushButton(0, closeAll2D);
          if ((doit) &&
              ((controlButton->buttonKey < graphStart) &&
               (controlButton->buttonKey > (graphSelectStart + maxGraphs))))
            doit = 0;
          
          switch(controlButton->buttonKey) {
            
          case translate2D:
            for (i=0; i<maxGraphs; i++) {
              if (graphStateArray[i].showing && graphStateArray[i].selected) {
                if (transXON) {
                  graphStateArray[i].centerX -= mouseXY.x * 0.1;
                  if (graphStateArray[i].centerX > maxDelta)
                    graphStateArray[i].centerX = maxDelta;
                  else if (graphStateArray[i].centerX < -maxDelta)
                    graphStateArray[i].centerX = maxDelta;
                }
                if (transYON) {
                  graphStateArray[i].centerY -= mouseXY.y * 0.1;
                  if (graphStateArray[i].centerY > maxDelta)
                    graphStateArray[i].centerY = maxDelta;
                  else if (graphStateArray[i].centerY < -maxDelta)
                    graphStateArray[i].centerY = maxDelta;
                }
              } /* graph showing or selected */
            }  /* for graphs */
            drawViewport(Xoption);
            break;
            
          case scale2D:
            for (i=0; i<maxGraphs; i++) {
              if (graphStateArray[i].showing && graphStateArray[i].selected) {
                if (zoomXON) {
                  graphStateArray[i].scaleX *= (1 - mouseXY.y * 0.3);
                  if (graphStateArray[i].scaleX > maxScale)
                    graphStateArray[i].scaleX = maxScale;
                  else if (graphStateArray[i].scaleX < minScale)
                    graphStateArray[i].scaleX = minScale;
                }
                if (zoomYON) {
                  graphStateArray[i].scaleY *= (1 - mouseXY.y * 0.3);
                  if (graphStateArray[i].scaleY > maxScale)
                    graphStateArray[i].scaleY = maxScale;
                  else if (graphStateArray[i].scaleY < minScale)
                    graphStateArray[i].scaleY = minScale;
                }
              } /* graph showing or selected */
            }  /* for graphs */
            drawViewport(Xoption);
            break;
            
          default:
            buttonAction(controlButton->buttonKey);
          } /* switch on buttonKey */
        } /* else - not closing */
      } /* if checkButton */
    } /* if FD_ISSET(Xcon.... */
    else if (FD_ISSET(0,&rd)) {
      externalControl=spadAction(); /* returns (-1) if broken ,0 if success */
      if (spadDraw && (externalControl==0)) drawViewport(Xoption);
    }
  } /* while */
} /* processEvents() */






void 
clickedOnGraph (int i,int bKey)
{
  
  switch (doit) {
  case pick2D:
    currentGraph = queriedGraph = i;
    doPick(i,bKey);
    break;
  case drop2D:
    currentGraph = queriedGraph = i;
    doDrop(i,bKey);
    break;
  case query2D:
    queriedGraph = i;
    makeMessageFromData(queriedGraph);
    writeControlMessage();
    /* reset indicator so that we're not in pick/drop/query mode anymore */
    doit = 0;
    break;
  default:
    graphStateArray[i].showing = !(graphStateArray[i].showing);
    if (mono) {
      if (graphStateArray[i].showing) {
        GSetForeground(graphGC,(float)backgroundColor,Xoption);
        GSetBackground(graphGC,(float)foregroundColor,Xoption);
      } else {
        GSetForeground(graphGC,(float)foregroundColor,Xoption);
        GSetBackground(graphGC,(float)backgroundColor,Xoption);
      }
      GDrawImageString(graphGC,
                       control->controlWindow,
                       (control->buttonQueue[bKey]).buttonX +
                       centerX(graphGC,(control->buttonQueue[bKey]).text,1,
                               (control->buttonQueue[bKey]).buttonWidth),
                       (control->buttonQueue[bKey]).buttonY +
                       centerY(graphGC,(control->buttonQueue[bKey]).buttonHeight),
                       (control->buttonQueue[bKey]).text,
                       1,
                       Xoption);
    } else {
      if (graphStateArray[i].showing)
        GSetForeground(graphGC,(float)graphBarShowingColor,Xoption);
      else
        GSetForeground(graphGC,(float)graphBarHiddenColor,Xoption);
      GDrawString(graphGC,
                  control->controlWindow,
                  (control->buttonQueue[bKey]).buttonX +
                  centerX(graphGC,(control->buttonQueue[bKey]).text,1,
                          (control->buttonQueue[bKey]).buttonWidth),
                  (control->buttonQueue[bKey]).buttonY +
                  centerY(graphGC,(control->buttonQueue[bKey]).buttonHeight),
                  (control->buttonQueue[bKey]).text,1,Xoption);
    }
    drawViewport(Xoption);
    break;
  } /* switch doit */
  
}