Main 115 KB
Newer Older
Neil Turton's avatar
Neil Turton committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright 1996 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.
 */
/* > c.Main
 *
17
 *  Paint: RISC OS sprite editor
Neil Turton's avatar
Neil Turton committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
 *   Main loop and resource loading
 *
 *  Author: A.P. Thompson
 *  Others:
 *     DAHE David Elworthy
 *     JRC  Jonathan Coxhead
 *     JSR  Jonathan Roach
 *     JAB  James Bye
 *     ECN  Edward Nevill
 *     CDP  Christopher Partington (Cambridge Systems Design)
 *     TMD  Tim Dobson
 *     OSS  Owen Smith
 *
 *  DAHE, 28 Aug  89 - internationalisation
 *        06 Sept 89 - make heap non compacting
 *        13 Sept 89 - get round static data init. problem for module
                         version
 *  JSR,  25 Oct  89 - Add save desk
 *  JAB,  02 Oct  90 - Changed 'Create Sprite Method'
 *  JAB,  17 Oct  90 - Auto-Opens create when new sprite filer window is
                         opened
 *  JAB,  30 Oct  90 - Added call to 'help.h' for interactive menu help
 *  JAB,  23 Jan  91 - Added 'Snapshot' to replace 'get screen area'
 *  JAB,  22 Mar  91 - Fixed templates
 *  JAB,  22 Mar  91 - Fixed timer corruption bug in snapshot
 *  JAB,  30 Apr  91 - Added function to check the modes before loading
 *  ECN,  13 Jan  92 - Set fixed stack size
 *  CDP,  20 Feb  92
 *     Added FIX7631: fixes G-RO-7631 (incorrect error message and empty window
 *     when loaded file does not exist).
 *     Added FIX0780: fixes RP-0780 (incorrect selection of sprite in 2-column
 *     full-info display mode).
 *     Added FIXSIGNAL: ensures that signal causes the error text to be printed
 *     rather than just "(%s)".
52
*   TMD,  17 Mar  92 - Made print_file call menus_do_print, not queue_print.
Neil Turton's avatar
Neil Turton committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
 *  OSS,  23 Mar  92 - RP-0716 - split into two directories in ResourceFS
 *                     so the application name can change on a RAM loaded
 *                     localisation.
 *  TMD,  25 Mar  92 - Change main_set_printer_data to only update
 *                     menus_print_where if the printer's bottom left origin
 *                     has actually changed.
 *  JRC  6th Feb 1995  Import JPEG's into sprites with full palette.
 */

#include <assert.h>
#include <ctype.h>
#include <kernel.h>
#include <limits.h>
#include <locale.h>
#include <signal.h>
#include <setjmp.h>
#include <stdarg.h>
#include <swis.h>
71
#include "Global/CMOS.h"
72
#include "Global/FileTypes.h"
73
#include "Global/OsBytes.h"
74
#include "Global/VduExt.h"
Neil Turton's avatar
Neil Turton committed
75 76 77 78 79 80

#include "pointer.h"
#include "akbd.h"
#include "alarm.h"
#include "baricon.h"
#include "bbc.h"
81
#include "colourtran.h"
Neil Turton's avatar
Neil Turton committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95
#include "dboxquery.h"
#include "heap.h"
#include "help.h"
#include "flex.h"
#include "msgs.h"
#include "msgtrans.h"
#include "res.h"
#include "resspr.h"
#include "template.h"
#include "visdelay.h"
#include "werr.h"
#include "wimp.h"
#include "wimpt.h"
#include "xferrecv.h"
Robert Sprowson's avatar
Robert Sprowson committed
96 97
#include "jpeg.h"
#include "xfersend.h"
Neil Turton's avatar
Neil Turton committed
98 99 100 101 102 103 104

#define FILENAMEMAX 255

#include "ftrace.h"
#include "m.h"

#include "main.h"
105
#include "Menus.h"
Neil Turton's avatar
Neil Turton committed
106
#include "MenuD.h"
107 108 109 110 111
#include "PSprite.h"
#include "SprWindow.h"
#include "ToolWindow.h"
#include "Tools.h"
#include "Colours.h"
ROOL's avatar
ROOL committed
112
#include "ColourPanel.h"
113 114
#include "AltRename.h"
#include "PaintLib.h"
115 116
#include "CnPDrag.h"
#include "Clipboard.h"
Neil Turton's avatar
Neil Turton committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130

#define FREEZE_STACK 0
#define CATCH_SIGNALS 1

#define EMPTY(s) ((s) [0] == '\0')
#define CLEAR(s) ((s) [0] =  '\0')

#define SIG_LIMIT 11 /*largest signal number + 1*/

#if FREEZE_STACK
  int __root_stack_size = 64*1024; /*64K*/
  extern _kernel_ExtendProc flex_dont_budge;
#endif

ROOL's avatar
ROOL committed
131
#define ICON_SPACE_SIZE 1400 /* For icons in the template */
132
#define DISPLAY_MARGIN 0
133
#define SPACE_FOR_HELP_TEXT (main_FILER_TextHeight+DISPLAY_MARGIN)
Neil Turton's avatar
Neil Turton committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

typedef
  struct
  { BOOL     active;          /*snapshot being taken*/
    BOOL     first;           /*used by the alarm callback*/
    BOOL     delay;           /*whether there is a delay*/
    int      user_sec;        /*seconds field in dbox*/
    int      snap_now;        /*time to snap*/
    BOOL     whole_screen;    /*grab whole screen?*/
    wimp_box box;             /*area to snap if not*/
    BOOL     want_timer;      /*display timer dbox?*/
    dbox     timer;           /*handle if so*/
  }
  snapshotstr;

149
main_window *main_windows;
Neil Turton's avatar
Neil Turton committed
150

151
main_options main_current_options;
Neil Turton's avatar
Neil Turton committed
152

153 154
const wimp_box main_big_extent =
    {-0x1FFFFFFF, -0x1FFFFFFF, 0x1FFFFFFF, 0x1FFFFFFF};
Neil Turton's avatar
Neil Turton committed
155 156 157 158 159 160 161

/**************************************************************************
 *                                                                        *
 *  Static variables.                                                     *
 *                                                                        *
 **************************************************************************/

162
static main_template file_template;
Neil Turton's avatar
Neil Turton committed
163

164
static menu main_menu;
Neil Turton's avatar
Neil Turton committed
165

166 167
static main_file fudgefile =
      {NULL, NULL, NULL, NULL, 0, 0, 1, 0, NULL /* &fudgewindow */};
Neil Turton's avatar
Neil Turton committed
168

169 170
static main_window fudgewindow = { NULL, 0, main_window_is_file,
                              NULL /* (main_info_block *) &fudgefile */ };
Neil Turton's avatar
Neil Turton committed
171

172
char *ramfetch_buffer = NULL;
Neil Turton's avatar
Neil Turton committed
173

174
static char Paint_Dir [FILENAME_MAX + 1];
Neil Turton's avatar
Neil Turton committed
175

176
static void init_statics (void)
Robert Sprowson's avatar
Robert Sprowson committed
177

178 179 180 181 182
{ /* Required to generate relocatable code */
  fudgefile.window = &fudgewindow;
  fudgewindow.data = (main_info_block *)&fudgefile;
  main_windows    = &fudgewindow;
}
Neil Turton's avatar
Neil Turton committed
183 184 185 186 187 188 189 190 191

static
  main_options
    initial_options =
    { {/*full info?*/ FALSE, /*use desktop colours?*/ TRUE},
      {/*show colours?*/ TRUE, /*small colours?*/ FALSE},
      {/*show tools?*/ TRUE},
      {/*zoom*/ 1, 1},
      {/*grid?*/ TRUE, /*colour*/ 7},
ROOL's avatar
ROOL committed
192 193 194
      {/*extended*/ TRUE /*always on in versions from Black onwards*/},
      {/*h.lt. bgcolour?*/TRUE, /*art. friendly?*/TRUE,
        /*ask remap pal?*/TRUE}
Neil Turton's avatar
Neil Turton committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    };

static
  snapshotstr
    sshot =
    { /*active?*/ FALSE,
      /*first?*/ FALSE,
      /*delay?*/ FALSE,
      /*user_sec*/ 10,
      /*snap_now*/ 0,
      /*whole_screen?*/ FALSE,
      /*box*/ {0, 0, 0, 0},
      /*want_timer?*/ TRUE,
      /*timer*/ NULL
    };

ROOL's avatar
ROOL committed
211
#define MAX_OPTIONS 86
Neil Turton's avatar
Neil Turton committed
212 213 214 215 216 217 218 219 220 221 222

#if CATCH_SIGNALS
  static jmp_buf Buf;
  static void Signal_Handler (int signal) {longjmp (Buf, signal);}
  static void (*Saved_Handlers [SIG_LIMIT]) (int);
#endif

/*******************************************************************
 * Now the code                                                    *
 *******************************************************************/

Robert Sprowson's avatar
Robert Sprowson committed
223 224 225
static void main_clear_background (wimp_redrawstr *rds)

{ sprite_area *base;
226 227 228 229 230 231 232
  sprite_area *rombase;
  sprite_pixtrans transtab[16];
  sprite_info sinfo;
  sprite_id sid;
  int dummy;
  BOOL use_sprite;
  BOOL scaled = FALSE;
233
  BOOL use_transtab = FALSE;
234
  char name[NAME_LIMIT + 1];
235

236
  /* Test tiling disabled in CMOS */
237
  os_byte (OsByte_ReadCMOS, (dummy = DesktopFeaturesCMOS, &dummy), &use_sprite);
238
  use_sprite = (use_sprite & desktopwindowtile) == 0;
239

240
  if (use_sprite)
241
  { /* Find tile sprite for mode */
Robert Sprowson's avatar
Robert Sprowson committed
242
    os_swix2r (Wimp_BaseOfSprites, 0, 0, &rombase, &base);
Robert Sprowson's avatar
Robert Sprowson committed
243
    sprintf (name, "tile_1-%d", 1<<bbc_vduvar (bbc_Log2BPP));
244 245 246 247 248 249 250
    sid.s.name = name;
    sid.tag = sprite_id_name;
    if (sprite_readsize (base, &sid, &sinfo) != NULL)
    { if (sprite_readsize (rombase, &sid, &sinfo) != NULL)
        use_sprite = 0;
      base = rombase;
    }
251 252 253 254 255
    int mode_log2bpp = bbc_vduvar (bbc_Log2BPP);
    int sprite_log2bpp = bbc_modevar (sinfo.mode, bbc_Log2BPP);
    if ((mode_log2bpp != sprite_log2bpp)
     || ((bbc_vduvar (bbc_ModeFlags) ^ bbc_modevar (sinfo.mode, bbc_ModeFlags)) & (ModeFlag_FullPalette | ModeFlag_64k | ModeFlag_DataFormat_Mask))
     || (bbc_vduvar (bbc_NColour) != bbc_modevar (sinfo.mode, bbc_NColour)))
256
    { scaled = TRUE;
257 258 259 260 261 262 263
      /* Wimp translation tables only possible for <=8bpp screen and <4bpp sprite. We should probably be asking ColourTrans for a table instead. */
      if ((mode_log2bpp < 4) && (sprite_log2bpp < 3))
      {
        use_transtab = TRUE;
        if (wimp_readpixtrans (base, &sid, NULL, transtab) != NULL)
          use_sprite = 0;
      }
264
    }
265 266
  }

267
  if (use_sprite)
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
  { int left, top;

    /* Adjust for eigen factors */
    sinfo.width <<= bbc_vduvar (bbc_XEigFactor);
    sinfo.height <<= bbc_vduvar (bbc_YEigFactor);

    left = WORKAREA_TO_SCREEN_X(rds, 0);
    left = left + (((left - rds->g.x0)/sinfo.width) * sinfo.width);
    top = WORKAREA_TO_SCREEN_Y(rds, 0);
    top = top - (((top - rds->g.y1)/sinfo.height) * sinfo.height);

    /*werr(0, "%d %d %d %d %d", left, top, width, height, rds->g.y0);*/

    for (; top>rds->g.y0; top -= sinfo.height)
    { int x;
      for (x = left; x < rds->g.x1; x += sinfo.width)
      { if (scaled)
285
          use_sprite = (sprite_put_scaled (base, &sid, 0, x, top - sinfo.height, NULL, (use_transtab?transtab:NULL)) == NULL);
286 287 288 289
        else
          use_sprite = (sprite_put_given (base, &sid, 0, x, top - sinfo.height) == NULL);
        if (!use_sprite) break;
      }
290 291 292 293
    }
  }

  if (!use_sprite)
294 295 296 297
  { wimp_paletteword palette_grey;

    palette_grey.word = (int)0xdfdfdf00; /* background_colour */
    wimpt_noerr (colourtran_setGCOL (palette_grey, 1 << 7, 0, &dummy));
Robert Sprowson's avatar
Robert Sprowson committed
298
    bbc_clg ();
299 300 301
  }
}

Neil Turton's avatar
Neil Turton committed
302 303 304 305 306 307 308 309
os_error *main_read_pixel (sprite_area *area, sprite_id *id,
    int x, int y, sprite_colour *colour)

{ os_error *error = NULL;
  os_regset reg_set;
  int pal_size;
  sprite_header *header;

Robert Sprowson's avatar
Robert Sprowson committed
310 311 312
  /*This routine is used instead of sprite_readpixel, which is badly
    broken under Medusa. J R C 24th Feb 1994*/

Neil Turton's avatar
Neil Turton committed
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
  reg_set.r [0] = 37 /*create/remove palette*/;

  if (id->tag == sprite_id_addr)
  { reg_set.r [0] |= 512;
    reg_set.r [2] = (int) id->s.addr;
  }
  else
  { if (area != NULL) reg_set.r [0] |= 256;
    reg_set.r [2] = (int) id->s.name;
  }

  reg_set.r [1] = (int) area;

  reg_set.r [3] = -1 /*read palette size*/;

  if ((error = os_swix (OS_SpriteOp, &reg_set)) != NULL)
    goto finish;

  pal_size = reg_set.r [3];
  ftracef1 ("palette size is %d\n", pal_size);

  if (pal_size == 256)
  { /*Must do this "by hand."*/
    if ((error = sprite_select_rp (area, id, (sprite_ptr *) &header)) !=
        NULL)
      goto finish;

    ftracef4 ("width %d, height %d, image %d, lbit %d\n",
        header->width, header->height, header->image, header->lbit);

    colour->colour =
        ((char *) (((int *) ((char *) header + header->image) +
        (header->width + 1)*(header->height - y)))) [header->lbit/8 + x];
    colour->tint = 0;

    ftracef4 ("read_pixel from (%d, %d) -> (%d, %d)\n",
        x, y, colour->colour, colour->tint);
  }
  else
    /*All other cases o k.*/
    if ((error = sprite_readpixel (area, id, x, y, colour)) == NULL)
      goto finish;

finish:
  if (error != NULL) ftracef1 ("got error %s\n", error->errmess);
  return error;
}

static int mouseX;
static int mouseY;

static int mouseB (void)

{ os_regset r;

  ftracef0 ("mouseB\n");
  os_swix (OS_Mouse, &r);
  mouseX=r.r[0];
  mouseY=r.r[1];
  return r.r[2];
}

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
static sprite_header *main_make_newjpeg (sprite_area *area, int size, jpeg_info *info)

{ sprite_header *header;
  int log2bpp = bbc_modevar (-1, bbc_Log2BPP);

  area->size    = size + sizeof (sprite_area);
  area->number  = 1;
  area->sproff  = 16;
  area->freeoff = size + sizeof (sprite_area);

  header = (sprite_header *)((char *)area + sizeof (sprite_area));
  header->next = size;
  strcpy (header->name, "!newjpeg");
  header->width  = ((info->width << log2bpp) - 1)/32;
  header->height = info->height - 1;
  header->lbit   = 0;
  header->rbit   = (info->width << log2bpp) - 32 * header->width - 1;
  header->image  =
  header->mask   = sizeof (sprite_header) +
                   (log2bpp <= 3 ? 8 << (1 << log2bpp) : 0);
  header->mode   = log2bpp + 1 << 27 | 180/wimpt_dy () << 14 |
                   180/wimpt_dx () << 1 | 1;

  return header;
}

static os_error *main_plot_fromjpeg (sprite_area *area, sprite_header *header, jpeg_id *jid)

{ os_error *error;
  int       s1, s2, s3;

406
  /* Switch to sprite, plot the JPEG */
407 408 409 410 411 412 413
  error = os_swix4r (OS_SpriteOp, 0x23C, area, header, 0,
                                  NULL, &s1, &s2, &s3);
  if (error != NULL) return error;

  error = jpeg_put_scaled (jid, 0, 0, NULL,
                           jpeg_PUT_DITHER_ENABLE | jpeg_PUT_ERROR_DIFFUSED_DITHER);

414 415 416 417
  /* Unconditionally switch back */
  os_swix4 (OS_SpriteOp, 0x23C, s1, s2, s3);

  return error;
418 419
}

Robert Sprowson's avatar
Robert Sprowson committed
420
static void main_icon_bboxes (main_window *window, main_sprite *sprite, wimp_box *iniconbbox,
Robert Sprowson's avatar
Robert Sprowson committed
421
                              wimp_box *spritebbox, wimp_box *namebbox)
Robert Sprowson's avatar
Robert Sprowson committed
422

Robert Sprowson's avatar
Robert Sprowson committed
423
{ /* Return the bounding boxes for this sprite */
424
  psprite_info sinfo;
Robert Sprowson's avatar
Robert Sprowson committed
425

426 427
  psprite_read_full_info (sprite, &sinfo);

Robert Sprowson's avatar
Robert Sprowson committed
428 429 430 431
  if (namebbox != NULL)
  { int width;
    os_swix3r (Wimp_TextOp, 1, sinfo.name, 0, &width, NULL, NULL);
    width += 16;
432

Robert Sprowson's avatar
Robert Sprowson committed
433
    if (window->data->file.fullinfo)
Robert Sprowson's avatar
Robert Sprowson committed
434
    { namebbox->x0 = iniconbbox->x0 + 6*main_FILER_TextWidth;
435 436 437 438
      namebbox->x1 = namebbox->x0 + width;
      namebbox->y0 = iniconbbox->y0 + main_FILER_TextHeight;
      namebbox->y1 = namebbox->y0 + main_FILER_TextHeight;
    }
Robert Sprowson's avatar
Robert Sprowson committed
439 440 441 442 443 444
    else
    { namebbox->x0 = (iniconbbox->x0 + iniconbbox->x1 - width)/2;
      namebbox->x1 = namebbox->x0 + width;
      namebbox->y0 = iniconbbox->y0;
      namebbox->y1 = namebbox->y0 + main_FILER_TextHeight;
    }
445
  }
Robert Sprowson's avatar
Robert Sprowson committed
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460

  if (spritebbox != NULL)
  { int swidth, sheight;
    swidth = sinfo.width * sprite->iconsize.scale_xmul/sprite->iconsize.scale_xdiv;
    swidth = swidth * sprite->mode.scale_xmul;
    sheight = sinfo.height * sprite->iconsize.scale_ymul/sprite->iconsize.scale_ydiv;
    sheight = sheight * sprite->mode.scale_ymul;

    if (window->data->file.fullinfo)
    { spritebbox->x0 = iniconbbox->x0 + main_FILER_Border/2;
      spritebbox->x1 = spritebbox->x0 + swidth;
      spritebbox->y0 = iniconbbox->y0 + main_FILER_Border/2;
      spritebbox->y1 = spritebbox->y0 + sheight;
    }
    else
Robert Sprowson's avatar
Robert Sprowson committed
461
    { spritebbox->x0 = (iniconbbox->x0 + iniconbbox->x1-swidth)/2;
462 463 464 465 466 467 468
      spritebbox->x1 = spritebbox->x0 + swidth;
      spritebbox->y0 = iniconbbox->y1 - (main_FILER_Border + main_FILER_YSize + sheight)/2;
      spritebbox->y1 = spritebbox->y0 + sheight;
    }
  }
}

Neil Turton's avatar
Neil Turton committed
469 470
void main_iprintf (int flags, int x, int y, char *format, ...)

Robert Sprowson's avatar
Robert Sprowson committed
471 472 473 474
{ char s [500];
  va_list list;
  wimp_icon icon;
  int width;
Neil Turton's avatar
Neil Turton committed
475

Robert Sprowson's avatar
Robert Sprowson committed
476
  ftracef0 ("main_iprintf\n");
Neil Turton's avatar
Neil Turton committed
477

Robert Sprowson's avatar
Robert Sprowson committed
478 479 480
  va_start (list, format);
  vsprintf (s, format, list);
  va_end (list);
Neil Turton's avatar
Neil Turton committed
481

Robert Sprowson's avatar
Robert Sprowson committed
482 483 484
  if (flags & wimp_IFONT)
    width = 16*strlen (s);
  else
Robert Sprowson's avatar
Robert Sprowson committed
485
  { os_swix3r (Wimp_TextOp, 1, s, 0, &width, NULL, NULL);
Robert Sprowson's avatar
Robert Sprowson committed
486 487 488
    width/=2;
    width+=8;
  }
Neil Turton's avatar
Neil Turton committed
489

Robert Sprowson's avatar
Robert Sprowson committed
490 491 492 493 494 495 496 497 498 499 500 501
  if (flags & wimp_IHCENTRE)
  { icon.box.x0 = x - width;
    icon.box.x1 = x + width;
  }
  else if (flags & wimp_IRJUST)
  { icon.box.x0 = x - 2*width;
    icon.box.x1 = x;
  }
  else
  { icon.box.x0 = x;
    icon.box.x1 = x + 2*width; /*be on the safe side*/
  }
Neil Turton's avatar
Neil Turton committed
502

Robert Sprowson's avatar
Robert Sprowson committed
503 504
  icon.box.y0 = y - main_FILER_TextHeight;
  icon.box.y1 = y;
505

Robert Sprowson's avatar
Robert Sprowson committed
506 507 508
  icon.flags = (wimp_iconflags) (flags | wimp_ITEXT | wimp_INDIRECT | 7 << 24);
  icon.data.indirecttext.buffer = s;
  icon.data.indirecttext.validstring = (char *) -1;
Neil Turton's avatar
Neil Turton committed
509

Robert Sprowson's avatar
Robert Sprowson committed
510
  wimpt_noerr (wimp_ploticon (&icon));
Neil Turton's avatar
Neil Turton committed
511 512
}

Robert Sprowson's avatar
Robert Sprowson committed
513 514 515 516
static int escape (void)

{ return bbc_inkey (-113) || bbc_inkey (-45);
}
Neil Turton's avatar
Neil Turton committed
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640

/******************************************
 * Snapshot functions - external for      *
 *                      'menu.c' access   *
 ******************************************/

static BOOL get_snapshot_box (wimp_box *box)

{ int x0 = 0, y0 = 0, x1 = 0, y1 = 0;

  ftracef0 ("get_snapshot_box\n");
  if (!sshot.whole_screen)
  { sprite_id id;

    id.s.name = "grabptr";
    id.tag    = sprite_id_name;
    pointer_set_shape (resspr_area (), &id, 0, 0);

    ftracef0 ("wait for button down\n");
    while (!(mouseB () & 4))
      if (escape ())
      { pointer_reset_shape ();
        return FALSE;
      }

    x1 = x0 = mouseX;
    y1 = y0 = mouseY;

    os_swi2 (OS_SetColour, 3, -1);
  #if 0
    /*replaces the following, avoiding need to check full-palette bit*/
    bbc_gcol (3, 127);
    bbc_tint (2 /*was 4?? JRC 27 Aug 1991*/, 3);
  #endif

    bbc_rectangle (x0, y0, x1 - x0, y1 - y0);
    ftracef0 ("wait for button up\n");
    while (mouseB () & 4)
    { if (mouseX != x1 || mouseY != y1)
      { bbc_rectangle (x0, y0, x1 - x0, y1 - y0);
        if (escape ())
        { pointer_reset_shape ();
          return FALSE;
        }
        x1 = mouseX;
        y1 = mouseY;
        bbc_rectangle (x0, y0, x1 - x0, y1 - y0);
      }
    }
    bbc_rectangle (x0, y0, x1 - x0, y1 - y0);
    pointer_reset_shape ();
  }
  else
  { ftracef0 ("User wants whole screen\n");

    x0 = 0;
    y0 = 0;
    x1 = bbc_vduvar (bbc_XWindLimit) << bbc_vduvar (bbc_XEigFactor);
    y1 = bbc_vduvar (bbc_YWindLimit) << bbc_vduvar (bbc_YEigFactor);
  }

  box->x0 = x0, box ->y0 = y0, box->x1 = x1, box->y1 = y1;

  return TRUE;
}

/* smile for the camera */
static void snapshot_happysnapper (int x0, int y0, int x1, int y1)

{ main_sprite tempsprite;
  int width, height, dx, xt, size;
  main_file tsfb;
  sprite_id sid;
  os_error *error = NULL;
  BOOL found_translation = FALSE;

  ftracef4 ("snapshot_happysnapper: x0 %d, y0 %d; x1 %d, y1 %d\n",
      x0, y0, x1, y1);
  tsfb.spritearea = NULL;
  dx   = wimpt_dx ();
  if (x1 < x0) {int t = x1; x1 = x0; x0 = t;}
  xt = x0 & ~ ((32 >> bbc_modevar (-1, bbc_Log2BPC))*dx - 1);
                              /* round down to word boundary */
  ftracef5 ("snapshot_happysnapper: x0 %d (xt %d), y0 %d;"
    " x1 %d, y1 %d\n", x0, xt, y0, x1, y1);
  width = (x1 - xt)/dx + 2;
  if (y1 < y0) {int t = y1; y1 = y0; y0 = t;}
  height = (y1 - y0)/wimpt_dy () + 2;

  size = psprite_size (width, height, wimpt_mode (), /*mask?*/ 0, /*palette?*/ 2) +
      sizeof (sprite_area) + 256;
  ftracef3 ("snapshot_happysnapper: need %d (%dx%d) bytes\n",
     size, width, height);
  if (flex_alloc ((flex_ptr) &tsfb.spritearea, size) == 0)
  { error = main_error ("PntEG");
    goto finish;
  }

  sprite_area_initialise (tsfb.spritearea, size);

  ftracef1 ("capturing sprite as \"%s\"\n", msgs_lookup ("PntF8"));
  /*First try with a palette, then without. J R C 6th Dec 1993*/
  if ((error = sprite_get_given_rp (tsfb.spritearea, msgs_lookup ("PntF8"),
      sprite_haspalette, xt /*JRC*/, y0, x1, y1, &sid.s.addr)) != NULL &&
      (error = sprite_get_given_rp (tsfb.spritearea, msgs_lookup ("PntF8"),
      sprite_nopalette, xt, y0, x1, y1, &sid.s.addr)) != NULL)
    goto finish;
  ftracef0 ("capturing sprite done\n");
  sid.tag = sprite_id_addr;

  /* fake main_sprite  */
  tempsprite.file            = &tsfb;
  tempsprite.offset          = (int) sid.s.addr - (int) tsfb.spritearea;
  tempsprite.spriteno        = 0;
  tempsprite.mode.scale_xmul =
  tempsprite.mode.scale_xdiv = 1 << bbc_modevar (-1, bbc_XEigFactor);
  tempsprite.mode.scale_ymul =
  tempsprite.mode.scale_ydiv = 1 << bbc_modevar (-1, bbc_YEigFactor);
                                 /* might be needed for printing!*/
  ftracef1 ("snapshot_happysnapper: offset: %d\n", tempsprite.offset);
  menus_hack_palette (&tempsprite);
  /* Create a translation table. If you don't do this, you get
     fatal overwriting on saving the screen area to the printer
  */
Robert Sprowson's avatar
Robert Sprowson committed
641
  ftracef0 ("calling psprite_ttab_for_sprite ()\n");
Neil Turton's avatar
Neil Turton committed
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
  if ((tempsprite.transtab = psprite_ttab_for_sprite (&tempsprite,
      -1, (int *) -1)) == NULL)
  { error = main_error ("PntEG");
    goto finish;
  }
  found_translation = TRUE;

  /*JRC 24th Nov 1993*/
  sprwindow_remove_wastage (&tempsprite);

  menus_save_sprite (&tempsprite); /* save via xfersend */

  sid.tag = sprite_id_addr;
  sid.s.addr = psprite_address (&tempsprite);
  sprite_delete (tsfb.spritearea, &sid);

finish:
  /*Check there is a translation table first. JRC 31st Jan 1995*/
  if (found_translation)
  { /* Discard the translation table */
    psprite_drop_translation (&tempsprite.transtab);
  }
  if (tsfb.spritearea != NULL) flex_free ((flex_ptr) &tsfb.spritearea);
  pointer_reset_shape ();

  wimpt_complain (error);
}

static void Timer_Cb (dbox d, void *h)

Robert Sprowson's avatar
Robert Sprowson committed
672 673
{ /*Callback for timer events - the only one is Cancel.*/
  ftracef0 ("Timer_Cb\n");
Neil Turton's avatar
Neil Turton committed
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
  h = h;

  switch (dbox_get (d))
  { case d_SnapshotTimer_Cancel:
      ftracef0 ("cancel hit\n");

      /*Cancel the snapshot.*/
      sshot.active = FALSE;

      alarm_removeall ((void *) sshot.snap_now);

      /*Kill the timer.*/
      dbox_dispose (&sshot.timer);
      sshot.timer = NULL;
    break;
Robert Sprowson's avatar
Robert Sprowson committed
689 690
  }
}
Neil Turton's avatar
Neil Turton committed
691 692 693

static void Snapshot_Cb (int due, void *h)

Robert Sprowson's avatar
Robert Sprowson committed
694 695
{ /*Callback for the second timer, ending in a call to take the snapshot.*/
  int time_now, time_left, secs_left;
Neil Turton's avatar
Neil Turton committed
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739

  ftracef0 ("Snapshot_Cb\n");
  due = due, h = h; /*for Norcroft*/
  time_now = alarm_timenow ();

  if (sshot.first)
  { if (!get_snapshot_box (&sshot.box))
    { sshot.active = FALSE;
      return; /*do nothing - escape pressed*/
    }
    time_now = alarm_timenow (); /*might be much later now ...*/

    if (sshot.delay)
    { sshot.snap_now = time_now + 100*sshot.user_sec;

      if (sshot.want_timer)
      { if ((sshot.timer = dbox_new ("snpshottime")) != NULL)
        { dbox_showstatic (sshot.timer);
          dbox_eventhandler (sshot.timer, &Timer_Cb, NULL);
        }
      }
      else
        sshot.timer = NULL;
    }
    else
    { sshot.snap_now = time_now;
      sshot.timer = NULL;
    }

    sshot.first = FALSE;
  }

  time_left = alarm_timedifference (time_now, sshot.snap_now);
  ftracef1 ("Snapshot_Cb called with %dcs left to go\n", time_left);
  secs_left = time_left > 0? (time_left + 99)/100 /*round up!*/: 0;

  if (sshot.timer != NULL)
  { /*Update the dbox.*/
    dbox_setnumeric (sshot.timer, d_SnapshotTimer_SecsLeft, secs_left);

    if (secs_left <= 1)
    { /*Kill it.*/
      dbox_dispose (&sshot.timer);
      sshot.timer = NULL;
Robert Sprowson's avatar
Robert Sprowson committed
740 741
    }
  }
Neil Turton's avatar
Neil Turton committed
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 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 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911

  if (secs_left == 0)
  { /*Smile for the camera.*/
    ftracef0 ("ACTUALLY GRABBING NOW\n");
    snapshot_happysnapper (sshot.box.x0, sshot.box.y0, sshot.box.x1,
        sshot.box.y1);
    sshot.active = FALSE;
  }
  else
    /*Queue the next call to this routine.*/
    alarm_set (sshot.snap_now - 100*(secs_left - 1), &Snapshot_Cb,
        (void *) sshot.snap_now);
}
/*------------------------------------------------------------------------*/
static BOOL snapshot_raw_event_handler (dbox d, void *event, void *h)

{ BOOL handled = FALSE;

  ftracef0 ("snapshot_raw_event_handler\n");
  h = h; /*for Norcroft*/

  if (help_dboxrawevents (d, event, (void *) "PntHE"))
    return TRUE;

  /*We handle the E S G here, since I don't trust the WIMP: it gets Adjust
    clicks wrong (ending up with nothing selected).*/
  switch (((wimp_eventstr *) event)->e)
  { case wimp_EBUT:
      switch (((wimp_eventstr *) event)->data.but.m.i)
      { case d_Snapshot_NoDelay:
          dbox_setnumeric (d, d_Snapshot_UserDef, !dbox_getnumeric (d,
              d_Snapshot_NoDelay));
          handled = TRUE;
        break;

        case d_Snapshot_UserDef:
          dbox_setnumeric (d, d_Snapshot_NoDelay, !dbox_getnumeric (d,
              d_Snapshot_UserDef));
          handled = TRUE;
        break;
      }
    break;

    /*Fix MED-3201: removed this! J R C 8th Jun 1994*/
    /*case wimp_EKEY:
      handled = TRUE;
    break;*/
  }

  /*Make sure the User defined seconds field is faded if necessary.*/
  if (!dbox_getnumeric (d, d_Snapshot_UserDef))
    dbox_fadefield (d, d_Snapshot_UserSec);
  else
    dbox_unfadefield (d, d_Snapshot_UserSec);

  return handled;
}

/*show snapshot window*/
void main_snapshot_show (void)

{ BOOL open;
  dbox d;

  ftracef0 ("main_shapshot_show\n");
  if (sshot.active)
  { werr (FALSE, msgs_lookup ("PntHF"));
    return;
  }

  ftracef0 ("showing snapshot\n");

  if ((d = dbox_new ("snapshot")) == 0)
    return;

  /*Set all fields to default values.*/
  dbox_setnumeric (d, d_Snapshot_NoDelay,  !sshot.delay);
  dbox_setnumeric (d, d_Snapshot_UserDef,   sshot.delay);
  dbox_setnumeric (d, d_Snapshot_UserSec,   sshot.user_sec);
  dbox_setnumeric (d, d_Snapshot_WholeScr,  sshot.whole_screen);
  dbox_setnumeric (d, d_Snapshot_ShowTimer, sshot.want_timer);

  /*Make sure the User defined seconds field is faded if necessary.*/
  if (!sshot.delay)
    dbox_fadefield (d, d_Snapshot_UserSec);
  else
    dbox_unfadefield (d, d_Snapshot_UserSec);

  /*Supply raw event handler for help messages etc.*/
  dbox_raw_eventhandler (d, &snapshot_raw_event_handler, NULL);

  ftracef4 ("created dbox: delay %d, user_sec %d, "
      "whole_screen %d, want_timer %d\n", sshot.delay, sshot.user_sec,
      sshot.whole_screen, sshot.want_timer);

  dbox_show (d);
  open = TRUE;

  while (open)
  { wimp_i i = dbox_fillin (d);

    ftracef1 ("event on icon %d\n", i);

    switch (i)
    { case d_Snapshot_Go:
        ftracef0 ("Get current values from template\n");
        sshot.active        = TRUE;
        sshot.first         = TRUE;
        sshot.delay         = dbox_getnumeric (d, d_Snapshot_UserDef);
        sshot.user_sec      = dbox_getnumeric (d, d_Snapshot_UserSec);
        sshot.whole_screen  = dbox_getnumeric (d, d_Snapshot_WholeScr);
        sshot.want_timer    = dbox_getnumeric (d, d_Snapshot_ShowTimer);

        ftracef4 ("read dbox: delay %d, snap_now %s, "
            "whole_screen %s, want_timer %s\n", sshot.delay,
            WHETHER (sshot.snap_now), WHETHER (sshot.whole_screen),
            WHETHER (sshot.want_timer));

        /*Set an immediate alarm to do all this.*/
        alarm_set (alarm_timenow (), &Snapshot_Cb, NULL);

        dbox_hide (d);
        open = FALSE;
      break;

      case dbox_CLOSE:
        open = FALSE;
      break;
    }
  }

  dbox_dispose (&d);
}

/* Claim idle events */
/* From RISCOSlib on (rather than cwimp, when we claim idle events, we must
   change the wimp's event mask
 */
void main_claim_idle (wimp_w window)

{ ftracef0 ("main_claim_idle\n");
  event_setmask ((wimp_emask) (window == -1? event_getmask () | wimp_EMNULL:
      event_getmask () & ~wimp_EMNULL));
  win_claim_idle_events (window);
}

os_error *main_error (char *token)

{ static os_error Error;

  ftracef0 ("main_error\n");

  Error.errnum = 1;
  sprintf (Error.errmess, "%.*s", sizeof Error.errmess - 1,
      msgs_lookup (token));

  return &Error;
}

static int ramfetch_buffer_extender (char **buffer, int *size)

{ int currsize = flex_size ((flex_ptr) &ramfetch_buffer);

  ftracef1 ("ramfetch_buffer_extender: extend ram buffer from %d\n",
      currsize);
  *buffer = (char *) ramfetch_buffer + currsize;
  *size = 256;
  return flex_extend ((flex_ptr) &ramfetch_buffer, currsize + 256);
}

912
BOOL ramtransfer_file (wimp_msgdatasave *ms)
Neil Turton's avatar
Neil Turton committed
913

914
{ int size, import = -1, file_type, sprite_size,
Neil Turton's avatar
Neil Turton committed
915 916 917 918 919 920 921
      ram_size, log2_bpp = bbc_modevar (-1, bbc_Log2BPP);
  sprite_area *area;
  sprite_header *header;

  ftracef0 ("ramtransfer_file\n");
  /*only interested in sprites*/
      /*and jpeg's. J R C 27th Jun 1994*/
922
  if (!((file_type = xferrecv_checkimport (&size)) == FileType_Sprite ||
923
      file_type == FileType_JPEG))
Neil Turton's avatar
Neil Turton committed
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
  { werr (FALSE, msgs_lookup ("PntEB"), ms->leaf);
    return FALSE;
  }

  /*Don't allocate a buffer of size nought - it's wrong! 6 Aug 1991*/
  if (size <= 0) size = 256;

  ftracef1 ("flexalloc RAM buffer size %d\n", size);
  if (!flex_alloc ((flex_ptr) &ramfetch_buffer, size))
  { main_NO_ROOM ("RAM buffer");
    return FALSE;
  }

  /*Initialise the area (harmless for JPEG's). If no bytes arrive, it's
    empty. If 4 or 8 bytes arrive, we're dead.*/
  ((int *) ramfetch_buffer) [0] = 0;
  ((int *) ramfetch_buffer) [1] = 16;
  ((int *) ramfetch_buffer) [2] = 16;

  import = xferrecv_doimport (ramfetch_buffer, size,
      &ramfetch_buffer_extender);
  ftracef1 ("doimport returned %d\n", import);

  if (import == -1)
  { flex_free ((flex_ptr) &ramfetch_buffer);
    return FALSE;
  }

952 953 954 955 956 957
  if (file_type == FileType_JPEG)
  { jpeg_id   jid;
    jpeg_info jinfo;

    /*For JPEG's, we find it convenient to read the file data into
      |ramfetch_buffer| (since that is how ramfetch_buffer_extender() is set
Neil Turton's avatar
Neil Turton committed
958 959 960 961
      up), then read the consequent sprite size, malloc that much space,
      paint the sprite and copy it back into |ramfetch_buffer|.*/
    ram_size = flex_size ((flex_ptr) &ramfetch_buffer);

962 963 964 965 966 967
#ifdef XTRACE
    { char cmd [128];
      sprintf (cmd, "%%Save $.JPEG %X +%X", ramfetch_buffer, ram_size);
      os_cli (cmd);
    }
#endif
Neil Turton's avatar
Neil Turton committed
968

969 970 971 972
    jid.s.image.addr = ramfetch_buffer;
    jid.s.image.size = ram_size;
    jid.tag = jpeg_id_addr;
    if (wimpt_complain (jpeg_readinfo (&jid, &jinfo)) != NULL)
Neil Turton's avatar
Neil Turton committed
973 974 975 976
    { flex_free ((flex_ptr) &ramfetch_buffer);
      return FALSE;
    }

977 978
    sprite_size = psprite_size (jinfo.width, jinfo.height, wimpt_mode (),
                                FALSE /*mask?*/, 2 /*full palette*/);
Neil Turton's avatar
Neil Turton committed
979

Robert Sprowson's avatar
Robert Sprowson committed
980
    if ((area = m_ALLOC (sprite_size + sizeof (sprite_area))) == NULL)
Neil Turton's avatar
Neil Turton committed
981 982 983 984
    { flex_free ((flex_ptr) &ramfetch_buffer);
      return FALSE;
    }

985 986 987
    /*Create an empty sprite called '!newjpeg' in area*/
    header = main_make_newjpeg(area, sprite_size, &jinfo);

Neil Turton's avatar
Neil Turton committed
988
    /*Set the name with a sprite op to get the case right.*/
Robert Sprowson's avatar
Robert Sprowson committed
989 990
    if (wimpt_complain (os_swix4 (OS_SpriteOp, 0x1A | 512,
                                  area, header, ms->leaf)) != NULL)
Neil Turton's avatar
Neil Turton committed
991
      return -1;
Robert Sprowson's avatar
Robert Sprowson committed
992 993 994
    ftracef5 ("SPRITE %.12s: mode 0x%X, width %d words, height %d pixels, rbit %d\n",
              header->name, header->mode, 1 + header->width, 1 + header->height,
              header->rbit);
Neil Turton's avatar
Neil Turton committed
995 996 997 998

    if (log2_bpp <= 3)
      /*Set the sprite's palette to be the same as the screen one. JRC 6th Feb
          1995.*/
Robert Sprowson's avatar
Robert Sprowson committed
999 1000 1001
      if (wimpt_complain (os_swix5 (ColourTrans_ReadPalette, -1, -1,
                                    header + 1, 8 << (1 << log2_bpp), 1 << 1)) != NULL)
      { m_FREE (area, sprite_size + sizeof (sprite_area));
Neil Turton's avatar
Neil Turton committed
1002 1003 1004 1005
        flex_free ((flex_ptr) &ramfetch_buffer);
        return FALSE;
      }

1006 1007
    jid.s.image.addr = ramfetch_buffer; /*Might have shifted*/
    if (wimpt_complain (main_plot_fromjpeg (area, header, &jid)) != NULL)
Robert Sprowson's avatar
Robert Sprowson committed
1008
    { m_FREE (area, sprite_size + sizeof (sprite_area));
Neil Turton's avatar
Neil Turton committed
1009 1010 1011 1012
      flex_free ((flex_ptr) &ramfetch_buffer);
      return FALSE;
    }

1013
    if (!flex_extend ((flex_ptr) &ramfetch_buffer, sprite_size + sizeof (sprite_area)))
Robert Sprowson's avatar
Robert Sprowson committed
1014
    { m_FREE (area, sprite_size + sizeof (sprite_area));
Neil Turton's avatar
Neil Turton committed
1015 1016 1017 1018
      flex_free ((flex_ptr) &ramfetch_buffer);
      return FALSE;
    }

1019 1020 1021 1022 1023 1024 1025
    /*Make it look like a sprite file by trimming off 4 bytes*/
    memcpy (ramfetch_buffer, &area->number,
                             sprite_size + sizeof (sprite_area) - sizeof (area->size));
#ifdef XTRACE
    { char cmd [128];
      sprintf (cmd, "%%Save $.Sprite %X +%X", &area->size,
                    sprite_size + sizeof (sprite_area) - sizeof (area->size));
Neil Turton's avatar
Neil Turton committed
1026 1027
      os_cli (cmd);
    }
1028
#endif
Neil Turton's avatar
Neil Turton committed
1029

Robert Sprowson's avatar
Robert Sprowson committed
1030
    m_FREE (area, sprite_size + sizeof (sprite_area));
Neil Turton's avatar
Neil Turton committed
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
  }

  return TRUE;
}

/***********************************
 * Force complete redraw of window *
 ***********************************/

void main_force_redraw (wimp_w handle)

{ wimp_redrawstr rds;

  ftracef0 ("main_force_redraw\n");
  rds.w   = handle;
  rds.box = main_big_extent;
Robert Sprowson's avatar
Robert Sprowson committed
1047
  ftracef0 ("do wimp_force_redraw\n");
Neil Turton's avatar
Neil Turton committed
1048
  wimpt_noerr (wimp_force_redraw (&rds));
Robert Sprowson's avatar
Robert Sprowson committed
1049
  ftracef0 ("done\n");
Neil Turton's avatar
Neil Turton committed
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
}

/************************************+**
 *                                     *
 *  Set the extent of a file window.   *
 *                                     *
 ***************************************/

void main_set_extent (main_window *window)

{ int nsprites, width, height, x, y, no_across, no_down;
  wimp_winfo curr;
  wimp_redrawstr newext;
  char *name = window->data->file.filename;
  BOOL changed = FALSE;

  ftracef0 ("main_set_extent\n");
  if (window->data->file.fullinfo)
    x = main_FILER_FullInfoWidth, y = main_FILER_FullInfoHeight;
  else
    x = main_FILER_TotalWidth, y = main_FILER_TotalHeight;

  curr.w = window->handle;
Robert Sprowson's avatar
Robert Sprowson committed
1073
  wimpt_noerr (paintlib_get_wind_info (&curr));
Neil Turton's avatar
Neil Turton committed
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 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

  nsprites = window->data->file.spritearea->number;
  ftracef1 ("main_set_extent: %d sprites across\n", nsprites);

  /*title width*/
  width = main_FILER_TextWidth*((name == NULL? 12: strlen (name)) + 10);
  if (width < x*nsprites) width = x*nsprites;
  ftracef1 ("main_set_extent: x extent is %d\n", width);

  no_across = (curr.info.box.x1 - curr.info.box.x0)/x;
  if (no_across < 1) no_across = 1;
  ftracef1 ("main_set_extent: room for %d sprites\n", no_across);

  if (no_across == window->data->file.lastwidth)
  { ftracef0 ("main_set_extent: same number - nothing to do\n");
    return;
  }

  no_down = (nsprites + no_across - 1)/no_across;
  if (no_down < 1) no_down = 1;
  ftracef1 ("main_set_extent: %d sprites down\n", no_down);

  height = y*no_down;

  newext.box.x0 = 0;
  newext.box.y0 =-height;
  newext.box.x1 = width;
  newext.box.y1 = SPACE_FOR_HELP_TEXT;

  if (curr.info.box.x1 - curr.info.box.x0 != width)
  { ftracef0 ("Resetting displayed width\n");
    changed = TRUE;
    curr.info.box.x1 = curr.info.box.x0 + width;
  }

  if (curr.info.box.y1 - curr.info.box.y0 != height + SPACE_FOR_HELP_TEXT)
  { ftracef0 ("Resetting displayed height\n");
    changed = TRUE;
    curr.info.box.y0 = curr.info.box.y1 - (height + SPACE_FOR_HELP_TEXT);
  }

  if (changed)
  { wimp_redrawstr rds;

    newext.w = window->handle;
    wimpt_noerr (wimp_set_extent (&newext));

    curr.w = window->handle;
Robert Sprowson's avatar
Robert Sprowson committed
1122
    wimpt_noerr (paintlib_get_wind_info (&curr));
Neil Turton's avatar
Neil Turton committed
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132

    ftracef0 ("Really resetting\n");
    wimpt_noerr (wimp_open_wind ((wimp_openstr *) &curr));
    rds.w   = window->handle;
    rds.box = curr.info.ex;

    wimp_force_redraw (&rds);

    window->data->file.lastwidth = no_across; /*avoid ghastly flicker. J R C
        18th Oct 1993*/
Robert Sprowson's avatar
Robert Sprowson committed
1133 1134
  }
}
Neil Turton's avatar
Neil Turton committed
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153

/**************************
 *                        *
 *  Window killer         *
 *                        *
 **************************/

void main_delete_window (wimp_w h)

{ ftracef0 ("main_delete_window\n");
  win_register_event_handler (h, (win_event_handler) 0, NULL);
  event_attachmenu (h, (menu) 0, NULL, NULL);
  wimp_delete_wind (h);
}

void main_window_delete (main_window *thiswindow)

{ main_window *window = (main_window *) &main_windows;

Robert Sprowson's avatar
Robert Sprowson committed
1154
  altrename_delete ();
1155

Neil Turton's avatar
Neil Turton committed
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
  ftracef2 ("main_window_delete: killing %s window: %d\n",
      thiswindow->tag == main_window_is_file? "file": "sprite",
      thiswindow->handle);

  while (window->link != thiswindow && window->link != NULL)
    window = window->link;

  if (window->link != NULL)
  { window->link = thiswindow->link;
    main_delete_window (thiswindow->handle);
    m_FREE (thiswindow->data,
        thiswindow->tag == main_window_is_file? sizeof (main_file):
        sizeof (main_sprite_window));
    m_FREE (thiswindow, sizeof (main_window));
    win_activedec ();
Robert Sprowson's avatar
Robert Sprowson committed
1171 1172
  }
}
Neil Turton's avatar
Neil Turton committed
1173 1174 1175 1176 1177 1178 1179

/**************************
 *  File window killer    *
 **************************/

static void delete_file_window (main_window *window)

1180 1181 1182
{ main_sprite *sprite;
  main_file *file = &window->data->file;
  int loop = 0, count = 0;
Neil Turton's avatar
Neil Turton committed
1183 1184

  ftracef0 ("delete_file_window\n");
1185

Robert Sprowson's avatar
Robert Sprowson committed
1186
  for (sprite = file->sprites; sprite != NULL; sprite = sprite->link)
1187 1188 1189 1190 1191
    count++;
  if (count)
  { main_sprite *todelete[count];

    /* First copy all their pointers into an array */
Robert Sprowson's avatar
Robert Sprowson committed
1192
    for (sprite = file->sprites; sprite != NULL; sprite = sprite->link)
1193
    { todelete[loop] = sprite;
1194
      loop++;
1195
    }
1196

1197 1198 1199
    /* Delete all the pointers in the array backwards (flex faster) */
    for (loop=count-1;loop>=0;loop--)
      psprite_delete (window, todelete[loop]);
Neil Turton's avatar
Neil Turton committed
1200

1201 1202
    ftracef0 ("deleted all sprite blocks\n");
  }
Neil Turton's avatar
Neil Turton committed
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 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318

  flex_free ((flex_ptr) &file->spritearea);
  if (file->filename != NULL) flex_free ((flex_ptr) &file->filename);

  m_FREE (file->title, 256);

  main_window_delete (window);
}


/***************************************************************************
 *                                                                         *
 *  Set the (file) window title to the given string.                       *
 *                                                                         *
 ***************************************************************************/

void main_set_title (main_window *window, char *name)

{ wimp_redrawstr r;
  wimp_wstate currinfo;

  ftracef1 ("main_set_title: Resetting window title to '%s'\n", name);
  strcpy (window->data->file.title, name);

  if (window->data->file.filename == NULL)
  { ftracef0 ("allocate filename flex\n");
    flex_alloc ((flex_ptr) &window->data->file.filename, strlen (name) + 1);
  }
  else
  { ftracef0 ("extend filename flex\n");
    flex_extend ((flex_ptr) &window->data->file.filename, strlen (name) +1);
  }

  strcpy (window->data->file.filename, name);
  wimpt_noerr (wimp_get_wind_state (window->handle, &currinfo));
  r.w = window->handle;
  wimp_getwindowoutline (&r);

  r.w = -1;
  r.box.y0 = currinfo.o.box.y1;
  wimp_force_redraw (&r);
}

void main_set_modified (main_file *file)

{ ftracef0 ("main_set_modified\n");
  if (!file->modified)
  { wimp_redrawstr r;
    wimp_wstate currinfo;

    if (strlen (file->title) < 256 - 2) strcat (file->title, " *");
    wimpt_noerr (wimp_get_wind_state (file->window->handle,&currinfo));
    r.w = file->window->handle;
    wimp_getwindowoutline (&r);

    r.w = -1;
    r.box.y0 = currinfo.o.box.y1;
    wimp_force_redraw (&r);
  }

  file->modified = 1;
}

static int startx;
static int starty;
static int nextx;
static int nexty;

void main_allocate_position (wimp_box *box)

{ ftracef0 ("main_allocate_position\n");
  box->x1 += nextx - box->x0;
  box->x0  = nextx;
  box->y0 += nexty - box->y1;
  box->y1  = nexty;
  nexty   -= 48;
}

void main_check_position (main_window *w)

{ int zap = 0;
  wimp_wstate currinfo;
  wimpt_noerr (wimp_get_wind_state (w->handle,&currinfo));

  ftracef0 ("main_check_position\n");
  if (currinfo.o.box.y1 != nexty + 48)
  { currinfo.o.box.y0 += nexty+48-currinfo.o.box.y1;
    currinfo.o.box.y1  = nexty+48;
    zap = 1;
  }

  if (currinfo.o.box.y0 < 140)
  { nexty = starty;
    currinfo.o.box.y0 += nexty-currinfo.o.box.y1;
    currinfo.o.box.y1  = nexty;
    nexty -= 48;
    zap = 1;
  }

  if (zap)
  { wimpt_noerr (wimp_close_wind (w->handle));
    wimpt_noerr (wimp_open_wind (&currinfo.o));
  }
}

/***************************************************************************
 *                                                                         *
 *  Load the given spritefile into the given window, returning error       *
 *   indicator.  Merge with current area if merge flag set                 *
 * return > 0: worked fine                                                 *
 *          0: file type was recognised                                    *
 *         -1: failed but data still intact                                *
 *        <-1: really failed; window wiped out                             *
 *                                                                         *
 ***************************************************************************/

1319
int Load_File (main_window *window, char *filename, int merge,
Neil Turton's avatar
Neil Turton committed
1320 1321 1322
    int safe)

{ int rc = 1, i = 0, offset, ramcopy = filename == (char *) -1,
1323
    temp_file = !(safe || xferrecv_file_is_safe ()), file_type = FileType_Sprite,
1324
    sprite_size,
Neil Turton's avatar
Neil Turton committed
1325 1326 1327 1328
    log2_bpp = bbc_modevar (-1, bbc_Log2BPP);
  os_filestr filestr;
  main_sprite *sprite, **sprptr;
  sprite_header *header;
1329
  main_file *file;
Neil Turton's avatar
Neil Turton committed
1330
  wimp_winfo curr;
1331 1332
  jpeg_id jid;
  jpeg_info jinfo;
Neil Turton's avatar
Neil Turton committed
1333

1334 1335
  if (window->selection.flags & MW_SELSAVING) return 0;

Neil Turton's avatar
Neil Turton committed
1336 1337
  ftracef0 ("Load_File\n");

Robert Sprowson's avatar
Robert Sprowson committed
1338
  altrename_delete ();
1339

1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
  /* If there's a selection present, delete it (we overwrite selections now) */
  if (window->selection.count != 0)
  { char hit [2];
    hit [0] = m_Sprite;
    hit [1] = m_Sprite_Delete;
    menus_file_handler(window, hit);
  }

  file = &window->data->file; /* Need to do this after any deletion (block may move) */

Neil Turton's avatar
Neil Turton committed
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
  ftracef4 ("Asked to %s %s %s into window 0x%X\n",
      merge? "merge": "load",
      ramcopy? "RAM": "file",
      ramcopy? "buffer": filename,
      window->handle);

  /*Get the total size |filestr.start| of the sprite(s) to be loaded.*/
  if (!ramcopy)
  { filestr.name   = filename;
    filestr.action = 5;
    os_file (&filestr);

    if (filestr.action != 1)
    { filestr.loadaddr = filestr.action;
      filestr.name     = filename;
      filestr.action   = 19;
      wimpt_complain (os_file (&filestr));
      return 0;      /* failed */
    }

    file_type = (filestr.loadaddr & 0xFFF00) >> 8;
1371
    if (!(file_type == FileType_Sprite || file_type == FileType_JPEG))
Neil Turton's avatar
Neil Turton committed
1372 1373
    { werr (FALSE, msgs_lookup ("PntEB"), filename);
      return 0;      /* failed */
Robert Sprowson's avatar
Robert Sprowson committed
1374 1375
    }
  }
Neil Turton's avatar
Neil Turton committed
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
  else
    filestr.start = flex_size ((flex_ptr) &ramfetch_buffer);

  /*Set the file name.*/
  if (!(merge || ramcopy || temp_file))
  { if (window->data->file.filename == NULL)
    { if (!(flex_alloc ((flex_ptr) &file->filename, strlen (filename) + 1)))
      { main_NO_ROOM ("file title");
        return -1;
      }
    }
    else
    { if (!flex_extend ((flex_ptr) &file->filename, strlen (filename) + 1))
      { main_NO_ROOM ("extended file title");
        return -1;
Robert Sprowson's avatar
Robert Sprowson committed
1391 1392 1393
      }
    }
  }
Neil Turton's avatar
Neil Turton committed
1394 1395 1396 1397 1398 1399

  /*If ramcopy or merge, read the stuff to be loaded into ramfetch_buffer
    (already there if ramcopy). Otherwise (filecopy and not merge), just make
    sure that file->spritearea is big enough to load the file into.*/
  if (!ramcopy)
  { /*Copying from a file.*/
1400
    if (file_type == FileType_Sprite)
Neil Turton's avatar
Neil Turton committed
1401 1402
      sprite_size = filestr.start - 12;
    else
1403 1404 1405
    { jid.s.name = filename;
      jid.tag = jpeg_id_name;
      if (wimpt_complain (jpeg_readinfo (&jid, &jinfo)))
Neil Turton's avatar
Neil Turton committed
1406
        return 0;
1407
      ftracef2 ("JPEG info: %d x %d\n", jinfo.width, jinfo.height);
Neil Turton's avatar
Neil Turton committed
1408

1409 1410
      sprite_size = psprite_size (jinfo.width, jinfo.height, wimpt_mode (),
                                  FALSE /*mask?*/, 2 /*full palette*/);
Neil Turton's avatar
Neil Turton committed
1411 1412 1413 1414 1415
    }

    if (merge)
    { /*filecopy && merge*/
      ftracef0 ("allocate ram buffer for file\n");
Robert Sprowson's avatar
Robert Sprowson committed
1416
      if (!flex_alloc ((flex_ptr) &ramfetch_buffer, sprite_size + sizeof (sprite_area)))
Neil Turton's avatar
Neil Turton committed
1417 1418 1419 1420 1421
      { werr (FALSE, msgs_lookup ("PntEG"));
        return -1;
      }

      ftracef0 ("load file into ram buffer\n");
1422
      if (file_type == FileType_Sprite)
Robert Sprowson's avatar
Robert Sprowson committed
1423 1424
      { ftracef1 ("writing size (%d) at front of buffer\n", sprite_size + sizeof (sprite_area));
        *(int *) ramfetch_buffer = sprite_size + sizeof (sprite_area);
Neil Turton's avatar
Neil Turton committed
1425 1426 1427 1428 1429 1430
            /* convert into sprite area */

        if (wimpt_complain (sprite_area_load
            ((sprite_area *) ramfetch_buffer, filename)))
        { flex_free ((flex_ptr) &ramfetch_buffer);
          return -1;
Robert Sprowson's avatar
Robert Sprowson committed
1431 1432
        }
      }
Neil Turton's avatar
Neil Turton committed
1433 1434 1435
      else
      { char *cc;

1436 1437
        /*Create an empty sprite called '!newjpeg' in ramfetch_buffer*/
        header = main_make_newjpeg((sprite_area *)ramfetch_buffer, sprite_size, &jinfo);
Robert Sprowson's avatar
Robert Sprowson committed
1438

Neil Turton's avatar
Neil Turton committed
1439
        /*Set the name with a sprite op to get the case right.*/
Robert Sprowson's avatar
Robert Sprowson committed
1440 1441 1442 1443
        if (wimpt_complain (os_swix4 (OS_SpriteOp, 0x1A | 512,
                            file->spritearea, header,
                            (cc = strrchr (filename, '.')) != NULL ||
                            (cc = strrchr (filename, ':')) != NULL ? cc + 1 : "jpeg")) != NULL)
Neil Turton's avatar
Neil Turton committed
1444
          return -1;
Robert Sprowson's avatar
Robert Sprowson committed
1445 1446 1447
        ftracef5 ("SPRITE %.12s: mode 0x%X, width %d words, height %d pixels, rbit %d\n",
                  header->name, header->mode, 1 + header->width, 1 + header->height,
                  header->rbit);
Neil Turton's avatar
Neil Turton committed
1448 1449 1450 1451

        if (log2_bpp <= 3)
          /*Set the sprite's palette to be the same as the screen one. JRC 6th
              Feb 1995.*/
Robert Sprowson's avatar
Robert Sprowson committed
1452 1453
          if (wimpt_complain (os_swix5 (ColourTrans_ReadPalette, -1, -1,
                                        header + 1, 8 << (1 << log2_bpp), 1 << 1)) != NULL)
Neil Turton's avatar
Neil Turton committed
1454 1455
            return -1;

1456 1457
          if (wimpt_complain (main_plot_fromjpeg ((sprite_area *)ramfetch_buffer, header, &jid)) != NULL)
            return -1;
Neil Turton's avatar
Neil Turton committed
1458 1459 1460 1461 1462 1463

        rc = 1;
      }
    }
    else
    { /*filecopy && !merge*/
Robert Sprowson's avatar
Robert Sprowson committed
1464
      if (!menus_ensure_size (&file->spritearea, sprite_size + sizeof (sprite_area)))
Neil Turton's avatar
Neil Turton committed
1465 1466 1467 1468 1469
      { werr (FALSE, msgs_lookup ("PntEG"));
        return -1;
      }

      ftracef0 ("initialise sprite area\n");
Robert Sprowson's avatar
Robert Sprowson committed
1470
      sprite_area_initialise (file->spritearea, sprite_size + sizeof (sprite_area));
Neil Turton's avatar
Neil Turton committed
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
    }
  }
  else /*ramcopy - file contents already converted to sprite*/
  { sprite_size = flex_size ((flex_ptr) &ramfetch_buffer) - 12;

    if (!flex_midextend ((flex_ptr) &ramfetch_buffer, 0, 4))
    { main_NO_ROOM ("4 bytes for ram buffer");
      flex_free ((flex_ptr) &ramfetch_buffer);
      return -1;
    }

Robert Sprowson's avatar
Robert Sprowson committed
1482 1483
    ftracef1 ("writing size (%d) at front of buffer\n", sprite_size + sizeof (sprite_area));
    *(int *) ramfetch_buffer = sprite_size + sizeof (sprite_area);
Neil Turton's avatar
Neil Turton committed
1484 1485 1486
        /* convert into sprite area */

    ftracef0 ("verify area ...\n"); /*J R C 18th Nov 1994*/
Robert Sprowson's avatar
Robert Sprowson committed
1487
    if (wimpt_complain (os_swix2 (OS_SpriteOp, 512 | 17, ramfetch_buffer)))
Neil Turton's avatar
Neil Turton committed
1488 1489 1490 1491 1492 1493 1494 1495
    { ftracef0 ("area invalid\n");
      return NULL;
    }
    ftracef0 ("area valid\n");
  }

  if (ramcopy || merge)
  { ftracef0 ("copy all the ram sprites across\n");
Robert Sprowson's avatar
Robert Sprowson committed
1496
    rc = psprite_merge_area (window, file, (sprite_area **) &ramfetch_buffer)? 1:
Neil Turton's avatar
Neil Turton committed
1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
          -1;
    flex_free ((flex_ptr) &ramfetch_buffer);
  }
  else
  { ftracef0 ("full file load\n");

  #if TRACE
    if (file->sprites != NULL)
      werr (TRUE, "file not empty");
  #endif

    ftracef2 ("Load_File: loading \"%s\" into area 0x%X\n",
        filename, file->spritearea);
1510
    if (file_type == FileType_Sprite)
Neil Turton's avatar
Neil Turton committed
1511 1512 1513 1514
    { if (wimpt_complain (sprite_area_load (file->spritearea, filename)))
        return NULL;

      ftracef0 ("verify area ...\n"); /*J R C 7th Nov 1994*/
Robert Sprowson's avatar
Robert Sprowson committed
1515
      if (wimpt_complain (os_swix2 (OS_SpriteOp, 512 | 17, file->spritearea)))
Neil Turton's avatar
Neil Turton committed
1516 1517 1518 1519 1520 1521 1522 1523
      { ftracef0 ("area invalid\n");
        return NULL;
      }
      ftracef0 ("area valid\n");
    }
    else
    { char *cc;

1524 1525
      /*Create an empty sprite called '!newjpeg' in file->spritearea*/
      header = main_make_newjpeg(file->spritearea, sprite_size, &jinfo);
Robert Sprowson's avatar
Robert Sprowson committed
1526

Neil Turton's avatar
Neil Turton committed
1527
      /*Set the name with a sprite op to get the case right.*/
Robert Sprowson's avatar
Robert Sprowson committed
1528 1529 1530 1531
      if (wimpt_complain (os_swix4 (OS_SpriteOp, 0x1A | 512,
                          file->spritearea, header,
                          (cc = strrchr (filename, '.')) != NULL ||
                          (cc = strrchr (filename, ':')) != NULL ? cc + 1 : "jpeg")) != NULL)
Neil Turton's avatar
Neil Turton committed
1532
        return -1;
Robert Sprowson's avatar
Robert Sprowson committed
1533 1534 1535
      ftracef5 ("SPRITE %.12s: mode 0x%X, width %d words, height %d pixels, rbit %d\n",
                header->name, header->mode, 1 + header->width, 1 + header->height,
                header->rbit);
Neil Turton's avatar
Neil Turton committed
1536 1537 1538 1539

      if (log2_bpp <= 3)
        /*Set the sprite's palette to be the same as the screen one. JRC 6th
          Feb 1995.*/
Robert Sprowson's avatar
Robert Sprowson committed
1540 1541
        if (wimpt_complain (os_swix5 (ColourTrans_ReadPalette, -1, -1,
                                      header + 1, 8 << (1 << log2_bpp), 1 << 1)) != NULL)
Neil Turton's avatar
Neil Turton committed
1542 1543
          rc = -1;

1544
      if (wimpt_complain (main_plot_fromjpeg (file->spritearea, header, &jid)) != NULL)
Neil Turton's avatar
Neil Turton committed
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
        rc = -1;
    }

    if (!temp_file) strcpy (file->filename, filename);

    sprptr = &file->sprites;
    for (offset = psprite_first (&file->spritearea); offset != 0;
        offset = psprite_next (&file->spritearea, offset))
    { ftracef2 ("area 0x%X sproff 0x%X\n", file->spritearea, offset);

      if ((*sprptr = psprite_new (offset, i++, file)) == NULL)
      { delete_file_window (window);
        return -2;    /* really couldn't cope */
      }

      sprptr = &(*sprptr)->link; /* keep at end of list */
Robert Sprowson's avatar
Robert Sprowson committed
1561 1562
    }
  }
Neil Turton's avatar
Neil Turton committed
1563 1564 1565 1566 1567 1568 1569 1570

  if (merge) main_set_modified (file);
  psprite_set_plot_info (file);
  psprite_set_colour_info (file);
  psprite_set_brush_translations (file);

  /* now shrink box if not enough sprites to fill it */
  curr.w = window->handle;
Robert Sprowson's avatar
Robert Sprowson committed
1571
  wimpt_noerr (paintlib_get_wind_info (&curr));
Neil Turton's avatar
Neil Turton committed
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
  if (curr.info.box.x1-curr.info.box.x0 >
      file->spritearea->number*main_FILER_TotalWidth)
  { curr.info.box.x1 = curr.info.box.x0 +
        file->spritearea->number*main_FILER_TotalWidth;
    wimpt_noerr (wimp_open_wind ((wimp_openstr *) &curr));
  }

  if ((!merge || window->data->file.filename == NULL) &&
      !(ramcopy || temp_file))
  { /* Clear modified flag and redraw title */
    file->modified = 0;
    main_set_title (window, filename);
  }
  else
    /* force summary window to be updated */
    file->lastwidth = 0; /*was
        main_force_redraw (window->handle); J R C 18th Oct 1993*/

  main_set_extent (window);

Robert Sprowson's avatar
Robert Sprowson committed
1592
  for (sprite = file->sprites; sprite != NULL; sprite = sprite->link)
Neil Turton's avatar
Neil Turton committed
1593 1594 1595 1596 1597 1598 1599 1600
  { main_sprite_window *sprw;

    for (sprw = sprite->windows; sprw != NULL; sprw = sprw->link)
      sprwindow_set_work_extent (sprw->window, TRUE);

    if (sprite->colourdialogue == 0)
      colours_set_extent (sprite); /*was main_force_redraw
                                 (sprite->colourhandle); JRC 4th Dec '89*/
ROOL's avatar
ROOL committed
1601 1602 1603
    sprw = sprite->windows;
    if (sprw != NULL)
      colourpanel_redraw (sprw->window);
Neil Turton's avatar
Neil Turton committed
1604 1605 1606 1607 1608 1609
  }

  ftracef0 ("Loaded file\n");
  return rc;
}

Robert Sprowson's avatar
Robert Sprowson committed
1610 1611 1612
static void main_draw_icon (main_window *window, main_sprite *sprite, int x0, int y0, wimp_redrawstr *rds)

{ sprite_info infoblock;
1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
  int sx, sy;
  psprite_info sinfo;

  psprite_read_full_info (sprite, &sinfo);

  if (psprite_read_size (sprite, &infoblock))
    sinfo.width = -1, sinfo.height = -1;
  else
  { infoblock.width  *= sprite->iconsize.scale_xmul;
    infoblock.height *= sprite->iconsize.scale_ymul;
    infoblock.width  /= sprite->iconsize.scale_xdiv;
    infoblock.height /= sprite->iconsize.scale_ydiv;
  }

  if (window->data->file.fullinfo)
1628
  {
1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
    char sizebuf [20];

    main_iprintf ((sprite->flags & MSF_SELECTED?(wimp_ISELECTED | wimp_IFILLED):0),
        x0 + 6*main_FILER_TextWidth,
        y0 + 2*main_FILER_TextHeight,
        "%s", &sinfo.name);

    /*Check for silly numbers here. JRC 14 June 1990*/
    main_iprintf
    ( 0,
      x0 + 20*main_FILER_TextWidth,
      y0 + 2*main_FILER_TextHeight,
      msgs_lookup
      ( sinfo.width == -1 || sinfo.height == -1?
        "PntW22":
        "PntW21"
      ),
      sinfo.width,
      sinfo.height
    );

    if ((unsigned) sinfo.mode < 256)
      main_iprintf (0,
          x0 + 33*main_FILER_TextWidth,
          y0 + 2*main_FILER_TextHeight,
          msgs_lookup ("PntW23"),
          sinfo.mode);
    else
1657 1658
    {
      char *ncol = psprite_get_colours(sinfo.mode);
1659 1660 1661
      main_iprintf (0,
          x0 + 33*main_FILER_TextWidth,
          y0 + 2*main_FILER_TextHeight,
1662
          msgs_lookup ("PntW24"), ncol);
1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
    }

    (void) os_swix3 (OS_ConvertFixedFileSize, sinfo.size, sizebuf,
        sizeof sizebuf);

    main_iprintf (wimp_IRJUST,
        x0 + 55*main_FILER_TextWidth,
        y0 + 2*main_FILER_TextHeight,
        "%s", sizebuf);

1673 1674 1675 1676 1677 1678 1679
    static char *masktypes[] =
    {
      "PntW6", // transparency_type_none
      "PntW5", // transparency_type_onoffmask
      "PntWD", // transparency_type_alphamask
      "PntWE", // transparency_type_alphachannel
    };
1680 1681 1682 1683
    main_iprintf (0, x0 + 20*main_FILER_TextWidth,
        y0 + main_FILER_TextHeight,
        "%s, %s", msgs_lookup (sinfo.palette?
        sinfo.truepalette? "PntW3a": "PntW3": "PntW4"),
1684
        msgs_lookup (masktypes[psprite_transparency_type(sprite)]));
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707

    sx = x0 + main_FILER_Border/2;
    sy = y0 + main_FILER_Border/2;
  }
  else
  { main_iprintf (wimp_IHCENTRE |
        (sprite->flags & MSF_SELECTED?(wimp_ISELECTED | wimp_IFILLED):0),
        x0 + main_FILER_TotalWidth/2, y0 + main_FILER_TextHeight,
        "%.12s", sinfo.name);

    sx = x0 + (main_FILER_Border + main_FILER_XSize -
       infoblock.width)/2;
    sy = y0 + main_FILER_TotalHeight - (main_FILER_Border + main_FILER_YSize +
       infoblock.height)/2;
  }

  if (!(sinfo.width == -1 || sinfo.height == -1))
    wimpt_noerr (psprite_plot_scaled (sx + rds->box.x0 - rds->scx,
        sy + rds->box.y1 - rds->scy, sprite,
        &sprite->iconsize, (sprite->flags & MSF_SELECTED?3:0)));
}

/* bbox in work area coords */
Robert Sprowson's avatar
Robert Sprowson committed
1708 1709 1710
static void main_update_icon (main_window *window, main_sprite *sprite, wimp_box *bbox)

{ wimp_redrawstr ds;
1711
  int more;
1712
  os_error *e;
Robert Sprowson's avatar
Robert Sprowson committed
1713 1714 1715 1716

  ds.w = window->handle;
  ds.box = *bbox;

1717
  /* just redraw area containing higlighted parts in fullinfo to reduce flicker */
Robert Sprowson's avatar
Robert Sprowson committed
1718 1719 1720 1721
  if (window->data->file.fullinfo) ds.box.x1 = ds.box.x0+20*main_FILER_TextWidth;
  for (e = wimp_update_wind(&ds,&more); !e && more; e = wimp_get_rectangle(&ds, &more))
  { main_clear_background (&ds);
    main_draw_icon(window, sprite, bbox->x0, bbox->y0, &ds);
1722 1723 1724 1725 1726 1727 1728
  }
}

/********************************************************
 *  selections                                          *
 ********************************************************/
typedef enum
Robert Sprowson's avatar
Robert Sprowson committed
1729
{ SI_SELECT,
1730 1731 1732 1733
  SI_CLEAR,
  SI_TOGGLE
} select_icon_how;

1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744
void main_clear_other_selections(main_window *window)
{
  /* When we select an icon, ensure that there are no selections in other main windows (like the Filer) */
  /* Iterate the list of main_windows, but exclude the current one */

  main_window *ptr;
  for (ptr = main_windows; ptr != NULL; ptr = ptr->link)
    if (ptr != window && ptr->selection.count > 0)
      main_clear_all (ptr);
}

Robert Sprowson's avatar
Robert Sprowson committed
1745 1746
static void main_select_icon(main_window *window, main_sprite *sprite, wimp_box *iconbbox, select_icon_how how)

Robert Sprowson's avatar
Robert Sprowson committed
1747 1748
{ switch (how)
  { case SI_CLEAR: /* clear */
1749
       if (sprite->flags & MSF_SELECTED)
Robert Sprowson's avatar
Robert Sprowson committed
1750
       { sprite->flags &= ~MSF_SELECTED;
1751
         window->selection.count--;
Robert Sprowson's avatar
Robert Sprowson committed
1752
         main_update_icon (window, sprite, iconbbox);
1753 1754 1755 1756
       }
       break;
    case SI_SELECT: /* set */
       if (!(sprite->flags & MSF_SELECTED))
Robert Sprowson's avatar
Robert Sprowson committed
1757
       { sprite->flags |= MSF_SELECTED;
1758
         window->selection.count++;
Robert Sprowson's avatar
Robert Sprowson committed
1759
         main_update_icon (window, sprite, iconbbox);
1760
         main_clear_other_selections (window);
1761 1762 1763 1764
       }
       break;
    case SI_TOGGLE: /* toggle */
       if (sprite->flags & MSF_SELECTED)
Robert Sprowson's avatar
Robert Sprowson committed
1765
       { sprite->flags &= ~MSF_SELECTED;
1766
         window->selection.count--;
Robert Sprowson's avatar
Robert Sprowson committed
1767
         main_update_icon (window, sprite, iconbbox);
1768 1769
       }
       else