dtcm: Coverity 89287
[oweals/cde.git] / cde / programs / dtpad / helpDlg.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: helpDlg.c /main/4 1999/09/15 15:16:22 mgreess $ */
24 /**********************************<+>*************************************
25 ***************************************************************************
26 **
27 **  File:        helpDlg.c
28 **
29 **  Project:     HP DT dtpad, a memo maker type editor based on the
30 **               Dt Editor widget.
31 **
32 **  Description:  Routines which manipulate the dialogs associated with
33 **                operations in the Help menu.
34 **  -----------
35 **
36 *******************************************************************
37 **  (c) Copyright Hewlett-Packard Company, 1990, 1991, 1992.  All rights are
38 **  reserved.  Copying or other reproduction of this program
39 **  except for archival purposes is prohibited without prior
40 **  written consent of Hewlett-Packard Company.
41 ********************************************************************
42 **
43 *******************************************************************
44 **  (c) Copyright 1993, 1994 Hewlett-Packard Company
45 **  (c) Copyright 1993, 1994 International Business Machines Corp.
46 **  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
47 **  (c) Copyright 1993, 1994 Novell, Inc.
48 ********************************************************************
49 **
50 **
51 **************************************************************************
52 **********************************<+>*************************************/
53 #include <stdio.h>
54
55 #include "dtpad.h"
56 #include "help.h"
57
58 #include <Xm/DialogS.h>
59 #include <Xm/Form.h>
60 #include <Xm/MwmUtil.h>
61 #include <Dt/HelpDialog.h>
62
63
64 /************************************************************************
65  * GetHelpDialog - obtains an "unused" help dialog from the cached list
66  *      for the pad or, if one doesn't exist, creates a new help dialog
67  *      and puts it in the cache.
68  ************************************************************************/
69 static Widget
70 GetHelpDialog(
71         Editor *pPad)
72 {
73     HelpStruct *pHelp;
74
75     if (pPad->pHelpCache == (HelpStruct *)NULL) {
76         pHelp = pPad->pHelpCache = (HelpStruct *)XtMalloc(sizeof(HelpStruct));
77         pHelp->pNext = (HelpStruct *)NULL;
78         pHelp->pPrev = (HelpStruct *)NULL;
79         pHelp->inUse = True;
80         pHelp->dialog = CreateHelpDialog(pPad);
81         return pHelp->dialog;
82     } else {
83         for (pHelp = pPad->pHelpCache; pHelp != (HelpStruct *)NULL;
84           pHelp = pHelp->pNext) {
85             if (pHelp->inUse == False) {
86                 pHelp->inUse = True;
87                 return pHelp->dialog;
88             }
89         }
90         pHelp = (HelpStruct *) XtMalloc(sizeof(HelpStruct));
91         pPad->pHelpCache->pPrev = pHelp;
92         pHelp->pNext = pPad->pHelpCache;
93         pPad->pHelpCache = pHelp;
94         pHelp->pPrev = (HelpStruct *)NULL;
95         pHelp->inUse = True;
96         pHelp->dialog = CreateHelpDialog(pPad);
97         return pHelp->dialog;
98     }
99 }
100
101
102 /************************************************************************
103  * SetHelpVolAndDisplayHelp - sets the help volume and displays the help
104  *      text beginning at the specified location id within the volume
105  ************************************************************************/
106 void
107 SetHelpVolAndDisplayHelp(
108         Editor *pPad,
109         char *locationId,
110         char *helpVolume)
111 {
112
113     if (helpVolume == (char *) NULL)
114        helpVolume = TEXTEDITOR_HELP_VOLUME;
115
116     DisplayHelp(pPad, helpVolume, locationId);
117 }
118
119
120 /************************************************************************
121  * DisplayHelp - is called to display all the "normal" help windows.  It
122  *      uses the one "MainHelp" dialog associated with the relevant pad.
123  ************************************************************************/
124 void
125 DisplayHelp(
126         Editor *pPad,
127         char *helpVolume,
128         char *locationId)
129 {
130     if (pPad->MainHelp == NULL) {
131         pPad->MainHelp = CreateHelpDialog(pPad);
132     }
133
134     DisplayHelpDialog(pPad, pPad->MainHelp, helpVolume, locationId);
135 }
136
137
138
139 /************************************************************************
140  * CreateHelpDialog - creates the "MainHelp" help for the pad and sets
141  *      up the hyperLink and Close callbacks for it.
142  ************************************************************************/
143 Widget
144 CreateHelpDialog(
145         Editor *pPad)
146 {
147     Arg args[10];
148     int n = 0;
149     Widget dialog;
150
151     dialog = DtCreateHelpDialog(pPad->app_shell, "helpDlg", args, n);
152     XtAddCallback(dialog, DtNhyperLinkCallback, 
153                   (XtCallbackProc)HelpHyperlinkCB, pPad);
154     XtAddCallback(dialog, DtNcloseCallback, 
155                   (XtCallbackProc)HelpCloseCB, pPad);
156     return dialog;
157 }
158
159
160 /************************************************************************
161  * DisplayNewHelpWindow - is called by the hyperLink callback,
162  *      helpCB.c:HelpHyperlinkCB to open a new, cached, help dialog for
163  *      this pad.
164  ************************************************************************/
165 void
166 DisplayNewHelpWindow(
167         Editor *pPad,
168         char *helpVolume,
169         char *locationId)
170 {
171     DisplayHelpDialog(pPad, GetHelpDialog(pPad), helpVolume, locationId);
172 }
173
174
175 /************************************************************************
176  * DisplayHelpDialog - sets the help type topic, volume, location id and
177  *      title for the specified help dialog.
178  ************************************************************************/
179 void
180 DisplayHelpDialog(
181         Editor *pPad,
182         Widget helpDialog,
183         char *helpVolume,
184         char *locationId)
185 {
186     Arg args[10];
187     int n = 0;
188     char *titleStr, *helpStr;
189
190     n = 0;
191     XtSetArg(args[n], DtNhelpType, DtHELP_TYPE_TOPIC);n++;
192     XtSetArg(args[n], DtNhelpVolume, helpVolume);       n++;
193     XtSetArg(args[n], DtNlocationId, locationId);       n++;
194     XtSetValues(helpDialog, args, n);
195
196     /*
197      * It's a bit bogus to set the dialog title each time we display the
198      * dialog, but it's an easy way to make sure the title is right if the
199      * pad is reused and the previous user had a mainTitle specified.
200      */
201     helpStr = (char*)GETMESSAGE(2, 1, "Help");
202     titleStr = (char*)XtMalloc(strlen(helpStr)+strlen(DialogTitle(pPad))+1);
203     sprintf(titleStr, "%s%s", DialogTitle(pPad), helpStr);
204     n = 0;
205     XtSetArg (args[n], XmNtitle, titleStr);             n++;
206     XtSetValues(XtParent(helpDialog), args, n);
207
208     XtManageChild(helpDialog);
209
210     XtFree(titleStr);
211 }