Draw 104 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
/* 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.Draw
 *
 * Main file for Draw
 *
 * Author:  Richard Manby, David Elworthy
 * Version: 0.17
 * History: 0.10 - 12 June 1989 - header added. Old code weeded.
 *                                converted to use Drawmod for paths
 *                 16 June 1989 - upgrade to use msgs for messages
 *          0.11 - 19 June 1989 - DrawObjects extracted
 *                                broke down big functions
 *                                added keystroke actions
 *                                claim input focus on more actions
 *          0.12 - 23 June 1989 - bug 1501 fixed
 *                                use heap (so we can avoid some mallocs)
 *          0.13 - 29 June 1989 - shift/button actions
 *          0.14 - 11 July 1989 - auto scrolling. Zooms moved out.
 *                 12 July 1989 - Draw$Options added
 *          0.15 - 18 July 1989 - undo added
 *                  7 Aug  1989 - Undo buffer size option
 *          0.16 - 25 Aug  1989 - more work on paper limits
 *          0.17 - 06 Sept 1989 - make heap non-compacting
 *          0.79 - 13 Jan  1992 - set fixed size stack to 6K
 *   OSS    0.83 - 12 Mar  1992 - Split in half to allow name translation as
 *                                part of fix to RP-0716.
 *   OSS    0.84 - 13 Mar  1992 - Get it right this time.
 */

#include <assert.h>
#include <ctype.h>
#include "kernel.h"
#include <limits.h>
#include <locale.h>
#include <math.h>
#include <signal.h>
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
54
#include <swis.h>
55
#include "Global/Variables.h"
56
#include "Global/FileTypes.h"
Neil Turton's avatar
Neil Turton committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

#include "os.h"
#include "akbd.h"
#include "bbc.h"
#include "flex.h"
#include "heap.h"
#include "menu.h"
#include "pointer.h"
#include "res.h"
#include "resspr.h"
#include "wimp.h"
#include "wimpt.h"
#include "win.h"
#include "werr.h"
#include "baricon.h"
#include "template.h"
#include "visdelay.h"
#include "msgs.h"
#include "msgtrans.h"
#include "dboxquery.h"
#include "xferrecv.h"
#include "saveas.h"
#include "bezierarc.h"
#include "help.h"
81 82 83 84
#include "drawmod.h"
#include "jpeg.h"
#include "dbox.h"
#include "font.h"
Neil Turton's avatar
Neil Turton committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 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 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

#include "ftrace.h"
#include "guard.h"

#include "Draw.h"
#include "DrawAction.h"
#include "DrawDispl.h"
#include "DrawDXF.h"
#include "DrawEdit.h"
#include "DrawEnter.h"
#include "DrawFileIO.h"
#include "DrawGrid.h"
#include "DrawHelp.h"
#include "DrawMenu.h"
#include "DrawObject.h"
#include "DrawPrint.h"
#include "DrawSelect.h"
#include "DrawTrans.h"
#include "DrawUndo.h"

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

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

#undef  FREEZE_STACK
#define FREEZE_STACK 1

extern _kernel_ExtendProc flex_budge;

/*External variables*/
fontcatstr draw_fontcat = /*JRC 6 Aug 1990*/
  { 0 /*menu size - not used 12 April 1991*/,
    1 /*system font present*/
    /*, ...*/
  };

currentmodestr draw_currentmode; /*= {15, 1, 2, 0x200, 0x400 };*/
coloursstr     draw_colours; /*initialised from template file*/
diagrec        *draw_startdiagchain = NULL;

draw_objcoord draw_stdcircpoints [13];
static draw_objcoord zero = {0, 0 };

static int modevarlist [] =
{ bbc_GCharSizeX, bbc_GCharSizeY, bbc_GCharSpaceX, bbc_GCharSpaceY,
  bbc_NColour,    bbc_XEigFactor, bbc_YEigFactor,
  -1
};

scale_str draw_scale_cb;
rotat_str draw_rotate_cb;
captu_str draw_capture_cb;
trans_str draw_translate_cb;

/*Data block for recording the palette - set up on a mode or palette change*/
wimp_palettestr draw_palette;

/*Handle of the window that the pointer is in: -1 if none*/
static diagrec *pointer_diag = NULL;

/*N.B. We have chosen 180 graphics units per inch so for A4 paper,*/
/*exact work area limits are:*/
/*A4 portrait (0,0, 1488,2104)              ie  8.25"w x 11.75"h*/
/*A4 landscape (0,0, 2104,1488)             ie 11.75"w x  8.25"h*/

#define A4portrait_scroll    0, 0 /*2104*/
#define A4portrait_extent   {0, 0, 1488, 2104}
#define A4landscape_scroll   0, 1488 /*was 0???*/
#define A4landscape_extent  {0, 0, 2104, 1488}

static
  struct
  { wimp_wind paper;
    wimp_icon colouricons [10];
  }
  blank_view =
  { { {300, 600, 800, 1000},                     /*work area coordinates*/
      A4landscape_scroll,                        /*scroll bar positions*/
      (wimp_w) -1,                               /*in front of all windows*/
      (wimp_wflags) (wimp_WMOVEABLE | wimp_WNEW | wimp_WBACK |
          wimp_WQUIT | wimp_WTITLE | wimp_WTOGGLE |
          wimp_WSIZE | wimp_WVSCR | wimp_WHSCR),
      {0xE,0xD,0x7, Window_WORKBG, 0x4,0xD,0xC,0x0}, /*colours*/
      A4landscape_extent,                            /*work area extent*/
      (wimp_iconflags) (wimp_ITEXT | wimp_IHCENTRE | wimp_IVCENTRE |   /*title icon flags*/
      wimp_INDIRECT | wimp_IFORECOL*1 | wimp_IBACKCOL*0),
      (wimp_iconflags) (wimp_IBTYPE*10),             /*default button type*/
      (void *) 0,                                    /*sprite area pointer*/
      0x00010001,                                    /*minimum window size*/
      {'D','i','a','g',' ',' ','V','i','e','w',' ',0x0},       /*title*/
      0,                                             /*number of icons*/
    }
  };

static struct
{ wimp_wind pane;
  wimp_icon toolcons [15];
} blank_pane;

/*Block to contain the initial settings for the blank view and pane, and
   the shift so far*/
static struct
{ int papery0, papery1;
  int paney0,  paney1;
  int yshift;           /*Positive downwards*/
} blank_position;

static
  draw_options
    initial_options =
    { {Paper_A4, (paperoptions_typ) 0},
      {1.0, 2, {0, 0, 0, 0, 1}},
      {1, 1, 0},
      TRUE,
      {0, 1, 0, 0, 0, 0, 0, 0},
      5000
    };

draw_options draw_current_options;

/*The directory we started up from (<Draw$Dir> variable might not be the
  right place because it is changed if another Draw starts up)*/
static char Draw_Dir [FILENAMEMAX];
#define MAX_OPTIONS 80

/*Raw bounding box, used in routines which form bounds*/
draw_bboxtyp draw_big_box = {INT_MAX, INT_MAX, INT_MIN, INT_MIN };

#if TRACE
  #if FREEZE_STACK
  static int Dont_Budge (int n, void **a)

  { ftracef0 ("NOT BUDGING FLEX NOW!!!\n");
    n = n, a = a;
    return 0;
  }
  #else
  static int Do_Budge (int n, void **a)

  { ftracef0 ("BUDGING FLEX NOW!!!\n");
    return flex_budge (n, a);
  }
  #endif
#endif

BOOL draw_jpegs_rotate = TRUE;
232
BOOL draw_fonts_blend = TRUE;
Neil Turton's avatar
Neil Turton committed
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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 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 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 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 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 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 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599

static void cache_currentmodevars (void)

{ int junk;

  ftracef0 ("cache_currentmodevars\n");
  bbc_vduvars (modevarlist, &draw_currentmode.gcharsizex);

  draw_currentmode.gcharaltered = 0;
  draw_currentmode.pixx     =     1 << draw_currentmode.xeigfactor;
  draw_currentmode.pixy     =     1 << draw_currentmode.yeigfactor;
  draw_currentmode.pixsizex = 0x100 << draw_currentmode.xeigfactor;
  draw_currentmode.pixsizey = 0x100 << draw_currentmode.yeigfactor;

  draw_currentmode.x_wind_limit = bbc_vduvar (bbc_XWindLimit) + 1 <<
      draw_currentmode.xeigfactor << 8;
  draw_currentmode.y_wind_limit = bbc_vduvar (bbc_YWindLimit) + 1 <<
      draw_currentmode.yeigfactor << 8;

  (void) os_byte (135, &junk, &draw_currentmode.mode);
  ftracef1 ("ncolour %u\n", draw_currentmode.ncolour);
}

/*Routines to dump the whole of a diagram - what a wheeze.*/
#if TRACE
  static void *trace_objects (draw_objptr start, size_t size, int indent)

  { draw_objptr obj_ptr, ptr;
    int i;
    size_t obj_size;
    draw_path_tagtype tag;

    ftracef (__FILE__, __LINE__, "trace_objects: start: 0x%X, "
        "size: %d; indent: %d\n",
        start.bytep, size, indent);

    for (i = 0; i < indent; i++) ftracef (NULL, 0, "  ");

    for
    ( ptr = start;
      ptr.bytep < start.bytep + size;
      ptr.bytep += ptr.objhdrp->size
    )
    { if ((obj_size = ptr.objhdrp->size) == 0)
      { ftracef (NULL, 0, "  ***Zero sized object***\n");
        return (void *) 1;
      }

      switch (ptr.objhdrp->tag)
      { default:
          ftracef (NULL, 0, "  ***Unrecognised object type: %d***\n",
              ptr.objhdrp->tag);
        return (void *) 1;

        case draw_OBJFONTLIST:
          ftracef (NULL, 0, "  Font list (size %d)\n", obj_size);
        break;

        case draw_OBJTEXT:
          ftracef (NULL, 0, "  Text line (size %d): \"%s\"\n",
              obj_size, ptr.textp->text);
        break;

        case draw_OBJPATH:
          { drawmod_pathelemptr pathptr = draw_obj_pathstart (ptr);

            ftracef (NULL, 0, "  Path (size %d):", obj_size);
            do
            { if (pathptr.bytep >= start.bytep + size)
              { ftracef (NULL, 0, " ***incomplete path***");
                return (void *) 1;
              }

              switch (tag = pathptr.end->tag)
              { case path_term:
                  ftracef (NULL, 0, " term");
                break;

                case path_ptr:
                  ftracef (NULL, 0, " ptr at 0x%X", pathptr.ptr->ptr);
                  pathptr.ptr++;
                break;

                case path_move_2:
                  ftracef (NULL, 0, " move_2 to (%d, %d)",
                      pathptr.move2->x,  pathptr.move2->y);
                  pathptr.move2++;
                break;

                case path_move_3:
                  ftracef (NULL, 0, " move_3 to (%d, %d)",
                      pathptr.move3->x, pathptr.move3->y);
                  pathptr.move3++;
                break;

                case path_closegap:
                  ftracef (NULL, 0, " closegap");
                  pathptr.closegap++;
                break;

                case path_closeline:
                  ftracef (NULL, 0, " closeline");
                  pathptr.closeline++;
                break;

                case path_bezier:
                  ftracef
                  ( NULL, 0,
                    " bezier via (%d, %d) and (%d, %d) to (%d, %d)",
                    pathptr.bezier->x1, pathptr.bezier->y1,
                    pathptr.bezier->x2, pathptr.bezier->y2,
                    pathptr.bezier->x3, pathptr.bezier->y3
                  );
                  pathptr.bezier++;
                break;

                case path_gapto:
                  ftracef (NULL, 0, " gapto (%d, %d)",
                      pathptr.gapto->x, pathptr.gapto->y);
                  pathptr.gapto++;
                break;

                case path_lineto:
                  ftracef (NULL, 0, " lineto (%d, %d)",
                      pathptr.lineto->x, pathptr.lineto->y);
                  pathptr.lineto++;
                break;
              }
            }
            while (tag != path_term);
            ftracef (NULL, 0, "\n");
          }
        break;

        case draw_OBJSPRITE:
          ftracef (NULL, 0, "  Sprite (size %d)\n", obj_size);
        break;

        case draw_OBJGROUP:
          ftracef (NULL, 0, "  Group (size %d):\n", obj_size);
          if
          ( trace_objects
            ( (obj_ptr.bytep = ptr.bytep + sizeof (draw_groustr), obj_ptr),
              ptr.groupp->size - sizeof (draw_groustr),
              indent + 1
            ) != NULL
          )
            return (void *) 1;

        case draw_OBJTAGG:
          ftracef (NULL, 0, "  Tagged object (size %d)\n", obj_size);
        break;

        case draw_OBJTEXTAREA:
          ftracef (NULL, 0, "  Text area (size %d)\n", obj_size);
        break;

        case draw_OBJTEXTCOL:
          ftracef (NULL, 0, "  Text column (size %d)\n", obj_size);
        break;

        case draw_OBJTRFMTEXT:
          ftracef (NULL, 0, "  Transformed text line (size %d): \"%s\"\n",
              obj_size, ptr.trfmtextp->text);
        break;

        case draw_OBJTRFMSPRITE:
          ftracef (NULL, 0, "  Transformed sprite (size %d)\n", obj_size);
        break;

        case draw_OBJJPEG:
          ftracef (NULL, 0, "  JPEG (size %d)\n", obj_size);
        break;
      }
    }

    return NULL;
  }
#endif
#if TRACE
/*-------------------------------------------------------------------*/
void draw_trace_db (diagrec *diag)

{ draw_objptr obj_ptr;

  ftracef
  ( NULL, 0,
    "Current diagram: 0x%X; next: 0x%X; prev: 0x%X; misc at 0x%X\n",
    diag, diag->nextdiag, diag->prevdiag, diag->misc
  );

  ftracef
  ( NULL, 0,
    "Misc\n"
    " ( Solid: %d ~ %d; ghost: %d ~ %d; stack lt: %d; buff lt: %d\n",
    diag->misc->solidstart, diag->misc->solidlimit,
    diag->misc->ghoststart, diag->misc->ghostlimit,
    diag->misc->stacklimit, diag->misc->bufferlimit
  );

  ftracef
  ( NULL, 0,
    "  Main state: (draw_state) %d; substate: (draw_state) %d; "
        "options:%s%s%s\n",
    diag->misc->mainstate, diag->misc->substate,
    diag->misc->options.curved? " curved": "",
    diag->misc->options.closed? " closed": "",
    diag->misc->options.modified? " modified": ""
  );

  ftracef
  ( NULL, 0,
    "  paperstate: *0x%X; wantsnulls: 0x%X; path: *0x%X; font: *0x%X\n"
    "  pta_off: %d; ptb_off: %d; "
      "ptw_off: %d; ptx_off: %d; pty_off: %d; ptz_off: %d\n"
    "  ptzzz: (%d, %d); ellicentre: (%d, %d)\n"
    "  filename: %s; vuuecnt: %d\n",
    &diag->misc->paperstate, diag->misc->wantsnulls, &diag->misc->path,
      &diag->misc->font,
    diag->misc->pta_off, diag->misc->ptb_off,
      diag->misc->ptw_off, diag->misc->ptx_off, diag->misc->pty_off,
      diag->misc->ptz_off,
    diag->misc->ptzzz.x, diag->misc->ptzzz.y,
      diag->misc->ellicentre.x, diag->misc->ellicentre.y,
    diag->misc->filename, diag->misc->vuuecnt
  );

  ftracef
  ( NULL, 0,
    "  pathedit_cb:\n"
    "  ( over: (region) %d; obj_off: %d; sub_off: %d\n"
    "    fele_off: %d; pele_off: %d; cele_off: %d\n"
    "    cor_off: %d; corA_off: %d; corB_off: %d; corC_off: %d; "
      "corD_off: %d\n"
    "    changed: %c\n"
    "  )\n",
    diag->misc->pathedit_cb.over, diag->misc->pathedit_cb.obj_off,
      diag->misc->pathedit_cb.sub_off,
    diag->misc->pathedit_cb.fele_off, diag->misc->pathedit_cb.pele_off,
      diag->misc->pathedit_cb.cele_off,
    diag->misc->pathedit_cb.cor_off,
      diag->misc->pathedit_cb.corA_off, diag->misc->pathedit_cb.corB_off,
      diag->misc->pathedit_cb.corC_off, diag->misc->pathedit_cb.corD_off,
    diag->misc->pathedit_cb.changed? 'T': 'F'
  );

  ftracef (NULL, 0, "  save: *0x%X\n)\n", &diag->misc->save);

  ftracef
  ( NULL, 0,
    "paper: 0x%X; view 0x%X; undo: 0x%X\n",
    diag->paper, diag->view, diag->undo
  );

  ftracef (NULL, 0, "Solid objects:\n");
  if (trace_objects
      ((obj_ptr.bytep = diag->paper + diag->misc->solidstart + 0, obj_ptr),
      (size_t) (diag->misc->solidlimit - diag->misc->solidstart - 0), 0) !=
      NULL)
    return;

  ftracef (NULL, 0, "Ghost objects:\n");
  if
  ( trace_objects
    ( (obj_ptr.bytep = diag->paper + diag->misc->ghoststart + 0, obj_ptr),
      (size_t) (diag->misc->ghostlimit - diag->misc->ghoststart - 0),
      0
    ) != NULL
  )
    return;

  ftracef
  ( NULL, 0,
    "Stack contains %d object%s; top: %d; bottom: %d\n",
    diag->misc->bufferlimit - diag->misc->stacklimit,
    diag->misc->bufferlimit - diag->misc->stacklimit == 1? "": "s",
    *(int *) (diag->paper + diag->misc->stacklimit),
    *(int *) (diag->paper + diag->misc->bufferlimit)
  );

  ftracef (NULL, 0, "Selection is at 0x%X, containing:\n  {",
      draw_selection);
  { int i;
    for (i = 0; i < draw_selection->indx; i++)
      ftracef (NULL, 0, i == 0? "%d": ", %d", draw_selection->array [i]);
  }
  ftracef (NULL, 0, "}\n");
}
#endif

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

void draw_sort (int *a, int *b)

{ ftracef0 ("draw_sort\n");
  if (*a > *b) {int tmp = *b; *b = *a; *a = tmp;}
}

void draw_reset_gchar (void)

{ ftracef0 ("draw_reset_gchar\n");
  if (draw_currentmode.gcharaltered)
  { draw_displ_setVDU5charsize (draw_currentmode.gcharsizex,
        draw_currentmode.gcharsizey, draw_currentmode.gcharspacex,
        draw_currentmode.gcharspacey);
    draw_currentmode.gcharaltered = 0;
  }
}

/*Make origin, typically from a wimp_openstr*/
static void make_origin (draw_objcoord *to, wimp_box *box, int *scroll)

{ ftracef0 ("make_origin\n");
  to->x = box->x0 - scroll [0];
  to->y = box->y1 - scroll [1];
}

/*Set the window extent of view, diag; based on paper size & zoom factor*/
void draw_setextent (viewrec *vuue)

{ diagrec *diag         = vuue->diag;
  wimp_w w              = vuue->w;
  draw_bboxtyp newlimit = diag->misc->paperstate.viewlimit;
  double scale          = vuue->zoomfactor;
  wimp_redrawstr blk;

  ftracef0 ("draw_setextent\n");
  /*Adjust the window extent*/
  blk.w = w;
  draw_box_scale ((draw_bboxtyp *) &blk.box, &newlimit,
      draw_draw_to_osD (scale));

  /*Window manager gets confused if the window isn't an exact number of*/
  /*pixels, this may mean that expand to full size (which works) then*/
  /*shrink may fail. The window manager thinks the window isn't full*/
  /*size and refuses to shrink it.*/
  blk.box.x0 &= ~(draw_currentmode.pixx - 1);
  blk.box.y0 &= ~(draw_currentmode.pixy - 1);
  blk.box.x1 = (blk.box.x1 + (draw_currentmode.pixx - 1)) &
      ~(draw_currentmode.pixx - 1);
  blk.box.y1 = (blk.box.y1 + (draw_currentmode.pixy - 1)) &
      ~(draw_currentmode.pixy - 1);

  wimpt_noerr (wimp_set_extent (&blk));
  ftracef4 ("extent set to (%d, %d, %d, %d)\n",
      blk.box.x0, blk.box.y0, blk.box.x1, blk.box.y1);

  /*Mark the whole window as invalid*/
  blk.w = w;
  blk.box.x0 = blk.box.y0 = -0x1fffffff;
  blk.box.x1 = blk.box.y1 =  0x1fffffff;
  wimp_force_redraw (&blk);
}

/*Change pointer to reflect state*/
static void draw_setpointer (diagrec *diag)

{ ftracef0 ("draw_setpointer\n");
  if (pointer_diag)
    switch (diag->misc->mainstate)
    { case state_path:
      case state_elli:
      case state_rect:
600 601 602 603 604 605 606 607
      { sprite_id id;
        sprite_info info;

        /* read sprite information for the crosshairs */
        id.tag    = sprite_id_name;
        id.s.name = "crosshairs";
        wimpt_noerr (sprite_readsize (resspr_area (), &id, &info));
        wimpt_noerr (pointer_set_shape (resspr_area (), &id, (info.width-1)/2, (info.height-1)/2));
Neil Turton's avatar
Neil Turton committed
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 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
      }
      break;

      default:
        pointer_reset_shape ();  /*'normal' shape*/
      break;
    }
}

static void draw_set_icon_state (wimp_w w, wimp_i i, BOOL selected)

{ ftracef0 ("draw_set_icon_state\n");
  wimpt_noerr
  ( wimp_set_icon_state
    ( w,
      i,
      selected? wimp_ISELECTED: (wimp_iconflags) 0,
      wimp_ISELECTED
    )
  );
}

/*Show diag state in toolbox pane of vuue*/
/*show = TRUE,  highlight icons*/
/*show = FALSE, unhighlight icons*/
static void draw_toolbox_showstate (diagrec *diag, viewrec *vuue, int show)

{ wimp_w pane = vuue->pw;
  draw_state mainstate = diag->misc->mainstate;
  int curved = diag->misc->options.curved;
  int closed = diag->misc->options.closed;

  ftracef3 ("draw_toolbox_showstate: diag: 0x%X; vuue: 0x%X; show: %d\n",
      diag, vuue, show);
  switch (mainstate)
  { case state_path:
      draw_set_icon_state
      ( pane,
        curved?
          (closed? tbi_curv_c: tbi_curv_o):
          (closed? tbi_line_c: tbi_line_o),
        show
      );
    break;

    case state_rect:
      draw_set_icon_state (pane, tbi_rect, show);
    break;

    case state_elli:
      draw_set_icon_state (pane, tbi_elli, show);
    break;

    case state_text:
      draw_set_icon_state (pane, tbi_text, show);
    break;

    case state_sel:
      draw_set_icon_state (pane, tbi_select, show);
    break;
  }

  /*if (FALSE)
    draw_set_icon_state (pane, tbi_move, show);*/
}

/*Show toolbox for all views in a diagram*/
void draw_toolbox_showall (diagrec *diag, int show)

{ viewrec *vuue;

  ftracef2 ("draw_toolbox_showall: diag: 0x%X; show: %d\n", diag, show);
  for (vuue = diag->view; vuue != 0; vuue = vuue->nextview)
    draw_toolbox_showstate (diag, vuue, show);
}

/*Low level caret call*/
void draw_set_caret (wimp_w w, int x, int y, int height)

{ wimp_caretstr c;

  ftracef0 ("draw_set_caret\n");
  c.w      = w;
  c.i      = -1;
  c.x      = x;
  c.y      = y;
  c.height = height;
  c.index  = 0;
  wimpt_noerr (wimp_set_caret_pos (&c));
}

/*Get the input focus, with no caret*/
void draw_get_focus (void)

{ wimp_wstate   wstate;
  int           x = 0, y = 0;

  ftracef1 ("draw_get_focus: owner 0x%X\n", draw_enter_focus_owner.diag);
  if (draw_enter_focus_owner.hand != -1)
  { wimpt_noerr (wimp_get_wind_state (draw_enter_focus_owner.hand, &wstate));
    x = wstate.o.x;
    y = wstate.o.y;
  }

  draw_set_caret (draw_enter_focus_owner.hand, x, y, 0x02000000);
}
#if 0 /*not used*/
void draw_kill_caret (void)

{ ftracef0 ("draw_kill_caret\n");
  draw_set_caret (-1, -1, -1, 0x01000010);
}
#endif
void draw_action_abandon (diagrec *diag) /*flush any object, restore screen*/

{ ftracef1 ("draw_action_abandon: diag: 0x%X\n", diag);
  switch (diag->misc->substate)
  { case state_path:
    case state_rect:
    case state_elli:
    case state_edit:
    case state_text:
    return;

    case state_path_move:
    case state_path_point1:
    case state_path_point2:
    case state_path_point3:
    case state_rect_drag:
    case state_elli_drag:
      draw_displ_eor_skeleton (diag);       /*remove construction lines*/
      draw_obj_flush (diag);
    break;

    case state_text_caret:
    case state_text_char:
    { draw_bboxtyp bbox;
      BOOL found;
      int hdroff = * (int*) (diag->paper + diag->misc->stacklimit);
      draw_objptr hdrptr;

      hdrptr.bytep = diag->paper + hdroff;

      found = draw_obj_findTextBox (hdrptr, &bbox);

      draw_obj_flush (diag);
      draw_get_focus ();       /*Kills caret but keeps focus*/

      if (found)
        draw_displ_redrawarea (diag, &bbox);
      else  /*Only happens if font manager blows up*/
        draw_displ_forceredraw (diag);
    }
    break;

    case state_sel_select:  /*dragging to select objects*/
    case state_sel_adjust:  /*dragging to adjust selection*/
    case state_sel_shift_select:  /*dragging to select objects*/
    case state_sel_shift_adjust:  /*dragging to adjust selection*/
    case state_zoom:        /*dragging zoom*/
      draw_displ_eor_capturebox (diag);
    break;

    case state_sel_trans:  /*translating selection*/
      draw_displ_eor_transboxes (diag);
    break;

    case state_sel_scale:  /*stretching selection*/
      draw_displ_eor_scaleboxes (diag);
    break;

    case state_sel_rotate:  /*rotating selection*/
      draw_displ_eor_rotatboxes (diag);
    break;

    case state_edit_drag:  /*we drag anchor/control point(s) directly,*/
    case state_edit_drag1:
    case state_edit_drag2:
    break;               /*so nothing to unplot*/
  }

  diag->misc->substate = diag->misc->mainstate;

  if (draw_enter_null_owner.diag == diag) draw_enter_release_nulls (diag);
}

/*Change state.
   'setundo' indicates that a the changestate constitutes a single undo
   operation.
*/
void draw_action_changestate (diagrec *diag, draw_state newstate,
                             int curved, /*only used if*/
                             int closed, /*mainstate is state_path*/
                             BOOL setundo)

{ draw_state   oldstate = diag->misc->mainstate;
  diag_options oldopts  = diag->misc->options;

  ftracef3 ("draw_action_changestate: diag: 0x%X; newstate: %d; "
      "setundo: %s\n", diag, newstate, setundo? "true": "false");
  /*Do nothing if there is really no change*/
  if (oldstate == newstate && oldopts.curved == curved &&
      oldopts.closed == closed)
  { draw_menu_entry_option (newstate, curved, closed);
    return;
  }

  /*Start undo putting if asked*/
  if (setundo) draw_undo_separate_major_edits (diag);

  draw_toolbox_showall (diag, FALSE);

  if (oldstate == newstate)
  { /*Save the old state and options*/
    draw_undo_put (diag, draw_undo__changestate, (int) oldstate,
        *(int *) &oldopts);

    if (oldstate == state_path)
    { os_error *err;

      draw_displ_eor_cons2 (diag);

      err = draw_enter_state_changelinecurve (diag,
          curved? path_bezier: path_lineto);
      diag->misc->options.curved = curved;
      draw_displ_eor_cons2 (diag);

      if (wimpt_complain (err))
      { draw_undo_prevent_undo (diag);
        return;
Robert Sprowson's avatar
Robert Sprowson committed
838 839 840
      }
    }
  }
Neil Turton's avatar
Neil Turton committed
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 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 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
  else
  { ftracef0 ("Complete any path etc\n");
    draw_enter_complete (diag);

    ftracef0 ("Kill anything that won't complete\n");
    draw_action_abandon (diag);

    if (oldstate == state_sel && draw_selection->owner == diag)
        /*was just oldstate == state_sel. J R C 16th Feb 1994*/
      draw_select_release_selection (diag);

    if (oldstate == state_edit)
      draw_edit_release_edit (diag);

    /*Save the old state and options*/
    if (oldstate != -1) /*JRC 18 Dec '89*/
      draw_undo_put
      (diag, draw_undo__changestate, (int) oldstate, *(int *) &oldopts);

    diag->misc->mainstate = diag->misc->substate = newstate;

    if (newstate == state_path)
      diag->misc->options.curved = curved;

    if (diag && newstate == state_sel)
      draw_select_claim_selection (diag);     /*pinch it from anyone else*/

    if (newstate == state_edit)
      draw_edit_claim_edit (diag);

    draw_setpointer (diag);  /*change pointer to reflect state*/
  }

  if (newstate == state_path)
    diag->misc->options.closed = closed;

  draw_toolbox_showall (diag, TRUE);

  draw_menu_entry_option (newstate, curved, closed);
}

/*Code for handling null events, both real and simulated*/
/*If the mouse is within a pixel of the window edge, then scroll the window,
   but only if the buttons are down.*/
/*Returns TRUE if the mouse is scrolled back into the window*/
static BOOL draw_null_event_handler (wimp_mousestr mouse)

{ wimp_wstate r;
  diagrec *diag;
  viewrec *vuue;
  draw_objcoord org, mouseD;

  ftracef0 ("draw_null_event_handler\n");
  /*If there is a null_owner (just being paranoid) pass event to it if the
    pointer is in the window or if null owner is in path edit mode. This
    means points can be dragged outside the window, but all other operations
    freeze at window edge
  */
  if ((diag = draw_enter_null_owner.diag) != 0)
    if (pointer_diag == diag || diag->misc->mainstate == state_edit)
      if ((vuue = diag->misc->wantsnulls) != 0)
      { BOOL scrolled = FALSE;

        draw_displ_scalefactor = vuue->zoomfactor;
        wimp_get_wind_state (vuue->w, &r);

        /*Check for scrolling at window edge*/
        if
        (
#if 1 /*JRC*/
          ( diag->misc->mainstate == state_sel ||
            diag->misc->mainstate == state_edit ||
            diag->misc->substate == state_zoom ||
            diag->misc->substate == state_printerI ||
            diag->misc->substate == state_printerO
          ) &&
#endif
          (mouse.bbits & ~wimp_BMID) != 0
        )
        { int dx = 0, dy = 0, nearx, neary;
          BOOL xscroll = TRUE, yscroll = TRUE;

          /*Scroll window if we are near the edge of it*/
          nearx = (r.o.box.x1 - r.o.box.x0)/20;
          if ((dx = mouse.x - r.o.box.x1) >= -nearx)
            /*Near right of window*/
            r.o.x += 2*(nearx + dx);
          else if ((dx = mouse.x - r.o.box.x0) <= nearx)
            /*Near left of window*/
            r.o.x -= 2*(nearx - dx);
          else
            xscroll = FALSE;

          neary = (r.o.box.y1 - r.o.box.y0)/20;
          if ((dy = mouse.y - r.o.box.y1) >= -neary)
            /*Near top of window*/
            r.o.y += 2*(neary + dy);
          else if ((dy = mouse.y - r.o.box.y0) <= neary)
            /*Near bottom of window*/
            r.o.y -= 2*(neary - dy);
          else
            yscroll = FALSE;

          /*Apply scroll, move pointer and change mouse block*/
          if (xscroll || yscroll)
          {
#if 0
            char block [5];
#endif

            /*Adjust scroll and re-open window*/
            draw_open_wind (&r.o, vuue);
#if 0
            /*Alter mouse position on screen*/
            block [0] = 3;
            block [1] = mouse.x;
            block [2] = mouse.x>>8;
            block [3] = mouse.y;
            block [4] = mouse.y>>8;
            wimpt_noerr (os_word (0x15, &block));
#endif
            /*Make sure window data is up to date*/
            wimp_get_wind_state (vuue->w, &r);
            scrolled = TRUE;
          }
        }

        /*draw_make_clip (&r, &org, &clip);*/
        make_origin (&org, &r.o.box, &r.o.x);
        draw_point_scale (&mouseD, (draw_objcoord *) &mouse.x, &org);

        /*Snap mouse, except when dragging a zoom or printer box -- or a
          capture box. JRC 11 Oct 1990*/
        if (!(diag->misc->substate == state_zoom ||
            /*diag->misc->substate == state_printerI ||
            diag->misc->substate == state_printerO ||
                Snap when dragging limits JRC 25 June 1991*/
            diag->misc->substate == state_sel_select ||
            diag->misc->substate == state_sel_adjust ||
            diag->misc->substate == state_sel_shift_select ||
            diag->misc->substate == state_sel_shift_adjust))
          draw_grid_snap_if_locked (vuue, &mouseD);

        if (mouseD.x != diag->misc->ptzzz.x ||
            mouseD.y != diag->misc->ptzzz.y)
          draw_obj_move_construction (diag, &mouseD);

        return scrolled;
      }

  return FALSE;
}

#if 0 /*not used. JRC 11 Oct 1990*/
/*Centre the view on the given point, as far as possible without actually
moving the window. x and y are screen coordinates*/
static void centre_view (viewrec *vuue, wimp_wstate *blk, int x, int y)

{ ftracef0 ("centre_view\n");
  blk->o.x += x - (blk->o.box.x1 + blk->o.box.x0)/2;
  blk->o.y += y - (blk->o.box.y1 + blk->o.box.y0)/2;

  draw_open_wind (&blk->o, vuue);
}
#endif

/*Set the paper limits to lie at the given bottom left location*/
/*The flag indicates whether select or adjust was used*/
static void set_paper_limit (viewrec *vuue, draw_objcoord pt, BOOL select)

{ diagrec        *diag  = vuue->diag;
  paperstate_typ *paper = &diag->misc->paperstate;

  ftracef0 ("set_paper_limit\n");
  /*draw_displ_show_printmargins() no longer xors. J R C 1st Feb 1994*/
  /*Remove old print margins*/
  /*was draw_displ_show_printmargins (diag);*/

  /*Indicate that we are no longer using default values*/
  paper->options = (paperoptions_typ) (paper->options & ~Paper_Default);

  /*Snap the point to the grid*/
  draw_grid_snap_if_locked (vuue, &pt);

  if (select)
  { /*Click position is bottom left*/
    paper->setlimit.x1 += pt.x - paper->setlimit.x0;
    paper->setlimit.y1 += pt.y - paper->setlimit.y0;
    paper->setlimit.x0  = pt.x;
    paper->setlimit.y0  = pt.y;
  }
  else
  { /*Click position is top right*/
    paper->setlimit.x0 += pt.x - paper->setlimit.x1;
    paper->setlimit.y0 += pt.y - paper->setlimit.y1;
    paper->setlimit.x1  = pt.x;
    paper->setlimit.y1  = pt.y;
  }

  /*Display new print margins*/
  draw_displ_forceredraw (diag);
      /*was draw_displ_show_printmargins (diag);*/
}

/*Set printer limits from box*/
void draw_set_paper_limits (diagrec *diag, captu_str box)

{ paperstate_typ *paper = &diag->misc->paperstate;

  ftracef0 ("draw_set_paper_limits\n");
  /*Print margins no longer xorred. J R C 1st Feb 1994*/
  /*Remove old print margins*/
  /*was draw_displ_show_printmargins (diag);*/

  /*Indicate that we are no longer using default values*/
  paper->options = (paperoptions_typ) (paper->options & ~Paper_Default);

  /*Reorder coordinates of box*/
  draw_sort (&box.x0, &box.x1);
  draw_sort (&box.y0, &box.y1);

  if (diag->misc->mainstate == state_printerI)
  { /*Setting inner limits*/
    draw_bboxtyp pbox, vbox;
    draw_print_get_limits (diag, &pbox, &vbox);

    paper->setlimit.x0 = box.x0 - (vbox.x0 - pbox.x0);
    paper->setlimit.y0 = box.y0 - (vbox.y0 - pbox.y0);
    paper->setlimit.x1 = box.x1 + (vbox.x1 - pbox.x1);
    paper->setlimit.y1 = box.y1 + (vbox.y1 - pbox.y1);
  }
  else
  { /*Setting outer limits*/
    paper->setlimit = box;
  }

  /*Display new print margins*/
  draw_displ_forceredraw (diag);
      /*was draw_displ_show_printmargins (diag);*/
}

/*Start a capture. Used for select and for zoom drag.*/
/*Also abandons current action first, if asked nicely*/
void draw_start_capture (viewrec *vuue, draw_state state, draw_objcoord *pt,
                        BOOL abandon)

{ diagrec *diag = vuue->diag;

  ftracef0 ("draw_start_capture\n");
  if (abandon) draw_action_abandon (diag);

  /*Prepare for drag*/
  diag->misc->substate = state;
  draw_enter_claim (diag, vuue);

  /*Snap box, except for zoom and printer drag -- or a
          capture box. JRC 11 Oct 1990*/
  if (!(state == state_sel_select ||
        state == state_sel_adjust ||
        state == state_sel_shift_select ||
        state == state_sel_shift_adjust ||
        state == state_zoom /*||
        state == state_printerI ||
        state == state_printerO
            Snap when dragging limits JRC 25 June 1991*/))
    draw_grid_snap_if_locked (vuue, pt);

  /*Fiddle it by using the select capture box*/
  draw_capture_cb.x0 = draw_capture_cb.x1 = pt->x;
  draw_capture_cb.y0 = draw_capture_cb.y1 = pt->y;

  draw_displ_eor_capturebox (diag);
}

/*-----------------------------------------------------------------------*/
/*The main paper event handler*/
/*Subsidiary routines first*/

void draw_make_clip (wimp_redrawstr *r, draw_objcoord *orgO, draw_bboxtyp *clip)

{ draw_objcoord org;

  ftracef0 ("draw_make_clip\n");
  make_origin (&org, &r->box, &r->scx);
  orgO->x = draw_os_to_draw (org.x);
  orgO->y = draw_os_to_draw (org.y);

  draw_point_scale ((draw_objcoord *) &clip->x0, (draw_objcoord *) &r->g.x0, &org);
  draw_point_scale ((draw_objcoord *) &clip->x1, (draw_objcoord *) &r->g.x1, &org);
}

static void paper_redraw (viewrec *vuue)

{ diagrec *diag = vuue->diag;
  int more;
  wimp_redrawstr r;

  ftracef0 ("paper_redraw\n");

  r.w = vuue->w;
  wimpt_noerr (wimp_redraw_wind (&r, &more));
  draw_displ_scalefactor = vuue->zoomfactor;

  #if TRACE
    ftracef (__FILE__, __LINE__,
        "paper_redraw: r.box: (%d, %d, %d, %d); r.sc: (%d, %d); "
        "r.g: (%d, %d, %d, %d)\n",
        r.box.x0, r.box.y0, r.box.x1, r.box.y1, r.scx, r.scy,
        r.g.x0, r.g.y0, r.g.x1, r.g.y1);
  #endif

  while (more)
  { draw_bboxtyp clip;
    draw_objcoord org;
    draw_make_clip (&r, &org, &clip);

    if (diag->misc->paperstate.options & Paper_Show)
      draw_displ_do_printmargin (diag, &org);

    ftracef1 ("obj_off=%d\n", diag->misc->mainstate == state_edit?
        diag->misc->pathedit_cb.obj_off: 0);

    if (diag->misc->mainstate == state_edit &&
        diag->misc->pathedit_cb.obj_off >= 0)
    { draw_objptr stop;

      stop.bytep = diag->paper + diag->misc->pathedit_cb.obj_off;

    #if TRACE
      wimpt_noerr (draw_displ_do_objects (diag, diag->misc->solidstart,
          diag->misc->pathedit_cb.obj_off, &org, &clip));

      wimpt_noerr (draw_displ_do_objects (diag,
          diag->misc->pathedit_cb.obj_off + stop.objhdrp->size,
          diag->misc->solidlimit, &org, &clip));
    #else
      (void) draw_displ_do_objects (diag, diag->misc->solidstart,
          diag->misc->pathedit_cb.obj_off, &org, &clip);

      (void) draw_displ_do_objects (diag,
          diag->misc->pathedit_cb.obj_off + stop.objhdrp->size,
          diag->misc->solidlimit, &org, &clip);
    #endif
    }
    else
      /*repaint all objects in the database*/
    #if TRACE
      wimpt_noerr (draw_displ_do_objects (diag,
          diag->misc->solidstart, diag->misc->solidlimit, &org, &clip));
    #else
      (void) draw_displ_do_objects (diag,
          diag->misc->solidstart, diag->misc->solidlimit, &org, &clip);
    #endif

    /*Repaint construction lines if this diag claims nulls*/
    draw_displ_paint_skeleton (diag, &org);

    /*Paint selection boxes if the diagram is the selection owner*/
    draw_displ_paint_bboxes (diag, &org);

    /*Unusual in that it takes a vuue as different grids apply to each
      vuue*/
    if (vuue->flags.show)
      draw_displ_paint_grid (vuue, &org, clip);

    wimpt_noerr (wimp_get_rectangle (&r, &more));
  }
}

static void dispose_view (viewrec *vuue)

{ diagrec *diag = vuue->diag;
  ftracef2 ("dispose_view: diag: 0x%X; vuue: 0x%X\n", diag, vuue);

  #if 0 /*J R C 22nd Sep 1993*/
  /*Get rid of the diag pointer in draw_selection->owner, if any.*/
  if (diag == draw_selection->owner)
    draw_select_set (NULL);
  #endif

  /*Remove this vuue from the view list:
     IF not first record THEN point our predecessor at our successor
                         ELSE point diag->view at our successor
     IF not last record  THEN point our successor at our predecessor*/
  if (vuue->prevview)
    vuue->prevview->nextview = vuue->nextview;
  else
    diag->view = vuue->nextview;

  if (vuue->nextview) vuue->nextview->prevview = vuue->prevview;
  diag->misc->vuuecnt--;  /*view no longer in list, so dec viewcnt*/

  win_register_event_handler (vuue->w, NULL, NULL);
  win_register_event_handler (vuue->pw, NULL, NULL); /*JRC 13 September
      1991*/

  /*Delete window and its pane*/
  wimp_close_wind (vuue->pw);
  wimp_delete_wind (vuue->pw);
  wimp_close_wind (vuue->w);
  wimp_delete_wind (vuue->w);
  win_activedec ();

  /*Free the memory*/
  Free (vuue->title);
  Free (vuue);
}

void draw_paper_close (viewrec *vuue)

{ int singlevuue;
  diagrec *diag = vuue->diag;
  BOOL shifted = akbd_pollsh ();

  ftracef0 ("draw_paper_close\n");
  /*Kill any menus*/
  draw_menu_kill (vuue->w);

  ftracef3 ("** Close_Window_Request, diag 0x%X, vuue 0x%X, vuuecnt %d\n",
      diag, vuue, diag->misc->vuuecnt);

  singlevuue = diag->misc->vuuecnt == 1;

  /*FIX G-RO-9964 JRC 29 Oct '91 Don't close if SHIFT is down.*/
  { /*The magic 'open directory on close with right click' code*/
    wimp_mousestr m;

    wimpt_noerr (wimp_get_point_info (&m));
    ftracef1 ("mouse button state is 0x%X\n", m.bbits);
    if ((m.bbits & wimp_BRIGHT) != 0)
    { /*Can trample on filename now - need to strip off the leafname.*/
      char *dot = strrchr (diag->misc->filename, '.');

      if (dot != NULL)
      { char cli [15 + sizeof diag->misc->filename];

        *dot = 0;
        sprintf (cli, "%%Filer_OpenDir %s", diag->misc->filename);
        *dot = '.';  /*put back the '.' we trampled on*/

        wimpt_complain (os_cli (cli));
        /*don't close if shift down!*/
        if (shifted) return;
Robert Sprowson's avatar
Robert Sprowson committed
1284 1285 1286
      }
    }
  }
Neil Turton's avatar
Neil Turton committed
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299

  /*If only view on a modified diagram, give user a chance to save it*/
  if (singlevuue && diag->misc->options.modified)
  { char a [300], *name = diag->misc->filename;

    if (*name == 0 || strlen (name) > 255)
      sprintf (a, msgs_lookup ("DrawS1"));
    else
      sprintf (a, msgs_lookup ("DrawS2"), name);

    switch (dboxquery_close (a))
    { case dboxquery_close_SAVE:
        if (*name == 0) name = filename_whole;
1300
          saveas (FileType_Draw, name, 1024,
Neil Turton's avatar
Neil Turton committed
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
              draw_file_file_saveall, draw_file_ram_saveall,
              draw_file_printall, (void*)diag);
      break;

      case dboxquery_close_DISCARD:
        diag->misc->options.modified = 0; /*data not wanted*/
      break;

      default:
      break;    /*cancel, do nothing, it stays updated.*/
Robert Sprowson's avatar
Robert Sprowson committed
1311 1312
    }
  }
Neil Turton's avatar
Neil Turton committed
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331

  /*If singlevuue & modified, either reply was cancel OR yes & save failed
    so don't delete diagram. If singlevuue & !modified, (includes reply
    no), delete diagram. If !singlevuue, delete this view*/
  if (singlevuue && diag->misc->options.modified) return;

  /*If this window has the input focus, give it away*/
  if (draw_enter_focus_owner.hand == vuue->w) draw_enter_release_focus ();

  /*If this window owns nulls, either: release them (if this is the only
    vuue) or pass them to another vuue on this diagram*/
  if (draw_enter_null_owner.hand == vuue->w)
  { if (singlevuue)
      draw_enter_release_nulls (diag);
    else
    { if (vuue->prevview)
        draw_enter_claim_nulls (diag, vuue->prevview);
      else
        draw_enter_claim_nulls (diag, vuue->nextview);
Robert Sprowson's avatar
Robert Sprowson committed
1332 1333
    }
  }
Neil Turton's avatar
Neil Turton committed
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351

  /*Give away selection if this is the only vuue*/
  if (draw_select_owns (diag) && singlevuue)
    draw_select_set (NULL);

  /*SMC: Reset pointer if it's in this diag*/
  if (pointer_diag == diag)
  {
    pointer_diag = 0;
    pointer_reset_shape ();
  }

  if (singlevuue)
    draw_dispose_diag (diag);
  else
  { dispose_view (vuue); /*remove from view list, free memory*/
                                  /*closes and deletes the window*/
    draw_displ_redrawtitle (diag); /*Redraw other titles*/
Robert Sprowson's avatar
Robert Sprowson committed
1352 1353
  }
}
Neil Turton's avatar
Neil Turton committed
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

static void paper_but (wimp_mousestr *m, viewrec *vuue)

{ draw_objcoord org, pt;
  wimp_wstate r;
  os_error *err;
  BOOL shifted = akbd_pollsh ();
  BOOL control = akbd_pollctl ();
  diagrec *diag = vuue->diag;

  ftracef0 ("paper_but\n");
  ftracef4 ("draw_selection 0x%X, draw_selection->indx %d, "
      "draw_selection->limit %d, draw_selection->array [0] %d\n",
      draw_selection, draw_selection->indx,
      draw_selection->limit, draw_selection->array [0]);
  ftracef2 ("draw_selection->owner 0x%X, "
      "draw_selection->owner->paper 0x%X\n",
      draw_selection->owner, draw_selection->owner->paper);

  /*Make sure we have input focus*/
  draw_enter_claim_focus (diag, vuue);

  draw_displ_scalefactor = vuue->zoomfactor;
  wimp_get_wind_state (m->w, &r);
  make_origin (&org, &r.o.box, &r.o.x);
  pt.x = draw_scaledown (m->x - org.x);       /*mouse position relative to*/
  pt.y = draw_scaledown (m->y - org.y);       /*window (ie diagram) origin*/

  if ((err = draw_obj_drop_construction (diag)) != NULL)
    return;

  if (m->bbits & wimp_BLEFT)       /*0x004 Double 'select'*/
  { switch (diag->misc->mainstate)
    { case state_path:
      case state_text:
      case state_rect:
      case state_elli:
        err = draw_enter_doubleselect (diag);
      break;

      case state_sel:
        draw_select_doubleselect (diag, &pt);
      break;
    }
  }
  else if (m->bbits & wimp_BDRAGLEFT)     /*0x040 Long 'select'*/
  { if (control)
    { /*Start printer limits drag (outer box)*/
      /*We must avoid being in state_zoom while there is a path edit in
        progress, because while in state_zoom path being editing is not
        redrawn. JRC 23 Jan 1990*/
      if (diag->misc->mainstate == state_edit)
        draw_edit_select (diag);

      draw_start_capture (vuue, state_printerO, &pt, TRUE);
    }
    else
    { switch (diag->misc->mainstate)
      { case state_sel:
          draw_select_longselect (diag, vuue, &pt, shifted);
        break;
Robert Sprowson's avatar
Robert Sprowson committed
1415 1416 1417
      }
    }
  }
Neil Turton's avatar
Neil Turton committed
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
  else if (m->bbits & wimp_BCLICKLEFT)   /*0x400 Short 'select'*/
  { if (control)
      set_paper_limit (vuue, pt, TRUE);
    /*else if (shifted)
      centre_view (vuue, &r, m->x, m->y); Removed JRC 11 Oct 1990*/
    else /*Same for SELECT and shift-SELECT*/
    { switch (diag->misc->mainstate)
      { case state_path:
        case state_text:
        case state_rect:
        case state_elli:
          draw_grid_snap_if_locked (vuue, &pt);

          if (pt.x != diag->misc->ptzzz.x || pt.y != diag->misc->ptzzz.y)
            draw_obj_move_construction (diag, &pt);

          err = draw_enter_select (diag, vuue, &pt);
        break;

        case state_sel:
          err = draw_select_select (diag, &pt);
        break;

       case state_edit:
         draw_edit_select (diag);
       break;
Robert Sprowson's avatar
Robert Sprowson committed
1444 1445 1446
      }
    }
  }
Neil Turton's avatar
Neil Turton committed
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
  else if (m->bbits & wimp_BRIGHT)      /*0x001 Double 'adjust'*/
  { if (shifted)
    { draw_action_abandon (diag);
      draw_action_zoom_alter (vuue, -1);
    }
    else
    { switch (diag->misc->mainstate)
      { case state_edit:
          draw_edit_doubleadjust (diag, &pt);
        break;
Robert Sprowson's avatar
Robert Sprowson committed
1457 1458 1459
      }
    }
  }
Neil Turton's avatar
Neil Turton committed
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
  else if (m->bbits & wimp_BDRAGRIGHT)    /*0x010 Long 'adjust'*/
  { if (control)
    { /*Start printer limits drag (inner box)*/
      /*We must avoid being in state_zoom while there is a path edit in
        progress, because while in state_zoom path being editing is not
        redrawn. JRC 23 Jan 1990*/
      if (diag->misc->mainstate == state_edit)
        draw_edit_select (diag);

      draw_start_capture (vuue, state_printerI, &pt, TRUE);
    }
    else if (shifted)
    { switch (diag->misc->mainstate)
      { default:
          /*Start Zoom drag*/
          draw_start_capture (vuue, state_zoom, &pt, TRUE);
          draw_action_zoom_view (vuue);
        break;

        case state_edit:
          draw_edit_long_shift_adjust (diag, vuue, &pt);
        break;
Robert Sprowson's avatar
Robert Sprowson committed
1482 1483
      }
    }
Neil Turton's avatar
Neil Turton committed
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
    else /*not shifted*/
    { switch (diag->misc->mainstate)
      { case state_sel:
          draw_select_longadjust (diag, vuue, &pt, FALSE); /*TRUE not
              invoked from anywhere*/
        break;

        case state_edit:
          draw_edit_longadjust (diag, vuue, &pt);
        break;
Robert Sprowson's avatar
Robert Sprowson committed
1494 1495 1496
      }
    }
  }
Neil Turton's avatar
Neil Turton committed
1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
  else if (m->bbits & wimp_BCLICKRIGHT)  /*0x100 Short 'adjust'*/
  { if (control)
      set_paper_limit (vuue, pt, FALSE);
    else if (!shifted)
    { switch (diag->misc->mainstate)
      { case state_sel:
          err = draw_select_adjust (diag, &pt);
        break;

        case state_path:
        case state_rect:
        case state_elli:
        case state_text:
          err = draw_enter_adjust (diag, &pt);
        break;

        case state_edit:
          draw_edit_adjust (diag, &pt);
        break;
Robert Sprowson's avatar
Robert Sprowson committed
1516 1517
      }
    }
Neil Turton's avatar
Neil Turton committed
1518 1519 1520 1521 1522 1523
    else /*shifted*/
    { switch (diag->misc->mainstate)
      { case state_edit:
          draw_edit_adjust (diag, &pt); /*Do the same for shift-adjust as
              for adjust in edit mode. JRC 6 Feb 1990*/
        break;
Robert Sprowson's avatar
Robert Sprowson committed
1524 1525 1526
      }
    }
  }
Neil Turton's avatar
Neil Turton committed
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561

  diag->misc->ptzzz = pt;

  ftracef2
  ( "paper_but: ptzzz = pt (%d, %d)\n",
    diag->misc->ptzzz.x, diag->misc->ptzzz.y
  );
}

/*Check and copy the file name*/
static BOOL check_filename (char *name, char *to, int maxlen)

{ ftracef0 ("check_filename\n");

  /*Fix - if name is very long, give up*/
  if (strlen (name) > maxlen - 1)
  { Error (0, "FileP1");
    return FALSE;
  }
  strcpy (to, name);
  return TRUE;
}

/*Load operations*/
/*Creates the diagram if it is NULL. vuue should also be NULL if so.*/
/*If insert is TRUE, then data loads are allowed only for draw files*/
static os_error *load_file (diagrec *diag, viewrec *vuue, draw_objcoord *pt,
                           BOOL insert)

{ int estsize, filetype = xferrecv_checkimport (&estsize);

  ftracef0 ("load_file\n");
  if (diag == NULL) vuue = NULL;

  switch (filetype)
1562
  { case FileType_DataExchangeFormat:
Neil Turton's avatar
Neil Turton committed
1563 1564 1565
      /*if (!draw_dxf_setOptions ()) break; can't do this yet - breaks the
        data transfer protocol*/
      /*fall into general case*/
1566
    case FileType_Draw:
Neil Turton's avatar
Neil Turton committed
1567
    #if ALLOW_DFILES
1568
      case FileType_EarlyDrawingProgram:
Neil Turton's avatar
Neil Turton committed
1569
    #endif
1570 1571 1572
    case FileType_Sprite:
    case FileType_Text:
    case FileType_JPEG:
Neil Turton's avatar
Neil Turton committed
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
      draw_file_loadfile (diag, vuue, "", filetype, estsize, via_RAM, pt);
    break;

    default:
      #if REJECTUNKNOWNFILETYPES
        return makmsg ("DrawCL");
      #endif
    break;

    case -1:                                    /*Not an import*/
    { char *name, filename [FILENAMEMAX];

      filetype = xferrecv_checkinsert (&name);

      switch (filetype)
1588
      { case FileType_Sprite:
Neil Turton's avatar
Neil Turton committed
1589
        #if ALLOW_DFILES
1590
          case FileType_EarlyDrawingProgram:
Neil Turton's avatar
Neil Turton committed
1591
        #endif
1592 1593 1594
        case FileType_Text:
        case FileType_DataExchangeFormat:
        case FileType_JPEG:
Neil Turton's avatar
Neil Turton committed
1595 1596
          if (insert) break;

1597
        case FileType_Draw:
Neil Turton's avatar
Neil Turton committed
1598
          if (check_filename (name, filename, FILENAMEMAX))
1599
          { /*if (filetype == FileType_DataExchangeFormat && !draw_dxf_setOptions ())
Neil Turton's avatar
Neil Turton committed
1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
              break; Can't do this yet - breaks the data transfer
              protocol.*/

            draw_file_loadfile (diag, vuue, filename, 0, 0, via_FILE, pt);

            ftracef0 ("sending Message_DataSaveAck\n");
            xferrecv_insertfileok (); /*Must respond before putting up
                dbox (in draw_dxf_setOptions ()).*/
          }
        break;

        default:
          #if REJECTUNKNOWNFILETYPES
            if (!insert) return makmsg ("DrawCL");
          #endif
        break;
      }
      break;
Robert Sprowson's avatar
Robert Sprowson committed
1618 1619
    }
  }
Neil Turton's avatar
Neil Turton committed
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658

  return NULL;
}

/*Event handler for nulls, redraws, key clicks etc on the main window*/
/*(the paper) of a view*/
static void draw_paper__wimp_event_handler (wimp_eventstr *e, void *handle)

{ os_error *err = 0;
  viewrec  *vuue = (viewrec *) handle;
  diagrec  *diag = vuue->diag;

  ftracef1 ("draw_paper__wimp_event_handler: diag 0x%X\n", diag);
  ftracef4 ("draw_selection 0x%X, draw_selection->indx %d, "
      "draw_selection->limit %d, draw_selection->array [0] %d\n",
      draw_selection, draw_selection->indx,
      draw_selection->limit, draw_selection->array [0]);
  ftracef2 ("draw_selection->owner 0x%X, "
      "draw_selection->owner->paper 0x%X\n",
      draw_selection->owner, draw_selection->owner->paper);
  #if 0 /*RISC_OS_PLUS*/
  if (!help_process (e))
  #endif
    switch (e->e)
    { case wimp_ENULL:
      { wimp_mousestr mouse;

        ftracef0 ("** Null_Reason_Code **\n");
        wimp_get_point_info (&mouse);
        draw_null_event_handler (mouse); /*Get the construction up to date*/

        /*If the 'select' & 'adjust' buttons are up, give each null*/
        /*claimer the chance to drop any stretch/translate/rotate boxes,*/
        /*area grab rectangles or edited points.*/
        if ((mouse.bbits & 0x005)==0)
        { diagrec *diag;
          for (diag = draw_startdiagchain; diag != 0; diag = diag->nextdiag)
            if (diag->misc->wantsnulls)
              draw_obj_drop_construction (diag);
Robert Sprowson's avatar
Robert Sprowson committed
1659 1660
        }
      }
Neil Turton's avatar
Neil Turton committed
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 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 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931
      break;

      case wimp_EREDRAW:
        ftracef0 ("** Redraw_Window_Request **\n");
        paper_redraw (vuue);
      break;

      case wimp_EOPEN:
        ftracef0 ("** Open_Window_Request **\n");
        draw_open_wind (&e->data.o, vuue);   /*open paper and toolbox*/
      break;

      case wimp_ECLOSE:
        ftracef0 ("** Close_Window_Request **\n");
        draw_paper_close (vuue);
      break;

      case wimp_EPTRLEAVE:
      { wimp_mousestr mouse;

        ftracef0 ("** Pointer_Leaving_Window **\n");
        wimp_get_point_info (&mouse);

        /*Simulate null event on the window (for better visual effect)*/
        if (pointer_diag)
          if (draw_null_event_handler (mouse))
            break;

        /*Reset the pointer to normal shape*/
        pointer_diag = 0;
        pointer_reset_shape ();
      }
      break;

      case wimp_EPTRENTER:
        ftracef0 ("** Pointer_Entering_Window **\n");

        pointer_diag = diag;
        draw_setpointer (diag);     /*change pointer to reflect state*/

        /*if dragging a line/box from view-to-view change current_handle*/
        if (diag->misc->mainstate != diag->misc->substate)
          if (diag->misc->wantsnulls)
            draw_enter_claim (diag, vuue);

        /*Force a null event to get the construction up to date*/
        { wimp_mousestr mouse;
          wimp_get_point_info (&mouse);
          draw_null_event_handler (mouse);
        }
      break;

      case wimp_EBUT:
        ftracef0 ("** Mouse_Button **\n");
        paper_but (&e->data.but.m, vuue);
      break;

      case wimp_EUSERDRAG:
        ftracef0 ("** User_DragBox **\n");
      break;

      case wimp_EKEY:
        ftracef4 ("** Key_Pressed, window=%d, caret at (%d,%d), "
            "key code=%d **\n", e->data.key.c.w, e->data.key.c.x,
            e->data.key.c.y,e->data.key.chcode);

        draw_menu_processkeys (diag, vuue, e->data.key.chcode);
      break;

      case wimp_EMENU:
        ftracef0 ("** Menu_Select ** This should not happen\n");
      break;

      case wimp_ESCROLL:
        ftracef0 ("** Scroll_Request **\n");
      break;

      case wimp_ESEND:
      case wimp_ESENDWANTACK:
        ftracef0 (e->e == wimp_ESEND? "** User_Message **\n":
            "** User_Message_Acknowledge **\n");
        switch (e->data.msg.hdr.action)
        { case wimp_MHELPREQUEST:
            ftracef0 ("   Help Request\n");
            draw_help_helpmessage (e, diag);
          break;

          case wimp_MDATASAVE:   /*import*/
          case wimp_MDATALOAD:   /*insert*/
          case wimp_MDATAOPEN:   /*insert*/
            { wimp_mousestr mouse;
              wimp_wstate   r;
              draw_objcoord org, mouseD;

              ftracef0
              ( e->data.msg.hdr.action == wimp_MDATASAVE? "   Data Save\n":
                e->data.msg.hdr.action == wimp_MDATALOAD? "   Data Load\n":
                                                          "   Data Open\n"
              );
              /*Find the mouse location*/
              wimp_get_point_info (&mouse);
              wimp_get_wind_state (vuue->w, &r);
              draw_displ_scalefactor = vuue->zoomfactor;

              make_origin (&org, &r.o.box, &r.o.x);
              draw_point_scale (&mouseD, (draw_objcoord *)&mouse.x, &org);

              /*Gravitate to grid position*/
              draw_grid_snap_if_locked (vuue, &mouseD);

              /*Do the load*/
              err = load_file (diag, vuue, &mouseD, FALSE);
            }
          break;

          default:
            ftracef1 ("  Message Type %d\n", e->data.msg.hdr.action);
          break;
        }
      break;
    }

  /*Reset VDU5 character size*/
  draw_reset_gchar ();

  wimpt_complain (err);
}

/*Event handler for nulls,redraws,key clicks etc on the pane window*/
/*(the toolbox) of a view*/
static void draw_toolbox__wimp_event_handler (wimp_eventstr *e, void *handle)

{ os_error *err  = 0;
  viewrec  *vuue = (viewrec *)handle;
  diagrec  *diag = vuue->diag;   /*the diagram that owns the view*/
                                 /*that owns the pane window on*/
                                 /*which the event occured*/

  ftracef0 ("draw_toolbox__wimp_event_handler\n");
  #if 0 /*RISC_OS_PLUS*/
  if (!help_process (e))
  #endif
    switch (e->e)
    { case wimp_EOPEN:
        ftracef0 ("** Open_Window_Request on pane (toolbox) window **\n");
        wimp_open_wind (&e->data.o);
      break;

      case wimp_EBUT:
        ftracef2 ("** Mouse_Click on pane (toolbox) window, icon %d, "
          "buttons &%x **\n",
          e->data.but.m.i,e->data.but.m.bbits);

        if (e->data.but.m.bbits & 0x5)   /*'select' or 'adjust'*/
        { BOOL setundo, fromedit;

          ftracef0 ("Grab input focus\n");
          draw_enter_claim_focus (diag, vuue);

          ftracef0 ("Changing state from the toolbox registers a change "
              "for undo, except when we are changing between different "
              "path styles in the middle of entering a path object. Or "
              "when changing from path edit\n");
          fromedit = diag->misc->mainstate == state_edit;
          setundo = ! ((diag->misc->mainstate == state_path
                      && diag->misc->substate != diag->misc->mainstate)
                      || fromedit);

          switch (e->data.but.m.i)
          { case tbi_line_o: ftracef0 ("Line, open\n");
              draw_action_changestate (diag, state_path, 0, 0, setundo);
            break;

            case tbi_line_c: ftracef0 ("Line, auto-closed\n");
              draw_action_changestate (diag, state_path, 0, 1, setundo);
            break;

            case tbi_curv_o: ftracef0 ("Curve, open\n");
              draw_action_changestate (diag, state_path, 1, 0, setundo);
            break;

            case tbi_curv_c: ftracef0 ("Curve, auto-closed\n");
              draw_action_changestate (diag, state_path, 1, 1, setundo);
            break;

            case tbi_move: ftracef0 ("Move (new subpath)\n");
              err = draw_enter_movepending (diag);
            break;

            case tbi_rect: ftracef0 ("Rectangle\n");
              draw_action_changestate (diag, state_rect, 0, 0, !fromedit);
            break;

            case tbi_elli: ftracef0 ("Ellipse\n");
              draw_action_changestate (diag, state_elli, 0, 0, !fromedit);
            break;

            case tbi_text: ftracef0 ("Text\n");
              draw_action_changestate (diag, state_text, 0, 0, !fromedit);
            break;

            case tbi_select: ftracef0 ("Select mode\n");
              /*draw_action_abandon (diag);*/ /*Abandon any line entry -
                  removed JRC 21 June 1991*/
              draw_action_changestate (diag, state_sel, 0, 0, !fromedit);
            break;
          }
        }
      break;

      case wimp_EKEY:
        ftracef1 ("** Key_Pressed on tool window, key code=%d**\n",
          e->data.key.chcode);
        wimp_processkey (e->data.key.chcode);   /*toolbox can't handle it*/
      break;                                    /*so give back to the wimp*/

      case wimp_ESEND:
      case wimp_ESENDWANTACK:
        switch (e->data.msg.hdr.action)
        { case wimp_MHELPREQUEST:
            draw_help_helptoolbox (e);
          break;
        }
      break;
    }

  /*Reset VDU5 character size*/
  draw_reset_gchar ();

  wimpt_complain (err);
}

/*Register the event handler for a window*/
static void draw_register_events (viewrec *vuue)

{ ftracef0 ("draw_register_events\n");
  win_register_event_handler (vuue->w, &draw_paper__wimp_event_handler,
      vuue);
  event_attachmenumaker (vuue->w, draw_menu_filler, draw_menu_proc, vuue);
  if (vuue->pw != -1) win_register_event_handler (vuue->pw,
      &draw_toolbox__wimp_event_handler, vuue);
}

/*--------------------------- Quit handling -----------------------------*/

static int draw_countupdated (void)

{ diagrec *diag;
  int count = 0;

  ftracef0 ("draw_countupdated\n");
  for (diag = draw_startdiagchain; diag; diag = diag->nextdiag)
    if (diag->misc->options.modified) count++;
  return count;
}

/*Check we are allowed to quit*/
static BOOL draw_mayquit (void)

{ int count = draw_countupdated ();

  ftracef0 ("draw_mayquit\n");
  if (count == 0)
    return TRUE;
  else
  { char a [80];
    if (count == 1)
      strcpy (a, msgs_lookup ("DrawQ1"));
    else
      sprintf (a, msgs_lookup ("DrawQ2"), count);
    return dboxquery_quit (a) == dboxquery_quit_DISCARD;
Robert Sprowson's avatar
Robert Sprowson committed
1932 1933
  }
}
Neil Turton's avatar
Neil Turton committed
1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976