/* AmiResizeBrush.c - Low Level Objects for Amiga                      */
/* Copyright (c) 1990 by J.K. Lindsey                                  */
/* Additions to XLISP-STAT 2.1 Copyright (c) 1990, by Luke Tierney     */
/* Additions to Xlisp 2.1, Copyright (c) 1989 by David Michael Betz    */
/* You may give out copies of this software; for conditions see the    */
/* file COPYING included with this distribution.                       */

#define _IVIEWWINDOW_
#include <proto/intuition.h>
#include <proto/exec.h>
#include <proto/graphics.h>
#include <graphics/gfxmacros.h>
#include "autil2.h"
#include "xlisp.h"
#include "osdef.h"
#include "xlproto.h"
#include "xlsproto.h"
#include "iviewproto.h"
#include "amivar.h"

#define WW 350
#define WH 300

static struct {
   int left,width;
   int top,height;}
oldBrush,brush;
static short xy[18]={2,-2,78,-2,82,2,82,18,78,22,2,22,-2,18,-2,2,2,-2};
static struct Border bd1={0,0,BLACK,WHITE,JAM1,9,xy,0},
                     bd2={0,0,BLACK,WHITE,JAM1,9,xy,0};
static struct IntuiText itext1={BLACK,WHITE,JAM1, 32,6,0,"OK",0},
                        itext2={BLACK,WHITE,JAM1, 16,6,0,"Cancel",0},
                        rmess2={BLACK,WHITE,JAM1, 59,25,0,"click in this window and drag",0},
                        rmess1={BLACK,WHITE,JAM1,115,15,0,"To resize brush",&rmess2};
static struct Gadget CancelButton={
   0            ,230,WH-30,80,20,GADGHCOMP,RELVERIFY,BOOLGADGET,(APTR)&bd2,0,&itext2,0,0,1,0},
                     OKButton={
   &CancelButton, 40,WH-30,80,20,GADGHCOMP,RELVERIFY,BOOLGADGET,(APTR)&bd1,0,&itext1,0,0,0,0};
static struct Window *wind;
static struct RastPort *rp;

static void setup_window(void){
   wind=MakeWind(150,50,WW,WH,WHITE,BLACK,MOUSEBUTTONS|GADGETUP,WINDOWDRAG
   |SMART_REFRESH|ACTIVATE|WINDOWDEPTH|NOCAREREFRESH|SIZEBBOTTOM,
   &OKButton,0,"Brush Resizer",screen,0,0,0,0,0,screentype);
   if(!wind)xlfail("window creation failed");
   rp=wind->RPort;
   PrintIText(rp,&rmess1,0,0);
   SetAPen(rp,BLACK);
   SetDrMd(rp,COMPLEMENT);
   SetDrPt(wind->RPort,0x5555);
   RefreshGList(&OKButton,wind,0,-1);}

static void DrawBrush(void){
   static short xy[10]={0,0,0,0,0,0,0,0,0,0};
   static struct Border bd={0,0,BLACK,WHITE,COMPLEMENT,5,xy,0};
   xy[2]=xy[4]=brush.width;
   xy[5]=xy[7]=brush.height;
   bd.TopEdge=brush.top;
   bd.LeftEdge=brush.left;
   DrawBorder(rp,&bd,0,0);}

static int event_loop(void){
   int done=0,x,y,ox,oy;
   unsigned long class;
   unsigned short code;
   struct Gadget *address;
   struct IntuiMessage *message;
   int cancelled;
   cancelled=1;
   do {
      Wait(1<<wind->UserPort->mp_SigBit);
      if(message=(struct IntuiMessage *)GetMsg(wind->UserPort)){
         class=message->Class;
         code=message->Code;
         address=(struct Gadget *)message->IAddress;
         ReplyMsg((struct Message *)message);
         ox=wind->MouseX;
         oy=wind->MouseY;
         switch(class) {
            case MOUSEBUTTONS: {
               if(code==SELECTDOWN&&ox>3+brush.width&&ox<347&&oy>35+brush.height&&oy<WH-30){
                  if(cancelled)cancelled=0;
                  else DrawBrush();           /* if old brush, erase */
                  brush.left=ox-brush.width;
                  brush.top=oy-brush.height;
                  DrawBrush();
                  for(;;){
                     if(message=(struct IntuiMessage *)GetMsg(wind->UserPort)){
                        class=message->Class;
                        code=message->Code;
                        ReplyMsg((struct Message *)message);
                        if(class==MOUSEBUTTONS&&code==SELECTUP)break;}
                     x=wind->MouseX;
                     y=wind->MouseY;
                     if(x<=0||x>=wind->Width||y<=0||y>=wind->Height)break;
                     if(x>3&&x<347&&y>35&&y<WH-30){
                        DrawBrush();                /* to erase */
                        brush.width+=x-ox;
                        brush.height+=y-oy;
                        DrawBrush();                /* to redraw */
                        ox=x;
                        oy=y;}}}
               break;}
            case GADGETUP: {
               if(address->GadgetID){
                  brush=oldBrush;
                  cancelled=1;}
               done=TRUE;
               break;}}}}
   while(!done);
   return(cancelled);}

IViewGetNewBrushSize(IVIEW_WINDOW w,int *new_width,int *new_height){
   int left,top,width,height,cancelled;
   IViewGetBrush(w,&left,&top,&width,&height);
   oldBrush.left=left;
   oldBrush.top=top;
   oldBrush.width=width;
   oldBrush.height=height;
   brush.width=width;
   brush.height=height;
   setup_window();
   cancelled=event_loop();
   CloseWindow(wind);
   if(new_width)*new_width=brush.width;
   if(new_height)*new_height=brush.height;
   return(!cancelled);}
