Menus 106 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 17 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 52 53 54 55 56 57 58 59
/* 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.Menus
 *
 *  Paint: Arthur 2 sprite editor
 *   Menu handling
 *
 *  Author: A.P. Thompson
 *  Others:
 *     DAHE David Elworthy
 *     JAB  James Bye
 *     CDP  Christopher Partington (Cambridge Systems Design)
 *     TMD  Tim Dobson
 *
 *  DAHE, 28 Aug  89 - internationalisation
 *         1 Sept 89 - made menu divisions symbolic
 *         6 Sept 89 - enable nulls during dboxtcol
 *  JAB,   3 Oct  90 - Changed sprite creation stuff
 *  JAB,  26 Oct  90 - Gave all dboxes raw event handlers for
 *                     interactive help purposes
 *  JAB,  30 Oct  90 - Added interactive menu help
 *  JAB,  22 Oct  90 - Now auto opens sprite window on sprite creation
 *  JAB,  04 Feb  91 - Fixed bug where lanscape/portrait always = portrait on
 *                     start up
 *  JAB,  06 May  91 - Added 'Misc' entry to sprite menu
 *  JAB,  08 May  91 - Added 'Misc' entry to sprite filer menu
 *  CDP,  20 Feb  92
 *     Added FIX9544: part of fix of G-RO-9544 (large shear factor causes
 *     arithmetic exception). Checks flex requests. See also PSprite.c.
 *     Added FIX0770: fixes RP-0770 (adjust size smaller causes crash) by
 *     substantial changes to menus_ensure_size (flex de/allocator).
 *     Added FIXDELSPR: fixes deletion of sprites causing crash, a bug that
 *     only showed up when the flex routine was sorted out.
 *  CDP,  27 Feb  92
 *     Added FIX0553: fixes a bug similar to RP-0553 (summary window not
 *     updated on create sprite) but copy rather than create sprite.
 *  TMD,  11 Mar  92
 *     Added FIX1764: fixes copy sprite to same name not giving error
 *  TMD,  12 Mar  92
 *     Disallow zero for width or height in 'Adjust size'
 */

#include <assert.h>
#include <ctype.h>
#include <stddef.h>
#include <swis.h>
#include <signal.h>
60
#include "Global/FileTypes.h"
61 62
#include "Global/VduExt.h"
#include "Global/Sprite.h"
Neil Turton's avatar
Neil Turton committed
63 64 65 66

#include "bbc.h"
#include "colourmenu.h"
#include "colourtran.h"
ROOL's avatar
ROOL committed
67
#include "dbox.h"
Neil Turton's avatar
Neil Turton committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
#include "dboxquery.h"
#include "dboxtcol.h"
#include "flex.h"
#include "help.h"
#include "magnify.h"
#include "msgtrans.h"
#include "saveas.h"
#include "visdelay.h"
#include "werr.h"
#include "wimpt.h"
#include "xfersend.h"

#include "m.h"
#include "ftrace.h"
#include "main.h"
83 84 85 86 87 88
#include "PSprite.h"
#include "Menus.h"
#include "SprWindow.h"
#include "ToolWindow.h"
#include "Tools.h"
#include "Colours.h"
Neil Turton's avatar
Neil Turton committed
89 90
#include "MenuD.h"
#include "trace.h"
91 92
#include "PaintLib.h"
#include "AltRename.h"
93
#include "Clipboard.h"
Neil Turton's avatar
Neil Turton committed
94

95
#define COPY_N(s1, s2, n) (sprintf ((s1), "%.*s", (n), (s2)), s1) /*this is strncpy() really*/
Neil Turton's avatar
Neil Turton committed
96 97 98

static int Do_Print (char *, void *);

99 100 101 102 103 104 105 106 107 108
typedef struct
{
  union
  {
    char       orgname[NAME_LIMIT + 1];
    int        orgsize;
  } type;
  main_sprite *orgsprite;
} sprite_tosort;

Neil Turton's avatar
Neil Turton committed
109 110 111 112 113 114 115 116 117 118 119 120 121
/***********************
 * Static menu handles *
 ***********************/

static menu
  sprite_main_menu,
  sprite_save_menu,
  sprite_paint_menu,
  sprite_edit_menu,
  spritefile_main_menu,
  spritefile_misc_menu,
  filedisplay_menu,
  sprite_colour_menu,
122
  sprite_selection_menu,
ROOL's avatar
ROOL committed
123
  sprite_advopts_menu,
Neil Turton's avatar
Neil Turton committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
  sprite_misc_menu;

static char Spritename [22];
static main_sprite *hit_sprite;

static int Row;
static int Column;

/* data for graphic entry of insert/delete */

static int insdel_pending = 0; /* to frig entry by click in sprite window */
static int insdel_rowfield;
static int insdel_colfield;
static int insdel_row;
static int insdel_col;
static main_sprite *insdel_sprite;
static toolwindow_block *insdel_tool;
static dbox insdel_d;
ROOL's avatar
ROOL committed
142
static dbox artistfriendly_dbox;
Neil Turton's avatar
Neil Turton committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

static char
  spritenamebuffer [13],
  rotatebuffer [13] = "0",
  scalebuffer [13] = "1",
  shearbuffer [13] = "0";

/*************************************
 *   Static variables for printing   *
 *************************************/

#define PRINT_INCHES_TO_72000 72000.0
#define PRINT_CM_TO_72000 (18144.0*72000.0/ (180<<8))
 /* above is at least the same as Draw uses */

print_positionstr
    menus_print_where = {0, 0}, menus_print_last_where = {0, 0};
static int print_copies = 1;
static int print_landscape = 1;
static main_scaling_block print_scale = {1, 1, 1, 1};
static double print_scalefactor = PRINT_INCHES_TO_72000;

/*************************************
 *   Generate sprite info dbox       *
 *************************************/
static char *yesno (int from)

{ ftracef0 ("yesno\n");
  return msgs_lookup (from? "PntG3": "PntG4");
}

static void display_sprite_info (main_sprite *sprite)

{ dbox d;
  psprite_info info;
178
  char temp[64];
Neil Turton's avatar
Neil Turton committed
179 180 181 182 183 184 185

  ftracef0 ("display_sprite_info\n");
  psprite_read_full_info (sprite, &info);

  if ((d = dbox_new ("spriteInfo")) != 0)
  { dbox_setfield   (d, d_Info_Name, info.name);
    if ((unsigned) info.mode < 256u)
186
    {
Neil Turton's avatar
Neil Turton committed
187 188 189
      dbox_setnumeric (d, d_Info_Mode, info.mode);
    }
    else
190
    {
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
      sprintf(temp,"&%08x",info.mode);
      dbox_setfield (d, d_Info_Mode, temp);
    }

    dbox_setnumeric (d, d_Info_Size,    info.size);

    strcpy(temp,psprite_get_colours(info.mode));
    /* Attempt to extract the mode flags manually, so that we can display
       somewhat sensible information even if the kernel doesn't recognise the
       format. E.g. prior to full CMYK support in the kernel+SpriteExtend,
       RISC OS 5 will report CMYK sprites as being RGB */
    int modeflags;
    if(((unsigned) info.mode) < 256u)
    {
      /* Mode number - ask the OS, just in case something special */
      modeflags = bbc_modevar(info.mode,bbc_ModeFlags);
    }
    else if(((info.mode>>27) & 15) == SpriteType_RISCOS5)
    {
      /* RISC OS 5 mode word */
      modeflags = info.mode & 0xff00;
    }
    else
    {
      /* RISC OS 3.5 mode word */
      if(((info.mode>>27) & 15) == SpriteType_CMYK)
        modeflags = ModeFlag_DataFormatFamily_Misc;
      else
        modeflags = 0;
    }
    unsigned int colours = colours_count(sprite);
    if((modeflags != -1) && ((colours > 256) || (modeflags & ModeFlag_DataFormat_Mask)))
    {
      switch(modeflags & ModeFlag_DataFormatFamily_Mask)
      {
      case ModeFlag_DataFormatFamily_RGB:
        if(modeflags & ModeFlag_DataFormatSub_Alpha)
          strcat(temp," A");
        else
          strcat(temp," ");
        if(modeflags & ModeFlag_DataFormatSub_RGB)
          strcat(temp,"RGB");
        else
          strcat(temp,"BGR");
        break;
      case ModeFlag_DataFormatFamily_Misc:
        if(!(modeflags & ModeFlag_DataFormatSub_Mask))
          strcat(temp," CMYK");
        break;
      case ModeFlag_DataFormatFamily_YCbCr:
        strcat(temp," YCbCr"); /* Skip giving sub-format info */
        break;
      }
    }
    dbox_setfield (d, d_Info_Colours, temp);

    if((((unsigned) info.mode) >= 256u) && (((info.mode >> 27) & 15) != 15))
    {
      /* Looks like RISC OS 3.5 mode word, extract DPI manually, in case there's something unusual there */
      int xdpi = (info.mode >> 1) & 8191;
      int ydpi = (info.mode >> 14) & 8191;
      sprintf(temp,msgs_lookup("PntGF"),(float)xdpi,(float)ydpi);
      dbox_setfield(d, d_Info_DPI, temp);
    }
    else
    {
      int xeig = bbc_modevar(info.mode,bbc_XEigFactor);
      int yeig = bbc_modevar(info.mode,bbc_YEigFactor);

      if((xeig == -1) || (yeig == -1))
        dbox_setfield(d, d_Info_DPI, "?");
      else
      {
ROOL's avatar
ROOL committed
264
        sprintf(temp,msgs_lookup("PntGF"),180.0f/(float)(1L<<xeig),180.0f/(float)(1L<<yeig));
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
        dbox_setfield(d, d_Info_DPI, temp);
      }
    }

    transparency_type transparency = psprite_transparency_type(sprite);

    static char *masktypes[] =
    {
      "PntG2", "PntG4", // transparency_type_none
      "PntG2", "PntG3", // transparency_type_onoffmask
      "PntG2", "PntGI", // transparency_type_alphamask
      "PntG7", "PntGE", // transparency_type_alphachannel
    };
    dbox_setfield (d, d_Info_MaskType, msgs_lookup(masktypes[transparency*2]));
    int alphabits=8;
    if(transparency == transparency_type_alphachannel)
    {
      switch(colours)
      {
      case 4096:
        alphabits = 4;
        break;
      case 32768:
        alphabits = 1;
        break;
      }
Neil Turton's avatar
Neil Turton committed
291
    }
292 293
    sprintf(temp,msgs_lookup(masktypes[transparency*2+1]),alphabits);
    dbox_setfield (d, d_Info_MaskSize, temp);
Neil Turton's avatar
Neil Turton committed
294

295 296 297 298 299 300 301 302 303
    if ((colours == 256) && info.palette)
    {
      if (info.truepalette)
        dbox_setfield (d, d_Info_Palette, msgs_lookup("PntGH"));
      else
        dbox_setfield (d, d_Info_Palette, msgs_lookup("PntGD"));
    }
    else
      dbox_setfield (d, d_Info_Palette, yesno(info.palette));
Neil Turton's avatar
Neil Turton committed
304 305 306 307 308 309 310 311 312 313 314

    if (info.width == -1)
      dbox_setfield (d, d_Info_Width, "?");
    else
      dbox_setnumeric (d, d_Info_Width, info.width);

    if (info.height == -1)
      dbox_setfield (d, d_Info_Height, "?");
    else
      dbox_setnumeric (d, d_Info_Height, info.height);

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 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 406 407 408 409 410 411 412 413 414 415
    int type = (info.mode>>27) & 31;
    if(type && !(info.mode & 1))
      type = -1;
    switch(type)
    {
    case SpriteType_Old:
      /* Arc */
      if(((unsigned) info.mode) >= 128u)
        dbox_setfield(d, d_Info_Compat, "?"); /* Shadow bit set or not mode number */
      else
        dbox_setfield(d, d_Info_Compat, msgs_lookup("PntG8"));
      break;
    case SpriteType_New1bpp:
    case SpriteType_New2bpp:
    case SpriteType_New4bpp:
    case SpriteType_New8bpp:
    case SpriteType_New16bpp:
    case SpriteType_New32bpp:
      /* 3.5 */
      dbox_setfield(d, d_Info_Compat, msgs_lookup("PntG9"));
      break;
    case SpriteType_CMYK:
    case 16+SpriteType_CMYK:
      /* CMYK -> Select */
      dbox_setfield(d, d_Info_Compat, msgs_lookup("PntGC"));
      break;
    case 16+SpriteType_New1bpp:
    case 16+SpriteType_New2bpp:
    case 16+SpriteType_New4bpp:
    case 16+SpriteType_New8bpp:
    case 16+SpriteType_New16bpp:
    case 16+SpriteType_New32bpp:
      /* Alpha masks -> Select, 5.21 */
      dbox_setfield(d, d_Info_Compat, msgs_lookup("PntGG"));
      break;
    case SpriteType_New64K:
    case 16+SpriteType_New64K:
      /* 64K -> RISC OS 5.21, 6 */
      dbox_setfield(d, d_Info_Compat, msgs_lookup("PntGB"));
      break;
    case SpriteType_RISCOS5:
    case 16+SpriteType_RISCOS5:
      /* RISC OS 5 types */
      /* However, only list types which are known to be fully supported */
      type = (info.mode>>20) & 127;
      if((info.mode & 0xf000f) != 1)
        type = -1;
      switch(type)
      {
      case SpriteType_New1bpp:
      case SpriteType_New2bpp:
      case SpriteType_New4bpp:
      case SpriteType_New8bpp:
        /* Nonstandard but valid: palettised sprite with RO 5 mode word */
        if(!(info.mode & 0xff00)) /* Can't have any mode flags set */
        {
          dbox_setfield(d, d_Info_Compat, msgs_lookup("PntGA"));
          break;
        }
        dbox_setfield(d, d_Info_Compat, "?");
        break;
      case SpriteType_New16bpp:
      case SpriteType_New32bpp:
      case SpriteType_New4K:
        /* RGB flag possible. Alpha flag only possible if no mask. */
        if(!(info.mode & 0x3f00)
           && !((info.mode & ModeFlag_DataFormatSub_Alpha) && (info.mask || (info.mode & 0x80000000))))
        {
          dbox_setfield(d, d_Info_Compat, msgs_lookup("PntGA"));
          break;
        }
        dbox_setfield(d, d_Info_Compat, "?");
        break;
      case SpriteType_New64K:
        /* Only RGB flag possible */
        if(!(info.mode & 0xbf00))
        {
          dbox_setfield(d, d_Info_Compat, msgs_lookup("PntGA"));
          break;
        }
        dbox_setfield(d, d_Info_Compat, "?");
        break;
      case SpriteType_CMYK:
        /* Nonstandard but valid. Make sure mode flags agree with colourspace. */       if((info.mode & 0xff00) == 0x1000)
        {
          dbox_setfield(d, d_Info_Compat, msgs_lookup("PntGA"));
          break;
        }
        dbox_setfield(d, d_Info_Compat, "?");
        break;
      default:
        /* Not defined or not fully supported yet */
        dbox_setfield(d, d_Info_Compat, "?");
        break;
      }
      break;
    default:
      /* Not defined or not fully supported yet */
      dbox_setfield(d, d_Info_Compat, "?");
      break;
    }
Neil Turton's avatar
Neil Turton committed
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433

    dbox_show (d);
    dbox_raw_eventhandler (d, &help_dboxrawevents, (void *) "PntHA");
  }
}
/********************************
 * Generate info dbox           *
 ********************************/

static void show_info_box (void)

{ dbox d;

  ftracef0 ("show_info_box\n");
  if ((d = dbox_new ("progInfo"))!= 0)
  { dbox_setfield (d, d_Prog_Name,     msgs_lookup ("Pnt00"));
    dbox_setfield (d, d_Prog_Copy,     msgs_lookup ("PntM1"));
    dbox_setfield (d, d_Prog_WhatIsIt, msgs_lookup ("PntM2"));
434
    dbox_setfield (d, d_Prog_Version,  msgs_lookup ("_Version"));
Neil Turton's avatar
Neil Turton committed
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
    dbox_show (d);

    dbox_raw_eventhandler (d, &help_dboxrawevents, (void *) "PntH8");
  }
}

/********************************
 *  Force rethink on a file     *
 ********************************/

static void iconsize_has_changed (main_window *window)

{ main_file *file = &window->data->file;

  ftracef0 ("iconsize_has_changed\n");
  psprite_set_plot_info (file);

  window->data->file.lastwidth = 0; /*force screen update*/
  main_set_extent (window);
  main_force_redraw (window->handle);
}

void menus_file_has_changed (main_window *window)

{ main_file *file = &window->data->file;

  ftracef0 ("menus_file_has_changed\n");
  psprite_recalculate_offsets (file);
  iconsize_has_changed (window);
}

/****************************
 * Common create/copy code  *
 ****************************/

void menus_sprite_new (main_window *window, BOOL hack_palette)

{ main_file *file = &window->data->file;
  main_sprite **sprptr, *sprite;
  int o, offset = 0 /**/, spriteno;

  ftracef0 ("menus_sprite_new\n");
  /* find linked list end */
  for (sprptr = &file->sprites; *sprptr != NULL; sprptr = &(*sprptr)->link)
    ;

  /*Find the right offset and spriteno - they are actually used by
    psprite_new to get the number of colours.*/
  spriteno = 0;
  for (o = psprite_first (&file->spritearea); o != 0;
      o = psprite_next (&file->spritearea, o))
  { offset = o;
    spriteno++;
  }

  ftracef2 ("creating main_sprite for offset %d, spriteno %d\n",
      offset, spriteno);
  if ((sprite = *sprptr = psprite_new (offset, spriteno, file) /* at end */) == NULL)
    werr (FALSE, msgs_lookup ("PntEG"));

  menus_file_has_changed (window);   /* force recache of offsets */
496 497 498 499 500 501
  sprite->gcol.colour = colours_count (sprite) - 1;
  sprite->gcol.alpha = 255;
  sprite->gcol.ecf = FALSE;
  sprite->gcol2.colour = 0;
  sprite->gcol2.alpha = 255;
  sprite->gcol2.ecf = FALSE;
Neil Turton's avatar
Neil Turton committed
502 503 504 505 506 507 508 509 510

  if (hack_palette) menus_hack_palette (sprite);
  psprite_set_colour (sprite);
  psprite_set_brush_translation (sprite);
}

/*************************************
 * Ensure spritearea large enough    *
 *************************************/
511

Neil Turton's avatar
Neil Turton committed
512 513 514 515
BOOL menus_ensure_size (sprite_area **sarea, int bytesneeded)

{ int bytesfree = (*sarea)->size - (*sarea)->freeoff;
  int newsize;
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534

  /*
   * OK: this is what I think menus_ensure_size() is supposed to do.
   *
   * bytesneeded > 0: if not at least <bytesneeded> bytes free in sprite area,
   *                  extend it.
   * bytesneeded < 0: reduce size of sprite area by <bytesneeded> bytes. This
   *                  used to do this regardless but I have changed it so
   *                  that it never reduces it such that the free offset points
   *                  outside the area. This should fix many bugs.
   * bytesneeded = 0: reduce size of sprite area to exactly what is needed by
   *                  the contents of the area.
   *
   * Hence I expect an operation that changes the size of a sprite area to
   * do an menus_ensure_size( , 0) on completion to release the free space. This seems
   * to be what is done so was probably the intention.
   *
   * Christopher Partington  18-Feb-1992
   */
Neil Turton's avatar
Neil Turton committed
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554

  ftracef1 ("menus_ensure_size of %d\n", bytesneeded);
  ftracef3("bytesfree = %d - %d = %d\n", (*sarea)->size, (*sarea)->freeoff,
                                         bytesfree);
  #if TRACE
    assert (bytesfree >= 0);
  #endif

  ftracef1 ("menus_ensure_size: %d bytes free\n", bytesfree);

  if (bytesneeded > 0 && bytesneeded <= bytesfree)
  { ftracef0 ("... enough space already - do nothing\n"); /*JRC*/
    return TRUE;
  }

  bytesneeded -= bytesfree;         /* amount needed */
  if (bytesneeded > 0)
    bytesneeded += 256;             /* and a little headroom */

  ftracef1 ("... do flex_extend of %d\n", bytesneeded);
555

Neil Turton's avatar
Neil Turton committed
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
  if (bytesneeded < 0)
  {
    ftracef1("shrinking area, change = %d bytes\n", bytesneeded);
    /* shrinking area - make sure that ok to do so */
    if ((*sarea)->size + bytesneeded < (*sarea)->freeoff)
    {
       bytesneeded = - ((*sarea)->size - (*sarea)->freeoff);
       ftracef1("...would be too much - change by %d instead\n", bytesneeded);
    }
  }
  if (bytesneeded == 0)
  {
    ftracef0("area is just the right size - no change\n");
    return TRUE;
  }

  newsize = flex_size((flex_ptr) sarea) + bytesneeded;
  /* check for overflow before calling flex */
#if TRACE
  if (newsize < 0)
     ftracef0("arithmetic overflow, so not enough memory\n");
#endif
  if (newsize < 0 || !flex_extend((flex_ptr) sarea, newsize))
    return FALSE;

  (*sarea)->size += bytesneeded;
  ftracef1 ("menus_ensure_size: leaving area with %d bytes free\n",
      (*sarea)->size - (*sarea)->freeoff);
  return TRUE;
}

/**********************************
 * Check for sprite existence     *
 **********************************/

BOOL menus_sprite_exists (sprite_area *sarea, char *name)

{ sprite_id sid;

  ftracef0 ("menus_sprite_exists\n");
  sid.s.name = name; sid.tag = sprite_id_name;
  if (!sprite_select (sarea, &sid))
  { werr (FALSE, msgs_lookup ("PntE6"), name);
    return TRUE;
  }

  return FALSE;
}

/************************************************
 * xfersend handlers for sprite and file saving *
 ************************************************/

static BOOL myxfersend (int filetype, char *name, int estsize,
    xfersend_saveproc p1, xfersend_sendproc p2, xfersend_printproc p3,
    void *handle)

{ wimp_w nullc = win_idle_event_claimer ();
  BOOL res;

  ftracef0 ("myxfersend\n");
  main_claim_idle ((wimp_w) -1);
Robert Sprowson's avatar
Robert Sprowson committed
618

Neil Turton's avatar
Neil Turton committed
619 620
  ftracef1 ("myxfersend: calling saveas (..., \"%s\", ...)\n", name);
  res = saveas (filetype, name, estsize, p1, p2, p3, handle);
Robert Sprowson's avatar
Robert Sprowson committed
621

Neil Turton's avatar
Neil Turton committed
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 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 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
  main_claim_idle (nullc);
  return res;
}

static int My_File_Is_Safe;
static int File_Saved;

static BOOL save_file_proc (char *filename, void *handle)

{ main_window *window = (main_window *) handle;
  os_error *error = NULL;

  ftracef1 ("save_file_proc %s\n", filename);

  visdelay_begin ();

  if ((error = sprite_area_save (window->data->file.spritearea,
      filename)) != NULL)
    goto finish;

  if (My_File_Is_Safe || xfersend_file_is_safe ())
  { main_set_title (window, filename);
    window->data->file.modified = 0;
    main_set_extent (window);
  }

  File_Saved = 1;

finish:
  visdelay_end ();

  return wimpt_complain (error) == NULL;
}

static BOOL save_sprite (char *filename, void *shandle)

{ main_sprite *sprite = (main_sprite *) shandle;
  sprite_header *spriteaddr = psprite_address (sprite);
  int spritesize = spriteaddr->next, handle = 0, hdr [3];
  os_regset reg_set;
  os_filestr file_str;
  os_gbpbstr gbpb_str;
  os_error *error;
  char *txt = NULL;

  ftracef1 ("save_sprite %s\n", filename);

  visdelay_begin ();

  /* Open file */
  reg_set.r [0] = 0x8C;
  reg_set.r [1] = (int) filename;
  if ((error = os_find (&reg_set)) != NULL)
  { txt = error->errmess;
    goto finish;
  }
  handle = reg_set.r [0];

  if (handle == 0)
  { /*In case the FileSwitch bug returns a 0 handle.*/
    txt = msgs_lookup ("PntE4");
    goto finish;;
  }

  hdr [0] = 1;               /* no of sprites */
  hdr [1] = 16;              /* offset to sprite */
  hdr [2] = spritesize + 16; /* offset to first free */

  /* GBPB the header */
  gbpb_str.action = 2;
  gbpb_str.file_handle = handle;

  gbpb_str.data_addr = hdr;
  gbpb_str.number = sizeof hdr;
  if ((error = os_gbpb (&gbpb_str)) != NULL)
  { txt = error->errmess;
    goto finish;
  }

  gbpb_str.data_addr = spriteaddr;
  gbpb_str.number = spritesize;
  if ((error = os_gbpb (&gbpb_str)) != NULL)
  { txt = error->errmess;
    goto finish;
  }

finish:
  if (handle != 0)
  { /* Close the file */
    os_error *error1;

    reg_set.r [0] = 0;
    reg_set.r [1] = handle;
    error1 = os_find (&reg_set);
    if (txt == NULL && error1 != NULL) txt = error1->errmess;

    /*Set the file to type "Sprite"*/
    file_str.action = 18;
    file_str.name = filename;
721
    file_str.loadaddr = FileType_Sprite;
Neil Turton's avatar
Neil Turton committed
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
    error1 = os_file (&file_str);
    if (txt == NULL && error1 != NULL) txt = error1->errmess;
  }

  visdelay_end ();

  if (txt != NULL)
    werr (FALSE, txt, filename);

  return txt == NULL;
}

#if 0
static char vdu19bytes [] =
  { 0, 16,     1, 16,     2, 16,     3, 16,
    4, 16,     5, 16,     6, 16,     7, 16,
    8, 16,     9, 16,    10, 16,    11, 16,
   12, 16,    13, 16,    14, 16,    15, 16,
    0, 24,     1, 25,     2, 25,     3, 25 /*oops*/
  };
#endif

static void build_sprite_palette_file (main_sprite *sprite,
    char *palette)

{ int len, i;
  char *p = palette;
  os_error *error = NULL;

  static int *Palette = NULL;

  ftracef0 ("build_sprite_palette_file\n");
  if ((error = psprite_build_palette (sprite, &Palette)) != NULL)
    goto finish;

  len = psprite_palsize (sprite)/sizeof (int);

  for (i = 0; i < len; i++)
  { *p++ = 19;
    *p++ = i;
    *p++ = 16;
    *p++ = Palette [i] >> 8;
    *p++ = Palette [i] >> 16;
    *p++ = Palette [i] >> 24;
  }

  if (len == 16)
  { /*Append the current mouse and border colours, for historical reasons.*/
    wimp_palettestr palette_str;

    if ((error = wimp_readpalette (&palette_str)) != NULL)
      goto finish;
    for (i = 0; i < 4; i++)
    { *p++ = 19;
      *p++ = i;
      *p++ = i == 0? 24: 25;
      *p++ = palette_str.c [16 + i].bytes.red;
      *p++ = palette_str.c [16 + i].bytes.green;
      *p++ = palette_str.c [16 + i].bytes.blue;
Robert Sprowson's avatar
Robert Sprowson committed
781 782
    }
  }
Neil Turton's avatar
Neil Turton committed
783 784 785 786 787 788 789 790 791 792

finish:
  wimpt_noerr (error);
}

static BOOL save_sprite_palette (char *filename, void *shandle)

{ main_sprite *sprite = (main_sprite *) shandle;
  char palette [6*256];
  os_regset r;
793
  int len, oldattribs = -1;
Neil Turton's avatar
Neil Turton committed
794 795 796 797 798 799 800 801 802
  BOOL saved;

  ftracef1 ("save_sprite_palette %s\n", filename);

  /*Work out how long this palette file is.*/
  len = 6*(psprite_palsize (sprite)/sizeof (int));
  if (len == 6*16) len += 6*4;

  build_sprite_palette_file (sprite, palette);
803 804

  r.r [0] = 17; /* Read cat info */
Neil Turton's avatar
Neil Turton committed
805
  r.r [1] = (int) filename;
806
  (void) os_swix (OS_File, &r);
Robert Sprowson's avatar
Robert Sprowson committed
807
  /* if something is already there, remember its attributes */
808 809 810
  if (r.r [0] != 0) oldattribs = r.r [5];

  r.r [0] = 10;
811
  r.r [2] = FileType_Palette;
Neil Turton's avatar
Neil Turton committed
812 813 814 815 816
  r.r [4] = (int) &palette;
  r.r [5] = (int) &palette + len;

  visdelay_begin ();
  saved = wimpt_complain (os_swix (OS_File, &r)) == NULL;
817 818 819 820 821 822 823

  if (oldattribs != -1) {
    r.r [0] = 4; /* Write attribs */
    r.r [5] = oldattribs;
    (void) os_swix (OS_File, &r);
  };

Neil Turton's avatar
Neil Turton committed
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 912 913 914 915 916 917
  visdelay_end ();

  return saved;
}

static BOOL mysendbuf (char **buffer, int size, int *max, int fudge)

{ BOOL OK = TRUE;
  int done = 0;

  ftracef0 ("mysendbuf\n");
  while (done < size && OK)
  { int buffsize = size - done;

    if (*max < buffsize) buffsize = *max;
    OK = xfersend_sendbuf (*buffer + done + fudge, buffsize);
    done += buffsize;
  }

  return OK;
}

static BOOL buffer_file_proc (void *handle, int *max)

{ main_window *window = (main_window *)handle;
  sprite_area **sarea = &window->data->file.spritearea;

  ftracef1 ("buffer_file_proc %s\n", window->data->file.filename);
  return File_Saved = mysendbuf ((char **) sarea, (*sarea)->freeoff - 4,
      max, sizeof (int));
}

static BOOL print_file_proc (char *filename, void *handle)

{ main_sprite *sprite = ((main_window *) handle)->data->file.sprites;

  filename = filename; /*for Norcroft*/
  return sprite != NULL && menus_do_print (sprite)? xfersend_printPrinted:
      xfersend_printFailed;
}

static BOOL buffer_sprite (void *shandle, int *max)

{ main_sprite *sprite = (main_sprite *)shandle;
  int  hdr [3], i;
  char name [13];
  sprite_header *spriteaddr = psprite_address (sprite);
  int spritesize = spriteaddr->next;
  char *buffer;

  ftracef0 ("buffer_sprite\n");
  saveas_read_leafname_during_send (name, 13);
  ftracef1 ("sprite name is \"%.12s\"\n", name);

  if (strlen (name) == 0)
  { werr (FALSE, msgs_lookup ("PntE7"));
    return 0;
  }

  for (i = 0 ; i < 13 && name [i] != 0 ; i++) name [i] = tolower (name [i]);

  ftracef2 ("Buffering out sprite @ %x, size %d\n", spriteaddr, spritesize);
  hdr [0] = 1;       /* no of sprites */
  hdr [1] = 16;      /* offset to sprite */
  hdr [2] = spritesize + 16; /* offset to first free */

  buffer = (char *)hdr;
  if (!mysendbuf (&buffer, 12, max, 0)) return 0;
  if (!mysendbuf ((char **)&sprite->file->spritearea,
                 4, max, sprite->offset)) return 0;

  buffer = (char *) &name;
  if (!mysendbuf (&buffer, 12, max, 0)) return 0;

  return mysendbuf ((char **)&sprite->file->spritearea,
                   spritesize, max, sprite->offset+16);
}

static BOOL buffer_sprite_palette (void *shandle, int *max)

{ main_sprite *sprite = (main_sprite *)shandle;
  char palette [6*256], *pp = palette;
  int len;

  ftracef0 ("buffer_sprite_palette\n");

  /*Work out how long this palette is.*/
  len = 6*(psprite_palsize (sprite)/sizeof (int));
  if (len == 6*16) len += 6*4;

  build_sprite_palette_file (sprite, palette);
  return mysendbuf (&pp, len, max, 0);
}

918
static void menus_save_selection (main_window *window)
Robert Sprowson's avatar
Robert Sprowson committed
919 920

{ ftracef0 ("menus_save_selection\n");
921 922

  if (window->selection.count==1)
Robert Sprowson's avatar
Robert Sprowson committed
923 924 925 926 927 928 929 930 931
  { main_sprite *sprite;

    /* Just one selected, find it, and save it on its own with
       the sprite name as the filename instead */
    for (sprite = window->data->file.sprites; sprite != NULL; sprite = sprite->link)
    { if (sprite->flags & MSF_SELECTED)
      { menus_save_sprite (sprite);
        break;
      }
932 933 934 935
    }
    return;
  }

936 937 938
  myxfersend (FileType_Sprite, msgs_lookup ("PntG6"),
              main_selection_file_size (window),
              main_save_selection, 0, 0, window);
939 940
}

Neil Turton's avatar
Neil Turton committed
941 942 943 944 945 946
void menus_save_sprite (main_sprite *sprite)

{ char name [NAME_LIMIT + 1];

  ftracef0 ("menus_save_sprite\n");
  (void) COPY_N (name, psprite_address (sprite)->name, NAME_LIMIT);
947

Neil Turton's avatar
Neil Turton committed
948
  ftracef1 ("sprite called \"%s\"\n", name);
949

950
  myxfersend (FileType_Sprite, name,
Robert Sprowson's avatar
Robert Sprowson committed
951 952
              psprite_address (sprite)->next + sizeof (sprite_area),
              &save_sprite, &buffer_sprite, &Do_Print, sprite);
Neil Turton's avatar
Neil Turton committed
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
}

BOOL menus_save_file (main_window *window, int askfor_name)

{ char *name = window->data->file.filename;
  File_Saved = 0;

  ftracef0 ("menus_save_file\n");
  if (name == NULL)
  { name = msgs_lookup ("PntF1");
    askfor_name = TRUE; /*JRC 12 July 1991*/
  }

  if (askfor_name)
  { My_File_Is_Safe = FALSE;
    ftracef0 ("menus_save_file: myxfersend\n");
969
    myxfersend (FileType_Sprite, name, window->data->file.spritearea->size,
Neil Turton's avatar
Neil Turton committed
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
        &save_file_proc, &buffer_file_proc,
        &print_file_proc, /*Protocol now in xfersend(), immediate printing
          allowed. Was NULL. JRC 4 Apr 1992*/
        /*FIX G-RO-7139 JRC 16 Oct '91 Use new printer protocol - was
          print_spritefile*/
        window);
  }
  else
  { My_File_Is_Safe = TRUE;
    save_file_proc (name, window);
  }

  return File_Saved;
}

/*******************************
 *  Add/delete columns/rows    *
 *******************************/

static void Change_Size (sprite_area **sarea, sprite_id *sid,
    main_sprite *sprite, int number, BOOL rows, int at)

{ BOOL insert = number > 0, hourglass = FALSE;
  main_sprite_window *sprw;
  psprite_info info;
  char *msg = NULL;
  os_error *error;

  ftracef3 ("Change_Size: number %d, rows %s, at %d\n",
      number, WHETHER (rows), at);
  sprwindow_remove_wastage (sprite);

  psprite_read_full_info (sprite, &info);
  ftracef2 ("BEFORE: sprite is %d columns by %d rows\n",
      info.width, info.height);
  if (at < 0) at = 0;      /* can menu outside the sprite */
  if (rows)
    {if (at > info.height) at = info.height;}
  else
    {if (at > info.width) at = info.width;}

  ftracef4 ("Insert %d %s%s at %d\n", number, rows? "row": "column",
      number == 1? "": "s", at);

  if (insert)
  { int newsize;

    newsize = psprite_size (info.width + (!rows? number: 0),
        info.height + (rows? number: 0), info.mode, info.mask,
        !info.palette? 0: !info.truepalette? 1: 2);
    ftracef2 ("newsize %d, oldsize %d\n", newsize, info.size);
    if (!menus_ensure_size (sarea, newsize - info.size))
    { msg = msgs_lookup ("PntEG");
      goto finish;
Robert Sprowson's avatar
Robert Sprowson committed
1024 1025
    }
  }
Neil Turton's avatar
Neil Turton committed
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
  else
  { /* Deleting - find the maximum number or rows or columns that can go */
    int max;

    max = (rows? info.height: info.width) - at;

    number = -number;
    if (number > max) number = max;
  }
  if (number == 0) goto finish;

  visdelay_begin ();
  hourglass = TRUE;

  ftracef3 ("sprite_change_size: rows %s, at %d, number %d\n",
      WHETHER (rows), at, insert? number: -number);
  if ((error = sprite_change_size (*sarea, sid, rows, at,
      insert? number: -number)) != NULL)
  { msg = error->errmess;
    goto finish;
  }

finish:
  if (hourglass) visdelay_end ();

  #if TRACE
    psprite_read_full_info (sprite, &info);
    ftracef2 ("AFTER: sprite is %d columns by %d rows\n",
        info.width, info.height);
  #endif

  sprwindow_invalidate (sprite);
  psprite_set_icon_scale (sprite);
  psprite_recalculate_offsets (sprite->file);

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

  if (msg != NULL) werr (FALSE, msg);
}

/***************************************
 * Graphical display of insert/delete  *
 ***************************************/

void menus_restart_tool (void)

{ main_window *window;
  main_sprite *sprite;

  ftracef0 ("menus_restart_tool\n");
  for (window = main_windows; window != NULL; window = window->link)
  if (window->tag == main_window_is_file)
    for (sprite = window->data->file.sprites; sprite != NULL;
Robert Sprowson's avatar
Robert Sprowson committed
1080
         sprite = sprite->link)
Neil Turton's avatar
Neil Turton committed
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
      sprite->needsnull = 1;
}

static void stop_insdel_graphics (void)

{ ftracef0 ("stop_insdel_graphics\n");
  toolwindow_current_tool->stop_action (insdel_sprite);
  toolwindow_current_tool = insdel_tool;

  /* now restart the tool if necessary */
  if (toolwindow_current_tool == &tools_textpaint ||
      toolwindow_current_tool == &tools_brushpaint)
    menus_restart_tool ();
}

1096
static void insdel_drawEOR(main_sprite_window *sprw, int x0, int y0)
Robert Sprowson's avatar
Robert Sprowson committed
1097 1098
{
  int xmul = sprw->blobsize.scale_xmul*insdel_sprite->mode.scale_xmul;
Steve Revill's avatar
Steve Revill committed
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
  int xdiv = sprw->blobsize.scale_xdiv;
  int ymul = sprw->blobsize.scale_ymul*insdel_sprite->mode.scale_ymul;
  int ydiv = sprw->blobsize.scale_ydiv;
  os_swi2 (OS_SetColour, 3, -1);
#if 0 /*replace the following to avoid checking for full-palette modes.
    J R C 22nd Feb 1994*/
  bbc_gcol (3, 127);
  bbc_tint (2, 3); /*27 August 1991*/
#endif

  if (insdel_rowfield != -1)
  { ftracef0 ("insdel_EOR row\n");
Robert Sprowson's avatar
Robert Sprowson committed
1111 1112
    int top = MAX(insdel_row, Row) + 1;
    int bottom = MIN(insdel_row, Row);
Steve Revill's avatar
Steve Revill committed
1113 1114 1115 1116 1117 1118
    bbc_rectanglefill (MAX (x0, 0), y0 + bottom*ymul/ydiv,
        0x1FFF, (top - bottom)*ymul/ydiv-1);
  }

  if (insdel_colfield != -1)
  { ftracef0 ("insdel_EOR column\n");
Robert Sprowson's avatar
Robert Sprowson committed
1119 1120
    int left = MIN(insdel_col, Column);
    int right = MAX(insdel_col, Column) + 1;
Steve Revill's avatar
Steve Revill committed
1121 1122 1123 1124 1125
    bbc_rectanglefill (x0 + left*xmul/xdiv, MAX (y0, 0),
        (right-left)*xmul/xdiv-1, 0x1FFF);
  }
}

Neil Turton's avatar
Neil Turton committed
1126 1127 1128 1129 1130 1131 1132
static void insdel_EOR (void)

{ main_sprite_window *sprw;

  ftracef0 ("insdel_EOR\n");

  for (sprw = insdel_sprite->windows; sprw != NULL; sprw = sprw->link)
Steve Revill's avatar
Steve Revill committed
1133
  { int more, x0, y0;
Neil Turton's avatar
Neil Turton committed
1134 1135 1136 1137
    wimp_redrawstr rds;
    wimp_winfo curr;

    curr.w = sprw->window->handle;
Robert Sprowson's avatar
Robert Sprowson committed
1138
    wimpt_noerr (paintlib_get_wind_info (&curr));
Neil Turton's avatar
Neil Turton committed
1139 1140 1141 1142 1143 1144 1145 1146
    rds.w = sprw->window->handle;
    rds.box = main_big_extent;
    wimpt_noerr (wimp_update_wind (&rds, &more));

    x0 = rds.box.x0 - rds.scx;
    y0 = rds.box.y1 - rds.scy + curr.info.ex.y0;

    while (more)
Robert Sprowson's avatar
Robert Sprowson committed
1147
    { insdel_drawEOR(sprw, x0, y0);
Neil Turton's avatar
Neil Turton committed
1148 1149 1150 1151 1152 1153 1154 1155 1156
      wimpt_noerr (wimp_get_rectangle (&rds, &more));
    }
  }
}

static void insdeltool_click (main_window *window, wimp_mousestr *mpos)

{ ftracef0 ("insdeltool_click\n");

1157
  if ((mpos->bbits & wimp_BLEFT) && window->data->sprite.sprite == insdel_sprite)
Neil Turton's avatar
Neil Turton committed
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
                           /* only SELECT in appropriate window */
  { int hit = (insdel_pending>>30)&3;
    sprite_area **sarea = &insdel_sprite->file->spritearea;
    sprite_id sid;
    char name [NAME_LIMIT + 1];

    ftracef0 ("perform insdeltool_click\n");

    insdel_pending &= 0x3FFFFFFF;
    sid.tag = sprite_id_name;
    sid.s.name = COPY_N (name, psprite_address (insdel_sprite)->name,
        NAME_LIMIT); /*doesn't flex*/

    if (hit > 1) insdel_pending = -insdel_pending;
    Change_Size (sarea, &sid, insdel_sprite, insdel_pending, hit & 1,
        hit & 1? MIN (Row, insdel_row): MIN (Column, insdel_col));
    menus_ensure_size (sarea, 0);
  }
}

static void insdeltool_null (main_window *window, wimp_mousestr *mpos)

{ ftracef0 ("insdeltool_null\n");
  if (mpos->w == window->handle)
1182 1183 1184 1185
  {
    int x, y;

    tools_mouse_to_pixel (window, mpos, &x, &y);
Neil Turton's avatar
Neil Turton committed
1186

1187
    ftracef2 ("extent coords are (%d, %d)\n", x, y);
Neil Turton's avatar
Neil Turton committed
1188 1189 1190 1191 1192 1193

    if (x != insdel_col || y != insdel_row)
    { insdel_EOR ();       /* remove old data */
      insdel_col = x; insdel_row = y;

      if (insdel_rowfield != -1)
1194
        dbox_setnumeric (insdel_d, 0, abs (insdel_row - Row)+1);
Neil Turton's avatar
Neil Turton committed
1195
      if (insdel_colfield != -1)
1196
        dbox_setnumeric (insdel_d, 0, abs (insdel_col - Column)+1);
Neil Turton's avatar
Neil Turton committed
1197 1198 1199 1200 1201 1202 1203 1204

      insdel_EOR ();       /* EOR on the new */
    }
  }
}

static void insdeltool_redraw (main_window *window, int x0, int y0)

Robert Sprowson's avatar
Robert Sprowson committed
1205 1206
{
  ftracef0 ("insdeltool_redraw\n");
Neil Turton's avatar
Neil Turton committed
1207

Steve Revill's avatar
Steve Revill committed
1208
  if (window->data->sprite.sprite->needsnull)
Robert Sprowson's avatar
Robert Sprowson committed
1209
    insdel_drawEOR(&window->data->sprite, x0, y0);
Neil Turton's avatar
Neil Turton committed
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
}

static void insdeltool_stop (main_sprite *sprite)

{ ftracef0 ("insdeltool_stop\n");
  if (sprite->needsnull)
  { sprwindow_release_idle_events (sprite);
    insdel_EOR ();        /* get rid of on-screen guff */
  }
}

static toolwindow_block insdel_toolblock =
 { &insdeltool_click,
   &insdeltool_null,
   &insdeltool_redraw,
   &insdeltool_stop,
   NULL
 };

static void start_insdel_graphics (main_sprite *sprite, int rowfield,
    int colfield)

{ main_window *window;

  ftracef0 ("start_insdel_graphics\n");
  insdel_rowfield = rowfield;
  insdel_colfield = colfield;
  insdel_sprite   = sprite;

  if (toolwindow_current_tool != &insdel_toolblock)
      insdel_tool = toolwindow_current_tool;
  spencer (else ftracef0 ("!"))

1243
  insdel_row = Row; insdel_col = Column;
Neil Turton's avatar
Neil Turton committed
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262

  /* stop the tool in all sprite windows */
  for (window = main_windows; window != NULL; window = window->link)
    if (window->tag == main_window_is_sprite)
      toolwindow_current_tool->stop_action (window->data->sprite.sprite);

  toolwindow_current_tool = &insdel_toolblock;
  sprite->needsnull = 1;

  insdel_EOR ();        /* EOR current data on */
}

void menus_insdel_frig (void)

{ ftracef0 ("menus_insdel_frig\n");
  if (insdel_pending) stop_insdel_graphics ();
  insdel_pending = 0;
}

1263
void menus_showmag (void *handle)
Neil Turton's avatar
Neil Turton committed
1264 1265

{ main_sprite_window *sprite = (main_sprite_window *) handle;
ROOL's avatar
ROOL committed
1266
  int m, d, window_width;
Neil Turton's avatar
Neil Turton committed
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
  wimp_wstate wstate;

  ftracef0 ("showmsg\n");
  wimpt_noerr (wimp_get_wind_state (sprite->window->handle, &wstate));
  m = sprite->blobsize.scale_ymul;
  d = sprite->blobsize.scale_ydiv;

  main_current_options.zoom.mul =
      sprite->blobsize.scale_ymul = sprite->blobsize.scale_xmul;
  main_current_options.zoom.div =
      sprite->blobsize.scale_ydiv = sprite->blobsize.scale_xdiv;
ROOL's avatar
ROOL committed
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289

  window_width = (wstate.o.box.x1-wstate.o.box.x0)*d*main_current_options.zoom.mul/
      (m*main_current_options.zoom.div);

  /* Prevent the user from making the window narrow enough to squash
     the buttons. With the colour panel present, this is possible for
     very short sprite names */
  if (window_width < sprite->minwidth)
    window_width = sprite->minwidth;
  ftracef1 ("Window width will be set to %d\n", window_width);

  sprwindow_set_work_extent_custom (sprite->window, FALSE, window_width, 0);
Neil Turton's avatar
Neil Turton committed
1290 1291 1292 1293 1294

  /* now scale window size too */
  spencer (ftracef (__FILE__, __LINE__, "psize %d %d %d %d\n",
                      wstate.o.box.x0, wstate.o.box.x1,
                      wstate.o.box.y0, wstate.o.box.y1))
ROOL's avatar
ROOL committed
1295
  wstate.o.box.x1 = wstate.o.box.x0 + window_width;
Neil Turton's avatar
Neil Turton committed
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
  wstate.o.box.y0 = wstate.o.box.y1 -
     (wstate.o.box.y1-wstate.o.box.y0)*d*main_current_options.zoom.mul/
     (m*main_current_options.zoom.div);
  spencer (ftracef (__FILE__, __LINE__, "nsize %d %d %d %d\n",
                      wstate.o.box.x0, wstate.o.box.x1,
                      wstate.o.box.y0, wstate.o.box.y1))

  /* and keep the same point at the top left */
  wstate.o.x = wstate.o.x*d*main_current_options.zoom.mul/
      (m*main_current_options.zoom.div);
  wstate.o.y = wstate.o.y*d*main_current_options.zoom.mul/
      (m*main_current_options.zoom.div);

  wimpt_noerr (wimp_open_wind (&wstate.o));
}


/**************************
 * Palette handling       *
 **************************/

static void sprite_palette_has_changed (main_sprite *sprite)

{ ftracef0 ("sprite_palette_has_changed\n");
ROOL's avatar
ROOL committed
1320
  sprite->artistfriendly = artist_unknown;
Neil Turton's avatar
Neil Turton committed
1321 1322 1323 1324 1325 1326 1327 1328
  psprite_set_colour (sprite);
  psprite_set_brush_translation (sprite);

  sprwindow_redisplay (sprite, NULL);
  /*if (sprite->colourhandle != 0)
    main_force_redraw (sprite->colourhandle);*/

  if (sprite->colourdialogue == 0) colours_set_extent (sprite);
ROOL's avatar
ROOL committed
1329 1330 1331
  main_sprite_window *windows = sprite->windows;
  if (windows)
    colourpanel_redraw (windows->window);
Neil Turton's avatar
Neil Turton committed
1332 1333 1334 1335 1336
  main_set_modified (sprite->file);
}

void menus_set_palette (main_sprite *sprite, int entries, int *palette)

Robert Sprowson's avatar
Robert Sprowson committed
1337 1338 1339
  /*Note: this routine assumes the palette is the right length for the
    sprite.*/

Neil Turton's avatar
Neil Turton committed
1340 1341 1342 1343 1344
{ BOOL create = palette != NULL;
  char *start, *end;
  int old_size, new_size, diff;
  sprite_area **sarea = &sprite->file->spritearea;
  sprite_header *header;
ROOL's avatar
ROOL committed
1345
  os_error *error;
Neil Turton's avatar
Neil Turton committed
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366

  ftracef1 ("%s palette\n", create? "Create": "Delete");

  header = psprite_address (sprite);
  old_size = MIN (header->image, header->mask) - sizeof (sprite_header);
  new_size = create? 2*sizeof (int)*entries: 0;
  diff = new_size - old_size;

  ftracef3 ("Sprite \"%.12s\" had %d bytes of palette; wants %d bytes\n",
      header->name, old_size, new_size);

  /*Adjust the area to insert/delete 'diff' bytes at the right
    place for this sprite. The "right place" is the start of the image or the
    start of the mask, whichever comes first.*/
  if (diff > 0 && !menus_ensure_size (sarea, diff))
  { werr (FALSE, msgs_lookup ("PntEG"));
    return;
  }

  header = psprite_address (sprite); /*flex moved?*/

ROOL's avatar
ROOL committed
1367 1368 1369 1370 1371
  if (create && entries == 256)
  { if ((error = os_swix4 (OS_SpriteOp, 37 | 0x200 /*create/remove palette*/,
          (int) *sarea, (int) header, 1 /*create*/ | (1U << 31))) != NULL)
    { werr (FALSE, error->errmess);
      return;
Robert Sprowson's avatar
Robert Sprowson committed
1372 1373
    }
  }
ROOL's avatar
ROOL committed
1374 1375 1376 1377 1378 1379 1380 1381
  else
  { start  = (char *) header + MIN (header->image, header->mask);
    end    = (char *) *sarea + (*sarea)->freeoff /*end of whole area*/;
    memmove (start + diff, start, end - start);

    if (create)
    { /*Now copy the new palette into the space just made.*/
      int i, *e = (int *) ((char *) header + sizeof (sprite_header));
Neil Turton's avatar
Neil Turton committed
1382

ROOL's avatar
ROOL committed
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
      for (i = 0; i < entries; i++)
      { *e = *(e + 1) = palette [i];
        e += 2;
      }
    }

    /*Fix up every pointer in the world.*/
    header->image     += diff;
    header->mask      += diff;
    header->next      += diff;
    (*sarea)->freeoff += diff;
  }
Neil Turton's avatar
Neil Turton committed
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407

  sprwindow_invalidate (sprite);
  menus_ensure_size (sarea, 0);
  psprite_recalculate_offsets (sprite->file);
  sprite_palette_has_changed (sprite);
}

/*********************************
 *                               *
 *  Sprite window menu handler.  *
 *                               *
 *********************************/

ROOL's avatar
ROOL committed
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 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
static BOOL remap_to_artistfriendly_dboxrawevents (dbox d, void *event, void *handle)

{
  wimp_eventstr *e = wimpt_last_event();
  if (
       (e->e == wimp_ESEND || e->e == wimp_ESENDWANTACK)
       &&
       e->data.msg.hdr.action == wimp_MHELPREQUEST
     )
  { return help_dboxrawevents (d, event, handle);
  }
  e = (wimp_eventstr*)event;

  if (e->e == wimp_EOPEN)
  { /* Keep the window on top */
    e->data.o.behind = -1;
    wimpt_noerr (wimp_open_wind (&e->data.o));
    return(TRUE); /* message has been handled. */
  }
  else
    return(FALSE);
}

static int remap_to_artistfriendly_dbox (void)

{ char question [512];
  sprintf (question, msgs_lookup ("PntWG"));
  dbox_field answer;
  artistfriendly_dbox = dbox_new ("remap");
  if (artistfriendly_dbox == 0) return dboxquery_CANCEL; /* out of space - embarassing */
  if (question == 0 || question[0] == 0) return 2;

  dbox_raw_eventhandler (artistfriendly_dbox, remap_to_artistfriendly_dboxrawevents, "PntHK");
  dbox_setfield (artistfriendly_dbox, 1, question);
  dbox_show (artistfriendly_dbox);
  /* Set an invisible caret so Return works */
  wimp_caretstr caretstr;
  caretstr.w = dbox_syshandle (artistfriendly_dbox);
  caretstr.i = -1;
  caretstr.x = 0;
  caretstr.y = 0;
  caretstr.height = 0; /* Invisible caret */
  caretstr.index = 0;
  wimpt_noerr (wimp_set_caret_pos (&caretstr));
  wimp_msgstr msg;
  msg.hdr.size = 24;
  msg.hdr.your_ref = 0;
  msg.hdr.action = wimp_MCLAIMENTITY;
  msg.data.claimentity.flags = wimp_MCLAIMENTITY_flags_caretselect;
  wimpt_noerr (wimp_sendmessage (wimp_ESEND, &msg, 0));

  do
  { answer = dbox_fillin (artistfriendly_dbox);
  }
  while (dbox_persist ());

  ftracef1 ("Remap to Artist Friendly dbox answer: %d.\n", answer);
  return (int)answer;
}

static void destroy_artistfriendly_dbox (void)

{ if (artistfriendly_dbox != 0)
  { dbox_hide (artistfriendly_dbox);
    dbox_dispose (&artistfriendly_dbox);
    artistfriendly_dbox = 0;
  }
}

Neil Turton's avatar
Neil Turton committed
1477 1478 1479 1480
static void Apply_Colour (dboxtcol_colour col, void *handle)

{ main_sprite *sprite = (main_sprite *) handle;
  sprite_header *spr = psprite_address (sprite);
1481
  int *colour = &(&spr->mode + 1) [2*sprite->gcol.colour];
Neil Turton's avatar
Neil Turton committed
1482 1483 1484 1485 1486 1487 1488 1489

  ftracef0 ("Apply_Colour\n");
  /*Update source of colour.*/
  *colour = *(colour + 1) = col;

  sprite_palette_has_changed (sprite);
}

ROOL's avatar
ROOL committed
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 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
void menus_select_colour (main_window *window, BOOL backgrnd_col, BOOL refetch_coords)

{ main_sprite_window *sprw = &window->data->sprite;
  main_sprite *sprite = sprw->sprite;
  main_colour *gcol = backgrnd_col ? &sprite->gcol2 : &sprite->gcol;

  ftracef0 ("menus_select_colour\n");
  sprite_id sid;
  char name [NAME_LIMIT + 1];
  sprite_maskstate mask = sprite_masksolid;
  sprite_colour colour;

  if (refetch_coords)
  { wimp_mousestr mouse;

    wimpt_noerr (wimp_get_point_info (&mouse));
    tools_mouse_to_pixel (window, &mouse, &Column, &Row);
    ftracef4 ("row %d, column %d, width %d, height %d\n", Row, Column,
        psprite_address (sprite)->width + 1,
        psprite_address (sprite)->height + 1);
  }

  ftracef2 ("pick pixel colour %d %d\n", Column, Row);

  sid.tag = sprite_id_name;
  sid.s.name = COPY_N (name, psprite_address (sprite)->name,
      NAME_LIMIT); /*doesn't flex*/

  /*Redraw all colour panels.*/
  colourpanel_redraw (window);

  /*Force a redraw of the colours window, if necessary.*/
  if (sprite->colourhandle != NULL && sprite->colourdialogue == NULL)
    main_force_redraw (sprite->colourhandle);

  if (psprite_hasmask (sprite))
    wimpt_noerr (sprite_readmask (sprite->file->spritearea, &sid,
          Column, Row, &mask));

  wimpt_noerr (main_read_pixel (sprite->file->spritearea,
        &sid, Column, Row, &colour));

  *gcol = colours_unpack_colour2(sprite, colour, mask);

  ftracef1 ("select colour 0x%X\n", *gcol);

  if ((!backgrnd_col && sprite->colourdialogue != 0) ||
    (backgrnd_col && sprite->bgcolourdialogue != 0)
  )
  { colourpicker_dialogue dialogue;

    if (!gcol->alpha)
    { dialogue.flags = colourpicker_DIALOGUE_TRANSPARENT;
      ftracef0 ("ColourPicker_UpdateDialogue: setting transparent\n");
      os_swi3 (ColourPicker_UpdateDialogue,
          colourpicker_UPDATE_TRANSPARENT,
          (backgrnd_col) ? sprite->bgcolourdialogue : sprite->colourdialogue,
          &dialogue);
    }
    else
    { dialogue.colour = colours_entry (psprite_address(sprite)->mode, gcol->colour);

      ftracef1 ("ColourPicker_UpdateDialogue: setting RGB colour to 0x%X\n",
          dialogue.colour);
      os_swi3 (ColourPicker_UpdateDialogue,
          colourpicker_UPDATE_COLOUR,
          (backgrnd_col) ? sprite->bgcolourdialogue : sprite->colourdialogue,
          &dialogue);
    }
  }
}

Neil Turton's avatar
Neil Turton committed
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
void menus_sprite_handler (void *handle, char *hit)

{ main_window *window = (main_window *) handle;
  main_sprite_window *sprw = &window->data->sprite;
  main_sprite *sprite = sprw->sprite;

  ftracef0 ("menus_sprite_handler\n");
  ftracef5 ("menu hit is {%d, %d, %d, %d, %d}\n",
      hit [0], hit [1], hit [2], hit [3], hit [4]);

  switch (hit [0])
  { case s_Misc:
      switch (hit [1])
      { case s_Misc_AppInfo:
          if (hit [2]) show_info_box ();
        break;

        case s_Misc_SpriteInfo:
          if (hit [2]) display_sprite_info (sprite);
        break;

        case s_Misc_Print:
          /*FIX G-RO-9923 JRC 15 Oct '91 Print on choosing Print.*/
          menus_print_sprite (sprite, /*ask?*/ hit [2] != 0);
        break;
      }
    break;

    case s_Save:
      switch (hit [1])
      { case s_Save_Sprite:
          ftracef1 ("Sprite save: hit [2] == %d\n", hit [2]);
          if (hit [2] != 0)
          { /*If there is only one sprite, offer to save the whole sprite
              file. This acts as a counterpart to the code that opens a
              sprite window if there is only one sprite. J R C 7th Feb 1995*/
            if (sprite->file->spritearea->number == 1)
              menus_save_file (sprite->file->window, TRUE);
            else
              menus_save_sprite (sprite);
          }
        break;

        case s_Save_Palette:
          if (hit [2] != 0)
          { int size = psprite_palsize (sprite);

            ftracef0 ("Sprite palette save\n");
1610
            myxfersend (FileType_Palette, msgs_lookup ("PntG5"), size,
Neil Turton's avatar
Neil Turton committed
1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
                &save_sprite_palette, &buffer_sprite_palette, 0, sprite);
          }
        break;
      }
    break;

    case s_Paint:
      switch (hit [1])
      { case s_Paint_ECF: /*rewritten. JRC 4 June 1991*/
        { BOOL open;
          dbox d;

          ftracef0 ("Select E C F\n");
          if ((d = dbox_new ("selectECF")) != NULL)
          { dbox_setfield (d, d_ECF_Sprite, "");

            dbox_raw_eventhandler (d, &help_dboxrawevents,
                (void *) "PntHC");
            dbox_show (d);

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

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

              switch (i)
              { case d_ECF_Ok:
                { char name [13];
                  int ECF, i;
                  main_sprite *ECFsprite;

                  /*Get the selected icon number (0, ..., 3).*/
                  ECF = -1;
                  for (i = 0; i < 4; i++)
                    if (dbox_getnumeric (d, d_ECF_1 + i))
                    { ECF = i;
                      break;
                    }

                  if (ECF != -1)
                  { dbox_getfield (d, d_ECF_Sprite, name, 13);

                    if ((ECFsprite = psprite_find (name, "PntE5b")) != NULL)
                      psprite_setup_ecf (sprite, ECF, ECFsprite);
                  }

                  open = dbox_persist ();
                }
                break;

                case dbox_CLOSE:
                  open = FALSE;
                break;
              }
            }
            dbox_dispose (&d);
          }
        }
        break;

        case s_Paint_Sel_Col:
ROOL's avatar
ROOL committed
1673
        { menus_select_colour (window, FALSE, FALSE);
Robert Sprowson's avatar
Robert Sprowson committed
1674
        }
Neil Turton's avatar
Neil Turton committed
1675 1676 1677 1678
        break;

        case s_Paint_Show_Col:
          ftracef0 ("show colour menu\n");
ROOL's avatar
ROOL committed
1679
          colours_create_window (sprite, FALSE);
Neil Turton's avatar
Neil Turton committed
1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
          main_current_options.colours.show_colours = TRUE;
        break;

        case s_Paint_Show_Tool:
          ftracef0 ("show tool menu\n");
          toolwindow_display (/*at pointer?*/ TRUE);
          main_current_options.tools.show_tools = TRUE;
        break;

        case s_Paint_Small:
          ftracef0 ("Toggle colour size\n");
          sprite->coloursize =
              sprite->coloursize == colours_SIZE? colours_SIZE/2: colours_SIZE;

          main_current_options.colours.small_colours =
              sprite->coloursize != colours_SIZE;

          colours_set_extent (sprite);
        break;

        case s_Paint_Edit_Pal:
          if (hit [2])
ROOL's avatar
ROOL committed
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
          { wimp_eventstr ev = {0}, *ev2;
            int mx = 0, my = 0;
            BOOL askToRemap = main_current_options.advanced.artist_friendly
              && colours_is_artist_friendly (sprite);

            if (askToRemap)
            { /* Save coordinates from the menu. We'll need them for dboxtcol */
              ev2 = wimpt_last_event ();
              if (ev2 && (ev2->e == wimp_EMENU || ev2->e == wimp_ESEND) &&
                ev2->data.msg.hdr.action == wimp_MMENUWARN)
              { mx = ev2->data.msg.data.menuwarn.x;
                my = ev2->data.msg.data.menuwarn.y;
              }
              else
              { wimp_mousestr mouse_str;
                wimpt_noerr (wimp_get_point_info (&mouse_str));
                mx = mouse_str.x;
                my = mouse_str.y;
              }
              int dboxResult;
              /* dbox can be disabled in the options. If so, the remap
               will just go ahead without asking. */
              if (main_current_options.advanced.ask_remap_palette)
                dboxResult = remap_to_artistfriendly_dbox ();
              else
                dboxResult = 0;

              if (dboxResult == 0) /* if Rearrange */
              { colours_remap_to_artist_friendly (sprite);
                /* Don't forget to remap the currently selected colours too! */
                sprite->gcol.colour = colours_get_artist_friendly_index (sprite->gcol.colour, sprite);
                sprite->gcol2.colour = colours_get_artist_friendly_index (sprite->gcol2.colour, sprite);
                sprite_palette_has_changed (sprite);
              }
              if (dboxResult == -1)
              { /* They cancelled the operation. We'll just ask again next time */
                destroy_artistfriendly_dbox ();
                break;
              }

              sprite->artistfriendly = artist_unfriendly;

              /* Hide this dialogue box but keep the menus open */
              if (main_current_options.advanced.ask_remap_palette &&
                  artistfriendly_dbox != NULL)
              { int dboxhandle = dbox_syshandle (artistfriendly_dbox);
                if (dboxhandle != NULL)
                  wimpt_noerr (wimp_close_wind (dbox_syshandle (artistfriendly_dbox)));
              }
              /* Now we have to make dboxtcol think it was opened from the menu */
              ev.e = wimp_ESEND;
              ev.data.msg.hdr.action = wimp_MMENUWARN;
              ev.data.msg.data.menuwarn.x = mx;
              ev.data.msg.data.menuwarn.y = my;
              wimpt_fake_event (&ev);
              wimpt_poll ((wimp_emask)0, &ev);
            }
            else if (!main_current_options.advanced.artist_friendly)
            { /* Make sure artist-friendly logic will be re-run if the
              setting is later re-enabled */
              sprite->artistfriendly = artist_unknown;
            }

            sprite_header *spr = psprite_address (sprite);
1766
            int *colour = &(&spr->mode + 1) [sprite->gcol.colour*2];
Neil Turton's avatar
Neil Turton committed
1767 1768 1769 1770 1771 1772 1773 1774 1775 1776
            wimp_emask emask;

            ftracef0 ("Edit colour\n");
            /* Enable nulls, so slider drags work */
            event_setmask ((wimp_emask) (((emask = event_getmask ()) & ~wimp_EMNULL)));

            dboxtcol (colour, /*allow transparent?*/ FALSE,
                msgs_lookup ("PntW8"), &Apply_Colour, (void *) sprite);

            event_setmask (emask);
ROOL's avatar
ROOL committed
1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811

            destroy_artistfriendly_dbox ();
          }
        break;

        case s_Paint_Adv_Opt:
          switch (hit [2])
          { case s_Adv_HiBackCol:
               main_current_options.advanced.highlight_bgcolour =
                !main_current_options.advanced.highlight_bgcolour;
               for (main_sprite *spr = sprite->file->sprites; spr != NULL;
                    spr = spr->link)
                { /* Redraw all colour selectors */
                  if (spr->colourdialogue == 0) colours_set_extent (spr);
                }
            break;
            case s_Adv_ArtFriend:
               main_current_options.advanced.artist_friendly =
                !main_current_options.advanced.artist_friendly;

                for (main_sprite *spr = sprite->file->sprites; spr != NULL;
                    spr = spr->link)
                { if (!main_current_options.advanced.artist_friendly)
                  { /* Clear artist-friendly status of sprites so it won't get
                    out of sync while the feature is disabled */
                    spr->artistfriendly = artist_unknown;
                  }
                  /* Redraw all colour selectors */
                  if (spr->colourdialogue == 0) colours_set_extent (spr);
                }
            break;
            case s_Adv_AskRemapPal:
               main_current_options.advanced.ask_remap_palette =
                !main_current_options.advanced.ask_remap_palette;
            break;
Neil Turton's avatar
Neil Turton committed
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 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 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917
          }
        break;
      }
    break;

    case s_Edit:
    { sprite_area **sarea = &sprite->file->spritearea;
      sprite_id sid;
      char name [NAME_LIMIT + 1];

      sid.tag = sprite_id_name;
      sid.s.name = COPY_N (name, psprite_address (sprite)->name, NAME_LIMIT);
        /*doesn't flex*/

      switch (hit [1])
      { case s_Edit_FlipV:
        case s_Edit_FlipH :
          ftracef1 ("flip %c\n", hit [1] == s_Edit_FlipV ? 'x' : 'y');
          wimpt_complain
             ((hit [1] == s_Edit_FlipV? sprite_flip_x: sprite_flip_y)
             (*sarea, &sid));
        break;

        case s_Edit_Rotate:
          ftracef0 ("Rotate\n");
          if (hit [2])
          { int angle, n;

            if (sscanf (rotatebuffer, "%d%n", &angle, &n) == 1 &&
                n == strlen (rotatebuffer))
              psprite_rotate (sprite, angle);
          }
        break;

        case s_Edit_Scale_X:
          ftracef0 ("Scale X\n");
          if (hit [2])
          { double factor = 1.0;
            int n;

            ftracef1 ("menus_sprite_handler: scalebuffer: %s\n",
                scalebuffer);
            if (sscanf (scalebuffer, "%lg%n", &factor, &n) == 1 &&
                n == strlen (scalebuffer) &&
                factor > 0.0)
              psprite_scale (sprite, factor, 1.0);
          }
        break;

        case s_Edit_Scale_Y:
          ftracef0 ("Scale Y\n");
          if (hit [2])
          { double factor = 1.0;
            int n;

            ftracef1 ("menus_sprite_handler: scalebuffer: %s\n",
                scalebuffer);
            if (sscanf (scalebuffer, "%lg%n", &factor, &n) == 1 &&
                n == strlen (scalebuffer) &&
                factor > 0.0)
              psprite_scale (sprite, 1.0, factor);
          }
        break;

        case s_Edit_Shear:
          ftracef0 ("Shear\n");
          if (hit [2])
          { double factor = 1.0;
            int n;

            if (sscanf (shearbuffer, "%lg%n", &factor, &n) == 1 &&
                n == strlen (shearbuffer))
              psprite_shear (sprite, factor);
          }
        break;

        case s_Edit_Adjust:
        { BOOL open;
          int rows, columns;
          psprite_info info;
          dbox d;

          ftracef0 ("adjust size");
          if ((d = dbox_new ("spritesize"))!=0)
          { psprite_read_full_info (sprite, &info);
            rows    = info.height;
            columns = info.width;

            dbox_setnumeric (d, d_Adjust_Cols, columns);
            dbox_setnumeric (d, d_Adjust_Rows, rows);

            dbox_show (d);
            dbox_raw_eventhandler (d, &help_dboxrawevents,
                (void *) "PntHD");

            open = TRUE;
            while (open)
            { wimp_i i = dbox_fillin (d);
              wimp_mousestr mouse_str;
              BOOL adjust;

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

              columns = dbox_getnumeric (d, d_Adjust_Cols);
              rows    = dbox_getnumeric (d, d_Adjust_Rows);
              wimpt_noerr (wimp_get_point_info (&mouse_str));
1918
              adjust = (mouse_str.bbits & wimp_BRIGHT) != 0;
Neil Turton's avatar
Neil Turton committed
1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057

              switch (i)
              { case d_Adjust_Ok:
                { int width_by, height_by, size_by;

                  if (rows == 0 || columns == 0)
                    werr (FALSE, msgs_lookup ("PntEA"));
                  else
                  { sprwindow_remove_wastage (sprite);

                    /*Adjust the smaller dimension first: then you don't run
                      out of store in an intermediate step.*/

                    psprite_read_full_info (sprite, &info);

                    height_by = rows - info.height;
                    width_by  = columns - info.width;
                    size_by   = psprite_size (columns, rows, info.mode,
                        info.mask, 0) - psprite_size (info.width,
                        info.height, info.mode, info.mask, 0);

                    /*Make sure there's enough space for the new sprite.*/
                    if (!menus_ensure_size (sarea, size_by))
                      werr (FALSE, msgs_lookup ("PntEG"));
                    else
                    { if (height_by < width_by)
                      { /*adjust no of rows*/
                        if (height_by != 0)
                          Change_Size (sarea, &sid, sprite, height_by, 1,
                              height_by > 0? info.height: rows);

                        /*adjust no of columns*/
                        if (width_by != 0)
                          Change_Size (sarea, &sid, sprite, width_by, 0,
                              width_by > 0? info.width: columns);
                      }
                      else
                      { /*adjust no of columns*/
                        if (width_by != 0)
                          Change_Size (sarea, &sid, sprite, width_by, 0,
                              width_by > 0? info.width: columns);

                        /*adjust no of rows*/
                        if (height_by != 0)
                          Change_Size (sarea, &sid, sprite, height_by, 1,
                              height_by > 0? info.height: rows);
                      }

                      menus_ensure_size (sarea, 0);
                      psprite_recalculate_offsets (sprite->file);
                      sprwindow_redisplay (sprite, NULL);
                    }
                  }
                  open = dbox_persist ();
                }
                break;

                case d_Adjust_RowDown:
                  adjust = !adjust;
                /*fall through*/

                case d_Adjust_RowUp:
                  if (adjust)
                  { if (rows > 1)
                      dbox_setnumeric (d, d_Adjust_Rows, rows - 1);
                  }
                  else
                    dbox_setnumeric (d, d_Adjust_Rows, rows + 1);
                break;

                case d_Adjust_ColDown:
                  adjust = !adjust;
                /*fall through*/

                case d_Adjust_ColUp:
                  if (adjust)
                  { if (columns > 1)
                      dbox_setnumeric (d, d_Adjust_Cols, columns - 1);
                  }
                  else
                    dbox_setnumeric (d, d_Adjust_Cols, columns + 1);
                break;

                case dbox_CLOSE:
                  open = FALSE;
                break;
              }
            }
            dbox_dispose (&d);
          }
        }
        break;

        case s_Edit_InsCol:
        case s_Edit_InsRow:
        case s_Edit_DelCol:
        case s_Edit_DelRow:
        { BOOL col = hit [1] == s_Edit_InsCol || hit [1] == s_Edit_DelCol,
            insert = hit [1] == s_Edit_InsCol || hit [1] == s_Edit_InsRow;
          int number = 0 /*for Norcroft*/;

          #if 0 /*dbox on menu hit is against style guide*/
          if (FALSE)
          { ftracef0 ("hit on menu - insert/delete 1 row/column\n");

            number     = 1;
            insdel_row = Row;
            insdel_col = Column;
          }
          else /* Hit is on writable menu */
          #endif

          ftracef0 ("submenu hit - start graphics and show dbox\n");
          menus_insdel_frig ();
          insdel_pending = 0;
          start_insdel_graphics (sprite, col? -1: 1, !col? -1: 1);

          if (hit [2])
          { wimp_i i;

            if ((insdel_d = dbox_new ("number")) == 0)
              return;
            ftracef0 ("dbox made\n");

            dbox_setnumeric (insdel_d, 0, 1);
            dbox_show (insdel_d);
            i = dbox_fillin (insdel_d);
            insdel_pending = dbox_getnumeric (insdel_d, 0);

            if (i != dbox_CLOSE)
            { ftracef0 ("Non-close menu event\n");
              number = insdel_pending;
              insdel_pending = 0;
            }
            else
            { int flags = insert? (col? 4: 5): (col? 6: 7);

              ftracef0 ("Close menu event\n");
              insdel_pending |= flags << 30; /*top two bits are flags*/
2058
              number = (int)0x80000000;
Neil Turton's avatar
Neil Turton committed
2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072
              insert = col = FALSE; /* A fiddle */
            }
            dbox_dispose (&insdel_d);
          }

          if (!insdel_pending) stop_insdel_graphics ();

          if (hit [2])
          { if (!insert) number = -number;
            ftracef1 ("number %d\n", number);
            if (number != 0x80000000)
              Change_Size (sarea, &sid, sprite, number, !col,
                  !col? MIN (Row, insdel_row): MIN (Column, insdel_col));
            menus_ensure_size (sarea, 0);
Robert Sprowson's avatar
Robert Sprowson committed
2073 2074
          }
        }
Neil Turton's avatar
Neil Turton committed
2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104
        break;

        case s_Edit_Mask:
        { int create = !psprite_hasmask (sprite);
          char *msg = NULL;
          os_error *error;

          if (sprite == tools_brushsprite_ptr && toolwindow_current_tool == &tools_brushpaint)
            toolwindow_stop_all_tools ();

          ftracef1 ("%s mask\n", create? "Create" : "Delete");
          if (create)
            if (!menus_ensure_size (sarea, psprite_address (sprite)->next))
            { msg = msgs_lookup ("PntEG");
              goto masked;
            }

          if ((error = (create? sprite_create_mask: sprite_remove_mask)
              (*sarea, &sid)) != NULL)
          { msg = error->errmess;
            goto masked;
          }

          if (sprite->colourdialogue == 0) colours_set_extent (sprite);
          menus_ensure_size (sarea, 0);
          sprwindow_invalidate (sprite);

          if (sprite == tools_brushsprite_ptr && toolwindow_current_tool == &tools_brushpaint)
            menus_restart_tool ();

ROOL's avatar
ROOL committed
2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122
          for (int backgrnd_col = 0; backgrnd_col <= 1; backgrnd_col++)
          { if ((!backgrnd_col && sprite->colourdialogue != 0) ||
                (backgrnd_col && sprite->bgcolourdialogue != 0))
            { colourpicker_dialogue dialogue;

              if (!create)
              { /*Unset transparent if it is currently selected.*/
                dialogue.flags = 0;
                ftracef0 ("ColourPicker_UpdateDialogue: setting "
                    "'transparent'\n");
                if ((error = os_swix3 (ColourPicker_UpdateDialogue,
                    colourpicker_UPDATE_TRANSPARENT, backgrnd_col ?
                      sprite->bgcolourdialogue : sprite->colourdialogue,
                    &dialogue)) != NULL)
                { msg = error->errmess;
                  goto masked;
                }
              }
Neil Turton's avatar
Neil Turton committed
2123

ROOL's avatar
ROOL committed
2124 2125 2126 2127 2128
              /*If creating a mask, set to offering transparent, otherwise to
                  not.*/
              dialogue.flags = create?
                  colourpicker_DIALOGUE_OFFERS_TRANSPARENT: 0;
              ftracef0 ("ColourPicker_UpdateDialogue: setting 'offers transparent'\n");
Neil Turton's avatar
Neil Turton committed
2129
              if ((error = os_swix3 (ColourPicker_UpdateDialogue,
ROOL's avatar
ROOL committed
2130 2131 2132
                  colourpicker_UPDATE_OFFERS_TRANSPARENT, backgrnd_col ?
                    sprite->bgcolourdialogue : sprite->colourdialogue,
                    &dialogue)) != NULL)
Neil Turton's avatar
Neil Turton committed
2133 2134 2135 2136
              { msg = error->errmess;
                goto masked;
              }
            }
Robert Sprowson's avatar
Robert Sprowson committed
2137
          }
Neil Turton's avatar
Neil Turton committed
2138 2139 2140

          /*Fix MED-xxx J R C 21st Jul 1994*/
          if (!create)
2141 2142 2143
          {
            sprite->gcol.alpha = 255;
            sprite->gcol2.alpha = 255;
Neil Turton's avatar
Neil Turton committed
2144 2145
          }

ROOL's avatar
ROOL committed
2146 2147
          colourpanel_redraw (window);

Neil Turton's avatar
Neil Turton committed
2148 2149 2150 2151 2152 2153
        masked:
          if (msg != NULL) werr (FALSE, msg);
        }
        break;

        case s_Edit_Palette:
ROOL's avatar
ROOL committed
2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166
        { int entries;

          /*When deleting the palette, see if it was previously remapped
            to the artist-friendly layout. If that's detected, the pixel
            remap will be reversed to the default colours.*/
          if (psprite_haspal (sprite))
          { if (colours_try_remap_from_artist_friendly (sprite))
            { sprite->gcol.colour = colours_get_artist_friendly_colour (sprite->gcol.colour, NULL);
              sprite->gcol2.colour = colours_get_artist_friendly_colour (sprite->gcol2.colour, NULL);
            }
          }

          int lb_bpp =
Neil Turton's avatar
Neil Turton committed
2167 2168
              bbc_modevar (psprite_address (sprite)->mode, bbc_Log2BPP);

ROOL's avatar
ROOL committed
2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180
          if (lb_bpp == 3 &&
              bbc_modevar (SpriteType_New32bpp << 27 | 1, bbc_Log2BPP) != -1)
          { /*It's a 256 colour sprite on a Risc PC or later,
              so we'll add a full, 256 entry palette.*/
            entries = 256;
          }
          else
          { /*Otherwise stick with the historic behaviour*/
            entries = ENTRIES (lb_bpp);
          }

          menus_set_palette (sprite, entries,
Neil Turton's avatar
Neil Turton committed
2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194
              psprite_haspal (sprite)? NULL:
              psprite_std_palettes [sprite->file->use_current_palette? 0: 1]
                [lb_bpp]);
        }
        break;
      }
      psprite_recalculate_offsets (sprite->file);
      sprwindow_redisplay (sprite, NULL);
    }
    break;

    case s_Zoom:
      if (hit [1])
        magnify_select (&sprw->blobsize.scale_xmul,
2195
            &sprw->blobsize.scale_xdiv, 999, 999, menus_showmag, sprw);
Neil Turton's avatar
Neil Turton committed
2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 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
    break;

    case s_Grid:
      switch (hit [1])
      { case 0:
          /*Toggle grid*/
          sprw->gridcol =
              sprw->gridcol == 255? main_current_options.grid.colour: 255;

          main_current_options.grid.show = sprw->gridcol != 255;
        break;

        default:
          sprw->gridcol = hit [1] - 1;

          main_current_options.grid.show = TRUE;
          main_current_options.grid.colour = hit [1] - 1;
        break;
      }
      main_force_redraw (window->handle);
    break;
  }
}

static void print_setcorner (dbox d)

{ char realbuf [100];

  ftracef0 ("print_setcorner\n");
  main_set_printer_data (); /*Update menus_print_where if pagesize changed*/
  sprintf (realbuf, "%.2f", menus_print_where.dx/print_scalefactor);
  dbox_setfield (d, d_Print_XCorner, realbuf);
  sprintf (realbuf, "%.2f", menus_print_where.dy/print_scalefactor);
  dbox_setfield (d, d_Print_YCorner, realbuf);
}

static void print_readcorner (dbox d)

{ double t, max;
  char realbuf [100];
  int n;

  ftracef0 ("print_readcorner\n");
  max = 0x7FFFFFFF / print_scalefactor;

  dbox_getfield (d, d_Print_XCorner, realbuf, 100);
  ftracef2 ("readcorner x '%s', %g\n", realbuf, print_scalefactor);
  if (sscanf (realbuf, "%lf%n", &t, &n) == 1 &&
      n == strlen (realbuf))
  { if (t>max) t = max;
    if (t<-max) t = -max;
    menus_print_where.dx = (int) (t*print_scalefactor);
  }

  dbox_getfield (d, d_Print_YCorner, realbuf, 100);
  ftracef2 ("readcorner x '%s', %g\n", realbuf, print_scalefactor);
  if (sscanf (realbuf, "%lf%n", &t, &n) == 1 &&
      n == strlen (realbuf))
  { if (t>max) t = max;
    if (t<-max) t = -max;
    menus_print_where.dy = (int) (t*print_scalefactor);
  }

  ftracef2 ("read corner %d %d\n",
      menus_print_where.dx, menus_print_where.dy);
}


/**********************************************
 * Print Sprite DBox stuff                    *
 **********************************************/

/*FIX G-RO-7139 JRC 17 Oct '91 menus_do_print() split into two parts:
  queue_print() displays the dialogue box (if necessary) and then queues the
  file for later printing by calling xfersend_print(). menus_do_print() actually
  prints the file using options saved at the end of the file.*/

/*FIX G-RO-7139 JRC 17 Oct '91 New function queue_print() queues a file for
  later printing.*/

static int Do_Print(char *filename, void *handle)

{ filename = filename; /*for Norcroft*/
  return menus_do_print (handle)? xfersend_printPrinted: xfersend_printFailed;
}

static BOOL queue_print (dbox d, main_sprite *sprite)

{ psprite_info info;

  ftracef1 ("queue_print, sprite 1 called \"%.12s\"\n",
      psprite_address (sprite)->name);

  if (d != NULL)
  { print_copies           = dbox_getnumeric (d, d_Print_Copies);
    print_landscape        = dbox_getnumeric (d, d_Print_Landscape);
    print_scale.scale_xmul = dbox_getnumeric (d, d_Print_XMul);
    print_scale.scale_xdiv = dbox_getnumeric (d, d_Print_XDiv);
    print_scale.scale_ymul = dbox_getnumeric (d, d_Print_YMul);
    print_scale.scale_ydiv = dbox_getnumeric (d, d_Print_YDiv);
    print_readcorner (d);
  }

  ftracef2 ("landscape: %s; unit: %s\n", WHETHER (print_landscape),
      print_scalefactor == PRINT_INCHES_TO_72000? "inches": "cm");

  psprite_read_full_info (sprite, &info);

2304
  if (!xfersend_print (FileType_Sprite, info.name, info.size,
Neil Turton's avatar
Neil Turton committed
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
      /*saver*/ &save_sprite, /*sender - no point yet*/ NULL,
      /*printer*/ &Do_Print, /*handle*/ sprite))
    return FALSE;

  return TRUE;
}

/*FIX G-RO-7139 JRC 17 Oct '91 New function menus_do_print() prints a given
  file - called when the printer manager sends Message_PrintTypeOdd.*/

BOOL menus_do_print (main_sprite *sprite)

{ BOOL more, done_begin = FALSE, done_select_job = FALSE,
    done_translate = FALSE;
  print_box pbox;
  print_transmatstr transform;
  os_error *err;
  int id, job = 0, oldjob;
  print_pagesizestr psize;
  psprite_info info;
  print_infostr infostr;