Initial commit of pristine erwise source
[erwise] / erwise / Page.c
1 static char *rcsid = "$Id: Page.c,v 1.1 1992/05/18 21:43:03 tvr Exp $";
2
3 #include "Includes.h"
4
5
6 void getanddisplaypage(char *topaddress, HText_t * htext,
7                         HTextObject_t * htextobject);
8 int matchingstring(char *word);
9
10
11 char FindText[256] = "\0";
12 char SearchText[256] = "\0";
13 int SearchDepth = 1;
14 int SearchCase = FALSE;
15
16
17 static int SearchDialogType;
18 static int PageSettingsDialogType;
19
20 static char *CopyTopAddress;
21 static HText_t *CopyHText;
22 static HTextObject_t *CopyHTextObject;
23
24
25 void PageSearchCB(topaddress, htext, htextobject, parameter)
26 char *topaddress;
27 HText_t *htext;
28 HTextObject_t *htextobject;
29 void *parameter;
30 {
31     UiDisplaySearchDialog(SearchDialogType);
32 }
33
34
35 void PageCopyCB(topaddress, htext, htextobject, parameter)
36 char *topaddress;
37 HText_t *htext;
38 HTextObject_t *htextobject;
39 void *parameter;
40 {
41     CopyHTextObject = htextobject;
42     CopyTopAddress = strdup(topaddress);
43     CopyHText = htext;
44 }
45
46
47 void PageListCB(topaddress, htext, htextobject, parameter)
48 char *topaddress;
49 HText_t *htext;
50 HTextObject_t *htextobject;
51 void *parameter;
52 {
53     int i = 0, j, stringlength;
54     char **items = (char **) NULL;
55     char **addresses = (char **) NULL;
56     HTextObject_t *tmphtextobject = htext->first;
57     HTAnchor *tmpanchor, *destanchor;
58
59     while (tmphtextobject)
60         if (tmpanchor = (HTAnchor *) tmphtextobject->anchor) {
61             addresses = (char **) ReAlloc((void *) addresses,
62                                           ++i * sizeof(char *));
63             destanchor =
64                 HTAnchor_followMainLink((HTAnchor *) tmphtextobject->anchor);
65             addresses[i - 1] = HTAnchor_address(destanchor);
66             items = (char **) ReAlloc((void *) items, i * sizeof(char *));
67             j = 1;
68             items[i - 1] = (char *) NULL;
69             while (tmphtextobject &&
70                    (HTAnchor *) tmphtextobject->anchor == tmpanchor) {
71                 stringlength = strlen(tmphtextobject->data);
72                 j += stringlength;
73                 items[i - 1] = (char *) ReAlloc((void *) items[i - 1],
74                                                 j * sizeof(char));
75                 strcpy(&items[i - 1][j - stringlength - 1],
76                        tmphtextobject->data);
77                 items[i - 1][j - 1] = '\0';
78                 tmphtextobject = tmphtextobject->next;
79             }
80         } else
81             tmphtextobject = tmphtextobject->next;
82
83     UiDisplayListDialog(items, addresses, i, StartLoading);
84 }
85
86
87 void PageLoadToFileCB(topaddress, htext, htextobject, parameter)
88 char *topaddress;
89 HText_t *htext;
90 HTextObject_t *htextobject;
91 void *parameter;
92 {
93     HTAnchor *destanchor;
94
95     if (!htextobject || !htextobject->anchor) {
96         UiDisplayWarningDialog("No active tag", (void (*) (int)) NULL);
97         return;
98     }
99     destanchor = HTAnchor_followMainLink((HTAnchor *) htextobject->anchor);
100     if (ClCanLoadToFile(HTAnchor_address(destanchor)))
101         UiDisplayFileSelection(PageGetPageCB);
102     else
103         UiDisplayWarningDialog("Load to file not supported for this tag",
104                                (void (*) (int)) NULL);
105 }
106
107
108 int TruthValue(value)
109 char *value;
110 {
111     if (value && (!strncasecmp(value, STR_TRUE, strlen(STR_TRUE)) ||
112                   !strncasecmp(value, STR_ON, strlen(STR_ON)) ||
113                   !strncasecmp(value, STR_YES, strlen(STR_YES))))
114         return TRUE;
115     return FALSE;
116 }
117
118
119 void PagePrintCB(topaddress, htext, htextobject, parameter)
120 char *topaddress;
121 HText_t *htext;
122 HTextObject_t *htextobject;
123 void *parameter;
124 {
125     static void *table = (void *) NULL;
126     int update = FALSE;
127     char *configstr;
128
129     if (!table) {
130         update = TRUE;
131         table = ConfigGetValue((void *) NULL, C_PRINT);
132         PrintTopMargin = atoi((char *) ConfigGetValue(table, C_TOPMARGIN));
133         PrintBottomMargin =
134             atoi((char *) ConfigGetValue(table, C_BOTTOMMARGIN));
135         PrintLeftMargin = atoi((char *) ConfigGetValue(table, C_LEFTMARGIN));
136         PrintWidth = atoi((char *) ConfigGetValue(table, C_WIDTH));
137         if (configstr = (char *) ConfigGetValue(table, C_COMMAND))
138             strcpy(PrintCommand, configstr);
139         else
140             strcpy(PrintCommand, "");
141         PrintToFile = TruthValue((char *) ConfigGetValue(table, C_PRINTTOFILE));
142         if (configstr = (char *) ConfigGetValue(table, C_FILENAME))
143             strcpy(PrintFileName, configstr);
144         else
145             strcpy(PrintFileName, "");
146     }
147     UiDisplayPrintDialog(htext);
148
149     if (update) {
150         UiUpdateVariable("PrintTopMargin");
151         UiUpdateVariable("PrintBottomMargin");
152         UiUpdateVariable("PrintLeftMargin");
153         UiUpdateVariable("PrintWidth");
154         UiUpdateVariable("PrintCommand");
155         UiUpdateVariable("PrintToFile");
156         UiUpdateVariable("PrintFileName");
157     }
158 }
159
160
161 void PageSettingsCB(topaddress, htext, htextobject, parameter)
162 char *topaddress;
163 HText_t *htext;
164 HTextObject_t *htextobject;
165 void *parameter;
166 {
167     UiDisplayPageSettingsDialog(PageSettingsDialogType);
168 }
169
170
171 void PageCloseCB(topaddress, htext, htextobject, parameter)
172 char *topaddress;
173 HText_t *htext;
174 HTextObject_t *htextobject;
175 void *parameter;
176 {
177     char *address = HTAnchor_address((HTAnchor *) htext->node_anchor);
178     Page_t *toppage, *page;
179
180     toppage = FindPage(Pages, topaddress);
181     page = FindPage(toppage->Children, address);
182
183     UiDeletePage(toppage->Address, htext);
184     page->HText = (HText_t *) NULL;
185     HText_free(htext);
186
187     page = toppage->Children;
188     while (page)
189         if (page->HText)
190             return;
191         else
192             page = page->Next;
193
194     HierarchyClose(topaddress, htext, htextobject, parameter);
195 }
196
197
198 void PagePrevWordCB(topaddress, htext, htextobject, parameter)
199 char *topaddress;
200 HText_t *htext;
201 HTextObject_t *htextobject;
202 void *parameter;
203 {
204     HTextObject_t *tmphtextobject;
205
206     if (!htextobject)
207         return;
208
209     tmphtextobject = htextobject->prev;
210
211     while (tmphtextobject && !CanBeCursor(tmphtextobject))
212         tmphtextobject = tmphtextobject->prev;
213
214     if (tmphtextobject)
215         UiSetCursor(topaddress, htext, tmphtextobject);
216 }
217
218
219 void PageNextWordCB(topaddress, htext, htextobject, parameter)
220 char *topaddress;
221 HText_t *htext;
222 HTextObject_t *htextobject;
223 void *parameter;
224 {
225     HTextObject_t *tmphtextobject;
226
227     if (!htextobject)
228         return;
229
230     tmphtextobject = htextobject->next;
231
232     while (tmphtextobject && !CanBeCursor(tmphtextobject))
233         tmphtextobject = tmphtextobject->next;
234
235     if (tmphtextobject)
236         UiSetCursor(topaddress, htext, tmphtextobject);
237 }
238
239
240 void PagePrevTagCB(topaddress, htext, htextobject, parameter)
241 char *topaddress;
242 HText_t *htext;
243 HTextObject_t *htextobject;
244 void *parameter;
245 {
246     HTextAnchor_t *tmphtanchor;
247     HTAnchor *tmpanchor, *destanchor;
248     HText_t *newhtext;
249     HTextObject_t *newhtextobject;
250
251     if (!htextobject)
252         return;
253
254     tmpanchor = (HTAnchor *) htextobject->anchor;
255
256     /* Ignore htextobject with same anchor */
257     while (tmpanchor && htextobject &&
258            ((HTAnchor *) htextobject->anchor == tmpanchor))
259         htextobject = htextobject->prev;
260
261     /* Search for previous anchor */
262     while (htextobject && !htextobject->anchor)
263         htextobject = htextobject->prev;
264
265     /* No anchor found */
266     if (!htextobject)
267         return;
268
269     /* We want the first word in the tag to be highlighted */
270     while (htextobject->prev &&
271            (htextobject->anchor == htextobject->prev->anchor))
272         htextobject = htextobject->prev;
273
274     UiSetCursor(topaddress, htext, htextobject);
275
276     if ((int) parameter == NO_AUTOGET)
277         return;
278
279     getanddisplaypage(topaddress, htext, htextobject);
280 }
281
282
283 void PageNextTagCB(topaddress, htext, htextobject, parameter)
284 char *topaddress;
285 HText_t *htext;
286 HTextObject_t *htextobject;
287 void *parameter;
288 {
289     HTAnchor *tmpanchor;
290     HTextObject_t *newhtextobject;
291
292     if (!htextobject)
293         return;
294
295     tmpanchor = (HTAnchor *) htextobject->anchor;
296
297     /* Ignore htextobject with same anchor */
298     while (tmpanchor && htextobject &&
299            ((HTAnchor *) htextobject->anchor == tmpanchor))
300         htextobject = htextobject->next;
301
302     /* Search for previous anchor */
303     while (htextobject && !htextobject->anchor)
304         htextobject = htextobject->next;
305
306     /* No anchor found */
307     if (!htextobject)
308         return;
309
310     UiSetCursor(topaddress, htext, htextobject);
311
312     if ((int) parameter == NO_AUTOGET)
313         return;
314
315     getanddisplaypage(topaddress, htext, htextobject);
316 }
317
318
319 void PageHomeCB(topaddress, htext, htextobject, parameter)
320 char *topaddress;
321 HText_t *htext;
322 HTextObject_t *htextobject;
323 void *parameter;
324 {
325     StartLoading(topaddress, topaddress, (char *) NULL);
326 }
327
328
329 void PageRecallCB(topaddress, htext, htextobject, parameter)
330 char *topaddress;
331 HText_t *htext;
332 HTextObject_t *htextobject;
333 void *parameter;
334 {
335     Page_t *toppage, *page;
336     int i = 0;
337     char **items = (char **) NULL;
338
339     toppage = FindPage(Pages, topaddress);
340     page = toppage->Children;
341
342     while (page) {
343         items = (char **) ReAlloc((void *) items, ++i * sizeof(char *));
344         items[i - 1] = strdup(page->Address);
345         page = page->Next;
346     }
347
348     UiDisplayRecallDialog(items, i, StartLoading);
349 }
350
351
352 void PageBackCB(topaddress, htext, htextobject, parameter)
353 char *topaddress;
354 HText_t *htext;
355 HTextObject_t *htextobject;
356 void *parameter;
357 {
358     char *address = HTAnchor_address((HTAnchor *) htext->node_anchor);
359     Page_t *toppage, *parentpage, *page;
360
361     toppage = FindPage(Pages, topaddress);
362     page = FindPage(toppage->Children, address);
363
364     if (parentpage = page->Parents) {
365         if (parentpage->Next)
366             PageGeneratePopup(parentpage, topaddress);
367         else {
368             while (parentpage->Next)
369                 parentpage = parentpage->Next;
370             StartLoading(parentpage->Address, topaddress, parentpage->Address);
371         }
372     }
373 }
374
375
376 PageGeneratePopup(parentpage, topaddress)
377 Page_t *parentpage;
378 char *topaddress;
379 {
380     char **items = (char **) NULL;
381     int nitems = 0;
382
383     while (parentpage) {
384         items = (char **) ReAlloc((void *) items, ++nitems * sizeof(char *));
385         items[nitems - 1] = parentpage->Address;
386         parentpage = parentpage->Next;
387     }
388
389     UiDisplayPopup(StartLoading, topaddress, items, nitems);
390 }
391
392
393 HTextObject_t *
394  FindHTextObject(htext, address)
395 HText_t *htext;
396 char *address;
397 {
398     HTextObject_t *newhtextobject;
399     HTAnchor *destanchor;
400
401     newhtextobject = htext->first;
402     while (newhtextobject) {
403         if (newhtextobject->anchor) {
404             destanchor =
405                 HTAnchor_followMainLink((HTAnchor *) newhtextobject->anchor);
406             if (destanchor && !strcmp(HTAnchor_address(destanchor), address))
407                 return newhtextobject;
408         }
409         newhtextobject = newhtextobject->next;
410     }
411
412     return (HTextObject_t *) NULL;
413 }
414
415
416 void PagePrevPageCB(topaddress, htext, htextobject, parameter)
417 char *topaddress;
418 HText_t *htext;
419 HTextObject_t *htextobject;
420 void *parameter;
421 {
422     char *address = HTAnchor_address((HTAnchor *) htext->node_anchor);
423     Page_t *toppage, *parentpage, *page;
424     HTextObject_t *newhtextobject;
425
426     toppage = FindPage(Pages, topaddress);
427     page = FindPage(toppage->Children, address);
428
429     if (parentpage = page->Parents) {
430         if (parentpage->Next)
431             PageGeneratePopup(parentpage, topaddress);
432         while (parentpage->Next)
433             parentpage = parentpage->Next;
434         parentpage = parentpage->ParentPage;
435         if (parentpage->HText) {
436             newhtextobject = FindHTextObject(parentpage->HText, page->Address);
437             if (newhtextobject)
438                 PagePrevTagCB(topaddress, parentpage->HText,
439                               newhtextobject, (void *) AUTOGET);
440         } else
441             StartLoading(parentpage->Address, topaddress, (char *) NULL);
442     }
443 }
444
445
446 void PageNextPageCB(topaddress, htext, htextobject, parameter)
447 char *topaddress;
448 HText_t *htext;
449 HTextObject_t *htextobject;
450 void *parameter;
451 {
452     char *address = HTAnchor_address((HTAnchor *) htext->node_anchor);
453     Page_t *toppage, *parentpage, *page;
454     HTextObject_t *newhtextobject;
455
456     toppage = FindPage(Pages, topaddress);
457     page = FindPage(toppage->Children, address);
458
459     if (parentpage = page->Parents) {
460         if (parentpage->Next)
461             PageGeneratePopup(parentpage, topaddress);
462         while (parentpage->Next)
463             parentpage = parentpage->Next;
464         parentpage = parentpage->ParentPage;
465         if (parentpage->HText) {
466             newhtextobject = FindHTextObject(parentpage->HText, page->Address);
467             if (newhtextobject)
468                 PageNextTagCB(topaddress, parentpage->HText,
469                               newhtextobject, (void *) AUTOGET);
470         } else
471             StartLoading(parentpage->Address, topaddress, (char *) NULL);
472     }
473 }
474
475
476 void PageGetPageCB(topaddress, htext, htextobject, parameter)
477 char *topaddress;
478 HText_t *htext;
479 HTextObject_t *htextobject;
480 void *parameter;
481 {
482     if (!htextobject || !htextobject->anchor)
483         return;
484
485     if (parameter)
486         ClSetFileNameForLoadingToFile((char *) parameter);
487
488     getanddisplaypage(topaddress, htext, htextobject);
489 }
490
491
492 void PageClickCB(topaddress, htext, htextobject, parameter)
493 char *topaddress;
494 HText_t *htext;
495 HTextObject_t *htextobject;
496 void *parameter;
497 {
498     HTextObject_t *tmphtextobject = htext->first;
499     int state = 0;
500
501     if (CopyHTextObject && !strcmp(topaddress, CopyTopAddress)
502         && htext == CopyHText)
503         while (tmphtextobject) {
504             if (tmphtextobject == CopyHTextObject ||
505                 tmphtextobject == htextobject)
506                 state += 1 + (CopyHTextObject == htextobject);
507             switch (state) {
508             case 2:
509                 if (tmphtextobject->data)
510                     UiAddStringToCutBuffer(tmphtextobject->data);
511                 else
512                     UiAddStringToCutBuffer("\n");
513                 UiAddStringToCutBuffer((char *) NULL);
514                 tmphtextobject = (HTextObject_t *) NULL;
515                 Free(CopyTopAddress);
516                 break;
517             case 1:
518                 if (tmphtextobject->data)
519                     UiAddStringToCutBuffer(tmphtextobject->data);
520                 else
521                     UiAddStringToCutBuffer("\n");
522                 /* Fall through */
523             default:
524                 tmphtextobject = tmphtextobject->next;
525             }
526         }
527
528     CopyHTextObject = (HTextObject_t *) NULL;
529 }
530
531
532 void IndexFindCB(topaddress, htext, htextobject, parameter)
533 char *topaddress;
534 HText_t *htext;
535 HTextObject_t *htextobject;
536 void *parameter;
537 {
538     char *newaddress;
539
540     if (!FindText[0])
541         return;
542
543     HTMainAnchor = htext->node_anchor;  /* Kludguality */
544     newaddress = (char *) HTSearchAddress(FindText);    /* missa proto, meeTu? */
545
546     StartLoading(newaddress, topaddress,
547                  HTAnchor_address((HTAnchor *) htext->node_anchor));
548     Free(newaddress);
549 }
550
551
552 static char *hiertopaddress;
553 static HText_t *hierhtext;
554 static HTextObject_t *hierhtextobject;
555 static void *hierparameter;
556
557 void HierarchyCloseCB(topaddress, htext, htextobject, parameter)
558 char *topaddress;
559 HText_t *htext;
560 HTextObject_t *htextobject;
561 void *parameter;
562 {
563     hiertopaddress = topaddress;
564     hierhtext = htext;
565     hierhtextobject = htextobject;
566     hierparameter = parameter;
567
568     UiDisplayWarningDialog("Close hierarchy?", HierarchyNukeCB);
569 }
570
571
572 void HierarchyNukeCB(button)
573 int button;
574 {
575     HierarchyClose(hiertopaddress, hierhtext, hierhtextobject, hierparameter);
576 }
577
578
579 void HierarchyClose(topaddress, htext, htextobject, parameter)
580 char *topaddress;
581 HText_t *htext;
582 HTextObject_t *htextobject;
583 void *parameter;
584 {
585     Page_t *toppage;
586
587     toppage = FindPage(Pages, topaddress);
588
589     while (toppage->Children) {
590         if (toppage->Children->HText) {
591             HText_free(toppage->Children->HText);
592             UiDeletePage(toppage->Address, toppage->Children->HText);
593         }
594         while (toppage->Children->Parents)
595             DeletePage(&toppage->Children->Parents,
596                        toppage->Children->Parents->Address);
597         DeletePage(&toppage->Children, toppage->Children->Address);
598     }
599
600     DeletePage(&Pages, toppage->Address);
601 }
602
603
604 void SearchBackwardCB(topaddress, htext, htextobject, parameter)
605 char *topaddress;
606 HText_t *htext;
607 HTextObject_t *htextobject;
608 void *parameter;
609 {
610     HTextObject_t *newhtextobject;
611
612     if (!htextobject)
613         return;
614
615     newhtextobject = htextobject->prev;
616     while (newhtextobject && !matchingstring(newhtextobject->data))
617         newhtextobject = newhtextobject->prev;
618
619     if (newhtextobject)
620         UiSetCursor(topaddress, htext, newhtextobject);
621 }
622
623
624 void SearchForwardCB(topaddress, htext, htextobject, parameter)
625 char *topaddress;
626 HText_t *htext;
627 HTextObject_t *htextobject;
628 void *parameter;
629 {
630     HTextObject_t *newhtextobject;
631
632     if (!htextobject)
633         return;
634
635     newhtextobject = htextobject->next;
636     while (newhtextobject && !matchingstring(newhtextobject->data))
637         newhtextobject = newhtextobject->next;
638
639     if (newhtextobject)
640         UiSetCursor(topaddress, htext, newhtextobject);
641 }
642
643
644 void ConnectionsCB(topaddress, htext, htextobject, parameter)
645 char *topaddress;
646 HText_t *htext;
647 HTextObject_t *htextobject;
648 void *parameter;
649 {
650     Connection_t *tmpconnection = Connections;
651     char **listitems = (char **) NULL;
652     void **connections = (void **) NULL;
653     int nitems = 0;
654
655     while (tmpconnection) {
656         listitems = (char **) ReAlloc((void *) listitems,
657                                       ++nitems * sizeof(char *));
658         listitems[nitems - 1] = tmpconnection->Address;
659         connections = (void **) ReAlloc((void *) connections,
660                                         nitems * sizeof(void *));
661         connections[nitems - 1] = tmpconnection;
662         tmpconnection = tmpconnection->Next;
663     }
664
665     UiDisplayConnectionsDialog(listitems, connections, nitems, KillCB);
666 }
667
668
669 void KillCB(connection)
670 void *connection;
671 {
672     Connection_t *tmpconnection = (Connection_t *) connection;
673
674     switch (tmpconnection->Status) {
675     case SELECTING:
676         UiDeleteInputFD(tmpconnection->InputId);
677         break;
678     case POLLING:
679         UiDeleteTimeOut(tmpconnection->TimeOutId);
680     }
681
682     ClCloseConnection(tmpconnection->ClConnection);
683     DeleteConnection(tmpconnection->Address);
684 }
685
686
687 void ControlPanelCB(topaddress, htext, htextobject, parameter)
688 char *topaddress;
689 HText_t *htext;
690 HTextObject_t *htextobject;
691 void *parameter;
692 {
693     UiDisplayControlPanel();
694 }
695
696
697 void DefaultsCB(topaddress, htext, htextobject, parameter)
698 char *topaddress;
699 HText_t *htext;
700 HTextObject_t *htextobject;
701 void *parameter;
702 {
703     UiDisplayDefaultsDialog();
704 }
705
706
707 void getanddisplaypage(topaddress, htext, htextobject)
708 char *topaddress;
709 HText_t *htext;
710 HTextObject_t *htextobject;
711 {
712     HTAnchor *destanchor;
713
714     destanchor = HTAnchor_followMainLink((HTAnchor *) htextobject->anchor);
715     HTMainAnchor = (HTParentAnchor *) NULL;
716
717     StartLoading(HTAnchor_address(destanchor),
718                  topaddress,
719                  HTAnchor_address((HTAnchor *) htext->node_anchor));
720 }
721
722
723 int matchingstring(word)
724 char *word;
725 {
726     char *tmpptr = word;
727     int searchlength = strlen(SearchText);
728
729     while (tmpptr && strlen(tmpptr) >= searchlength) {
730         if (SearchCase) {
731             if (!strncmp(tmpptr, SearchText, searchlength))
732                 return TRUE;
733         } else if (!strncasecmp(tmpptr, SearchText, searchlength))
734             return TRUE;
735         tmpptr++;
736     }
737
738     return FALSE;
739 }