Menus 125 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 <limits.h>
61
#include "Global/FileTypes.h"
62 63
#include "Global/VduExt.h"
#include "Global/Sprite.h"
Neil Turton's avatar
Neil Turton committed
64 65 66 67

#include "bbc.h"
#include "colourmenu.h"
#include "colourtran.h"
ROOL's avatar
ROOL committed
68
#include "dbox.h"
Neil Turton's avatar
Neil Turton committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
#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"
84 85 86 87 88 89
#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
90 91
#include "MenuD.h"
#include "trace.h"
92 93
#include "PaintLib.h"
#include "AltRename.h"
94
#include "Clipboard.h"
Neil Turton's avatar
Neil Turton committed
95

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

static int Do_Print (char *, void *);

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

Neil Turton's avatar
Neil Turton committed
110 111 112 113 114 115 116 117 118 119 120 121 122
/***********************
 * 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,
123
  sprite_selection_menu,
ROOL's avatar
ROOL committed
124
  sprite_advopts_menu,
125 126
  sprite_misc_menu,
  sprite_rotate_menu;
Neil Turton's avatar
Neil Turton committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

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
144
static dbox artistfriendly_dbox;
Neil Turton's avatar
Neil Turton committed
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 178 179

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;
180
  char temp[64];
Neil Turton's avatar
Neil Turton committed
181 182 183 184 185 186 187

  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)
188
    {
Neil Turton's avatar
Neil Turton committed
189 190 191
      dbox_setnumeric (d, d_Info_Mode, info.mode);
    }
    else
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 264 265
      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
266
        sprintf(temp,msgs_lookup("PntGF"),180.0f/(float)(1L<<xeig),180.0f/(float)(1L<<yeig));
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
        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
293
    }
294 295
    sprintf(temp,msgs_lookup(masktypes[transparency*2+1]),alphabits);
    dbox_setfield (d, d_Info_MaskSize, temp);
Neil Turton's avatar
Neil Turton committed
296

297 298 299 300 301 302 303 304 305
    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
306 307 308 309 310 311 312 313 314 315 316

    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);

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 416 417
    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
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435

    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"));
436
    dbox_setfield (d, d_Prog_Version,  msgs_lookup ("_Version"));
Neil Turton's avatar
Neil Turton committed
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 496 497
    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 */
498 499 500 501 502 503
  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
504 505 506 507 508 509 510 511 512

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

/*************************************
 * Ensure spritearea large enough    *
 *************************************/
513

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

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

  /*
   * 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
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556

  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);
557

Neil Turton's avatar
Neil Turton committed
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
  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
620

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

Neil Turton's avatar
Neil Turton committed
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 721 722
  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;
723
    file_str.loadaddr = FileType_Sprite;
Neil Turton's avatar
Neil Turton committed
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 781 782
    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
783 784
    }
  }
Neil Turton's avatar
Neil Turton committed
785 786 787 788 789 790 791 792 793 794

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;
795
  int len, oldattribs = -1;
Neil Turton's avatar
Neil Turton committed
796 797 798 799 800 801 802 803 804
  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);
805 806

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

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

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

  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
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 918 919
  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);
}

920
static void menus_save_selection (main_window *window)
Robert Sprowson's avatar
Robert Sprowson committed
921 922

{ ftracef0 ("menus_save_selection\n");
923 924

  if (window->selection.count==1)
Robert Sprowson's avatar
Robert Sprowson committed
925 926 927 928 929 930 931 932 933
  { 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;
      }
934 935 936 937
    }
    return;
  }

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

Neil Turton's avatar
Neil Turton committed
943 944 945 946 947 948
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);
949

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

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

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");
971
    myxfersend (FileType_Sprite, name, window->data->file.spritearea->size,
Neil Turton's avatar
Neil Turton committed
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 1024 1025
        &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
1026 1027
    }
  }
Neil Turton's avatar
Neil Turton committed
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
  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
1082
         sprite = sprite->link)
Neil Turton's avatar
Neil Turton committed
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
      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 ();
}

1098
static void insdel_drawEOR(main_sprite_window *sprw, int x0, int y0)
Robert Sprowson's avatar
Robert Sprowson committed
1099 1100
{
  int xmul = sprw->blobsize.scale_xmul*insdel_sprite->mode.scale_xmul;
Steve Revill's avatar
Steve Revill committed
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
  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
1113 1114
    int top = MAX(insdel_row, Row) + 1;
    int bottom = MIN(insdel_row, Row);
Steve Revill's avatar
Steve Revill committed
1115 1116 1117 1118 1119 1120
    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
1121 1122
    int left = MIN(insdel_col, Column);
    int right = MAX(insdel_col, Column) + 1;
Steve Revill's avatar
Steve Revill committed
1123 1124 1125 1126 1127
    bbc_rectanglefill (x0 + left*xmul/xdiv, MAX (y0, 0),
        (right-left)*xmul/xdiv-1, 0x1FFF);
  }
}

Neil Turton's avatar
Neil Turton committed
1128 1129 1130 1131 1132 1133 1134
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
1135
  { int more, x0, y0;
Neil Turton's avatar
Neil Turton committed
1136 1137 1138 1139
    wimp_redrawstr rds;
    wimp_winfo curr;

    curr.w = sprw->window->handle;
Robert Sprowson's avatar
Robert Sprowson committed
1140
    wimpt_noerr (paintlib_get_wind_info (&curr));
Neil Turton's avatar
Neil Turton committed
1141 1142 1143 1144 1145 1146 1147 1148
    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
1149
    { insdel_drawEOR(sprw, x0, y0);
Neil Turton's avatar
Neil Turton committed
1150 1151 1152 1153 1154 1155 1156 1157 1158
      wimpt_noerr (wimp_get_rectangle (&rds, &more));
    }
  }
}

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

{ ftracef0 ("insdeltool_click\n");

1159
  if ((mpos->bbits & wimp_BLEFT) && window->data->sprite.sprite == insdel_sprite)
Neil Turton's avatar
Neil Turton committed
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
                           /* 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)
1184 1185 1186 1187
  {
    int x, y;

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

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

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

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

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

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

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

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

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 ("!"))

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

  /* 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;
}

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 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
void menus_insdel (main_window *window, BOOL col, BOOL insert,
                   BOOL show_dbox, BOOL refetch_coords)
{ main_sprite *sprite = window->data->sprite.sprite;
  sprite_area **sarea = &sprite->file->spritearea;
  int number = 0 /*for Norcroft*/;
  sprite_id sid;
  char name [NAME_LIMIT + 1];

  ftracef0 ("menus_insdel\n");

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

  wimp_mousestr mouse;
  wimpt_noerr (wimp_get_point_info (&mouse));
  if (refetch_coords)
  {
    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);
  }

  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 (show_dbox)
  { wimp_i i;

    if (col)
    { if (insert)
        insdel_d = dbox_new ("insertcols");
      else
        insdel_d = dbox_new ("deletecols");
    }
    else
    { if (insert)
        insdel_d = dbox_new ("insertrows");
      else
        insdel_d = dbox_new ("deleterows");
    }
    if (insdel_d == 0)
      return;
    ftracef0 ("dbox made\n");

    /* If last event was a keypress, RISCOS_Lib will open the dbox near */
    /* the caret, which we need to reposition on the sprite window.     */
    wimp_eventstr *last_event = wimpt_last_event ();
    if (last_event && last_event->e == wimp_EKEY)
    { /* We'll set the caret position near the mouse coordinates */
      wimp_wstate s;
      wimp_caretstr c;
      wimp_get_caret_pos (&c);
      if (c.w != (wimp_w) -1)
      { wimpt_noerr (wimp_get_wind_state (c.w, &s));
        c.x = mouse.x - (s.o.box.x0 - s.o.x) - 52;
        c.y = mouse.y - (s.o.box.y1 - s.o.y) + 72;
        wimpt_noerr (wimp_set_caret_pos (&c));
      }
    }

    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*/
      number = (int)0x80000000;
      insert = col = FALSE; /* A fiddle */
    }
    dbox_dispose (&insdel_d);
  }

  if (!insdel_pending) stop_insdel_graphics ();

  if (show_dbox)
  { 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);
  }
}

1362
void menus_showmag (void *handle)
Neil Turton's avatar
Neil Turton committed
1363

1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
{ menus_showmag_getstate (handle, 0, 0);
  /* Reset the offsets for the Ctrl-resize zoom function */
  /* An actual Ctrl-resize will call menus_showmag_getstate() directly */
  main_sprite_window *sprite = (main_sprite_window *) handle;
  sprite->resizestart.offsetx = -1;
  sprite->resizestart.offsety = -1;
  sprite->resizestart.window_width = 0;
  sprite->resizestart.window_height = 0;
}

wimp_wstate menus_showmag_getstate (void *handle, int window_width, int window_height)

Neil Turton's avatar
Neil Turton committed
1376
{ main_sprite_window *sprite = (main_sprite_window *) handle;
1377
  int m, d;
Neil Turton's avatar
Neil Turton committed
1378 1379
  wimp_wstate wstate;

1380
  ftracef0 ("menus_showmag_getstate\n");
Neil Turton's avatar
Neil Turton committed
1381 1382 1383 1384 1385 1386 1387 1388
  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
1389

1390 1391 1392 1393
  if (window_width < 1)
  { window_width = (wstate.o.box.x1-wstate.o.box.x0)*d*main_current_options.zoom.mul/
       (m*main_current_options.zoom.div);
  }
ROOL's avatar
ROOL committed
1394 1395 1396 1397 1398 1399 1400
  /* 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);

1401
  sprwindow_set_work_extent_custom (sprite->window, FALSE, window_width, window_height);
Neil Turton's avatar
Neil Turton committed
1402 1403 1404 1405 1406

  /* 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
1407
  wstate.o.box.x1 = wstate.o.box.x0 + window_width;
Neil Turton's avatar
Neil Turton committed
1408
  wstate.o.box.y0 = wstate.o.box.y1 -
1409 1410
     ((window_height > 0) ? window_height : ((wstate.o.box.y1-wstate.o.box.y0)*d*main_current_options.zoom.mul/
     (m*main_current_options.zoom.div)));
Neil Turton's avatar
Neil Turton committed
1411 1412 1413 1414 1415
  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 */
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
  if (sprite->resizestart.offsetx < 0)
  { /* Bug fix. Previously the scroll offsets were updated and truncated at
     * every change in zoom level, causing the scrollbars to wander, so now
     * we store the sprite offsets independent of zoom level and only
     * recalculate when the scroll bars are moved.
     */
    sprite->resizestart.offsetx = wstate.o.x * d / m;
    sprite->resizestart.offsety = wstate.o.y * d / m;
  }
#if 0
  /* Old code with accumulated rounding errors */
Neil Turton's avatar
Neil Turton committed
1427 1428 1429 1430
  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);
1431 1432 1433 1434
#else
  wstate.o.x = sprite->resizestart.offsetx * main_current_options.zoom.mul / main_current_options.zoom.div;
  wstate.o.y = sprite->resizestart.offsety * main_current_options.zoom.mul / main_current_options.zoom.div;
#endif
Neil Turton's avatar
Neil Turton committed
1435 1436

  wimpt_noerr (wimp_open_wind (&wstate.o));
1437
  return wstate;
Neil Turton's avatar
Neil Turton committed
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
}


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

static void sprite_palette_has_changed (main_sprite *sprite)

{ ftracef0 ("sprite_palette_has_changed\n");
ROOL's avatar
ROOL committed
1448
  sprite->artistfriendly = artist_unknown;
Neil Turton's avatar
Neil Turton committed
1449 1450 1451 1452 1453 1454 1455 1456
  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
1457 1458 1459
  main_sprite_window *windows = sprite->windows;
  if (windows)
    colourpanel_redraw (windows->window);
Neil Turton's avatar
Neil Turton committed
1460 1461 1462 1463 1464
  main_set_modified (sprite->file);
}

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

Robert Sprowson's avatar
Robert Sprowson committed
1465 1466 1467
  /*Note: this routine assumes the palette is the right length for the
    sprite.*/

Neil Turton's avatar
Neil Turton committed
1468 1469 1470 1471 1472
{ 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
1473
  os_error *error;
Neil Turton's avatar
Neil Turton committed
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494

  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
1495 1496 1497 1498 1499
  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
1500 1501
    }
  }
ROOL's avatar
ROOL committed
1502 1503 1504 1505 1506 1507 1508 1509
  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
1510

ROOL's avatar
ROOL committed
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522
      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
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535

  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
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604
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
1605 1606 1607 1608
static void Apply_Colour (dboxtcol_colour col, void *handle)

{ main_sprite *sprite = (main_sprite *) handle;
  sprite_header *spr = psprite_address (sprite);
ROOL's avatar
ROOL committed
1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623
  /* If we need to remap the palette layout to artist-friendly
     without showing the confirmation dbox, then we must do it AFTER
     the user has edited the colour, so it's on a click rather than
     just a menu hit */
  BOOL could_remap = main_current_options.advanced.artist_friendly
                     && main_current_options.advanced.remap_palette
                     && colours_is_artist_friendly (sprite);
  if (could_remap && !main_current_options.advanced.ask_remap_palette)
  { sprite->artistfriendly = artist_friendly;
    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->artistfriendly = artist_unfriendly;
  }
1624
  int *colour = &(&spr->mode + 1) [2*sprite->gcol.colour];
Neil Turton's avatar
Neil Turton committed
1625 1626 1627 1628 1629 1630 1631 1632

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

  sprite_palette_has_changed (sprite);
}

ROOL's avatar
ROOL committed
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704
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);
    }
  }
}

1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 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
static int count_dbox_digits (dbox d, dbox_field f)

{ char a[20];
  int n = 0;
  int i = 0;
  BOOL neg = FALSE;
  BOOL leading0s = TRUE;

  dbox_getfield ((dbox) d, f, a, 20);

  while (1)
  { if (a[i] == 0)
      break;
    if (a[i] == '-')
    { if (neg || !leading0s)
      { /* Fail */
        return 0;
      }
      else
        neg = TRUE;
    }
    else if (a[i] >= '0' && a[i] <= '9')
    { if (a[i] > '0' || !leading0s)
      {
        n++;
        leading0s = FALSE;
      }
    }
    else
    { /* Fail */
      return 0;
    }
    i++;
  }

  if (neg)
  { /* Negative no. of digits indicates negative number */
    return -n;
  }
  return n;
}

static BOOL adjust_size_dboxrawevents (dbox d, void *event, void *handle)

{ int top, bottom, left, right, rows, cols, toadd, newrows, newcols;
  int roomw, roomh;
  int icon;
  psprite_info info;
  main_sprite *sprite = (main_sprite *)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, (void *)"PntHD");
  }

  e = (wimp_eventstr*)event;
  icon = -1;

  switch (e->e)
  {
    case wimp_EKEY:
      icon = e->data.key.c.i;
      /* Fall through */
    case wimp_EBUT:
      psprite_read_full_info (sprite, &info);
      rows    = info.height;
      cols    = info.width;

      /* We validate everything but don't correct a field visibly while the
         user is typing in it. */
      top = dbox_getnumeric (d, d_Adjust_Top);
      bottom = dbox_getnumeric (d, d_Adjust_Bottom);
      left = dbox_getnumeric (d, d_Adjust_Left);
      right = dbox_getnumeric (d, d_Adjust_Right);
      newcols = dbox_getnumeric (d, d_Adjust_Cols);
      newrows = dbox_getnumeric (d, d_Adjust_Rows);

      /* Overflow is possible if 10 digits are entered. It can't happen
         with a negative number as only 9 digits will fit with the '-' */
      if (count_dbox_digits (d, d_Adjust_Top) > 9 ||
          top > 999999999)
        top = 999999999;
      else if (top < -999999999)
        top = -999999999;

      if (count_dbox_digits (d, d_Adjust_Bottom) > 9 ||
          bottom > 999999999)
        bottom = 999999999;
      else if (bottom < -999999999)
        bottom = -999999999;

      if (count_dbox_digits (d, d_Adjust_Left) > 9 ||
          left > 999999999)
        left = 999999999;
      else if (left < -999999999)
        left = -999999999;

      if (count_dbox_digits (d, d_Adjust_Right) > 9 ||
          right > 999999999)
        right = 999999999;
      else if (right < -999999999)
        right = -999999999;

      if (newcols < 1)
        newcols = 1;
      else if (newcols > 999999999)
        newcols = 999999999;

      if (newrows < 1)
        newrows = 1;
      else if (newrows > 999999999)
        newrows = 999999999;

      /*Check for integer overflow*/
      roomw = INT_MAX - cols;
      if (cols < 0) roomw = 0;
      roomh = INT_MAX - rows;
      if (rows < 0) roomh = 0;

      switch (icon)
      { case d_Adjust_Top:
        case d_Adjust_Bottom:
          toadd = top + bottom;
          if (toadd < 0 && -toadd >= rows)
          { /* Can't make sprite shorter than 1 pixel */
            toadd = 1 - rows;
          }
          if (toadd > roomh)
            toadd = roomh;

          if (rows + toadd > 999999999)
            newrows = 999999999;
          else
            newrows = rows + toadd;

          dbox_setnumeric (d, d_Adjust_Rows, newrows);
        break;
        case d_Adjust_Left:
        case d_Adjust_Right:
          toadd = left + right;
          if (toadd < 0 && -toadd >= cols)
          { /* Can't make sprite narrower than 1 pixel */
            toadd = 1 - cols;
          }
          if (toadd > roomw)
            toadd = roomw;

          if (cols + toadd > 999999999)
            newcols = 999999999;
          else
            newcols = cols + toadd;

          dbox_setnumeric (d, d_Adjust_Cols, newcols);
        break;
        case d_Adjust_Cols:
          toadd = newcols - cols;
          if (toadd > roomw)
            toadd = roomw;
          right = toadd - left;

          if (right > 999999999)
            right = 999999999;
          else if (right < -999999999)
            right = -999999999;

          dbox_setnumeric (d, d_Adjust_Right, right);
        break;
        case d_Adjust_Rows:
          toadd = newrows - rows;
          if (toadd > roomh)
            toadd = roomh;
          top = toadd - bottom;

          if (top > 999999999)
            top = 999999999;
          else if (top < -999999999)
            top = -999999999;

          dbox_setnumeric (d, d_Adjust_Top, top);
        break;
      }

      if (icon != d_Adjust_Top)
        dbox_setnumeric (d, d_Adjust_Top, top);
      if (icon != d_Adjust_Bottom)
        dbox_setnumeric (d, d_Adjust_Bottom, bottom);
      if (icon != d_Adjust_Left)
        dbox_setnumeric (d, d_Adjust_Left, left);
      if (icon != d_Adjust_Right)
        dbox_setnumeric (d, d_Adjust_Right, right);
      if (icon != d_Adjust_Cols)
        dbox_setnumeric (d, d_Adjust_Cols, newcols);
      if (icon != d_Adjust_Rows)
        dbox_setnumeric (d, d_Adjust_Rows, newrows);

    break;
  }
  /* RISCOS_Lib still needs to handle the message */
  return(FALSE);
}

Neil Turton's avatar
Neil Turton committed
1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957
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");
1958
            myxfersend (FileType_Palette, msgs_lookup ("PntG5"), size,
Neil Turton's avatar
Neil Turton committed
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
                &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
2021
        { menus_select_colour (window, FALSE, FALSE);
Robert Sprowson's avatar
Robert Sprowson committed
2022
        }
Neil Turton's avatar
Neil Turton committed
2023 2024 2025 2026
        break;

        case s_Paint_Show_Col:
          ftracef0 ("show colour menu\n");
ROOL's avatar
ROOL committed
2027
          colours_create_window (sprite, FALSE);
Neil Turton's avatar
Neil Turton committed
2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049
          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
2050 2051
          { wimp_eventstr ev = {0}, *ev2;
            int mx = 0, my = 0;
ROOL's avatar
ROOL committed
2052 2053
            BOOL couldRemap = main_current_options.advanced.artist_friendly
              && main_current_options.advanced.remap_palette
ROOL's avatar
ROOL committed
2054 2055
              && colours_is_artist_friendly (sprite);

ROOL's avatar
ROOL committed
2056 2057 2058 2059
            if (couldRemap)
            { int dboxResult;
              BOOL inMenu = FALSE;
              /* Save coordinates from the menu. We'll need them for dboxtcol */
ROOL's avatar
ROOL committed
2060 2061 2062
              ev2 = wimpt_last_event ();
              if (ev2 && (ev2->e == wimp_EMENU || ev2->e == wimp_ESEND) &&
                ev2->data.msg.hdr.action == wimp_MMENUWARN)
ROOL's avatar
ROOL committed
2063 2064
              { inMenu = TRUE;
                mx = ev2->data.msg.data.menuwarn.x;
ROOL's avatar
ROOL committed
2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075
                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;
              }
              /* 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)
ROOL's avatar
ROOL committed
2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089
              { dboxResult = remap_to_artistfriendly_dbox ();

                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;
                }
ROOL's avatar
ROOL committed
2090

ROOL's avatar
ROOL committed
2091
                sprite->artistfriendly = artist_unfriendly;
ROOL's avatar
ROOL committed
2092

ROOL's avatar
ROOL committed
2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
                /* 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)));
                }

                /*if (inMenu)*/
                { /* 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);
                }
ROOL's avatar
ROOL committed
2110 2111 2112 2113 2114 2115 2116 2117 2118
              }
            }
            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);
2119
            int *colour = &(&spr->mode + 1) [sprite->gcol.colour*2];
Neil Turton's avatar
Neil Turton committed
2120 2121 2122 2123 2124 2125
            wimp_emask emask;

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

ROOL's avatar
ROOL committed
2126
            dboxtcol (colour, /*allow transparent?*/ FALSE,
Neil Turton's avatar
Neil Turton committed
2127 2128 2129
                msgs_lookup ("PntW8"), &Apply_Colour, (void *) sprite);

            event_setmask (emask);
ROOL's avatar
ROOL committed
2130 2131 2132 2133 2134 2135 2136

            destroy_artistfriendly_dbox ();
          }
        break;

        case s_Paint_Adv_Opt:
          switch (hit [2])
ROOL's avatar
ROOL committed
2137
          { case s_Adv_ArtFriend:
ROOL's avatar
ROOL committed
2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151
               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;
ROOL's avatar
ROOL committed
2152 2153 2154 2155
            case s_Adv_RemapPal:
              main_current_options.advanced.remap_palette =
                !main_current_options.advanced.remap_palette;
            break;
ROOL's avatar
ROOL committed
2156 2157 2158 2159
            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
2160 2161
          }
        break;
ROOL's avatar
ROOL committed
2162 2163 2164 2165 2166

        case s_Paint_Extra_Click:
          main_current_options.advanced.extra_click_focus =
            !main_current_options.advanced.extra_click_focus;
        break;
Neil Turton's avatar
Neil Turton committed
2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 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
      }
    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:
2242
          menus_adjust_size (sprite);
Neil Turton's avatar
Neil Turton committed
2243 2244 2245 2246 2247 2248 2249 2250
        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;
2251
          menus_insdel (window, col, insert, hit [2], FALSE);
Robert Sprowson's avatar
Robert Sprowson committed
2252
        }
Neil Turton's avatar
Neil Turton committed
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
        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))