Linux compilation of dtcm (Calendar), (Matthew Howkins)
[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 #if defined(linux)
92 #undef SVR4
93 #endif
94 #include <X11/Xos_r.h>
95
96 /* if positioning of months in year view changes this must change too!
97 This specifies row and col for easy selection in year view */
98 int month_row_col[12][2] =
99 {
100   0, 0,
101   0, 1,
102   0, 2,
103   1, 0,
104   1, 1,
105   1, 2,
106   2, 0,
107   2, 1,
108   2, 2,
109   3, 0,
110   3, 1,
111   3, 2,
112 };
113
114 extern int prolog_found;
115
116 extern char *months[];
117 extern char *months2[];
118
119 static void     create_month_panels(Calendar *);
120 static void     year_btn_cb(Widget , XtPointer , XtPointer );
121 static void     update_year(Calendar *);
122 static void     allocator(Calendar *);
123 static void     deallocator(Calendar *);
124 static Boolean  allocated(Calendar *);
125
126
127 /*
128  * check whether year view is allocated yet
129  */
130 static Boolean
131 allocated(Calendar *c)
132 {
133         return (c->view->year_info != NULL);
134 }
135
136
137 /*
138  * allocate memory used for year view
139  */
140 static void
141 allocator(Calendar *c)
142 {
143         Year *y;
144
145         c->view->year_info = (caddr_t) ckalloc(sizeof(Year));
146
147         y = (Year *)c->view->year_info;
148         y->month_panels =
149                 (Widget *) ckalloc(12 * sizeof(Widget));
150
151         /* create the month panel widgets */
152         create_month_panels(c);
153 }
154
155
156 /*
157  * release memory used for year view
158  */
159 static void
160 deallocator(Calendar *c)
161 {
162         Year *y = (Year *)c->view->year_info;
163 /*
164         int i;
165 */
166
167         XtDestroyWidget(y->form);
168         XtDestroyWidget(y->label_form);
169
170         /* Destroy Monthpanels and free space for caching them */
171 /*
172         for (i=0; i<12; i++)
173                 XtDestroyWidget(y->month_panels[i]);
174 */
175  
176         free(y->month_panels); 
177
178         /* free memory stored in calendar for year stuff */
179         free(y);
180         c->view->year_info = NULL;
181
182 }
183
184 extern CSA_return_code
185 paint_year(Calendar *c)
186 {
187         Year *y;        /* can't initialize this - may not be alloc'd yet! */
188
189         if (!c->cal_handle)
190                 return(CSA_E_INVALID_PARAMETER);
191
192         XmToggleButtonGadgetSetState(c->year_scope, True, False);
193         
194 /*
195  * Do memory allocation if necessary
196  */
197         if (!allocated(c))
198                 allocator(c);
199
200         y = (Year *)c->view->year_info;
201 /*
202  * update year displayed if it's not 'current'
203  */
204         if (y->year_shown != year(c->view->date))
205                 update_year(c);
206
207 /*
208  * swap in year form if it's not already there
209  */
210         XtUnmapWidget(c->canvas);
211         XtManageChild(y->label_form);
212         XtMapWidget(y->label_form);
213         XtManageChild(y->form);
214         XtMapWidget(y->form);
215
216         return(CSA_SUCCESS);
217 }
218
219 /*
220  * handler for button to switch to year view.
221  */
222 void
223 year_button (Widget widget, XtPointer data, XtPointer cbs)
224 {
225         Calendar *c = calendar;
226
227         if (c->view->glance == yearGlance)
228                 return;
229
230         XtUnmapWidget(c->canvas);
231
232         switch (c->view->glance) {
233                 case dayGlance:
234                         c->view->glance = yearGlance;
235                         cleanup_after_dayview(c);
236                         break;
237                 case weekGlance:
238                         c->view->glance = yearGlance;
239                         cleanup_after_weekview(c);
240                         break;
241                 case monthGlance:
242                         c->view->glance = yearGlance;
243                         cleanup_after_monthview(c);
244                         break;
245                 default:
246                         break;
247         }
248         paint_year(c);
249
250 }
251
252 extern void
253 print_std_year_range(int start_year, int end_year)
254 {
255         void *xp;
256         int n, year;
257         Calendar *c = calendar;
258
259         if ((xp = x_open_file(c)) == (void *)NULL)
260           return;
261
262         n = end_year - start_year + 1;
263
264         year = start_year;
265         if (n <= 0)
266                 n = 1;
267
268         for (; n > 0; n--)
269         {
270           x_init_printer(xp, PORTRAIT);
271           x_init_std_year(xp);
272
273           /* print the year at top */
274           x_std_year_name(xp, year);
275
276           x_print_year(xp, year);
277           x_finish_printer(xp);
278           year++;
279         }
280
281         x_print_file(xp, c);
282 }
283
284 /*
285  * Set the panels to the right year/month display
286  */
287 static void
288 update_year(Calendar *c)
289 {
290         XmString str;
291         char buf[BUFSIZ];
292         Year *y = (Year *)c->view->year_info;
293         int year_num = year(c->view->date);
294         int i;
295         _Xltimeparams localtime_buf;
296
297         /* change year label in yearview */
298         /* NL_COMMENT
299            Attention Translator:
300
301            This string is used in the calendar year view.  In the C locale
302            it has the form:
303
304                 1995
305
306            strftime conversion string: "%Y" is used.
307
308            Use the appropriate strftime conversion for your locale.
309         */
310         strftime(buf, 100, catgets(c->DT_catd, 1, 1092, "%Y"), 
311                  _XLocaltime(&c->view->date, localtime_buf));
312         XtVaGetValues(y->year_label, XmNlabelString, &str, NULL);
313         XmStringFree(str);
314         str = XmStringCreateLocalized(buf);
315         XtVaSetValues(y->year_label, XmNlabelString, str, NULL);
316
317         /* update each panel */
318         for (i=0; i<12; i++) {
319                 XtVaSetValues(y->month_panels[i], XmNyear, year_num,
320                                 XmNmonth, i+1, NULL);
321         }
322
323         /* record the year that's been displayed */
324         y->year_shown = year_num;
325 }
326
327
328 /*
329  * this creates the 3x4 grid of MonthPanel widgets that
330  * make up the year view.
331  */
332 static void
333 create_month_panels(Calendar *c) {
334         Year *y = (Year *)c->view->year_info;
335         Arg al[20];
336         int ac=0, i=0;
337
338 /*
339  * This array holds the relative positioning for panels
340  * in the year view.  Each row below is a set of 4 numbers:
341  * top, right, bottom, left.  The numbers are used as the
342  * appropriate <side>Position constraints on the MonthPanel
343  * children of the Form.  Relies on Form's fractionBase resource
344  * being set up as 100, which is is at create time.
345  */
346 static int anchors[] = {
347         0,      33,     24,     1,
348         0,      66,     24,     34,
349         0,      99,     24,     67,
350         25,     33,     49,     1,
351         25,     66,     49,     34,
352         25,     99,     49,     67,
353         50,     33,     74,     1,
354         50,     66,     74,     34,
355         50,     99,     74,     67,
356         75,     33,     100,    1,
357         75,     66,     100,    34,
358         75,     99,     100,    67
359 }; 
360  /*
361 static int anchors[] = {
362         0,      24,     33,     1,
363         0,      49,     33,     26,
364         0,      74,     33,     51,
365         0,      99,     33,     76,
366         34,     24,     66,     1,
367         34,     49,     66,     26,
368         34,     74,     66,     51,
369         34,     99,     66,     76,
370         67,     24,     100,    1,
371         67,     49,     100,    26,
372         67,     74,     100,    51,
373         67,     99,     100,    76
374 };  */
375
376
377 /*
378  * Create a container to replace the old canvas
379  */
380         ac=0;
381         XtSetArg(al[ac], XmNtopAttachment, XmATTACH_WIDGET); ac++;
382         XtSetArg(al[ac], XmNtopWidget, c->previous); ac++;
383         XtSetArg(al[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
384         XtSetArg(al[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
385         XtSetArg(al[ac], XmNleftOffset, 1); ac++;
386         y->label_form = XmCreateForm(c->form, "labelForm", al, ac);
387
388         ac=0;
389         XtSetArg(al[ac], XmNtopAttachment, XmATTACH_WIDGET); ac++;
390         XtSetArg(al[ac], XmNtopWidget, y->label_form); ac++;
391         XtSetArg(al[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
392         XtSetArg(al[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
393         XtSetArg(al[ac], XmNleftOffset, 1); ac++;
394         XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_POSITION); ac++;
395         XtSetArg(al[ac], XmNbottomPosition, 95); ac++;
396         y->form = XmCreateForm(c->form, "yearForm", al, ac);
397         XtVaSetValues(c->message_text, XmNtopWidget, y->form, NULL);
398         XtManageChild(y->label_form);
399         XtUnmapWidget(y->label_form);
400         XtManageChild(y->form);
401         XtUnmapWidget(y->form);
402
403         XtAddCallback(y->form, XmNhelpCallback, (XtCallbackProc)help_cb, 
404                         (XtPointer) CALENDAR_HELP_YEAR_WINDOW);
405
406 /*
407  * Label displaying year for this view
408  */
409         ac=0;
410         XtSetArg(al[ac], XmNtopAttachment, XmATTACH_FORM); ac++;
411         XtSetArg(al[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
412         y->year_label = XmCreateLabel(y->label_form, "yearLabel", al, ac);
413         XtManageChild(y->year_label);
414
415 /*
416  * Create 12 monthPanels
417  */
418         /* fixed part of arglist */
419         ac=0;
420         XtSetArg(al[ac], XmNtopAttachment, XmATTACH_POSITION); ac++;
421         XtSetArg(al[ac], XmNrightAttachment, XmATTACH_POSITION); ac++;
422         XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_POSITION); ac++;
423         XtSetArg(al[ac], XmNleftAttachment, XmATTACH_POSITION); 
424
425         for (i=0; i<12; i++) {
426                 char name[BUFSIZ];
427
428                 sprintf(name, "panel%d", i+1);
429                 ac=4;           /* start adding after fixed part */
430                 XtSetArg(al[ac], XmNtopPosition, anchors[4*i]); ac++;
431                 XtSetArg(al[ac], XmNrightPosition, anchors[4*i+1]); ac++;
432                 XtSetArg(al[ac], XmNbottomPosition, anchors[4*i+2]); ac++;
433                 XtSetArg(al[ac], XmNleftPosition, anchors[4*i+3]); ac++;
434                 XtSetArg(al[ac], XmNwidth, 150); ac++;
435                 XtSetArg(al[ac], XmNheight, 100); ac++;
436                 y->month_panels[i] = XmCreateMonthPanel(y->form, name, al, ac);
437                 XtAddCallback(y->month_panels[i], XmNactivateCallback,
438                                 year_btn_cb, (XtPointer) c);
439         }
440         XtManageChildren(y->month_panels, 12);
441 }
442
443 /*
444  * This is the callback for all the buttons on the month
445  * panels.  It figures out what was pressed, and invokes
446  * the appropriate function.
447  */
448 static void
449 year_btn_cb(Widget w, XtPointer client, XtPointer call)
450 {
451         XmMonthPanelCallbackStruct *cbs = (XmMonthPanelCallbackStruct *) call;
452         Calendar *c = (Calendar *)client;
453         int monthno, year;
454  
455         XtVaGetValues(w, XmNmonth, &monthno, XmNyear, &year, NULL);
456
457         if (cbs->type == MONTH_SELECTION) {
458                 /*
459                  * Date update:
460                  *      if month chosen is not current month
461                  *      set date to 1st of new month, otherwise
462                  *      don't change date.
463                  */
464                 if (month(c->view->date) != monthno) {
465                         calendar_deselect (c);
466                         c->view->olddate = c->view->date;
467                         c->view->date = monthdayyear(monthno, 1, year);
468                         c->view->nwks = numwks(c->view->date);
469                         calendar_select (c, monthSelect, NULL);
470                 }
471  
472
473                 /* switch to month view */
474                 cleanup_after_yearview(c);
475                 c->view->glance = monthGlance;
476                 prepare_to_paint_monthview(c, NULL);
477                 XtMapWidget(c->canvas);
478         }
479         else {  /* type == DAY_SELECTION */
480                 c->view->olddate = c->view->date;
481                 c->view->date = monthdayyear(monthno, cbs->day, year);
482                 cleanup_after_yearview(c);
483
484                 c->view->glance = dayGlance;
485                 init_mo(c);
486                 (void)init_dayview(c);
487
488                 XtMapWidget(c->canvas);
489                 paint_day(c);
490         }
491 }
492
493 extern void
494 cleanup_after_yearview(Calendar *c)
495 {
496         Year *y = (Year *)c->view->year_info;
497
498         if (!y) return;
499
500         XtUnmapWidget(y->form);
501         XtUnmapWidget(y->label_form);
502
503         /* Unmanaging the form is necessary to keep the stupid 
504            traversal order right */
505
506         XtUnmanageChild(y->form);
507         XtUnmanageChild(y->label_form);
508         XmProcessTraversal(c->appt_btn, XmTRAVERSE_CURRENT); 
509
510         XmToggleButtonGadgetSetState(c->year_scope, False, False);
511
512         /*
513          * Reclaim year view memory.  You should comment this out
514          * if rendering performance is more important than
515          * minimizing memory usage.  It saves 400 *dget instances
516          * lying around when they may never be needed again, but
517          * at the expense of recreating them if they are needed.
518          */
519 /*
520         deallocator(c);
521 */
522 }
523