Browser 151 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright 1997 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/***************************************************/
/* File   : Browser.c                              */
17
/*                                                 */
18
/* Purpose: Browser window services.               */
19
/*                                                 */
20
/* Author : A.D.Hodgkinson                         */
21 22
/*                                                 */
/* History: 15-Mar-97: Created from Windows.c.     */
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/***************************************************/

#include <stdlib.h>
#include <string.h>

#include "swis.h"
#include "flex.h"

#include "wimp.h"
#include "wimplib.h"
#include "event.h"

#include "toolbox.h"
#include "window.h"

#include "Dialler.h"
#include "NestWimp.h"

#include "svcprint.h"
#include "Global.h"
#include "FromROSLib.h"
44
#include "MiscDefs.h"
45 46
#include "Utils.h"

47
#include "CSIM.h"
48
#include "Fetch.h" /* (Which itself includes URLstat.h) */
49 50
#include "Frames.h"
#include "Forms.h"
51
#include "History.h"
52 53
#include "Images.h"
#include "Mouse.h"
54
#include "PrintStyle.h"
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#include "Redraw.h"
#include "Reformat.h"
#include "TokenUtils.h"
#include "Toolbars.h"

#include "Browser.h"

/* Static function prototypes */

static HStream         * browser_find_selectable_top_r (browser_data * b, reformat_cell * cell, HStream ** current, int y_origin, WimpGetWindowStateBlock * s);
static HStream         * browser_find_selectable_bot_r (browser_data * b, reformat_cell * cell, HStream ** current, int y_origin, WimpGetWindowStateBlock * s);
static int               browser_navigate_map          (browser_data * b, int key);

static _kernel_oserror * browser_redraw_border         (browser_data * b, HStream * token);
static HStream         * browser_get_pointer_token_r   (browser_data * b, reformat_cell * cell, WimpGetPointerInfoBlock * p, WimpGetWindowStateBlock * state, int * ox, int * oy);
static int               browser_top_line_r            (browser_data * b, reformat_cell * cell, reformat_cell ** ret_cell, WimpGetWindowStateBlock * s, int fully_visible);
static int               browser_bottom_line_r         (browser_data * b, reformat_cell * cell, reformat_cell ** ret_cell, WimpGetWindowStateBlock * s, int fully_visible);
static _kernel_oserror * browser_update_token_r        (browser_data * b, reformat_cell * cell, HStream * token, int first, int last, int chunk, int base_x, int base_y, int noback, HStream * nocontent);

74
static _kernel_oserror * browser_set_look_r            (browser_data * b, ObjectId source, int underline_links, int use_source_cols, int show_foreground, int show_background);
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
/* Local statics */

static ObjectId pointer_is_over = 0; /* Object that the pointer is over, if any */

/*************************************************/
/* browser_scroll_page_v()                       */
/*                                               */
/* Scrolls a page vertically by a given amount.  */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the page;             */
/*                                               */
/*             A WimpOpenWindowBlock pointer,    */
/*             holding the window's current      */
/*             details (e.g. visible area) or    */
/*             NULL if this is not known;        */
/*                                               */
/*             1 to scroll up, else down;        */
/*                                               */
/*             1 to page up/down, else 0;        */
/*                                               */
/*             1 to move one line, else 0;       */
/*                                               */
/*             An amount to scroll by, ignored   */
/*             unless the above two parameters   */
/*             are zero;                         */
/*                                               */
/*             Pointer to an int, in which 1 is  */
/*             written if the window didn't      */
/*             shift scroll position as it was   */
/*             at the limit of its work area,    */
/*             else 0 is written.                */
/*************************************************/

_kernel_oserror * browser_scroll_page_v(browser_data * b, WimpOpenWindowBlock * o, int dir, int page, int line, int amount, int * limit)
{
  int                       scrollby;
  WimpGetWindowStateBlock   open;
  _kernel_oserror         * e;

  /* Work out the WimpOpenWindowBlock if NULL was passed in */

  if (!o)
  {
    open.window_handle = b->window_handle;

    e = wimp_get_window_state(&open);
    if (e) return e;

    o = (WimpOpenWindowBlock *) &open;
  }

  if (limit)
  {
    /* If required, see if we're at the limit of the scroll position */

    BBox extent;
    int  scrollmax;

    e = window_get_extent(0, b->self_id, &extent);
    if (e) return e;

    scrollmax = !dir ? extent.ymin + (o->visible_area.ymax - o->visible_area.ymin) : 0;

    if (scrollmax == o->yscroll) *limit = 1;
    else                         *limit = 0;
  }

  /* Work out how much to scroll by */

146
  if      (page) scrollby = o->visible_area.ymax - o->visible_area.ymin - toolbars_url_height(b) - toolbars_button_height(b) - toolbars_status_height(b) - wimpt_dy();
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
  else if (line) scrollby = 42;
  else           scrollby = amount;

  /* If greater than zero, move the page */

  if (scrollby > 0)
  {
    if (!dir) scrollby = -scrollby;

    o->yscroll += scrollby;

    return wimp_open_window(o);
  }

  return NULL;
}

/*************************************************/
/* browser_scroll_page_h()                       */
/*                                               */
/* Scrolls a page horizontally a given amount.   */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the page;             */
/*                                               */
/*             A WimpOpenWindowBlock pointer,    */
/*             holding the window's current      */
/*             details (e.g. visible area) or    */
/*             NULL if this is not known;        */
/*                                               */
/*             1 to scroll left, else right;     */
/*                                               */
/*             1 to page left/right, else 0;     */
/*                                               */
/*             1 to move one line, else 0;       */
/*                                               */
/*             An amount to scroll by, ignored   */
/*             unless the above two parameters   */
/*             are zero;                         */
/*                                               */
/*             Pointer to an int, in which 1 is  */
/*             written if the window didn't      */
/*             shift scroll position as it was   */
/*             at the limit of its work area,    */
/*             else 0 is written.                */
/*************************************************/

_kernel_oserror * browser_scroll_page_h(browser_data * b, WimpOpenWindowBlock * o, int dir, int page, int line, int amount, int * limit)
{
  int                       scrollby;
  WimpGetWindowStateBlock   open;
  _kernel_oserror         * e;

  /* Work out the WimpOpenWindowBlock if NULL was passed in */

  if (!o)
  {
    open.window_handle = b->window_handle;

    e = wimp_get_window_state(&open);
    if (e) return e;

    o = (WimpOpenWindowBlock *) &open;
  }

  if (limit)
  {
    /* If required, see if we're at the limit of the scroll position */

    BBox extent;
    int  scrollmax;

    e = window_get_extent(0, b->self_id, &extent);
    if (e) return e;

    scrollmax = !dir ? extent.xmin - (o->visible_area.xmax - o->visible_area.xmin) : 0;

    if (scrollmax == o->xscroll) *limit = 1;
    else                         *limit = 0;
  }

  /* Work out how much to scroll by */

  if      (page) scrollby = o->visible_area.ymax - o->visible_area.ymin;
  else if (line) scrollby = 42;
  else           scrollby = amount;

  /* If greater than zero, move the page */

  if (scrollby > 0)
  {
    if (dir) scrollby = -scrollby;

    o->xscroll += scrollby;

    return wimp_open_window(o);
  }

  return NULL;
}

/*************************************************/
/* browser_scroll_page_by_key()                  */
/*                                               */
/* Scrolls a page according to a given key code. */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the page;             */
/*                                               */
/*             Key code from the Wimp (to define */
/*             left / right, page movement or    */
/*             line movement, etc.);             */
/*                                               */
/*             Pointer to an int, in which 1 is  */
/*             written if the window didn't      */
/*             shift scroll position as it was   */
/*             at the limit of its work area,    */
/*             else 0 is written (this only      */
265
/*             applies for vertical scrolling -  */
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 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
/*             the written value will always be  */
/*             0 if moving horizontally).        */
/*************************************************/

_kernel_oserror * browser_scroll_page_by_key(browser_data * b, int key, int * limit)
{
  int                       page = 0, line = 0, dir = 0;
  WimpGetWindowStateBlock   s;
  _kernel_oserror         * e;

  s.window_handle = b->window_handle;
  e = wimp_get_window_state(&s);
  if (e) return e;

  if (
       key == akbd_UpK                         ||
       key == akbd_PageUpK                     ||
       key == akbd_LeftK                       ||
       key == akbd_HomeK                       ||
       key == akbd_UpK   + akbd_Ctl            ||
       key == akbd_UpK   + akbd_Ctl + akbd_Sh  ||
       key == akbd_LeftK + akbd_Ctl            ||
       key == akbd_LeftK + akbd_Ctl + akbd_Sh
     )
     dir = 1;

  if (
       key == akbd_PageUpK                     ||
       key == akbd_PageDownK
     )
     page = 1;

  if (
       key == akbd_UpK                         ||
       key == akbd_DownK                       ||
       key == akbd_LeftK                       ||
       key == akbd_RightK                      ||
       key == akbd_UpK    + akbd_Ctl + akbd_Sh ||
       key == akbd_DownK  + akbd_Ctl + akbd_Sh ||
       key == akbd_LeftK  + akbd_Ctl + akbd_Sh ||
       key == akbd_RightK + akbd_Ctl + akbd_Sh
     )
     line = 1;

  if (
       key == akbd_LeftK                       ||
       key == akbd_RightK                      ||
       key == akbd_LeftK  + akbd_Ctl           ||
       key == akbd_LeftK  + akbd_Ctl + akbd_Sh ||
       key == akbd_RightK + akbd_Ctl           ||
       key == akbd_RightK + akbd_Ctl + akbd_Sh
     )
  {
    /* For left/right key presses, want to make sure as */
    /* the caller that the input focus is not in a      */
    /* writable icon, or if it is, the effect of having */
    /* the page scroll as the caret tries to move has   */
    /* been taken into account (e.g. the caret is known */
    /* to be at the start/end of the writable's text).  */

    if (limit) *limit = 0;

    return browser_scroll_page_h(b,
                                 (WimpOpenWindowBlock *) &s,
                                 dir,
                                 page,
                                 line,
                                 (!(page + line) ? 0x1000000 : 0),
                                 NULL);
  }
  else
  {
    e = browser_scroll_page_v(b,
                              (WimpOpenWindowBlock *) &s,
                              dir,
                              page,
                              line,
                              (!(page + line) ? 0x1000000 : 0),
                              limit);
    if (e) return e;

    /* For Home, make sure the page is scrolled to the far left */

    if (!page && !line && dir) return browser_scroll_page_h(b,
                                                            (WimpOpenWindowBlock *) &s,
                                                            1,
                                                            0,
                                                            0,
                                                            0x1000000,
                                                            NULL);

// This seems generally undesirable...
//
//    /* For End, make sure the page is scrolled to the far right */
//
//    if (!page && !line && !dir) return browser_scroll_page_h(b,
//                                                             (WimpOpenWindowBlock *) &s,
//                                                             0,
//                                                             0,
//                                                             0,
//                                                             0x1000000,
//                                                             NULL);

  }

  return NULL;
}

/*************************************************/
/* browser_find_first_selectable()               */
/*                                               */
/* Examines the visible area of a given browser  */
/* window to see if a selectable token is        */
/* present in it, and returns the address of the */
/* token if so. The token returned may not be    */
/* fully visible - the caller must use           */
/* browser_check_visible on the returned token   */
/* if the token must be fully visible.           */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the window;           */
/*                                               */
/*             A WimpGetWindowStateBlock pointer */
/*             holding details of the window     */
/*             (if NULL because the caller       */
/*             doesn't have this information to  */
/*             hand, the function will work it   */
/*             out);                             */
/*                                               */
/*             Direction to search in; 1 for     */
/*             bottom right to top left, 0 for   */
/*             top left to bottom right.         */
/*                                               */
/* Returns:    Pointer to the token to select,   */
/*             or NULL if none are visible.      */
/*************************************************/

HStream * browser_find_first_selectable(browser_data * b, WimpGetWindowStateBlock * s, int dir)
{
  HStream                 * token_null = NULL;
  WimpGetWindowStateBlock   state;

  if (!b) return NULL;

  /* Get the window state if it wasn't given */

  if (!s)
  {
    state.window_handle = b->window_handle;

    if (wimp_get_window_state(&state)) return NULL;

    s = &state;
  }

  /* Find the selectable, marking that nothing is to be */
  /* skipped (*(&token_null) = NULL).                   */

  if (!dir) return browser_find_selectable_top_r(b, b->cell, &token_null, 0, s);
  else      return browser_find_selectable_bot_r(b, b->cell, &token_null, 0, s);
}

/*************************************************/
/* browser_find_another_selectable()             */
/*                                               */
/* Takes a given selected token, and finds the   */
/* previous or next selectable, optionally       */
/* constraining the search to moving to a new    */
/* line, rather than allowing to stay on the     */
/* same one.                                     */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the window;           */
/*                                               */
/*             Pointer to the currently selected */
/*             token;                            */
/*                                               */
/*             Direction to search in; 1 for     */
/*             bottom right to top left, 0 for   */
/*             top left to bottom right;         */
/*                                               */
/*             Constraint (1 to allow tokens on  */
/*             the same line, else must go to a  */
/*             new line).                        */
/*                                               */
/* Returns:    Pointer to the new token that     */
/*             should be selected, or NULL for   */
/*             none.                             */
/*************************************************/

HStream * browser_find_another_selectable(browser_data * b, HStream * current, int dir, int constrain)
{
  WimpGetWindowStateBlock   state;
  HStream                 * current_rec = current;

  /* Get the browser window's state */

  if (!b) return NULL;

  state.window_handle = b->window_handle;

  if (wimp_get_window_state(&state)) return NULL;

  /* Ensure that the selected token in current_rec is */
  /* at the top of the tokens representing the same   */
  /* selectable.                                      */

  tokenutils_anchor_range(b, current_rec, &current_rec, NULL);

  /* Call the relevant function to find the item */

  if (!dir) return browser_find_selectable_top_r(b, b->cell, &current_rec, 0, &state);
  else      return browser_find_selectable_bot_r(b, b->cell, &current_rec, 0, &state);
}

/*************************************************/
/* browser_find_selectable_top_r()               */
/*                                               */
/* Recursive back-end to the 'from top-left'     */
/* call to browser_find_another_selectable and   */
/* browser_find_first_selectable.                */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the window;           */
/*                                               */
/*             Pointer to a reformat_cell struct */
/*             holding the lines to scan;        */
/*                                               */
/*             Pointer to a pointer to the       */
/*             currently selected token (this    */
/*             will be written to) - if there is */
/*             no such token, this should point  */
/*             to a word holding NULL;           */
/*                                               */
/*             y origin of that cell, in OS      */
/*             units from the top left of the    */
/*             whole page;                       */
/*                                               */
/*             A WimpGetWindowStateBlock pointer */
/*             holding details of the window;    */
/*                                               */
/* Returns:    As browser_find_first_selectable. */
/*************************************************/

HStream * browser_find_selectable_top_r(browser_data * b, reformat_cell * cell, HStream ** current, int y_origin, WimpGetWindowStateBlock * s)
{
  HStream * t    = NULL;
  HStream * last = NULL;
  int       exit;
515
  int       ytop, ybot, htop, hbot;
516 517 518 519 520 521 522 523 524 525
  int       line, chunk, chunkmax;

  if (!cell || !cell->nlines || !cell->ldata || !cell->cdata) return NULL;

  #ifdef TRACE
    if (tl & (1u<<22)) Printf("browser_find_selectable_top_r: Proceeding for %p, cell %p\n", b, cell);
  #endif

  /* Work out where the visible page region starts and ends */

526
  if (!controls.swap_bars)
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
  {
    htop = toolbars_button_height(b) + toolbars_url_height(b);
    hbot = toolbars_status_height(b);
  }
  else
  {
    htop = toolbars_status_height(b);
    hbot = toolbars_button_height(b) + toolbars_url_height(b);
  }

  if (htop) htop += wimpt_dy();
  if (hbot) hbot += wimpt_dy();

  ytop = s->yscroll - htop;
  ybot = s->yscroll - (s->visible_area.ymax - s->visible_area.ymin) + hbot;
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571

  #ifdef TRACE
    if (tl & (1u<<22)) Printf("browser_find_selectable_top_r: ytop, ybot: -%d, -%d\n",-ytop,-ybot);
  #endif

  /* Go through the cell's chunks, getting tokens for as long */
  /* as the chunk holding lines are in the visible area.      */

  line = 0;
  exit = 0;

  /* Find the line visible at the top of the window. This can be slow if */
  /* there are a lot of lines, so use a Cunning Plan - divide the window */
  /* extent (take the y coordinate of the last line) by the number of    */
  /* lines to get the average line height, use this and the ytop coord   */
  /* to have a good guess at the line, then move a short distance up or  */
  /* down to get the actual correct line.                                */
  /*                                                                     */
  /* Of course, this only works for the main line list. It could be      */
  /* adjusted for tables, but there's no time to do it right now...      */
  /* Tables are rarely large enough to need it anyway.                   */

  if (cell != b->cell)
  {
    while (line < cell->nlines && y_origin + cell->ldata[line].y > ytop) line ++;
  }
  else
  {
    int extent  = cell->ldata[cell->nlines - 1].y;
    int average = extent / cell->nlines;
572 573 574 575
    int startat;

    if (average) startat = ytop / average;
    else         startat =0;
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665

    if (startat < 0)             startat = 0;
    if (startat >= cell->nlines) startat = cell->nlines - 1;

    if (y_origin + cell->ldata[startat].y > ytop)
    {
      int lastline = -1;

      line = startat;
      while (line < cell->nlines && y_origin + cell->ldata[line].y > ytop) lastline = line, line ++;

      if (lastline >= 0) line = lastline;
      else               line = startat;
    }
    else
    {
      line = startat;
      while (line >= 0 && y_origin + cell->ldata[line].y <= ytop) line --;

      if (line < 0) line = 0;
    }
  }

  /* Proceed until the line visible at the bottom */

  while (line < cell->nlines && y_origin + cell->ldata[line].y + cell->ldata[line].h > ybot && !exit)
  {
    #ifdef TRACE
      if (tl & (1u<<22)) Printf("browser_find_selectable_top_r: Line %d\n",line);
    #endif

    chunk    = cell->ldata[line].chunks;
    chunkmax = cell->ldata[line].n + chunk;

    while (chunk < chunkmax && !exit)
    {
      /* Find the token represented by this chunk */

      t = cell->cdata[chunk].t;
      if (!t) break;

      #ifdef TRACE
        if (tl & (1u<<22))
        {
          Printf("browser_find_selectable_top_r: Chunk %d of %d, token %p\n", chunk, chunkmax, t);
          if (t == last) Printf("browser_find_selectable_top_r: t = last, so won't deal with this chunk\n");
        }
      #endif

      /* Several chunks can represent the same token - don't want to */
      /* deal with it multiple times, though.                        */

      if (t != last)
      {
        last = t;

        /* Exit successfully (exit = 1, t != NULL) if the token can be */
        /* selected and is visible.                                    */

        if (!*current && CanBeSelected(t) && browser_check_visible(b, s, t))
        {
          #ifdef TRACE
            if (tl & (1u<<22)) Printf("browser_find_selectable_top_r: ** Found %p **\n", t);
          #endif

          exit = 1;
          break;
        }

        /* If we're supposed to select the token after *current - i.e. *current */
        /* is still not NULL - then find the first token making up that same    */
        /* link. If this matches *current, we've found that link - so, clear    */
        /* *current and skip past the link.                                     */

        if (*current && CanBeSelected(t))
        {
          HStream * top, * bot;

          tokenutils_anchor_range(b, t, &top, &bot);

          if (top == *current)
          {
            *current = NULL;

            tokenutils_find_token(b, cell, bot, NULL, NULL, &line, &chunk);
          }
        }

        /* Deal with tables */

666
        if (t->tagno == TAG_TABLE)
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
        {
          table_stream   * table     = (table_stream *) t;
          table_row      * row       = table->List;
          table_headdata * head      = NULL;
          HStream        * found     = NULL;
          reformat_cell  * c         = NULL;
          reformat_cell  * cellarray = table->cells;
          int              cellmax   = table->ColSpan * table->RowSpan;
          int              cellindex;
          int              xorg, yorg;

          #ifdef TRACE
            if (tl & (1u<<22)) Printf("browser_find_selectable_top_r: Dealing with table\n");
          #endif

          /* Proceed if the cell array can be found */

          if (cellarray)
          {
            while (row && !found)
            {
              head = row->List;

              while (
                      head                           &&
                      !found                         &&
                      head->RowOffs < table->RowSpan &&
                      head->ColOffs < table->ColSpan
                    )
              {
                switch (head->Tag)
                {
                  case TagTableData:
                  case TagTableHead:
                  {
                    /* Find the reformat_cell structure for this table cell */

                    cellindex = head->RowOffs * table->ColSpan + head->ColOffs;

                    if (cellindex < cellmax)
                    {
                      c = &cellarray[cellindex];

                      #ifdef TRACE
                        if (tl & (1u<<22)) Printf("browser_find_selectable_top_r: Cell index %d, cell %p\n",cellindex,cellarray);
                      #endif

714
                      convert_pair_to_os(c->x, c->y, &xorg, &yorg);
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824

                      /* Recursive call to look at cell contents */

                      found = browser_find_selectable_top_r(b,
                                                            c,
                                                            current,
                                                            cell->ldata[line].y + cell->ldata[line].h + y_origin + yorg,
                                                            s);

                      #ifdef TRACE
                        if (tl & (1u<<22)) Printf("browser_find_selectable_top_r: Cell index %d, cell %p - Found: %p\n",cellindex, cellarray, found);
                      #endif
                    }
                  }

                /* Closure of 'switch (head->Tag)' */
                }

                head = head->Next;

              /* Closure of 'while (head && ...)' */
              }

              row = row->Next;

            /* Closure of 'while (row && ...)' */
            }

          /* Closure of 'if (cellarray)' */
          }

          if (found)
          {
            t    = found;
            exit = 1;

            #ifdef TRACE
              if (tl & (1u<<22)) Printf("browser_find_selectable_top_r: ** Found %p in table, exitting **\n",t);
            #endif

            break;
          }
          #ifdef TRACE

            else if (tl & (1u<<22)) Printf("browser_find_selectable_top_r: Nothing found in table\n");

          #endif
        }

      /* Closure of 'if (t != last)' */
      }

      chunk ++;

    /* Closure of 'while' loop scanning chunks */
    }

    line ++;

  /* Closure of 'while' loop scanning the lines */
  }

  /* If exit wasn't forced, we scanned the whole of the visible line list */
  /* for this cell and didn't find a visible, selectable token.           */

  if (!exit) t = NULL;

  /* Return the found value, be it NULL or a valid selectable token */

  #ifdef TRACE
    if (tl & (1u<<22)) Printf("browser_find_selectable_top_r: -- Returning %p --\n", t);
  #endif

  return t;
}

/*************************************************/
/* browser_find_selectable_bot_r()               */
/*                                               */
/* Recursive back-end to the 'from bottom-right' */
/* call to browser_find_another_selectable and   */
/* browser_find_first_selectable.                */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the window;           */
/*                                               */
/*             Pointer to a reformat_cell struct */
/*             holding the lines to scan;        */
/*                                               */
/*             Pointer to a pointer to the       */
/*             currently selected token (this    */
/*             will be written to) - if there is */
/*             no such token, this should point  */
/*             to a word holding NULL;           */
/*                                               */
/*             y origin of that cell, in OS      */
/*             units from the top left of the    */
/*             whole page;                       */
/*                                               */
/*             A WimpGetWindowStateBlock pointer */
/*             holding details of the window.    */
/*                                               */
/* Returns:    As browser_find_first_selectable. */
/*************************************************/

HStream * browser_find_selectable_bot_r(browser_data * b, reformat_cell * cell, HStream ** current, int y_origin, WimpGetWindowStateBlock * s)
{
  HStream * t    = NULL;
  HStream * last = NULL;
  int       exit;
825
  int       ytop, ybot, htop, hbot;
826 827 828 829 830 831 832 833 834 835
  int       line, chunk, chunkmin;

  if (!cell || !cell->nlines || !cell->ldata || !cell->cdata) return NULL;

  #ifdef TRACE
    if (tl & (1u<<22)) Printf("browser_find_selectable_bot_r: Proceeding for %p, cell %p\n", b, cell);
  #endif

  /* Work out where the visible page region starts and ends */

836
  if (!controls.swap_bars)
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
  {
    htop = toolbars_button_height(b) + toolbars_url_height(b);
    hbot = toolbars_status_height(b);
  }
  else
  {
    htop = toolbars_status_height(b);
    hbot = toolbars_button_height(b) + toolbars_url_height(b);
  }

  if (htop) htop += wimpt_dy();
  if (hbot) hbot += wimpt_dy();

  ytop = s->yscroll - htop;
  ybot = s->yscroll - (s->visible_area.ymax - s->visible_area.ymin) + hbot;
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973

  #ifdef TRACE
    if (tl & (1u<<22)) Printf("browser_find_selectable_bot_r: ytop, ybot: -%d, -%d\n",-ytop,-ybot);
  #endif

  /* Go through the cell's chunks, getting tokens for as long */
  /* as the chunk holding lines are in the visible area.      */

  line = cell->nlines - 1;
  exit = 0;

  /* Find the line visible at the top of the window. This can be slow if */
  /* there are a lot of lines, so use a Cunning Plan - divide the window */
  /* extent (take the y coordinate of the last line) by the number of    */
  /* lines to get the average line height, use this and the ybot coord   */
  /* to have a good guess at the line, then move a short distance up or  */
  /* down to get the actual correct line.                                */
  /*                                                                     */
  /* Of course, this only works for the main line list. It could be      */
  /* adjusted for tables, but there's no time to do it right now...      */
  /* Tables are rarely large enough to need it anyway.                   */

  if (cell != b->cell)
  {
    while (line >= 0 && y_origin + cell->ldata[line].y + cell->ldata[line].h < ybot) line --;
  }
  else
  {
    int extent  = cell->ldata[cell->nlines - 1].y;
    int average = extent / cell->nlines;
    int startat = ybot / average + 1;

    if (startat < 0)             startat = 0;
    if (startat >= cell->nlines) startat = cell->nlines - 1;

    if (y_origin + cell->ldata[startat].y + cell->ldata[startat].h < ybot)
    {
      int lastline = -1;

      line = startat;
      while (line >= 0 && y_origin + cell->ldata[line].y + cell->ldata[line].h < ybot) lastline = line, line --;

      if (lastline >= 0) line = lastline;
      else               line = startat;
    }
    else
    {
      line = startat;
      while (line < cell->nlines && y_origin + cell->ldata[line].y + cell->ldata[line].h >= ybot) line ++;

      if (line >= cell->nlines) line = cell->nlines - 1;
    }
  }

  /* Proceed until the line visible at the top */

  while (line >= 0 && y_origin + cell->ldata[line].y < ytop && !exit)
  {
    #ifdef TRACE
      if (tl & (1u<<22)) Printf("browser_find_selectable_bot_r: Line %d\n",line);
    #endif

    chunkmin = cell->ldata[line].chunks;
    chunk    = cell->ldata[line].n + chunkmin - 1;

    while (chunk >= chunkmin && !exit)
    {
      /* Find the token represented by this chunk */

      t = cell->cdata[chunk].t;
      if (!t) break;

      #ifdef TRACE
        if (tl & (1u<<22))
        {
          Printf("browser_find_selectable_bot_r: Chunk %d of %d minimum, token %p\n", chunk, chunkmin, t);
          if (t == last) Printf("browser_find_selectable_bot_r: t = last, so won't deal with this chunk\n");
        }
      #endif

      /* Several chunks can represent the same token - don't want to */
      /* deal with it multiple times, though.                        */

      if (t != last)
      {
        last = t;

        /* Exit successfully (exit = 1, t != NULL) if the token can be */
        /* selected and is visible.                                    */

        if (!*current && CanBeSelected(t) && browser_check_visible(b, s, t))
        {
          #ifdef TRACE
            if (tl & (1u<<22)) Printf("browser_find_selectable_bot_r: ** Found %p **\n", t);
          #endif

          exit = 1;
          break;
        }

        /* If we're supposed to select the token after *current - i.e. *current */
        /* is still not NULL - then, having made sure that the current token    */
        /* 't' represents an anchor, find the first token making up that same   */
        /* link. If this matches *current, we've found that link - so, clear    */
        /* *current and skip over the link.                                     */

        if (*current && CanBeSelected(t))
        {
          HStream * top, * bot;

          tokenutils_anchor_range(b, t, &top, &bot);

          if (top == *current)
          {
            *current = NULL;

            tokenutils_find_token(b, cell, top, &line, &chunk, NULL, NULL);
          }
        }

        /* Deal with tables */

974
        if (t->tagno == TAG_TABLE)
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
        {
          table_stream   * table     = (table_stream *) t;
          table_row      * row       = NULL;
          table_headdata * head      = NULL;
          HStream        * found     = NULL;
          reformat_cell  * c         = NULL;
          reformat_cell  * cellarray = table->cells;
          int              cellmax   = table->ColSpan * table->RowSpan;
          int              cellindex;
          int              xorg, yorg;

          #ifdef TRACE
            if (tl & (1u<<22)) Printf("browser_find_selectable_bot_r: Dealing with table\n");
          #endif

          /* Proceed if the cell array can be found */

          if (cellarray)
          {
            /* Start on the last row and work backwards */

            row = table->List;

            while (row && row->Next) row = row->Next;

            while (row && !found)
            {
              head = row->List;

              /* Start on the last cell and work backwards */

              while (head && head->Next) head = head->Next;

              while (
                      head                           &&
                      !found                         &&
                      head->RowOffs < table->RowSpan &&
                      head->ColOffs < table->ColSpan
                    )
              {
                switch (head->Tag)
                {
                  case TagTableData:
                  case TagTableHead:
                  {
                    /* Find the reformat_cell structure for this table cell */

                    cellindex = head->RowOffs * table->ColSpan + head->ColOffs;

                    if (cellindex < cellmax)
                    {
                      c = &cellarray[cellindex];

                      #ifdef TRACE
                        if (tl & (1u<<22)) Printf("browser_find_selectable_bot_r: Cell index %d, cell %p\n",cellindex,cellarray);
                      #endif

1032
                      convert_pair_to_os(c->x, c->y, &xorg, &yorg);
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117

                      /* Recursive call to look at cell contents */

                      found = browser_find_selectable_bot_r(b,
                                                            c,
                                                            current,
                                                            cell->ldata[line].y + cell->ldata[line].h + y_origin + yorg,
                                                            s);

                      #ifdef TRACE
                        if (tl & (1u<<22)) Printf("browser_find_selectable_bot_r: Cell index %d, cell %p - Found: %p\n",cellindex, cellarray, found);
                      #endif
                    }
                  }

                /* Closure of 'switch (head->Tag)' */
                }

                head = head->Prev;

              /* Closure of 'while (head && ...)' */
              }

              row = row->Prev;

            /* Closure of 'while (row && ...)' */
            }

          /* Closure of 'if (cellarray)' */
          }

          if (found)
          {
            t    = found;
            exit = 1;

            #ifdef TRACE
              if (tl & (1u<<22)) Printf("browser_find_selectable_bot_r: ** Found %p in table, exitting **\n",t);
            #endif

            break;
          }
          #ifdef TRACE

            else if (tl & (1u<<22)) Printf("browser_find_selectable_bot_r: Nothing found in table\n");

          #endif
        }

      /* Closure of 'if (t != last)' */
      }

      chunk --;

    /* Closure of 'while' loop scanning chunks */
    }

    line --;

  /* Closure of 'while' loop scanning the lines */
  }

  /* If exit wasn't forced, we scanned the whole of the visible line list */
  /* for this cell and didn't find a visible, selectable token.           */

  if (!exit) t = NULL;

  /* Return the found value, be it NULL or a valid selectable token */

  #ifdef TRACE
    if (tl & (1u<<22)) Printf("browser_find_selectable_bot_r: -- Returning %p --\n", t);
  #endif

  return t;
}

/*************************************************/
/* browser_move_selection()                      */
/*                                               */
/* Moves the selected item up or down (to a      */
/* previous link, picture or forms item, or to a */
/* next item).                                   */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the selected item;    */
1118
/*                                               */
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
/*             Key press from a keyboard handler */
/*             (used to determine the direction  */
/*             and distance of motion).          */
/*                                               */
/* Returns:    1 if the keypress was used for    */
/*             something, else 0 (e.g. there are */
/*             no more objects to select).       */
/*************************************************/

int browser_move_selection(browser_data * b, int key)
{
  int                       page = 0, line = 0, dir = 0, horiz = 0;
  WimpGetWindowStateBlock   s;
  HStream                 * new            = NULL;
  HStream                 * first_selected = NULL;
  HStream                 * last_selected  = NULL;
1135
  browser_data            * ancestor       = utils_ancestor(b);
1136 1137 1138 1139 1140
  browser_data            * owner;

  owner             = ancestor->selected_owner;
  if (!owner) owner = b;

1141
  if (!choices.keyboard_ctrl) return 0;
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279

  if (ancestor->in_image_map || b->in_image_map)
  {
    /* If browser_navigate_map returns a non-zero value, */
    /* drop the keypress through.                        */

    key = browser_navigate_map(b, key);
    if (!key) return 1;
  }

  s.window_handle = b->window_handle;
  if (wimp_get_window_state(&s)) return 0;

  /* Ctrl+Shift+Arrows nudges the page scroll position, */
  /* it doesn't move any selections.                    */

  if (
       key == akbd_UpK    + akbd_Ctl + akbd_Sh  ||
       key == akbd_DownK  + akbd_Ctl + akbd_Sh  ||
       key == akbd_LeftK  + akbd_Ctl + akbd_Sh  ||
       key == akbd_RightK + akbd_Ctl + akbd_Sh
     )
     return 0;

  /* Otherwise, work out which direction to move in, is this line or */
  /* page movement, etc.                                             */

  if (
       key == akbd_UpK               ||
       key == akbd_PageUpK           ||
       key == akbd_LeftK             ||
       key == akbd_HomeK             ||
       key == akbd_UpK   + akbd_Ctl  ||
       key == akbd_LeftK + akbd_Ctl
     )
     dir = 1;

  if (
       key == akbd_PageUpK           ||
       key == akbd_PageDownK
     )
     page = 1;

  if (
       key == akbd_UpK               ||
       key == akbd_DownK             ||
       key == akbd_LeftK             ||
       key == akbd_RightK
     )
     line = 1;

  if (
       key == akbd_LeftK             ||
       key == akbd_RightK            ||
       key == akbd_LeftK  + akbd_Ctl ||
       key == akbd_RightK + akbd_Ctl
     )
     horiz = 1;

  /* If there's no selected token, or there is but it's not visible, */
  /* then reselect as appropriate from the tokens (if any) currently */
  /* visible on the page.                                            */

  if (ancestor->selected) tokenutils_anchor_range(owner,
                                                  ancestor->selected,

                                                  &first_selected,
                                                  &last_selected);
  else first_selected = last_selected = NULL;

  if (
       line &&
       (
         !ancestor->selected ||
         (
           ancestor->selected                                &&
           !browser_check_visible(owner, &s, first_selected) &&
           !browser_check_visible(owner, &s, last_selected)
         )
       )
     )
  {
    ancestor->selected       = NULL; /* No redraw problems as the conditions above ensure selected token is not in visible area now */
    ancestor->selected_owner = NULL;
    new                      = browser_find_first_selectable(owner, &s, dir);
  }

  /* Alternatively, if there's a selected token, move up or down from it */

  else if (line && ancestor->selected)
  {
    new = browser_find_another_selectable(owner, ancestor->selected, dir, horiz);

    /* If up/down is used but the next item is not visible on screen so */
    /* the page would scroll, want to first ensure that everything on   */
    /* the current line is selected - i.e. try moving horizontally.     */

    if (
         new                                   &&
         !horiz                                &&
         !browser_check_visible(owner, &s, new)
       )
       new = browser_find_another_selectable(owner, ancestor->selected, dir, 1);
  }

  if (new)
  {
//    if (
//         tokenutils_within_distance(owner,
//                                    new,
//                                    ancestor->selected,
//                                    s.visible_area.ymax -
//                                    s.visible_area.ymin -
//                                    toolbars_status_height(b) -
//                                    toolbars_url_height(b) -
//                                    toolbars_button_height(b))
//       )

    if (browser_check_visible(owner, &s, new))
    {
      /* If there are any forms menus open, close them */

      if (menusrc == Menu_Form) form_abandon_menu();

      browser_ensure_visible(owner, &s, new);
      browser_select_token(owner, new, 0);

      /* Update the status bar */

      toolbars_update_status(owner, Toolbars_Status_LinkTo);

      /* Move inside writable elements */

      if (
           form_token_cursor_editable(owner, new)
         )
         form_click_field(owner,
                          new,
1280
                          ((key == akbd_DownK || key == akbd_RightK) ? 0 : 2),
Kevin Bracey's avatar
Kevin Bracey committed
1281
                          0, 0);
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316

      /* Turn the pointer off, and reset the check to see if */
      /* the user has moved it manually.                     */

      mouse_pointer_off();
      mouse_force_unused();

      return 1;
    }
    else
    {
      /* This is so that if there's another selectable, but it's off screen, */
      /* the user doesn't suddenly start scrolling left / right, only up or  */
      /* down to reach the next selectable.                                  */

      if (horiz) return 1;
    }
  }

  return 0;
}

/*************************************************/
/* browser_navigate_map()                        */
/*                                               */
/* Moves around an image map by keyboard control */
/* with the machine single tasking during a key  */
/* autorepeat.                                   */
/*                                               */
/* To exit this 'mode', drop off the image map   */
/* or follow a link with Return.                 */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the browser the image */
/*             map lies in;                      */
1317
/*                                               */
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
/*             Wimp keycode used to move in it.  */
/*                                               */
/* Returns:    0 if the pointer is still in the  */
/*             map, else a Wimp key code saying  */
/*             which way it fell off.            */
/*************************************************/

static int browser_navigate_map(browser_data * b, int key)
{
  HStream                 * tp;
  int                       map_x  = -1;
  int                       map_y  = -1;
  WimpGetPointerInfoBlock   p;

  if (!wimp_get_pointer_info(&p))
  {
    tp = browser_get_pointer_token(b, &p, NULL, NULL);

1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
    if (
         tp                     &&
         redraw_selected(b, tp) &&
         (
           (
             (tp->style & IMG) &&
             (tp->type  & TYPE_ISMAP)
           )
           ||
           (
             tp->tagno         == TAG_INPUT &&
             HtmlINPUTtype(tp) == inputtype_IMAGE
           )
1349 1350 1351 1352
           ||
           (
             tp->type & TYPE_ISCLIENTMAP
           )
1353 1354
         )
       )
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
    {
      int       last_move = 4;
      int       last_key, start_key;
      int       time_delay, time_now;
      int       repeat_delay, repeat_rate;
      HStream * current;

      _swix(OS_Byte,
            _INR(0,1) | _OUT(1),

            121, /* Keyboard scan */
            0,   /* Scan all keys */

            &start_key); /* Record the key(s) being pressed (not the Wimp keycode) */

      mouse_watch_pointer_control(0);
      mouse_pointer_on();
      b->pointer_over = NULL;
      browser_pointer_check(0, NULL,NULL, b);

      /* Get the keyboard repeat rate and delay */

      _swix(OS_Byte,
            _INR(0, 2) | _OUTR(1, 2),

            196, /* Read auto-repeat delay */
            0,
            255,

            &repeat_delay,
            &repeat_rate);

      /* Read the current time into time_delay and set it negative, */
      /* to flag that the repeat delay part of autorepeat is active */

      _swix(OS_ReadMonotonicTime, _OUT(0), &time_delay);
      time_delay = -time_delay + 1; /* +1 as a '>' is used in the comparisson later, not '>='. */

      do
      {
        /* Move according to key used */

        if (key == akbd_UpK)    p.y += last_move;
        if (key == akbd_DownK)  p.y -= last_move;
        if (key == akbd_LeftK)  p.x -= last_move;
        if (key == akbd_RightK) p.x += last_move;

        if (key == akbd_Sh + akbd_UpK)    p.y += last_move * 3;
        if (key == akbd_Sh + akbd_DownK)  p.y -= last_move * 3;
        if (key == akbd_Sh + akbd_LeftK)  p.x -= last_move * 3;
        if (key == akbd_Sh + akbd_RightK) p.x += last_move * 3;

        map_x = p.x, map_y = p.y;

        mouse_to(p.x, p.y, 1);

        /* Loop round waiting for the keyboard repeat delay or rate */

        do
        {
          _swix(OS_Byte,
                _INR(0,1) | _OUT(1),

                121, /* Keyboard scan */
                0,   /* Scan all keys */

                &last_key);

          _swix(OS_ReadMonotonicTime, _OUT(0), &time_now);

          /* If time_delay is negative, wait for the repeat delay */

          if (time_delay < 0)
          {
            if (time_now + time_delay > repeat_delay) time_delay = time_now;
          }

          /* Otherwise, wait for the repeat rate */

          else
          {
            if (time_now - time_delay > repeat_rate) time_delay = time_now, last_move += 1;
          }
        }
        while (time_now != time_delay && last_key != 255 && last_key == start_key);

        /* Make sure that next time round the loop, the exit condition above doesn't */
        /* immediately activate, giving about 1 centisecond of very fast repeats...  */

        time_delay = time_now - 1; /* Hence the use of '>' rather than '>=' above */

        _swix(OS_Byte, _INR(0,1), 21, 0); /* Flush keyboard buffer */

        current = browser_get_pointer_token(b, &p, NULL, NULL);

        /* Keep going whilst we're over the same token and keys are being pressed */
      }
      while (tp == current && last_key != 255 && last_key == start_key);

      /* If we're on the same token, exit, claiming the key press. */
      /* Else, allow the press to drop through (so you'll go on to */
      /* select the next/previous token if running off the edge of */
      /* the image).                                               */

      if (tp == current) return 0;

      /* Otherwise, reenable pointer watching etc. and allow the */
      /* key press to drop to through.                           */
    }

    /* (This code also executes if the pointer isn't */
    /* over an image map any more - e.g. the user    */
    /* moved the mouse away).                        */

    mouse_set_pointer_shape(Mouse_Shape_Normal);
    debounce_keypress();

    /* Clear the flag saying an image map is selected */
    /* and sort out mouse pointer issues              */

    {
1476
      browser_data * ancestor = utils_ancestor(b);
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520

      mouse_pointer_off();
      b->in_image_map = ancestor->in_image_map = 0;
      b->pointer_over = ancestor->pointer_over = NULL;
      browser_pointer_check(0, NULL,NULL, b);
      mouse_watch_pointer_control(1);
    }

    return key;
  }

  return 0;
}

/*************************************************/
/* browser_fetch_url()                           */
/*                                               */
/* Looks in the browser_data structure given to  */
/* the function and returns the URL that is      */
/* currently being fetched, or NULL for none.    */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the fetch.            */
/*                                               */
/* Returns:    Pointer to the URL string or NULL */
/*             if there is no fetch URL present. */
/*************************************************/

char * browser_fetch_url(browser_data * b)
{
  return b->urlfdata;
}

/*************************************************/
/* browser_current_url()                         */
/*                                               */
/* Returns a pointer to the current URL being    */
/* displayed in a browser window, unless there   */
/* is none, in which case the fetch URL is       */
/* given; the pointer may still be NULL though.  */
/*                                               */
/* Parameters: A pointer to a browser_data       */
/*             structure relevant to the window. */
/*                                               */
1521 1522 1523
/* Returns:    Pointer to the displayed URL, or  */
/*             the fetch URL if none is          */
/*             displayed.                        */
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
/*************************************************/

char * browser_current_url(browser_data * b)
{
  /* NB, should this function ever become more complex than this, */
  /* note that external callers rely on it NOT corrupting the     */
  /* 'tokens' buffer used by lookup_token or lookup_choice - i.e. */
  /* this function may not call these.                            */

  return b->urlddata;
}

1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
/*************************************************/
/* browser_current_title()                       */
/*                                               */
/* Examines an HStream list for a given browser  */
/* trying to find a TITLE tag; if it finds it,   */
/* a pointer to the title text is returned.      */
/*                                               */
/* Parameters: A pointer to a browser_data       */
/*             structure relevant to the HStream */
/*             list.                             */
/*                                               */
/* Returns:    Pointer to the page title, or     */
/*             NULL if none is found.            */
/*************************************************/

char * browser_current_title(browser_data * b)
{
  HStream * current = b->stream;

  while (current)
  {
    if (ISHEAD(current) && current->tagno == TAG_TITLE && current->text) return current->text;

    current = current->next;
  }

  return NULL;
}

1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604
/*************************************************/
/* browser_destroy_source()                      */
/*                                               */
/* Pass a browser_data structure pointer, and if */
/* this points to a block of memory holding      */
/* fetched HTML source, free up that block.      */
/*                                               */
/* Parameters: A pointer to a browser_data       */
/*             structure relevant to the source. */
/*************************************************/

void browser_destroy_source(browser_data * b)
{
  if (b->source)
  {
    #ifdef TRACE
      if (tl & (1u<<12)) Printf("browser_destroy_source: flex_free block %p which held page source\n",&b->source);
      flexcount -= flex_size((flex_ptr) &b->source);
      if (tl & (1u<<14)) Printf("**   flexcount: %d\n",flexcount);
    #endif

    flex_free((flex_ptr) &b->source);
    b->source = NULL;
  }
}

/*************************************************/
/* browser_pointer_entering()                    */
/*                                               */
/* Called when the pointer goes over a browser   */
/* window. Installs a null event handler to      */
/* watch over the pointer's position relative to */
/* links on the page.                            */
/*                                               */
/* Parameters are as standard for a Wimp event   */
/* handler.                                      */
/*************************************************/

int browser_pointer_entering(int eventcode, WimpPollBlock * block, IdBlock * idb, void * handle)
{
1605 1606 1607 1608 1609 1610 1611 1612
  browser_data * b = NULL;

  /* If dragging, don't want to know about this at all (at least */
  /* for now, when drags only correspond to resizing frames)     */

  if (drag_in_progress) return 0;

  /* Quick sanity check (this can and does happen - Toolbox oddities) */
1613 1614 1615 1616 1617 1618 1619

  if (!idb->self_id) return 0;

  /* Is this a browser we know about? */

  ChkError(toolbox_get_client_handle(0, idb->self_id, (void *) &b));

1620
  if (is_known_browser(b)) register_null_claimant(Wimp_ENull, (WimpEventHandler *) browser_pointer_check, b);
1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634

  pointer_is_over = idb->self_id;

  return 0;
}

/*************************************************/
/* browser_pointer_leaving()                     */
/*                                               */
/* Called when the pointer goes out of a browser */
/* window. Deinstalls a null event handler that  */
/* watched over the pointer's position relative  */
/* to links on the page.                         */
/*                                               */
1635 1636 1637 1638 1639
/* Note that objects may be deleted and this     */
/* function wouldn't be called, so anything that */
/* goes in here should be echoed somewhere in    */
/* windows_close_browser.                        */
/*                                               */
1640 1641 1642 1643 1644 1645
/* Parameters are as standard for a Wimp event   */
/* handler.                                      */
/*************************************************/

int browser_pointer_leaving(int eventcode, WimpPollBlock * block, IdBlock * idb, void * handle)
{
1646 1647 1648
  browser_data * b = NULL;

  /* Quick sanity check (as for browser_pointer_entering) */
1649 1650 1651

  if (!idb->self_id) return 0;

1652
  #ifdef TRACE
1653

1654
    /* If the pointer is leaving, we should know what it was over */
1655

1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
    if (idb->self_id != pointer_is_over)
    {
      erb.errnum = Utils_Error_Custom_Normal;
      sprintf(erb.errmess,
              "Existing pointer_is_over ID %08x doesn't match ID %08x given to browser_pointer_leaving",
              pointer_is_over,
              idb->self_id);

      show_error_ret(&erb);
    }

  #endif

  ChkError(toolbox_get_client_handle(0, idb->self_id, (void *) &b));

  if (is_known_browser(b))
1672 1673 1674
  {
    deregister_null_claimant(Wimp_ENull, (WimpEventHandler *) browser_pointer_check, b);

1675 1676 1677 1678 1679 1680 1681 1682
    if (!drag_in_progress)
    {
      mouse_set_pointer_shape(Mouse_Shape_Normal);
      if (mouse_pointer_is_on()) toolbars_cancel_status(b, Toolbars_Status_LinkTo);

      b->pointer_over = NULL;

    }
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724

    pointer_is_over = 0;

    return 1;
  }

  pointer_is_over = 0;

  return 0;
}

/*************************************************/
/* browser_pointer_over_deleted()                */
/*                                               */
/* Checks to see if the pointer is over an       */
/* object that has just been deleted, and if     */
/* so, deregisters any pointer-related event     */
/* handlers associated with it.                  */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the window / object.  */
/*************************************************/

void browser_pointer_over_deleted(browser_data * b)
{
  if (pointer_is_over && pointer_is_over == b->self_id)
  {
    deregister_null_claimant(Wimp_ENull, (WimpEventHandler *) browser_pointer_check, b);
    mouse_set_pointer_shape(Mouse_Shape_Normal);
  }
}

/*************************************************/
/* browser_pointer_check()                       */
/*                                               */
/* Checks the pointer position relative to any   */
/* links on the page; if it is over one,         */
/* the pointer shape is changed and the status   */
/* bar updated. Alternatively, it is changed     */
/* back to the normal pointer shape and the      */
/* status bar put back to its Ready state.       */
/*                                               */
1725 1726 1727 1728
/* This function will also change the pointer to */
/* a shape indicating frame borders may be       */
/* dragged to resize them, where appropriate.    */
/*                                               */
1729 1730 1731 1732
/* Parameters are as for a standard Wimp event   */
/* handler, though only browser_data * handle is */
/* of interest (it points to a browser_data      */
/* struct relevant to the window in question).   */
1733 1734 1735 1736
/*                                               */
/* The function may be used as a NULL handler,   */
/* so never return 1 from it (you'll claim the   */
/* null event - this is Bad).                    */
1737 1738 1739 1740
/*************************************************/

int browser_pointer_check(int eventcode, WimpPollBlock * b, IdBlock * idb, browser_data * handle)
{
1741
  int                       changed;
1742 1743 1744
  HStream                 * tp = NULL;
  WimpGetPointerInfoBlock   p;

1745 1746 1747 1748 1749 1750 1751 1752
  /* If we're dragging, exit */

  if (drag_in_progress) return 0;

  if (wimp_get_pointer_info(&p)) return 0;

  /* If this browser has children, we must be over frame borders */

1753
  if (handle->nchildren && !choices.fixed_pointer)
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774
  {
    int row, col;

    if (!frames_find_pointer_in_frameset(handle,
                                         p.x,
                                         p.y,
                                         &row,
                                         &col,
                                         NULL,
                                         NULL,
                                         0))
    {
      if      (row > 0   && col < 0)   mouse_set_pointer_shape(Mouse_Shape_UD);
      else if (row < 0   && col > 0)   mouse_set_pointer_shape(Mouse_Shape_LR);
      else if (row > 0   && col > 0)   mouse_set_pointer_shape(Mouse_Shape_UDLR);
      else if (row == -2 || col == -2) mouse_set_pointer_shape(Mouse_Shape_NoResize);
      else                             mouse_set_pointer_shape(Mouse_Shape_Normal);
    }

    return 0;
  }
1775 1776 1777 1778 1779 1780

  /* Find the token that the pointer is over */

  tp = browser_get_pointer_token(handle,&p,NULL,NULL);

  if (
1781
       choices.highlight_links      &&
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
       tp                           &&
       CanBeSelected(tp)            &&
       mouse_pointer_is_on()        &&
       !redraw_selected(handle, tp)
     )
  {
    browser_clear_selection(handle, 0);
    browser_select_token(handle, tp, 0);
  }

1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
  /* Is this an image map? If so, have the coordinates of the pointer */
  /* over the map changed?                                            */

  if (tp && (tp->type & (TYPE_ISCLIENTMAP | TYPE_ISMAP)))
  {
    int nx, ny;

    /* Find out which pixel we clicked on */

    ChkError(image_return_click_offset(handle, tp, &p, &nx, &ny));

    if (nx != handle->map_x || ny != handle->map_y)
    {
      handle->map_x = nx;
      handle->map_y = ny;
      changed       = 1;
    }
    else changed = 0;
  }

  /* Otherwise, work out if we've changed from the token address. */

  else changed = (tp != handle->pointer_over);

1816 1817 1818 1819 1820 1821 1822
  /* Sometimes pointer_over has to be zeroed because a token */
  /* list is getting freed, e.g. at the beginning of a new   */
  /* fetch. In this case, we need to always restore the      */
  /* pointer shape if tp is NULL.                            */

  if (tp == NULL) changed = 1;

1823 1824 1825
  /* If this isn't the same as the token the pointer */
  /* was last recorded as being over...              */

1826
  if (changed)
1827 1828 1829
  {
    handle->pointer_over = tp;

1830
    if (!choices.fixed_pointer)
1831 1832 1833 1834 1835 1836
    {
      /* If the Choices say the pointer can change shape, */
      /* and the pointer is over an identified token,     */
      /* set the pointer to ptr_link. Else restore the    */
      /* pointer to its normal shape.                     */

1837 1838 1839 1840 1841 1842 1843
      if (
           tp &&
           (
             (tp->type & TYPE_ISCLIENTMAP) ||
             (ISLINK(tp))
           )
         )
1844
      {
1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903
        int dealt_with = 0;

        /* Client side map */

        if (tp->type & TYPE_ISCLIENTMAP)
        {
          char * url;

          /* Find out what we're over - affects the pointer shape, you see... */

          csim_return_info(handle,
                           tp,
                           handle->map_x,
                           handle->map_y,
                           &url,
                           NULL,
                           NULL);

          /* Use a Link pointer for areas defined in the map */

          if (url && *url)
          {
            mouse_set_pointer_shape(Mouse_Shape_Link);
            dealt_with = 1;
          }
        }

        /* For anything which isn't a client side map, we must have */
        /* an anchor.                                               */

        if (!dealt_with && tp->anchor && *tp->anchor)
        {
          /* If this is a server side map, use the Map pointer */

          if (tp->type & TYPE_ISMAP)
          {
            mouse_set_pointer_shape(Mouse_Shape_Map);
          }

          /* Otherwise, use the normal Link pointer */

          else
          {
            mouse_set_pointer_shape(Mouse_Shape_Link);
          }

          dealt_with = 1;
        }

        /* If we've still not worked out what this is, cancel any */
        /* LinkTo message (e.g. over a client side image map with */
        /* no alternative URLs and not on any area defined by the */
        /* map itself).                                           */

        if (!dealt_with)
        {
          handle->pointer_over = NULL;
          mouse_set_pointer_shape(Mouse_Shape_Normal);
        }
1904
      }
1905

1906
      else if (
1907 1908
                tp                     &&
                tp->tagno == TAG_INPUT &&
1909 1910 1911 1912 1913 1914 1915 1916
                HtmlINPUTtype(tp) == inputtype_IMAGE
              )
              mouse_set_pointer_shape(Mouse_Shape_Link);
      else
      {
        handle->pointer_over = NULL;
        mouse_set_pointer_shape(Mouse_Shape_Normal);
      }
1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963
    }

    /* If the mouse pointer is on, then update the status bar */
    /* to reflect the link it is over (or cancel a LinkTo     */
    /* message if it has moved off a link),                   */

    if (mouse_pointer_is_on())
    {
      if (handle->pointer_over) toolbars_update_status(handle, Toolbars_Status_LinkTo);
      else                      toolbars_cancel_status(handle, Toolbars_Status_LinkTo);
    }
  }

  return 0;
}

/*************************************************/
/* browser_get_pointer_token()                   */
/*                                               */
/* Returns the token number that the pointer is  */
/* over (if any) and an X and Y offset into      */
/* the line chunk representing that token.       */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             for the window the pointer is     */
/*             over;                             */
/*                                               */
/*             Pointer to a block describing     */
/*             the pointer condition;            */
/*                                               */
/*             Pointer to an int, in which the X */
/*             offset into the chunk is placed;  */
/*             Same for the Y offset.            */
/*                                               */
/* Returns:    Address of the token the pointer  */
/*             is over, or NULL for unknown /    */
/*             none.                             */
/*                                               */
/* Assumes:    The pointers to the ints for the  */
/*             X and Y offsets *can* be NULL.    */
/*************************************************/

HStream * browser_get_pointer_token(browser_data * b, WimpGetPointerInfoBlock * p, int * ox, int * oy)
{
  WimpGetWindowStateBlock   state;
  reformat_cell           * cell = b->cell;

1964 1965 1966 1967
  /* If we're dragging, return NULL */

  if (drag_in_progress) return NULL;

1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110
  /* No point proceeding if there are no lines */

  if (!cell->ldata) return NULL;

  /* No point searching the tokens if we're not over the right window and */
  /* then only in the display area of that window.                        */

  if (b->window_handle != p->window_handle || p->icon_handle < -1) return NULL;

  /* Return 0 if there's an error getting the window's state information */

  state.window_handle = p->window_handle;
  if (wimp_get_window_state(&state)) return NULL;

  return browser_get_pointer_token_r(b, cell, p, &state, ox, oy);
}

/*************************************************/
/* browser_get_pointer_token_r()                 */
/*                                               */
/* Recursive back-end to                         */
/* browser_get_pointer_token.                    */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             for the window the pointer is     */
/*             over;                             */
/*                                               */
/*             Pointer to a reformat_cell struct */
/*             holding the line/chunk data to    */
/*             check against the pointer         */
/*             position;                         */
/*                                               */
/*             Pointer to a block describing     */
/*             the pointer condition;            */
/*                                               */
/*             A WimpGetWindowStateBlock pointer */
/*             where the block contains details  */
/*             of the browser window;            */
/*                                               */
/*             Pointer to an int, in which the X */
/*             offset into the chunk is placed;  */
/*                                               */
/*             Same for the Y offset.            */
/*                                               */
/* Returns:    As browser_get_pointer_token.     */
/*                                               */
/* Assumes:    As browser_get_pointer_token.     */
/*************************************************/

static HStream * browser_get_pointer_token_r(browser_data * b, reformat_cell * cell, WimpGetPointerInfoBlock * p,
                                             WimpGetWindowStateBlock * state, int * ox, int * oy)
{
  int x, y, line;

  /* Convert the pointer's screen x and y coordinates to window coordinatess */

  x = coords_x_toworkarea(p->x, (WimpRedrawWindowBlock *) state);
  y = coords_y_toworkarea(p->y, (WimpRedrawWindowBlock *) state);

  /* Find the line that the pointer is over */

  line = browser_line_at_y(b, cell, y);
  if (line < 0) return 0;

  /* Convert the x coordinate to millipoints */

  convert_to_points(x, &x);

  if (ox) *ox = 0;
  if (oy) *oy = 0;

  /* Proceed only if the line has some chunks associated with it */

  if (cell->ldata[line].n)
  {
    int       cx, n, i;
    HStream * tp;

    /* Find the token's address for the first chunk on the line, */
    /* work out where its X coordinate should be and compare     */
    /* this to the pointer X. If the pointer X is less than the  */
    /* line X, the pointer lies to the left of all the chunks so */
    /* exit here with 0.                                         */

    tp = cell->cdata[cell->ldata[line].chunks].t;

    cx = redraw_start_x(b,
                        cell,
                        tp,
                        line);

    convert_to_points(cx, &cx);

    if (x < cx) return 0;

    /* For each chunk, take the calculated left hand coordinate */
    /* for the whole line and add the width of each chunk until */
    /* the pointer X lies to the left of the calculated coord.  */

    n = 0;
    i = cell->ldata[line].n - 1;

    while (i >= 0 && x > (cx + cell->cdata[cell->ldata[line].chunks + n].w))
    {
      cx += cell->cdata[cell->ldata[line].chunks + n].w;
      n++;
      i--;
    }

    /* If a chunk was found that the pointer is over... */

    if (i >= 0)
    {
      BBox box;

      /* Get the address of the token corresponding to the chunk */

      tp = cell->cdata[cell->ldata[line].chunks + n].t;

      #ifdef TRACE
        if (tl & (1u<<3))
        {
          static tracelastchunk = -1;

          if (tracelastchunk != cell->ldata[line].chunks + n)
          {
            Printf("Chunk : %d, token: %d\n",cell->ldata[line].chunks + n,cell->cdata[cell->ldata[line].chunks + n].t);
            Printf("Text  : '%s'\n",tp->text);
            Printf("Style : %p\n",(void *) tp->style);

            if (ISLINK(tp))
            {
              Printf("Link  : '%s'\n",tp->anchor);
              Printf("Target: '%s'\n",tp->target);
            }

            tracelastchunk = cell->ldata[line].chunks + n;
          }
        }
      #endif

      /* If the token represents a table... */

2111
      if (tp->tagno == TAG_TABLE)
2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248
      {
        /* In this case there are table streams hung from d->cdata */

        table_stream            * table     = (table_stream *) tp;
        table_row               * row       = NULL;
        table_headdata          * head      = NULL;
        reformat_cell           * cellarray = table->cells;
        reformat_cell           * c;
        WimpGetWindowStateBlock   s         = *state;
        HStream                 * over      = NULL;
        int                       tablx, tably;
        int                       lineh;
        int                       cellindex;
        int                       cellcount = 0;
        int                       cellmax   = table->ColSpan * table->RowSpan;

        /* Only proceed if there are table cells to redraw */

        if (cellarray)
        {
          /* Get the bottom left of the table in tablx, tably, */
          /* in window coords, units of millipoints.           */

          tablx = cx;
          convert_to_points(cell->ldata[line].y, &tably);

          /* Get the line height in millipoints */

          convert_to_points(cell->ldata[line].h, &lineh);

          /* Table cells aren't scrolled! */

          s.xscroll = s.yscroll = 0;

          /* Start going through the rows */

          row = table->List;

          while (row && !over && cellcount < cellmax)
          {
            head = row->List;

            while (head && !over && cellcount < cellmax)
            {
              switch (head->Tag)
              {
                case TagTableData:
                case TagTableHead:
                {
                  cellindex = head->RowOffs * table->ColSpan + head->ColOffs;

                  if (cellindex < cellmax)
                  {
                    c = &cellarray[cellindex];

                    /* No point proceeding without lines to scan */

                    if (c->nlines)
                    {
                      /* Set the visible area BBox to be that of the current cell */
                      /* (this will be in millipoint window coords)               */

                      s.visible_area.xmin = tablx + c->x;
                      s.visible_area.ymin = tably + c->y + lineh - c->cellheight;
                      s.visible_area.xmax = s.visible_area.xmin + c->cellwidth  - 1;
                      s.visible_area.ymax = s.visible_area.ymin + c->cellheight - 1;

                      /* Convert the above to OS units, then to screen coords */

                      convert_box_to_os(&s.visible_area, &s.visible_area);
                      coords_box_toscreen(&s.visible_area, (WimpRedrawWindowBlock *) state);

                      /* Recursive call */

                      over = browser_get_pointer_token_r(b, c, p, &s, ox, oy);

                      /* If something has been found, may need to fill in ox and oy */

                      if (over)
                      {
                        /* If an address of an int to return Y information to was given... */

                        if (oy)
                        {
                          /* Set the int to hold the distance from the top of the line */
                          /* that the pointer was at.                                  */

                          convert_to_os(lineh, oy);
                          *oy -= (p->y - s.visible_area.ymin);

                          if (*oy < 0) *oy = 0;
                          if (*oy > s.visible_area.ymax) *oy = s.visible_area.ymax;
                        }

                        /* Similarly for X information, get the X offset into the chunk */

                        if (ox)
                        {
                          *ox = p->x - s.visible_area.xmin;

                          if (ISLINK(over)) *ox -= over->maxlen * 2;

                          if (*ox < 0) *ox = 0;
                          if (*ox > s.visible_area.xmax) *ox = s.visible_area.xmax;
                        }
                      }

                    /* Closure of 'if (c->nlines)' */
                    }

                  /* Closure of 'if (cellindex < cellmax)' */
                  }

                /* Closure of specific 'case' item */
                }
                break;

              /* Closure of 'switch (head->Tag)' */
              }

              cellcount++;

              head = head->Next;

            /* Closure of 'while (head)' */
            }

            row = row->Next;

          /* Closure of 'while (row)' */
          }

        /* Closure of 'if (cellarray)' */
        }

        return over;

2249
      /* Closure of 'if (tp->tagno == TAG_TABLE)' */
2250 2251 2252 2253
      }

      /* If the token represents an image... */

2254
      else if (
2255 2256
                (tp->style & IMG) ||
                (ISOBJECT(tp))    ||
2257
                (
2258
                  tp->tagno == TAG_INPUT &&
2259 2260 2261
                  HtmlINPUTtype(tp) == inputtype_IMAGE
                )
              )
2262
      {
2263
        int brdr = 0;
2264

2265 2266
        if (ISOBJECT(tp)) reformat_get_object_size(b, tp, &box);
        else              reformat_get_image_size (b, tp, &box);
2267

2268 2269
        /* Correct for the border size if the image is a link, */
        /* or if this is an Object.                            */
2270

2271
        if (ISLINK(tp) && (tp->style & IMG)) brdr = tp->maxlen * 2;
2272
        else if (ISOBJECT(tp))               brdr = HtmlOBJECTborder(tp) * 2;
2273

2274 2275 2276 2277 2278 2279
        if (brdr)
        {
          box.xmin += brdr;
          box.ymin += brdr;
          box.xmax -= brdr;
          box.ymax -= brdr;
2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357
        }
      }
      else
      {
        /* The token does not represent an image, and so is text. */

        box.xmin = 0;

        /* Convert the width of the line chunk to OS units */

        convert_to_os(cell->cdata[cell->ldata[line].chunks + n].w, &box.xmax);

        /* Set the Y coordinates of the BBox structure to the base */
        /* line and base line plus height of the chunk.            */

        box.ymin = -cell->ldata[line].b;
        box.ymax =  cell->ldata[line].h - cell->ldata[line].b;
      }

      /* If an address of an int to return Y information to was given... */

      if (oy)
      {
        /* Set the int to hold the distance from the bottom of the line */
        /* that the pointer was at.                                     */

        *oy = (cell->ldata[line].y + cell->ldata[line].b + box.ymax) - y;

        if (*oy < 0) *oy = 0;
        if (*oy >= (box.ymax - box.ymin)) *oy = box.ymax - box.ymin - 1;
      }

      /* Similarly for X information, get the X offset into the chunk */

      if (ox)
      {
        *ox = x - cx;

        convert_to_os(*ox, ox);

        if (ISLINK(tp)) *ox -= tp->maxlen * 2;
        if (*ox < 0) *ox = 0;
        if (*ox >= box.xmax) *ox = box.xmax - 1;

      }

      /* Return the token address */

      return cell->cdata[cell->ldata[line].chunks + n].t;
    }
  }

  return NULL;
}

/*************************************************/
/* browser_line_at_y()                           */
/*                                               */
/* Returns the line number for a given y window  */
/* coordinate.                                   */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the window;           */
/*                                               */
/*             Pointer to a reformat_cell struct */
/*             holding the line information;     */
/*                                               */
/*             The y coordinate.                 */
/*                                               */
/* Returns:    The line number in which the Y    */
/*             coordinate lives, or -1 for none  */
/*             / an error.                       */
/*************************************************/

int browser_line_at_y(browser_data * b, reformat_cell * cell, int y)
{
  int l;

2358 2359
  if (!cell) cell = b->cell;

2360 2361 2362 2363
  /* Find a line who's y coordinate is lower than the given one, */
  /* i.e. its baseline is the first one of all lines in th cell  */
  /* that lies below that coordinate.                            */

2364 2365
  for (l = 0; (l <= cell->nlines) && (cell->ldata[l].y > y); l++);

2366 2367 2368 2369 2370 2371
  /* Either we have run out of lines, or the top of the line we    */
  /* found also lies below the given coordinate, in which case the */
  /* coordinate does not lie within that line and we should not    */
  /* return its number.                                            */

  if (l >= cell->nlines || cell->ldata[l].y + cell->ldata[l].h < y) return -1;
2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397

  return l;
}

/*************************************************/
/* browser_top_line()                            */
/*                                               */
/* Returns the line number displayed at the top  */
/* of the visible area of the browser window.    */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the window;           */
/*                                               */
/*             Pointer to a word into which the  */
/*             address of a reformat_cell will   */
/*             be returned - the given line lies */
/*             in this cell;                     */
/*                                               */
/*             A WimpGetWindowStateBlock pointer */
/*             holding details of the browser    */
/*             window;                           */
/*                                               */
/*             1 if the line must be wholly      */
/*             visible, else 0 if it may be just */
/*             partially visible.                */
/*                                               */
2398
/* Returns:    Directly, the number of the line  */
2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448
/*             displayed at the top of the       */
/*             window, or -1 for none / an       */
/*             error.                            */
/*                                               */
/* Assumes:    That *none* of the parameter      */
/*             pointers are NULL.                */
/*************************************************/

int browser_top_line(browser_data * b, reformat_cell ** ret_cell,
                     WimpGetWindowStateBlock * s, int fully_visible)
{
  if (!ret_cell) return -1;

  *ret_cell = NULL;

  return browser_top_line_r(b, b->cell, ret_cell, s, fully_visible);
}

/*************************************************/
/* browser_top_line_r()                          */
/*                                               */
/* Recursive back-end to browser_top_line.       */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the window;           */
/*                                               */
/*             Pointer to a reformat_cell struct */
/*             holding the cells to scan;        */
/*                                               */
/*             Pointer to a word into which the  */
/*             address of a reformat_cell will   */
/*             be returned - the given line lies */
/*             in this cell;                     */
/*                                               */
/*             A WimpGetWindowStateBlock pointer */
/*             holding details of the browser    */
/*             window;                           */
/*                                               */
/*             1 if the line must be wholly      */
/*             visible, else 0 if it may be just */
/*             partially visible.                */
/*                                               */
/* Returns:    As browser_top_line.              */
/*                                               */
/* Assumes:    As browser_top_line.              */
/*************************************************/

static int browser_top_line_r(browser_data * b, reformat_cell * cell, reformat_cell ** ret_cell,
                              WimpGetWindowStateBlock * s, int fully_visible)
{
2449
  int y, l, htop;
2450

2451 2452
  if (!cell) cell = b->cell;

2453 2454
  if (!cell->nlines || !cell->ldata || !cell->cdata) return -1;

2455 2456
  if (!controls.swap_bars) htop = toolbars_button_height(b) + toolbars_url_height(b);
  else                     htop = toolbars_status_height(b);
2457

2458 2459 2460
  if (htop) htop += wimpt_dy();

  y = s->yscroll - htop;
2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482

  l = 0;

  /* Find the line */

  while (
          l < cell->nlines &&
          cell->ldata[l].y + (fully_visible ? cell->ldata[l].h : 0) > y
        )
        l++;

  /* If l >= number of lines, nothing was found */

  if (l >= cell->nlines) return -1;
  else
  {
    HStream * first_token = cell->cdata[cell->ldata[l].chunks].t;

    *ret_cell = cell;

    /* Otherwise, is there a table in the line? */

2483
    if (first_token->tagno == TAG_TABLE)
2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552
    {
      table_stream   * table     = (table_stream *) first_token;
      table_row      * row       = NULL;
      table_headdata * head      = NULL;
      reformat_cell  * c         = NULL;
      reformat_cell  * cellarray = table->cells;
      int              cellmax   = table->ColSpan * table->RowSpan;
      int              cellcount = 0;
      int              cellindex;

      /* Proceed if the cell array can be found */

      if (cellarray)
      {
        row = table->List;

        while (row && cellcount < cellmax)
        {
          head = row->List;

          while (head && cellcount < cellmax)
          {
            switch (head->Tag)
            {
              case TagTableData:
              case TagTableHead:
              {
                /* Find the reformat_cell structure for this table cell */

                cellindex = head->RowOffs * table->ColSpan + head->ColOffs;

                if (cellindex < cellmax)
                {
                  c = &cellarray[cellindex];

                  /* If it has lines, recursively call this function to find the */
                  /* cell line at the top of the window.                         */

                  if (c->nlines)
                  {
                    l = browser_top_line_r(b, c, ret_cell, s, fully_visible);

                    /* If a line can be found inside this cell then return it immediately, */
                    /* rather than worrying about the other cells. Otherwise, continue     */
                    /* scanning cells. Since table cells are arranged left right, top to   */
                    /* bottom, and we are looking for the top line, this will work OK.     */

                    if (l >= 0) return l;
                  }
                }
              }
              break;
            }

            cellcount ++;

            head = head->Next;

          /* Closure of 'while (head && ...)' */
          }

          row = row->Next;

        /* Closure of 'while (row && ...)' */
        }

      /* Closure of 'if (cellarray)' */
      }

2553
    /* Closure of 'if (first_token->tagno == TAG_TABLE)' */
2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584
    }

  /* Closure of 'else' case for 'if (l >= cell->nlines)' */
  }

  return l;
}

/*************************************************/
/* browser_bottom_line()                         */
/*                                               */
/* Returns the line number displayed at the      */
/* bottom of the visible area of the browser     */
/* window.                                       */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the window;           */
/*                                               */
/*             Pointer to a word into which the  */
/*             address of a reformat_cell will   */
/*             be returned - the given line lies */
/*             in this cell;                     */
/*                                               */
/*             A WimpGetWindowStateBlock pointer */
/*             holding details of the browser    */
/*             window;                           */
/*                                               */
/*             1 if the line must be wholly      */
/*             visible, else 0 if it may be just */
/*             partially visible.                */
/*                                               */
2585
/* Returns:    Directly, the number of the line  */
2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635
/*             displayed at the bottom of the    */
/*             window, or -1 for none / an       */
/*             error.                            */
/*                                               */
/* Assumes:    That none of the parameter        */
/*             pointers are NULL.                */
/*************************************************/

int browser_bottom_line(browser_data * b, reformat_cell ** ret_cell,
                        WimpGetWindowStateBlock * s, int fully_visible)
{
  if (!ret_cell) return -1;

  *ret_cell = NULL;

  return browser_bottom_line_r(b, b->cell, ret_cell, s, fully_visible);
}

/*************************************************/
/* browser_bottom_line_r()                       */
/*                                               */
/* Recursive back-end to browser_bottom_line.    */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the window;           */
/*                                               */
/*             Pointer to a reformat_cell struct */
/*             holding the cells to scan;        */
/*                                               */
/*             Pointer to a word into which the  */
/*             address of a reformat_cell will   */
/*             be returned - the given line lies */
/*             in this cell;                     */
/*                                               */
/*             A WimpGetWindowStateBlock pointer */
/*             holding details of the browser    */
/*             window;                           */
/*                                               */
/*             1 if the line must be wholly      */
/*             visible, else 0 if it may be just */
/*             partially visible.                */
/*                                               */
/* Returns:    As browser_bottom_line.           */
/*                                               */
/* Assumes:    As browser_bottom_line.           */
/*************************************************/

static int browser_bottom_line_r(browser_data * b, reformat_cell * cell, reformat_cell ** ret_cell,
                                 WimpGetWindowStateBlock * s, int fully_visible)
{
2636
  int y, l, hbot;
2637

2638 2639
  if (!cell) cell = b->cell;

2640 2641
  if (!cell->nlines || !cell->ldata || !cell->cdata) return -1;

2642 2643
  if (!controls.swap_bars) hbot = toolbars_status_height(b);
  else                     hbot = toolbars_button_height(b) + toolbars_url_height(b);
2644

2645 2646 2647 2648 2649
  if (hbot) hbot += wimpt_dy();

  y = s->yscroll                                    -
      (s->visible_area.ymax - s->visible_area.ymin) -
      hbot;
2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671

  l = cell->nlines - 1;

  /* Find the line */

  while (
          l >= 0 &&
          cell->ldata[l].y + (fully_visible ? 0 : cell->ldata[l].h) < y
        )
        l--;

  /* If l < 0, nothing was found */

  if (l < 0) return -1;
  else
  {
    HStream * first_token = cell->cdata[cell->ldata[l].chunks].t;

    *ret_cell = cell;

    /* Otherwise, is there a table in the line? */

2672
    if (first_token->tagno == TAG_TABLE)
2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720
    {
      table_stream   * table     = (table_stream *) first_token;
      table_row      * row       = NULL;
      table_headdata * head      = NULL;
      reformat_cell  * c         = NULL;
      reformat_cell  * cellarray = table->cells;
      int              cellmax   = table->ColSpan * table->RowSpan;
      int              cellcount = 0;
      int              cellindex;

      /* Proceed if the cell array can be found */

      if (cellarray)
      {
        /* Since we're finding the bottom line and table cells are arranged */
        /* left to right, top to bottom, we need to start at the last row   */
        /* of the last column.                                              */

        row = table->List;

        while (row && row->Next) row = row->Next;

        while (row && cellcount < cellmax)
        {
          head = row->List;

          while (head && head->Next) head = head->Next;

          while (head && cellcount < cellmax)
          {
            switch (head->Tag)
            {
              case TagTableData:
              case TagTableHead:
              {
                /* Find the reformat_cell structure for this table cell */

                cellindex = head->RowOffs * table->ColSpan + head->ColOffs;

                if (cellindex < cellmax)
                {
                  c = &cellarray[cellindex];

                  /* If it has lines, recursively call this function to find the */
                  /* cell line at the top of the window.                         */

                  if (c->nlines)
                  {
2721
                    l = browser_bottom_line_r(b, c, ret_cell, s, fully_visible);
2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749

                    /* If a line can be found inside this cell then return it immediately, */
                    /* rather than worrying about the other cells. Otherwise, continue     */
                    /* scanning cells. Since table cells are arranged left right, top to   */
                    /* bottom, and we are looking for the top line, this will work OK.     */

                    if (l >= 0) return l;
                  }
                }
              }
              break;
            }

            cellcount ++;

//            head = head->Prev;

          /* Closure of 'while (head && ...)' */
          }

//          row = row->Prev;

        /* Closure of 'while (row && ...)' */
        }

      /* Closure of 'if (cellarray)' */
      }

2750
    /* Closure of 'if (first_token->tagno == TAG_TABLE)' */
2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767
    }

  /* Closure of 'else' case for 'if (l >= cell->nlines)' */
  }

  return l;
}

/*************************************************/
/* browser_redraw_border()                       */
/*                                               */
/* Does a series of Wimp_ForceRedraw calls that  */
/* cause a border of 4 OS units around a given   */
/* image to be redrawn.                          */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the image;            */
2768
/*                                               */
2769 2770 2771 2772 2773 2774 2775
/*             Pointer to a token representing   */
/*             the image.                        */
/*************************************************/

static _kernel_oserror * browser_redraw_border(browser_data * b, HStream * token)
{
  BBox                      r1;
2776
  int                       more, x, y, w, h;
2777 2778 2779 2780 2781 2782 2783 2784 2785
  _kernel_oserror         * e;
  WimpGetWindowStateBlock   state;
  WimpRedrawWindowBlock     redraw;

  /* Find the position of the image */

  e = reformat_get_image_size(b, token, &r1);
  if (e) return e;

2786 2787 2788
  w = r1.xmax - r1.xmin;
  h = r1.ymax - r1.ymin;

2789 2790 2791 2792 2793 2794 2795 2796
  state.window_handle = redraw.window_handle = b->window_handle;
  e = wimp_get_window_state(&state);
  if (e) return e;

  /* Find the x and y coordinates of the image's bottom left hand corner */

  if (image_get_token_image_position(b, token, &x, &y)) return NULL;

2797 2798
  /* Work out the screen coordinates of the image, with an extra */
  /* amount account for the selection border.                    */
2799

2800 2801 2802 2803
  r1.xmin = x - 4;
  r1.ymin = y - 4;
  r1.xmax = r1.xmin + w + 8;
  r1.ymax = r1.ymin + h + 8;
2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014

  redraw.xscroll = state.xscroll;
  redraw.yscroll = state.yscroll;

  /* Bottom edge */

  redraw.visible_area.xmin = r1.xmin, redraw.visible_area.ymin = r1.ymin;
  redraw.visible_area.xmax = r1.xmax, redraw.visible_area.ymax = r1.ymin + 4;

  e = wimp_update_window(&redraw, &more);
  if (e) return e;

  if (more)
  {
    e = redraw_draw(b, &redraw, 0, token);
    if (e) return e;
  }

  /* Left edge */

  redraw.visible_area.xmin = r1.xmin,     redraw.visible_area.ymin = r1.ymin;
  redraw.visible_area.xmax = r1.xmin + 4, redraw.visible_area.ymax = r1.ymax;

  e = wimp_update_window(&redraw, &more);
  if (e) return e;

  if (more)
  {
    e = redraw_draw(b, &redraw, 0, token);
    if (e) return e;
  }

  /* Top edge */

  redraw.visible_area.xmin = r1.xmin, redraw.visible_area.ymin = r1.ymax - 4;
  redraw.visible_area.xmax = r1.xmax, redraw.visible_area.ymax = r1.ymax;

  e = wimp_update_window(&redraw, &more);
  if (e) return e;

  if (more)
  {
    e = redraw_draw(b, &redraw, 0, token);
    if (e) return e;
  }

  /* Right edge */

  redraw.visible_area.xmin = r1.xmax - 4, redraw.visible_area.ymin = r1.ymin;
  redraw.visible_area.xmax = r1.xmax,     redraw.visible_area.ymax = r1.ymax;

  e = wimp_update_window(&redraw, &more);
  if (e) return e;

  if (more) return redraw_draw(b, &redraw, 0, token);

  return NULL;
}

/*************************************************/
/* browser_update()                              */
/*                                               */
/* Updates a window contents, using calls to     */
/* Wimp_UpdateWindow (so this can be used for    */
/* animations, etc., as the Wimp won't clear     */
/* the redraw area first).                       */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the window;           */
/*                                               */
/*             Pointer to a RedrawWindow block,  */
/*             with details of the area to       */
/*             redraw within it;                 */
/*                                               */
/*             1 to not draw backgrounds, or 0   */
/*             to allow them to be drawn;        */
/*                                               */
/*             0 to draw tokens normally, else a */
/*             pointer to a token which is not   */
/*             to have its contents redrawn,     */
/*             except as a selection indicator   */
/*             (see redraw_draw for more).       */
/*************************************************/

_kernel_oserror * browser_update(browser_data * b, WimpRedrawWindowBlock * r, int noback, HStream * nocontent)
{
  int more;

  r->window_handle = b->window_handle;

  wimp_update_window(r, &more);

  if (more) return redraw_draw(b, r, noback, nocontent);

  return NULL;
}

/*************************************************/
/* browser_update_token()                        */
/*                                               */
/* Redraws a given token, trying to minimise any */
/* flicker as the redraw is done.                */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the token to redraw;  */
/*                                               */
/*             The token address;                */
/*                                               */
/*             1 to not draw backgrounds, or 0   */
/*             to allow them to be drawn;        */
/*                                               */
/*             0 to draw tokens normally, else a */
/*             pointer to a token which is not   */
/*             to have its contents redrawn,     */
/*             except as a selection indicator   */
/*             (see redraw_draw for more).       */
/*                                               */
/* Returns:    Pointer to a _kernel_oserror      */
/*             struct, which is NULL unless the  */
/*             actual redraw call fails - so if  */
/*             the given token does not appear   */
/*             to be represented by any line,    */
/*             the routine fails silently.       */
/*************************************************/

_kernel_oserror * browser_update_token(browser_data * b, HStream * token, int noback, HStream * nocontent)
{
  token_path      * path  = NULL;
  int               first = -1;
  int               fchnk = -1;
  int               last  = -1;
  int               depth;
  reformat_cell   * cell  = NULL;
  _kernel_oserror * e     = NULL;
  int               x, y;

  /* Find the range of lines the token spans */

  depth = tokenutils_line_range(b, token, &first, &fchnk, &last, NULL, &path);

  /* Find out the x and y offset of the cell the */
  /* token lies in.                              */

  tokenutils_token_offset(b, path, &x, &y);

  /* If there are valid entries in the token_path structure, */
  /* need to find out what line list browser_update_token_r  */
  /* should be called on. Otherwise, it's the main line      */
  /* list.                                                   */

  cell = tokenutils_find_cell(b->cell, depth, path);

  if (path) free (path);

  if (cell)
  {
    e = browser_update_token_r(b, cell, token, first, fchnk, last, x, y, noback, nocontent);
  }
  else
  {
    e = browser_update_token_r(b, b->cell, token, first, fchnk, last, 0, 0, noback, nocontent);
  }

  return e;
}

/*************************************************/
/* browser_update_token_r()                      */
/*                                               */
/* Recursive back-end to browser_update_token.   */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the token to redraw;  */
/*                                               */
/*             Pointer to a reformat_cell struct */
/*             which holds the token or a table  */
/*             holding the token;                */
/*                                               */
/*             First line to check in the cell;  */
/*                                               */
/*             First chunk on the first line;    */
/*                                               */
/*             Last line to check in the cell;   */
/*                                               */
/*             The token address;                */
/*                                               */
/*             Cell origin x (window coords);    */
/*                                               */
/*             Cell origin y (window coords);    */
/*                                               */
/*             1 to not draw backgrounds, or 0   */
/*             to allow them to be drawn;        */
/*                                               */
/*             0 to draw tokens normally, else a */
/*             pointer to a token which is not   */
/*             to have its contents redrawn,     */
/*             except as a selection indicator   */
/*             (see redraw_draw for more).       */
/*                                               */
/* Returns:    As browser_update_token.          */
/*************************************************/

static _kernel_oserror * browser_update_token_r(browser_data * b, reformat_cell * cell, HStream * token,
                                                int first, int chunk, int last, int base_x, int base_y,
                                                int noback, HStream * nocontent)
{
  int                     l, widen;
  WimpRedrawWindowBlock   r;
  _kernel_oserror       * e;
  int                     x, y;

3015 3016
  if (!cell) cell = b->cell;

3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099
  /* Items that may have borders drawn around them need to have */
  /* the redraw area widened to cope with that.                 */

  if ((token->style & FORM) || (token->style & IMG)) widen = 4;
  else                                               widen = 0;

  if (first >= 0)
  {
    for (l = first; l <= last; l++)
    {
      /* Get the y coordinate of the bottom of the line into y */

      y = base_y + cell->ldata[l].y;

      /* Get the left hand x coordinate in x - the given token for the */
      /* first line, or the left hand edge for subsequent ones.        */

      if (l == first) x = base_x + redraw_token_x(b, cell, token, l);
      else            x = base_x + redraw_start_x(b, cell, cell->cdata[cell->ldata[l].chunks].t, l);

      /* Fill in the redraw block's visible area field */

      r.visible_area.xmin = x - 4;
      r.visible_area.ymin = y - widen;

      /* xmax is up to the last chunk's right hand edge for anything but */
      /* the last line, when we go as far as chunks that use this token. */

      if (l == last)
      {
        int w = 0, c, mc;

        /* For the first line, the chunk to start counting on will be  */
        /* defined by the given token; otherwise, it will be the first */
        /* one on the line.                                            */

        if (l == first) c = chunk;
        else            c = cell->ldata[l].chunks;

        /* Count up to either a chunk which doesn't use the given token */
        /* or is the last chunk on the line                             */

        mc = cell->ldata[l].chunks + cell->ldata[l].n;

        while (c < mc && cell->cdata[c].t == token) w += cell->cdata[c].w, c++;

        /* Convert to OS units and add to the left hand edge */

        convert_to_os(w, &w);

        r.visible_area.xmax = w + r.visible_area.xmin + 8;
      }
      else
      {
        /* Width of the last chunk */

        convert_to_os(cell->cdata[cell->ldata[l].chunks + cell->ldata[l].n - 1].w, &r.visible_area.xmax);

        /* Add the left hand edge of that chunk */

        r.visible_area.xmax += base_x + redraw_chunk_x(b, cell, cell->ldata[l].chunks + cell->ldata[l].n - 1, l) + 8;
      }

      /* ymax is the bottom line plus the line height */

      r.visible_area.ymax = r.visible_area.ymin + cell->ldata[l].h + widen * 2 - (widen == 0);

      e = browser_update(b, &r, noback, nocontent);
      if (e) return e;
    }
  }

  return NULL;
}

/*************************************************/
/* browser_update_bottom()                       */
/*                                               */
/* Redraws the browser window, from a given      */
/* work area y coordinate downwards.             */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the window to redraw; */
3100
/*                                               */
3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120
/*             The y coordinate to redraw from.  */
/*************************************************/

_kernel_oserror * browser_update_bottom(browser_data * b, int top_y)
{
  int                       more;
  _kernel_oserror         * e;
  WimpRedrawWindowBlock     r;

  #ifdef TRACE
    if (tl & (1u<<9)) Printf("\nbrowser_update_bottom: Called, -top_y = %d\n",-top_y);
  #endif

  /* Fill in the visible area and scroll info from the */
  /* GetWindowState call                               */

  r.window_handle = b->window_handle;

  /* Due to the way the veneer works and the structure is defined, */
  /* the visible_area here is actually the redraw block. The call  */
3121
  /* to wimp_update_window will exit eventually through the        */
3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155
  /* Wimp_GetRectangle SWI, which fills in 'r' properly.           */

  r.visible_area.xmin = -0x1000001; /* Big numbers to ensure the whole */
  r.visible_area.xmax = 0x1000000;  /* work area width is redrawn      */

  r.visible_area.ymin = -0x1000001; /* Go right to the bottom          */
  r.visible_area.ymax = top_y;

  /* The redraw loop itself */

  wimp_update_window(&r,&more);

  /* 'r' now holds information that more sensibly relates to its field names... */

  if (more)
  {
    e = redraw_draw(b, &r, 0, 0);

    if (e) return e;
  }

  #ifdef TRACE
    else if (tl & (1u<<9)) Printf("\nbrowser_update_bottom: Nothing to redraw\n");

    if (tl & (1u<<9)) Printf("\nbrowser_update_bottom: Successful\n");
  #endif

  return NULL;
}

/*************************************************/
/* browser_highlight_token()                     */
/*                                               */
/* Redraws a given token in a highlighted state; */
3156
/* see redraw_token_colour in Redraw.c for the   */
3157 3158 3159 3160
/* colour it will be drawn as.                   */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the token;            */
3161
/*                                               */
3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197
/*             The token address.                */
/*************************************************/

void browser_highlight_token(browser_data * b, HStream * token)
{
  int noback = 0;

  /* Ensure all current highlighting is cleared and remember  */
  /* that this token has been highlighted in the browser_data */
  /* structure.                                               */

  browser_clear_highlight(b, 1);
  b->highlight=token;

  /* If the token represents a link, that link may be made of several */
  /* tokens (e.g. a heading where font sizes were used to make the    */
  /* first letters bigger, or where there is a style change in the    */
  /* middle to highlight some search result for a Web search engine,  */
  /* say). So need to find the first of the list of tokens with the   */
  /* same anchor, and the last.                                       */

  if ((token->style & A) && token->anchor)
  {
    HStream * top = NULL;
    HStream * end = NULL;

    tokenutils_anchor_range(b, token, &top, &end);

    /* Now redraw between those two tokens */

    if (top && end)
    {
      while (top != end->next)
      {
        #ifndef ANTI_TWITTER
          if (
3198
               b->background_image < 0 ||
3199 3200 3201 3202
               (
                 (top->style & IMG) &&
                 (
                   (
3203
                     b->show_foreground               &&
3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237
                     image_token_plot_started(b, top)
                   )
                   || b->displayed == Display_External_Image
                 )
               )
             )
             noback = 1;

          else noback = 0;
        #endif

        b->highlight = top;
        browser_update_token(b, top, noback, 0);
        top = top->next;
      }
    }
    else
    {
      /* Something went wrong above, or there is only one token  */
      /* for this link - so use quicker redraw code.             */

      browser_update_token(b, token, noback, 0);
    }
  }

  /* If the token is not a link, just redraw it, as asked. */

  else browser_update_token(b, token, noback, 0);
}

/*************************************************/
/* browser_clear_highlight()                     */
/*                                               */
/* Removes a highlight shown using the           */
3238 3239
/* browser_highlight_token function, with an     */
/* optional delay before doing so.               */
3240 3241 3242
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the token;            */
3243
/*                                               */
3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271
/*             1 to wait first, else 0.          */
/*************************************************/

void browser_clear_highlight(browser_data * b, int wait)
{
  int noback = 0;

  /* If nothing's highlighted, don't need to do anything else! */

  if (b->highlight)
  {
    HStream * token;

    /* Clear the highlight, possibly waiting 20 centiseconds first. */

    token = b->highlight;
    b->highlight = NULL;

    if (wait)
    {
      int start_time, now;

      _swix(OS_ReadMonotonicTime, _OUT(0), &start_time);
      now = start_time;

      while (now - start_time <= 20) _swix(OS_ReadMonotonicTime, _OUT(0), &now);
    }

3272 3273 3274
    /* For details on this, see browser_highlight_token. Basically, */
    /* may need to redraw several tokens if the highlighted one was */
    /* a link, if they all represent the same link.                 */
3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290

    if ((token->style & A) && token->anchor)
    {
      HStream * top = NULL;
      HStream * end = NULL;

      tokenutils_anchor_range(b, token, &top, &end);

      /* Now redraw between those two tokens */

      if (top && end)
      {
        while (top != end->next)
        {
          #ifndef ANTI_TWITTER
            if (
3291
                 b->background_image < 0 ||
3292 3293 3294 3295
                 (
                   (top->style & IMG) &&
                   (
                     (
3296
                       b->show_foreground               &&
3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327
                       image_token_plot_started(b, top)
                     )
                     || b->displayed == Display_External_Image
                   )
                 )
               )
               noback = 1;

            else noback = 0;
          #endif

          browser_update_token(b, top, noback, 0);
          top = top->next;
        }
      }
      else
      {
        /* Something went wrong above, or there is only one token  */
        /* for this link - so use quicker redraw code.             */

        browser_update_token(b, token, noback, 0);
      }
    }
    else browser_update_token(b, token, noback, 0);
  }
}

/*************************************************/
/* browser_flash_token()                         */
/*                                               */
/* 'Flashes' a token, by highlighting it with    */
3328 3329
/* browser_highlight_token and then clearing     */
/* the highlight after a short delay.            */
3330 3331 3332
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the token;            */
3333
/*                                               */
3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349
/*             The token address.                */
/*************************************************/

void browser_flash_token(browser_data * b, HStream * token)
{
  browser_highlight_token(b, token);
  browser_clear_highlight(b, 1);
}

/*************************************************/
/* browser_select_token()                        */
/*                                               */
/* Redraws a given token in a selected state.    */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the token;            */
3350
/*                                               */
3351
/*             The token address;                */
3352
/*                                               */
3353 3354 3355 3356 3357 3358 3359 3360
/*             1 to scroll the window if needed  */
/*             to keep the token in the visible  */
/*             area, else 0.                     */
/*************************************************/

void browser_select_token(browser_data * b, HStream * token, int visible)
{
  int            noback   = 0;
3361
  browser_data * ancestor = utils_ancestor(b);
3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413

  /* Ensure any selected token is deselected, and remember */
  /* that this token has been marked as selected in the    */
  /* browse_data structure.                                */

  if (ancestor->selected_owner) browser_clear_selection(ancestor->selected_owner, 0);

  ancestor->selected       = token;
  ancestor->selected_owner = b;

  /* The selected frame is always the one with the highlighted token in it */

  if (ancestor->selected_frame != b)
  {
    frames_highlight_frame(b);

    ancestor->selected_frame = b;
  }

  /* If asked to, ensure the token is visible */

  if (visible)
  {
    WimpGetWindowStateBlock s;

    s.window_handle = b->window_handle;

    if (!wimp_get_window_state(&s)) browser_ensure_visible(b, &s, token);
  }

  /* If the token represents a link, that link may be made of several */
  /* tokens (e.g. a heading where font sizes were used to make the    */
  /* first letters bigger, or where there is a style change in the    */
  /* middle to highlight some search result for a Web search engine,  */
  /* say). So need to find the first of the list of tokens with the   */
  /* same anchor, and the last.                                       */

  if ((token->style & A) && token->anchor)
  {
    HStream * top = NULL;
    HStream * end = NULL;

    tokenutils_anchor_range(b, token, &top, &end);

    /* Now redraw between those two tokens */

    if (top && end)
    {
      while (top != end->next)
      {
        #ifndef ANTI_TWITTER
          if (
3414
               b->background_image < 0 ||
3415 3416 3417 3418
               (
                 (top->style & IMG) &&
                 (
                   (
3419
                     b->show_foreground               &&
3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433
                     image_token_plot_started(b, top)
                   )
                   || b->displayed == Display_External_Image
                 )
               )
             )
             noback = 1;

          else noback = 0;
        #endif

        b->selected = top;

        if (
3434
             b->show_foreground               &&
3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452
             (top->style & IMG)               &&
             (ISLINK(top))                    &&
             !top->maxlen                     && /* maxlen=0 means there's no border already there */
             image_token_plot_started(b, top)
           )
           browser_redraw_border(b, top);

        else browser_update_token(b, top, noback, 0);

        top = top->next;
      }
    }
    else
    {
      /* Something went wrong above, or there is only one token  */
      /* for this link - so use quicker redraw code.             */

      if (
3453
           b->show_foreground                 &&
3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475
           (token->style & IMG)               &&
           (ISLINK(token))                    &&
           !top->maxlen                       && /* maxlen=0 means there's no border already there */
           image_token_plot_started(b, token)
         )
         browser_redraw_border(b, token);

      else browser_update_token(b, token, noback, 0);
    }
  }

  /* If the token is not a link, just redraw it, as asked. */

  else browser_update_token(b, token, noback, 0);

  return;
}

/*************************************************/
/* browser_clear_selection()                     */
/*                                               */
/* Removes a selection shown using the           */
3476 3477
/* browser_select_token function, with an        */
/* delay before doing so.                        */
3478 3479 3480
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the token;            */
3481
/*                                               */
3482 3483 3484 3485 3486 3487
/*             1 to wait first, else 0.          */
/*************************************************/

void browser_clear_selection(browser_data * b, int wait)
{
  int            noback   = 0;
3488
  browser_data * ancestor = utils_ancestor(b);
3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516
  browser_data * owner;

  if (!ancestor) return;

  /* If nothing's selected, don't need to do anything else! */

  if (ancestor->selected)
  {
    HStream * token;

    /* Clear the selection, possibly waiting 20 centiseconds first. */

    token = ancestor->selected;
    owner = ancestor->selected_owner;

    ancestor->selected       = NULL;
    ancestor->selected_owner = NULL;

    if (wait)
    {
      int start_time, now;

      _swix(OS_ReadMonotonicTime, _OUT(0), &start_time);
      now = start_time;

      while (now - start_time <= 20) _swix(OS_ReadMonotonicTime, _OUT(0), &now);
    }

3517 3518 3519
    /* For details on this, see browser_select_token. Basically, may */
    /* need to redraw several tokens if the selected one was a link, */
    /* if they all represent the same link.                          */
3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542

    if ((token->style & A) && token->anchor)
    {
      HStream * top = NULL;
      HStream * end = NULL;

      tokenutils_anchor_range(owner, token, &top, &end);

      /* Now redraw between those two tokens */

      if (top && end)
      {
        while (top != end->next)
        {
          #ifndef ANTI_TWITTER
            /* If using anti-twitter redraws, always redraw the background.   */
            /* Otherwise, redraw it unless this is a text item (well, neither */
            /* an image nor a form element) as for text items, can just       */
            /* replot the text.                                               */

            if (
                 !(top->style & IMG)  &&
                 !(top->style & FORM) &&
3543
                 owner->background_image < 0
3544 3545 3546 3547 3548 3549 3550 3551 3552 3553
               )
               noback = 1;

            else noback = 0;
          #endif

          /* Images which are links and are having only the selection marker */
          /* redrawn go through a special routine.                           */

          if (
3554
               owner->show_foreground               &&
3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572
               (top->style & IMG)                   &&
               (ISLINK(top))                        &&
               !top->maxlen                         && /* maxlen=0 means there's no border already there */
               image_token_plot_started(owner, top)
             )
             browser_redraw_border(owner, top);

          else browser_update_token(owner, top, noback, 0);

          top = top->next;
        }
      }
      else
      {
        /* Something went wrong above, or there is only one token  */
        /* for this link - so use quicker redraw code.             */

        if (
3573
             owner->show_foreground                 &&
3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606
             (token->style & IMG)                   &&
             (ISLINK(token))                        &&
             !top->maxlen                           && /* maxlen=0 means there's no border already there */
             image_token_plot_started(owner, token)
           )
           browser_redraw_border(owner, token);

        else browser_update_token(owner, token, noback, 0);
      }
    }
    else browser_update_token(owner, token, noback, 0);
  }

  return;
}

/*************************************************/
/* browser_show_token()                          */
/*                                               */
/* Shows a given token at the top of the browser */
/* window.                                       */
/*                                               */
/* If the token is near the bottom of the page,  */
/* then the page extent is increased so that the */
/* window may still be scrolled to show the      */
/* token at the top. Despite introducing some    */
/* dead space at the page base, it gets very     */
/* confusing - particularly with Find functions  */
/* - to not be able to rely on the token being   */
/* actually moved to the top of the window.      */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the window;           */
3607
/*                                               */
3608
/*             The token address;                */
3609
/*                                               */
3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625
/*             Offset into the token data which  */
/*             the chunk representing the token  */
/*             must straddle - i.e., it must     */
/*             start at less than or equal to    */
/*             this offset and end at greater    */
/*             than it.                          */
/*                                               */
/* Returns:    1 for success, 0 for failure.     */
/*************************************************/

int browser_show_token(browser_data * b, HStream * token, int offset)
{
  WimpGetWindowStateBlock   s;
  token_path              * path = NULL;
  reformat_cell           * cell = NULL;
  int                       orx, ory;
3626
  int                       c, l, topy, htop, fy;
3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665
  int                       found;
  int                       depth;

  if (!token) return 0;

  /* Find the range of lines the token spans */

  depth = tokenutils_line_range(b, token, &l, NULL, NULL, NULL, &path);

  /* Find out the x and y offset of the cell the */
  /* token lies in.                              */

  tokenutils_token_offset(b, path, &orx, &ory);

  /* If there are valid entries in the token_path structure, */
  /* need to find out what line list browser_update_token_r  */
  /* should be called on. Otherwise, it's the main line      */
  /* list.                                                   */

  cell = tokenutils_find_cell(b->cell, depth, path);

  if (path) free (path);

  if (!cell)
  {
    cell = b->cell;
    orx  = 0;
    ory  = 0;
  }

  /* Can't do anything if there are no lines in the cell */

  if (!cell->nlines || !cell->ldata || !cell->cdata) return 0;

  /* Start by checking the token isn't already at the top of the window */

  s.window_handle = b->window_handle;
  if (wimp_get_window_state(&s)) return 0;

3666 3667
  if (!controls.swap_bars) htop = toolbars_button_height(b) + toolbars_url_height(b);
  else                     htop = toolbars_status_height(b);
3668 3669 3670 3671

  if (htop) htop += wimpt_dy();

  topy = s.yscroll - htop;
3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739

  l = 0;
  while (l < cell->nlines && cell->ldata[l].y + cell->ldata[l].h + ory > topy) l++;

  if (l < cell->nlines)
  {
    for (c = 0; c < cell->ldata[l].n; c++);
    {
      /* If the chunk represents the given token,                          */
      /* its data offset is less than or equal to the offset specified, or */
      /* its data offset plus length is greater than the offset specified, */
      /* then we've found the token, it's already at the top so just exit  */
      /* but flag that the routine was successful.                         */

      if (
           cell->cdata[cell->ldata[l].chunks + c].t == token &&
           cell->cdata[cell->ldata[l].chunks + c].o <= offset &&
           cell->cdata[cell->ldata[l].chunks + c].l + cell->cdata[cell->ldata[l].chunks + c].o > offset
         )
         return 1;
    }
  }

  /* It wasn't at the top, so need to do a bit more work. */

  l  = topy  = 0;
  fy = found = 0;

  /* Loop through all the lines */

  while (l < cell->nlines && !found)
  {
    /* Set y to the coordinate of the topmost part of this line (hence the '-1') */

    topy = cell->ldata[l].y + cell->ldata[l].h + ory - 1;

    /* Loop through this line's chunks */

    for (c = 0; c < cell->ldata[l].n && !found; c++)
    {
      /* Again, if the chunk represents the given token,                         */
      /* has an offset below or equal to the given offset, or                    */
      /* either has a data offset plus length greater than the offset specified  */
      /* or has zero data length,                                                */
      /* then flag that we've found the token and get the y coordinate of the    */
      /* top of this line into fy.                                               */

      if (
           cell->cdata[cell->ldata[l].chunks + c].t == token &&
           cell->cdata[cell->ldata[l].chunks + c].o <= offset &&
           (
             cell->cdata[cell->ldata[l].chunks + c].l + cell->cdata[cell->ldata[l].chunks + c].o > offset ||
             cell->cdata[cell->ldata[l].chunks + c].l == 0
           )
         )
         fy = topy, found = 1;
    }

    l++;
  }

  /* If found = 0, the loop didn't find the requested token, */
  /* so exit, flagging failure.                              */

  if (!found) return 0;

  /* Jump to the scroll position recorded in fy. */

3740
  s.yscroll = fy + htop;
3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775

  /* Is the window extent great enough? */

  {
    BBox extent;
    int  bottom_coord;

    window_get_extent(0, b->self_id, &extent);

    bottom_coord = s.yscroll - (s.visible_area.ymax - s.visible_area.ymin);

    /* If the extent is not great enough, increase it */

    if (extent.ymin >= bottom_coord)
    {
      extent.ymin = bottom_coord;

      if (window_set_extent(0, b->self_id, &extent)) return 0;
    }
  }

  if (wimp_open_window((WimpOpenWindowBlock *) &s)) return 0;

  return 1;
}

/*************************************************/
/* browser_ensure_visible()                      */
/*                                               */
/* Ensures that a given token is wholly visible  */
/* in the browser window, scrolling down or up   */
/* if needed.                                    */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the token;            */
3776
/*                                               */
3777 3778
/*             A WimpGetWindowStateBlock pointer */
/*             for the browser window;           */
3779
/*                                               */
3780 3781 3782 3783 3784 3785 3786 3787 3788 3789
/*             Pointer to the token itself.      */
/*                                               */
/* Returns:    1 if the window was scrolled,     */
/*             else 0. If there is an error      */
/*             internally, 2 is returned.        */
/*************************************************/

int browser_ensure_visible(browser_data * b, WimpGetWindowStateBlock * state, HStream * token)
{
  int             lfir, llas, ltop, lbot;
3790
  int             ytop, ybot, htop, hbot;
3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858
  HStream       * ttop;
  HStream       * tend;
  reformat_cell * cell_s  = NULL, * cell_e = NULL;
  token_path    * path_s  = NULL, * path_e = NULL;
  int             depth_s = 0;
  int             depth_e = 0;
  int             orx_s, ory_s;
  int             orx_e, ory_e;

  if (!token) return 0;

  /* Find the first and last lines to include this token range */

  tokenutils_anchor_range(b, token, &ttop, &tend);

  if (!ttop || !tend) depth_s = tokenutils_line_range(b, token, &lfir, NULL, &llas, NULL, &path_s);
  else
  {
    depth_s = tokenutils_line_range(b, ttop, &lfir, NULL, NULL, NULL, &path_s);
    depth_e = tokenutils_line_range(b, tend, NULL, NULL, &llas, NULL, &path_e);
  }

  /* If the first line can't be found, can't proceed */

  if (lfir < 0) return 0;

  /* Find the start/end origin offset */

  tokenutils_token_offset(b, path_s, &orx_s, &ory_s);
  tokenutils_token_offset(b, path_s, &orx_e, &ory_e);

  /* Find the start/end cell (*should* be the same...) */

  cell_s = tokenutils_find_cell(b->cell, depth_s, path_s);
  cell_e = tokenutils_find_cell(b->cell, depth_e, path_e);

  if (!cell_s)
  {
    cell_s = b->cell;
    orx_s  = 0;
    ory_s  = 0;
  }

  if (!cell_e)
  {
    cell_e = b->cell;
    orx_e  = 0;
    ory_e  = 0;
  }

  /* Free the token_path arrays */

  if (path_s) free (path_s);
  if (path_e) free (path_e);

  /* If the last line couldn't be found, the token only */
  /* spans the one line.                                */

  if (llas < 0) llas = lfir, cell_e = cell_s;

  /* Work out the top and bottom y coordinates that the token spans */

  ltop = ory_s + cell_s->ldata[lfir].y + cell_s->ldata[lfir].h - 1; /* '-1' as we want this to be an inclusive coord */
  lbot = ory_e + cell_e->ldata[llas].y;

  /* Work out where the visible page region starts and ends */
  /* (affected by window scrolling and toolbar presence).   */

3859
  if (!controls.swap_bars)
3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874
  {
    htop = toolbars_button_height(b) + toolbars_url_height(b);
    hbot = toolbars_status_height(b);
  }
  else
  {
    htop = toolbars_status_height(b);
    hbot = toolbars_button_height(b) + toolbars_url_height(b);
  }

  if (htop) htop += wimpt_dy();
  if (hbot) hbot += wimpt_dy();

  ytop = state->yscroll - htop;
  ybot = state->yscroll - (state->visible_area.ymax - state->visible_area.ymin) + hbot;
3875 3876 3877 3878 3879 3880 3881 3882

  /* It would be possible for the token to be big, or the window to be small, */
  /* and both the bottom and top of it be off the window. In this case, take  */
  /* the case where the top is not visible over the case where the bottom is  */
  /* not visible.                                                             */

  if (ltop > ytop)
  {
3883
    state->yscroll = ltop + htop + 8; /* '+8' = aesthetics */
3884 3885 3886 3887 3888
    if (wimp_open_window((WimpOpenWindowBlock *) state)) return 2;
    else return 1;
  }
  else if (lbot < ybot)
  {
3889
    state->yscroll = lbot + (state->visible_area.ymax - state->visible_area.ymin) - hbot - 8; /* '-8 = aesthetics */
3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930
    if (wimp_open_window((WimpOpenWindowBlock *) state)) return 2;
    else return 1;
  }

  return 0;
}

/*************************************************/
/* browser_check_visible()                       */
/*                                               */
/* Checks to see if a given token is wholly or   */
/* partially visible in a given browser window.  */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the token;            */
/*                                               */
/*             A WimpGetWindowStateBlock pointer */
/*             for the browser window (may be    */
/*             NULL);                            */
/*                                               */
/*             Pointer to the token itself.      */
/*                                               */
/* Returns:    1 if the token is wholly visible, */
/*             2 if it is partially visible,     */
/*             else 0. If there is an error      */
/*             internally, 0 is returned.        */
/*                                               */
/* Assumes:    If the WimpGetWindowStateBlock    */
/*             pointer is NULL, the function     */
/*             will find the information out     */
/*             itself. The block pointer can be  */
/*             passed in as calling functions    */
/*             may well already have this block  */
/*             available, so it makes sense to   */
/*             avoid finding it out again.       */
/*************************************************/

int browser_check_visible(browser_data * b, WimpGetWindowStateBlock * state, HStream * token)
{
  WimpGetWindowStateBlock   s;
  int                       lfir, llas, ltop, lbot;
3931
  int                       ytop, ybot, htop, hbot;
3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004
  HStream                 * ttop;
  HStream                 * tend;
  reformat_cell           * cell_s  = NULL, * cell_e = NULL;
  token_path              * path_s  = NULL, * path_e = NULL;
  int                       depth_s = 0;
  int                       depth_e = 0;
  int                       orx_s, ory_s;
  int                       orx_e, ory_e;

  if (!token) return 0;

  /* Get the window state, if necessary */

  if (!state)
  {
    s.window_handle = b->window_handle;
    if (wimp_get_window_state(&s)) return 0;

    state = &s;
  }

  /* Find the first and last lines to include this token range */

  tokenutils_anchor_range(b, token, &ttop, &tend);

  if (!ttop || !tend) depth_s = tokenutils_line_range(b, token, &lfir, NULL, &llas, NULL, &path_s);
  else
  {
    depth_s = tokenutils_line_range(b, ttop, &lfir, NULL, NULL, NULL, &path_s);
    depth_e = tokenutils_line_range(b, tend, NULL, NULL, &llas, NULL, &path_e);
  }

  /* If the first line can't be found, can't proceed */

  if (lfir < 0) return 0;

  /* Find the start/end origin offset */

  tokenutils_token_offset(b, path_s, &orx_s, &ory_s);
  tokenutils_token_offset(b, path_s, &orx_e, &ory_e);

  /* Find the start/end cell (*should* be the same...) */

  cell_s = tokenutils_find_cell(b->cell, depth_s, path_s);
  cell_e = tokenutils_find_cell(b->cell, depth_e, path_e);

  if (!cell_s)
  {
    cell_s = b->cell;
    orx_s  = 0;
    ory_s  = 0;
  }

  if (!cell_e)
  {
    cell_e = b->cell;
    orx_e  = 0;
    ory_e  = 0;
  }

  /* Free the token_path arrays */

  if (path_s) free (path_s);
  if (path_e) free (path_e);

  /* If the last line couldn't be found, the token only */
  /* spans the one line.                                */

  if (llas < 0) llas = lfir, cell_e = cell_s;

  /* Work out where the visible page region starts and ends */
  /* (affected by window scrolling and toolbar presence).   */

4005
  if (!controls.swap_bars)
4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020
  {
    htop = toolbars_button_height(b) + toolbars_url_height(b);
    hbot = toolbars_status_height(b);
  }
  else
  {
    htop = toolbars_status_height(b);
    hbot = toolbars_button_height(b) + toolbars_url_height(b);
  }

  if (htop) htop += wimpt_dy();
  if (hbot) hbot += wimpt_dy();

  ytop = state->yscroll - htop;
  ybot = state->yscroll - (state->visible_area.ymax - state->visible_area.ymin) + hbot;
4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056

  /* Work out the top and bottom y coordinates that the token spans */

  ltop = ory_s + cell_s->ldata[lfir].y + cell_s->ldata[lfir].h - 1; /* '-1' as we want this to be an inclusive coord */
  lbot = ory_e + cell_e->ldata[llas].y;

  /* Is the token fully visible? */

  if (ltop <= ytop && lbot >= ybot) return 1;

  /* Work out the top and bottom to check for partial visibility */

  ltop = ory_s + cell_s->ldata[lfir].y;
  lbot = ory_e + cell_e->ldata[llas].y + cell_e->ldata[llas].h - 1;

  /* Is the token partially visible? */

  if (ltop <= ytop && lbot >= ybot) return 2;

  /* The token is not in the visible area */

  return 0;
}

/*************************************************/
/* browser_show_named_anchor()                   */
/*                                               */
/* Given an anchor name, ensures that the window */
/* is scrolled to show the token associated      */
/* with that anchor name. If no match can be     */
/* found between the given name and the names of */
/* the tokens, the routine gives an appropriate  */
/* error message back to the user.               */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the window;           */
4057
/*                                               */
4058 4059 4060 4061 4062
/*             Pointer to the anchor name.       */
/*************************************************/

void browser_show_named_anchor(browser_data * b, char * anchor)
{
4063
  char name[Limits_NamedAnchor];
4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125

  memset(name, 0, sizeof(name));
  strncpy(name, anchor, sizeof(name) - 2);

  if (
       !browser_show_token(b,
                           fetch_find_anchor_token(b, name),
                           0)
     )
  {
    /* Some broken pages specify the names with a hash in front, */
    /* and still expect them to work. Try to deal with that.     */

    memmove(name + 1, name, strlen(name));
    name[0] = '#';

    if (
         !browser_show_token(b,
                             fetch_find_anchor_token(b, name),
                             0)
       )
    {
      erb.errnum = Utils_Error_Custom_Message;

      /* Give a different message if still fetching */

      if (fetch_fetching(b))
      {
        StrNCpy0(erb.errmess,
                 lookup_token("NoLabelF:The label '%0' cannot be found, but the page is still fetching - try again when the page fetch has finished.",
                              0,
                              anchor))
      }
      else
      {
        StrNCpy0(erb.errmess,
                 lookup_token("NoLabel:The label '%0' cannot be found on this page.",
                              0,
                              anchor))
      }

      /* Report the error and continue from here */

      show_error_ret(&erb);
    }
  }

  return;
}

/*************************************************/
/* browser_display_local_reference()             */
/*                                               */
/* Checks a given URL against a given base URL,  */
/* and if it contains a local reference (i.e.    */
/* has '#<name>' at the end) but otherwise       */
/* matches the base URL, will try to find and    */
/* subsequently display the reference on the     */
/* page.                                         */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the page;             */
4126
/*                                               */
4127 4128
/*             Pointer to the requested URL      */
/*             which may contain the reference;  */
4129
/*                                               */
4130 4131 4132 4133 4134
/*             Pointer to the base URL against   */
/*             which the first is compared.      */
/*                                               */
/* Returns:    1 if the reference is to be shown */
/*             (though if the reference may not  */
4135
/*             actually be found on the page) or */
4136
/*             0 if the reference will not be    */
4137 4138
/*             shown, because the base URL does  */
/*             not match the requested URL.      */
4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170
/*                                               */
/* Assumes:    Either pointer may be NULL,       */
/*             though this will ensure that 0 is */
/*             returned and thus it's not much   */
/*             use giving null pointers.         */
/*             The application must be able to   */
/*             write to the URL data.            */
/*************************************************/

int browser_display_local_reference(browser_data * b, char * url_requested, char * url_current)
{
  char * p1, * p2;

  /* Can't do anything if null pointers are given */

  if (!url_requested || !url_current) return 0;

  /* Can't do anything if the requested URL doesn't have a local reference in it */

  p1 = fetch_find_name_tag(url_requested);
  if (!p1) return 0;

  /* If the base URL contains a reference then point p2 to the first */
  /* character after the main URL (i.e. the '#'), else point p2 to   */
  /* the end of the string.                                          */

  p2 = fetch_find_name_tag(url_current);
  if (!p2) p2 = strchr(url_current, 0);

  /* If the two URLs don't match, return 0 */

  if (
4171 4172 4173
       utils_strncasecmp(url_requested,
                         url_current,
                         (int) p1 - (int) url_requested)
4174 4175 4176 4177 4178 4179 4180 4181 4182 4183
     )
     return 0;

  /* Otherwise, show the reference */

  browser_show_named_anchor(b, p1 + 1);

  return 1;
}

4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205
/*************************************************/
/* browser_set_look()                            */
/*                                               */
/* Sets the 'look' of a browser window - i.e.    */
/* underlined links, using document or default   */
/* colour schemes, etc.                          */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             to alter (any in a frameset will  */
/*             do, as all frames are updated);   */
/*                                               */
/*             Object ID of the item generating  */
/*             this change, if appropriate (or   */
/*             zero if not);                     */
/*                                               */
/*             1 to underline links, 0 not to,   */
/*             -1 to not change this state;      */
/*                                               */
/*             1 to use document colours, 0 to   */
/*             use defaults, -1 to not change    */
/*             this state;                       */
/*                                               */
4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216
/*             1 to show foreground images, 0    */
/*             not to (any pending image fetches */
/*             are started up again if 1 is      */
/*             given), or -1 to not change this  */
/*             state;                            */
/*                                               */
/*             1 to show background images, 0    */
/*             not to (any pending image fetch   */
/*             for this is started if 1 is       */
/*             given), or -1 to not change this  */
/*             state.                            */
4217 4218
/*************************************************/

4219 4220
_kernel_oserror * browser_set_look(browser_data * b, ObjectId source, int underline_links,
                                   int use_source_cols, int show_foreground, int show_background)
4221 4222 4223 4224 4225 4226 4227 4228 4229
{
  /* Check the browser_data pointer is valid */

  if (!b || !is_known_browser(b)) return NULL;

  /* Find the ancestor and call the recursive back-end with it */

  return browser_set_look_r(utils_ancestor(b),
                            source,
4230 4231 4232 4233
                            underline_links,
                            use_source_cols,
                            show_foreground,
                            show_background);
4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251
}

/*************************************************/
/* browser_set_look_r()                          */
/*                                               */
/* Recursive back-end to browser_set_look.       */
/*                                               */
/* Note that if open, the Print Style dialogue   */
/* is updated with these changes, if they apply  */
/* to the browser to which it is relevant (if    */
/* any).                                         */
/*                                               */
/* Parameters: As for browser_set_look. All      */
/*             children of the browser_data      */
/*             struct, along with that given     */
/*             struct, will be updated.          */
/*************************************************/

4252 4253
static _kernel_oserror * browser_set_look_r(browser_data * b, ObjectId source, int underline_links,
                                            int use_source_cols, int show_foreground, int show_background)
4254
{
4255 4256
  int child;
  int redraw = 0;
4257 4258 4259 4260 4261 4262 4263 4264 4265

  /* Scan the child tree */

  if (b->nchildren)
  {
    for (child = 0; child < b->nchildren; child ++)
    {
      RetError(browser_set_look_r(b->children[child],
                                  source,
4266 4267 4268 4269
                                  underline_links,
                                  use_source_cols,
                                  show_foreground,
                                  show_background));
4270 4271 4272 4273 4274 4275
    }
  }

  /* Work through the four options, updating the browser if they */
  /* seem to have changed.                                       */

4276
  if (underline_links >= 0 && underline_links != b->underline_links)
4277
  {
4278 4279
    b->underline_links = underline_links;
    redraw             = 1;
4280 4281
  }

4282
  if (use_source_cols >= 0 && use_source_cols != b->use_source_cols)
4283
  {
4284 4285
    b->use_source_cols = use_source_cols;
    redraw             = 1;
4286 4287
  }

4288
  if (show_foreground >= 0 && show_foreground != b->show_foreground)
4289
  {
4290 4291
    b->show_foreground = show_foreground;
    redraw             = 1;
4292 4293 4294

    /* Restart fetches if required */

4295
    if (show_foreground) image_restart_fetches(b, 1, 0);
4296 4297
  }

4298
  if (show_background >= 0 && show_background != b->show_background)
4299
  {
4300
    b->show_background = show_background;
4301 4302 4303

    /* No need to redraw if there's no background image to show or remove */

4304
    redraw = b->background_image >= 0 ? 1 : 0;
4305 4306 4307

    /* Again restart fetches if required */

4308
    if (show_background) image_restart_fetches(b, 0, 1);
4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333
  }

  /* If required, redraw the browser to reflect the changes */

  if (redraw)
  {
    WimpGetWindowStateBlock s;

    s.window_handle = b->window_handle;
    RetError(wimp_get_window_state(&s));

    coords_box_toworkarea(&s.visible_area, (WimpRedrawWindowBlock *) &s);

    RetError(wimp_force_redraw(b->window_handle,
                               s.visible_area.xmin,
                               s.visible_area.ymin,
                               s.visible_area.xmax,
                               s.visible_area.ymax));
  }

  /* If there's a Print Style dialogue opened for this browser, the */
  /* following call will update it appropriately.                   */

  return printstyle_set_look(source,
                             b->self_id,
4334 4335 4336 4337
                             underline_links,
                             use_source_cols,
                             show_foreground,
                             show_background);
4338 4339
}

4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375
/*************************************************/
/* browser_give_general_focus()                  */
/*                                               */
/* Places the caret in a browser window, but in  */
/* no particular icon - it will appear in the    */
/* URL bar if one is visible, else the main      */
/* window will gain the input focus but the      */
/* caret will not be visible.                    */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the window.           */
/*************************************************/

_kernel_oserror * browser_give_general_focus(browser_data * b)
{
  _kernel_oserror * e;
  ObjectId          t;

  if (!b) return NULL;

  /* Try to put the caret in a form - exit with NULL if it succeeds */

  if (form_give_focus(b)) return NULL;

  /* OK, put the focus in the URL bar, or if not there, in the main */
  /* window (the caret will be invisible in this latter case).      */
  /*                                                                */
  /* In the special case of the URL bar and status display being    */
  /* merged, the focus is given to the URL writable only if it is   */
  /* currently visible in the window.                               */

  t = toolbars_get_upper(b);

  if (
       t          &&
       b->url_bar &&
4376
       !gadget_hidden(t, URLBarWrit)
4377
     )
4378
     return gadget_set_focus(0, t, URLBarWrit);
4379 4380 4381

  else
  {
4382
    browser_data * ancestor = utils_ancestor(b);
4383 4384 4385 4386 4387 4388 4389 4390

    /* Only give input focus to the ancestor */

    e = wimp_set_caret_position(ancestor->window_handle, -1, 0, 0, -1, -1);
    if (e) return e;

    /* May need to select a new object */

4391
    if (!ancestor->selected && choices.keyboard_ctrl)
4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411
    {
      browser_data * next = frames_find_another_frame(ancestor, 0);

      if (next)
      {
        ancestor->selected = browser_find_first_selectable(next, NULL, 0);

        if (ancestor->selected)
        {
          ancestor->selected_owner = next;
          ancestor->selected_frame = b;

          frames_highlight_frame(b);
        }
      }
    }
  }

  return NULL;
}
4412 4413 4414 4415 4416 4417 4418

/*************************************************/
/* browser_inherit()                             */
/*                                               */
/* Makes a given child browser inherit some of   */
/* the characteristics of a given parent.        */
/*                                               */
4419 4420 4421
/* The post_data field is handled specially.     */
/* Please see browser_inherit_post_data for      */
/* details.                                      */
4422
/*                                               */
4423 4424 4425 4426 4427 4428 4429 4430 4431
/* Parameters: Pointer to a browser_data struct  */
/*             representing the parent;          */
/*                                               */
/*             Pointer to a browser_data struct  */
/*             representing the child.           */
/*************************************************/

_kernel_oserror * browser_inherit(browser_data * parent, browser_data * child)
{
4432
  /* Be very cautious! */
4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446

  if (!is_known_browser(parent) || !is_known_browser(child)) return NULL;

  /* The local History */

  RetError(history_inherit(parent, child));

  /* Some display flags */

  child->underline_links = parent->underline_links;
  child->show_foreground = parent->show_foreground;
  child->show_background = parent->show_background;
  child->use_source_cols = parent->use_source_cols;

4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494
  /* To finish, deal with post_data information */

  return browser_inherit_post_data(parent, child);
}

/*************************************************/
/* browser_inherit_post_data()                   */
/*                                               */
/* Part of browser_inherit which needs to be     */
/* used sometimes for windows that already exist */
/* but are being targetted by a POST form        */
/* submission.                                   */
/*                                               */
/* If there is a flex block attached through the */
/* post_data field of the parent, the contents   */
/* will be copied into a flex block attached to  */
/* the child's post_data field and then *freed*  */
/* in the parent.                                */
/*                                               */
/* This is because at present, the only time the */
/* parent will have such a block is if either an */
/* a button that submits a POST request was      */
/* clicked upon, or if that button targets       */
/* another browser window. In this case, you     */
/* don't want to leave the data attached to the  */
/* parent or the next fetch it does will         */
/* erroneously be sent as POST itself...!        */
/*                                               */
/* NB If the child which is to receive the flex  */
/* data from the parent (assuming the parent has */
/* any to give!) already had stuff attached to   */
/* post_data, this will obviously be freed       */
/* first.                                        */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             representing the parent;          */
/*                                               */
/*             Pointer to a browser_data struct  */
/*             representing the child.           */
/*************************************************/

_kernel_oserror * browser_inherit_post_data(browser_data * parent, browser_data * child)
{
  /* Once more, because we may be called from anywhere, */
  /* be very cautious.                                  */

  if (!is_known_browser(parent) || !is_known_browser(child)) return NULL;

4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505
  /* If the parent had extra data, we should carry that forward (this */
  /* may have been an adjust-click on a Submit button for a POST form */
  /* for example).                                                    */

  if (parent->post_data)
  {
    int success;
    int size = flex_size(&parent->post_data);

    if (size)
    {
4506 4507 4508 4509
      /* Clear any data in the child */

      if (child->post_data) flex_free(&child->post_data);

4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539
      /* Allocate an appropriate chunk of memory in the child */

      success = flex_alloc(&child->post_data, size);

      /* If the allocation succeeded, so switch off flex budging and */
      /* copy the block contents to the child browser                */

      if (success)
      {
        int oldstate = flex_set_budge(0);

        memcpy(child->post_data, parent->post_data, size);

        /* Restore flex's budge state */

        flex_set_budge(oldstate);
      }

      /* At this point, whether the new flex allocation worked */
      /* or not, free the parent's block (see the comments at  */
      /* the top of the function for more details).            */

      flex_free(&parent->post_data);

      /* If the flex allocation failed, return an error */

      if (!success) return make_no_fetch_memory_error(16);
    }
  }

4540 4541
  return NULL;
}