Windows 103 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   : Windows.c                              */
17
/*                                                 */
18 19
/* Purpose: Window related functions for the       */
/*          browser (open, close, create, etc.).   */
20
/*                                                 */
21
/* Author : A.D.Hodgkinson                         */
22 23 24
/*                                                 */
/* History: 20-Nov-96: Created.                    */
/*          15-Mar-97: Split up to form Browser.c. */
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/***************************************************/

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

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

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

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

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

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

#include "Browser.h"
50
#include "ChoiceDefs.h"
51 52
#include "Fetch.h" /* (Which itself includes URLstat.h) */
#include "FetchHTML.h"
53 54 55 56 57
#include "FetchPage.h"
#include "FontManage.h"
#include "Forms.h"
#include "Frames.h"
#include "Handlers.h"
58
#include "History.h"
59 60 61
#include "Images.h"
#include "Main.h"
#include "Memory.h"
62
#include "Menus.h"
63
#include "Meta.h"
64
#include "Object.h"
65
#include "OpenURL.h"
66
#include "PlugIn.h"
67
#include "Printing.h"
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
#include "Redraw.h"
#include "Reformat.h"
#include "Tables.h"
#include "Toolbars.h"
#include "URLutils.h"

#include "Windows.h"

/* Static function prototypes */

static void windows_check_reformat (browser_data * b, BBox * size, int old_width);

/* Locals */

static int title_height   = 0;
static int vscroll_width  = 0;
static int hscroll_height = 0;

/*************************************************/
/* windows_check_reformat()                      */
/*                                               */
/* After resizing, checks whether the contents   */
/* of a given browser need reformatting and      */
/* ensures tool presence is up to date.          */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the browser;          */
95
/*                                               */
96 97
/*             Pointer to a BBox describing the  */
/*             new visible area of the window;   */
98
/*                                               */
99 100 101 102 103 104 105 106 107 108 109 110 111 112
/*             The old display_width field of    */
/*             the browser_data struct to        */
/*             compare against the width defined */
/*             by the given BBox.                */
/*************************************************/

static void windows_check_reformat(browser_data * b, BBox * size, int old_width)
{
  /* If the width has changed, need to reformat. Notice that the  */
  /* horizontal extent is forced up to at least a certain minimum */
  /* value, but is never shrunk.                                  */

  if (size->xmax - size->xmin != old_width)
  {
113 114
    int h1, h2;

115 116 117
    b->display_extent = b->display_width = size->xmax - size->xmin;
    if (b->display_extent < MinimumWidth) b->display_extent = MinimumWidth;

118 119 120 121 122 123 124
    h1 = toolbars_button_height(b) + toolbars_url_height(b);
    h2 = toolbars_status_height(b);

    if (h1) h1 += wimpt_dy();
    if (h2) h2 += wimpt_dy();

    b->display_height = size->ymax - size->ymin - h1 - h2;
125

126 127 128 129
    if (b->ancestor || b->full_screen) windows_set_tools(b, size, !b->ancestor, 0, 0, 0);
    reformat_format_from(b, -1, 1, -1);
  }

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
// Blimey. This *really* needs sorting out. As for the windows_remember_size
// call - which duplicates this functionality more or less! - you should NOT
// need to redraw here. The display_width has not changed, so centered objects
// will not move. If it *has* changed, whatever changed it screwed up!

//  /* Otherwise just force a redraw (to make sure centred objects */
//  /* are OK) and check the tools are up to date.                 */
//
//  else
//  {
//    WimpGetWindowStateBlock state;
//
//    state.window_handle = b->window_handle;
//
//    if (!wimp_get_window_state(&state))
//    {
//      coords_box_toworkarea(&state.visible_area, (WimpRedrawWindowBlock *) &state);
//
//      wimp_force_redraw(state.window_handle,
//                        state.visible_area.xmin,
//                        state.visible_area.ymin,
//                        state.visible_area.xmax,
//                        state.visible_area.ymax);
//    }
//
//    windows_check_tools(b, NULL);
//  }
157 158 159 160 161 162 163 164 165 166 167 168 169
}

/*************************************************/
/* windows_new_browser()                         */
/*                                               */
/* Creates a new Browser window. Parameters are  */
/* as standard for a Toolbox event handler, with */
/* void * handle being NULL to open with the     */
/* current home page, else a pointer to a URL.   */
/*************************************************/

int windows_new_browser(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
170 171 172 173 174 175 176 177 178
  #ifndef SINGLE_USER

    /* If in a multiuser build and not logged in, claim */
    /* the event but don't do anything with it.         */

    if (!logged_in) return 1;

  #endif

179
  if (handle) ChkError(windows_create_browser((char *) handle, NULL, NULL, NULL, Windows_CreateBrowser_Normal));
180 181
  else
  {
182
    char home[Limits_URL];
183 184 185

    urlutils_create_home_url(home, sizeof(home));

186
    ChkError(windows_create_browser(home, NULL, NULL, NULL, Windows_CreateBrowser_Normal));
187 188 189 190 191
  }

  return 1;
}

192 193 194 195 196 197 198 199 200 201 202
/*************************************************/
/* windows_shut_browser()                        */
/*                                               */
/* Close a browser window. Closes the ancestor   */
/* and all child frames. Parameters are as       */
/* standard for a Toolbox event handler.         */
/*************************************************/

int windows_shut_browser(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  browser_data * b;
203
  ObjectId       browser = idb->ancestor_id;
204

205 206 207
  if (!browser) browser = idb->self_id;

  ChkError(toolbox_get_client_handle(0, browser, (void *) &b));
208 209 210 211 212 213 214 215 216 217 218
  if (!is_known_browser(b)) return 0;

  /* Always use the ancestor */

  b = utils_ancestor(b);

  /* Close it */

  return handle_close_browser(0, NULL, NULL, b);
}

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
/*************************************************/
/* windows_create_browser()                      */
/*                                               */
/* Creates and initialises a browser_data struct */
/* and may create an associated window object.   */
/*                                               */
/* Parameters: Pointer to a URL to fetch (or     */
/*             NULL to fetch none);              */
/*                                               */
/*             Pointer to a parent structure if  */
/*             this is a frame, else NULL;       */
/*                                               */
/*             Pointer to a BBox describing the  */
/*             visible area, or NULL to use      */
/*             default sizes and a window        */
/*             position based on the last        */
/*             default open position;            */
/*                                               */
/*             Pointer to the name for this      */
/*             window (for targetted URLs), or   */
/*             NULL for none. The string will be */
/*             copied to a browser local malloc  */
/*             block, so it can come from pretty */
/*             much anywhere that isn't going to */
/*             shift over a function call        */
244 245
/*             boundary (i.e. not flex!);        */
/*                                               */
246
/*             Fetch type value; see definitions */
247 248 249
/*             in Windows.h. Note that the       */
/*             ForUIElement type is not          */
/*             implemented - don't use it!       */
250 251
/*************************************************/

252 253
_kernel_oserror * windows_create_browser(const char * url, browser_data * real_parent,
                                         BBox * size, const char * name, int save_type)
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
{
  ObjectId          o;
  browser_data    * b;
  reformat_cell   * cell;
  _kernel_oserror * e = NULL;
  int               width, height;

  /* If no size BBox is given, use the Choices file specified sizes, */
  /* else use the sizes defined by the BBox.                         */

  if (!size) width = choices.width,           height = choices.height;
  else       width = size->xmax - size->xmin, height = size->ymax - size->ymin;

  /* malloc a block of space, treating it as holding a browser_data */
  /* structure. This is defined in global.h, and holds all the      */
  /* information needed with a given browser window (e.g. the       */
  /* document currently on display in that window, etc).            */

  #ifdef TRACE
    if (tl & (1u<<12))
    {
      Printf("windows_create_browser: memory_alloc_and_set %d for 'browser_data' structure\n",sizeof(browser_data));
      Printf("                                         and %d for 'reformat_cell' structure\n",sizeof(reformat_cell));
    }
  #endif

  b    = (browser_data  *) memory_alloc_and_set(sizeof(browser_data),  0);
  cell = (reformat_cell *) memory_alloc_and_set(sizeof(reformat_cell), 0);

  if (!b || !cell)
  {
    erb.errnum = Utils_Error_Custom_Message;

    StrNCpy0(erb.errmess,
             lookup_token("NoMemWin:There is not enough free memory to open another browser window.",
                          0,
                          0));

    return &erb;
  }
  else
  {
    #ifdef TRACE
      malloccount += (sizeof(browser_data) + sizeof(reformat_cell));
298
      if (tl & (1u<<13)) Printf("** malloccount (windows_create_browser): \0211%d\0217\n",malloccount);
299 300 301 302 303 304 305 306 307 308
    #endif

    /* The allocation succeeded, so create the object, fill in the browser_data */
    /* stucture, and link the stucture into the list of structures for all the  */
    /* browser windows. The toolbox is given a pointer to the structure as a    */
    /* client handle for the window, so whenever we get an event relating to    */
    /* the window this pointer is also supplied.                                */

    b->cell = cell;

309
    if (save_type != Windows_CreateBrowser_Normal && controls.use_small)
310 311 312 313 314 315 316 317
    {
      /* If the browser is created just to save an object, */
      /* build a 'mini' browser for just that.             */

      e = toolbox_create_object(0, "BrowsrFetch", &o);

      b->small_fetch = 1;
    }
318 319
    else
    {
320 321 322 323 324 325 326 327 328
      /* Otherwise, build a base browser, or if this is a frame, a child */
      /* browser object.                                                 */

      if (!real_parent) e = toolbox_create_object(0, "Browser", &o);
      else
      {
        if (nested_wimp) e = toolbox_create_object(0, "BrChild",     &o);
        else             e = toolbox_create_object(0, "BrChildBrdr", &o);
      }
329 330 331 332
    }

    if (e) return e;

333
    RetError(toolbox_set_client_handle(0, o, b));
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359

    /* Regardless of whether this is an ancestor browser window or a frame, */
    /* its next pointer must still be set to NULL.                          */

    b->next = NULL;

    /* Link this into the list of browser_data structures only if it's a parent   */
    /* browser window - i.e. not part of a frame set; there will be no parent     */
    /* browser_data struct passed into the function as this means you're a child. */

    b->previous = last_browser;
    if (last_browser) last_browser->next = b;
    last_browser = b;

    /* Sort out parent/child linking considerations */

    if (!real_parent)
    {
      b->ancestor    = NULL; /* If not a child, this is an ancestor object */
      b->parent      = NULL;
      b->real_parent = NULL;
    }
    else
    {
      /* Allocate memory in the array of children for this entry */

360
      RetError(memory_set_chunk_size(real_parent, NULL, CK_CHIL, (real_parent->nchildren + 1) * sizeof(browser_data *)));
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

      /* Add the entry - nchildren hasn't been incremented yet, so */
      /* [real_parent->nchildren] is correct for the new entry.    */

      real_parent->children[real_parent->nchildren] = b;

      /* Fill in the child's parent and ancestor fields */

      b->real_parent = real_parent;
      b->parent      = real_parent;
      b->ancestor    = real_parent->ancestor;

      if (!b->ancestor) b->ancestor = real_parent;

      /* Finally, increment the parent's number of children counter */

      real_parent->nchildren++;
    }

    b->self_id = o;

    #ifdef TRACE
      if (tl & (1u<<3))
      {
        Printf("\nCreated object ID %p\n",(void *) o);
        Printf("Associated structure is %p\n\n",(void *) b);
      }
    #endif

390
    /* Opening width considerations... */
391 392 393 394 395

    b->display_width = b->display_extent = width;

    if (b->display_extent < MinimumWidth) b->display_extent = MinimumWidth, b->display_width = MinimumWidth;

396 397 398 399
    /* ...and opening height */

    b->display_height = height;

400 401
    /* Default page layout information */

402 403 404 405 406
    b->left_margin  = choices.left_margin;
    b->right_margin = choices.right_margin;
    b->quote_margin = choices.quote_margin;
    b->leading      = choices.leading;
    b->left_indent  = choices.left_indent;
407

408 409 410 411 412
    /* Default encoding */

    b->encoding          = choices.encoding;
    b->encoding_priority = priority_default;

413
    /* Set up the deferred reformatter */
414

415 416
    b->refo_time = 0;
    b->refo_line = Reformat_UnrealisticallyHighLineNumber;
417 418 419

    /* Remember the Wimp window handle to save having to ask for it elsewhere */

420
    RetError(window_get_wimp_handle(0,o,&(b->window_handle)));
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435

    /* Allocate memory for the fetching URL and write that */
    /* URL into the block                                  */

    b->urlddata = NULL; /* No current displayed URL */

    /* If handle is non-zero treat as a pointer to a URL, else */
    /* fetch the Home Page.                                    */

    if (url)
    {
      #ifdef TRACE
        if (tl & (1u<<12)) Printf("windows_create_browser: Chunk CK_FURL set to %d\n",strlen(url) + 1);
      #endif

436
      RetError(memory_set_chunk_size(b, NULL, CK_FURL, strlen(url) + 1));
437 438 439 440 441 442 443 444 445 446 447 448

      strcpy(b->urlfdata, url);
    }

    /* If there's a given window name, set it */

    if (name && *name)
    {
      #ifdef TRACE
        if (tl & (1u<<12)) Printf("windows_create_browser: Chunk CK_NAME set to %d\n",strlen(name) + 1);
      #endif

449
      RetError(memory_set_chunk_size(b, NULL, CK_NAME, strlen(name) + 1));
450 451 452 453

      strcpy(b->window_name, name);
    }

454
    /* Set some local options according to the global Choices */
455

456 457 458 459 460
    b->underline_links = choices.underline_links;
    b->use_source_cols = choices.use_source_cols;
    b->show_foreground = choices.show_foreground;
    b->show_background = choices.show_background;

461 462
    /* Initialise various other fields */

463 464 465
    b->background_image = -1;
    b->full_screen      = !real_parent ? choices.full_screen : 0;
    b->full_size        = 0;
466 467 468

    if (save_type != Windows_CreateBrowser_Normal) b->save_link = 1;
    else                                           b->save_link = 0;
469 470 471

    /* Register some browser-specific Wimp event handlers */

472 473 474 475
    RetError(event_register_wimp_handler(o, Wimp_ERedrawWindow,  (WimpEventHandler *) windows_redraw_browser,   b));
    RetError(event_register_wimp_handler(o, Wimp_EOpenWindow,    (WimpEventHandler *) windows_open_browser,     b));
    RetError(event_register_wimp_handler(o, Wimp_EScrollRequest, (WimpEventHandler *) handle_scroll_requests,   b));
    RetError(event_register_wimp_handler(o, Wimp_EKeyPressed,    (WimpEventHandler *) handle_keys_from_browser, b));
476

477 478
    /* For toolbars and titles, need to be a parent (i.e. have no */
    /* parent browser_data struct, as that means you're a child). */
479 480 481

    if (!real_parent)
    {
482
      ObjectId ttop, tbot;
483
      int      scrap;
484
      char     text[Limits_URLBarWrit];
485

486 487 488 489 490 491 492 493 494
      if (!b->small_fetch)
      {
        /* Set the window title to contain the default URL for rendering. */

        RetError(window_set_title(0, o, lookup_token("BlankPage:Blank page",0,0)));
      }
      else
      {
        /* Set the window title to contain a default string for file fetching. */
495

496 497
        RetError(window_set_title(0, o, lookup_token("DataFetch:Fetching to file...",0,0)));
      }
498 499 500 501

      /* Set various toolbar flags up, and make */
      /* sure the right toolbars are visible.   */

502 503 504 505 506 507 508 509 510 511 512 513
      if (save_type != Windows_CreateBrowser_Normal && controls.use_small)
      {
        b->url_bar    = 1;
        b->button_bar = 1;
        b->status_bar = 1;
      }
      else
      {
        b->url_bar    = choices.url_bar;
        b->button_bar = choices.button_bar;
        b->status_bar = choices.status_bar;
      }
514 515 516

      /* Work out what toolbars are available */

517 518 519 520 521 522
      RetError(window_get_tool_bars(InternalTopLeft | InternalBottomLeft,
                                    o,
                                    &tbot,
                                    &ttop,
                                    NULL,
                                    NULL));
523 524 525 526 527

      if (!ttop) b->all_in_bottom = 1;
      if (!tbot) b->all_in_top = 1;

      /* Now make sure that ttop and tbot reflect the updated values in */
528
      /* light of the all_in_bottom/all_in_top/swap_bars flags.         */
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543

      ttop = toolbars_get_upper(b);
      tbot = toolbars_get_lower(b); /* (These are needed further down). */

      /* Read scroll bar choices */

      b->frame_hscroll = choices.h_scroll;
      b->frame_vscroll = choices.v_scroll;

      /* Only allow Wimp events to close a window if it's a parent; */
      /* otherwise, want entirely browser-driven control of when    */
      /* the child closes (for example, it Would Be Bad if some     */
      /* dodgy PD application that closed the window under the      */
      /* pointer on noticing some hotkey was activated on a child). */

544
      RetError(event_register_wimp_handler(o, Wimp_ECloseWindow, (WimpEventHandler *) handle_close_browser, b));
545 546 547 548 549 550

      /* Register event handlers for toolbar items, if a parent. Similarly, work out */
      /* what to do with toolbar buttons and writables, etc., only if a parent (a    */
      /* child as no toolbars).                                                      */

      if (ttop)
551
      {
552
        RetError(event_register_wimp_handler(ttop, Wimp_EMouseClick, (WimpEventHandler *) handle_clicks, b));
553 554 555 556

        /* If there's a dialler display field, install a long-term handler */
        /* to keep it up to date.                                          */

557
        e = gadget_get_type(0, ttop, URLBarDiallerStatus, &scrap);
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589

        if (!e)
        {
          if (!taskmodule_ds_registered)
          {
            /* Register the dialler's 'status changed' service call */
            /* with TaskModule. This allows a Wimp message to be    */
            /* sent whenever the status changes.                    */

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

                      0,
                      Service_DiallerStatus,
                      task_handle);

            /* If the above fails, don't bother reporting the error, */
            /* just skip the function - it is non-essential and the  */
            /* most likely cause of failure - unknown SWI - is taken */
            /* as an indicator that the dialler service is not       */
            /* needed.                                               */

            if (!e) taskmodule_ds_registered = 1;
          }
          else e = NULL;

          if (!e)
          {
            /* Message handler for the TaskModule's Message_Service message, */
            /* which is sent when the dialler sends out a 'status changed'   */
            /* service call following the above registration SWI call.       */

590
            RetError(event_register_message_handler(Message_Service, handle_dialler_service, b));
591 592 593 594 595 596 597
          }
        }
      }

      /* Handle border and colour control of the URL writable, status */
      /* field, and dialler display field.                            */

598
      if (ttop)
599
      {
600
        /* URL writable */
601

602
        RetError(windows_process_component_text(ttop, URLBarWrit, text, sizeof(text), 0, 1));
603

604
        /* Dialler status display field */
605

606
        RetError(windows_process_component_text(ttop, URLBarDiallerStatus, text, sizeof(text), 0, 1));
607
      }
608

609 610
      if (tbot)
      {
611 612
        int bytes_flags;

613
        /* Main status display field */
614

615
        RetError(windows_process_component_text(tbot, StatusBarStatus, text, sizeof(text), 0, 1));
616 617 618

        /* Progress indicator */

619
        RetError(windows_process_component_text(tbot, StatusBarProgress, text, sizeof(text), 0, 1));
620

621
        /* Remember whatever foreground colour the StatusBarProgress gadget now has */
622

623
        if (!button_get_flags(0, tbot, StatusBarProgress, &bytes_flags));
624 625 626
        {
          b->progress_colour = (bytes_flags & 0x0f000000) >> 24;
        }
627 628 629 630 631 632 633 634 635 636 637 638 639
      }

      /* Are the URL writable and status display to be merged? */
      /* Can only merge them if the toolbars are merged.       */
      /*                                                       */
      /* Take this opportunity to look for bistate and         */
      /* tristate button definitions, in the same way.         */

      if (b->all_in_top || b->all_in_bottom)
      {
        /* If the toolbars are merged, it doesn't matter if we try */
        /* to get the upper or lower toolbar object ID - the       */
        /* returned result will be the same.                       */
640 641
        /*                                                         */
        /* Can only merge things if there's a toolbar to look at!  */
642

643
        if (ttop)
644 645 646 647 648 649 650 651 652 653
        {
          /* See if the URL writable and status displays are to be merged. */

          {
            BBox sta, url;

            /* If finding the BBox of either generates an error, can't */
            /* say they're to be merged.                               */

            if (
654 655
                 !gadget_get_bbox(0, ttop, URLBarWrit,   &url) &&
                 !gadget_get_bbox(0, ttop, StatusBarStatus, &sta)
656 657 658 659 660 661 662 663 664 665 666 667 668
               )
            {
              /* Otherwise, compare their bounding boxes. If identical, */
              /* the two are to be merged.                              */

              if (
                   url.xmin == sta.xmin &&
                   url.ymin == sta.ymin &&
                   url.xmax == sta.xmax &&
                   url.ymax == sta.ymax
                 )
              {
                b->merged_url = 1;
669
                toolbars_merged_to_status(b, ttop);
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
              }

            /* Closure of long 'if' checking that the bounding boxes of */
            /* the URL writable and status display fields were obtained */
            /* without error (the code above excutes if this is true).  */
            }
          }

          /* See if there are any recognised bistate buttons present. */
          /* Code is similar to the above URL/status merge code.      */

          {
            BBox s1, s2;
            int  scrap;

685 686
            /* Must have a working gadget, component ID ButtonBarBistate, */
            /* to use the bistate button.                                 */
687

688
            if (!gadget_get_type(0, ttop, ButtonBarBistate, &scrap))
689 690 691 692
            {
              /* Check for type BiState_Cancel_Back */

              if (
693 694
                   !gadget_get_bbox(0, ttop, ButtonBarCancel, &s1) &&
                   !gadget_get_bbox(0, ttop, ButtonBarBack,   &s2)
695 696 697 698 699 700 701 702 703 704 705 706 707
                 )
              {
                if (
                     s1.xmin == s2.xmin &&
                     s1.ymin == s2.ymin &&
                     s1.xmax == s2.xmax &&
                     s1.ymax == s2.ymax
                   )
                {
                  b->bistate = BiState_Cancel_Back;

                  /* Ensure the reference gadgets are hidden */

708 709
                  hide_gadget(ttop, ButtonBarBack);
                  hide_gadget(ttop, ButtonBarCancel);
710 711 712

                  /* Set the default starting state to 'Back' */

713
                  toolbars_set_bistate_state(b, ttop, 1);
714 715 716 717 718 719 720 721 722 723 724 725
                }
              }
            }
          }

          /* See if there are any recognised tristate buttons present. */
          /* Code is similar to the above bistate button code.         */

          {
            BBox s1, s2, s3;
            int  scrap;

726 727
            /* Must have a working gadget, component ID ButtonBarTristate, */
            /* to use the tristate button.                                 */
728

729
            if (!gadget_get_type(0, ttop, ButtonBarTristate, &scrap))
730 731 732 733
            {
              /* Check for type TriState_Go_GoTo_Stop */

              if (
734 735 736
                   !gadget_get_bbox(0, ttop, ButtonBarGo,   &s1) &&
                   !gadget_get_bbox(0, ttop, ButtonBarGoTo, &s2) &&
                   !gadget_get_bbox(0, ttop, ButtonBarStop, &s3)
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
                 )
              {
                if (
                     s1.xmin == s2.xmin &&
                     s1.ymin == s2.ymin &&
                     s1.xmax == s2.xmax &&
                     s1.ymax == s2.ymax &&
                     s1.xmin == s3.xmin &&
                     s1.ymin == s3.ymin &&
                     s1.xmax == s3.xmax &&
                     s1.ymax == s3.ymax
                   )
                {
                  b->tristate = TriState_Go_GoTo_Stop;

                  /* Ensure the reference gadgets are hidden */

754 755 756
                  hide_gadget(ttop, ButtonBarGoTo);
                  hide_gadget(ttop, ButtonBarGo);
                  hide_gadget(ttop, ButtonBarStop);
757 758 759

                  /* If fetching, set the tristate to 'stop', else 'go to'. */

760 761
                  if (url) toolbars_set_tristate_state(b, ttop, 2);
                  else     toolbars_set_tristate_state(b, ttop, 1);
762 763 764 765 766 767 768 769 770 771 772 773 774
                }
              }
            }
          }

        /* Closure of long 'if' checking the upper toolbar object ID */
        /* was non-zero. If so, the code above executes.             */
        }

      /* Closure of long 'if' checking that the upper and lower toolbars */
      /* are merged into one; the code above executes if so.             */
      }

775 776 777 778 779 780 781
      /* Set the default status. Must do this after the */
      /* routines that process icon text etc. have been */
      /* run, or the status routines could trash a      */
      /* special icon string before it's dealt with...  */

      toolbars_update_status(b, Toolbars_Status_Ready);

782 783 784 785 786 787
    /* Closure of long 'if' checking if the window was a parent. The */
    /* code above executes if so.                                    */
    }

    /* Handler for on-page clicks */

788 789 790 791
    RetError(event_register_wimp_handler(o,
                                         Wimp_EMouseClick,
                                         (WimpEventHandler *) handle_link_clicks,
                                         b));
792 793 794 795 796 797 798 799

    /* Open the window and register a Wimp handler that will be called   */
    /* whenever there is an Open Window request generated for the window */
    /* (this deals with window extent settings etc.).                    */

    if (!size) b->small_size.visible_area.xmin = b->small_size.visible_area.ymin = -1;
    else       b->small_size.visible_area      = *size;

800
    /* This call will fill in small_size depending on the above. */
801

802
    if (save_type != Windows_CreateBrowser_ForPlugIn || choices.see_fetches) windows_show_browser(b);
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817

    /* Ensure the URL bar is up to date, and set the fetch status to BS_START. */

    if (url)
    {
      toolbars_update_url(b);
      b->fetch_status = BS_START;
    }

    /* Must be called after windows_show_browser so the parent/ancestor IDs are set up */

    if (!real_parent)
    {
      BBox w;

818
      toolbars_set_presence(b, InternalTopLeft | InternalBottomLeft);
819 820 821 822

      /* Ensure that the window extent is at least as wide and tall */
      /* as the minimum extent settings require.                    */

823 824 825
      if (!b->small_fetch)
      {
        RetError(window_get_extent(0,o,&w));
826

827 828
        if ((w.xmax - w.xmin) < MinimumWidth) w.xmin = 0, w.xmax = MinimumWidth;
        if ((w.ymax - w.ymin) < MinimumHeight) w.ymin = -MinimumHeight, w.ymax = 0;
829

830
        RetError(window_set_extent(0,o,&w));
831

832
        /* Give the window the input focus, if it's an ancestor */
833

834 835
        RetError(browser_give_general_focus(b));
      }
836 837
    }

838
    /* Start a fetch for this page's current URL. Note the allow_cancel */
839 840
    /* flag is unset, so that any flags set up above, which cancel      */
    /* functions may touch (e.g. save_link), will not be altered.       */
841

842
    if (url) e = fetchpage_new(b, b->urlfdata, 0, 0);
843

844 845 846 847 848 849 850 851 852 853
    else if (b->small_fetch)
    {
      /* For a small fetch window, we may have no URL (Plug-Ins - already */
      /* have the data in a file, and want the fetcher to be kicked       */
      /* briefly to notice the fetch is finished before it even started   */
      /* and fire off the relevant messages).                             */

      fetchpage_claim_nulls(b);
    }

854 855
    /* Now ensure that future fetches may call fetch_cancel */
    /* before proceeding.                                   */
856 857 858

    b->allow_cancel = 1;

859
    return e;
860 861 862 863

  /* Closure of long 'if' ensuring the mallocs for the browser_data */
  /* and associated reformat_cell structures were successful - the  */
  /* code above only executes if so.                                */
864 865 866 867 868 869 870 871 872 873
  }
}

/*************************************************/
/* windows_open_browser()                        */
/*                                               */
/* (Re)opens a Browser window, setting extent if */
/* necessary. Parameters are as standard for a   */
/* Wimp event handler.                           */
/*                                               */
874 875 876 877 878 879 880 881 882 883
/* Assumes:    The poll block and ID block       */
/*             pointers may be NULL, though in   */
/*             the latter case only for full     */
/*             screen opening;                   */
/*                                               */
/*             The event code is not read and    */
/*             can by any integer value;         */
/*                                               */
/*             That the browser_data pointer is  */
/*             not NULL.                         */
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
/*************************************************/

int windows_open_browser(int eventcode, WimpPollBlock * b, IdBlock * idb, browser_data * handle)
{
  /* Internally...                                                             */
  /*                                                                           */
  /* 's' holds the browser window state, and is a WimpGetWindowStateBlock      */
  /* 'w' holds various bits of data, and is a BBox. It always ends up holding  */
  /*     information for setting the window extent.                            */
  /* 't' is a BBox holding the browser window's initial extent.                */
  /* 'o' holds the current window outline, and is a WimpGetWindowOutlineBlock. */
  /*     It's used for the part of the code dealing with toggle size.          */
  /* 'i' is a WimpGetPointerInfoBlock and is briefly used in the toggle size   */
  /*     code to see if Adjust was pressed, since then you don't want to bring */
  /*     the window to the front.                                              */
  /*                                                                           */
  /* The Wimp will have sent a poll block containing open_window_request       */
  /* data; this is in 'b'. The toolbox will have filled in an ID block 'idb'   */
  /* and the routine is called with 'handle' containing a pointer to a         */
  /* browser_data structure (though it has to be declared as a void * rather   */
  /* than browser_data * for the function to be a Wimp event handler, hence    */
  /* all the casting that goes on).                                            */

  WimpGetWindowStateBlock s;
  BBox                    w, t;

  /* One simple case is Full Screen handling. The '!idb' check is */
  /* used for external functions which want to go to full screen  */
  /* mode; they can call this function with 0, NULL, NULL and the */
  /* browser_data pointer (with full_screen set to 1, as this is  */
  /* needed generally) without worrying that part of idb may be   */
  /* needed within the function for some reason - it is used      */
  /* further down and if conditions on this first 'if' failed,    */
  /* there would be a problem if idb were not filled in.          */

  if ((handle->full_screen && modechanged) || !idb)
  {
    WimpGetWindowStateBlock state;

    if (modechanged)
    {
      windows_open_full_screen(handle,
                               1,
                               0,
                               choices.v_scroll,
                               choices.h_scroll);

      modechanged = 0;
    }

    /* Reformat if required */

    state.window_handle = handle->window_handle;
    ChkError(wimp_get_window_state(&state));

939
    ChkError(frames_resize_frameset(handle, &state.visible_area));
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961

    windows_check_reformat(handle, &state.visible_area, handle->display_width);
  }
  else
  {
    int old_display_width;

    /* Remember the current visible area width for use later, */
    /* to see if a page reformat is needed.                   */

    old_display_width = handle->display_width;

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

    ChkError(window_get_extent(0,idb->self_id,&t));

    /* Was this a toggle size event? If so, handle manually */

    if ((s.flags & WimpWindow_Toggled) != 0)
    {
      WimpGetWindowOutlineBlock o;
962
      int                       shift, ctrl, cmos;
963 964 965 966 967

      /* First check if Shift is pressed; the 'shift' variable will hold  */
      /* 255 if pressed or 0 if not. This is because the full size window */
      /* shouldn't obscure the icon bar if shift is held down.            */

968 969 970 971 972 973 974
      _swix(OS_Byte,
            _INR(0,1) | _OUT(1),

            121,
            128,

            &shift);
975

976 977 978 979 980 981 982 983 984 985 986
      /* Similarly check for Control being held down. We'll only increase */
      /* the vertical size if this is the case.                           */

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

            121,
            129,

            &ctrl);

987
      /* Check CMOS RAM byte 28, bit 4 - if set, reverse the sense of Shift */
988

989 990 991 992 993 994 995 996
      _swix(OS_Byte,
            _INR(0,1) | _OUT(2),

            161,
            28,

            &cmos);

997
      if (cmos & (1u<<4)) shift = shift ? 0 : 255;
998 999 1000

      /* Read the screen size in OS units */

1001 1002 1003 1004 1005
      w.xmin = bbc_modevar(-1, BBC_XEigFactor);
      w.ymin = bbc_modevar(-1, BBC_YEigFactor);
      w.xmax = bbc_modevar(-1, BBC_XWindLimit);
      w.ymax = bbc_modevar(-1, BBC_YWindLimit);

1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
      w.xmax = (w.xmax + 1) << w.xmin;
      w.ymax = (w.ymax + 1) << w.ymin;

      /* This isn't good enough, as we need to take account of the window */
      /* frame and tools. Since the window must already be open, we can   */
      /* use Wimp_GetWindowOutline and compare this to the visible size,  */
      /* where the difference is the size of borders and tools.           */

      o.window_handle = b->open_window_request.window_handle;
      ChkError(wimp_get_window_outline(&o));

      w.xmin = (o.outline.xmax - o.outline.xmin) - (s.visible_area.xmax - s.visible_area.xmin);
      w.ymin = (o.outline.ymax - o.outline.ymin) - (s.visible_area.ymax - s.visible_area.ymin);

      /* Now have the information on the window dimensions to occupy the */
      /* whole screen.                                                   */

      if (!handle->full_size)
      {
1025 1026
        int h1, h2;

1027
        /* Toggle up to full size. First, remember the current (small) size. */
1028 1029 1030 1031 1032 1033 1034

        handle->small_size.visible_area = s.visible_area;
        handle->small_size.xscroll      = s.xscroll;
        handle->small_size.yscroll      = s.yscroll;
        handle->small_size.behind       = find_behind(b->open_window_request.window_handle);

        /* Set the poll block contents to open the window in the right place. */
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
        /* Notice if Shift is held down and don't cover icon bar, and if Ctrl */
        /* is held down we leave the X positions alone.                       */

        if (!ctrl)
        {
          b->open_window_request.visible_area.xmin = 2;
          b->open_window_request.visible_area.xmax = w.xmax - w.xmin + 2;
        }
        else
        {
          b->open_window_request.visible_area.xmin = s.visible_area.xmin;
          b->open_window_request.visible_area.xmax = s.visible_area.xmax;

          w.xmin = s.visible_area.xmin;
          w.xmax = s.visible_area.xmax;
        }
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

        b->open_window_request.visible_area.ymin = s.visible_area.ymin - o.outline.ymin + (shift ? 128 : 0);
        b->open_window_request.visible_area.ymax = w.ymax - w.ymin + (s.visible_area.ymin - o.outline.ymin);

        /* Is adjust being pressed? If so, want to open at the same place */
        /* in the window stack as at present. NB, the Wimp seems to take  */
        /* Select as higher priority than Adjust, so if both are pressed  */
        /* the window is brought to the front. This is why the code looks */
        /* at the state of both buttons.                                  */

        {
          WimpGetPointerInfoBlock i;

          ChkError(wimp_get_pointer_info(&i));

          if (
               (i.button_state & Wimp_MouseButtonAdjust) != 0 &&
               (i.button_state & Wimp_MouseButtonSelect) == 0
             )

             b->open_window_request.behind = s.behind;

          else b->open_window_request.behind = -1;
        }

1076 1077 1078 1079 1080
        /* Ensure horizontal and vertical extent isn't too small, and don't ever */
        /* shrink them - only reformat_set_extent is allowed to do that, in      */
        /* Reformat.c. The exception case is if the parent window has child      */
        /* frames, in which case we want the extent to track the visible area    */
        /* exactly, at all times.                                                */
1081

1082
        if (!handle->nchildren)
1083 1084 1085
        {
          BBox extent;

1086 1087
          if (w.xmax - w.xmin < handle->display_extent) w.xmin = 0, w.xmax = handle->display_extent;

1088 1089 1090 1091 1092 1093
          window_get_extent(0,idb->self_id,&extent);

          if (w.ymax - w.ymin < extent.ymax - extent.ymin) w.ymax = w.ymin + extent.ymax - extent.ymin;
          if (w.ymax - w.ymin < MinimumHeight)             w.ymax = w.ymin + MinimumHeight;
        }

1094
        ChkError(set_corrected_extent(0, idb->self_id, &w));
1095 1096 1097

        /* Show the object */

1098 1099 1100 1101 1102 1103
        ChkError(toolbox_show_object(0,
                                     idb->self_id,
                                     1,
                                     &(b->open_window_request.visible_area),
                                     idb->parent_id,
                                     -1));
1104 1105 1106 1107 1108 1109 1110

        /* Flag that the window is full size */

        handle->full_size = 1;

        /* Make sure the toolbars, if present, are up to date */

1111
        if (choices.move_gadgets != Choices_MoveGadgets_Never) toolbars_move_gadgets(handle);
1112

1113 1114 1115 1116
        /* Update the visible area width and height fields */

        handle->display_width  = b->open_window_request.visible_area.xmax -
                                 b->open_window_request.visible_area.xmin;
1117

1118 1119 1120 1121 1122 1123
        h1 = toolbars_button_height(handle) + toolbars_url_height(handle);
        h2 = toolbars_status_height(handle);

        if (h1) h1 += wimpt_dy();
        if (h2) h2 += wimpt_dy();

1124
        handle->display_height = b->open_window_request.visible_area.ymax -
1125 1126 1127
                                 b->open_window_request.visible_area.ymin -
                                 h1                                       -
                                 h2;
1128 1129 1130

        /* Resize any child frames and reformat if necessary */

1131
        ChkError(frames_resize_frameset(handle, &b->open_window_request.visible_area));
1132 1133 1134 1135
        windows_check_reformat(handle, &b->open_window_request.visible_area, old_display_width);
      }
      else
      {
1136 1137
        int h1, h2;

1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
        w = handle->small_size.visible_area;

        /* An irritating feature of the Wimp - if you call Wimp_SetExtent, */
        /* then the next Wimp_OpenWindow call is forced on-screen. This    */
        /* means that if toggle size is used when the window is partially  */
        /* dragged off-screen, then the shrinking horizontal extent when   */
        /* it is toggled back would ensure that the window was brought     */
        /* back fully on screen. Sometimes this is annoying.               */
        /*                                                                 */
        /* It turns out though, that if the window is *already* partially  */
        /* off-screen when the SetExtent call is made, the Wimp brings the */
        /* window (internally) back on screen, and doesn't bother to do    */
        /* this on the next OpenWindow call. So to allow the window to be  */
        /* toggled back to a small size which may be partially off-screen, */
        /* we open the window miles off screen somewhere before setting    */
        /* the extent.                                                     */
        /*                                                                 */
        /* This is not pretty but it is relatively solid; the worst that   */
        /* can happen is the Wimp *does* decide to force the window back   */
        /* on screen at the next OpenWindow call, in which case the        */
        /* behaviour is as it would be without the extra code here.        */

        b->open_window_request.visible_area.xmin = w.xmin + 16384;
        b->open_window_request.visible_area.ymin = w.ymin - 16384;
        b->open_window_request.visible_area.xmax = w.xmax + 16384;
        b->open_window_request.visible_area.ymax = w.ymax - 16384;
        b->open_window_request.behind            = handle->small_size.behind;

1166 1167 1168 1169 1170 1171
        toolbox_show_object(0,
                            idb->self_id,
                            1,
                            &(b->open_window_request.visible_area),
                            idb->parent_id,
                            -1);
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182

        /* Store the new details in b for later use in the show_object call */

        b->open_window_request.visible_area = w;
        b->open_window_request.xscroll      = handle->small_size.xscroll;
        b->open_window_request.yscroll      = handle->small_size.yscroll;
        b->open_window_request.behind       = handle->small_size.behind;

        /* Set the extent, with usual restrictions on when not to shrink it (see */
        /* elsewhere for more information, around similar pieces of code)        */

1183
        if (!handle->nchildren)
1184 1185 1186
        {
          BBox extent;

1187 1188
          if (w.xmax - w.xmin < handle->display_extent) w.xmin = 0, w.xmax = handle->display_extent;

1189 1190 1191 1192 1193 1194
          window_get_extent(0,idb->self_id,&extent);

          if (w.ymax - w.ymin < extent.ymax - extent.ymin) w.ymax = w.ymin + extent.ymax - extent.ymin;
          if (w.ymax - w.ymin < MinimumHeight)             w.ymax = w.ymin + MinimumHeight;
        }

1195 1196
        ChkError(set_corrected_extent(0, idb->self_id, &w));

1197 1198 1199 1200 1201 1202
        ChkError(toolbox_show_object(0,
                                     idb->self_id,
                                     1,
                                     &(b->open_window_request.visible_area),
                                     idb->parent_id,
                                     -1));
1203 1204 1205 1206 1207

        handle->full_size = 0;

        /* Make sure the toolbars, if present, are up to date */

1208
        if (choices.move_gadgets != Choices_MoveGadgets_Never) toolbars_move_gadgets(handle);
1209

1210
        /* Update the visible area width and height fields */
1211 1212 1213 1214

        handle->display_width  = b->open_window_request.visible_area.xmax -
                                 b->open_window_request.visible_area.xmin;

1215 1216 1217 1218 1219 1220
        h1 = toolbars_button_height(handle) + toolbars_url_height(handle);
        h2 = toolbars_status_height(handle);

        if (h1) h1 += wimpt_dy();
        if (h2) h2 += wimpt_dy();

1221
        handle->display_height = b->open_window_request.visible_area.ymax -
1222 1223 1224
                                 b->open_window_request.visible_area.ymin -
                                 h1                                       -
                                 h2;
1225

1226 1227
        /* Resize any child frames and reformat if needed */

1228
        ChkError(frames_resize_frameset(handle, &b->open_window_request.visible_area));
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
        windows_check_reformat(handle, &b->open_window_request.visible_area, old_display_width);
      }
    }

    /* This isn't a Toggle Size event; so move or resize. Whilst the   */
    /* Toggle Size code above remembers the small size of a window if  */
    /* toggling up to full screen, this isn't enough as the user can   */
    /* drag the window to full size, particularly if the machine has   */
    /* been configured to allow windows off screen to the bottom and   */
    /* right of the screen. In this case, dragging the resize icon of  */
    /* a window off screen shuffles the window up and/or left, so it   */
    /* becomes easy to drag out to full screen. In this case, we don't */
    /* know what the small size of the window was before the drag      */
    /* began, and so we can't return to it when the toggle size icon   */
    /* is used - you end up with the *previously stored* small size.   */
    /*                                                                 */
    /* To get round this, we need to install a null event handler when */
    /* a window drag is detected. As soon as a null event is received  */
    /* without a mouse button being pressed, the drag must have ended. */
    /* We can then check the size of the window, and if it isn't full  */
    /* screen, remember that size for toggle size later on. It's very  */
    /* long winded but that's what you get for taking over the window  */
    /* handling to such an extent...!                                  */

    else
    {
      /* For child frames (i.e. browser_data structs with a filled in ancestor field), */
      /* this function will only be called from frames_resize_frames itself. This in   */
      /* turn is originally only called if the ancestor window resizes in some way.    */
      /* It's not possible to apply the 'has the visible area changed' check as        */
      /* rounding errors can lead to some frames just shuffling a bit, not resizing,   */
      /* and any children they had would then not be resized (or rather, moved) to fit */
      /* the parent.                                                                   */
      /*                                                                               */
      /* We would in theory want to resize (and implicitly move) any frames even if    */
      /* the parent window moves, but doesn't resize. In the case of the nested Wimp   */
      /* though, the children are moved for you and moving them again produces some    */
      /* very odd effects! Consequently there are checks for the nested_wimp flag in   */
      /* the code.                                                                     */

      if (handle->ancestor || !nested_wimp)
      {
1272
        ChkError(frames_resize_frameset(handle, &b->open_window_request.visible_area));
1273 1274 1275 1276
      }

      /* Claim null events, if they aren't already being claimed */

1277
      if (!handle->watching_resize)
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
      {
        register_null_claimant(Wimp_ENull,(WimpEventHandler *) windows_remember_size,(void *) idb->self_id);
        handle->watching_resize = 1;
      }

      /* If the current and Wimp requested visible areas are the same in */
      /* terms of horizontal and vertical size, the window is being      */
      /* moved; so don't do any extent setting stuff. However, if the    */
      /* Force On Screen bit is set, the then window has been shrunk or  */
      /* moved due to a mode change, so make sure the extent is right    */
      /* and the toolbars are set up correctly. Because the Wimp won't   */
      /* actually set the window size up for us until the next call to   */
      /* WimpOpenWindow, we need a separate handler for this case.       */

1292 1293 1294 1295 1296
      if (
           b->open_window_request.visible_area.xmax - b->open_window_request.visible_area.xmin != s.visible_area.xmax - s.visible_area.xmin ||
           b->open_window_request.visible_area.ymax - b->open_window_request.visible_area.ymin != s.visible_area.ymax - s.visible_area.ymin ||
           modechanged
         )
1297
      {
1298 1299 1300
        /* See comments on the previous line that called frames_resize_frameset; */
        /* for the ancestor, want to only resize frames if the ancestor resized, */
        /* unless in a non-nested Wimp variant.                                  */
1301 1302 1303

        if (nested_wimp && !handle->ancestor)
        {
1304
          ChkError(frames_resize_frameset(handle, &b->open_window_request.visible_area));
1305 1306 1307 1308 1309 1310 1311
        }

        if (modechanged)
        {
          /* If there's been a mode change, call Wimp_OpenWindow to get the window's */
          /* correct visible area and then sort out the correct extent.              */

1312 1313 1314 1315 1316 1317 1318
          toolbox_show_object(0,
                              idb->self_id,
                              1,
                              &(b->open_window_request.visible_area),
                              idb->parent_id,
                              -1);

1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
          wimp_get_window_state(&s);

          w = s.visible_area;
        }
        else
        {
          /* The requested visible area is different, so consider this to be a window */
          /* resize event. In this case, set the horizontal extent to the visible     */
          /* area, provided this means the extent has grown.                          */

          w = b->open_window_request.visible_area;
        }

        /* Set the extent, with usual restrictions on when not to shrink it (see */
        /* elsewhere for more information, around similar pieces of code)        */

1335
        if (!handle->nchildren)
1336 1337
        {
          BBox extent;
1338 1339
//Printf("woo, resizing\n");
          if (w.xmax - w.xmin < handle->display_extent) w.xmin = 0, w.xmax = handle->display_extent;
1340 1341 1342

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

1343 1344
          if (w.ymax - w.ymin < extent.ymax - extent.ymin) w.ymax = w.ymin + extent.ymax - extent.ymin;
          if (w.ymax - w.ymin < MinimumHeight)             w.ymax = w.ymin + MinimumHeight;
1345 1346
        }

1347
        ChkError(set_corrected_extent(0, idb->self_id, &w));
1348 1349 1350 1351 1352

        if (modechanged) modechanged = 0;

        /* Show the object */

1353 1354 1355 1356 1357 1358
        toolbox_show_object(0,
                            idb->self_id,
                            1,
                            &(b->open_window_request.visible_area),
                            idb->parent_id,
                            -1);
1359 1360 1361

        /* Ensure toolbar gadgets are up to date */

1362
        if (choices.move_gadgets == Choices_MoveGadgets_During) toolbars_move_gadgets(handle);
1363 1364 1365 1366

        /* If an ancestor is highlighted, remove the highlight */

        if (!handle->ancestor && handle == highlight_frame) frames_remove_highlight();
1367 1368 1369 1370 1371
      }
      else
      {
        /* Nothing has altered size; just move the window */

1372 1373 1374 1375 1376 1377
        toolbox_show_object(0,
                            idb->self_id,
                            1,
                            &(b->open_window_request.visible_area),
                            idb->parent_id,
                            -1);
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
      }
    }
  }

  return 1;
}

/*************************************************/
/* windows_remember_size()                       */
/*                                               */
/* This event handler is registered when an Open */
/* Window request is received by the event       */
1390 1391
/* handler windows_open_browser, which should be */
/* referred to for more information. This        */
1392 1393 1394 1395 1396
/* handler is called on null polls, and when no  */
/* mouse buttons are being pressed, deregisters  */
/* itself and looks at the browser window it is  */
/* associated with. If the window is not open    */
/* full size, the size is remembered so that the */
1397 1398
/* toggle size code in windows_open_browser can  */
/* operate correctly.                            */
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
/*************************************************/

int windows_remember_size(int eventcode, WimpPollBlock * b, IdBlock * idb, void * handle)
{
  WimpGetPointerInfoBlock   p;
  browser_data            * d;

  #ifdef TRACE
    if (tl & (1u<<3)) Printf("windows_remember_size: Called\n");
  #endif

  /* First thing - move the gadgets, if the choices indicate that */
  /* we should, to fit the current window size.                   */

  ChkError(toolbox_get_client_handle(0, (ObjectId) handle, (void *) &d));

  /* Is this a valid pointer? If not, exit */

  if (!is_known_browser(d)) return 0;

  /* Otherwise, proceed */

1421
  if (choices.move_gadgets == Choices_MoveGadgets_AtEnd) toolbars_move_gadgets(d);
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

  /* Are buttons being pressed? */

  ChkError(wimp_get_pointer_info(&p));

  /* If not... */

  if (!p.button_state)
  {
    WimpGetWindowOutlineBlock o;
    WimpGetWindowStateBlock s;
    BBox w;

    /* Deregister the null event handler and flag that we */
    /* are not watching resize events anymore.            */

    deregister_null_claimant(Wimp_ENull,(WimpEventHandler *) windows_remember_size,handle);
    d->watching_resize = 0;

    /* Get the window state (of interest is the visible area) */
    /* and it's outline. This is so we can later work out the */
    /* size of the window's tools.                            */

    s.window_handle = d->window_handle;
    o.window_handle = s.window_handle;
    ChkError(wimp_get_window_state(&s));
    ChkError(wimp_get_window_outline(&o));

    /* Read the screen size in OS units */

1452 1453 1454 1455 1456
    w.xmin = bbc_modevar(-1, BBC_XEigFactor);
    w.ymin = bbc_modevar(-1, BBC_YEigFactor);
    w.xmax = bbc_modevar(-1, BBC_XWindLimit);
    w.ymax = bbc_modevar(-1, BBC_YWindLimit);

1457 1458 1459 1460 1461 1462 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
    w.xmax = (w.xmax + 1) << w.xmin;
    w.ymax = (w.ymax + 1) << w.ymin;

    /* Take account of window tools using Wimp_GetWindowOutline info from above */

    w.xmin = (o.outline.xmax - o.outline.xmin) - (s.visible_area.xmax - s.visible_area.xmin);
    w.ymin = (o.outline.ymax - o.outline.ymin) - (s.visible_area.ymax - s.visible_area.ymin);

    /* Now know what the extent would be for a full screen window, so */
    /* compare that to the current visible size.                      */

    if   (((s.visible_area.xmax - s.visible_area.xmin) < (w.xmax - w.xmin))
       | ((s.visible_area.ymax - s.visible_area.ymin) < (w.ymax - w.ymin)))
    {
      /* Window isn't full screen, so remember the size */

      d->small_size.visible_area = s.visible_area;
      d->small_size.behind       = find_behind(s.window_handle);

      d->full_size = 0;
    }
    else d->full_size = 1;

    /* Update the visible area width field */

    {
      int width;

      width = s.visible_area.xmax - s.visible_area.xmin;
      if (width < MinimumWidth) width = MinimumWidth;

1488 1489 1490 1491 1492
      /* Tolerance is defined in Windows.h. We leave the display_width */
      /* field alone until the change since it was last updated has    */
      /* exceeded the tolerance value, at which point it is set to the */
      /* visible width (along with display_extent) and the page gets   */
      /* reformatted from the top.                                     */
1493

1494
      if (width != d->display_width)
1495 1496
      {
        if (
1497 1498
             width - d->display_width > Tolerance ||
             width - d->display_width < -Tolerance
1499 1500
           )
        {
1501 1502
          if (!d->page_is_text)
          {
1503 1504
            int h1, h2;

1505
            d->display_extent = d->display_width = width;
1506 1507 1508 1509 1510 1511 1512 1513

            h1 = toolbars_button_height(d) + toolbars_url_height(d);
            h2 = toolbars_status_height(d);

            if (h1) h1 += wimpt_dy();
            if (h2) h2 += wimpt_dy();

            d->display_height = s.visible_area.ymax - s.visible_area.ymin - h1 - h2;
1514

1515 1516 1517
            if (d->ancestor) windows_set_tools(d, &s.visible_area, 0, 0, 0, 0);
            reformat_format_from(d, -1, 1, -1);
          }
1518 1519
        }

1520 1521 1522 1523 1524 1525
        /* Leaving display_width alone has the side effect that centred  */
        /* objects stay centred relative to that value, not the slightly */
        /* altered visible width, so no redraw problems occur if we      */
        /* don't bother forcing a redraw if the reformat isn't done.     */
        /*                                                               */
        /* Hence, no 'else' case here.                                   */
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
      }
    }

    /* In any case remember the scroll position, or it could jump */
    /* around when the small window size is later restored.       */

    d->small_size.xscroll = s.xscroll;
    d->small_size.yscroll = s.yscroll;
  }

  #ifdef TRACE
    if (tl & (1u<<3)) Printf("windows_remember_size: Successful\n");
  #endif

  return 0;
}

/*************************************************/
/* windows_close_browser()                       */
/*                                               */
/* Closes a Browser window, freeing up memory    */
/* claimed for it.                               */
/*                                               */
/* This DOES NOT CLOSE FRAMESETS, and memory     */
/* claimed for that isn't freed. You should use  */
/* the frames collapsing functions in Frames.c   */
/* to do this. If there's any doubt, always      */
/* close through the general handler in          */
/* c.Handlers as this takes care of frames.      */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the window.           */
/*************************************************/

1560
void windows_close_browser(browser_data * handle)
1561
{
1562
  browser_data * ancestor = handle->ancestor; /* Not utils_ancestor() as we want to know if 'ancestor' is NULL later */
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573

  #ifdef TRACE
    if (tl & (1u<<3)) Printf("windows_close_browser: Called\n");
  #endif

  /* This may take a while if there's lots of flex freeing to do... */

  _swix(Hourglass_Start, _IN(0), 10);

  /* Cancel any fetches */

1574
  ChkError(fetch_cancel(handle));
1575 1576 1577 1578 1579

  /* Stop the later code trying to destroy source / close the stream twice */

  if (handle->fetch_handle == handle->display_handle) handle->display_handle = 0;

1580 1581 1582 1583
  /* If this frame is highlighted, this will clear that highlight */

  ChkError(frames_remove_highlight_if_present(handle));

1584 1585 1586 1587 1588 1589
  /* If the Open URL dialogue was opened from the window, this will close it */

  ChkError(openurl_close(handle->self_id, 0));

  /* If a main menu is open for the window, this closes it */

1590
  menus_close_document_if_mine(handle);
1591 1592 1593 1594 1595 1596

  /* If the Print dialogue was opened from the window, this will close */
  /* it and the Print Style window (again, if open).                   */

  ChkError(print_close(handle->self_id, 0));

1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
  /* If there is an ancestor and it has a selected token, */
  /* is that token in this window? If so, want to clear   */
  /* that selection. At the same time, deal with status   */
  /* bar issues.                                          */

  if (ancestor)
  {
    if (ancestor->selected_owner == handle)
    {
      ancestor->selected       = NULL;
      ancestor->selected_owner = NULL;
    }

    if (ancestor->selected_frame == handle) ancestor->selected_frame = NULL;

    /* Ensure that this window is removed from the array of */
    /* status_contents structures associated with the       */
    /* ancestor's status bar.                               */

1616
    ChkError(toolbars_remove_status_item(handle, ancestor));
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
  }

  /* Get rid of any attached event handlers on the window and its */
  /* toolbars (if any).                                           */
  /*                                                              */
  /* Because null events need to be claimed/released through the  */
  /* pollword, need to still use the internal routine to get rid  */
  /* of null event handlers, before calling the general eventlib  */
  /* routine to remove anything else. Otherwise the internal      */
  /* counter checking if nulls should be released won't be        */
  /* decremented correctly (Bad Things May Then Happen...).       */

  if (handle->watching_resize) deregister_null_claimant(Wimp_ENull,(WimpEventHandler *) windows_remember_size,(void *) handle->self_id);
  handle->watching_resize = 0;

  if (handle->fetch_handler) fetchpage_release_nulls(handle);

1634 1635 1636 1637 1638
  if (handle->anim_drift)
  {
    deregister_null_claimant(Wimp_ENull,(WimpEventHandler *) toolbars_animation_drift, handle);
    handle->anim_drift = 0;
  }
1639

1640 1641 1642 1643 1644 1645
  if (handle->plugin_active)
  {
    deregister_null_claimant(Wimp_ENull,(WimpEventHandler *) toolbars_animation, handle);
    handle->plugin_active = 0;
  }

1646 1647 1648
  if (handle->status_handler) deregister_null_claimant(Wimp_ENull, (WimpEventHandler *) toolbars_timeout_status, handle);
  handle->status_handler = 0;

1649
  reformat_stop_pending(handle); /* Deregisters stuff as required */
1650

1651 1652 1653
  if (handle->meta_refresh_at) deregister_null_claimant(Wimp_ENull, (WimpEventHandler *) meta_check_refresh, handle);
  handle->meta_refresh_at = 0;

1654 1655 1656 1657
  if (handle->dragging) deregister_null_claimant(Wimp_ENull, (WimpEventHandler *) handle_drags, handle);
  handle->dragging = 0;
  drag_in_progress = 0;

1658 1659
  /* If watching the dialler's status changed service call,   */
  /* or displaying online time, remove the relevant handlers. */
1660

1661
  if (handle->dialler_status) event_deregister_message_handler(Message_Service, handle_dialler_service, handle);
1662
  if (handle->dialler_last) deregister_null_claimant(Wimp_ENull, (WimpEventHandler *) handle_dialler_display, handle);
1663

1664
  handle->dialler_status = 0;
1665
  handle->dialler_last   = 0;
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677

  /* Remove any pointer-related handlers */

  browser_pointer_over_deleted(handle);

  /* Now release everything else */

  {
    ObjectId tupper, tlower;

    /* Handlers for the main window */

1678 1679
    ChkError(event_deregister_wimp_handlers_for_object(handle->self_id));
    ChkError(event_deregister_toolbox_handlers_for_object(handle->self_id));
1680 1681 1682 1683 1684 1685 1686

    tupper = toolbars_get_upper(handle);

    if (tupper)
    {
      /* Handlers for the upper toolbar */

1687 1688
      ChkError(event_deregister_wimp_handlers_for_object(tupper));
      ChkError(event_deregister_toolbox_handlers_for_object(tupper));
1689 1690 1691 1692 1693 1694 1695 1696
    }

    tlower = toolbars_get_lower(handle);

    if (tlower && tlower != tupper)
    {
      /* Handlers for the lower toolbar */

1697 1698
      ChkError(event_deregister_wimp_handlers_for_object(tlower));
      ChkError(event_deregister_toolbox_handlers_for_object(tlower));
1699 1700 1701
    }
  }

1702 1703 1704 1705 1706 1707 1708 1709
  /* Delete any related run-time created objects */

  if (handle->save_dbox)
  {
    ChkError(toolbox_delete_object(0, handle->save_dbox));
    handle->save_dbox = 0;
  }

1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725
  /* Get rid of the fetched source / stream */

  browser_destroy_source(handle);

  if (handle->fetch_handle)
  {
    ChkError(html_close(handle->fetch_handle));
  }

  if (handle->display_handle)
  {
    ChkError(html_close(handle->display_handle));
  }

  /* Close any saving files */

1726
  if (handle->save_file)
1727
  {
1728 1729
    fclose(handle->save_file);
    handle->save_file = NULL;
1730 1731
  }

1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
  /* Get rid of any pending message blocks */

  if (handle->pending_data_load)
  {
    free(handle->pending_data_load);

    #ifdef TRACE
      malloccount -= sizeof(WimpMessage);
      if (tl & (1u<<13)) Printf("** malloccount (windows_close_browser): \0211%d\0217\n",malloccount);
    #endif
  }

1744 1745 1746 1747 1748
  /* If we were streaming data for a Plug-In, close the */
  /* stream.                                            */

  if (handle->pstream) plugin_abort_stream(handle);

1749 1750
  /* Get rid of forms related data */

1751
  ChkError(form_discard(handle));
1752 1753 1754 1755

  /* Get rid of any extra forms data */

  #ifdef TRACE
1756
    if (handle->post_data)
1757
    {
1758 1759
      if (tl & (1u<<12)) Printf("windows_close_browser: flex_free block %p\n",&handle->post_data);
      flexcount -= flex_size((flex_ptr) &handle->post_data);
1760 1761 1762 1763
      if (tl & (1u<<13)) Printf("**   flexcount: %d\n",flexcount);
    }
  #endif

1764
  if (handle->post_data) flex_free((flex_ptr) &handle->post_data);
1765

1766 1767 1768 1769 1770
  /* Remove any Plug-In status message */

  free(handle->plugin_status);
  handle->plugin_status = NULL;

1771 1772 1773 1774
  /* Remove any History references */

  history_remove(handle, NULL);

1775 1776
  /* Get rid of images and Objects */

1777
  ChkError(image_discard(handle));
1778
  ChkError(object_discard(handle));
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807

  #ifdef TRACE
    if ((tl & (1u<<3)) && handle->ancestor)
    {
      Printf("\nwindows_close_browser: Structure %p is a child structure, ancestor %p\n",handle,handle->ancestor);
      if (handle->nchildren) Printf("                       and has children.\n");
      else                   Printf("                       and has no children.\n");
    }
  #endif

  /* Delete the window object. */

  ChkError(toolbox_delete_object(0,handle->self_id));

  #ifdef TRACE
    if (tl & (1u<<3)) Printf("\nDeleted object ID %p\n",(void *) handle->self_id);
  #endif

  /* Ensure any line list data relating to tables is freed */

  #ifdef TRACE
    if (tl & (1u<<12)) Printf("windows_close_browser: Freeing table chunks\n");
  #endif

  tables_free_memory(1, handle, handle->cell, 0);

  /* Ensure all memory chunks are freed */

  #ifdef TRACE
1808
    if (tl & (1u<<12)) Printf("windows_close_browser: Chunk CK_FURL\n"
1809 1810 1811 1812 1813 1814 1815 1816 1817 1818
                          "                             CK_DURL\n"
                          "                             CK_CHIL\n"
                          "                             CK_NAME\n"
                          "                             CK_FWID\n"
                          "                             CK_FHEI\n"
                          "                             CK_LINE\n"
                          "                             CK_LDAT\n"
                          "                             CK_STAT set to 0\n");
  #endif

1819 1820 1821 1822 1823 1824 1825 1826 1827
  ChkError(memory_set_chunk_size(handle, NULL, CK_FURL, 0));
  ChkError(memory_set_chunk_size(handle, NULL, CK_DURL, 0));
  ChkError(memory_set_chunk_size(handle, NULL, CK_CHIL, 0));
  ChkError(memory_set_chunk_size(handle, NULL, CK_NAME, 0));
  ChkError(memory_set_chunk_size(handle, NULL, CK_FWID, 0));
  ChkError(memory_set_chunk_size(handle, NULL, CK_FHEI, 0));
  ChkError(memory_set_chunk_size(handle, NULL, CK_LINE, 0));
  ChkError(memory_set_chunk_size(handle, NULL, CK_LDAT, 0));
  ChkError(memory_set_chunk_size(handle, NULL, CK_STAT, 0));
1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855

  /* Lose any claimed fonts */

  fm_lose_fonts(handle);

  /* Sort out the linked list next and previous pointers in */
  /* the previous and next entries to this one (if they are */
  /* present)                                               */

  if (handle->previous) (handle->previous)->next = handle->next;
  if (handle->next) (handle->next)->previous = handle->previous;
  else last_browser = handle->previous;

  #ifdef TRACE
    if (tl & (1u<<3))
    {
      if (handle->previous) Printf("Previous structure %p: Next field pointed at %p.\n",(void *) handle->previous, (void *) handle->next);
      else Printf("No Previous structure\n");
      if (handle->next) Printf("Next structure %p: Previous field pointed at %p.\n",(void *) handle->next, (void *) handle->previous);
      else Printf("No Next structure, last_browser set to previous\n");
    }
  #endif

  /* Finally, free the browser_data and associated reformat_cell structures */

  #ifdef TRACE
    if (tl & (1u<<12)) Printf("windows_close_browser: free block %p, which held 'reformat_cell' structure\n",handle->cell);
    malloccount -= sizeof(reformat_cell);
1856
    if (tl & (1u<<13)) Printf("** malloccount (windows_close_browser): \0212%d\0217\n",malloccount);
1857 1858 1859 1860 1861 1862 1863
  #endif

  free(handle->cell);

  #ifdef TRACE
    if (tl & (1u<<12)) Printf("windows_close_browser: free block %p, which held 'browser_data' structure\n",handle);
    malloccount -= sizeof(browser_data);
1864
    if (tl & (1u<<13)) Printf("** malloccount (windows_close_browser): \0212%d\0217\n",malloccount);
1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
  #endif

  free(handle);

  #ifdef TRACE
    if (tl & (1u<<3)) Printf("Structure memory freed\n");
  #endif

  _swix(Hourglass_Off, 0);

  #ifdef TRACE
    if (tl & (1u<<3)) Printf("windows_close_browser: Successful\n");
  #endif

1879
  return;
1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
}

/*************************************************/
/* windows_redraw_browser()                      */
/*                                               */
/* Called on receiving a redraw request from the */
/* Wimp. Calls the browser redraw routines.      */
/*                                               */
/* Parameters are as for a standard Wimp event   */
/* handler.                                      */
/*************************************************/

int windows_redraw_browser(int eventcode, WimpPollBlock * b, IdBlock * idb, browser_data * handle)
{
  WimpRedrawWindowBlock * redraw = (WimpRedrawWindowBlock *) b;
  int                     more;

1897
  ChkError(wimp_redraw_window(redraw, &more));
1898

1899 1900 1901 1902 1903 1904 1905
  if (more)
  {
    ChkError(redraw_draw(handle,
                         redraw,
                         0,
                         0));
  }
1906

1907
  return 1;
1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924
}

/*************************************************/
/* windows_show_browser()                        */
/*                                               */
/* Shows a browser window, initially centred on  */
/* the screen but futher windows are opened at   */
/* successive sensible offsets to avoid them     */
/* exactly overlapping one another.              */
/*                                               */
/* This default show position can be overridden  */
/* in two ways; though the Choices specifying an */
/* absolute position to open at, or through the  */
/* small_size.visible_area field of the          */
/* browser_data struct holding an xmin and ymin  */
/* that are not -1 on entry.                     */
/*                                               */
1925 1926 1927
/* Small fetch windows will be shown near the    */
/* pointer.                                      */
/*                                               */
1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the window, with the  */
/*             small_size.visible_area BBox      */
/*             filled in with -1 or a valid      */
/*             visible area as described above.  */
/*************************************************/

void windows_show_browser(browser_data * b)
{
  WimpOpenWindowBlock w;
  static int          offset;
  ObjectId            p, o;
1940
  int                 fixed_size = 0;
1941 1942 1943 1944 1945 1946 1947

  #ifdef TRACE
    if (tl & (1u<<3)) Printf("windows_show_browser: Called\n");
  #endif

  o = b->self_id;

1948
  if (!b->small_fetch)
1949
  {
1950
    /* Is there a valid small_size.visible_area block? */
1951

1952
    if (b->small_size.visible_area.xmin != -1 && b->small_size.visible_area.ymin != -1)
1953
    {
1954 1955
      w.visible_area = b->small_size.visible_area;
      fixed_size = 1;
1956 1957 1958
    }
    else
    {
1959
      fixed_size = 0;
1960

1961 1962 1963 1964
      /* Find the screen x and y size in pixels and scale them */
      /* to OS units using OS_ReadModeVariable calls via. the  */
      /* bbc_modevar function; also work out the top left      */
      /* coordinates at the same time.                         */
1965

1966
      if (!choices.override_x)
1967 1968
      {
        w.visible_area.xmax = choices.width;
1969

1970 1971 1972 1973 1974 1975 1976 1977
        w.xscroll = bbc_modevar(-1, BBC_XWindLimit);
        w.yscroll = bbc_modevar(-1, BBC_XEigFactor);

        w.visible_area.xmin = (((w.xscroll + 1) << w.yscroll) - w.visible_area.xmax) / 2;
        w.visible_area.xmax += w.visible_area.xmin;
      }
      else
      {
1978 1979
        w.visible_area.xmin = choices.override_x;
        w.visible_area.xmax = choices.width + choices.override_x;
1980
      }
1981

1982
      if (!choices.override_y)
1983 1984
      {
        w.visible_area.ymin = choices.height;
1985

1986 1987 1988 1989 1990 1991 1992 1993
        w.xscroll = bbc_modevar(-1, BBC_YWindLimit);
        w.yscroll = bbc_modevar(-1, BBC_YEigFactor);

        w.visible_area.ymax = (((w.xscroll + 1) << w.yscroll) + w.visible_area.ymin) / 2;
        w.visible_area.ymin = w.visible_area.ymax - w.visible_area.ymin;
      }
      else
      {
1994 1995
        w.visible_area.ymin = choices.override_y;
        w.visible_area.ymax = choices.height + choices.override_y;
1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007
      }

      /* Handle offset for successive windows, but don't cover */
      /* up the icon bar                                       */

      if ((w.visible_area.ymin - offset) >= 132)
      {
        w.visible_area.ymin -= offset;
        w.visible_area.ymax -= offset;
        offset += 48;
      }
      else offset = 48;
2008 2009
    }

2010 2011 2012
    w.xscroll = 0;
    w.yscroll = 0;
    w.behind = -1;
2013

2014 2015
    ChkError(set_corrected_extent(0, o, &w.visible_area));
  }
2016 2017 2018 2019 2020 2021 2022 2023

  ChkError(toolbox_get_parent(0,o,&p,NULL));

  if (!b->ancestor || !nested_wimp)
  {
    /* For an ancestor window (i.e. a window which does not have */
    /* an ancestor itself) or no nested Wimp, open it as normal. */

2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041
    if (!b->small_fetch)
    {
      ChkError(toolbox_show_object(0,
                                   o,
                                   Toolbox_ShowObject_FullSpec,
                                   &w.visible_area,
                                   p,
                                   -1));
    }
    else
    {
      ChkError(toolbox_show_object(0,
                                   o,
                                   Toolbox_ShowObject_AtPointer,
                                   NULL,
                                   p,
                                   -1));
    }
2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071
  }
  else
  {
    /* Otherwise, take advantage of the nested Wimp for child windows */

    WindowShowObjectBlock   show;
    WimpGetWindowStateBlock state;

    state.window_handle = b->ancestor->window_handle;
    ChkError(wimp_get_window_state(&state));

    show.visible_area = w.visible_area;
    show.xscroll      = w.xscroll;
    show.yscroll      = w.yscroll;
    show.behind       = w.behind;

    show.parent_window_handle = (b->ancestor)->window_handle;

    show.alignment_flags = Alignment_LeftEdge_WorkArea | Alignment_RightEdge_WorkArea  |
                           Alignment_TopEdge_WorkArea  | Alignment_BottomEdge_WorkArea |
                           Alignment_XScroll_WorkArea  | Alignment_YScroll_WorkArea;

    ChkError(toolbox_show_object(Toolbox_ShowObject_AsSubWindow,
                                 o,
                                 Toolbox_ShowObject_FullSpec,
                                 &show,
                                 p,
                                 -1));
  }

2072
  if (!fixed_size && !b->small_fetch)
2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088
  {
    /* Fill in the small_size information for the window in its */
    /* browser_data structure                                   */

    b->small_size.visible_area = w.visible_area;

    /* Now that the window is open, can get it's outline etc.; */
    /* so can go up to full screen if required.                */

    if (b->full_screen) windows_open_full_screen(b,
                                                 1,
                                                 0,
                                                 choices.v_scroll,
                                                 choices.h_scroll);
  }

2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108
  /* If the requested size was larger than the available screen, */
  /* (most often seen on A5000s where 640x480x256 is in use and  */
  /* a new browser window is opened - the Choices want to make   */
  /* it larger than the space that's availale)                   */

//
//
//
//    /* Opening width considerations... */
//
//    b->display_width = b->display_extent = width;
//
//    if (b->display_extent < MinimumWidth) b->display_extent = MinimumWidth, b->display_width = MinimumWidth;
//
//    /* ...and opening height */
//
//    b->display_height = height;
//
//

2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122
  #ifdef TRACE
    if (tl & (1u<<3)) Printf("windows_show_browser: Successful\n");
  #endif
}

/*************************************************/
/* windows_open_full_screen()                    */
/*                                               */
/* Opens a browser window Full Screen or small,  */
/* optionally recording its previous size in the */
/* small_size fields when going to Full Screen.  */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the window;           */
2123
/*                                               */
2124
/*             1 for Full Screen, else 0;        */
2125
/*                                               */
2126
/*             1 to record the size, else 0;     */
2127
/*                                               */
2128 2129
/*             1 to leave a vertical scroll bar  */
/*             visible, else 0;                  */
2130
/*                                               */
2131 2132 2133 2134
/*             1 to leave a horizontal scroll    */
/*             bar visible, else 0.              */
/*************************************************/

2135
_kernel_oserror * windows_open_full_screen(browser_data * b, int full_screen, int record, int vertsc, int horisc)
2136
{
2137 2138 2139 2140 2141
  BBox                      w;
  WimpOpenWindowBlock       o;
  WimpGetWindowStateBlock   s;
  WimpGetWindowOutlineBlock outline;
  ObjectId                  parent;
2142 2143 2144 2145 2146 2147 2148 2149 2150

  #ifdef TRACE
    if (tl & (1u<<3)) Printf("windows_open_full_screen: Called\n");
  #endif

  /* Get the window state (for flags, scroll etc.) and its outline. */

  s.window_handle = o.window_handle = outline.window_handle = b->window_handle;

2151 2152
  RetError(wimp_get_window_state(&s));
  RetError(wimp_get_window_outline(&outline));
2153 2154 2155 2156 2157 2158 2159 2160

  /* If being asked to open to Full Screen, set up the BBox 'w' to */
  /* hold a visible area appropriate to a full screen window.      */

  if (full_screen)
  {
    /* Get the screen size in OS units */

2161 2162 2163 2164 2165
    w.xmin = bbc_modevar(-1, BBC_XEigFactor);
    w.ymin = bbc_modevar(-1, BBC_YEigFactor);
    w.xmax = bbc_modevar(-1, BBC_XWindLimit);
    w.ymax = bbc_modevar(-1, BBC_YWindLimit);

2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180
    w.xmax = (w.xmax + 1) << w.xmin;
    w.ymax = (w.ymax + 1) << w.ymin;

    w.xmax -= ((s.flags & WimpWindow_VScroll) && (choices.v_scroll)) ? (outline.outline.xmax - s.visible_area.xmax) : 2;
    w.xmin  = 2;
    w.ymax -= 2;
    w.ymin  = ((s.flags & WimpWindow_HScroll) && (choices.h_scroll)) ? (s.visible_area.ymin - outline.outline.ymin) : 2;

    if (record)
    {
      /* Remember the current (small) size */

      b->small_size.visible_area = s.visible_area;
      b->small_size.xscroll      = s.xscroll;
      b->small_size.yscroll      = s.yscroll;
2181
      b->small_size.behind       = -1;
2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218
    }
  }

  /* Otherwise, go back to the visible area stored in the window's */
  /* small_size.visible_area BBox.                                 */

  else
  {
    /* Revert to a previously stored small size */

    w = b->small_size.visible_area;
  }

  /* An irritating feature of the Wimp - if you call Wimp_SetExtent, */
  /* then the next Wimp_OpenWindow call is forced on-screen. This    */
  /* means that if toggle size is used when the window is partially  */
  /* dragged off-screen, then the shrinking horizontal extent when   */
  /* it is toggled back would ensure that the window was brought     */
  /* back fully on screen. Sometimes this is annoying.               */
  /*                                                                 */
  /* It turns out though, that if the window is *already* partially  */
  /* off-screen when the SetExtent call is made, the Wimp brings the */
  /* window (internally) back on screen, and doesn't bother to do    */
  /* this on the next OpenWindow call. So to allow the window to be  */
  /* toggled back to a small size which may be partially off-screen, */
  /* we open the window miles off screen somewhere before setting    */
  /* the extent.                                                     */
  /*                                                                 */
  /* This is not pretty but it is relatively solid; the worst that   */
  /* can happen is the Wimp *does* decide to force the window back   */
  /* on screen at the next OpenWindow call, in which case the        */
  /* behaviour is as it would be without the extra code here.        */

  o.visible_area.xmin = w.xmin + 16384;
  o.visible_area.ymin = w.ymin - 16384;
  o.visible_area.xmax = w.xmax + 16384;
  o.visible_area.ymax = w.ymax - 16384;
2219
  o.behind            = -1;
2220 2221 2222

  /* Show the object off-screen, making sure we don't trash the parent object ID information */

2223 2224
  RetError(toolbox_get_parent (0, b->self_id, &parent, NULL));
  RetError(toolbox_show_object(0, b->self_id, 1, &o.visible_area,parent, -1));
2225 2226 2227 2228 2229 2230 2231

  /* Set up the OpenWindow block */

  o.window_handle = b->window_handle;
  o.visible_area  = w;
  o.xscroll       = s.xscroll;
  o.yscroll       = s.yscroll;
2232
  o.behind        = -1;
2233 2234 2235

  /* w holds the extent we require. If the horizontal extent is smaller */
  /* than the horizontal minimum defined for this window, make sure it  */
2236 2237 2238 2239 2240
  /* is increased to that minimum. Ensure also that the vertical        */
  /* extent isn't too small, and never shrink it either. Note that the  */
  /* exception case is for when the parent has child frames - in that   */
  /* case, leave w alone, so the extent will track the visible area     */
  /* exactly (which is the desired effect).                             */
2241

2242
  if (!b->nchildren)
2243 2244 2245
  {
    BBox extent;

2246 2247
    if (w.xmax - w.xmin < MinimumWidth) w.xmax = w.xmin + MinimumWidth;

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

2250 2251
    if (w.ymax - w.ymin < extent.ymax - extent.ymin) w.ymax = w.ymin + extent.ymax - extent.ymin;
    if (w.ymax - w.ymin < MinimumHeight)             w.ymax = w.ymin + MinimumHeight;
2252 2253
  }

2254
  RetError(set_corrected_extent(0, b->self_id, &w));
2255 2256 2257 2258 2259 2260 2261 2262 2263 2264

  /* If the Wimp supports changing window flags, set the */
  /* Back and No Bounds bits for a full screen show,     */
  /* else clear them.                                    */

  if (nested_wimp)
  {
    unsigned int clear, eor;

    clear = (
2265
              (choices.full_screen && controls.back_window)
2266 2267 2268 2269 2270 2271 2272 2273 2274 2275
              ?
              WimpWindow_Back
              :
              0
            )
            | WimpWindow_NoBounds;

    if (full_screen) eor = clear;
    else             eor = 0;

2276
    RetError(set_window_flags(b->window_handle, clear, eor));
2277 2278 2279 2280
  }

  /* Show the object in the correct on-screen position */

2281 2282 2283 2284 2285 2286
  RetError(toolbox_show_object(0,
                               b->self_id,
                               1,
                               &o.visible_area,
                               parent,
                               -1));
2287 2288 2289

  /* Make sure the toolbars, if present, are up to date */

2290
  if (choices.move_gadgets != Choices_MoveGadgets_Never) toolbars_move_gadgets(b);
2291

2292
  /* Update the visible area width and height fields */
2293 2294

  b->display_width = b->display_extent = o.visible_area.xmax - o.visible_area.xmin;
2295
  if (b->display_extent < MinimumWidth) b->display_width = b->display_extent = MinimumWidth;
2296

2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307
  {
    int h1, h2;

    h1 = toolbars_button_height(b) + toolbars_url_height(b);
    h2 = toolbars_status_height(b);

    if (h1) h1 += wimpt_dy();
    if (h2) h2 += wimpt_dy();

    b->display_height = o.visible_area.ymax - o.visible_area.ymin - h1 - h2;
  }
2308

2309 2310 2311 2312 2313 2314
  /* Ensure any child frames are resized to fit */

  #ifdef TRACE
    if (tl & (1u<<3)) Printf("windows_open_full_screen: Exitting through frames_resize_frameset\n");
  #endif

2315
  return frames_resize_frameset(b, &o.visible_area);
2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360
}

/*************************************************/
/* windows_process_icon_text()                   */
/*                                               */
/* Takes a Wimp icon handle and looks at a given */
/* text string, which can contain a special      */
/* string of characters instructing the function */
/* to perform various changes to the icon.       */
/*                                               */
/* The string must start with '@!' to indicate   */
/* it isn't some ordinary preinitialised string, */
/* and can contain these comma separated values: */
/*                                               */
/* N       Ensure the icon has (N)o border.      */
/* H       Ensure the icon (H)as a border.       */
/* Bn      Set background to colour n (n=hex).   */
/* Fn      Set foreground to colour n (n=hex).   */
/*                                               */
/* Parameters: Pointer to WimpGetIconStateBlock  */
/*             holding the window handle the     */
/*             icon is in and the icon's handle  */
/*             (the full icon state will be      */
/*             read in by this function, it does */
/*             not need to be done externally);  */
/*                                               */
/*             Pointer to the special text;      */
/*                                               */
/*             1 to set the first character of   */
/*             the text to a null byte (i.e. get */
/*             rid of the string) if it is a     */
/*             special string (otherwise it is   */
/*             left untouched), else 0.          */
/*************************************************/

void windows_process_icon_text(WimpGetIconStateBlock * icon, char * text, int remove)
{
  /* Check this is special text */

  if (!text || strlen(text) < 3) return;
  if (text[0] != '@' || text[1] != '!') return;

  /* It is, so process it */

  if (wimp_get_icon_state(icon)) return;
2361
  else
2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497
  {
    WimpSetIconStateBlock state;
    int                   array, value;
    unsigned int          flags;

    array = 2;
    flags = icon->icon.flags;

    /* Parse the text */

    while (text[array])
    {
      switch (text[array])
      {
        case 'N': flags &= ~WimpIcon_Border, array++; break;
        case 'H': flags |=  WimpIcon_Border, array++; break;

        case 'B':
        case 'F':
        case 'C':
        {
          unsigned int mask, shift;

          if      (text[array] == 'B') shift = WimpIcon_BGColour;
          else if (text[array] == 'C') shift = WimpIcon_ButtonType;
          else                         shift = WimpIcon_FGColour;

          array ++;
          value = (int) strtoul(text + array, NULL, 16);
          array ++;

          if (value < 0)  value = 0;
          if (value > 15) value = 15;

          mask = 15 * shift;
          flags &= ~mask;

          flags |= (value * shift);
        }
        break;

        default: array++; break;
      }

      while (text[array] == ',') array++;
    }

    /* Set the new flags, if they've changed */

    if (flags != icon->icon.flags)
    {
      state.window_handle = icon->window_handle;
      state.icon_handle   = icon->icon_handle;
      state.clear_word    = 0xffffffff;
      state.EOR_word      = flags;

      wimp_set_icon_state(&state);
    }
  }

  /* Remove the text, if asked to do so */

  if (remove) text[0] = 0;

  return;
}

/*************************************************/
/* windows_process_component_text()              */
/*                                               */
/* Calls windows_process_icon_text for a given   */
/* component of a given Toolbox object.          */
/*                                               */
/* Parameters: Object ID the component lies in;  */
/*                                               */
/*             The component ID;                 */
/*                                               */
/*             Pointer to a buffer to hold the   */
/*             icon's text;                      */
/*                                               */
/*             Size of the buffer;               */
/*                                               */
/*             Out of the icons that make up the */
/*             component, the number of the one  */
/*             to alter (starting from 0);       */
/*                                               */
/*             1 to set the first character of   */
/*             the text to a null byte (i.e. get */
/*             rid of the string) if it is a     */
/*             special string (otherwise it is   */
/*             left untouched), else 0.          */
/*************************************************/

_kernel_oserror * windows_process_component_text(ObjectId o, ComponentId c, char * buffer,
                                                 int buffsize, int iconnum, int remove)
{
  _kernel_oserror       * e;
  WimpGetIconStateBlock   icon;
  int                     iconlist[10];

  if (buffsize < 2) return NULL;

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

  e = window_get_wimp_handle(0, o, &icon.window_handle);

  #ifdef TRACE
    if (e) return e;
  #else
    if (e) return NULL;
  #endif

  e = gadget_get_icon_list(0, o, c, iconlist, sizeof(iconlist), NULL);
  if (e) return NULL; /* Don't throw errors as may not want this component for some builds */

  icon.icon_handle = iconlist[iconnum];

  /* Read, process, and set the text */

  e = wimp_get_icon_state(&icon);
  if (e) return NULL;

  strncpy(buffer, icon.icon.data.it.buffer, buffsize - 1);
  buffer[buffsize - 1] = 0;

  windows_process_icon_text(&icon, buffer, remove);

  if (icon.icon.data.it.buffer_size > 1) strncpy(icon.icon.data.it.buffer, buffer, icon.icon.data.it.buffer_size - 1);

  return NULL;
}

/*************************************************/
/* windows_initialise_tool_sizes()               */
/*                                               */
/* Sets up local copies of the current window    */
2498
/* tool sizes through find_tool_sizes.           */
2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514
/*************************************************/

_kernel_oserror * windows_initialise_tool_sizes(void)
{
  return find_tool_sizes(&title_height, &hscroll_height, &vscroll_width);
}

/*************************************************/
/* windows_return_tool_sizes()                   */
/*                                               */
/* Returns the title bar and scroll bar widths   */
/* in OS units, including their outlines (some   */
/* of which will form part of the visible area   */
/* border).                                      */
/*                                               */
/* This will only work correctly if a call to    */
2515
/* windows_initialise_tool_sizes has been made   */
2516 2517 2518 2519
/* at least once before.                         */
/*                                               */
/* Parameters: Pointer to an int, in which the   */
/*             title bar height is placed;       */
2520
/*                                               */
2521 2522 2523
/*             Pointer to an int, in which the   */
/*             horizontal scroll bar bar height  */
/*             is placed;                        */
2524
/*                                               */
2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548
/*             Pointer to an int, in which the   */
/*             vertical scroll bar width is      */
/*             placed.                           */
/*                                               */
/* Assumes:    Any of the pointers may be NULL.  */
/*************************************************/

void windows_return_tool_sizes(int * theight, int * hheight, int * vwidth)
{
  if (theight) *theight = title_height;
  if (hheight) *hheight = hscroll_height;
  if (vwidth)  *vwidth  = vscroll_width;

  return;
}

/*************************************************/
/* windows_set_tools()                           */
/*                                               */
/* If using the nested Wimp, turns on/off        */
/* selected tools around a window.               */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the window;           */
2549
/*                                               */
2550 2551 2552 2553 2554 2555 2556 2557 2558
/*             Pointer to a BBox, in which the   */
/*             proposed visible area of the      */
/*             window may be placed. If NULL,    */
/*             it will be read from a call to    */
/*             Wimp_GetWindowState - however,    */
/*             scrollbars that you're trying to  */
/*             remove may have forced the area   */
/*             up somewhat, hence the ability to */
/*             give a desired pre-removal BBox;  */
2559
/*                                               */
2560
/*             1 for a title bar, else 0;        */
2561
/*                                               */
2562 2563
/*             1 for a vertical scroll bar, else */
/*             0;                                */
2564
/*                                               */
2565 2566
/*             1 for a horizontal scroll bar,    */
/*             else 0;                           */
2567
/*                                               */
2568 2569 2570 2571 2572 2573 2574 2575 2576 2577
/*             1 for a resize icon, else 0.      */
/*                                               */
/* Assumes:    The BBox pointer may be NULL.     */
/*************************************************/

_kernel_oserror * windows_set_tools(browser_data * b, BBox * box, int title, int vscroll, int hscroll, int resize)
{
  if (!nested_wimp) return NULL;
  else
  {
2578 2579 2580 2581 2582 2583
    _kernel_oserror         * e;
    WimpGetWindowStateBlock   s;
    BBox                      extent;
    unsigned int              parent, align;
    unsigned int              old_flags;
    int                       th, hh, vw, must_redraw = 0, must_reformat = 0;
2584

2585 2586 2587 2588 2589 2590 2591
    /* If there's a highlight on this frame, remove it to avoid */
    /* complications with it getting left in the wrong place.   */

    if (b == highlight_frame) frames_remove_highlight();

    /* Get the window state and extent */

2592 2593
    s.window_handle = b->window_handle;

2594 2595
    RetError(_swix(Wimp_GetWindowState,
                   _INR(1, 2) | _OUTR(3, 4),
2596

2597 2598
                   &s,
                   Magic_Word_TASK, /* See MiscDefs.h */
2599

2600 2601
                   &parent,
                   &align));
2602

2603
    RetError(window_get_extent(0, b->self_id, &extent));
2604 2605 2606 2607 2608 2609 2610

    /* Without the following line, the resize routines (such as windows_remember_size) can */
    /* set up display_extent and display_width to appropriate values and call the          */
    /* reformatter, waiting for it to set the actual extent to match these values. However */
    /* if this function gets in first - and it usually will - it will read the extent and  */
    /* then (re)set the values to the old settings; unless, that is, the following line is */
    /* included to spot that the actual extent and display_extent settings differ.         */
2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623
    /*                                                                                     */
    /* Of course, it's never *that* easy, so what we have to do is only set it if the      */
    /* extent we would have used is smaller. This is because during resizes of frames,     */
    /* documents can continue to reformat or reload (client pull) in the background, since */
    /* null events are coming in (compare to a parent window resize, where event 1,        */
    /* Wimp_EOpenWindow, is being generated). This can mean that display_extent is out of  */
    /* date, and forcing the extent to it shrinks the window. The right hand edge of the   */
    /* frame thus suddenly gets pulled along with the left (or vice versa) and the visual  */
    /* effect is that the frameset tears itself apart. Not nice.                           */
    /*                                                                                     */
    /* And yes, I know this is *another* overlong comment, but necessary nonetheless.      */

    if (extent.xmax - extent.xmin < b->display_extent) extent.xmax = extent.xmin + b->display_extent;
2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667

    /* Flags will only be set if they've changed */

    old_flags = s.flags;

    /* Need to know the window tool sizes */

    windows_return_tool_sizes(&th, &hh, &vw);

    /* The Wimp won't try to keep the visible footprint of the window */
    /* the same as border icons appear and disappear. This is pretty  */
    /* useless from the browser's point of view; need to manually     */
    /* play around with the visible area to cope.                     */

    if ((s.flags & WimpWindow_TitleIcon) && !title) s.visible_area.ymax += th, extent.ymax += th;
    if (!(s.flags & WimpWindow_TitleIcon) && title) s.visible_area.ymax -= th, extent.ymax -= th;

    if ((s.flags & WimpWindow_VScroll) && !vscroll) s.visible_area.xmax += vw, extent.xmax += vw, must_redraw = 1;
    if (!(s.flags & WimpWindow_VScroll) && vscroll) s.visible_area.xmax -= vw, extent.xmax -= vw, must_redraw = 1, must_reformat = 1;

    if ((s.flags & WimpWindow_HScroll) && !hscroll) s.visible_area.ymin -= hh, extent.ymin -= hh;
    if (!(s.flags & WimpWindow_HScroll) && hscroll) s.visible_area.ymin += hh, extent.ymin += hh;

    /* Modify them */

    if (title)   s.flags = s.flags |  WimpWindow_TitleIcon;
    else         s.flags = s.flags &~ WimpWindow_TitleIcon;

    if (vscroll) s.flags = s.flags |  WimpWindow_VScroll;
    else         s.flags = s.flags &~ WimpWindow_VScroll;

    if (hscroll) s.flags = s.flags |  WimpWindow_HScroll;
    else         s.flags = s.flags &~ WimpWindow_HScroll;

    if (resize)  s.flags = s.flags |  WimpWindow_SizeIcon;
    else         s.flags = s.flags &~ WimpWindow_SizeIcon;

    /* If the flags have changed... */

    if (s.flags != old_flags)
    {
      WindowShowObjectBlock show;
      ObjectId              tparent;

2668 2669 2670 2671 2672
      /* If there's a highlight on this frame, remove it to avoid */
      /* complications with it getting left in the wrong place.   */

      if (b == highlight_frame) frames_remove_highlight();

2673 2674
      /* Ensure the extent is corrected for the addition or removal of tools */

2675
      RetError(window_set_extent(0, b->self_id, &extent));
2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686

      /* Alter the flags */

      show.visible_area         = s.visible_area;
      show.xscroll              = s.xscroll;
      show.yscroll              = s.yscroll;
      show.behind               = s.behind;
      show.window_flags         = s.flags;
      show.parent_window_handle = parent;
      show.alignment_flags      = align | Alignment_NewFlagsGiven;

2687
      RetError(toolbox_get_parent(0, b->self_id, &tparent, NULL));
2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705

      if (nested_wimp)
      {
        /* For the nested Wimp, simple - just reopen with the new-style call */

        e = toolbox_show_object(Toolbox_ShowObject_AsSubWindow,
                                b->self_id,
                                Toolbox_ShowObject_FullSpec,
                                &show,
                                tparent,
                                -1);
      }
      else
      {
        // This code not done yet (not urgent enough for now).
        // *If* eventually implemented, the 'if' wrapper around
        // the whole of this function's code obviously needs to
        // be removed first.
2706 2707

        e = NULL;
2708 2709 2710 2711 2712 2713 2714 2715
      }

      if (e) return e;

      /* If must_redraw is set, redraw errors could occur unless the window is redrawn now. */

      if (must_redraw)
      {
2716
        BBox workarea = s.visible_area;
2717

2718
        coords_box_toworkarea(&workarea, (WimpRedrawWindowBlock *) &s);
2719

2720 2721 2722 2723 2724
        RetError(wimp_force_redraw(s.window_handle,
                                   workarea.xmin,
                                   workarea.ymin,
                                   workarea.xmax,
                                   workarea.ymax));
2725 2726 2727

        /* May well need to move the toolbar gadgets, too */

2728
        if (choices.move_gadgets != Choices_MoveGadgets_Never) toolbars_move_gadgets(b);
2729 2730 2731 2732 2733 2734 2735 2736
      }

      /* If must_reformat is set, for childless windows, reformat the page */

      if (must_reformat && !b->nchildren) reformat_format_from(b, -1, 1, -1);
    }

    /* Update the various browser width records in light of any */
2737
    /* changes in the window, and similarly update the height   */
2738 2739 2740 2741 2742

    b->display_extent = extent.xmax - extent.xmin;
    b->display_width  = s.visible_area.xmax - s.visible_area.xmin;
    if (b->display_width < MinimumWidth) b->display_width = MinimumWidth;

2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753
    {
      int h1, h2;

      h1 = toolbars_button_height(b) + toolbars_url_height(b);
      h2 = toolbars_status_height(b);

      if (h1) h1 += wimpt_dy();
      if (h2) h2 += wimpt_dy();

      b->display_height = s.visible_area.ymax - s.visible_area.ymin - h1 - h2;
    }
2754

2755 2756
    /* If there are children and this is an ancestor window, resize the children */

2757
    if (!b->ancestor && b->nchildren) frames_resize_frameset(b, &s.visible_area);
2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768

    return NULL;
  }
}

/*************************************************/
/* windows_check_tools()                         */
/*                                               */
/* Looks at a window, and if in a certain        */
/* direction the visible area and extent are     */
/* the same, turns off scroll bars (they aren't  */
2769
/* needed).                                      */
2770 2771
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
2772 2773
/*             relevant to the window;           */
/*                                               */
2774 2775 2776 2777 2778 2779 2780
/*             Pointer to a BBox, passed on to   */
/*             windows_set_tools (see that       */
/*             function for details).            */
/*************************************************/

_kernel_oserror * windows_check_tools(browser_data * b, BBox * box)
{
2781 2782 2783
  WimpGetWindowStateBlock s;
  BBox                    extent;
  int                     hscroll = 0, vscroll = 0;
2784

2785 2786 2787 2788 2789 2790
  /* For small fetch windows, don't want to touch the tools */

  if (b->small_fetch) return NULL;

  /* Otherwise, proceed */

2791 2792
  s.window_handle = b->window_handle;

2793 2794
  RetError(wimp_get_window_state(&s));
  RetError(window_get_extent(0, b->self_id, &extent));
2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848

  /* Turn on scroll bars if the visible area is less than the */
  /* extent in the appropriate direction.                     */

  if (s.visible_area.xmax - s.visible_area.xmin < extent.xmax - extent.xmin) hscroll = 1;
  if (s.visible_area.ymax - s.visible_area.ymin < extent.ymax - extent.ymin) vscroll = 1;

  if (!b->ancestor && !b->full_screen)
  {
    /* Ancestor windows should always have both scroll bars, unless */
    /* running full screen.                                         */

    hscroll = vscroll = 1;
  }
  else
  {
    /* Otherwise, read the scroll bar flags */

    if (!b->nchildren)
    {
      /* Frames or full screen windows with no children themselves*/

      if      (b->frame_hscroll == 0) hscroll = 0; /* No */
      else if (b->frame_hscroll == 2) hscroll = 1; /* Yes, else use values found above (i.e. auto) */

      /* If there is a vertical scroll bar present already, and this window */
      /* has no children, don't want to remove that bar. This is so that    */
      /* frames can start with no scroll bar, then later the reformatter    */
      /* may decide to add one. Since the page width changes the            */
      /* windows_set_tools call will call the reformatter again. However,   */
      /* the new reformat could turn off the vertical scroll bar - so the   */
      /* reformatter locks into a recursive loop. Hence, can only ever      */
      /* remove a vscroll for a childless window by an explicit call to     */
      /* windows_set_tools; otherwise, once it's added, it stays.           */
      /*                                                                    */
      /* The exception, of course, is for frames with scrolling explicitly  */
      /* set to 'no'.                                                       */

      if (!b->frame_vscroll)                                            vscroll = 0;
      else if (b->frame_vscroll == 2 || (s.flags & WimpWindow_VScroll)) vscroll = 1;

//      if      (b->frame_vscroll == 0) vscroll = 0;
//      else if (b->frame_vscroll == 2) vscroll = 1;
    }
    else
    {
      /* Frames with children *must* have no scroll bars, or resize routines */
      /* later on, which read visible areas, will fail, and the bars will be */
      /* visible in gaps between frames.                                     */

      hscroll = vscroll = 0;
    }
  }

2849
  RetError(windows_set_tools(b, box, !b->ancestor, vscroll, hscroll, !b->full_screen && !b->ancestor));
2850 2851 2852

  return NULL;
}