Redraw 107 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   : Redraw.c                               */
17 18 19
/*                                                 */
/* Purpose: Redraw functions for the browser.      */
/*                                                 */
20
/* Author : A.D.Hodgkinson                         */
21 22
/*                                                 */
/* History: 29-Nov-96: Created.                    */
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/***************************************************/

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

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

#include "HTMLLib.h" /* HTML library API, Which will include html2_ext.h, tags.h and struct.h */

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

#include "svcprint.h"
#include "Global.h"
#include "FromROSLib.h"
#include "MiscDefs.h"
#include "Utils.h"

#include "Browser.h"
45 46
#include "ChoiceDefs.h"
#include "CSIM.h"
47
#include "Fetch.h" /* (Which itself includes URLstat.h) */
48 49 50 51
#include "FontManage.h"
#include "Forms.h"
#include "Frames.h"
#include "Images.h"
52
#include "Object.h"
53 54
#include "Printing.h" /* Only for the PrintSplitFraction definition at present */
#include "PrintStyle.h"
55 56 57 58 59
#include "Reformat.h"
#include "Tables.h"
#include "TokenUtils.h"
#include "Toolbars.h"

60
#ifdef UNIFONT
61
  #include "Unicode/iso10646.h"
62
  #include "Unifont.h"
63 64
#endif

65 66 67 68
#include "Redraw.h"

/* Static function prototypes */

69 70 71 72
static void redraw_input_field (browser_data * b, HStream * t, BBox * box, int colour, int menu);
static void redraw_button      (browser_data * b, HStream * t, BBox * box, int in);
static void redraw_switch      (browser_data * b, HStream * t, int x, int y, char * spr, WimpRedrawWindowBlock * r);
static void redraw_bullet      (int x, int y, int bullet, WimpRedrawWindowBlock * r);
73

74 75 76 77
/* Used for printing */

static int use_noback = 0;

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
/* Internal recursive functions. These do the actual work that their */
/* similarly named and oft externally visible counterparts claim to  */
/* do, but are part of the recursive code needed for e.g. tables.    */

static _kernel_oserror * redraw_draw_r (int toplevel, int xorg, int yorg, browser_data * b, reformat_cell * d, WimpRedrawWindowBlock * r, int noback,  HStream * nocontent);

/*************************************************/
/* redraw_header()                               */
/*                                               */
/* Returns the header type (<H1>, <H2> etc. as   */
/* a number from 1 - 7) extracted from the flags */
/* bits of an HStream structure.                 */
/*                                               */
/* Parameters: The flags word.                   */
/*************************************************/

int redraw_header(unsigned int flags)
{
  /* H_MASK and H_SHIFT are defined in HTMLLib:tags.h */

  flags &= H_MASK;
  flags = (flags >> H_SHIFT);

  return flags;
}

/*************************************************/
/* redraw_backcol()                              */
/*                                               */
/* Small function to return the actual           */
/* background colour of a browser window.        */
/*                                               */
/* Parameters: A pointer to a browser_data       */
/*             structure associated with the     */
/*             window in question.               */
/*************************************************/

int redraw_backcol(browser_data * b)
{
  /* If the background colour isn't set or the Choices say  */
  /* to override document colours, return the default; else */
  /* return the document-specified background colour.       */

  #ifdef TRACE
122
    if (tl & (1u<<9)) Printf("redraw_backcol: Called with choices.background_colour = %p\n",(void *) choices.background_colour);
123 124
  #endif

125
  return (((b->background_colour == -1) || (!b->use_source_cols)) ? (choices.background_colour) : (b->background_colour));
126 127 128 129 130 131 132 133 134 135
}

/*************************************************/
/* redraw_background_colour()                    */
/*                                               */
/* Returns a background colour hint for text of  */
/* a given foreground colour.                    */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             holding the background info;      */
136
/*                                               */
137 138 139 140 141 142 143
/*             A foreground colour (as a palette */
/*             entry, for more details see       */
/*             redraw_set_colour()).             */
/*                                               */
/* Returns:    A background colour.              */
/*************************************************/

144
int redraw_background_colour(browser_data * b, int foregroundcolour)
145
{
146
  if (!b->show_background) return redraw_backcol(b);
147

148
  switch (b->antialias_colour)
149 150
  {
    /* Defeat anti-aliasing by giving the same background colour */
151
    /* as the foreground if antialias_colour is -1.              */
152 153 154

    case -1: return foregroundcolour;

155 156 157
    /* If antialias_colour is -2, return the background colour from the */
    /* browser_data struct unless this is -1, in which case return the  */
    /* foreground colour again.                                         */
158

159
    case -2: return (b->background_colour == -1 ? foregroundcolour : b->background_colour);
160 161 162 163 164 165
  }

  /* Return either the default background colour or the anti-alias */
  /* colour, depending on whether document colour overriding is    */
  /* on or off respectively.                                       */

166
  return (!b->use_source_cols ? choices.background_colour : b->antialias_colour);
167 168 169 170 171 172 173 174 175 176 177
}

/*************************************************/
/* redraw_token_colour()                         */
/*                                               */
/* Returns the colour to plot a token in, on the */
/* assumption that it contains some sort of text */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             with details of the token stream  */
/*             within it;                        */
178
/*                                               */
179 180 181 182 183 184 185 186 187
/*             Pointer to the token.             */
/*                                               */
/* Returns:    The colour to plot in, as a       */
/*             palette entry (see                */
/*             redraw_set_colour()).             */
/*************************************************/

int redraw_token_colour(browser_data * b, HStream * t)
{
188
  if (t->tagno == TAG_INPUT || t->tagno == TAG_TEXTAREA || t->tagno == TAG_SELECT) return 0;
189

190 191 192 193 194
  /* If we're printing, see if the Print Style dictates that any */
  /* text should be black.                                       */

  if (printing)
  {
195 196
    if (printstyle_always_use_black())                  return Redraw_Colour_Black;
    if (printstyle_black_no_background() && use_noback) return Redraw_Colour_Black;
197 198
  }

199 200 201 202 203 204 205
  /* If the token represents a link, use different colours according */
  /* to the state of that link (followed, unfollowed etc).           */

  if (ISLINK(t))
  {
    /* If tokens are highlighted, return the appropriate colour */

206
    if (b->highlight) return (b->use_source_cols ? b->followed_colour : choices.followed_colour);
207 208 209

    /* If tokens are selected, return the appropriate colour */

210
    if (redraw_selected(b, t)) return (b->use_source_cols ? b->selected_colour : choices.selected_colour);
211 212 213 214

    /* If the token has been followed in the past, give the used colour */
    /* - otherwise give the unfollowed link colour.                     */

215
    if (!printing && (t->flags & HFlags_LinkVisited)) return (b->use_source_cols ? b->used_colour : choices.used_colour);
216

217
    return (b->use_source_cols ? b->link_colour : choices.link_colour);
218 219 220 221
  }

  /* If the token has attached specific colour information, return that */

222
  if ((t->type & TYPE_COLOURED) && b->use_source_cols) return (t->colour << 8);
223 224 225

  /* If the token is just text, return the normal text colour */

226
  return (b->use_source_cols ? b->text_colour : choices.text_colour);
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
}

/*************************************************/
/* redraw_set_colour()                           */
/*                                               */
/* Sets the foreground colour for future plots.  */
/*                                               */
/* Parameters: A 32-bit colour number in the     */
/*             form BBGGRRcc where cc = GCOL,    */
/*             or BBGGRR are blue, green and     */
/*             red components.                   */
/*************************************************/

void redraw_set_colour(int colour)
{
  #ifdef TRACE
    if (tl & (1u<<9)) Printf("redraw_set_colour: Called with colour = %p\n",(void *) colour);
  #endif

  /* Don't use dithering if anti-twittering redraws */

  #ifdef ANTI_TWITTER

    _swix(ColourTrans_SetGCOL,
          _IN(0) | _INR(3,4),

          colour, /* Colour to change to */
          0,      /* No dithering        */
          0);     /* GCOL action 0       */

  #else

    _swix(ColourTrans_SetGCOL,
          _IN(0) | _INR(3,4),

          colour, /* Colour to change to                            */
          1<<8,   /* Use ECFs (dithering) for better representation */
          0);     /* GCOL action 0                                  */

  #endif
}

/*************************************************/
/* redraw_display_width()                        */
/*                                               */
/* Returns the available display width for a     */
/* given browser redraw cell, in OS units.       */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the cell;             */
/*                                               */
/*             Pointer to a reformat_cell struct */
/*             representing the redraw cell.     */
/*                                               */
/* Returns:    The display width, in OS units.   */
/*************************************************/

int redraw_display_width(browser_data * b, reformat_cell * d)
{
286
  if (!d || !d->table) return b->display_width;
287 288 289 290 291 292 293 294 295 296
  else
  {
    int osw;

    convert_to_os(d->cellwidth, &osw);

    return osw;
  }
}

297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
/*************************************************/
/* redraw_display_height()                       */
/*                                               */
/* Returns the available display height for a    */
/* given browser redraw cell, in OS units; for a */
/* base browser window, it will subtract the     */
/* toolbar heights as required.                  */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the cell;             */
/*                                               */
/*             Pointer to a reformat_cell struct */
/*             representing the redraw cell.     */
/*                                               */
/* Returns:    The display width, in OS units.   */
/*************************************************/

int redraw_display_height(browser_data * b, reformat_cell * d)
{
316
  if (!d || !d->table)
317
  {
318 319 320 321
    /* Subtract a bit for aesthetics and to account for the */
    /* amount a line might naturally be overheight, so that */
    /* (say) images scaled to 100% height don't lead to a   */
    /* vertically scrollable page.                          */
322

323
    return b->display_height - b->leading * 3;
324 325 326 327 328 329 330 331 332 333 334
  }
  else
  {
    int osh;

    convert_to_os(d->cellheight, &osh);

    return osh;
  }
}

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
/*************************************************/
/* redraw_left_margin()                          */
/*                                               */
/* Returns the left hand margin width for a      */
/* given browser redraw cell, in millipoints.    */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the cell;             */
/*                                               */
/*             Pointer to a reformat_cell struct */
/*             representing the redraw cell.     */
/*                                               */
/* Returns:    The left hand margin, in          */
/*             millipoints.                      */
/*************************************************/

int redraw_left_margin(browser_data * b, reformat_cell * d)
{
353
  if (!d || !d->table) return b->left_margin;
354 355 356 357 358 359 360 361 362 363
  else
  {
    /* Left margin -> cellpadding for a table cell */

    int cellpadmp = d->table->cellpadding * 2; /* 1 'web pixel' = 2 OS units */

    convert_to_points(cellpadmp, &cellpadmp);

    return cellpadmp;
  }
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
}

/*************************************************/
/* redraw_right_margin()                         */
/*                                               */
/* Returns the right hand margin width for a     */
/* given browser redraw cell, in millipoints.    */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the cell;             */
/*                                               */
/*             Pointer to a reformat_cell struct */
/*             representing the redraw cell.     */
/*                                               */
/* Returns:    The right hand margin, in         */
/*             millipoints.                      */
/*************************************************/

int redraw_right_margin(browser_data * b, reformat_cell * d)
{
384
  if (!d || !d->table) return b->right_margin;
385 386 387 388 389 390 391 392 393 394 395
  else
  {
    /* Left margin -> cellpadding for a table cell */

    int cellpadmp = d->table->cellpadding * 2; /* 1 'web pixel' = 2 OS units */

    convert_to_points(cellpadmp, &cellpadmp);

    return cellpadmp;
  }
//  else           return 0; /* No margin on table cells - cellpadding/spacing handled separately by reformatter */
396 397 398
}

/*************************************************/
399
/* redraw_left_gap()                             */
400 401 402
/*                                               */
/* Works out the left hand indented margin for a */
/* given browser redraw cell, in millipoints.    */
403 404 405
/* This will be redraw_left_margin plus a value  */
/* dependent upon the given token (to allow e.g. */
/* list items to be indented).                   */
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the cell;             */
/*                                               */
/*             Pointer to a reformat_cell struct */
/*             representing the redraw cell;     */
/*                                               */
/*             Pointer to a token holding        */
/*             indentation information.          */
/*                                               */
/* Returns:    The left hand margin, taking      */
/*             account of list indentations      */
/*             etc., in millipoints.             */
/*************************************************/

421
int redraw_left_gap(browser_data * b, reformat_cell * d, HStream * t)
422 423 424 425
{
  int s, i;

  s = t->style;
426
  i = t->indent * b->left_indent;
427

428
  /* Play about outdenting bullets and numbered list items. */
429

430
  if (t->tagno == TAG_LI)
431
  {
432 433
    if (t->text)
    {
434 435
      _kernel_oserror * e;
      int               h, width, bytes;
436 437 438

      /* We need to right-align the text. Find out its width... */

439
      h = fm_find_token_font(b, t, 0);
440

441 442 443 444 445 446 447 448
      e = fm_get_string_width(h,
                              t->text,
                              0x40000000,
                              0x40000000,
                              -1,
                              &bytes,
                              &width);

449
      if (!e) i -= width;
450 451 452
    }
    else
    {
453
      int bullet_width;
454

455
      /* Outdent the bullet */
456

457
      convert_to_points(reformat_bullet_width(t->indent), &bullet_width);
458

459
      i -= bullet_width;
460
    }
461 462
  }

463
  /* Add an amount for block quote or address text */
464

465
  if (s & (BLOCKQUOTE | ADDRESS)) i += b->quote_margin;
466

467 468
  /* Return the calculated left indent plus the left */
  /* margin value.                                   */
469

470 471 472
  i += redraw_left_margin(b, d);

  return i > 0 ? i : 0;
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
/*************************************************/
/* redraw_right_gap()                            */
/*                                               */
/* As redraw_left_gap, but for the right hand    */
/* edge of a given redraw browser cell.          */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the cell;             */
/*                                               */
/*             Pointer to a reformat_cell struct */
/*             representing the redraw cell;     */
/*                                               */
/*             Pointer to a token holding        */
/*             indentation information.          */
/*                                               */
/* Returns:    The right hand margin, taking     */
/*             account of block quote indents    */
/*             etc., in millipoints.             */
/*************************************************/

int redraw_right_gap(browser_data * b, reformat_cell * d, HStream * t)
{
  int i = 0;

  /* Add an amount for block quote text */

  if (t->style & BLOCKQUOTE) i += b->quote_margin;

  /* Return the calculated right indent plus the right */
  /* margin value.                                     */

  i += redraw_right_margin(b, d);

  return i > 0 ? i : 0;
}

511 512 513 514 515 516 517
/*************************************************/
/* redraw_start_x()                              */
/*                                               */
/* Examines current token and line structure     */
/* information within a redraw cell to return an */
/* indent from the left edge of the page at      */
/* which something should be drawn - handles     */
518
/* centre and right aligning of lines.           */
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the cell;             */
/*                                               */
/*             Pointer to a reformat_cell struct */
/*             representing the redraw cell;     */
/*                                               */
/*             Pointer to an HStream (token)     */
/*             so that a margin can be found;    */
/*                                               */
/*             Line number which the x offset    */
/*             needs to be found for.            */
/*                                               */
/* Returns:    The x offset to plot at, in OS    */
/*             units.                            */
/*************************************************/

int redraw_start_x(browser_data * b, reformat_cell * cell, HStream * t, int line)
{
538
  int x, cn;
539 540
  int align = 0;

541 542 543 544
  if (!cell) cell = b->cell;

  cn = cell->ldata[line].chunks;

545
  /* If the token isn't centred just return the margin value; */
546
  /* else work out centre or right alignment indentation.     */
547

548 549
  if      ((t->style & CENTER) || (t->type & TYPE_ALIGN_MASK) == TYPE_CENTRE) align = 1;
  else if ((t->style & RIGHT)  || (t->type & TYPE_ALIGN_MASK) == TYPE_RIGHT)  align = 2;
550 551 552 553

  if (align)
  {
    int i;
554 555
    int left  = redraw_left_gap (b, cell, t);
    int right = redraw_right_gap(b, cell, t);
556 557 558 559 560 561 562 563 564

    /* Get the window's display width in millipoints */

    convert_to_points(redraw_display_width(b, cell), &x);

    /* Subtract the width of each chunk from this value */

    for (i = 0; i < cell->ldata[line].n; x -= cell->cdata[cn].w, i++, cn++);

565 566 567 568 569
    /* Subtract the right hand gap value */

    x -= right;

    /* For centred objects, center between the margins */
570

571
    if (align == 1) x = left + ((x - left) / 2);
572 573 574

    /* Sanity check */

575
    if (x < left) x = left;
576 577 578 579 580 581 582 583

    /* Convert back to OS units */

    convert_to_os(x, &x);

    return x;
  }

584
  convert_to_os(redraw_left_gap(b, cell, t), &x);
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

  return x;
}

/*************************************************/
/* redraw_token_x()                              */
/*                                               */
/* Examines current token and line structure     */
/* information within a redraw cell to return an */
/* indent from the left edge of the page at      */
/* which a specific token should be drawn.       */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the cell;             */
/*                                               */
/*             Pointer to a reformat_cell struct */
/*             representing the redraw cell;     */
/*                                               */
/*             Pointer to the token;             */
/*                                               */
/*             Line number which the token lies  */
/*             in;                               */
/*                                               */
/* Returns:    The x offset from the left of the */
/*             page that the token starts at, in */
/*             OS units.                         */
/*************************************************/

int redraw_token_x(browser_data * b, reformat_cell * cell, HStream * t, int line)
{
  int x, chunk, mchunk;

617 618
  if (!cell) cell = b->cell;

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
  /* Find the starting left hand edge */

  convert_to_points(redraw_start_x(b,
                                   cell,
                                   cell->cdata[cell->ldata[line].chunks].t,
                                   line),
                    &x);

  /* Add up chunk widths */

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

  while (
          chunk < mchunk         &&
          cell->cdata[chunk].t != t
        )
        x += cell->cdata[chunk].w, chunk++;

  convert_to_os(x, &x);

  /* Return the total */

  return x;
}

/*************************************************/
/* redraw_chunk_x()                              */
/*                                               */
/* Examines current token and line structure     */
/* information within a redraw cell to return an */
/* indent from the left edge of the page at      */
651 652 653
/* which a specific chunk should be drawn (i.e.  */
/* as redraw_token_x, but you supply a chunk     */
/* number rather than a token).                  */
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the cell;             */
/*                                               */
/*             Pointer to a reformat_cell struct */
/*             representing the redraw cell;     */
/*                                               */
/*             The chunk number;                 */
/*                                               */
/*             Line number the chunk lies in.    */
/*                                               */
/* Returns:    The x offset from the left of the */
/*             page that the chunk starts at, in */
/*             OS units.                         */
/*                                               */
/* Assumes:    That the given line does indeed   */
/*             include the given chunk.          */
/*************************************************/

int redraw_chunk_x(browser_data * b, reformat_cell * cell, int chunk, int line)
{
  int x, cchunk, mchunk;

677 678
  if (!cell) cell = b->cell;

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 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
  /* Find the starting left hand edge */

  convert_to_points(redraw_start_x(b,
                                   cell,
                                   cell->cdata[cell->ldata[line].chunks].t,
                                   line),
                    &x);

  /* Add up chunk widths */

  cchunk = cell->ldata[line].chunks;
  mchunk = cchunk + cell->ldata[line].n;

  while (
          cchunk < mchunk &&
          cchunk < chunk
        )
        x += cell->cdata[cchunk].w, cchunk++;

  convert_to_os(x, &x);

  /* Return the total */

  return x;
}

/*************************************************/
/* redraw_selected()                             */
/*                                               */
/* Looks at the 'selected' field for the given   */
/* browser_data struct, and returns 1 if the     */
/* given token should be part of the selection   */
/* that 'selected' lies in.                      */
/*                                               */
/* This is for whole token selection, e.g. when  */
/* keyboard navigating a page - it isn't part of */
/* a more general mouse-driven text selection    */
/* model.                                        */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the selection;        */
/*             Pointer to the token to compare.  */
/*                                               */
/* Returns:    1 if the token is part of the     */
/*             selection, else 0.                */
/*************************************************/

int redraw_selected(browser_data * b, HStream * token)
{
  HStream      * top;
  HStream      * end;
  browser_data * owner;
731
  browser_data * ancestor = utils_ancestor(b);
732 733
  int            found    = 0;

734 735 736 737 738 739
  /* If printing, don't want to show anything as selected */

  if (printing) return 0;

  /* Otherwise, find out if the token is part of a selection. */

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
  owner = ancestor->selected_owner;

  if (!ancestor->selected) return 0;
  if (ancestor->selected == token) return 1;

  tokenutils_anchor_range(owner, ancestor->selected, &top, &end);

  if (top && end)
  {
    do
    {
      if (token == top) found = 1;
      else top = top->next;
    }
    while (top && top != end->next && !found);
  }

  return found;
}

/*************************************************/
/* redraw_border_around_box()                    */
/*                                               */
/* Draws a 2 pixel thick border around a given   */
/* bounding box, in a given colour.              */
/*                                               */
/* Parameters: Pointer to the BBox;              */
767
/*                                               */
768 769 770 771 772
/*             Colour to use, as a palette entry */
/*             (for more details see             */
/*             redraw_set_colour()).             */
/*************************************************/

773
void redraw_border_around_box(BBox * rbox, int colour)
774 775 776 777 778
{
  BBox box;

  box = *rbox;

779 780 781 782
  box.xmin &= ~(wimpt_dx() - 1);
  box.ymin &= ~(wimpt_dy() - 1);
  box.xmax &= ~(wimpt_dx() - 1);
  box.ymax &= ~(wimpt_dy() - 1);
783 784 785 786 787 788 789 790 791 792 793 794 795 796

  redraw_set_colour(colour);

  bbc_rectangle(box.xmin - 4, box.ymin - 4, box.xmax - box.xmin + 7, box.ymax - box.ymin + 7);
  bbc_rectangle(box.xmin - 2, box.ymin - 2, box.xmax - box.xmin + 3, box.ymax - box.ymin + 3);
}

/*************************************************/
/* redraw_input_field()                          */
/*                                               */
/* For forms, redraws an input field element.    */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the element;          */
797
/*                                               */
798 799
/*             Pointer to the token representing */
/*             this element;                     */
800
/*                                               */
801 802
/*             BBox of the field, in window      */
/*             coords (and thus OS units);       */
803
/*                                               */
804 805 806
/*             Border colour (as a palette       */
/*             entry, for more details see       */
/*             redraw_set_colour());             */
807
/*                                               */
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
/*             1 if this is a display field,     */
/*             i.e. it has a menu icon, and the  */
/*             border colour is ignored; else 0. */
/*************************************************/

static void redraw_input_field(browser_data * b, HStream * t, BBox * box, int colour, int menu)
{
  int w, h;

  w = box->xmax - box->xmin - 1;
  h = box->ymax - box->ymin - 1;

  if (menu)
  {
    int  sw;
    BBox shorter;

    shorter = *box;

    if (read_sprite_size("fgright", &sw, NULL)) sw = 44;

    shorter.xmax -= (sw + 8);

    if (shorter.xmax < shorter.xmin) shorter.xmax = shorter.xmin + sw;

    /* Redraw the display region as a slabbed button */

    redraw_button(b, t, &shorter, 2);
  }
  else
  {
    /* Redraw the inside in white */

    redraw_set_colour(Redraw_Colour_White);

    bbc_rectanglefill(box->xmin, box->ymin, w, h);

    /* Redraw the border */

    redraw_set_colour(colour);

    bbc_rectanglefill(box->xmin,     box->ymin,     3,     h);
    bbc_rectanglefill(box->xmax - 4, box->ymin,     3,     h);
    bbc_rectanglefill(box->xmin + 4, box->ymin,     w - 8, 3);
    bbc_rectanglefill(box->xmin + 4, box->ymax - 4, w - 8, 3);

    /* Draw a wider border if selected */

856
    if (redraw_selected(b, t)) redraw_border_around_box(box, b->selected_colour);
857 858 859 860 861 862 863 864 865 866
  }
}

/*************************************************/
/* redraw_button()                               */
/*                                               */
/* For forms, redraws a button element.          */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the element;          */
867
/*                                               */
868 869
/*             Pointer to the token representing */
/*             this element;                     */
870
/*                                               */
871 872
/*             BBox of the field, in window      */
/*             coords (and thus OS units);       */
873
/*                                               */
874 875 876 877 878 879 880 881
/*             1 to be slabbed in, 2 to be       */
/*             slabbed in but with a light grey  */
/*             background rather than dark, else */
/*             0.                                */
/*************************************************/

static void redraw_button(browser_data * b, HStream * t, BBox * box, int in)
{
882
  int w, h;
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904

  w = box->xmax - box->xmin - 1;
  h = box->ymax - box->ymin - 1;

  redraw_set_colour((in == 1) ? Redraw_Colour_MidGrey : Redraw_Colour_BackGrey);

  bbc_rectanglefill(box->xmin, box->ymin, w, h);

  redraw_set_colour(in ? Redraw_Colour_PlinthGrey : Redraw_Colour_AlmostWhite);

  bbc_rectanglefill(box->xmin,     box->ymin,     1,     h);
  bbc_rectanglefill(box->xmin + 2, box->ymin + 2, 1,     h - 2);
  bbc_rectanglefill(box->xmin + 4, box->ymax - 2, w - 4, 1);
  bbc_rectanglefill(box->xmin + 4, box->ymax - 4, w - 6, 1);

  redraw_set_colour(in ? Redraw_Colour_AlmostWhite : Redraw_Colour_PlinthGrey);

  bbc_rectanglefill(box->xmin + 2, box->ymin,     w - 2, 1);
  bbc_rectanglefill(box->xmin + 4, box->ymin + 2, w - 4, 1);
  bbc_rectanglefill(box->xmax - 4, box->ymin + 4, 1,     h - 8);
  bbc_rectanglefill(box->xmax - 2, box->ymin + 4, 1,     h - 6);

905
  if (redraw_selected(b, t)) redraw_border_around_box(box, b->selected_colour);
906 907 908 909 910 911 912 913 914 915
}

/*************************************************/
/* redraw_switch()                               */
/*                                               */
/* For forms, redraws a switch (radio or option) */
/* element.                                      */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the element;          */
916
/*                                               */
917 918
/*             Pointer to the token representing */
/*             this element;                     */
919
/*                                               */
920 921
/*             x coordinate (points, in screen   */
/*             coords) of left hand edge;        */
922
/*                                               */
923 924
/*             y coordinate (points, in screen   */
/*             coords) of right hand edge;       */
925
/*                                               */
926
/*             Pointer to sprite name to use;    */
927
/*                                               */
928 929 930 931 932 933 934 935 936 937
/*             A WimpRedrawWindowBlock pointer,  */
/*             if in a redraw loop (can be NULL  */
/*             if not in a redraw loop).         */
/*************************************************/

static void redraw_switch(browser_data * b, HStream * t, int x, int y, char * spr, WimpRedrawWindowBlock * r)
{
  int               ox, oy, w, h;
  WimpPlotIconBlock block;
  BBox              icon;
938

939 940 941 942 943 944 945 946
  convert_pair_to_os(x, y, &ox, &oy);

  oy -= 8;

  if (read_sprite_size(spr, &w, &h)) w = h = 44;

  icon.xmin = ox;
  icon.ymin = oy;
947 948
  icon.xmax = icon.xmin + w;
  icon.ymax = icon.ymin + h;
949 950 951

  if (r) coords_box_toworkarea(&icon, r);

952 953 954 955 956
  block.bbox                       = icon;
  block.flags                      = 0x1700311A;
  block.data.is.sprite             = spr;
  block.data.is.sprite_area        = (void *) sprite_block;
  block.data.is.sprite_name_length = strlen(spr);
957

958 959 960 961 962 963
  _swix(Wimp_PlotIcon,
        _IN(1) | _INR(4,5),

        &block,
        0,
        0);
964 965 966 967

  if (redraw_selected(b, t))
  {
    if (r) coords_box_toscreen(&icon, r);
968
    redraw_border_around_box(&icon, b->selected_colour);
969 970 971 972 973 974 975 976 977 978
  }
}

/*************************************************/
/* redraw_bullet()                               */
/*                                               */
/* Redraws a bullet point.                       */
/*                                               */
/* Parameters: x coordinate (points, in screen   */
/*             coords) of left hand edge;        */
979
/*                                               */
980 981
/*             y coordinate (points, in screen   */
/*             coords) of right hand edge;       */
982
/*                                               */
983
/*             The bullet number;                */
984
/*                                               */
985 986 987 988 989 990 991
/*             A WimpRedrawWindowBlock pointer,  */
/*             if in a redraw loop (can be NULL  */
/*             if not in a redraw loop).         */
/*************************************************/

static void redraw_bullet(int x, int y, int bullet, WimpRedrawWindowBlock * r)
{
992
  char              spr[32];
993 994 995 996
  int               w, h;
  BBox              icon;
  WimpPlotIconBlock block;

997
  sprintf(spr, "b%d", (bullet + bullets - 1) % bullets);
998

999
  if (read_sprite_size(spr, &w, &h)) w = h = 32;
1000 1001 1002

  icon.xmin = x;
  icon.ymin = y;
1003 1004
  icon.xmax = icon.xmin + w;
  icon.ymax = icon.ymin + h;
1005 1006 1007

  if (r) coords_box_toworkarea(&icon, r);

1008 1009 1010 1011 1012
  block.bbox                       = icon;
  block.flags                      = 0x1700311A; /* 11a */
  block.data.is.sprite             = spr;
  block.data.is.sprite_area        = (void *) sprite_block;
  block.data.is.sprite_name_length = strlen(spr);
1013

1014 1015 1016 1017 1018 1019
  _swix(Wimp_PlotIcon,
        _IN(1) | _INR(4,5),

        &block,
        0,
        0);
1020 1021
}

1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
/*************************************************/
/* redraw_draw_placeholder()                     */
/*                                               */
/* Redraws a slabbed in place holder (unless the */
/* item is very small, in which case just at     */
/* thin black border is plotted) for a given     */
/* token, with optional text inside.             */
/*                                               */
/* Parameters: A pointer to a browser_data       */
/*             structure relevant to the redraw; */
/*                                               */
/*             A WimpRedrawWindowBlock pointer,  */
/*             with window area and redraw       */
/*             rectangle details filled in;      */
/*                                               */
/*             Pointer to a BBox in which xmin   */
/*             and ymin hold the screen coords   */
/*             for the bottom left hand corner,  */
/*             and xmax and ymax hold the width  */
/*             and height of the placeholder in  */
/*             OS units;                         */
/*                                               */
/*             Pointer to the HStream struct the */
/*             placeholder is to represent;      */
/*                                               */
/*             Pointer to a null-terminated      */
/*             piece of to plot inside, or NULL. */
/*************************************************/

void redraw_draw_placeholder(browser_data * b, WimpRedrawWindowBlock * r, BBox * holder, HStream * token, const char * text)
{
1053 1054
  BBox ph = *holder; // In case we want to adjust it later, e.g. for H/VSPACE

1055
  /* A slabbed box if the size is great enough */
1056

1057
  if (ph.xmax > 8 && ph.ymax > 8)
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
  {
    /* xmin, ymin hold the bottom left hand corner coordinates, whilst */
    /* xmax, ymax hold the width and height. The adjustments are to    */
    /* account for the way the bbc_rectanglefill function works; e.g., */
    /* to get a width of 4 OS units, ask for 3 (as it adds this to the */
    /* x coordinate and treats it as an inclusive x coordinate max).   */
    /* There are corrections to plot 2 OS units inside of the real     */
    /* bounding box (looks better when images touch each other) and to */
    /* get the darker sides of the 'slabbed in' box overlapping the    */
    /* lighter sides by the right amount.                              */

    redraw_set_colour(Redraw_Colour_AlmostWhite);
1070 1071 1072
    bbc_rectanglefill(ph.xmin + 2,
                      ph.ymin + 2,
                      ph.xmax - 5,
1073
                      3);
1074 1075
    bbc_rectanglefill(ph.xmax + ph.xmin - 6,
                      ph.ymin + 2,
1076
                      3,
1077
                      ph.ymax - 5);
1078 1079

    redraw_set_colour(Redraw_Colour_MidGrey);
1080 1081 1082
    bbc_rectanglefill(ph.xmin + 2,
                      ph.ymax + ph.ymin - 6,
                      ph.xmax - 7,
1083
                      3);
1084 1085
    bbc_rectanglefill(ph.xmin + 2,
                      ph.ymin + 4,
1086
                      3,
1087
                      ph.ymax - 7);
1088 1089 1090 1091 1092 1093
  }

  /* Otherwise a thin black frame */

  else
  {
1094 1095
    if (ph.xmax < 2) ph.xmax = 2;
    if (ph.ymax < 2) ph.ymax = 2;
1096 1097

    redraw_set_colour(0);
1098
    bbc_rectangle(ph.xmin,ph.ymin,ph.xmax - 1,ph.ymax - 1);
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
  }

  /* Plot any text that there is */

  if (text && *text)
  {
    /* Find out the bounding box needed to contain the text */

    int    h, xpos, vcent, stringwidth, stringheight, size;
    BBox   fbox;
    BBox * ibox = NULL;

    fbox.xmin = fbox.ymin = 0;

    /* Claim the font */

    size = (fm_size(token->fontsize) * 80) / 100;

    h = fm_find_font(b,
                     "sans",
                     size,
                     size,
                     0,
                     0);

    /* Find the string width of the ALT text */

    fm_get_string_width(h,
                        text,
                        Reformat_AsWideAsPossible_MP,
                        strlen(text),
                        -1,
                        NULL,
                        &stringwidth);

    convert_to_os(stringwidth, &stringwidth);

    /* Find the font height */

    fm_font_box(h, &fbox);

    stringheight = fbox.ymax - fbox.ymin;

    /* Set xpos to the horizontal offset to plot at. */
    /* Remember that 'box' contains the bottom       */
    /* left coordinates of the image, then the width */
    /* and height in OS units in xmax and ymax.      */

    /* Similarly, centre vertically */

1149
    vcent = (ph.ymax - stringheight) / 2 - fbox.ymin;
1150
    if (vcent <= 0) vcent = 10;
1151
    vcent += ph.ymin;
1152

1153
    xpos = (ph.xmax - stringwidth) / 2;
1154
    if (xpos <= 0) xpos = 10;
1155
    xpos += ph.xmin;
1156 1157 1158 1159 1160 1161 1162

    /* Now set the graphics window to the image bounding box,    */
    /* taking account of the slabbed border already drawn above. */
    /* Need to set this to the intersection of the current       */
    /* graphics window though, or could end up scribbling over   */
    /* things that aren't meant to be touched.                   */

1163 1164 1165 1166
    fbox.xmin = ph.xmin + 8;
    fbox.xmax = ph.xmin + ph.xmax - 9;
    fbox.ymin = ph.ymin + 8;
    fbox.ymax = ph.ymin + ph.ymax - 9;
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189

    /* If the max coordinates are less than the min, the image BBox */
    /* is too small to fit anything in. Don't proceed, as the       */
    /* attempt to set the graphics rectangle would fail, default to */
    /* the whole screen, and then random bits of ALT text would get */
    /* scribbled all over the place...                              */

    if (fbox.xmin < fbox.xmax && fbox.ymin < fbox.ymax)
    {
      /* Need to ensure a graphics window is set up for the plot, as  */
      /* text may be clipped, but this needs to take the current      */
      /* redraw rectangle into account too - hence the function call. */

      ibox = set_graphics_intersection(&fbox, &r->redraw_area);

      if (ibox)
      {
        int colour;

        colour = redraw_token_colour(b, token);

        fm_set_font_colour(h,
                           colour,
1190
                           redraw_background_colour(b, colour));
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
        fm_puts(h,
                xpos,
                vcent,
                text,
                1,
                b->background_image >= 0 && b->show_background);

        /* Underline text if it's a link and the browser is set to underline links */

        if (b->underline_links && ISLINK(token))
        {
          redraw_set_colour(colour);
          bbc_move(xpos, vcent - 7);
          bbc_draw(xpos + stringwidth, vcent - 7);
        }

        /* Put the old graphics window back again. */

        restore_graphics_intersection(&r->redraw_area);
      }
    }
  }
}

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
/*************************************************/
/* redraw_draw()                                 */
/*                                               */
/* The main browser redraw engine.               */
/*                                               */
/* Parameters: A pointer to a browser_data       */
/*             structure relevant to the redraw; */
/*                                               */
/*             A WimpRedrawWindowBlock pointer,  */
/*             with window area and redraw       */
/*             rectangle details filled in;      */
/*                                               */
/*             1 to plot no backgrounds at all,  */
/*             else they will be shown;          */
/*                                               */
/*             0 for normal redraw, else pointer */
/*             to a token where no content is to */
/*             be drawn - only the elements that */
/*             are needed to indicate selection  */
/*             should be shown. This is used     */
/*             mostly for things like removing   */
/*             borders around images; if bits of */
/*             the image have to be redrawn this */
/*             can make the removal slow. Only   */
/*             one token is allowed as any       */
/*             adjacent images must be redrawn   */
/*             if the border was plotted over    */
/*             them, or redraw anomalies will be */
/*             seen as 'holes' are left behind.  */
/*             There is some intelligence to     */
/*             give different behaviour if       */
/*             selecting or deselecting things.  */
/*************************************************/

_kernel_oserror * redraw_draw(browser_data * b, WimpRedrawWindowBlock * r, int noback, HStream * nocontent)
{
1251 1252
  #ifdef UNIFONT

1253 1254
    /* Somewhat horrible code for the system font Unicode stuff. */
    /* Should be able to lose this eventually.                   */
1255

1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
    _kernel_oserror * e;

    e = unifont_start_redraw();
    if (e) return e;

    use_noback = noback;

    e = redraw_draw_r(1, 0, 0, b, b->cell, r, noback, nocontent);

    if (e)
    {
1267 1268
      unifont_end_redraw();
      return e;
1269 1270 1271
    }

    return unifont_end_redraw();
1272 1273

  #else
1274

1275 1276 1277 1278
    use_noback = noback;

    return redraw_draw_r(1, 0, 0, b, b->cell, r, noback, nocontent);

1279
  #endif
1280 1281 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 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
}

/*************************************************/
/* redraw_draw_r()                               */
/*                                               */
/* Recursive back-end to redraw_draw.            */
/*                                               */
/* Parameters: 1 for a top level call, else 0 if */
/*             being called recursively;         */
/*                                               */
/*             X origin for plotting (window     */
/*             coords);                          */
/*                                               */
/*             Y origin for plotting (window     */
/*             coords);                          */
/*                                               */
/*             A pointer to a browser_data       */
/*             structure relevant to the redraw; */
/*                                               */
/*             Pointer to a reformat_cell struct */
/*             holding the lines and chunks to   */
/*             redraw;                           */
/*                                               */
/*             A WimpRedrawWindowBlock pointer,  */
/*             with window area and redraw       */
/*             rectangle details filled in;      */
/*                                               */
/*             1 to plot no backgrounds at all,  */
/*             else they will be shown;          */
/*                                               */
/*             0 for normal redraw, else pointer */
/*             to a token where no content is to */
/*             be drawn - only the elements that */
/*             are needed to indicate selection  */
/*             should be shown. This is used     */
/*             mostly for things like removing   */
/*             borders around images; if bits of */
/*             the image have to be redrawn this */
/*             can make the removal slow. Only   */
/*             one token is allowed as any       */
/*             adjacent images must be redrawn   */
/*             if the border was plotted over    */
/*             them, or redraw anomalies will be */
/*             seen as 'holes' are left behind.  */
/*             There is some intelligence to     */
/*             give different behaviour if       */
/*             selecting or deselecting things.  */
1327 1328 1329
/*                                               */
/* Assumes:    Pointers to items may NOT be NULL */
/*             unless explicitly stated above.   */
1330 1331 1332 1333 1334
/*************************************************/

_kernel_oserror * redraw_draw_r(int toplevel, int xorg, int yorg, browser_data * b, reformat_cell * d, WimpRedrawWindowBlock * r, int noback, HStream * nocontent)
{
  _kernel_oserror * e;
1335
  browser_data    * ancestor = utils_ancestor(b);
1336 1337 1338 1339 1340
  int               more;
  int               l = 0;
  int               page_bottom, page_height;
  int               osxorg, osyorg;
  BBox              wbox, fbox, sbox;
1341 1342

  #ifdef TRACE
1343
    if (tl & (1u<<9)) Printf("\nredraw_draw_r: Called\n");
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
  #endif

   /* Place the x and y origin in OS units into osxorg and osyorg */

  convert_pair_to_os(xorg, yorg, &osxorg, &osyorg);

  /* Start redraw */

  do
  {
    /* There is an HTML stream, so there is something to redraw. */

1356
    sbox = r->redraw_area; /* Set sbox to hold the redraw rectangle details */
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370

    /* These corrections are to ensure everything is fully redrawn.   */
    /* In particular, Font_StringWidth returns widths based on the    */
    /* distances between adjacent characters. In kerned or italicised */
    /* fonts, one character will typically have a leftmost x coord    */
    /* less than the rightmost point of the previous character. The   */
    /* reformatter uses StringWidth boxes to maintain correct char    */
    /* spacing, but then the redraw routines can go wrong, as part of */
    /* the (say) rightmost letter may fall outside of the StringWidth */
    /* box. Rather than keep two width indications, it's easier just  */
    /* to add a general correction factor to ensure that all line     */
    /* chunks are redrawn within a certain 'tolerance' / distance of  */
    /* the actual redraw rectangle.                                   */

1371 1372
    sbox.xmin -= 12, sbox.xmax += 12;
    sbox.ymin -=  2, sbox.ymax +=  2;
1373

1374
    /* Convert the screen coord redraw rectangle into millipoints */
1375 1376 1377

    convert_box_to_points(&sbox, &fbox);

1378 1379
    /* Convert the screen coord redraw region into work */
    /* area coords, putting the result in wbox.         */
1380

1381 1382
    wbox = sbox;
    coords_box_toworkarea(&wbox, r);
1383

1384 1385 1386 1387
    /* Get the page bottom in work area coordinates, and the page height. */
    /* This is really a printing only concept, where the visible_area     */
    /* BBox will in fact hold the entire page bounding box. The variables */
    /* aren't used for anything else (at present, hence no 'if' wrapper). */
1388 1389

    page_bottom = coords_y_toworkarea(r->visible_area.ymin, r);
1390
    page_height = r->visible_area.ymax - r->visible_area.ymin;
1391

1392
    /* Now the main redraw section. */
1393 1394 1395

    if (b->nchildren)
    {
1396 1397
      /* If this browser has children, it has no directly redrawable content;  */
      /* however, the frames it contains may need borders drawing around them. */
1398 1399

      #ifdef TRACE
1400
        if (tl & (1u<<9)) Printf("redraw_draw_r: Have children\n");
1401 1402
      #endif

1403 1404 1405 1406 1407 1408
      frames_redraw_borders(b, r);
    }
    else
    {
      /* If the browser doesn't have child frames, want to draw */
      /* the document it holds instead.                         */
1409 1410

      #ifdef TRACE
1411
        if (tl & (1u<<9)) Printf("redraw_draw_r: Have no children\n");
1412 1413
      #endif

1414
      if (b->stream)
1415
      {
1416 1417 1418
        #ifdef TRACE
          if (tl &512) Printf("redraw_draw_r: Have a document\n");
        #endif
1419

1420
        /* If printing, handle display style override for backgrounds */
1421

1422 1423 1424
        if (printing)
        {
          /* Force background off for some cases... */
1425

1426 1427 1428 1429 1430
          if      (printstyle_show_none()) noback = 1;
          else if (printstyle_show_in_tables_only)
          {
            if (toplevel) noback = 1;
            else
1431
            {
1432
              /* Need to work out if this table cell has a background colour */
1433

1434
              if (d->cdata && d->cdata[0].t->parent)
1435
              {
1436 1437
                if (TD_HAS_BGCOL(d->cdata[0].t->parent)) noback = 0;
                else                                     noback = 1;
1438 1439 1440 1441
              }
            }
          }

1442
          /* ...and on for others */
1443

1444 1445
          if (printstyle_show_all() && toplevel) noback = 0;
        }
1446

1447
        use_noback = noback;
1448

1449
        /* If we've *not* been told *not* to plot any backgrounds... */
1450 1451 1452

        if (!noback)
        {
1453 1454
          int htop;

1455 1456
          if (!controls.swap_bars) htop = toolbars_button_height(b) + toolbars_url_height(b);
          else                     htop = toolbars_status_height(b);
1457 1458 1459

          if (htop) htop += wimpt_dy();

1460
          /* If background images are not to be shown, or there's no image */
1461 1462 1463 1464 1465
          /* to tile on the background, set the background to a uniform    */
          /* colour. The 'if' statement implicitly calls the background    */
          /* image tiler.                                                  */

          if (
1466
               !b->show_background ||
1467 1468 1469 1470 1471 1472
               (
                 !image_tile_window(b,
                                    r,
                                    0,
                                    -htop)
               )
1473 1474 1475
             )
          {
            redraw_set_colour(redraw_backcol(b));
1476
            bbc_rectanglefill(sbox.xmin, sbox.ymin, sbox.xmax - sbox.xmin + 4, sbox.ymax - sbox.ymin + 4);
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
          }
        }

        #ifdef TRACE

          if (tl & (1u<<23))
          {
            BBox rectangle = r->redraw_area;

            redraw_set_colour(0xff884400);
            bbc_rectangle(rectangle.xmin,rectangle.ymin,rectangle.xmax-rectangle.xmin-1,rectangle.ymax-rectangle.ymin-1);

            redraw_set_colour(0xffaa6600);
            bbc_rectangle(rectangle.xmin+2,rectangle.ymin+2,rectangle.xmax-rectangle.xmin-5,rectangle.ymax-rectangle.ymin-5);

            redraw_set_colour(0xffcc8800);
            bbc_rectangle(rectangle.xmin+4,rectangle.ymin+4,rectangle.xmax-rectangle.xmin-9,rectangle.ymax-rectangle.ymin-9);
          }

        #endif

1498 1499 1500
        /* Loop over every line in this cell. */

        for (l = 0; l < d->nlines; l++)
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
        {
          /* If there are line chunks for this line, and the bounding box y */
          /* coordinates lie within the redraw rectangle, process the line. */

          if (
               d->ldata[l].n                                      &&
               osyorg + d->ldata[l].y < wbox.ymax                 &&
               osyorg + d->ldata[l].y + d->ldata[l].h > wbox.ymin
             )
          {
            HStream * tp;            /* Token Pointer */
            fm_face   h;
            int       x, y;          /* Plotting origin */
            int       keepx, keepy;  /* Line's bottom left corner */
            int       base, i;
            char    * dp;            /* Data Pointer */
            int       cn;            /* Chunk Number */

1519
            if (printing == 1 && toplevel)
1520
            {
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
              /* If printing = 1, it signals that we're not to split lines */
              /* over the bottom of the page. So if this line will drop    */
              /* off the bottom, we need to be careful. This gets a bit    */
              /* hacky now...                                              */
              /*                                                           */
              /* To signal to the printing loop that a line was about to   */
              /* be split but wasn't drawn, the top coord of the line is   */
              /* returned in the xscroll field of the redraw block passed  */
              /* to the function. The printing routines use this to work   */
              /* out where to start the next redraw from.                  */
              /*                                                           */
              /* However, for large lines - e.g. very big images, tables,  */
              /* or lines taller than the whole page - we either should,   */
              /* or in the latter case must, split that line. So, if the   */
              /* line is taller than 1/PrintSplitFraction of the page      */
              /* height, will indeed be split over the page boundary.      */
              /*                                                           */
              /* If you add code here, remember that xscroll must be       */
              /* filled in eventually or the printing loop will exit,      */
              /* assuming there's no more page to draw.                    */
1541 1542 1543

              if (osyorg + d->ldata[l].y < page_bottom)
              {
1544 1545 1546 1547 1548 1549 1550 1551 1552
                if (d->ldata[l].h <= page_height / PrintSplitFraction) /* (See Print.h) */
                {
                  r->xscroll = osyorg + d->ldata[l].y + d->ldata[l].h;
                  return NULL;
                }

                /* The effective 'else' case here has to be handled at the end */
                /* of printing, or you've just scrolled the page a long way to */
                /* right...                                                    */
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571
              }
            }

            /* Put the base address of the line's chunks into cp, */
            /* and point to its associated token in tp.           */

            cn = d->ldata[l].chunks;
            tp = d->cdata[cn].t;

            /* Get the x and y coordinates of the bottom left of the line in */
            /* millipoints into keepx and keepy, and the x and y coordinates */
            /* of the window origin in millipoints into x and y.             */

            keepy = d->ldata[l].y;
            keepx = redraw_start_x(b, d, tp, l);

            convert_pair_to_points(keepx, keepy, &keepx, &keepy);

            x = coords_x_toscreen(0, r);
1572
            y = coords_y_toscreen(0, r);
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 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622

            convert_pair_to_points(x, y, &x, &y);

            /* Offset the line x and y coordinates by the origin passed into the */
            /* function, and thus get the actual screen coordinates, in milli-   */
            /* points, into x and y.                                             */

            keepx += xorg;
            keepy += yorg;

            x += keepx;
            y += keepy;

            /* Set 'base' to hold the baseline offset in millipoints */

            convert_to_points(d->ldata[l].b, &base);

            /* Loop round for up to all the line chunks whilst staying */
            /* within the redraw rectangle horizontally.               */

            for (
                  i = 0;
                  i < d->ldata[l].n && x < fbox.xmax;
                  x += d->cdata[cn].w, i++, cn++
                )
            {
              if (x + d->cdata[cn].w > fbox.xmin)
              {
                /* Get the token address for this line chunk */

                tp = d->cdata[cn].t;

                /* If 'selected' is not NULL, and nocontent is specifying th at a */
                /* token shouldn't have its contents drawn, then a borders-only   */
                /* redraw is in progress. For removing a border, when 'selected'  */
                /* *is* NULL, want to not draw the contents of the given token    */
                /* but must redraw all others fully, else edge effects will occur */
                /* where tokens directly abut the given one (bits can get knocked */
                /* out as the border goes but the tokens it was plotted over are  */
                /* not redraw).                                                   */
                /*                                                                */
                /* However, for a borders-only redraw when something is being     */
                /* selected, don't want to draw the contents of *anything* as the */
                /* border wants to overplot it. To effect this, set the nocontent */
                /* token to always be the same as the current one.                */

                if (ancestor->selected && nocontent) nocontent = tp;

                /* Deal with table tags */

1623
                if (tp->tagno == TAG_TABLE)
1624
                {
1625
                  int  oh;
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640

                  convert_to_points(d->ldata[l].h, &oh);

                  /* Use of recursion for redraw... So need to keep this code block */
                  /* as a code block, don't try to collapse it down a level and     */
                  /* merge in 'oh' above, etc.                                      */

                  {
                    /* 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  * cell;
1641

1642 1643 1644
                    int              oldback    = 0;
                    int              oldaa      = 0;
                    int              oldbgimage = -1;
1645
                    int              t_noback;
1646

1647 1648 1649
                    BBox             rbox;
                    BBox           * ibox;
                    BBox             tbox;
1650

1651
                    int              cellindex;
1652 1653
                    int              cellcount;
                    int              cellmax = table->ColSpan * table->RowSpan;
1654

1655
                    int              swap;
1656

1657 1658 1659
                    tbox.xmin = tbox.ymin = 0x1000000;
                    tbox.xmax = tbox.ymax = 0;

1660 1661 1662 1663
                    /* Find out the overall bounding box of the table, and put this in */
                    /* 'tbox'.                                                         */

                    cellcount = 0;
1664 1665 1666

                    if (cellarray)
                    {
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 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 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
                      cellcount = 0;
                      row       = table->List;

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

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

                              if (cellindex < cellmax)
                              {
                                BBox cbox;

                                cell = &cellarray[cellindex];

                                /* Set the graphics rectangle up for the redraw */

                                cbox.xmin = x + cell->x;
                                cbox.ymin = y + cell->y + oh - cell->cellheight;
                                cbox.xmax = cbox.xmin + cell->cellwidth;
                                cbox.ymax = cbox.ymin + cell->cellheight;

                                convert_box_to_os(&cbox, &cbox);

                                /* Update the table bounding box as required */

                                if (cbox.xmin < tbox.xmin) tbox.xmin = cbox.xmin;
                                if (cbox.ymin < tbox.ymin) tbox.ymin = cbox.ymin;
                                if (cbox.xmax > tbox.xmax) tbox.xmax = cbox.xmax;
                                if (cbox.ymax > tbox.ymax) tbox.ymax = cbox.ymax;

                              /* 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 && ...)' */
                      }

                      /* tbox doesn't take account of cell spacing yet - correct */
                      /* for this now.                                           */

                      tbox.xmin -= table->cellspacing * 2;
                      tbox.ymin -= table->cellspacing * 2;
                      tbox.xmax += table->cellspacing * 2;
                      tbox.ymax += table->cellspacing * 2;

                      /* If the table has a background colour, draw this */

                      if (TABLE_HAS_BGCOL(table))
                      {
                        int tx, ty;
                        int tw, th;

                        /* Work out the x and y coordinates of the lower left hand pixel */
                        /* of the table border, and the width and height of the table    */
                        /* including the border.                                         */

                        tx = tbox.xmin;
                        ty = tbox.ymin;
                        tw = tbox.xmax - tbox.xmin;
                        th = tbox.ymax - tbox.ymin;

                        /* Draw the background */

                        redraw_set_colour(TABLE_BGCOL(table));

                        bbc_rectanglefill(tx, ty, tw - 1, th - 1);
                      }

                      /* Now redraw the table cells */

                      cellcount = 0;
                      row       = table->List;
1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777

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

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

                              if (cellindex < cellmax)
                              {
1778
                                cell = &cellarray[cellindex];
1779 1780

                                #ifdef TRACE
1781
                                  if (tl & (1u<<20)) Printf("redraw_draw call: %d -%d\n", keepx + cell->x, -(keepy + cell->y));
1782 1783 1784 1785
                                #endif

                                if (TD_HAS_BGCOL(head))
                                {
1786 1787 1788 1789 1790 1791
                                  oldback              = b->background_colour;
                                  oldaa                = b->antialias_colour;
                                  oldbgimage           = b->background_image;
                                  b->background_colour = TD_BGCOL(head);
                                  b->antialias_colour  = b->background_colour;
                                  b->background_image  = -1; /* For now, no background images in table cells. */
1792

1793
                                  t_noback = 0;
1794
                                }
1795
                                else t_noback = 1;
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805

                                /* Set the graphics rectangle up for the redraw */

                                rbox.xmin = x + cell->x;
                                rbox.ymin = y + cell->y + oh - cell->cellheight;
                                rbox.xmax = rbox.xmin + cell->cellwidth;
                                rbox.ymax = rbox.ymin + cell->cellheight;

                                convert_box_to_os(&rbox, &rbox);

1806
                                /* (rbox's max coords should be inclusive, not exclusive) */
1807

1808 1809
                                rbox.xmax--;
                                rbox.ymax--;
1810

1811
                                /* If necessary, swap min and max coords */
1812

1813 1814
                                if (rbox.xmax < rbox.xmin) swap = rbox.xmax, rbox.xmax = rbox.xmin, rbox.xmin = swap;
                                if (rbox.ymax < rbox.ymin) swap = rbox.ymax, rbox.ymax = rbox.ymin, rbox.ymin = swap;
1815

1816
                                /* See if the cell redraw box intersects the overall redraw rectangle */
1817

1818 1819
                                if (rbox.xmin == rbox.xmax || rbox.ymin == rbox.ymax) ibox = NULL;
                                else                                                  ibox = set_graphics_intersection(&rbox, &r->redraw_area);
1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844

                                if (ibox)
                                {
                                  BBox oldrect;

                                  /* Other routines that set the graphics window do it the same way as */
                                  /* here - they assume the redraw rectangle = the graphics window, as */
                                  /* during printing it's not possible to read the VDU variables to    */
                                  /* obtain the actual window. To ensure that this holds true, before  */
                                  /* recursively calling the redraw functions, the redraw rectangle    */
                                  /* must be set to match the graphics rectangle (and then restored    */
                                  /* afterwards).                                                      */

                                  oldrect        = r->redraw_area;
                                  r->redraw_area = *ibox;

                                  /* All coords in ibox are inclusive; the max coords in a redraw rectangle */
                                  /* need to be exclusive.                                                  */

                                  r->redraw_area.xmax ++;
                                  r->redraw_area.ymax ++;

                                  /* Recursive call to redraw the cell contents */

                                  redraw_draw_r(0,
1845 1846
                                                keepx + cell->x,
                                                keepy + cell->y + oh,
1847 1848 1849
                                                b,
                                                cell,
                                                r,
1850
                                                t_noback,
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
                                                nocontent);

                                  /* Restore the WimpRedrawWindowBlock redraw rectangle */

                                  r->redraw_area = oldrect;

                                  /* Restore the actual graphics rectangle */

                                  restore_graphics_intersection(&oldrect);
                                }

                                #ifdef TRACE
                                  if (tl & (1u<<11))
                                  {
                                    /* Outline the cell BBox */

                                    redraw_set_colour(0xff00aa00);
                                    bbc_rectangle(rbox.xmin, rbox.ymin, rbox.xmax - rbox.xmin - 1, rbox.ymax - rbox.ymin - 1);

                                    redraw_set_colour(0xff22cc00);
                                    bbc_rectangle(rbox.xmin + 2, rbox.ymin + 2, rbox.xmax - rbox.xmin - 5, rbox.ymax - rbox.ymin - 5);
                                  }
                                #endif

                                /* Restore any data altered in b */

1877
                                if (!t_noback)
1878
                                {
1879 1880 1881
                                  b->background_colour = oldback;
                                  b->antialias_colour  = oldaa;
                                  b->background_image  = oldbgimage;
1882 1883
                                }

1884
                                /* Draw the slabbed in cell border. */
1885

1886 1887 1888 1889 1890 1891
                                if (TABLE_BORDER(table))
                                {
                                  int dx = wimpt_dx();
                                  int dy = wimpt_dy();
                                  int hx = dx - 1;
                                  int hy = dy - 1;
1892

1893
                                  int max;
1894

1895 1896
                                  int cx, cy;
                                  int cw, ch;
1897

1898 1899 1900 1901 1902
                                  /* Get the cell x,y and w,h in OS units from the redraw box. We want to */
                                  /* use this information rather than cell->x etc. as the redraw box is   */
                                  /* the item that any internal redraws will have adhered to, including   */
                                  /* plotting cell backgrounds (if present). Thus, we want any table      */
                                  /* borders to be based on those same coordinates.                       */
1903

1904 1905 1906 1907
                                  cx = rbox.xmin;
                                  cy = rbox.ymin;
                                  cw = rbox.xmax - rbox.xmin + 1;
                                  ch = rbox.ymax - rbox.ymin + 1;
1908

1909 1910
                                  /* Get the maximum horizontal OS to pixel scaling values, as this is */
                                  /* used as a threshold for the 2D border / 3D border switching.      */
1911

1912 1913
                                  if (dy > dx) max = dy;
                                  else         max = dx;
1914

1915 1916 1917 1918
                                  #ifdef TRACE
                                    if (tl & (1u<<20)) Printf("cell box at %d %d %d %d\n",cx,cy,cw,ch);
                                  #endif

1919 1920 1921 1922
                                  /* Don't do any actual drawing if the Choices don't say so. We have to */
                                  /* do the calculation stuff above (well, most of it...!) so that the   */
                                  /* outer border plotter code (below) will work, whether inner borders  */
                                  /* are plotted or not.                                                 */
1923

1924
                                  if (choices.table_inner != Choices_TableInner_Never)
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 1964 1965 1966 1967 1968 1969 1970 1971
                                    /* For tables with a cell spacing greater than the OS unit */
                                    /* to pixel scaling value, use a 3D border (depending on   */
                                    /* what is specified in the Choices).                      */

                                    if (
                                         choices.table_inner != Choices_TableInner_Always2D &&
                                         (
                                           table->cellspacing > max ||
                                           choices.table_inner == Choices_TableInner_Always3D
                                         )
                                       )
                                    {
                                      redraw_set_colour(Redraw_Colour_AlmostWhite);

                                      bbc_rectanglefill(cx,      cy - dy, cw - hx, hy);
                                      bbc_rectanglefill(cx + cw, cy - dy, hx,      ch + hy);

                                      redraw_set_colour(Redraw_Colour_PlinthGrey);

                                      bbc_rectanglefill(cx - dx, cy - dy,      hx, ch + dy + hy);
                                      bbc_rectanglefill(cx,      cy + ch, cw + hx, hy);
                                    }

                                    /* Otherwise use a black 2D border. If we've got half of the OS unit to */
                                    /* pixel spacing available, then there'll be at least 1 pixel between   */
                                    /* all cells so we can draw in that gap. Otherwise, we must plot over   */
                                    /* the cell contents.                                                   */

                                    else if (table->cellspacing >= (max >> 1))
                                    {
                                      redraw_set_colour(Redraw_Colour_Black);

                                      bbc_rectanglefill(cx - dx, cy - dy, cw + hx, hy);
                                      bbc_rectanglefill(cx + cw, cy - dy, hx,      ch + hy);
                                      bbc_rectanglefill(cx - dx, cy,      hx,      ch + hy);
                                      bbc_rectanglefill(cx,      cy + ch, cw + hx, hy);
                                    }
                                    else
                                    {
                                      redraw_set_colour(Redraw_Colour_Black);

                                      bbc_rectanglefill(cx,           cy,           cw - hx, hy);
                                      bbc_rectanglefill(cx + cw - dx, cy,           hx,      ch - hy);
                                      bbc_rectanglefill(cx,           cy,           hx,      ch - hy);
                                      bbc_rectanglefill(cx,           cy + ch - dy, cw - hx, hy);
                                    }
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

                              /* 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 && ...)' */
                      }

1997 1998
                      /* Right, now redraw the slabbed out outer table border */

1999
                      if (TABLE_BORDER(table) && choices.table_outer != Choices_TableOuter_Never)
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
                      {
                        int dx = wimpt_dx();
                        int dy = wimpt_dy();

                        int max;

                        int tx, ty;
                        int tw, th;
                        int tb;

                        tb = TABLE_BORDER(table) * 2; /* 1 'web pixel' = 2 OS */

                        /* Work out the x and y coordinates of the lower left hand pixel */
                        /* of the table border, and the width and height of the table    */
                        /* including the border.                                         */

                        tx = tbox.xmin - tb;
                        ty = tbox.ymin - tb;
                        tw = tbox.xmax - tbox.xmin + tb * 2;
                        th = tbox.ymax - tbox.ymin + tb * 2;

                        /* Get the maximum horizontal OS to pixel scaling values, as this is */
                        /* used as a threshold for the 2D border / 3D border switching.      */

                        if (dy > dx) max = dy;
                        else         max = dx;

                        /* Use the same threshold value on the border as for internal cell borders */

2029 2030 2031 2032 2033 2034 2035
                        if (
                             choices.table_outer != Choices_TableOuter_Always2D &&
                             (
                               table->cellspacing > max ||
                               choices.table_outer == Choices_TableOuter_Always3D
                             )
                           )
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
                        {
                          int x[4], y[4];

                          /* There are 8 corners to a plinth,  6------4  */
                          /* we draw using 8 triangles. Work   |\    /|  */
                          /* these out first in the arrays     | 7--5 |  */
                          /* declared above to make the        | |  | |  */
                          /* plotting code tidier and avoid    | 1--3 |  */
                          /* unnecessary recalculation of      |/    \|  */
                          /* corner coordinates.               0------2  */
                          /*                                             */
                          /* Note how there are only 4 unique x or y     */
                          /* values, so that's all we need to work out.  */

                          x[0] = tx;
                          x[1] = tx + tb - 1;
                          x[2] = tx + tw - tb;
                          x[3] = tx + tw - 1;

                          y[0] = ty;
                          y[1] = ty + tb - 1;
                          y[2] = ty + th - tb;
                          y[3] = ty + th - 1;

                          /* OK, now do the drawing. We need to be careful about  */
                          /* the direction that the drawing occurs to ensure that */
                          /* adjacent diagonal lines meet up correctly.           */

                          redraw_set_colour(Redraw_Colour_PlinthGrey);

                          /* Bottom edge */

                          bbc_trianglefill(x[0], y[0], x[1], y[1], x[3], y[0]); /* 0 -> 1 -> 2 */
                          bbc_trianglefill(x[1], y[1], x[3], y[0], x[2], y[1]); /* 1 -> 2 -> 3 */

                          /* Right hand edge */

                          bbc_trianglefill(x[3], y[0], x[2], y[1], x[3], y[3]); /* 2 -> 3 -> 4 */
                          bbc_trianglefill(x[2], y[1], x[3], y[3], x[2], y[2]); /* 3 -> 4 -> 5 */

                          /* Now the lighter section */

                          redraw_set_colour(Redraw_Colour_AlmostWhite);

                          /* Top edge */

                          bbc_trianglefill(x[3], y[3], x[2], y[2], x[0], y[3]); /* 4 -> 5 -> 6 */
                          bbc_trianglefill(x[2], y[2], x[0], y[3], x[1], y[2]); /* 5 -> 6 -> 7 */

                          /* Finally, the left hand edge. */

                          bbc_trianglefill(x[0], y[3], x[1], y[2], x[0], y[0]); /* 6 -> 7 -> 0 */
                          bbc_trianglefill(x[1], y[2], x[0], y[0], x[1], y[1]); /* 7 -> 0 -> 1 */
                        }

                        /* Otherwise, use a 2D outer border. */

                        else
                        {
                          redraw_set_colour(Redraw_Colour_Black);

                          bbc_rectanglefill(tx,           ty,           tw - 1, tb - 1);
                          bbc_rectanglefill(tx,           ty + th - tb, tw - 1, tb - 1);
                          bbc_rectanglefill(tx,           ty,           tb - 1, th - 1);
                          bbc_rectanglefill(tx + tw - tb, ty,           tb - 1, th - 1);
                        }
                      }

2104 2105 2106 2107 2108 2109 2110 2111 2112 2113
                    /* Closure of 'if (cellarray)' */
                    }

                  /* Closure of unconditional code block dealing with redrawing */
                  /* the body of a table.                                       */
                  }
                }

                /* Deal with forms elements */

2114
                else if (tp->tagno == TAG_INPUT || tp->tagno == TAG_TEXTAREA || tp->tagno == TAG_SELECT)
2115 2116 2117 2118
                {
                  /* A text-based element */

                  if (
2119 2120
                       tp->tagno == TAG_TEXTAREA               ||
                       tp->tagno == TAG_SELECT                 ||
Kevin Bracey's avatar
Kevin Bracey committed
2121 2122
                       HtmlINPUTtype(tp) == inputtype_TEXT     ||
                       HtmlINPUTtype(tp) == inputtype_PASSWORD
2123 2124 2125 2126 2127 2128 2129 2130
                     )
                  {
                    BBox    box;
                    int     ox, oy;
                    fm_face fh;

                    convert_pair_to_os(x, y + base, &ox, &oy);

2131
                    fh = fm_find_token_font(b, tp, 0);
2132 2133
                    fm_font_box(fh, &box);

2134
                    /* Set up the bounding box for a text area, with a minimum of 2 rows */
2135

2136
                    if (tp->tagno == TAG_TEXTAREA)
2137 2138
                    {
                      int r;
2139 2140 2141
                      int lh, lb;

                      form_get_linesize(&box, &lh, &lb);
2142 2143 2144

                      r = tp->rows;
                      if (r < 2) r = 2;
2145 2146

                      box.ymin -= lh * (r - 1); /* ymin is already below the first line, so want to drop it by (rows - 1) more */
2147 2148
                    }

2149 2150
                    /* Account for the borders */

2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162
                    box.ymin = box.ymin + oy - 8;
                    box.ymax = box.ymax + oy + 8;
                    box.xmin = ox + 4;

                    convert_to_os(x + d->cdata[cn].w, &box.xmax);

                    /* Account for a border */

                    box.xmax -= 4;

                    if (nocontent != tp)
                    {
2163
                      redraw_input_field(b, tp, &box, redraw_token_colour(b, tp), tp->tagno == TAG_SELECT);
2164 2165 2166

                      fm_set_font_colour(fh,
                                         redraw_token_colour(b, tp),
2167
                                         (tp->tagno == TAG_SELECT) ? Redraw_Colour_BackGrey : Redraw_Colour_White);
2168 2169 2170 2171 2172 2173

                      form_textarea_redraw(b,
                                           d->cdata[cn].t,
                                           &box,
                                           &r->redraw_area,
                                           fh,
2174 2175
                                           tp->tagno == TAG_TEXTAREA,
                                           tp->tagno == TAG_INPUT && HtmlINPUTtype(tp) == inputtype_PASSWORD);
2176 2177 2178 2179
                    }

                    /* If the element is a SELECT field, it needs a menu icon too */

2180
                    if (tp->tagno == TAG_SELECT)
2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203
                    {
                      int  width, height, offset;
                      BBox icon;

                      /* Get the sprite size, work out a bounding box and plot */
                      /* this as a virtual icon.                               */

                      read_sprite_size("fgright", &width, &height);

                      /* Allow for the border */

                      width  += 4;
                      height += 4;

                      /* Work out the vertical offset */

                      offset = (box.ymax - box.ymin - height) / 2;

                      icon.xmin = box.xmax - width;
                      icon.ymin = box.ymin + offset;
                      icon.xmax = box.xmax;
                      icon.ymax = box.ymin + offset + height;

2204
                      if (redraw_selected(b, tp)) redraw_border_around_box(&icon, b->selected_colour);
2205 2206 2207 2208 2209 2210 2211 2212

                      coords_box_toworkarea(&icon, r);

                      if (nocontent != tp)
                      {
                        WimpPlotIconBlock block;

                        block.bbox  = icon;
2213 2214
                        block.flags = 0x1700311A;

2215
                        if (redraw_backcol(b) != Redraw_Colour_BackGrey) block.flags |= (1<<2); /* Border if not using Wimp grey background */
2216 2217 2218 2219

                        block.data.is.sprite             = "fgright";
                        block.data.is.sprite_area        = (void *) sprite_block;
                        block.data.is.sprite_name_length = sizeof("fgright") - 1;
2220 2221 2222 2223 2224

                        wimp_plot_icon(&block);
                      }
                    }
                  }
Kevin Bracey's avatar
Kevin Bracey committed
2225
                  else switch(HtmlINPUTtype(tp))
2226 2227 2228
                  {
                    /* Graphics-based forms elements */

Kevin Bracey's avatar
Kevin Bracey committed
2229
                    case inputtype_CHECKBOX:
2230 2231 2232 2233 2234 2235 2236 2237
                    {
                      if (nocontent != tp) redraw_switch(b,
                                                         tp,
                                                         x,
                                                         y + base,
                                                         form_get_field(b, d->cdata[cn].t) -> checked ? "fopton" : "foptoff",
                                                         r);
                    }
2238 2239
                    break;

Kevin Bracey's avatar
Kevin Bracey committed
2240
                    case inputtype_RADIO:
2241 2242 2243 2244 2245 2246 2247 2248
                    {
                      if (nocontent != tp) redraw_switch(b,
                                                         tp,
                                                         x,
                                                         y + base,
                                                         form_get_field(b, d->cdata[cn].t) -> checked ? "fradioon" : "fradiooff",
                                                         r);
                    }
2249 2250
                    break;

2251
                    case inputtype_IMAGE: goto do_image; /* See a short distance below */
Kevin Bracey's avatar
Kevin Bracey committed
2252 2253

                    case inputtype_HIDDEN: break;
2254

Kevin Bracey's avatar
Kevin Bracey committed
2255
                    case inputtype_SUBMIT: /* SUBMIT same as RESET: no break */
2256
                    case inputtype_BUTTON: /* Again, no break                */
Kevin Bracey's avatar
Kevin Bracey committed
2257
                    case inputtype_RESET:
2258
                    {
Kevin Bracey's avatar
Kevin Bracey committed
2259 2260 2261
                      BBox         box;
                      int          fh, ox, oy, colour;
                      const char * p;
2262 2263 2264 2265 2266

                      p = form_button_text(tp);

                      convert_pair_to_os(x, y + base, &ox, &oy);

2267
                      fh = fm_find_token_font(b, tp, 0);
2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 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

                      fm_font_box(fh,&box);

                      box.ymin = box.ymin + oy - 8;
                      box.ymax = box.ymax + oy + 8;
                      box.xmin = ox + 4;

                      convert_to_os(x + d->cdata[cn].w,&box.xmax);

                      /* Account for a border */

                      box.xmax -= 4;

                      /* Draw the button's plinth */

                      colour = redraw_token_colour(b, tp);

                      if (b->highlight == tp)
                      {
                        if (nocontent != tp)
                        {
                          redraw_button(b, tp, &box, 1);
                          fm_set_font_colour(fh, colour, Redraw_Colour_MidGrey);
                        }
                      }
                      else
                      {
                        if (nocontent != tp)
                        {
                          redraw_button(b, tp, &box, 0);
                          fm_set_font_colour(fh, colour, Redraw_Colour_BackGrey);
                        }
                      }

                      /* Plot the text, centred horizontally */

                      if (p && nocontent != tp)
                      {
2306
                        int length, end, width;
2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317

                        length = strlen(p);
                        end    = 0;

                        while(end < length && p[end] != '\n') end++;

                        e = fm_get_string_width(fh,
                                                p,
                                                0x1000000,
                                                end - d->cdata[cn].o,
                                                -1,
2318
                                                NULL,
2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335
                                                &width);

                        width = (d->cdata[cn].w - width) / 2 + 4;
                        if (width < 0) width = 0;

                        fm_puts(fh, x + width, y + base, p, 0, 0);
                      }
                    }
                    break;
                  }
                }

                /* Plot an image */

                else if (tp->style & IMG)
                {
                  BBox box;
Kevin Bracey's avatar
Kevin Bracey committed
2336 2337
                  int  ox, oy, o;

2338
do_image: /* (This code is also used for form INPUT TYPE=IMAGE tags; see above) */
Kevin Bracey's avatar
Kevin Bracey committed
2339

2340 2341 2342 2343
                  convert_pair_to_os(x, y + base, &ox, &oy);

                  if (!reformat_get_image_size(b, tp, &box))
                  {
2344 2345
                    /* Correct the coordinates for plotting */

2346 2347 2348 2349 2350 2351 2352
                    ox -= box.xmin;

                    box.xmin += ox;
                    box.ymin += oy;
                    box.xmax += ox;
                    box.ymax += oy;

2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365
                    /* Find the border width (if any) */

                    o = (tp->style & IMG) ? tp->maxlen * 2 : 0;

                    /* Ensure the image has the plotting position recorded within */
                    /* it's associated image_info structure, so that update       */
                    /* routines elsewhere will know where to plot it              */

                    image_set_token_image_position(b,
                                                   tp,
                                                   coords_x_toworkarea(box.xmin + o, r),
                                                   coords_y_toworkarea(box.ymin + o, r));

2366 2367 2368 2369
                    /* Draw a border of tp->maxlen * 2 OS units width around an image. */
                    /* This comes from the image's BORDER attribute. Links default to  */
                    /* a 2 pixel (4 OS unit) border in HTMLLib, whereas non-link       */
                    /* images default to no colour.                                    */
2370

2371 2372 2373 2374 2375
                    if (tp->tagno == TAG_INPUT && redraw_selected(b, tp))
                    {
                      redraw_border_around_box(&box, b->selected_colour);
                    }
                    else
2376 2377 2378 2379 2380 2381 2382 2383 2384 2385
                    {
                      if (o)
                      {
                        redraw_set_colour(redraw_token_colour(b, tp));

                        bbc_rectanglefill(box.xmin,     box.ymin,     box.xmax - box.xmin - 1, o - 1);
                        bbc_rectanglefill(box.xmin,     box.ymin,     o - 1,                   box.ymax - box.ymin - 1);
                        bbc_rectanglefill(box.xmin,     box.ymax - o, box.xmax - box.xmin - 1, o - 1);
                        bbc_rectanglefill(box.xmax - o, box.ymin,     o - 1,                   box.ymax - box.ymin - 1);
                      }
2386 2387
                      else if (redraw_selected(b, tp))
                      {
2388
                        redraw_border_around_box(&box, b->selected_colour);
2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404

// Hmm. Need to find a
// way of invoking this
// reliably, and clearing
// the highlight without
// flicking horribly.
//
//                        /* Things get rather more complicated for client side maps, if */
//                        /* the pointer is over them.                                   */
//
//                        if (b->pointer_over == tp && (tp->type & TYPE_ISCLIENTMAP))
//                        {
//                          /* Ask the client side map handler to do this bit */
//
//                          csim_highlight_region(b, b->selected_colour, box.xmin + o, box.ymax - o);
//                        }
2405
                      }
2406 2407 2408 2409
                    }

                    /* Redraw the image itself */

2410
                    if (nocontent != tp) RetError(image_redraw(b, r, d->cdata[cn].t, o + box.xmin, o + box.ymin));
2411 2412 2413
                  }
                }

2414 2415
                /* Plot an OBJECT, EMBED or APPLET tag */

2416
                else if (ISOBJECT(tp))
2417 2418 2419 2420 2421 2422 2423 2424
                {
                  BBox box;
                  int  ox, oy, o;

                  convert_pair_to_os(x, y + base, &ox, &oy);

                  if (!reformat_get_object_size(b, tp, &box))
                  {
2425 2426
                    o = HtmlOBJECTborder(tp) * 2;

2427 2428
                    ox -= box.xmin;

2429 2430 2431 2432 2433
                    box.xmin += ox;
                    box.ymin += oy;
                    box.xmax += ox;
                    box.ymax += oy;

2434 2435 2436 2437 2438
                    object_set_token_object_position(b,
                                                     tp,
                                                     coords_x_toworkarea(box.xmin + o, r),
                                                     coords_y_toworkarea(box.ymin + o, r));

2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451
                    /* Draw a border, if required */

                    if (HtmlOBJECTborder(tp))
                    {
                      if (o)
                      {
                        redraw_set_colour(redraw_token_colour(b, tp));

                        bbc_rectanglefill(box.xmin,     box.ymin,     box.xmax - box.xmin - 1, o - 1);
                        bbc_rectanglefill(box.xmin,     box.ymin,     o - 1,                   box.ymax - box.ymin - 1);
                        bbc_rectanglefill(box.xmin,     box.ymax - o, box.xmax - box.xmin - 1, o - 1);
                        bbc_rectanglefill(box.xmax - o, box.ymin,     o - 1,                   box.ymax - box.ymin - 1);
                      }
2452 2453 2454 2455
                      else if (redraw_selected(b, tp))
                      {
                        redraw_border_around_box(&box, b->selected_colour);
                      }
2456
                    }
2457

2458 2459
                    if (nocontent != tp) RetError(object_redraw(b, r, d->cdata[cn].t, o + box.xmin, o + box.ymin));
                  }
2460 2461
                }

2462 2463 2464 2465
                /* Plot a horizontal rule */

                else if (tp->style & HR)
                {
2466
                  int w, h, lmarg, ox, oy = 0;
2467 2468 2469

                  convert_to_os(y, &oy);

2470
                  lmarg = redraw_start_x(b, d, tp, l);
2471

2472
                  convert_to_os(d->cdata[cn].w, &w);
2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506

                  /* Round width to a multiple of 2 and limit check it. */
                  /* Allow sizes greater than the available width, in   */
                  /* which case align to the left and draw to whatever  */
                  /* width was requested.                               */

                  w = w &~ 1;
                  if (w < 2) w = 2;

                  /* Deal with a size (height) specifier */

                  if (HR_HAS_SIZE(tp))
                  {
                    /* Currently only recognise pixels */

                    switch (HR_SIZE_UNITS(tp))
                    {
                      case UNITS_PIXELS: h = HR_SIZE(tp) * 2; break;

                      /* (IMPORTANT: If adding extra units, ensure h ends up a multiple of 2) */

                      default: h = 4; break;
                    }
                  }
                  else h = 4;

                  /* Limit check the height */

                  if (h < 2) h = 2;

                  /* Sort out the horizontal and vertical plotting offsets; */
                  /* centre vertically, and align horizontally as specified */
                  /* in the token.                                          */

2507
                  oy += ((d->ldata[l].h - h) / 2) &~3 - 4;
2508 2509
                  oy += 6;

2510
                  ox = (coords_x_toscreen(lmarg + osxorg, r) &~1);
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 2553 2554 2555 2556 2557 2558 2559 2560 2561

                  /* Plot a black rule if NOSHADE is specified or the height */
                  /* or width are less than 4 OS units, else plot a '3D'     */
                  /* rule.                                                   */

                  if (HR_NOSHADE(tp) || h < 4 || w < 4)
                  {
                    redraw_set_colour(0);
                    bbc_rectanglefill(ox, oy, w - 1, h - 1);
                  }
                  else
                  {
                    if (h == 4)
                    {
                      /* Simple 'groove' rule */

                      h = h / 2;

                      redraw_set_colour(Redraw_Colour_MidGrey);
                      bbc_rectanglefill(ox, oy, w - 1, h - 1);
                      redraw_set_colour(Redraw_Colour_AlmostWhite);
                      bbc_rectanglefill(ox, oy - h, w - 1, h - 1);
                    }
                    else
                    {
                      /* 3D 'box' rule */

                      redraw_set_colour(Redraw_Colour_AlmostWhite);
                      bbc_rectanglefill(ox, oy, w - 1, 1);
                      bbc_rectanglefill(ox + w - 2, oy, 1, h - 1);
                      redraw_set_colour(Redraw_Colour_MidGrey);
                      bbc_rectanglefill(ox, oy + h - 2, w - 1, 1);
                      bbc_rectanglefill(ox, oy, 1, h - 1);
                    }
                  }
                }

                /* Plot a bullet point */

                else if(ISBULLET(tp))
                {
                  int ox,oy;

                  convert_pair_to_os(x, y + base, &ox, &oy);
                  redraw_bullet(ox, oy, tp->indent, r);
                }

                /* Plot some text */

                else
                {
2562
                  dp = d->cdata[cn].t->text;
2563 2564 2565

                  if (dp)
                  {
2566 2567
                    BBox size;
                    int  c, yofs, height;
2568 2569 2570

                    /* Find the font handle for the token, and its colour */

2571
                    h = fm_find_token_font(b, tp, 0);
2572 2573
                    c = redraw_token_colour(b, tp);

2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594
                    /* Find the text height in OS units */

                    fm_font_box(h, &size);
                    height = size.ymax - size.ymin;

                    /* Work out the y offset to plot at */

                    if (ISSUP(tp))
                    {
                      /* Shift baseline up for superscript text. The following */
                      /* will be for the SUP size text, remember...            */

                      convert_to_points(height, &yofs);

                      /* SUP height = normht * 3 / 5, so to get normal  */
                      /* height from SUP do height * 5 / 3. Then want   */
                      /* to get the height remaining and use this as an */
                      /* addition for the y positioning, so need to add */
                      /* (normht - hormht * 3 / 5) = normht * 2 / 5.    */
                      /* This all simplifies out to height * 2 / 3, but */
                      /* this looks too high in practice, so it's taken */
2595
                      /* down a bit from that!                          */
2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606

                      yofs = y + base + (yofs / 2);
                    }
                    else if (ISSUB(tp))
                    {
                      /* Shift the baseline down a bit for subscript text */

                      yofs = y + (base * 4) / 5;
                    }
                    else yofs = y + base;

2607 2608 2609 2610 2611 2612
                    /* Set the font colour and plot the text */

                    fm_set_font_colour(h,c,redraw_background_colour(b,c));

                    if (dp) fm_putsl(h,
                                     x,
2613
                                     yofs,
2614 2615 2616
                                     dp + d->cdata[cn].o,
                                     d->cdata[cn].l,
                                     0,
2617
                                     b->background_image >= 0 && b->show_background);
2618

2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629
                    /* Deal with underlining. The position should not be affected */
                    /* by SUB or SUP text.                                        */

                    if (
                         (
                           (
                             ISLINK(tp) &&
                             b->underline_links
                           )
                           || ISUNDERLINE(tp)
                         )
2630
                         && !(tp->tagno == TAG_TABLE)
2631
                       )
2632 2633 2634 2635 2636
                    {
                      /* Underline the item - set the colour, and start at the item's x coordinate... */

                      int ox,oy;

2637
                      /* Set the colour */
2638

2639
                      redraw_set_colour(redraw_token_colour(b, tp));
2640

2641
                      /* Work out the coordinates (in OS units) */
2642

2643 2644
                      convert_pair_to_os(x, y + base, &ox, &oy);
                      oy -= 7;
2645

2646
                      /* Move to the start point */
2647

2648 2649 2650 2651 2652 2653 2654
                      bbc_move(ox, oy);

                      /* ...finish at x plus its width. */

                      convert_to_os(x + d->cdata[cn].w, &ox);

                      bbc_draw(ox, oy);
2655 2656
                    }

2657 2658 2659 2660 2661
                    /* Deal with STRIKE text. This needs to have the strikethrough */
                    /* line through the text middle, as opposed to following the   */
                    /* body text font baseline (so SUB and SUP *will* have an      */
                    /* effect on the positioning).                                 */

2662
                    if (ISSTRIKE(tp) && !(tp->tagno == TAG_TABLE))
2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678
                    {
                      int ox, oy;
                      int hs;

                      redraw_set_colour(redraw_token_colour(b, tp));

                      convert_pair_to_os(x, yofs, &ox, &oy);
                      hs = height / 4;
                      oy += hs;

                      bbc_move(ox, oy);

                      convert_to_os(x + d->cdata[cn].w, &ox);

                      bbc_draw(ox, oy);
                    }
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 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743
                  }
                }

                // Plot the bounding box of any object; green to
                // mark an image, else red

                #ifdef TRACE

                  if ((tl & (1u<<11)) || (tl & (1u<<19)))
                  {
                    int ox, oy, ow, oh;

                    convert_pair_to_os(x, y, &ox, &oy);

                    convert_to_os(d->cdata[cn].w, &ow);

                    oh = d->ldata[l].h;

                    if (tl & (1u<<11))
                    {
                      _swix(Wimp_SetColour,
                            _IN(0),

                            (tp->style & IMG) ? 10 : 11);

                      bbc_rectangle(ox, oy, ow - 1, oh - 1);
                    }

                    // Mark tokens with no lower bits set in the type word
                    // (so not head, body, frameset etc.) and a NULL text
                    // field, with a magenta dot in the bottom *right* of
                    // the token BBox and a cyan dot in the top right of the
                    // BBox respectively.

                    if (tl & (1u<<19))
                    {
                      if (!(tp->type & 0xff))
                      {
                        redraw_set_colour(0xff00ff00);
                        bbc_circlefill(ox + ow - 1, oy, 6);
                        redraw_set_colour(0);
                        bbc_circle(ox + ow - 1, oy, 6);
                      }

                      if (!tp->text)
                      {
                        redraw_set_colour(0xffff0000);
                        bbc_circlefill(ox + ow - 1, oy + oh - 1, 6);
                        redraw_set_colour(0);
                        bbc_circle(ox + ow - 1, oy + oh - 1, 6);
                      }
                    }
                  }
                #endif

              /* Closure of long 'if' checking if the current chunk */
              /* lies partially or entirely within the redraw area. */
              /* If it does, the code above executes.               */
              }

            /* Closure of 'for' looping round chunks on a given line */
            /* that lies partially or entirely within the redraw     */
            /* area.                                                 */
            }

2744 2745 2746 2747 2748 2749 2750 2751 2752
            /* For printing, tell the print routines where we were up to */

            if (
                 printing == 1 &&
                 toplevel      &&
                 osyorg + d->ldata[l].y < page_bottom
               )
               r->xscroll = osyorg + page_bottom;

2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767
          /* Closure of long 'if' checking if the current line lies */
          /* partially or entirely within the redraw area. The code */
          /* above executes if it does.                             */
          }

        /* Closure of 'for' looping for all lines in the document. */
        }

      /* Closure of long 'if' checking if d->stream was not NULL. */
      /* If not, then there is a document to plot; so execute the */
      /* above code. Else, execute the code below.                */
      }

      else
      {
2768
        if (!printing || !toplevel)
2769 2770 2771 2772
        {
          /* Set the graphics background colour to the default  */
          /* and clear the graphics rectangle [to this colour]. */

2773
          redraw_set_colour(choices.background_colour);
2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785

          bbc_rectanglefill(r->redraw_area.xmin,
                            r->redraw_area.ymin,
                            r->redraw_area.xmax - r->redraw_area.xmin,
                            r->redraw_area.ymax - r->redraw_area.ymin);
        }

        /* If there's a fetch URL but no stream, the document was empty */

        if (browser_current_url(b) && !fetch_fetching(b))
        {
          fm_face h;
2786
          int     x, y, htop;
2787 2788 2789 2790
          BBox    size;

          /* Claim a font */

2791
          h = fm_find_font(b, "sans", (int) (choices.font_size * 1.5), (int) (choices.font_size * 1.5), 0, 1);
2792 2793 2794 2795 2796 2797 2798

          /* Find the height of the tallest character */

          fm_font_box(h, &size);

          /* Use that height, and the toolbar sizes to work out the y coordinate to plot at */

2799 2800
          if (!controls.swap_bars) htop = toolbars_button_height(b) + toolbars_url_height(b);
          else                     htop = toolbars_status_height(b);
2801 2802 2803 2804

          if (htop) htop += wimpt_dy();

          y = coords_y_toscreen(htop - size.ymax - size.ymin - 40, r); /* -40 = arbitrary constant, aesthetic consideration */
2805 2806 2807 2808 2809 2810 2811 2812

          /* The x coordinate has a fixed offset from the left */

          x = coords_x_toscreen(32,r);

          #ifdef TRACE
            if (tl & (1u<<9))
            {
2813 2814
              Printf("redraw_draw_r: Empty page, claimed font %p\n",(void *) h);
              Printf("               Plotting x,y %d,%d\n",x,y);
2815 2816 2817 2818 2819
            }
          #endif

          /* Set a black-on-grey font colour */

2820
          fm_set_font_colour(h, choices.text_colour, choices.background_colour);
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

          /* Write the string */

          fm_puts(h,
                  x,
                  y,
                  lookup_token("NoData:The server returned a blank page.",
                               0,
                               0),
                  1,
                  1);

          if (printing)
          {
            r->xscroll = 0;
            return NULL;
          }
        }
      }

    /* Closure of long 'if' checking if the browser window had */
    /* children. If not, the code immediately above - normal   */
    /* redraw - may be run, else special frame border redraw   */
    /* code is run.                                            */
    }

    #ifdef ANTI_TWITTER

      if (!printing && toplevel) anti_twitter(r);

    #endif

    if (!printing && toplevel) wimp_get_rectangle(r,&more);
    else more = 0;
  }
  while (more);

2858
  /* Finished... */
2859 2860 2861

  return NULL;
}