Menus 40.8 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   : Menus.c                                */
17 18 19
/*                                                 */
/* Purpose: Handle browser menus.                  */
/*                                                 */
20
/* Author : A.D.Hodgkinson                         */
21
/*                                                 */
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/* History: 20-Nov-96: Created                     */
/***************************************************/

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

#include "swis.h"

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

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

#include "svcprint.h"
#include "Global.h"
#include "FromROSLib.h"
41
#include "MiscDefs.h"
42 43
#include "Utils.h"

44
#include "Browser.h"
45
#include "Choices.h"
46
#include "Fetch.h"    /* (For ISLINK macro) */
47
#include "FetchPage.h"
48
#include "Frames.h"
49
#include "Handlers.h"
50
#include "History.h"
51
#include "Hotlist.h"
52
#include "Images.h"
53
#include "ImgHistory.h"
54
#include "Multiuser.h"
55
#include "Object.h"
56
#include "OpenURL.h"
57
#include "Reformat.h"
58
#include "Save.h"     /* (For menu component IDs) */
59
#include "Toolbars.h"
60
#include "URLutils.h"
61 62 63 64
#include "Windows.h"

#include "Menus.h"

65 66
/* Local statics */

67 68
static browser_data * document_menu_browser     = NULL;
static HStream      * document_menu_opened_over = NULL;
69

70 71
/* Static function prototypes */

72 73
static void menus_toggle_bars       (IdBlock * idb);
static void menus_toggle_look       (IdBlock * idb);
74

75 76 77 78
static void menus_choices_bars      (IdBlock * idb);
static void menus_choices_other     (IdBlock * idb);

static void menus_history_selection (IdBlock * idb);
79
static void menus_main_selection    (IdBlock * idb);
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

/*************************************************/
/* menus_item_selected()                         */
/*                                               */
/* Following a Menu_Selection event from the     */
/* toolbox, work out what to do with it.         */
/*                                               */
/* Parameters are as for a standard Toolbox      */
/* event handler.                                */
/*************************************************/

int menus_item_selected(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  #ifdef TRACE
    if (tl & (1u<<4)) Printf("menus_item_selected: Called\n");
  #endif

  switch (idb->self_component)
  {
99 100 101
    case UtilsURLBar:
    case UtilsButtonBar:
    case UtilsStatusBar: menus_toggle_bars(idb);
102 103
    break;

104 105 106 107 108
    case UtilsFullScreen:
    case UtilsUnderlineLinks:
    case UtilsUseDocumentColours:
    case UtilsShowForegroundImages:
    case UtilsShowBackgroundImages: menus_toggle_look(idb);
109 110
    break;

111 112 113
    case ChoicesURLBar:
    case ChoicesButtonBar:
    case ChoicesStatusBar: menus_choices_bars(idb);
114 115
    break;

116 117 118 119 120
    case ChoicesFullScreen:
    case ChoicesUnderlineLinks:
    case ChoicesUseDocumentColours:
    case ChoicesShowForegroundImages:
    case ChoicesShowBackgroundImages: menus_choices_other(idb);
121 122
    break;

123
    case ChoicesSave: ChkError(save_save_choices(NULL));
124 125 126
    break;

    case HistoryEmptyLocal:
127 128
    case HistoryEmptyGlobal:
    case HistoryEmptyImage: menus_history_selection(idb);
129
    break;
130

131
    case MainLogOut:
132 133 134
    case MainCache: menus_main_selection(idb);
    break;

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    default: return 0;
  }

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

  return 1;
}

/*************************************************/
/* menus_toggle_tick()                           */
/*                                               */
/* Toggles the state of a given tick in a menu.  */
/*                                               */
/* Parameters: The object ID of the menu;        */
151
/*                                               */
152 153 154 155 156 157 158
/*             The component ID to alter.        */
/*                                               */
/* Returns:    1 if item is now ticked, else 0.  */
/*************************************************/

int menus_toggle_tick(ObjectId o, ComponentId c)
{
159
  int t = 0;
160 161 162 163 164

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

165
  menu_get_tick(0,o,c,&t);
166
  t ? (t = 0) : (t = 1);
167
  menu_set_tick(0,o,c,t);
168 169 170 171 172 173 174 175

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

  return t;
}

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
/*************************************************/
/* menus_show_main()                             */
/*                                               */
/* Called before the main menu is showed from    */
/* from the icon bar.                            */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

int menus_show_main(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  unsigned int task_handle = 0;

  utils_get_task_handle(lookup_token("ProxyName:Acorn WebServe",0,0), &task_handle);

  if (!task_handle) menu_set_fade(0, idb->self_id, MainCache, 1);
  else              menu_set_fade(0, idb->self_id, MainCache, 0);

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
  #ifndef SINGLE_USER

    if (!logged_in)
    {
      menu_set_fade(0, idb->self_id, MainChoices,     1);
      menu_set_fade(0, idb->self_id, MainOpenURL,     1);
      menu_set_fade(0, idb->self_id, MainShowHotlist, 1);
      menu_set_fade(0, idb->self_id, MainHelp,        1);
      menu_set_fade(0, idb->self_id, MainLogOut,      1);
    }
    else
    {
      menu_set_fade(0, idb->self_id, MainChoices,     0);
      menu_set_fade(0, idb->self_id, MainOpenURL,     0);
      menu_set_fade(0, idb->self_id, MainShowHotlist, 0);
      menu_set_fade(0, idb->self_id, MainHelp,        0);
      menu_set_fade(0, idb->self_id, MainLogOut,      0);
    }

  #endif

216 217 218
  return 1;
}

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
/*************************************************/
/* menus_show_utils()                            */
/*                                               */
/* Called when the utils menu is about to be     */
/* opened from a main menu in a browser window.  */
/* Handles turning ticks on and off in items.    */
/* Parameters are as standard for a Toolbox      */
/* event handler                                 */
/*************************************************/

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

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

  /* Find the browser_data structure for the browser (ancestor) window */

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

  #ifdef TRACE
    if (tl & (1u<<4)) Printf("menus_show_utils: For ancestor client handle %p\n",(void *) b);
  #endif

  /* Set ticks according to the current toolbar status. */
  /* This should be for an ancestor window, and not for */
  /* any child frames. Similarly, the Full Screen       */
  /* state comes from the ancestor browser.             */

  {
251
    browser_data * ancestor = utils_ancestor(b);
252

253 254 255 256
    menu_set_tick(0, idb->self_id, UtilsURLBar,     ancestor->url_bar);
    menu_set_tick(0, idb->self_id, UtilsButtonBar,  ancestor->button_bar);
    menu_set_tick(0, idb->self_id, UtilsStatusBar,  ancestor->status_bar);
    menu_set_tick(0, idb->self_id, UtilsFullScreen, ancestor->full_screen);
257 258 259 260 261
  }

  /* Set ticks according to general options. These are specific */
  /* to each window.                                            */

262 263 264 265
  menu_set_tick(0, idb->self_id, UtilsUnderlineLinks,       b->underline_links);
  menu_set_tick(0, idb->self_id, UtilsUseDocumentColours,   b->use_source_cols);
  menu_set_tick(0, idb->self_id, UtilsShowForegroundImages, b->show_foreground);
  menu_set_tick(0, idb->self_id, UtilsShowBackgroundImages, b->show_background);
266 267 268 269 270 271 272 273 274 275 276

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

  return 1;
}

/*************************************************/
/* menus_show_file()                             */
/*                                               */
277
/* Called when the File submenu is about to be   */
278 279 280 281 282 283 284
/* opened from a browser window's main menu.     */
/* Deals with greying of entries as appropriate. */
/*************************************************/

int menus_show_file(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  browser_data * b;
285 286
  browser_data * parent;
  browser_data * ancestor;
287
  char         * url;
288 289 290 291 292 293 294 295 296

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

  /* Find the browser_data structure for the browser (ancestor) window */

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

297 298
  if (!b) return 1;

299 300 301 302
  #ifdef TRACE
    if (tl & (1u<<4)) Printf("menus_show_file: For ancestor client handle %p\n",(void *) b);
  #endif

303 304 305 306
  parent = utils_parent(b);
  ancestor = utils_ancestor(b);
  if (ancestor == b) ancestor = NULL;

307 308
  /* If there is HTML source present, unfade the Save entry - else fade it out */

309 310
  if (b->source) menu_set_fade(0, idb->self_id, FileSaveFrame, 0);
  else           menu_set_fade(0, idb->self_id, FileSaveFrame, 1);
311

312 313 314 315 316 317
  /* Similarly, for a parent */

  if (parent && parent->source) menu_set_fade(0, idb->self_id, FileSaveParent, 0);
  else                          menu_set_fade(0, idb->self_id, FileSaveParent, 1);

  /* Lastly, the ancestor */
318

319 320
  if (ancestor && ancestor->source) menu_set_fade(0, idb->self_id, FileSaveAncestor, 0);
  else                              menu_set_fade(0, idb->self_id, FileSaveAncestor, 1);
321

322
  /* If the browser has children or no line list, you can't print from it */
323

324 325
  if (b->nchildren || !b->cell || !b->cell->nlines) menu_set_fade(0, idb->self_id, FilePrint, 1);
  else                                              menu_set_fade(0, idb->self_id, FilePrint, 0);
326

327 328 329 330
  /* Do we have a current location? */

  url = browser_current_url(b);

331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
  if (url && *url) menu_set_fade(0, idb->self_id, FileSaveFrameLocation, 0);
  else             menu_set_fade(0, idb->self_id, FileSaveFrameLocation, 1);

  /* Does the parent have a current location? */

  url = parent ? browser_current_url(parent) : NULL;

  if (url && *url) menu_set_fade(0, idb->self_id, FileSaveParentLocation, 0);
  else             menu_set_fade(0, idb->self_id, FileSaveParentLocation, 1);

  /* Does the ancestor have a current location? */

  url = ancestor ? browser_current_url(ancestor) : NULL;

  if (url && *url) menu_set_fade(0, idb->self_id, FileSaveAncestorLocation, 0);
  else             menu_set_fade(0, idb->self_id, FileSaveAncestorLocation, 1);
347

348 349 350 351 352 353 354
  #ifdef TRACE
    if (tl & (1u<<4)) Printf("menus_show_file: Successful\n");
  #endif

  return 1;
}

355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
/*************************************************/
/* menus_show_navigate()                         */
/*                                               */
/* Called when the Navigate submenu is about to  */
/* be opened from a browser window's main menu.  */
/* Deals with greying of entries as appropriate. */
/*************************************************/

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

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

  /* Find the browser_data structure for the browser (ancestor) window */

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

  if (!b) return 1;

  #ifdef TRACE
    if (tl & (1u<<4)) Printf("menus_show_navigate: For ancestor client handle %p\n",(void *) b);
  #endif

  /* Can only stop if there's activity of some sort */

  if (
       (b->anim_handler    && !b->anim_drift)      ||
       (b->meta_refresh_at && b->meta_refresh_url)
     )
     menu_set_fade(0, idb->self_id, NavigateStopAllFetches, 0);

  else menu_set_fade(0, idb->self_id, NavigateStopAllFetches, 1);

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

394 395
  if (!history_can_go_backwards(b)) menu_set_fade(0, idb->self_id, NavigateBack,    1);
  else                              menu_set_fade(0, idb->self_id, NavigateBack,    0);
396 397 398

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

399 400
  if (!history_can_go_forwards(b))  menu_set_fade(0, idb->self_id, NavigateForward, 1);
  else                              menu_set_fade(0, idb->self_id, NavigateForward, 0);
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421

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

  if (
       (
         !b->show_foreground &&
         b->displayed != Display_External_Image
       )
       ||
       !b->show_background
     )
     menu_set_fade(0, idb->self_id, NavigateLoadAllImages, 0);

  else menu_set_fade(0, idb->self_id, NavigateLoadAllImages, 1);

  /* Finished */

  return 1;
}

422 423 424 425 426 427 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
/*************************************************/
/* menus_show_export()                           */
/*                                               */
/* Called before the Export menu is showed.      */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

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

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

  if (!is_known_browser(b))
  {
    #ifdef TRACE

      erb.errnum = Utils_Error_Custom_Normal;

      sprintf(erb.errmess,
              "Browser %p is unknown in menus_show_export",
              b);

      ChkError(&erb);

    #endif

    return 0;
  }

455 456
  /* The rest are supported, but may not be valid depending on */
  /* what the menu was opened over.                            */
457

458
  over = menus_document_opened_over();
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475

  /* Export a link */

  if (!over || !ISLINK(over)) menu_set_fade(0, idb->self_id, ExportLink, 1);
  else                        menu_set_fade(0, idb->self_id, ExportLink, 0);

  /* Export a foreground image */

  if (
       !over ||
       !
       (
         (over->style & IMG) ||
         (
           over->tagno == TAG_INPUT &&
           HtmlINPUTtype(over) == inputtype_IMAGE
         )
476 477 478 479 480
         ||
         (
           ISOBJECT(over) &&
           object_token_is_image(b, over)
         )
481 482 483 484 485 486 487 488 489 490 491 492
       )
     )
     menu_set_fade(0, idb->self_id, ExportPicture, 1);

  else
  {
    if (image_token_can_be_saved_as_sprite(b, over)) menu_set_fade(0, idb->self_id, ExportPicture, 0);
    else                                             menu_set_fade(0, idb->self_id, ExportPicture, 1);
  }

  /* Export a background image */

493
  if (b && b->background_image < 0) menu_set_fade(0, idb->self_id, ExportBackground, 1);
494 495 496 497 498 499
  else
  {
    if (image_token_can_be_saved_as_sprite(b, NULL)) menu_set_fade(0, idb->self_id, ExportBackground, 0);
    else                                             menu_set_fade(0, idb->self_id, ExportBackground, 1);
  }

500 501 502 503 504 505 506 507 508 509 510 511 512
  /* Export page as Draw or a text file */

  if (b && !b->nchildren && b->cell && b->cell->nlines)
  {
    menu_set_fade(0, idb->self_id, ExportAsText, 0);
    menu_set_fade(0, idb->self_id, ExportAsDraw, 0);
  }
  else
  {
    menu_set_fade(0, idb->self_id, ExportAsText, 1);
    menu_set_fade(0, idb->self_id, ExportAsDraw, 1);
  }

513 514 515 516 517
  /* Finished */

  return 1;
}

518 519 520 521 522 523 524
/*************************************************/
/* menus_show_choices()                          */
/*                                               */
/* Called when the choices menu is about to be   */
/* opened from the icon bar menu. Handles        */
/* turning ticks on and off depending on the     */
/* Messages file's choices entries.              */
525 526 527
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
528 529 530 531 532 533 534 535
/*************************************************/

int menus_show_choices(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  #ifdef TRACE
    if (tl & (1u<<4)) Printf("menus_show_choices: Called\n");
  #endif

536 537 538
  menu_set_tick(0, idb->self_id, ChoicesURLBar,               choices.url_bar);
  menu_set_tick(0, idb->self_id, ChoicesButtonBar,            choices.button_bar);
  menu_set_tick(0, idb->self_id, ChoicesStatusBar,            choices.status_bar);
539

540 541 542 543
  menu_set_tick(0, idb->self_id, ChoicesUnderlineLinks,       choices.underline_links);
  menu_set_tick(0, idb->self_id, ChoicesUseDocumentColours,   choices.use_source_cols);
  menu_set_tick(0, idb->self_id, ChoicesShowForegroundImages, choices.show_foreground);
  menu_set_tick(0, idb->self_id, ChoicesShowBackgroundImages, choices.show_background);
544

545
  menu_set_tick(0, idb->self_id, ChoicesFullScreen,           choices.full_screen);
546 547 548 549 550 551 552 553

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

  return 1;
}

554
/*************************************************/
555
/* menus_show_document()                         */
556
/*                                               */
557 558
/* Called before the document menu is showed     */
/* from a browser window.                        */
559 560 561 562 563
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

564
int menus_show_document(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
565 566 567 568 569 570 571 572 573
{
  WimpGetPointerInfoBlock   info;
  browser_data            * b;

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

  if (!is_known_browser(b)) return 0;

574 575
  document_menu_browser     = b;
  document_menu_opened_over = browser_get_pointer_token(b, &info, NULL, NULL);
576 577 578 579

  return 1;
}

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
/*************************************************/
/* menus_show_history()                          */
/*                                               */
/* Called before the History menu is displayed.  */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

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

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

  if (!is_known_browser(b) || history_empty(b))
  {
    menu_set_fade(0, idb->self_id, HistorySaveLocal,  1);
    menu_set_fade(0, idb->self_id, HistoryEmptyLocal, 1);
  }
  else
  {
    menu_set_fade(0, idb->self_id, HistorySaveLocal,  0);
    menu_set_fade(0, idb->self_id, HistoryEmptyLocal, 0);
  }

  if (history_empty(NULL))
  {
    menu_set_fade(0, idb->self_id, HistorySaveGlobal,  1);
    menu_set_fade(0, idb->self_id, HistoryEmptyGlobal, 1);
  }
  else
  {
    menu_set_fade(0, idb->self_id, HistorySaveGlobal,  0);
    menu_set_fade(0, idb->self_id, HistoryEmptyGlobal, 0);
  }

617 618 619
  if (imghistory_empty()) menu_set_fade(0, idb->self_id, HistoryEmptyImage, 1);
  else                    menu_set_fade(0, idb->self_id, HistoryEmptyImage, 0);

620 621 622
  return 1;
}

623
/*************************************************/
624
/* menus_hide_document()                         */
625
/*                                               */
626
/* Called before the document menu is hidden.    */
627 628 629 630 631
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

632
int menus_hide_document(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
633
{
634 635
  document_menu_opened_over = NULL;
  document_menu_browser     = NULL;
636

637 638
  /* Pass on - others (eg Encoding.c) may be interested */
  return 0;
639 640 641
}

/*************************************************/
642
/* menus_close_document_if_mine()                */
643
/*                                               */
644 645
/* If a document menu is open and its origin was */
/* the given browser, close it.                  */
646 647 648 649 650 651 652
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the menu.             */
/*                                               */
/* Returns:    1 if the menu was closed, else 0. */
/*************************************************/

653
int menus_close_document_if_mine(browser_data * b)
654
{
655
  if (b && b == document_menu_browser)
656 657 658 659 660 661 662 663 664 665 666 667
  {
    _swix(Wimp_CreateMenu,
          _IN(1),

          -1);

    return 1;
  }

  return 0;
}

668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
/*************************************************/
/* menus_toggle_bars()                           */
/*                                               */
/* Called when one of the Utils menu items that  */
/* turns info/toolbars on and off is selected.   */
/*                                               */
/* The item is ticked or unticked as necessary   */
/* and the relevant toolbar turned on or off.    */
/*                                               */
/* Parameters: Pointer to an ID block, as given  */
/*             by the Toolbox when it raises an  */
/*             event.                            */
/*************************************************/

static void menus_toggle_bars(IdBlock * idb)
{
  int            t = 0, height;
  browser_data * b;

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

  /* Find the browser_data structure for the browser (ancestor) window */

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

  /* Bars are only attached to the ancestor browser_data struct, */
  /* not to any child frames. So want to point b to this.        */

698
  b = utils_ancestor(b);
699 700 701 702 703 704 705 706 707 708 709

  #ifdef TRACE
    if (tl & (1u<<4)) Printf("menus_toggle_bars: For ancestor client handle %p\n",(void *) b);
  #endif

  /* Swap the menu's tick state */

  t = menus_toggle_tick(idb->self_id, idb->self_component);

  /* Remember height of top toolbar */

710 711
  if (!controls.swap_bars) height = toolbars_button_height(b) + toolbars_url_height(b);
  else                     height = toolbars_status_height(b);
712 713 714 715 716 717 718

  /* For URL and Button bars, need to move the page contents up and down, */
  /* so remember the total bar height before removing or adding one of    */
  /* those bars. For the status bar it doesn't matter.                    */

  switch (idb->self_component)
  {
719 720 721
    case UtilsURLBar:    b->url_bar    = t, toolbars_set_presence(b, InternalTopLeft);    break;
    case UtilsButtonBar: b->button_bar = t, toolbars_set_presence(b, InternalTopLeft);    break;
    case UtilsStatusBar: b->status_bar = t, toolbars_set_presence(b, InternalBottomLeft); break;
722 723 724 725 726 727

    #ifdef TRACE
      default:
      {
        _kernel_oserror e;
        e.errnum = Utils_Error_Custom_Normal;
728
        strncpy(e.errmess,"Toolbar toggle handle not understood in menus_toggle_bars.",252);
729 730 731 732 733 734 735
        show_error_cont(&e);
      }
    #endif
  }

  /* Move any data inside the page as necessary */

736
  if (!controls.swap_bars)
737 738 739 740 741 742 743 744 745
  {
    ChkError(reformat_shift_vertically(b, 0, b->cell->nlines - 1, height - toolbars_url_height(b) - toolbars_button_height(b)));
  }
  else
  {
    ChkError(reformat_shift_vertically(b, 0, b->cell->nlines - 1, height - toolbars_status_height(b)));
  }

  ChkError(reformat_check_extent(b));
746 747 748 749 750 751 752 753 754 755

  /* If there are any frames on the page, resize them */

  if (b->nchildren)
  {
    WimpGetWindowStateBlock state;

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

756
    frames_resize_frameset(b, &state.visible_area);
757
  }
758 759 760 761

  #ifdef TRACE
    if (tl & (1u<<4)) Printf("menus_toggle_bars: Successful\n");
  #endif
762 763 764 765 766 767 768 769 770 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
}

/*************************************************/
/* menus_toggle_look()                           */
/*                                               */
/* Called when one of the Utils menu items not   */
/* related to toolbars is selected.              */
/*                                               */
/* The item is ticked or unticked as necessary   */
/* and the relevant visual aspect of the browser */
/* window turned on or off.                      */
/*                                               */
/* Any child frames at this frame level or       */
/* higher will inherit the new characteristics.  */
/*                                               */
/* Parameters: Pointer to an ID block, as given  */
/*             by the Toolbox when it raises an  */
/*             event.                            */
/*************************************************/

static void menus_toggle_look(IdBlock * idb)
{
  int            t = 0;
  ObjectId       browser;
  browser_data * b;
  browser_data * ancestor;

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

  /* Find the browser_data structure for the browser (ancestor) window */

  browser = idb->ancestor_id;

797
  ChkError(toolbox_get_client_handle(0, browser, (void *) &b));
798 799 800 801 802

  #ifdef TRACE
    if (tl & (1u<<4)) Printf("menus_toggle_look: For ancestor client handle %p\n",(void *) b);
  #endif

803
  ancestor = utils_ancestor(b);
804 805 806

  /* Swap the menu's tick state */

807
  t = menus_toggle_tick(idb->self_id, idb->self_component);
808 809 810

  /* Reflect changes in ancestor and children */

811
  /* (Un)Set the relevant flag inside the browser_data structure */
812

813
  switch (idb->self_component)
814
  {
815 816 817 818
    case UtilsUnderlineLinks:       ChkError(browser_set_look(b, idb->self_id, t, -1, -1, -1)); break;
    case UtilsUseDocumentColours:   ChkError(browser_set_look(b, idb->self_id, -1, t, -1, -1)); break;
    case UtilsShowForegroundImages: ChkError(browser_set_look(b, idb->self_id, -1, -1, t, -1)); break;
    case UtilsShowBackgroundImages: ChkError(browser_set_look(b, idb->self_id, -1, -1, -1, t)); break;
819

820
    case UtilsFullScreen:
821
    {
822
      browser_data * ancestor = utils_ancestor(b);
823

824
      /* Can only go Full Screen for ancestor objects */
825

826
      ancestor->full_screen = t;
827 828 829

      /* This opens full screen, remembering the previous window size */

830 831 832 833 834
      ChkError(windows_open_full_screen(ancestor,
                                        t,
                                        !t,
                                        choices.v_scroll,
                                        choices.h_scroll));
835 836 837

      /* Reformat the page */

838
      ChkError(reformat_format_from(ancestor, -1, 1, -1));
839 840 841
    }
    break;

842 843 844
    /* Report error for default case in TRACE builds as a warning */
    /* to the programmer only.                                    */

845
    #ifdef TRACE
846

847 848 849 850 851 852 853
      default:
      {
        _kernel_oserror e;

        if (b->ancestor) return; /* Avoid giving error for all the child windows */

        e.errnum = Utils_Error_Custom_Normal;
854
        strncpy(e.errmess,"Menu selection not understood in menus_toggle_look.",252);
855 856
        show_error_cont(&e);
      }
857

858 859 860
    #endif
  }

861
  /* Update the ancestor buttons */
862

863
  ChkError(toolbars_set_button_states(ancestor));
864

865 866 867
  #ifdef TRACE
    if (tl & (1u<<4)) Printf("menus_toggle_look: Successful\n");
  #endif
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
}

/*************************************************/
/* menus_choices_bars()                          */
/*                                               */
/* Called when one of the Choices menu items     */
/* relating to toolbars is selected. Toggles the */
/* menu item's tick and (un)sets the relevant    */
/* bit in the global Choices.                    */
/*                                               */
/* Parameters: Pointer to an ID block, as given  */
/*             by the Toolbox when it raises an  */
/*             event.                            */
/*************************************************/

static void menus_choices_bars(IdBlock * idb)
{
  int t;

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

  t = menus_toggle_tick(idb->self_id,idb->self_component);

  switch (idb->self_component)
  {
895
    case ChoicesURLBar:    choices.url_bar    = t;
896
    break;
897
    case ChoicesButtonBar: choices.button_bar = t;
898
    break;
899
    case ChoicesStatusBar: choices.status_bar = t;
900 901 902 903 904 905 906
    break;

    #ifdef TRACE
      default:
      {
        _kernel_oserror e;
        e.errnum = Utils_Error_Custom_Normal;
907
        strncpy(e.errmess,"Toolbar toggle handle not understood in menus_choices_bars.",252);
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
        show_error_cont(&e);
      }
    #endif
  }

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

/*************************************************/
/* menus_choices_other()                         */
/*                                               */
/* Called when one of the Choices menu items     */
/* relating to something other than toolbars is  */
/* selected. Toggles the menu item's tick and    */
/* (un)sets the relevant bit in the global       */
/* Choices.                                      */
/*                                               */
/* Parameters: Pointer to an ID block, as given  */
/*             by the Toolbox when it raises an  */
/*             event.                            */
/*************************************************/

static void menus_choices_other(IdBlock * idb)
{
  int t;

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

  t = menus_toggle_tick(idb->self_id,idb->self_component);

  switch (idb->self_component)
  {
944 945 946 947 948 949
    case ChoicesUnderlineLinks:       choices.underline_links = t; break;
    case ChoicesUseDocumentColours:   choices.use_source_cols = t; break;
    case ChoicesShowForegroundImages: choices.show_foreground = t; break;
    case ChoicesShowBackgroundImages: choices.show_background = t; break;

    case ChoicesFullScreen:           choices.full_screen     = t; break;
950 951 952 953 954 955

    #ifdef TRACE
      default:
      {
        _kernel_oserror e;
        e.errnum = Utils_Error_Custom_Normal;
956
        strncpy(e.errmess,"Menu selection item not understood in menus_choices_other.",252);
957 958 959 960 961 962 963 964 965 966 967
        show_error_cont(&e);
      }
    #endif
  }

  toolbars_set_all_button_states();

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

969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
/*************************************************/
/* menus_history_selection()                     */
/*                                               */
/* Called when one of the History menu items     */
/* is selected.                                  */
/*                                               */
/* Parameters: Pointer to an ID block, as given  */
/*             by the Toolbox when it raises an  */
/*             event.                            */
/*************************************************/

static void menus_history_selection(IdBlock * idb)
{
  browser_data * b;

  if (toolbox_get_client_handle(0, idb->ancestor_id, (void *) &b)) b = NULL;
  if (!is_known_browser(b)) b = NULL;

  switch (idb->self_component)
  {
    // Not really that useful? Amount of memory tied up in local
    //histories is typically pretty small.

    case HistoryEmptyLocal:
    {
      if (!b) return;

      history_remove(b, NULL);

      toolbars_set_all_button_states();
    }
    break;

    // Yikes! Perhaps we should have some confirmation here...

    case HistoryEmptyGlobal:
    {
      history_limit(0);

      toolbars_set_all_button_states();
    }
    break;
1011 1012 1013 1014 1015 1016

    case HistoryEmptyImage:
    {
      imghistory_limit(0);
    }
    break;
1017 1018 1019 1020 1021
  }

  return;
}

1022
/*************************************************/
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
/* menus_main_selection()                        */
/*                                               */
/* Called when one of the main menu items is     */
/* selected.                                     */
/*                                               */
/* Parameters: Pointer to an ID block, as given  */
/*             by the Toolbox when it raises an  */
/*             event.                            */
/*************************************************/

static void menus_main_selection(IdBlock * idb)
{
  switch (idb->self_component)
  {
    case MainCache:
    {
      unsigned int handle = 0;
      WimpMessage  msg;

      /* First, get WebServe's task handle */

      utils_get_task_handle(lookup_token("ProxyName:Acorn WebServe",0,0), &handle);

      #ifdef TRACE

        if (!handle)
        {
          /* Didn't find WebServe, so complain and exit */

          erb.errnum = Utils_Error_Custom_Message;

          StrNCpy0(erb.errmess, "WebServe is not present");

          show_error_ret(&erb);
        }

      #else

        if (!handle)
        {
          /* Didn't find WebServe, so exit */

          return;
        }

      #endif

      /* If WebServe is present, send the message */

      msg.hdr.size        = 32;
      msg.hdr.sender      = task_handle;
      msg.hdr.my_ref      = 0;
      msg.hdr.your_ref    = 0;
      msg.hdr.action_code = Wimp_MAppControl;

      msg.data.app_control.version = 1;
      msg.data.app_control.flags   = Wimp_MAppControl_ImmediateAction;
      msg.data.app_control.reason  = Wimp_MAppControl_Configure;

      ChkError(wimp_send_message(Wimp_EUserMessage, &msg, handle, -1, NULL));
    }
    break;
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125

    case MainLogOut:
    {
      browser_data * current;

      /* Close all open browser windows */

      while (last_browser)
      {
        /* Only close ancestor browsers */

        current = last_browser;

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

        if (current) handle_close_browser(0, NULL, NULL, current);
      }

      /* Close the OpenURL dialogue, in case it's open... */

      openurl_close(0, 0);

      #ifndef REMOTE_HOTLIST

        /* Make sure the hotlist window is closed */

        show_error_ret(hotlist_close());

      #endif

      /* Make sure the Choices are closed too */

      show_error_ret(choices_close());

      /* Finally, log the user out and bring back the */
      /* log in prompt.                               */

      show_error_ret(multiuser_logout());
      ChkError(multiuser_login());
    }
    break;
1126 1127 1128 1129 1130
  }
}

/*************************************************/
/* menus_document_opened_over()                  */
1131
/*                                               */
1132
/* When the document menu is opened, this sees   */
1133 1134
/* if the pointer was over a specific token.     */
/* If so, it records this information for        */
1135
/* future use (see menus_show_document). This    */
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
/* function returns that token address, or NULL  */
/* if the menu isn't open / no token could be    */
/* determined.                                   */
/*                                               */
/* Returns:    Pointer to the token the pointer  */
/*             was over when the main menu was   */
/*             opened, or NULL if the menu is no */
/*             longer open / no token could be   */
/*             determined when it was opened.    */
/*************************************************/

1147
HStream * menus_document_opened_over(void)
1148
{
1149
  return document_menu_opened_over;
1150 1151 1152
}

/*************************************************/
1153
/* menus_document_over_browser()                 */
1154
/*                                               */
1155 1156 1157
/* Companion function to                         */
/* menus_document_opened_over, so see that for   */
/* details.                                      */
1158 1159 1160 1161 1162 1163 1164
/*                                               */
/* Returns:    Pointer to the browser_data       */
/*             struct representing the browser   */
/*             the main menu was opened from, if */
/*             any (NULL if not).                */
/*************************************************/

1165
browser_data * menus_document_over_browser(void)
1166
{
1167
  return document_menu_browser;
1168
}
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300

/*************************************************/
/* menus_help_from_help_string()                 */
/*                                               */
/* Visit a URL encoded in the selected menu      */
/* item's Interactive Help string.               */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

int menus_help_from_help_string(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  browser_data * b;
  char           help[Limits_URL];

  /* Find the browser_data structure for the browser (ancestor) window */

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

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

  if (
       menu_get_entry_help_message(0,
                                   idb->self_id,
                                   idb->self_component,
                                   help,
                                   sizeof(help),
                                   NULL)
     )
     return 0;

  if (b) ChkError(fetchpage_new(b, help, 1, 0));
  else   ChkError(windows_create_browser(help, NULL, NULL, NULL, 0));

  return 1;
}

/*************************************************/
/* menus_help_release_notes()                    */
/*                                               */
/* Visit a URL encoded in the selected menu      */
/* item's Interactive Help string, appended by   */
/* a filename based on the browser version.      */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

int menus_help_release_notes(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  browser_data * b;
  char         * p;
  char           help[Limits_URL];

  /* Find the browser_data structure for the browser (ancestor) window */

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

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

  /* Get the Help string */

  if (
       menu_get_entry_help_message(0,
                                   idb->self_id,
                                   idb->self_component,
                                   help,
                                   sizeof(help),
                                   NULL)
     )
     return 0;

  /* Append the version */

  *tokens = '\0';
  lookup_token("Version",1,0);

  if (strlen(tokens) + strlen(help) + sizeof(".html") > sizeof(help)) return 0;

  /* Convert spaces to underscores and dots to commas */

  p = tokens;

  while (*p)
  {
    if (*p == ' ') *p = '_';
    if (*p == '.') *p = ',';

    p++;
  }

  /* Append '.html' */

  strcat(tokens, ".html");
  strcat(help, tokens);

  /* Fetch the page */

  if (b) ChkError(fetchpage_new(b, help, 1, 0));
  else   ChkError(windows_create_browser(help, NULL, NULL, NULL, 0));

  return 1;
}

/*************************************************/
/* menus_help_about_page()                       */
/*                                               */
/* Go to the 'about:' page.                      */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

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

  /* Find the browser_data structure for the browser (ancestor) window */

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

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

  if (b) ChkError(fetchpage_new(b, AboutMethod, 1, 0));
  else   ChkError(windows_create_browser(AboutMethod, NULL, NULL, NULL, 0));

  return 1;
}
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341

/*************************************************/
/* menus_help_miscellaneous()                    */
/*                                               */
/* Go to a page looked up from Controls under    */
/* the name 'JumpToxx' where 'xx' is a 2 digit   */
/* hex number obtained from the given parameter. */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             in which to fetch the item, or    */
/*             NULL for a new window;            */
/*                                               */
/*             Number of token to look for (see  */
/*             above).                           */
/*************************************************/

void menus_help_miscellaneous(browser_data * b, int which)
{
  char   name[9];
  char * url;

  /* Sanity check */

  if (which < 0 || which > 0x7f) return;

  /* Build the Choices token name */

  sprintf(name, "JumpTo%02x", which);

  /* Read it - exit if not found */

  url = lookup_control(name, 1, NULL);
  if (!strcmp(url, "!")) return;

  /* Fetch the URL */

  if (b) ChkError(fetchpage_new(b, url, 1, 0));
  else   ChkError(windows_create_browser(url, NULL, NULL, NULL, 0));

  return;
}