ToolWindow 70.2 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
/* 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.ToolWindow
 *
 *  Paint: Arthur 2 sprite editor
 *   Sprite tool window handling
 *
 *  Author: A.P. Thompson
 *
 * Upgraded to RISCOSlib - DAHE - 16 Aug 1989
 *  DAHE, 28 Aug  89 - internationalisation
 *  DAHE, 13 Sept 89 - get round static data init. problem for module version
 *  JAB,  30 Oct  90 - Now brings the toolbox to the front if it is already
 *                     open
 *  JAB,  26 Feb  91 - added better interactive help for the tool window
 */

ROOL's avatar
ROOL committed
30 31 32 33 34 35 36 37 38 39 40 41 42
#include <assert.h>
#include <ctype.h>
#include <stdint.h>
#include <string.h>
#include <swis.h>
#include "Global/FileTypes.h"
#include "Global/Sprite.h"
#include "bbc.h"
#include "colourtran.h"
#include "dbox.h"
#include "dboxquery.h"
#include "help.h"
#include "werr.h"
Neil Turton's avatar
Neil Turton committed
43 44 45
#include "wimpt.h"
#include "wimp.h"
#include "template.h"
ROOL's avatar
ROOL committed
46 47 48 49 50
#include "event.h"
#include "visdelay.h"
#include "xferrecv.h"
#include "jpeg.h"
#include "xfersend.h"
Neil Turton's avatar
Neil Turton committed
51 52

#include "main.h"
53 54 55 56 57
#include "Menus.h"
#include "PSprite.h"
#include "SprWindow.h"
#include "ToolWindow.h"
#include "Tools.h"
Neil Turton's avatar
Neil Turton committed
58 59
#include "ftrace.h"
#include "guard.h"
ROOL's avatar
ROOL committed
60 61
#include "CnPDrag.h"
#include "Colours.h"
Neil Turton's avatar
Neil Turton committed
62 63 64 65 66 67

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

tools_template tools_tool_template;
ROOL's avatar
ROOL committed
68
tools_brush_pane_template tools_brushpane_template;
Neil Turton's avatar
Neil Turton committed
69
wimp_w toolwindow_handle;
ROOL's avatar
ROOL committed
70
wimp_w tools_brushpane_handle = 0;
Neil Turton's avatar
Neil Turton committed
71 72 73 74 75
toolwindow_block *toolwindow_current_tool;
int toolwindow_current_mode = 0;

static toolwindow_block *toolarray [tools_COUNT];
static void init_toolarray (void)
76 77
{ /* Required to generate relocatable code */
  toolarray [0]  = NULL;
Neil Turton's avatar
Neil Turton committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
  toolarray [1]  = &tools_pixelpaint;
  toolarray [2]  = &tools_spraycanpaint;
  toolarray [3]  = &tools_floodfillpaint;
  toolarray [4]  = &tools_linepaint;
  toolarray [5]  = &tools_ellipseoutlinepaint;
  toolarray [6]  = &tools_ellipsepaint;
  toolarray [7]  = &tools_circleoutlinepaint;
  toolarray [8]  = &tools_circlepaint;
  toolarray [9]  = &tools_trianglepaint;
  toolarray [10] = &tools_arcpaint;
  toolarray [11] = &tools_segmentpaint;
  toolarray [12] = &tools_sectorpaint;
  toolarray [13] = &tools_camerapaint;
  toolarray [14] = &tools_scissorpaint;
  toolarray [15] = &tools_textpaint;
  toolarray [16] = &tools_grabberpaint;
  toolarray [17] = &tools_rectanglepaint;
  toolarray [18] = &tools_rectangleoutlinepaint;
  toolarray [19] = &tools_parallelogrampaint;
  toolarray [20] = &tools_parallelogramoutlinepaint;
  toolarray [21] = &tools_brushpaint;
}

ROOL's avatar
ROOL committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
static char recent_user_brush [NAME_LIMIT + 1];
static main_sprite *brushpane_icon_sprites [tools_MAX_BRUSH_ICONS];
static int num_user_brushes = 0;
static int num_brushpane_icons = 0;
static int selected_brushpane_icon = -1;

static char *onlydigits  = "A0-9;Kt";
static char *allkeys = "Kndt";
static char *allowtab = "Kt";
static char *onlydigitsallkeys = "A0-9;Kndt";
static char *decimaldigitsallkeys = "A0-9.;Kndt";

static void destroy_brush_pane (void);

static void make_brush_pane (void);

static unsigned int slider_set (unsigned int, int, int, int, char *);
Neil Turton's avatar
Neil Turton committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

/********************************
 * Set text field in toolwindow *
 ********************************/

static void setdescription (void)

{ wimp_icon descicon;
  wimpt_noerr (wimp_get_icon_info (toolwindow_handle, tools_DESC_ICON,
      &descicon));
  strcpy (descicon.data.indirecttext.buffer,
         msgs_lookup (toolwindow_current_tool->description));
  wimpt_noerr (wimp_set_icon_state (toolwindow_handle, tools_DESC_ICON,
      (wimp_iconflags) 0, (wimp_iconflags) 0));
}

static void set_tool_caret (int i)

{ wimp_caretstr grabcar;
  wimp_icon descicon;

  grabcar.w = toolwindow_handle;
  grabcar.i = i;
  grabcar.x = grabcar.y = grabcar.height = -1;

  /* put caret at end of string */
  wimpt_noerr (wimp_get_icon_info (toolwindow_handle, i, &descicon));
  grabcar.index = strlen (descicon.data.indirecttext.buffer);

  wimpt_noerr (wimp_set_caret_pos (&grabcar));
}

static wimp_wind *drop_wind;

static wimp_i createicon_ind (int n, int selected, char *buffer, int len,
    char *valid)

{ wimp_icreate icreate;
  wimp_i icon;

  icreate.w = toolwindow_handle;
  icreate.i = * (wimp_icon *) ( (char *)&drop_wind [1] + n * sizeof (wimp_icon));
  if (buffer)
  { icreate.i.flags = (wimp_iconflags) (icreate.i.flags | wimp_INDIRECT);
    icreate.i.data.indirecttext.buffer = buffer;
    icreate.i.data.indirecttext.validstring = valid;
    icreate.i.data.indirecttext.bufflen = len;
  }
  if (selected)
    icreate.i.flags = (wimp_iconflags) (icreate.i.flags | wimp_ISELECTED);
  wimpt_noerr (wimp_create_icon (&icreate, &icon));
  return icon;
}

static wimp_i createicon (int n, int selected)

{ return createicon_ind (n, selected, 0, 0, 0);
}

static void extend_tools (void)

{ wimp_wstate state;

  wimpt_noerr (wimp_get_wind_state (toolwindow_handle, &state));
  state.o.box.y0 = state.o.box.y1 - (drop_wind->box.y1 - drop_wind->box.y0);
  wimpt_noerr (wimp_open_wind (&state.o));
}

static void munge_window (void)

{ int caretgrab = -1;

  if (toolwindow_current_tool == &tools_textpaint)
  { drop_wind = template_syshandle ("tool_text");
    extend_tools ();
    tools_icons [0] = createicon (0, 0);
ROOL's avatar
ROOL committed
194
    tools_icons [1] = createicon_ind (1, 0, tools_text_buffer, 256, allowtab);
Neil Turton's avatar
Neil Turton committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    tools_icons [2] = createicon (6, 0);
    tools_icons [3] = createicon_ind (2, 0, tools_text_xsize, 5, onlydigits);
    tools_icons [4] = createicon (7, 0);
    tools_icons [5] = createicon_ind (3, 0, tools_text_ysize, 5, onlydigits);
    tools_icons [6] = createicon (5, 0);
    tools_icons [7] = createicon_ind (4, 0, tools_text_xspace, 5, onlydigits);
    caretgrab = 1;

    /* now enable nulls for all sprites */
    menus_restart_tool ();
  }
  else if (toolwindow_current_tool == &tools_floodfillpaint)
  { drop_wind = template_syshandle ("tool_fill");
    extend_tools ();
    tools_icons [0] = createicon (0, tools_flood_local);
    tools_icons [1] = createicon (1, !tools_flood_local);
  }
  else if (toolwindow_current_tool == &tools_scissorpaint ||
      toolwindow_current_tool == &tools_camerapaint)
  { drop_wind = template_syshandle ("tool_cut");
    extend_tools ();
    tools_icons [0] = createicon (0, !tools_exporting_sprite);
    tools_icons [1] = createicon (1, tools_exporting_sprite);
  }
ROOL's avatar
ROOL committed
219 220 221 222 223
  else if (toolwindow_current_tool == &tools_grabberpaint)
  { drop_wind = template_syshandle ("tool_grab");
    extend_tools ();
    tools_icons [0] = createicon (0, tools_grabber_wrap);
  }
Neil Turton's avatar
Neil Turton committed
224 225 226 227 228 229 230 231 232 233 234 235
  else if (toolwindow_current_tool == &tools_spraycanpaint)
  { drop_wind = template_syshandle ("tool_spray");
    extend_tools ();
    tools_icons [0] = createicon (2, 0);
    tools_icons [1] = createicon_ind (0, 0, tools_spray_density, 4,
        onlydigits);
    tools_icons [2] = createicon (3, 0);
    tools_icons [3] = createicon_ind (1, 0, tools_spray_radius, 4,
        onlydigits);
    caretgrab = 1;
  }
  else if (toolwindow_current_tool ==  &tools_brushpaint)
ROOL's avatar
ROOL committed
236 237 238
  { char *newbrushsprite;

    drop_wind = template_syshandle ("tool_brush");
Neil Turton's avatar
Neil Turton committed
239 240 241 242 243
    extend_tools ();
    tools_icons [tools_bicon_YScText] = createicon (10, 0);
    tools_icons [tools_bicon_YNScBox] = createicon (6, 0);
    tools_icons [tools_bicon_XScText] = createicon (9, 0);
    tools_icons [tools_bicon_XNScBox] = createicon (3, 0);
ROOL's avatar
ROOL committed
244 245 246 247 248 249 250 251 252 253
    tools_icons [tools_bicon_XNScaleMUp] = createicon (21, 0);
    tools_icons [tools_bicon_XNScaleMDown] = createicon (22, 0);
    tools_icons [tools_bicon_XNScaleDUp] = createicon (23, 0);
    tools_icons [tools_bicon_XNScaleDDown] = createicon (24, 0);
    tools_icons [tools_bicon_YNScaleMUp] = createicon (25, 0);
    tools_icons [tools_bicon_YNScaleMDown] = createicon (26, 0);
    tools_icons [tools_bicon_YNScaleDUp] = createicon (27, 0);
    tools_icons [tools_bicon_YNScaleDDown] = createicon (28, 0);
    tools_icons [tools_bicon_BrushText] = createicon (11, 0);
    tools_icons [tools_bicon_NameText] = createicon (13, 0);
Neil Turton's avatar
Neil Turton committed
254
    tools_icons [tools_bicon_UseNew] = createicon (0, 0);
ROOL's avatar
ROOL committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
    tools_icons [tools_bicon_UseGCOL] = createicon (2, tools_brush_use_gcol == 1);
    tools_icons [tools_bicon_UseStamp] = createicon (19, tools_brush_use_gcol == 0);
    tools_icons [tools_bicon_UseTint] = createicon (20, tools_brush_use_gcol == 2);
    tools_icons [tools_bicon_OpacText] = createicon (14, 0);
    tools_icons [tools_bicon_OpacBrdr] = createicon (16, 0);
    tools_icons [tools_bicon_OpacBgnd] = createicon (17, 0);
    tools_icons [tools_bicon_OpacBar] = createicon (18, 0);
    tools_icons [tools_bicon_Opacity] = createicon_ind (15, 0, tools_brush_nopacity,
        5, decimaldigitsallkeys);
    /* Fade the opacity slider if BlendTable / InverseTable unavailable */
    if (!HaveBlendTable())
    { wimpt_noerr (wimp_set_icon_state (toolwindow_handle, tools_icons [tools_bicon_Opacity],
          wimp_INOSELECT, wimp_INOSELECT));
      wimpt_noerr (wimp_set_icon_state (toolwindow_handle, tools_icons [tools_bicon_OpacBar],
          wimp_INOSELECT, wimp_INOSELECT));
      tools_brush_opacity = 1000;
    }
    /* Position the opacity slider correctly */
    tools_brush_opacity = slider_set (tools_brush_opacity,
        tools_bicon_OpacBar, tools_bicon_OpacBgnd,
        tools_bicon_Opacity, tools_brush_nopacity);
Neil Turton's avatar
Neil Turton committed
276
    /* SMC: Look up default brush name in Messages file. */
ROOL's avatar
ROOL committed
277 278 279 280 281 282 283 284 285 286 287 288
    newbrushsprite = msgs_lookup (tools_newbrushsprite);
    /* But keep it in our own indirected buffer */
    if (newbrushsprite != tools_newbrushsprite)
    { strncpy (tools_newbrushsprite, newbrushsprite, NAME_LIMIT);
      tools_newbrushsprite [NAME_LIMIT] = '\0';
    }
    tools_icons [tools_bicon_NewName] = createicon_ind(12, 0, tools_newbrushsprite, 13, allkeys);
    tools_icons [tools_bicon_XNScaleM] = createicon_ind(5, 0, tools_brush_nxscalem, 3, onlydigitsallkeys);
    tools_icons [tools_bicon_XNScaleD] = createicon_ind(4, 0, tools_brush_nxscaled, 3, onlydigitsallkeys);
    tools_icons [tools_bicon_YNScaleM] = createicon_ind(7, 0, tools_brush_nyscalem, 3, onlydigitsallkeys);
    tools_icons [tools_bicon_YNScaleD] = createicon_ind(8, 0, tools_brush_nyscaled, 3, onlydigitsallkeys);

Neil Turton's avatar
Neil Turton committed
289
    strncpy (tools_newbrushsprite, tools_brushsprite, 13);
ROOL's avatar
ROOL committed
290 291
#if 0
    /* These duplicate strings never seemed to be used for anything: */
Neil Turton's avatar
Neil Turton committed
292 293 294 295
    strncpy (tools_brush_nxscalem, tools_brush_cxscalem, 3);
    strncpy (tools_brush_nxscaled, tools_brush_cxscaled, 3);
    strncpy (tools_brush_nyscalem, tools_brush_cyscalem, 3);
    strncpy (tools_brush_nyscaled, tools_brush_cyscaled, 3);
ROOL's avatar
ROOL committed
296 297
    strncpy (tools_brush_nopacity, tools_brush_copacity, 3);
#endif
Neil Turton's avatar
Neil Turton committed
298 299
    caretgrab = tools_bicon_NewName;

ROOL's avatar
ROOL committed
300 301
    make_brush_pane ();

Neil Turton's avatar
Neil Turton committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
    /* now enable nulls for all sprites */
    menus_restart_tool ();
  }

  if (caretgrab >= 0) set_tool_caret (tools_icons [caretgrab]);
}

static void readscale (char *in, int *res, char *out)

{ sscanf (in, "%d", res);
  if (*res == 0) *res = 1;
  strcpy (out, in);
}

/**********************************************************************
 *                                                                    *
 *  Window event handler for sprite tool windows.                     *
 *                                                                    *
 **********************************************************************/

static void demunge_window (int really)

{ int iconlim = 0;

  if (toolwindow_current_tool == &tools_textpaint)
    iconlim = 8;
  else if (toolwindow_current_tool == &tools_spraycanpaint)
    iconlim = 4;
  else if (toolwindow_current_tool == &tools_brushpaint)
  { iconlim = tools_bicon_Max;
ROOL's avatar
ROOL committed
332
    destroy_brush_pane ();
Neil Turton's avatar
Neil Turton committed
333 334 335 336 337 338
    if (really) psprite_free_brush_blocks ();
  }
  else if (toolwindow_current_tool == &tools_floodfillpaint ||
      toolwindow_current_tool == &tools_scissorpaint ||
      toolwindow_current_tool == &tools_camerapaint)
    iconlim = 2;
ROOL's avatar
ROOL committed
339 340
  else if (toolwindow_current_tool == &tools_grabberpaint)
    iconlim = 1;
Neil Turton's avatar
Neil Turton committed
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

  if (iconlim != 0)
  { int i;

    for (i = 0; i < iconlim; i++)
      wimpt_noerr (wimp_delete_icon (toolwindow_handle, tools_icons [i]));
    drop_wind = &tools_tool_template.t;
    extend_tools ();
  }
}

static void set_tool_window_state (void)

{ int i;

  for (i=1; i <= tools_COUNT; i++)
    wimpt_noerr (wimp_set_icon_state (toolwindow_handle, i,
        toolarray [i] == toolwindow_current_tool?
            wimp_ISELECTED: (wimp_iconflags) 0,
        wimp_ISELECTED));

  for (i=0; i <= 3; i++)
    wimpt_noerr (wimp_set_icon_state (toolwindow_handle,
        i + tools_MODE_ICON_BASE,
        i == toolwindow_current_mode? wimp_ISELECTED: (wimp_iconflags) 0,
        wimp_ISELECTED));

  demunge_window (0);
  munge_window ();
  setdescription ();
}

void toolwindow_stop_all_tools (void)

  /*Stop the tool in all sprite windows. Actually, stop it in all sprite
    blocks, not just those with open windows (J R C, 6th Dec 1993).*/

{ main_window *w;
  main_sprite *s;

  ftracef0 ("toolwindow_stop_all_tools\n");

  for (w = main_windows; w != NULL; w = w->link)
    if (w->tag == main_window_is_file)
      for (s = w->data->file.sprites; s != NULL; s = s->link)
        toolwindow_current_tool->stop_action (s);
}

ROOL's avatar
ROOL committed
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 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
void toolwindow_readbrushscale (void)

{ ftracef0 ("readbrushscale\n");
  readscale (tools_brush_nxscalem, &tools_brushscale.scale_xmul,
      tools_brush_cxscalem);
  readscale (tools_brush_nxscaled, &tools_brushscale.scale_xdiv,
      tools_brush_cxscaled);
  readscale (tools_brush_nyscalem, &tools_brushscale.scale_ymul,
      tools_brush_cyscalem);
  readscale (tools_brush_nyscaled, &tools_brushscale.scale_ydiv,
      tools_brush_cyscaled);
}

void toolwindow_readbrushmode (void)

{ ftracef0 ("readbrushmode\n");
  if (toolwindow_handle == 0)
    return;
  /* Reads Shape / Stamp / Tint */
  wimp_icon shape, /*stamp,*/ tint;
  wimpt_noerr (wimp_get_icon_info (toolwindow_handle,
            tools_icons [tools_bicon_UseGCOL], &shape));
  /*wimpt_noerr (wimp_get_icon_info (toolwindow_handle,
            tools_icons [tools_bicon_UseStamp], &stamp));*/
  wimpt_noerr (wimp_get_icon_info (toolwindow_handle,
            tools_icons [tools_bicon_UseTint], &tint));

  if ((shape.flags & wimp_ISELECTED) != 0)
    tools_brush_use_gcol = 1;
  else if ((tint.flags & wimp_ISELECTED) != 0)
    tools_brush_use_gcol = 2;
  else /* Stamp mode: */
    tools_brush_use_gcol = 0;
}

static int get_number_of_builtin_brushes (void)
{ /* Messages tells us how many built-in brush sprites are supplied in
     the Paint:Sprites file, capped at a maximum of
     tools_MAX_BUILTIN_BRUSHES for the brush menu */
  int num_sys_brushes;
  char *msg = msgs_lookup ("BRUSHCOUNT");
  if (!msg || strcmp (msg, "BRUSHCOUNT") == 0)
    num_sys_brushes = 4;
  else
    num_sys_brushes = atoi (msg);

  return MIN(num_sys_brushes, tools_MAX_BUILTIN_BRUSHES);
}

static char *get_first_builtin_brush (void)
{ /* Messages also tells us the name of the first brush sprite. They need
     to all be adjacent to one another in the sprite file. */
  char *brush_name = msgs_lookup ("BRUSH1ST");
  if (!brush_name || strcmp (brush_name, "BRUSH1ST") == 0)
    brush_name = "brush";
  return brush_name;
}

static BOOL Matches (char *a, char *b)

{ ftracef0 ("Matches\n");
  while (*a || *b)
  { if (tolower (*a++) != tolower (*b++))
      return 0;
  }
  return 1;
}

void performbrushGOaction (void)
Neil Turton's avatar
Neil Turton committed
458 459

{ main_sprite *newbrushsprite_ptr, *oldbrushsprite_ptr;
ROOL's avatar
ROOL committed
460 461 462 463 464 465
  char oldbrushsprite [NAME_LIMIT + 1];
  char *newbrushsprite;
  main_sprite *sprite_ptr = NULL;
  int i, num_sys_brushes;
  int prev_brushpane_icon = selected_brushpane_icon;
  BOOL alreadyseen = FALSE;
Neil Turton's avatar
Neil Turton committed
466 467 468 469 470 471

  ftracef0 ("performbrushGOaction\n");
  oldbrushsprite_ptr = tools_brushsprite_ptr;
  strcpy (oldbrushsprite, tools_brushsprite);

  /* SMC: Look up default brush name in Messages file. */
ROOL's avatar
ROOL committed
472 473 474 475 476 477 478 479 480
  newbrushsprite = msgs_lookup (tools_newbrushsprite);
  ftracef2 ("newbrushsprite: '%s' tools_newbrushsprite: '%s'\n", newbrushsprite, tools_newbrushsprite);

  /* We'll check if this brush sprite name is already on the menu, in a clunky way */
  num_sys_brushes = get_number_of_builtin_brushes ();
  if (num_sys_brushes > num_brushpane_icons)
  { /* We're in trouble. This function's been called before the brush pane
       has been built, most likely. */
    ftracef2 ("num_sys_brushes: %d num_brushpane_icons: %d\n", num_sys_brushes, num_brushpane_icons);
Neil Turton's avatar
Neil Turton committed
481
    return;
ROOL's avatar
ROOL committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
  }

  /* Check all the built-in brushes that we put on the menu */
  /* Don't use psprite_find() here because it breaks Paint if the user
     decides to open and close Paint's own Sprites file or possibly
     other sprites with the same names as the built-in brushes. */
  for (i = 0; i < num_sys_brushes; i++)
  { psprite_info sinfo;
    sprite_ptr = brushpane_icon_sprites[i];
    if (sprite_ptr == NULL)
      break;
    psprite_read_full_info (sprite_ptr, &sinfo);
    if (Matches (sinfo.name, newbrushsprite))
    { alreadyseen = TRUE;
      selected_brushpane_icon = i;
      break;
    }
  }

  /* Check any user brush */
  if (!alreadyseen && num_user_brushes > 0 &&
      Matches (recent_user_brush, newbrushsprite))
  { alreadyseen = TRUE;
    selected_brushpane_icon = num_sys_brushes;
  }

  /* If this is a newly seen brush sprite, replace the user brush icon */
  if (!alreadyseen)
  { newbrushsprite_ptr = psprite_find (newbrushsprite, "PntE5a");
    if (newbrushsprite_ptr == NULL)
    { ftracef1 ("Brush sprite %s cannot be found.\n", newbrushsprite);
      return;
    }
    strncpy (recent_user_brush, newbrushsprite, NAME_LIMIT);
    recent_user_brush [NAME_LIMIT] = '\0';
    if (num_user_brushes == 0)
    { num_user_brushes = 1;
      num_brushpane_icons++;
    }
    selected_brushpane_icon = num_sys_brushes;
    brushpane_icon_sprites [selected_brushpane_icon] = newbrushsprite_ptr;
    ftracef0 ("Newly seen user brush added\n");
  }
  else
  { /* Otherwise make sure we use the existing sprite, not a new one */
    newbrushsprite_ptr = brushpane_icon_sprites [selected_brushpane_icon];
  }
Neil Turton's avatar
Neil Turton committed
529

ROOL's avatar
ROOL committed
530 531
#if 0
  /* This shouldn't ever be needed. The user text is always left unchanged. */
Neil Turton's avatar
Neil Turton committed
532 533
  ftracef0 ("calling main_force_redraw\n");
  main_force_redraw (toolwindow_handle);
ROOL's avatar
ROOL committed
534
#endif
Neil Turton's avatar
Neil Turton committed
535 536 537 538 539 540 541 542 543 544 545 546 547 548

  tools_remove_brush ();  /* do before corrupting brushsprite_ptr! */

  ftracef0 ("calling psprite_free_brush_blocks\n");
  wimpt_noerr (psprite_free_brush_blocks ());

  tools_brushsprite_ptr = newbrushsprite_ptr;
  strcpy (tools_brushsprite, tools_newbrushsprite);

  ftracef0 ("calling psprite_set_brush_colour_translations\n");
  if (wimpt_complain (psprite_set_brush_colour_translations ()) != NULL)
  { tools_brushsprite_ptr = oldbrushsprite_ptr;
    strcpy (tools_brushsprite, oldbrushsprite);
    wimpt_noerr (psprite_set_brush_colour_translations ());
ROOL's avatar
ROOL committed
549
    selected_brushpane_icon = prev_brushpane_icon;
Neil Turton's avatar
Neil Turton committed
550 551 552 553 554
    return;
  }

  /*Moved to after psprite_set_brush_colour_translations - less state to
    restore. J R C 5th Jul 1994*/
ROOL's avatar
ROOL committed
555
  toolwindow_readbrushscale ();
Neil Turton's avatar
Neil Turton committed
556 557 558 559 560

  ftracef0 ("calling tools_replace_brush\n");
  tools_replace_brush ();
}

ROOL's avatar
ROOL committed
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 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 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 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
/********************************
 * Brush window pane functions  *
 ********************************/

static void destroy_brush_pane (void)

{ ftracef0 ("destroy_brush_pane\n");
  main_delete_window (tools_brushpane_handle);
  tools_brushpane_handle = 0;
}

static void draw_brush_pane_icon (int i, wimp_redrawstr *rds)

{ main_sprite *sprite = brushpane_icon_sprites [i];
  int x0 = tools_BRUSH_ICON_STRIDE * i + tools_BRUSH_ICON_MARGIN;
  int y1 = -(tools_BRUSH_ICON_MARGIN + tools_BRUSH_ICON_BORDER + tools_BRUSH_ICON_PADDING);
  int y0 = y1 - tools_BRUSH_ICON_SIZE - tools_BRUSH_ICON_BORDER - tools_BRUSH_ICON_PADDING;
  sprite_info infoblock;
  psprite_info sinfo;
  main_scaling_block iconsize;
  BOOL hasSize, selected;
  
  ftracef0 ("draw_brush_pane_icon\n");

  if (i < 0 || i >= num_brushpane_icons)
  { ftracef1 ("Error: brush pane index %d out of range.\n", i);
    return;
  }
  if (sprite == NULL)
  { ftracef2 ("Error: brushpane icon sprite at %d is NULL. num brushpane icons: %d\n", i, num_brushpane_icons);
    return;
  }

  hasSize = !psprite_read_size (sprite, &infoblock);

  psprite_read_full_info (sprite, &sinfo);

  if (hasSize)
  { int iconwidth = tools_BRUSH_ICON_SIZE;
    int iconheight = tools_BRUSH_ICON_SIZE;
    int iconOuterWidth = 2 * (tools_BRUSH_ICON_BORDER +
        tools_BRUSH_ICON_PADDING) + tools_BRUSH_ICON_SIZE;
    int iconOuterHeight = iconOuterWidth;
    int spritewidth, spriteheight; /* sizes after scaling */

    /* Scale the sprite to fit into the brush pane icon, if it's too big */
    iconsize.scale_xmul =
    iconsize.scale_xdiv =
    iconsize.scale_ymul =
    iconsize.scale_ydiv = 1;

    ftracef4 ("Brush sprite '%s' height: %d mode ymul: %d in pixels: %d\n",
        sinfo.name, infoblock.height, sprite->mode.scale_ymul,
        (infoblock.height / sprite->mode.scale_ymul));

    if (infoblock.width > iconwidth)
    { iconsize.scale_ymul =
      iconsize.scale_xmul = iconwidth;
      iconsize.scale_ydiv =
      iconsize.scale_xdiv = infoblock.width;
    }

    if (infoblock.height * iconsize.scale_ymul > iconsize.scale_ydiv * iconheight)
    /* Y dimension larger - it governs scale */
    { iconsize.scale_ymul =
      iconsize.scale_xmul = iconheight;
      iconsize.scale_ydiv =
      iconsize.scale_xdiv = infoblock.height;
    }

    spritewidth = infoblock.width * iconsize.scale_xmul / iconsize.scale_xdiv;
    spriteheight = infoblock.height * iconsize.scale_ymul / iconsize.scale_ydiv;
    if (spritewidth <= 0 || spritewidth > iconwidth)
      spritewidth = iconwidth;
    if (spriteheight <= 0 || spriteheight > iconheight)
      spriteheight = iconheight;

    selected = selected_brushpane_icon == i;

    x0 += rds->box.x0 - rds->scx;
    y0 += rds->box.y1 - rds->scy;
    wimpt_noerr (wimp_setcolour (selected ? 4 : 0));

    bbc_move (x0, y0);
    bbc_plot (bbc_SolidBoth + bbc_MoveCursorRel, 0, iconOuterHeight);
    /* Triangle fill at relative coordinates */
    bbc_plot (bbc_TriangleFill + bbc_DrawRelFore, iconOuterWidth, 0);
    wimpt_noerr (wimp_setcolour (selected ? 0 : 4));
    bbc_plot (bbc_SolidBoth + bbc_MoveCursorRel, 0, -iconOuterHeight);
    bbc_plot (bbc_TriangleFill + bbc_DrawRelFore, -iconOuterWidth, 0);
    wimpt_noerr (wimp_setcolour (selected ? 2 : 1));
    bbc_move (x0 + tools_BRUSH_ICON_BORDER, y0 + tools_BRUSH_ICON_BORDER);
    /* Rectangle fill at relative coordinates */
    bbc_plot (bbc_RectangleFill + bbc_DrawRelFore, tools_BRUSH_ICON_SIZE + 2 * tools_BRUSH_ICON_PADDING,
        tools_BRUSH_ICON_SIZE + 2 * tools_BRUSH_ICON_PADDING);
    ftracef5 ("y0: %d rds->box.y1: %d rds->scy: %d rds->box.y0: %d y1: %d\n",
        y0, rds->box.y1, rds->scy, rds->box.y0, y1);

    /* Move inside the icon's borders and padding to plot the brush sprite */
    x0 += tools_BRUSH_ICON_BORDER + tools_BRUSH_ICON_PADDING +
        tools_BRUSH_ICON_SIZE / 2 - spritewidth / 2;
    y0 += tools_BRUSH_ICON_BORDER + tools_BRUSH_ICON_PADDING +
        tools_BRUSH_ICON_SIZE / 2 - spriteheight / 2;
    wimpt_noerr (psprite_plot_scaled (x0, y0, sprite,
        &iconsize, (/*selected ? 3 :*/ 0)));
  }
}

static void get_brush_pane_icon_bbox (int index, wimp_box *bbox)

{ int x_eig = bbc_vduvar (bbc_XEigFactor);
  int y_eig = bbc_vduvar (bbc_YEigFactor);

  bbox->x0 = tools_BRUSH_ICON_STRIDE * index + tools_BRUSH_ICON_MARGIN - (1 << x_eig);
  bbox->x1 = bbox->x0 + tools_BRUSH_ICON_SIZE + 2 *
      (tools_BRUSH_ICON_BORDER + tools_BRUSH_ICON_PADDING) + (2 << x_eig);
  bbox->y0 = -tools_BRUSH_ICON_MARGIN - tools_BRUSH_ICON_SIZE - 2 *
      (tools_BRUSH_ICON_BORDER + tools_BRUSH_ICON_PADDING) - (1 << y_eig);
  bbox->y1 = -tools_BRUSH_ICON_MARGIN + (2 << y_eig);
}

/* bbox in work area coords */
static void update_brush_pane_icon (int index, wimp_box *bbox)

{ wimp_redrawstr rds;
  int more, dummy;
  os_error *e;
  wimp_paletteword palette_grey; /* To clear the background */

  rds.w = tools_brushpane_handle;
  rds.box = *bbox;
  palette_grey.word = (int)0xdfdfdf00; /* background_colour */

  for (e = wimp_update_wind (&rds,&more);
       !e && more;
       e = wimp_get_rectangle (&rds, &more))
  { /* Clear background to grey */
    wimpt_noerr (colourtran_setGCOL (palette_grey, 1 << 7, 0, &dummy));
    bbc_clg ();
    draw_brush_pane_icon (index, &rds);
  }
}

static void update_brush_pane_icons (int lasticon, int thisicon)

{ ftracef0 ("update_brush_pane_icons\n");
  wimp_box bbox;
  if (lasticon >= 0 && lasticon < num_brushpane_icons)
  { get_brush_pane_icon_bbox (lasticon, &bbox);
    update_brush_pane_icon (lasticon, &bbox);
  }
  if (thisicon >= 0 && thisicon < num_brushpane_icons)
  { get_brush_pane_icon_bbox (thisicon, &bbox);
    update_brush_pane_icon (thisicon, &bbox);
  }
}

static int get_brush_pane_icon_index (char *spritename)

{ char sname [NAME_LIMIT + 1];
  int len, i;
  if (spritename == NULL || strlen (spritename) < 1)
    return -1;
  ftracef1 ("get_brush_pane_icon_index for '%s'\n", spritename);

  for (i = 0; i < num_brushpane_icons; i++)
  { main_sprite *sprite = brushpane_icon_sprites [i];
    if (sprite != NULL)
    { os_error *error;
      if ((error = sprite_getname (sprite->file->spritearea, sname,
            (len = NAME_LIMIT + 1, &len), sprite->spriteno + 1)) != NULL)
      { ftracef1 ("Error getting brush sprite name: %s\n", error->errmess);
        break;
      }
      sname [len] = '\0';
      if (Matches (spritename, sname))
        return i;
    }
  }
  return -1;
}

static int scroll_to_brush_pane_icon (int index)

{ wimp_box bbox;
  wimp_wstate state;
  int scrollx, panewidth, iconcentre;
  ftracef0 ("scroll_to_brush_pane_icon\n");

  /* First see if the icon's already visible */
  get_brush_pane_icon_bbox (index, &bbox);

  wimpt_noerr (wimp_get_wind_state (tools_brushpane_handle, &state));
  panewidth = state.o.box.x1 - state.o.box.x0;
  scrollx = state.o.x;
  ftracef4 ("state.o.x: %d box.x0: %d box.x1: %d width: %d.\n", scrollx,
      state.o.box.x0, state.o.box.x1, panewidth);
  ftracef3 ("icon index: %d x0: %d x1: %d.\n", index, bbox.x0, bbox.x1);

  if (bbox.x0 >= scrollx && bbox.x0 < scrollx + panewidth &&
      bbox.x1 > scrollx && bbox.x1 <= scrollx + panewidth)
  { ftracef1 ("Brush icon %d already visible. No scrolling needed.\n", index);
    return scrollx;
  }

  /* Scroll the right amount to centre the icon. If it's past the right *
   * hand end, the WIMP should prevent scrolling too far. */
  iconcentre = (bbox.x0 + bbox.x1) / 2;
  scrollx = iconcentre - panewidth / 2;
  ftracef3 ("To scroll to icon: %d with centre x: %d, new scroll x is: %d.\n", index, iconcentre, scrollx);
  if (scrollx < 0)
    scrollx = 0;
  return scrollx;
}

static void brush_change_text (char *newname)

{ ftracef1 ("brush_change_text '%s'\n", newname);
  /* Update the Name text icon */
  wimp_caretstr caret;
  wimp_i nameicon = tools_icons [tools_bicon_NewName];
  int oldlen = strlen (tools_newbrushsprite);

  strncpy (tools_newbrushsprite, newname, NAME_LIMIT);
  tools_newbrushsprite [NAME_LIMIT] = '\0';

  if (toolwindow_handle == 0) return;

  /* Reposition the caret if necessary */
  wimpt_noerr (wimp_get_caret_pos (&caret));
  if (caret.w == toolwindow_handle && caret.i == nameicon)
  { /* See if it's at the very end of the string, if so, move to end of
      new string! */
    int len = strlen (tools_newbrushsprite);
    if (caret.index > len || caret.index >= oldlen)
      caret.index = len;
    caret.height = -1;  /* calc from index */
    wimpt_noerr (wimp_set_caret_pos (&caret));
  }
  /* Make the Wimp redraw the writeable icon */
  wimpt_noerr (wimp_set_icon_state (toolwindow_handle, nameicon, 0, 0));
}

static void brush_pane_event_handler (wimp_eventstr *e, void *handle)
Neil Turton's avatar
Neil Turton committed
805 806 807 808

{ handle = handle;
  switch (e->e)
  { case wimp_EKEY:
ROOL's avatar
ROOL committed
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
      wimp_processkey (e->data.key.chcode);
    break;

    case wimp_EOPEN:
      wimpt_complain (wimp_open_wind (&e->data.o));
    break;

    case wimp_EREDRAW:
    { int more, y0, y1;
      wimp_redrawstr rds;

      rds.w = e->data.o.w;
      wimpt_noerr (wimp_redraw_wind (&rds, &more));
      ftracef4 ("visible area ((%d, %d), (%d, %d))\n",
          rds.box.x0, rds.box.y0, rds.box.x1, rds.box.y1);

      /* To clear the background */
      wimp_paletteword palette_grey;
      palette_grey.word = (int)0xdfdfdf00; /* background_colour */

      y1 = -tools_BRUSH_ICON_MARGIN;
      y0 = y1 - tools_BRUSH_ICON_SIZE - 2 * (tools_BRUSH_ICON_PADDING + tools_BRUSH_ICON_BORDER);

      /* Paint always has a brush selected, so force one */
      if (selected_brushpane_icon < 0)
      { /* Defaults to the second brush, currently "circle" */
        selected_brushpane_icon = 1;
        if (selected_brushpane_icon >= num_brushpane_icons)
          selected_brushpane_icon = num_brushpane_icons - 1;
      }

      while (more)
      { int i, dummy;

        /* Clear background to grey */
        wimpt_noerr (colourtran_setGCOL (palette_grey, 1 << 7, 0, &dummy));
        bbc_clg ();

        for (i = 0; i < num_brushpane_icons; i++)
        { /* now do clipping */
          int x0 = tools_BRUSH_ICON_STRIDE * i + tools_BRUSH_ICON_MARGIN;
          int x1 = x0 + tools_BRUSH_ICON_SIZE + 2 * (tools_BRUSH_ICON_PADDING + tools_BRUSH_ICON_BORDER);

          if (main_CLIPS (&rds.g, x0 + rds.box.x0 - rds.scx,
                                  y0 + rds.box.y1 - rds.scy,
                                  x1 + rds.box.x0 - rds.scx,
                                  y1 + rds.box.y1 - rds.scy))
          { draw_brush_pane_icon (i, &rds);
          }
        }
        wimpt_noerr (wimp_get_rectangle (&rds, &more));
      }
    }
    break;

    case wimp_EBUT:
    { wimp_wstate state;
      wimp_box bbox;
      int mx = e->data.but.m.x, my = e->data.but.m.y;
      int i, x, y;
      int lasticon = selected_brushpane_icon;

      wimpt_noerr (wimp_get_wind_state (tools_brushpane_handle, &state));

      /* convert to work extent coordinates */
      x =   mx - state.o.box.x0 + state.o.x;
      y = -(my - state.o.box.y1 + state.o.y);

      if (y <= tools_BRUSH_ICON_MARGIN ||
          y > tools_BRUSH_ICON_STRIDE - tools_BRUSH_ICON_MARGIN)
        break;
      else
      { /* Work out which brush icon the click was over */
        i = x / tools_BRUSH_ICON_STRIDE;
        if (i < 0 || i >= num_brushpane_icons)
          i = -1;

        /* Reject the click if it was on the left or right margin */
        x = x % tools_BRUSH_ICON_STRIDE;
        if (x <= tools_BRUSH_ICON_MARGIN ||
            x > tools_BRUSH_ICON_STRIDE - tools_BRUSH_ICON_MARGIN)
          break;
        else
          selected_brushpane_icon = i;
      }
      get_brush_pane_icon_bbox (lasticon, &bbox);

      if (lasticon >= 0 && lasticon < num_brushpane_icons)
        update_brush_pane_icon (lasticon, &bbox);
      if (selected_brushpane_icon >= 0 && selected_brushpane_icon < num_brushpane_icons)
      { char sname [NAME_LIMIT + 1];
        os_error *error;
        int len, oldlen;
        wimp_icon shape, tint;
        main_sprite *sprite = brushpane_icon_sprites [selected_brushpane_icon];

        /* Update the brush name text as well */
        get_brush_pane_icon_bbox (selected_brushpane_icon, &bbox);
        update_brush_pane_icon (selected_brushpane_icon, &bbox);
        oldlen = strlen (tools_newbrushsprite);
        if (sprite == NULL)
        { ftracef1 ("Error: brushpane icon sprite at %d is NULL\n", selected_brushpane_icon);
          break;
        }
        len = NAME_LIMIT + 1;
        error = sprite_getname (sprite->file->spritearea, sname, &len, sprite->spriteno + 1);
        if (error != NULL)
        { ftracef1 ("Error getting brush sprite name: %s\n", error->errmess);
          break;
        }
        sname [len] = '\0';
        /* Update the brush name text icon */
        brush_change_text (sname);

        /* Clicks on a brush pane icon should actually apply the brush choice */
        wimpt_noerr (wimp_get_icon_info (toolwindow_handle,
                  tools_icons [tools_bicon_UseGCOL], &shape));
        wimpt_noerr (wimp_get_icon_info (toolwindow_handle,
                  tools_icons [tools_bicon_UseTint], &tint));

        if ((shape.flags & wimp_ISELECTED) != 0)
          tools_brush_use_gcol = 1;
        else if ((tint.flags & wimp_ISELECTED) != 0)
          tools_brush_use_gcol = 2;
        else /* Stamp mode: */
          tools_brush_use_gcol = 0;

        performbrushGOaction ();
      }
    }
    break;

    case wimp_ECLOSE:
      ftracef0 ("Close brushes pane window\n");
      destroy_brush_pane ();
    break;

    case wimp_ESEND:
    case wimp_ESENDWANTACK:
      switch (e->data.msg.hdr.action)
      { case wimp_MDATASAVE:
          ftracef0 ("Error: Unexpected MDATASAVE message on brushes pane!\n");
        break;

        case wimp_MDATALOAD:
        { char *name;
          int type;

          type = xferrecv_checkinsert (&name);    /* sets up reply */
          if (type == FileType_Sprite || type == FileType_JPEG)
          { int ok;
            main_window *window = main_new_window (TRUE);

            ftracef1 ("Loading file \"%s\"\n", name);
            visdelay_begin ();
            ok = Load_File (window, name, 1, 0);
            visdelay_end ();
            if (ok > 0)
            { xferrecv_insertfileok ();
              toolwindow_brush_pane_add_sprite (window->data->file.sprites, TRUE);
            }
            else
              main_window_delete (window);
          }
        }
        break;

        case wimp_MHELPREQUEST:
          main_help_message ("PntHTQ", e);
        break;
      }
    break;
  }
  menus_insdel_frig ();
}

static void open_brush_pane (wimp_openstr *toolwinopenptr,
                             wimp_openstr *brushpaneopenptr,
                             int scrolltoicon, BOOL bringtofront)

{ static int scx = -1;
  wimp_wstate toolwinstate, brushpanestate;
  wimp_redrawstr extent;
  int offsetx = 12, offsety = -100, width, height, toolswidth;
  wimp_icon nameIcon, brushIcon;

  ftracef0 ("open_brush_pane\n");
  if (tools_brushpane_handle == 0)
    return;
  if (toolwinopenptr == NULL)
  { wimpt_noerr (wimp_get_wind_state (toolwindow_handle, &toolwinstate));
    toolwinopenptr = &toolwinstate.o;
  }
  if (brushpaneopenptr == NULL)
  { wimpt_noerr (wimp_get_wind_state (tools_brushpane_handle, &brushpanestate));
    brushpaneopenptr = &brushpanestate.o;
  }
  brushpaneopenptr->w = tools_brushpane_handle;

  /* We'll position the brushes pane relative to the other icons */
  wimpt_noerr (wimp_get_icon_info ((wimp_w)toolwindow_handle,
      tools_icons [tools_bicon_NewName], &nameIcon));
  wimpt_noerr (wimp_get_icon_info ((wimp_w)toolwindow_handle,
      tools_icons [tools_bicon_BrushText/*NameText*/], &brushIcon));
  offsetx = nameIcon.box.x0;
  offsety = brushIcon.box.y0;
  toolswidth = tools_tool_template.t.box.x1 - tools_tool_template.t.box.x0;
  width = tools_brushpane_template.t.box.x1 - tools_brushpane_template.t.box.x0;
  height = tools_brushpane_template.t.box.y1 - tools_brushpane_template.t.box.y0;
  /* Calculate the window extent based on the number of icons, with lower limit
     given by window width */
  extent.w      = brushpaneopenptr->w;
  extent.box.y1 = 0;
  extent.box.y0 = -height;
  extent.box.x0 = 0;
  extent.box.x1 = num_brushpane_icons * tools_BRUSH_ICON_STRIDE;
  if (extent.box.x1 < width)
    extent.box.x1 = width;

  ftracef2 ("Resetting brush pane window extent to (%d, %d)\n",
      extent.box.x1, extent.box.y0);

  wimpt_noerr (wimp_set_extent (&extent));

  brushpaneopenptr->box.x0 = toolwinopenptr->box.x0 + offsetx;
  brushpaneopenptr->box.y0 = toolwinopenptr->box.y1 + offsety;
  brushpaneopenptr->box.x1 = brushpaneopenptr->box.x0 + width;
  brushpaneopenptr->box.y1 = brushpaneopenptr->box.y0 + height;

  if (toolwinopenptr->behind == tools_brushpane_handle)
  { if (bringtofront)
      brushpaneopenptr->behind = -1; /* Note: it's not necessarily at the very front! Any way to interrogate the whole window stack? */
  }
  else
  { brushpaneopenptr->behind = (bringtofront)? -1 : toolwinopenptr->behind;
    toolwinopenptr->behind = tools_brushpane_handle;
  }

  brushpaneopenptr->y = 0;
  if (scrolltoicon >= 0)
  { int x = scroll_to_brush_pane_icon (scrolltoicon);
    if (x >= 0)
      brushpaneopenptr->x = x;
  }
  else
    scx = brushpaneopenptr->x;

  if (wimpt_complain (wimp_open_wind (brushpaneopenptr)) == 0)
  { ftracef0 ("Opened brushes window pane\n");
  }
}

static void make_brush_pane (void)

{ char *brush_name;
  int num_sys_brushes = 0;
  main_sprite *sprite_ptr;
ROOL's avatar
ROOL committed
1066 1067
  int panewidth, max_brush_index;
  wimp_box last_bbox;
ROOL's avatar
ROOL committed
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089

  ftracef0 ("make_brush_pane\n");

  num_sys_brushes = get_number_of_builtin_brushes ();
  brush_name = get_first_builtin_brush ();

  /* Go find the first brush sprite */
  sprite_ptr = psprite_find (brush_name, NULL);
  if (sprite_ptr == NULL)
  { werr (TRUE, "Couldn't find first brush sprite for popup menu.");
    return;
  }

  num_brushpane_icons = 0;

  while (num_brushpane_icons < MIN(num_sys_brushes, tools_MAX_BUILTIN_BRUSHES))
  { brushpane_icon_sprites [num_brushpane_icons] = sprite_ptr;
    num_brushpane_icons++;
    sprite_ptr = sprite_ptr->link;
    if (sprite_ptr == NULL)
      break;
  }
ROOL's avatar
ROOL committed
1090
  max_brush_index = num_brushpane_icons;
ROOL's avatar
ROOL committed
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107

  /* Add the last user brush, if any. Mustn't have duplicates *
   * or invalid characters! */
  if (num_user_brushes > 0 && num_brushpane_icons < tools_MAX_BRUSH_ICONS)
  { sprite_ptr = psprite_find (recent_user_brush, NULL);
    brushpane_icon_sprites [num_brushpane_icons] = sprite_ptr;
    if (sprite_ptr == NULL)
    { ftracef1("Couldn't find user brush sprite %s for popup menu.\n",
          recent_user_brush);
    }
    else
      num_brushpane_icons++;
  }

  if (tools_brushpane_handle != 0)
    destroy_brush_pane ();

ROOL's avatar
ROOL committed
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
  /* As the number of brushes is fixed when there's a user brush, we don't
     want a scroll bar if they all fit on the screen */
  get_brush_pane_icon_bbox (max_brush_index, &last_bbox);
  panewidth = tools_brushpane_template.t.box.x1 - tools_brushpane_template.t.box.x0;
  if (last_bbox.x0 >= 0 && last_bbox.x0 <  panewidth &&
      last_bbox.x1 >  0 && last_bbox.x1 <= panewidth)
    tools_brushpane_template.t.flags &= ~wimp_WHSCR;
  else
    tools_brushpane_template.t.flags |= wimp_WHSCR;

ROOL's avatar
ROOL committed
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
  ftracef0 ("Create brushes window pane\n");
  if (main_create_window (&tools_brushpane_template.t, &tools_brushpane_handle,
                          &brush_pane_event_handler, NULL) == 0)
    return; /* Failed to create it */

  open_brush_pane (NULL, NULL, -1, FALSE);
}

void toolwindow_brush_pane_add_sprite (main_sprite *sprite, BOOL selectit)

{ char *brush_name;
  int index;
  int oldlen;
  psprite_info sinfo;
  int lasticon = selected_brushpane_icon;

  ftracef0 ("toolwindow_brush_pane_add_sprite\n");
  if (sprite == NULL)
  { ftracef0 ("Error: Null sprite passed to toolwindow_brush_pane_add_sprite.\n");
    return;
  }

  psprite_read_full_info (sprite, &sinfo);
  brush_name = sinfo.name;
  oldlen = strlen (tools_newbrushsprite);
  if (selectit)
  { strncpy (tools_newbrushsprite, brush_name, NAME_LIMIT);
    tools_newbrushsprite [NAME_LIMIT] = '\0';
  }

  index = get_brush_pane_icon_index (brush_name);
  if (index >= 0)
  { os_error err;

    err.errnum = 0;
    sprintf (err.errmess, msgs_lookup ("PntWI"), brush_name);
    if (selectit)
      selected_brushpane_icon = index;
    ftracef0 (err.errmess);
    wimpt_complain (&err);
  }
  else
  { /* If this is a newly seen brush sprite, set the user brush to it */
    int num_sys_brushes = num_brushpane_icons - num_user_brushes;

    strncpy (recent_user_brush, brush_name, NAME_LIMIT);
    recent_user_brush [NAME_LIMIT] = '\0';
    if (num_user_brushes == 0)
    { num_user_brushes = 1;
      num_brushpane_icons++;
    }
    index = num_sys_brushes;
    if (selectit)
      selected_brushpane_icon = index;
    brushpane_icon_sprites [index] = sprite;
    ftracef2 ("num_user_brushes: %d num_brushpane_icons: %d\n",
        num_user_brushes, num_brushpane_icons);
  }

  if (selectit)
  { wimp_icon shape, /*stamp,*/ tint;

    wimpt_noerr (wimp_get_icon_info (toolwindow_handle,
              tools_icons [tools_bicon_UseGCOL], &shape));
    wimpt_noerr (wimp_get_icon_info (toolwindow_handle,
              tools_icons [tools_bicon_UseTint], &tint));

    if ((shape.flags & wimp_ISELECTED) != 0)
      tools_brush_use_gcol = 1;
    else if ((tint.flags & wimp_ISELECTED) != 0)
      tools_brush_use_gcol = 2;
    else /* Stamp mode: */
      tools_brush_use_gcol = 0;

    performbrushGOaction ();

    /* Reposition the caret if necessary and redraw the text icon */
    brush_change_text (tools_newbrushsprite);
  }
  open_brush_pane (NULL, NULL, selected_brushpane_icon, FALSE);

  update_brush_pane_icons (lasticon, index);
  if (index != selected_brushpane_icon && selected_brushpane_icon >= 0)
  { wimp_box bbox;
    get_brush_pane_icon_bbox (selected_brushpane_icon, &bbox);
    update_brush_pane_icon (selected_brushpane_icon, &bbox);
  }
}

/***********************************************************
 * Checks if a deleted sprite was used as a brush, if so   *
 * it's removed from the brushes pane.                     *
 ***********************************************************/

void toolwindow_brush_check_deletion (main_sprite *sprite)

{ BOOL delcurrentbrush = FALSE, redrawneeded = FALSE;
  BOOL selectedindexchanged = FALSE;
  int num_sys_brushes;

  ftracef0 ("tools_brush_check_deletion\n");
  if (sprite == NULL)
  { ftracef0 ("Sprite for deletion was NULL\n");
    return;
  }
  ftracef2 ("num_user_brushes: %d num_brushpane_icons: %d\n",
      num_user_brushes, num_brushpane_icons);
  if (tools_brushsprite_ptr != NULL)
    delcurrentbrush = (psprite_address (sprite) == psprite_address (tools_brushsprite_ptr));

  num_sys_brushes = get_number_of_builtin_brushes ();

  /* Check if it's the user brush that's been deleted */
  if (num_user_brushes > 0)
  { main_sprite *brush_sprite = brushpane_icon_sprites [num_sys_brushes];
    if (brush_sprite == sprite)
    { redrawneeded = TRUE;
      num_user_brushes = 0;
      brushpane_icon_sprites [num_sys_brushes] = NULL;
      num_brushpane_icons--;
      if (selected_brushpane_icon >= num_sys_brushes)
      { selected_brushpane_icon = 1;
        delcurrentbrush = TRUE;
      }
    }
  }

  ftracef2 ("num_user_brushes: %d num_brushpane_icons: %d\n",
      num_user_brushes, num_brushpane_icons);

  /* If the current brush got deleted, change back to the default one */
  if (delcurrentbrush)
  { /* Update the brush name text with default brush name from Messages */
    brush_change_text (msgs_lookup ("circle"));

    /* As we're selecting a different brush, apply any other settings */
    if (toolwindow_handle != 0)
    { wimp_icon shape, tint;
      wimpt_noerr (wimp_get_icon_info (toolwindow_handle,
                tools_icons [tools_bicon_UseGCOL], &shape));
      wimpt_noerr (wimp_get_icon_info (toolwindow_handle,
                tools_icons [tools_bicon_UseTint], &tint));

      if ((shape.flags & wimp_ISELECTED) != 0)
        tools_brush_use_gcol = 1;
      else if ((tint.flags & wimp_ISELECTED) != 0)
        tools_brush_use_gcol = 2;
      else /* Stamp mode: */
        tools_brush_use_gcol = 0;
    }

    performbrushGOaction ();
    selectedindexchanged = TRUE;
  }
  /* If the selected brush moved, we need to scroll to it */
  if (selectedindexchanged)
    open_brush_pane (NULL, NULL, selected_brushpane_icon, FALSE);

  if (redrawneeded && tools_brushpane_handle != 0)
  { /* Just redraw the entire pane */
    main_force_redraw (tools_brushpane_handle);
  }
}

/***********************************************************
 * Checks if a renamed sprite was used as a brush, if so   *
 * the brush pane and Name box will be updated.            *
 ***********************************************************/

void toolwindow_brush_check_rename (main_sprite *sprite, char *newname)

{ int num_sys_brushes = get_number_of_builtin_brushes ();

  ftracef0 ("toolwindow_brush_check_rename\n");
  if (sprite == NULL)
  { ftracef0 ("Sprite for rename was NULL\n");
    return;
  }

  if (num_user_brushes > 0)
  { main_sprite *brush_sprite = brushpane_icon_sprites [num_sys_brushes];
    if (brush_sprite == sprite)
    { /* Rename */
      strncpy (recent_user_brush, newname, NAME_LIMIT);
      recent_user_brush [NAME_LIMIT] = '\0';
      /* Was this the selected brush? */
      if (num_sys_brushes == selected_brushpane_icon)
      { /* Update the name text icon */
        brush_change_text (newname);
      }
    }
  }
}

static void slider_validate (int textIcon)

{ wimp_caretstr caret;

  ftracef0 ("slider_validate\n");
  if (toolwindow_current_tool != &tools_brushpaint) return;
  wimp_get_caret_pos (&caret);
  if (caret.w == toolwindow_handle)
  { if (caret.i != tools_icons [tools_bicon_Opacity])
    { char opacity [6];
      int value = tools_brush_opacity;

      if (value > 1000)
        value = 1000;
      if (value == 1000)
        strcpy (opacity, "100");
      else
        sprintf (opacity, "%d.%d", value / 10, value % 10);
      /* If the above text is different from the user input, correct it */
      if (strcmp (opacity, tools_brush_nopacity) != 0)
      { wimp_i txticon = tools_icons [textIcon];
        strcpy (tools_brush_nopacity, opacity);
        /* Make the Wimp redraw the writeable icon */
        wimpt_noerr (wimp_set_icon_state (toolwindow_handle, txticon, 0, 0));
      }
    }
  }
}

static unsigned int slider_update (int mx, int barIcon, int bgndIcon, int textIcon, char *textString)

{ ftracef0 ("slider_update\n");
  wimp_wstate wstate;
  wimp_icon bgndicon, baricon;
  int dx, width, max, x_eig;
  unsigned int value = 0;

  wimpt_noerr (wimp_get_wind_state (toolwindow_handle, &wstate));
  mx -= wstate.o.box.x0 + wstate.o.x;
  wimpt_noerr (wimp_get_icon_info (toolwindow_handle, tools_icons [bgndIcon], &bgndicon));
  width = bgndicon.box.x1 - bgndicon.box.x0;
  dx = mx - bgndicon.box.x0;
  wimpt_noerr (wimp_get_icon_info (toolwindow_handle, tools_icons [barIcon], &baricon));
  if (dx > width)
    dx = width;
  else if (dx < 0)
    dx = 0;
  /* Update value */
  /* Subtract 2 pixels so pixels for 0th and max value fall
     well inside the bounds of the icon */
  x_eig = bbc_vduvar (bbc_XEigFactor);
  max = width - (2 << x_eig);
  if (dx > max)
    dx = max;
  if (max > 0)
  { wimp_i txticon = tools_icons [textIcon];
    value = (unsigned int)(1000 * dx / max);
    /* Update the text */
    if (value < 1000)
      sprintf (textString, "%d.%d", value / 10, value % 10);
    else
      strcpy (textString, "100");
    /* Make the Wimp redraw the writeable icon */
    wimpt_noerr (wimp_set_icon_state (toolwindow_handle, txticon, 0, 0));
    /* If we set max or 0, make sure the bar will reflect that */
    if (value == 1000)
      dx = width;
    else if (value == 0)
      dx = 0;
  }

  baricon.box.x1 = baricon.box.x0 + dx;
  wimpt_noerr (os_swix6 (Wimp_ResizeIcon, toolwindow_handle,
      tools_icons [barIcon], baricon.box.x0,
      baricon.box.y0, baricon.box.x1, baricon.box.y1));
  wimp_redrawstr rds;
  rds.w = toolwindow_handle;
  rds.box = bgndicon.box;
  wimp_force_redraw (&rds);
  return value;
}

static unsigned int slider_set (unsigned int value, int barIcon, int bgndIcon, int textIcon, char *textString)

{ wimp_wstate wstate;
  wimp_icon bgndicon, baricon;
  int dx, width, max, x_eig;
  BOOL textChanged = FALSE;

  ftracef0 ("slider_set\n");
  wimpt_noerr (wimp_get_wind_state (toolwindow_handle, &wstate));
  wimpt_noerr (wimp_get_icon_info (toolwindow_handle, tools_icons [bgndIcon], &bgndicon));
  width = bgndicon.box.x1 - bgndicon.box.x0;
  wimpt_noerr (wimp_get_icon_info (toolwindow_handle, tools_icons [barIcon], &baricon));
  /* Correct value if invalid */
  if (value > 1000)
  { value = 1000;
    textChanged = TRUE;
  }
  /* Subtract 2 pixels so pixels for 0th and max value fall
     well inside the bounds of the icon */
  x_eig = bbc_vduvar (bbc_XEigFactor);
  max = width - (2 << x_eig);
  dx = (int)value * max / 1000;
  if (dx > width)
    dx = width;
  else if (dx < 0)
    dx = 0;
  if (dx > max)
    dx = max;
  if (textChanged)
  { wimp_i txticon = tools_icons [textIcon];
    /* Update the text */
    if (value < 1000)
      sprintf (textString, "%d.%d", value / 10, value % 10);
    else strcpy (textString, "100");
    /* Make the Wimp redraw the writeable icon */
    wimpt_noerr (wimp_set_icon_state (toolwindow_handle, txticon, 0, 0));
  }
  /* If we set max or 0, make sure the bar will reflect that */
  if (value == 1000)
    dx = width;
  else if (value == 0)
    dx = 0;

  baricon.box.x1 = baricon.box.x0 + dx;
  wimpt_noerr (os_swix6 (Wimp_ResizeIcon, toolwindow_handle,
      tools_icons [barIcon], baricon.box.x0,
      baricon.box.y0, baricon.box.x1, baricon.box.y1));
  wimp_redrawstr rds;
  rds.w = toolwindow_handle;
  rds.box = bgndicon.box;
  wimp_force_redraw (&rds);

  return value;
}

static BOOL slider_drag_processor  (wimp_eventstr *event, void *handle)

{ wimp_i icon = event->data.but.m.i;
  if (event->e != wimp_EUSERDRAG)
    return FALSE;
  ftracef0("slider_drag_processor\n");
  win_remove_unknown_event_processor (slider_drag_processor, handle);

  if (toolwindow_handle == 0)
  { ftracef0 ("Error: toolwindow_handle NULL in drag processor.\n");
    return TRUE;
  }

  ftracef0("Slider user drag event received.\n");

  if (toolwindow_current_tool == &tools_brushpaint &&
      (icon == tools_icons [tools_bicon_OpacBar] ||
       icon == tools_icons [tools_bicon_OpacBgnd]) &&
       HaveBlendTable())
  { /* Update the slider */
    int mx = event->data.but.m.x;
    ftracef2 ("slider null prev opacity: %d prev text: %s\n", tools_brush_opacity, tools_brush_nopacity);
    tools_brush_opacity = slider_update (mx, tools_bicon_OpacBar,
        tools_bicon_OpacBgnd, tools_bicon_Opacity, tools_brush_nopacity);
    ftracef2 ("slider null new opacity: %d prev text: %s\n", tools_brush_opacity, tools_brush_nopacity);
  }

  return TRUE;
}

static void sprite_tool_event_handler (wimp_eventstr *e, void *handle)

{ handle = handle;
  static wimp_w prevnullholder = -1;
  static int dragging_icon = -1;

  switch (e->e)
  { case wimp_ENULL:
    { static int lasticon;
      static int lastmx;
      if (dragging_icon > -1)
      { wimp_mousestr mouse;
        wimpt_complain (wimp_get_point_info (&mouse));
        /* If the mouse button's released, stop the drag */
        if (!(mouse.bbits & (wimp_BDRAGLEFT | wimp_BDRAGRIGHT | wimp_BLEFT | wimp_BRIGHT)))
        { /* Stop tracking */
          main_claim_idle (prevnullholder);
          dragging_icon = -1;
          lasticon = -1;
          lastmx = -1;
          break;
        }

        if (toolwindow_current_tool == &tools_brushpaint &&
            HaveBlendTable() &&
            (mouse.i == tools_icons [tools_bicon_OpacBar] ||
            mouse.i == tools_icons [tools_bicon_OpacBgnd]) &&
            (lasticon == -1 || lasticon == tools_icons [tools_bicon_OpacBar] ||
            lasticon == tools_icons [tools_bicon_OpacBgnd]) &&
            (lastmx == -1 || lastmx != mouse.x))
        { /* Update the slider */
          tools_brush_opacity = slider_update (mouse.x,
                  tools_bicon_OpacBar, tools_bicon_OpacBgnd,
                  tools_bicon_Opacity, tools_brush_nopacity);
        }
        lasticon = mouse.i;
        lastmx = mouse.x;
      }
      else
      { /* Stop tracking */
        main_claim_idle (prevnullholder);
        dragging_icon = -1;
        lasticon = -1;
        lastmx = -1;
      }
    }
    break;

    case wimp_EKEY:
Neil Turton's avatar
Neil Turton committed
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
      if (e->data.key.chcode == 13)
      { wimp_caretstr caret;
        wimp_get_caret_pos (&caret);
        if (caret.w == toolwindow_handle && caret.i != -1)
        { wimp_icon istate;

          caret.i++;

          while (TRUE)
          { if (wimp_get_icon_info (toolwindow_handle, caret.i, &istate) ||
             (istate.flags & wimp_IDELETED) != 0)
            { if (toolwindow_current_tool == &tools_brushpaint)
ROOL's avatar
ROOL committed
1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
              { int lasticon = selected_brushpane_icon;
                wimp_icon shape, /*stamp,*/ tint;
                wimpt_noerr (wimp_get_icon_info (toolwindow_handle,
                          tools_icons [tools_bicon_UseGCOL], &shape));
                /*wimpt_noerr (wimp_get_icon_info (toolwindow_handle,
                          tools_icons [tools_bicon_UseStamp], &stamp));*/
                wimpt_noerr (wimp_get_icon_info (toolwindow_handle,
                          tools_icons [tools_bicon_UseTint], &tint));

                if ((shape.flags & wimp_ISELECTED) != 0)
                  tools_brush_use_gcol = 1;
                else if ((tint.flags & wimp_ISELECTED) != 0)
                  tools_brush_use_gcol = 2;
                else /* Stamp mode: */
                  tools_brush_use_gcol = 0;
Neil Turton's avatar
Neil Turton committed
1555
                performbrushGOaction ();
ROOL's avatar
ROOL committed
1556 1557 1558 1559
                update_brush_pane_icons (lasticon, selected_brushpane_icon);
                /* Also scroll to the newly selected icon if it's not visible */
                open_brush_pane (NULL, NULL, selected_brushpane_icon, FALSE);
              }
Neil Turton's avatar
Neil Turton committed
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
              caret.i = 0;
            }
            else if ( (istate.flags & wimp_IBTYPE*15) ==
                wimp_IBTYPE*wimp_BWRITABLE)
            { ftracef1 ("Picked icon %d for caret\n", caret.i);
              set_tool_caret (caret.i);
              break;
            }
            caret.i++;
          }
        }
        else
          ftracef0 ("!!!!!!!!!CR can't have been for us!!!!!!!\n");
      }
      else
ROOL's avatar
ROOL committed
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
      { wimp_processkey (e->data.key.chcode);
        if (toolwindow_current_tool == &tools_brushpaint)
        { wimp_caretstr caret;
          wimp_get_caret_pos (&caret);
          if (caret.w == toolwindow_handle)
          { if (caret.i ==
                tools_icons [tools_bicon_NewName])
            { /* If the user typed a brush name, scroll to that icon on the
                 brush pane, if it's present */
              char brushname [NAME_LIMIT + 1];
              int  i;
              strncpy (brushname, tools_newbrushsprite, NAME_LIMIT);
              brushname [NAME_LIMIT] = '\0';
              i = get_brush_pane_icon_index (brushname);
              if (i >= 0)
                open_brush_pane (NULL, NULL, i, FALSE);
            }
            else if (caret.i == tools_icons [tools_bicon_Opacity] && HaveBlendTable())
            { int s, n, d = 0;
              const char *decimal;
              ftracef1 ("Opacity typed '%s'\n", tools_brush_nopacity);
              decimal = strstr (tools_brush_nopacity, ".");
              if (decimal != NULL && strlen (decimal) > 1)
              { if (sscanf (decimal + 1, "%1d%n", &d, &n) < 1 || n == 0)
                  d = 0;
              }
              if (sscanf (tools_brush_nopacity, "%d%n", &s, &n) != 1 ||
                  n == 0)
              { ftracef3 ("Opacity. decimal: %s s: %d n: %d\n", (decimal)?decimal:"", s, n);
                s = 0;
              }
              /* Convert the percentage to fixed-point per-thousand */
              s = s * 10 + d;

              /* Update the slider */
              tools_brush_opacity = slider_set (s, tools_bicon_OpacBar,
                                                   tools_bicon_OpacBgnd,
                                                   tools_bicon_Opacity,
                                                   tools_brush_nopacity);
            }
            else if (caret.i == tools_icons [tools_bicon_XNScaleM] ||
                     caret.i == tools_icons [tools_bicon_XNScaleD] ||
                     caret.i == tools_icons [tools_bicon_YNScaleM] ||
                     caret.i == tools_icons [tools_bicon_YNScaleD])
            { tools_remove_brush ();
              toolwindow_readbrushscale ();
              tools_replace_brush ();
            }
          }
        }
      }
      slider_validate (tools_bicon_Opacity);
Neil Turton's avatar
Neil Turton committed
1627 1628 1629 1630
    break;

    case wimp_EOPEN:
      wimpt_complain (wimp_open_wind (&e->data.o));
ROOL's avatar
ROOL committed
1631 1632 1633

      /* Now reopen the brush pane if necessary */
      open_brush_pane (&e->data.o, NULL, -1, FALSE);
Neil Turton's avatar
Neil Turton committed
1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
    break;

    case wimp_EREDRAW:
    { int more;
      wimp_redrawstr rds;

      rds.w = e->data.o.w;
      wimpt_noerr (wimp_redraw_wind (&rds, &more));
      while (more)
        wimpt_noerr (wimp_get_rectangle (&rds, &more));
    }
    break;

    case wimp_EBUT:
ROOL's avatar
ROOL committed
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
#if 0
      if (toolwindow_current_tool == &tools_brushpaint) /* TODO Or the spray can */
      { /* Enable the opacity controls whenever the tool window is clicked *
         because it's shared across sprites so some may support opacity */
        wimpt_noerr (wimp_set_icon_state (toolwindow_handle, tools_icons [tools_bicon_Opacity],
            0, wimp_INOSELECT));
        wimpt_noerr (wimp_set_icon_state (toolwindow_handle, tools_icons [tools_bicon_OpacBar],
            0, wimp_INOSELECT));
      }
#endif
      if (e->data.but.m.bbits & (wimp_BDRAGLEFT | wimp_BDRAGRIGHT))
      { if (toolwindow_current_tool == &tools_brushpaint &&
              HaveBlendTable() &&
              e->data.but.m.i == tools_icons [tools_bicon_OpacBar] ||
              e->data.but.m.i == tools_icons [tools_bicon_OpacBgnd])
        { /* Update the slider */
          wimp_dragstr drag;
          ftracef0("Opacity slider drag detected.\n");
          wimp_wstate ws;
          wimp_w claimer;
          wimp_icon bgndicon;
          int width;
          int mx = e->data.but.m.x;
          wimpt_noerr (wimp_get_wind_state (toolwindow_handle, &ws));
          wimpt_noerr (wimp_get_icon_info (toolwindow_handle,
              tools_icons [tools_bicon_OpacBgnd], &bgndicon));
          width = bgndicon.box.x1 - bgndicon.box.x0;
          drag.box.x0 = mx;
          drag.box.x1 = mx;
          drag.box.y0 = bgndicon.box.y0 + ws.o.box.y1 - ws.o.y;
          drag.box.y1 = bgndicon.box.y1 + ws.o.box.y1 - ws.o.y - 4;
          drag.parent.x0 = bgndicon.box.x0 + ws.o.box.x0 - ws.o.x;
          drag.parent.x1 = bgndicon.box.x1 + ws.o.box.x0 - ws.o.x -
                           2 * bbc_vduvar (bbc_XEigFactor);
          drag.parent.y0 = bgndicon.box.y0 + ws.o.box.y1 - ws.o.y;
          drag.parent.y1 = bgndicon.box.y1 + ws.o.box.y1 - ws.o.y -
                           2 * bbc_vduvar (bbc_YEigFactor);
          drag.window = toolwindow_handle;
          drag.type = wimp_USER_HIDDEN;
          dragging_icon = e->data.but.m.i;
          claimer = win_idle_event_claimer ();
          if (claimer != toolwindow_handle)
            prevnullholder = claimer;
          main_claim_idle (toolwindow_handle);
          os_swix4 (Wimp_DragBox, 0, &drag, 0, 0);
          win_add_unknown_event_processor (slider_drag_processor, (void *)toolwindow_handle);
        }
      }
      else if (e->data.but.m.bbits & (wimp_BLEFT | wimp_BRIGHT))
Neil Turton's avatar
Neil Turton committed
1697 1698 1699 1700
      { ftracef1 ("Icon %d clicked\n", e->data.but.m.i);

        /*Wind up the previous tool, if it needs it.*/
        if (toolwindow_current_tool == &tools_brushpaint)
ROOL's avatar
ROOL committed
1701 1702 1703 1704 1705 1706 1707
        { BOOL adjust = (e->data.but.m.bbits & wimp_BRIGHT) != 0;

          if (e->data.but.m.i == tools_icons [tools_bicon_UseNew])
          { int lasticon = selected_brushpane_icon;
            wimp_icon shape, tint;
            wimpt_noerr (wimp_get_icon_info (toolwindow_handle,
                      tools_icons [tools_bicon_UseGCOL], &shape));
Neil Turton's avatar
Neil Turton committed
1708
            wimpt_noerr (wimp_get_icon_info (toolwindow_handle,
ROOL's avatar
ROOL committed
1709
                      tools_icons [tools_bicon_UseTint], &tint));
Neil Turton's avatar
Neil Turton committed
1710

ROOL's avatar
ROOL committed
1711 1712 1713 1714 1715 1716
            if ((shape.flags & wimp_ISELECTED) != 0)
              tools_brush_use_gcol = 1;
            else if ((tint.flags & wimp_ISELECTED) != 0)
              tools_brush_use_gcol = 2;
            else /* Stamp mode: */
              tools_brush_use_gcol = 0;
Neil Turton's avatar
Neil Turton committed
1717 1718

            performbrushGOaction ();
ROOL's avatar
ROOL committed
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
            /* scroll to the newly selected icon if it's not visible */
            open_brush_pane (NULL, NULL, selected_brushpane_icon, FALSE);

            update_brush_pane_icons (lasticon, selected_brushpane_icon);
          }
          else if ((e->data.but.m.i == tools_icons [tools_bicon_OpacBar] ||
                    e->data.but.m.i == tools_icons [tools_bicon_OpacBgnd]) &&
                   HaveBlendTable())
          { /* Update the slider */
            int mx = e->data.but.m.x;
            tools_brush_opacity = slider_update (mx, tools_bicon_OpacBar,
                    tools_bicon_OpacBgnd, tools_bicon_Opacity,
                    tools_brush_nopacity);
          } /* Next handle the scale x / y arrow buttons */
          else if (e->data.but.m.i >= tools_icons [tools_bicon_XNScaleMUp] &&
                   e->data.but.m.i <= tools_icons [tools_bicon_YNScaleDDown])
          { wimp_i texticon = 0;
            tools_remove_brush ();
            toolwindow_readbrushscale ();
            if (e->data.but.m.i == tools_icons [tools_bicon_XNScaleMUp])
            { if (adjust && tools_brushscale.scale_xmul > 1)
                tools_brushscale.scale_xmul--;
              else if (!adjust && tools_brushscale.scale_xmul < 99)
                tools_brushscale.scale_xmul++;
              sprintf (tools_brush_nxscalem, "%d", tools_brushscale.scale_xmul);
              texticon = tools_icons [tools_bicon_XNScaleM];
            }
            else if (e->data.but.m.i == tools_icons [tools_bicon_XNScaleMDown])
            { if (!adjust && tools_brushscale.scale_xmul > 1)
                tools_brushscale.scale_xmul--;
              else if (adjust && tools_brushscale.scale_xmul < 99)
                tools_brushscale.scale_xmul++;
              sprintf (tools_brush_nxscalem, "%d", tools_brushscale.scale_xmul);
              texticon = tools_icons [tools_bicon_XNScaleM];
            }
            else if (e->data.but.m.i == tools_icons [tools_bicon_XNScaleDUp])
            { if (adjust && tools_brushscale.scale_xdiv > 1)
                tools_brushscale.scale_xdiv--;
              else if (!adjust && tools_brushscale.scale_xdiv < 99)
                tools_brushscale.scale_xdiv++;
              sprintf (tools_brush_nxscaled, "%d", tools_brushscale.scale_xdiv);
              texticon = tools_icons [tools_bicon_XNScaleD];
            }
            else if (e->data.but.m.i == tools_icons [tools_bicon_XNScaleDDown])
            { if (!adjust && tools_brushscale.scale_xdiv > 1)
                tools_brushscale.scale_xdiv--;
              else if (adjust && tools_brushscale.scale_xdiv < 99)
                tools_brushscale.scale_xdiv++;
              sprintf (tools_brush_nxscaled, "%d", tools_brushscale.scale_xdiv);
              texticon = tools_icons [tools_bicon_XNScaleD];
            }
            else if (e->data.but.m.i == tools_icons [tools_bicon_YNScaleMUp])
            { if (adjust && tools_brushscale.scale_ymul > 1)
                tools_brushscale.scale_ymul--;
              else if (!adjust && tools_brushscale.scale_ymul < 99)
                tools_brushscale.scale_ymul++;
              sprintf (tools_brush_nyscalem, "%d", tools_brushscale.scale_ymul);
              texticon = tools_icons [tools_bicon_YNScaleM];
            }
            else if (e->data.but.m.i == tools_icons [tools_bicon_YNScaleMDown])
            { if (!adjust && tools_brushscale.scale_ymul > 1)
                tools_brushscale.scale_ymul--;
              else if (adjust && tools_brushscale.scale_ymul < 99)
                tools_brushscale.scale_ymul++;
              sprintf (tools_brush_nyscalem, "%d", tools_brushscale.scale_ymul);
              texticon = tools_icons [tools_bicon_YNScaleM];
            }
            else if (e->data.but.m.i == tools_icons [tools_bicon_YNScaleDUp])
            { if (adjust && tools_brushscale.scale_ydiv > 1)
                tools_brushscale.scale_ydiv--;
              else if (!adjust && tools_brushscale.scale_ydiv < 99)
                tools_brushscale.scale_ydiv++;
              sprintf (tools_brush_nyscaled, "%d", tools_brushscale.scale_ydiv);
              texticon = tools_icons [tools_bicon_YNScaleD];
            }
            else if (e->data.but.m.i == tools_icons [tools_bicon_YNScaleDDown])
            { if (!adjust && tools_brushscale.scale_ydiv > 1)
                tools