history.c
上传用户:zlh9724
上传日期:2007-01-04
资源大小:1991k
文件大小:11k
源码类别:

浏览器

开发平台:

Unix_Linux

  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #include "www.h"
  4. extern Context *context;
  5. extern int debug;
  6. extern Doc *CurrentDoc;
  7. extern int sbar_width;
  8. extern unsigned int win_width, win_height;
  9. extern int statusHeight;
  10. extern int ToolBarHeight;
  11. extern long buf_height;
  12. extern int buf_width;
  13. extern int PixelIndent;
  14. /* The multithreaded library complicates the history
  15.    mechannism. Several documents may be requested simultaneously, and
  16.    they may arrive in a different order than they were clicked
  17.    in. Therefore, putting a document into the history is a two-stem
  18.    porcess: when the user clicks, HistoryRecord put it into the
  19.    history, but does not enable it -- then, when a document arrives,
  20.    HistoryVerify enables it. */
  21. History *NewHistory()
  22. {
  23.     History *h;
  24.     h = ((History *) calloc (sizeof(History), 1));
  25.     if (!h)
  26. return NULL;
  27.     h->state = HISTORY_NOTREGISTERED;
  28.     h->tag = NULL;
  29.     h->y = 0;
  30.     return h;
  31. }
  32. /* debugging routine which prints out the current history stack */
  33. void HistoryList()
  34. {
  35.     HTList *ll, *l = context->history;
  36.     History *h;
  37.     HTAnchor *a;
  38.     ll = l;
  39.     fprintf(stderr,"n+++++++ list history, current position %d n_ele %dn", context->history_pos, HTList_count(context->history));
  40.     while ( (h = (History *)HTList_nextObject(l)) ) {
  41. a = h->anchor;
  42. #if defined PRINTF_HAS_PFORMAT
  43. fprintf(stderr," h=%p anchor=%p state %d ",h,a,h->state);
  44. #else
  45. fprintf(stderr," h=%lx anchor=%lx state %d ",h,a,h->state);
  46. #endif /* PRINTF_HAS_PFORMAT */
  47. if (h->title)
  48.     fprintf(stderr,"h->title="%s"",h->title);
  49. if (a->parent->physical)
  50.     fprintf(stderr,"%s",a->parent->physical);
  51. else if (a->parent->address)
  52.     fprintf(stderr,"%s",a->parent->address);
  53. else if (a->parent->document) {
  54.     Doc *d = (Doc *)a->parent->document;
  55.     fprintf(stderr,"%s",d->url);
  56. }
  57. if ((HTAnchor *)a->parent == a)
  58.     fprintf(stderr," PARENT ");
  59. else
  60.     fprintf(stderr," CHILD: %s ",h->tag);
  61. fprintf(stderr," list position %dn",HTList_indexOf(ll,h));
  62.     }
  63.     fprintf(stderr,"+++++++ end list historynn");
  64. }
  65. /* go to home document */
  66. void HomeDoc()
  67. {
  68.     if (context->home_anchor->parent->document) {
  69. if (HISTORY_TRACE)
  70.     fprintf(stderr,"HomeDocn");
  71. context->history_pos = HTList_count (context->history) - 1;
  72. context->current_history = (History *)HTList_objectAt(context->history, context->history_pos);
  73. CurrentDoc = (Doc *) context->home_anchor->parent->document;
  74. CurrentDoc->show_raw = FALSE;
  75. NewBuffer(CurrentDoc);
  76. DeltaHTMLPosition(context->current_history->y);
  77. DisplayDoc(WinLeft, WinTop, WinWidth, WinHeight);
  78. SetScrollBarHPosition(PixelIndent, buf_width);
  79. SetScrollBarVPosition(context->current_history->y, buf_height);
  80. DisplayScrollBar();
  81. Announce(CurrentDoc->url);
  82.     }
  83.     else {
  84. if (HISTORY_TRACE)
  85.     fprintf(stderr,"HomeDoc: home_doc freed??????n");
  86. libLoadAnchor((HTAnchor *)context->home_anchor, NULL, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE);
  87.     }
  88.     if (HISTORY_TRACE)
  89. HistoryList();
  90. }
  91. void ForwardDoc()
  92. {
  93.     History *h = NULL;
  94.     HTAnchor *a;
  95.     Doc *d;
  96.     int pos; 
  97.     if (HISTORY_TRACE)
  98. fprintf(stderr,"->ForwardDocn");
  99.     pos = context->history_pos;
  100.     while (pos > 0) {
  101. pos--;
  102. h = (History *)HTList_objectAt(context->history, pos);
  103. if (!h) {
  104.     if (HISTORY_TRACE)
  105. fprintf(stderr,"ForwardDoc: no history to be found n");
  106.     continue;
  107. }
  108. if (h->state == HISTORY_VERIFIED) {
  109.     break;
  110. }
  111. if (HISTORY_TRACE)
  112.     fprintf(stderr,"ForwardDoc: selected item either not loaded or deleted %dn",h->state);
  113. h = NULL;
  114.     }
  115.     if (!h) {
  116. if (HISTORY_TRACE)
  117.     fprintf(stderr,"ForwardDoc: no history to be found n");
  118. return;
  119.     }
  120.     /* from here we assume success */
  121.     context->history_pos = pos;
  122.     context->current_history = (History *)HTList_objectAt(context->history, context->history_pos);
  123.     a = h->anchor;
  124.     if (HISTORY_TRACE)
  125. fprintf(stderr,"ForwardDoc, pos = %d, a = %sn", context->history_pos, a->parent->physical);
  126.     
  127.     if ((d = (Doc *) a->parent->document)) {
  128. if (d->state >= DOC_LOADED) {
  129.     CurrentDoc = (Doc *) d;
  130.     CurrentDoc->tag = h->tag;
  131.     CurrentDoc->show_raw = FALSE;
  132.     NewBuffer(CurrentDoc);
  133.     /* if there is a tag, the positioning is being taken care of */
  134.     if (!h->tag)
  135. DeltaHTMLPosition(context->current_history->y);
  136.     DisplayDoc(WinLeft, WinTop, WinWidth, WinHeight);
  137.     if (!h->tag) {
  138. SetScrollBarHPosition(PixelIndent, buf_width);
  139. SetScrollBarVPosition(context->current_history->y, buf_height);
  140.     }
  141.     DisplayScrollBar();
  142.     DisplayUrl();
  143. }
  144. else {
  145.     fprintf(stderr,"ForwardDoc: WEIRD, doc is reg'd but state is lown");
  146. }
  147.     } else {
  148. libLoadAnchor(a, NULL, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE);
  149.     }
  150.     if (HISTORY_TRACE)
  151. HistoryList();
  152. }
  153. void BackDoc()
  154. {
  155.     History *h = NULL;
  156.     HTAnchor *a;
  157.     Doc *d;
  158.     int pos; 
  159.     if (HISTORY_TRACE)
  160. fprintf(stderr,"->BackDocn");
  161.     pos = context->history_pos;
  162.     while (pos < (HTList_count(context->history) - 1)) {
  163. pos++;
  164. h = (History *)HTList_objectAt(context->history, pos);
  165. if (!h) {
  166.     if (HISTORY_TRACE)
  167. fprintf(stderr,"BackDoc: no history to be found n");
  168.     continue;
  169. }
  170. if (h->state == HISTORY_VERIFIED) {
  171.     break;
  172. }
  173. if (HISTORY_TRACE)
  174.     fprintf(stderr,"BackDoc: selected item not yet confirmed loaded %dn",h->state);
  175. h = NULL;
  176.     }
  177.     if (!h) {
  178. if (HISTORY_TRACE)
  179.     fprintf(stderr,"BackDoc: no history to be found n");
  180. return;
  181.     }
  182.     /* from here we assume success */
  183.     context->history_pos = pos;
  184.     context->current_history = (History *)HTList_objectAt(context->history, context->history_pos);
  185.     a = h->anchor;
  186.     if (HISTORY_TRACE)
  187. fprintf(stderr,"BackDoc, pos = %d, a = %sn", context->history_pos, a->parent->physical);
  188.     
  189.     if ((d = (Doc *) a->parent->document)) {
  190. if (d->state >= DOC_LOADED) {
  191.     CurrentDoc = (Doc *) d;
  192.     CurrentDoc->tag = h->tag;
  193.     CurrentDoc->show_raw = FALSE;
  194.     NewBuffer(CurrentDoc);
  195.     DeltaHTMLPosition(context->current_history->y);
  196.     DisplayDoc(WinLeft, WinTop, WinWidth, WinHeight);
  197.     SetScrollBarHPosition(PixelIndent, buf_width);
  198.     SetScrollBarVPosition(context->current_history->y, buf_height);
  199.     DisplayScrollBar();
  200.     DisplayUrl();
  201. }
  202. else {
  203.     fprintf(stderr,"BackDoc: WEIRD, doc is reg'd but state is lown");
  204. }
  205.     } else {
  206. libLoadAnchor(a, NULL, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE);
  207.     }
  208.     if (HISTORY_TRACE)
  209. HistoryList();
  210. }
  211. void GotoDoc(int pos)
  212. {
  213.     History *h = NULL;
  214.     HTAnchor *a;
  215.     Doc *d;
  216.     if (HISTORY_TRACE)
  217. fprintf(stderr,"->ForwardDocn");
  218.     h = (History *)HTList_objectAt(context->history, pos);
  219.     if (!h) {
  220. if (HISTORY_TRACE)
  221.     fprintf(stderr,"BackDoc: no history to be found n");
  222. return;
  223.     }
  224.     if (h->state != HISTORY_VERIFIED) {
  225. if (HISTORY_TRACE)
  226.     fprintf(stderr,"BackDoc: selected item not yet confirmed loaded %dn",h->state);
  227. return;
  228.     }
  229.     /* from here we assume success */
  230.     context->history_pos = pos;
  231.     context->current_history = (History *)HTList_objectAt(context->history, context->history_pos);
  232.     a = h->anchor;
  233.     if (HISTORY_TRACE)
  234. fprintf(stderr,"BackDoc, pos = %d, a = %sn", context->history_pos, a->parent->physical);
  235.     
  236.     if ((d = (Doc *) a->parent->document)) {
  237. if (d->state >= DOC_LOADED) {
  238.     CurrentDoc = (Doc *) d;
  239.     CurrentDoc->tag = h->tag;
  240.     CurrentDoc->show_raw = FALSE;
  241.     NewBuffer(CurrentDoc);
  242.     DeltaHTMLPosition(context->current_history->y);
  243.     DisplayDoc(WinLeft, WinTop, WinWidth, WinHeight);
  244.     SetScrollBarHPosition(PixelIndent, buf_width);
  245.     SetScrollBarVPosition(context->current_history->y, buf_height);
  246.     DisplayScrollBar();
  247.     DisplayUrl();
  248. }
  249. else {
  250.     fprintf(stderr,"BackDoc: WEIRD, doc is reg'd but state is lown");
  251. }
  252.     } else {
  253. libLoadAnchor(a, NULL, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE);
  254.     }
  255.     if (HISTORY_TRACE)
  256. HistoryList();
  257. }
  258. void HistoryRecord(HTAnchor *a)
  259. {
  260.     History *h;
  261.     int i;
  262.     if (HISTORY_TRACE)
  263. #if defined PRINTF_HAS_PFORMAT
  264. fprintf(stderr, "->HistoryRecord %pn",a);
  265. #else
  266. fprintf(stderr, "->HistoryRecord %lxn",a);
  267. #endif /* PRINTF_HAS_PFORMAT */
  268.     /* we're putting a new doc into the history list. First mark the
  269.        rest of the history for death since this is a deleting history
  270.        mechanism */
  271.     i = context->history_pos - 1;
  272.     while(i >= 0) {
  273. if (HISTORY_TRACE)
  274.     fprintf(stderr, "HistoryRecord: in loop %dn",i);     
  275. h = (History *)HTList_objectAt(context->history, i);
  276. if (h && h->state == HISTORY_VERIFIED) {
  277.     h->state = HISTORY_DELETED;
  278.     if (HISTORY_TRACE)
  279. fprintf(stderr, "HistoryRecord: deleting %dn",i);     
  280. }
  281. i--;
  282.     }
  283.     /* then, create a new history record to be put onto the history */
  284.     h = NewHistory();
  285.     h->state = HISTORY_REGISTERED;
  286.     h->anchor = a;
  287.     if ((HTAnchor *)a->parent != a) {
  288. HTChildAnchor *c = (HTChildAnchor *) a;
  289. h->tag = c->tag;
  290. if (HISTORY_TRACE)
  291.     fprintf(stderr,"HistoryRecord: recording CHILS tag %sn", c->tag);
  292.     }    
  293.     if (!context->history)
  294. context->history = HTList_new();
  295.     HTList_addObject(context->history, h);
  296.     context->history_pos++;
  297.     context->current_history = (History *)HTList_objectAt(context->history, context->history_pos);
  298. }
  299. void HistoryDelete(HTAnchor *a)
  300. {
  301.     /* janet 21/07/95: not used:   Doc *d; */
  302.     History *h;
  303.     HTList *l = context->history;
  304.     if (HISTORY_TRACE)
  305. #if defined PRINTF_HAS_PFORMAT
  306. fprintf(stderr,"->HistoryDelete %pn",a);
  307. #else
  308. fprintf(stderr,"->HistoryDelete %lxn",a);
  309. #endif /* PRINTF_HAS_PFORMAT */
  310.     while ( (h = (History *)HTList_nextObject(l)) ) {
  311. if (a == h->anchor) {
  312.     h->state = HISTORY_DELETED;
  313.     if (HISTORY_TRACE)
  314. fprintf(stderr,"HistoryDelete %sn",a->parent->address);
  315. }
  316.     }
  317.     if (HISTORY_TRACE)
  318. HistoryList();
  319. }
  320. void HistoryVerify(HTAnchor *a)
  321. {
  322.     /* janet 21/07/95: not used:    Doc *d; */
  323.     History *h;
  324.     HTList *l;
  325.     int count = 0;
  326.     int i;
  327.     if (HISTORY_TRACE)
  328. #if defined PRINTF_HAS_PFORMAT
  329. fprintf(stderr, "->HistoryVerify %pn",a);
  330. #else
  331. fprintf(stderr, "->HistoryVerify %lxn",a);
  332. #endif /* PRINTF_HAS_PFORMAT */
  333.     /* the incoming anchor has been loaded and should be verified on
  334.        the history list. As it is verified, we should delete the rest
  335.        of the branch. The mark-for-death had to be on at a stage not
  336.        called if we were just moving around in history,
  337.        i.e. HistoryRecord */
  338.     i = 0;
  339.     l = context->history;
  340.     while ( (h = (History *)HTList_nextObject(l)) ) {
  341. if (h->state == HISTORY_DELETED) {
  342.     if (HISTORY_TRACE) {
  343. HTAnchor *a = h->anchor;
  344. fprintf(stderr, "*****libHistoryVerify: deleting %d %sn",i,a->parent->physical);
  345.     }
  346.     l = l->next; /* since we'll remove the object l is pointing to */
  347.     HTList_removeObjectAt(context->history, i);  /* dumps here if error on start_up */
  348. } else /* if we just deleted element, it moved ahead automatically */
  349.     i++;
  350.     }
  351.     l = context->history;
  352.     while ( (h = (History *)HTList_nextObject(l)) ) {
  353. if (h->anchor == a) {
  354.     context->history_pos = count;
  355.     context->current_history = (History *)HTList_objectAt(context->history, context->history_pos);
  356.     if (h->state == HISTORY_REGISTERED) {
  357. h->state = HISTORY_VERIFIED;
  358. if (CurrentDoc->title)
  359.     h->title = strdup(CurrentDoc->title);
  360. if (HISTORY_TRACE)
  361.     fprintf(stderr, "-->HistoryVerify: context->history_pos = %dn", context->history_pos);
  362.     }
  363.     else
  364. if (HISTORY_TRACE)
  365.     fprintf(stderr, "libHistoryVerify: check STATE n");
  366. }
  367. count++;
  368.     }
  369.     if (HISTORY_TRACE)
  370. HistoryList();
  371. }