dthelp: Change to ANSI function definitions
[oweals/cde.git] / cde / programs / dtfile / 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 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 /* $XConsortium: Help.c /main/6 1996/10/29 17:08:14 mustafa $ */
24 /************************************<+>*************************************
25  ****************************************************************************
26  *
27  *   FILE:           Help.c
28  *
29  *   COMPONENT_NAME: Desktop File Manager (dtfile)
30  *
31  *   Description:    Source file for the help dialog.
32  *
33  *   FUNCTIONS: Create
34  *              FreeValues
35  *              GetDefaultValues
36  *              GetResourceValues
37  *              GetValues
38  *              InstallClose
39  *              MapFileTypeToHelpString
40  *              SetValues
41  *              WriteResourceValues
42  *
43  *   (c) Copyright 1993, 1994, 1995 Hewlett-Packard Company
44  *   (c) Copyright 1993, 1994, 1995 International Business Machines Corp.
45  *   (c) Copyright 1993, 1994, 1995 Sun Microsystems, Inc.
46  *   (c) Copyright 1993, 1994, 1995 Novell, Inc.
47  *
48  ****************************************************************************
49  ************************************<+>*************************************/
50
51
52 #include <stdio.h>
53 #include <errno.h>
54 #include <time.h>
55 #include <sys/types.h>
56 #include <sys/stat.h>
57 #include <unistd.h>
58 #include <limits.h>
59
60
61 #include <Xm/XmP.h>
62 #include <Xm/DialogS.h>
63 #include <Xm/VendorSEP.h>
64 #include <Xm/MwmUtil.h>
65
66 #include <X11/ShellP.h>
67 #include <X11/Shell.h>
68 #include <X11/Xatom.h>
69 #include <Xm/Protocols.h>
70
71 #include <Dt/Action.h>
72 #include <Dt/Connect.h>
73 #include <Dt/DtNlUtils.h>
74 #include <Dt/FileM.h>
75 #include <Dt/Dts.h>
76 #include "Encaps.h"
77 #include "SharedProcs.h"
78
79 #include <Dt/HelpDialog.h>
80
81 #include "Desktop.h"
82 #include "FileMgr.h"
83 #include "Main.h"
84 #include "Help.h"
85 #include "SharedMsgs.h"
86
87
88 /*  Resource definitions for the help dialog  */
89
90 static DialogResource resources[] =
91 {
92    { "idString", XmRString, sizeof(String),
93      XtOffset(HelpDataPtr, idString),
94      (caddr_t) NULL, _DtStringToString },
95
96    { "volString", XmRString, sizeof(String),
97      XtOffset(HelpDataPtr, volString),
98      (caddr_t) NULL, _DtStringToString },
99
100    { "topicTitle", XmRString, sizeof(String),
101      XtOffset(HelpDataPtr, topicTitle),
102      (caddr_t) NULL, _DtStringToString },
103
104    { "fileType", XmRString, sizeof(String),
105      XtOffset(HelpDataPtr, fileType),
106      (caddr_t) NULL, _DtStringToString },
107
108    { "helpType", XmRInt, sizeof(int),
109       XtOffset(HelpDataPtr, helpType),
110       (caddr_t) DtHELP_TYPE_TOPIC, _DtIntToString },
111 };
112
113
114 static char * HELP_DIALOG = "HelpDialog";
115
116 /********    Static Function Declarations    ********/
117
118 static void Create(
119                         Display *display,
120                         Widget parent,
121                         Widget *return_widget,
122                         XtPointer *dialog) ;
123 static void InstallClose(
124                         HelpRec *helpRec,
125                         XtCallbackProc callback,
126                         XtPointer clientData) ;
127 static XtPointer GetValues(
128                         HelpRec *helpRec) ;
129 static XtPointer GetDefaultValues( void ) ;
130 static XtPointer GetResourceValues(
131                         XrmDatabase data_base,
132                         char **name_list) ;
133 static void SetValues(
134                         HelpRec *helpRec,
135                         HelpData *helpData) ;
136 static void WriteResourceValues(
137                         DialogData *values,
138                         int fd,
139                         char **name_list) ;
140 static void FreeValues(
141                         HelpData *helpData) ;
142
143 /********    End Static Function Declarations    ********/
144
145 /*
146  *  The Dialog Class structure.
147  */
148
149 static DialogClass helpClassRec =
150 {
151    resources,
152    XtNumber(resources),
153    (DialogCreateProc)Create,
154    (DialogInstallChangeProc)NULL,
155    (DialogInstallCloseProc)InstallClose,
156    (DialogDestroyProc)_DtGenericDestroy,
157    (DialogGetValuesProc)GetValues,
158    (DialogGetDefaultValuesProc)GetDefaultValues,
159    (DialogGetResourceValuesProc)GetResourceValues,
160    (DialogSetValuesProc)SetValues,
161    (DialogWriteResourceValuesProc)WriteResourceValues,
162    (DialogFreeValuesProc)FreeValues,
163    (DialogMapWindowProc)_DtGenericMapWindow,
164    (DialogSetFocusProc)NULL,
165 };
166
167 DialogClass * helpClass = (DialogClass *) &helpClassRec;
168
169
170
171
172 /************************************************************************
173  *
174  *  Create
175  *
176  ************************************************************************/
177
178 static void
179 Create(
180         Display *display,
181         Widget parent,
182         Widget *return_widget,
183         XtPointer *dialog )
184
185 {
186    HelpRec * helpRec;
187    Arg args[2];
188    Widget helpDialog;
189    int mwmDecs;
190
191    /*  Allocate the audio annotation dialog instance record.  */
192
193    helpRec = (HelpRec *) XtMalloc(sizeof(HelpRec));
194
195
196    /*  Create the canned help dialog.  */
197
198    helpDialog = DtCreateHelpDialog(parent, "helpDialog", args, 0);
199
200    /* Fill in our instance structure */
201    helpRec->shell = XtParent(helpDialog);
202    helpRec->helpDialog = helpDialog;
203
204    /*  Set the return values for the dialog widget and dialog instance.  */
205    *return_widget = helpDialog;
206    *dialog = (XtPointer) helpRec;
207 }
208
209
210 /************************************************************************
211  *
212  *  InstallClose
213  *
214  ************************************************************************/
215
216 static void
217 InstallClose(
218         HelpRec *helpRec,
219         XtCallbackProc callback,
220         XtPointer clientData )
221
222 {
223    XtAddCallback(helpRec->helpDialog, DtNcloseCallback, callback,
224                  clientData);
225 }
226
227
228 /************************************************************************
229  *
230  *  GetValues
231  *
232  ************************************************************************/
233
234 static XtPointer
235 GetValues(
236         HelpRec *helpRec )
237
238 {
239    DialogData * dialog_data;
240    HelpData * helpData;
241    HelpData * oldData;
242    Arg args[4];
243    int n;
244
245
246    /*  Allocate and initialize the help dialog data.  */
247
248    helpData = (HelpData *) XtMalloc(sizeof(HelpData));
249
250    helpData->displayed = True;
251
252    n = 0;
253    XtSetArg(args[n], XmNx, &helpData->x);           n++;
254    XtSetArg(args[n], XmNy, &helpData->y);           n++;
255    XtSetArg(args[n], XmNwidth, &helpData->width);   n++;
256    XtSetArg(args[n], XmNheight, &helpData->height); n++;
257    XtGetValues(helpRec->helpDialog, args, n);
258
259    dialog_data = _DtGetInstanceData((XtPointer)helpRec);
260    oldData = (HelpData *) dialog_data->data;
261    helpData->idString = XtNewString(oldData->idString);
262    helpData->volString = XtNewString(oldData->volString);
263    helpData->topicTitle = XtNewString(oldData->topicTitle);
264    helpData->fileType = XtNewString(oldData->fileType);
265    helpData->helpType = oldData->helpType;
266
267    return((XtPointer)helpData);
268 }
269
270
271
272
273 /************************************************************************
274  *
275  *  GetDefaultValues
276  *
277  ************************************************************************/
278
279 static XtPointer
280 GetDefaultValues( void )
281
282
283 {
284    HelpData * helpData;
285
286    /*  Allocate and initialize the default help dialog data.  */
287    helpData = (HelpData *) XtMalloc(sizeof(HelpData));
288
289    helpData->displayed = False;
290    helpData->x = 0;
291    helpData->y = 0;
292    helpData->height = 0;
293    helpData->width = 0;
294
295    helpData->idString = NULL;
296    helpData->volString = NULL;
297    helpData->topicTitle = NULL;
298    helpData->fileType = NULL;
299    helpData->helpType = 0;
300
301    return((XtPointer) helpData);
302 }
303
304
305
306
307 /************************************************************************
308  *
309  *  GetResourceValues
310  *
311  ************************************************************************/
312
313 static XtPointer
314 GetResourceValues(
315         XrmDatabase data_base,
316         char **name_list )
317
318 {
319    HelpData * helpData;
320
321    helpData = (HelpData *) XtMalloc(sizeof(HelpData));
322
323    _DtDialogGetResources(data_base, name_list, HELP_DIALOG, (char *)helpData,
324                       resources, helpClass->resource_count);
325
326    /* We need copies of all strings returned by the resource manager */
327    if (helpData->idString)
328       helpData->idString = XtNewString(helpData->idString);
329    if (helpData->volString)
330       helpData->volString = XtNewString(helpData->volString);
331    if (helpData->topicTitle)
332       helpData->topicTitle = XtNewString(helpData->topicTitle);
333    if (helpData->fileType)
334       helpData->fileType = XtNewString(helpData->fileType);
335
336    return((XtPointer) helpData);
337 }
338
339
340
341
342 /************************************************************************
343  *
344  *  SetValues
345  *
346  ************************************************************************/
347
348 static void
349 SetValues(
350         HelpRec *helpRec,
351         HelpData *helpData )
352
353 {
354    Arg args[10];
355    int n;
356    String helpString = NULL;
357    DtHelpDialogWidget diaShell;
358
359    diaShell = (DtHelpDialogWidget)helpRec->helpDialog;
360
361    n = 0;
362    XtSetArg(args[n], DtNhelpVolume, helpData->volString); n++;
363    XtSetArg(args[n], DtNhelpType, (unsigned char) helpData->helpType); n++;
364    if (helpData->helpType == DtHELP_TYPE_TOPIC)
365    {
366       XtSetArg(args[n], DtNlocationId, helpData->idString); n++;
367    }
368    else
369    {
370       helpString = MapFileTypeToHelpString(helpData->idString,
371                                            helpData->fileType);
372       XtSetArg(args[n], DtNstringData, helpString); n++;
373       XtSetArg(args[n], DtNtopicTitle, helpData->topicTitle); n++;
374    }
375    XtSetValues(helpRec->helpDialog, args, n);
376    XtFree(helpString);
377    diaShell = (DtHelpDialogWidget)helpRec->helpDialog;
378 }
379
380
381
382
383 /************************************************************************
384  *
385  *  WriteResourceValues
386  *
387  ************************************************************************/
388
389 static void
390 WriteResourceValues(
391         DialogData *values,
392         int fd,
393         char **name_list )
394
395 {
396    Arg args[5];
397    HelpRec * helpRec;
398    HelpData * helpData = (HelpData *) values->data;
399    String volString;
400    String idString;
401    String topicTitle;
402    String stringData;
403    unsigned char helpType;
404    int n;
405
406
407    /*  If the dialog is currently displayed, update the geometry  */
408    /*  fields to their current values.                            */
409    if (helpData->displayed == True)
410    {
411       _DtGenericUpdateWindowPosition(values);
412       helpRec = (HelpRec *)_DtGetDialogInstance(values);
413
414       /* Get the current help volume and locationId */
415       n = 0;
416       XtSetArg(args[n], DtNhelpVolume, &volString); n++;
417       XtSetArg(args[n], DtNlocationId, &idString); n++;
418       XtSetArg(args[n], DtNtopicTitle, &topicTitle); n++;
419       XtSetArg(args[n], DtNstringData, &stringData); n++;
420       XtSetArg(args[n], DtNhelpType, &helpType); n++;
421       XtGetValues(helpRec->helpDialog, args, n);
422       XtFree(helpData->volString);
423       helpData->helpType = (int) helpType;
424       helpData->volString = XtNewString(volString);
425       if (helpData->helpType == DtHELP_TYPE_TOPIC)
426       {
427          XtFree(helpData->idString);
428          XtFree(helpData->topicTitle);
429          helpData->idString = XtNewString(idString);
430          helpData->topicTitle = NULL;
431       }
432
433       _DtDialogPutResources(fd, name_list, HELP_DIALOG, values->data,
434                           resources, helpClass->resource_count);
435    }
436 }
437
438
439
440 /************************************************************************
441  *
442  *  FreeValues
443  *
444  ************************************************************************/
445
446 static void
447 FreeValues(
448         HelpData *helpData )
449
450 {
451    XtFree((char *) helpData->topicTitle);
452    XtFree((char *) helpData->idString);
453    XtFree((char *) helpData->volString);
454    XtFree((char *) helpData->fileType);
455    XtFree((char *) helpData);
456 }
457
458
459 /*
460  * The application must free up the returned string.
461  * If 'miscData' is not NULL, then we are attempting to get the description
462  * for a specific action, as defined for a specific filetype.
463  */
464
465 XtPointer
466 MapFileTypeToHelpString(
467    String filetype,
468    String miscData )
469
470 {
471    char * buf;
472    char * helpPattern;
473    char * desc;
474    Boolean isAction;
475    char * tmpStr;
476    char * label = NULL;
477
478    /* Special case for the 2 built in directory commands */
479    if (strcmp(filetype, openNewView) == 0)
480    {
481       tmpStr = (GETMESSAGE(29,7, "The `%s` action opens a new File Manager view window\nof the requested folder."));
482       desc = XtNewString(tmpStr);
483
484       buf = XtMalloc(strlen(desc) + strlen(filetype) + 100);
485       sprintf(buf, desc, filetype);
486       XtFree(desc);
487       return(buf);
488    }
489    if (strcmp(filetype, openInPlace) == 0)
490    {
491       tmpStr =
492        (GETMESSAGE(29,8, "The `%s` action displays the requested folder in\nthe current File Manager view window."));
493       desc = XtNewString(tmpStr);
494
495       buf = XtMalloc(strlen(desc) + strlen(filetype) + 100);
496       sprintf(buf, desc, filetype);
497       XtFree(desc);
498       return(buf);
499    }
500
501    desc = DtDtsDataTypeToAttributeValue(filetype,
502                                         DtDTS_DA_DESCRIPTION,
503                                         NULL);
504
505    isAction = DtDtsDataTypeIsAction(filetype);
506
507    if (desc)
508    {
509       char *descptr = XtNewString(desc);
510       XtFree((char *)desc);
511       if (descptr)
512          return descptr;
513       else
514          return NULL;
515    }
516
517    /* No description found for this filetype */
518    if (isAction)
519    {
520       tmpStr=(GETMESSAGE(29,5, "There is no help available for the %s action"));
521       helpPattern = XtNewString(tmpStr);
522    }
523    else
524    {
525       tmpStr =
526          (GETMESSAGE(29,6, "There is no help available for the %s filetype"));
527       helpPattern = XtNewString(tmpStr);
528    }
529
530    label = DtDtsDataTypeToAttributeValue(filetype,
531                                          DtDTS_DA_LABEL,
532                                          NULL);
533    if(label)
534    {
535        buf = XtMalloc(strlen(label) + strlen(helpPattern) + 100);
536        sprintf(buf, helpPattern, label);
537        DtDtsFreeAttributeValue(label);
538        label = NULL;
539    }
540    else
541    {
542        buf = XtMalloc(strlen(filetype) + strlen(helpPattern) + 100);
543        sprintf(buf, helpPattern, filetype);
544    }
545
546    XtFree(helpPattern);
547    DtDtsFreeAttributeValue(label);
548    return (buf);
549 }