1 /********************************************************************
2 This file is part of the abs 0.907 distribution.  abs is a spreadsheet
3 with graphical user interface.
4 
5 Copyright (C) 1998-2001  Andr� Bertin (Andre.Bertin@ping.be)
6 
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version if in the same spirit as version 2.
11 
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 Concact: abs@pi.be
22          http://home.pi.be/bertin/abs.shtml
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 #include "callback.h"
53 #include "callback_decl.h"
54 #include "inputbox.h"
55 #include "gr_interf.h"
56 #include "main.h"
57 #include "mainwin.h"
58 #include "memory.h"
59 
60 #include "cursor.h"
61 #include "application.h"
62 
63 #include "finite_state.h"
64 
65 extern void setscrollv (double v);
66 extern void setscrollh (double v);
67 
68 #include "newplot.h"
69 
70 extern int rebuild_thumb ();
71 
72 #define StDebug(a) state_print((a))
73 
74 #define AW ActiveWorksheet
75 #define CW worksheet_getcolw
76 #define LH worksheet_getrowh
77 #define SW worksheet_setcolw
78 #define SH worksheet_setrowh
79 #define Letext ActiveMainwin->commandline_obj->text
80 #define Drawarea ActiveMainwin->draw
81 #define linindex ActiveMainwin->linindex
82 #define colindex ActiveMainwin->colindex
83 #define corner ActiveMainwin->corner
84 #define toplevel ActiveMainwin->toplevel
85 #define Infoarea ActiveMainwin->info
86 #define DrawAreaPixmap ActiveMainwin->DrawAreaPixmap
87 #define linpixmap ActiveMainwin->LinPixmap
88 #define colpixmap ActiveMainwin->ColPixmap
89 #define cornerpixmap ActiveMainwin->CornerPixmap
90 
91 #define dpy ActiveMainwin->dpy
92 #define screen ActiveMainwin->screen
93 #define AMwin  ActiveMainwin->draw_window
94 #define Height ActiveMainwin->height
95 #define Width ActiveMainwin->width
96 
97 
98 
99 static int Shift = 0;
100 static int Control = 0;
101 
102 static Cell *EditedCell = NULL;
103 
104 
105 int
cbsetup(Dimension width,Dimension height)106 cbsetup (Dimension width, Dimension height)
107 {
108   TEXT_WIDGET = Letext;
109   setzoom (100);
110   init_application ();
111   FS->l1 = StCell;
112   return 0;
113 }
114 
115 
116 
117 int
DoUndoFormula(int r,int c,char * formula)118 DoUndoFormula (int r, int c, char *formula)
119 {
120   char *old = (char *) get_formula (r, c);
121   char redo[256];
122   char undo[256];
123 
124   if (old == NULL)
125     sprintf (undo, "Cells(%d,%d).Formula = \"\"\n", r, c);
126   else
127     {
128       sprintf (undo, "Cells(%d,%d).Formula = \"%s\"\n", r, c, old);
129     }
130   if (formula != NULL)
131     sprintf (redo, "Cells(%d,%d).Formula = \"%s\"\n", r, c, formula);
132   else
133     sprintf (redo, "Cells(%d,%d).Formula = \"\"\n", r, c);
134 
135   newmacroline (redo);
136 
137   return 0;
138 }
139 
140 void
echofct(char * buf)141 echofct (char *buf)
142 {
143   long p = XawTextGetInsertionPoint (Letext);
144   if (p == 0)
145     {
146       insertstring (Letext, "=");
147       KeyPressed (-1);
148     }
149   insertstring (Letext, buf);
150   KeyPressed (-2);
151   moveinsertionpoint (Letext, -1);
152 
153 }
154 
155 void
stopedit()156 stopedit ()
157 {
158   if (FS->l1 == StCell && FS->l2 == EDITING && FS->l3 == MODIFIED)
159     a_update ();
160 }
161 
162 void
a_update()163 a_update ()
164 {
165   char *buf;
166   char *str;
167 
168 
169 
170   if (ActiveWorksheet != NULL)
171     {
172       if (FS->l1 == StCell && FS->l2 == EDITING && FS->l3 == MODIFIED)
173 	{
174 	  str = GetEntryString (Letext);
175 	  if (str == NULL)
176 	    return;
177 
178 
179 
180 
181 
182 
183 
184 
185 
186 
187 
188 
189 
190 
191 
192 
193 
194 
195 
196 
197 	  buf = str;
198 
199 	  cell_activate ((Cell *) applicationcell (AW->cur_r, AW->cur_c, 1));
200 	  DoUndoFormula (AW->cur_r, AW->cur_c, buf);
201 	  set_formula (AW->cur_r, AW->cur_c, buf);
202 
203 
204 	}
205     }
206 
207   if (FS->l1 == StCell && FS->l2 == EDITING)
208     {
209       SetEntryString (Letext, "");
210       SetEntryString (ActiveMainwin->cell, "");
211       LetextLooseFocus ();
212 
213       state_reset_levels ();
214       FS->l1 = StCell;
215 
216       popdownactivecell ();
217       xdrawcell (AW->cur_r, AW->cur_c, 4, 0);
218     }
219   return;
220 }
221 
222 
223 int
inform(char * str)224 inform (char *str)
225 {
226   return mainwin_inform (str);
227 }
228 
229 
230 
231 
232 
233 int
repaint_activesheet(char * message)234 repaint_activesheet (char *message)
235 {
236   char tmp[256];
237   sprintf (tmp, "repaint_activesheet(%s)", message);
238   if (FS->l1 != StGrap)
239     resetselect (0, 0, 0);
240   xrepaint (tmp);
241   return 0;
242 }
243 
244 int
selectingto0()245 selectingto0 ()
246 {
247 
248   return 0;
249 }
250 
251 
252 
253 int
setcursor(int r,int c)254 setcursor (int r, int c)
255 {
256   int oldr, oldc;
257   char *label;
258 
259   if (AW == NULL)
260     return -1;
261   if (FS->l1 == StGrap || FS->l1 == StBatch)
262     return 0;
263 
264   if (FS->l1 == StCell && FS->l2 == EDITING && FS->l3 == MODIFIED)
265     a_update ();
266 
267   LetextGetFocus ();
268   FS->l1 = StCell;
269   FS->l2 = 0;
270   FS->l3 = 0;
271 
272   oldr = AW->cur_r;
273   oldc = AW->cur_c;
274   worksheet_setcursor (AW, r, c);
275 
276   xdrawlincell (oldr, 0, 0);
277   xdrawcolcell (0, oldc, 0);
278   xdrawlincell (AW->cur_r, 0, 0);
279   xdrawcolcell (0, AW->cur_c, 0);
280   clearwin (linindex);
281   clearwin (colindex);
282 
283   setselection (AW->cur_r, AW->cur_c, AW->cur_r, AW->cur_c);
284 
285   set_visible (AW->cur_r, AW->cur_c);
286   label = (char *) get_formula (AW->cur_r, AW->cur_c);
287   if (label != NULL)
288     {
289       SetEntryString (Letext, label);
290       SetEntryString (ActiveMainwin->cell, label);
291     }
292   else
293     {
294       SetEntryString (Letext, "");
295       SetEntryString (ActiveMainwin->cell, "");
296     }
297 
298   xdrawcursor (0, 0);
299   copypix ("setcursor:after xdrawcursor");
300   return 0;
301 }
302 
303 
304 int
movecursor(int dr,int dc)305 movecursor (int dr, int dc)
306 {
307   int oldr, oldc;
308   char *label;
309 
310   if (AW == NULL)
311     return -1;
312 
313 
314   if (FS->l1 == StCell && FS->l2 == EDITING && FS->l3 == MODIFIED)
315     a_update ();
316 
317 
318   LetextGetFocus ();
319 
320   if (FS->l2 != HAND)
321     {
322       FS->l1 = StCell;
323       FS->l2 = 0;
324       FS->l3 = 0;
325     }
326 
327   oldr = AW->cur_r;
328   oldc = AW->cur_c;
329   worksheet_movecursor (AW, dr, dc);
330   xdrawlincell (oldr, 0, 0);
331   xdrawcolcell (0, oldc, 0);
332   xdrawlincell (AW->cur_r, 0, 0);
333   xdrawcolcell (0, AW->cur_c, 0);
334   clearwin (linindex);
335   clearwin (colindex);
336 
337   if (FS->l2 != HAND)
338     {
339       setselection (AW->cur_r, AW->cur_c, AW->cur_r, AW->cur_c);
340 
341       set_visible (AW->cur_r, AW->cur_c);
342     }
343 
344   label = (char *) get_formula (AW->cur_r, AW->cur_c);
345   if (label != NULL)
346     {
347       SetEntryString (Letext, label);
348       SetEntryString (ActiveMainwin->cell, label);
349     }
350   else
351     {
352       SetEntryString (Letext, "");
353       SetEntryString (ActiveMainwin->cell, label);
354     }
355 
356   xdrawcursor (0, 0);
357   copypix ("movecursor:after xdrawcursor");
358   return 0;
359 }
360 
361 static int hnumover = 0;
362 static int vnumover = 0;
363 
364 int
popupactivecell()365 popupactivecell ()
366 {
367   int cheight, cwidth;
368   int width, height;
369   int i = AW->cur_r;
370   int j = AW->cur_c;
371   char *str = GetEntryString (Letext);
372   int font = cell_getfont (ActiveCell);
373   int fontw = cell_getfontw (ActiveCell);
374   int fonts = cell_getfonts (ActiveCell);
375 
376   int x1 = CW (AW, j) - CW (AW, AW->cc) - 1;
377   int y1 = LH (AW, i) - LH (AW, AW->ll) - 1;
378   int x2, y2;
379 
380   x2 = CW (AW, j + 1 + hnumover) - CW (AW, AW->cc);
381   y2 = LH (AW, i + 1 + vnumover) - LH (AW, AW->ll);
382   width = x2 - x1;
383   height = y2 - y1;
384 
385   cheight = gettexth (str, strlen (str), font, fontw, fonts);
386   cwidth = gettextw (str, strlen (str), font, fontw, fonts) + 5;
387 
388   SetEntryString (ActiveMainwin->cell, GetEntryString (Letext));
389 
390   if (width < cwidth || height < cheight)
391     {
392       popdowncell ();
393 
394     }
395 
396   while (width < cwidth && hnumover < 50)
397     {
398       hnumover++;
399       x2 = CW (AW, j + 1 + hnumover) - CW (AW, AW->cc);
400       width = x2 - x1;
401 
402     }
403   while (height < cheight && vnumover < 50)
404     {
405       vnumover++;
406       y2 = LH (AW, i + 1 + vnumover) - LH (AW, AW->ll);
407       height = y2 - y1;
408     }
409 
410   popupcell (x1 * AW->zoom / 100, y1 * AW->zoom / 100, width, height);
411   return 0;
412 }
413 
414 int
popdownactivecell()415 popdownactivecell ()
416 {
417   hnumover = 0;
418   vnumover = 0;
419   popdowncell ();
420   return 0;
421 }
422 
423 
424 
425 
426 
427 
428 
429 void
cb_TextChange()430 cb_TextChange ()
431 {
432   KeyPressed (-3);
433 }
434 
435 int
KeyPressed(int key)436 KeyPressed (int key)
437 {
438   char message[64];
439   sprintf (message, "KeyPressed(%d)", key);
440   StDebug (message);
441 
442   if (FS->l1 == StGrap && FS->l2 == StCell)
443     {
444       restoretopleft ();
445       popupactivecell ();
446       return -3;
447     }
448 
449   if (FS->l1 == StButton && FS->l3 == StUp)
450     {
451     };
452 
453   if (FS->l1 != StCell)
454     return -1;
455   if (FS->l2 == StMotion)
456     {
457       FS->l2 = 0;
458       return -2;
459     }
460 
461   if (FS->l2 != EDITING)
462     {
463       if (key != 22 &&
464 	  key != 98 &&
465 	  key != 100 &&
466 	  key != 102 &&
467 	  key != 104)
468 	{
469 	  char label[3];
470 	  char *buf = GetEntryString (Letext);
471 
472 	  if (key != -3)
473 	    {
474 
475 	      strncpy (label, buf, 1);
476 	      label[1] = '\0';
477 	      SetEntryString (Letext, label);
478 	    }
479 	  popupactivecell ();
480 	  moveinsertionpoint (Letext, 1);
481 	  FS->l2 = EDITING;
482 	  FS->l3 = MODIFIED;
483 	  storetopleft ();
484 	}
485     }
486 
487   if (FS->l3 == MODIFIED)
488     {
489       if (key == 105 ||
490 	  key == 97 ||
491 	  key == 99 ||
492 	  key == 103)
493 	{
494 	  popdownactivecell ();
495 	  return 0;
496 	}
497       else
498 	{
499 	  restoretopleft ();
500 	  popupactivecell ();
501 
502 	}
503     }
504 
505   return 0;
506 }
507 
508 void
cb_TextReturn(Widget w,XEvent * event,String * params,Cardinal * num_params)509 cb_TextReturn (Widget w, XEvent * event, String * params, Cardinal * num_params)
510 {
511   movecursor (1, 0);
512   FS->l2 = StMotion;
513 }
514 
515 void
cb_TextUp(Widget w,XEvent * event,String * params,Cardinal * num_params)516 cb_TextUp (Widget w, XEvent * event, String * params, Cardinal * num_params)
517 {
518   movecursor (-1, 0);
519   FS->l2 = StMotion;
520 }
521 
522 void
cb_TextDown(Widget w,XEvent * event,String * params,Cardinal * num_params)523 cb_TextDown (Widget w, XEvent * event, String * params, Cardinal * num_params)
524 {
525   movecursor (1, 0);
526   FS->l2 = StMotion;
527 }
528 
529 void
cb_TextLeft(Widget w,XEvent * event,String * params,Cardinal * num_params)530 cb_TextLeft (Widget w, XEvent * event, String * params, Cardinal * num_params)
531 {
532 
533   if (FS->l1 == StCell && FS->l2 == EDITING && FS->l3 == MODIFIED)
534     {
535       if (!moveinsertionpoint (Letext, -1))
536 	return;
537     }
538   movecursor (0, -1);
539   FS->l2 = StMotion;
540 }
541 
542 void
cb_TextRight(Widget w,XEvent * event,String * params,Cardinal * num_params)543 cb_TextRight (Widget w, XEvent * event, String * params, Cardinal * num_params)
544 {
545 
546   if (FS->l1 == StCell && FS->l2 == EDITING && FS->l3 == MODIFIED)
547     {
548       if (!moveinsertionpoint (Letext, 1))
549 	return;
550     }
551   movecursor (0, 1);
552   FS->l2 = StMotion;
553 }
554 
555 void
cb_Delete(Widget w,XEvent * event,String * params,Cardinal * num_params)556 cb_Delete (Widget w, XEvent * event, String * params, Cardinal * num_params)
557 {
558   if ((FS->l1 == StCell && FS->l2 != EDITING) || FS->l1 == StSelection)
559     {
560       m_cut ();
561       return;
562     }
563 
564   if (FS->l1 == StCell && FS->l2 == EDITING)
565     w = Letext;
566 
567   if (!EntryDelChr (w) && FS->l1 == StCell && FS->l2 == EDITING)
568     FS->l3 = MODIFIED;
569   return;
570 }
571 
572 void
cb_BackSpace(Widget w,XEvent * event,String * params,Cardinal * num_params)573 cb_BackSpace (Widget w, XEvent * event, String * params, Cardinal * num_params)
574 {
575   if ((FS->l1 == StCell && FS->l2 != EDITING) || FS->l1 == StSelection)
576     {
577       return;
578     }
579 
580   if (FS->l1 == StCell && FS->l2 == EDITING)
581     w = Letext;
582   if (!EntryBackChr (w) && FS->l1 == StCell && FS->l2 == EDITING)
583     FS->l3 = MODIFIED;
584   return;
585 }
586 
587 void
cb_DocDelete()588 cb_DocDelete ()
589 {
590   stopedit ();
591   m_cut ();
592 }
593 
594 
595 
596 
597 void
cb_Shift(int shift)598 cb_Shift (int shift)
599 {
600   Shift = shift;
601 
602 }
603 void
cb_Control(int control)604 cb_Control (int control)
605 {
606   Control = control;
607 }
608 
609 void
cb_PageHome(Widget w,XEvent * event,String * params,Cardinal * num_params)610 cb_PageHome (Widget w, XEvent * event, String * params, Cardinal * num_params)
611 {
612   settopleft (1, 1);
613   if (!(FS->l1 == StCell && FS->l2 == EDITING && FS->l3 == MODIFIED))
614     {
615       setcursor (1, 1);
616       FS->l2 = StMotion;
617     }
618 }
619 
620 void
cb_PageUp(Widget w,XEvent * event,String * params,Cardinal * num_params)621 cb_PageUp (Widget w, XEvent * event, String * params, Cardinal * num_params)
622 {
623   int i1, j1, i2, j2;
624   int h;
625   get_visible_cells (&i1, &j1, &i2, &j2);
626   h = i2 - i1 - 1;
627   if (i1 - h < 1)
628     h = i1 + 1;
629   settopleft (i1 - h, -1);
630   if (!(FS->l1 == StCell && FS->l2 == EDITING && FS->l3 == MODIFIED))
631     {
632       movecursor (-h, 0);
633       FS->l2 = StMotion;
634     }
635 }
636 
637 void
cb_PageDown(Widget w,XEvent * event,String * params,Cardinal * num_params)638 cb_PageDown (Widget w, XEvent * event, String * params, Cardinal * num_params)
639 {
640   int i1, j1, i2, j2;
641   get_visible_cells (&i1, &j1, &i2, &j2);
642 
643   settopleft (i2 - 1, -1);
644 
645   if (!(FS->l1 == StCell && FS->l2 == EDITING && FS->l3 == MODIFIED))
646     {
647       movecursor ((i2 - i1 - 1), 0);
648       FS->l2 = StMotion;
649     }
650 }
651 
652 void
cb_PageLeft(Widget w,XEvent * event,String * params,Cardinal * num_params)653 cb_PageLeft (Widget w, XEvent * event, String * params, Cardinal * num_params)
654 {
655   int i1, j1, i2, j2;
656   int h;
657   if (FS->l1 == StCell && FS->l2 == EDITING && FS->l3 == MODIFIED)
658     return;
659 
660   get_visible_cells (&i1, &j1, &i2, &j2);
661   h = j2 - j1;
662   j1 -= h + 1;
663   if (j1 < 1)
664     j1 = 1;
665 
666   settopleft (-1, j1);
667   movecursor (0, -(j2 - j1 - 2));
668   FS->l2 = StMotion;
669 }
670 
671 void
cb_PageRight(Widget w,XEvent * event,String * params,Cardinal * num_params)672 cb_PageRight (Widget w, XEvent * event, String * params, Cardinal * num_params)
673 {
674   int i1, j1, i2, j2;
675   if (FS->l1 == StCell && FS->l2 == EDITING && FS->l3 == MODIFIED)
676     return;
677 
678   get_visible_cells (&i1, &j1, &i2, &j2);
679 
680   settopleft (-1, j2 - 1);
681   movecursor (0, (j2 - j1 - 2));
682   FS->l2 = StMotion;
683 
684 }
685 
686 
687 
688 
689 
690 
691 void
cb_DocMotion(Widget w,XEvent * event,String * params,Cardinal * num_params)692 cb_DocMotion (Widget w, XEvent * event, String * params, Cardinal * num_params)
693 {
694   int x, y, absx, absy;
695   int xmin, xmax, ymin, ymax;
696   int i1, i2, j1, j2, k;
697 
698 
699 
700 
701   if (FS->l1 == StCell && FS->l2 == EDITING)
702     return;
703 
704 
705   x = event->xbutton.x * 100 / AW->zoom;
706   y = event->xbutton.y * 100 / AW->zoom;
707   absx = x + CW (AW, AW->cc);
708   absy = y + LH (AW, AW->ll);
709 
710 
711   if (FS->l1 == StDrawing)
712     {
713       Drawing *dr;
714       movex = x;
715       movey = y;
716       absmovex = movex + CW (AW, AW->cc);
717       absmovey = movey + LH (AW, AW->ll);
718 
719       if (FS->l2 == READYEDITDRAWING)
720 	{
721 	  for (k = 0; k < getnumdra (); k++)
722 	    {
723 	      dr = (Drawing *) getdra (k);
724 	      if (drawing_pointed (dr, absmovex, absmovey, 5, 5) && ActiveDrawing != dr)
725 		{
726 		  ActivateDrawing (dr);
727 		  worksheet_select_drawing (AW, dr);
728 		  clearnflush ();
729 		  drawselect ();
730 		}
731 	    }
732 	}
733       return;
734     }
735 
736 
737   if (FS->l1 == StChart)
738     {
739       if (ActiveGraph == NULL)
740 	return;
741       xa = ActiveGraph->x;
742       ya = ActiveGraph->y;
743       xb = xa + ActiveGraph->w;
744       yb = ya + ActiveGraph->h;
745       FS->l4 = TurnOnLimitCursor (Drawarea, xa, ya, xb, yb, absx, absy);
746       StDebug ("cursor set for chart");
747       return;
748     }
749 
750 
751   if (w == colindex)
752     {
753       TurnOnHand (colindex);
754       return;
755     }
756   if (w == linindex)
757     {
758       TurnOnHand (linindex);
759       return;
760     }
761   if (w == corner)
762     {
763       TurnOnHand (corner);
764       return;
765     }
766 
767 
768 
769   if (FS->l1 == StBrush)
770     return;
771 
772   if (FS->l1 == StUndefined)
773     {
774       state_reset_levels ();
775       get_select (&i1, &j1, &i2, &j2);
776       if (i1 == 0 && j1 == 0)
777 	FS->l1 = StCell;
778       else
779 	FS->l1 = StSelection;
780     }
781 
782 
783   get_select (&i1, &j1, &i2, &j2);
784   if (i1 == 0 && j1 == 0 && ActiveCell != NULL)
785     {
786       i1 = i2 = ActiveCell->r;
787       j1 = j2 = ActiveCell->c;
788     }
789 
790   ijtoxy (i1, j1, &xmin, &ymin);
791   ijtoxy (i2 + 1, j2 + 1, &xmax, &ymax);
792 
793   if ((FS->l1 == StCell || FS->l1 == StSelection))
794     {
795       if (xmin < x && x < xmax && ymin - 3 < y && y < ymin + 3)
796 	{
797 	  if (FS->l2 != HAND)
798 	    {
799 	      TurnOnHand (Drawarea);
800 	      FS->l2 = HAND;
801 	    }
802 	  return;
803 	}
804 
805       if (FS->l2 == HAND)
806 	{
807 	  TurnOffCursor (Drawarea);
808 	  FS->l2 = 0;
809 	}
810 
811       if (xmax - 3 < x && x < xmax + 3 && ymax - 3 < y && y < ymax + 3)
812 	{
813 	  if (FS->l2 != EXTEND_SELECT)
814 	    {
815 	      TurnOnBottomRight (Drawarea);
816 	      FS->l2 = EXTEND_SELECT;
817 	    }
818 	  return;
819 	}
820 
821       if (FS->l2 == EXTEND_SELECT)
822 	{
823 	  TurnOffCursor (Drawarea);
824 	  FS->l2 = 0;
825 	}
826     }
827 
828 }
829 
830 
831 
832 void
cb_DocBtn1Down(Widget w,XEvent * event,String * params,Cardinal * num_params)833 cb_DocBtn1Down (Widget w, XEvent * event, String * params, Cardinal * num_params)
834 {
835   int i, j, k;
836   int x, y;
837   Graph *gr;
838   Button *btn;
839 
840   x = event->xbutton.x * 100 / AW->zoom;
841   y = event->xbutton.y * 100 / AW->zoom;
842 
843   downx = x;
844   downy = y;
845   absdownx = downx + CW (AW, AW->cc);
846   absdowny = downy + LH (AW, AW->ll);
847 
848   FS->downx = x;
849   FS->downy = y;
850   StDebug ("Btn1Down");
851 
852   xytoij (downx, downy, &i, &j);
853 
854   downi = i;
855   downj = j;
856   lasti = i;
857   lastj = j;
858 
859   BTN1_STATE = DOWN;
860 
861   if (FS->l1 == StDrawing)
862     {
863       if (w != Drawarea)
864 	return;
865 
866       if (FS->l2 == READYEDITDRAWING && ActiveDrawing != NULL)
867 	{
868 	  FS->l2 = EDITINGDRAWING;
869 	  lx = x;
870 	  ly = y;
871 	  if (abs (ActiveDrawing->x1 - absdownx) <= 5 && abs (ActiveDrawing->y1 - absdowny) <= 5)
872 	    {
873 	      TurnOnTopLeft (Drawarea);
874 	      FS->l3 = TOP_LEFT;
875 	    }
876 	  else if (abs (ActiveDrawing->x2 - absdownx) <= 5 && abs (ActiveDrawing->y2 - absdowny) <= 5)
877 	    {
878 	      TurnOnBottomRight (Drawarea);
879 	      FS->l3 = BOTTOM_RIGHT;
880 	    }
881 	  else
882 	    {
883 	      TurnOnMove (Drawarea);
884 	      FS->l3 = CROSS;
885 	    }
886 	}
887       return;
888     }
889 
890 
891   if (w == colindex)
892     {
893       stopedit ();
894       FS->l1 = StBorder;
895       FS->l2 = StColindex;
896       FS->l3 = StDown;
897 
898       xytoclotherij (downx, downy, &i, &j);
899       cj = j;
900       lx = downx;
901 
902       return;
903     }
904   if (w == linindex)
905     {
906       stopedit ();
907       FS->l1 = StBorder;
908       FS->l2 = StLinindex;
909       FS->l3 = StDown;
910 
911       xytoclotherij (downx, downy, &i, &j);
912       li = i;
913       ly = downy;
914 
915       return;
916     }
917   if (w == corner)
918     {
919       stopedit ();
920       FS->l1 = StBorder;
921       FS->l2 = StCorner;
922       FS->l3 = StDown;
923 
924 
925       return;
926     }
927 
928 
929 
930   if (FS->l1 == StChart)
931     {
932       if (FS->l2 == CREATE_CHART)
933 	{
934 	  FS->l3 = StDown;
935 	  return;
936 	}
937       if (FS->l2 == SELECT_CHART)
938 	{
939 	  if (FS->l4 == BOTTOM_LEFT ||
940 	      FS->l4 == BOTTOM_RIGHT ||
941 	      FS->l4 == TOP_RIGHT ||
942 	      FS->l4 == TOP_SIDE ||
943 	      FS->l4 == RIGHT_SIDE ||
944 	      FS->l4 == BOTTOM_SIDE ||
945 	      FS->l4 == LEFT_SIDE ||
946 	      FS->l4 == TOP_LEFT)
947 	    {
948 	      lx = downx;
949 	      ly = downy;
950 	      return;
951 	    }
952 
953 	  gr = worksheet_getchart_at (ActiveWorksheet, absdownx, absdowny);
954 	  if (gr == ActiveGraph)
955 	    {
956 	      TurnOnMove (Drawarea);
957 	      FS->l4 = CMOVE;
958 	      lx = downx;
959 	      ly = downy;
960 	      return;
961 	    }
962 	  else
963 	    {
964 	      worksheet_select_graph (ActiveWorksheet, NULL);
965 	      ActiveGraph = NULL;
966 	      state_reset_levels ();
967 	      copypix ("cb_docbtn1down:after click out of the active chart");
968 	    }
969 	}
970     }
971 
972 
973   if (FS->l1 == StGrap)
974     {
975       copypix ("cb_docbtn1down:StGrap basic state");
976       drawselectbox (downi, downj, downi, downj);
977       FS->l3 = StDown;
978       return;
979     }
980 
981   if (FS->l1 == StBrush)
982     {
983       copypix ("cb_docbtn1down:StBrush basic state");
984       drawselectbox (downi, downj, downi, downj);
985       FS->l3 = StDown;
986       return;
987     }
988 
989 
990 
991   if ((FS->l1 == StCell || FS->l1 == StSelection) && FS->l2 == HAND)
992     {
993       FS->l3 = StDown;
994       return;
995     }
996 
997   if (FS->l1 == StCell && FS->l2 == EXTEND_SELECT)
998     {
999       FS->l3 = StDown;
1000       FS->downr = ActiveCell->r;
1001       FS->downc = ActiveCell->c;
1002       StDebug ("start extend select");
1003       return;
1004     }
1005 
1006   if (FS->l1 == StSelection && FS->l2 == EXTEND_SELECT)
1007     {
1008       int i1, i2, j1, j2;
1009       get_select (&i1, &j1, &i2, &j2);
1010       if (i1 == i2 && j1 == j2)
1011 	return;
1012 
1013       FS->upr = i1;
1014       FS->upc = j1;
1015       FS->downr = i2;
1016       FS->downc = j2;
1017       FS->l3 = StDown;
1018       return;
1019     }
1020 
1021   if (FS->l1 == StCell && FS->l2 == EDITING && FS->l3 == MODIFIED)
1022     {
1023       FS->l1 = StGrap;
1024       FS->l2 = StCell;
1025       FS->l3 = StDown;
1026       copypix ("cb_docbtn1down:StCell, Editing, modified");
1027       drawselectbox (downi, downj, downi, downj);
1028       return;
1029     }
1030 
1031   if (FS->l1 == StCell && FS->l2 == EDITING && FS->l3 != MODIFIED)
1032     {
1033       a_update ();
1034     }
1035 
1036 
1037   gr = worksheet_getchart_at (ActiveWorksheet, absdownx, absdowny);
1038   if (gr != NULL)
1039     {
1040       lx = downx;
1041       ly = downy;
1042 
1043       if (ActiveGraph != gr || !(FS->l1 == StChart && FS->l2 == SELECT_CHART))
1044 	{
1045 	  worksheet_select_graph (AW, gr);
1046 	  ActiveGraph = gr;
1047 	  grx = gr->x;
1048 	  gry = gr->y;
1049 	  grh = gr->h;
1050 	  grw = gr->w;
1051 	  FS->l1 = StChart;
1052 	  FS->l2 = SELECT_CHART;
1053 	  FS->l3 = StDown;
1054 	  FS->l4 = CMOVE;
1055 	  TurnOnMove (Drawarea);
1056 	}
1057       return;
1058     }
1059 
1060 
1061   if (FS->l1 == StUndefine ||
1062       FS->l1 == StCell ||
1063       FS->l1 == StBorder ||
1064       FS->l1 == StSelection ||
1065       FS->l1 == StChart)
1066     {
1067       for (k = 0; k < getnumbtn (); k++)
1068 	{
1069 	  btn = (Button *) getbtn (k);
1070 
1071 	  if (downx > btn->x1 - CW (AW, AW->cc) && downy > btn->y1 - LH (AW, AW->ll) &&
1072 	      downx < btn->x2 - CW (AW, AW->cc) && downy < btn->y2 - LH (AW, AW->ll)
1073 	    )
1074 	    {
1075 
1076 	      lx = downx;
1077 	      ly = downy;
1078 	      state_reset_levels ();
1079 	      FS->l1 = StButton;
1080 	      FS->l2 = StDown;
1081 	      StDebug ("buttondown");
1082 	      setactivebtn (k);
1083 	      redrawbtdown (btn);
1084 	      copypix ("cb_btn1down:redrawbtdown");
1085 	      return;
1086 	    }
1087 	}
1088     }
1089 
1090 
1091 
1092   if (Shift)
1093     {
1094       FS->l2 = StShift;
1095       FS->l3 = StDown;
1096       StDebug ("shift btn1 down");
1097       return;
1098     }
1099   else
1100 
1101     {
1102       FS->l1 = StCell;
1103     }
1104   FS->l2 = 0;
1105   FS->l3 = StDown;
1106 }
1107 
1108 
1109 void
cb_DocBtn1Motion(Widget w,XEvent * event,String * params,Cardinal * num_params)1110 cb_DocBtn1Motion (Widget w, XEvent * event, String * params, Cardinal * num_params)
1111 {
1112   int i, j;
1113   int x, y;
1114   int dx, dy;
1115   Drawing *dr;
1116 
1117   x = event->xbutton.x * 100 / AW->zoom;
1118   y = event->xbutton.y * 100 / AW->zoom;
1119 
1120   movex = x;
1121   movey = y;
1122   absmovex = movex + CW (AW, AW->cc);
1123   absmovey = movey + LH (AW, AW->ll);
1124   xytoij (x, y, &i, &j);
1125   movei = i;
1126   movej = j;
1127 
1128   FS->movex = x;
1129   FS->movey = y;
1130   StDebug ("Btn1Motion");
1131 
1132 
1133 
1134   if (Shift)
1135     return;
1136 
1137 
1138   if (FS->l1 == StDrawing)
1139     {
1140       if (w != Drawarea)
1141 	return;
1142       if (FS->l2 == STARTCREATEDRAWING)
1143 	FS->l2 = CREATINGDRAWING;
1144 
1145       if (FS->l2 == CREATINGDRAWING)
1146 	{
1147 	  set_visible (i, j);
1148 	  drplot1 ();
1149 	  return;
1150 	}
1151 
1152       if (FS->l2 == EDITINGDRAWING)
1153 	{
1154 	  set_visible (i, j);
1155 	  dx = x - lx;
1156 	  dy = y - ly;
1157 
1158 	  if (FS->l3 == CROSS)
1159 	    {
1160 	      dr = ActiveDrawing;
1161 	      dr->x1 += dx;
1162 	      dr->y1 += dy;
1163 	      dr->x2 += dx;
1164 	      dr->y2 += dy;
1165 	    }
1166 	  else if (FS->l3 == BOTTOM_RIGHT)
1167 	    {
1168 	      dr = ActiveDrawing;
1169 	      dr->x2 += dx;
1170 	      dr->y2 += dy;
1171 	    }
1172 	  else if (FS->l3 == TOP_LEFT)
1173 	    {
1174 	      dr = ActiveDrawing;
1175 	      dr->x1 += dx;
1176 	      dr->y1 += dy;
1177 	    }
1178 	  lx = x;
1179 	  ly = y;
1180 	  drplot1 ();
1181 	  copypix ("cb_docbtn1motion: modification d un dessin");
1182 	}
1183       return;
1184     }
1185 
1186 
1187   if (FS->l1 == StBorder)
1188     {
1189       FS->l3 = StMotion;
1190       switch (FS->l2)
1191 	{
1192 	case StLinindex:
1193 	  {
1194 	    if ((ly - y) * (ly - y) < 9)
1195 	      break;
1196 	    dy = y - ly;
1197 	    ly = y;
1198 
1199 	    if (dy < 0 && LH (AW, li) + dy - LH (AW, li - 1) < 10)
1200 	      break;
1201 
1202 	    SH (AW, li - 1, LH (AW, li) + dy - LH (AW, li - 1));
1203 	    border_repaint (1);
1204 	    break;
1205 	  };
1206 
1207 	case StColindex:
1208 	  {
1209 	    if ((lx - x) * (lx - x) < 9)
1210 	      break;
1211 	    dx = x - lx;
1212 	    lx = x;
1213 
1214 	    if (dx < 0 && CW (AW, cj) + dx - CW (AW, cj - 1) < 10)
1215 	      break;
1216 
1217 	    SW (AW, cj - 1, CW (AW, cj) + dx - CW (AW, cj - 1));
1218 	    border_repaint (1);
1219 	    break;
1220 	  }
1221 	}
1222 
1223 
1224       lasti = i;
1225       lastj = j;
1226 
1227       return;
1228     }
1229 
1230 
1231   if (FS->l1 == StChart)
1232     {
1233       if (FS->l2 == CREATE_CHART)
1234 	{
1235 	  FS->l3 = StMotion;
1236 	  set_visible (i, j);
1237 	  drplot ();
1238 	}
1239       if (FS->l2 == SELECT_CHART)
1240 	{
1241 	  set_visible (i, j);
1242 	  dx = x - lx;
1243 	  dy = y - ly;
1244 
1245 	  if (FS->l4 == CMOVE)
1246 	    {
1247 	      grx += dx;
1248 	      gry += dy;
1249 	      lx = x;
1250 	      ly = y;
1251 	      copypix ("cb_btn1motion: StChart: CMOVE");
1252 	      return;
1253 	    }
1254 	  if (FS->l4 == BOTTOM_LEFT)
1255 	    {
1256 	      grx += dx;
1257 	      grw -= dx;
1258 	      grh += dy;
1259 	    }
1260 	  if (FS->l4 == BOTTOM_RIGHT)
1261 	    {
1262 	      grw += dx;
1263 	      grh += dy;
1264 	    }
1265 	  if (FS->l4 == TOP_LEFT)
1266 	    {
1267 	      grx += dx;
1268 	      gry += dy;
1269 	      grw -= dx;
1270 	      grh -= dy;
1271 	    }
1272 	  if (FS->l4 == TOP_RIGHT)
1273 	    {
1274 	      gry += dy;
1275 	      grw += dx;
1276 	      grh -= dy;
1277 	    }
1278 	  if (FS->l4 == LEFT_SIDE)
1279 	    {
1280 	      grx += dx;
1281 	      grw -= dx;
1282 	    }
1283 	  if (FS->l4 == RIGHT_SIDE)
1284 	    {
1285 	      grw += dx;
1286 	    }
1287 	  if (FS->l4 == TOP_SIDE)
1288 	    {
1289 	      gry += dy;
1290 	      grh -= dy;
1291 	    }
1292 	  if (FS->l4 == BOTTOM_SIDE)
1293 	    {
1294 	      grh += dy;
1295 	    }
1296 	  if (grw < 50)
1297 	    grw = 50;
1298 	  if (grh < 40)
1299 	    grh = 40;
1300 	  lx = x;
1301 	  ly = y;
1302 	  copypix ("cb_docbnt1motion:chart size modified");
1303 	}
1304 
1305     }
1306 
1307 
1308 
1309   if (FS->l1 == StButton)
1310     return;
1311 
1312 
1313   if (FS->l1 == StGrap || FS->l1 == StBrush)
1314     {
1315       int di = i - lasti;
1316       int dj = j - lastj;
1317       FS->l3 = StMotion;
1318 
1319       if (!(di) && !(dj))
1320 	return;
1321 
1322       xytoij (x, y, &i, &j);
1323       if (i != 0 && j != 0)
1324 	set_visible (i, j);
1325       copypix ("cb_docbtn1motion: grap or brush, after set_visible");
1326       drawselectbox (downi, downj, i, j);
1327       return;
1328     }
1329 
1330   if (FS->l1 == StSelection && FS->l2 == HAND)
1331     {
1332       int i1, j1, i2, j2;
1333       int di = i - lasti;
1334       int dj = j - lastj;
1335       if (FS->l3 == StDown)
1336 	TurnOnMove (Drawarea);
1337       FS->l3 = StMotion;
1338       if (!(di) && !(dj))
1339 	return;
1340       copypix ("cb_docbtn1motion: moving a selection");
1341 
1342       getselection (&i1, &j1, &i2, &j2);
1343       di = i - downi;
1344       dj = j - downj;
1345       set_visible (i, j);
1346       drawselectbox (i1 + di, j1 + dj, i2 + di, j2 + dj);
1347       lasti = i;
1348       lastj = j;
1349       return;
1350     }
1351   if (FS->l1 == StCell && FS->l2 == HAND)
1352     {
1353       int i1, j1;
1354       int di = i - lasti;
1355       int dj = j - lastj;
1356       if (FS->l3 == StDown)
1357 	TurnOnMove (Drawarea);
1358       FS->l3 = StMotion;
1359       if (!(di) && !(dj))
1360 	return;
1361       copypix ("cb_docbtn1motion: moving a cell");
1362 
1363       i1 = ActiveCell->r;
1364       j1 = ActiveCell->c;
1365       di = i - i1;
1366       dj = j - j1;
1367       set_visible (i, j);
1368       drawselectbox (i1 + di, j1 + dj, i1 + di, j1 + dj);
1369       lasti = i;
1370       lastj = j;
1371       return;
1372     }
1373   if ((FS->l1 == StCell || FS->l1 == StSelection) && (FS->l2 == 0 || FS->l2 == SELECTING))
1374     {
1375       if (FS->l3 == StDown)
1376 	{
1377 	  setcursor (downi, downj);
1378 	  FS->l3 = StMotion;
1379 	  lasti = downi;
1380 	  lastj = downj;
1381 	}
1382       else
1383 	{
1384 	  int di;
1385 	  int dj;
1386 	  xytoij (x, y, &i, &j);
1387 	  di = i - lasti;
1388 	  dj = j - lastj;
1389 
1390 	  if (di || dj)
1391 	    {
1392 	      FS->l2 = SELECTING;
1393 	      FS->l1 = StSelection;
1394 	      if (i != 0 && j != 0)
1395 		setselection (downi, downj, i, j);
1396 	      copypix ("cb_docbtn1motion:after setselection");
1397 	      set_visible (i, j);
1398 
1399 	    }
1400 	  lasti = i;
1401 	  lastj = j;
1402 	}
1403       return;
1404     }
1405 
1406   if ((FS->l1 == StCell || FS->l1 == StSelection) && FS->l2 == EXTEND_SELECT)
1407     {
1408       int i1, j1, i2, j2, di, dj;
1409       di = i - FS->downr;
1410       dj = j - FS->downc;
1411 
1412       if (FS->l3 == StDown)
1413 	{
1414 	  FS->mover = FS->downr;
1415 	  FS->movec = FS->downc;
1416 	  lasti = 0;
1417 	  lastj = 0;
1418 	  if (FS->movey - FS->downy > 3)
1419 	    {
1420 	      FS->l4 = StDown;
1421 	      FS->l3 = StMotion;
1422 	    }
1423 	  if (FS->movex - FS->downx > 3)
1424 	    {
1425 	      FS->l4 = StRight;
1426 	      FS->l3 = StMotion;
1427 	    }
1428 	}
1429 
1430       if (FS->l3 == StMotion)
1431 	{
1432 	  if (FS->l4 == StDown && di <= 0)
1433 	    {
1434 	      di = 0;
1435 	      FS->mover = FS->downr;
1436 	      FS->l4 = StFree;
1437 	    };
1438 
1439 	  if (FS->l4 == StRight && dj <= 0)
1440 	    {
1441 	      dj = 0;
1442 	      FS->movec = FS->downc;
1443 	      FS->l4 = StFree;
1444 	    };
1445 
1446 	  if (FS->l4 == StFree)
1447 	    {
1448 	      if (di > 0)
1449 		{
1450 		  FS->l4 = StDown;
1451 		}
1452 	      else if (dj > 0)
1453 		{
1454 		  FS->l4 = StRight;
1455 		}
1456 	    }
1457 
1458 	  if (FS->l4 == StDown)
1459 	    {
1460 	      i2 = i1 + di;
1461 	      FS->mover = i;
1462 	    }
1463 	  else if (FS->l4 == StRight)
1464 	    {
1465 	      j2 = j1 + dj;
1466 	      FS->movec = j;
1467 	    }
1468 	  if (i != lasti || j != lastj)
1469 	    {
1470 	      set_visible (FS->mover, FS->movec);
1471 	      copypix ("cb_docbtn1motion: extend selection");
1472 	      if (FS->l1 == StCell)
1473 		drawselectbox (FS->downr, FS->downc, FS->mover, FS->movec);
1474 	      if (FS->l1 == StSelection)
1475 		drawselectbox (FS->upr, FS->upc, FS->mover, FS->movec);
1476 	    }
1477 	}
1478       lasti = i;
1479       lastj = j;
1480 
1481       return;
1482     }
1483 
1484 }
1485 
1486 
1487 void
cb_DocBtn1Up(Widget w,XEvent * event,String * params,Cardinal * num_params)1488 cb_DocBtn1Up (Widget w, XEvent * event, String * params, Cardinal * num_params)
1489 {
1490   char *label;
1491   char buf[64];
1492   char buf1[64];
1493   int x, y;
1494   int i, j;
1495   Graph *gr;
1496   Button *btn;
1497 
1498   FS->upx = event->xbutton.x * 100 / AW->zoom;
1499   FS->upy = event->xbutton.y * 100 / AW->zoom;
1500   StDebug ("Btn1Up");
1501 
1502   if (1)
1503     {
1504       x = event->xbutton.x * 100 / AW->zoom;
1505       y = event->xbutton.y * 100 / AW->zoom;
1506       upx = x;
1507       upy = y;
1508       absupx = upx + CW (AW, AW->cc);
1509       absupy = upy + LH (AW, AW->ll);
1510 
1511       xytoij (x, y, &i, &j);
1512       upi = i;
1513       upj = j;
1514     }
1515 
1516 
1517   if (FS->l1 == StDrawing)
1518     {
1519       if (FS->l2 == EDITINGDRAWING)
1520 	{
1521 	  FS->l2 = READYEDITDRAWING;
1522 	  TurnOnPencil (Drawarea);
1523 	  xrepaint ("btn1up: editing drawing");
1524 	  return;
1525 	}
1526 
1527       if (FS->l2 == CREATINGDRAWING)
1528 	{
1529 	  FS->l2 = ENDCREATEDRAWING;
1530 	  drplot1 ();
1531 	  state_reset_levels ();
1532 	  return;
1533 	}
1534       FS->l2 = READYEDITDRAWING;
1535       TurnOnPencil (Drawarea);
1536       return;
1537     }
1538 
1539 
1540   if (FS->l1 == StBorder)
1541     {
1542       switch (FS->l2)
1543 	{
1544 	case StLinindex:
1545 	  {
1546 	    if (FS->l3 == StDown && w == linindex)
1547 	      {
1548 		setcursor (i, 1);
1549 		selectlin (i, Shift);
1550 		state_reset_levels ();
1551 		FS->l1 = StSelection;
1552 		drawselect ();
1553 		li = 0;
1554 		cj = 0;
1555 		return;
1556 	      }
1557 	    else
1558 	      {
1559 		xrepaint ("btn1up StBorder");
1560 		cj = 0;
1561 		li = 0;
1562 		return;
1563 	      }
1564 	  }
1565 	case StColindex:
1566 	  {
1567 	    if (FS->l3 == StDown && w == colindex)
1568 	      {
1569 		setcursor (1, j);
1570 		selectcol (j, Shift);
1571 		state_reset_levels ();
1572 		FS->l1 = StSelection;
1573 		drawselect ();
1574 		li = 0;
1575 		cj = 0;
1576 		return;
1577 	      }
1578 	    else
1579 	      {
1580 		xrepaint ("Btn1up StBorder");
1581 		cj = 0;
1582 		li = 0;
1583 		return;
1584 	      }
1585 	  }
1586 	case StCorner:
1587 	  {
1588 	    if (FS->l3 == StDown && w == corner)
1589 	      {
1590 		setcursor (1, 1);
1591 		selectall ();
1592 		state_reset_levels ();
1593 		FS->l1 = StSelection;
1594 		drawselect ();
1595 		li = 0;
1596 		cj = 0;
1597 		return;
1598 	      }
1599 	    else
1600 	      {
1601 		xrepaint ("btn1up Stcorner");
1602 		cj = 0;
1603 		li = 0;
1604 		return;
1605 	      }
1606 	  }
1607 	}
1608       state_reset_levels ();
1609       return;
1610     }
1611 
1612 
1613   if (FS->l1 == StChart)
1614     {
1615       if (FS->l2 == CREATE_CHART)
1616 	{
1617 	  TurnOffCursor (Drawarea);
1618 	  FS->l2 = END_CREATE_CHART;
1619 	  FS->l3 = StUp;
1620 	  drplot ();
1621 	  state_reset_levels ();
1622 	}
1623 
1624       if (FS->l2 == SELECT_CHART)
1625 	{
1626 	  StDebug ("UP - SELECT_CHART");
1627 	  if (FS->l4 == CMOVE)
1628 	    {
1629 	      gr = ActiveGraph;
1630 	      gr->x = grx;
1631 	      gr->y = gry;
1632 	      gr->h = grh;
1633 	      gr->w = grw;
1634 	      plot ();
1635 	      TurnOffCursor (Drawarea);
1636 	      FS->l4 = 0;
1637 	      xrepaint ("btn1up:  StChart");
1638 	    }
1639 	  if (FS->l4 == BOTTOM_LEFT ||
1640 	      FS->l4 == BOTTOM_RIGHT ||
1641 	      FS->l4 == TOP_RIGHT ||
1642 	      FS->l4 == TOP_SIDE ||
1643 	      FS->l4 == RIGHT_SIDE ||
1644 	      FS->l4 == BOTTOM_SIDE ||
1645 	      FS->l4 == LEFT_SIDE ||
1646 	      FS->l4 == TOP_LEFT)
1647 	    {
1648 	      gr = ActiveGraph;
1649 	      gr->x = grx;
1650 	      gr->y = gry;
1651 	      gr->h = grh;
1652 	      gr->w = grw;
1653 	      resizegra ();
1654 	      plot ();
1655 	      xrepaint ("btn1up: StChart 2");
1656 	    }
1657 	  FS->l3 = StUp;
1658 	}
1659       return;
1660     }
1661 
1662 
1663 
1664   if (FS->l1 == StButton)
1665     {
1666       FS->l2 = StUp;
1667       StDebug ("buttonup");
1668 
1669       btn = ActiveButton;
1670       if (btn == NULL)
1671 	return;
1672 
1673       if (upx > btn->x1 - CW (AW, AW->cc) && upy > btn->y1 - LH (AW, AW->ll) &&
1674 	  upx < btn->x2 - CW (AW, AW->cc) && upy < btn->y2 - LH (AW, AW->ll)
1675 	)
1676 	{
1677 	  button_click (btn);
1678 	}
1679       redrawbt (btn);
1680       copypix ("cb_docbtn1up: after redrawbtup");
1681       ActiveButton = NULL;
1682       state_reset_levels ();
1683       return;
1684     }
1685 
1686 
1687   if (FS->l1 == StGrap)
1688     {
1689       numtoalpha (buf, downi, downj);
1690       if (TEXT_WIDGET == Letext)
1691 	{
1692 	  if (ActiveCell != NULL)
1693 	    {
1694 	      if (ActiveCell->worksheet != NULL)
1695 		{
1696 		  if (EditedCell != NULL)
1697 		    {
1698 		      if (ActiveWorksheet != EditedCell->worksheet)
1699 			{
1700 			  if (strchr (ActiveWorksheet->Name, ' '))
1701 			    {
1702 			      sprintf (buf1, "\'%s\'!%s", ActiveWorksheet->Name, buf);
1703 			    }
1704 			  else
1705 			    {
1706 			      sprintf (buf1, "%s!%s", ActiveWorksheet->Name, buf);
1707 			    }
1708 			  ActivateWorksheet (EditedCell->worksheet);
1709 			  EditedCell = NULL;
1710 			  insertstring (Letext, buf1);
1711 			  KeyPressed (-9);
1712 			  xdrawcursor (0, 0);
1713 			  copypix ("btn1up : StGrap:after xdrawcursor");
1714 			}
1715 		      else
1716 
1717 			{
1718 			  insertstring (Letext, buf);
1719 			  KeyPressed (-4);
1720 			}
1721 		    }
1722 		  else
1723 
1724 		    {
1725 		      insertstring (Letext, buf);
1726 		      KeyPressed (-5);
1727 		    }
1728 		}
1729 	    }
1730 	}
1731       else
1732 
1733 	{
1734 	  if (DIALOG_WIDGET != NULL)
1735 	    XtManageChild (DIALOG_WIDGET);
1736 	  sprintf (buf1, "=%s", buf);
1737 	  SetEntryString (TEXT_WIDGET, buf1);
1738 	}
1739 
1740       if (downi != i || downj != j)
1741 	{
1742 	  numtoalpha (buf1, i, j);
1743 
1744 	  if (TEXT_WIDGET != Letext)
1745 	    {
1746 	      sprintf (buf, "%s:%s", buf, buf1);
1747 	      SetEntryString (TEXT_WIDGET, buf);
1748 	    }
1749 	  else
1750 	    {
1751 	      sprintf (buf, ":%s", buf1);
1752 	      insertstring (Letext, buf);
1753 	      KeyPressed (-6);
1754 	    }
1755 	}
1756       copypix ("cb_docbtn1up: after text inserted by Stgrap");
1757       TEXT_WIDGET = Letext;
1758 
1759       BTN1_WHAT = CB_CELL;
1760       BTN1_STATE = UP;
1761 
1762       switch (FS->l2)
1763 	{
1764 	case StChart:
1765 	  {
1766 	    FS->l1 = StChart;
1767 	    FS->l2 = SELECT_CHART;
1768 	    FS->l3 = StUp;
1769 	    break;
1770 	  }
1771 	case StCell:
1772 	  {
1773 	    FS->l1 = StCell;
1774 	    FS->l2 = EDITING;
1775 	    FS->l3 = MODIFIED;
1776 	    break;
1777 	  }
1778 	default:
1779 	  {
1780 	    state_reset_levels ();
1781 	    break;
1782 	  }
1783 	}
1784       return;
1785     }
1786 
1787   if (FS->l1 == StBrush)
1788     {
1789       makebrush (downi, downj, i, j);
1790       TurnOffCursor (Drawarea);
1791       switch (FS->l2)
1792 	{
1793 	case StCell:
1794 	  {
1795 	    FS->l1 = StCell;
1796 	  }
1797 	case StSelection:
1798 	  {
1799 	    FS->l1 = StSelection;
1800 	  }
1801 	default:
1802 	  {
1803 	    state_reset_levels ();
1804 	  }
1805 	}
1806       return;
1807     }
1808 
1809 
1810 
1811   if (FS->l1 == StSelection && FS->l2 == HAND)
1812     {
1813       FS->l3 = StUp;
1814       TurnOffMove (Drawarea);
1815       if (downi - i || downj - j)
1816 	{
1817 	  fprintf (stderr, "downi %d downj %d i%d j %d\n", downi, downj, i, j);
1818 	  moveselect (downi, downj, i, j, 1);
1819 	  movecursor (i - downi, j - downj);
1820 
1821 	  upi += i - downi;
1822 	  upj += j - downj;
1823 	  downi = i;
1824 	  downj = j;
1825 	  state_reset_levels ();
1826 	  FS->l1 = StSelection;
1827 	  return;
1828 	}
1829     }
1830   if (FS->l1 == StCell && FS->l2 == HAND)
1831     {
1832       FS->l3 = StUp;
1833       TurnOffMove (Drawarea);
1834       downi = ActiveCell->r;
1835       downj = ActiveCell->c;
1836       if (downi - i || downj - j)
1837 	{
1838 	  moveselect (downi, downj, i, j, 1);
1839 	  movecursor (i - downi, j - downj);
1840 
1841 	  upi += i - downi;
1842 	  upj += j - downj;
1843 	  downi = i;
1844 	  downj = j;
1845 	  state_reset_levels ();
1846 	  FS->l1 = StCell;
1847 	  return;
1848 	}
1849     }
1850 
1851   if ((FS->l1 == StCell || FS->l1 == StSelection) && FS->l2 == SELECTING)
1852     {
1853       FS->l1 = StSelection;
1854       FS->l2 = 0;
1855       FS->l3 = 0;
1856       return;
1857     }
1858 
1859   if ((FS->l1 == StCell || FS->l1 == StSelection) && FS->l2 == EXTEND_SELECT)
1860     {
1861 
1862       TurnOffCursor (Drawarea);
1863 
1864       if (FS->l1 == StCell)
1865 	setselection (FS->downr, FS->downc, FS->mover, FS->movec);
1866       if (FS->l1 == StSelection)
1867 	setselection (FS->upr, FS->upc, FS->mover, FS->movec);
1868 
1869       if (FS->l4 == StDown)
1870 	{
1871 	  copyd ();
1872 	}
1873       else if (FS->l4 == StRight)
1874 	{
1875 	  copyr ();
1876 	}
1877       FS->l4 = 0;
1878       FS->l3 = 0;
1879       FS->l2 = 0;
1880       FS->l1 = StSelection;
1881       copypix ("cb_docbtn1up: extend select");
1882 
1883       return;
1884     }
1885 
1886   if ((FS->l1 == StCell || FS->l1 == StSelection) && FS->l2 == StShift)
1887     {
1888       int i1, j1, i2, j2;
1889       extendselection (downi, downj);
1890 
1891       get_select (&i1, &j1, &i2, &j2);
1892 
1893       state_reset_levels ();
1894       if (i1 == 0 && j1 == 0)
1895 	FS->l1 = StCell;
1896       else
1897 	FS->l1 = StSelection;
1898       copypix ("cb_docbtn1up: extend select Shift");
1899       return;
1900     }
1901   ijtoxy (i, j, &x, &y);
1902 
1903   if (w == Drawarea)
1904     {
1905       label = (char *) get_formula (i, j);
1906       if (label != NULL)
1907 	{
1908 	  SetEntryString (Letext, label);
1909 	  SetEntryString (ActiveMainwin->cell, label);
1910 	}
1911       else
1912 	{
1913 	  SetEntryString (Letext, "");
1914 	  SetEntryString (ActiveMainwin->cell, label);
1915 	}
1916 
1917       left = 0;
1918       set_visible (i, j);
1919       if (FS->l1 == StSelection || FS->l1 == StChart)
1920 	{
1921 	  FS->l1 = StCell;
1922 	  resetselect (0, 0, 0);
1923 	  xrepaint ("btn1up StCell");
1924 	}
1925       FS->l1 = StCell;
1926       if (AW->cur_r == i && AW->cur_c == j && (FS->l1 == StCell && FS->l2 != EDITING))
1927 	{
1928 	  LetextGetFocus ();
1929 	  FS->l2 = EDITING;
1930 	  FS->l3 = MODIFIED;
1931 	  storetopleft ();
1932 	  KeyPressed (-8);
1933 	}
1934       else
1935 	{
1936 	  setcursor (i, j);
1937 	}
1938     }
1939 
1940 }
1941 
1942 
1943 
1944 void
cb_DocBtn2Down(Widget w,XEvent * event,String * params,Cardinal * num_params)1945 cb_DocBtn2Down (Widget w, XEvent * event, String * params, Cardinal * num_params)
1946 {
1947   downx = event->xbutton.x * 100 / AW->zoom;
1948   downy = event->xbutton.y * 100 / AW->zoom;
1949 }
1950 
1951 void
cb_DocBtn2Motion(Widget w,XEvent * event,String * params,Cardinal * num_params)1952 cb_DocBtn2Motion (Widget w, XEvent * event, String * params, Cardinal * num_params)
1953 {
1954   int i0, j0, i1, j1;
1955 
1956   movex = event->xbutton.x * 100 / AW->zoom;
1957   movey = event->xbutton.y * 100 / AW->zoom;
1958 
1959   if (movex != downx || movey != downy)
1960     {
1961       xytoij (downx, downy, &i0, &j0);
1962       xytoij (movex, movey, &i1, &j1);
1963       i0 = AW->ll - (i1 - i0);
1964       j0 = AW->cc - (j1 - j0);
1965       if (i0 < 1)
1966 	i0 = 1;
1967       if (j0 < 1)
1968 	j0 = 1;
1969       settopleft (i0, j0);
1970     }
1971 
1972   downx = movex;
1973   downy = movey;
1974 }
1975 
1976 void
cb_DocBtn2Up(Widget w,XEvent * event,String * params,Cardinal * num_params)1977 cb_DocBtn2Up (Widget w, XEvent * event, String * params, Cardinal * num_params)
1978 {
1979   int i0, j0, i1, j1;
1980 
1981   upx = event->xbutton.x * 100 / AW->zoom;
1982   upy = event->xbutton.y * 100 / AW->zoom;
1983 
1984   if (upx != downx || upy != downy)
1985     {
1986       xytoij (downx, downy, &i0, &j0);
1987       xytoij (upx, upy, &i1, &j1);
1988       i0 = AW->ll - (i1 - i0);
1989       j0 = AW->cc - (j1 - j0);
1990 
1991       if (i0 < 1)
1992 	i0 = 1;
1993       if (j0 < 1)
1994 	j0 = 1;
1995       settopleft (i0, j0);
1996     }
1997 }
1998 
1999 
2000 void
cb_DocBtn3Up(Widget w,XEvent * event,String * params,Cardinal * num_params)2001 cb_DocBtn3Up (Widget w, XEvent * event, String * params, Cardinal * num_params)
2002 {
2003   event_DocBtn3Up (w, NULL, event, 0);
2004 }
2005 
2006 void
event_DocBtn3Up(Widget w,XtPointer cd,XEvent * event,Boolean * ctd)2007 event_DocBtn3Up (Widget w, XtPointer cd, XEvent * event, Boolean * ctd)
2008 {
2009 
2010   Arg args[20];
2011   register int n;
2012   int x, y;
2013 
2014   if (((XButtonEvent *) event)->button != Button3)
2015     return;
2016 
2017   n = 0;
2018   XtSetArg (args[n], XtNx, &x);
2019   n++;
2020   XtSetArg (args[n], XtNy, &y);
2021   n++;
2022   XtGetValues (ActiveMainwin->draw, args, n);
2023 
2024   x = event->xbutton.x_root - 30;
2025   y = event->xbutton.y_root - 10;
2026 
2027   XtSetArg (args[n], XtNx, x);
2028   n++;
2029   XtSetArg (args[n], XtNy, y);
2030   n++;
2031   XtSetValues (ActiveMainwin->popup, args, n);
2032   XtPopup (ActiveMainwin->popup, XtGrabNone);
2033 
2034 }
2035 
2036 
2037 void
cb_popdown(Widget w,XEvent * event,String * params,Cardinal * num_params)2038 cb_popdown (Widget w, XEvent * event, String * params, Cardinal * num_params)
2039 {
2040 
2041   XtPopdown (ActiveMainwin->popup);
2042 
2043 }
2044 
2045 
2046 
2047 
2048 
2049 int
cb_changesheet(int sheet)2050 cb_changesheet (int sheet)
2051 {
2052   if (FS->l1 == StCell && FS->l2 == EDITING)
2053     {
2054       FS->l1 = StGrap;
2055       FS->l2 = StCell;
2056       EditedCell = ActiveCell;
2057       popdownactivecell ();
2058     }
2059   if (application_changesheet (sheet))
2060     return -1;
2061   if (EditedCell == ActiveCell && FS->l1 != StGrap)
2062     {
2063       popupactivecell ();
2064     }
2065   return 0;
2066 }
2067 
2068 void
cb_BtnOk()2069 cb_BtnOk ()
2070 {
2071 
2072   a_update ();
2073 }
2074 
2075 void
cb_BtnCancel()2076 cb_BtnCancel ()
2077 {
2078   if (FS->l1 == StCell && FS->l2 == EDITING)
2079     {
2080       xdrawcell (AW->cur_r, AW->cur_c, 4, 0);
2081       SetEntryString (Letext, "");
2082       SetEntryString (ActiveMainwin->cell, "");
2083       LetextLooseFocus ();
2084       state_reset_levels ();
2085       FS->l1 = StCell;
2086       popdownactivecell ();
2087       xdrawcursor (0, 0);
2088       copypix ("BtnCancel:after xdrawcursor");
2089       return;
2090     }
2091 }
2092 
2093 void
cb_grap(Widget w,Widget dialog)2094 cb_grap (Widget w, Widget dialog)
2095 {
2096   TEXT_WIDGET = w;
2097   DIALOG_WIDGET = dialog;
2098 
2099   if (FS->l1 == StChart && FS->l2 == SELECT_CHART && FS->l3 == StUp)
2100     {
2101       FS->l1 = StGrap;
2102       FS->l2 = StChart;
2103     }
2104   else if (FS->l1 == StCell && FS->l2 == EDITING)
2105     {
2106       FS->l1 = StGrap;
2107       FS->l2 = StCell;
2108     }
2109   else
2110     {
2111       FS->l1 = StGrap;
2112       FS->l2 = StUndefine;
2113     }
2114 }
2115 
2116 
2117 
2118 void
m_paste(Widget w,XEvent * event,String * params,Cardinal * num_params)2119 m_paste (Widget w, XEvent * event, String * params, Cardinal * num_params)
2120 {
2121   stopedit ();
2122   paste ();
2123   xrepaint ("m_paste");
2124 }
2125 
2126 void
m_paste_special(int all,int formula,int value,int format,int none,int add,int sub,int div,int mul,int trans)2127 m_paste_special (int all, int formula, int value, int format, int none, int add, int sub, int div, int mul, int trans)
2128 {
2129   stopedit ();
2130   paste_special (all, formula, value, format, none, add, sub, div, mul, trans);
2131   xrepaint ("m_paste special");
2132 }
2133 
2134 void
m_copy(Widget w,XEvent * event,String * params,Cardinal * num_params)2135 m_copy (Widget w, XEvent * event, String * params, Cardinal * num_params)
2136 {
2137   stopedit ();
2138   copy ();
2139 }
2140 
2141 
2142 void
m_cut()2143 m_cut ()
2144 {
2145   stopedit ();
2146   if (FS->l1 == StChart)
2147     {
2148       cut ();
2149       state_reset_levels ();
2150     }
2151   else if (FS->l1 == StDrawing)
2152     {
2153       cut ();
2154     }
2155   else
2156     {
2157       cut ();
2158     }
2159   resetselect (0, 0, 0);
2160   xrepaint ("m_cut");
2161 }
2162 
2163 void
m_copyd()2164 m_copyd ()
2165 {
2166   stopedit ();
2167   copyd ();
2168 }
2169 
2170 void
m_copyr()2171 m_copyr ()
2172 {
2173   stopedit ();
2174   copyr ();
2175 }
2176 
2177 
2178 
2179 void
m_color()2180 m_color ()
2181 {
2182   stopedit ();
2183   color ();
2184 }
2185 
2186 void
m_fit()2187 m_fit ()
2188 {
2189   stopedit ();
2190   fit ();
2191   xrepaint ("m_fit");
2192 }
2193 
2194 void
m_brush(Widget w,XEvent * event,String * params,Cardinal * num_params)2195 m_brush (Widget w, XEvent * event, String * params, Cardinal * num_params)
2196 {
2197   stopedit ();
2198   if (ActiveCell == NULL)
2199     return;
2200   setbrush (ActiveCell);
2201   resetselect (0, 0, 0);
2202   TurnOnSpray (Drawarea);
2203   FS->l1 = StBrush;
2204 }
2205 
2206 int
coldrag(double c)2207 coldrag (double c)
2208 {
2209   int C;
2210   C = c;
2211   settopleft (-1, C);
2212   return 0;
2213 }
2214 
2215 int
lindrag(double l)2216 lindrag (double l)
2217 {
2218   int L;
2219   L = l;
2220   settopleft (L, -1);
2221   return 0;
2222 }
2223 
2224 int
cb_setzoom(int z)2225 cb_setzoom (int z)
2226 {
2227   Graph *gr;
2228   int i;
2229 
2230   if (AW->zoom == z)
2231     return 0;
2232   setzoom (z);
2233   ActiveWorksheet->zoom = z;
2234 
2235   for (i = 0; i < getnumgra (); i++)
2236     {
2237       gr = (Graph *) getgra (i);
2238       gr->zoom = z;
2239       resizegra (i);
2240     }
2241   redrawgra ();
2242   xrepaint ("cb_setzoom");
2243   return 0;
2244 }
2245 
2246 
2247 
2248 void
m_sum(Widget w,XEvent * event,String * params,Cardinal * num_params)2249 m_sum (Widget w, XEvent * event, String * params, Cardinal * num_params)
2250 {
2251   char buf[128];
2252   char buf2[8];
2253   int col, startr, endr;
2254   char *txt;
2255 
2256   if (!(FS->l1 == StCell && FS->l2 == EDITING))
2257     return;
2258   if (AW->cur_r < 2)
2259     return;
2260 
2261   col = AW->cur_c;
2262   endr = AW->cur_r - 1;
2263   startr = endr;
2264 
2265   txt = (char *) get_text (startr, col);
2266   if (txt == NULL)
2267     return;
2268 
2269   while (startr > 0 && txt != NULL)
2270     {
2271       startr--;
2272       txt = (char *) get_text (startr, col);
2273 
2274     }
2275   startr++;
2276 
2277   sprintf (buf, "+sum (");
2278   numtoalpha (buf2, startr, col);
2279   strcat (buf, buf2);
2280   strcat (buf, ":");
2281   numtoalpha (buf2, endr, col);
2282   strcat (buf, buf2);
2283   strcat (buf, ")");
2284 
2285   insertstring (Letext, buf);
2286   KeyPressed (-7);
2287 }
2288 
2289 void
m_sorta(Widget w,XEvent * event,String * params,Cardinal * num_params)2290 m_sorta (Widget w, XEvent * event, String * params, Cardinal * num_params)
2291 {
2292   stopedit ();
2293   makesorta ();
2294 }
2295 
2296 void
m_sortd(Widget w,XEvent * event,String * params,Cardinal * num_params)2297 m_sortd (Widget w, XEvent * event, String * params, Cardinal * num_params)
2298 {
2299   stopedit ();
2300   makesortd ();
2301 }
2302 
2303 
2304 
2305 
2306 void
m_createplot()2307 m_createplot ()
2308 {
2309   stopedit ();
2310   state_reset_levels ();
2311   FS->l1 = StChart;
2312   FS->l2 = CREATE_CHART;
2313   TurnOnCross (Drawarea);
2314   return;
2315 }
2316 
2317 void
m_plotline()2318 m_plotline ()
2319 {
2320   stopedit ();
2321   WHAT = CB_LINE;
2322   TurnOnCross (Drawarea);
2323 
2324   state_reset_levels ();
2325   FS->l1 = StDrawing;
2326   FS->l2 = STARTCREATEDRAWING;
2327   FS->l3 = StLine;
2328 
2329   resetselect (0, 0, 0);
2330   return;
2331 }
2332 
2333 void
m_plotrect()2334 m_plotrect ()
2335 {
2336   stopedit ();
2337   WHAT = CB_RECT;
2338   TurnOnCross (Drawarea);
2339 
2340   state_reset_levels ();
2341   FS->l1 = StDrawing;
2342   FS->l2 = STARTCREATEDRAWING;
2343   FS->l3 = StRect;
2344 
2345   resetselect (0, 0, 0);
2346   return;
2347 }
2348 void
m_plotarc()2349 m_plotarc ()
2350 {
2351   stopedit ();
2352   WHAT = CB_ARC;
2353   TurnOnCross (Drawarea);
2354 
2355   state_reset_levels ();
2356   FS->l1 = StDrawing;
2357   FS->l2 = STARTCREATEDRAWING;
2358   FS->l3 = StArc;
2359 
2360   resetselect (0, 0, 0);
2361   return;
2362 }
2363 void
m_plotcircle()2364 m_plotcircle ()
2365 {
2366   stopedit ();
2367   WHAT = CB_CIRCLE;
2368   TurnOnCross (Drawarea);
2369 
2370   state_reset_levels ();
2371   FS->l1 = StDrawing;
2372   FS->l2 = STARTCREATEDRAWING;
2373   FS->l3 = StCircle;
2374 
2375   resetselect (0, 0, 0);
2376   return;
2377 }
2378 void
m_plotbutton()2379 m_plotbutton ()
2380 {
2381   stopedit ();
2382   WHAT = CB_BUTTON;
2383   TurnOnCross (Drawarea);
2384 
2385   state_reset_levels ();
2386   FS->l1 = StDrawing;
2387   FS->l2 = STARTCREATEDRAWING;
2388   FS->l3 = StButton;
2389 
2390   resetselect (0, 0, 0);
2391   return;
2392 }
2393 
2394 void
m_editdraw()2395 m_editdraw ()
2396 {
2397   stopedit ();
2398 
2399   state_reset_levels ();
2400   FS->l1 = StDrawing;
2401   FS->l2 = READYEDITDRAWING;
2402 
2403   TurnOnPencil (Drawarea);
2404 
2405   resetselect (0, 0, 0);
2406   worksheet_select_drawing (AW, AW->activedrawing);
2407 }
2408 
2409 void
m_exitdraw()2410 m_exitdraw ()
2411 {
2412   stopedit ();
2413   TurnOffCursor (Drawarea);
2414   state_reset_levels ();
2415 }
2416 
2417 
2418 
2419 
2420 
2421 int
drplot()2422 drplot ()
2423 {
2424   Graph *gr;
2425 
2426   if (FS->l1 == StBatch)
2427     return 0;
2428 
2429   if (FS->l1 == StChart && FS->l2 == END_CREATE_CHART)
2430     {
2431       plotx = absdownx;
2432       ploty = absdowny;
2433       ploth = absupy - absdowny;
2434       if (ploth < 0)
2435 	{
2436 	  ploth = -ploth;
2437 	  ploty = absupy;
2438 	}
2439       plotw = absupx - absdownx;
2440       if (plotw < 0)
2441 	{
2442 	  plotw = -plotw;
2443 	  plotx = absupx;
2444 	}
2445 
2446       gr = (Graph *) newgra ();
2447       if (plotw < 50)
2448 	plotw = 50;
2449       if (ploth < 40)
2450 	ploth = 40;
2451       if (plotx < 0)
2452 	plotx = 0;
2453       if (ploty < 0)
2454 	ploty = 0;
2455 
2456       gr->x = plotx;
2457       gr->y = ploty;
2458       gr->w = plotw;
2459       gr->h = ploth;
2460       gr->pixmap = createpixmap (Drawarea, plotw, ploth);
2461       fillrectangle (dpy, gr->pixmap, 0, 0, 0, plotw, ploth, 0, 0, 0);
2462       plot ();
2463       copypix ("drplot: newchart");
2464 
2465     }
2466 
2467   if (FS->l1 == StChart && FS->l2 == CREATE_CHART)
2468     {
2469       clearnflush ();
2470 
2471       drawline (dpy, AMwin, 1, absdownx - CW (AW, AW->cc), absdowny - LH (AW, AW->ll), movex, absdowny - LH (AW, AW->ll), 0, 0);
2472       drawline (dpy, AMwin, 1, movex, absdowny, movex, movey, 0, 0);
2473       drawline (dpy, AMwin, 1, movex, movey, absdownx - CW (AW, AW->cc), movey, 0, 0);
2474       drawline (dpy, AMwin, 1, absdownx - CW (AW, AW->cc), movey, absdownx - CW (AW, AW->cc), absdowny - LH (AW, AW->ll), 0, 0);
2475 
2476     }
2477   return 0;
2478 }
2479 
2480 
2481 int
drplot1()2482 drplot1 ()
2483 {
2484   Drawing *dr;
2485   Button *btn;
2486   int centerx, centery, width, height, a1 = 0, a2 = 0;
2487   int pi = 90 * 64;
2488 
2489   if (FS->l1 == StBatch)
2490     return 0;
2491 
2492   if (FS->l1 == StDrawing && FS->l2 == ENDCREATEDRAWING)
2493     {
2494       StDebug ("drplot1 up");
2495       plotx = absdownx;
2496       ploty = absdowny;
2497       ploth = absupy - absdowny;
2498       if (ploth < 0)
2499 	{
2500 	  ploth = -ploth;
2501 	  ploty = absupy;
2502 	}
2503       plotw = absupx - absdownx;
2504       if (plotw < 0)
2505 	{
2506 	  plotw = -plotw;
2507 	  plotx = absupx;
2508 	}
2509       switch (FS->l3)
2510 	{
2511 	case StLine:
2512 	  {
2513 	    dr = (Drawing *) newdra ();
2514 	    dr->x1 = absdownx;
2515 	    dr->x2 = absupx;
2516 	    dr->y1 = absdowny;
2517 	    dr->y2 = absupy;
2518 	    dr->type = LINE;
2519 	    drawline (dpy, DrawAreaPixmap, 1, absdownx - CW (AW, AW->cc), absdowny - LH (AW, AW->ll), upx, upy, 0, 0);
2520 	    worksheet_select_drawing (AW, dr);
2521 	    break;
2522 	  }
2523 	case StRect:
2524 	  {
2525 	    dr = (Drawing *) newdra ();
2526 	    dr->x1 = absdownx;
2527 	    dr->x2 = absupx;
2528 	    dr->y1 = absdowny;
2529 	    dr->y2 = absupy;
2530 	    dr->type = RECT;
2531 	    drawline (dpy, DrawAreaPixmap, 1, absdownx - CW (AW, AW->cc), absdowny - LH (AW, AW->ll), upx, absdowny - LH (AW, AW->ll), 0, 0);
2532 	    drawline (dpy, DrawAreaPixmap, 1, upx, absdowny, upx, upy, 0, 0);
2533 	    drawline (dpy, DrawAreaPixmap, 1, upx, upy, absdownx - CW (AW, AW->cc), upy, 0, 0);
2534 	    drawline (dpy, DrawAreaPixmap, 1, absdownx - CW (AW, AW->cc), upy, absdownx - CW (AW, AW->cc), absdowny - LH (AW, AW->ll), 0, 0);
2535 	    worksheet_select_drawing (AW, dr);
2536 	    break;
2537 	  }
2538 	case StButton:
2539 	  {
2540 	    char *label;
2541 	    btn = (Button *) newbtn ();
2542 	    label = inputbox ("Button label:", "Button Label", "button");
2543 	    button_setlabel (btn, label);
2544 	    label = inputbox ("Button macro:", "Button Macro", "macro1");
2545 	    button_setmacro (btn, label);
2546 
2547 	    btn->x1 = absdownx;
2548 	    btn->x2 = absupx;
2549 	    btn->y1 = absdowny;
2550 	    btn->y2 = absupy;
2551 	    fillrectangle (dpy, DrawAreaPixmap, 3, absdownx - CW (AW, AW->cc), absdowny - LH (AW, AW->ll),
2552 			   upx - (absdownx - CW (AW, AW->cc)), upy - (absdowny - LH (AW, AW->ll)), 0, 0, 0);
2553 	    worksheet_select_button (AW, btn);
2554 	    break;
2555 	  }
2556 	case StArc:
2557 	  {
2558 	    dr = (Drawing *) newdra ();
2559 	    dr->x1 = absdownx;
2560 	    dr->x2 = absupx;
2561 	    dr->y1 = absdowny;
2562 	    dr->y2 = absupy;
2563 	    dr->type = ARC;
2564 	    centerx = absdownx;
2565 	    centery = absdowny;
2566 	    width = 2 * abs (absdownx - absupx);
2567 	    height = 2 * abs (absdowny - absupy);
2568 
2569 	    if (absdownx <= absupx && absdowny <= absupy)
2570 	      {
2571 		a1 = 0;
2572 		a2 = pi;
2573 		centery += height / 2;
2574 	      }
2575 	    if (absdownx >= absupx && absdowny <= absupy)
2576 	      {
2577 		a1 = -pi;
2578 		a2 = pi;
2579 		centerx -= width / 2;
2580 	      }
2581 	    if (absdownx >= absupx && absdowny >= absupy)
2582 	      {
2583 		a1 = 2 * pi;
2584 		a2 = pi;
2585 		centery -= height / 2;
2586 	      }
2587 	    if (absdownx <= absupx && absdowny >= absupy)
2588 	      {
2589 		a1 = pi;
2590 		a2 = pi;
2591 		centerx += width / 2;
2592 	      }
2593 	    drawarc
2594 	      (dpy, DrawAreaPixmap, 1, (centerx - width / 2) - CW (AW, AW->cc), (centery - height / 2) - LH (AW, AW->ll), width, height, a1, a2, 0, 0);
2595 	    worksheet_select_drawing (AW, dr);
2596 	    break;
2597 	  }
2598 	case StCircle:
2599 	  {
2600 	    dr = (Drawing *) newdra ();
2601 	    dr->x1 = absdownx;
2602 	    dr->x2 = absupx;
2603 	    dr->y1 = absdowny;
2604 	    dr->y2 = absupy;
2605 	    dr->type = CIRCLE;
2606 	    centerx = (absdownx + absupx) / 2;
2607 	    centery = (absdowny + absupy) / 2;
2608 	    width = abs (absdownx - absupx);
2609 	    height = abs (absdowny - absupy);
2610 	    drawarc
2611 	      (dpy, DrawAreaPixmap, 1, (centerx - width / 2) - CW (AW, AW->cc), (centery - height / 2) - LH (AW, AW->ll), width, height, 0, 360 * 64, 0, 0);
2612 	    worksheet_select_drawing (AW, dr);
2613 	    break;
2614 	  }
2615 	}
2616       WHAT = -1;
2617       TurnOffCross (Drawarea);
2618       return 0;
2619     }
2620 
2621   if (FS->l1 == StDrawing && (FS->l2 == CREATINGDRAWING || FS->l2 == EDITINGDRAWING))
2622     {
2623       clearnflush ();
2624 
2625       switch (FS->l3)
2626 	{
2627 	case StLine:
2628 	  {
2629 	    drawline (dpy, AMwin, 1, absdownx - CW (AW, AW->cc), absdowny - LH (AW, AW->ll), movex, movey, 0, 0);
2630 	    break;
2631 	  }
2632 	case StRect:
2633 	  {
2634 	    drawline (dpy, AMwin, 1, absdownx - CW (AW, AW->cc), absdowny - LH (AW, AW->ll), movex, absdowny - LH (AW, AW->ll), 0, 0);
2635 	    drawline (dpy, AMwin, 1, movex, absdowny - LH (AW, AW->ll), movex, movey, 0, 0);
2636 	    drawline (dpy, AMwin, 1, movex, movey, absdownx - CW (AW, AW->cc), movey, 0, 0);
2637 	    drawline (dpy, AMwin, 1, absdownx - CW (AW, AW->cc), movey, absdownx - CW (AW, AW->cc), absdowny - LH (AW, AW->ll), 0, 0);
2638 	    break;
2639 	  }
2640 	case StButton:
2641 	  {
2642 	    fillrectangle (dpy, AMwin, 3, absdownx - CW (AW, AW->cc), absdowny - LH (AW, AW->ll),
2643 			   movex - (absdownx - CW (AW, AW->cc)), movey - (absdowny - LH (AW, AW->ll)), 0, 0, 0);
2644 	    break;
2645 	  }
2646 	case StArc:
2647 	  {
2648 	    absupx = movex + CW (AW, AW->cc);
2649 	    absupy = movey + LH (AW, AW->ll);
2650 	    centerx = absdownx;
2651 	    centery = absdowny;
2652 	    width = 2 * abs (absdownx - absupx);
2653 	    height = 2 * abs (absdowny - absupy);
2654 	    if (absdownx <= absupx && absdowny <= absupy)
2655 	      {
2656 		a1 = 0;
2657 		a2 = pi;
2658 		centery += height / 2;
2659 	      }
2660 	    if (absdownx >= absupx && absdowny <= absupy)
2661 	      {
2662 		a1 = -pi;
2663 		a2 = pi;
2664 		centerx -= width / 2;
2665 	      }
2666 	    if (absdownx >= absupx && absdowny >= absupy)
2667 	      {
2668 		a1 = 2 * pi;
2669 		a2 = pi;
2670 		centery -= height / 2;
2671 	      }
2672 	    if (absdownx <= absupx && absdowny >= absupy)
2673 	      {
2674 		a1 = pi;
2675 		a2 = pi;
2676 		centerx += width / 2;
2677 	      }
2678 	    drawarc
2679 	      (dpy, AMwin, 1, (centerx - width / 2) - CW (AW, AW->cc), (centery - height / 2) - LH (AW, AW->ll), width, height, a1, a2, 0, 0);
2680 
2681 	    break;
2682 	  }
2683 	case StCircle:
2684 	  {
2685 	    absupx = movex + CW (AW, AW->cc);
2686 	    absupy = movey + LH (AW, AW->ll);
2687 	    centerx = (absdownx + absupx) / 2;
2688 	    centery = (absdowny + absupy) / 2;
2689 	    width = abs (absdownx - absupx);
2690 	    height = abs (absdowny - absupy);
2691 	    drawarc
2692 	      (dpy, AMwin, 1, (centerx - width / 2) - CW (AW, AW->cc), (centery - height /
2693 		    2) - LH (AW, AW->ll), width, height, 0, 360 * 64, 0, 0);
2694 	    break;
2695 	  }
2696 	}
2697     }
2698   return 0;
2699 }
2700 
2701 
2702 int
drawselect()2703 drawselect ()
2704 {
2705 
2706   int i1, j1, i2, j2;
2707   Drawing *dr;
2708 
2709   if (FS->l1 == StBatch)
2710     return 0;
2711 
2712   if ((FS->l1 == StCell || FS->l1 == StSelection) && FS->l2 == HAND && FS->l3 == StMotion)
2713     return 0;
2714 
2715   if (FS->l1 == StDrawing && (FS->l2 == READYEDITDRAWING || FS->l2 == EDITINGDRAWING))
2716     {
2717       dr = ActiveDrawing;
2718       if (dr == NULL || !getnumdra ())
2719 	{
2720 	  return -1;
2721 	}
2722       xa = dr->x1 - CW (AW, AW->cc);
2723       ya = dr->y1 - LH (AW, AW->ll);
2724       xb = dr->x2 - CW (AW, AW->cc);
2725       yb = dr->y2 - LH (AW, AW->ll);
2726       fillrectangle (dpy, AMwin, 1, xa - 4, ya - 4, 8, 8, 0, 0, 0);
2727       fillrectangle (dpy, AMwin, 1, xb - 4, yb - 4, 8, 8, 0, 0, 0);
2728       return 0;
2729     }
2730 
2731   if (FS->l1 == StChart && FS->l2 == SELECT_CHART)
2732     {
2733       xa = grx - CW (AW, AW->cc) - 4;
2734       ya = gry - LH (AW, AW->ll) - 4;
2735       xb = grx - CW (AW, AW->cc) + grw + 4;
2736       yb = gry - LH (AW, AW->ll) + grh + 4;
2737       drawline (dpy, AMwin, 1, xa, ya, xb, ya, 0, 0);
2738       drawline (dpy, AMwin, 1, xb, ya, xb, yb, 0, 0);
2739       drawline (dpy, AMwin, 1, xb, yb, xa, yb, 0, 0);
2740       drawline (dpy, AMwin, 1, xa, yb, xa, ya, 0, 0);
2741       fillrectangle (dpy, AMwin, 1, xa - 4, ya - 4, 8, 8, 0, 0, 0);
2742       fillrectangle (dpy, AMwin, 1, xa - 4, yb - 4, 8, 8, 0, 0, 0);
2743       fillrectangle (dpy, AMwin, 1, xb - 4, ya - 4, 8, 8, 0, 0, 0);
2744       fillrectangle (dpy, AMwin, 1, xb - 4, yb - 4, 8, 8, 0, 0, 0);
2745       fillrectangle (dpy, AMwin, 1, (xa + xb) / 2, ya - 4, 8, 8, 0, 0, 0);
2746       fillrectangle (dpy, AMwin, 1, (xa + xb) / 2, yb - 4, 8, 8, 0, 0, 0);
2747       fillrectangle (dpy, AMwin, 1, xa - 4, (ya + yb) / 2, 8, 8, 0, 0, 0);
2748       fillrectangle (dpy, AMwin, 1, xb - 4, (ya + yb) / 2, 8, 8, 0, 0, 0);
2749 
2750       return 0;
2751     }
2752 
2753   if (!get_select (&i1, &j1, &i2, &j2))
2754     return 0;
2755 
2756   if (FS->l1 == StSelection)
2757     drawselectbox (i1, j1, i2, j2);
2758   xdrawcursor (0, 0);
2759   copypix ("drawselect:after xdrawcursor");
2760 
2761   return 0;
2762 }
2763 
2764 
2765 
2766 
2767 int
set_visible(int i,int j)2768 set_visible (int i, int j)
2769 {
2770   int c, l;
2771   double fact;
2772 
2773   if (AW == NULL)
2774     return -1;
2775   fact = 100.0 / AW->zoom;
2776 
2777   if (i < 0 || i > AW->nblin || j < 0 || j > AW->nbcol)
2778     return -1;
2779 
2780   c = AW->cc;
2781   l = AW->ll;
2782 
2783   if (i >= l && j >= c && (CW (AW, c) + Width * fact) > (CW (AW, j + 1))
2784       && (LH (AW, l) + Height * fact) > (LH (AW, i + 1)))
2785     return 0;
2786 
2787   if (c > j)
2788     c = j;
2789   else
2790     while ((CW (AW, c) + Width * fact) < (CW (AW, j + 1)) && c < AW->nbcol)
2791       c++;
2792 
2793   if (l > i)
2794     l = i;
2795   else
2796     while ((LH (AW, l) + Height * fact) < (LH (AW, i + 1)) && l < AW->nblin)
2797       l++;
2798 
2799   if (l < 1)
2800     l = 1;
2801   if (c < 1)
2802     c = 1;
2803 
2804   settopleft (l, c);
2805   return 0;
2806 }
2807 
2808 int
storetopleft()2809 storetopleft ()
2810 {
2811   FS->row = AW->ll;
2812   FS->col = AW->cc;
2813   return 0;
2814 }
2815 
2816 int
restoretopleft()2817 restoretopleft ()
2818 {
2819   settopleft (FS->row, FS->col);
2820   return 0;
2821 }
2822 
2823 int
settopleft(int l,int c)2824 settopleft (int l, int c)
2825 {
2826   double L, C;
2827 
2828 
2829   if (c < 1 || c > AW->nbcol)
2830     c = AW->cc;;
2831   if (l < 1 || l > AW->nblin)
2832     l = AW->ll;
2833   if (AW->cc == c && AW->ll == l)
2834     return 0;
2835 
2836   lx -= CW (AW, c) - CW (AW, AW->cc);
2837   ly -= LH (AW, l) - LH (AW, AW->ll);
2838 
2839   AW->cc = c;
2840   AW->ll = l;
2841 
2842   C = c;
2843   L = l;
2844 
2845   setscrollv (L);
2846   setscrollh (C);
2847 
2848   xrepaint ("settopleft");
2849   return 0;
2850 }
2851 
2852 
2853 
2854 
2855 
2856 
2857 int
border_repaint(int n)2858 border_repaint (int n)
2859 {
2860   int i, j;
2861   if (BatchMode)
2862     return 0;
2863 
2864   if (FS->l1 == StBatch)
2865     return 0;
2866 
2867   setfont2 (0, 1, 0, 3);
2868   {
2869     desactivate_zoom ();
2870     fillrectangle (XtDisplay (corner), cornerpixmap, 2, 0, 0, 40, 20, 0, 0, 0);
2871     drawline (XtDisplay (corner), cornerpixmap, 3, 0, 0, 0, 20, 0, 0);
2872     drawline (XtDisplay (corner), cornerpixmap, 1, 1, 0, 1, 20, 0, 0);
2873     drawline (XtDisplay (corner), cornerpixmap, 3, 0, 0, 40, 0, 0, 0);
2874     drawline (XtDisplay (corner), cornerpixmap, 1, 1, 1, 40, 1, 0, 0);
2875 
2876     drawline (XtDisplay (corner), cornerpixmap, 0, 2, 2, 39, 2, 0, 0);
2877     drawline (XtDisplay (corner), cornerpixmap, 0, 2, 2, 2, 18, 0, 0);
2878     drawline (XtDisplay (corner), cornerpixmap, 3, 2, 19, 39, 19, 0, 0);
2879     drawline (XtDisplay (corner), cornerpixmap, 3, 39, 2, 39, 18, 0, 0);
2880     reactivate_zoom ();
2881     clearwin (corner);
2882   }
2883   if (n == 0 || (FS->l1 == StBorder && FS->l2 == StLinindex))
2884     {
2885       desactivate_zoom ();
2886       fillrectangle (XtDisplay (linindex), linpixmap, 2, 0, 0, 40, Height, 0, 0, 0);
2887       drawline (XtDisplay (linindex), linpixmap, 3, 0, 0, 0, Height, 0, 0);
2888       drawline (XtDisplay (linindex), linpixmap, 1, 1, 0, 1, Height, 0, 0);
2889 
2890       reactivate_zoom ();
2891       for (i = AW->ll; LH (AW, i) - LH (AW, AW->ll) < Height * 100 / AW->zoom; i++)
2892 	xdrawlincell (i, 0, 0);
2893       clearwin (linindex);
2894     }
2895   if (n == 0 || (FS->l1 == StBorder && FS->l2 == StColindex))
2896     {
2897       desactivate_zoom ();
2898       fillrectangle (XtDisplay (colindex), colpixmap, 2, 0, 0, Width, 20, 0, 0, 0);
2899       drawline (XtDisplay (colindex), colpixmap, 3, 0, 0, Width, 0, 0, 0);
2900       drawline (XtDisplay (colindex), colpixmap, 1, 0, 1, Width, 1, 0, 0);
2901 
2902       reactivate_zoom ();
2903       for (j = AW->cc; CW (AW, j) - CW (AW, AW->cc) < Width * 100 / AW->zoom; j++)
2904 	xdrawcolcell (0, j, 0);
2905       clearwin (colindex);
2906     }
2907   if (FS->l1 == StBorder && (FS->l2 == StLinindex || FS->l2 == StColindex))
2908     {
2909       xdrawcursor (0, 0);
2910       copypix ("Borderrepaint: StBorder :after xdrawcursor");
2911     }
2912   return 0;
2913 }
2914 
2915 int
xrepaint(char * message)2916 xrepaint (char *message)
2917 {
2918   int i, j;
2919   char tmp[256];
2920 
2921   if (FS->l1 == StBatch)
2922     return 0;
2923 
2924   sprintf (tmp, "xrepaint (%s)", message);
2925   StDebug (tmp);
2926 
2927 
2928   border_repaint (0);
2929 
2930   paint = 1;
2931 
2932   fillrectangle (dpy, DrawAreaPixmap, 0, 0, 0, Width * 100 / AW->zoom, Height * 100 / AW->zoom, 0, 0, 0);
2933 
2934   for (i = AW->ll + 1; LH (AW, i) - LH (AW, AW->ll) < Height * 100 / AW->zoom; i++)
2935     drawline (dpy, DrawAreaPixmap, 2, 0, LH (AW, i) - LH (AW, AW->ll), Width * 100 / AW->zoom, LH (AW, i) - LH (AW, AW->ll), 0, 0);
2936   for (j = AW->cc + 1; CW (AW, j) - CW (AW, AW->cc) < Width * 100 / AW->zoom; j++)
2937     drawline (dpy, DrawAreaPixmap, 2, CW (AW, j) - CW (AW, AW->cc), 0, CW (AW, j) - CW (AW, AW->cc), Height * 100 / AW->zoom, 0, 0);
2938 
2939 
2940 
2941   if (AW->maxcol != NULL)
2942     for (i = AW->ll; i <= AW->maxrow && LH (AW, i) - LH (AW, AW->ll) < Height * 100 / AW->zoom; i++)
2943       {
2944 	for (j = AW->cc; j <= AW->maxcol[i] && CW (AW, j) - CW (AW, AW->cc) < Width * 100 / AW->zoom; j++)
2945 	  xdrawcell (i, j, 4, 0);
2946       }
2947 
2948   redrawdra ();
2949 
2950   redrawbtn ();
2951 
2952   copypix ("xrepaint: after redrawbtn");
2953 
2954   paint = 0;
2955 
2956   return 0;
2957 }
2958 
2959 void
event_Repaint(Widget w,XtPointer cd,XEvent * event,Boolean * ctd)2960 event_Repaint (Widget w, XtPointer cd, XEvent * event, Boolean * ctd)
2961 {
2962   xrepaint ("event repaint");
2963 }
2964 
2965 void
cb_Repaint(Widget w,XtPointer data,XEvent * event)2966 cb_Repaint (Widget w, XtPointer data, XEvent * event)
2967 {
2968   xrepaint ("cb repaint");
2969 }
2970 
2971 int
creategraphpixmap(Graph * gr)2972 creategraphpixmap (Graph * gr)
2973 {
2974   deletegraphpixmap (gr);
2975   gr->pixmap = createpixmap (Drawarea, gr->w, gr->h);
2976   fillrectangle (dpy, gr->pixmap, 0, 0, 0, gr->w, gr->h, 0, 0, 0);
2977   return 0;
2978 }
2979 
2980 int
deletegraphpixmap(Graph * gr)2981 deletegraphpixmap (Graph * gr)
2982 {
2983   if (gr)
2984     if (gr->pixmap)
2985       {
2986 	freepixmap (Drawarea, gr->pixmap);
2987 	gr->pixmap = 0;
2988       }
2989   return 0;
2990 }
2991 
2992 
2993 void
cb_redrawdra()2994 cb_redrawdra ()
2995 {
2996   redrawdra ();
2997 }
2998 
2999 int
redrawdra()3000 redrawdra ()
3001 {
3002   Drawing *gr;
3003   int i;
3004 
3005   int numdra;
3006 
3007   if (FS->l1 == StBatch)
3008     return 0;
3009 
3010   if (ActiveWorksheet == NULL)
3011     {
3012       fprintf (stderr, "ActiveWorksheet =NULL\n");
3013       return -1;
3014     }
3015 
3016 
3017   numdra = ActiveWorksheet->nbdrawing;
3018   for (i = 0; i < numdra; i++)
3019     {
3020       gr = (Drawing *) getdra (i);
3021       if (gr != NULL)
3022 	redrawdr (gr);
3023       else
3024 	fprintf (stderr, "dr =NULL\n");
3025 
3026     }
3027   return 0;
3028 }
3029 
3030 void
plot()3031 plot ()
3032 {
3033   int j;
3034   Graph *gr;
3035   int i1, i2, j1, j2;
3036   gr = ActiveGraph;
3037   if (gr == NULL)
3038     return;
3039   getselection (&i1, &j1, &i2, &j2);
3040 
3041   if (gr->numrange == 0 && (i1 != i2 || j1 != j2) && (i1 != 0 && i2 != 0 && j1 != 0 &&
3042 						      j2 != 0))
3043     for (j = 0; j <= j2 - j1; j++)
3044       {
3045 	(gr->range[j]).i1 = i1;
3046 	(gr->range[j]).i2 = i2;
3047 	(gr->range[j]).j1 = j + j1;
3048 	(gr->range[j]).j2 = j + j1;
3049 	gr->numrange++;
3050       }
3051 
3052   newplot (Drawarea, gr);
3053   return;
3054 
3055 }
3056 
3057 void
doplot(Graph * gr)3058 doplot (Graph * gr)
3059 {
3060   newplot (Drawarea, gr);
3061   return;
3062 
3063 }
3064 
3065 
3066 void
cb_redrawgra()3067 cb_redrawgra ()
3068 {
3069   redrawgra ();
3070 }
3071 
3072 int
redrawgra()3073 redrawgra ()
3074 {
3075   Graph *gr;
3076   int i;
3077   int numgra;
3078 
3079   if (FS->l1 == StBatch)
3080     return 0;
3081 
3082   if (ActiveWorksheet == NULL)
3083     {
3084       fprintf (stderr, "ActiveWorksheet =NULL\n");
3085       return -1;
3086     }
3087   numgra = ActiveWorksheet->nbgraph;
3088 
3089   for (i = 0; i < numgra; i++)
3090     {
3091       gr = (Graph *) getgra (i);
3092       if (gr != NULL)
3093 	newplot (Drawarea, gr);
3094 
3095 
3096 
3097     }
3098   return 0;
3099 
3100 }
3101 
3102 int
redrawbtn()3103 redrawbtn ()
3104 {
3105   Button *gr;
3106   int i;
3107   int numbtn;
3108 
3109   if (FS->l1 == StBatch)
3110     return 0;
3111 
3112   if (ActiveWorksheet == NULL)
3113     {
3114       fprintf (stderr, "ActiveWorksheet =NULL\n");
3115       return -1;
3116     }
3117   numbtn = ActiveWorksheet->nbbutton;
3118 
3119   for (i = 0; i < numbtn; i++)
3120     {
3121 
3122       gr = (Button *) getbtn (i);
3123       if (gr != NULL)
3124 	redrawbt (gr);
3125       else
3126 	fprintf (stderr, "btn =NULL\n");
3127 
3128     }
3129   paint = 0;
3130   return 0;
3131 }
3132 
3133 static int incopypix = 0;
3134 
3135 void
copypix(char * message)3136 copypix (char *message)
3137 {
3138   Graph *gr;
3139   int i;
3140   int numgra;
3141   char tmp[256];
3142   sprintf (tmp, "copypix (%s)", message);
3143   StDebug (tmp);
3144   if (FS->l1 == StBatch)
3145     return;
3146 
3147   if (incopypix)
3148     return;
3149   incopypix = 1;
3150 
3151   if (ActiveWorksheet == NULL)
3152     {
3153       fprintf (stderr, "ActiveWorksheet =NULL\n");
3154       return;
3155     }
3156 
3157   numgra = ActiveWorksheet->nbgraph;
3158 
3159   for (i = 0; i < numgra; i++)
3160     {
3161       gr = (Graph *) getgra (i);
3162       if (gr != NULL)
3163 	{
3164 	  if (gr->pixmap == 0)
3165 	    {
3166 	      creategraphpixmap (gr);
3167 	    }
3168 	  if (gr->pixmap != 0)
3169 	    {
3170 	      copyarea (gr->pixmap, DrawAreaPixmap, 1, 0, 0,
3171 			gr->w * gr->zoom / 100, gr->h * gr->zoom / 100,
3172 			(gr->x - CW (AW, AW->cc)) * gr->zoom / 100,
3173 			(gr->y - LH (AW, AW->ll)) * gr->zoom / 100);
3174 	    }
3175 	}
3176     }
3177 
3178   clearnflush ();
3179   drawselect ();
3180   if (FS->l1 != StChart && FS->l1 != StDrawing && FS->l1 != StGrap &&
3181       !(FS->l1 == StSelection && FS->l2 == 1 && FS->l3 == StMotion))
3182     {
3183       xdrawcursor (0, 0);
3184 
3185     }
3186 
3187   incopypix = 0;
3188 }
3189