Printing 56.2 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 "Protocols.h"
58
#include "PrintStyle.h"
59 60 61 62 63 64 65 66
#include "Redraw.h"
#include "Reformat.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 underline_links :1;
  unsigned int use_source_cols :1;
  unsigned int show_foreground :1;
  unsigned int show_background :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
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);

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
/*************************************************/
/* print_open_for()                              */
/*                                               */
/* Creates and opens a Print dialogue for a      */
/* given browser, opening near the pointer.      */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             which is the ancestor of the      */
/*             dialogue;                         */
/*                                               */
/*             Object ID to use as a parent, or  */
/*             0 for none.                       */
/*************************************************/

_kernel_oserror * print_open_for(browser_data * b, ObjectId parent)
{
  _kernel_oserror * e;
  ObjectId          id;

  /* Create the object - if it is already created, this will */
  /* just return the ID of the existing object.              */

  RetError(toolbox_create_object(0,
                                 "PrintDbox",
                                 &id));

  RetError(toolbox_show_object(0,
                               id,
                               Toolbox_ShowObject_Centre,
                               NULL,
                               parent,
                               -1));

  return NULL;
}

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
/*************************************************/
/* 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 */

187
  RetError(radiobutton_get_state(0, dialogue, PStartWhole, &state, NULL));
188 189 190
  contents->start = !!state;

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

193 194
  RetError(radiobutton_get_state(0, dialogue, PEndWhole, NULL, &selected));
  RetError(numberrange_get_value(0, dialogue, PEndManyNum, &contents->pages));
195

196 197 198
  /* Note that PEndVisible 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.      */
199 200 201

  switch (selected)
  {
202 203
    default:
    case PEndWhole:   contents->end = End_Whole;
204 205
    break;

206
    case PEndVisible: contents->end = End_Visible;
207 208
    break;

209
    case PEndMany:    contents->end = End_Many;
210 211 212 213
    break;
  }

  /* Reformat page to fit */
214

215
  RetError(optionbutton_get_state(0, dialogue, PReformatToFit, &state));
216
  contents->reformat = !!state;
217

218
  /* Orientation radios; portrait (1) or landscape (0) */
219

220
  RetError(radiobutton_get_state(0, dialogue, POriUpright, &state, NULL));
221 222 223 224
  contents->orientation = !!state;

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

225
  RetError(numberrange_get_value(0, dialogue, PCopiesNum, &contents->copies));
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247

  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 */

248 249
  if (!contents->start) RetError(radiobutton_set_state(0, dialogue, PStartVisible, 1))
  else                  RetError(radiobutton_set_state(0, dialogue, PStartWhole,   1));
250 251 252 253 254 255

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

  switch (contents->end)
  {
    default:
256
    case End_Whole:     RetError(radiobutton_set_state(0, dialogue, PEndWhole,   1));
257
    break;
258
    case End_Visible:   RetError(radiobutton_set_state(0, dialogue, PEndVisible, 1));
259
    break;
260
    case End_Many:      RetError(radiobutton_set_state(0, dialogue, PEndMany,    1));
261 262 263
    break;
  }

264
  RetError(numberrange_set_value(0, dialogue, PEndManyNum, contents->pages));
265 266 267

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

268
  RetError(optionbutton_set_state(0, dialogue, PReformatToFit, contents->reformat));
269 270 271 272 273 274 275 276

  /* 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 */

277 278
  if (!contents->orientation) RetError(radiobutton_set_state(0, dialogue, POriSideways, 1))
  else                        RetError(radiobutton_set_state(0, dialogue, POriUpright,  1));
279 280 281

  /* Number of copies */

282
  RetError(numberrange_set_value(0, dialogue, PCopiesNum, contents->copies));
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355

  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;
}
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371

/*************************************************/
/* 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)
{
372 373 374 375 376 377 378 379 380 381 382 383 384
  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);
385

386
    if (ps_window) was_open = 1;
387

388
    /* This will close the Print window and Print Style (if open) */
389

390 391 392 393 394 395 396 397 398 399 400
    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));

401 402 403 404 405 406 407 408 409 410
  /* If this is for an ancestor browser, use whatever frame is selected */
  /* instead - allows toolbar buttons and keyboard shortcuts to work in */
  /* a sensible fashion...                                              */

  if (ancestor_browser->selected_frame)
  {
    ancestor_browser = ancestor_browser->selected_frame;
    ancestor_id      = ancestor_browser->self_id;
  }

411 412 413 414
  /* If we have a browser, remember its restorable details. */

  if (ancestor_browser)
  {
415 416 417 418
    restore.underline_links = ancestor_browser->underline_links;
    restore.use_source_cols = ancestor_browser->use_source_cols;
    restore.show_foreground = ancestor_browser->show_foreground;
    restore.show_background = ancestor_browser->show_background;
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444

    /* 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) */
445

446
    ps_id.ancestor_id = NULL;
447

448 449
    printstyle_to_be_shown(0, NULL, &ps_id, NULL);
  }
450 451
  /* Register handlers for alternate Print/Cancel buttons */

452
  ChkError(event_register_toolbox_handler(window_id,
453
                                          EPStartPrint,
454 455
                                          print_start,
                                          (void *) ancestor_id));
456

457
  ChkError(event_register_toolbox_handler(window_id,
458
                                          EPCancelPrint,
459 460
                                          print_cancel,
                                          (void *) ancestor_id));
461 462 463

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

464
  if (!strcmp(lookup_control("AlterNumranges:no",0,0),"yes"))
465 466 467
  {
    _kernel_oserror       * e;
    WimpGetIconStateBlock   icon;
468 469
    int                     iconlist [Limits_NRangeIcons];
    char                    buffer   [Limits_Message];
470 471 472

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

473
    e = window_get_wimp_handle(0, window_id, &icon.window_handle);
474 475 476 477 478 479 480 481

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

      for (loop = 0; loop < 2; loop ++)
      {
482
        /* Get the number range's writable component ID */
483 484

        e = numberrange_get_components(NumberRange_GetComponents_ReturnNumericalField,
485
                                      window_id,
486
                                      loop == 1 ? PCopiesNum : PEndManyNum,
487 488 489 490 491 492 493
                                      &writable,
                                      NULL,
                                      NULL,
                                      NULL);

        /* Turn this into an icon handle */

494
        if (!e) e = gadget_get_icon_list(0, window_id, writable, iconlist, sizeof(iconlist), NULL);
495 496 497 498 499 500 501 502 503 504 505 506

        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)
          {
507
            strncpy(buffer, lookup_control("AlterWith",1,0), sizeof(buffer) - 1);
508 509 510 511 512 513 514 515 516
            buffer[sizeof(buffer) - 1] = 0;

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

517
  /* Register a handler to cope with the pages number range changing */
518

519 520 521 522
  ChkError(event_register_toolbox_handler(window_id,
                                          NumberRange_ValueChanged,
                                          print_check_contents,
                                          (void *) window_id));
523

524 525 526
  /* 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.                 */
527

528
  ChkError(event_register_toolbox_handler(window_id,
529
                                          EPEnableReformat,
530 531
                                          print_check_contents,
                                          (void *) window_id));
532

533
  /* Install an animation handler, if there's an appropriate gadget */
534

535
  if (
536
       controls.dbox_anims &&
537
       !gadget_get_type(0, window_id, StatusBarAnimAnim, NULL)
538 539 540 541
     )
     register_null_claimant(Wimp_ENull,
                            toolbars_animate_slow,
                            (void *) window_id);
542

543
  /* If defaults have never been set before, set them now */
544

545
  print_set_defaults();
546

547
  /* Make sure the Print Style dialogue is set up, too */
548

549
  printstyle_set_defaults();
550

551
  /* Done! */
552

553 554
  return 1;
}
555

556 557 558 559 560 561 562 563 564
/*************************************************/
/* print_start()                                 */
/*                                               */
/* Handles clicks on the 'OK' (or 'Print', etc.) */
/* button in the Print dialogue.                 */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/
565

566 567 568 569
static int print_start(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  /* First, make sure we effectively OK the contents of the */
  /* Print Style dialogue.                                  */
570

571
  printstyle_ok(0, NULL, NULL, NULL);
572

573 574 575
  /* 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.    */
576

577 578
  ChkError(print_read_contents(window_id, &contents));
  ChkError(print_close(0, 0));
579

580
  /* First stage of printing protocol: Broadcast a PrintSave message */
581

582
  ChkError(protocols_pp_send_print_save());
583

584 585
  return 1;
}
586

587 588 589 590 591 592 593 594 595
/*************************************************/
/* print_cancel()                                */
/*                                               */
/* Handles clicks on the 'Cancel' button in the  */
/* Print dialogue.                               */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/
596

597 598 599
static int print_cancel(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  WimpGetPointerInfoBlock info;
600

601
  /* Restore the old contents */
602

603
  ChkError(print_set_contents(window_id, &contents));
604

605 606
  /* If Select was pressed, the dialogue should close. */
  /* (No button => Escape was pressed).                */
607

608
  ChkError(wimp_get_pointer_info(&info));
609

610
  if ((info.button_state & Wimp_MouseButtonSelect) || !info.button_state)
611
  {
612
    ChkError(print_close(0, 0));
613

614
    /* If we forced background images off, put them back again */
615

616 617 618 619 620
    if (!strcmp(lookup_choice("PrintPlain:yes",0,0),"yes")) browser_set_look(ancestor_browser,
                                                                             window_id,
                                                                             -1,
                                                                             -1,
                                                                             -1,
621
                                                                             restore.show_background);
622
  }
623 624 625 626 627

  return 1;
}

/*************************************************/
628
/* print_close()                                 */
629
/*                                               */
630 631 632
/* If the Print dialogue is opened, this will    */
/* close it, deregistering any associated event  */
/* handlers.                                     */
633
/*                                               */
634 635 636 637 638 639 640
/* 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.           */
641 642
/*************************************************/

643
_kernel_oserror * print_close(ObjectId ancestor, int do_not_close)
644
{
645
  _kernel_oserror * e = NULL;
646

647
  if (ancestor && ancestor != ancestor_id) return NULL;
648

649
  /* If the Print Style window is open, this will close it */
650

651
  printstyle_close(ancestor_id, do_not_close);
652

653 654 655
  if (window_id)
  {
    /* Deregister associated event handlers */
656

657 658
    e = event_deregister_toolbox_handlers_for_object(window_id);
    if (e) goto print_close_exit;
659

660
    /* If there was a null handler, remove it */
661 662

    if (
663
         controls.dbox_anims &&
664
         !gadget_get_type(0, window_id, StatusBarAnimAnim, NULL)
665 666 667
       )
       deregister_null_claimant(Wimp_ENull,
                                toolbars_animate_slow,
668
                                (void *) window_id);
669

670 671 672
    if (!do_not_close)
    {
      /* Restore input focus to the browser window */
673

674 675 676 677 678
      if (ancestor_id)
      {
        e = browser_give_general_focus(ancestor_browser);
        if (e) goto print_close_exit;
      }
679

680
      /* Close the dialogue */
681

682 683
      e = toolbox_hide_object(0, self_id);
    }
684 685
  }

686
print_close_exit:
687

688
  self_id = window_id = 0;
689

690
  return e;
691 692 693
}

/*************************************************/
694
/* print_check_contents()                        */
695
/*                                               */
696 697 698 699 700 701 702 703
/* 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.                */
704 705
/*                                               */
/* Parameters are as standard for a Toolbox      */
706
/* event handler.                                */
707 708
/*************************************************/

709
static int print_check_contents(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
710
{
711 712
  int          state1, state2, pages;
  unsigned int flags;
713

714
  /* Get the radio button states */
715

716 717
  ChkError(radiobutton_get_state(0, window_id, PStartWhole, NULL, &state1));
  ChkError(radiobutton_get_state(0, window_id, PEndWhole,   NULL, &state2));
718

719 720 721
  /* If the PStartVisible and PEndVisible 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).     */
722

723
  ChkError(gadget_get_flags(0, window_id, PReformatToFit, &flags));
724

725
  if (state1 != PStartVisible && state2 != PEndVisible)
726
  {
727 728
    if (flags & Gadget_Faded)
    {
729
      ChkError(gadget_set_flags(0, window_id, PReformatToFit, flags & ~Gadget_Faded));
730 731 732 733 734 735
    }
  }
  else
  {
    if (!(flags & Gadget_Faded))
    {
736
      ChkError(gadget_set_flags(0, window_id, PReformatToFit, flags | Gadget_Faded));
737
    }
738 739
  }

740
  /* Check the pages number range, and update the label if necessary. */
741 742

  {
743
    char text[Limits_PEndManyLabel];
744

745
    ChkError(numberrange_get_value(0, window_id, PEndManyNum, &pages));
746

747
    ChkError(button_get_value(0, window_id, PEndManyLabel, text, Limits_PEndManyLabel, NULL));
748
    text[sizeof(text) - 1] = 0;
749

750 751 752 753
    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.           */
754

755 756
      if (strcmp(text, lookup_token("PagesSingle:sheet is filled",0,0)))
      {
757
        ChkError(button_set_value(0, window_id, PEndManyLabel, lookup_token("PagesSingle:sheet is filled",0,0)));
758 759 760 761 762
      }
    }
    else
    {
      /* Again, only change the text - don't set the same thing twice. */
763

764 765
      if (strcmp(text, lookup_token("PagesMany:sheets are filled",0,0)))
      {
766
        ChkError(button_set_value(0, window_id, PEndManyLabel, lookup_token("PagesMany:sheets are filled",0,0)));
767 768
      }
    }
769 770 771 772 773 774 775 776 777 778 779 780
  }

  return 1;
}

/*************************************************/
/* print_print()                                 */
/*                                               */
/* Calls the printing engine with parameters     */
/* specified in the local static print_info      */
/* structure 'print_current'.                    */
/*                                               */
781 782 783 784
/* Entry point is typically from a handler       */
/* dealing with the printing message protocol    */
/* (see handle_messages).                        */
/*                                               */
785 786 787
/* Parameters: Pointer to pathname to print to,  */
/*             or NULL to go straight to the     */
/*             'printer:' device.                */
788 789 790 791 792 793
/*************************************************/

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

794 795 796 797 798 799
  /* Must have a browser to print */

  if (!ancestor_browser) return;

  /* Do the printing */

800 801
  printing = 1;

802 803 804 805 806 807 808
  e = print_page(ancestor_browser,
                 contents.copies,
                 contents.start,
                 contents.end,
                 contents.pages,
                 contents.reformat,
                 contents.orientation,
809 810 811 812 813
                 path);

  printing = 0;

  if (e) show_error_ret(e);
814 815 816 817 818 819 820 821 822 823

  /* 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,
824 825 826 827
                                    restore.underline_links,
                                    restore.use_source_cols,
                                    restore.show_foreground,
                                    restore.show_background));
828
  }
829 830 831 832 833 834 835 836 837 838 839
}

/*************************************************/
/* 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;    */
840
/*                                               */
841
/*             The number of copies to print;    */
842
/*                                               */
843 844 845
/*             1 to start at the top of the web  */
/*             page, else start from the top of  */
/*             the visible area in the window;   */
846
/*                                               */
847 848 849 850 851 852
/*             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;                             */
853
/*                                               */
854 855 856 857 858
/*             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...!);         */
859
/*                                               */
860
/*             1 = portrait, 0 = landscape;      */
861 862 863 864
/*                                               */
/*             Pointer to pathname to print to,  */
/*             or NULL to go straight to the     */
/*             'printer:' device.                */
865 866
/*************************************************/

867 868
static _kernel_oserror * print_page(browser_data * b, int copies, int from, int end, int to,
                                    int reformat, int orientation, const char * path)
869 870 871 872
{
  _kernel_oserror       * e = NULL;

  WimpRedrawWindowBlock   redraw;
873

874
  BBox                    box, last_rect;
875 876 877 878 879 880 881 882 883 884 885

  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;

886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
  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)
  {
923 924
    Swap(tmarg,rmarg);
    Swap(bmarg,lmarg);
925 926
  }

927 928 929 930 931 932 933 934
  /* 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);
935 936 937 938

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

939
  if (reformat && end != End_Visible && from)
940
  {
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
    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;
970

971
    /* Now call the reformatter, and loop round until finished. */
972

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

976 977 978
    while (reformat_formatting(b))
    {
      reformat_reformatter(b);
979

980 981 982 983 984 985
      /* 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.                     */
986

987
      leds ^= 3;
988

989 990
      _swix(Hourglass_LEDs,
            _INR(0,1),
991

992 993
            3,
            leds);
994 995
    }

996
    _swix(Hourglass_LEDs, 0, 0);
997
  }
998
  else must_restore = 0;
999

1000
  localbrowser.use_source_cols = 0;
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022

  /* 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);

1023
    estimated_pages = (reformat_return_extent(b, NULL) / page_height) + 1;
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
  }

  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.                                    */

1057
    if (!choices.system_font)
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
    {
      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;
1100
    int                     htop, hbot;
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112

    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 */

1113
    if (!controls.swap_bars)
1114
    {
1115 1116
      htop = toolbars_button_height(b) + toolbars_url_height(b);
      hbot = toolbars_status_height(b);
1117 1118 1119
    }
    else
    {
1120 1121
      htop = toolbars_status_height(b);
      hbot = toolbars_button_height(b) + toolbars_url_height(b);
1122
    }
1123

1124 1125
    if (htop) htop += wimpt_dy();
    if (hbot) hbot += wimpt_dy();
1126

1127 1128
    top    += htop;
    bottom -= (htop + hbot - top);
1129

1130 1131 1132 1133
    /* 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;
1134
  }
1135 1136 1137 1138 1139 1140 1141 1142 1143

  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.                      */

1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
  page = 0;

  while (
          (
            end == End_Many &&
            page < to
          )
          ||
          (
            end != End_Many
          )
        )
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
  {
    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.                  */

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

1175
    if (end != End_Visible)
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
    {
      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 */

1285
      e = redraw_draw(b,
1286
                      &redraw,
1287
                      0,
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 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
                      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 */
1369 1370 1371
    /* 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.                               */
1372

1373
    if (end == End_Visible && !bottom) break;
1374 1375 1376 1377 1378 1379 1380

    /* 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;

1381 1382
    /* Otherwise, close the outer while loop - which */
    /* may mean we loop for another page.            */
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
  }

  /* 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;

1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
  /* 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);
  }
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488

  /* 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);

1489 1490 1491 1492
  /* Flag that printing has finished */

  printing = 0;

1493 1494 1495 1496
  /* Force the hourglass off */

  _swix(Hourglass_Smash, 0);

1497
  /* Put the browser back together again */
1498

1499 1500 1501 1502 1503
  if (must_restore)
  {
    print_restore_browser(b, &localbrowser);
    reformat_format_from(b, -1, 1, -1);
  }
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513

  /* Ensure images are still OK */

  image_mode_change();

  return e;

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

  signal(SIGINT, old_sigint_handler);
1514

1515
  print_abort_print();
1516 1517 1518 1519 1520 1521 1522 1523 1524

  printing = 0;

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

1525 1526 1527 1528
  image_mode_change();

  return e;

1529
out3: /* 'Emergency exit' for errors after printing */
1530 1531

  _swix(Hourglass_Smash, 0);
1532 1533 1534 1535 1536 1537 1538 1539 1540

  printing = 0;

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

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 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
  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);
}

/*************************************************/
1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601
/* 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;                      */
1602
/*                                               */
1603 1604
/*             Right hand page margin, in        */
/*             millipoints.                      */
1605 1606
/*************************************************/

1607
static void print_prepare_browser(browser_data * source, browser_data * store, int lmarg, int rmarg)
1608
{
1609 1610 1611
  store->previous        = source->previous;
  store->next            = source->next;
  source->previous       = source->next = NULL;
1612

1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
  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;
1625 1626 1627
}

/*************************************************/
1628
/* print_restore_browser()                       */
1629
/*                                               */
1630 1631 1632 1633
/* 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.                         */
1634
/*                                               */
1635 1636 1637 1638 1639 1640 1641 1642
/* 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.    */
1643 1644
/*************************************************/

1645
static void print_restore_browser(browser_data * original, browser_data * copy)
1646
{
1647 1648 1649 1650 1651 1652 1653
  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;
}
1654

1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685
/*************************************************/
/* 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.  */
/*************************************************/
1686

1687 1688 1689 1690 1691 1692
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;
1693
}