File: help.c

package info (click to toggle)
via 1.6.0-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,480 kB
  • ctags: 2,266
  • sloc: ansic: 30,757; makefile: 101; sh: 46
file content (317 lines) | stat: -rwxr-xr-x 9,178 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 *  $Id: help.c 3177 2008-04-01 14:47:24Z karstenm $
 *
 *  This file implements help dialogs.
 */

/*
 *  Copyright 1993, 1994 University of British Columbia
 *
 *  Permission to use, copy, modify, distribute, and sell this software and its
 *  documentation for any purpose is hereby granted without fee, provided that
 *  the above copyright notice appears in all copies and that both that
 *  copyright notice and this permission notice appear in supporting
 *  documentation. UBC makes no representations about the suitability of this
 *  software for any purpose. It is provided "as is" without express or
 *  implied warranty.
 *
 *  Author: Arthur Pope, UBC Laboratory for Computational Intelligence
 */

/* From the local directory: */
#include "vxview.h"

/* From the Motif toolkit: */
#include <Xm/Form.h>
#include <Xm/LabelG.h>
#include <Xm/List.h>
#include <Xm/MessageB.h>
#include <Xm/MwmUtil.h>
#include <Xm/PanedW.h>
#include <Xm/PushBG.h>
#include <Xm/ScrolledW.h>
#include <Xm/Text.h>

/* From the Vista library: */
#include <viaio/mu.h>

/* From the standard C library: */
#include <ctype.h>

/* File identification string: */
VRcsId ("$Id: help.c 3177 2008-04-01 14:47:24Z karstenm $");

/* File containing help text: */
#ifndef helpFilename
#define helpFilename	"vxview.help"
#endif

/* List of topics and their blurbs: */
#define maxTopics	30
typedef struct {
    char *topic;			/* name of topic */
    char *text;				/* blurb on topic */
    int len;				/* length of blurb */
} Topic;
static int ntopics;
static Topic *topics;

/* Widgets used to display help info: */
static Widget helpDialog;		/* dialog */
static Widget helpTopics;		/* XmList of help topics */
static Widget helpText;			/* XmText displaying help text */

/* Later in this file: */
static void CreateHelpDialog (void);
static void LoadHelpFile (void);
static void HelpSelectCB (Widget, XtPointer, XtPointer);
static void HelpDestroyCB (Widget, XtPointer, XtPointer);


/*
 *  ShowVersionDialog
 *
 *  Handler for a Vista warning, which reports it with a pop-up dialog.
 */

void ShowVersionDialog (View view)
{
    Widget dialog;
    Arg al[] = { { XmNmessageString },
		 { XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL } };

    #define programVersion          \
        "vxview version " ## Version ## ", compiled " ## __DATE__ ## "\n\n" \
        "Laboratory for Computational Intelligence\n" \
	"University of British Columbia"

    /* Create a dialog displaying the message: */
    al[0].value = (XtArgVal) XmStringCreateLtoR ((char *) "bla",
						 XmSTRING_DEFAULT_CHARSET);
    dialog = XmCreateInformationDialog (view->view_shell, "version",
					al, XtNumber (al));
    XmStringFree ((XmString) al[0].value);
    XtVaSetValues (XtParent (dialog),
		   XmNdeleteResponse, XmDESTROY, (char *) NULL);
    XtUnmanageChild (XmMessageBoxGetChild (dialog, XmDIALOG_CANCEL_BUTTON));
    XtUnmanageChild (XmMessageBoxGetChild (dialog, XmDIALOG_HELP_BUTTON));
    XtAddCallback (dialog, XmNokCallback,
		   (XtCallbackProc) XtDestroyWidget, NULL);
    XtManageChild (dialog);

    #undef programVersion
}


/*
 *  ShowHelpDialog
 *
 *  Pop-up dialog providing help information.
 *  Arguments are defined for use as a callback.
 */

void ShowHelpDialog (Widget w, XtPointer cl_data, XtPointer ca_data)
{
    VStringConst topic = (VStringConst) cl_data;
    int i;

    /* Create the help dialog if it doesn't already exist: */
    if (! helpDialog) {
	CreateHelpDialog ();
	XmListSelectPos (helpTopics, 1, TRUE);
    }

    /* If a topic was specified, bring it into view: */
    if (topic) {
	for (i = 0; i < ntopics; i++)
	    if (strcmp (topic, topics[i].topic) == 0)
		break;
	if (i < ntopics)
	    XmListSelectPos (helpTopics, i + 1, TRUE);
    }

    /* Pop-up the help dialog: */
    XtManageChild (helpDialog);
    XRaiseWindow (myDisplay, XtWindow (helpDialog));
}


/*
 *  CreateHelpDialog
 *
 *  Create the help index and text dialogs for later use
 */

static void CreateHelpDialog (void)
{
    Widget pane, form, buttons, w;
    int i;
    XmString str;
    static Arg list_args[] = { { XmNlistSizePolicy, XmCONSTANT },
			       { XmNselectionPolicy, XmSINGLE_SELECT } };
    static Arg text_args[] = { { XmNcursorPositionVisible, FALSE },
			       { XmNeditable, FALSE },
			       { XmNeditMode, XmMULTI_LINE_EDIT } };

    /* Read text about various topics from the help file: */
    if (ntopics == 0)
	LoadHelpFile ();

    /* Create the help dialog: */
    helpDialog = XVCPS ("help", topLevelShellWidgetClass, topLevelShell,
			XmNcolormap, VColormapColormap (vcolormap),
			XmNiconPixmap, iconPixmap,
			XmNmwmInputMode, MWM_INPUT_MODELESS,
			XmNvisual, VColormapVisual (vcolormap),
			(char *) NULL);
    XtAddCallback (helpDialog, XmNdestroyCallback, HelpDestroyCB, NULL);

    /* Use a pane widget to separate the dialog into a top and bottom areas: */
    pane = XVCW ("pane", xmPanedWindowWidgetClass, helpDialog,
		 XmNsashWidth, 1,		/* to prevent moving sash */
		 XmNsashHeight, 1, (char *) NULL);

    /* Create a form containing the topic list and text at the top of
       the dialog: */
    form = XVCW ("form1", xmFormWidgetClass, pane, (char *) NULL);

    w = XVCMW ("topics_label", xmLabelGadgetClass, form,
	       XmNtopAttachment, XmATTACH_FORM,
	       XmNleftAttachment, XmATTACH_FORM, (char *) NULL);

    helpTopics = XmCreateScrolledList (form, "topic_list",
				       list_args, XtNumber (list_args));
    XtVaSetValues (XtParent (helpTopics),
		   XmNtopAttachment, XmATTACH_WIDGET,
		   XmNtopWidget, w,
		   XmNleftAttachment, XmATTACH_FORM,
		   XmNbottomAttachment, XmATTACH_FORM, (char *) NULL);
    XtAddCallback (helpTopics, XmNsingleSelectionCallback, HelpSelectCB, NULL);
    for (i = 0; i < ntopics; i++) {
	str = XmStringCreateSimple (topics[i].topic);
	XmListAddItemUnselected (helpTopics, str, 0);
    }

    XtManageChild (helpTopics);

    w = XVCMW ("text_label", xmLabelGadgetClass, form,
	       XmNtopAttachment, XmATTACH_FORM,
	       XmNleftAttachment, XmATTACH_WIDGET,
	       XmNleftWidget, helpTopics, (char *) NULL);

    helpText = XmCreateScrolledText (form, "text",
				     text_args, XtNumber (text_args));
    XtVaSetValues (XtParent (helpText),
		   XmNtopAttachment, XmATTACH_WIDGET,
		   XmNtopWidget, w,
		   XmNbottomAttachment, XmATTACH_FORM,
		   XmNleftAttachment, XmATTACH_WIDGET,
		   XmNleftWidget, helpTopics,
		   XmNrightAttachment, XmATTACH_FORM, (char *) NULL);
    XtManageChild (helpText);

    /* Create a form containing the Close button at the bottom of the
       dialog: */
    buttons = XVCW ("buttons", xmFormWidgetClass, pane,
		    XmNfractionBase, 10, (char *) NULL);

    w = XVCMW ("close", xmPushButtonGadgetClass, buttons,
	       XmNtopAttachment, XmATTACH_FORM,
	       XmNbottomAttachment, XmATTACH_FORM,
	       XmNleftAttachment, XmATTACH_POSITION,
	       XmNleftPosition, 1,
	       XmNrightAttachment, XmATTACH_POSITION,
	       XmNrightPosition, 3,
	       XmNshowAsDefault, TRUE,
	       (char *) NULL);
    XtAddCallback (w, XmNactivateCallback,
		   UnmanageCallback, (XtPointer) helpDialog);
    XtVaSetValues (form, XmNcancelButton, w, (char *) NULL);
    XtVaSetValues (form, XmNdefaultButton, w, (char *) NULL);
    XtVaSetValues (buttons, XmNcancelButton, w, (char *) NULL);
    XtVaSetValues (buttons, XmNdefaultButton, w, (char *) NULL);

    XtManageChild (buttons);
    FixButtonPane (w);
    XtManageChild (form);
    XtManageChild (pane);
}


/*
 *  LoadHelpFile
 *
 *  Load the file containing help topics and their blurbs.
 */

static void LoadHelpFile (void)
{
    FILE *f;
    size_t len, incr;
    Topic *topic;
    char buf[100];

    ntopics = 0;
    topics = topic = VMalloc (sizeof (Topic) * maxTopics);
    if (! (f = fopen (helpFilename, "r"))) {
	VWarning ("Unable to open help database %s", helpFilename);
	ntopics = 1;
	topic->topic = "(No help topics)";
	topic->text = "(No help text)";
	topic->len = strlen (topic->text);
	return;
    }

    do {
	fgets (buf, sizeof (buf), f);
    } while (! feof (f) && buf[0] != '@');

    while (! feof (f) && ntopics < maxTopics) {
	len = strlen (buf);
	if (buf[len - 1] == '\n')
	    buf[len - 1] = 0;
	topic->topic = VNewString (buf + 1);
	topic->text = NULL;
	len = 0;
	while (1) {
	    fgets (buf, sizeof (buf), f);
	    if (feof (f) || buf[0] == '@')
		break;
	    incr = strlen (buf);
	    topic->text = VRealloc ((XtPointer) topic->text, len + incr + 1);
	    strcpy (topic->text + len, buf);
	    len += incr;
	}
	while (len > 0 && isspace (topic->text[len - 1]))
	    len--;
	topic->text[len] = 0;
	topic->len = len;
	ntopics++;
	topic++;
    }

    fclose (f);
}


/*
 *  Help dialog callbacks.
 *
 *  HelpSelectCB	help topic selected
 *  HelpDestroyCB	help dialog destroyed via window manager
 */

/* ARGSUSED */
static void HelpSelectCB (Widget w, XtPointer cl_data, XtPointer ca_data)
{
    XmListCallbackStruct *cb = (XmListCallbackStruct *) ca_data;
    Topic *topic = & topics[cb->item_position - 1];

    XmTextSetString (helpText, (char *) topic->text);
}

/* ARGSUSED */
static void HelpDestroyCB (Widget w, XtPointer cl_data, XtPointer ca_data)
{
    helpDialog = NULL;
}