Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / programs / dtcm / dtcm / yearglance.c
1 /*******************************************************************************
2 **
3 **  yearglance.c
4 **
5 **  $XConsortium: yearglance.c /main/8 1996/11/21 19:43:58 drk $
6 **
7 **  RESTRICTED CONFIDENTIAL INFORMATION:
8 **
9 **  The information in this document is subject to special
10 **  restrictions in a confidential disclosure agreement between
11 **  HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
12 **  document outside HP, IBM, Sun, USL, SCO, or Univel without
13 **  Sun's specific written approval.  This document and all copies
14 **  and derivative works thereof must be returned or destroyed at
15 **  Sun's request.
16 **
17 **  Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
18 **
19 *******************************************************************************/
20
21 /*                                                                      *
22  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
23  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
24  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
25  * (c) Copyright 1993, 1994 Novell, Inc.                                *
26  */
27
28 #ifndef lint
29 static char sccsid[] = "@(#)yearglance.c 1.37 95/07/27 Copyr 1991 Sun Microsystems, Inc.";
30 #endif
31
32 #include <EUSCompat.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <sys/stat.h>
37 #include <sys/param.h> /* MAXPATHLEN defined here */
38 #ifdef SVR4
39 #include <sys/utsname.h> /* SYS_NMLN */
40 #endif /* SVR4 */
41 #include <dirent.h>
42 #include <ctype.h>
43 #include <string.h>
44 #include <sys/time.h>
45 #include <rpc/rpc.h>
46 #include <sys/resource.h>
47 #include <sys/wait.h>
48 #include <netinet/in.h>
49 #include <X11/Xlib.h>
50 #include <Xm/Label.h>
51 #include <Xm/PushB.h>
52 #include <Xm/Form.h>
53 #include <Xm/ToggleBG.h>
54 #include "MonthPanel.h"
55 #include "util.h"
56 #include "timeops.h"
57 #include "select.h"
58 #include "editor.h"
59 #include "x_graphics.h"
60 #include "dayglance.h"
61 #include "weekglance.h"
62 #include "monthglance.h"
63 #include "yearglance.h"
64 #include "calendar.h"
65 #include "help.h"
66
67 #define XOS_USE_XT_LOCKING
68 #define X_INCLUDE_TIME_H
69 #include <X11/Xos_r.h>
70
71 /* if positioning of months in year view changes this must change too!
72 This specifies row and col for easy selection in year view */
73 int month_row_col[12][2] =
74 {
75   0, 0,
76   0, 1,
77   0, 2,
78   1, 0,
79   1, 1,
80   1, 2,
81   2, 0,
82   2, 1,
83   2, 2,
84   3, 0,
85   3, 1,
86   3, 2,
87 };
88
89 extern int prolog_found;
90
91 extern char *months[];
92 extern char *months2[];
93
94 static void     create_month_panels(Calendar *);
95 static void     year_btn_cb(Widget , XtPointer , XtPointer );
96 static void     update_year(Calendar *);
97 static void     allocator(Calendar *);
98 static void     deallocator(Calendar *);
99 static Boolean  allocated(Calendar *);
100
101
102 /*
103  * check whether year view is allocated yet
104  */
105 static Boolean
106 allocated(Calendar *c)
107 {
108         return (c->view->year_info != NULL);
109 }
110
111
112 /*
113  * allocate memory used for year view
114  */
115 static void
116 allocator(Calendar *c)
117 {
118         Year *y;
119
120         c->view->year_info = (caddr_t) ckalloc(sizeof(Year));
121
122         y = (Year *)c->view->year_info;
123         y->month_panels =
124                 (Widget *) ckalloc(12 * sizeof(Widget));
125
126         /* create the month panel widgets */
127         create_month_panels(c);
128 }
129
130
131 /*
132  * release memory used for year view
133  */
134 static void
135 deallocator(Calendar *c)
136 {
137         Year *y = (Year *)c->view->year_info;
138 /*
139         int i;
140 */
141
142         XtDestroyWidget(y->form);
143         XtDestroyWidget(y->label_form);
144
145         /* Destroy Monthpanels and free space for caching them */
146 /*
147         for (i=0; i<12; i++)
148                 XtDestroyWidget(y->month_panels[i]);
149 */
150  
151         free(y->month_panels); 
152
153         /* free memory stored in calendar for year stuff */
154         free(y);
155         c->view->year_info = NULL;
156
157 }
158
159 extern CSA_return_code
160 paint_year(Calendar *c)
161 {
162         Year *y;        /* can't initialize this - may not be alloc'd yet! */
163
164         if (!c->cal_handle)
165                 return(CSA_E_INVALID_PARAMETER);
166
167         XmToggleButtonGadgetSetState(c->year_scope, True, False);
168         
169 /*
170  * Do memory allocation if necessary
171  */
172         if (!allocated(c))
173                 allocator(c);
174
175         y = (Year *)c->view->year_info;
176 /*
177  * update year displayed if it's not 'current'
178  */
179         if (y->year_shown != year(c->view->date))
180                 update_year(c);
181
182 /*
183  * swap in year form if it's not already there
184  */
185         XtUnmapWidget(c->canvas);
186         XtManageChild(y->label_form);
187         XtMapWidget(y->label_form);
188         XtManageChild(y->form);
189         XtMapWidget(y->form);
190
191         return(CSA_SUCCESS);
192 }
193
194 /*
195  * handler for button to switch to year view.
196  */
197 void
198 year_button (Widget widget, XtPointer data, XtPointer cbs)
199 {
200         Calendar *c = calendar;
201
202         if (c->view->glance == yearGlance)
203                 return;
204
205         XtUnmapWidget(c->canvas);
206
207         switch (c->view->glance) {
208                 case dayGlance:
209                         c->view->glance = yearGlance;
210                         cleanup_after_dayview(c);
211                         break;
212                 case weekGlance:
213                         c->view->glance = yearGlance;
214                         cleanup_after_weekview(c);
215                         break;
216                 case monthGlance:
217                         c->view->glance = yearGlance;
218                         cleanup_after_monthview(c);
219                         break;
220                 default:
221                         break;
222         }
223         paint_year(c);
224
225 }
226
227 extern void
228 print_std_year_range(int start_year, int end_year)
229 {
230         void *xp;
231         int n, year;
232         Calendar *c = calendar;
233
234         if ((xp = x_open_file(c)) == (void *)NULL)
235           return;
236
237         n = end_year - start_year + 1;
238
239         year = start_year;
240         if (n <= 0)
241                 n = 1;
242
243         for (; n > 0; n--)
244         {
245           x_init_printer(xp, PORTRAIT);
246           x_init_std_year(xp);
247
248           /* print the year at top */
249           x_std_year_name(xp, year);
250
251           x_print_year(xp, year);
252           x_finish_printer(xp);
253           year++;
254         }
255
256         x_print_file(xp, c);
257 }
258
259 /*
260  * Set the panels to the right year/month display
261  */
262 static void
263 update_year(Calendar *c)
264 {
265         XmString str;
266         char buf[BUFSIZ];
267         Year *y = (Year *)c->view->year_info;
268         int year_num = year(c->view->date);
269         int i;
270         _Xltimeparams localtime_buf;
271
272         /* change year label in yearview */
273         /* NL_COMMENT
274            Attention Translator:
275
276            This string is used in the calendar year view.  In the C locale
277            it has the form:
278
279                 1995
280
281            strftime conversion string: "%Y" is used.
282
283            Use the appropriate strftime conversion for your locale.
284         */
285         strftime(buf, 100, catgets(c->DT_catd, 1, 1092, "%Y"), 
286                  _XLocaltime(&c->view->date, localtime_buf));
287         XtVaGetValues(y->year_label, XmNlabelString, &str, NULL);
288         XmStringFree(str);
289         str = XmStringCreateLocalized(buf);
290         XtVaSetValues(y->year_label, XmNlabelString, str, NULL);
291
292         /* update each panel */
293         for (i=0; i<12; i++) {
294                 XtVaSetValues(y->month_panels[i], XmNyear, year_num,
295                                 XmNmonth, i+1, NULL);
296         }
297
298         /* record the year that's been displayed */
299         y->year_shown = year_num;
300 }
301
302
303 /*
304  * this creates the 3x4 grid of MonthPanel widgets that
305  * make up the year view.
306  */
307 static void
308 create_month_panels(Calendar *c) {
309         Year *y = (Year *)c->view->year_info;
310         Arg al[20];
311         int ac=0, i=0;
312
313 /*
314  * This array holds the relative positioning for panels
315  * in the year view.  Each row below is a set of 4 numbers:
316  * top, right, bottom, left.  The numbers are used as the
317  * appropriate <side>Position constraints on the MonthPanel
318  * children of the Form.  Relies on Form's fractionBase resource
319  * being set up as 100, which is is at create time.
320  */
321 static int anchors[] = {
322         0,      33,     24,     1,
323         0,      66,     24,     34,
324         0,      99,     24,     67,
325         25,     33,     49,     1,
326         25,     66,     49,     34,
327         25,     99,     49,     67,
328         50,     33,     74,     1,
329         50,     66,     74,     34,
330         50,     99,     74,     67,
331         75,     33,     100,    1,
332         75,     66,     100,    34,
333         75,     99,     100,    67
334 }; 
335  /*
336 static int anchors[] = {
337         0,      24,     33,     1,
338         0,      49,     33,     26,
339         0,      74,     33,     51,
340         0,      99,     33,     76,
341         34,     24,     66,     1,
342         34,     49,     66,     26,
343         34,     74,     66,     51,
344         34,     99,     66,     76,
345         67,     24,     100,    1,
346         67,     49,     100,    26,
347         67,     74,     100,    51,
348         67,     99,     100,    76
349 };  */
350
351
352 /*
353  * Create a container to replace the old canvas
354  */
355         ac=0;
356         XtSetArg(al[ac], XmNtopAttachment, XmATTACH_WIDGET); ac++;
357         XtSetArg(al[ac], XmNtopWidget, c->previous); ac++;
358         XtSetArg(al[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
359         XtSetArg(al[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
360         XtSetArg(al[ac], XmNleftOffset, 1); ac++;
361         y->label_form = XmCreateForm(c->form, "labelForm", al, ac);
362
363         ac=0;
364         XtSetArg(al[ac], XmNtopAttachment, XmATTACH_WIDGET); ac++;
365         XtSetArg(al[ac], XmNtopWidget, y->label_form); ac++;
366         XtSetArg(al[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
367         XtSetArg(al[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
368         XtSetArg(al[ac], XmNleftOffset, 1); ac++;
369         XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_POSITION); ac++;
370         XtSetArg(al[ac], XmNbottomPosition, 95); ac++;
371         y->form = XmCreateForm(c->form, "yearForm", al, ac);
372         XtVaSetValues(c->message_text, XmNtopWidget, y->form, NULL);
373         XtManageChild(y->label_form);
374         XtUnmapWidget(y->label_form);
375         XtManageChild(y->form);
376         XtUnmapWidget(y->form);
377
378         XtAddCallback(y->form, XmNhelpCallback, (XtCallbackProc)help_cb, 
379                         (XtPointer) CALENDAR_HELP_YEAR_WINDOW);
380
381 /*
382  * Label displaying year for this view
383  */
384         ac=0;
385         XtSetArg(al[ac], XmNtopAttachment, XmATTACH_FORM); ac++;
386         XtSetArg(al[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
387         y->year_label = XmCreateLabel(y->label_form, "yearLabel", al, ac);
388         XtManageChild(y->year_label);
389
390 /*
391  * Create 12 monthPanels
392  */
393         /* fixed part of arglist */
394         ac=0;
395         XtSetArg(al[ac], XmNtopAttachment, XmATTACH_POSITION); ac++;
396         XtSetArg(al[ac], XmNrightAttachment, XmATTACH_POSITION); ac++;
397         XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_POSITION); ac++;
398         XtSetArg(al[ac], XmNleftAttachment, XmATTACH_POSITION); 
399
400         for (i=0; i<12; i++) {
401                 char name[BUFSIZ];
402
403                 sprintf(name, "panel%d", i+1);
404                 ac=4;           /* start adding after fixed part */
405                 XtSetArg(al[ac], XmNtopPosition, anchors[4*i]); ac++;
406                 XtSetArg(al[ac], XmNrightPosition, anchors[4*i+1]); ac++;
407                 XtSetArg(al[ac], XmNbottomPosition, anchors[4*i+2]); ac++;
408                 XtSetArg(al[ac], XmNleftPosition, anchors[4*i+3]); ac++;
409                 XtSetArg(al[ac], XmNwidth, 150); ac++;
410                 XtSetArg(al[ac], XmNheight, 100); ac++;
411                 y->month_panels[i] = XmCreateMonthPanel(y->form, name, al, ac);
412                 XtAddCallback(y->month_panels[i], XmNactivateCallback,
413                                 year_btn_cb, (XtPointer) c);
414         }
415         XtManageChildren(y->month_panels, 12);
416 }
417
418 /*
419  * This is the callback for all the buttons on the month
420  * panels.  It figures out what was pressed, and invokes
421  * the appropriate function.
422  */
423 static void
424 year_btn_cb(Widget w, XtPointer client, XtPointer call)
425 {
426         XmMonthPanelCallbackStruct *cbs = (XmMonthPanelCallbackStruct *) call;
427         Calendar *c = (Calendar *)client;
428         int monthno, year;
429  
430         XtVaGetValues(w, XmNmonth, &monthno, XmNyear, &year, NULL);
431
432         if (cbs->type == MONTH_SELECTION) {
433                 /*
434                  * Date update:
435                  *      if month chosen is not current month
436                  *      set date to 1st of new month, otherwise
437                  *      don't change date.
438                  */
439                 if (month(c->view->date) != monthno) {
440                         calendar_deselect (c);
441                         c->view->olddate = c->view->date;
442                         c->view->date = monthdayyear(monthno, 1, year);
443                         c->view->nwks = numwks(c->view->date);
444                         calendar_select (c, monthSelect, NULL);
445                 }
446  
447
448                 /* switch to month view */
449                 cleanup_after_yearview(c);
450                 c->view->glance = monthGlance;
451                 prepare_to_paint_monthview(c, NULL);
452                 XtMapWidget(c->canvas);
453         }
454         else {  /* type == DAY_SELECTION */
455                 c->view->olddate = c->view->date;
456                 c->view->date = monthdayyear(monthno, cbs->day, year);
457                 cleanup_after_yearview(c);
458
459                 c->view->glance = dayGlance;
460                 init_mo(c);
461                 (void)init_dayview(c);
462
463                 XtMapWidget(c->canvas);
464                 paint_day(c);
465         }
466 }
467
468 extern void
469 cleanup_after_yearview(Calendar *c)
470 {
471         Year *y = (Year *)c->view->year_info;
472
473         if (!y) return;
474
475         XtUnmapWidget(y->form);
476         XtUnmapWidget(y->label_form);
477
478         /* Unmanaging the form is necessary to keep the stupid 
479            traversal order right */
480
481         XtUnmanageChild(y->form);
482         XtUnmanageChild(y->label_form);
483         XmProcessTraversal(c->appt_btn, XmTRAVERSE_CURRENT); 
484
485         XmToggleButtonGadgetSetState(c->year_scope, False, False);
486
487         /*
488          * Reclaim year view memory.  You should comment this out
489          * if rendering performance is more important than
490          * minimizing memory usage.  It saves 400 *dget instances
491          * lying around when they may never be needed again, but
492          * at the expense of recreating them if they are needed.
493          */
494 /*
495         deallocator(c);
496 */
497 }
498