SaveFile 72.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/* 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   : SaveFile.c                             */
/*                                                 */
/* Purpose: Handle the Save File dialogue (actual  */
/*          file saving is done in Save.c). Relies */
/*          on there being only one Save File      */
/*          dialogue open at a time (it is a       */
/*          shared object).                        */
/*                                                 */
/* Author : A.D.Hodgkinson                         */
/*                                                 */
/* History: 03-Sep-97: Created.                    */
27 28 29 30
/*                                                 */
/*          08-Dec-97: Added code for option or    */
/*                     radio buttons and auto      */
/*                     widthing of the dialogue.   */
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/***************************************************/

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

#include "swis.h"

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

#include "svcprint.h"
#include "Global.h"
50
#include "MiscDefs.h"
51 52 53 54 55
#include "Utils.h"

#include "Browser.h"
#include "Fetch.h"  /* (For ISLINK macro) */
#include "Filetypes.h"
56
#include "History.h"
57
#include "Hotlist.h"
58 59
#include "Images.h"
#include "Menus.h"
60
#include "MimeMap.h"
61
#include "Object.h"
62 63
#include "Protocols.h"
#include "Save.h"
64
#include "SaveDraw.h"
65
#include "SaveText.h"
66 67 68 69 70 71 72 73
#include "Toolbars.h"
#include "URLutils.h"
#include "Windows.h"

#include "SaveFile.h"

/* Local statics */

74 75 76 77
static browser_data         * savefile_browser = NULL;
static HStream              * savefile_token   = NULL;
static int                    savefile_type    = 0x000;
static savefile_open_reason   savefile_reason  = save_as_html;
78

79 80 81
static ObjectId               window_id        = 0;
static ComponentId            parent_component = -1;
static ObjectId               ancestor_id      = 0;
82

83 84 85 86 87 88 89 90 91 92
/* These are to remember the state of the 'alternative'  */
/* selector (option button or radio buttons) for when    */
/* the Save File dialogue is opened for a particular     */
/* parent component. The 'mhso' etc. represent the first */
/* letters of the component name from Menus.h (so in the */
/* above example, mhso = MiscHotlistSaveObject). Where   */
/* several components lead to the same section of code   */
/* in a 'switch' statement, the name of the first        */
/* as appearing in savefile_to_be_shown is used.         */

93 94 95 96 97 98 99 100 101 102 103
#ifndef REMOTE_HOTLIST

  static int alt_mhso = 0; /* MiscHotlistSaveObject  */

#endif

static   int alt_ead  = 0; /* ExportAsDraw           */
static   int alt_fsfl = 0; /* FrameSaveFrameLocation */
static   int alt_el   = 0; /* ExportLink             */
static   int alt_ep   = 0; /* ExportPicture          */
static   int alt_eb   = 0; /* ExportBackground       */
104

105 106
/* Static function prototypes */

107 108 109 110 111 112 113
static int               savefile_drag_ended                         (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
static int               savefile_ok                                 (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
static int               savefile_cancel                             (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);

static _kernel_oserror * savefile_switch_to_normal                   (ObjectId window);
static _kernel_oserror * savefile_switch_to_option                   (ObjectId window);
static _kernel_oserror * savefile_switch_to_radios                   (ObjectId window);
114

115
static _kernel_oserror * savefile_set_items                          (ObjectId window, int selected);
116

117 118 119
static int               savefile_option_changed                     (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
static int               savefile_radio_changed                      (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
static int               savefile_item_changed                       (IdBlock * idb);
120

121 122 123 124 125 126
static _kernel_oserror * savefile_text_uri_or_url                    (ObjectId window);
static _kernel_oserror * savefile_toggle_uri_or_url                  (ObjectId window);
static _kernel_oserror * savefile_text_sprite_or_original            (ObjectId window);
static _kernel_oserror * savefile_toggle_sprite_or_original          (ObjectId window, const char * src);
static _kernel_oserror * savefile_text_without_backgrounds_or_with   (ObjectId window);
static _kernel_oserror * savefile_toggle_without_backgrounds_or_with (ObjectId window);
127

128 129
static _kernel_oserror * savefile_set_correct_extent                 (ObjectId window, BBox * ra1);
static _kernel_oserror * savefile_auto_width                         (ObjectId window);
130 131 132 133 134 135 136 137 138 139 140

/*************************************************/
/* savefile_open_for()                           */
/*                                               */
/* Creates and opens a Save File dialogue for a  */
/* given browser, opening near the pointer.      */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             which is the ancestor of the      */
/*             dialogue;                         */
/*                                               */
141 142 143
/*             A savefile_open_reason describing */
/*             why the dialogue is being opened  */
/*             (see SaveFile.h).                 */
144 145
/*************************************************/

146
_kernel_oserror * savefile_open_for(browser_data * b, savefile_open_reason reason)
147
{
148 149
  ObjectId    id;
  ComponentId pc;
150 151 152 153 154 155 156 157 158 159 160 161 162

  /* Create the dialogue - as this is shared object it */
  /* will not be recreated if it already exists, but   */
  /* it's more efficient to not bother even trying!    */

  if (!window_id)
  {
    RetError(toolbox_create_object(0,
                                   "SaveFile",
                                   &id));
  }
  else id = window_id;

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
  /* Remember the reason we're being opened here */

  savefile_reason = reason;

  /* Work out which parent object and component IDs */
  /* we want to set                                 */

  switch (savefile_reason)
  {
    default:
    case save_as_html: pc = FileSaveFrame; break;
    case save_as_draw: pc = ExportAsDraw;  break;
    case save_as_text: pc = ExportAsText;  break;
  }

  /* Show the dialogue */
179 180 181 182 183

  RetError(toolbox_show_object(Toolbox_ShowObject_AsMenu,
                               id,
                               Toolbox_ShowObject_AtPointer,
                               NULL,
184 185
                               b->self_id,
                               pc));
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
  return NULL;
}

/*************************************************/
/* savefile_to_be_shown()                        */
/*                                               */
/* Fills in the Save File dialogue prior to      */
/* being shown, on the basis of the parent       */
/* component ID and ancestor object ID in the    */
/* event.                                        */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

int savefile_to_be_shown(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  char           text[Limits_OS_Pathname];
  browser_data * b;
  int            first_time = !window_id;
  int            ok         = 0;
  ObjectId       pc         = idb->parent_component;

209 210
  /* If this is the first time we've been opened, do a few */
  /* initialisation bits and pieces                        */
211

212 213 214
  if (first_time)
  {
    /* Process the writable icon text */
215

216
    ChkError(windows_process_component_text(idb->self_id, SaveFileWrit, text, sizeof(text), 0, 1));
217

218 219 220 221 222 223 224 225
    /* The following ensures that we increment the usage */
    /* count for the dialogue so it is never deleted.    */
    /* Otherwise, we have to start keeping track of when */
    /* it goes and deal with event handlers etc. as      */
    /* appropriate. Creating the object like this only   */
    /* gives back the existing ID of what is already     */
    /* there, so this works out as taking up less time   */
    /* and less code than an alternative approach.       */
226

227 228
    ChkError(toolbox_create_object(0, "SaveFile", NULL));
  }
229

230
  /* Where did we come from? */
231

232
  ChkError(toolbox_get_client_handle(0, idb->ancestor_id, (void *) &b));
233 234 235 236 237 238 239

  /* Deal with each parent menu item case */

  switch (pc)
  {
    case -1:
    {
240 241 242
      /* If the dialogue was raised directly from a keyboard shortcut, */
      /* (hmm, preferable to go through an event...) want to save the  */
      /* current page source.                                          */
243

244
      pc = FileSaveFrame;
245 246 247 248 249 250 251

      /* Slight complication is that only an ancestor browser ever   */
      /* has the input focus, so we need to find the actual selected */
      /* frame that the user is going to think the dialogue is for.  */

      if (b->selected_frame) b = b->selected_frame;

252 253 254 255 256
      /* Can't rely on any earlier sanity checks in this case, as this will */
      /* be the first and only function called before the dialogue opens.   */

      if (!b || !b->source) return 0;

257 258 259
      /* No 'break' - let this drop through... */
    }

260
    /* Save a page's source */
261

262
    case FileSaveFrame:
263
    case FileSaveParent:
264
    case FileSaveAncestor:
265
    {
266
      browser_data * alt = b;
267

268 269
      /* For saving a frameset, work out the required browser_data struct */

270
      if (pc == FileSaveParent)
271
      {
272 273
        alt = utils_parent(b);
        if (!alt) alt = utils_ancestor(b);
274 275
      }

276 277 278 279 280 281
      else if (pc == FileSaveAncestor) alt = utils_ancestor(b);

      b = alt;

      ChkError(savefile_switch_to_normal(idb->self_id));

282 283 284 285 286 287 288 289
      /* Reset the transferred data counter */

      b->save_transferred = 0;

      ChkError(savefile_set_leafname_from_url(idb->self_id, SaveFileWrit, browser_current_url(b)));

      /* Set the draggable sprite */

290
      ChkError(savefile_set_filetype(idb->self_id, SaveFileDrag, b->page_is_text ? FileType_TEXT : FileType_HTML, 0));
291

292 293 294 295
      /* Ensure the width is correct */

      ChkError(savefile_auto_width(idb->self_id));

296 297 298 299 300 301 302 303 304
      /* Remember various details about the dialogue's source */

      savefile_browser = b;
      savefile_token   = NULL;

      ok = 1;
    }
    break;

305 306 307 308
    #ifndef REMOTE_HOTLIST

      case HotlistSaveHotlist:
      {
309
        ChkError(savefile_switch_to_normal(idb->self_id));
310
        ChkError(savefile_set_leafname(idb->self_id, SaveFileWrit, lookup_token("HotlistLeafname:Hotlist",0,0)));
311
        ChkError(savefile_set_filetype(idb->self_id, SaveFileDrag, FileType_HTML, 0));
312
        ChkError(savefile_auto_width(idb->self_id));
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329

        ok = 1;
      }
      break;

      /* Save an object from the hotlist - a URL, directory, */
      /* or general selection                                */

      case MiscHotlistSaveObject:
      {
        hotlist_item * source = hotlist_find_selected_item();
        unsigned int   items  = hotlist_count_selected_items();

        if (items && source)
        {
          if (items == 1 && source->type == hl_url)
          {
330
            ChkError(savefile_switch_to_option(idb->self_id));
331
            ChkError(savefile_set_items(idb->self_id, alt_mhso));
332
            ChkError(savefile_set_leafname_from_url(idb->self_id, SaveFileWrit, source->data.url));
333 334 335
            ChkError(savefile_text_uri_or_url(idb->self_id));
            ChkError(savefile_toggle_uri_or_url(idb->self_id));
            ChkError(savefile_auto_width(idb->self_id));
336 337 338
          }
          else
          {
339
            ChkError(savefile_switch_to_normal(idb->self_id));
340
            ChkError(savefile_set_leafname(idb->self_id, SaveFileWrit, lookup_token("HotlistLeafname:Hotlist",0,0)));
341
            ChkError(savefile_set_filetype(idb->self_id, SaveFileDrag, FileType_HTML, 0));
342
            ChkError(savefile_auto_width(idb->self_id));
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
          }

          ok = 1;
        }

        #ifdef TRACE

          else
          {
            erb.errnum = Utils_Error_Custom_Normal;

            sprintf(erb.errmess,
                    "Should have more than a selected hotlist item %p and item count %d in savefile_to_be_shown",
                    source,
                    items);

            ChkError(&erb);
          }

        #endif
      }
      break;

    #endif

368 369 370
    case HistorySaveLocal:
    case HistorySaveGlobal:
    {
371
      ChkError(savefile_switch_to_normal(idb->self_id));
372
      ChkError(savefile_set_leafname(idb->self_id, SaveFileWrit, lookup_token("HistoryLeafname:History",0,0)));
373
      ChkError(savefile_set_filetype(idb->self_id, SaveFileDrag, FileType_HTML, 0));
374 375 376 377 378 379

      savefile_browser = b;
      ok               = 1;
    }
    break;

380 381
    case ExportAsDraw:
    {
382 383 384 385 386
      ChkError(savefile_switch_to_option(idb->self_id));
      ChkError(savefile_set_items(idb->self_id, alt_ead));
      ChkError(savefile_set_leafname_from_url(idb->self_id, SaveFileWrit, browser_current_url(b)));
      ChkError(savefile_text_without_backgrounds_or_with(idb->self_id));
      ChkError(savefile_toggle_without_backgrounds_or_with(idb->self_id));
387
      ChkError(savefile_auto_width(idb->self_id));
388 389 390 391 392

      savefile_browser = b;
      savefile_token   = NULL;

      ok = 1;
393 394 395 396 397
    }
    break;

    case ExportAsText:
    {
398
      ChkError(savefile_switch_to_normal(idb->self_id));
399
      ChkError(savefile_set_leafname_from_url(idb->self_id, SaveFileWrit, browser_current_url(b)));
400
      ChkError(savefile_set_filetype(idb->self_id, SaveFileDrag, FileType_TEXT, 0));
401
      ChkError(savefile_auto_width(idb->self_id));
402 403 404 405 406

      savefile_browser = b;
      savefile_token   = NULL;

      ok = 1;
407 408 409
    }
    break;

410
    /* Save a location as a URI file */
411

412 413 414
    case FileSaveFrameLocation:
    case FileSaveParentLocation:
    case FileSaveAncestorLocation:
415
    {
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
      browser_data * alt = b;
      char         * url;

      /* For saving a frameset, work out the required browser_data struct */

      if (pc == FileSaveParentLocation)
      {
        alt = utils_parent(b);
        if (!alt) alt = utils_ancestor(b);
      }

      else if (pc == FileSaveAncestorLocation) alt = utils_ancestor(b);

      b   = alt;
      url = browser_current_url(b);
431 432 433

      if (!url) url = " ";

434
      ChkError(savefile_switch_to_option(idb->self_id));
435
      ChkError(savefile_set_items(idb->self_id, alt_fsfl));
436
      ChkError(savefile_set_leafname_from_url(idb->self_id, SaveFileWrit, url));
437 438 439
      ChkError(savefile_text_uri_or_url(idb->self_id));
      ChkError(savefile_toggle_uri_or_url(idb->self_id));
      ChkError(savefile_auto_width(idb->self_id));
440 441 442 443 444 445 446 447

      savefile_browser = b;
      savefile_token   = NULL;

      ok = 1;
    }
    break;

448 449 450 451
    /* Export the link the pointer was over when the menu opened */

    case ExportLink:
    {
452
      HStream * link = menus_document_opened_over();
453

454
      if (!link || !ISLINK(link))
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
      {
        #ifndef TRACE

          return 0;

        #else

          erb.errnum = Utils_Error_Custom_Normal;

          sprintf(erb.errmess,
                  "Menu token %p is not a link or has no anchor text in savefile_to_be_shown",
                  link);

          show_error_ret(&erb);

          return 0;

        #endif
      }

      /* Set the leafname and filetype */

477
      ChkError(savefile_switch_to_option(idb->self_id));
478
      ChkError(savefile_set_items(idb->self_id, alt_el));
479
      ChkError(savefile_set_leafname_from_url(idb->self_id, SaveFileWrit, link->anchor));
480 481 482
      ChkError(savefile_text_uri_or_url(idb->self_id));
      ChkError(savefile_toggle_uri_or_url(idb->self_id));
      ChkError(savefile_auto_width(idb->self_id));
483 484 485 486 487 488 489 490 491 492

      /* Remember various details about the dialogue's source */

      savefile_browser = b;
      savefile_token   = link;

      ok = 1;
    }
    break;

493
    case ExportPicture:
494
    {
495 496 497 498
      HStream    * image = menus_document_opened_over();
      const char * src   = NULL;

      ChkError(savefile_switch_to_radios(idb->self_id));
499
      ChkError(savefile_set_items(idb->self_id, alt_ep));
500 501 502 503 504 505 506 507 508 509

      if (
           !image ||
           !
           (
             (image->style & IMG) ||
             (
               image->tagno == TAG_INPUT &&
               HtmlINPUTtype(image) == inputtype_IMAGE
             )
510 511
             ||
             (
512
               ISOBJECT(image) &&
513 514
               object_token_is_image(b, image)
             )
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
           )
         )
      {
        #ifndef TRACE

          return 0;

        #else

          erb.errnum = Utils_Error_Custom_Normal;

          sprintf(erb.errmess,
                  "Menu token %p is not an image in savefile_to_be_shown",
                  image);

          show_error_ret(&erb);

          return 0;

        #endif
      }

537 538 539
      if (image->style & IMG)             src = image->src;
      else if (image->tagno == TAG_INPUT) src = HtmlINPUTsrc(image);
      else                                src = HtmlOBJECTdata(image);
540

541 542 543 544
      ChkError(savefile_set_leafname_from_url(idb->self_id, SaveFileWrit, (char *) src));
      ChkError(savefile_text_sprite_or_original(idb->self_id));
      ChkError(savefile_toggle_sprite_or_original(idb->self_id, src));
      ChkError(savefile_auto_width(idb->self_id));
545 546 547 548 549 550 551 552 553 554

      /* Remember various details about the dialogue's source */

      savefile_browser = b;
      savefile_token   = image;

      ok = 1;
    }
    break;

555
    case ExportBackground:
556
    {
557 558 559
      char src[Limits_URL];

      ChkError(savefile_switch_to_radios(idb->self_id));
560
      ChkError(savefile_set_items(idb->self_id, alt_eb));
561

562
      if (b->background_image == -1)
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
      {
        #ifndef TRACE

          return 0;

        #else

          erb.errnum = Utils_Error_Custom_Normal;

          StrNCpy0(erb.errmess,
                   "There is no background image on this page in savefile_to_be_shown");

          show_error_ret(&erb);

          return 0;

        #endif
      }

582 583 584 585 586 587 588 589 590
      *src = 0;
      image_get_background_image_url(b, src, sizeof(src));

      if (!*src) ChkError(savefile_set_leafname         (idb->self_id, SaveFileWrit, lookup_token("BackName:Background",0,0)));
      else       ChkError(savefile_set_leafname_from_url(idb->self_id, SaveFileWrit, src));

      ChkError(savefile_text_sprite_or_original(idb->self_id));
      ChkError(savefile_toggle_sprite_or_original(idb->self_id, src));
      ChkError(savefile_auto_width(idb->self_id));
591 592 593 594 595

      savefile_browser = b;
      savefile_token   = NULL;

      ok = 1;
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 621 622 623 624 625 626 627
    }
    break;

    #ifdef TRACE

      default:
      {
        erb.errnum = 0;
        StrNCpy0(erb.errmess,
                 "Save dialogue origin not understood in savefile_to_be_shown");

        show_error_ret(&erb);

        return 0;
      }
      break;

    #endif
  }

  /* If we were successful, install relevant event handlers etc., assuming */
  /* this is the first time the dialogue was opened.                       */

  if (ok)
  {
    parent_component   = pc;
    ancestor_id        = b->self_id;

    if (first_time)
    {
      window_id = idb->self_id;

628 629 630 631 632
      ChkError(event_register_toolbox_handler(window_id, Draggable_DragEnded, savefile_drag_ended,     NULL));
      ChkError(event_register_toolbox_handler(window_id, ESaveFileOK,         savefile_ok,             NULL));
      ChkError(event_register_toolbox_handler(window_id, ESaveFileCancel,     savefile_cancel,         NULL));
      ChkError(event_register_toolbox_handler(window_id, ESaveFileOption,     savefile_option_changed, NULL));
      ChkError(event_register_toolbox_handler(window_id, ESaveFileRadio,      savefile_radio_changed,  NULL));
633 634 635 636 637 638 639
    }
  }

  return 1;
}

/*************************************************/
640
/* savefile_set_leafname()                       */
641 642
/*                                               */
/* Sets the leafname in the Save File dialogue   */
643 644 645 646 647
/* to a given value, preserving whatever path    */
/* component may already have been present.      */
/* Any last save path recorded by the save       */
/* routines in Save.c will take precedence over  */
/* an existing dialogue path, though.            */
648 649 650 651 652 653
/*                                               */
/* Parameters: Object ID of the dialogue;        */
/*                                               */
/*             Component ID of the writable      */
/*             gadget;                           */
/*                                               */
654
/*             Pointer to a null terminated leaf */
655 656 657
/*             to work with.                     */
/*************************************************/

658
_kernel_oserror * savefile_set_leafname(ObjectId object, ComponentId component, char * leaf)
659
{
660 661 662
  char       * dot;
  char         path[Limits_OS_Pathname];
  const char * last;
663

664
  if (!leaf) return NULL;
665

666
  /* Is there an existing last pathname we should use? */
667

668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
  last = save_return_last_path();

  if (last && *last)
  {
    StrNCpy0(path, last);
  }
  else
  {
    /* See what filename is already in the dialogue */

    path[0] = 0; /* Important so that when we strcat the leafname later, */
                 /* things will work even if the writable holds no text. */

    RetError(writablefield_get_value(0,
                                     object,
                                     component,
                                     path,
                                     sizeof(path),
                                     NULL));
    path[sizeof(path) - 1] = 0;
  }
689 690

  /* If there's a leafname here, force a terminator in place of the '.' */
691
  /* - otherwise, want to only have the leaf, so clear the string.      */
692 693 694 695

  dot = strrchr(path, '.');

  if (dot) *(++dot) = 0;
696
  else *path = 0;
697

698
  /* Can we fit the leaf in? */
699

700 701 702 703 704 705 706 707
  if (strlen(path) + strlen(leaf) + 1 > sizeof(path))
  {
    /* No, can we just fit in the leaf? If not, leave */
    /* everything alone...                            */

    if (strlen(leaf) + 1 <= sizeof(path)) strcpy(path, leaf);
  }
  else strcat(path, leaf);
708 709 710 711 712 713 714 715 716

  /* Set the value */

  return writablefield_set_value(0,
                                 object,
                                 component,
                                 path);
}

717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
/*************************************************/
/* savefile_set_leafname_from_url()              */
/*                                               */
/* As savefile_set_leafname, but works out the   */
/* leaf from a given URL.                        */
/*                                               */
/* Parameters: Object ID of the dialogue;        */
/*                                               */
/*             Component ID of the writable      */
/*             gadget;                           */
/*                                               */
/*             Pointer to a null terminated URL  */
/*             to work with.                     */
/*************************************************/

_kernel_oserror * savefile_set_leafname_from_url(ObjectId object, ComponentId component, char * url)
{
  char leaf[Limits_OS_Pathname];

  /* Get a leafname */

  urlutils_leafname_from_url(url, leaf, sizeof(leaf));

  /* Use it */

  return savefile_set_leafname(object, component, leaf);
}

745 746 747 748 749 750
/*************************************************/
/* savefile_set_filetype()                       */
/*                                               */
/* Sets the sprite of the draggable object in    */
/* the Save File dialogue according to the given */
/* filetype, and records that filetype in        */
751
/* savefile_type (unless asked not to).          */
752 753 754 755 756 757 758 759 760
/*                                               */
/* If the sprite cannot be found in the Wimp     */
/* sprite pool, 'file_xxx' is used instead.      */
/*                                               */
/* Parameters: Object ID of the dialogue;        */
/*                                               */
/*             Component ID of the draggable     */
/*             gadget;                           */
/*                                               */
761 762 763 764 765 766 767 768 769 770 771 772
/*             The filetype;                     */
/*                                               */
/*             1 to *not* set savefile_type to   */
/*             the filetype - this would be used */
/*             if (for example) this function    */
/*             was being called for a dialogue   */
/*             similar to, but not the same as   */
/*             the Save File dialogue (e.g. Save */
/*             Object, where several can exist   */
/*             at once); if for a 'genuine' Save */
/*             File dialogue, you *must* pass    */
/*             zero here.                        */
773 774
/*************************************************/

775
_kernel_oserror * savefile_set_filetype(ObjectId object, ComponentId component, int type, int dont_record)
776 777 778 779
{
  char sprite[Limits_OS_SpriteName];
  int  len;

780 781 782
  if (dont_record || savefile_type != type)
  {
    if (!dont_record) savefile_type = type;
783

784
    /* Will it fit in the buffer? */
785

786
    len = utils_len_printf("file_%03x", type);
787

788
    /* If so, build the sprite name and tell the gadget to use that sprite */
789

790 791 792
    if (len < sizeof(sprite))
    {
      sprintf(sprite, "file_%03x", type);
793

794 795 796
      if (
           !_swix(Wimp_SpriteOp,
                  _IN(0) | _IN(2),
797

798 799 800 801 802
                  0x18, /* Select sprite */
                  sprite)
         )
      {
        /* If it has been found, use this sprite */
803

804 805 806 807 808 809 810 811
        return draggable_set_sprite(0,
                                    object,
                                    component,
                                    sprite);
      }
      else
      {
        /* Otherwise use the generic 'file_xxx' instead */
812

813 814 815 816 817
        return draggable_set_sprite(0,
                                    object,
                                    component,
                                    "file_xxx");
      }
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
    }
  }

  return NULL;
}

/*************************************************/
/* savefile_drag_ended()                         */
/*                                               */
/* Handle Draggable_DragEnded events from the    */
/* Save File dialogue draggable sprite gadget.   */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

static int savefile_drag_ended(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  DraggableDragEndedEvent * drag   = (DraggableDragEndedEvent *) event;
  protocols_saving          saving = protocols_saving_nothing;
838
  int                       size   = 4096; /* More or less arbitrary */
839
  int                       alt    = savefile_alternative_selected();
840 841 842 843
  WimpGetPointerInfoBlock   info;
  int                       window_handle;
  char                      path[Limits_OS_Pathname];
  char                    * leaf;
844
  void                    * extra  = savefile_token;
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876

  /* If the user dragged back to the save dialogue, do nothing */

  ChkError(window_get_wimp_handle(0,
                                  window_id,
                                  &window_handle));

  if (window_handle == drag->window_handle) return 1;

  /* Get the pathname from the Save File dialogue. */

  ChkError(writablefield_get_value(0,
                                   window_id,
                                   SaveFileWrit,
                                   path,
                                   sizeof(path),
                                   NULL));

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

  /* Point to the leafname component */

  leaf = strrchr(path, '.');
  if (!leaf) leaf = path;
  else       leaf ++;

  /* Work out the estimated data size */

  switch (parent_component)
  {
    /* Size of an HTML file */

877 878
    case FileSaveFrame:
    case FileSaveParent:
879
    case FileSaveAncestor:
880 881 882 883 884 885 886 887
    {
      saving = protocols_saving_document_source;

      if (is_known_browser(savefile_browser)) size = save_source_size(savefile_browser);
      else                                    size = 0;
    }
    break;

888
    /* Size of a URI file for a location */
889

890 891 892
    case FileSaveFrameLocation:
    case FileSaveParentLocation:
    case FileSaveAncestorLocation:
893
    {
894 895
      char * url   = browser_current_url  (savefile_browser);
      char * title = browser_current_title(savefile_browser);
896 897 898

      if (!url) url = " ";

899
      saving = protocols_saving_frame_location;
900
      size   = save_uri_size(url, title, alt);
901 902 903
    }
    break;

904 905 906 907 908 909 910 911 912 913 914
    /* Size of a text file */

    case ExportAsText:
    {
      saving = protocols_saving_document_as_text;

      if (is_known_browser(savefile_browser)) size = savetext_text_size(savefile_browser);
      else                                    size = 0;
    }
    break;

915 916 917 918 919 920 921 922 923 924 925
    /* Size of a Draw file */

    case ExportAsDraw:
    {
      saving = protocols_saving_document_as_draw;

      if (is_known_browser(savefile_browser)) size = savedraw_draw_size(savefile_browser, alt);
      else                                    size = 0;
    }
    break;

926 927 928 929 930 931
    /* Size of a URI file */

    case ExportLink:
    {
      saving = protocols_saving_link;

932
      if (savefile_token && savefile_token->anchor) size = save_uri_size(savefile_token->anchor, NULL, alt);
933 934 935 936 937 938
      else                                          size = 0;
    }
    break;

    /* Size of an image to export */

939
    case ExportPicture:
940 941 942
    {
      saving = protocols_saving_image_sprite;

943 944 945 946 947
      if (savefile_token && is_known_browser(savefile_browser))
      {
        if (!alt) size = image_sprite_size(savefile_browser, savefile_token);
      }
      else size = 0;
948
    }
949 950 951 952 953 954 955 956
    break;

    /* Save a background image */

    case ExportBackground:
    {
      saving = protocols_saving_image_sprite;

957 958 959 960 961
      if (is_known_browser(savefile_browser))
      {
        if (!alt) size = image_sprite_size(savefile_browser, NULL);
      }
      else size = 0;
962 963 964 965 966
    }
    break;

    #ifndef REMOTE_HOTLIST

967
      /* File size of the entire hotlist */
968 969 970 971 972 973 974

      case HotlistSaveHotlist:
      {
        saving = protocols_saving_entire_hotlist;
      }
      break;

975 976
      /* Size of an object from the hotlist - a URL, directory, */
      /* or general selection                                   */
977 978 979 980 981 982 983 984 985 986 987 988

      case MiscHotlistSaveObject:
      {
        hotlist_item * source = hotlist_find_selected_item();
        unsigned int   items  = hotlist_count_selected_items();

        if (items && source)
        {
          if (items == 1 && source->type == hl_url)
          {
            saving = protocols_saving_hotlist_entry;
            extra  = (void *) source;
989
            size   = save_uri_size(source->data.url, source->name, alt);
990 991 992 993 994 995 996
          }
          else saving = protocols_saving_hotlist_selection;
        }
      }
      break;

    #endif
997

998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
    case HistorySaveLocal:
    {
      saving = protocols_saving_local_history;
      size   = 0;
    }
    break;

    case HistorySaveGlobal:
    {
      saving = protocols_saving_global_history;
      size   = 0;
    }
    break;

1012
    /* For others, could leave size as 4096 - *but must set 'saving' appropriately* */
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
  }

  /* Send out the DataSave message */

  info.x             = drag->x;
  info.y             = drag->y;
  info.window_handle = drag->window_handle;
  info.icon_handle   = drag->icon_handle;

  ChkError(protocols_atats_send_data_save(savefile_browser,
1023
                                          extra,
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
                                          leaf,
                                          size,
                                          savefile_type,
                                          saving,
                                          &info));
  return 1;
}

/*************************************************/
/* savefile_ok()                                 */
/*                                               */
/* Handles clicks on the 'OK' button in the      */
/* Save File dialogue.                           */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

static int savefile_ok(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
1044 1045 1046
  _kernel_oserror * e   = NULL;
  int               ok  = 0;
  int               alt = savefile_alternative_selected();
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
  char            * leaf;
  char              path[Limits_OS_Pathname];

  /* Get the pathname from the Save File dialogue. */

  ChkError(writablefield_get_value(0,
                                   window_id,
                                   SaveFileWrit,
                                   path,
                                   sizeof(path),
                                   NULL));

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

  /* Is this fully specified? */

  leaf = strrchr(path, '.');

  if (!leaf)
  {
    StrNCpy0(erb.errmess,
             lookup_token("GivePath:To save, drag the file icon to a directory viewer",
                          0,
                          0));

    erb.errnum = Utils_Error_Custom_Message;

    ChkError(&erb);
  }

  /* Save the file */

  switch (parent_component)
  {
    /* Save as HTML */

    case FileSaveFrame:
    case FileSaveParent:
1085
    case FileSaveAncestor:
1086 1087 1088 1089 1090 1091 1092 1093 1094
    {
      if (is_known_browser(savefile_browser))
      {
        e = save_save_source(path, savefile_browser);
        if (!e) ok = 1;
      }
    }
    break;

1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
    /* Save as text */

    case ExportAsText:
    {
      if (is_known_browser(savefile_browser))
      {
        e = savetext_save_text(savefile_browser, path);
        if (!e) ok = 1;
      }
    }
    break;

1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
    /* Save as Draw */

    case ExportAsDraw:
    {
      if (is_known_browser(savefile_browser))
      {
        e = savedraw_save_draw(savefile_browser, path, alt);
        if (!e) ok = 1;
      }
    }
    break;

1119 1120 1121
    /* Save the current location or a link as a URI or URL file */

    case ExportLink:
1122 1123 1124
    case FileSaveFrameLocation:
    case FileSaveParentLocation:
    case FileSaveAncestorLocation:
1125 1126 1127 1128 1129
    {
      if (is_known_browser(savefile_browser))
      {
        if (savefile_token)
        {
1130 1131
          char * title = browser_current_title(savefile_browser);

1132 1133
          /* Save a link as a URI file */

1134
          e = save_save_uri(path, savefile_token->anchor, title, alt);
1135 1136 1137 1138 1139 1140
          if (!e) ok = 1;
        }
        else
        {
          /* Save the current location as a URI file */

1141 1142
          char * url   = browser_current_url  (savefile_browser);
          char * title = browser_current_title(savefile_browser);
1143 1144 1145

          if (!url) url = " ";

1146
          e = save_save_uri(path, url, title, alt);
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
          if (!e) ok = 1;
        }
      }
    }
    break;

    /* Export a foreground image */

    case ExportPicture:
    {
      if (is_known_browser(savefile_browser))
      {
1159 1160 1161
        if (!alt) e = image_export_sprite  (path, savefile_browser, savefile_token);
        else      e = image_export_original(path, savefile_browser, savefile_token);

1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
        if (!e) ok = 1;
      }
    }
    break;

    /* Export a background image */

    case ExportBackground:
    {
      if (is_known_browser(savefile_browser))
      {
1173 1174 1175
        if (!alt) e = image_export_sprite  (path, savefile_browser, NULL);
        else      e = image_export_original(path, savefile_browser, NULL);

1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
        if (!e) ok = 1;
      }
    }
    break;

    #ifndef REMOTE_HOTLIST

      /* Save the hotlist */

      case HotlistSaveHotlist:
      {
1187
        e = hotlist_save_hotlist(path, NULL, 0);
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
        if (!e) ok = 1;
      }
      break;

      /* Save an object from the hotlist - a URL, directory, */
      /* or general selection                                */

      case MiscHotlistSaveObject:
      {
        hotlist_item * source = hotlist_find_selected_item();
        unsigned int   items  = hotlist_count_selected_items();

        if (items && source)
        {
          if (items == 1 && source->type == hl_url)
          {
1204
            e = save_save_uri(path, source->data.url, source->name, alt);
1205 1206 1207 1208 1209 1210 1211 1212 1213

            if (!e)
            {
              ok = 1;
              e = hotlist_clear_selection();
            }
          }
          else
          {
1214
            e = hotlist_save_hotlist(path, NULL, 1);
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

            if (!e)
            {
              ok = 1;
              e = hotlist_clear_selection();
            }
          }
        }
      }
      break;

    #endif

    case HistorySaveLocal:
    {
      browser_data * local_browser = savefile_browser;

      if (!is_known_browser(savefile_browser)) local_browser = NULL;

      e = history_save_as_html(path, local_browser);
      if (!e) ok = 1;
    }
    break;

    case HistorySaveGlobal:
    {
      e = history_save_as_html(path, NULL);
      if (!e) ok = 1;
    }
    break;
  }

  /* If everything is OK, close the menu */

  if (!e)
  {
    ChkError(savefile_close(0,1)); /* Make sure we've tidied up */

    _swix(Wimp_CreateMenu,
          _IN(1),

          -1);
  }

  /* Finished */

  if (e) ChkError(e);

1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
  return 1;
}

/*************************************************/
/* savefile_cancel()                             */
/*                                               */
/* Handles clicks on the 'Cancel' button in the  */
/* Save File dialogue                            */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

static int savefile_cancel(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  /* If we were fetching, stop the fetch */

  if (savefile_browser && is_known_browser(savefile_browser))
  {
1282
    if (savefile_browser->save_link) fetch_stop(savefile_browser, 0);
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
  }

  /* We don't do anything sophisticated, like */
  /* restoring previous options here, as the  */
  /* dialogue is too simple for it to be      */
  /* worthwhile. Therefore, just close it.    */

  ChkError(savefile_close(0,0));

  return 1;
}

/*************************************************/
/* savefile_close()                              */
/*                                               */
/* If the Save File dialogue is opened, this     */
1299
/* will close it.                                */
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
/*                                               */
/* Parameters: An object ID, or 0. If not zero,  */
/*             the ID must match the ancestor    */
/*             recorded when the dialogue was    */
/*             opened or no action is taken.     */
/*                                               */
/*             0 to close the dialogue, 1 to do  */
/*             everything except that.           */
/*************************************************/

_kernel_oserror * savefile_close(ObjectId ancestor, int do_not_close)
{
  _kernel_oserror * e = NULL;

  if (!window_id) return NULL;
  if (ancestor && ancestor != ancestor_id) return NULL;

  /* If required, close the dialogue */

  if (!do_not_close && !e)
  {
    /* If the dialogue came from a menu tree, collapse the tree */

    if (parent_component != -1) e = _swix(Wimp_CreateMenu, _IN(1), -1);

    /* Close the dialogue */

    if (!e) e = toolbox_hide_object(0, window_id);
  }

  parent_component = -1;
  ancestor_id      = 0;

  savefile_browser = NULL;
  savefile_token   = NULL;
  savefile_type    = 0x000;

  return e;
}

/*************************************************/
/* savefile_return_dialogue_info()               */
/*                                               */
/* Returns information on the Save File dialogue */
/* and its ancestor.                             */
/*                                               */
/* Parameters: Pointer to an ObjectId, in which  */
/*             the ID of the dialogue is placed; */
/*                                               */
/*             Pointer to an ObjectId, in which  */
/*             the ID of the ancestor window is  */
/*             placed.                           */
/*                                               */
/* Returns:    See parameters list, and note     */
/*             that the returned values will be  */
/*             0 and 0 if the Save File dialogue */
/*             is closed.                        */
/*                                               */
/* Assumes:    Either pointer may be NULL.       */
/*************************************************/

void savefile_return_dialogue_info(ObjectId * window, ObjectId * ancestor)
{
  if (window)   *window   = window_id;
  if (ancestor) *ancestor = ancestor_id;
}
1366

1367 1368 1369
/*************************************************/
/* savefile_switch_to_normal()                   */
/*                                               */
1370 1371 1372 1373 1374
/* Move the radio buttons or option buttons from */
/* view in the Save File dialogue, if either is  */
/* present.                                      */
/*                                               */
/* Parameters: Object ID of the dialogue.        */
1375
/*************************************************/
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457

static _kernel_oserror * savefile_switch_to_normal(ObjectId window)
{
  BBox opt, ra1, ra2;
  int  showing = 0;
  int  noopt   = 0;
  int  norad   = 0;
  int  moveby;

  /* Find the option button and/or radio gadgets */

  if (gadget_get_bbox(0, window, SaveFileOption, &opt)) noopt = 1;
  if (gadget_get_bbox(0, window, SaveFileRadio1, &ra1)) norad = 1;
  if (gadget_get_bbox(0, window, SaveFileRadio2, &ra2)) norad = 1;

  if      (!noopt && opt.xmin < opt.xmax - opt.xmin) showing = 1;
  else if (!norad && ra1.xmin < ra1.xmax - ra1.xmin) showing = 2;

  /* If 'showing' is zero, we're on the normal display already */

  if (!showing) goto savefile_switch_to_normal_set_extent;

  /* Otherwise, need to move things about a bit */

  if (showing == 1)
  {
    /* We're on the option button display */

    moveby = (opt.xmax - opt.xmin) * 2;

    opt.xmin += moveby;
    opt.xmax += moveby;

    RetError(gadget_move_gadget(0, window, SaveFileOption, &opt));
  }
  else
  {
    /* We're on the radio buttons display */

    moveby = (ra1.xmax - ra1.xmin) * 2;

    ra1.xmin += moveby;
    ra1.xmax += moveby;
    ra2.xmin += moveby;
    ra2.xmax += moveby;

    RetError(gadget_move_gadget(0, window, SaveFileRadio1, &ra1));
    RetError(gadget_move_gadget(0, window, SaveFileRadio2, &ra2));
  }

  /* Move the Y coordinates of the Cancel and Save buttons back */
  /* to the normal position. First, Cancel.                     */

  RetError(gadget_get_bbox(0, window, SaveFileCancel,           &ra1));
  RetError(gadget_get_bbox(0, window, SaveFileCancelMarkNormal, &ra2));

  ra1.ymin = ra2.ymin;
  ra1.ymax = ra2.ymax;

  RetError(gadget_move_gadget(0, window, SaveFileCancel, &ra1));

savefile_switch_to_normal_set_extent:

  /* Now, OK (or 'Save' or whatever) */

  RetError(gadget_get_bbox(0, window, SaveFileOK,           &ra1));
  RetError(gadget_get_bbox(0, window, SaveFileOKMarkNormal, &ra2));

  ra1.ymin = ra2.ymin;
  ra1.ymax = ra2.ymax;

  RetError(gadget_move_gadget(0, window, SaveFileOK, &ra1));

  /* Set the extent */

  RetError(savefile_set_correct_extent(window, &ra1));

  /* Finished */

  return NULL;
}

1458 1459 1460
/*************************************************/
/* savefile_switch_to_option()                   */
/*                                               */
1461 1462 1463 1464 1465
/* Move the option button into the Save File     */
/* dialogue, moving out the radio buttons if     */
/* present.                                      */
/*                                               */
/* Parameters: Object ID of the dialogue.        */
1466 1467
/*************************************************/

1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 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 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
static _kernel_oserror * savefile_switch_to_option(ObjectId window)
{
  BBox opt, ra1, ra2, wri;
  int  showing = 0;
  int  noopt   = 0;
  int  norad   = 0;
  int  moveby;

  /* Find the writable gadget */

  RetError(gadget_get_bbox(0, window, SaveFileWrit, &wri));

  /* Find the option button and/or radio gadgets */

  if (gadget_get_bbox(0, window, SaveFileOption, &opt)) noopt = 1;
  if (gadget_get_bbox(0, window, SaveFileRadio1, &ra1)) norad = 1;
  if (gadget_get_bbox(0, window, SaveFileRadio2, &ra2)) norad = 1;

  /* If there are no option buttons, try switching to the */
  /* radios instead.                                      */

  if (noopt)
  {
    if (norad) return NULL;
    else       return savefile_switch_to_radios(window);
  }

  if      (!noopt && opt.xmin < opt.xmax - opt.xmin) showing = 1;
  else if (!norad && ra1.xmin < ra1.xmax - ra1.xmin) showing = 2;

  /* If 'showing' is 1, we're on the option display already */

  if (showing == 1) goto savefile_switch_to_option_set_extent;

  /* Move back to the normal display as a starting point */

  RetError(savefile_switch_to_normal(window));

  /* Move the option button to the same xmin as the writable */

  moveby = opt.xmin - wri.xmin;

  opt.xmin -= moveby;
  opt.xmax -= moveby;

  RetError(gadget_move_gadget(0, window, SaveFileOption, &opt));

  /* Move the Y coordinates of the Cancel and Save buttons */
  /* to the option position. First, Cancel.                */

  RetError(gadget_get_bbox(0, window, SaveFileCancel,           &ra1));
  RetError(gadget_get_bbox(0, window, SaveFileCancelMarkOption, &ra2));

  ra1.ymin = ra2.ymin;
  ra1.ymax = ra2.ymax;

  RetError(gadget_move_gadget(0, window, SaveFileCancel, &ra1));

savefile_switch_to_option_set_extent:

  /* Now, OK (or 'Save' or whatever) */

  RetError(gadget_get_bbox(0, window, SaveFileOK,           &ra1));
  RetError(gadget_get_bbox(0, window, SaveFileOKMarkOption, &ra2));

  ra1.ymin = ra2.ymin;
  ra1.ymax = ra2.ymax;

  RetError(gadget_move_gadget(0, window, SaveFileOK, &ra1));

  /* Set the extent */

  RetError(savefile_set_correct_extent(window, &ra1));

  /* Finished */

  return NULL;
}

1547 1548 1549
/*************************************************/
/* savefile_switch_to_radios()                   */
/*                                               */
1550 1551 1552 1553 1554
/* Move the radio buttons into the Save File     */
/* dialogue, moving out the option button if     */
/* present.                                      */
/*                                               */
/* Parameters: Object ID of the dialogue.        */
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
static _kernel_oserror * savefile_switch_to_radios(ObjectId window)
{
  BBox opt, ra1, ra2, wri;
  int  showing = 0;
  int  noopt   = 0;
  int  norad   = 0;
  int  moveby;

  /* Find the writable gadget */

  RetError(gadget_get_bbox(0, window, SaveFileWrit, &wri));

  /* Find the option button and/or radio gadgets */

  if (gadget_get_bbox(0, window, SaveFileOption, &opt)) noopt = 1;
  if (gadget_get_bbox(0, window, SaveFileRadio1, &ra1)) norad = 1;
  if (gadget_get_bbox(0, window, SaveFileRadio2, &ra2)) norad = 1;

  /* If there are no radio buttons, try switching to the option */
  /* button instead.                                            */

  if (norad)
  {
    if (noopt) return NULL;
    else       return savefile_switch_to_option(window);
  }

  if      (!noopt && opt.xmin < opt.xmax - opt.xmin) showing = 1;
  else if (!norad && ra1.xmin < ra1.xmax - ra1.xmin) showing = 2;

  /* If 'showing' is 2, we're on the radios display already */

  if (showing == 2) goto savefile_switch_to_radios_set_extent;

  /* Move back to the normal display as a starting point */

  RetError(savefile_switch_to_normal(window));

  /* Move the radio buttons to the same xmin as the writable */

  moveby = ra1.xmin - wri.xmin;

  ra1.xmin -= moveby;
  ra1.xmax -= moveby;

  moveby = ra2.xmin - wri.xmin;

  ra2.xmin -= moveby;
  ra2.xmax -= moveby;

  RetError(gadget_move_gadget(0, window, SaveFileRadio1, &ra1));
  RetError(gadget_move_gadget(0, window, SaveFileRadio2, &ra2));

  /* Move the Y coordinates of the Cancel and Save buttons */
  /* to the radios position. First, Cancel.                */

  RetError(gadget_get_bbox(0, window, SaveFileCancel,           &ra1));
  RetError(gadget_get_bbox(0, window, SaveFileCancelMarkRadios, &ra2));

  ra1.ymin = ra2.ymin;
  ra1.ymax = ra2.ymax;

  RetError(gadget_move_gadget(0, window, SaveFileCancel, &ra1));

savefile_switch_to_radios_set_extent:

  /* Now, OK (or 'Save' or whatever) */

  RetError(gadget_get_bbox(0, window, SaveFileOK,           &ra1));
  RetError(gadget_get_bbox(0, window, SaveFileOKMarkRadios, &ra2));

  ra1.ymin = ra2.ymin;
  ra1.ymax = ra2.ymax;

  RetError(gadget_move_gadget(0, window, SaveFileOK, &ra1));

  /* Set the extent */

  RetError(savefile_set_correct_extent(window, &ra1));

  /* Finished */

  return NULL;
}

1642 1643 1644
/*************************************************/
/* savefile_set_items()                          */
/*                                               */
1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
/* Set the radio buttons or option button in a   */
/* Save File dialogue, if present, to the given  */
/* state.                                        */
/*                                               */
/* Parameters: Object ID of the dialogue;        */
/*                                               */
/*             1 to tick the option box or       */
/*             select the second of the two      */
/*             radios - 0 to untick the option   */
/*             box or select the first of the    */
/*             two radios.                       */
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
/*************************************************/

static _kernel_oserror * savefile_set_items(ObjectId window, int selected)
{
  optionbutton_set_state(0, window, SaveFileOption, selected);

  radiobutton_set_state (0, window, SaveFileRadio1, !selected);
  radiobutton_set_state (0, window, SaveFileRadio2, selected);

  return NULL;
}

/*************************************************/
/* savefile_option_changed()                     */
/*                                               */
1671 1672 1673 1674 1675
/* Called when the option button in a Save File  */
/* dialogue (if any) is toggled.                 */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
1676 1677
/*************************************************/

1678 1679 1680 1681 1682
static int savefile_option_changed(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  return savefile_item_changed(idb);
}

1683 1684 1685
/*************************************************/
/* savefile_radio_changed()                      */
/*                                               */
1686 1687 1688 1689 1690
/* Called when the radio buttons in a Save File  */
/* dialogue (if any) are toggled.                */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
1691 1692
/*************************************************/

1693 1694 1695 1696 1697
static int savefile_radio_changed(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  return savefile_item_changed(idb);
}

1698 1699 1700
/*************************************************/
/* savefile_item_changed()                       */
/*                                               */
1701 1702 1703 1704 1705 1706 1707
/* Called when either one of the two radios that */
/* can appear in a Save File dialogue, or the    */
/* option button that can appear, is used.       */
/*                                               */
/* Parameters: Pointer an IdBlock from the       */
/*             Toolbox event that was raised by  */
/*             the button(s).                    */
1708 1709
/*************************************************/

1710 1711 1712
static int savefile_item_changed(IdBlock * idb)
{
  browser_data * b;
1713 1714
  ComponentId    pc  = idb->parent_component;
  int            alt = savefile_alternative_selected();
1715 1716 1717 1718 1719

  /* Where did we come from? */

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

1720 1721 1722 1723 1724 1725 1726 1727 1728 1729
//  /* If the parent ID is the same as a toolbar, this was */
//  /* from a toolbar button - in which case, use a parent */
//  /* ID of FileSaveParent, as that is what we actually    */
//  /* want to do.                                         */
//
//  if (
//       idb->parent_id == toolbars_get_upper(b) ||
//       idb->parent_id == toolbars_get_lower(b)
//     )
//     pc = FileSaveParent;
1730 1731 1732 1733 1734 1735 1736

  /* Actions will vary according to the parent component */

  switch (pc)
  {
    case FileSaveFrame:
    case FileSaveParent:
1737
    case FileSaveAncestor:
1738 1739 1740 1741 1742 1743 1744 1745
    case HotlistSaveHotlist:
    case HistorySaveLocal:
    case HistorySaveGlobal:
    case ExportAsText:
    {
      /* Nothing to do at the moment, shouldn't be able to access */
      /* the option button gadget anyway...                       */

1746 1747 1748 1749 1750
#ifndef REMOTE_HOTLIST

  savefile_item_changed_gripe:

#endif
1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767

      #ifdef TRACE

        erb.errnum = Utils_Error_Custom_Normal;

        sprintf(erb.errmess,
                "Shouldn't be using the option button in SaveFile dialogue for parent component 0x%x in savefile_item_changed!",
                pc);

        show_error_ret(&erb);

      #endif

      return 1;
    }
    break;

1768
    #ifndef REMOTE_HOTLIST
1769

1770
      case MiscHotlistSaveObject:
1771
      {
1772 1773
        hotlist_item * source = hotlist_find_selected_item();
        unsigned int   items  = hotlist_count_selected_items();
1774

1775 1776 1777
        alt_mhso = alt;

        if (items && source)
1778
        {
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
          if (items == 1 && source->type == hl_url)
          {
            /* Saving a single hotlist item, so can do this as */
            /* a URI or ANT URL file.                          */

            ChkError(savefile_toggle_uri_or_url(idb->self_id));
          }
          else
          {
            /* Saving multiple items as HTML - no defined action */
            /* for the option button.                            */
1790

1791 1792
            goto savefile_item_changed_gripe;
          }
1793 1794
        }
      }
1795 1796 1797
      break;

    #endif
1798

1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809
    case ExportAsDraw:
    {
      alt_ead = alt;

      ChkError(savefile_toggle_without_backgrounds_or_with(idb->self_id));
    }
    break;

    case FileSaveFrameLocation:
    case FileSaveParentLocation:
    case FileSaveAncestorLocation:
1810 1811
    case ExportLink:
    {
1812 1813 1814
      if (pc == ExportLink) alt_el   = alt;
      else                  alt_fsfl = alt;

1815 1816 1817 1818 1819 1820 1821 1822 1823
      ChkError(savefile_toggle_uri_or_url(idb->self_id));
    }
    break;

    case ExportPicture:
    {
      HStream    * image = menus_document_opened_over();
      const char * src   = NULL;

1824 1825
      alt_ep = alt;

1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837
      if (image->style & IMG)             src = image->src;
      else if (image->tagno == TAG_INPUT) src = HtmlINPUTsrc(image);
      else                                src = HtmlOBJECTdata(image);

      ChkError(savefile_toggle_sprite_or_original(idb->self_id, src));
    }
    break;

    case ExportBackground:
    {
      char src[Limits_URL];

1838 1839
      alt_eb = alt;

1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886
      if (b->background_image == -1)
      {
        #ifndef TRACE

          return 0;

        #else

          erb.errnum = Utils_Error_Custom_Normal;

          StrNCpy0(erb.errmess,
                   "There is no background image on this page in savefile_item_changed.");

          show_error_ret(&erb);

          return 0;

        #endif
      }

      *src = 0;
      image_get_background_image_url(b, src, sizeof(src));

      ChkError(savefile_toggle_sprite_or_original(idb->self_id, src));
    }
    break;

    #ifdef TRACE

      default:
      {
        erb.errnum = 0;
        StrNCpy0(erb.errmess,
                 "Save dialogue origin not understood in savefile_item_changed.");

        show_error_ret(&erb);

        return 0;
      }
      break;

    #endif
  }

  return 1;
}

1887 1888 1889
/*************************************************/
/* savefile_alternative_selected()               */
/*                                               */
1890 1891 1892 1893 1894 1895 1896 1897 1898 1899
/* If the second of the two radios in a Save     */
/* File dialogue is selected, or if there is     */
/* an option button present instead and it is    */
/* itself selected, returns 1. Otherwise returns */
/* 0.                                            */
/*                                               */
/* Returns:    See above.                        */
/*                                               */
/* Assumes:    window_id holds the Object ID of  */
/*             a relevant Save file dialogue.    */
1900 1901
/*************************************************/

1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946
int savefile_alternative_selected(void)
{
  int  alt_set    = 0;
  int  try_radios = 0;
  BBox box;

  if (!window_id) return 0;

  /* Need to work out whether the option button or radio buttons */
  /* are present, and if so, which is visible at the moment and  */
  /* use that.                                                   */

  if (!gadget_get_bbox(0, window_id, SaveFileOption, &box))
  {
    /* If we can get the BBox, is it visible right now? */

    if (box.xmin >= box.xmax - box.xmin) try_radios = 1; /* No, so signal we should try the radios */
    else
    {
      /* Get the option button state */

      if (optionbutton_get_state(0, window_id, SaveFileOption, &alt_set)) alt_set = 0;
    }
  }

  /* Couldn't get the option button BBox, so try the radios */

  else try_radios = 1;

  /* If required, do similar things to the above with the radio buttons */

  if (try_radios)
  {
    if (!gadget_get_bbox(0, window_id, SaveFileRadio2, &box))
    {
      if (box.xmin < box.xmax - box.xmin)
      {
        if (radiobutton_get_state(0, window_id, SaveFileRadio2, &alt_set, NULL)) alt_set = 0;
      }
    }
  }

  return alt_set;
}

1947 1948 1949
/*************************************************/
/* savefile_text_uri_or_url()                    */
/*                                               */
1950 1951 1952 1953 1954 1955 1956
/* Set the text in the radio buttons or option   */
/* button of a Save File dialogue giving the     */
/* choice of saving a URL as a URI or ANT URL    */
/* file.                                         */
/*                                               */
/* Parameters: Object ID of the relevant Save    */
/*             File dialogue.                    */
1957 1958
/*************************************************/

1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980
static _kernel_oserror * savefile_text_uri_or_url(ObjectId window)
{
  /* These can all fail silently if the gadget isn't present */

  optionbutton_set_label(0,
                         window,
                         SaveFileOption,
                         lookup_token("SaveFileOpURL:Save URL file",0,0));

  radiobutton_set_label (0,
                         window,
                         SaveFileRadio1,
                         lookup_token("SaveFileRaURI:URI format",0,0));

  radiobutton_set_label (0,
                         window,
                         SaveFileRadio2,
                         lookup_token("SaveFileRaURL:URL format",0,0));

  return 0;
}

1981 1982 1983
/*************************************************/
/* savefile_toggle_uri_or_url()                  */
/*                                               */
1984 1985 1986 1987 1988
/* Called when the option to save a URL as a URI */
/* or ANT URL file is changed.                   */
/*                                               */
/* Parameters: Object ID of the relevant Save    */
/*             File dialogue.                    */
1989 1990
/*************************************************/

1991 1992 1993 1994
static _kernel_oserror * savefile_toggle_uri_or_url(ObjectId window)
{
  int set_url = savefile_alternative_selected();

1995 1996
  if (set_url) return savefile_set_filetype(window, SaveFileDrag, FileType_URL, 0);
  else         return savefile_set_filetype(window, SaveFileDrag, FileType_URI, 0);
1997 1998
}

1999 2000 2001
/*************************************************/
/* savefile_text_sprite_or_original()            */
/*                                               */
2002 2003 2004 2005 2006 2007 2008
/* Set the text in the radio buttons or option   */
/* button of a Save File dialogue giving the     */
/* choice of saving an image as a sprite or in   */
/* its original format.                          */
/*                                               */
/* Parameters: Object ID of the relevant Save    */
/*             File dialogue.                    */
2009 2010
/*************************************************/

2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032
static _kernel_oserror * savefile_text_sprite_or_original(ObjectId window)
{
  /* These can all fail silently if the gadget isn't present */

  optionbutton_set_label(0,
                         window,
                         SaveFileOption,
                         lookup_token("SaveFileOpOriginal:Save original image",0,0));

  radiobutton_set_label (0,
                         window,
                         SaveFileRadio1,
                         lookup_token("SaveFileRaSprite:Sprite format",0,0));

  radiobutton_set_label (0,
                         window,
                         SaveFileRadio2,
                         lookup_token("SaveFileRaOriginal:Original format",0,0));

  return 0;
}

2033 2034 2035
/*************************************************/
/* savefile_toggle_sprite_or_original()          */
/*                                               */
2036 2037 2038 2039 2040
/* Called when the option to save an image as a  */
/* sprite or in its original format is changed.  */
/*                                               */
/* Parameters: Object ID of the relevant Save    */
/*             File dialogue.                    */
2041 2042
/*************************************************/

2043 2044 2045 2046
static _kernel_oserror * savefile_toggle_sprite_or_original(ObjectId window, const char * src)
{
  int set_original = savefile_alternative_selected();

2047
  if (!set_original) return savefile_set_filetype(window, SaveFileDrag, FileType_SPR, 0);
2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062
  else
  {
    const char * dot = NULL;
    int          filetype;

    /* Try and determine the filetype from the URL of the image */

    if (src) dot = strrchr(src, '.');

    if (dot)
    {
      if (mimemap_extension_to_riscos(dot, &filetype)) filetype = FileType_DATA;
    }
    else filetype = FileType_DATA;

2063
    return savefile_set_filetype(window, SaveFileDrag, filetype, 0);
2064 2065 2066
  }
}

2067 2068 2069
/*************************************************/
/* savefile_text_without_backgrounds_or_with()   */
/*                                               */
2070 2071 2072 2073 2074 2075 2076
/* Set the text in the radio buttons or option   */
/* button of a Save File dialogue giving the     */
/* choice of saving a Draw file with or without  */
/* a background image.                           */
/*                                               */
/* Parameters: Object ID of the relevant Save    */
/*             File dialogue.                    */
2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103
/*************************************************/

static _kernel_oserror * savefile_text_without_backgrounds_or_with(ObjectId window)
{
  /* These can all fail silently if the gadget isn't present */

  optionbutton_set_label(0,
                         window,
                         SaveFileOption,
                         lookup_token("SaveFileOpBack:With background",0,0));

  radiobutton_set_label (0,
                         window,
                         SaveFileRadio1,
                         lookup_token("SaveFileRaNoBack:No background",0,0));

  radiobutton_set_label (0,
                         window,
                         SaveFileRadio2,
                         lookup_token("SaveFileRaBack:With background",0,0));

  return 0;
}

/*************************************************/
/* savefile_toggle_without_backgrounds_or_with() */
/*                                               */
2104 2105 2106 2107 2108 2109
/* Called when the option to save as a Draw file */
/* with or without a background image is         */
/* changed.                                      */
/*                                               */
/* Parameters: Object ID of the relevant Save    */
/*             File dialogue.                    */
2110 2111 2112 2113 2114 2115
/*************************************************/

static _kernel_oserror * savefile_toggle_without_backgrounds_or_with(ObjectId window)
{
  /* There's very little to do here presently */

2116
  return savefile_set_filetype(window, SaveFileDrag, FileType_DRAW, 0);
2117 2118 2119 2120 2121
}

/*************************************************/
/* savefile_set_correct_extent()                 */
/*                                               */
2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134
/* Ensure the width and height (in terms of the  */
/* visible area and actual extent) is correct    */
/* according to the presence of:                 */
/*                                               */
/* SaveFileRightGapMarker                        */
/* SaveFileBottomGapMarker                       */
/*                                               */
/* Parameters: Object ID of the dialogue to      */
/*             alter;                            */
/*                                               */
/*             Pointer to a BBox describing the  */
/*             new size (xmax - xmin, ymax -     */
/*             ymin).                            */
2135 2136
/*************************************************/

2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188
static _kernel_oserror * savefile_set_correct_extent(ObjectId window, BBox * ra1)
{
  WimpGetWindowStateBlock state;
  int                     open;
  BBox                    opt, ra2;

  /* Find out what the window extent should be */

  RetError(window_get_extent(0, window, &opt));

  /* Set the window extent ymin to the extent ymax minus the  */
  /* height; the height being the ymin of the OK button minus */
  /* the height of the SaveFileBottomGapMarker gadget. Set    */
  /* the width on a similar basis.                            */

  RetError(gadget_get_bbox(0, window, SaveFileBottomGapMarker, &ra2));
  opt.ymin = opt.ymax + ra1->ymin - (ra2.ymax - ra2.ymin);

  RetError(gadget_get_bbox(0, window, SaveFileRightGapMarker, &ra2));
  opt.xmax = opt.xmin + ra1->xmax + (ra2.xmax - ra2.xmin);

  RetError(window_set_extent(0, window, &opt));

  /* We need to ensure the window is opened to full size, but it may be */
  /* already open - so get the state first.                             */

  RetError(window_get_wimp_handle(0, window, &state.window_handle));
  RetError(wimp_get_window_state(&state));

  /* Remember if it is open or not */

  open = state.flags & WimpWindow_Open;

  /* Set the visible area to match the extent, extending */
  /* it to the right and downwards.                      */

  state.visible_area.xmax = state.visible_area.xmin + opt.xmax - opt.xmin;
  state.visible_area.ymin = state.visible_area.ymax - opt.ymax + opt.ymin;

  /* Show the window with these coordinates */

  RetError(wimp_open_window((WimpOpenWindowBlock *) &state));

  /* If it wasn't already open, close the window */

  if (!open) RetError(wimp_close_window(&state.window_handle));

  /* Finished. */

  return NULL;
}

2189 2190 2191
/*************************************************/
/* savefile_auto_width()                         */
/*                                               */
2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203
/* If the SaveFileAutoWidthMarker gadget         */
/* (SaveFile.h) is present, check the width of   */
/* various text items in the Save File dialogue  */
/* and auto-width other components to make the   */
/* dialogue fit the widest. The following        */
/* components are assumed to be present:         */
/*                                               */
/* SaveFileRightGapMarker                        */
/* SaveFileBottomGapMarker                       */
/*                                               */
/* Parameters: Object ID of the dialogue to      */
/*             auto-width.                       */
2204 2205
/*************************************************/

2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424
static _kernel_oserror * savefile_auto_width(ObjectId window)
{
  BBox min,    max;
  BBox optbox, ra1box, ra2box, miscbox;

  char opt[Limits_SaveFile_Option];
  char ra1[Limits_SaveFile_Radios];
  char ra2[Limits_SaveFile_Radios];

  int  showing = 0;
  int  noopt   = 0;
  int  norad   = 0;

  int  width;
  int  margin;

  /* If we haven't got the auto width marker, we shouldn't resize */

  if (gadget_get_bbox(0, window, SaveFileAutoWidthMarker, &min)) return NULL;

  /* Use the right hand gap marker as a margin indicator */

  RetError(gadget_get_bbox(0, window, SaveFileRightGapMarker, &max));
  margin = (max.xmax - max.xmin) * 2;

  /* Use the bottom gap marker as a maximum width indicator */

  RetError(gadget_get_bbox(0, window, SaveFileBottomGapMarker, &max));

  /* Get the option and radio button label texts */

  if (
       optionbutton_get_label(0,
                              window,
                              SaveFileOption,
                              opt,
                              sizeof(opt),
                              NULL)
     )
     *opt = '\0';

  else opt[sizeof(opt) - 1] = '\0';

  if (
       radiobutton_get_label (0,
                              window,
                              SaveFileRadio1,
                              ra1,
                              sizeof(ra1),
                              NULL)
     )
     *ra1 = '\0';

  else ra1[sizeof(ra1) - 1] = '\0';

  if (
       radiobutton_get_label (0,
                              window,
                              SaveFileRadio2,
                              ra2,
                              sizeof(ra2),
                              NULL)
     )
     *ra2 = '\0';

  else ra2[sizeof(ra2) - 1] = '\0';

  /* Find the bounding boxes of the option and radio */
  /* button gadgets to see what is visible right now */

  if (gadget_get_bbox(0, window, SaveFileOption, &optbox)) noopt = 1;
  if (gadget_get_bbox(0, window, SaveFileRadio1, &ra1box)) norad = 1;
  if (gadget_get_bbox(0, window, SaveFileRadio2, &ra2box)) norad = 1;

  if      (!noopt && optbox.xmin < optbox.xmax - optbox.xmin) showing = 1;
  else if (!norad && ra1box.xmin < ra1box.xmax - ra1box.xmin) showing = 2;

  /* Minimum width comes from the auto width marker,  */
  /* but this includes the margin width which we want */
  /* to exclude for the moment.                       */

  width = min.xmax - min.xmin - margin;

  if (showing == 1)
  {
    int text;

    /* Showing the option button, so work out width of it */

    RetError(utils_text_width(opt, &text, 0));

    text += 64; /* Hack :-) - overestimate of the option button size itself plus gap between button and label */

    if (text > width) width = text;
  }
  else if (showing == 2)
  {
    int text1, text2;

    /* Showing the radio buttons, so work out width of them */

    RetError(utils_text_width(ra1, &text1, 0));
    RetError(utils_text_width(ra2, &text2, 0));

    text1 += 64; /* Magic number as for 'showing == 1' case above */
    text2 += 64;

    if (text1 > width) width = text1;
    if (text2 > width) width = text2;
  }

  if (width > max.xmax - max.xmin - margin) width = max.xmax - max.xmin - margin;

  /* We now know how wide things are to be. The writable gadget   */
  /* and option or radio buttons should be 'width', the draggable */
  /* icon should just move to stay centred, to start with.        */

  if (!noopt)
  {
    optbox.xmax = optbox.xmin + width;

    RetError(gadget_move_gadget(0, window, SaveFileOption, &optbox));
  }

  if (!norad)
  {
    ra1box.xmax = ra1box.xmin + width;
    ra2box.xmax = ra2box.xmin + width;

    RetError(gadget_move_gadget(0, window, SaveFileRadio1, &ra1box));
    RetError(gadget_move_gadget(0, window, SaveFileRadio2, &ra2box));
  }

  RetError(gadget_get_bbox(0, window, SaveFileWrit, &miscbox));

  miscbox.xmax = miscbox.xmin + width;

  RetError(gadget_move_gadget(0, window, SaveFileWrit, &miscbox));

  {
    int drag;

    RetError(gadget_get_bbox(0, window, SaveFileDrag, &miscbox));

    drag = miscbox.xmax - miscbox.xmin;

    miscbox.xmin = (width + margin - drag) / 2;
    miscbox.xmax = miscbox.xmin + drag;

    RetError(gadget_move_gadget(0, window, SaveFileDrag, &miscbox));
  }

  /* The OK and Cancel buttons - these have a 'margin' gap between */
  /* them, and the OK button should be 8 wider than Cancel (it's   */
  /* the default action button, and has a thicker border). 16 is   */
  /* the technically correct value, but it doesn't look good!      */

  {
    int button;

    button = (width - margin - 8) / 2;

    RetError(gadget_get_bbox(0, window, SaveFileCancel, &ra1box));
    RetError(gadget_get_bbox(0, window, SaveFileOK,     &ra2box));

    ra1box.xmax = ra1box.xmin + button;

    ra2box.xmin = ra1box.xmax + margin;
    ra2box.xmax = ra2box.xmin + button + 8;

    RetError(gadget_move_gadget(0, window, SaveFileCancel, &ra1box));
    RetError(gadget_move_gadget(0, window, SaveFileOK,     &ra2box));
  }

  /* Finally, set the window width */

  {
    WimpGetWindowStateBlock state;
    int                     open;

    /* Find out what the window vertical extent should be */

    RetError(window_get_extent(0, window, &miscbox));

    /* Set the width */

    miscbox.xmax = miscbox.xmin + width + margin;

    RetError(window_set_extent(0, window, &miscbox));

    /* We need to ensure the window is opened to full size, but it may be */
    /* already open - so get the state first.                             */

    RetError(window_get_wimp_handle(0, window, &state.window_handle));
    RetError(wimp_get_window_state(&state));

    /* Remember if it is open or not */

    open = state.flags & WimpWindow_Open;

    /* Set the visible area to match the extent, extending */
    /* it to the right and downwards.                      */

    state.visible_area.xmax = state.visible_area.xmin + miscbox.xmax - miscbox.xmin;
    state.visible_area.ymin = state.visible_area.ymax - miscbox.ymax + miscbox.ymin;

    /* Show the window with these coordinates */

    RetError(wimp_open_window((WimpOpenWindowBlock *) &state));

    /* If it wasn't already open, close the window */

    if (!open) RetError(wimp_close_window(&state.window_handle));
  }

  /* Finished. */

  return NULL;
}