Handlers 92.9 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   : Handlers.c                             */
17
/*                                                 */
18 19
/* Purpose: Event handlers for driving the browser */
/*          front-end.                             */
20
/*                                                 */
21
/* Author : A.D.Hodgkinson                         */
22 23
/*                                                 */
/* History: 07-Feb-97: Created from Main.h.        */
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/***************************************************/

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

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

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

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

#include "toolbox.h"
#include "quit.h"
#include "proginfo.h"

#include "Dialler.h"

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

#include "Browser.h"
53
#include "ChoiceDefs.h"
54
#include "Choices.h"
55
#include "CSIM.h"
56
#include "Fetch.h" /* (For ISOBJECT macro) */
57
#include "FetchPage.h"
58
#include "Filetypes.h"
59 60 61 62
#include "FontManage.h"
#include "Forms.h"
#include "Frames.h"
#include "History.h"
63
#include "Hotlist.h"
64
#include "Images.h"
65
#include "JavaScript.h"
66
#include "Menus.h"
67
#include "Meta.h"
68 69
#include "MiscDefs.h"
#include "Mouse.h"
70
#include "PlugIn.h"
71
#include "Printing.h"
72
#include "Protocols.h"
73 74 75
#include "Redraw.h"
#include "Reformat.h"
#include "Save.h"
76
#include "SaveFile.h"
77
#include "SetPBoxes.h"
78 79 80 81 82 83 84 85 86
#include "TokenUtils.h"
#include "Toolbars.h"
#include "URLutils.h"
#include "Windows.h"

#include "Handlers.h"

/* Local statics */

87 88
static int  last_click_x;
static int  last_click_y;
89 90 91

/* Static function prototypes */

92 93 94
static void handlers_get_call_info   (browser_data ** bp, ObjectId * op, IdBlock * idb, ComponentId button);
static int  handlers_menu_or_toolbar (IdBlock * idb);
static void handle_go_to_with_key    (browser_data * b, char c, int clear);
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

/*************************************************/
/* handlers_menu_or_toolbar()                    */
/*                                               */
/* Returns 1 if an event came from a menu, else  */
/* 0 if it was a toolbar (only call this where   */
/* these are the only two possibilities!).       */
/*                                               */
/* Parameters: Pointer to the event ID block.    */
/*                                               */
/* Returns:    1 if the event came from a menu,  */
/*             else 0.                           */
/*************************************************/

static int handlers_menu_or_toolbar(IdBlock * idb)
{
  _kernel_oserror * e;
112
  ObjectId          tt, tb, sw;
113 114 115 116 117 118 119 120 121 122 123 124 125

  /* If we can't get toolbars on the ancestor, there are no */
  /* toolbars so the event certainly isn't from a toolbar.  */
  /* So if the call gives an error, return 1.               */

  e = window_get_tool_bars(InternalTopLeft | InternalBottomLeft,
                           idb->ancestor_id,
                           &tb,
                           &tt,
                           NULL,
                           NULL);
  if (e) return 1;

126
  if (controls.swap_bars) sw = tt, tt = tb, tb = sw;
127

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  /* If the object ID of the event generator matches either toolbar  */
  /* object ID, the event came from that toolbar; else, from a menu. */

  if (idb->self_id == tt || idb->self_id == tb) return 0;
  else                                          return 1;
}

/*************************************************/
/* handlers_get_call_info()                      */
/*                                               */
/* For button handlers, a standard set of calls  */
/* is used for each handler - find out the       */
/* browser_data struct and toolbar, debounce any */
/* keypress and if this had to be done slab the  */
/* button manually.                              */
/*                                               */
/* Parameters: Pointer to a pointer to a         */
/*             browser_data struct which will be */
/*             filled with a browser_data * for  */
/*             the event's underlying browser    */
/*             window;                           */
149
/*                                               */
150 151 152 153
/*             Pointer to an ObjectId into which */
/*             the object ID of the toolbar from */
/*             which the event came is placed;   */
/*             Pointer to the event's ID block;  */
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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
/*             ComponentId of the button.        */
/*                                               */
/* Assumes:    Any pointer may be NULL, but the  */
/*             caller should check that returned */
/*             values are sensible.              */
/*************************************************/

static void handlers_get_call_info(browser_data ** bp, ObjectId * op, IdBlock * idb, ComponentId button)
{
  browser_data * b;
  ObjectId       t;
  int            key;

  /* If the object has an ancestor, the event was from a toolbar, */
  /* else it was from the window itself (e.g. due to a keyboard   */
  /* shortcut being activated).                                   */

  _swix(OS_Byte, _INR(0,1) | _OUT(1), 121, 0, &key);

  if (toolbox_get_client_handle(0, idb->ancestor_id, (void *) &b))
  {
    ChkError(toolbox_get_client_handle(0, idb->self_id, (void *) &b));

    t = toolbars_get_upper(b);

    if (key != 255)
    {
      slab_gadget_in(t, button);
      debounce_keypress();
      slab_gadget_out(t, button);
    }
  }
  else
  {
    t = toolbars_get_upper(b);

    if (key != 255)
    {
      slab_gadget_in(t, button);
      debounce_keypress();
      slab_gadget_out(t, button);
    }
  }

  if (op) *op = t;
  if (bp) *bp = b;
}

/*************************************************/
/* handle_messages()                             */
/*                                               */
/* Deal with Wimp messages for reason codes 17   */
/* and 18 (type 19, acknowledges, are handled in */
208 209
/* handle_ack below). Parameters are as standard */
/* for a Wimp message handler.                   */
210 211 212
/*                                               */
/* This function farms off a lot of its          */
/* responsibilities to functions in Protocols.c. */
213 214 215 216
/*************************************************/

int handle_messages(WimpMessage * m, void * handle)
{
217 218 219 220 221 222 223 224 225 226 227 228 229 230
  #ifndef SINGLE_USER

    /* Multiuser builds will refuse to handle various message types */

    int complain = 0;

    #define ChkLogin if (!logged_in) {complain = 1; break;} else

  #else

    #define ChkLogin {}

  #endif

231 232
  switch (m->hdr.action_code)
  {
233
    case Wimp_MQuit: quit = 1;
234 235
    break;

236
    case Wimp_MMenusDeleted: menusrc = Menu_None;
237 238 239 240
    break;

    case Wimp_MHelpReply:
    {
241
      ChkError(protocols_ih_got_help_reply(m));
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
    }
    break;

    case Wimp_MModeChange:
    {
      browser_data * b;

      modechanged = 1;

      ChkError(image_mode_change());

      if (!printing) wimpt_read();

      read_os_to_points(); /* Handles the 'printing' flag internally */

      b = last_browser;

      while (b)
      {
        ChkError(fm_rescale_fonts(b));
        b = b->previous;
      }

      ChkError(windows_initialise_tool_sizes());
266
      ChkError(choices_mode_change());
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
    }
    break;

    case Wimp_MAppControl:
    {
      /* AppControl message - stop all activity */

      if (
           m->data.app_control.reason == Wimp_MAppControl_Stop &&
           m->hdr.sender != task_handle
         )
      {
        browser_data * b;
        IdBlock        idb;

        b = last_browser;

        while (b)
        {
          idb.ancestor_id = 0;
          idb.self_id     = b->self_id;

          handle_stop(0, NULL, &idb, NULL);

          b = b->previous;
        }
      }
    }
    break;

297
    /* App to app transfer (Message_DataSaveAck is in the multiple source section) */
298

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
    case Wimp_MDataLoadAck:             ChkLogin ChkError(protocols_atats_got_data_load_ack(m));  break;
    case Wimp_MRAMFetch:                ChkLogin ChkError(protocols_atats_got_ram_fetch(m));      break;

    case Wimp_MDataOpen:                         ChkError(protocols_atatl_got_data_open(m));      break;
    case Wimp_MDataLoad:
    {
      /* Allow certain browser systems to get a look at the message first */

      #ifndef SINGLE_USER

        if (setpboxes_check_message(m)) break;

      #endif

      ChkLogin ChkError(protocols_atatl_got_data_load(m));
    }
    break;
316

317 318
    case Wimp_MDataSave:                ChkLogin ChkError(protocols_atatl_got_data_save(m));      break;
    case Wimp_MRAMTransmit:             ChkLogin ChkError(protocols_atatl_got_ram_transmit(m));   break;
319 320

    /* Printing protocol (Message_DataSaveAck is in the multiple source section) */
321

322 323
    case Browser_Message_PrintError:    ChkLogin ChkError(protocols_pp_got_print_error(m));       break;
    case Browser_Message_PrintTypeOdd:  ChkLogin ChkError(protocols_pp_got_print_type_odd(m));    break;
324

325
    /* Multiple source messages */
326

327
    case Wimp_MDataSaveAck:             ChkLogin ChkError(protocols_multi_got_data_save_ack(m));  break;
328

329
    /* URI handler */
330

331 332 333
    case URI_MProcess:                           ChkError(protocols_auh_got_process(m));          break;
    case URI_MReturnResult:                      ChkError(protocols_auh_got_return_result(m));    break;
    case URI_MDying:                             ChkError(protocols_auh_got_dying(m));            break;
334 335 336

    /* ANT URL broadcast protocol */

337
    case Message_ANTOpenURL:            ChkLogin ChkError(protocols_aub_got_open_url(m));         break;
338 339 340

    /* Plug-In protocol */

341 342 343 344 345 346
    case Message_PlugIn_Opening:        ChkLogin ChkError(plugin_got_opening(m));                 break;
    case Message_PlugIn_URLAccess:      ChkLogin ChkError(plugin_got_url_access(m));              break;
    case Message_PlugIn_StreamNew:      ChkLogin ChkError(plugin_got_stream_new(m));              break;
    case Message_PlugIn_ReshapeRequest: ChkLogin ChkError(plugin_got_reshape_request(m));         break;
    case Message_PlugIn_Status:         ChkLogin ChkError(plugin_got_status(m));                  break;
    case Message_PlugIn_Busy:           ChkLogin ChkError(plugin_got_busy(m));                    break;
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367

// For debugging. This should only ever bounce, i.e. go through handle_ack.
//
//    case Message_PlugIn_Open:
//    {
//      MPlugIn_Open * open = (MPlugIn_Open *) &m->data;
//
//      Printf("PlugIn_Open filename: '%s'\n", plugin_return_string(m, &open->file_name));
//
//      {
//        char combuf[4096];
//
//        sprintf(combuf, "Copy %s ADFS::4.$.ParamsFile ~Q~V~C~NF", plugin_return_string(m, &open->file_name));
//
//        _swix(OS_CLI,
//              _IN(0),
//
//              combuf);
//      }
//    }
//    break;
368

369
    default: return 0; break;
370 371
  }

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
  #ifndef SINGLE_USER

    if (complain)
    {
      erb.errnum = Utils_Error_Custom_Message;

      StrNCpy0(erb.errmess,
               lookup_token("MustLogIn:The browser cannot fetch Web pages until you log in.",
                            0,
                            0));

      show_error_ret(&erb);
    }

  #endif

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
  return 1;
}

/*************************************************/
/* handle_ack()                                  */
/*                                               */
/* Handles UserMessage_Acknowledge from the      */
/* Wimp (message bouncing, etc.).                */
/*                                               */
/* Parameters are as standard for a Wimp event   */
/* handler.                                      */
/*************************************************/

int handle_ack(int eventcode, WimpPollBlock * block, IdBlock * idb, void * handle)
{
403 404
  WimpMessage * m = &block->user_message_acknowledge;

405
  switch (m->hdr.action_code)
406
  {
407
    case Browser_Message_PrintSave: ChkError(protocols_pp_print_save_bounced(m));      break;
408

409
    case Wimp_MHelpRequest:         ChkError(protocols_ih_help_request_bounced(m));    break;
410

411 412 413 414 415
    case Wimp_MDataLoad:            ChkError(protocols_atats_data_load_bounced(m));    break;
    case Wimp_MDataOpen:            ChkError(protocols_atats_data_open_bounced(m));    break;
    case Wimp_MRAMTransmit:         ChkError(protocols_atats_ram_transmit_bounced(m)); break;

    case Wimp_MRAMFetch:            ChkError(protocols_atatl_ram_fetch_bounced(m));    break;
416

417 418
    case Message_PlugIn_Open:       ChkError(plugin_open_bounced(m));                  break;
    case Message_PlugIn_StreamNew:  ChkError(plugin_stream_new_bounced(m));            break;
419

420
    default:
421
    {
422
      return 0;
423
    }
424
    break;
425 426
  }

427
  return 1;
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
}

/*************************************************/
/* handle_keys()                                 */
/*                                               */
/* Deal with keyboard pressed events from the    */
/* Wimp. Parameters are as standard for a Wimp   */
/* event handler.                                */
/*************************************************/

int handle_keys(int eventcode, WimpPollBlock * block, IdBlock * idb, void * handle)
{
  browser_data    * b        = NULL;
  browser_data    * ancestor = NULL;
  browser_data    * curframe = NULL;
  _kernel_oserror * e;
  int               key;

  /* Get the browser_data structure associated with either this object's */
  /* ancestor, if it has one, or this object directly, if not. If either */
  /* call fails this isn't a keypress from an ancestor object obtained   */
  /* from a browser window and it isn't from a browser window directly.  */

  if (idb->ancestor_id) e = toolbox_get_client_handle(0, idb->ancestor_id, (void *) &b);
  else                  e = toolbox_get_client_handle(0, idb->self_id,     (void *) &b);

  /* Some key presses may come from windows that have non-zero */
  /* client handles which aren't browser_data struct pointers, */
  /* e.g. a print dialogue with an animation in it will        */
  /* return an animation frame. So if b is non-zero but not a  */
  /* known browser_data struct pointer, set it to NULL so that */
  /* later routines can quickly know there is no valid pointer */
  /* available.                                                */

  if (b && !is_known_browser(b)) b = NULL;

  if (b)
  {
466
    ancestor = utils_ancestor(b);
467
    curframe = ancestor->selected_frame;
468

469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 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
    if (!curframe) curframe = b;
  }

  key = ((WimpKeyPressedEvent *) block)->key_code;

  /* Is this from a URL bar? To find out, get the toolbar ID of */
  /* this object, if possible.                                  */

  if (!e && idb->ancestor_id)
  {
    ObjectId i;

    i = toolbars_get_upper(b);

    if (!e && i == idb->self_id)
    {
      /* Make sure we have a client handle for the underlying browser */
      /* window before attempting to proceed                          */

      if (b)
      {
        switch (key)
        {
          /* Scrolling the page. Remember, keypresses trapped here */
          /* are from a toolbar object (probably the URL writable) */
          /* so certain key presses - such as Copy / End to go to  */
          /* the bottom of the page - should *not* be trapped, as  */
          /* they have other meanings (e.g. in the above example,  */
          /* delete character to the right).                       */

          case akbd_UpK:
          case akbd_DownK:
          case akbd_PageUpK:
          case akbd_PageDownK:
          case akbd_HomeK:
          case akbd_UpK + akbd_Ctl:
          case akbd_UpK + akbd_Ctl + akbd_Sh:
          case akbd_DownK + akbd_Ctl:
          case akbd_DownK + akbd_Ctl + akbd_Sh:
          {
            if (!browser_scroll_page_by_key(curframe, key, NULL))
            {
              key = 0;
              _swix(OS_Byte, _INR(0,1), 21, 0); /* Flush keyboard buffer */
            }
          }
          break;

          case akbd_LeftK:
          case akbd_LeftK + akbd_Sh:
          case akbd_LeftK + akbd_Ctl:
          case akbd_LeftK + akbd_Sh + akbd_Ctl:
          {
            /* For left, only scroll when the caret is at the start of the */
            /* string in the URL writable.                                 */

            if (((WimpKeyPressedEvent *) block)->caret.index == 0)
            {
              if (!browser_scroll_page_by_key(curframe, key, NULL))
              {
                key = 0;
                _swix(OS_Byte, _INR(0,1), 21, 0); /* Flush keyboard buffer */
              }
            }
          }
          break;

          case akbd_RightK:
          case akbd_RightK + akbd_Sh:
          case akbd_RightK + akbd_Ctl:
          case akbd_RightK + akbd_Sh + akbd_Ctl:
          {
            /* For right, only scroll when the caret is at the end of the */
            /* string in the URL writable.                                */

544
            char writable[Limits_URLBarWrit];
545 546
            int  len;

547
            *writable = 0;
548
            writablefield_get_value(0, idb->self_id, URLBarWrit, writable, sizeof(writable), &len);
549
            writable[sizeof(writable) - 1] = 0; /* (Ensure termination) */
550 551 552 553 554 555 556 557 558 559 560 561 562 563

            if (((WimpKeyPressedEvent *) block)->caret.index >= len)
            {
              if (!browser_scroll_page_by_key(curframe, key, NULL))
              {
                key = 0;
                _swix(OS_Byte, _INR(0,1), 21, 0); /* Flush keyboard buffer */
              }
            }
          }
          break;

          case akbd_TabK:
          {
564
            if (choices.keyboard_ctrl)
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
            {
              wimp_set_caret_position(ancestor->window_handle, -1, 0, 0, -1, -1);

              /* If there's no selected token, select one */

              if (!ancestor->selected || !ancestor->selected_owner)
              {
                /* Ensure *both* values are NULL (sanity check) */

                ancestor->selected       = NULL;
                ancestor->selected_owner = NULL;

                /* Select a token */

                browser_move_selection(curframe, akbd_DownK);
              }
              else
              {
                /* If there's a selected token, does it belong to this */
                /* window?                                             */

                if (ancestor->selected_owner == curframe)
                {
                  WimpGetWindowStateBlock s;

                  /* If it belongs to this window but it's not visible, */
                  /* find a new token that is.                          */

                  s.window_handle = curframe->window_handle;

                  if (!wimp_get_window_state(&s) && !browser_check_visible(b, &s, ancestor->selected))
                  {
                    browser_clear_selection(curframe, 0);
                    browser_move_selection(curframe, akbd_DownK);
                  }
                }
//                else
//                {
//                  /* The selected token doesn't belong to this window, */
//                  /* so select a new one here.                         */
//
//                  browser_move_selection(b, akbd_DownK);
//                }
              }
            }

// This is unfinished - it only works if the form has already
// had an input focus at least once, and won't scroll the page
// if the focus should drop off it.

            else form_give_focus(curframe);

            key = 0;
          }
          break;

621 622
          case akbd_TabK + akbd_Ctl:
          {
623
            char url[Limits_URLBarWrit];
624
            int  changed = 0;
625 626 627 628 629 630 631 632 633 634

            /* Read whatever is in the URL bar writable */

            *url = 0;
            ChkError(writablefield_get_value(0, i, URLBarWrit, url, sizeof(url), NULL));
            url[sizeof(url) - 1] = 0; /* (Ensure termination) */

            /* Try and find something appropriate in the hotlist, */
            /* then the history.                                  */

635 636 637 638
            #ifndef REMOTE_HOTLIST
              changed = hotlist_find_match(url, sizeof(url));
            #endif

639 640 641 642 643 644 645 646 647 648
            if (!changed) changed = history_find_match(url, sizeof(url));

            if (changed)
            {
              /* Update the URL writable */

              writablefield_set_value(0, i, URLBarWrit, url);
            }

            key = 0;
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
          }
          break;

          case akbd_TabK + akbd_Sh:
          {
            char url[Limits_URLBarWrit];
            int  changed;

            /* Read whatever is in the URL bar writable */

            *url = 0;
            ChkError(writablefield_get_value(0, i, URLBarWrit, url, sizeof(url), NULL));
            url[sizeof(url) - 1] = 0; /* (Ensure termination) */

            /* Cycle the protocol specifier (adding one in if not already present) */

            changed = urlutils_cycle_protocol(url, sizeof(url));

            if (changed)
            {
              /* Update the URL writable */

              writablefield_set_value(0, i, URLBarWrit, url);
            }

            key = 0;
          }
          break;

678 679
          case 0x00d:
          {
680
            char url[Limits_URLBarWrit];
681 682 683

            /* Read the new URL from the URL bar writable */

684
            *url = 0;
685
            ChkError(writablefield_get_value(0, i, URLBarWrit, url, sizeof(url), NULL));
686
            url[sizeof(url) - 1] = 0; /* (Ensure termination) */
687 688 689 690 691

            #ifdef ALIAS_URLS
            // Not implemented yet...
            #endif

692 693
            /* If we're displaying a temporary file and the URL hasn't changed, */
            /* treat this as an attempt to reload the page.                     */
694

695 696 697 698 699 700
            if (
                 b->displayed == Display_Scrap_File   &&
                 !strcmp(browser_current_url(b), url)
               )
            {
              erb.errnum = Utils_Error_Custom_Message;
701

702 703 704 705
              StrNCpy0(erb.errmess,
                       lookup_token("CantReload:This page cannot be reloaded, as it was sent directly from another application.",
                                    0,
                                    0));
706

707 708 709 710 711
              show_error_ret(&erb);
            }
            else
            {
              #ifdef HIDE_CGI
712

713 714 715 716 717
                /* If HIDE_CGI is defined, the URL bar may have only part of    */
                /* the URL in it - the CGI information could be stripped off.   */
                /* In that case, check if the URL matches the browser's current */
                /* one with the exception of the CGI stuff, and only do the     */
                /* fetch if not.                                                */
718

719 720 721 722 723
                if (
                     browser_current_url(b) &&                               /* If there *is* a current URL, and            */
                     !strncmp(browser_current_url(b), url, strlen(url)) &&   /* the URL bar matches the start of it...      */
                     strlen(browser_current_url(b)) > strlen(url) &&         /* ...but there's more of the current URL left */
                     browser_current_url(b)[strlen(url)] == '?'              /* ...and the first extra character is a '?',  */
724

725 726
                   )
                   ChkError(fetchpage_new(b, browser_current_url(b), 1, 1)); /* ...then fetch the current URL instead.      */
727

728
                else ChkError(fetchpage_new(b, url, 1, 1)); /* Otherwise do what the user asked! */
729

730 731 732 733 734 735 736 737
              #else

                /* Start the new fetch */

                ChkError(fetchpage_new(b, url, 1, 1));

              #endif
            }
738

739
            key = 0;
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
          }
          break;
        }
      }
    }
  }

  if (key) wimp_process_key(key);

  return 1;
}

/*************************************************/
/* handle_keys_from_browser()                    */
/*                                               */
/* Called when a Wimp key pressed event is       */
/* generated for a specific browser window.      */
/* Parameters are as standard for a Wimp event   */
/* handler.                                      */
/*************************************************/

int handle_keys_from_browser(int eventcode, WimpPollBlock * block, IdBlock * idb, browser_data * handle)
{
  int            key = ((WimpKeyPressedEvent *) block)->key_code;
  browser_data * ancestor;
  browser_data * curframe;

  if (!handle) return 0;

769
  ancestor = utils_ancestor(handle);
770
  curframe = ancestor->selected_frame;
771

772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
  if (!curframe) curframe = handle;

  form_process_key(handle, &key);

  /* If 'key' is non-zero, the forms library couldn't handle it */

  switch (key)
  {
    /* Scrolling the page. Here, unlike the code above, the */
    /* keypress is straight off the browser page, so unless */
    /* the forms library has trapped the key, things such   */
    /* as left/right and copy/end can be used to move the   */
    /* page around as well as up/down etc.                  */

    case akbd_PageUpK:
    case akbd_PageDownK:

    case akbd_HomeK:
    case akbd_CopyK: /* Or 'End' */

    case akbd_UpK:
    case akbd_UpK    + akbd_Ctl:
    case akbd_UpK    + akbd_Ctl + akbd_Sh:

    case akbd_DownK:
    case akbd_DownK  + akbd_Ctl:
    case akbd_DownK  + akbd_Ctl + akbd_Sh:

    case akbd_LeftK:
    case akbd_LeftK  + akbd_Sh:
    case akbd_LeftK  + akbd_Ctl:
    case akbd_LeftK  + akbd_Ctl + akbd_Sh:

    case akbd_RightK:
    case akbd_RightK + akbd_Sh:
    case akbd_RightK + akbd_Ctl:
    case akbd_RightK + akbd_Ctl + akbd_Sh:
    {
      int limit;

      if (browser_move_selection(curframe, key))
      {
        key = 0;
        _swix(OS_Byte, _INR(0,1), 21, 0); /* Flush keyboard buffer */

        /* Rehighlight the frame if required */

819
        if (controls.keep_highlight) frames_highlight_frame(curframe);
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
      }
      else
      {
        if (!browser_scroll_page_by_key(curframe, key, &limit))
        {
          /* If limit is set, the window is scrolled as far as it */
          /* will go - can we jump to another frame, then? (Only  */
          /* jumping out for certain key presses, though).        */

          if (
               limit &&
               (
                 key == akbd_UpK ||
                 key == akbd_DownK
               )
             )
          {
            browser_data * next = frames_find_another_frame(curframe, key == akbd_UpK ? 1 : 0);

            if (next)
            {
              /* Another frame has been identified */

              browser_clear_selection(curframe, 0);

              /* Scroll the frame appropriately */

              browser_scroll_page_v(next,
                                    NULL,
                                    key == akbd_UpK ? 0 : 1,
                                    0,
                                    0,
                                    0x1000000,
                                    NULL);

              /* Make sure the ancestor(s) are updated */

              ancestor->selected_frame = NULL;

859
              ancestor = utils_ancestor(next);
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875

              ancestor->selected_frame = curframe = next;

              /* Find a selectable */

              browser_move_selection(curframe, key);

              /* Highlight the frame */

              frames_highlight_frame(curframe);
            }
          }
          else
          {
            /* Scrolled page - rehighlight the frame if required */

876
            if (controls.keep_highlight) frames_highlight_frame(curframe);
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
          }
        }

        key = 0;
        _swix(OS_Byte, _INR(0,1), 21, 0);
      }
    }
    break;

    case akbd_TabK: if (!browser_give_general_focus(curframe)) key = 0;
    break;

    case 0x00d:
    {
      browser_data * owner;

      owner = ancestor->selected_owner;

      if (ancestor->selected && browser_check_visible(owner, NULL, ancestor->selected))
      {
        /* If an item is selected and at least partially visible, */
        /* act as if it were clicked upon with Select             */

900 901 902
        // Optimise this? Lose the check for IMG, and rely on
        // HTMLLib returning valid information?

903
        if (
904 905 906 907 908 909 910 911 912 913 914
             (
               (
                 (ancestor->selected->style & IMG)       &&
                 (ancestor->selected->type & TYPE_ISMAP)
               )
               ||
               (
                 ancestor->selected->type & TYPE_ISCLIENTMAP
               )
             )
             && !ancestor->in_image_map
915 916 917 918 919 920
           )
        {
          /* For image maps, if not already selected, start keyboard */
          /* navigation of the map.                                  */

          BBox      box;
921
          HStream * map  = ancestor->selected;
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981

          if (!image_get_token_image_size(owner, map, &box))
          {
            WimpGetWindowStateBlock s;
            int                     x, y;

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

            if (!image_get_token_image_position(owner, map, &x, &y))
            {
              x = coords_x_toscreen(x, (WimpRedrawWindowBlock *) &s);
              y = coords_y_toscreen(y, (WimpRedrawWindowBlock *) &s);

              box.xmin += x + 2;
              box.ymin += y + 2;
              box.xmax += x - 4;
              box.ymax += y - 4;

              // Re: Next line of code.
              //
              // Why? Can't easily force rectangle constraint, as
              // whenever the pointer changes shape it is unconstrained.
              // So the navigation function has to handle being called
              // with the pointer moved off the map (and it does); so
              // this isn't wanted, really...
              //
              // Still, left in just in case it's needed for something!
              //
              // /* Constrain the pointer to the image map rectangle */
              //
              // mouse_rectangle(&box, 1);

              /* Move x and y to the middle of the image map */

              x += (box.xmax - box.xmin) / 2, y += (box.ymax - box.ymin) / 2;

              /* Move pointer, and update internal details of what pointer is over */

              mouse_to(x, y, 0);

              owner->pointer_over = ancestor->pointer_over = map;
              browser_pointer_check(0, NULL,NULL, owner);
              mouse_set_pointer_shape(Mouse_Shape_Map);
              mouse_watch_pointer_control(0);
              mouse_pointer_on();

              owner->in_image_map = ancestor->in_image_map = 1;

              return 1;
            }
          }
        }
        else
        {
          /* Not an image map, or a selected map - so follow the link. */

          owner->pointer_over = NULL;
          browser_pointer_check(0, NULL,NULL, owner);

982
          if (choices.keyboard_ctrl)
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
          {
            mouse_pointer_off();
            mouse_watch_pointer_control(1);
          }

          handle_link_clicks(-1, NULL, NULL, owner);
        }
      }
    }
    break;

    default:
    {
      /* In merged status bar situations, want alphanumeric characters */
      /* to pop up the URL writable with that key in it.               */

      if (isalnum(key)) /* Wimp_ProcessKey numbers are equal to ASCII codes for alphanumerics */
      {
1001
        handle_go_to_with_key(handle, (char) key, controls.clear_first);
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
        key = 0;
      }
    }
  }

  if (key) ChkError(wimp_process_key(key));

  return 1;
}

/*************************************************/
/* handle_menus()                                */
/*                                               */
/* Deal with menu selection events from the Wimp */
/* (for forms etc.). Parameters are as standard  */
/* for a Wimp event handler.                     */
/*************************************************/

int handle_menus(int eventcode, WimpPollBlock * block, IdBlock * idb, void * handle)
{
  switch (menusrc)
  {

1025 1026 1027 1028
    case Menu_Form:
    {
      form_select_menu_event(block);
    }
1029 1030
    break;

1031 1032 1033 1034 1035
    case Menu_LocalHist: /* Same as for global, so no break */
    case Menu_GlobalHist:
    {
      ChkError(history_menu_selection((browser_data *) menuhdl, block));
    }
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 1085 1086 1087 1088
    break;

    default: return 0;
  }

  return 1;
}

/*************************************************/
/* handle_scroll_requests()                      */
/*                                               */
/* Deal with Scroll Request events from the Wimp */
/* (e.g. for page up/down). Parameters are as    */
/* standard for a Wimp event handler.            */
/*************************************************/

int handle_scroll_requests(int eventcode, WimpPollBlock * b, IdBlock * idb, browser_data * handle)
{
  if (b->scroll_request.yscroll)
  {
    ChkError(browser_scroll_page_v(handle,
                                   &b->scroll_request.open,
                                   b->scroll_request.yscroll > 0,
                                   b->scroll_request.yscroll == 2 || b->scroll_request.yscroll == -2,
                                   b->scroll_request.yscroll == 1 || b->scroll_request.yscroll == -1,
                                   0,
                                   NULL));
  }

  if (b->scroll_request.xscroll)
  {
    ChkError(browser_scroll_page_h(handle,
                                   &b->scroll_request.open,
                                   b->scroll_request.xscroll < 0,
                                   b->scroll_request.xscroll == 2 || b->scroll_request.xscroll == -2,
                                   b->scroll_request.xscroll == 1 || b->scroll_request.xscroll == -1,
                                   0,
                                   NULL));
  }

  return 1;
}

/*************************************************/
/* handle_clicks()                               */
/*                                               */
/* Deal with mouse click events from the wimp,   */
/* for specific object IDs. Parameters are as    */
/* standard for a Wimp event handler.            */
/*************************************************/

int handle_clicks(int eventcode, WimpPollBlock * b, IdBlock * idb, browser_data * handle)
{
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
  int shift;

  /* Is Shift being pressed? */

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

        121,
        128,

        &shift);

1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
  /* Process the event only if the browser_data structure contents */
  /* match the ancestor ID of the item clicked upon - i.e. if a    */
  /* toolbar has been clicked upon.                                */

  if (idb->ancestor_id == handle->self_id)
  {
    /* If the toolbox hasn't filled in the component ID, e.g. because */
    /* icon flags were forced to change to give a button type the     */
    /* Toolbox didn't expect, fill it in now.                         */

    if (idb->self_component == -1)
    {
      if (
           window_wimp_to_toolbox(0,
                                  b->mouse_click.window_handle,
                                  b->mouse_click.icon_handle,
                                  &idb->self_id,
                                  &idb->self_component)
         )
         return 0;
    }

    switch (idb->self_component)
    {
1125 1126
      case StatusBarStatusCover:  /* Clicking on a covering gadget is equivalent  */
      case StatusBarStatus:       /* to clicking on the underlying gadget instead */
1127 1128 1129 1130 1131 1132 1133 1134
      {
        /* If the URL writable and status display are merged, want to */
        /* now swap the display for the writable and put the caret in */
        /* the field.                                                 */

        if (handle->merged_url)
        {
          toolbars_merged_to_url(handle, idb->self_id);
1135
          gadget_set_focus(0, idb->self_id, URLBarWrit);
1136 1137 1138 1139
        }
      }
      break;

1140 1141
      case URLBarHistoryMenuL: /* Drop through to URLBarHistoryMenuR case */
      case URLBarHistoryMenuR:
1142
      {
1143 1144 1145 1146
        ChkError(history_menu_popup(handle,
                                    idb->self_id,
                                    idb->self_component,
                                    shift,
1147
                                    b->mouse_click.buttons & Wimp_MouseButtonAdjust ? !choices.show_urls : choices.show_urls));
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
      }
      break;
    }

    /* Grey / ungrey buttons, as the state may change as */
    /* a result of being selected.                       */

    toolbars_set_button_states(handle);

    return 1;
  }

  return 0;
}

/*************************************************/
/* handle_link_clicks()                          */
/*                                               */
/* Deal with mouse click events from the wimp,   */
/* on gadgets in the browser window. Parameters  */
/* are as standard for a Wimp event handler.     */
/*************************************************/

int handle_link_clicks(int eventcode, WimpPollBlock * b, IdBlock * idb, browser_data * handle)
{
  HStream                 * p = NULL;
  int                       ox, oy, adj, used = 0;
1175
  int                       shift, ctrl;
1176
  WimpGetPointerInfoBlock   i;
1177
  browser_data            * ancestor = utils_ancestor(handle);
1178 1179 1180 1181
  browser_data            * owner;

  owner = ancestor->selected_owner;

1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
  /* Is Shift held down? (Do this very early to minimise the */
  /* changes of missing the keypress).                       */

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

        121,
        128,

        &shift);

  /* Similarly, for Control */

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

        121,
        129,

        &ctrl);

1203 1204 1205
  /* Remember where the click was so other functions can find this  */
  /* without calling Wimp_GetPointerInfo, by which time the pointer */
  /* may have moved a bit.                                          */
1206 1207 1208 1209

  last_click_x = b->mouse_click.mouse_x;
  last_click_y = b->mouse_click.mouse_y;

1210 1211 1212 1213
  /* There are circumstances under which pointer watching, to see if */
  /* the mouse pointer should be turned off, is disabled but should  */
  /* be enabled at this stage. If this is so, turn it back on.       */

1214
  if (choices.keyboard_ctrl && ancestor->selected)
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
  {
    owner->pointer_over = NULL;
    browser_pointer_check(0, NULL, NULL, owner);
    mouse_watch_pointer_control(1);
  }

  /* If this is entered with an event code of -1, this signals that */
  /* the keyboard handler is calling the function. In that case,    */
  /* the routine will treat the item in handle->selected as if it   */
  /* were clicked on with Select.                                   */
  /*                                                                */
  /* Otherwise, the token that was clicked on is discovered         */
  /* from the pointer position, and the mouse button used from the  */
  /* actual mouse button state.                                     */

  if (eventcode >= 0 && b->mouse_click.buttons & Wimp_MouseButtonMenu) return 0;

1232
  /* Use the 'adjust' function as this may return special information if running Full Screen. */
1233

1234
  adj = (controls.ignore_adjust || eventcode < 0) ? 0 : adjust();
1235

1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
  /* This may be a drag, not a click; also, if the browser has */
  /* children, this must be a frame border resize event.       */

  if (
       b->mouse_click.buttons == 16 || /* (Adjust-drag) */
       b->mouse_click.buttons == 64    /* (Select-drag) */
     )
  {
    /* For now, we have no defined action for drags */

    return 1;
  }

  /* For dragging borders, don't do this on the drag event (i.e. keep */
  /* this code in a position to execute after the check for a drag).  */
  /* Otherwise, when the routine calls Wimp_GetPointerInfo, it'll get */
  /* the original starting coordinates just after the drag begins.    */
  /* This makes the frames 'twitch' briefly back to their starting    */
  /* position.                                                        */

  if (handle->nchildren)
  {
    drag_in_progress = 1;
    handle->dragging = 1;

    register_null_claimant(Wimp_ENull, (WimpEventHandler *) handle_drags, handle);

    /* Call it now to minimise the time during which the pointer */
    /* could wander when it's meant to be constrained            */

    handle_drags(0, NULL, NULL, handle);

    return 1;
  }

1271 1272
  /* Get the token that was clicked upon, if any. */

1273 1274 1275 1276
  ChkError(wimp_get_pointer_info(&i));

  if (eventcode >= 0) p = browser_get_pointer_token(handle, &i, &ox, &oy);
  else                p = ancestor->selected, handle = owner;
1277 1278 1279 1280 1281 1282 1283 1284

  if (p)
  {
    /* First - forms. */

    if (
         (p->style & FORM) &&
         (
1285 1286 1287
           (p->tagno == TAG_INPUT)    ||
           (p->tagno == TAG_TEXTAREA) ||
           (p->tagno == TAG_SELECT)
1288 1289 1290
         )
       )
    {
Kevin Bracey's avatar
Kevin Bracey committed
1291 1292
      int x = 0, y = 0;

1293 1294
      /* Get the offset into an IMAGE button type */

1295
      if (p->tagno == TAG_INPUT && HtmlINPUTtype(p) == inputtype_IMAGE && eventcode >= 0)
1296 1297 1298
      {
        ChkError(image_return_click_offset(handle, p, &i, &x, &y));
      }
Kevin Bracey's avatar
Kevin Bracey committed
1299 1300

      ChkError(form_click_field(handle, p, 0, x, y));
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
      used = 1;
    }
    else
    {
      /* If the token is an anchor, flash it briefly. This isn't done */
      /* for images unless they're turned off and the image has ALT   */
      /* text, which would show the highlight.                        */

      if (
           p->anchor &&
           (
             !(p->style & IMG) ||
             (
               (p->style & IMG)                            &&
               p->text                                     &&
               handle->displayed != Display_External_Image &&
               !image_token_fetched(handle, p)
             )
           )
         )
1321
         browser_flash_token(handle, p);
1322

1323
      /* If shift and control are not held down, and we have a link, follow the link */
1324

1325
      if (!shift && !ctrl)
1326
      {
1327
        if (p->anchor || (p->type & TYPE_ISCLIENTMAP)) /* An anchor or client-side map */
1328
        {
1329 1330
          int ignore = 0;

1331 1332
          /* First, do we have JavaScript code to deal with? */

1333
          if (p->onclick && *p->onclick) ChkError(javascript_href_onclick(handle, p, &ignore));
1334

1335 1336
          /* If ignore is zero, we're supposed to deal with the HREF attribute */
          /* on the link - otherwise, ignore it.                               */
1337

1338
          if (!ignore)
1339
          {
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
            /* Client-side image maps */

            if (p->type & TYPE_ISCLIENTMAP)
            {
              char * url;
              char * target;
              char * alt;

              /* Find out which pixel we clicked on */

              ChkError(image_return_click_offset(handle, p, &i, &ox, &oy));

              csim_return_info(handle,
                               p,
                               ox,
                               oy,
                               &url,
                               &target,
                               &alt);

              if (url && *url)
              {
                ChkError(fetchpage_fetch_targetted(handle,
                                                   url,
                                                   target,
                                                   NULL,
                                                   adj));

                used = 1;
              }
            }

            /* Server-side image maps */
1373

1374 1375 1376 1377 1378
            if (
                 !used            &&
                 (p->style & IMG) &&
                 (p->type & TYPE_ISMAP)
               )
1379
            {
1380
              char coords[64];
1381

1382 1383 1384 1385 1386 1387
              if (eventcode < 0)
              {
                ChkError(wimp_get_pointer_info(&i));
                p = browser_get_pointer_token(handle, &i, NULL, NULL);
                if (!p) return 0;
              }
1388

Kevin Bracey's avatar
Kevin Bracey committed
1389
              /* Find out which pixel we clicked on */
1390 1391

              ChkError(image_return_click_offset(handle, p, &i, &ox, &oy));
1392

1393 1394
              if (ox >= 0 && oy >= 0)
              {
Kevin Bracey's avatar
Kevin Bracey committed
1395
                /* Build an appropriate CGI string including this information. */
1396

1397
                sprintf(coords, "?%d,%d", ox, oy);
1398

1399 1400 1401 1402 1403
                ChkError(fetchpage_fetch_targetted(handle,
                                                   p->anchor,
                                                   p->target,
                                                   coords,
                                                   adj));
1404 1405
              }

1406 1407
              used = 1;
            }
1408

1409
            /* Otherwise, a simple link */
1410

1411
            if (!used)
1412
            {
1413
              /* Note that running full screen will cause non-targetted links */
1414

1415 1416 1417 1418 1419
              ChkError(fetchpage_fetch_targetted(handle,
                                                 p->anchor,
                                                 p->target,
                                                 NULL,
                                                 adj));
1420 1421 1422 1423 1424 1425 1426 1427 1428

              used = 1;
            }
          }
          else
          {
            /* The JavaScript routines said that the HREF contents of the link */
            /* should be ignored; so just flag that we've dealt with this, but */
            /* do nothing else.                                                */
1429 1430 1431 1432 1433 1434 1435

            used = 1;
          }
        }
      }
      else
      {
1436
        /* If Shift is held down, open the object into a save dialogue. */
1437 1438 1439
        /* Always do this through a new window, if the Choices say so;  */
        /* this will be a 'small fetch' window rather than a full,      */
        /* large browser.                                               */
1440 1441

        if (shift)
1442
        {
1443 1444
          if (p->anchor)
          {
1445
            if (!adj && !controls.use_small)
1446
            {
1447 1448 1449 1450 1451 1452 1453 1454 1455
              if (!handle->save_oldstore && handle->source)
              {
                /* If there is store and its size hasn't already been remembered, */
                /* keep a record of the old source store size before the new file */
                /* save clears the data away.                                     */

                handle->save_oldstore = flex_size((flex_ptr) &handle->source);
              }

1456
              /* Set the save_link flag and start the fetch */
1457

1458
              handle->save_link = 1;
1459

1460 1461 1462 1463
              /* If control is held down, set the reloading flag to bypass the cache */

              if (ctrl) handle->reloading = 1;

1464 1465
              ChkError(fetchpage_new(handle,
                                     p->anchor,
1466
                                     0,
1467 1468
                                     1));
            }
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
            else
            {
              /* Open the link in a new window */

              ChkError(windows_create_browser(p->anchor,
                                              NULL,
                                              NULL,
                                              NULL,
                                              Windows_CreateBrowser_SaveToFile));

              /* Again, if control is held down, set the reloading flag to bypass the cache */

              if (ctrl) last_browser->reloading = 1;
            }
1483

1484 1485
            used = 1;
          }
1486
        }
1487
        else
1488
        {
1489 1490 1491 1492
          /* For Control, if it's an image, show it. This overrides */
          /* any 'don't show images' flags and will load the image  */
          /* if it isn't already fetched.                           */

1493
          if ((p->style & IMG) || (ISOBJECT(p)))
1494 1495 1496 1497
          {
            image_reload(handle, p); /* (Slightly misleading function name) */
            used = 1;
          }
1498 1499 1500 1501 1502 1503 1504 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
        }
      }
    }
  }

  /* For mouse clicks, if nothing has flagged that the click was used */
  /* in some way, place the input focus generally into the ancestor   */
  /* window and mark the frame as selected.                           */

  if (eventcode >= 0 && !used)
  {
    wimp_set_caret_position(ancestor->window_handle, -1, 0, 0, -1, -1);

    ancestor->selected_frame = handle;
    frames_highlight_frame(handle);

    /* If there's an object selected in another frame, must move */
    /* the selection to this one - otherwise, keyboard movement  */
    /* would jump back to the other one.                         */

    if (ancestor->selected && ancestor->selected_owner != handle)
    {
      if (ancestor->selected_owner) browser_clear_selection(ancestor->selected_owner, 0);

      ancestor->selected_owner = NULL; /* Make sure these */
      ancestor->selected       = NULL; /* are cleared...  */

      browser_move_selection(handle, akbd_DownK);
    }
  }

  return 1;
}

1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 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 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 1815 1816 1817 1818 1819
/*************************************************/
/* handle_drags()                                */
/*                                               */
/* A null event handler to handle dragging on    */
/* the page. Parameters are as standard for a    */
/* Wimp event handler, though only the last      */
/* parameter, a pointer to the browser_data      */
/* struct for which the drag applies, is used.   */
/*************************************************/

int handle_drags(int eventcode, WimpPollBlock * b, IdBlock * idb, browser_data * handle)
{
  static browser_data     * browser_resizing = NULL;
  static int                resizing_row     = -1;
  static int                resizing_col     = -1;
  static int                resizing_ofsleft = 0;
  static int                resizing_ofstop  = 0;

  WimpGetPointerInfoBlock   info;
  WimpGetWindowStateBlock   state;
  int                       wx, wy;

  /* If handle is NULL, this was called for a Wimp_EUserDrag */
  /* event from the Wimp. In that case, cancel the global    */
  /* drag handling flag and allow various event handlers     */
  /* elsewhere to now progress normally but wait until the   */
  /* null event handler calls this function to deregister    */
  /* any browser-specific stuff.                             */

  if (!handle)
  {
    drag_in_progress = 0;
    return 0;
  }

  /* Get the pointer position and browser window state */

  if (wimp_get_pointer_info(&info)) return 0;

  state.window_handle = handle->window_handle;
  if (wimp_get_window_state(&state)) return 0;

  if (!info.button_state)
  {
    /* No button being pressed - the drag has ended */

    if (handle->dragging)
    {
      drag_in_progress = 0;
      handle->dragging = 0;
      deregister_null_claimant(Wimp_ENull, (WimpEventHandler *) handle_drags, handle);

      if (browser_resizing)
      {
        /* If we were resizing a frameset, lock the new positions in place */

        frames_lock_frameset(browser_resizing);

        /* Reset the various state variables ready for the next drag */

        browser_resizing = NULL;
        resizing_row     = -1;
        resizing_col     = -1;
      }

      return 0;
    }
  }

  /* Work out where the pointer is relative to the window work area */

  wx = coords_x_toworkarea(info.x, (WimpRedrawWindowBlock *) &state);
  wy = coords_y_toworkarea(info.y, (WimpRedrawWindowBlock *) &state);

  /* The mouse button is still held down. So what are we doing? */

  if (handle->nchildren)
  {
    /* If the frame has children, then the pointer must be over a blank bit  */
    /* - i.e. a border between frames. In that case, this is a frame resize. */

    browser_data              * child;
    WimpGetWindowOutlineBlock   co;
    int                         delta_x = 0, delta_y = 0;
    int                         num_rows, num_cols;
    int                         i, c;
    BBox                        span;

    num_rows = handle->frameset->rows;
    num_cols = handle->frameset->cols;

    if (!num_rows) num_rows = 1;
    if (!num_cols) num_cols = 1;

    /* A parent frame has got the click, so it must be in the border between */
    /* other frames in its defined frameset.                                 */

    if (!browser_resizing)
    {
      _kernel_oserror * e;

      e = frames_find_pointer_in_frameset(handle,
                                          last_click_x,
                                          last_click_y,
                                          &resizing_row,
                                          &resizing_col,
                                          &resizing_ofsleft,
                                          &resizing_ofstop,
                                          1);

      if (e || (resizing_row < 0 && resizing_col < 0))
      {
        browser_resizing = NULL;
        resizing_row     = -1;
        resizing_col     = -1;

        return 0;
      }

      browser_resizing = handle;

    /* Closure of 'if (!browser_resizing)' */
    }

    /* Whether or not we had to work out the initial drag */
    /* conditions above, deal with moving the frames now. */

    c = (resizing_row > 0 ? resizing_row : 0) * num_cols + (resizing_col > 0 ? resizing_col : 0);
    if (c >= handle->nchildren) return 0;

    child            = handle->children[c];
    co.window_handle = child->window_handle;

    if (wimp_get_window_outline(&co)) return 0;

    if (resizing_row > 0) delta_y = (info.y - co.outline.ymax) - resizing_ofstop;
    if (resizing_col > 0) delta_x = resizing_ofsleft - (co.outline.xmin - info.x);

    /* Deal with resizing rows first. */

    span.xmin = span.ymin = 0x1000000; /* Very big to start with */
    span.xmax = span.ymax = -1;        /* Small to start with    */

    if (delta_y)
    {
      /* Resize the row above the pointer */

      for (i = 0; i < num_cols; i++)
      {
        /* What child number are we on? */

        c = i + (resizing_row - 1) * num_cols;

        if (c < handle->nchildren)
        {
          /* Get this child frame's outline coordinates */

          child            = handle->children[c];
          co.window_handle = child->window_handle;

          if (!wimp_get_window_outline(&co))
          {
            /* Alter its visible area appropriately */

            co.outline.ymin += delta_y;

            /* Keep a record of the region over which */
            /* things have moved for redraw purposes  */

            if (co.outline.xmin < span.xmin) span.xmin = co.outline.xmin;
            if (co.outline.ymin < span.ymin) span.ymin = co.outline.ymin;
            if (co.outline.xmax > span.xmax) span.xmax = co.outline.xmax;
            if (co.outline.ymax > span.ymax) span.ymax = co.outline.ymax;

            /* Resize any child frames */

            ChkError(frames_resize_frame(child, &co.outline));
          }
        }
      }

      /* Same again for the row below */

      for (i = 0; i < num_cols; i++)
      {
        c = i + resizing_row * num_cols;

        if (c < handle->nchildren)
        {
          child            = handle->children[c];
          co.window_handle = child->window_handle;

          if (!wimp_get_window_outline(&co))
          {
            co.outline.ymax += delta_y;

            if (co.outline.xmin < span.xmin) span.xmin = co.outline.xmin;
            if (co.outline.ymin < span.ymin) span.ymin = co.outline.ymin;
            if (co.outline.xmax > span.xmax) span.xmax = co.outline.xmax;
            if (co.outline.ymax > span.ymax) span.ymax = co.outline.ymax;

            ChkError(frames_resize_frame(child, &co.outline));
          }
        }
      }

    /* Closure of 'if (delta_y)' */
    }

    /* Now do columns; first, to the left of the pointer */

    if (delta_x)
    {
      for (i = 0; i < num_rows; i++)
      {
        c = i * num_cols + resizing_col - 1;

        if (c < handle->nchildren)
        {
          child            = handle->children[c];
          co.window_handle = child->window_handle;

          if (!wimp_get_window_outline(&co))
          {
            co.outline.xmax += delta_x;

            if (co.outline.xmin < span.xmin) span.xmin = co.outline.xmin;
            if (co.outline.ymin < span.ymin) span.ymin = co.outline.ymin;
            if (co.outline.xmax > span.xmax) span.xmax = co.outline.xmax;
            if (co.outline.ymax > span.ymax) span.ymax = co.outline.ymax;

            ChkError(frames_resize_frame(child, &co.outline));
          }
        }
      }

      /* Next, columns to the right of the pointer */

      for (i = 0; i < num_rows; i++)
      {
        c = i * num_cols + resizing_col;

        if (c < handle->nchildren)
        {
          child            = handle->children[c];
          co.window_handle = child->window_handle;

          if (!wimp_get_window_outline(&co))
          {
            co.outline.xmin += delta_x;

            if (co.outline.xmin < span.xmin) span.xmin = co.outline.xmin;
            if (co.outline.ymin < span.ymin) span.ymin = co.outline.ymin;
            if (co.outline.xmax > span.xmax) span.xmax = co.outline.xmax;
            if (co.outline.ymax > span.ymax) span.ymax = co.outline.ymax;

            ChkError(frames_resize_frame(child, &co.outline));
          }
        }
      }

    /* Closure of 'if (delta_x)' */
    }

    /* Now redraw the parent to ensure borders are correctly plotted */

    if (
         span.xmin >= 0 &&
         span.ymin >= 0 &&
         span.xmax >= 0 &&
         span.ymax >= 0
       )
    {
      coords_box_toworkarea(&span, (WimpRedrawWindowBlock *) &state);

      ChkError(wimp_force_redraw(state.window_handle,
                                 span.xmin,
                                 span.ymin,
                                 span.xmax,
                                 span.ymax));
    }

  /* Closure of 'if (handle->nchildren)' */
  }

  return 0;
}

1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846
/*************************************************/
/* handle_close_browser()                        */
/*                                               */
/* Close a browser window, and frames within it. */
/* Parameters are as standard for a Wimp event   */
/* handler.                                      */
/*************************************************/

int handle_close_browser(int eventcode, WimpPollBlock * b, IdBlock * idb, browser_data * handle)
{
  frames_collapse_set(handle);
  windows_close_browser(handle);

  return 1;
}

/*************************************************/
/* handle_home()                                 */
/*                                               */
/* Handles clicks on the Home button. Parameters */
/* are as standard for a Toolbox event handler.  */
/*************************************************/

int handle_home(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  int            from_menu = 0;
  browser_data * b;
1847
  char           home[Limits_URL];
1848

1849
  handlers_get_call_info(&b, NULL, idb, ButtonBarHome);
1850 1851 1852 1853
  from_menu = handlers_menu_or_toolbar(idb);

  urlutils_create_home_url(home, sizeof(home));

1854
  if (from_menu || controls.ignore_adjust || !adjust()) ChkError(fetchpage_new(b, home, 1, 1));
1855 1856 1857 1858 1859 1860 1861 1862 1863 1864
  else
  {
    ChkError(windows_create_browser(home,
                                    NULL,
                                    NULL,
                                    NULL,
                                    Windows_CreateBrowser_Normal));

    ChkError(browser_inherit(b, last_browser));
  }
1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885

  /* Grey / ungrey buttons, as the state may change as */
  /* a result of being selected.                       */

  ChkError(toolbars_set_button_states(b));

  return 1;
}

/*************************************************/
/* handle_back()                                 */
/*                                               */
/* Handles clicks on the Back button. Parameters */
/* are as standard for a Toolbox event handler.  */
/*************************************************/

int handle_back(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  int            from_menu = 0;
  browser_data * b;

1886
  handlers_get_call_info(&b, NULL, idb, ButtonBarBack);
1887 1888
  from_menu = handlers_menu_or_toolbar(idb);

1889
  ChkError(history_fetch_backwards(b, (from_menu || controls.ignore_adjust) ? 0 : adjust()));
1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911

  /* Grey / ungrey buttons, as the state may change as */
  /* a result of being selected.                       */

  ChkError(toolbars_set_button_states(b));

  return 1;
}

/*************************************************/
/* handle_forward()                              */
/*                                               */
/* Handles clicks on the Forward button.         */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

int handle_forwards(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  int            from_menu = 0;
  browser_data * b;

1912
  handlers_get_call_info(&b, NULL, idb, ButtonBarForward);
1913 1914
  from_menu = handlers_menu_or_toolbar(idb);

1915
  ChkError(history_fetch_forwards(b, (from_menu || controls.ignore_adjust) ? 0 : adjust()));
1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934

  /* Grey / ungrey buttons, as the state may change as */
  /* a result of being selected.                       */

  ChkError(toolbars_set_button_states(b));

  return 1;
}

/*************************************************/
/* handle_stop()                                 */
/*                                               */
/* Handles clicks on the Stop button. Parameters */
/* are as standard for a Toolbox event handler.  */
/*************************************************/

int handle_stop(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  browser_data * b;
1935

1936
  #ifdef SINGLE_USER
1937

1938 1939
    int shift;

1940
    /* Is Shift being pressed? */
1941

1942 1943
    _swix(OS_Byte,
          _INR(0,1) | _OUT(1),
1944

1945 1946 1947 1948 1949 1950
          121,
          128,

          &shift);

  #endif
1951 1952

  /* Find out where we were called from */
1953

1954
  handlers_get_call_info(&b, NULL, idb, ButtonBarStop);
1955 1956 1957

  /* If using the tristate TriState_Go_GoTo_Stop, */
  /* then stop kills everything first time. Else, */
1958 1959
  /* it stops everything except image fetches,    */
  /* then stops image fetches too.                */
1960 1961 1962

  if (b->tristate != TriState_Go_GoTo_Stop)
  {
1963 1964
    if (frames_fetching(b)) frames_abort_fetching(utils_ancestor(b), 0, 1);
    else frames_abort_fetching(utils_ancestor(b), 1, 1);
1965
  }
1966
  else frames_abort_fetching(utils_ancestor(b), 1, 1);
1967

1968 1969 1970 1971
  /* Clear the flag saying this is a History based fetch */

  b->from_history = 0;

1972 1973 1974 1975
  /* Cancel local client pull */

  meta_cancel_refresh(b);

1976 1977 1978 1979 1980
  /* Grey / ungrey buttons, as the state may change as */
  /* a result of being selected.                       */

  ChkError(toolbars_set_button_states(b));

1981 1982 1983 1984
  /* Ensure everything is up to date in the toolbars */

  toolbars_update_progress(b);

1985 1986 1987 1988 1989
  #ifdef SINGLE_USER

    /* Broadcast an AppControl message to stop any further     */
    /* activity in WebServe - this will eventually be directed */
    /* straight at WebServe rather than broadcast...           */
1990

1991 1992
//  if (controls.stop_proxy && !b->ancestor) ChkError(utils_stop_proxy());

1993 1994 1995 1996 1997 1998 1999 2000 2001 2002
    if (
         (
           controls.stop_proxy &&
           !shift
         )
         ||
         (
           !controls.stop_proxy &&
           shift
         )
2003
       )
2004 2005 2006
       ChkError(utils_stop_proxy());

  #endif
2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026

  return 1;
}

/*************************************************/
/* handle_reload()                               */
/*                                               */
/* Handles clicks on the Reload button.          */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

int handle_reload(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  _kernel_oserror * e;
  browser_data    * b;
  ObjectId          t;
  int               new_view;
  int               from_menu = 0;

2027
  handlers_get_call_info(&b, &t, idb, ButtonBarReload);
2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044

  /* If the browser is displaying a temporary file, can't reload it */

  if (b->displayed == Display_Scrap_File)
  {
    erb.errnum = Utils_Error_Custom_Message;

    StrNCpy0(erb.errmess,
             lookup_token("CantReload:This page cannot be reloaded, as it was sent directly from another application.",
                          0,
                          0));

    show_error_ret(&erb);

    return 0;
  }

2045
  from_menu = handlers_menu_or_toolbar(idb);
2046
  new_view  = (from_menu || controls.ignore_adjust) ? 0 : adjust();
2047 2048 2049 2050 2051

  /* If there's a displayed URL already, reload it */

  if (b->urlddata)
  {
2052 2053
    /* If not going to a new view, set the reloading flag */
    /* and get the URL.                                   */
2054 2055 2056

    if (!new_view)
    {
2057 2058 2059
      b->reloading   = 1;
      b->reload_lock = 1;

2060
      e = fetchpage_new(b, b->urlddata, 0, 1);
2061

2062
      if (e) show_error_ret(e);
2063 2064 2065 2066
    }

    /* Otherwise, just fetch the URL in a new window. */

2067 2068 2069 2070 2071 2072 2073 2074 2075 2076
    else
    {
      ChkError(windows_create_browser(b->urlddata,
                                      NULL,
                                      NULL,
                                      NULL,
                                      Windows_CreateBrowser_Normal));

      ChkError(browser_inherit(b, last_browser));
    }
2077 2078 2079 2080 2081 2082
  }
  else
  {
    /* Otherwise, get a URL string from the URL bar and do */
    /* a fresh load of that.                               */

2083
    char url[Limits_URLBarWrit];
2084

2085
    *url = 0;
2086
    writablefield_get_value(0, t, URLBarWrit, url, sizeof(url), NULL);
2087
    url[sizeof(url) - 1] = 0; /* (Ensure termination) */
2088 2089 2090

    if (*url)
    {
2091
      if (!new_view) ChkError(fetchpage_new(b, url, 1, 1));
2092 2093 2094 2095 2096 2097 2098 2099 2100 2101
      else
      {
        ChkError(windows_create_browser(url,
                                        NULL,
                                        NULL,
                                        NULL,
                                        Windows_CreateBrowser_Normal));

        ChkError(browser_inherit(b, last_browser));
      }
2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122
    }
  }

  /* Grey / ungrey buttons, as the state may change as */
  /* a result of being selected.                       */

  ChkError(toolbars_set_button_states(b));

  return 1;
}

/*************************************************/
/* handle_view_hotlist()                         */
/*                                               */
/* Handles clicks on the View Hotlist button.    */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

int handle_view_hotlist(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
2123
  #ifndef REMOTE_HOTLIST
2124

2125 2126
    int from_menu = handlers_menu_or_toolbar(idb);
    int adjust    = 0;
2127

2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146
  #else

    browser_data * b;
    char           path[Limits_URL];
    int            from_menu = 0;

  #endif

  #ifndef SINGLE_USER

    /* In multiuser builds, if not logged in, claim the event */
    /* but don't do anything with it.                         */

    if (!logged_in) return 1;

  #endif

  #ifndef REMOTE_HOTLIST

2147 2148 2149
    /* If not from a menu, allow Adjust to toggle the display type */
    /* at open time to the opposite of that specified by the       */
    /* HotlistShow option in the Choices file.                     */
2150

2151 2152 2153
    if (!from_menu)
    {
      WimpGetPointerInfoBlock info;
2154

2155
      ChkError(wimp_get_pointer_info(&info));
2156

2157 2158
      if (info.button_state & Wimp_MouseButtonAdjust) adjust = 1;
    }
2159

2160 2161
    /* Open the hotlist */

2162 2163 2164 2165 2166 2167 2168 2169
    ChkError(hotlist_open(Toolbox_ShowObject_Centre,
                          NULL,

                          adjust
                          ?
                          choices.hotlist_show == Choices_HotlistType_Descriptions
                          :
                          choices.hotlist_show == Choices_HotlistType_URLs));
2170 2171 2172

  #else

2173
    handlers_get_call_info(&b, NULL, idb, ButtonBarViewHotlist);
2174 2175 2176
    from_menu = handlers_menu_or_toolbar(idb);

    /* Create the hotlist URL */
2177

2178
    urlutils_create_hotlist_url(path, sizeof(path));
2179

2180
    /* Deal with appending the current URL, if necessary */
2181

2182 2183
  // For future implementation:
  //
2184
  //  if (controls.append_urls)
2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196
  //  {
  //    char url[Limits_URL];
  //
  //    if (browser_current_url(b))
  //    {
  //      StrNCpy0(url, browser_current_url(b));
  //      history_record_local(b, url);
  //    }
  //
  //    history_pull_local_last(b, url, sizeof(url));
  //
  // Then do the rest on url, instead of browser_current_url.
2197

2198
    if (controls.append_urls && browser_current_url(b))
2199
    {
2200
      int len;
2201

2202
      lookup_control("AppendWith:?url=",0,0);
2203

2204 2205 2206 2207 2208 2209
      /* Need to translate some chars, so working out the length of the final */
      /* string is a little complex.                                          */

      len = strlen(path) + strlen(tokens);

      if (len + 1 < sizeof(path))
2210
      {
2211 2212 2213 2214 2215
        char * p = browser_current_url(b);

        strcat(path, tokens);

        while (*p && len + 4 < sizeof(path)) /* +4 = +1 for terminator, +3 for maximum step size within loop */
2216
        {
2217 2218 2219 2220 2221 2222 2223 2224
          if (isalnum(*p)) path[len] = *p, len++;
          else
          {
            sprintf(path + len, "%%%02X", *p);
            len += 3;
          }

          p++;
2225 2226
        }

2227 2228
        if (len >= sizeof(path)) len = sizeof(path) - 1;
        path[len] = 0;
2229 2230 2231
      }
    }

2232 2233
    /* Finally, fetch the required hotlist */

2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244
    if (adjust() && !from_menu && !controls.ignore_adjust)
    {
      ChkError(windows_create_browser(path,
                                      NULL,
                                      NULL,
                                      NULL,
                                      Windows_CreateBrowser_Normal));

      ChkError(browser_inherit(b, last_browser));
    }
    else ChkError(fetchpage_new(b, path, 1, 1));
2245

2246
  #endif
2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260

  return 1;
}

/*************************************************/
/* handle_add_hotlist()                          */
/*                                               */
/* Handles clicks on the Add To Hotlist button.  */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

int handle_add_hotlist(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
2261 2262 2263 2264 2265 2266
  #ifndef REMOTE_HOTLIST

    browser_data * b;
    int            from_menu = 0;
    int            adjust    = 0;

2267
    handlers_get_call_info(&b, NULL, idb, ButtonBarAddToHotlist);
2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296
    from_menu = handlers_menu_or_toolbar(idb);

    /* If not from a menu, allow Adjust to add to the other end of the */
    /* hotlist compared to that specified in the AddHotlist Choices    */
    /* file item.                                                      */

    if (!from_menu)
    {
      WimpGetPointerInfoBlock info;

      ChkError(wimp_get_pointer_info(&info));

      if (info.button_state & Wimp_MouseButtonAdjust) adjust = 1;
    }

    /* Can't proceed if we don't have a URL */

    if (b && browser_current_url(b) && *browser_current_url)
    {
      char title[Limits_Title];

      ChkError(window_get_title(0,
                                b->self_id,
                                title,
                                sizeof(title),
                                NULL));

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

2297 2298 2299 2300 2301 2302 2303 2304
      ChkError(hotlist_add(title,
                           browser_current_url(b),

                           adjust
                           ?
                           choices.add_hotlist == Choices_AddHotlist_Top
                           :
                           choices.add_hotlist == Choices_AddHotlist_Bottom));
2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318
    }

  #else

    #ifdef TRACE

      erb.errnum = Utils_Error_Custom_Normal;
      strcpy(erb.errmess, "There is no defined action for Add To Hotlist in REMOTE_HOTLIST builds");
      show_error_ret(&erb);

    #endif

  #endif

2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344
  return 0;
}

/*************************************************/
/* handle_view_resources()                       */
/*                                               */
/* Handles clicks on the View Resources button.  */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

int handle_view_resources(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  return 0;
}

/*************************************************/
/* handle_load_images()                          */
/*                                               */
/* Handles clicks on the Load Images button.     */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

int handle_load_images(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
2345 2346 2347
  browser_data * b;
  ObjectId       t;

2348
  handlers_get_call_info(&b, &t, idb, ButtonBarLoadImages);
2349

2350
  ChkError(browser_set_look(b, 0, -1, -1, 1, 1));
2351 2352

  return 1;
2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364
}

/*************************************************/
/* handle_view_source()                          */
/*                                               */
/* Handles clicks on the View Source button.     */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

int handle_view_source(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
2365 2366 2367 2368 2369 2370
  char           path[Limits_URL]; /* Generated from a URL, but can't be any longer than one */
  browser_data * b;
  ObjectId       t;
  FILE         * file;
  int            len, bytes;

2371
  handlers_get_call_info(&b, &t, idb, ButtonBarViewSource);
2372

2373 2374
  if (!is_known_browser(b) || !b->source) return 1;

2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423
  /* If the item is loaded from a file (not a scrap file, */
  /* or image, obviously), then load from there. Else, go */
  /* via. Scrap.                                          */

  if (
       browser_current_url(b)                                           &&
       b->displayed == Display_Fetched_Page                             &&
       !strncmp(browser_current_url(b), FileMethod, strlen(FileMethod))
     )
  {
    StrNCpy0(path, browser_current_url(b));

    urlutils_url_to_pathname(path, sizeof(path));
  }
  else
  {
    /* Save the source to a scrap file */

    file = fopen(Save_ScrapFile, "wb");

    if (!file) goto handle_view_source_exit;

    len   = flex_size((flex_ptr) &b->source);
    bytes = fwrite(b->source,
                   1,
                   len,
                   file);

    fclose(file);

    if (len < bytes) goto handle_view_source_exit;

    /* Set the filetype */

    ChkError(_swix(OS_File,
                   _INR(0,2),

                   18,
                   Save_ScrapFile,
                   FileType_HTML));

    /* Record that we've used a scrap file */

    StrNCpy0(path, Save_ScrapFile);
  }

  /* Broadcast a Message_DataOpen for the file, claiming the */
  /* filetype is text (which is what the Filer does...).     */

2424
  ChkError(protocols_atats_send_data_open(FileType_TEXT, path));
2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440

  return 1;

  /* Exit for errors whilst writing the scrap file */

handle_view_source_exit:

  erb.errnum = Utils_Error_Custom_Message;

  StrNCpy0(erb.errmess,
           lookup_token("CannotShow:Can't show the source this way (problem with the scrap file).",
                        0,0));

  show_error_ret(&erb);

  return 1;
2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455
}

/*************************************************/
/* handle_go_to()                                */
/*                                               */
/* Handles clicks on the Go To button.           */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

int handle_go_to(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  browser_data * b;
  ObjectId       t;

2456
  handlers_get_call_info(&b, &t, idb, ButtonBarGoTo);
2457 2458 2459 2460 2461 2462 2463

  /* Ensure the URL bar is up to date, if the URL writable and */
  /* status displays are merged then change to the URL display */
  /* and finally give the window the input focus.              */

  toolbars_update_url(b);
  if (b->merged_url) toolbars_merged_to_url(b, t);
2464
  if (t) gadget_set_focus(0, t, URLBarWrit);
2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495

  /* Update the buttons and exit */

  ChkError(toolbars_set_button_states(b));

  return 1;
}

/*************************************************/
/* handle_go_to_with_key()                       */
/*                                               */
/* Handles going to a 'go to' state (merged      */
/* toolbar showing the URL writable instead of   */
/* the status display) with a given character    */
/* entered into that writable.                   */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the toolbar;          */
/*                                               */
/*             The character to append to the    */
/*             writable field's contents;        */
/*                                               */
/*             1 to in fact clear the writable   */
/*             before putting the character in,  */
/*             else 0 to append to existing      */
/*             contents.                         */
/*************************************************/

static void handle_go_to_with_key(browser_data * b, char c, int clear)
{
  ObjectId t;
2496
  char     url[Limits_URLBarWrit];
2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515
  char     cat[2];

  t = toolbars_get_upper(b);

  /* Ensure the URL bar is up to date, if the URL writable and */
  /* status displays are merged then change to the URL display */
  /* and finally give the window the input focus.              */

  toolbars_update_url(b);

  /* (Append the given character) */

  if (clear)
  {
    url[0] = c;
    url[1] = 0;
  }
  else
  {
2516
    *url = 0;
2517
    writablefield_get_value(0, t, URLBarWrit, url, sizeof(url), NULL);
2518 2519 2520
    url[sizeof(url) - 1] = 0;

    if (strlen(url) < sizeof(url))
2521 2522 2523 2524 2525 2526 2527
    {
      cat[0] = c;
      cat[1] = 0;
      strcat(url, cat);
    }
  }

2528
  writablefield_set_value(0, t, URLBarWrit, url);
2529 2530 2531 2532

  /* (Show the writable / give the input focus) */

  if (b->merged_url) toolbars_merged_to_url(b, t);
2533
  if (t) gadget_set_focus(0, t, URLBarWrit);
2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554

  /* Update the buttons and exit. */

  ChkError(toolbars_set_button_states(b));

  return;
}

/*************************************************/
/* handle_go()                                   */
/*                                               */
/* Handles clicks on the Go button. Parameters   */
/* are as standard for a Toolbox event handler.  */
/*************************************************/

int handle_go(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  browser_data        * b;
  WimpKeyPressedEvent   keys;
  IdBlock               id;

2555
  handlers_get_call_info(&b, NULL, idb, ButtonBarGo);
2556 2557 2558 2559 2560 2561 2562 2563 2564 2565

  // Nasty... But then robust, as quite a lot goes on in the key handler
  // and duplicating the code would lead to problems if it gets out of
  // sync.

  /* Fake a keyboard pressed event from the URL bar for the key handler */

  keys.key_code     = 0x00d;
  id.ancestor_id    = b->self_id;
  id.self_id        = toolbars_get_upper(b);
2566
  id.self_component = URLBarWrit;
2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582

  return handle_keys(eventcode, (WimpPollBlock *) &keys, &id, NULL);
}

/*************************************************/
/* handle_cancel()                               */
/*                                               */
/* Handles clicks on the Cancel button.          */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

int handle_cancel(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  browser_data * b;

2583
  handlers_get_call_info(&b, NULL, idb, ButtonBarCancel);
2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604

  /* If the URL writable and status display are merged, switch to */
  /* status display. Update the URL bar with the original text,   */
  /* cancelling any changes made by the user, and set the caret   */
  /* position by the general method (so focus may go to a form,   */
  /* the URL writable, or the page generally).                    */

  if (b->merged_url) toolbars_merged_to_status(b, toolbars_get_upper(b));
  toolbars_update_url(b);
  browser_give_general_focus(b);

  /* Update the buttons and exit */

  ChkError(toolbars_set_button_states(b));

  return 1;
}

/*************************************************/
/* handle_bistate()                              */
/*                                               */
2605 2606 2607 2608
/* Handles the EButtonBarBistate event, which    */
/* says that the bistate button should be        */
/* activated.                                    */
/*                                               */
2609 2610 2611 2612 2613 2614 2615 2616
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

int handle_bistate(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  browser_data * b;

2617
  handlers_get_call_info(&b, NULL, idb, ButtonBarBistate);
2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636

  /* Deal with each button type individually */

  switch (b->bistate)
  {
    case BiState_Cancel_Back:
    {
      if (!b->bistate_state) return handle_cancel(eventcode, event, idb, handle);
      else                   return handle_back(eventcode, event, idb, handle);
    }
    break;
  }

  return 0;
}

/*************************************************/
/* handle_tristate()                             */
/*                                               */
2637 2638 2639 2640
/* Handles the EButtonBarTristate event, which   */
/* says that the tristate button should be       */
/* activated.                                    */
/*                                               */
2641 2642 2643 2644 2645 2646 2647 2648
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

int handle_tristate(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  browser_data * b;

2649
  handlers_get_call_info(&b, NULL, idb, ButtonBarTristate);
2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672

  /* Deal with each button type individually */

  switch (b->bistate)
  {
    case TriState_Go_GoTo_Stop:
    {
      switch (b->tristate_state)
      {
        case 0: return handle_go(eventcode, event, idb, handle);
        break;
        case 1: return handle_go_to(eventcode, event, idb, handle);
        break;
        case 2: return handle_stop(eventcode, event, idb, handle);
        break;
      }
    }
    break;
  }

  return 0;
}

2673
/*************************************************/
2674
/* handle_save_source()                          */
2675 2676 2677 2678 2679
/*                                               */
/* Handles clicks on the Save button. Parameters */
/* are as standard for a Toolbox event handler.  */
/*************************************************/

2680
int handle_save_source(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
2681 2682 2683
{
  browser_data * b;

2684
  handlers_get_call_info(&b, NULL, idb, ButtonBarSaveSource);
2685

2686
  if (is_known_browser(b) && b->source) ChkError(savefile_open_for(b, save_as_html));
2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702

  return 1;
}

/*************************************************/
/* handle_print()                                */
/*                                               */
/* Handles clicks on the Print button in the     */
/* toolbar of browser windows. Parameters are as */
/* standard for a Toolbox event handler.          */
/*************************************************/

int handle_print(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  browser_data * b;

2703
  handlers_get_call_info(&b, NULL, idb, ButtonBarPrint);
2704

2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743
  if (is_known_browser(b) && b->cell && b->cell->nlines) ChkError(print_open_for(b, idb->self_id));

  return 1;
}

/*************************************************/
/* handle_save_as_text()                         */
/*                                               */
/* Handles clicks on the Save As Text button.    */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

int handle_save_as_text(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  browser_data * b;

  handlers_get_call_info(&b, NULL, idb, ButtonBarSaveAsText);

  if (is_known_browser(b) && b->cell && b->cell->nlines) ChkError(savefile_open_for(b, save_as_text));

  return 1;
}

/*************************************************/
/* handle_save_as_draw()                         */
/*                                               */
/* Handles clicks on the Save As Draw button.    */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

int handle_save_as_draw(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  browser_data * b;

  handlers_get_call_info(&b, NULL, idb, ButtonBarSaveAsDraw);

  if (is_known_browser(b) && b->cell && b->cell->nlines) ChkError(savefile_open_for(b, save_as_draw));
2744 2745 2746 2747

  return 1;
}

2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770
/*************************************************/
/* handle_clear_url()                            */
/*                                               */
/* Clears the URL writable (like Ctrl+U),        */
/* assuming the input focus is in the relevant   */
/* place...                                      */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

int handle_clear_url(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  WimpGetCaretPositionBlock   caret;
  browser_data              * b;
  ObjectId                    t;
  char                        url[2];

  ChkError(wimp_get_caret_position(&caret));

  if (caret.icon_handle < 0 || caret.height < 0 || caret.index < 0) return 0;

  ChkError(toolbox_get_client_handle(0, idb->ancestor_id, (void *) &b));
2771

2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786
  if (!b) return 0;

  t = toolbars_get_upper(b);

  /* Ensure the URL bar is up to date, if the URL writable and */
  /* status displays are merged then change to the URL display */
  /* and finally give the window the input focus.              */

  toolbars_update_url(b);

  /* (Append the given character) */

  url[0] = 0;
  url[1] = 0;

2787
  writablefield_set_value(0, t, URLBarWrit, url);
2788 2789 2790 2791

  /* (Show the writable / give the input focus) */

  if (b->merged_url) toolbars_merged_to_url(b, t);
2792
  if (t) gadget_set_focus(0, t, URLBarWrit);
2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804

  /* Update the buttons and exit. */

  ChkError(toolbars_set_button_states(b));

  return 1;
}

/*************************************************/
/* handle_show_history_menu()                    */
/*                                               */
/* Opens the history menu near the menu popup    */
2805 2806 2807
/* gadget (acts as if that were clicked upon).   */
/* If the gadget doesn't exist, the opening      */
/* position is undefined.                        */
2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

int handle_show_history_menu(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  browser_data * b;
  ObjectId       t;
  BBox           test;
  int            which;
2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831
  int            shift;

  /* Is Shift being pressed? */

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

        121,
        128,

        &shift);

  /* Which browser is this for? */
2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842

  ChkError(toolbox_get_client_handle(0,
                                     idb->ancestor_id ? idb->ancestor_id : idb->self_id,
                                     (void *) &b));
  if (!b) return 0;

  t = toolbars_get_upper(b);

  /* Is the popup present (either the 'to the left' or */
  /* the 'to the right' version)?                      */

2843 2844 2845
  if      (!gadget_get_bbox(0, t, URLBarHistoryMenuR,   &test)) which = 1;
  else if (!gadget_get_bbox(0, t, URLBarHistoryMenuL,   &test)) which = 2;
  else                                                          which = 0;
2846 2847 2848 2849 2850

  if (which)
  {
    /* Show the menu */

2851 2852
    ChkError(history_menu_popup(b,
                                t,
2853
                                which == 1 ? URLBarHistoryMenuR : URLBarHistoryMenuL,
2854 2855
                                shift,
                                choices.show_urls));
2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869
    return 1;
  }

  /* Gadget isn't present, so can't open the menu */

  return 0;
}

/*************************************************/
/* handle_show_info()                            */
/*                                               */
/* Put version number in program info window;    */
/* called on a ProgInfo_AboutToBeShown event.    */
/* Parameters are as standard for a Toolbox      */
2870
/* event handler.                                */
2871 2872 2873 2874
/*************************************************/

int handle_show_info(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893
  #ifndef SINGLE_USER

    /* Make sure that the tokens buffer is invalidated, */
    /* or we could end up appending multiple '(M)'s to  */
    /* the string if lookup_token thinks it has no      */
    /* reason to look up the token again.               */

    *lasttokn = 0;

  #endif

  lookup_token("Version:(Unknown!)",1,0);

  #ifndef SINGLE_USER

    strcat(tokens, " (M)");

  #endif

2894 2895
  ChkError(proginfo_set_version(0,
                                idb->self_id,
2896 2897
                                tokens));

2898 2899 2900 2901 2902 2903 2904 2905
  return 1;
}

/*************************************************/
/* handle_quit()                                 */
/*                                               */
/* Deal with a Quit_Quit event from any source;  */
/* parameters are as standard for a Toolbox      */
2906
/* event handler.                                */
2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952
/*************************************************/

int handle_quit(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  browser_data * b;

  /* Ensure the pointer is visible */

  mouse_pointer_on();

  /* Close all open windows, thereby freeing memory and terminating */
  /* any open connections. This may take some time, so put up an    */
  /* hourglass.                                                     */

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

  while (last_browser)
  {
    b = last_browser;

    while (b && b->ancestor) b = b->previous;

    if (b && !b->ancestor) handle_close_browser(0, NULL, NULL, b);
  }

  _swix(Hourglass_Off, 0);

  /* Signal that the application should exit through setting 'quit'. */

  quit = 1;

  return 1;
}

/*************************************************/
/* handle_lose_caret()                           */
/*                                               */
/* Call to drag the input focus back to the      */
/* window in case it is lost.                    */
/*                                               */
/* Parameters are as standard for a Wimp event   */
/* handler.                                      */
/*************************************************/

int handle_lose_caret(int eventcode, WimpPollBlock * block, IdBlock * idb, void * handle)
{
2953
  if (controls.keep_caret)
2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000
  {
    WimpGetPointerInfoBlock i;
    int                     handle;

    /* Only grab the caret if this task owns the window */
    /* the pointer is currently over.                   */

    ChkError(wimp_get_pointer_info(&i));

    handle = task_from_window(i.window_handle);

    if (handle == task_handle)
    {
      WimpGetCaretPositionBlock caret;

      /* The pointer is over a browser-owned window, but if the */
      /* caret is also in a browser-owned window, don't want to */
      /* move it right now.                                     */

      if (wimp_get_caret_position(&caret)) return 0;

      handle = task_from_window(caret.window_handle);

      /* If we don't own the window the caret has moved to, */
      /* grab it back into the browser window.              */

      if (handle != task_handle) browser_give_general_focus(last_browser);

      return 1;
    }
  }

  return 0;
}

/*************************************************/
/* handle_dialler_display()                      */
/*                                               */
/* A null event handler to update the dialler    */
/* status display field, if present.             */
/*                                               */
/* Parameters are as standard for a Wimp event   */
/* handler.                                      */
/*************************************************/

int handle_dialler_display(int eventcode, WimpPollBlock * b, IdBlock * idb, browser_data * handle)
{
3001
  if (handle->url_bar) ChkError(toolbars_update_dialler_time(handle));
3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030

  return 0;
}

/*************************************************/
/* handle_dialler_service()                      */
/*                                               */
/* Handles Message_Service messages from the     */
/* TaskModule module, which may be (for example) */
/* watching out for 'status changed' service     */
/* calls from the Dialler.                       */
/*                                               */
/* Parameters are as standard for a Wimp message */
/* handler.                                      */
/*************************************************/

int handle_dialler_service(WimpMessage * m, void * handle)
{
  browser_data     * b = (browser_data *) handle;
  _kernel_swi_regs * r;

  if (!is_known_browser(b)) return 0;

  if (m->hdr.action_code != Message_Service) return 0;

  r = (_kernel_swi_regs *) &m->data;

  if (r->r[1] == Service_DiallerStatus)
  {
3031 3032
    if (b->url_bar) ChkError(toolbars_update_dialler_status(b));

3033 3034 3035 3036 3037
    return 1;
  }

  return 0;
}
3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069

/*************************************************/
/* handle_miscellaneous_event()                  */
/*                                               */
/* Handle any other user event not already       */
/* covered by other functions, passing it on if  */
/* we don't know what to do with it.             */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

int handle_miscellaneous_event(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  if (eventcode >= EHelpGenericBase && eventcode <= EHelpGenericTop)
  {
    browser_data * b;

    if (toolbox_get_client_handle(0, idb->ancestor_id, (void *) &b))
    {
      ChkError(toolbox_get_client_handle(0, idb->self_id, (void *) &b));
    }

    if (!is_known_browser(b)) b = NULL;

    menus_help_miscellaneous(b, eventcode - EHelpGenericBase);

    return 1;
  }

  return 0;
}