#ifndef MAKE_ID
#define MAKE_ID(a,b,c,d) ((ULONG) (a)<<24 | (ULONG) (b)<<16 | (ULONG) (c)<<8 | (ULONG) (d))
#endif

#ifdef _DCC
#define __inline
#endif

#include "MyEd.h"

#define USE_ABOUT_COLORS
#define USE_ABOUT_BODY
#include "About.h"

extern char* body_text;

Object *MakeImage(LONG w,LONG h,LONG d,const UBYTE *body,const ULONG *colors)
{
	Object *obj = BodychunkObject,
		MUIA_FixWidth             , w,
		MUIA_FixHeight            , h,
		MUIA_Bitmap_Width         , w,
		MUIA_Bitmap_Height        , h,
		MUIA_Bodychunk_Depth      , d,
		MUIA_Bodychunk_Body       , (UBYTE *)body,
		MUIA_Bodychunk_Compression, ABOUT_COMPRESSION,
		MUIA_Bodychunk_Masking    , ABOUT_MASKING,
		MUIA_Bitmap_SourceColors  , (ULONG *)colors,
		MUIA_Bitmap_Transparent   , 0,
		End;
	return(obj);
}

struct ObjApp * CreateApp(char* base)
{
	struct ObjApp * Object;

	APTR	MNlabel_Project, MNlabel_Include, MNlabel_Save, MNMenuesBarLabel10;
	APTR	MNlabel_About, MNlabel_Quit, MNMenuesEdit, MNlabel_Cut, MNlabel_Copy;
	APTR	MNlabel_Paste, MNMenuesBarLabel11, MNlabel_Clear, GROUP_ROOT, GR_EditArea;
	APTR	GR_Buttons, GROUP_ROOT_0, GR_Title, IM_Picture;

	if (!(Object = AllocVec(sizeof(struct ObjApp), MEMF_PUBLIC|MEMF_CLEAR)))
		return(NULL);

	clip_handle = OpenClipboard(0);
	undo_handle = OpenClipboard(42);

	Object->LV_label_1Content =	"\33bGeditor\33n is a simple text editor to use"
											" as an external editor for MUI applications that"
											" needs one."
											"\nIt does the job, but not very much more...\n\n"
											"\33bGeditor\33n © 1996 by Lars Malmborg.\n\n"
											"\33bMUI\33n © 1996 by Stefan Stuntz.\n\n"
											"\33btextfield.gadget\33n © 1995 by Mark Thomas.";

	GR_EditArea = GroupObject,
		MUIA_Group_Horiz, TRUE,
		Child, Object->text = BoopsiObject,  /* MUI and Boopsi tags mixed */
			InputListFrame,
			MUIA_Boopsi_Class,		TextFieldClass,
			MUIA_Boopsi_Smart,		TRUE,
			MUIA_Boopsi_MinWidth,	100, /* boopsi objects don't know */
			MUIA_Boopsi_MinHeight,	50, /* their sizes, so we help   */
			ICA_TARGET,				ICTARGET_IDCMP, /* needed for notification */
			TEXTFIELD_Text,			body_text,
			TEXTFIELD_ClipStream,	clip_handle,
			TEXTFIELD_UndoStream,	undo_handle,
			TEXTFIELD_PassCommand,	TRUE,
			TEXTFIELD_BlockCursor,	TRUE,
		End,
		Child, Object->sbar = ScrollbarObject,
		End,
	End;

	Object->BT_OK = SimpleButton("_OK");

	Object->BT_Cancel = SimpleButton("_Cancel");

	GR_Buttons = GroupObject,
		MUIA_HelpNode, "GR_Buttons",
		MUIA_Group_Horiz, TRUE,
		Child, Object->BT_OK,
		Child, Object->BT_Cancel,
	End;

	GROUP_ROOT = GroupObject,
		Child, GR_EditArea,
		Child, GR_Buttons,
	End;

	MNlabel_Include = MenuitemObject,
		MUIA_Menuitem_Title, "Include...",
		MUIA_Menuitem_Shortcut, "I",
	End;

	MNlabel_Save = MenuitemObject,
		MUIA_Menuitem_Title, "Save",
		MUIA_Menuitem_Shortcut, "S",
	End;

	MNMenuesBarLabel10 = MUI_MakeObject(MUIO_Menuitem, NM_BARLABEL, 0, 0, 0);

	MNlabel_About = MenuitemObject,
		MUIA_Menuitem_Title, "About...",
		MUIA_Menuitem_Shortcut, "?",
	End;

	MNlabel_Quit = MenuitemObject,
		MUIA_Menuitem_Title, "Quit",
		MUIA_Menuitem_Shortcut, "Q",
	End;

	MNlabel_Project = MenuitemObject,
		MUIA_Menuitem_Title, "Project",
		MUIA_Family_Child, MNlabel_Include,
		MUIA_Family_Child, MNlabel_Save,
		MUIA_Family_Child, MNMenuesBarLabel10,
		MUIA_Family_Child, MNlabel_About,
		MUIA_Family_Child, MNlabel_Quit,
	End;

	MNlabel_Cut = MenuitemObject,
		MUIA_Menuitem_Title, "Cut",
		MUIA_Menuitem_Shortcut, "X",
	End;

	MNlabel_Copy = MenuitemObject,
		MUIA_Menuitem_Title, "Copy",
		MUIA_Menuitem_Shortcut, "C",
	End;

	MNlabel_Paste = MenuitemObject,
		MUIA_Menuitem_Title, "Paste",
		MUIA_Menuitem_Shortcut, "V",
	End;

	MNMenuesBarLabel11 = MUI_MakeObject(MUIO_Menuitem, NM_BARLABEL, 0, 0, 0);

	MNlabel_Clear = MenuitemObject,
		MUIA_Menuitem_Title, "Clear",
		MUIA_Menuitem_Shortcut, "K",
	End;

	MNMenuesEdit = MenuitemObject,
		MUIA_Menuitem_Title, "Edit",
		MUIA_Family_Child, MNlabel_Cut,
		MUIA_Family_Child, MNlabel_Copy,
		MUIA_Family_Child, MNlabel_Paste,
		MUIA_Family_Child, MNMenuesBarLabel11,
		MUIA_Family_Child, MNlabel_Clear,
	End;

	Object->MN_Menues = MenustripObject,
		MUIA_Family_Child, MNlabel_Project,
		MUIA_Family_Child, MNMenuesEdit,
	End;

	Object->Geditor = WindowObject,
		MUIA_Window_Title, "Geditor",
		MUIA_Window_Menustrip, Object->MN_Menues,
		MUIA_Window_ID, MAKE_ID('G', 'D', 'I', 'T'),
		WindowContents, GROUP_ROOT,
	End;

/*
	IM_Picture = ImageObject,
		MUIA_Image_Spec, "5:Jurassic.bsh",
		MUIA_Frame, MUIV_Frame_ImageButton,
		MUIA_Image_FreeVert, TRUE,
		MUIA_Image_FreeHoriz, TRUE,
		MUIA_FixHeight, 64,
		MUIA_FixWidth, 64,
	End;
*/
	IM_Picture = MakeImage(ABOUT_WIDTH,ABOUT_HEIGHT,ABOUT_DEPTH,About_body,About_colors),

	Object->LV_label_1 = FloattextObject,
		MUIA_Floattext_Text, Object->LV_label_1Content,
		MUIA_Frame, MUIV_Frame_InputList,
	End;

	Object->LV_label_1 = ListviewObject,
		MUIA_HelpNode, "LV_label_1",
		MUIA_Listview_Input, FALSE,
		MUIA_Listview_List, Object->LV_label_1,
	End;

	GR_Title = GroupObject,
		MUIA_HelpNode, "GR_Title",
		MUIA_Group_Horiz, TRUE,
		Child, IM_Picture,
		Child, Object->LV_label_1,
	End;

	GROUP_ROOT_0 = GroupObject,
		Child, GR_Title,
	End;

	Object->about = WindowObject,
		MUIA_Window_Title, "About...",
		MUIA_Window_ID, MAKE_ID('B', 'O', 'U', 'T'),
		MUIA_Window_Open, FALSE,
		WindowContents, GROUP_ROOT_0,
	End;

	Object->App = ApplicationObject,
		MUIA_Application_Author, "Lars Malmborg",
		MUIA_Application_Base, base?base:"GEDITOR",
		MUIA_Application_Title, "Geditor",
		MUIA_Application_Version, "$VER: Geditor 1.0ß (15.08.96)",
		MUIA_Application_Copyright, "Freeware",
		MUIA_Application_Description, "A simple text editor using textfield.gadget and MUI.",
		SubWindow, Object->Geditor,
		SubWindow, Object->about,
	End;


	if (!Object->App)
	{
		if (undo_handle)
		{
			CloseClipboard(undo_handle);
			undo_handle = NULL;
		}
		if (clip_handle)
		{
			CloseClipboard(clip_handle);
			clip_handle = NULL;
		}
		FreeVec(Object);
		return(NULL);
	}

	DoMethod(MNlabel_Include,
		MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
		Object->App,
		2,
		MUIM_Application_ReturnID, Include
		);

	DoMethod(MNlabel_Save,
		MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
		Object->App,
		2,
		MUIM_Application_ReturnID, Save
		);

	DoMethod(MNlabel_About,
		MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
		Object->about,
		3,
		MUIM_Set, MUIA_Window_Open, TRUE
		);

	DoMethod(MNlabel_Quit,
		MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
		Object->Geditor,
		3,
		MUIM_Set, MUIA_Window_Open, FALSE
		);

	DoMethod(MNlabel_Quit,
		MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
		Object->about,
		3,
		MUIM_Set, MUIA_Window_Open, FALSE
		);

	DoMethod(MNlabel_Quit,
		MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
		Object->App,
		2,
		MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit
		);

	DoMethod(MNlabel_Cut,
		MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
		Object->App,
		2,
		MUIM_Application_ReturnID, Edit_Cut
		);

	DoMethod(MNlabel_Copy,
		MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
		Object->App,
		2,
		MUIM_Application_ReturnID, Edit_Copy
		);

	DoMethod(MNlabel_Paste,
		MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
		Object->App,
		2,
		MUIM_Application_ReturnID, Edit_Paste
		);

	DoMethod(MNlabel_Clear,
		MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
		Object->App,
		2,
		MUIM_Application_ReturnID, Edit_Clear
		);

	DoMethod(Object->Geditor,
		MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
		Object->Geditor,
		3,
		MUIM_Set, MUIA_Window_Open, FALSE
		);

	DoMethod(Object->Geditor,
		MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
		Object->App,
		2,
		MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit
		);

	DoMethod(Object->BT_OK,
		MUIM_Notify, MUIA_Pressed, FALSE,
		Object->App,
		2,
		MUIM_Application_ReturnID, OK
		);

	DoMethod(Object->BT_Cancel,
		MUIM_Notify, MUIA_Pressed, TRUE,
		Object->App,
		2,
		MUIM_Application_ReturnID, Cancel
		);

	DoMethod(Object->Geditor,
		MUIM_Window_SetCycleChain,
		Object->BT_OK,
		Object->BT_Cancel,
		0
		);

	DoMethod(Object->about,
		MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
		Object->about,
		3,
		MUIM_Set, MUIA_Window_Open, FALSE
		);

	DoMethod(Object->about,
		MUIM_Window_SetCycleChain, Object->LV_label_1,
		0
		);

	DoMethod(Object->text, MUIM_Notify, TEXTFIELD_Lines, MUIV_EveryTime,
				Object->sbar, 3, MUIM_Set, MUIA_Prop_Entries, MUIV_TriggerValue);
	DoMethod(Object->text, MUIM_Notify, TEXTFIELD_Visible, MUIV_EveryTime,
				Object->sbar, 3, MUIM_Set, MUIA_Prop_Visible, MUIV_TriggerValue);
	DoMethod(Object->text, MUIM_Notify, TEXTFIELD_Top, MUIV_EveryTime,
				Object->sbar, 3, MUIM_NoNotifySet, MUIA_Prop_First, MUIV_TriggerValue);
	DoMethod(Object->sbar, MUIM_Notify, MUIA_Prop_First, MUIV_EveryTime,
				Object->text, 3, MUIM_NoNotifySet, TEXTFIELD_Top, MUIV_TriggerValue);

	return(Object);
}

void DisposeApp(struct ObjApp * Object)
{
	MUI_DisposeObject(Object->App);
	if (undo_handle)
	{
		CloseClipboard(undo_handle);
		undo_handle = NULL;
	}
	if (clip_handle)
	{
		CloseClipboard(clip_handle);
		clip_handle = NULL;
	}
	FreeVec(Object);
}
