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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
/*****
* example_1.c : simple demonstration on how to use XmHTML
*
* This file Version $Revision: 1.6 $
*
* Creation date: Mon Jan 27 02:06:08 GMT+0100 1997
* Last modification: $Date: 1997/10/23 00:28:37 $
* By: $Author: newt $
* Current State: $State: Exp $
*
* Author: newt
*
* Copyright (C) 1994-1997 by Ripley Software Development
* All Rights Reserved
*
* This file is part of the XmHTML Widget Library.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU [Library] General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU [Library] General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*****/
/*****
* ChangeLog
* $Log: example_1.c,v $
* Revision 1.6 1997/10/23 00:28:37 newt
* XmHTML Beta 1.1.0 release
*
* Revision 1.5 1997/05/28 02:02:49 newt
* Changes to reflect updated convenience function protos.
*
* Revision 1.4 1997/03/20 08:20:37 newt
* enlarged default with and height
*
* Revision 1.3 1997/03/11 20:08:55 newt
* Changed XmHTMLSetText to XmHTMLTextSet
*
* Revision 1.2 1997/03/04 01:02:45 newt
* Added printing of XmHTML version strings
*
* Revision 1.1 1997/02/11 01:58:25 newt
* Initial Revision
*
*****/
#define _XOPEN_SOURCE 500
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <Xm/Xm.h>
#include <Xm/PushB.h>
#include <Xm/Frame.h>
#include <Xm/Form.h>
#include <XmHTML/XmHTML.h>
/*** Private Variable Declarations ***/
static Widget html;
struct history {
struct history *prev;
char *href;
};
static struct history *history_top;
static char *current_page;
/*****
* Change this to change the application class of the examples
*****/
#define APP_CLASS "HTMLDemos"
/*****
* Name: exitCB
* Return Type: void
* Description: callback for the exit button
* In:
* widget: button widget id
* client_data:unused
* call_data: unused
* Returns:
*
*****/
static void
exitCB(Widget widget, XtPointer client_data, XtPointer call_data)
{
printf("Bye!\n");
exit(EXIT_SUCCESS);
}
static void
history_init(void) {
current_page = NULL;
history_top = NULL;
}
static void
history_push(String href) {
struct history *h = calloc(sizeof (struct history), 1);
if (h == NULL) {
perror("malloc");
exit(1);
}
h->href = strdup(href);
if (h->href == NULL) {
free(h);
perror("malloc");
exit(1);
}
h->prev = history_top;
history_top = h;
fprintf(stdout, "added history \"%s\"\n", h->href);
}
/*
* Caller must free the returned String.
*/
static String
history_pop(void) {
String s;
struct history *old = history_top;
if (history_top == NULL)
return NULL;
history_top = history_top->prev;
s = old->href;
free(old);
return s;
}
static String loadFile(String);
/*****
* Name: anchorCB
* Return Type: void
* Description: XmNactivateCallback for the XmHTML widget
* In:
* widget: widget id, in this case that of the HTML widget
* client_data:data registered with callback, unused
* cbs: XmHTML callback structure.
* Returns:
* nothing.
* Note:
* We don't care what sort of url has been selected.
* Setting the doit field to True instructs XmHTML to do it's own scrolling.
* XmHTML is smart enought to only scroll to a location in this document if
* it really exists.
* Setting the visited field also to True will cause XmHTML to render the
* selected anchor as being visited.
*****/
static void
anchorCB(Widget widget, XtPointer client_data,
XmHTMLAnchorCallbackStruct *cbs)
{
cbs->doit = True;
cbs->visited = True;
fprintf(stdout, "clicked link \"%s\"\n", cbs->href);
String content = loadFile(cbs->href);
if (content != NULL) {
/*
if (content[1] == 'p') {
printf("got content:\n%s\n", content);
}
*/
history_push(current_page);
free(current_page);
current_page = strdup(cbs->href);
/* XmHTMLTextSetString will mess up cbs, so need to use cbs first */
XmHTMLTextSetString(html, content);
}
free(content);
}
static void
history_print(void) {
struct history *h = history_top;
fprintf(stdout, "History:\n");
for (; h != NULL; h = h->prev)
fprintf(stdout, "%s\n", h->href);
fprintf(stdout, "\n");
}
static void
backCB(Widget widget, XtPointer client_data, XtPointer call_data) {
history_print();
String b = history_pop();
if (b == NULL)
return;
fprintf(stdout, "loading history \"%s\"\n", b);
String content = loadFile(b);
if (content != NULL) {
free(current_page);
current_page = strdup(b);
XmHTMLTextSetString(html, content);
}
free(content);
free(b);
}
/*****
* Name: loadFile
* Return Type: String
* Description: loads the contents of the given file.
* In:
* filename: name of the file to load
* Returns:
* contents of the loaded file.
*****/
static String
loadFile(String filename)
{
String file_to_open = filename;
FILE *file;
int size, istxt = 0;
static String content;
struct stat sb;
if (stat(filename, &sb) == -1) {
perror(filename);
return NULL;
}
if (S_ISDIR(sb.st_mode)) {
if (chdir(filename) != 0) {
perror(filename);
return NULL;
}
file_to_open = "index.html";
}
if (strcmp(filename + strlen(file_to_open) - 4, ".txt") == 0) {
istxt = 1;
fprintf(stdout, "IS TXT\n");
}
/* open the given file */
if((file = fopen(file_to_open, "r")) == NULL)
{
perror(file_to_open);
return(NULL);
}
/* see how large this file is */
if (fseek(file, 0, SEEK_END) == -1)
perror("fseek");
size = ftell(file);
if (size == -1)
perror("ftell");
rewind(file);
int filesize = size;
if (istxt)
size += 11; /* for <pre> </pre> */
/* allocate a buffer large enough to contain the entire file */
if((content = malloc(size+1)) == NULL)
{
fprintf(stderr, "malloc failed for %i bytes\n", size);
exit(EXIT_FAILURE);
}
/* now read the contents of this file */
if (istxt) {
content[0] = '<';
content[1] = 'p';
content[2] = 'r';
content[3] = 'e';
content[4] = '>';
}
if((fread(istxt ? content + 5 : content, 1, filesize, file)) != filesize)
printf("Warning: did not read entire file!\n");
if (istxt) {
content[size - 6] = '<';
content[size - 5] = '/';
content[size - 4] = 'p';
content[size - 3] = 'r';
content[size - 2] = 'e';
content[size - 1] = '>';
}
fclose(file);
/* sanity */
content[size] = '\0';
if (istxt) {
fprintf(stdout, "TXT file is:\n%s\n", content);
}
return(content);
}
/*****
* Name: main
* Return Type: int
* Description: main for example 1
* In:
* argc: no of arguments on command line
* argv: array of command line arguments
* Returns:
* EXIT_FAILURE when an error occurs, EXIT_SUCCESS otherwise
* Note:
* this example should be started with the name of a HTML file to display.
*****/
int
main(int argc, char **argv)
{
XtAppContext context;
Widget toplevel, form, frame, button, back;
String content;
fprintf(stderr, "%s, %i\n", XmHTMLVERSION_STRING, XmHTMLGetVersion());
/* check command line arguments, but allow for X11 options */
if(argc < 2)
{
printf("%s: simple XmHTML example\n", argv[0]);
printf("\tUsage: %s <filename>\n", argv[0]);
exit(EXIT_FAILURE);
}
content = loadFile(argv[1]);
/* create toplevel widget */
toplevel = XtVaAppInitialize(&context, APP_CLASS, NULL, 0,
&argc, argv, NULL, NULL, NULL);
/* XSynchronize( XtDisplay( toplevel ), True ); */
/* create a form as the main container */
form = XtVaCreateWidget("form",
xmFormWidgetClass, toplevel,
NULL);
/* create the exit button */
button = XtVaCreateManagedWidget("Exit",
xmPushButtonWidgetClass, form,
XmNtopAttachment, XmATTACH_FORM,
XmNtopOffset, 10,
XmNleftAttachment, XmATTACH_FORM,
XmNleftOffset, 10,
NULL);
/* add the exit callback */
XtAddCallback(button, XmNactivateCallback, (XtCallbackProc)exitCB,
NULL);
back = XtVaCreateManagedWidget("Back",
xmPushButtonWidgetClass, form,
XmNtopAttachment, XmATTACH_FORM,
XmNtopOffset, 10,
XmNleftAttachment, XmATTACH_WIDGET,
XmNleftWidget, button,
NULL);
XtAddCallback(back, XmNactivateCallback, backCB, NULL);
/* create a frame as the html container */
frame = XtVaCreateManagedWidget("frame",
xmFrameWidgetClass, form,
XmNtopAttachment, XmATTACH_WIDGET,
XmNtopWidget, button,
XmNtopOffset, 10,
XmNleftAttachment, XmATTACH_FORM,
XmNleftOffset, 10,
XmNbottomAttachment, XmATTACH_FORM,
XmNbottomOffset, 10,
XmNrightAttachment, XmATTACH_FORM,
XmNrightOffset, 10,
XmNshadowType, XmSHADOW_IN,
NULL);
/* create the HTML widget using the default resources */
html = XtVaCreateManagedWidget("html",
xmHTMLWidgetClass, frame,
XmNmarginWidth, 20,
XmNmarginHeight, 20,
XmNwidth, 600,
XmNheight, 500,
NULL);
/* Disable warnings (hopefully) */
XtVaSetValues(html,
XmNdebugDisableWarnings, 1,
XmNdebugEnableFullOutput, 0,
NULL);
history_init();
if(content == NULL)
XmHTMLTextSetString(html, "<html><body>Could not read given "
"file</body></html>");
else
{
current_page = strdup(argv[1]);
XmHTMLTextSetString(html, content);
free(content);
}
/* add a simple anchor callback so XmHTML can jump to local anchors */
XtAddCallback(html, XmNactivateCallback,
(XtCallbackProc)anchorCB, NULL);
/* manage the form */
XtManageChild(form);
/* realize the main application */
XtRealizeWidget(toplevel);
/* The HTML widget has the focus */
XmProcessTraversal(html, XmTRAVERSE_CURRENT);
/* enter the event loop */
XtAppMainLoop(context);
/* never reached, but keeps compiler happy */
exit(EXIT_SUCCESS);
}
|