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 "mainwin.h"
53 #include "worksheet.h"
54 #include "memory.h"
55 #include <X11/keysym.h>
56
57 #include "callback.h"
58 #include "buildall.h"
59 #include "popnindex.h"
60 #include "gr_interf.h"
61
62 extern int lindrag (double l);
63 extern int coldrag (double l);
64
65
66
67
68
69
70
71
72
73
74 static XtActionsRec actionsTable[] =
75 {
76 {"DocBtn1Up", cb_DocBtn1Up},
77 {"PopDown", cb_popdown},
78 {"DocMotion", cb_DocMotion},
79 {"DocBtn1Motion", cb_DocBtn1Motion},
80 {"DocBtn1Down", cb_DocBtn1Down},
81 {"DocBtn2Up", cb_DocBtn2Up},
82 {"DocBtn2Motion", cb_DocBtn2Motion},
83 {"DocBtn2Down", cb_DocBtn2Down},
84 {"DocUp", cb_TextUp},
85 {"DocDown", cb_TextDown},
86 {"DocLeft", cb_TextLeft},
87 {"DocRight", cb_TextRight},
88 {"TextBtn1Up", cb_TextChange},
89 {"TextReturn", cb_TextReturn},
90 {"TextUp", cb_TextUp},
91 {"TextDown", cb_TextDown},
92 {"TextLeft", cb_TextLeft},
93 {"TextRight", cb_TextRight},
94 {"DocBtn3Up", cb_DocBtn3Up},
95 {"Delete", cb_Delete},
96 {"DocDelete", cb_DocDelete},
97 {"BackSpace", cb_BackSpace},
98 {"PageUp", cb_PageUp},
99 {"PageDown", cb_PageDown},
100 {"PageLeft", cb_PageLeft},
101 {"PageRight", cb_PageRight},
102 {"PageHome", cb_PageHome},
103 {"Break", cb_Break},
104 };
105 static char defaultTranslations[] = "\
106 <Btn1Up>: DocBtn1Up()\n\
107 <Btn1Motion>: DocBtn1Motion()\n\
108 <Btn2Up>: DocBtn2Up()\n\
109 <Btn2Motion>: DocBtn2Motion()\n\
110 <Motion>: DocMotion()\n\
111 <Btn1Down>: PopDown() DocBtn1Down()\n\
112 <Btn2Down>: PopDown() DocBtn2Down()\n\
113 <Btn3Down>: PopDown()\n\
114 <Key>Delete: DocDelete() \n\
115 <Key>Page_Up: PageUp() \n\
116 <Key>Page_Down: PageDown() \n\
117 Shift<Key>Tab: PageLeft() \n\
118 <Key>Tab: PageRight() \n\
119 <Key>Home: PageHome() \n\
120 <Key>Up: DocUp() \n\
121 <Key>Down: DocDown() \n\
122 <Key>Left: DocLeft() \n\
123 <Key>Right: DocRight() \n\
124 Ctrl<Key>B: Break() \n\
125 ";
126
127 static char textTranslations[] = "\
128 <Key>Return: TextReturn(%d)\n\
129 <Key>KP_Enter: TextReturn(%d)\n\
130 <Key>osfUp: TextUp() \n\
131 <Key>osfDown: TextDown() \n\
132 <Key>osfLeft: TextLeft() \n\
133 <Key>osfRight: TextRight() \n\
134 <Key>Up: TextUp() \n\
135 <Key>Down: TextDown() \n\
136 <Key>Left: TextLeft() \n\
137 <Key>Right: TextRight() \n\
138 <Key>BackSpace: BackSpace() \n\
139 <Key>Delete: Delete() \n\
140 <Btn1Up>: TextBtn1Up()\n\
141 <Key>Home: PageHome() \n\
142 <Key>Up: DocUp() \n\
143 <Key>Page_Up: PageUp() \n\
144 <Key>Page_Down: PageDown() \n\
145 Shift<Key>Tab: PageLeft() \n\
146 <Key>Tab: PageRight() \n\
147 ";
148
149
150
151
152
153
154
155 static char popupTranslations[] = "\
156 <Motion>: unhighlight() highlight()\n\
157 <Btn1Up>: PopDown() notify() unhighlight()\n\
158 ";
159
160 Mainwin *ActiveMainwin = NULL;
161
162 void
scrollv(v)163 scrollv (v)
164 int v;
165 {
166 double V = v;
167 if (!BatchMode)
168 lindrag (V);
169 return;
170 }
171
172 void
scrollh(v)173 scrollh (v)
174 int v;
175 {
176 double V = v;
177 if (!BatchMode)
178 coldrag (V);
179 return;
180 }
181
182 void
setscrollv(double v)183 setscrollv (double v)
184 {
185 if (BatchMode)
186 return;
187 setscrollval (ActiveMainwin->scrollv, v);
188
189 }
190
191 void
setscrollh(double v)192 setscrollh (double v)
193 {
194 if (!BatchMode)
195 setscrollval (ActiveMainwin->scrollh, v);
196 }
197
198 void
setscrollvlimit(vmin,vmax)199 setscrollvlimit (vmin, vmax)
200 int vmin, vmax;
201 {
202 if (BatchMode)
203 return;
204
205
206
207
208
209
210
211
212
213
214
215
216 setscrolllimit (ActiveMainwin->scrollv, vmin, vmax);
217 }
218
219 void
setscrollhlimit(vmin,vmax)220 setscrollhlimit (vmin, vmax)
221 int vmin, vmax;
222 {
223 if (!BatchMode)
224 setscrolllimit (ActiveMainwin->scrollh, vmin, vmax);
225 }
226
227 extern GC getTheGc ();
228 extern Pixel getforeground ();
229 extern Pixel getbackground ();
230
231 int
popupcell(int dx,int dy,int w,int h)232 popupcell (int dx, int dy, int w, int h)
233 {
234 w_n = 0;
235 if (BatchMode)
236 return 0;
237 XtSetArg (w_args[w_n], XtNforeground, (Pixel) getcolor (cell_getfg (ActiveCell)));
238 w_n++;
239 XtSetArg (w_args[w_n], XtNbackground, (Pixel) getcolor (cell_getbg (ActiveCell)));
240 w_n++;
241 XtSetArg (w_args[w_n], XtNfont, (XFontStruct *) font2fontstr (cell_getfont (ActiveCell),
242 cell_getfontw (ActiveCell), cell_getfonts (ActiveCell)));
243 w_n++;
244
245 w_rel (NULL, dx + 40, dy + 20);
246 w_dim (w, h);
247 XtSetValues (ActiveMainwin->cell, w_args, w_n);
248 XtManageChild (ActiveMainwin->cell);
249 return 0;
250 }
251
252
253 int
LetextLooseFocus()254 LetextLooseFocus ()
255 {
256 if (ActiveMainwin == NULL)
257 return 0;
258 XtSetSensitive (ActiveMainwin->commandline_obj->text, False);
259 XtSetKeyboardFocus (ActiveMainwin->toplevel, ActiveMainwin->draw);
260 XawTextDisplayCaret (ActiveMainwin->commandline_obj->text, False);
261 return 0;
262 }
263
264 int
LetextGetFocus()265 LetextGetFocus ()
266 {
267 if (ActiveMainwin == NULL)
268 return 0;
269
270 XtSetSensitive (ActiveMainwin->commandline_obj->text, True);
271 XtSetKeyboardFocus (ActiveMainwin->toplevel, ActiveMainwin->commandline_obj->text);
272 XawTextDisplayCaret (ActiveMainwin->commandline_obj->text, True);
273 return 0;
274 }
275
276 int
mainwin_inform(char * str)277 mainwin_inform (char *str)
278 {
279 Arg arglist[1];
280 if (BatchMode)
281 return 0;
282 XtSetArg (arglist[0], XtNstring, str);
283 XtSetValues (ActiveMainwin->commandline_obj->info, arglist, 1);
284 return 0;
285 }
286
287 Display *
mainwin_drawdpy()288 mainwin_drawdpy ()
289 {
290 if (BatchMode)
291 return NULL;
292 else
293 return XtDisplay (ActiveMainwin->draw);
294 }
295
296
297
298 int
popdowncell()299 popdowncell ()
300 {
301 if (BatchMode)
302 return 0;
303 XtUnmanageChild (ActiveMainwin->cell);
304 return 0;
305 }
306
307 Widget
make_cell(Widget parent,char * name)308 make_cell (Widget parent, char *name)
309 {
310 Widget entry;
311 w_n = 0;
312 XtSetArg (w_args[w_n], XtNeditType, XawtextEdit);
313 w_n++;
314 XtSetArg (w_args[w_n], XtNborderWidth, 1);
315 w_n++;
316 XtSetArg (w_args[w_n], XtNbackground, abswhite);
317 w_n++;
318 XtSetArg (w_args[w_n], XtNforeground, absblack);
319 w_n++;
320 w_dim (50, 18);
321 w_rel (NULL, 0, 0);
322 entry = XtCreateManagedWidget ("Cell", asciiTextWidgetClass, parent, w_args, w_n);
323 XawTextDisplayCaret (entry, False);
324
325 return entry;
326 }
327
328
329 void
mainwin_resize(widget,pointer,event,ctd)330 mainwin_resize (widget, pointer, event, ctd)
331 Widget widget;
332 XtPointer pointer;
333 XEvent *event;
334 Boolean *ctd;
335 {
336 int n;
337 Dimension h, w;
338 Widget wi;
339 Arg args[2];
340 wi = (Widget) pointer;
341
342 n = 0;
343 XtSetArg (args[n], XtNheight, &h);
344 n++;
345 XtSetArg (args[n], XtNwidth, &w);
346 n++;
347 XtGetValues (wi, args, n);
348 cb_resize (w, h);
349 }
350
351 Mainwin *
newmainwin(Widget top)352 newmainwin (Widget top)
353 {
354 XtTranslations trans_table;
355 XtTranslations text_trans_table;
356 XtTranslations popup_trans_table;
357 Mainwin *mainwin = (Mainwin *) absmalloc (sizeof (Mainwin), "newmainwin:mainwin");
358 ActiveMainwin = mainwin;
359
360 mainwin->toplevel = top;
361 mainwin->dpy = XtDisplay (top);
362 mainwin->screen = DefaultScreen (mainwin->dpy);
363
364 w_n = 0;
365 w_dim (800, 600);
366 w_inbord (0);
367 w_arg (XtNdefaultDistance, 1);
368 mainwin->baseform = XtCreateManagedWidget
369 ("main_window", formWidgetClass, mainwin->toplevel, w_args, w_n);
370
371 w_n = 0;
372 w_dim (800, 20);
373 w_rel (NULL, 0, 0);
374 w_bord (0);
375 w_inbord (0);
376 w_noresize ();
377 mainwin->menubar = XtCreateManagedWidget
378 ("menubar", boxWidgetClass, mainwin->baseform, w_args, w_n);
379 mainwin->menubar_obj = (Menubar *) newmenubar (mainwin->menubar);
380
381 XtAddEventHandler (mainwin->menubar, ExposureMask, FALSE, shadow, (XtPointer) mainwin->menubar);
382
383 w_n = 0;
384 w_dim (800, 20);
385 w_relh (NULL, 0);
386 w_relv (mainwin->menubar, 0);
387 w_bord (0);
388 w_inbord (0);
389 w_noresize ();
390 mainwin->buttonbar = XtCreateManagedWidget
391 ("buttonbar", boxWidgetClass, mainwin->baseform, w_args, w_n);
392 XtAddEventHandler (mainwin->buttonbar, ExposureMask, FALSE, shadow, (XtPointer) mainwin->buttonbar);
393 mainwin->buttonbar_ismanaged = 1;
394
395 w_n = 0;
396 w_dim (800, 20);
397 w_relh (NULL, 0);
398 w_relv (mainwin->buttonbar, 0);
399 w_bord (0);
400 w_inbord (0);
401 w_noresize ();
402 mainwin->formatbar = XtCreateManagedWidget
403 ("formatbar", boxWidgetClass, mainwin->baseform, w_args, w_n);
404 XtAddEventHandler (mainwin->formatbar, ExposureMask, FALSE, shadow, (caddr_t) mainwin->formatbar);
405 mainwin->formatbar_ismanaged = 1;
406
407
408 w_n = 0;
409 w_dim (800, 25);
410 w_relh (NULL, 0);
411 w_relv (mainwin->formatbar, 0);
412 w_bord (0);
413 w_novresize ();
414 w_hresize ();
415 w_arg (XtNright, XtChainRight);
416 mainwin->commandline = XtCreateManagedWidget
417 ("commandline", formWidgetClass, mainwin->baseform, w_args, w_n);
418
419
420 mainwin->commandline_obj = newcommandline (mainwin->commandline);
421
422
423 w_n = 0;
424 w_relh (NULL, 0);
425 w_relv (mainwin->commandline, 0);
426 w_bord (0);
427 w_arg (XtNleft, XtChainLeft);
428 w_arg (XtNtop, XtChainTop);
429 w_arg (XtNbottom, XtChainBottom);
430 w_arg (XtNright, XtChainRight);
431 w_arg (XtNdefaultDistance, 0);
432
433 mainwin->wbform = (Widget) XtCreateManagedWidget ("window", formWidgetClass, mainwin->baseform,
434 w_args, w_n);
435 XtAddEventHandler (mainwin->wbform, ExposureMask, FALSE, shadow, (XtPointer) mainwin->wbform);
436 XtAddEventHandler (mainwin->wbform, StructureNotifyMask, False, mainwin_resize, (XtPointer) mainwin->wbform);
437
438 w_n = 0;
439 w_dim (40, 20);
440 w_rel (NULL, 0, 0);
441 w_bord (0);
442 w_noresize ();
443 mainwin->corner = (Widget) XtCreateManagedWidget ("window", simpleWidgetClass, mainwin->wbform,
444 w_args, w_n);
445
446 w_n = 0;
447 w_dim (40, 510);
448 w_relh (NULL, 0);
449 w_relv (mainwin->corner, 0);
450 w_bord (0);
451 w_nohresize ();
452 w_vresize ();
453 w_arg (XtNtop, XtChainTop);
454 mainwin->linindex = (Widget) XtCreateManagedWidget ("window", panedWidgetClass, mainwin->wbform,
455 w_args, w_n);
456
457 w_n = 0;
458 w_dim (774, 20);
459 w_relv (NULL, 0);
460 w_relh (mainwin->corner, 0);
461 w_bord (0);
462 w_novresize ();
463 w_hresize ();
464 w_arg (XtNleft, XtChainLeft);
465 mainwin->colindex = (Widget) XtCreateManagedWidget ("window", panedWidgetClass, mainwin->wbform,
466 w_args, w_n);
467
468 w_n = 0;
469 w_dim (774, 510);
470 w_relv (mainwin->colindex, 0);
471 w_relh (mainwin->linindex, 0);
472 w_bord (0);
473 w_hresize ();
474 w_vresize ();
475 mainwin->draw = (Widget) XtCreateManagedWidget ("window", panedWidgetClass, mainwin->wbform,
476 w_args, w_n);
477
478
479
480
481
482 w_n = 0;
483 w_relv (mainwin->commandline, 0);
484 w_relh (mainwin->wbform, 1);
485 w_bord (0);
486 w_arg (XtNtop, XtChainTop);
487 w_arg (XtNleft, XtChainRight);
488 w_arg (XtNright, XtChainRight);
489 w_arg (XtNbottom, XtChainBottom);
490 w_arg (XtNdefaultDistance, 1);
491 mainwin->scrollv = CreateMyscroll (mainwin->baseform, 528, 14, 0, 400, scrollv);
492
493
494
495
496
497
498
499 w_n = 0;
500 w_dim (80, 20);
501 w_relh (NULL, 0);
502 w_relv (mainwin->wbform, 0);
503 w_bord (0);
504 w_inbord (0);
505 w_arg (XtNdefaultDistance, 0);
506 w_arg (XtNtop, XtChainBottom);
507 w_arg (XtNbottom, XtChainBottom);
508 w_arg (XtNleft, XtChainLeft);
509 w_arg (XtNright, XtChainLeft);
510 mainwin->sheetbtn = XtCreateManagedWidget ("window", formWidgetClass, mainwin->baseform,
511 w_args, w_n);
512
513 w_n = 0;
514 w_dim (320, 20);
515 w_relh (NULL, 80);
516 w_relv (mainwin->wbform, 0);
517 w_bord (0);
518 w_inbord (0);
519 w_arg (XtNtop, XtChainBottom);
520 w_arg (XtNbottom, XtChainBottom);
521 w_arg (XtNleft, XtChainLeft);
522 w_arg (XtNright, XtChainLeft);
523 mainwin->sheetselform = XtCreateManagedWidget ("window", formWidgetClass, mainwin->baseform,
524 w_args, w_n);
525 build_thumb (mainwin->sheetselform);
526
527
528 w_n = 0;
529 w_relv (mainwin->wbform, 1);
530 w_relh (NULL, 453);
531 w_bord (0);
532 w_arg (XtNtop, XtChainBottom);
533 w_arg (XtNbottom, XtChainBottom);
534 w_arg (XtNleft, XtChainLeft);
535 w_arg (XtNright, XtChainRight);
536 w_arg (XtNdefaultDistance, 1);
537 mainwin->scrollh = CreateMyscroll (mainwin->baseform, 14, 350, 0, NB_COLUMN, scrollh);
538
539
540 mainwin->buttonbar_obj = newbuttonbar (mainwin->buttonbar);
541 mainwin->formatbar_obj = newformatbar (mainwin->formatbar);
542
543
544 mainwin->popup = (Widget) make_alone_popup (mainwin->wbform, "pop1");
545
546
547 XtAddEventHandler (mainwin->draw, ExposureMask, FALSE, event_Repaint, NULL);
548
549
550 XtAppAddActions (XtWidgetToApplicationContext (mainwin->toplevel), actionsTable, XtNumber (actionsTable));
551
552 trans_table = XtParseTranslationTable (defaultTranslations);
553 text_trans_table = XtParseTranslationTable (textTranslations);
554 popup_trans_table = XtParseTranslationTable (popupTranslations);
555
556 XtOverrideTranslations (mainwin->commandline_obj->text, text_trans_table);
557
558
559 XtOverrideTranslations (mainwin->popup, popup_trans_table);
560
561 XtOverrideTranslations (mainwin->draw, trans_table);
562 XtOverrideTranslations (mainwin->linindex, trans_table);
563 XtOverrideTranslations (mainwin->colindex, trans_table);
564 XtOverrideTranslations (mainwin->corner, trans_table);
565
566 XtAddEventHandler (mainwin->draw, ButtonReleaseMask, True, event_DocBtn3Up,
567 (XtPointer) mainwin->popup);
568
569 XtAddEventHandler (mainwin->commandline_obj->text, KeyReleaseMask, True,
570 event_KeyReleased, mainwin->commandline_obj->text);
571 XtAddEventHandler (mainwin->commandline_obj->text, KeyPressMask, True,
572 event_KeyPressed, mainwin->commandline_obj->text);
573 XtAddEventHandler (mainwin->draw, KeyReleaseMask, True,
574 event_KeyReleased, mainwin->commandline_obj->text);
575 XtAddEventHandler (mainwin->draw, KeyPressMask, True,
576 event_KeyPressed, mainwin->commandline_obj->text);
577
578 XtRealizeWidget (mainwin->toplevel);
579
580 mainwin->draw_window = XtWindow (mainwin->draw);
581
582
583 mainwin->cell = make_cell (mainwin->wbform, "Cell");
584 popdowncell ();
585
586 return mainwin;
587 }
588
589
590 int
mainwin_popup_abv()591 mainwin_popup_abv ()
592 {
593 XtManageChild (ActiveMainwin->abv_editor);
594 return 0;
595 }
596
597 int
mainwin_popdown_abv()598 mainwin_popdown_abv ()
599 {
600 XtUnmanageChild (ActiveMainwin->abv_editor);
601 return 0;
602 }
603
604 void
buttonbar_onoff()605 buttonbar_onoff ()
606 {
607 if (ActiveMainwin == NULL)
608 return;
609
610 if (ActiveMainwin->buttonbar_ismanaged)
611 {
612 XtUnmanageChild (ActiveMainwin->buttonbar);
613 ActiveMainwin->buttonbar_ismanaged = 0;
614
615 w_n = 0;
616 w_relv (ActiveMainwin->menubar, 0);
617 if (ActiveMainwin->formatbar_ismanaged)
618 {
619 w_set (ActiveMainwin->formatbar);
620 }
621 else
622 {
623 w_set (ActiveMainwin->commandline);
624 }
625 }
626 else
627 {
628 XtManageChild (ActiveMainwin->buttonbar);
629 ActiveMainwin->buttonbar_ismanaged = 1;
630 w_n = 0;
631 w_relv (ActiveMainwin->buttonbar, 0);
632 if (ActiveMainwin->formatbar_ismanaged)
633 {
634 w_set (ActiveMainwin->formatbar);
635 }
636 else
637 {
638 w_set (ActiveMainwin->commandline);
639 }
640 }
641
642 }
643 void
formatbar_onoff()644 formatbar_onoff ()
645 {
646 if (ActiveMainwin == NULL)
647 return;
648 if (ActiveMainwin->formatbar_ismanaged)
649 {
650 XtUnmanageChild (ActiveMainwin->formatbar);
651 ActiveMainwin->formatbar_ismanaged = 0;
652 w_n = 0;
653 if (ActiveMainwin->buttonbar_ismanaged)
654 {
655 w_relv (ActiveMainwin->buttonbar, 0);
656 }
657 else
658 {
659 w_relv (ActiveMainwin->menubar, 0);
660 }
661 w_set (ActiveMainwin->commandline);
662 }
663 else
664 {
665 ActiveMainwin->formatbar_ismanaged = 1;
666 XtManageChild (ActiveMainwin->formatbar);
667 w_relv (ActiveMainwin->formatbar, 0);
668 w_set (ActiveMainwin->commandline);
669 }
670
671 }
672
673 void
NewWindow()674 NewWindow ()
675 {
676 ActiveMainwin = newmainwin ((Widget) gettop ());
677 }
678
679
680
681 void
event_KeyPressed(Widget widget,XtPointer pointer,XEvent * event,Boolean * ctd)682 event_KeyPressed (Widget widget, XtPointer pointer, XEvent * event, Boolean * ctd)
683
684 {
685
686
687
688
689 switch (event->xkey.keycode)
690 {
691 case 50:
692 cb_Shift (1);
693 break;
694 case 62:
695 cb_Shift (2);
696 break;
697 case 37:
698 cb_Control (1);
699 break;
700
701 }
702
703 }
704
705 void
event_KeyReleased(Widget widget,XtPointer pointer,XEvent * event,Boolean * ctd)706 event_KeyReleased (Widget widget, XtPointer pointer, XEvent * event, Boolean * ctd)
707
708 {
709
710
711 if (event->xkey.keycode >= 67 && event->xkey.keycode <= 76)
712 return;
713 switch (event->xkey.keycode)
714 {
715 case 50:
716 cb_Shift (0);
717 case 62:
718 cb_Shift (0);
719 case 37:
720 cb_Control (0);
721 case 95:
722 case 96:
723 case 111:
724 case 115:
725 case 117:
726 case 9:
727 case 23:
728 case 66:
729 case 64:
730 case 113:
731 case 106:
732 case 107:
733 return;
734
735 }
736 KeyPressed (event->xkey.keycode);
737 }
738