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