home *** CD-ROM | disk | FTP | other *** search
- #include "ToolWindow.h"
- #include "BuilderWindow.h"
- #include "BuilderMethods.h"
- #ifndef __GNUC__
- #include <clib/exec_protos.h>
- #include <clib/intuition_protos.h>
- #include <clib/graphics_protos.h>
- #endif
- #ifdef __GNUC__
- #include <proto/exec.h>
- #include <proto/intuition.h>
- #include <proto/graphics.h>
- #endif
- #ifdef __SASC
- #include <proto/exec.h>
- #include <proto/intuition.h>
- #include <proto/graphics.h>
- #endif
- #include "amigamem.h"
- #include "project.h"
- #include "funclists.h"
-
- #define BOB_WAITING 0
- #define BOB_DRAGGING 1
- #define KEYUP 0x80L
-
- #define SELF &self->pw
-
- extern struct MinList DeferredResponses;
-
- void ToolWindow_BeSelfish( ToolWindow *self );
- void ToolWindow_DragBack( ToolWindow *self, short x, short y );
- void ToolWindow_StopDragging( ToolWindow *self );
-
-
- void ToolWindow_SetBobLocation( ToolWindow *self, PIXELS x, PIXELS y )
- {
-
- /* use Screen relative coordinates. */
-
- self->ibob.VSprite.X = x + self->offset.x;
- self->ibob.VSprite.Y = y + self->offset.y;
-
- }
-
- void ToolWindow_StartDragging( ToolWindow *self, PIXELS x, PIXELS y )
- {
- Point location;
-
- if( self->state == BOB_WAITING )
- {
- /* Install bob. */
- if( InitImageBob( self->archetype, &self->ibob ) )
- {
- location = Location( (struct GraphicObject *)SELF );
- self->offset.x = location.x - self->ibob.Image.Width + 1;
- self->offset.y = location.y - self->ibob.Image.Height + 1;
- self->dragback.x = x;
- self->dragback.y = y;
-
- /* printf("got image bob\n"); */
- ToolWindow_SetBobLocation( self, x, y );
- /* printf("set its location\n"); */
-
- AddBob( &self->ibob.Bob, self->GelsRPort );
- /* printf("added bob to list.\n"); */
- DrawGList( self->GelsRPort, self->GelsVPort );
- /* printf("drew list.\n"); */
- self->state = BOB_DRAGGING;
-
- self->IDCMPbuf = IDCMPFlags( (struct Interactor *)SELF );
- SetIDCMPFlags( SELF, self->IDCMPbuf | MOUSEMOVE );
-
- Forbid();
- self->pw.Window->Flags |= RMBTRAP; /* right mouse trap. */
- Permit();
-
- QueueFunc( &DeferredResponses,
- (ptr2func) ToolWindow_BeSelfish, self );
- }
- else
- printf( "couldn't get Imagery for %s\%n", ClassName( (struct PObject *)self->archetype ) );
- }
- }
-
- Window *ToolWindow_FindDropWindow( Screen *screen, short x, short y )
- /* NOTE: x&y are in SCREEN space, not WINDOW space! */
- {
- Window *w;
- Window *dropwindow = NULL;
-
- /* traverse screen's window list. */
- Forbid();
- for (w = screen->FirstWindow; w != NULL; w=w->NextWindow)
- {
- /* check for hit on this window */
- if ( ( x >= w->LeftEdge ) && ( x < (w->LeftEdge + w->Width) )
- && ( y >= w->TopEdge ) && ( y < (w->TopEdge + w->Height) ) )
- {
- dropwindow = w;
- break;
- }
- }
- Permit();
-
- return dropwindow;
- }
-
- void ToolWindow_HandleDrop( ToolWindow *self, IntuiMessage *event )
- {
- Screen *screen;
- Window *dropwindow;
- BuilderWindow *bw;
- GraphicObject *new_object;
- Point p,
- location,
- size,
- drop_coords;
- int drop_ok = FALSE;
-
- screen = self->pw.Window->WScreen;
-
- location = Location( (struct GraphicObject *)SELF );
-
- p.x = event->MouseX + location.x;
- p.y = event->MouseY + location.y;
-
- dropwindow = ToolWindow_FindDropWindow( screen, p.x, p.y );
- if (dropwindow)
- {
- bw = (BuilderWindow *)dropwindow->UserData;
-
- if( isa( (struct PObject *)bw, BuilderWindowClass() ) )
- {
- drop_ok = TRUE;
- }
- if( !drop_ok ) /* Window is not a BuilderWindow -- EDB */
-
- ToolWindow_DragBack( self, event->MouseX, event->MouseY );
-
- }
- else /* If !dropwindow = no Window! return home -- EDB */
- {
- ToolWindow_DragBack( self, event->MouseX, event->MouseY );
- }
-
- ToolWindow_StopDragging(self);
-
- if (drop_ok)
- {
- if( new_object = (struct GraphicObject *)New( (struct PObject *)self->archetype ) )
- {
- location = Location( (struct GraphicObject *)bw );
- size = Size( new_object );
- drop_coords.x = p.x - location.x - size.x +1;
- drop_coords.y = p.y - location.y - size.y +1;
- SetLocation( new_object, drop_coords.x, drop_coords.y );
- BuilderWindow_AddPObject( bw, new_object );
- Project_Modify();
- }
- else
- {
- printf("Couldn't copy archetype object %s\n",
- ClassName( (struct PObject *)self->archetype ) );
- }
- }
-
- }
-
-
- void ToolWindow_RemoveBob( ToolWindow *self )
- {
- RemBob( &self->ibob.Bob );
- DrawGList( self->GelsRPort, self->GelsVPort );
- }
-
- void ToolWindow_CleanUpFromDragging( ToolWindow *self )
- {
- RastPort *rport;
- Point location, size;
- ToolButton *button;
-
- SetIDCMPFlags( SELF, self->IDCMPbuf );
-
- Forbid();
- self->pw.Window->Flags &= ~RMBTRAP; /* right mouse trap OFF. */
- Permit();
-
- button = self->selected_button;
- SetValue( (Valuator *)button, FALSE ); /* turn off button. */
-
- /* clear area of button to erase highlight. */
- rport = RPort( &self->pw );
- SetDrMd( rport, JAM1 );
- SetAPen( rport, 0 );
- location = Location( (struct GraphicObject *)button );
- size = Size( (struct GraphicObject *)button );
-
- RectFill( rport, location.x, location.y,
- location.x+size.x-1, location.y+size.y-1 );
-
- Refresh( (struct Interactor *)button );
-
- /* release memory of image. */
- CleanUpImageBob( self->archetype, &self->ibob );
- self->state = BOB_WAITING;
- }
-
- void ToolWindow_StopDragging( ToolWindow *self )
- {
- if (self->state == BOB_DRAGGING)
- {
- ToolWindow_RemoveBob(self);
- ToolWindow_CleanUpFromDragging(self);
- }
- }
-
-
-
- void ToolWindow_Drag( ToolWindow *self, short x, short y )
- {
- if (self->state == BOB_DRAGGING)
- {
- ToolWindow_SetBobLocation( self, x, y );
- DrawGList( self->GelsRPort, self->GelsVPort );
- }
- }
-
- #define CONVERGE_BY 20
- #define CONVERGE_ON(n,m) \
- if (n>m){ n -=((n>m+CONVERGE_BY)?CONVERGE_BY:n-m); } \
- else if (n<m){ n +=((n<m-CONVERGE_BY)?CONVERGE_BY:m-n); }
-
- void ToolWindow_DragBack( ToolWindow *self, short x, short y )
- {
- int home_x, home_y;
-
- home_x = self->dragback.x;
- home_y = self->dragback.y;
-
- for(;;)
- {
- if (x == home_x && y== home_y) break;
- ToolWindow_SetBobLocation( self, x, y );
- DrawGList( self->GelsRPort, self->GelsVPort );
- CONVERGE_ON(x, home_x);
- CONVERGE_ON(y, home_y);
- /* printf("Bob=(%d,%d), home=(%d,%d)\n", x,y, home_x, home_y ); */
- }
- /* Draw once more at home location */
- ToolWindow_SetBobLocation( self, x, y );
- DrawGList( self->GelsRPort, self->GelsVPort );
- }
-
- USHORT ToolWindow_Respond( ToolWindow *self, IntuiMessage *event )
- {
- USHORT response =0;
- ToolButton *tb;
-
- if (event->IDCMPWindow == iWindow(&self->pw))
- {
- response |= RESPONDED;
-
- switch (event->Class)
- {
- case MOUSEMOVE:
- ToolWindow_Drag( self, event->MouseX, event->MouseY );
- break;
-
- case REFRESHWINDOW:
- BeginRefresh( self->pw.Window );
- Refresh( (struct Interactor *)&self->pw );
- if (self->state == BOB_DRAGGING)
- DrawGList( self->GelsRPort, self->GelsVPort );
-
- EndRefresh( self->pw.Window, TRUE );
- break;
-
- case GADGETDOWN:
- for (tb = (ToolButton*) self->pw.FirstInteractor;
- tb != NULL;
- tb = tb->Next)
- {
- if( Respond( (struct Interactor *)tb, event ) & CHANGED_STATE )
- {
- /* start dragging. */
- self->selected_button = tb;
- self->archetype = Archetype(tb);
-
-
- ToolWindow_StartDragging( self, event->MouseX, event->MouseY );
- break; /* end loop. */
- }
- }
- break;
-
- case GADGETUP:
- /* cancel. */
- if (self->state == BOB_DRAGGING)
- {
- ToolWindow_DragBack( self, event->MouseX, event->MouseY );
- ToolWindow_StopDragging( self );
- }
- break;
-
-
- case MOUSEBUTTONS:
- switch (event->Code)
- {
- case MENUDOWN:
- /* Cancel Movement. */
- ToolWindow_DragBack( self, event->MouseX, event->MouseY );
- ToolWindow_StopDragging( self );
- break;
-
- case SELECTUP:
- /* Accept Movement. */
- if (self->state == BOB_DRAGGING)
- {
- ToolWindow_HandleDrop( self, event );
- }
- break;
- }
- break;
- }
- }
- return response;
- }
-
-
- Window *pcgWindow_OpenWindow( pcgWindow *self );
-
- Window *ToolWindow_OpenWindow( ToolWindow *self )
- {
- Window *w = NULL;
-
- if (w = pcgWindow_OpenWindow( &self->pw ))
- {
- /* Adjust size and location to window border. */
- ;
-
- /* Initialize gels. */
- self->GelsRPort = &w->WScreen->RastPort;
- self->GelsVPort = &w->WScreen->ViewPort;
- GelsSystem_Init( &self->gelssystem, self->GelsRPort, 0 );
- }
- return w;
- }
-
- void ToolWindow_CleanUp( ToolWindow *self )
- {
- Interactor *iactor;
-
- while ((iactor=self->pw.FirstInteractor) != NULL)
- {
- RemoveWindowPObject( (struct pcgWindow *)self, (struct GraphicObject *)iactor );
- CleanUp( (struct PObject *)iactor );
- Free( (struct PObject *)iactor );
- }
- }
-
-
- BOOL ToolWindow_elaborated = FALSE;
-
- struct pcgWindowClass ToolWindow_Class;
-
- void ToolWindowClass_Init( struct pcgWindowClass *class )
- {
- pcgWindowClass_Init( (struct pcgWindowClass *) class );
- class->isa = pcgWindowClass();
- class->ClassName = "ToolWindow";
- class->Respond = (USHORT(*)(Interactor *, IntuiMessage *))ToolWindow_Respond;
- class->OpenWindow = (Window *(*)(pcgWindow *))ToolWindow_OpenWindow;
- class->CleanUp = (void(*)(PObject *))ToolWindow_CleanUp;
- }
-
-
- struct pcgWindowClass *ToolWindowClass( void )
- {
- if( ! ToolWindow_elaborated )
- {
- ToolWindowClass_Init( &ToolWindow_Class );
- ToolWindow_elaborated = TRUE;
- }
-
- return &ToolWindow_Class;
- }
-
-
- void ToolWindow_AddArchetype( ToolWindow *self,
- GraphicObject *archetype )
- {
- ToolButton *tb;
- Point size, wsize;
-
- if (tb = (ToolButton*) Amalloc( sizeof(ToolButton) ) )
- {
- wsize = Size( (struct GraphicObject *)SELF );
-
- ToolButton_Init( tb, 4, self->y, wsize.x-8, self->pens, archetype );
-
- size = Size( (struct GraphicObject *)tb );
- self->y += size.y;
-
- SetSize( (struct GraphicObject *)SELF, wsize.x, self->y+2 );
-
- AddWindowPObject( (struct pcgWindow *)SELF, (struct GraphicObject *)tb );
- }
- else
- printf("couldn't allocate toolbutton\n");
- }
-
-
-
- void ToolWindow_Init( ToolWindow *self,
- USHORT leftedge,
- USHORT topedge,
- Screen *screen,
- struct MsgPort *SharedPort,
- pcg_3DPens pens,
- char *title )
- {
-
- pcgWindow_Init( &self->pw, leftedge, topedge, 120, 200, 1, 1, 65535, 65535, title,
- REFRESHWINDOW | MOUSEBUTTONS | MENUPICK | MENUVERIFY,
- SIMPLE_REFRESH | WINDOWDRAG | WINDOWDEPTH | REPORTMOUSE,
- screen );
-
- self->pw.isa = ToolWindowClass();
- self->pw.SharedUserPort = SharedPort;
-
- self->state = BOB_WAITING;
- self->IDCMPbuf = NULL;
- self->GelsRPort = NULL;
- self->GelsVPort = NULL;
-
- self->selected_button = NULL;
- self->archetype = NULL;
-
- self->y = 12;
- self->pens = pens;
- self->builder = NULL;
- }
-
-
- extern struct MsgPort *shared_port;
-
- void ToolWindow_BeSelfish( ToolWindow *self )
- {
- struct IntuiMessage *message, local_msg;
- BOOL got_a_msg;
-
- while( self->state != BOB_WAITING )
- {
- got_a_msg = FALSE;
-
- for (;;) /* Strip all events from message queue. */
- {
- message = (struct IntuiMessage*) GetMsg( shared_port );
-
- if (message)
- {
- local_msg = *message;
- ReplyMsg( (struct Message *)message );
- /* EDB -- bug fix
- * Need to respond to all messages or we lose GADGETUPs
- * while dragging image.
- */
- ToolWindow_Respond( self, &local_msg );
- got_a_msg = TRUE;
- }
- else /* No message */
- {
- if (got_a_msg) /* We responded to all of them */
- {
- break; /* Get out of for loop */
- }
- else /* no message to respond to yet */
- WaitPort( shared_port );
- }
- }
- }
-
- }
-