dtcalc: Resolve 82 warnings causing by enabling -Wall
[oweals/cde.git] / cde / programs / dtcalc / help.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 /* $XConsortium: help.c /main/4 1996/03/25 13:12:20 ageorge $ */
24 /*                                                                      *
25  *  help.c                                                              *
26  *   Contains all support for help in the Calculator.                   *
27  *                                                                      *
28  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
29  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
30  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
31  * (c) Copyright 1993, 1994 Novell, Inc.                                *
32  */
33
34 #include <Xm/Xm.h>
35 #include <Xm/XmP.h>
36 #include <Xm/MwmUtil.h>
37 #include <Xm/MessageB.h>
38
39 #include <Dt/Dt.h>
40 #include <Dt/Wsm.h>
41 #include <Dt/HelpDialog.h>
42
43 #include "calctool.h"
44 #include "motif.h"
45
46 #define  DIALOG_MWM_FUNC MWM_FUNC_MOVE | MWM_FUNC_CLOSE
47
48 extern char *base_str[] ;       /* Strings for each base value. */
49 extern char *dtype_str[] ;      /* Strings for each display mode value. */
50 extern char *mode_str[] ;       /* Strings for each mode value. */
51 extern char *ttype_str[] ;      /* Strings for each trig type value. */
52 extern Vars v ;                 /* Calctool variables and options. */
53 extern struct button buttons[] ;           /* Calculator button values. */
54
55
56 extern Boolean ignore_event;
57 extern XtIntervalId timerId;
58 extern void _DtChildPosition     P((Widget, Widget, Position *, Position *)) ;
59
60 typedef struct _helpStruct {
61     struct _helpStruct *pNext;
62     struct _helpStruct *pPrev;
63     Widget dialog;
64     Boolean inUse;
65 } HelpStruct;
66
67 static Widget GetHelpDialog     P(()) ;
68 static void UnmanageCB          P(()) ;
69
70 void
71 Help(char *helpVolume, char *locationId)
72 {
73     Arg args[10];
74     Position newX, newY;
75     int n;
76
77     if(X->helpDialog == NULL)
78     {
79         n = 0;
80         XtSetArg(args[n], DtNhelpType, DtHELP_TYPE_TOPIC); n++;
81         XtSetArg(args[n], DtNhelpVolume, helpVolume);   n++;
82         XtSetArg(args[n], DtNlocationId, locationId);   n++;
83         XtSetArg(args[n], XmNtitle, GETMESSAGE(4, 1, "Calculator - Help")); n++;
84         X->helpDialog = DtCreateHelpDialog(X->mainWin, "helpDlg", args, n);
85
86         DtWsmRemoveWorkspaceFunctions(X->dpy,
87                       XtWindow(XtParent(X->helpDialog)));
88         XtAddCallback(X->helpDialog, DtNhyperLinkCallback,
89                       (XtCallbackProc)HelpHyperlinkCB, NULL);
90         XtAddCallback(X->helpDialog, DtNcloseCallback,
91                       (XtCallbackProc)HelpCloseCB, NULL);
92     }
93     else
94     {
95         n = 0;
96         XtSetArg(args[n], DtNhelpType, DtHELP_TYPE_TOPIC); n++;
97         XtSetArg(args[n], DtNhelpVolume, helpVolume);   n++;
98         XtSetArg(args[n], DtNlocationId, locationId);   n++;
99         XtSetValues(X->helpDialog, args, n);
100     }
101
102     XtSetMappedWhenManaged(XtParent(X->helpDialog), False);
103     XSync(X->dpy, False);
104
105     XtManageChild(X->helpDialog) ;
106
107     _DtChildPosition(X->helpDialog, X->kframe, &newX, &newY);
108     XtSetArg(args[0], XmNx, newX);
109     XtSetArg(args[1], XmNy, newY);
110     XtSetValues(X->helpDialog, args, 2);
111
112     XtSetMappedWhenManaged(XtParent(X->helpDialog), True);
113     XSync(X->dpy, False);
114
115     XtMapWidget(XtParent(X->helpDialog));
116     X->helpMapped = True;
117
118 }
119
120 void
121 HelpCloseCB(Widget widget, caddr_t client_data, caddr_t call_data)
122 {
123
124   HelpStruct *pHelpCache = (HelpStruct *)client_data;
125   HelpStruct *pHelp;
126  
127   for (pHelp = pHelpCache; pHelp != (HelpStruct *)NULL && pHelp->dialog !=widget;
128                            pHelp = pHelp->pNext);
129   if (pHelp != (HelpStruct *)NULL)
130   {
131      pHelp->inUse = False;
132   }
133   XtUnmapWidget(XtParent(widget));
134   X->helpMapped = False;
135 }
136
137 void 
138 HelpHyperlinkCB(Widget widget, caddr_t client_data, caddr_t call_data)
139 {
140   DtHelpDialogCallbackStruct *pHyper = (DtHelpDialogCallbackStruct *) call_data;
141   
142   switch(pHyper->hyperType)
143   {
144      case DtHELP_LINK_TOPIC:
145           DisplayHelp(pHyper->helpVolume, pHyper->locationId);
146           break;
147      default:
148           ;
149    }
150 }
151  
152
153 void 
154 HelpRequestCB(Widget widget, caddr_t client_data, caddr_t call_data)
155 {
156   char  *helpVolume, *locationId;
157   int   topic;
158
159   topic = ((int) client_data) & 0xFFFF;
160   helpVolume = HELP_VOLUME;
161
162   if(topic < 56)
163   {
164      switch (topic)
165      {
166            case 0:
167                locationId = HELP_BLANK_STR;
168                break;
169            case 8:
170                locationId = HELP_ACC_STR;
171                break;
172            case 12:
173                if(v->modetype == LOGICAL)
174                   locationId = HELP_LSHIFT_STR;
175                else
176                   locationId = buttons[topic].str;
177                break;
178            case 13:
179                if(v->modetype == LOGICAL)
180                   locationId = HELP_RSHIFT_STR;
181                else if(v->modetype == FINANCIAL)
182                   locationId = HELP_INT_STR;
183                else
184                   locationId = buttons[topic].str;
185                break;
186            case 14:
187                if(v->modetype == LOGICAL)
188                   locationId = HELP_TRUNC16_STR;
189                else if(v->modetype == SCIENTIFIC)
190                   locationId = HELP_ETOX_STR;
191                else
192                   locationId = buttons[topic].str;
193                break;
194            case 15:
195                if(v->modetype == LOGICAL)
196                   locationId = HELP_TRUNC32_STR;
197                else if(v->modetype == SCIENTIFIC)
198                   locationId = HELP_TENTOX_STR;
199                else
200                   locationId = buttons[topic].str;
201                break;
202            case 16:
203                if(v->modetype == SCIENTIFIC)
204                   locationId = HELP_YTOX_STR;
205                else
206                   locationId = buttons[topic].str;
207                break;
208            case 17:
209                if(v->modetype == SCIENTIFIC)
210                   locationId = HELP_XFACT_STR;
211                else if(v->modetype == FINANCIAL)
212                   locationId = HELP_PAYPYR_STR;
213                else
214                   locationId = buttons[topic].str;
215                break;
216            case 21:
217                if(v->modetype == LOGICAL)
218                   locationId = HELP_BLANK_STR;
219                else
220                   locationId = buttons[topic].str;
221                break;
222            case 22:
223                if(v->modetype == LOGICAL)
224                   locationId = HELP_BLANK_STR;
225                else
226                   locationId = buttons[topic].str;
227                break;
228            case 23:
229                if(v->modetype == LOGICAL || v->modetype == FINANCIAL)
230                   locationId = HELP_BLANK_STR;
231                else
232                   locationId = buttons[topic].str;
233                break;
234            case 24:
235                locationId = HELP_RECIP_STR;
236                break;
237            case 25:
238                locationId = HELP_SQUARE_STR;
239                break;
240            case 26:
241                locationId = HELP_SQRT_STR;
242                break;
243            case 27:
244                locationId = HELP_PERCENT_STR;
245                break;
246            case 28:
247                locationId = HELP_LPAREN_STR;
248                break;
249            case 29:
250                locationId = HELP_RPAREN_STR;
251                break;
252            case 35:
253                locationId = HELP_CHGSIGN_STR;
254                break;
255            case 39:
256                locationId = HELP_TIMES_STR;
257                break;
258            case 40:
259                locationId = HELP_SEVEN_STR;
260                break;
261            case 41:
262                locationId = HELP_EIGHT_STR;
263                break;
264            case 42:
265                locationId = HELP_NINE_STR;
266                break;
267            case 43:
268                locationId = HELP_DIVIDE_STR;
269                break;
270            case 44:
271                locationId = HELP_FOUR_STR;
272                break;
273            case 45:
274                locationId = HELP_FIVE_STR;
275                break;
276            case 46:
277                locationId = HELP_SIX_STR;
278                break;
279            case 47:
280                locationId = HELP_MINUS_STR;
281                break;
282            case 48:
283                locationId = HELP_ONE_STR;
284                break;
285            case 49:
286                locationId = HELP_TWO_STR;
287                break;
288            case 50:
289                locationId = HELP_THREE_STR;
290                break;
291            case 51:
292                locationId = HELP_PLUS_STR;
293                break;
294            case 52:
295                locationId = HELP_ZERO_STR;
296                break;
297            case 53:
298                locationId = HELP_DECIMAL_STR;
299                break;
300            case 54:
301                locationId = HELP_EQUAL_STR;
302                break;
303            default:
304                locationId = buttons[topic].str;
305      }
306   }
307   else
308   {
309      switch (topic)
310      {
311            case HELP_HELP_MENU:
312                locationId = HELP_HELP_MENU_STR;
313                break;
314            case HELP_INTRODUCTION:
315                locationId = HELP_INTRODUCTION_STR;
316                break;
317            case HELP_TABLEOFCONTENTS:
318                locationId = HELP_TABLEOFCONTENTS_STR;
319                break;
320            case HELP_TASKS:
321                locationId = HELP_TASKS_STR;
322                break;
323            case HELP_REFERENCE:
324                locationId = HELP_REFERENCE_STR;
325                break;
326            case HELP_ITEM:
327                locationId = HELP_ITEM_STR;
328                break;
329            case HELP_USING:
330                locationId = HELP_USING_STR;
331                helpVolume = HELP_USING_HELP_VOLUME;
332                break;
333            case HELP_VERSION:
334                locationId = HELP_VERSION_STR;
335                break;
336            case HELP_ASCII:
337                locationId = HELP_ASCII_STR;
338                break;
339            case HELP_CONSTANT:
340                locationId = HELP_CONSTANT_STR;
341                break;
342            case HELP_FUNCTION:
343                locationId = HELP_FUNCTION_STR;
344                break;
345            case HELP_DISPLAY:
346                locationId = HELP_DISPLAY_STR;
347                break;
348            case HELP_MODELINE:
349                locationId = HELP_MODELINE_STR;
350                break;
351            case HELP_MODE:
352                locationId = mode_str[(int) v->modetype];
353                break;
354            case HELP_BASE:
355                locationId = base_str[(int) v->base];
356                break;
357            case HELP_NOTATION:
358                locationId = dtype_str[(int) v->dtype];
359                break;
360            case HELP_TRIG:
361                locationId = ttype_str[(int) v->ttype];
362                break;
363            case HELP_MENUBAR:
364                locationId = HELP_MENUBAR_STR;
365                break;
366            default:
367                locationId = HELP_INTRODUCTION_STR;
368      }
369   }
370   Help(helpVolume, locationId);
371   ignore_event = True;
372   timerId = XtAppAddTimeOut (XtWidgetToApplicationContext (X->kframe), 300,
373                          (XtTimerCallbackProc) TimerEvent, (XtPointer) NULL);
374 }
375
376 void
377 HelpModeCB(Widget w, caddr_t client_data, caddr_t call_data)
378 {
379   Widget widget;
380   char *errorMsg, *tmp;
381  
382   switch(DtHelpReturnSelectedWidgetId(X->mainWin, (Cursor)NULL, &widget))
383    {
384       case DtHELP_SELECT_VALID:
385         while (!XtIsShell(widget))
386         {
387                 if(XtHasCallbacks(widget, XmNhelpCallback) == XtCallbackHasSome)
388                 {
389                         XtCallCallbacks(widget, XmNhelpCallback, 
390                                                        (XtPointer)client_data);
391                         return;
392                 }
393                 widget = XtParent(widget);
394         }
395         break;
396
397      case DtHELP_SELECT_INVALID:
398         errorMsg = GETMESSAGE(4, 2, "You must select an item within Calculator");
399         tmp = XtNewString(errorMsg);
400         ErrDialog((char *) tmp, X->mainWin);
401         XtFree(tmp);
402         break;
403    }
404 }
405
406 void
407 DisplayHelp(char *helpVolume, char *locationId)
408 {
409     Arg args[10];
410     int n;
411
412     X->helpDialog = GetHelpDialog();
413
414     n = 0;
415     XtSetArg(args[n], DtNhelpType, DtHELP_TYPE_TOPIC); n++;
416     XtSetArg(args[n], DtNhelpVolume, helpVolume);       n++;
417     XtSetArg(args[n], DtNlocationId, locationId);       n++;
418     XtSetValues(X->helpDialog, args, n);
419
420     XtManageChild(X->helpDialog);
421     XtMapWidget(XtParent(X->helpDialog));
422     X->helpMapped = True;
423 }
424
425
426 static Widget
427 GetHelpDialog(void)
428 {
429     static HelpStruct       *pHelpCache;
430
431     HelpStruct *pHelp;
432     Arg args[5];
433
434     if(pHelpCache == (HelpStruct *)NULL)
435     {
436         pHelp = pHelpCache = (HelpStruct *)XtMalloc(sizeof(HelpStruct));
437         pHelp->pNext = (HelpStruct *)NULL;
438         pHelp->pPrev = (HelpStruct *)NULL;
439         pHelp->inUse = True;
440
441         XtSetArg(args[0], XmNtitle, GETMESSAGE(4, 1, "Calculator - Help"));
442         pHelp->dialog = DtCreateHelpDialog(X->mainWin, "helpDlg",  args, 1);
443
444         DtWsmRemoveWorkspaceFunctions(X->dpy,
445                 XtWindow(XtParent(pHelp->dialog)));
446         XtAddCallback(pHelp->dialog, DtNhyperLinkCallback,
447                       (XtCallbackProc)HelpHyperlinkCB, NULL);
448         XtAddCallback(pHelp->dialog, DtNcloseCallback,
449                       (XtCallbackProc)HelpCloseCB, pHelpCache);
450         return pHelp->dialog;
451     }
452     else
453     {
454         for(pHelp = pHelpCache;
455             pHelp != (HelpStruct *)NULL;
456             pHelp = pHelp->pNext)
457         {
458             if(pHelp->inUse == False)
459             {
460                 pHelp->inUse = True;
461                 return pHelp->dialog;
462             }
463         }
464         pHelp = (HelpStruct *) XtMalloc(sizeof(HelpStruct));
465         pHelpCache->pPrev = pHelp;
466         pHelp->pNext = pHelpCache;
467         pHelpCache = pHelp;
468         pHelp->pPrev = (HelpStruct *)NULL;
469         pHelp->inUse = True;
470
471         XtSetArg(args[0], XmNtitle, GETMESSAGE(4, 1, "Calculator - Help"));
472         pHelp->dialog = DtCreateHelpDialog(X->mainWin, "helpDlg",  args, 1);
473         DtWsmRemoveWorkspaceFunctions(X->dpy,
474                       XtWindow(XtParent(pHelp->dialog)));
475         XtAddCallback(pHelp->dialog, DtNhyperLinkCallback,
476                       (XtCallbackProc)HelpHyperlinkCB, NULL);
477         XtAddCallback(pHelp->dialog, DtNcloseCallback,
478                       (XtCallbackProc)HelpCloseCB, pHelpCache);
479         return pHelp->dialog;
480     }
481 }
482
483 void
484 ErrDialog(char *errString, Widget visualParent)
485 {
486   int   n;
487   Arg   args[10];
488   XmString label, ok;
489
490   label = XmStringCreateLocalized(errString) ;
491   X->errParent = visualParent;
492
493   if (X->errDialog == NULL)
494   {
495     ok = XmStringCreateLocalized(GETMESSAGE(4, 4, "OK"));
496
497     n = 0;
498     XtSetArg (args[n], XmNokLabelString, ok);           n++;
499     XtSetArg (args[n], XmNmessageString, label);        n++;
500     XtSetArg (args[n], XmNmwmFunctions, DIALOG_MWM_FUNC); n++;
501     XtSetArg (args[n], XmNautoUnmanage,  FALSE);        n++;
502     XtSetArg (args[n], XmNdefaultPosition, FALSE);      n++;
503
504     X->errDialog = XmCreateErrorDialog (X->mainWin, "ErroNotice", args, n);
505
506     XtAddCallback (X->errDialog, XmNokCallback, UnmanageCB, NULL);
507     XtAddCallback (X->errDialog, XmNmapCallback, CenterMsgCB, NULL);
508
509     XtUnmanageChild (XmMessageBoxGetChild (X->errDialog, 
510                                                     XmDIALOG_CANCEL_BUTTON));
511
512     XtUnmanageChild (XmMessageBoxGetChild (X->errDialog, XmDIALOG_HELP_BUTTON));
513
514     n = 0;
515     XtSetArg (args[n], XmNmwmInputMode, 
516                                   MWM_INPUT_PRIMARY_APPLICATION_MODAL);n++;
517     XtSetArg (args[n], XmNuseAsyncGeometry, TRUE); n++;
518     XtSetArg (args[n], XmNtitle, GETMESSAGE(4, 3, "Error")); n++;
519     XtSetValues (XtParent(X->errDialog), args, n);
520   }
521   else
522   {
523      XtSetArg (args[0], XmNmessageString, label);
524      XtSetValues (X->errDialog, args, 1);
525   }
526   XmStringFree (label);
527   XtManageChild(X->errDialog);
528 }
529
530 static void
531 UnmanageCB(Widget  widget, XtPointer client_data, XtPointer call_data)
532 {
533     XtUnmanageChild(widget);
534 }
535
536 void
537 CenterMsgCB (Widget widget, XtPointer client_data, XtPointer call_data)
538 {
539     int n;
540     Position newX, newY;
541     Arg args[4];
542     Widget   shell;
543
544     shell = (Widget)client_data;
545     if (shell == NULL)
546     {
547         if (!X->errParent || !XtParent(X->errParent) ||
548                 !XtIsManaged(X->errParent))
549                         shell = X->mainWin;
550         else
551                 shell = XtParent(X->errParent);
552     }
553     else
554         shell = XtParent (shell);
555
556     newX = XtX(shell) + XtWidth(shell)/2 - XtWidth(widget)/2;
557     newY = XtY(shell) + XtHeight(shell)/2 - XtHeight(widget)/2;
558
559     if (newX < 0) newX = 0;
560     if (newY < 0) newY = 0;
561
562     n = 0;
563     XtSetArg (args[n], XmNx, newX);             n++;
564     XtSetArg (args[n], XmNy, newY);             n++;
565     XtSetValues(widget, args, n);
566 }
567