Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / programs / dtcm / dtcm / timezone.c
1 /* $TOG: timezone.c /main/10 1999/07/01 16:36:43 mgreess $ */
2 /*
3  *  (c) Copyright 1993, 1994 Hewlett-Packard Company
4  *  (c) Copyright 1993, 1994 International Business Machines Corp.
5  *  (c) Copyright 1993, 1994 Novell, Inc.
6  *  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
7  */
8
9 #include <EUSCompat.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <csa.h>
13 #include <Xm/Xm.h>
14 #include <Xm/Form.h>
15 #include <Xm/LabelG.h>
16 #include <Xm/Label.h>
17 #include <Xm/List.h>
18 #include <Xm/DialogS.h>
19 #include <Xm/PushB.h>
20 #include <Xm/RowColumn.h>
21 #include <Xm/Scale.h>
22 #include <Xm/SeparatoG.h>
23 #include <Xm/Text.h>
24 #include <Xm/ToggleBG.h>
25 #include <Xm/MwmUtil.h>
26 #include <Xm/ComboBox.h>
27 #include <Dt/HourGlass.h>
28 #include "calendar.h"
29 #include "util.h"
30 #include "timeops.h"
31 #include "timezone.h"
32 #include "format.h"
33 #include "datefield.h"
34 #include "props.h"
35 #include "editor.h"
36 #include "todo.h"
37 #include "group_editor.h"
38 #include "select.h"
39 #include "help.h"
40
41 extern caddr_t
42 make_timezone(c)
43 Calendar *c;
44 {
45         Timezone *t;
46         Widget text, separator, button_form;
47         XmString xmstr;
48         int ac;
49         Arg args[10];
50         char buf[BUFSIZ], tmp_buf[BUFSIZ];
51         char *title;
52         void tz_customtime_cb(), tz_mytime_cb(), 
53                         tz_apply_cb(), tz_cancel_cb(), tz_ok_cb();
54
55         if (c->timezone == NULL) {
56                 c->timezone = (caddr_t)ckalloc(sizeof(Timezone));
57                 t = (Timezone*)c->timezone;
58         }
59         else
60                 t = (Timezone*)c->timezone;
61
62         title = XtNewString(catgets(c->DT_catd, 1, 649, "Calendar : Time Zone"));
63         t->frame = XtVaCreatePopupShell("frame",
64                 xmDialogShellWidgetClass, c->frame,
65                 XmNtitle,               title,
66                 XmNallowShellResize,    False,
67                 XmNmwmFunctions,        MWM_FUNC_CLOSE | MWM_FUNC_MOVE,
68                 XmNmappedWhenManaged,   False,
69                 NULL);
70         XtFree(title);
71
72         t->form = XtVaCreateWidget("timezone_form",
73                 xmFormWidgetClass,      t->frame,
74                 XmNautoUnmanage,        False,
75                 XmNhorizontalSpacing,   5,
76                 XmNverticalSpacing,     5,
77                 XmNmarginWidth,         0,
78                 XmNmarginHeight,        0,
79                 XmNfractionBase,        4,
80                 NULL);
81
82         t->timezone_rc_mgr = XtVaCreateWidget("search_rc_mgr",
83                 xmRowColumnWidgetClass, t->form,
84                 XmNpacking,             XmPACK_COLUMN,
85                 XmNorientation,         XmVERTICAL,
86                 XmNradioBehavior,       True,
87                 XmNisHomogeneous,       True,
88                 XmNentryClass,          xmToggleButtonGadgetClass,
89                 XmNleftAttachment,      XmATTACH_FORM,
90                 XmNleftOffset,          5,
91                 XmNtopAttachment,       XmATTACH_FORM,
92                 XmNtopOffset,           5,
93                 NULL);
94  
95         xmstr = XmStringCreateLocalized(catgets(c->DT_catd, 1, 650, "My Time"));
96         t->mytime = XtVaCreateWidget("myTime",
97                 xmToggleButtonGadgetClass, t->timezone_rc_mgr,
98                 XmNlabelString, xmstr,
99                 XmNset, True,
100                 XmNuserData, t,
101                 NULL);
102         XmStringFree(xmstr);
103         XtAddCallback(t->mytime, XmNvalueChangedCallback, tz_mytime_cb, NULL);
104  
105         xmstr = XmStringCreateLocalized(catgets(c->DT_catd, 1, 652, "Custom Time"));
106         t->customtime = XtVaCreateWidget("customTime",
107                 xmToggleButtonGadgetClass, t->timezone_rc_mgr,
108                 XmNlabelString, xmstr,
109                 XmNuserData, t,
110                 NULL);
111         XmStringFree(xmstr);
112         XtAddCallback(t->customtime, XmNvalueChangedCallback, tz_customtime_cb, NULL);
113
114         xmstr = XmStringCreateLocalized(catgets(c->DT_catd, 1, 653, "GMT"));
115         t->gmtlabel = XtVaCreateWidget("gmtLabel", xmLabelWidgetClass, t->form,
116                 XmNlabelString, xmstr,
117                 XmNleftAttachment, XmATTACH_WIDGET,
118                 XmNleftWidget, t->customtime,
119                 XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
120                 XmNbottomWidget, t->timezone_rc_mgr,
121                 XmNbottomOffset, 7,
122                 NULL);
123         XmStringFree(xmstr);
124         XtSetSensitive(t->gmtlabel, False);
125
126         ac = 0;  
127         XtSetArg(args[ac], XmNcomboBoxType, XmDROP_DOWN_COMBO_BOX); ++ac;
128         XtSetArg(args[ac], XmNleftAttachment, XmATTACH_WIDGET); ++ac;
129         XtSetArg(args[ac], XmNleftWidget, t->gmtlabel); ++ac;
130         XtSetArg(args[ac], XmNleftOffset, 8); ++ac;
131         XtSetArg(args[ac], XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET); ++ac;
132         XtSetArg(args[ac], XmNbottomOffset, 5); ++ac;
133         XtSetArg(args[ac], XmNbottomWidget, t->timezone_rc_mgr); ++ac;
134         XtSetArg(args[ac], XmNpositionMode, XmONE_BASED); ++ac;
135         t->gmtcombo = XmCreateComboBox(t->form, "gmtcombo", args, ac);
136
137         XtVaSetValues(t->gmtcombo, XmNwidth, 75, NULL);
138         XtSetSensitive(t->gmtcombo, False);
139
140         for(ac = -12; ac <= 12; ac += 1) {
141                 if (ac >= 0)
142                         sprintf(buf, "+%d", ac);  
143                 else
144                         sprintf(buf, "%d", ac);  
145                 xmstr = XmStringCreateLocalized(buf);
146                 XmComboBoxAddItem(t->gmtcombo, xmstr, 0, False);
147                 XmStringFree(xmstr);
148         }
149         /* Remind: remove this kludge after timezone attribute is available */
150         /* Remind: don't do this because of daylight savings time */
151         /*
152         XtVaGetValues(t->gmtcombo, XmNtextField, &text, NULL);
153         XmTextSetString(text, "-6");
154         */
155
156         xmstr = XmStringCreateLocalized(catgets(c->DT_catd, 1, 654, "Hours"));
157         t->hourlabel = XtVaCreateWidget("gmtHours", xmLabelWidgetClass, t->form,
158                 XmNlabelString, xmstr,
159                 XmNleftAttachment, XmATTACH_WIDGET,
160                 XmNleftWidget, t->gmtcombo,
161                 XmNrightAttachment, XmATTACH_FORM,
162                 XmNrightOffset, 10,
163                 XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
164                 XmNbottomWidget, t->timezone_rc_mgr,
165                 XmNbottomOffset, 7,
166                 NULL);
167         XmStringFree(xmstr);
168         XtSetSensitive(t->hourlabel, False);
169
170         separator = XtVaCreateWidget("separator",
171                 xmSeparatorGadgetClass,
172                 t->form,
173                 XmNleftAttachment,      XmATTACH_FORM,
174                 XmNrightAttachment,     XmATTACH_FORM,
175                 XmNtopAttachment,       XmATTACH_WIDGET,
176                 XmNtopWidget,           t->timezone_rc_mgr,
177                 XmNtopOffset,           1,
178                 NULL);
179
180         button_form = XtVaCreateWidget("button_form",
181                 xmFormWidgetClass,      t->form,
182                 XmNautoUnmanage,        False,
183                 XmNleftAttachment,      XmATTACH_FORM,
184                 XmNleftOffset,          5,
185                 XmNrightAttachment,     XmATTACH_FORM,
186                 XmNrightOffset,         5,
187                 XmNtopAttachment,       XmATTACH_WIDGET,
188                 XmNtopWidget,           separator,
189                 XmNhorizontalSpacing,   3,
190                 XmNfractionBase,        4,
191                 NULL);
192
193         xmstr = XmStringCreateLocalized(catgets(c->DT_catd, 1, 655, "OK"));
194        t->okbutton = XtVaCreateWidget("OK", 
195                 xmPushButtonWidgetClass, button_form,
196                 XmNlabelString,         xmstr,
197                 XmNtopAttachment,       XmATTACH_WIDGET,
198                 XmNtopWidget,           separator,
199                 XmNtopOffset,           8,
200                 XmNleftAttachment,      XmATTACH_POSITION,
201                 XmNleftPosition,        0,
202                 XmNrightAttachment,     XmATTACH_POSITION,
203                 XmNrightPosition,       1,
204                 XmNnavigationType,      XmTAB_GROUP,
205                 XmNuserData,            c,
206                 NULL);
207         XmStringFree(xmstr);
208         XtAddCallback(t->okbutton, XmNactivateCallback, tz_ok_cb, NULL);
209
210         xmstr = XmStringCreateLocalized(catgets(c->DT_catd, 1, 460, "Apply"));
211        t->applybutton = XtVaCreateWidget("Apply", 
212                 xmPushButtonWidgetClass, button_form,
213                 XmNlabelString,         xmstr,
214                 XmNtopAttachment,       XmATTACH_WIDGET,
215                 XmNtopWidget,           separator,
216                 XmNtopOffset,           8,
217                 XmNleftAttachment,      XmATTACH_POSITION,
218                 XmNleftPosition,        1,
219                 XmNrightAttachment,     XmATTACH_POSITION,
220                 XmNrightPosition,       2,
221                 XmNnavigationType,      XmTAB_GROUP,
222                 XmNuserData,            c,
223                 NULL);
224         XmStringFree(xmstr);
225         XtAddCallback(t->applybutton, XmNactivateCallback, tz_apply_cb, NULL);
226
227         xmstr = XmStringCreateLocalized(catgets(c->DT_catd, 1, 923, "Cancel"));
228         t->cancelbutton = XtVaCreateWidget("Cancel", 
229                 xmPushButtonWidgetClass, button_form,
230                 XmNlabelString,         xmstr,
231                 XmNtopAttachment,       XmATTACH_WIDGET,
232                 XmNtopWidget,           separator,
233                 XmNtopOffset,           8,
234                 XmNleftAttachment,      XmATTACH_POSITION,
235                 XmNleftPosition,        2,
236                 XmNrightAttachment,     XmATTACH_POSITION,
237                 XmNrightPosition,       3,
238                 XmNnavigationType,      XmTAB_GROUP,
239                 XmNuserData,            c,
240                 NULL);
241         XmStringFree(xmstr);
242         XtAddCallback(t->cancelbutton, XmNactivateCallback, tz_cancel_cb, NULL);
243
244         xmstr = XmStringCreateLocalized(catgets(c->DT_catd, 1, 77, "Help"));
245         t->helpbutton = XtVaCreateWidget("Help", 
246                 xmPushButtonWidgetClass, button_form,
247                 XmNlabelString,         xmstr,
248                 XmNtopAttachment,       XmATTACH_WIDGET,
249                 XmNtopWidget,           separator,
250                 XmNtopOffset,           8,
251                 XmNleftAttachment,      XmATTACH_POSITION,
252                 XmNleftPosition,        3,
253                 XmNrightAttachment,     XmATTACH_POSITION,
254                 XmNrightPosition,       4,
255                 XmNnavigationType,      XmTAB_GROUP,
256                 NULL);
257         XmStringFree(xmstr);
258         XtAddCallback(t->helpbutton, XmNactivateCallback,
259                                 (XtCallbackProc)help_cb, TIMEZONE_HELP_BUTTON);
260
261
262         t->timezone_message = XtVaCreateWidget("message",
263                 xmLabelGadgetClass, t->form,
264                 XmNalignment, XmALIGNMENT_BEGINNING,
265                 XmNtopAttachment, XmATTACH_WIDGET,
266                 XmNtopWidget, t->helpbutton,
267                 XmNtopOffset, 8,
268                 XmNleftAttachment, XmATTACH_FORM,
269                 XmNrightAttachment, XmATTACH_FORM,
270                 XmNbottomAttachment, XmATTACH_FORM,
271                 NULL);
272
273         /* set timezone */
274         /* Remind - get this from the structure after it's available */
275         cm_strcpy(t->gmttimezone, "");
276         cm_strcpy(t->mytimezone, "US/Pacific");
277         cm_strcpy(tmp_buf, (char*)getenv("TZ"));
278         if (tmp_buf)
279                 cm_strcpy(t->mytimezone, tmp_buf);
280
281         sprintf(buf, "%s %s", catgets(c->DT_catd, 1, 659, "Time Zone:"),
282                 t->mytimezone);
283         set_message(t->timezone_message, buf);
284         set_message(c->message_text, buf);
285
286         XtVaSetValues(t->form, 
287                         XmNdefaultButton,       t->applybutton,
288                         XmNcancelButton,        t->cancelbutton,
289                         NULL);
290         XtVaSetValues(button_form, 
291                         XmNdefaultButton,       t->applybutton,
292                         XmNcancelButton,        t->cancelbutton,
293                         NULL);
294         ManageChildren(t->timezone_rc_mgr);
295         ManageChildren(button_form);
296         ManageChildren(t->form);
297         XtManageChild(t->form);
298         XtVaSetValues(t->frame, XmNmappedWhenManaged, True, NULL);
299         XtRealizeWidget(t->frame);
300
301         XtPopup(t->frame, XtGrabNone);
302
303         return(caddr_t)t;
304 }
305
306 extern void
307 refresh_timezone(t)
308 Timezone *t;
309 {
310     XmToggleButtonSetState(t->mytime, t->timezone_type == mytime, FALSE);
311     XmToggleButtonSetState(t->customtime, t->timezone_type == customtime,FALSE);
312     XtSetSensitive(t->gmtlabel, t->timezone_type == customtime);
313     XtSetSensitive(t->gmtcombo, t->timezone_type == customtime);
314     XtSetSensitive(t->hourlabel, t->timezone_type == customtime);
315
316     if (t->timezone_type == mytime)
317     {
318         Widget text;
319         XtVaGetValues(t->gmtcombo, XmNtextField, &text, NULL);
320         XmTextSetString(text, "");
321     }
322 }
323
324 void
325 tz_mytime_cb(Widget widget, XtPointer client_data, XtPointer call_data)
326 {
327         Timezone *t;
328         XmToggleButtonCallbackStruct *state =
329                 (XmToggleButtonCallbackStruct *) call_data;
330
331         XtVaGetValues(widget, XmNuserData, &t, NULL);
332
333         if (state->set)
334                 t->edit_timezone_type = mytime;
335         XtSetSensitive(t->gmtlabel, !state->set);
336         XtSetSensitive(t->gmtcombo, !state->set);
337         XtSetSensitive(t->hourlabel, !state->set);
338 }
339
340 void
341 tz_customtime_cb(Widget widget, XtPointer client_data, XtPointer call_data)
342 {
343         Timezone *t;
344         XmToggleButtonCallbackStruct *state =
345                 (XmToggleButtonCallbackStruct *) call_data;
346
347         XtVaGetValues(widget, XmNuserData, &t, NULL);
348
349         if (state->set)
350                 t->edit_timezone_type = customtime;
351         XtSetSensitive(t->gmtlabel, state->set);
352         XtSetSensitive(t->gmtcombo, state->set);
353         XtSetSensitive(t->hourlabel, state->set);
354 }
355
356 void
357 tz_set_timezone(Calendar *c, Timezone *t)
358 {
359     char *tmp_buf, buf[BUFSIZ], gmt[BUFSIZ];
360     Widget text;
361     Dtcm_calendar *cal_handle;
362     int time_diff;
363
364     t->timezone_type = t->edit_timezone_type;
365     if (t->timezone_type == mytime)
366     {
367         set_timezone(t->mytimezone);
368         sprintf(buf, "%s %s", catgets(c->DT_catd, 1, 659, "Time Zone:"),
369                 t->mytimezone);
370
371         /* get utc time */
372         /* Remind: this won't work because of daylight savings time */
373         /* Remind: add this after we can get more info from backend */
374         /*
375           cal_handle = allocate_cal_struct(
376                                         appt_read, 
377                                         c->general->version,
378                                         DtCM_ATTR_UTC_OFFSET_I, 
379                                         NULL);
380           if (query_cal_struct(c->my_cal_handle, cal_handle) == CSA_SUCCESS)
381             time_diff =
382               seconds_to_hours(cal_handle->attrs->value->item.number_value);
383           if (time_diff >= 0) sprintf(gmt, "GMT+%d", time_diff);
384           else sprintf(gmt, "GMT%d", time_diff);
385           set_timezone(gmt);
386         */
387
388     }
389     else if (t->timezone_type == customtime)
390     {
391         XtVaGetValues(t->gmtcombo, XmNtextField, &text, NULL);
392         tmp_buf = XmTextGetString(text);
393         cm_strcpy(t->gmttimezone, tmp_buf);
394         sprintf(gmt, "GMT%s", tmp_buf);
395         set_timezone(gmt);
396         sprintf(buf, "%s %s", catgets(c->DT_catd, 1, 659, "Time Zone:"), gmt);
397     }
398     set_message(t->timezone_message, buf);
399     paint_canvas(c, NULL, RENDER_CLEAR_FIRST);
400     set_message(c->message_text, buf);
401     if (editor_showing((Editor*)c->editor))
402       add_all_appt((Editor*)c->editor);
403     if (geditor_showing((GEditor*)c->geditor))
404       add_all_gappt((GEditor*)c->geditor);
405     if (todo_showing((ToDo*)c->todo))
406       add_all_todo((ToDo*)c->todo);
407 }
408
409 void
410 tz_apply_cb(Widget widget, XtPointer client_data, XtPointer call_data)
411 {
412         Calendar *c;
413         Timezone *t;
414
415         XtVaGetValues(widget, XmNuserData, &c, NULL);
416         t = (Timezone*)c->timezone;
417         tz_set_timezone(c, t);
418 }
419
420  
421 void
422 tz_ok_cb(Widget widget, XtPointer client_data, XtPointer call_data)
423 {
424         Calendar *c;
425         Timezone *t;
426
427         XtVaGetValues(widget, XmNuserData, &c, NULL);
428         t = (Timezone*)c->timezone;
429         tz_set_timezone(c, t);
430
431         XtPopdown(t->frame);
432 }
433
434 void
435 tz_cancel_cb(Widget widget, XtPointer client_data, XtPointer call_data)
436 {
437         Calendar *c;
438         Timezone *t;
439
440         XtVaGetValues(widget, XmNuserData, &c, NULL);
441         t = (Timezone*)c->timezone;
442
443         XtPopdown(t->frame);
444 }