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