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 #include <stdio.h>
52 #include <sys/stat.h>
53 #include <string.h>
54 
55 #include "style.h"
56 #include "cell.h"
57 #include "cell_vb.h"
58 #include "worksheet.h"
59 #include "draw.h"
60 #include "graph.h"
61 #include "range.h"
62 #include "application.h"
63 #include "y.tab.h"
64 #include "info_dialog.h"
65 #include "memory.h"
66 #include "main.h"
67 #include "callback.h"
68 #include "newplot.h"
69 #include "fig.h"
70 #include "gr_interf.h"
71 #include "clipboard.h"
72 
73 static int copy_incr = 1;
74 static int high = 0;
75 static int sj;
76 static int si;
77 static int lj, li;
78 static int brushing = 0;
79 
80 #define AW ActiveWorksheet
81 
82 int
incr()83 incr ()
84 {
85   scanf ("%d", &copy_incr);
86   return 0;
87 }
88 
89 static Cell *brushref = NULL;
90 
91 int
setbrush(Cell * cell)92 setbrush (Cell * cell)
93 {
94   brushref = cell;
95   brushing = 1;
96   return 0;
97 }
98 
99 int
makebrush(int i1,int j1,int i2,int j2)100 makebrush (int i1, int j1, int i2, int j2)
101 {
102   int i, j;
103   if (!brushing)
104     return 0;
105   brushing = 0;
106   if (brushref == NULL)
107     return -1;
108   if (ActiveWorksheet == NULL)
109     return -1;
110 
111   if (i1 < 1 || j1 < 1 || i2 > ActiveWorksheet->nblin || j2 > ActiveWorksheet->nbcol)
112     {
113       return -1;
114     }
115 
116   for (i = i1; i <= i2; i++)
117     for (j = j1; j <= j2; j++)
118       cell_stycpy ((Cell *) applicationcell (i, j, 1), brushref);
119   brushref = NULL;
120   return 0;
121 }
122 
123 
124 int
makesorta()125 makesorta ()
126 {
127   int i, j;
128 
129   Cell **tosort;
130   int k = 0;
131   char buf[256];
132   int istext = 0;
133 
134   if (ActiveWorksheet == NULL)
135     return -1;
136 
137   if (si < 1 || sj < 1 || li > ActiveWorksheet->nblin || lj > ActiveWorksheet->nbcol)
138     {
139       return -1;
140     }
141 
142   tosort = (Cell **) absmalloc (sizeof (Cell *) * (lj - sj + 1) * (li - si + 1), "makesorta:tosort");
143   k = 0;
144 
145 
146   for (i = si; i <= li; i++)
147     for (j = sj; j <= lj; j++)
148       {
149 	tosort[k] = (Cell *) applicationcell (i, j, 1);
150 	if (tosort[k] != NULL)
151 	  {
152 	    if (cell_istext (tosort[k]))
153 	      istext = 1;
154 	  }
155 	k++;
156       }
157 
158 
159   if (k < 2)
160     {
161       absfree (tosort, "makesortd:tosort");
162       return 0;
163     }
164 
165   if (istext)
166     for (i = 0; i < k; i++)
167       {
168 	for (j = i + 1; j < k; j++)
169 	  {
170 	    if (cell_gettext (tosort[i]) != NULL && cell_gettext (tosort[j]) != NULL)
171 	      if (strcmp (cell_gettext (tosort[i]), cell_gettext (tosort[j])) > 0)
172 		{
173 		  if (cell_getformula (tosort[i]) != NULL)
174 		    strcpy (buf, cell_getformula (tosort[i]));
175 		  else
176 		    buf[0] = '\0';
177 		  cell_setformula (tosort[i], cell_getformula (tosort[j]));
178 		  cell_setformula (tosort[j], buf);
179 		}
180 	  }
181       }
182   else
183     for (i = 0; i < k; i++)
184       {
185 	for (j = i + 1; j < k; j++)
186 	  {
187 	    if (cell_getvalue (tosort[i]) > cell_getvalue (tosort[j]))
188 	      {
189 		if (cell_getformula (tosort[i]) != NULL)
190 		  strcpy (buf, cell_getformula (tosort[i]));
191 		else
192 		  buf[0] = '\0';
193 		cell_setformula (tosort[i], cell_getformula (tosort[j]));
194 		cell_setformula (tosort[j], buf);
195 	      }
196 
197 	  }
198       }
199 
200   absfree (tosort, "makesortd:tosort");
201   xrepaint ("makesortd");
202   return 0;
203 }
204 int
makesortd()205 makesortd ()
206 {
207   int i, j;
208 
209   Cell **tosort;
210   int k = 0;
211   char buf[256];
212   int istext = 0;
213 
214   if (ActiveWorksheet == NULL)
215     return -1;
216 
217   if (si < 1 || sj < 1 || li > ActiveWorksheet->nblin || lj > ActiveWorksheet->nbcol)
218     {
219       return -1;
220     }
221 
222   tosort = (Cell **) absmalloc (sizeof (Cell *) * (lj - sj + 1) * (li - si + 1), "makesorta:tosort");
223   k = 0;
224 
225 
226   for (i = si; i <= li; i++)
227     for (j = sj; j <= lj; j++)
228       {
229 	tosort[k] = (Cell *) applicationcell (i, j, -1);
230 	if (tosort[k] != NULL)
231 	  {
232 	    if (cell_istext (tosort[k]))
233 	      istext = 1;
234 	  }
235 	k++;
236       }
237 
238   if (k < 2)
239     {
240       absfree (tosort, "makesorta:tosort");
241       return 0;
242     }
243 
244   if (istext)
245     for (i = 0; i < k; i++)
246       {
247 	for (j = i + 1; j < k; j++)
248 	  {
249 	    if (cell_gettext (tosort[i]) != NULL && cell_gettext (tosort[j]) != NULL)
250 	      if (strcmp (cell_gettext (tosort[i]), cell_gettext (tosort[j])) < 0)
251 		{
252 		  if (cell_getformula (tosort[i]) != NULL)
253 		    strcpy (buf, cell_getformula (tosort[i]));
254 		  else
255 		    buf[0] = '\0';
256 		  cell_setformula (tosort[i], cell_getformula (tosort[j]));
257 		  cell_setformula (tosort[j], buf);
258 		}
259 	  }
260       }
261   else
262     for (i = 0; i < k; i++)
263       {
264 	for (j = i + 1; j < k; j++)
265 	  {
266 	    if (cell_getvalue (tosort[i]) < cell_getvalue (tosort[j]))
267 	      {
268 		if (cell_getformula (tosort[i]) != NULL)
269 		  strcpy (buf, cell_getformula (tosort[i]));
270 		else
271 		  buf[0] = '\0';
272 		cell_setformula (tosort[i], cell_getformula (tosort[j]));
273 		cell_setformula (tosort[j], buf);
274 	      }
275 
276 	  }
277       }
278   absfree (tosort, "makesorta:tosort");
279   xrepaint ("makesorta");
280   return 0;
281 }
282 
283 int
insertfile(char * filename,int icomma,int itab,int ispace,int idotcom,int other,int asone,char * o)284 insertfile (char *filename, int icomma, int itab, int ispace, int idotcom, int other, int asone, char *o)
285 {
286   int i, j;
287   FILE *fp = NULL;
288   char line[1024];
289   char word[1024];
290   int cp = 0;
291   int pos = 0;
292   int sep = 0;
293   int curi = 1;
294   int curj = 1;
295 
296   i = 1;
297   j = 1;
298 
299   fp = fopen (filename, "r");
300   if (fp == NULL)
301     return -1;
302 
303   word[0] = '\0';
304 
305   while (fgets (line, 1020, fp) != NULL)
306     {
307       pos = 0;
308       cp = 0;
309       while (line[pos] != '\0' && pos < 1020)
310 	{
311 	  if (asone)
312 	    {
313 	      while (((line[pos] == ',' && icomma) ||
314 		      (line[pos] == '	' && itab) ||
315 		      (line[pos] == ' ' && ispace) ||
316 		      (line[pos] == '\n') ||
317 		      (line[pos] == EOF) ||
318 		      (line[pos] == ';' && idotcom)
319 		     ) && line[pos] != '\0' && pos < 1020
320 		)
321 		{
322 		  sep = 1;
323 		  pos++;
324 		}
325 	      if (sep)
326 		j++;
327 	    }
328 	  else
329 	    {
330 	      if (line[pos] == ',' && icomma)
331 		{
332 		  j++;
333 		  sep = 1;
334 		  pos++;
335 		}
336 	      if (line[pos] == '	' && itab)
337 		{
338 		  j++;
339 		  sep = 1;
340 		  pos++;
341 		}
342 	      if (line[pos] == ' ' && ispace)
343 		{
344 		  j++;
345 		  sep = 1;
346 		  pos++;
347 		}
348 	      if (line[pos] == ';' && idotcom)
349 		{
350 		  j++;
351 		  sep = 1;
352 		  pos++;
353 		}
354 	      if (line[pos] == '\n')
355 		{
356 		  j++;
357 		  sep = 1;
358 		  pos++;
359 		}
360 	      if (line[pos] == EOF)
361 		{
362 		  j++;
363 		  sep = 1;
364 		  pos++;
365 		}
366 	    }
367 
368 	  if (sep)
369 	    {
370 	      word[cp] = '\0';
371 	      if (strlen (word) > 0)
372 		set_formula (curi + i - 1, curj + j - 2, word);
373 	      sep = 0;
374 	      cp = 0;
375 	    }
376 	  else
377 	    {
378 	      word[cp] = line[pos];
379 	      pos++;
380 	      cp++;
381 	    }
382 	}
383       j = 1;
384       i++;
385     }
386   fclose (fp);
387   return 0;
388 }
389 
390 void
exportfile(char * filename,int icomma,int itab,int ispace,int idotcom,int other,int asone,char * o)391 exportfile (char *filename, int icomma, int itab, int ispace, int idotcom, int other, int asone, char *o)
392 {
393   int i, j;
394   FILE *fp = NULL;
395 
396   if (ActiveWorksheet == NULL)
397     return;
398 
399   if (si < 1 || sj < 1 || li > ActiveWorksheet->nblin || lj > ActiveWorksheet->nbcol)
400     {
401       si = 1;
402       sj = 1;
403       li = ActiveWorksheet->maxrow;
404       lj = 0;
405       for (i = 0; i < ActiveWorksheet->maxrow; i++)
406 	{
407 	  if (ActiveWorksheet->maxcol[i] > lj)
408 	    lj = ActiveWorksheet->maxcol[i];
409 	}
410     }
411 
412   fp = fopen (filename, "w");
413   if (fp == NULL)
414     return;
415   {
416     for (i = si; i <= li; i++)
417       {
418 	if (i > si)
419 	  fprintf (fp, "\n");
420 	for (j = sj; j <= lj; j++)
421 	  {
422 	    if (j > sj)
423 	      {
424 		if (itab)
425 		  fprintf (fp, "    ");
426 		if (icomma)
427 		  fprintf (fp, ",");
428 		if (ispace)
429 		  fprintf (fp, " ");
430 		if (idotcom)
431 		  fprintf (fp, ";");
432 	      }
433 	    if (get_text (i, j) != NULL)
434 	      fprintf (fp, "%s", get_text (i, j));
435 	    else
436 	      fprintf (fp, "0");
437 	  }
438       }
439   }
440   fclose (fp);
441 
442 }
443 
444 
445 void
m_exit()446 m_exit ()
447 {
448   if (Ask ("Really quit?", NULL) == RET_OK)
449     exit (0);
450 }
451 
452 int
copy()453 copy ()
454 {
455   if (ActiveWorksheet == NULL)
456     return -1;
457   if (ActiveWorksheet->selection == NULL)
458     return -1;
459 
460   copy_clipboard (ActiveWorksheet->selection);
461   return 0;
462 }
463 
464 int
paste()465 paste ()
466 {
467 
468   if (ActiveWorksheet == NULL)
469     return -1;
470   if (ActiveWorksheet->selection == NULL)
471     return -1;
472   paste_clipboard (ActiveWorksheet->selection);
473   return 0;
474 
475 }
476 
477 int
paste_special(int all,int formula,int value,int format,int none,int add,int sub,int div,int mul,int trans)478 paste_special (int all, int formula, int value, int format, int none, int add, int sub, int div, int mul, int trans)
479 {
480 
481   if (ActiveWorksheet == NULL)
482     return -1;
483   if (ActiveWorksheet->selection == NULL)
484     return -1;
485   paste_special_clipboard (ActiveWorksheet->selection, all, formula, value, format, none, add, sub, div, mul, trans);
486   return 0;
487 
488 }
489 
490 
491 
492 int
cut()493 cut ()
494 {
495   if (ActiveWorksheet == NULL)
496     return -1;
497   if (ActiveWorksheet->selection == NULL)
498     return -1;
499   cut_clipboard (ActiveWorksheet->selection);
500 
501   return 0;
502 }
503 
504 int
makeformat(int type,int decimal)505 makeformat (int type, int decimal)
506 {
507   int i, j;
508   Cell *cell;
509 
510   if (type < 1 || type > 5)
511     return -1;
512 
513 
514 
515   if (sj > 0 && si > 0 && lj <= ActiveWorksheet->nbcol && li <= ActiveWorksheet->nblin)
516     for (i = 0; i <= lj - sj; i++)
517       {
518 	for (j = 0; j <= li - si; j++)
519 	  {
520 	    cell = (Cell *) applicationcell (si + j, sj + i, 1);
521 	    cell_setformat (cell, type, decimal);
522 	    xdrawcell2 (cell, 4, 1);
523 	  }
524       }
525   else
526     {
527       if (ActiveCell != NULL)
528 	{
529 	  cell_setformat (ActiveCell, type, decimal);
530 	  xdrawcell2 (ActiveCell, 4, 1);
531 	}
532     }
533   copypix ("makeformat");
534   return 0;
535 }
536 
537 int
makejust(int just)538 makejust (int just)
539 {
540 
541   Cell *cell;
542 
543 
544   cell = worksheet_selection_cells (ActiveWorksheet);
545   while (cell != NULL)
546     {
547       cell_setjust (cell, just);
548       xdrawcell2 (cell, 4, 1);
549       cell = worksheet_selection_cells (ActiveWorksheet);
550     }
551 
552   copypix ("makejust");
553   return 0;
554 }
555 
556 int
makefont(int fontnum,int fontweight,int fontsize)557 makefont (int fontnum, int fontweight, int fontsize)
558 {
559   int i, j;
560   Cell *cell;
561 
562 
563 
564   if (sj > 0 && si > 0 && lj <= ActiveWorksheet->nbcol && li <= ActiveWorksheet->nblin)
565     for (i = 0; i <= lj - sj; i++)
566       {
567 	for (j = 0; j <= li - si; j++)
568 	  {
569 	    cell = (Cell *) applicationcell (si + j, sj + i, 1);
570 	    cell_setfont (cell, fontnum, fontweight, fontsize);
571 	    xdrawcell2 (cell, 4, 1);
572 	  }
573       }
574   else
575     {
576       if (ActiveCell != NULL)
577 	{
578 	  cell_setfont (ActiveCell, fontnum, fontweight, fontsize);
579 	  xdrawcell2 (ActiveCell, 4, 1);
580 	}
581     }
582 
583   copypix ("makefont");
584   return 0;
585 }
586 
587 
588 
589 int
border(int border,int type)590 border (int border, int type)
591 {
592   int i, j;
593   Cell *cell;
594 
595   if (sj > 0 && si > 0 && lj <= ActiveWorksheet->nbcol && li <= ActiveWorksheet->nblin)
596     {
597       switch (border)
598 	{
599 	case 8:
600 	  {
601 	    for (j = 0; j <= lj - sj; j++)
602 	      {
603 		cell = (Cell *) applicationcell (si, sj + j, 1);
604 		cell_setborders (cell, border, type);
605 		xdrawcell2 (cell, 4, 1);
606 	      }
607 	    break;
608 	  }
609 	case 9:
610 	  {
611 	    for (j = 0; j <= lj - sj; j++)
612 	      {
613 
614 
615 
616 		cell = (Cell *) applicationcell (li + 1, sj + j, 1);
617 		cell_setborders (cell, 8, type);
618 		xdrawcell2 (cell, 4, 1);
619 	      }
620 	    break;
621 	  }
622 	case 10:
623 	  {
624 	    for (i = 0; i <= li - si; i++)
625 	      {
626 
627 
628 
629 		cell = (Cell *) applicationcell (si + i, lj + 1, 1);
630 		cell_setborders (cell, 7, type);
631 		xdrawcell2 (cell, 4, 1);
632 	      }
633 	    break;
634 	  }
635 	case 7:
636 	  {
637 	    for (i = 0; i <= li - si; i++)
638 	      {
639 		cell = (Cell *) applicationcell (si + i, sj, 1);
640 		cell_setborders (cell, border, type);
641 		xdrawcell2 (cell, 4, 1);
642 	      }
643 	    break;
644 	  }
645 	case 1:
646 	  {
647 	    for (j = 1; j <= lj - sj; j++)
648 	      {
649 		for (i = 0; i <= li - si; i++)
650 		  {
651 		    cell = (Cell *) applicationcell (si + i, sj + j, 1);
652 		    cell_setborders (cell, 7, type);
653 		    xdrawcell2 (cell, 4, 1);
654 		  }
655 	      }
656 	    break;
657 	  }
658 	case 2:
659 	  {
660 
661 	    for (j = 0; j <= lj - sj; j++)
662 	      {
663 		for (i = 1; i <= li - si; i++)
664 		  {
665 		    cell = (Cell *) applicationcell (si + i, sj + j, 1);
666 		    cell_setborders (cell, 8, type);
667 		    xdrawcell2 (cell, 4, 1);
668 		  }
669 	      }
670 
671 	    break;
672 	  }
673 
674 
675 	}
676     }
677   else
678     {
679       if (ActiveCell != NULL)
680 	{
681 	  cell_setborders (ActiveCell, border, type);
682 	  xdrawcell2 (ActiveCell, 4, 1);
683 	}
684     }
685   copypix ("border");
686   return 0;
687 }
688 
689 int
fit()690 fit ()
691 {
692   int i, j;
693 
694 
695 
696   if (sj > 0 && si > 0 && lj <= ActiveWorksheet->nbcol && li <= ActiveWorksheet->nblin)
697     for (i = 0; i <= lj - sj; i++)
698       {
699 	for (j = 0; j <= li - si; j++)
700 	  {
701 	    xfit (si + j, sj + i);
702 	  }
703       }
704   return 0;
705 }
706 
707 void
formatcolumn()708 formatcolumn ()
709 {
710   int i, j;
711 
712 
713 
714   if (sj > 0 && si > 0 && lj <= ActiveWorksheet->nbcol && li <= ActiveWorksheet->nblin)
715     for (i = 0; i <= lj - sj; i++)
716       {
717 	for (j = 0; j <= li - si; j++)
718 	  {
719 	    xfitcolumn (si + j, sj + i);
720 	  }
721       }
722   xrepaint ("format column");
723   return;
724 }
725 
726 void
formatline()727 formatline ()
728 {
729   int i, j;
730 
731 
732 
733   if (sj > 0 && si > 0 && lj <= ActiveWorksheet->nbcol && li <= ActiveWorksheet->nblin)
734     for (i = 0; i <= lj - sj; i++)
735       {
736 	for (j = 0; j <= li - si; j++)
737 	  {
738 	    xfitline (si + j, sj + i);
739 	  }
740       }
741   xrepaint ("format line");
742   return;
743 }
744 
745 
746 
747 int
color()748 color ()
749 {
750   makecolor (1, 0);
751   return 0;
752 }
753 
754 int
makecolor(int fg,int bg)755 makecolor (int fg, int bg)
756 {
757   int i, j;
758   Cell *cell;
759 
760 
761   if (sj > 0 && si > 0 && lj <= ActiveWorksheet->nbcol && li <= ActiveWorksheet->nblin)
762     for (i = 0; i <= lj - sj; i++)
763       {
764 	for (j = 0; j <= li - si; j++)
765 	  {
766 	    cell = (Cell *) applicationcell (si + j, sj + i, 1);
767 	    cell_setfg (cell, fg);
768 	    cell_setbg (cell, bg);
769 	    xdrawcell2 (cell, 4, 1);
770 	  }
771       }
772   else
773     {
774       if (ActiveCell != NULL)
775 	{
776 	  cell_setfg (ActiveCell, fg);
777 	  cell_setbg (ActiveCell, bg);
778 	  xdrawcell2 (ActiveCell, 4, 1);
779 	}
780     }
781 
782   copypix ("makecolor");
783   return 0;
784 }
785 
786 
787 int
copyd()788 copyd ()
789 {
790   int i, j;
791 
792 
793   for (i = 0; i <= lj - sj; i++)
794     {
795       for (j = 1; j <= li - si; j++)
796 	{
797 	  copymod (si, sj + i, si + j, sj + i, copy_incr);
798 	}
799     }
800   for (j = 0; j <= lj - sj; j++)
801     {
802       for (i = 0; i <= li - si; i++)
803 	{
804 	  xdrawcell (si + i, sj + j, 4, 1);
805 	}
806     }
807 
808   copypix ("copyd");
809   return 0;
810 }
811 
812 int
copyr()813 copyr ()
814 {
815   int i, j;
816 
817   for (i = 1; i <= lj - sj; i++)
818     {
819       for (j = 0; j <= li - si; j++)
820 	{
821 	  copymod (si + j, sj, si + j, sj + i, copy_incr);
822 	}
823     }
824 
825   for (j = 0; j <= lj - sj; j++)
826     {
827       for (i = 0; i <= li - si; i++)
828 	{
829 	  xdrawcell (si + i, sj + j, 4, 1);
830 	}
831     }
832 
833   copypix ("copyr");
834   return 0;
835 }
836 
837 void
m_auditcell()838 m_auditcell ()
839 {
840   cell_auditcell (ActiveCell);
841 };
842 
843 
844 int
insertline()845 insertline ()
846 {
847   int i, j;
848 
849   int nbrow = ActiveWorksheet->maxrow;
850   int r = ActiveCell->r;
851   Cell *backup = ActiveCell;
852 
853   if (ActiveCell == NULL)
854     return -1;
855   if (ActiveWorksheet == NULL)
856     return -1;
857   if (r < 1 || r > nbrow)
858     return -2;
859 
860   setsi (r);
861 
862   for (i = nbrow; i > r; i--)
863     {
864       for (j = 1; j <= ActiveWorksheet->maxcol[i]; j++)
865 	{
866 	  copymod (i, j, i + 1, j, 1);
867 	}
868     }
869   for (j = 1; j <= ActiveWorksheet->maxcol[r + 1]; j++)
870     {
871       clear_cell (applicationcell (r + 1, j, 1));
872     }
873 
874   for (i = nbrow + 1; i > r; i--)
875     {
876       for (j = 1; j <= ActiveWorksheet->maxcol[i]; j++)
877 	{
878 	  xdrawcell (i, j, 4, 1);
879 	}
880     }
881 
882   setsi (-1);
883   ActiveCell = backup;
884   copypix ("insertline");
885   return 0;
886 }
887 
888 int
insertcolumn()889 insertcolumn ()
890 {
891   int i, j;
892 
893   int nbrow = ActiveWorksheet->maxrow;
894   int c = ActiveCell->c;
895   int nbcol;
896   int maxcol = 0;
897   Cell *backup = ActiveCell;
898 
899   if (ActiveCell == NULL)
900     return -1;
901   if (ActiveWorksheet == NULL)
902     return -1;
903 
904   for (i = 1; i <= nbrow; i++)
905     {
906       nbcol = ActiveWorksheet->maxcol[i];
907       if (nbcol > maxcol)
908 	maxcol = nbcol;
909     }
910 
911   if (c < 1 || c > maxcol)
912     return -2;
913 
914 
915   setsj (c);
916 
917 
918 
919   for (i = 1; i <= nbrow; i++)
920     {
921       nbcol = ActiveWorksheet->maxcol[i];
922       for (j = nbcol; j > c; j--)
923 	{
924 	  copymod (i, j, i, j + 1, 1);
925 	}
926     }
927   for (i = 1; i <= nbrow; i++)
928     {
929       clear_cell (applicationcell (i, c + 1, 1));
930     }
931 
932   for (i = 1; i <= nbrow; i++)
933     {
934       nbcol = ActiveWorksheet->maxcol[i];
935       for (j = nbcol; j > c; j--)
936 	{
937 	  xdrawcell (i, j, 4, 1);
938 	}
939     }
940 
941   setsj (-1);
942   ActiveCell = backup;
943   copypix ("insertcolumn");
944   return 0;
945 }
946 
947 int
delline()948 delline ()
949 {
950   int i, j;
951 
952   int nbrow = ActiveWorksheet->maxrow;
953   int r = ActiveCell->r;
954   Cell *backup = ActiveCell;
955 
956   if (ActiveCell == NULL)
957     return -1;
958   if (ActiveWorksheet == NULL)
959     return -1;
960   if (r < 1 || r > nbrow)
961     return -2;
962 
963   setsi (r - 1);
964 
965   for (i = r; i < nbrow; i++)
966     {
967       for (j = 1; j <= ActiveWorksheet->maxcol[i]; j++)
968 	{
969 	  copymod (i + 1, j, i, j, 1);
970 	}
971     }
972   for (j = 1; j <= ActiveWorksheet->maxcol[nbrow]; j++)
973     {
974       clear_cell (applicationcell (nbrow, j, 1));
975     }
976 
977   for (i = r; i <= nbrow; i++)
978     {
979       for (j = 1; j <= ActiveWorksheet->maxcol[i]; j++)
980 	{
981 	  xdrawcell (i, j, 4, 1);
982 	}
983     }
984 
985   setsi (-1);
986   ActiveCell = backup;
987   copypix ("delline");
988   return 0;
989 }
990 
991 int
delcolumn()992 delcolumn ()
993 {
994   int i, j;
995 
996   int nbrow = ActiveWorksheet->maxrow;
997   int c = ActiveCell->c;
998   int nbcol;
999   int maxcol = 0;
1000   Cell *backup = ActiveCell;
1001 
1002   if (ActiveCell == NULL)
1003     return -1;
1004   if (ActiveWorksheet == NULL)
1005     return -1;
1006 
1007   for (i = 1; i <= nbrow; i++)
1008     {
1009       nbcol = ActiveWorksheet->maxcol[i];
1010       if (nbcol > maxcol)
1011 	maxcol = nbcol;
1012     }
1013 
1014   if (c < 1 || c > maxcol)
1015     return -2;
1016 
1017 
1018   setsj (c - 1);
1019 
1020 
1021 
1022   for (i = 1; i <= nbrow; i++)
1023     {
1024       nbcol = ActiveWorksheet->maxcol[i];
1025       for (j = c; j < nbcol; j++)
1026 	{
1027 	  copymod (i, j + 1, i, j, 1);
1028 	}
1029     }
1030   for (i = 1; i <= nbrow; i++)
1031     {
1032       nbcol = ActiveWorksheet->maxcol[i];
1033       if (nbcol > 0)
1034 	clear_cell (applicationcell (i, nbcol, 1));
1035     }
1036 
1037   for (i = 1; i <= nbrow; i++)
1038     {
1039       nbcol = ActiveWorksheet->maxcol[i];
1040       for (j = c; j <= nbcol; j++)
1041 	{
1042 	  xdrawcell (i, j, 4, 1);
1043 	}
1044     }
1045 
1046   setsj (-1);
1047   ActiveCell = backup;
1048   copypix ("delcolumn");
1049   return 0;
1050 }
1051 
1052 
1053 
1054 int
resetselect(int ii,int jj,int k)1055 resetselect (int ii, int jj, int k)
1056 {
1057   char label[2];
1058   sprintf (label, " ");
1059   inform (label);
1060   worksheet_select_cell (ActiveWorksheet, ActiveCell);
1061 
1062   if (ii == 0 && jj == 0 && k == 0)
1063     {
1064       si = li = 0;
1065       sj = lj = 0;
1066       high = 0;
1067       selectingto0 ();
1068       copypix ("resetselect 1");
1069       return 0;
1070     }
1071   if (ii >= si && ii <= li && jj >= sj && jj <= lj && !k)
1072     return -1;
1073 
1074 
1075   si = li = 0;
1076   sj = lj = 0;
1077   high = 0;
1078   selectingto0 ();
1079   copypix ("resetselect 2");
1080   return 0;
1081 }
1082 
1083 int
extendselection(int i,int j)1084 extendselection (int i, int j)
1085 {
1086   int i1, i2, j1, j2;
1087   getselection (&i1, &j1, &i2, &j2);
1088 
1089   if (i < i1)
1090     {
1091       i1 = i;
1092     }
1093   else
1094     {
1095       i2 = i;
1096     }
1097   if (j < j1)
1098     {
1099       j1 = j;
1100     }
1101   else
1102     {
1103       j2 = j;
1104     }
1105 
1106 
1107   if (i1 == 0 || j1 == 0)
1108     {
1109       if (ActiveCell == NULL)
1110 	{
1111 	  return -1;
1112 	}
1113       else
1114 	{
1115 	  i1 = ActiveCell->r;
1116 	  j1 = ActiveCell->c;
1117 	}
1118     }
1119   return setselection (i1, j1, i2, j2);
1120 }
1121 
1122 int
setselection(int i1,int j1,int i2,int j2)1123 setselection (int i1, int j1, int i2, int j2)
1124 {
1125   int t;
1126   char label[16];
1127 
1128   worksheet_select_range (ActiveWorksheet, i1, j1, i2, j2);
1129 
1130   if (i1 > i2)
1131     {
1132       t = i1;
1133       i1 = i2;
1134       i2 = t;
1135     }
1136   if (j1 > j2)
1137     {
1138       t = j1;
1139       j1 = j2;
1140       j2 = t;
1141     }
1142 
1143   si = i1;
1144   sj = j1;
1145   li = i2;
1146   lj = j2;
1147 
1148   if (i1 != i2 || j1 != j2)
1149     {
1150       high = 1;
1151 
1152       sprintf (label, "%d L x %d C\n", li - si + 1, lj - sj + 1);
1153       inform (label);
1154     }
1155   return 0;
1156 }
1157 
1158 int
setselectionrange(Range * range)1159 setselectionrange (Range * range)
1160 {
1161   if (range == NULL)
1162     return 1;
1163   if (range->nplage < 1)
1164     return 2;
1165 
1166   return setselection (range->plage[0].r1, range->plage[0].c1, range->plage[0].r2, range->plage[0].c2);
1167 
1168 }
1169 
1170 int
getselection(int * i1,int * j1,int * i2,int * j2)1171 getselection (int *i1, int *j1, int *i2, int *j2)
1172 {
1173   *i1 = si;
1174   *i2 = li;
1175   *j1 = sj;
1176   *j2 = lj;
1177   return 0;
1178 }
1179 
1180 
1181 
1182 int
selectlin(int lin,int shift)1183 selectlin (int lin, int shift)
1184 {
1185   char label[16];
1186 
1187 
1188 
1189 
1190 
1191 
1192   sj = 1;
1193   lj = ActiveWorksheet->nbcol - 1;
1194   if (shift)
1195     {
1196       if (lin <= si)
1197 	si = lin;
1198       else
1199 	li = lin;
1200     }
1201   else
1202     {
1203       si = li = lin;
1204     }
1205   high = 1;
1206   worksheet_select_range (ActiveWorksheet, si, sj, li, lj);
1207   sprintf (label, "%d L x %d C\n", li - si + 1, lj - sj + 1);
1208   inform (label);
1209 
1210 
1211   return 0;
1212 }
1213 
1214 int
selectcol(int col,int shift)1215 selectcol (int col, int shift)
1216 {
1217   char label[16];
1218 
1219 
1220 
1221 
1222 
1223 
1224   si = 1;
1225   li = ActiveWorksheet->nblin - 1;
1226   if (shift)
1227     {
1228       if (sj <= col)
1229 	sj = col;
1230       else
1231 	lj = col;
1232     }
1233   else
1234     {
1235       sj = lj = col;
1236     }
1237   high = 1;
1238   worksheet_select_range (ActiveWorksheet, si, sj, li, lj);
1239   sprintf (label, "%d L x %d C\n", li - si + 1, lj - sj + 1);
1240   inform (label);
1241 
1242   return 0;
1243 }
1244 
1245 int
selectall()1246 selectall ()
1247 {
1248   char label[64];
1249   si = 1;
1250 
1251   sj = 1;
1252 
1253   li = ActiveWorksheet->nblin;
1254   lj = ActiveWorksheet->nbcol;
1255   high = 1;
1256   worksheet_select_range (ActiveWorksheet, si, sj, li, lj);
1257   sprintf (label, "%d L x %d C\n", li - si + 1, lj - sj + 1);
1258   inform (label);
1259   return 0;
1260 }
1261 
1262 
1263 
1264 int
selection(int ii,int jj)1265 selection (int ii, int jj)
1266 {
1267   char label[16];
1268 
1269   if (lj == jj && li == ii)
1270     return 0;
1271   if (high == 0)
1272     {
1273       sj = lj = jj;
1274       si = li = ii;
1275     }
1276   lj = jj;
1277   li = ii;
1278   high = 1;
1279 
1280 
1281   worksheet_select_range (ActiveWorksheet, si, sj, li, lj);
1282 
1283   sprintf (label, "%d L x %d C\n", li - si + 1, lj - sj + 1);
1284   inform (label);
1285 
1286   copypix ("selection");
1287   return 0;
1288 
1289 }
1290 
1291 int
moveselect(int fri,int frj,int toi,int toj,int k)1292 moveselect (int fri, int frj, int toi, int toj, int k)
1293 {
1294   int i, j;
1295   Cell *cell1;
1296   Cell *cell2;
1297 
1298   if (toi < 1)
1299     toi = 1;
1300   if (toj < 1)
1301     toj = 1;
1302 
1303 
1304 
1305 
1306   if (si + toi - fri < 1 || sj + toj - frj < 1
1307       || li + toi - fri > AW->nblin - 1
1308       || lj + toj - frj > AW->nbcol - 1)
1309     return 0;
1310 
1311 
1312   if (k)
1313     {
1314       if (si == 0 && sj == 0 && ActiveCell != NULL)
1315 	{
1316 	  cell2 = ActiveCell;
1317 	  cell1 = (Cell *) applicationcell (ActiveCell->r + toi - fri, ActiveCell->c + toj - frj, 1);
1318 	  copymod3 (cell1, cell2, 1);
1319 	  cell_stycpy (cell1, cell2);
1320 	  clear_cell (cell2);
1321 	  xdrawcell2 (cell2, 4, 1);
1322 	  xdrawcell2 (cell1, 4, 1);
1323 	  return 0;
1324 	}
1325 
1326       if (toi - fri >= 0 && toj - frj >= 0)
1327 	{
1328 	  for (i = li; i >= si; i--)
1329 	    {
1330 	      for (j = lj; j >= sj; j--)
1331 		{
1332 		  cell2 = (Cell *) applicationcell (i, j, 1);
1333 		  cell1 = (Cell *) applicationcell (i + toi - fri, j + toj - frj, 1);
1334 		  copymod3 (cell1, cell2, 1);
1335 		  cell_stycpy (cell1, cell2);
1336 		  clear_cell (cell2);
1337 		  xdrawcell2 (cell2, 4, 1);
1338 		  xdrawcell2 (cell1, 4, 1);
1339 		}
1340 	    }
1341 	}
1342       else if (toi - fri <= 0 && toj - frj >= 0)
1343 	{
1344 	  for (i = si; i <= li; i++)
1345 	    {
1346 	      for (j = lj; j >= sj; j--)
1347 		{
1348 		  cell2 = (Cell *) applicationcell (i, j, 1);
1349 		  cell1 = (Cell *) applicationcell (i + toi - fri, j + toj - frj, 1);
1350 		  copymod3 (cell1, cell2, 1);
1351 		  cell_stycpy (cell1, cell2);
1352 		  clear_cell (cell2);
1353 		  xdrawcell2 (cell2, 4, 1);
1354 		  xdrawcell2 (cell1, 4, 1);
1355 		}
1356 	    }
1357 	}
1358       else if (toi - fri >= 0 && toj - frj <= 0)
1359 	{
1360 	  for (i = li; i >= si; i--)
1361 	    {
1362 	      for (j = sj; j <= lj; j++)
1363 		{
1364 		  cell2 = (Cell *) applicationcell (i, j, 1);
1365 		  cell1 = (Cell *) applicationcell (i + toi - fri, j + toj - frj, 1);
1366 		  copymod3 (cell1, cell2, 1);
1367 
1368 		  cell_stycpy (cell1, cell2);
1369 		  clear_cell (cell2);
1370 		  xdrawcell2 (cell2, 4, 1);
1371 		  xdrawcell2 (cell1, 4, 1);
1372 		}
1373 	    }
1374 
1375 
1376 	}
1377       else if (toi - fri <= 0 && toj - frj <= 0)
1378 	{
1379 	  for (i = si; i <= li; i++)
1380 	    {
1381 	      for (j = sj; j <= lj; j++)
1382 		{
1383 		  cell2 = (Cell *) applicationcell (i, j, 1);
1384 		  cell1 = (Cell *) applicationcell (i + toi - fri, j + toj - frj, 1);
1385 		  copymod3 (cell1, cell2, 1);
1386 
1387 		  cell_stycpy (cell1, cell2);
1388 		  clear_cell (cell2);
1389 		  xdrawcell2 (cell2, 4, 1);
1390 		  xdrawcell2 (cell1, 4, 1);
1391 		}
1392 	    }
1393 	}
1394 
1395       si += toi - fri;
1396       li += toi - fri;
1397       sj += toj - frj;
1398       lj += toj - frj;
1399       copypix ("moveselect 1");
1400     }
1401   else
1402     {
1403       si += toi - fri;
1404       li += toi - fri;
1405       sj += toj - frj;
1406       lj += toj - frj;
1407       copypix ("moveselect 2");
1408       si -= toi - fri;
1409       li -= toi - fri;
1410       sj -= toj - frj;
1411       lj -= toj - frj;
1412 
1413     }
1414 
1415   setselection (si, sj, li, lj);
1416 
1417   return 0;
1418 }
1419 
1420 int
myfprintf(FILE * fp,char * word,int l)1421 myfprintf (FILE * fp, char *word, int l)
1422 {
1423   int i;
1424   if (strcmp (word, "\n") == 0)
1425     return -1;
1426   for (i = 0; i <= l - strcspn (word, " "); i++)
1427     fprintf (fp, " ");
1428   fprintf (fp, "%s", word);
1429   return 0;
1430 }
1431 
1432 
1433 
1434 
1435 
1436 
1437 
1438 
1439 
1440 
1441 
1442 
1443 
1444 
1445 
1446 
1447 
1448 
1449 
1450 
1451 
1452 
1453 
1454 
1455 
1456 
1457 
1458 int
printplot(int number)1459 printplot (int number)
1460 {
1461   return 0;
1462 }
1463 
1464 int
is_selecting()1465 is_selecting ()
1466 {
1467   return high;
1468 }
1469 
1470 int
get_select(int * i1,int * j1,int * i2,int * j2)1471 get_select (int *i1, int *j1, int *i2, int *j2)
1472 {
1473   *i1 = si;
1474   *i2 = li;
1475   *j1 = sj;
1476   *j2 = lj;
1477   return high;
1478 }
1479 
1480 
1481 
1482 
1483 
1484 void
updategraph()1485 updategraph ()
1486 {
1487   plot (0);
1488 }
1489 
1490 int
alphatonum(char * buf,int * ii,int * jj)1491 alphatonum (char *buf, int *ii, int *jj)
1492 {
1493 
1494   int len;
1495   int i;
1496   int c = 0;
1497   int r = 0;
1498   int pos = 0;
1499 
1500   len = strlen (buf);
1501 
1502   for (i = 0; i < len; i++)
1503     {
1504       if ('a' <= buf[i] && buf[i] <= 'z')
1505 	buf[i] = 'A' + buf[i] - 'a';
1506     }
1507 
1508   if (buf[0] == '$')
1509     {
1510       pos++;
1511     }
1512 
1513   while ('A' <= buf[pos] && buf[pos] <= 'Z')
1514     {
1515       c *= 26;
1516       c += (buf[pos] - 'A' + 1);
1517       pos++;
1518     }
1519 
1520   if (buf[pos] == '$')
1521     {
1522       pos++;
1523     }
1524   sscanf (buf + pos, "%d", &r);
1525 
1526   *ii = r;
1527   *jj = c;
1528 
1529   return 0;
1530 }
1531 
1532 static char *ALPHA[] =
1533 {
1534   "A", "B", "C", "D", "E", "F", "G", "H", "I",
1535   "J", "K", "L", "M", "N", "O", "P", "Q", "R",
1536   "S", "T", "U", "V", "W", "X", "Y", "Z",
1537   NULL};
1538 
1539 
1540 int
numtoalpha(char * buf,int j,int i)1541 numtoalpha (char *buf, int j, int i)
1542 {
1543 
1544   buf[0] = '\0';
1545   if (i > 26 * 26)
1546     return -1;
1547   if (i > 26)
1548     {
1549       sprintf (buf, "%s%s", buf, ALPHA[(i - 1) / 26 - 1]);
1550       i = i - 26 * ((i - 1) / 26);
1551     }
1552   if (i < 1)
1553     i = 1;
1554   sprintf (buf, "%s%s", buf, ALPHA[i - 1]);
1555   sprintf (buf, "%s%d", buf, j);
1556 
1557 
1558   return 0;
1559 }
1560 int
numtoalpha2(char * buf,int j,int i,int dj,int di)1561 numtoalpha2 (char *buf, int j, int i, int dj, int di)
1562 {
1563 
1564   buf[0] = '\0';
1565   if (di)
1566     sprintf (buf, "$");
1567   if (i > 26 * 26)
1568     return -1;
1569   if (i > 26)
1570     {
1571       sprintf (buf, "%s%s", buf, ALPHA[(i - 1) / 26 - 1]);
1572       i = i - 26 * ((i - 1) / 26);
1573     }
1574   if (i < 1)
1575     i = 1;
1576   sprintf (buf, "%s%s", buf, ALPHA[i - 1]);
1577   if (dj)
1578     sprintf (buf, "%s$", buf);
1579   sprintf (buf, "%s%d", buf, j);
1580 
1581 
1582   return 0;
1583 }
1584 
1585 int
coltoalpha(char * buf,int i)1586 coltoalpha (char *buf, int i)
1587 {
1588 
1589   buf[0] = '\0';
1590   if (i > 26 * 26)
1591     return -1;
1592   if (i > 26)
1593     {
1594       sprintf (buf, "%s%s", buf, ALPHA[(i - 1) / 26 - 1]);
1595       i = i - 26 * ((i - 1) / 26);
1596     }
1597   if (i < 1)
1598     i = 1;
1599   sprintf (buf, "%s%s", buf, ALPHA[i - 1]);
1600 
1601 
1602   return 0;
1603 }
1604