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

#include <stdlib.h>
#include <stdio.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 "gadgets.h"

#include "Dialler.h"

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

#include "Browser.h"
48
#include "ChoiceDefs.h"
49
#include "CSIM.h"
50
#include "CtrlDefs.h"
51
#include "Fetch.h" /* (Which itself includes URLstat.h) */
52
#include "Handlers.h"
53
#include "History.h"
54 55 56 57
#include "Images.h"
#include "Memory.h"
#include "Mouse.h"
#include "Reformat.h"
58 59
#include "URLutils.h"
#include "URLveneer.h"
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

#include "Toolbars.h"

/* Locals */

static int bar_overlap = -1;

/* Local types */

/* Used to record the requirement of a particular browser to */
/* display a certain message in the status bar. For a given  */
/* page which may include frames, an array of these is built */
/* as the frameset is defined and starts to fetch pages.     */
/* The actual status message is derived from examining the   */
/* priorities of all of the messages.                        */

typedef struct status_content
{
  browser_data * b;
  status_type    type;
  unsigned int   start;
  unsigned int   end;
}
status_content;

/* Static function prototypes */

87 88 89
static ObjectId          toolbars_get_upper_backend      (browser_data * b);
static ObjectId          toolbars_get_lower_backend      (browser_data * b);

90 91 92 93
static void              toolbars_set_size               (ObjectId o, int height, int yscroll, int flags);
static int               toolbars_find_overlap           (ObjectId o);
static int               toolbars_bar_overlap            (ObjectId o);

94
static _kernel_oserror * toolbars_animation_set_sprite   (browser_data * b);
95 96 97 98 99 100 101
static _kernel_oserror * toolbars_update_specific_status (browser_data * b, browser_data * ancestor, int entry, status_type type);
static int               toolbars_write_status           (browser_data * ancestor);
static status_type       toolbars_return_inferred        (browser_data * b);
static _kernel_oserror * toolbars_infer_status           (browser_data * b, browser_data * ancestor, int entry);
static _kernel_oserror * toolbars_add_status_item        (browser_data * ancestor);
static status_type       toolbars_return_act_status      (browser_data * b);

102 103
static int               toolbars_count_file_saves       (browser_data * b);
static int               toolbars_count_file_saves_r     (browser_data * b);
104
static int               toolbars_calculate_progress     (browser_data * b);
105 106
static int               toolbars_calculate_progress_r   (browser_data * b, int saves);
static int               toolbars_create_progress        (browser_data * b, char * buffer, int buffer_size);
107 108 109 110 111 112

/*************************************************/
/* toolbars_get_upper()                          */
/*                                               */
/* Returns the object ID of the upper toolbar.   */
/* This may well not be the internal top left    */
113
/* toolbar of the window due to the swap_bars    */
114
/* flag or merged toolbars.                      */
115 116 117 118 119 120 121 122 123
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the toolbar.          */
/*                                               */
/* Returns:    Object ID of the toolbar, or NULL */
/*             for an error / no upper toolbar.  */
/*************************************************/

ObjectId toolbars_get_upper(browser_data * b)
124
{
125 126
  if (controls.swap_bars) return toolbars_get_lower_backend(b);
  else                    return toolbars_get_upper_backend(b);
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
}

/*************************************************/
/* toolbars_get_upper_backend()                  */
/*                                               */
/* Returns the object ID of the upper toolbar.   */
/* This may well not be the internal top left    */
/* toolbar of the window due to merged toolbars. */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the toolbar.          */
/*                                               */
/* Returns:    Object ID of the toolbar, or NULL */
/*             for an error / no upper toolbar.  */
/*************************************************/

static ObjectId toolbars_get_upper_backend(browser_data * b)
144 145 146
{
  ObjectId t;

147
  if (b->all_in_bottom)
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
  {
    if (
         window_get_tool_bars(InternalBottomLeft,
                              b->self_id,
                              &t,
                              NULL,
                              NULL,
                              NULL)
       )
       return NULL;

    else return t;
  }

  if (
       window_get_tool_bars(InternalTopLeft,
                            b->self_id,
                            NULL,
                            &t,
                            NULL,
                            NULL)
     )
     return NULL;

  else return t;
}

/*************************************************/
/* toolbars_get_lower()                          */
/*                                               */
/* Returns the object ID of the lower toolbar.   */
/* This may well not be the internal bottom      */
180
/* left toolbar of the window due to the         */
181
/* swap_bars flag or merged toolbars.            */
182 183 184 185 186 187 188 189 190
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the toolbar.          */
/*                                               */
/* Returns:    Object ID of the toolbar, or NULL */
/*             for an error / no lower toolbar.  */
/*************************************************/

ObjectId toolbars_get_lower(browser_data * b)
191
{
192 193
  if (controls.swap_bars) return toolbars_get_upper_backend(b);
  else                    return toolbars_get_lower_backend(b);
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
}

/*************************************************/
/* toolbars_get_lower_backend()                  */
/*                                               */
/* Returns the object ID of the lower toolbar.   */
/* This may well not be the internal bottom      */
/* left toolbar of the window due to merged      */
/* toolbars.                                     */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the toolbar.          */
/*                                               */
/* Returns:    Object ID of the toolbar, or NULL */
/*             for an error / no lower toolbar.  */
/*************************************************/

static ObjectId toolbars_get_lower_backend(browser_data * b)
212 213 214
{
  ObjectId t;

215
  if (b->all_in_top)
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
  {
    if (
         window_get_tool_bars(InternalTopLeft,
                              b->self_id,
                              NULL,
                              &t,
                              NULL,
                              NULL)
       )
       return NULL;

    else return t;
  }

  if (
       window_get_tool_bars(InternalBottomLeft,
                            b->self_id,
                            &t,
                            NULL,
                            NULL,
                            NULL)
     )
     return NULL;

  else return t;
}

/*************************************************/
/* toolbars_set_size()                           */
/*                                               */
/* Sets the vertical visible height and scroll   */
/* position of a given toolbar.                  */
/*                                               */
/* Parameters: The object ID of the toolbar;     */
250
/*                                               */
251
/*             The visible height to set it to;  */
252
/*                                               */
253
/*             The y scroll offset to give it;   */
254
/*                                               */
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
/*             Flags - alter InternalTopLeft, or */
/*             InternalBottomLeft toolbar.       */
/*************************************************/

static void toolbars_set_size(ObjectId o, int height, int yscroll, int flags)
{
  WimpGetWindowStateBlock s;
  ObjectId                p;

  /* Get the toolbar's parent object (the window) */

  #ifdef TRACE
    if (tl & (1u<<1)) Printf("\ntoolbars_set_size: Called with object ID %p\n",(void *) o);
  #endif

270
  toolbox_get_parent(0, o, &p, NULL);
271 272 273 274 275

  #ifdef TRACE
    if (tl & (1u<<1)) Printf("toolbars_set_size: Parent ID is %p\n",(void *) p);
  #endif

276 277 278 279
  /* This has to be here, or the toolbar can drop out of */
  /* the parent window (?!)...                           */

  ChkError(toolbox_show_object(0, o, 0, 0, p, -1));
280 281 282

  /* Get the toolbar's wimp handle and size through Wimp_GetWindowState */

283
  ChkError(window_get_wimp_handle(0, o, &s.window_handle));
284 285
  ChkError(wimp_get_window_state(&s));

286
  /* Set the vertical size. */
287

288
  if (!controls.swap_bars)
289 290 291 292 293 294 295 296 297
  {
    s.visible_area.ymin = s.visible_area.ymax - height;
    ChkError(toolbox_show_object(0, o, 1, &s.visible_area, p, -1));
  }
  else
  {
    s.visible_area.ymax = s.visible_area.ymin + height;
    ChkError(toolbox_show_object(0, o, 1, &s.visible_area, p, -1));
  }
298 299 300

  /* This call is needed for the toolbox to recognise the new size */

301
  ChkError(window_set_tool_bars(flags, p, o, o, 0, 0));
302

303 304 305 306
  /* Set the scroll position - the set_tool_bars call will */
  /* trounce the scroll position (sigh) if this call is    */
  /* done beforehand, as part of the first show_object     */
  /* call.                                                 */
307 308

  s.yscroll = yscroll;
309
  ChkError(toolbox_show_object(0, o, 1, &s.visible_area, p, -1));
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331

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

/*************************************************/
/* toolbars_find_overlap()                       */
/*                                               */
/* Finds the overlap of the height gadgets 0xd   */
/* and 0xe in the upper toolbar, recording this  */
/* in the local bar_overlap int to make future   */
/* references faster.                            */
/*                                               */
/* Parameters: The object ID of the toolbar.     */
/*************************************************/

static int toolbars_find_overlap(ObjectId o)
{
  BBox url, but;
  int  overlap;

332 333
  if (gadget_get_bbox(0, o, URLBarSpacer,    &url)) return 0;
  if (gadget_get_bbox(0, o, ButtonBarSpacer, &but)) return 0;
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366

  overlap = but.ymax - url.ymin;
  if (overlap >= 0) return overlap;

  return 0;
}

/*************************************************/
/* toolbars_bar_overlap()                        */
/*                                               */
/* Returns the overlap of the height gadgets 0xd */
/* and 0xe in the upper toolbar, using a local   */
/* cached value for speed.                       */
/*                                               */
/* Parameters: The object ID of the toolbar.     */
/*************************************************/

static int toolbars_bar_overlap(ObjectId o)
{
  if (bar_overlap < 0) bar_overlap = toolbars_find_overlap(o);

  return bar_overlap;
}

/*************************************************/
/* toolbars_set_presence()                       */
/*                                               */
/* Reads the browser_data structure associated   */
/* with a given browser window object ID, and    */
/* ensures the toolbars associated with that     */
/* window match the flags set inside the data    */
/* structure.                                    */
/*                                               */
367 368
/* Parameters: A browser_data struct relevant to */
/*             the window holding the toolbars;  */
369 370 371 372 373 374
/*                                               */
/*             A flags word holding the toolbars */
/*             to alter - InternalTopLeft, or    */
/*             InternalBottomLeft.               */
/*************************************************/

375
void toolbars_set_presence(browser_data * b, unsigned int flags)
376
{
377 378
  ObjectId o;
  ObjectId t;
379 380

  #ifdef TRACE
381
    if (tl & (1u<<1)) Printf("\ntoolbars_set_presence: Called with %p\n", b);
382 383 384 385 386 387 388 389 390
  #endif

  /* If not a valid browser_data structure, exit */

  if (!is_known_browser(b)) return;

  /* Toolbars can only exist on ancestor browser windows, not */
  /* in any child frames.                                     */

391 392
  b = utils_ancestor(b);
  o = b->self_id;
393

394
  /* Deal with merged toolbars separately - they're a simple case */
395

396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
  if (b->all_in_top || b->all_in_bottom)
  {
    /* The URL bar flag is looked at to see if the toolbar is on or off */
    /* as well as the status bar flag, as the former is used for upper  */
    /* toolbars (as it prompts the vertical shift of page objects), and */
    /* the latter for bottom merged toolbars.                           */

    if (b->all_in_top)
    {
      t = toolbars_get_upper(b);

      if (t)
      {
        #ifdef TRACE
          if (tl & (1u<<1)) Printf("toolbars_set_presence: Top left toolbar ID is %p\n",(void *) t);
        #endif

        if (b->url_bar && b->status_bar) ChkError(toolbox_show_object(0, t, 0, NULL, o, -1));
        else ChkError(toolbox_hide_object(0, t));
      }
    }
    else
418
    {
419 420 421 422 423 424 425 426 427 428 429
      t = toolbars_get_lower(b);

      if (t)
      {
        #ifdef TRACE
          if (tl & (1u<<1)) Printf("toolbars_set_presence: Bottom left toolbar ID is %p\n",(void *) t);
        #endif

        if (b->url_bar && b->status_bar) ChkError(toolbox_show_object(0, t, 0, NULL, o, -1));
        else ChkError(toolbox_hide_object(0, t));
      }
430 431
    }

432 433
    return;
  }
434

435
  /* Proceed with 'normal' toolbars */
436 437 438

  if (flags & InternalTopLeft)
  {
439
    t = toolbars_get_upper(b);
440 441 442 443 444 445 446 447 448 449 450 451

    if (t)
    {
      #ifdef TRACE
        if (tl & (1u<<1)) Printf("toolbars_set_presence: Top left toolbar ID is %p\n",(void *) t);
      #endif

      /* URL and button bars both present */

      if ((b->url_bar) && (b->button_bar)) toolbars_set_size(t,
                                                             toolbars_button_height(b) + toolbars_url_height(b),
                                                             0,
452
                                                             controls.swap_bars ? InternalBottomLeft : InternalTopLeft);
453 454 455 456 457
      /* URL bar only present */

      else if ((b->url_bar) && !(b->button_bar)) toolbars_set_size(t,
                                                                   toolbars_url_height(b),
                                                                   0,
458
                                                                   controls.swap_bars ? InternalBottomLeft : InternalTopLeft);
459 460 461 462 463 464 465 466
      /* Button bar only present */

      else if (!(b->url_bar) && (b->button_bar))
      {
        /* Find what would be the URL bar height if it was present on its own */

        BBox w;

467
        gadget_get_bbox(0, t, URLBarSpacer, &w);
468 469 470 471 472 473

        /* Now set the toolbar size and Y scroll offset to show only the buttons */

        toolbars_set_size(t,
                          toolbars_button_height(b),
                          - (w.ymax - w.ymin) + toolbars_bar_overlap(t),
474
                          controls.swap_bars ? InternalBottomLeft : InternalTopLeft);
475 476 477 478
      }

      /* URL bar and button bar both absent */

479
      else toolbox_hide_object(0, t);
480 481 482 483 484
    }
  }

  if (flags & InternalBottomLeft)
  {
485
    t = toolbars_get_lower(b);
486 487 488 489 490 491 492 493 494

    if (t)
    {
      #ifdef TRACE
        if (tl & (1u<<1)) Printf("toolbars_set_presence: Bottom left toolbar ID is %p\n",(void *) t);
      #endif

      /* Status bar present */

495
      if (b->status_bar) ChkError(toolbox_show_object(0, t, 0, NULL, o, -1));
496 497 498

      /* Status bar absent */

499
      else ChkError(toolbox_hide_object(0, t));
500 501 502 503 504
    }
  }

  /* Make sure that the toolbars have the correct gadget positions */

505
  if (choices.move_gadgets != Choices_MoveGadgets_Never) toolbars_move_gadgets(b);
506

507
//  /* Make sure that the window is completely redrawn */
508 509 510 511 512
//
//  {
//    WimpGetWindowStateBlock s;
//    s.window_handle = b->window_handle;
//    ChkError(wimp_get_window_state(&s));
513 514
//    coords_box_toworkarea(&s.visible_area, (WimpRedrawWindowBlock *) &s);
//    ChkError(wimp_force_redraw(b->window_handle,
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
//                               s.visible_area.xmin,
//                               s.visible_area.ymin,
//                               s.visible_area.xmax,
//                               s.visible_area.ymax));
//  }

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

/*************************************************/
/* toolbars_move_gadgets()                       */
/*                                               */
/* Generally called as part of some window       */
/* resize event, this function moves and/or      */
/* resizes various gadgets in various toolbars,  */
/* if the toolbars are present in the window.    */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the toolbars.         */
/*************************************************/

void toolbars_move_gadgets(browser_data * p)
{
  BBox                      b, g1, g2;
  int                       right, gap, width;
  ObjectId                  t;
  WimpGetWindowStateBlock   s;
  _kernel_oserror         * e;

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

550 551 552 553 554 555
  /* Small fetch windows can't move their status line contents */

  if (p->small_fetch) return;

  /* Otherwise, continue as normal */

556 557 558 559
  s.window_handle = p->window_handle;
  ChkError(wimp_get_window_state(&s));

  b = s.visible_area;
560
  if ((b.xmax - b.xmin) < controls.minimum_convergence) b.xmax = b.xmin + controls.minimum_convergence;
561 562 563 564 565 566 567 568 569 570 571 572 573

  /* If the URL bar is present... */

  if (p->url_bar)
  {
    t = toolbars_get_upper(p);

    if (t)
    {
      /* If the pop-up menu gadget for the History list needs to move, move it */
      /* and resize the URL writable to maintain a fixed distance between the  */
      /* two gadgets.                                                          */

574 575
      e      = gadget_get_bbox(0, t, URLBarHistoryMenuR, &g1);  /* Get the popup's bounding box    */
      ChkError(gadget_get_bbox(0, t, URLBarWrit,  &g2)); /* Get the writable's bounding box */
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603

      /* Where should the right hand edge of the popup be? */

      right = b.xmax - b.xmin - 4;

      /* If the popup doesn't exist, want to set up a BBox as if a zero */
      /* width popup was present.                                       */

      if (e) g1.xmin = g2.xmax, g1.ymin = 0, g1.xmax = !right, g1.ymax = 0;

      /* Gap is the distance between the right hand edge of the URL writable */
      /* and the left hand edge of the popup.                                */

      gap = g1.xmin - g2.xmax;

      /* Is this the same as it is already? If not, move / resize the gadgets. */

      if (right != g1.xmax)
      {
        if (!e)
        {
          /* Move the popup */

          width = g1.xmax - g1.xmin;

          g1.xmax = right;
          g1.xmin = right - width;

604
          ChkError(gadget_move_gadget(0, t, URLBarHistoryMenuR, &g1));
605 606 607 608 609 610
        }

        /* Resize the URL writable */

        g2.xmax = g1.xmin - gap;

611
        ChkError(gadget_move_gadget(0, t, URLBarWrit,  &g2));
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
      }
    }
  }

  /* If the status bar is present... */

  if (p->status_bar)
  {
    t = toolbars_get_lower(p);

    if (t)
    {
      /* As above, move the byte counter display field if need be, */
      /* and if it does move, resize the status display field.     */

627 628
      ChkError(gadget_get_bbox(0, t, StatusBarProgress, &g1));
      ChkError(gadget_get_bbox(0, t, StatusBarStatus, &g2));
629 630 631 632 633 634 635 636 637 638 639 640

      gap = g1.xmin - g2.xmax;

      right = b.xmax - b.xmin - 20;

      if (right != g1.xmax)
      {
        width = g1.xmax - g1.xmin;

        g1.xmax = right;
        g1.xmin = right - width;

641
        ChkError(gadget_move_gadget(0, t, StatusBarProgress, &g1));
642 643 644

        g2.xmax = g1.xmin - gap;

645
        ChkError(gadget_move_gadget(0, t, StatusBarStatus, &g2));
646 647 648 649 650 651 652 653 654 655
      }
    }
  }

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

/*************************************************/
656
/* toolbars_animation_set_sprite()               */
657
/*                                               */
658 659
/* Sets the sprite in the button gadget used for */
/* the status bar animation.                     */
660
/*                                               */
661 662
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the animation.        */
663 664
/*************************************************/

665
static _kernel_oserror * toolbars_animation_set_sprite(browser_data * b)
666
{
667
  ObjectId t;
668
  char     v[32];
669 670 671 672 673 674 675 676 677 678 679 680 681
  int      time_now;

  /* Update the current frame time */

  if (
       _swix(OS_ReadMonotonicTime,
             _OUT(0),

             &time_now)
     )
     b->current_time = 0;

  else b->current_time = time_now;
682

683
  /* Find the status bar object Id */
684

685 686
  t = toolbars_get_lower(b);
  if (!t) return 0;
687

688 689 690
  /* Advance the frame counter and put a validation string  */
  /* that would give a button gadget the relevant animation */
  /* sprite into v.                                         */
691

692 693
  b->current_frame ++;
  if (b->current_frame >= animation_frames) b->current_frame = 0;
694

695 696 697 698 699
  sprintf(v, "sa%d\0", b->current_frame);

  /* Set the validation string on the status bar animation */
  /* button icon to v, so that the new sprite is shown.    */

700
  return button_set_validation(0, t, StatusBarAnimAnim, v);
701 702 703 704 705 706 707 708 709 710 711 712 713 714
}

/*************************************************/
/* toolbars_animation()                          */
/*                                               */
/* Advances the browser animation in the status  */
/* bar by one frame.                             */
/*                                               */
/* Parameters are as for a standard Wimp event   */
/* handler (this is called on null events).      */
/*************************************************/

int toolbars_animation(int eventcode, WimpPollBlock * b, IdBlock * idb, browser_data * handle)
{
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
  int time_now;

  /* What is the time now? */

  if (
       _swix(OS_ReadMonotonicTime,
             _OUT(0),

             &time_now)
     )
     return 0;

  /* If we have animation frames, a displayed status bar, and */
  /* enough time has passed, advance the animation.           */

730 731
  if (
       handle->status_bar   &&
732
       animation_frames > 0 &&
733
       time_now         > (handle->current_time + controls.anim_delay)
734 735
     )
     ChkError(toolbars_animation_set_sprite(handle));
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752

  return 0;
}

/*************************************************/
/* toolbars_animation_drift()                    */
/*                                               */
/* Advances the browser animation in the status  */
/* bar by one frame, until it reaches the first  */
/* frame, when it deregisters itself.            */
/*                                               */
/* Parameters are as for a standard Wimp event   */
/* handler (this is called on null events).      */
/*************************************************/

int toolbars_animation_drift(int eventcode, WimpPollBlock * b, IdBlock * idb, browser_data * handle)
{
753 754 755 756 757 758 759 760 761 762 763 764
  int time_now;

  /* What is the time now? */

  if (
       _swix(OS_ReadMonotonicTime,
             _OUT(0),

             &time_now)
     )
     return 0;

765 766
  if (
       handle->status_bar   &&
767
       animation_frames > 0 &&
768
       time_now         > (handle->current_time + controls.anim_delay)
769
     )
770
  {
771
    ChkError(toolbars_animation_set_sprite(handle));
772 773 774 775 776

    /* Deregister the handler if at frame 0. */

    if (handle->current_frame == 0)
    {
777
      deregister_null_claimant(Wimp_ENull, (WimpEventHandler *) toolbars_animation_drift, handle);
778 779 780 781 782 783 784 785 786 787 788
      handle->anim_drift = 0;
    }
  }

  return 0;
}

/*************************************************/
/* toolbars_animate_slow()                       */
/*                                               */
/* Advances an animation in component            */
789 790 791 792 793 794
/* StatusBarAnimAnim (see Toolbars.h) in an      */
/* object of ID given in void * handle (must be  */
/* cast to this to fit as a Wimp event handler)  */
/* by one frame for each complete cycle of the   */
/* main status animation (whether that is        */
/* actually animating or not).                   */
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
/*                                               */
/* So that more than one dialogue could be up    */
/* and animating at once, the object's client    */
/* handle is used to store the current animation */
/* frame. Consequently this routine is of no use */
/* to dialogues that need the client handle for  */
/* other reasons.                                */
/*                                               */
/* Parameters are as for a standard Wimp event   */
/* handler (this is called on null events).      */
/*************************************************/

int toolbars_animate_slow(int eventcode, WimpPollBlock * b, IdBlock * idb, void * handle)
{
  ObjectId o = (ObjectId) handle;
810
  char     v[32];
811 812
  int      slow_animation_frame;

813 814 815 816 817 818 819 820
  /* Because client handles can only store one thing, in this case */
  /* the animation frame, we have no easy way of storing the time  */
  /* that the animation was last advanced by a frame. So, make the */
  /* assumption of a 1cs poll time, and use a strategy of integer  */
  /* division of frame count to get the actual frame number. The   */
  /* timing is thus not at all independent of machine load, but    */
  /* the dialogue box animations are a very low priority issue.    */

821 822 823 824 825 826 827
  if (toolbox_get_client_handle(0, o, (void **) &slow_animation_frame)) return 0;

  /* Advance the frame counter and put a validation string  */
  /* that would give a button gadget the relevant animation */
  /* sprite into v.                                         */

  slow_animation_frame ++;
828
  if (slow_animation_frame >= animation_frames * animation_frames * controls.anim_delay) slow_animation_frame = 0;
829

830
  sprintf(v, "sa%d\0", slow_animation_frame / (animation_frames * controls.anim_delay));
831 832 833 834

  /* Set the validation string on the status bar animation */
  /* button icon to v, so that the new sprite is shown.    */

835
  button_set_validation(0, o, StatusBarAnimAnim, v);
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
  toolbox_set_client_handle(0, o, (void *) slow_animation_frame);

  return 0;
}

/*************************************************/
/* toolbars_hide_cgi()                           */
/*                                               */
/* This routine will look through a URL for a    */
/* ? and turn it into a terminator. This can be  */
/* used to hide CGI information from the user.   */
/*                                               */
/* Parameters: Pointer to the URL, which must be */
/*             writable in memory, as the first  */
/*             '?' it contains (if any) will be  */
/*             turned into a zero byte.          */
/*************************************************/

void toolbars_hide_cgi(char * url)
{
  char * search = url;

  while (*search)
  {
    if (*search == '?')
    {
      *search = '\0';
      break;
    }

    search++;
  }
}

/*************************************************/
/* toolbars_hide_internal()                      */
/*                                               */
/* When passed a pointer to an internal URL,     */
/* this routine finds the separator between the  */
/* internal specifier plus message token and the */
/* extra information in the URL, and copies this */
/* extra data down to the start of the URL.      */
/*                                               */
/* Parameters: Pointer to the URL, which may be  */
/*             altered quite significantly; so   */
/*             this should be a local copy held  */
/*             by the caller to avoid corrupting */
/*             any important full version of the */
/*             URL.                              */
/*************************************************/

void toolbars_hide_internal(char * iurl)
{
  char * extra;
  int    exoff;

  exoff = urlutils_internal_extra(iurl);
  if (!exoff) return;

  extra = iurl + exoff;

  memmove(iurl, extra, strlen(extra) + 1);
}

/*************************************************/
/* toolbars_update_status()                      */
/*                                               */
/* The standard external way of updating a       */
/* browser window's status bar. The given        */
/* requests that a given message type (see the   */
/* definition of status_type in Toolbars.h)      */
/* be used for the status bar.                   */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             which wants the message putting   */
/*             in its (or its ancestor's)        */
/*             status bar;                       */
/*                                               */
/*             Type of message to display.       */
/*************************************************/

_kernel_oserror * toolbars_update_status(browser_data * b, status_type type)
{
  _kernel_oserror * e;
920
  browser_data    * ancestor = utils_ancestor(b);
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
  status_content  * contents;
  int               i, found;

  #ifdef TRACE
    if (tl & (1u<<1)) Printf("toolbars_update_status: Called for %p, type %d\n",b,type);
  #endif

  /* Refer to the array through 'contents' - slightly more   */
  /* convenient than 'ancestor->status_contents' every time! */

  contents = ancestor->status_contents;

  /* See if the given browser has already got a message */
  /* registered with the base browser...                */

  if (contents)
  {
    #ifdef TRACE
      if (tl & (1u<<1))
      {
941
        Printf("\ntoolbars_update_status:");
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
        Printf("\nUpdating for browser %p; old contents:\n",b);

        Printf("\nEntry | b        | Message    | Decay");
        Printf("\n------+----------+------------+------\n");

        for (i = 0; i < ancestor->nstatus; i++)
        {
          switch (contents[i].type)
          {
            default: Printf("%d     | %08x | %02d UNKNOWN | %d\n",i, contents[i].b, contents[i].type, contents[i].end - contents[i].start); break;

            case 0:  Printf("%d     | %08x | %02d Ready   | %d\n",i, contents[i].b, contents[i].type, contents[i].end - contents[i].start); break;
            case 1:  Printf("%d     | %08x | %02d Viewing | %d\n",i, contents[i].b, contents[i].type, contents[i].end - contents[i].start); break;

            case 2:  Printf("%d     | %08x | %02d Format  | %d\n",i, contents[i].b, contents[i].type, contents[i].end - contents[i].start); break;
            case 3:  Printf("%d     | %08x | %02d Process | %d\n",i, contents[i].b, contents[i].type, contents[i].end - contents[i].start); break;
            case 4:  Printf("%d     | %08x | %02d GetPics | %d\n",i, contents[i].b, contents[i].type, contents[i].end - contents[i].start); break;
            case 5:  Printf("%d     | %08x | %02d Fetch   | %d\n",i, contents[i].b, contents[i].type, contents[i].end - contents[i].start); break;

            case 6:  Printf("%d     | %08x | %02d LinkTo  | %d\n",i, contents[i].b, contents[i].type, contents[i].end - contents[i].start); break;
            case 7:  Printf("%d     | %08x | %02d Help    | %d\n",i, contents[i].b, contents[i].type, contents[i].end - contents[i].start); break;
          }
        }

        Printf("\n");
      }
    #endif

    /* Loop round all the status entries, looking for */
    /* one which matches the given browser_data.      */
    /* If found, set 'found' to the array index of    */
    /* that entry.                                    */

    found = -1;

    for (i = 0; i < ancestor->nstatus; i++)
    {
      if (contents[i].b == b)
      {
        found = i;
        break;
      }
    }
  }
  else
  {
    found = -1;

    #ifdef TRACE
      if (tl & (1u<<1))
      {
993
        Printf("\ntoolbars_update_status:");
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
        Printf("\nUpdating for browser %p; this has no contents array.\n\n",b);
      }
    #endif
  }

  /* If not found, add an entry for this browser */

  if (found < 0)
  {
    #ifdef TRACE
      if (tl & (1u<<1)) Printf("toolbars_update_status: Adding item\n");
    #endif

    e = toolbars_add_status_item(ancestor);
    if (e) return e;

    found    = ancestor->nstatus - 1;
    contents = ancestor->status_contents;

    if (contents && found >= 0)
    {
      contents[found].b     = b;
      contents[found].start = 0;
      contents[found].end   = 0;
      contents[found].type  = Toolbars_Status_NoType;
    }
  }

  /* Proceed if there's definitely an entry to proceed with... */

  if (contents && found >= 0)
  {
    #ifdef TRACE
      if (tl & (1u<<1)) Printf("toolbars_update_status: Exitting through toolbars_update_specific_status\n");
    #endif

    return toolbars_update_specific_status(b, ancestor, found, type);
  }
  else
  {
    #ifdef TRACE
      if (tl & (1u<<1)) Printf("toolbars_update_status: Failed, exitting quietly\n");
    #endif

    return NULL;
  }
}

/*************************************************/
/* toolbars_update_specific_status()             */
/*                                               */
/* Updates the given browser's given entry in    */
/* the given ancestor's status_content array the */
/* given message type... Messages will only be   */
/* allowed to rise in priority, with the         */
/* timeouts on all messages dealing with letting */
/* things fall back again.                       */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             which owns the entry to be        */
/*             updated;                          */
/*                                               */
/*             Pointer to the ancestor           */
/*             browser_data struct holding the   */
/*             status_content array;             */
/*                                               */
/*             Index of the entry to update;     */
/*                                               */
/*             Message type to update it with.   */
/*************************************************/

static _kernel_oserror * toolbars_update_specific_status(browser_data * b, browser_data * ancestor, int entry, status_type type)
{
  _kernel_oserror * e;
  status_content  * contents = ancestor->status_contents;
  int               timeout  = 0;
  int               highest;

  #ifdef TRACE
    if (tl & (1u<<1)) Printf("toolbars_update_specific_status: Called for %p (ancestor %p)\n", b, ancestor);
  #endif

  /* This should never happen, but be defensive anyway... Fail */
  /* if the ancestor seems to have no status_content array.    */

  if (!contents || !ancestor->nstatus)
  {
    #ifdef TRACE
      erb.errnum = Utils_Error_Custom_Normal;

      StrNCpy0(erb.errmess,
1085
               "No status_content array in toolbars_update_specific_status")
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127

      show_error_ret(&erb);
    #endif

    return NULL;
  }

  /* If the message type requested is lower priority */
  /* than that already in use by this browser, don't */
  /* do anything - let the timeouts handle it.       */

  if (type < contents[entry].type) return NULL;

  /* Otherwise, update the message type */

  contents[entry].type = type;

  /* Reset the timeout counter */

  e = _swix(OS_ReadMonotonicTime,
            _OUT(0),

            &contents[entry].start);

  switch (contents[entry].type)
  {
    case Toolbars_Status_NoType:
    case Toolbars_Status_Ready:
    case Toolbars_Status_Viewing:
    {
      /* For the idle cases, set all timers to zero */

      contents[entry].start = timeout = 0;

      break;
    }

    default:
    case Toolbars_Status_Formatting:
    case Toolbars_Status_Processing:
    case Toolbars_Status_GetPics:
    case Toolbars_Status_Fetching:
1128 1129 1130 1131 1132
    case Toolbars_Status_Connected:
    case Toolbars_Status_SentReq:
    case Toolbars_Status_Responded:
    case Toolbars_Status_Redirected:
    case Toolbars_Status_Connecting:
1133 1134 1135
    {
      /* For some messages, a general timeout */

1136
      timeout = atoi(lookup_control("ShowMiscFor:50",0,0));
1137 1138 1139 1140

      break;
    }

1141 1142 1143 1144 1145 1146 1147 1148 1149
    case Toolbars_Status_PlugIn:
    {
      /* For Plug-In messages, use two times the general value. */

      timeout = 2 * atoi(lookup_control("ShowMiscFor:50",0,0));

      break;
    }

1150 1151 1152 1153
    case Toolbars_Status_LinkTo:
    {
      /* Specific timeout for LinkTo messages */

1154
      timeout = atoi(lookup_control("ShowLinksFor:200",0,0));
1155 1156 1157 1158 1159 1160 1161 1162

      break;
    }

    case Toolbars_Status_Help:
    {
      /* Specific timeout for Help messages */

1163
      timeout = atoi(lookup_control("ShowHelpFor:600",0,0));
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248

      break;
    }
  }

  contents[entry].end = contents[entry].start + timeout;

  /* Now reflect the highest priority message of the array */

  highest = toolbars_write_status(ancestor);

  /* Work out if the timeout handler needs to be registered */
  /* or deregistered.                                       */

  switch (highest)
  {
    case Toolbars_Status_NoType:
    case Toolbars_Status_Ready:
    case Toolbars_Status_Viewing:
    {
      /* If the highest priority is an idle case and the handler is */
      /* registered, deregister it.                                 */

      if (ancestor->status_handler)
      {
        ancestor->status_handler = 0;

        deregister_null_claimant(Wimp_ENull,
                                 (WimpEventHandler *) toolbars_timeout_status,
                                 ancestor);
      }

      break;
    }

    default:
    {
      /* Otherwise, for non-idle cases, register the handler if it */
      /* isn't already present.                                    */

      if (!ancestor->status_handler)
      {
        ancestor->status_handler = 1;

        register_null_claimant(Wimp_ENull,
                               (WimpEventHandler *) toolbars_timeout_status,
                               ancestor);
      }

      break;
    }
  }

  /* Finished */

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

  return NULL;
}

/*************************************************/
/* toolbars_write_status()                       */
/*                                               */
/* Reflects the highest priority message in a    */
/* status_content array in the status bar of     */
/* the array's owner.                            */
/*                                               */
/* Called by toolbars_update_status as part of   */
/* its normal operation.                         */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             which is an ancestor holding a    */
/*             status_content array.             */
/*                                               */
/* Returns:    The message type that was chosen  */
/*             as the highest priority.          */
/*                                               */
/* Assumes:    That the ancestor browser_data    */
/*             pointer is not NULL.              */
/*************************************************/

static int toolbars_write_status(browser_data * ancestor)
{
1249 1250
  char             new_status [Limits_StatusBarStatus];
  char             url        [Limits_StatusBarStatus];
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
  char           * use_status = new_status;
  int              dontappend = 0;
  int              found      = -1;
  int              i, count;
  status_type      highest;
  ObjectId         t;
  browser_data   * priority;
  status_content * contents = ancestor->status_contents;

  /* Fail if there's no status_content array */

  if (!contents || !ancestor->nstatus)
  {
    #ifdef TRACE
      erb.errnum = Utils_Error_Custom_Normal;

      StrNCpy0(erb.errmess,
1268
               "No status_content array in toolbars_write_status")
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315

      show_error_ret(&erb);
    #endif

    return 0;
  }

  /* Get the ID of the toolbar in which the status bar resides */

  t = toolbars_get_lower(ancestor);
  if (!t) return 0;

  /* Examine all entries for the highest priority message */

  highest = Toolbars_Status_NoType;
  count   = 0;

  for (i = 0; i < ancestor->nstatus; i++)
  {
    /* If the message for this entry is higher than the highest recorded */
    /* so far, then record this message as the highest. Reset the count  */
    /* of the number of times this message has been encountered to zero  */
    /* and remember the index into the array in 'found'.                 */

    if (contents[i].type > highest) highest = contents[i].type, count = 1, found = i;

    /* If the message for this entry is the same as the highest recorded */
    /* so far, then increment the count of the number of times the       */
    /* message has been encountered.                                     */

    else if (contents[i].type == highest) count++;
  }

  /* Now have the highest priority message in 'highest', the number of  */
  /* times it has been requested in 'count', and the index of the first */
  /* occurrence of this message in 'found'.                             */

  priority = contents[found].b;

  /* According to the highest priority message, write the new status into 'new_status' */

  switch (highest)
  {
    default:                     /* (Drop through */
    case Toolbars_Status_Ready:  /* to Viewing)   */
    case Toolbars_Status_Viewing:
    {
1316
      char title[Limits_Title];
1317

1318
      *title = 0;
1319 1320 1321 1322 1323

      /* Try to get the window's title. If this generates an error or   */
      /* is equal to the 'blank page' case, use 'Ready' for the status. */

      if (
1324
           window_get_title(0, ancestor->self_id, title, sizeof(title), NULL) ||
1325 1326 1327 1328 1329 1330
           !strcmp(title, lookup_token("BlankPage:Blank page",0,0))
         )
         StrNCpy0(new_status, lookup_token("Ready:Ready",0,0))

      else
      {
1331 1332 1333
        char format[Limits_StatusFormat];

        title[sizeof(title) - 1] = 0;
1334 1335 1336

        StrNCpy0(format, lookup_token("Page:Viewing '%s'",0,0))
        StrNCpy0(url, title)
1337

1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
        url[sizeof(new_status) - strlen(format) - 1] = 0;
        sprintf(new_status, format, url);
      }

      if (new_status[0] == '-') use_status++, dontappend = 1;
    }
    break;

    case Toolbars_Status_GetPics:
    {
      int images = image_count_pending(ancestor);

      if (images == 1) StrNCpy0(new_status, lookup_token("GetPic:Fetching 1 image...", 0, 0))
      else if (images)  sprintf(new_status, lookup_token("GetPics:Fetching %d images...", 0, 0), images);
      else             StrNCpy0(new_status, lookup_token("GetPic0:All current images fetched...", 0, 0));

      if (new_status[0] == '-') use_status++, dontappend = 1;
    }
    break;

    case Toolbars_Status_Fetching:   /* (Drop through  */
    case Toolbars_Status_Processing: /* to Formatting) */
1360 1361 1362 1363 1364
    case Toolbars_Status_Connected:
    case Toolbars_Status_SentReq:
    case Toolbars_Status_Responded:
    case Toolbars_Status_Redirected:
    case Toolbars_Status_Connecting:
1365 1366
    case Toolbars_Status_Formatting:
    {
1367
      char format[Limits_StatusFormat];
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378

      /* For many occurrences, report an appropriate 'cumulative' */
      /* message; otherwise, report the one specific case.        */
      /*                                                          */
      /* If the browser has a parent - i.e. this is a frames      */
      /* document - then the 'many' equivalents are always used;  */
      /* this is to stop flickering between the 'single' and      */
      /* 'many' states during a fetch/reformat/etc.               */

      if (count > 1 || priority->real_parent)
      {
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
        if (highest == Toolbars_Status_Fetching)
        {
          /* This one is a little special, as we could be saving an object to disc */
          /* and the user may not have dealt with the save dialogue yet.           */

          if (priority->fetch_status == BS_DATAFETCH && !priority->save_file)
          {
            StrNCpy0(new_status, lookup_token("FetchWait:Waiting for a Save dialogue to be dealt with...", 0, 0))
          }
          else
          {
            StrNCpy0(new_status, lookup_token("FetchMany:Fetching frames contents...", 0, 0))
          }
        }

        /* Simpler cases */

1396 1397 1398 1399 1400 1401 1402
        else if (highest == Toolbars_Status_Processing) StrNCpy0(new_status, lookup_token("ProcessMany:Processing frames contents...",                      0, 0))
        else if (highest == Toolbars_Status_Connected)  StrNCpy0(new_status, lookup_token("ConnectedMany:Connected to servers to fetch frames contents...", 0, 0))
        else if (highest == Toolbars_Status_SentReq)    StrNCpy0(new_status, lookup_token("SentReqMany:Sent requests to fetch frames contents...",          0, 0))
        else if (highest == Toolbars_Status_Responded)  StrNCpy0(new_status, lookup_token("RespondedMany:Responses received for frames contents...",        0, 0))
        else if (highest == Toolbars_Status_Connecting) StrNCpy0(new_status, lookup_token("ConnectingMany:Trying to connect to fetch frames contents...",   0, 0))
        else if (highest == Toolbars_Status_Redirected) StrNCpy0(new_status, lookup_token("RedirectedMany:Frame fetches are being redirected...",           0, 0))
        else                                            StrNCpy0(new_status, lookup_token("FormatMany:Formatting frames contents...",                       0, 0))
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
      }
      else
      {
        /* Find the URL to display, if any */

        if      (browser_fetch_url  (priority)) StrNCpy0(url, browser_fetch_url  (priority))
        else if (browser_current_url(priority)) StrNCpy0(url, browser_current_url(priority))
        else *url = 0;

        if (*url)
        {
          /* Get the Messages file token as a format string for sprintf, into */
          /* which the URL can be substituted.                                */

1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
          if (highest == Toolbars_Status_Fetching)
          {
            if (priority->fetch_status == BS_DATAFETCH && !priority->save_file)
            {
              StrNCpy0(format, lookup_token("FetchWait:Waiting for a Save dialogue to be dealt with...", 0, 0))
            }
            else
            {
              StrNCpy0(format, lookup_token("Fetch:Fetching '%s'...", 0, 0))
            }
          }
1428 1429 1430 1431 1432 1433 1434
          else if (highest == Toolbars_Status_Processing) StrNCpy0(format, lookup_token("Process:Processing '%s'...",                        0, 0))
          else if (highest == Toolbars_Status_Connected)  StrNCpy0(format, lookup_token("Connected:Connected to server to fetch '%s'...",    0, 0))
          else if (highest == Toolbars_Status_SentReq)    StrNCpy0(format, lookup_token("SentReq:Sent request to fetch '%s'...",             0, 0))
          else if (highest == Toolbars_Status_Responded)  StrNCpy0(format, lookup_token("RespondedMany:Response received for '%s'...",       0, 0))
          else if (highest == Toolbars_Status_Connecting) StrNCpy0(format, lookup_token("ConnectingMany:Trying to connect to fetch '%s'...", 0, 0))
          else if (highest == Toolbars_Status_Redirected) StrNCpy0(format, lookup_token("RedirectedMany:Redirecting to '%s'...",             0, 0))
          else                                            StrNCpy0(format, lookup_token("Format:Formatting '%s'...",                         0, 0))
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461

          /* Possibly hide CGI information in the URL */

          #ifdef HIDE_CGI
            toolbars_hide_cgi(url);
          #endif

          /* Ensure the URL isn't too long to fit int he new_status */
          /* buffer along with the format string defined above. If  */
          /* the format string comes from a messages file we can't  */
          /* make assumptions about how many of its characters will */
          /* actually appear in the final output string after the   */
          /* sprintf call, so we must treat it as if they all will  */
          /* appear - hence the ' - 1'.                             */

          url[sizeof(new_status) - strlen(format) - 1] = 0;

          toolbars_hide_internal(url);

          /* Write the combined message into new_status */

          sprintf(new_status, format, url);
        }
        else
        {
          /* The URL is not known, so can do a direct lookup into new_status */

1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
          if (highest == Toolbars_Status_Fetching)
          {
            if (priority->fetch_status == BS_DATAFETCH && !priority->save_file)
            {
              StrNCpy0(new_status, lookup_token("FetchWait:Waiting for a Save dialogue to be dealt with...", 0, 0))
            }
            else
            {
              StrNCpy0(new_status, lookup_token("FetchUK:Fetching web page...", 0, 0))
            }
          }
1473 1474 1475 1476 1477 1478
          else if (highest == Toolbars_Status_Processing) StrNCpy0(new_status, lookup_token("ProcessUK:Processing web page...",            0, 0))
          else if (highest == Toolbars_Status_Connected)  StrNCpy0(new_status, lookup_token("ConnectedUK:Connected to server...",          0, 0))
          else if (highest == Toolbars_Status_SentReq)    StrNCpy0(new_status, lookup_token("SentReqUK:Sent request for web page...",      0, 0))
          else if (highest == Toolbars_Status_Responded)  StrNCpy0(new_status, lookup_token("RespondedUK:Server response received...",     0, 0))
          else if (highest == Toolbars_Status_Connecting) StrNCpy0(new_status, lookup_token("ConnectingUK:Trying to connect to server...", 0, 0))
          else                                            StrNCpy0(new_status, lookup_token("FormatUK:Formatting web page...",             0, 0))
1479 1480 1481 1482 1483 1484 1485 1486 1487
        }
      }

      if (new_status[0] == '-') use_status++, dontappend = 1;
    }
    break;

    case Toolbars_Status_Help:
    {
1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
      StrNCpy0(new_status, priority->status_help);

      dontappend = 1;
    }
    break;

    case Toolbars_Status_LinkTo:
    {
      HStream * over;
      char      format[Limits_StatusFormat];
      int       allowed;
      int       dealt_with = 0;

      /* For comments on the use of the 'format' buffer, see the */
      /* code above.                                             */

      over = priority->pointer_over;
1505

1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
      /* If the token that the pointer is over isn't suitable for a LinkTo */
      /* message, use the keyboard selected item instead; so the pointer   */
      /* always takes priority over the keyboard.                          */

      if (
           !over ||
           (
             (
               !over->anchor  ||
               !*over->anchor
             )
             && !(over->type & (TYPE_ISMAP | TYPE_ISCLIENTMAP))
           )
         )
         over = ancestor->selected;

      /* So, do we now have a suitable token, be it from the token the */
      /* pointer was over or whatever is keyboard selected?            */

      if (
           over &&
           (
             (
               over->anchor &&
               *over->anchor
             )
             || (over->type & (TYPE_ISMAP | TYPE_ISCLIENTMAP))
           )
         )
1535
      {
1536 1537 1538 1539 1540 1541 1542
        /* Yes, so create an appropriate status message. */

        StrNCpy0(format, lookup_token("LinkTo:Link to '%s'...",0,0));

        /* If a client-side image map, must work out the URL */

        if (over->type & TYPE_ISCLIENTMAP)
1543
        {
1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
          char * csim_url;

          csim_return_info(priority,
                           over,
                           priority->map_x,
                           priority->map_y,
                           &csim_url,
                           NULL,
                           NULL);

          if (csim_url && *csim_url)
          {
            dealt_with = 1;

            StrNCpy0(url, csim_url)
          }
        }
1561

1562 1563 1564 1565 1566 1567
        /* If a server side map and either not client side as well, */
        /* or the client side map gave us no URL, use the anchor    */
        /* instead, and append coordinate information.              */
        /*                                                          */
        /* If not server side, just use the anchor information in   */
        /* the token on its own.                                    */
1568

1569 1570
        if (!dealt_with && over->anchor && *over->anchor)
        {
1571 1572 1573 1574 1575 1576
          StrNCpy0(url, over->anchor);

          #ifdef HIDE_CGI
            toolbars_hide_cgi(url);
          #endif

1577 1578
          allowed          = sizeof(new_status) - strlen(format);
          url[allowed - 1] = 0;
1579 1580 1581

          toolbars_hide_internal(url);

1582
          /* If a server-side map, want to append coordinate info. */
1583

1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
          if (over->type & TYPE_ISMAP)
          {
            int required = utils_number_length(priority->map_x) +
                           utils_number_length(priority->map_y) +
                           5; /* 5 = length of " (, )" */

            if (strlen(url) + required + 1 <= allowed)
            {
              char append[128];

              sprintf(append, " (%d, %d)", priority->map_x, priority->map_y);

              strcat(url, append);
            }
          }
1599

1600
          dealt_with = 1;
1601 1602 1603
        }
      }

1604
      /* Did we end up with something we can use in a LinkTo message? */
1605

1606
      if (dealt_with)
1607
      {
1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618
        /* Yes, so compile the final message together now */

        sprintf(new_status, format, url);
        if (new_status[0] == '-') use_status++, dontappend = 1;
      }
      else
      {
        /* No suitable token, so the message is out of date; that */
        /* is, no object is selected (say) but the message is     */
        /* still on a timer. In that case, cancel it.             */

1619 1620 1621 1622 1623 1624
        toolbars_cancel_status(priority, highest);

        return Toolbars_Status_Ready;
      }
    }
    break;
1625 1626 1627 1628 1629 1630 1631

    case Toolbars_Status_PlugIn:
    {
      StrNCpy0(new_status, priority->plugin_status ? priority->plugin_status : "");

      dontappend = 1;
    }
1632 1633 1634 1635 1636 1637 1638 1639 1640
  }

  /* Right, finally have a message in new_status. Now */
  /* append the fetch progress, if required.          */
  /*                                                  */
  /* Any messages starting with '-' will never have a */
  /* counter appended, as dontappend will have been   */
  /* set to 1 by things checking this above.          */

1641
  if (controls.append_status && !dontappend)
1642
  {
1643
    char progress[Limits_FetchProgress];
1644 1645
    int  add;

1646
    toolbars_create_progress(contents[found].b, progress, sizeof(progress));
1647

1648
    add = 1 + 1 + (controls.use_brackets ? 2 : 0); /* (Terminating byte, plus separating space, possibly plus 2 brackets) */
1649 1650 1651 1652 1653 1654 1655 1656

    /* Only proceed if there's room for the extra text */

    if (strlen(new_status) + strlen(progress) + add <= sizeof(new_status))
    {
      /* Concatenate the extra text */

      strcat(new_status, " ");
1657
      if (controls.use_brackets) strcat(new_status, "(");
1658
      strcat(new_status, progress);
1659
      if (controls.use_brackets) strcat(new_status, ")");
1660 1661 1662 1663 1664 1665 1666 1667 1668
    }
  }

  /* Check that the string isn't the same as already */
  /* present, and if not, update the status bar. The */
  /* 'url' char array used temporarily is now free,  */
  /* with the new status line in new_status, so we   */
  /* can reuse that here.                            */

1669
  ChkError(displayfield_get_value(0, t, StatusBarStatus, url, sizeof(url),NULL));
1670 1671 1672 1673 1674 1675 1676 1677 1678

  if (
       strcmp(use_status, url)
     )
  {
    /* If the string has changed, display it */

    ChkError(displayfield_set_value(0,
                                    t,
1679
                                    StatusBarStatus,
1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721
                                    use_status));

    /* Ensure the toolbar buttons are up to date in light of the new status */

    ChkError(toolbars_set_button_states(priority));
  }

  #ifdef TRACE
    if (tl & (1u<<1)) Printf("toolbars_write_status: Successful, returning with message type %d\n",priority);
  #endif

  return highest;
}

/*************************************************/
/* toolbars_cancel_status()                      */
/*                                               */
/* toolbars_remove_status_item is used to        */
/* remove a browser from its ancestor's array of */
/* status_content structures because the browser */
/* is about to become invalid (e.g. be deleted   */
/* because its associated window was closed).    */
/*                                               */
/* This function is used if an existing browser  */
/* wants to cancel a message it registered       */
/* earlier, before it times out. The message     */
/* type that is being cancelled must be stated;  */
/* if the browser's entry in the status_content  */
/* array does not record this type, no action    */
/* will be taken. Otherwise the message will be  */
/* cancelled as asked.                           */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             which wants the message           */
/*             cancelling;                       */
/*                                               */
/*             Message type that it wants to     */
/*             remove.                           */
/*************************************************/

_kernel_oserror * toolbars_cancel_status(browser_data * b, status_type type)
{
1722
  browser_data    * ancestor = utils_ancestor(b);
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788
  status_content  * contents;
  int               i, found;

  #ifdef TRACE
    if (tl & (1u<<1)) Printf("toolbars_cancel_status: Called for %p, type %d\n",b,type);
  #endif

  contents = ancestor->status_contents;
  if (!contents || !ancestor->nstatus)
  {
    #ifdef TRACE
      if (tl & (1u<<1)) Printf("toolbars_cancel_status: Exitting - ancestor has no array\n");
    #endif

    return NULL;
  }

  /* Find the entry */

  found = -1;

  for (i = 0; i < ancestor->nstatus; i++)
  {
    if (contents[i].b == b)
    {
      found = i;
      break;
    }
  }

  /* If not found, exit */

  if (found < 0)
  {
    #ifdef TRACE
      if (tl & (1u<<1)) Printf("toolbars_cancel_status: Exitting - can't find entry\n");
    #endif

    return NULL;
  }

  /* If not the given message type, exit */

  if (contents[found].type != type)
  {
    #ifdef TRACE
      if (tl & (1u<<1)) Printf("toolbars_cancel_status: Exitting - entry message type %d doesn't match given type %d\n", contents[found].type, type);
    #endif

    return NULL;
  }

  /* Otherwise, clear the message and restore something */
  /* more meaningful with the inference routine         */

  contents[found].start = 0;
  contents[found].end   = 0;
  contents[found].type  = Toolbars_Status_NoType;

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

  return toolbars_infer_status(b, ancestor, found);
}

1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
/*************************************************/
/* toolbars_cancel_all()                         */
/*                                               */
/* If a browser wants to cancel all messages it  */
/* registered earlier, except for LinkTo or Help */
/* messages, perhaps to ensure that some message */
/* is displayed now rather than after another    */
/* times out, it should call this function. The  */
/* original idea was for browsers that have just */
/* completed a full fetch, which want to get rid */
/* of any timing out formatting or fetching      */
/* messages.                                     */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             which wants the message           */
/*             cancelling.                       */
/*************************************************/

_kernel_oserror * toolbars_cancel_all(browser_data * b)
{
  _kernel_oserror * e;

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

1815
  RetError(toolbars_cancel_status(b, Toolbars_Status_PlugIn));
1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
  RetError(toolbars_cancel_status(b, Toolbars_Status_Connecting));
  RetError(toolbars_cancel_status(b, Toolbars_Status_Redirected));
  RetError(toolbars_cancel_status(b, Toolbars_Status_Responded));
  RetError(toolbars_cancel_status(b, Toolbars_Status_SentReq));
  RetError(toolbars_cancel_status(b, Toolbars_Status_Connected));
  RetError(toolbars_cancel_status(b, Toolbars_Status_Fetching));
  RetError(toolbars_cancel_status(b, Toolbars_Status_GetPics));
  RetError(toolbars_cancel_status(b, Toolbars_Status_Processing));
  RetError(toolbars_cancel_status(b, Toolbars_Status_Formatting));

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

  return NULL;
}

1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866
/*************************************************/
/* toolbars_timeout_status()                     */
/*                                               */
/* A null event handler registered and deregis-  */
/* tered by toolbars_update_specific_status,     */
/* which handles checking the 'start' and 'end'  */
/* fields of the given ancestor browser_data     */
/* struct to see if any messages have expired.   */
/*                                               */
/* toolbars_update_specific_status will only     */
/* allow browsers to register progressively      */
/* higher priority messages than previously      */
/* registered - this routine allows those        */
/* previous priorities to drop down.             */
/*                                               */
/* Parameters are as standard for a Wimp null    */
/* event handler (handle = pointer to the        */
/* ancestor browser_data structure).             */
/*************************************************/

int toolbars_timeout_status(int eventcode, WimpPollBlock * b, IdBlock * idb, browser_data * handle)
{
  browser_data   * ancestor = handle; /* Just to make things clearer */
  status_content * contents = ancestor->status_contents;
  int              i, timenow;

  /* Fail if there's no status_content array */

  if (!contents || !ancestor->nstatus)
  {
    #ifdef TRACE
      erb.errnum = Utils_Error_Custom_Normal;

      StrNCpy0(erb.errmess,
1867
               "No status_content array in toolbars_timeout_status")
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949

      show_error_ret(&erb);
    #endif

    return 0;
  }

  /* Go through all entries in the array */

  for (i = 0; i < ancestor->nstatus; i++)
  {
    if (contents[i].start > 0)
    {
      /* An active message that is timing out */

      _swix(OS_ReadMonotonicTime,
            _OUT(0),

            &timenow);

      if (timenow > contents[i].end)
      {
        /* Message has expired */

        contents[i].start = 0;
        contents[i].end   = 0;
        contents[i].type  = Toolbars_Status_NoType;

        ChkError(toolbars_infer_status(contents[i].b, ancestor, i));
      }
    }
    else
    {
      /* Message has <= 0 in 'start' field; if non-idle, this is an expiry indicator */

      switch (contents[i].type)
      {
        case Toolbars_Status_NoType:
        case Toolbars_Status_Ready:
        case Toolbars_Status_Viewing:
        {
          /* Idle, so do nothing */

          i = i; /* Ensure compiler handles this OK... */
        }

        default:
        {
          /* Non-idle message; it has expired */

          contents[i].start = 0;
          contents[i].end   = 0;
          contents[i].type  = Toolbars_Status_NoType;
        }
        break;
      }
    }
  }

  return 0;
}

/*************************************************/
/* toolbars_return_inferred()                    */
/*                                               */
/* Looks at a given browser_data struct to work  */
/* out what state it is in, returning this       */
/* information as a status_type value (see       */
/* Toolbars.h).                                  */
/*                                               */
/* Parameters: Pointer to the browser_data       */
/*             struct in question.               */
/*                                               */
/* Returns:    Its status, as a status_type      */
/*             value.                            */
/*************************************************/

static status_type toolbars_return_inferred(browser_data * b)
{
  status_type type = Toolbars_Status_Ready;

  /* Work out what message should be displayed. */
1950 1951 1952 1953 1954 1955 1956
  /* Since this is most often called when a     */
  /* status message expires, any messages which */
  /* should appear persistently in the status   */
  /* bar - such as Fetching or Viewing - should */
  /* be returned by this function. Otherwise,   */
  /* the message will be replaced by another    */
  /* that this function chooses.                */
1957

1958 1959 1960
  /* Is there a Plug-In status message? */

  if (b->plugin_status && *b->plugin_status)
1961
  {
1962
    type = Toolbars_Status_PlugIn;
1963

1964
    /* Otherwise, is a page fetch in progress? */
1965

1966 1967 1968 1969 1970
    if (fetch_fetching(b))
    {
      /* If fetching, may in fact just be passing tokens through the fetcher */
      /* with no new actual data to get from the server. In that case want   */
      /* to give a 'processing' message; otherwise, definitely 'fetching'.   */
1971

1972 1973
      if (b->fetch_status == BS_PROCESS) type = Toolbars_Status_Processing;
      else
1974
      {
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991
        /* Need to find out the genuine fetch status, or all of the */
        /* varied fetch information that we can get out of the      */
        /* fetchers gets collapsed to some less meaningful generic  */
        /* message by this routine.                                 */

        int s;

        if (!url_status(0, b->fetch_handle, &s, NULL, NULL))
        {
          if      (s & URL_Status_Transfer)  type = Toolbars_Status_Fetching;
          else if (s & URL_Status_Responded) type = Toolbars_Status_Responded;
          else if (s & URL_Status_SentReq)   type = Toolbars_Status_SentReq;
          else if (s & URL_Status_SentData)  type = Toolbars_Status_SentReq;
          else if (s & URL_Status_Connected) type = Toolbars_Status_Connected;
          else if (!s)                       type = Toolbars_Status_Connecting;
        }
        else type = Toolbars_Status_Fetching;
1992 1993
      }
    }
1994 1995 1996
    else
    {
      int specimg;
1997

1998 1999
      /* If not fetching pages, are we fetching images? If so, display an */
      /* appropriate message.                                             */
2000

2001
      specimg = image_count_specific_pending(b);
2002

2003 2004 2005 2006
      if (specimg && image_fetching(b)) type = Toolbars_Status_GetPics;
      else
      {
        /* If not fetching images either, then may be formatting or idle. */
2007

2008 2009 2010
        if (reformat_formatting(b)) type = Toolbars_Status_Formatting;
        else                        type = Toolbars_Status_Viewing;
      }
2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056
    }
  }

  return type;
}

/*************************************************/
/* toolbars_infer_status()                       */
/*                                               */
/* When a toolbar message in the status_content  */
/* array of an ancestor browser has expired,     */
/* this routine is called to work out what the   */
/* current status for the owner browser should   */
/* be. The toolbar is appropriately updated.     */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             which owns the message;           */
/*                                               */
/*             Pointer to a browser_data struct  */
/*             which is the above's ancestor;    */
/*                                               */
/*             Index into the ancestor's         */
/*             status_content array for the      */
/*             message owner.                    */
/*                                               */
/* Assumes:    That the ancestor pointer is not  */
/*             NULL.                             */
/*************************************************/

static _kernel_oserror * toolbars_infer_status(browser_data * b, browser_data * ancestor, int entry)
{
  status_content * contents = ancestor->status_contents;
  status_type      type;

  #ifdef TRACE
    if (tl & (1u<<1)) Printf("toolbars_infer_status: Called for %p (ancestor %p)\n",b,ancestor);
  #endif

  /* Fail if there's no status_content array */

  if (!contents || !ancestor->nstatus)
  {
    #ifdef TRACE
      erb.errnum = Utils_Error_Custom_Normal;

      StrNCpy0(erb.errmess,
2057
               "No status_content array in toolbars_infer_status")
2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191

      show_error_ret(&erb);
    #endif

    return NULL;
  }

  /* Find the message type */

  type = toolbars_return_inferred(b);

  /* Exit through the toolbar update routine */

  #ifdef TRACE
    if (tl & (1u<<1)) Printf("toolbars_infer_status: Exitting through toolbars_update_specific_status with type %d\n",type);
  #endif

  return toolbars_update_specific_status(b, ancestor, entry, type);
}

/*************************************************/
/* toolbars_add_status_item()                    */
/*                                               */
/* Adds an item to a given ancestor's array of   */
/* status_content structures. The contents are   */
/* not initialised - this is left to the caller. */
/*                                               */
/* Parameters: Pointer to browser_data struct in */
/*             which the addition should be made */
/*             (i.e. the ancestor).              */
/*                                               */
/* Assumes:    That the ancestor browser_data    */
/*             pointer is not NULL.              */
/*************************************************/

static _kernel_oserror * toolbars_add_status_item(browser_data * ancestor)
{
  /* Increment the status counter */

  ancestor->nstatus ++;

  /* Allocate the required memory */

  return memory_set_chunk_size(ancestor, NULL, CK_STAT, ancestor->nstatus * sizeof(status_content));
}

/*************************************************/
/* toolbars_remove_status_item()                 */
/*                                               */
/* Removes a browser_data structure from its     */
/* ancestor's array of status_content structs.   */
/*                                               */
/* Typically called by windows_close_browser, so */
/* issues of having children within the window   */
/* relating to the given browser are dealt with  */
/* automatically (children would be being closed */
/* through a function in Frames.c, with the      */
/* Windows.c function called as part of this).   */
/*                                               */
/* Parameters: Pointer to the browser_data       */
/*             struct to remove from the array;  */
/*                                               */
/*             Pointer to the ancestor           */
/*             browser_data structure.           */
/*                                               */
/* Assumes:    That the ancestor pointer is not  */
/*             NULL.                             */
/*************************************************/

_kernel_oserror * toolbars_remove_status_item(browser_data * b, browser_data * ancestor)
{
  status_content * contents = ancestor->status_contents;
  int              i, found;

  /* Can't proceed if the ancestor has no contents array */
  /* (this is fine, so TRACE builds don't give an error  */
  /* either).                                            */

  if (!contents || !ancestor->nstatus) return NULL;

  /* Find the browser's entry in the status contents array */

  found = -1;

  for (i = 0; i < ancestor->nstatus; i++)
  {
    if (contents[i].b == b)
    {
      found = i;
      break;
    }
  }

  /* If not found, exit */

  if (found < 0) return NULL;

  /* Otherwise, remove the entry */

  if (found < ancestor->nstatus - 1)
  {
    memmove(&contents[found],
            &contents[found + 1],
            (ancestor->nstatus - found - 1) * sizeof(status_content));
  }

  ancestor->nstatus--;

  return memory_set_chunk_size(ancestor, NULL, CK_STAT, ancestor->nstatus * sizeof(status_content));
}

/*************************************************/
/* toolbars_return_act_status()                  */
/*                                               */
/* Returns the highest status for the ancestor   */
/* of a given browser window that does not       */
/* include non-active transient items            */
/* (specifically, LinkTo and Help messages).     */
/*                                               */
/* This allows the caller to determine whether a */
/* browser and any of its frames are fetching,   */
/* for example.                                  */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the ancestor in       */
/*             question.                         */
/*                                               */
/* Returns:    A status_type (see Toolbars.h)    */
/*             describing the ancestor's current */
/*             status.                           */
/*************************************************/

static status_type toolbars_return_act_status(browser_data * b)
{
2192 2193
  browser_data   * ancestor = utils_ancestor(b);
  status_type      highest  = Toolbars_Status_NoType;
2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242
  status_content * contents;
  int              i;

  if (!b) return Toolbars_Status_NoType;

  /* Find the highest item that isn't a LinkTo or Help message */

  contents = ancestor->status_contents;

  for (i = 0; i < ancestor->nstatus; i++)
  {
    if (
         contents[i].type == Toolbars_Status_LinkTo ||
         contents[i].type == Toolbars_Status_Help
       )
    {
      status_type type;

      /* Non-active status entry, so need to work out what */
      /* it would say if this message had timed out        */

      type = toolbars_return_inferred(b);

      if (type > highest) highest = type;
    }
    else
    {
      /* Otherwise, remember this status if its the highest */

      if (contents[i].type > highest) highest = contents[i].type;
    }
  }

  return highest;
}

/*************************************************/
/* toolbars_update_progress()                    */
/*                                               */
/* Reflects a fetch's progress in the browser    */
/* window's status bar.                          */
/*                                               */
/* Parameters: A pointer to the browser_data     */
/*             structure associated with the     */
/*             fetch.                            */
/*************************************************/

void toolbars_update_progress(browser_data * b)
{
2243 2244
  char              progress [Limits_StatusBarProgress];
  char              old      [Limits_StatusBarProgress];
2245 2246
  ObjectId          t;
  _kernel_oserror * e;
2247
  browser_data    * ancestor = utils_ancestor(b);
2248
  int               saves;
2249

2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277
  /* May want to only update at specific intervals of time */

  if (
       controls.progress_update_delay   &&
       !
       (
         b->fetch_status == BS_IDLE    ||
         b->fetch_status == BS_PROCESS
       )
     )
  {
    int time_now;

    if (
         !_swix(OS_ReadMonotonicTime,
                _OUT(0),

                &time_now)
       )
    {
      if (time_now - ancestor->progress_updated < controls.progress_update_delay) return;

      ancestor->progress_updated = time_now;
    }
  }

  /* Find out how many file saves, if any, are in progress */

2278
  saves = toolbars_create_progress(b, progress, sizeof(progress));
2279

2280 2281
  /* Find the toolbar and try to get the old progress string */

2282 2283 2284
  t = toolbars_get_lower(ancestor);
  if (!t) return;

2285
  e = button_get_value(0, t, StatusBarProgress, old, sizeof(old), NULL);
2286 2287 2288 2289

  /* Only update the display field if the contents have */
  /* changed, to avoid flicker.                         */

2290 2291 2292 2293 2294 2295 2296
  if (!e && strcmp(progress, old))
  {
    int flags;
    int colour_old, colour_new;

    /* Set the colour if required */

2297
    if (controls.colour_progress != Controls_ColourProgress_NotAColour)
2298
    {
2299
      if (!button_get_flags(0, t, StatusBarProgress, &flags))
2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310
      {
        browser_data * ancestor = b->ancestor;

        if (!ancestor) ancestor = b;

        /* Remember the old colour, and set the new according  */
        /* to whether or not file saves are in progress. If    */
        /* there, use the Messages file defined colour; else   */
        /* use the colour recorded in the browser_data struct. */

        colour_old = (flags & 0x0f000000) >> 24;
2311
        colour_new = saves ? controls.colour_progress : ancestor->progress_colour;
2312 2313 2314 2315 2316

        if (colour_new != colour_old)
        {
          button_set_flags(0,
                           t,
2317
                           StatusBarProgress,
2318 2319 2320 2321 2322 2323
                           0x0f000000,
                           colour_new << 24);
        }
      }
    }

2324
    button_set_value(0, t, StatusBarProgress, progress);
2325
  }
2326
  else if (e && controls.append_status) toolbars_update_status(b, Toolbars_Status_Ready);
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 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381
/*************************************************/
/* toolbars_count_file_saves()                   */
/*                                               */
/* Checks to see if any frames in a given        */
/* frameset are saving out files.                */
/*                                               */
/* Parameters: Pointer to any browser_data       */
/*             structure in the frameset.        */
/*                                               */
/* Returns:    The number of file saves going on */
/*             in the parent and any children it */
/*             might have.                       */
/*************************************************/

static int toolbars_count_file_saves(browser_data * b)
{
  browser_data * ancestor = b->ancestor;

  if (!ancestor) ancestor = b;

  return toolbars_count_file_saves_r(ancestor);
}

/*************************************************/
/* toolbars_count_file_saves_r()                 */
/*                                               */
/* Recursive back-end to                         */
/* toolbars_count_file_saves.                    */
/*                                               */
/* Parameters: Pointer to the ancestor           */
/*             browser_data struct in the        */
/*             frameset.                         */
/*                                               */
/* Returns:    As toolbars_count_file_saves.     */
/*************************************************/

static int toolbars_count_file_saves_r(browser_data * b)
{
  int i, count = 0;

  if (b->nchildren)
  {
    for (i = 0; i < b->nchildren; i++)
    {
      count += toolbars_count_file_saves_r(b->children[i]);
    }
  }

  if (b->save_file) count ++;

  return count;
}

2382 2383 2384 2385
/*************************************************/
/* toolbars_calculate_progress()                 */
/*                                               */
/* Works out how much data has been fetched for  */
2386
/* a frameset.                                   */
2387
/*                                               */
2388 2389 2390 2391 2392 2393 2394 2395 2396 2397
/* If a file save is in progress, the amount of  */
/* data fetched for that file save and nothing   */
/* else is returned as a negative number (to     */
/* flag that this is happening). Callers must    */
/* remember to check for this, even if only to   */
/* reverse the sign on the returned value.       */
/*                                               */
/* Parameters: Pointer to any browser_data       */
/*             struct in the frameset in         */
/*             question.                         */
2398 2399
/*                                               */
/* Returns:    The amount collectively fetched,  */
2400 2401 2402 2403
/*             in bytes, or the amount fetched   */
/*             for a file save with its sign     */
/*             reversed (i.e. a negative number) */
/*             again in bytes.                   */
2404 2405 2406
/*************************************************/

static int toolbars_calculate_progress(browser_data * b)
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
{
  browser_data * ancestor = b->ancestor;
  int            saves    = toolbars_count_file_saves(b);

  if (!ancestor) ancestor = b;

  return toolbars_calculate_progress_r(ancestor, saves);
}

/*************************************************/
/* toolbars_calculate_progress_r()               */
/*                                               */
/* Recursive back-end to                         */
/* toolbars_calculate_progress.                  */
/*                                               */
/* Parameters: Pointer to the ancestor           */
/*             browser_data struct in the        */
/*             frameset;                         */
/*                                               */
/*             0 if no browser in the frameset   */
/*             is saving data to a file, else    */
/*             non-zero.                         */
/*                                               */
/* Returns:    As toolbars_calculate_progress.   */
/*************************************************/

static int toolbars_calculate_progress_r(browser_data * b, int saves)
2434 2435 2436 2437 2438
{
  int i, fetched = 0;

  if (b->nchildren)
  {
2439 2440 2441 2442 2443 2444 2445
    int localfetched;

    /* For children, if the frameset has some file saving going on */
    /* use only negative numbers returned, i.e. report cumulative  */
    /* file sizes. Otherwise, use only positive numbers, i.e.      */
    /* report image / page source fetched data.                    */

2446 2447
    for (i = 0; i < b->nchildren; i++)
    {
2448 2449 2450 2451 2452 2453 2454 2455 2456 2457
      localfetched = toolbars_calculate_progress_r(b->children[i], saves);

      if (saves)
      {
        if (localfetched < 0) fetched += localfetched;
      }
      else
      {
        if (localfetched > 0) fetched += localfetched;
      }
2458 2459 2460
    }
  }

2461 2462 2463 2464 2465
  if (b->save_file)
  {
    /* If saving, don't want to confuse the issue with any image */
    /* fetch information or whatever. So just use the current    */
    /* output file size.                                         */
2466

2467 2468 2469 2470 2471
    fetched = (int) (-(ftell(b->save_file)));
  }
  else
  {
    /* Count the image data obtained so far */
2472

2473
    fetched += image_total_bytes_fetched(b);
2474

2475
    /* Add the current source store size */
2476

2477 2478 2479
    if (b->save_oldstore) fetched += b->save_oldstore;
    else if (b->source)   fetched += flex_size((flex_ptr) &b->source);
  }
2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491

  return fetched;
}

/*************************************************/
/* toolbars_create_progress()                    */
/*                                               */
/* Builds a string indicating fetch progress in  */
/* a given buffer.                               */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the fetch;            */
2492
/*                                               */
2493
/*             Pointer to the buffer;            */
2494
/*                                               */
2495 2496 2497 2498 2499
/*             Size of that buffer.              */
/*                                               */
/* Returns:    The number of file saves going on */
/*             inside the frameset, from         */
/*             toolbars_count_file_saves.        */
2500 2501
/*************************************************/

2502
static int toolbars_create_progress(browser_data * b, char * buffer, int buffer_size)
2503
{
2504 2505 2506 2507
  browser_data * ancestor   = utils_ancestor(b);
  int            saves      = 0;
  int            len        = 1; /* Start with the terminating zero byte accounted for */
  int            percentage = 0;
2508 2509 2510
  int            fetched;

  fetched = toolbars_calculate_progress(ancestor);
2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537
  if (fetched < 0) fetched = -fetched, saves = toolbars_count_file_saves(b); /* If a negative number, the absolute value is the amount of data being saved to a file */

  /* Is the buffer big enough? */

  if      (fetched < 10240)    len = utils_number_length(fetched);
  else if (fetched < 10485760) len = utils_number_length((fetched + 512)    / 1024)    + 1; /* For 'K' */
  else                         len = utils_number_length((fetched + 524288) / 1048576) + 1; /* For the 'M' */

  if (saves) len += utils_number_length(saves) + 2;

  if (len > buffer_size)
  {
    #ifdef TRACE

      erb.errnum = Utils_Error_Custom_Normal;

      sprintf(erb.errmess,
              "Buffer size of %d isn't as large as required (%d) in toolbars_create_progress",
              buffer_size,
              len);

      show_error_ret(&erb);

    #endif

    return saves;
  }
2538

2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551
  /* For single file saves, we may be able to use a percentage counter */

  if (saves == 1 && b->data_size > 0 && b->save_link)
  {
    percentage = (fetched * 100) / b->data_size;

    /* If the data size was misjudged, we can exceed 100% fetched. In */
    /* that case, drop back to a byte counter, rather than sticking   */
    /* at 100%.                                                       */

    if (percentage > 100) percentage = 0;
  }

2552 2553 2554 2555
  /* Write the amount into the buffer; '0' for <=0 bytes, a byte  */
  /* amount for less than 10K, a K amount for less than 10M, else */
  /* an amount in megabytes.                                      */

2556 2557
  if (saves < 2)
  {
2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570
    if (percentage)
    {
      /* Special case - if we know the data size, and we're saving an object, */
      /* then display a percentage amount fetched instead.                    */

      sprintf(buffer, "%d%%", percentage);
    }
    else
    {
      if      (fetched < 10240)    sprintf(buffer, "%d",  fetched);
      else if (fetched < 10485760) sprintf(buffer, "%dK", (fetched + 512)    / 1024);
      else                         sprintf(buffer, "%dM", (fetched + 524288) / 1048576);
    }
2571 2572 2573
  }
  else
  {
2574 2575 2576
    if      (fetched < 10240)    sprintf(buffer, "%d: %d",  saves, fetched);
    else if (fetched < 10485760) sprintf(buffer, "%d: %dK", saves, (fetched + 512)    / 1024);
    else                         sprintf(buffer, "%d: %dM", saves, (fetched + 524288) / 1048576);
2577
  }
2578

2579
  return saves;
2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593
}

/*************************************************/
/* toolbars_update_url()                         */
/*                                               */
/* Reflects the currently fetching URL in the    */
/* URL bar of a browser window.                  */
/*                                               */
/* Parameters: A pointer to the browser_data     */
/*             structure relevant to the window. */
/*************************************************/

void toolbars_update_url(browser_data * b)
{
2594
  char     url[Limits_URLBarWrit];
2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634
  char     cmp[sizeof(url)];
  ObjectId t;

  t = toolbars_get_upper(b);
  if (!t) return;

  if (browser_fetch_url(b))
  {
    StrNCpy0(url, browser_fetch_url(b));
  }
  else if (browser_current_url(b))
  {
    StrNCpy0(url, browser_current_url(b));
  }
  else *url = 0;

  /* If we can use URL aliases, then compare the text to put in */
  /* the bar against the Hotlist URL string. If the same, use   */
  /* an alias for the hotlist instead of the given URL.         */

  #ifdef ALIAS_URLS
  {
    char compare[sizeof(url)];

    urlutils_create_hotlist_url(compare, sizeof(compare));

    if (!strcmp(url, compare)) StrNCpy0(url, lookup_token("AtHotlist:Hotlist",0,0));
  }
  #endif

  #ifdef HIDE_CGI
    toolbars_hide_cgi(url);
  #endif

  /* For internal URLs, strip off the internal bit at the start */

  toolbars_hide_internal(url);

  /* Write the URL in only if it's different from the existing contents */

2635 2636
  writablefield_get_value(0, t, URLBarWrit, cmp, sizeof(cmp), NULL);
  if (strcmp(cmp, url)) writablefield_set_value(0, t, URLBarWrit, url);
2637 2638 2639
}

/*************************************************/
2640
/* toolbars_update_dialler_time()                */
2641 2642 2643 2644 2645 2646
/*                                               */
/* Updates the dialler status display in the     */
/* upper toolbar. Should only be called if there */
/* is a Display Field component, ID 0xF, which   */
/* can take the required display values.         */
/*                                               */
2647 2648 2649 2650
/* This shows online time. It is expected that   */
/* the function will only be called from a null  */
/* event handler to update the time regularly.   */
/*                                               */
2651 2652 2653 2654
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the display.          */
/*************************************************/

2655
_kernel_oserror * toolbars_update_dialler_time(browser_data * b)
2656 2657 2658 2659
{
  _kernel_oserror * e;
  ObjectId          t;
  int               status, start_time, time_now;
2660 2661
  char              display[Limits_URLBarDiallerStatus];
  char              compare[Limits_URLBarDiallerStatus];
2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685

  t = toolbars_get_upper(b);
  if (!t) return NULL;

  e = _swix(Dialler_Status,
            _IN(0) | _OUTR(0,1),

            Dialler_Status_ConnectTime,

            &status,
            &start_time);

  /* If the dialler isn't present, don't want to end up */
  /* with a recursive error report, so fail silently.   */

  if (e) return NULL;

  _swix(OS_ReadMonotonicTime,
        _OUT(0),

        &time_now);

  /* Only update as often as specified in the Messages file */

2686
  if (time_now - b->dialler_last > controls.show_dstat_for)
2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707
  {
    /* If connected, must display an online time, else show offline. */

    if (status & Dialler_Connected)
    {
      int hours, minutes, seconds;

      /* Remember when this was done */

      b->dialler_last = time_now;

      /* Work out how long we've been online */

      time_now -= start_time;

      if (time_now < 0) time_now = 0;

      hours     = time_now / 360000;
      time_now -= (hours * 360000);
      minutes   = time_now / 6000;
      time_now -= (minutes * 6000);
2708
      seconds   = (time_now / (controls.quantise * 100)) * controls.quantise;
2709 2710 2711

      if (hours > 99) hours = 99;

2712 2713
      if (sizeof(display) >= 9) sprintf(display, "%02u:%02u:%02u\0", hours, minutes, seconds);
      else *display = 0;
2714 2715 2716

      /* Get the current string into 'compare' */

2717
      e = displayfield_get_value(0, t, URLBarDiallerStatus, compare, sizeof(compare), NULL);
2718
      if (e) return e;
2719
      compare[sizeof(compare) - 1] = 0;
2720 2721 2722 2723 2724

      /* Update the display field if necessary, else exit */

      if (strcmp(display, compare))
      {
2725
        e = displayfield_set_value(0, t, URLBarDiallerStatus, display);
2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756
        if (e) return e;
      }
    }
  }

  return NULL;
}

/*************************************************/
/* toolbars_update_dialler_status()              */
/*                                               */
/* Updates the dialler status display in the     */
/* upper toolbar. Should only be called if there */
/* is a Display Field component, ID 0xF, which   */
/* can take the required display values.         */
/*                                               */
/* If the status is 'online', a null event       */
/* handler will be installed to call             */
/* toolbars_update_dialler_time to show the      */
/* online time. If the status is not 'online',   */
/* that handler will be deregistered if present. */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the display.          */
/*************************************************/

_kernel_oserror * toolbars_update_dialler_status(browser_data * b)
{
  _kernel_oserror * e;
  ObjectId          t;
  int               status;
2757
  int               showing_time = 0;
2758 2759
  char              display[Limits_URLBarDiallerStatus];
  char              statstr[Limits_URLBarDiallerStatus];
2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796
  char              compare[sizeof(display)];

  t = toolbars_get_upper(b);
  if (!t) return NULL;

  memset(statstr, 0, sizeof(statstr));

  e = _swix(Dialler_Status,
            _INR(0,2) | _OUT(0),

            Dialler_Status_StatusString,
            statstr,
            sizeof(statstr),

            &status);

  /* If the dialler isn't present, don't want to end up */
  /* with a recursive error report, so fail silently.   */

  if (e) return NULL;

  statstr[sizeof(statstr) - 1] = 0;

  if (status & Dialler_StatusChanged)
  {
    /* Use the status string. Put the text in the 'tokens' messages buffer */
    /* as this is used lower down for the actual dialler status display    */
    /* string's source. To ensure that any future lookup_token calls don't */
    /* look at the 'lasttokn' buffer and think they don't need to do any   */
    /* work, must mark the contents of 'tokens' as invalid by setting      */
    /* the first char of 'lasttokn' to 0.                                  */

    *lasttokn = 0;
    StrNCpy0(tokens, statstr);
  }
  else
  {
2797
    int info = status & Dialler_GeneralInfoMask;
2798 2799 2800 2801 2802 2803

    /* Get the relevant status text in the 'tokens' messages buffer. */
    /* If an item is missing from the Messages file, the lookup will */
    /* fail silently leaving '!' in the tokens buffer. This is taken */
    /* to mean 'don't show this status', and the routine will exit.  */

2804
    switch (info)
2805
    {
2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817
      case Dialler_ExecutingScript_Dial:     lookup_token("Dialling",  0, 0); break; /* Executing dialling script */
      case Dialler_ExecutingScript_Hangup:   lookup_token("Hangup",    0, 0); break; /* Executing hangup script   */
      case Dialler_ExecutingScript_Answer:   lookup_token("Answering", 0, 0); break; /* Answering                 */
      case Dialler_AbortedScript_Syntax:     lookup_token("SError",    0, 0); break; /* Script syntax error       */
      case Dialler_AbortedScript_Timeout:    lookup_token("Timeout",   0, 0); break; /* Timed out                 */
      case Dialler_AbortedScript_NoCarrier:  lookup_token("Carrier",   0, 0); break; /* No carrier                */
      case Dialler_AbortedScript_Error:      lookup_token("MError",    0, 0); break; /* 'ERROR' from modem        */
      case Dialler_AbortedScript_NoDialtone: lookup_token("Dialtone",  0, 0); break; /* No dialtone               */
      case Dialler_AbortedScript_Busy:       lookup_token("MBusy",     0, 0); break; /* 'BUSY' from modem         */
      case Dialler_AbortedScript_NoAnswer:   lookup_token("Answer",    0, 0); break; /* No answer                 */

      /* If there's no specific special status, are we online? */
2818 2819 2820

      default:
      {
2821 2822
        if (status & Dialler_Connected) showing_time = 1;
        else lookup_token("Offline", 0, 0);
2823 2824 2825 2826 2827
      }
      break;
    }
  }

2828
  if (showing_time)
2829
  {
2830 2831
    /* If we want to show online time, install the handler to do so */
    /* (if it isn't already installed).                             */
2832

2833
    if (!b->dialler_last)
2834
    {
2835 2836 2837 2838 2839 2840 2841 2842 2843
      /* dialler_last holds a timer, and if non-zero is used by window close */
      /* routines to show that the null claimant needs deregistering. It is  */
      /* unlikely, but nonetheless possible, that the window could be closed */
      /* after the handler is registered but before it is called, so the     */
      /* dialler_last field would still be zero and no deregistration would  */
      /* take place. Hence to ensure that this doesn't happen, set the field */
      /* to a (small) non-zero value for now.                                */

      b->dialler_last = 1;
2844 2845 2846 2847 2848 2849
      register_null_claimant(Wimp_ENull, (WimpEventHandler *) handle_dialler_display, b);
    }
  }
  else
  {
    /* Otherwise, remove the handler if it is installed */
2850

2851 2852
    if (b->dialler_last) deregister_null_claimant(Wimp_ENull, (WimpEventHandler *) handle_dialler_display, b);
    b->dialler_last = 0;
2853

2854 2855
    /* Copy the token contents to the display buffer and update the field */
    /* if the value has changed.                                          */
2856

2857
    StrNCpy0(display, tokens);
2858

2859
    e = displayfield_get_value(0, t, URLBarDiallerStatus, compare, sizeof(compare), NULL);
2860
    if (e) return e;
2861

2862
    /* Update the display field if necessary, else exit */
2863

2864 2865
    if (strcmp(display, compare))
    {
2866
      e = displayfield_set_value(0, t, URLBarDiallerStatus, display);
2867 2868
      if (e) return e;
    }
2869 2870 2871 2872 2873
  }

  /* May need to alter the label on this display, if there is one */

  {
2874
    char label[Limits_URLBarDiallerStatusLabel];
2875

2876
    if (button_get_value(0, t, URLBarDiallerStatusLabel, label, sizeof(label), NULL)) return NULL;
2877
    label[sizeof(label) - 1] = 0;
2878 2879 2880 2881 2882 2883 2884 2885

    if (!showing_time) lookup_token("DiaStatusDial:Dialler",0,0);
    else               lookup_token("DiaStatusTime:Time"   ,0,0);

    if (strcmp(label, tokens))
    {
      StrNCpy0(label, tokens);

2886
      return button_set_value(0, t, URLBarDiallerStatusLabel, label);
2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901
    }
  }

  return NULL;
}

/*************************************************/
/* toolbars_merged_to_status()                   */
/*                                               */
/* If the URL writable and status lines are      */
/* merged, this ensures that the combined field  */
/* is put into 'status' display.                 */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the field;            */
2902
/*                                               */
2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914
/*             Object ID of the toolbar the      */
/*             gadgets are in.                   */
/*                                               */
/* Assumes:    That the fields are indeed merged */
/*             though a null object ID can be    */
/*             given.                            */
/*************************************************/

void toolbars_merged_to_status(browser_data * b, ObjectId t)
{
  if (t)
  {
2915
    char label[Limits_StatusBarStatusLabel];
2916

2917 2918
    show_gadget(t, StatusBarStatus);
    hide_gadget(t, URLBarWrit);
2919 2920 2921

    /* Update the label, if present */

2922
    if (!button_get_value(0, t, StatusBarStatusLabel, label, sizeof(label), NULL))
2923
    {
2924 2925
      label[sizeof(label) - 1] = 0;

2926
      lookup_token("DisplayStats:Status",0,0);
2927

2928 2929 2930
      if (strcmp(label, tokens))
      {
        StrNCpy0(label, tokens);
2931
        button_set_value(0, t, StatusBarStatusLabel, label);
2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945
      }
    }
  }
}

/*************************************************/
/* toolbars_merged_to_url()                      */
/*                                               */
/* If the URL writable and status lines are      */
/* merged, this ensures that the combined field  */
/* is put into 'URL entry' display.              */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the field;            */
2946
/*                                               */
2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958
/*             Object ID of the toolbar the      */
/*             gadgets are in.                   */
/*                                               */
/* Assumes:    That the fields are indeed merged */
/*             though a null object ID can be    */
/*             given.                            */
/*************************************************/

void toolbars_merged_to_url(browser_data * b, ObjectId t)
{
  if (t)
  {
2959
    char label[Limits_StatusBarStatusLabel];
2960

2961 2962
    show_gadget(t, URLBarWrit);
    hide_gadget(t, StatusBarStatus);
2963 2964 2965

    /* Update the label, if present */

2966
    if (!button_get_value(0, t, StatusBarStatusLabel, label, sizeof(label), NULL))
2967
    {
2968 2969
      label[sizeof(label) - 1] = 0;

2970 2971 2972 2973 2974
      lookup_token("DisplayURL:URL",0,0);

      if (strcmp(label, tokens))
      {
        StrNCpy0(label, tokens);
2975
        button_set_value(0, t, StatusBarStatusLabel, label);
2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994
      }
    }
  }
}

/*************************************************/
/* toolbars_set_bistate_state()                  */
/*                                               */
/* Sets a bistate button to a given state, which */
/* depends on the type of button in use.         */
/*                                               */
/* Naming conventions on the button types should */
/* dictate which state is which, with the word   */
/* order of the name relating to the states (so  */
/* for example, BiState_Cancel_Back would be at  */
/* Cancel for state 0, and Back for state 1).    */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the button;           */
2995
/*                                               */
2996 2997
/*             Object ID of the toolbar the      */
/*             button is in;                     */
2998
/*                                               */
2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011
/*             State (0 or 1) to set it to.      */
/*************************************************/

void toolbars_set_bistate_state(browser_data * b, ObjectId t, int state)
{
  switch (b->bistate)
  {
    case BiState_Cancel_Back:
    {
      ObjectId source;

      /* State 0 = Cancel, state 1 = Back */

3012
      source = state ? ButtonBarBack : ButtonBarCancel;
3013 3014 3015

      /* Copy the characteristics of the gadget over. */

3016
      ChkError(copy_toolaction_info(t, source, t, ButtonBarBistate));
3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038

      b->bistate_state = state;
    }
    break;
  }
}

/*************************************************/
/* toolbars_set_tristate_state()                 */
/*                                               */
/* Sets a tristate button to a given state,      */
/* which depends on the type of button in use.   */
/*                                               */
/* Naming conventions on the button types should */
/* dictate which state is which, with the word   */
/* order of the name relating to the states (so  */
/* for example, TriState_Go_GoTo_Stop would be   */
/* at Go for state 0, GoTo for state 1, and Stop */
/* for state 2).                                 */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the button;           */
3039
/*                                               */
3040 3041
/*             Object ID of the toolbar the      */
/*             button is in;                     */
3042
/*                                               */
3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055
/*             State (0, 1 or 2) to set it to.   */
/*************************************************/

void toolbars_set_tristate_state(browser_data * b, ObjectId t, int state)
{
  ObjectId source;

  switch (b->tristate)
  {
    case TriState_Go_GoTo_Stop:
    {
      /* State 0 = Go, 1 = GoTo, 2 = Stop */

3056 3057 3058
      if (!state)          source = ButtonBarGo;
      else if (state == 1) source = ButtonBarGoTo;
      else                 source = ButtonBarStop;
3059 3060 3061

      /* Copy the characteristics of the gadget over. */

3062
      ChkError(copy_toolaction_info(t, source, t, ButtonBarTristate));
3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079

      b->tristate_state = state;
    }
    break;
  }
}

/*************************************************/
/* toolbars_button_height()                      */
/*                                               */
/* Returns the button bar height for a window,   */
/* or 0 if there is no bar present. If there is  */
/* both a button bar and URL bar present the     */
/* combined height is less than the height of    */
/* both individual bars summed, due to an extra  */
/* gap at the edges for aesthetics. This         */
/* function does not attempt to compensate for   */
3080
/* this (see toolbars_url_height, which does).   */
3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             which holds the toolbars of       */
/*             interest.                         */
/*************************************************/

int toolbars_button_height(browser_data * b)
{
  if (b->button_bar)
  {
    ObjectId t;
    BBox     w;

    if (b->all_in_bottom) return 0;

    t = toolbars_get_upper(b);
    if (!t) return 0;

3099
    if (!gadget_get_bbox(0, t, ButtonBarSpacer, &w)) return (w.ymax - w.ymin);
3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132
  }

  return 0;
}

/*************************************************/
/* toolbars_url_height()                         */
/*                                               */
/* Returns the URL bar height for a window, or 0 */
/* if there is no URL bar present. If there is   */
/* both a button bar and a URL bar present the   */
/* returned value will be reduced to account for */
/* the effective overlap of the two bars, as     */
/* when each is present individually there is a  */
/* gap at the edges for aesthetics.              */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             which holds the toolbars of       */
/*             interest.                         */
/*************************************************/

int toolbars_url_height(browser_data * b)
{
  if (b->url_bar)
  {
    ObjectId t;
    BBox     w;

    if (b->all_in_bottom) return 0;

    t = toolbars_get_upper(b);
    if (!t) return 0;

3133
    if (!gadget_get_bbox(0, t, URLBarSpacer, &w))
3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165
    {
      if (b->button_bar) return (w.ymax - w.ymin - toolbars_bar_overlap(t));
      else return (w.ymax - w.ymin);
    }
  }

  return 0;
}

/*************************************************/
/* toolbars_status_height()                      */
/*                                               */
/* Returns the status bar height for a window,   */
/* or 0 if there is no status bar present.       */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             which holds the toolbars of       */
/*             interest.                         */
/*************************************************/

int toolbars_status_height(browser_data * b)
{
  if (b->status_bar)
  {
    ObjectId t;
    BBox     w;

    if (b->all_in_top) return 0;

    t = toolbars_get_lower(b);
    if (!t) return 0;

3166
    if (!gadget_get_bbox(0, t, StatusBarSpacer, &w)) return (w.ymax - w.ymin);
3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179
  }

  return 0;
}

/*************************************************/
/* toolbars_set_button_states()                  */
/*                                               */
/* Sets the button states in the button bar and  */
/* URL bar of a given browser window, greying or */
/* ungreying them according to the browser's     */
/* current state.                                */
/*                                               */
3180 3181 3182 3183 3184
/* This call will not return errors derived from */
/* attempts to access non-existant gadgets; they */
/* will fail silently, to allow more varied UI   */
/* designs to work correctly.                    */
/*                                               */
3185 3186 3187 3188 3189 3190
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the browser window.   */
/*************************************************/

_kernel_oserror * toolbars_set_button_states(browser_data * b)
{
3191
  ObjectId t;
3192 3193

  t = toolbars_get_upper(b);
3194 3195

  if (!t) return NULL; /* (E.g., parents (but not ancestors) in framesets won't have toolbars) */
3196

3197
  if (controls.dont_grey == Controls_DontGrey_GreyAll)
3198
  {
3199 3200 3201 3202 3203 3204 3205 3206 3207
    /* Can only stop if there's activity of some sort */

    if (
         (b->anim_handler    && !b->anim_drift)      ||
         (b->meta_refresh_at && b->meta_refresh_url)
       )
       set_gadget_state(t, ButtonBarStop, 0);

    else set_gadget_state(t, ButtonBarStop, 1);
3208 3209 3210 3211

    /* Can only go back if we're not at the start of the history */
    /* and there's a history to go into.                         */

3212 3213
    if (!history_can_go_backwards(b)) set_gadget_state(t, ButtonBarBack,    1);
    else                              set_gadget_state(t, ButtonBarBack,    0);
3214 3215 3216

    /* Can only go forward if we're in the history somewhere */

3217 3218
    if (!history_can_go_forwards(b))  set_gadget_state(t, ButtonBarForward, 1);
    else                              set_gadget_state(t, ButtonBarForward, 0);
3219

3220 3221
    /* Can only view source, save it, or print if there's source */
    /* present in the first place.                               */
3222

3223 3224
    if (!b->source)
    {
3225 3226 3227
      set_gadget_state(t, ButtonBarViewSource, 1);
      set_gadget_state(t, ButtonBarSaveSource, 1);
      set_gadget_state(t, ButtonBarPrint, 1);
3228 3229 3230
    }
    else
    {
3231 3232 3233
      set_gadget_state(t, ButtonBarViewSource, 0);
      set_gadget_state(t, ButtonBarSaveSource, 0);
      set_gadget_state(t, ButtonBarPrint, 0);
3234
    }
3235

3236 3237
    /* Only need to load images if delayed image loading is set  */
    /* in the local browser flags, or plain backgrounds are set. */
3238

3239 3240
    if (
         (
3241
           !b->show_foreground &&
3242 3243 3244
           b->displayed != Display_External_Image
         )
         ||
3245
         !b->show_background
3246
       )
3247
       set_gadget_state(t, ButtonBarLoadImages, 0);
3248

3249
    else set_gadget_state(t, ButtonBarLoadImages, 1);
3250 3251
  }

3252
  if (controls.dont_grey != Controls_DontGrey_GreyNone)
3253 3254 3255
  {
    /* Can only get a history menu if there's a history present. */

3256 3257
    if (history_empty(b)) set_gadget_state(t, URLBarHistoryMenuR, 1);
    else                  set_gadget_state(t, URLBarHistoryMenuR, 0);
3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272
  }

  /* Handle bistate buttons */

  if (b->bistate)
  {
    switch (b->bistate)
    {
      case BiState_Cancel_Back:
      {
        /* If the URL writable and status display are merged, */
        /* and the URL display is visible, then the field is  */
        /* in writable mode and the bistate should be at      */
        /* 'cancel'. Otherwise, it should be at 'back'.       */

3273
        if (b->merged_url && !gadget_hidden(t, URLBarWrit)) toolbars_set_bistate_state(b, t, 0);
3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292
        else                                                toolbars_set_bistate_state(b, t, 1);
      }
      break;
    }
  }

  /* Handle tristate buttons */

  if (b->tristate)
  {
    switch (b->tristate)
    {
      case TriState_Go_GoTo_Stop:
      {
        /* If the URL writable and status display are merged, */
        /* and the status display is hidden, then the field   */
        /* is in writable mode and the tristate should be at  */
        /* 'go'.                                              */

3293
        if (b->merged_url && !gadget_hidden(t, URLBarWrit)) toolbars_set_tristate_state(b, t, 0);
3294 3295
        else
        {
3296 3297 3298 3299 3300 3301 3302 3303 3304 3305
          /* If we have an animation handler, should be showing 'Stop', */
          /* else we should be at 'go to'. However, odd states can      */
          /* occur so it's wise to check with this tristate, where the  */
          /* state is very important (you can't go anywhere if it's     */
          /* stuck on Stop, for example), what the status bar says.     */

          if (
               (b->anim_handler    && !b->anim_drift)      ||
               (b->meta_refresh_at && b->meta_refresh_url)
             )
3306
          {
3307 3308 3309 3310 3311 3312 3313
            toolbars_set_tristate_state(b, t, 0); /* Stop */
          }
          else
          {
            status_type type = toolbars_return_act_status(b);

            switch (type)
3314
            {
3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336
              default:
              case Toolbars_Status_NoType:
              case Toolbars_Status_Ready:
              case Toolbars_Status_Viewing:
              {
                toolbars_set_tristate_state(b, t, 1); /* Go To */
              }
              break;

              case Toolbars_Status_Formatting:
              case Toolbars_Status_Processing:
              case Toolbars_Status_GetPics:
              case Toolbars_Status_Connected:
              case Toolbars_Status_SentReq:
              case Toolbars_Status_Responded:
              case Toolbars_Status_Redirected:
              case Toolbars_Status_Connecting:
              case Toolbars_Status_Fetching:
              {
                toolbars_set_tristate_state(b, t, 2); /* Stop */
              }
              break;
3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373
            }
          }
        }
      }
      break;
    }
  }

  return NULL;
}

/*************************************************/
/* toolbars_set_all_button_states()              */
/*                                               */
/* Sets all the button states in the toolbars of */
/* all the browser windows currently open; that  */
/* is, it greys and ungreys them as appropriate  */
/* for that browser's current state.             */
/*************************************************/

_kernel_oserror * toolbars_set_all_button_states(void)
{
  _kernel_oserror * e;
  browser_data    * b;

  b = last_browser;

  while (b)
  {
    e = toolbars_set_button_states(b);
    if (e) return e;

    b = b->previous;
  }

  return NULL;
}