Printing 55.5 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   : Printing.c                             */
17 18 19
/*                                                 */
/* Purpose: Printing functions for the browser.    */
/*                                                 */
20 21 22 23 24
/*          This source is fairly closely tied to  */
/*          PrintStyle.c, as the Print dialogue    */
/*          can open and close the Print Style     */
/*          dialogue.                              */
/*                                                 */
25
/* Author : A.D.Hodgkinson                         */
26 27
/*                                                 */
/* History: 27-Jan-97: Created.                    */
28 29 30
/*          25-Aug-97: Overhaul (read rewrite) to  */
/*                     the new dialogue handling   */
/*                     model, as for Open URL etc. */
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
/***************************************************/

#include "signal.h"

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

#include "swis.h"

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

#include "toolbox.h"
#include "printdbox.h"

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

#include "Browser.h"
#include "FontManage.h"
#include "Images.h"
#include "Memory.h"
57
#include "PrintStyle.h"
58 59 60 61 62 63 64 65 66
#include "Redraw.h"
#include "Reformat.h"
#include "Save.h"
#include "Toolaction.h"
#include "Toolbars.h"
#include "Windows.h"

#include "Printing.h"

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
/* Local structures.                                           */
/*                                                             */
/* Holds info on the Print dialogue's contents; small enough   */
/* to hold as a static, as the code to dynamically allocate it */
/* would occupy more room than the structure itself.           */

#define End_Whole   0
#define End_Visible 1
#define End_Many    2

typedef struct
{
  int            copies;          /* Number of copies to print.                                                        */
  int            pages;           /* If 'end' is 2, the number of pages to fill.                                       */
  unsigned int   end          :2; /* 0 = whole page, 1 = to bottom of visible area, 2 = for 'pages' pages.             */
  unsigned int   start        :1; /* 1 = whole page, 0 = top of visible area.                                          */
  unsigned int   reformat     :1; /* 1 = reformat to fit page (if start is not 0 and end is not -1), else don't.       */
  unsigned int   orientation  :1; /* 1 = portrait, 0 = landscape.                                                      */

} print_contents;

/* The following stores the four basic display type settings */
/* (underline links, show images etc.) for the browser to be */
/* printed. This is so that the settings may be restored     */
/* after a print.                                            */
92 93 94

typedef struct
{
95 96 97 98
  unsigned int underlinelks  :1;
  unsigned int sourcecolours :1;
  unsigned int displayimages :1;
  unsigned int plainback     :1;
99

100
} print_restorable;
101 102 103

/* Local variables */

104 105 106 107
static int              globaljob        = 0;
static int              globalold_job    = 0;

static int              defaults_set     = 0;
108

109 110 111 112 113 114 115
static ObjectId         self_id          = 0;
static ObjectId         window_id        = 0;
static ObjectId         ancestor_id      = 0;
static browser_data   * ancestor_browser = NULL;

static print_contents   contents;
static print_restorable restore;
116 117 118

/* Static function prototypes */

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
static _kernel_oserror * print_read_contents   (ObjectId dialogue, print_contents * contents);
static _kernel_oserror * print_set_contents    (ObjectId dialogue, print_contents * contents);

static int               print_start           (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
static int               print_cancel          (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);

static int               print_check_contents  (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);

static _kernel_oserror * print_page            (browser_data * b, int copies, int from, int end, int to, int reformat, int orientation, const char * path);

static void              print_prepare_browser (browser_data * source, browser_data * store, int lmarg, int rmarg);
static void              print_restore_browser (browser_data * original, browser_data * copy);

/*************************************************/
/* print_read_contents()                         */
/*                                               */
/* Reads the contents of the Print dialogue      */
/* into a print_contents structure.              */
/*                                               */
/* Parameters: Object ID of the dialogue;        */
/*                                               */
/*             Pointer to the structure to write */
/*             to.                               */
/*************************************************/

static _kernel_oserror * print_read_contents(ObjectId dialogue, print_contents * contents)
{
  _kernel_oserror * e;
  int               state, selected;

  /* Start at top of visible area (0) or whole page (1) radios */

  RetError(radiobutton_get_state(0, dialogue, StartWhole, &state, NULL));
  contents->start = !!state;

  /* End radios - bottom of page, of visible area, or stop after */
  /* pages defined in the 'EndManyNum' number range gadget       */

  RetError(radiobutton_get_state(0, dialogue, EndWhole, NULL, &selected));
  RetError(numberrange_get_value(0, dialogue, EndManyNum, &contents->pages));

  /* Note that EndVisible etc. are component IDs defined in */
  /* Print.h, whilst End_Visible (with the underscore) etc. */
  /* are option values defined at the top of this file.     */

  switch (selected)
  {
    case EndVisible: contents->end = End_Visible;
    break;

    case EndMany:    contents->end = End_Many;
    break;

    default:
    case EndWhole:   contents->end = End_Whole;
    break;
  }

  /* Reformat page to fit */
178

179 180
  RetError(optionbutton_get_state(0, dialogue, ReformToFit, &state));
  contents->reformat = !!state;
181

182
  /* Orientation radios; portrait (1) or landscape (0) */
183

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
  RetError(radiobutton_get_state(0, dialogue, OriUpright, &state, NULL));
  contents->orientation = !!state;

  /* Read the 'Number of copies' number range gadget */

  RetError(numberrange_get_value(0, dialogue, CopiesNum, &contents->copies));

  return NULL;
}

/*************************************************/
/* print_set_contents()                          */
/*                                               */
/* Sets the contents of the Print dialogue from  */
/* a print_contents structure.                   */
/*                                               */
/* Parameters: Object ID of the dialogue;        */
/*                                               */
/*             Pointer to the structure to read  */
/*             from.                             */
/*************************************************/

static _kernel_oserror * print_set_contents(ObjectId dialogue, print_contents * contents)
{
  _kernel_oserror * e;

  /* Start position */

  if (!contents->start)       RetError(radiobutton_set_state(0, dialogue, StartVisible, 1))
  else                        RetError(radiobutton_set_state(0, dialogue, StartWhole,   1));

  /* End position, including the 'number of pages to fill' number range */

  switch (contents->end)
  {
    default:
    case End_Whole:    RetError(radiobutton_set_state(0, dialogue, EndWhole,   1));
    break;
    case End_Visible:  RetError(radiobutton_set_state(0, dialogue, EndVisible, 1));
    break;
    case End_Many:     RetError(radiobutton_set_state(0, dialogue, EndMany,    1));
    break;
  }

  RetError(numberrange_set_value(0, dialogue, EndManyNum, contents->pages));

  /* The reformat option, including greying / ungreying it */

  RetError(optionbutton_set_state(0, dialogue, ReformToFit, contents->reformat));

  /* As well as greying / ungreying the reformat option, this handles */
  /* the label text on the 'pages to fill' number range.              */

  print_check_contents(0, NULL, NULL, NULL);

  /* Orientation */

  if (!contents->orientation) RetError(radiobutton_set_state(0, dialogue, OriSideways, 1))
  else                        RetError(radiobutton_set_state(0, dialogue, OriUpright,  1));

  /* Number of copies */

  RetError(numberrange_set_value(0, dialogue, CopiesNum, contents->copies));

  return NULL;
}

/*************************************************/
/* print_set_defaults()                          */
/*                                               */
/* Fills in the local print_contents structure   */
/* with the default values to put in a Print     */
/* dialogue, if they have not already been       */
/* filled in.                                    */
/*                                               */
/* If the dialogue is open, the contents are     */
/* updated.                                      */
/*                                               */
/* Returns:    1 if the structure was filled in, */
/*             else 0.                           */
/*************************************************/

int print_set_defaults(void)
{
  if (!defaults_set)
  {
    /* Number of copies */

    contents.copies = atoi(lookup_choice("PrintCopies:1",0,0));

    /* Check it is within bounds */

    if (contents.copies < Limits_Lower_Copies) contents.copies = Limits_Lower_Copies;
    if (contents.copies > Limits_Upper_Copies) contents.copies = Limits_Upper_Copies;

    /* Start position - 'start' or 'visible', though in fact any */
    /* non-'visible' string defaults as 'start'.                 */

    if (!strcmp(lookup_choice("PrintStart:start",0,0),"visible")) contents.start = 0;
    else contents.start = 1;

    /* End position - print the whole page, down to the bottom of the */
    /* visible area, or fill up as many sheets as specified in the    */
    /* 'pages' field of the print_contents structure (see below).     */

    if (!strcmp(lookup_choice("PrintEnd:end",0,0),"end"))      contents.end = End_Whole;
    else if (!strcmp(lookup_choice("PrintEnd",1,0),"visible")) contents.end = End_Visible;
    else                                                       contents.end = End_Many;

    contents.pages = atoi(lookup_choice("PrintEnd",1,0));

    /* Check it is within bounds */

    if (contents.pages < Limits_Lower_Sheets) contents.pages = Limits_Lower_Sheets;
    if (contents.pages > Limits_Upper_Sheets) contents.pages = Limits_Upper_Sheets;

    /* Reformat - 'yes' or 'no', default to 'yes' */

    if (!strcmp(lookup_choice("PrintReform:yes",0,0),"no")) contents.reformat = 0;
    else contents.reformat = 1;

    /* Orientation - 'upright' or 'sideways', though in fact any */
    /* non-'sideways' string defaults as 'upright'.              */

    if (!strcmp(lookup_choice("PrintOrient:upright",0,0),"sideways")) contents.orientation = 0;
    else contents.orientation = 1;

    defaults_set = 1;

    if (window_id) print_set_contents(window_id, &contents);

    return 1;
  }

  else return 0;
}
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335

/*************************************************/
/* print_to_be_shown()                           */
/*                                               */
/* Called before a print dialogue opens. Deals   */
/* with setting this up with default values and  */
/* filling in print_old as appropriate, so that  */
/* if the dialogue is cancelled its contents may */
/* be correctly restored.                        */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event hander.                                 */
/*************************************************/

int print_to_be_shown(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
336 337 338 339 340 341 342 343 344 345 346 347 348
  int      was_open = 0;
  ObjectId ps_window;
  ObjectId ps_ancestor;

  /* If the stored dialogue ID is non-zero on entry, the dialogue */
  /* was reopened without closing - so get rid of the various     */
  /* event handlers before we reregister them.                    */

  if (window_id)
  {
    /* Was the Print Style window open too? */

    printstyle_return_dialogue_info(&ps_window, &ps_ancestor);
349

350
    if (ps_window) was_open = 1;
351

352
    /* This will close the Print window and Print Style (if open) */
353

354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
    print_close(0, 1);
  }

  /* Record the dialogue ID, the ancestor ID, and if this is */
  /* non-zero, the browser to which that ID refers.          */

  self_id     = idb->self_id;
  ancestor_id = idb->ancestor_id;

  if (ancestor_id) ChkError(toolbox_get_client_handle(0, ancestor_id, (void *) &ancestor_browser));

  /* If we have a browser, remember its restorable details. */

  if (ancestor_browser)
  {
    restore.underlinelks  = ancestor_browser->underlinelks;
    restore.sourcecolours = ancestor_browser->sourcecolours;
    restore.displayimages = ancestor_browser->displayimages;
    restore.plainback     = ancestor_browser->plainback;

    /* If required, force background images off */

    if (!strcmp(lookup_choice("PrintPlain:yes",0,0),"yes")) browser_set_look(ancestor_browser,
                                                                             window_id,
                                                                             -1,
                                                                             -1,
                                                                             -1,
                                                                             1);
  }

  /* Get the underlying window ID */

  ChkError(printdbox_get_window_id(0, self_id, &window_id));

  /* The Print Style dialogue may be open, too. We could take the lazy route */
  /* and just close it, but instead, we will ask it for its window details,  */
  /* and then recall its close code and ToBeShown code with the right info.  */

  if (was_open)
  {
    IdBlock  ps_id;

    ps_id.self_id = ps_window;

    /* (Make sure the Print Style routines ask the Print routines for the (new) ancestor) */
399

400
    ps_id.ancestor_id = NULL;
401

402 403
    printstyle_to_be_shown(0, NULL, &ps_id, NULL);
  }
404 405
  /* Register handlers for alternate Print/Cancel buttons */

406
  ChkError(event_register_toolbox_handler(window_id,
407
                                          EStartPrint,
408 409
                                          print_start,
                                          (void *) ancestor_id));
410

411
  ChkError(event_register_toolbox_handler(window_id,
412
                                          ECancelPrint,
413 414
                                          print_cancel,
                                          (void *) ancestor_id));
415 416 417

  /* Various alterations of icons / buttons for different UI styles */

418
  if (!strcmp(lookup_control("AlterNumranges:no",0,0),"yes"))
419 420 421
  {
    _kernel_oserror       * e;
    WimpGetIconStateBlock   icon;
422 423
    int                     iconlist [Limits_NRangeIcons];
    char                    buffer   [Limits_Message];
424 425 426

    /* Get the object's window handle and the icon handle for the given component */

427
    e = window_get_wimp_handle(0, window_id, &icon.window_handle);
428 429 430 431 432 433 434 435

    if (!e)
    {
      ComponentId writable;
      int         loop;

      for (loop = 0; loop < 2; loop ++)
      {
436
        /* Get the number range's writable component ID */
437 438

        e = numberrange_get_components(NumberRange_GetComponents_ReturnNumericalField,
439
                                      window_id,
440 441 442 443 444 445 446 447
                                      loop == 1 ? CopiesNum : EndManyNum,
                                      &writable,
                                      NULL,
                                      NULL,
                                      NULL);

        /* Turn this into an icon handle */

448
        if (!e) e = gadget_get_icon_list(0, window_id, writable, iconlist, sizeof(iconlist), NULL);
449 450 451 452 453 454 455 456 457 458 459 460

        if (!e)
        {
          icon.icon_handle = iconlist[0];

          /* Get the icon state and set the icon flags with the */
          /* programming text defined in the Messages file      */

          e = wimp_get_icon_state(&icon);

          if (!e)
          {
461
            strncpy(buffer, lookup_control("AlterWith",1,0), sizeof(buffer) - 1);
462 463 464 465 466 467 468 469 470
            buffer[sizeof(buffer) - 1] = 0;

            windows_process_icon_text(&icon, buffer, 0);
          }
        }
      }
    }
  }

471
  /* Register a handler to cope with the pages number range changing */
472

473 474 475 476
  ChkError(event_register_toolbox_handler(window_id,
                                          NumberRange_ValueChanged,
                                          print_check_contents,
                                          (void *) window_id));
477

478 479 480
  /* Similarly, the same function is called to ensure things are greyed */
  /* or ungreyed as required when the radio buttons that affect the     */
  /* 'Reformat page to fit paper' option are activated.                 */
481

482 483 484 485
  ChkError(event_register_toolbox_handler(window_id,
                                          EEnableReformat,
                                          print_check_contents,
                                          (void *) window_id));
486

487
  /* Install an animation handler, if there's an appropriate gadget */
488

489 490 491 492 493 494 495
  if (
       fixed.dboxanims &&
       !gadget_get_type(0, window_id, DisplayAnim, NULL)
     )
     register_null_claimant(Wimp_ENull,
                            toolbars_animate_slow,
                            (void *) window_id);
496

497
  /* If defaults have never been set before, set them now */
498

499
  print_set_defaults();
500

501
  /* Make sure the Print Style dialogue is set up, too */
502

503
  printstyle_set_defaults();
504

505
  /* Done! */
506

507 508
  return 1;
}
509

510 511 512 513 514 515 516 517 518
/*************************************************/
/* print_start()                                 */
/*                                               */
/* Handles clicks on the 'OK' (or 'Print', etc.) */
/* button in the Print dialogue.                 */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/
519

520 521 522
static int print_start(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  WimpMessage m;
523

524 525
  /* First, make sure we effectively OK the contents of the */
  /* Print Style dialogue.                                  */
526

527
  printstyle_ok(0, NULL, NULL, NULL);
528

529 530 531
  /* Because the printing starts from reception of an external message, */
  /* can't use a local copy of the print_contents structure and allow   */
  /* Adjust-clicks on Print/OK/Whatever. So always close the window.    */
532

533 534
  ChkError(print_read_contents(window_id, &contents));
  ChkError(print_close(0, 0));
535

536
  /* First stage of printing protocol: Broadcast a PrintSave message */
537

538 539
  m.hdr.your_ref    = 0;
  m.hdr.action_code = Browser_Message_PrintSave;
540

541 542 543 544 545 546
  m.data.data_save.destination_window = 0;
  m.data.data_save.destination_icon   = 0;
  m.data.data_save.destination_x      = 0;
  m.data.data_save.destination_y      = 0;
  m.data.data_save.estimated_size     = -1;
  m.data.data_save.file_type          = FileType_POUT;
547

548 549
  StrNCpy0(m.data.data_save.leaf_name,
           lookup_token("PrintName:WebPage",0,0));
550

551 552
  m.hdr.size = (strlen(m.data.data_save.leaf_name) + 44);
  if (m.hdr.size & 3) m.hdr.size = (m.hdr.size & ~3) + 4;
553

554
  ChkError(wimp_send_message(Wimp_EUserMessageRecorded, &m, 0, 0, NULL));
555

556
  printer_message_ref = m.hdr.my_ref;
557

558 559
  return 1;
}
560

561 562 563 564 565 566 567 568 569
/*************************************************/
/* print_cancel()                                */
/*                                               */
/* Handles clicks on the 'Cancel' button in the  */
/* Print dialogue.                               */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/
570

571 572 573
static int print_cancel(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  WimpGetPointerInfoBlock info;
574

575
  /* Restore the old contents */
576

577
  ChkError(print_set_contents(window_id, &contents));
578

579 580
  /* If Select was pressed, the dialogue should close. */
  /* (No button => Escape was pressed).                */
581

582
  ChkError(wimp_get_pointer_info(&info));
583

584
  if ((info.button_state & Wimp_MouseButtonSelect) || !info.button_state)
585
  {
586
    ChkError(print_close(0, 0));
587

588
    /* If we forced background images off, put them back again */
589

590 591 592 593 594 595 596
    if (!strcmp(lookup_choice("PrintPlain:yes",0,0),"yes")) browser_set_look(ancestor_browser,
                                                                             window_id,
                                                                             -1,
                                                                             -1,
                                                                             -1,
                                                                             restore.plainback);
  }
597 598 599 600 601

  return 1;
}

/*************************************************/
602
/* print_close()                                 */
603
/*                                               */
604 605 606
/* If the Print dialogue is opened, this will    */
/* close it, deregistering any associated event  */
/* handlers.                                     */
607
/*                                               */
608 609 610 611 612 613 614
/* Parameters: An object ID, or 0. If not zero,  */
/*             the ID must match the ancestor    */
/*             recorded when the dialogue was    */
/*             opened or no action is taken.     */
/*                                               */
/*             0 to close the dialogue, 1 to do  */
/*             everything except that.           */
615 616
/*************************************************/

617
_kernel_oserror * print_close(ObjectId ancestor, int do_not_close)
618
{
619
  _kernel_oserror * e = NULL;
620

621
  if (ancestor && ancestor != ancestor_id) return NULL;
622

623
  /* If the Print Style window is open, this will close it */
624

625
  printstyle_close(ancestor_id, do_not_close);
626

627 628 629
  if (window_id)
  {
    /* Deregister associated event handlers */
630

631 632
    e = event_deregister_toolbox_handlers_for_object(window_id);
    if (e) goto print_close_exit;
633

634
    /* If there was a null handler, remove it */
635 636

    if (
637 638
         fixed.dboxanims &&
         !gadget_get_type(0, window_id, DisplayAnim, NULL)
639 640 641
       )
       deregister_null_claimant(Wimp_ENull,
                                toolbars_animate_slow,
642
                                (void *) window_id);
643

644 645 646
    if (!do_not_close)
    {
      /* Restore input focus to the browser window */
647

648 649 650 651 652
      if (ancestor_id)
      {
        e = browser_give_general_focus(ancestor_browser);
        if (e) goto print_close_exit;
      }
653

654
      /* Close the dialogue */
655

656 657
      e = toolbox_hide_object(0, self_id);
    }
658 659
  }

660
print_close_exit:
661

662
  self_id = window_id = 0;
663

664
  return e;
665 666 667
}

/*************************************************/
668
/* print_check_contents()                        */
669
/*                                               */
670 671 672 673 674 675 676 677
/* If the state of the various radio buttons     */
/* changes, this may be called to see if the     */
/* Reformat option in the Print dialogue should  */
/* be enabled (ungreyed) or disabled (greyed).   */
/* Similarly, if the contents of the number of   */
/* sheets to fill number range changes, this     */
/* should be called to ensure the label has the  */
/* correct pluralisation applied.                */
678 679
/*                                               */
/* Parameters are as standard for a Toolbox      */
680
/* event handler.                                */
681 682
/*************************************************/

683
static int print_check_contents(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
684
{
685 686
  int          state1, state2, pages;
  unsigned int flags;
687

688
  /* Get the radio button states */
689

690 691
  ChkError(radiobutton_get_state(0, window_id, StartWhole, NULL, &state1));
  ChkError(radiobutton_get_state(0, window_id, EndWhole,   NULL, &state2));
692

693 694 695
  /* If the StartVisible and EndVisible radios are not selected, */
  /* can enable the Reformat option; else disable it. But only   */
  /* change it's state (don't grey it if already greyed, say).   */
696

697
  ChkError(gadget_get_flags(0, window_id, ReformToFit, &flags));
698

699
  if (state1 != StartVisible && state2 != EndVisible)
700
  {
701 702 703 704 705 706 707 708 709 710 711
    if (flags & Gadget_Faded)
    {
      ChkError(gadget_set_flags(0, window_id, ReformToFit, flags & ~Gadget_Faded));
    }
  }
  else
  {
    if (!(flags & Gadget_Faded))
    {
      ChkError(gadget_set_flags(0, window_id, ReformToFit, flags | Gadget_Faded));
    }
712 713
  }

714
  /* Check the pages number range, and update the label if necessary. */
715 716

  {
717
    char text[Limits_EndManyLabel];
718

719
    ChkError(numberrange_get_value(0, window_id, EndManyNum, &pages));
720

721 722
    ChkError(button_get_value(0, window_id, EndManyLabel, text, Limits_EndManyLabel, NULL));
    text[sizeof(text) - 1] = 0;
723

724 725 726 727
    if (pages == 1)
    {
      /* If the existing text isn't what we intend to change it to, then change it; */
      /* i.e. don't set the same thing twice, as this will flicker badly.           */
728

729 730 731 732 733 734 735 736
      if (strcmp(text, lookup_token("PagesSingle:sheet is filled",0,0)))
      {
        ChkError(button_set_value(0, window_id, EndManyLabel, lookup_token("PagesSingle:sheet is filled",0,0)));
      }
    }
    else
    {
      /* Again, only change the text - don't set the same thing twice. */
737

738 739 740 741 742
      if (strcmp(text, lookup_token("PagesMany:sheets are filled",0,0)))
      {
        ChkError(button_set_value(0, window_id, EndManyLabel, lookup_token("PagesMany:sheets are filled",0,0)));
      }
    }
743 744 745 746 747 748 749 750 751 752 753 754
  }

  return 1;
}

/*************************************************/
/* print_print()                                 */
/*                                               */
/* Calls the printing engine with parameters     */
/* specified in the local static print_info      */
/* structure 'print_current'.                    */
/*                                               */
755 756 757 758
/* Entry point is typically from a handler       */
/* dealing with the printing message protocol    */
/* (see handle_messages).                        */
/*                                               */
759 760 761
/* Parameters: Pointer to pathname to print to,  */
/*             or NULL to go straight to the     */
/*             'printer:' device.                */
762 763 764 765 766 767
/*************************************************/

void print_print(const char * path)
{
  _kernel_oserror * e;

768 769 770 771 772 773
  /* Must have a browser to print */

  if (!ancestor_browser) return;

  /* Do the printing */

774 775
  printing = 1;

776 777 778 779 780 781 782
  e = print_page(ancestor_browser,
                 contents.copies,
                 contents.start,
                 contents.end,
                 contents.pages,
                 contents.reformat,
                 contents.orientation,
783 784 785 786 787
                 path);

  printing = 0;

  if (e) show_error_ret(e);
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802

  /* On completion, with or without error (as e.g. Escape may */
  /* be pressed and you'd still want the following), restore  */
  /* the basic browser display characteristics, if the Print  */
  /* dialogue was closed (i.e. Print activated with Select).  */

  if (!window_id)
  {
    show_error_ret(browser_set_look(ancestor_browser,
                                    0,
                                    restore.underlinelks,
                                    restore.sourcecolours,
                                    restore.displayimages,
                                    restore.plainback));
  }
803 804 805 806 807 808 809 810 811 812 813
}

/*************************************************/
/* print_page()                                  */
/*                                               */
/* Prints out a page, assuming that all the      */
/* relevant protocol stuff to ensure it's OK to  */
/* proceed has been done already.                */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the page to print;    */
814
/*                                               */
815
/*             The number of copies to print;    */
816
/*                                               */
817 818 819
/*             1 to start at the top of the web  */
/*             page, else start from the top of  */
/*             the visible area in the window;   */
820
/*                                               */
821 822 823 824 825 826
/*             Where to end - End_Whole,         */
/*             End_Visible or End_Many (as       */
/*             defined at the top of the file);  */
/*                                               */
/*             For End_Many, how many sheets to  */
/*             fill;                             */
827
/*                                               */
828 829 830 831 832
/*             1 to reformat to fit the page     */
/*             width (orientation is taken into  */
/*             account), else 0 to keep the      */
/*             width of the window (if it falls  */
/*             off the page, tough...!);         */
833
/*                                               */
834
/*             1 = portrait, 0 = landscape;      */
835 836 837 838
/*                                               */
/*             Pointer to pathname to print to,  */
/*             or NULL to go straight to the     */
/*             'printer:' device.                */
839 840
/*************************************************/

841 842
static _kernel_oserror * print_page(browser_data * b, int copies, int from, int end, int to,
                                    int reformat, int orientation, const char * path)
843 844 845 846
{
  _kernel_oserror       * e = NULL;

  WimpRedrawWindowBlock   redraw;
847

848
  BBox                    box, last_rect;
849 850 851 852 853 854 855 856 857 858 859

  int                     must_restore;
  int                     job,            old_job;
  int                     more,           page;
  int                     top,            bottom;
  int                     next_line,      temp;
  int                     estimated_pages;
  int                     area_completed, page_area;
  int                     lmarg,          bmarg;
  int                     rmarg,          tmarg;

860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
  unsigned int            features;

  int                     portrait  [2] [2] = { {0x10000,  0}, {0, 0x10000} };
  int                     landscape [2] [2] = { {0, -0x10000}, {0x10000, 0} };

  browser_data            localbrowser;

  void (*old_sigint_handler) (int);

  /* Check to see if there is a printer driver ready */

  e = _swix(PDriver_Info,
            _OUT(3),

            &features);

  if (e) return e;

  /* Find the current page margins (and therefore, page size, */
  /* as all margins are expressed as offsets from the bottom  */
  /* left hand corner of the paper).                          */

  e = _swix(PDriver_PageSize,
            _OUTR(3,6),

            &lmarg,
            &bmarg,
            &rmarg,
            &tmarg);

  if (e) return e;

  /* If in landscape mode, want to treat the margins */
  /* in a reversed sense.                            */

  if (!orientation)
  {
897 898
    Swap(tmarg,rmarg);
    Swap(bmarg,lmarg);
899 900
  }

901 902 903 904 905 906 907 908
  /* Start the hourglass, this could take a while.              */
  /*                                                            */
  /* Using Hourglass_Start as otherwise the first percentage    */
  /* setting may be missed, since the hourglass isn't actually  */
  /* on yet (there's a default delay before appearance with     */
  /* calling Hourglass_On).                                     */

  _swix(Hourglass_Start, _IN(0), 1);
909 910 911 912

  /* If the user specified printing to or from something that */
  /* depends upon the visible area, can't then reformat.      */

913
  if (reformat && end != End_Visible && from)
914
  {
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
    int leds = 1;

    /* Reformat the page ready for printing. It used to be possible to do all of */
    /* this in a separate browser_data structure and, being careful about flex,  */
    /* reformat in that 'virtual' browser. This enabled reformatting internally  */
    /* not to affect the main browser page.                                      */
    /*                                                                           */
    /* Tables, however, screwed this up big time. Table cells were malloced, but */
    /* no record of this was kept, in the first cut of the code. So in the end,  */
    /* the address of the cell array was kept in the HStream defining the table. */
    /* However, you can only have one user of that at any one time...            */
    /*                                                                           */
    /* Four solutions to this (where NA = Not Acceptable):                       */
    /*                                                                           */
    /* 1. Get rid of the 'reformat to fit page' option (NA)                      */
    /* 2. Only allow the above when there are no tables on the page (NA)         */
    /* 3. Copy the entire token stream as well as the flex data (NA)             */
    /* 4. Reformat in the actual browser and have it reformat again afterwards.  */
    /*                                                                           */
    /* Since 1 to 3 aren't acceptable - 3 mostly because not only is it a lot of */
    /* memory to have to find, but it's in malloc space -> WimpSlot problems -   */
    /* only 4 is left. So this is what we now do here. Consequently, lots of     */
    /* bits of the browser_data structure have to be copied away and restored    */
    /* later, which can get quite messy.                                         */
    /*                                                                           */
    /* The fact that option 4 was chosen doesn't mean it isn't hideous...        */

    print_prepare_browser(b, &localbrowser, lmarg, rmarg);
    must_restore = 1;
944

945
    /* Now call the reformatter, and loop round until finished. */
946

947 948
    e = reformat_format_from(b, -1, 1, -1);
    if (e) return e;
949

950 951 952
    while (reformat_formatting(b))
    {
      reformat_reformatter(b);
953

954 955 956 957 958 959
      /* It is virtually impossible to assess progress without */
      /* doing something time consuming like scan the token    */
      /* list and work out how far down it we are, compared to */
      /* the whole length. Instead, alternate the LEDs - this  */
      /* fits in well with what the table reformatter code     */
      /* will be doing with the hourglass.                     */
960

961
      leds ^= 3;
962

963 964
      _swix(Hourglass_LEDs,
            _INR(0,1),
965

966 967
            3,
            leds);
968 969
    }

970
    _swix(Hourglass_LEDs, 0, 0);
971
  }
972
  else must_restore = 0;
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996

  localbrowser.sourcecolours = 0;

  /* Open up the output stream */

  e = _swix(OS_Find,
            _INR(0,1) | _OUT(0),

            0x8F,
            path ? path : "printer:",

            &job);

  if (e) goto out3;

  /* Estimate the number of pages to print */

  {
    int page_height;

    /* Get the printable page height */

    convert_to_os(tmarg - bmarg, &page_height);

997
    estimated_pages = (reformat_return_extent(b, NULL) / page_height) + 1;
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
  }

  globaljob = job;

  /* Stop the C library intercepting Escape, since */
  /* this should be left to the print SWIs.        */

  old_sigint_handler = signal(SIGINT, SIG_IGN);

  /* Start up the printing system */

  e = _swix(PDriver_SelectJob,
            _INR(0,1) | _OUT(0),

            job,
            lookup_token("PJobName:Web page",0,0),

            &old_job);

  if (e) goto out1;

  globalold_job = old_job;

  /* Declare fonts that have been used */

  if (features & Browser_Printer_DeclareFont)
  {
    fm_face h;

    /* If using system font, only the system faces will be */
    /* used; otherwise, need to declare the sans, serif    */
    /* and fixed faces.                                    */

    if (choices.systemfont)
    {
      h = fm_find_font(NULL, "system", 192,192,0,0); _swix(PDriver_DeclareFont,_INR(0,2),h,0,2); fm_lose_font(NULL, h);
      h = fm_find_font(NULL, "system", 192,192,1,0); _swix(PDriver_DeclareFont,_INR(0,2),h,0,2); fm_lose_font(NULL, h);
      h = fm_find_font(NULL, "system", 192,192,0,1); _swix(PDriver_DeclareFont,_INR(0,2),h,0,2); fm_lose_font(NULL, h);
      h = fm_find_font(NULL, "system", 192,192,1,1); _swix(PDriver_DeclareFont,_INR(0,2),h,0,2); fm_lose_font(NULL, h);
    }
    else
    {
      h = fm_find_font(NULL, "sans",   192,192,0,0); _swix(PDriver_DeclareFont,_INR(0,2),h,0,2); fm_lose_font(NULL, h);
      h = fm_find_font(NULL, "sans",   192,192,1,0); _swix(PDriver_DeclareFont,_INR(0,2),h,0,2); fm_lose_font(NULL, h);
      h = fm_find_font(NULL, "sans",   192,192,0,1); _swix(PDriver_DeclareFont,_INR(0,2),h,0,2); fm_lose_font(NULL, h);
      h = fm_find_font(NULL, "sans",   192,192,1,1); _swix(PDriver_DeclareFont,_INR(0,2),h,0,2); fm_lose_font(NULL, h);

      h = fm_find_font(NULL, "serif",  192,192,0,0); _swix(PDriver_DeclareFont,_INR(0,2),h,0,2); fm_lose_font(NULL, h);
      h = fm_find_font(NULL, "serif",  192,192,1,0); _swix(PDriver_DeclareFont,_INR(0,2),h,0,2); fm_lose_font(NULL, h);
      h = fm_find_font(NULL, "serif",  192,192,0,1); _swix(PDriver_DeclareFont,_INR(0,2),h,0,2); fm_lose_font(NULL, h);
      h = fm_find_font(NULL, "serif",  192,192,1,1); _swix(PDriver_DeclareFont,_INR(0,2),h,0,2); fm_lose_font(NULL, h);

      h = fm_find_font(NULL, "fixed",  192,192,0,0); _swix(PDriver_DeclareFont,_INR(0,2),h,0,2); fm_lose_font(NULL, h);
      h = fm_find_font(NULL, "fixed",  192,192,1,0); _swix(PDriver_DeclareFont,_INR(0,2),h,0,2); fm_lose_font(NULL, h);
      h = fm_find_font(NULL, "fixed",  192,192,0,1); _swix(PDriver_DeclareFont,_INR(0,2),h,0,2); fm_lose_font(NULL, h);
      h = fm_find_font(NULL, "fixed",  192,192,1,1); _swix(PDriver_DeclareFont,_INR(0,2),h,0,2); fm_lose_font(NULL, h);
    }

    /* Finish declaring fonts */

    e = _swix(PDriver_DeclareFont,
              _INR(0,2),

              0,
              0,
              2);

    if (e) goto out2;
  }

  /* Set the bottom left hand corner of the rectangle to redraw */

  box.xmin = orientation ? lmarg : bmarg;
  box.ymin = orientation ? bmarg : rmarg;

  /* Set 'top' to the offset from the top of the document to get  */
  /* to the top of the currently visible portion, and 'bottom'    */
  /* to the offset to get to the bottom of the currently visible  */
  /* portion. The positive direction is downwards (so they should */
  /* both be positive numbers).                                   */

  {
    WimpGetWindowStateBlock state;
1081
    int                     htop, hbot;
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093

    state.window_handle = b->window_handle;
    e = wimp_get_window_state(&state);
    if (e) goto out2;

    /* Get the basic offsets */

    top    = -state.yscroll;
    bottom = state.visible_area.ymax - state.visible_area.ymin;

    /* Correct for toolbar presence */

1094 1095
    if (!fixed.swapbars)
    {
1096 1097
      htop = toolbars_button_height(b) + toolbars_url_height(b);
      hbot = toolbars_status_height(b);
1098 1099 1100
    }
    else
    {
1101 1102
      htop = toolbars_status_height(b);
      hbot = toolbars_button_height(b) + toolbars_url_height(b);
1103
    }
1104

1105 1106
    if (htop) htop += wimpt_dy();
    if (hbot) hbot += wimpt_dy();
1107

1108 1109
    top    += htop;
    bottom -= (htop + hbot - top);
1110

1111 1112 1113 1114
    /* If 'from' is non-zero, want to print from the top of the whole   */
    /* page; else from the top of the visible area as worked out above. */

    if (from) top = htop;
1115
  }
1116 1117 1118 1119 1120 1121 1122 1123 1124

  redraw.yscroll = -top;
  redraw.xscroll = 0;

  /* Loop round for all pages. The y scroll position is set */
  /* initially to be correct for the place we want to print */
  /* from, and xscroll is set to 0 to mark that no calls    */
  /* to redraw_draw have happened yet.                      */

1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
  page = 0;

  while (
          (
            end == End_Many &&
            page < to
          )
          ||
          (
            end != End_Many
          )
        )
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
  {
    page ++;

    /* Set up the Redraw block ready for the calls to redraw_draw */

    redraw.visible_area.xmax = rmarg - lmarg;
    redraw.visible_area.xmin = 0;

    convert_to_os(redraw.visible_area.xmax, &redraw.visible_area.xmax);

    /* Vertical margins are complicated by the user settings. For printing    */
    /* down to the bottom of the web page, want to use a full page rectangle; */
    /* for printing down to the bottom of the visible area, want to use the   */
    /* 'bottom' variable worked out above. Note the checking to work out      */
    /* pagination - it *could* be possible that the visible area is taller    */
    /* than a single sheet of paper for the current printer.                  */

1154
    convert_to_os(tmarg - bmarg, &temp); /* 'temp' now holds the printable page height in OS units */
1155

1156
    if (end != End_Visible)
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
    {
      redraw.visible_area.ymax = temp,
      redraw.visible_area.ymin = 0;
    }
    else
    {
      /* Need to subtract an amount from 'bottom' to mark that a page has been  */
      /* done, but must do that after the page rendering so the redraw routines */
      /* have filled in redraw.xscroll with the last coordinate used - if we    */
      /* don't do this, we can't tell how much of the web page was actually     */
      /* used. Remember that xscroll and yscroll are negative; xscroll refers   */
      /* to the vertical offset to start the next page at, and yscroll was the  */
      /* offset that this one started at.                                       */

      if (redraw.xscroll && bottom) bottom -= (redraw.yscroll - redraw.xscroll);

      if (bottom - top > temp) /* And *not* '>= temp'! See 'else' code below */
      {
        redraw.visible_area.ymax = temp;
      }
      else
      {
        redraw.visible_area.ymax = bottom - top;
        bottom = 0;

        /* Since this rectangle is at most a full page in height but probably */
        /* less, must shift the bottom left hand coordinate of it up an       */
        /* appropriate amount to print the page fragment at the top of the    */
        /* paper rather than the bottom.                                      */

        temp -= redraw.visible_area.ymax; /* (Page height minus rectangle height in OS units) */

        convert_to_points(temp, &temp);

        if (orientation) box.ymin += temp;
        else             box.xmin += temp;

        /* Signal to redraw_draw that the last line on this page may be split */
        /* over the bottom of the page, as that's what the user is seeing in  */
        /* the real window right now.                                         */

        printing = 2;
      }

      redraw.visible_area.ymin = 0;
    }

    /* redraw_draw will give the y coordinate of the next line to */
    /* print when it exits by placing an appropriate coordinate   */
    /* in the xscroll field of the redraw block that is passed to */
    /* it during the main printer redraw loop.                    */

    if (redraw.xscroll) redraw.yscroll = redraw.xscroll, redraw.xscroll = 0;

    /* Start drawing things */

    e = _swix(PDriver_GiveRectangle,
              _INR(0,4),

              0, /* Rectangle ID word - only 1 per page, so 0 will do */
              &redraw.visible_area,
              orientation ? &portrait : &landscape,
              &box,
              Redraw_Colour_White);

    if (e) goto out2;

    e = _swix(PDriver_DrawPage,
              _INR(0,3) | _OUT(0),

              (copies) | ((features & Browser_Printer_PreScansRectangles) ? (1<<24) : (0)),
              &redraw.redraw_area,
              0,
              0,

              &more);

    if (e) goto out2;

    /* Give an indication of progress */

    {
      int percent;

      percent = (100 * (page - 1)) / estimated_pages;

      if (percent > 99) percent = 99;

      _swix(Hourglass_Percentage, _IN(0), percent);
    }

    /* The redraw loop itself. The area stuff is for the */
    /* in-page hourglass percentage; see later comments. */

    page_area = (redraw.visible_area.xmax - redraw.visible_area.xmin) *
                (redraw.visible_area.ymax - redraw.visible_area.ymin);

    last_rect.xmin = last_rect.xmax = last_rect.ymin = last_rect.ymax = 0;

    next_line = area_completed = 0;

    while (more)
    {
      /* Ensure images are correct for the current mode */

      image_mode_change();

      /* Do the redraw */

1266
      e = redraw_draw(b,
1267
                      &redraw,
1268
                      0,
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 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 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
                      0);

      if (e) goto out2;

      /* Don't want to start pagination issues if this is just a prescan */

      if ((features & Browser_Printer_PreScansRectangles) && (more & 1<<24))
      {
        redraw.xscroll = 0;
      }
      else
      {
        int    this_area;
        BBox * i;

        if (redraw.xscroll != 0) next_line = redraw.xscroll, redraw.xscroll = 0;

        /* Give a percentage completed indicator. This is first based */
        /* on the current page being printed, so there's some scaling */
        /* of the 100% range to cope with the fact that if you're on  */
        /* page 3 of 4, the variation must be between 50% and 75%,    */
        /* for example. Since rectangle order cannot be relied upon,  */
        /* need to use the area printed so far for the calculation.   */
        /* This may fail under unusual circumstances, and certainly   */
        /* is not fully accurate as the rectangles always overlap by  */
        /* a small amount (the printer driver gives room for rounding */
        /* errors by overlapping the rectangles) but in any case      */
        /* there will at least be some kind of percentage indication! */
        /* With bit image printing, which can be painfully slow, this */
        /* is extremely important.                                    */
        /*                                                            */
        /* There is an attempt to correct for overlapping rectangles, */
        /* if this one and the last were overlapping.                 */

        i = intersection(&redraw.redraw_area, &last_rect);

        if (i)
        {
          this_area = (redraw.redraw_area.xmax - redraw.redraw_area.xmin) *
                      (redraw.redraw_area.ymax - redraw.redraw_area.ymin)
                    - (i->xmax                 - i->xmin) *
                      (i->ymax                 - i->ymin);
        }
        else
        {
          this_area = (redraw.redraw_area.xmax - redraw.redraw_area.xmin) *
                      (redraw.redraw_area.ymax - redraw.redraw_area.ymin);
        }

        last_rect = redraw.redraw_area;

        area_completed += this_area;
        if (area_completed > page_area) area_completed = page_area;

        {
          int percent;

          percent = (100 * (page - 1)) / estimated_pages +
                    ((100 / estimated_pages) * area_completed) / page_area;

          if (percent < 0)  percent = 0;
          if (percent > 99) percent = 99;

          _swix(Hourglass_Percentage, _IN(0), percent);
        }
      }

      /* Get the next rectangle */

      e = _swix(PDriver_GetRectangle,
                _IN(1) | _OUT(0),

                &redraw.redraw_area,
                &more);

      if (e) goto out2;
    }

    if (next_line) redraw.xscroll = next_line;

    /* If we should print down to the bottom of the visible */
1350 1351 1352
    /* area of the page, this is flagged with 'end' set to  */
    /* End_Visible; if 'bottom' is zero as well, there's    */
    /* nothing more to print.                               */
1353

1354
    if (end == End_Visible && !bottom) break;
1355 1356 1357 1358 1359 1360 1361

    /* If xscroll is 0, redraw_draw must not have found any */
    /* lines that fell off the bottom of the page - so      */
    /* there cannot be any more pages.                      */

    if (!redraw.xscroll) break;

1362 1363
    /* Otherwise, close the outer while loop - which */
    /* may mean we loop for another page.            */
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
  }

  /* Finished, so end the job, close the output stream, */
  /* turn off the hourglass and restore the previous    */
  /* job.                                               */

  e = _swix(PDriver_EndJob,
            _IN(0),

            job);

  if (e) goto out2;

  /* Ensure images are restored to the correct mode */

  image_mode_change();

  /* Restore the old Escape handler */

  signal(SIGINT, old_sigint_handler);

  /* Close the output stream */

  globaljob = 0;

  e = _swix(OS_Find,
            _INR(0,1),

            0x00,
            job);

  if (e) goto out3;

1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
  /* Remove the hourglass percentage indicator */

  _swix(Hourglass_Percentage,
        _IN(0),

        100);

  /* Now we have to put the page back where it was... */

  if (must_restore)
  {
    int leds = 1;

    print_restore_browser(b, &localbrowser);

    /* As before, sit around whilst the reformatter reformats. */

    e = reformat_format_from(b, -1, 1, -1);
    if (e) return e;

    while (reformat_formatting(b))
    {
      reformat_reformatter(b);

      leds ^= 3;

      _swix(Hourglass_LEDs,
            _INR(0,1),

            3,
            leds);
    }

    /* We don't need to restore anything now */

    must_restore = 0;
  }

  /* Turn off the hourglass */

  _swix(Hourglass_Off, 0);

  if (must_restore)
  {
    print_restore_browser(b, &localbrowser);
    reformat_format_from(b, -1, 1, -1);
  }
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469

  /* Restore the previous print job */

  globalold_job = 0;

  e = _swix(PDriver_SelectJob,
            _INR(0,1),

            old_job,
            0);

  return e;

out1: /* 'Emergency exit' if PDriver_SelectJob fails */

  signal(SIGINT, old_sigint_handler);
  globaljob = 0;

  /* Close the output stream */

  _swix(OS_Find,
        _INR(0,1),

        0x00,
        job);

1470 1471 1472 1473
  /* Flag that printing has finished */

  printing = 0;

1474 1475 1476 1477
  /* Force the hourglass off */

  _swix(Hourglass_Smash, 0);

1478
  /* Put the browser back together again */
1479

1480 1481 1482 1483 1484
  if (must_restore)
  {
    print_restore_browser(b, &localbrowser);
    reformat_format_from(b, -1, 1, -1);
  }
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494

  /* Ensure images are still OK */

  image_mode_change();

  return e;

out2: /* 'Emergency exit' for errors whilst printing */

  signal(SIGINT, old_sigint_handler);
1495

1496
  print_abort_print();
1497 1498 1499 1500 1501 1502 1503 1504 1505

  printing = 0;

  if (must_restore)
  {
    print_restore_browser(b, &localbrowser);
    reformat_format_from(b, -1, 1, -1);
  }

1506 1507 1508 1509
  image_mode_change();

  return e;

1510
out3: /* 'Emergency exit' for errors after printing */
1511 1512

  _swix(Hourglass_Smash, 0);
1513 1514 1515 1516 1517 1518 1519 1520 1521

  printing = 0;

  if (must_restore)
  {
    print_restore_browser(b, &localbrowser);
    reformat_format_from(b, -1, 1, -1);
  }

1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
  image_mode_change();

  return e;
}

/*************************************************/
/* print_abort_print()                           */
/*                                               */
/* Forcibly aborts a print job.                  */
/*************************************************/

void print_abort_print(void)
{
  /* Abort the current print job */

  _swix(PDriver_AbortJob,
        _IN(0),

        globaljob);

  /* Close the output stream */

  _swix(OS_Find,
        _INR(0,1),

        0x00,
        globaljob);

  globaljob = 0;

  /* Restore the previous print job */

  _swix(PDriver_SelectJob,
        _INR(0,1),

        globalold_job,
        0);

  globalold_job = 0;

  /* Force the hourglass off */

  _swix(Hourglass_Smash, 0);
}

/*************************************************/
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
/* print_prepare_browser()                       */
/*                                               */
/* Prepares a browser for internal reformatting  */
/* prior to printing a page, storing various     */
/* overwritten values into an alternative        */
/* given structure.                              */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the page to print;    */
/*                                               */
/*             Pointer to the browser_data       */
/*             struct to copy into;              */
/*                                               */
/*             Left hand page margin, in         */
/*             millipoints;                      */
1583
/*                                               */
1584 1585
/*             Right hand page margin, in        */
/*             millipoints.                      */
1586 1587
/*************************************************/

1588
static void print_prepare_browser(browser_data * source, browser_data * store, int lmarg, int rmarg)
1589
{
1590 1591 1592
  store->previous        = source->previous;
  store->next            = source->next;
  source->previous       = source->next = NULL;
1593

1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
  store->display_width   = source->display_width;
  source->display_width  = rmarg - lmarg;

  convert_to_os(source->display_width, &source->display_width);

  store->display_extent  = source->display_extent;
  source->display_extent = source->display_width;

  store->fetch_status    = source->fetch_status;
  source->fetch_status   = BS_IDLE;
  store->fetch_handle    = source->fetch_handle;
  source->fetch_handle   = source->display_handle;
1606 1607 1608
}

/*************************************************/
1609
/* print_restore_browser()                       */
1610
/*                                               */
1611 1612 1613 1614
/* Puts back the bits and pieces replaced in the */
/* browser_data struct given to the print        */
/* routines, copied out because a reformat was   */
/* required on the page.                         */
1615
/*                                               */
1616 1617 1618 1619 1620 1621 1622 1623
/* Parameters: Pointer to the browser_data       */
/*             struct given to the print         */
/*             routines;                         */
/*                                               */
/*             Pointer to the browser_data       */
/*             struct used to store the over-    */
/*             written values from the original  */
/*             copy in print_prepare_browser.    */
1624 1625
/*************************************************/

1626
static void print_restore_browser(browser_data * original, browser_data * copy)
1627
{
1628 1629 1630 1631 1632 1633 1634
  original->previous       = copy->previous;
  original->next           = copy->next;
  original->display_width  = copy->display_width;
  original->display_extent = copy->display_extent;
  original->fetch_status   = copy->fetch_status;
  original->fetch_handle   = copy->fetch_handle;
}
1635

1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
/*************************************************/
/* print_return_dialogue_info()                  */
/*                                               */
/* Returns information on the Print dialogue,    */
/* and its ancestor.                             */
/*                                               */
/* Parameters: Pointer to an ObjectId, in which  */
/*             the ID of the PrintDBox object    */
/*             is placed;                        */
/*                                               */
/*             Pointer to an ObjectId, in which  */
/*             the ID of the underlying window   */
/*             object is placed;                 */
/*                                               */
/*             Pointer to an ObjectId, in which  */
/*             the ID of the ancestor window is  */
/*             placed;                           */
/*                                               */
/*             Pointer to a pointer to a         */
/*             browser_data struct, in which the */
/*             address of the browser_data       */
/*             struct associated with the        */
/*             ancestor object is placed.        */
/*                                               */
/* Returns:    See parameters list, and note     */
/*             that the returned values will be  */
/*             0, 0, 0 and NULL if the Print     */
/*             dialogue is closed.               */
/*                                               */
/* Assumes:    Any of the pointers may be NULL.  */
/*************************************************/
1667

1668 1669 1670 1671 1672 1673
void print_return_dialogue_info(ObjectId * self, ObjectId * window, ObjectId * ancestor, browser_data ** ancestor_b)
{
  if (self)       *self       = self_id;
  if (window)     *window     = window_id;
  if (ancestor)   *ancestor   = ancestor_id;
  if (ancestor_b) *ancestor_b = ancestor_browser;
1674
}