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