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