/*********************************************/
/*                                           */
/*       Designer (C) Ian OConnor 1994       */
/*                                           */
/*      Designer Produced C include file     */
/*                                           */
/*********************************************/

#include <exec/types.h>
#include <exec/memory.h>
#include <intuition/screens.h>
#include <intuition/intuition.h>
#include <intuition/gadgetclass.h>
#include <libraries/gadtools.h>
#include <graphics/gfxbase.h>
#include <workbench/workbench.h>
#include <clib/exec_protos.h>
#include <clib/wb_protos.h>
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/graphics_protos.h>
#include <clib/utility_protos.h>
#include <string.h>

#include "stickit2.h"


struct Menu *commodMenu = NULL;

struct NewMenu commodMenuNewMenu[] =
	{
	NM_TITLE, (STRPTR)"Project"      ,  0 , 0, 0, 0,
	NM_ITEM , (STRPTR)"Save"         , (STRPTR)"S", 0, 0, 0,
	NM_ITEM , (STRPTR)"About..."     , (STRPTR)"?", 0, 0, 0,
	NM_ITEM , (STRPTR)"Hide"         , (STRPTR)"H", 0, 0, 0,
	NM_ITEM , NM_BARLABEL            ,  0 , 0, 0, 0,
	NM_ITEM , (STRPTR)"Quit"         , (STRPTR)"Q", 0, 0, 0,
	NM_TITLE, (STRPTR)"Notes"        ,  0 , 0, 0, 0,
	NM_ITEM , (STRPTR)"New note"     , (STRPTR)"N", 0, 0, 0,
	NM_ITEM , (STRPTR)"Delete note"  , (STRPTR)"D", 0, 0, 0,
	NM_ITEM , NM_BARLABEL            ,  0 , 0, 0, 0,
	NM_ITEM , (STRPTR)"Redraw notes" , (STRPTR)"R", 0, 0, 0,
	NM_END  , NULL                   ,  0 , 0, 0, 0
	};

ULONG commodMenuTags[] =
	{
	GT_TagBase+67, TRUE,
	TAG_DONE
	};

struct Menu *noteMenu = NULL;

struct NewMenu noteMenuNewMenu[] =
	{
	NM_TITLE, (STRPTR)"Project"           ,  0 , 0, 0, 0,
	NM_ITEM , (STRPTR)"Save"              , (STRPTR)"S", 0, 0, 0,
	NM_ITEM , NM_BARLABEL                 ,  0 , 0, 0, 0,
	NM_ITEM , (STRPTR)"Commodities panel" , (STRPTR)"P", 0, 0, 0,
	NM_TITLE, (STRPTR)"Edit"              ,  0 , 0, 0, 0,
	NM_ITEM , (STRPTR)"Cut"               , (STRPTR)"X", 0, 0, 0,
	NM_ITEM , (STRPTR)"Copy"              , (STRPTR)"C", 0, 0, 0,
	NM_ITEM , (STRPTR)"Paste"             , (STRPTR)"V", 0, 0, 0,
	NM_ITEM , (STRPTR)"Delete"            , (STRPTR)"B", 0, 0, 0,
	NM_END  , NULL                        ,  0 , 0, 0, 0
	};

ULONG noteMenuTags[] =
	{
	GT_TagBase+67, TRUE,
	TAG_DONE
	};

struct Window *commod = NULL;
APTR commodVisualInfo;
struct Gadget *commodGList;
struct Gadget *commodGadgets[11];

STRPTR commod_cyccolourLabels[] =
{
	(STRPTR)"Background",
	(STRPTR)"Text",
	NULL
};

ULONG commodGadgetTags[] =
	{
	GTLV_ShowSelected, 0,
	GTLV_Selected, 0,
	TAG_END,
	GT_Underscore, '_',
	TAG_END,
	GT_Underscore, '_',
	TAG_END,
	GT_Underscore, '_',
	TAG_END,
	GT_Underscore, '_',
	TAG_END,
	GTPA_Depth, 0,
	GTPA_IndicatorWidth, 30,
	GA_Disabled, TRUE,
	TAG_END,
	GA_Disabled, TRUE,
	GTCY_Labels, &commod_cyccolourLabels[0],
	TAG_END,
	GTST_MaxChars, 128,
	GA_Disabled, TRUE,
	GT_Underscore, '_',
	TAG_END,
	GTTX_Border, TRUE,
	TAG_END,
	GT_Underscore, '_',
	GA_Disabled, TRUE,
	TAG_END,
	GA_Disabled, TRUE,
	GT_Underscore, '_',
	TAG_END,
	};

UWORD commodGadgetTypes[] =
	{
	LISTVIEW_KIND,
	BUTTON_KIND,
	BUTTON_KIND,
	BUTTON_KIND,
	BUTTON_KIND,
	PALETTE_KIND,
	CYCLE_KIND,
	STRING_KIND,
	TEXT_KIND,
	BUTTON_KIND,
	STRING_KIND,
	};

struct NewGadget commodNewGadgets[] =
	{
	4, 2, 226, 70, NULL, NULL, commod_listview, 1, NULL,  &commodGadgetTags[0],
	4, 73, 50, 14, (UBYTE *)"_New", NULL, commod_new, 16, NULL,  &commodGadgetTags[5],
	57, 73, 50, 14, (UBYTE *)"De_l", NULL, commod_del, 16, NULL,  &commodGadgetTags[8],
	110, 73, 50, 14, (UBYTE *)"_Up", NULL, commod_up, 16, NULL,  &commodGadgetTags[11],
	163, 73, 50, 14, (UBYTE *)"_Down", NULL, commod_down, 16, NULL,  &commodGadgetTags[14],
	340, 37, 150, 15, NULL, NULL, commod_palette, 1, NULL,  &commodGadgetTags[17],
	340, 20, 150, 14, (UBYTE *)"Note colours", NULL, commod_cyccolour, 1, NULL,  &commodGadgetTags[24],
	340, 3, 150, 14, (UBYTE *)"_Title bar", NULL, commod_title, 1, NULL,  &commodGadgetTags[29],
	340, 55, 150, 16, NULL, NULL, commod_fontstr, 1, NULL,  &commodGadgetTags[36],
	236, 56, 100, 14, (UBYTE *)"_Font...", NULL, commod_font, 16, NULL,  &commodGadgetTags[39],
	340, 74, 150, 14, (UBYTE *)"Pub. _Screen", NULL, commod_pubscreen, 1, NULL,  &commodGadgetTags[44],
	};
UWORD commodDepth;

int MakeMenucommodMenu( APTR MenuVisualInfo )
{
	if (NULL == (commodMenu = CreateMenusA( commodMenuNewMenu, NULL))) 
		return( 1L );
	LayoutMenusA( commodMenu, MenuVisualInfo, &commodMenuTags[0]);
	return( 0L );
}

int MakeMenunoteMenu( APTR MenuVisualInfo )
{
	if (NULL == (noteMenu = CreateMenusA( noteMenuNewMenu, NULL))) 
		return( 1L );
	LayoutMenusA( noteMenu, MenuVisualInfo, &noteMenuTags[0]);
	return( 0L );
}

int OpenWindowcommod( struct MsgPort *mp)
{
struct Screen *Scr;
UWORD offx, offy;
UWORD loop;
struct NewGadget newgad;
struct Gadget *Gad;
if (commod == NULL)
	{
	if (NULL != (Scr = LockPubScreen(NULL)))
		{
		ULONG scalex = 65535*Scr->RastPort.Font->tf_XSize/8;
		ULONG scaley = 65535*Scr->RastPort.Font->tf_YSize/8;
		offx = Scr->WBorLeft;
		offy = Scr->WBorTop + Scr->Font->ta_YSize+1;
		if (NULL != ( commodVisualInfo = GetVisualInfoA( Scr, NULL)))
			{
			commodGList = NULL;
			Gad = CreateContext( &commodGList);
			commodDepth = Scr->BitMap.Depth;
			commodGadgetTags[18] = Scr->BitMap.Depth;
			for ( loop=0 ; loop<11 ; loop++ )
				{
				CopyMem((char * )&commodNewGadgets[loop], ( char * )&newgad, (long)sizeof( struct NewGadget ));
				newgad.ng_VisualInfo = commodVisualInfo;
				newgad.ng_LeftEdge = newgad.ng_LeftEdge*scalex/65535;
				newgad.ng_TopEdge = newgad.ng_TopEdge*scaley/65535;
				if (commodGadgetTypes[loop] != GENERIC_KIND)
					{
					newgad.ng_Width = newgad.ng_Width*scalex/65535;
					newgad.ng_Height = newgad.ng_Height*scaley/65535;
					};
				newgad.ng_TextAttr = Scr->Font;
				newgad.ng_LeftEdge += offx;
				newgad.ng_TopEdge += offy;
				commodGadgets[ newgad.ng_GadgetID - commodFirstID ] = Gad = CreateGadgetA( commodGadgetTypes[loop], Gad, &newgad, newgad.ng_UserData );
				}
			if (Gad != NULL)
				{
				if (NULL != (commod = OpenWindowTags( NULL, WA_Left, 0,
								WA_Top, 11,
								WA_Width, 502*scalex/65535+offx,
								WA_Height, 93*scaley/65535+offy,
								WA_Title, "StickIt2 ©1994 Andy Dean",
								WA_ScreenTitle, "StickIt2 ©1994 Andy Dean",
								WA_MinWidth, 150,
								WA_MinHeight, 25,
								WA_MaxWidth, 1200,
								WA_MaxHeight, 1200,
								WA_DragBar, TRUE,
								WA_DepthGadget, TRUE,
								WA_CloseGadget, TRUE,
								WA_Activate, TRUE,
								WA_Dummy+0x30, TRUE,
								WA_SmartRefresh, TRUE,
								WA_AutoAdjust, TRUE,
								WA_Gadgets, commodGList,
								TAG_END)))
					{
					commod->UserPort = mp;
					ModifyIDCMP( commod, 6292348);
					GT_RefreshWindow( commod, NULL);
					UnlockPubScreen( NULL, Scr);
					if (commodMenu == NULL)
						MakeMenucommodMenu( commodVisualInfo);
					if (commodMenu != NULL)
						{
						SetMenuStrip( commod, commodMenu);
						return( 0L );
						}
					CloseWindowSafely( commod );
					}
				}
			FreeGadgets( commodGList);
			FreeVisualInfo( commodVisualInfo );
			}
		UnlockPubScreen( NULL, Scr);
		}
	}
else
	{
	WindowToFront(commod);
	ActivateWindow(commod);
	return( 0L );
	}
return( 1L );
}

void CloseWindowcommod( void )
{
if (commod != NULL)
	{
	ClearMenuStrip( commod);
	FreeMenus( commodMenu);
	commodMenu = NULL;
	CloseWindowSafely( commod);
	commod = NULL;
	FreeVisualInfo( commodVisualInfo);
	FreeGadgets( commodGList);
	}
}

void StripIntuiMessages( struct MsgPort *mp, struct Window *win)
{
struct IntuiMessage *msg;
struct Node *succ;
msg = (struct IntuiMessage *)mp->mp_MsgList.lh_Head;
while (succ = msg->ExecMessage.mn_Node.ln_Succ)
	{
	if (msg->IDCMPWindow == win)
		{
		Remove(msg);
		ReplyMsg(msg);
		}
	msg = (struct IntuiMessage *)succ;
	}
}

void CloseWindowSafely( struct Window *win)
{
	Forbid();
	StripIntuiMessages( win->UserPort, win);
	win->UserPort = NULL;
	ModifyIDCMP( win, 0L);
	Permit();
	CloseWindow( win);
}

