/*
**
** TBTalk.c
**
** Version:- 1.02
**
** By Stuart Kelly
** Copyright 1996 ©
**
** NOTES:-
**
** 1. Do NOT alter this file or tbmessage_def.h or tbm.o
**
** 2. To Compile using Dice:-
**
** dcc:bin/dcc TBTalk.c tbm.o -o TBTalk
**
** 3. For use with TBPort or TBPort2.
**
** 4. Don`t need to open any libraries as dice will do this.
**    If your compiler does not do this, the code will need altering.
**
** What I Do:- Let the user draw ellispes on the window.
**
** Programers Note:- This code shows you how you can use TBPort2.
**
*/

#define GD_Gadget00 0

/* includes */
#include <stdio.h>
#include <clib/gadtools_protos.h>
/* don`t need any more includes as tbmessage_def.h has all I need */
#include "tbmessage_def.h"

/* global structures and values */
struct Screen *my_screen = NULL;
struct Window *AWnd = NULL;
struct RastPort *rp;

__chip USHORT Image1data[] =
{
   0x0000, 0x0000,
   0x7FFF, 0xFE00,
   0x7FFF, 0xFE00,
   0x7FFF, 0xFE00,
   0x7FFF, 0xFE00,
   0x7FFF, 0xFE00,
   0x7FFF, 0xFE00,
   0x7FFF, 0xFE00,
   0x7FFF, 0xFE00,
   0x7FFF, 0xFE00,
   0xFFFF, 0xFE00,

   0xFFFF, 0xFE00,
   0xFFFF, 0xFC00,
   0xF03C, 0x3C00,
   0xF3F9, 0xFC00,
   0xF3F3, 0xFC00,
   0xF0F3, 0xFC00,
   0xF3F3, 0xFC00,
   0xF3F9, 0xFC00,
   0xF03C, 0x3C00,
   0xFFFF, 0xFC00,
   0x0000, 0x0000,

   };

struct Image Image1 =
{
   0,0,     /* X, Y */
   23, 11, 2,    /* Width, Height, Depth */
   &Image1data[0],   /* Image Data*/
   3, 0,    /* PlanePick, PlaneOnOff */
   NULL     /* Next Image Structure*/
};

struct Gadget ec_ButGad =
{
NULL,
10,-10,23,11,
GFLG_GADGIMAGE | GFLG_GADGHCOMP | GFLG_RELBOTTOM,
GACT_RELVERIFY | GACT_BOTTOMBORDER,
GTYP_BOOLGADGET,
&Image1,
NULL,
NULL,
NULL,
NULL,
GD_Gadget00,
NULL
};


void main(void)
{
 /* structures and vales */
 struct IntuiMessage *imsg;
 BOOL quit_now = FALSE;
 SHORT mousex, mousey;
 struct Gadget *c_gad;

 int option = 0;

 int pen_number = 0;

 /* ------ */

 my_screen = TB_GetTBScreen();    /* Get TB`s Screen - TBPort Function*/

 if (my_screen == NULL) goto cleanup; /* quit if I didn`t get it */

 /* open a window on TB`s screen */
 if (!(AWnd = OpenWindowTags(NULL,
       WA_Title, "TBTalk By Stuart Kelly",
       WA_CloseGadget, TRUE,
       WA_Top, 11,
       WA_Left, 0,
       WA_Height, 100,
       WA_Width, 300,
       WA_Gadgets, &ec_ButGad,
       WA_SizeGadget, TRUE,
       WA_SizeBBottom, TRUE,
       WA_MinWidth, 300,   /* <- need these because we have a size gadget */
       WA_MinHeight, 100,  /* <- as above.      */
       WA_MaxHeight, 256,  /* <- as above.      */
       WA_MaxWidth, 640,   /* <- as above.      */
       WA_DragBar, TRUE,
       WA_DepthGadget, TRUE,
       WA_CustomScreen, my_screen, /* TB Screen ( if we have it ) */
       WA_Activate, TRUE,
       WA_RMBTrap, TRUE,  /* catch right mouse button events */
       WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_MOUSEBUTTONS |IDCMP_GADGETUP,
       TAG_DONE)))
       {
       /* couldn`t open the window */
       printf(" Can`t Open Window\n");
       goto cleanup;
       }

 /* draw on the window */
 rp = AWnd->RPort;

 SetAPen(rp, 3);  /* bule */
 RectFill(rp, 5, 11 + 3, 50, 20);

 SetAPen(rp, 1); /* black */
 Move(rp, 30, 20);
 Draw(rp, 50, 50);

 SetAPen(rp, 2); /* white, 0 = background colour */
 DrawEllipse(rp, 60, 60, 10, 10);

 SetAPen(rp, 3);
 DrawEllipse(rp, 80, 70, 10, 15);

 SetAPen(rp, 1);
 Move(rp, 20, 40);
 Text(rp, "Close The Window To Exit", 24);
 Move(rp, 20, 50);
 Text(rp, "Right MB Change Colour", 22);
 Move(rp, 20, 60);
 Text(rp, "Left MB Draw Ellipse", 20);

 /* TB_Wait(2); */ /* Wait 2 Seconds */

 /*
 ** TB_SayM(char *tb_text);
 **
 ** tb_text:- Text to be used by TBPort2
 */

 TB_SayM("     TBTalk By Stuart Kelly           \n"
         "        Copyright 1996 ©              \n"
         "--------------------------------------\n"
         " Example of how to use TBPort 2       \n"
         " Source code is included.             \n"
         " Compiled using Dice C by Matt Dillon \n"
         "--------------------------------------  ");

 /* ------------------------------- */

 /* IDCMP stuff here */

 pen_number = 1; /* set pen colour to black */

 while (!quit_now) /* quit now while */
   {
    Wait(1L << AWnd->UserPort->mp_SigBit);

    while ( imsg = GT_GetIMsg(AWnd->UserPort))
     {
      /* get mouse position */
      mousex = imsg->MouseX;
      mousey = imsg->MouseY;
      c_gad = (struct Gadget *)imsg->IAddress;
      /* switch IDCMP messages */
      switch (imsg->Class)
       {
    case IDCMP_CLOSEWINDOW:
     /* use pressed close gadget, so lets quit */
     quit_now = TRUE;
     break;

    case IDCMP_GADGETUP:

    switch(c_gad->GadgetID)
     {
     case GD_Gadget00:

     /* user pressed EC Gadget */

     option = 1;

     }

    break;

    case IDCMP_MOUSEBUTTONS:
       /* switch  mouse button messages */
       switch (imsg->Code)
   {
       case SELECTDOWN:
   /* left mouse button, draw an ellipse at mousex and */
   /* mousey.       */
   DrawEllipse(rp, mousex, mousey, 10, 10);
       break;

       case MENUDOWN:
   /* right mouse button, change colour of ellipse drawn*/
   pen_number = pen_number + 1;
   if (pen_number>3) pen_number = 0;
   SetAPen(rp, pen_number);
   break;

       default:
   /* another code not supported */
   break;
       } /* end of mouse switch */
     /* break mouse IDCMP */
     break;

       }  /* end of IDCMP switch */
     GT_ReplyIMsg(imsg); /* reply IDCMP message */

     /* if option == 1 run MODS/Colour */

     if (option == 1) {
     option = 0;

  /* Must send screen to back as Edit Colours opens on the last screen. */

     ScreenToBack(my_screen);

  /* Run Edit Colours */

     SystemTags("MODS/Colour", TAG_DONE);
     }

     } /* end of imsg collection while */
   } /* end of while not quit_now */

 cleanup:

 /* close the window */
 if (AWnd) { CloseWindow(AWnd); AWnd = NULL; }

 /* note If you use TB_GetTBScreen() then you must use TB_FinishNow() */
 /* If you don`t get the screen you still have to use TB_FinishNow() */
 /* See Below:- */

 if (!my_screen) TB_FinishNow();   /* TBPort Function */
 /* You have to tell TB you have finished even if you didn`t get the */
 /* screen */

 if (my_screen) {TB_FinishNow(); my_screen = NULL;}
 /* if I had the screen tell TB I have */
 /* finished with it. */


 /* END OF PROGRAM */
return;
}

