/* $Revision Header *** Header built automatically - do not edit! ***********
 *
 *	(C) Copyright 1991 by Torsten Jürgeleit
 *
 *	Name .....: editor.c
 *	Created ..: Sunday 22-Dec-91 21:22:28
 *	Revision .: 0
 *
 *	Date        Author                 Comment
 *	=========   ====================   ====================
 *	22-Dec-91   Torsten Jürgeleit      Created this file!
 *
 ****************************************************************************
 *
 *	Editor main display
 *
 * $Revision Header ********************************************************/

	/* Includes */

#include "includes.h"
#include "defines.h"
#include "imports.h"
#include "protos.h"

	/* Defines for editor gadgets */

#define FIRST_MODE_GADGET	EDITOR_GADGET_CREATE

#define EDITOR_GADGET1_TYPE		GADGET_DATA_TYPE_CYCLE
#define EDITOR_GADGET1_FLAGS		(GADGET_DATA_FLAG_HOTKEY | GADGET_DATA_FLAG_TEXT_LEFT)
#define EDITOR_GADGET1_TEXT		"_Type"
#define EDITOR_GADGET1_LEFT_EDGE	(10 + (4 + 1) * 8)
#define EDITOR_GADGET1_TOP_EDGE		5
#define EDITOR_GADGET1_WIDTH		110
#define EDITOR_GADGET1_HEIGHT		15
#define EDITOR_GADGET1_TEXT_ATTR	&topaz80_attr
#define EDITOR_GADGET1_ACTIVE		1
#define EDITOR_GADGET1_TEXT_ARRAY	&template_type_text_array[0]

#define EDITOR_GADGET2_TYPE		GADGET_DATA_TYPE_BUTTON
#define EDITOR_GADGET2_FLAGS		(GADGET_DATA_FLAG_HOTKEY | GADGET_DATA_FLAG_BUTTON_TOGGLE)
#define EDITOR_GADGET2_LEFT_EDGE	(EDITOR_GADGET1_LEFT_EDGE + EDITOR_GADGET1_WIDTH + 80 + 4 * 8)
#define EDITOR_GADGET2_TOP_EDGE		5
#define EDITOR_GADGET2_WIDTH		((6 + 3) * 8)
#define EDITOR_GADGET2_HEIGHT		15
#define EDITOR_GADGET2_TEXT		"_Create"
#define EDITOR_GADGET2_TEXT_ATTR	&topaz80_attr

#define EDITOR_GADGET3_TYPE		GADGET_DATA_TYPE_BUTTON
#define EDITOR_GADGET3_FLAGS		(GADGET_DATA_FLAG_HOTKEY | GADGET_DATA_FLAG_BUTTON_TOGGLE)
#define EDITOR_GADGET3_LEFT_EDGE	(EDITOR_GADGET2_LEFT_EDGE + EDITOR_GADGET2_WIDTH + 10)
#define EDITOR_GADGET3_TOP_EDGE		EDITOR_GADGET2_TOP_EDGE
#define EDITOR_GADGET3_WIDTH		EDITOR_GADGET2_WIDTH
#define EDITOR_GADGET3_HEIGHT		EDITOR_GADGET2_HEIGHT
#define EDITOR_GADGET3_TEXT		"_Modify"
#define EDITOR_GADGET3_TEXT_ATTR	&topaz80_attr

#define EDITOR_GADGET4_TYPE		GADGET_DATA_TYPE_BUTTON
#define EDITOR_GADGET4_FLAGS		(GADGET_DATA_FLAG_HOTKEY | GADGET_DATA_FLAG_BUTTON_TOGGLE)
#define EDITOR_GADGET4_LEFT_EDGE	(EDITOR_GADGET3_LEFT_EDGE + EDITOR_GADGET3_WIDTH + 10)
#define EDITOR_GADGET4_TOP_EDGE		EDITOR_GADGET2_TOP_EDGE
#define EDITOR_GADGET4_WIDTH		EDITOR_GADGET2_WIDTH
#define EDITOR_GADGET4_HEIGHT		EDITOR_GADGET2_HEIGHT
#define EDITOR_GADGET4_TEXT		"C_lone"
#define EDITOR_GADGET4_TEXT_ATTR	&topaz80_attr

#define EDITOR_GADGET5_TYPE		GADGET_DATA_TYPE_BUTTON
#define EDITOR_GADGET5_FLAGS		(GADGET_DATA_FLAG_HOTKEY | GADGET_DATA_FLAG_BUTTON_TOGGLE)
#define EDITOR_GADGET5_LEFT_EDGE	EDITOR_GADGET2_LEFT_EDGE
#define EDITOR_GADGET5_TOP_EDGE		(EDITOR_GADGET2_TOP_EDGE + EDITOR_GADGET2_HEIGHT + 5)
#define EDITOR_GADGET5_WIDTH		EDITOR_GADGET2_WIDTH
#define EDITOR_GADGET5_HEIGHT		EDITOR_GADGET2_HEIGHT
#define EDITOR_GADGET5_TEXT		"_Delete"
#define EDITOR_GADGET5_TEXT_ATTR	&topaz80_attr

#define EDITOR_GADGET6_TYPE		GADGET_DATA_TYPE_BUTTON
#define EDITOR_GADGET6_FLAGS		(GADGET_DATA_FLAG_HOTKEY | GADGET_DATA_FLAG_BUTTON_TOGGLE)
#define EDITOR_GADGET6_LEFT_EDGE	EDITOR_GADGET3_LEFT_EDGE
#define EDITOR_GADGET6_TOP_EDGE		EDITOR_GADGET5_TOP_EDGE
#define EDITOR_GADGET6_WIDTH		EDITOR_GADGET2_WIDTH
#define EDITOR_GADGET6_HEIGHT		EDITOR_GADGET2_HEIGHT
#define EDITOR_GADGET6_TEXT		"_Edit"
#define EDITOR_GADGET6_TEXT_ATTR	&topaz80_attr

#define EDITOR_GADGET7_TYPE		GADGET_DATA_TYPE_BUTTON
#define EDITOR_GADGET7_FLAGS		(GADGET_DATA_FLAG_HOTKEY | GADGET_DATA_FLAG_BUTTON_TOGGLE)
#define EDITOR_GADGET7_LEFT_EDGE	EDITOR_GADGET4_LEFT_EDGE
#define EDITOR_GADGET7_TOP_EDGE		EDITOR_GADGET5_TOP_EDGE
#define EDITOR_GADGET7_WIDTH		EDITOR_GADGET2_WIDTH
#define EDITOR_GADGET7_HEIGHT		EDITOR_GADGET2_HEIGHT
#define EDITOR_GADGET7_TEXT		"_Use"
#define EDITOR_GADGET7_TEXT_ATTR	&topaz80_attr

#define EDITOR_GADGET8_TYPE		GADGET_DATA_TYPE_LISTVIEW
#define EDITOR_GADGET8_FLAGS		GADGET_DATA_FLAG_HOTKEY
#define EDITOR_GADGET8_LEFT_EDGE	10
#define EDITOR_GADGET8_TOP_EDGE		(EDITOR_GADGET1_TOP_EDGE + EDITOR_GADGET1_HEIGHT + 19)
#define EDITOR_GADGET8_WIDTH		242
#define EDITOR_GADGET8_HEIGHT		70
#define EDITOR_GADGET8_TEXT		"Defined Tem_plates"
#define EDITOR_GADGET8_TEXT_ATTR	&topaz80_attr
#define EDITOR_GADGET8_SPACING		0
#define EDITOR_GADGET8_TOP		1
#define EDITOR_GADGET8_LIST		NULL

	/* Statics & globals for editor main gadgets */

BYTE *template_type_text_array[] = {
	"Border", "Text", "Button", "Check", "Radio", "String", "Integer",
	"Slider", "Scroller", "Cycle", "Count", "Listview", "Palette", NULL
};
STATIC struct GadgetData  editor_gadget_data[] = {
   {
	EDITOR_GADGET1_TYPE,		/* gd_Type */
	EDITOR_GADGET1_FLAGS,		/* gd_Flags */
	EDITOR_GADGET1_LEFT_EDGE,	/* gd_LeftEdge */
	EDITOR_GADGET1_TOP_EDGE,	/* gd_TopEdge */
	EDITOR_GADGET1_WIDTH,		/* gd_Width */
	EDITOR_GADGET1_HEIGHT,		/* gd_Height */
	EDITOR_GADGET1_TEXT,		/* *gd_Text */
	EDITOR_GADGET1_TEXT_ATTR,	/* *gd_TextAttr */
	{
	0,				/* gd_CyclePad */
	EDITOR_GADGET1_ACTIVE,		/* gd_CycleActive */
	EDITOR_GADGET1_TEXT_ARRAY	/* gd_CycleTextArray */
	}
   }, {
	EDITOR_GADGET2_TYPE,		/* gd_Type */
	EDITOR_GADGET2_FLAGS,		/* gd_Flags */
	EDITOR_GADGET2_LEFT_EDGE,	/* gd_LeftEdge */
	EDITOR_GADGET2_TOP_EDGE,	/* gd_TopEdge */
	EDITOR_GADGET2_WIDTH,		/* gd_Width */
	EDITOR_GADGET2_HEIGHT,		/* gd_Height */
	EDITOR_GADGET2_TEXT,		/* *gd_Text */
	EDITOR_GADGET2_TEXT_ATTR,	/* *gd_TextAttr */
	{ 0, 0, 0 }
   }, {
	EDITOR_GADGET3_TYPE,		/* gd_Type */
	EDITOR_GADGET3_FLAGS,		/* gd_Flags */
	EDITOR_GADGET3_LEFT_EDGE,	/* gd_LeftEdge */
	EDITOR_GADGET3_TOP_EDGE,	/* gd_TopEdge */
	EDITOR_GADGET3_WIDTH,		/* gd_Width */
	EDITOR_GADGET3_HEIGHT,		/* gd_Height */
	EDITOR_GADGET3_TEXT,		/* *gd_Text */
	EDITOR_GADGET3_TEXT_ATTR,	/* *gd_TextAttr */
	{ 0, 0, 0 }
   }, {
	EDITOR_GADGET4_TYPE,		/* gd_Type */
	EDITOR_GADGET4_FLAGS,		/* gd_Flags */
	EDITOR_GADGET4_LEFT_EDGE,	/* gd_LeftEdge */
	EDITOR_GADGET4_TOP_EDGE,	/* gd_TopEdge */
	EDITOR_GADGET4_WIDTH,		/* gd_Width */
	EDITOR_GADGET4_HEIGHT,		/* gd_Height */
	EDITOR_GADGET4_TEXT,		/* *gd_Text */
	EDITOR_GADGET4_TEXT_ATTR,	/* *gd_TextAttr */
	{ 0, 0, 0 }
   }, {
	EDITOR_GADGET5_TYPE,		/* gd_Type */
	EDITOR_GADGET5_FLAGS,		/* gd_Flags */
	EDITOR_GADGET5_LEFT_EDGE,	/* gd_LeftEdge */
	EDITOR_GADGET5_TOP_EDGE,	/* gd_TopEdge */
	EDITOR_GADGET5_WIDTH,		/* gd_Width */
	EDITOR_GADGET5_HEIGHT,		/* gd_Height */
	EDITOR_GADGET5_TEXT,		/* *gd_Text */
	EDITOR_GADGET5_TEXT_ATTR,	/* *gd_TextAttr */
	{ 0, 0, 0 }
   }, {
	EDITOR_GADGET6_TYPE,		/* gd_Type */
	EDITOR_GADGET6_FLAGS,		/* gd_Flags */
	EDITOR_GADGET6_LEFT_EDGE,	/* gd_LeftEdge */
	EDITOR_GADGET6_TOP_EDGE,	/* gd_TopEdge */
	EDITOR_GADGET6_WIDTH,		/* gd_Width */
	EDITOR_GADGET6_HEIGHT,		/* gd_Height */
	EDITOR_GADGET6_TEXT,		/* *gd_Text */
	EDITOR_GADGET6_TEXT_ATTR,	/* *gd_TextAttr */
	{ 0, 0, 0 }
   }, {
	EDITOR_GADGET7_TYPE,		/* gd_Type */
	EDITOR_GADGET7_FLAGS,		/* gd_Flags */
	EDITOR_GADGET7_LEFT_EDGE,	/* gd_LeftEdge */
	EDITOR_GADGET7_TOP_EDGE,	/* gd_TopEdge */
	EDITOR_GADGET7_WIDTH,		/* gd_Width */
	EDITOR_GADGET7_HEIGHT,		/* gd_Height */
	EDITOR_GADGET7_TEXT,		/* *gd_Text */
	EDITOR_GADGET7_TEXT_ATTR,	/* *gd_TextAttr */
	{ 0, 0, 0 }
   }, {
	EDITOR_GADGET8_TYPE,		/* gd_Type */
	EDITOR_GADGET8_FLAGS,		/* gd_Flags */
	EDITOR_GADGET8_LEFT_EDGE,	/* gd_LeftEdge */
	EDITOR_GADGET8_TOP_EDGE,	/* gd_TopEdge */
	EDITOR_GADGET8_WIDTH,		/* gd_Width */
	EDITOR_GADGET8_HEIGHT,		/* gd_Height */
	EDITOR_GADGET8_TEXT,		/* *gd_Text */
	EDITOR_GADGET8_TEXT_ATTR,	/* *gd_TextAttr */
	{
	EDITOR_GADGET8_SPACING,		/* gd_ListViewSpacing */
	EDITOR_GADGET8_TOP,		/* gd_ListViewTop */
	EDITOR_GADGET8_LIST		/* gd_ListViewList */
	}
   }, {
	INTUISUP_DATA_END		/* mark end of gadget data array */
   }
};
	/* Defines for editor texts */

#define EDITOR_TEXT1_TYPE	TEXT_DATA_TYPE_TEXT
#define EDITOR_TEXT1_FLAGS	TEXT_DATA_FLAG_PLACE_LEFT
#define EDITOR_TEXT1_LEFT_EDGE	(EDITOR_GADGET2_LEFT_EDGE - 8)
#define EDITOR_TEXT1_TOP_EDGE	(EDITOR_GADGET2_TOP_EDGE + (EDITOR_GADGET2_HEIGHT - 8) / 2)
#define EDITOR_TEXT1_TEXT	"Mode"
#define EDITOR_TEXT1_TEXT_ATTR	&topaz80_attr

#define EDITOR_TEXT2_TYPE	TEXT_DATA_TYPE_TEXT
#define EDITOR_TEXT2_FLAGS	TEXT_DATA_FLAG_BOLD
#define EDITOR_TEXT2_LEFT_EDGE	EDITOR_GADGET5_LEFT_EDGE
#define EDITOR_TEXT2_TOP_EDGE	(EDITOR_GADGET5_TOP_EDGE + EDITOR_GADGET5_HEIGHT + 10)
#define EDITOR_TEXT2_TEXT	"Template Information:"
#define EDITOR_TEXT2_TEXT_ATTR	&topaz80_attr

#define EDITOR_TEXT3_TYPE	TEXT_DATA_TYPE_TEXT
#define EDITOR_TEXT3_FLAGS	TEXT_DATA_FLAG_PLACE_LEFT
#define EDITOR_TEXT3_LEFT_EDGE	(EDITOR_TEXT2_LEFT_EDGE + 20 + 4 * 8)
#define EDITOR_TEXT3_TOP_EDGE	(EDITOR_TEXT2_TOP_EDGE + 14)
#define EDITOR_TEXT3_TEXT	"Name"
#define EDITOR_TEXT3_TEXT_ATTR	&topaz80_attr

#define EDITOR_TEXT4_TYPE	TEXT_DATA_TYPE_TEXT
#define EDITOR_TEXT4_FLAGS	TEXT_DATA_FLAG_PLACE_LEFT
#define EDITOR_TEXT4_LEFT_EDGE	EDITOR_TEXT3_LEFT_EDGE
#define EDITOR_TEXT4_TOP_EDGE	(EDITOR_TEXT3_TOP_EDGE + 12)
#define EDITOR_TEXT4_TEXT	"Type"
#define EDITOR_TEXT4_TEXT_ATTR	&topaz80_attr

#define EDITOR_TEXT5_TYPE	TEXT_DATA_TYPE_TEXT
#define EDITOR_TEXT5_FLAGS	TEXT_DATA_FLAG_PLACE_LEFT
#define EDITOR_TEXT5_LEFT_EDGE	EDITOR_TEXT4_LEFT_EDGE
#define EDITOR_TEXT5_TOP_EDGE	(EDITOR_TEXT4_TOP_EDGE + 12)
#define EDITOR_TEXT5_TEXT	"Pos"
#define EDITOR_TEXT5_TEXT_ATTR	&topaz80_attr

#define EDITOR_TEXT6_TYPE	TEXT_DATA_TYPE_TEXT
#define EDITOR_TEXT6_FLAGS	TEXT_DATA_FLAG_PLACE_LEFT
#define EDITOR_TEXT6_LEFT_EDGE	EDITOR_TEXT5_LEFT_EDGE
#define EDITOR_TEXT6_TOP_EDGE	(EDITOR_TEXT5_TOP_EDGE + 12)
#define EDITOR_TEXT6_TEXT	"Size"
#define EDITOR_TEXT6_TEXT_ATTR	&topaz80_attr

	/* Statics for texts */

STATIC struct TextData  editor_text_data[] = {
   {
	EDITOR_TEXT1_TYPE,	/* td_Type */
	EDITOR_TEXT1_FLAGS,	/* td_Flags */
	EDITOR_TEXT1_LEFT_EDGE,	/* td_LeftEdge */
	EDITOR_TEXT1_TOP_EDGE,	/* td_TopEdge */
	EDITOR_TEXT1_TEXT,	/* *td_Text */
	EDITOR_TEXT1_TEXT_ATTR	/* *td_TextAttr */
   }, {
	EDITOR_TEXT2_TYPE,	/* td_Type */
	EDITOR_TEXT2_FLAGS,	/* td_Flags */
	EDITOR_TEXT2_LEFT_EDGE,	/* td_LeftEdge */
	EDITOR_TEXT2_TOP_EDGE,	/* td_TopEdge */
	EDITOR_TEXT2_TEXT,	/* *td_Text */
	EDITOR_TEXT2_TEXT_ATTR	/* *td_TextAttr */
   }, {
	EDITOR_TEXT3_TYPE,	/* td_Type */
	EDITOR_TEXT3_FLAGS,	/* td_Flags */
	EDITOR_TEXT3_LEFT_EDGE,	/* td_LeftEdge */
	EDITOR_TEXT3_TOP_EDGE,	/* td_TopEdge */
	EDITOR_TEXT3_TEXT,	/* *td_Text */
	EDITOR_TEXT3_TEXT_ATTR	/* *td_TextAttr */
   }, {
	EDITOR_TEXT4_TYPE,	/* td_Type */
	EDITOR_TEXT4_FLAGS,	/* td_Flags */
	EDITOR_TEXT4_LEFT_EDGE,	/* td_LeftEdge */
	EDITOR_TEXT4_TOP_EDGE,	/* td_TopEdge */
	EDITOR_TEXT4_TEXT,	/* *td_Text */
	EDITOR_TEXT4_TEXT_ATTR	/* *td_TextAttr */
   }, {
	EDITOR_TEXT5_TYPE,	/* td_Type */
	EDITOR_TEXT5_FLAGS,	/* td_Flags */
	EDITOR_TEXT5_LEFT_EDGE,	/* td_LeftEdge */
	EDITOR_TEXT5_TOP_EDGE,	/* td_TopEdge */
	EDITOR_TEXT5_TEXT,	/* *td_Text */
	EDITOR_TEXT5_TEXT_ATTR	/* *td_TextAttr */
   }, {
	EDITOR_TEXT6_TYPE,	/* td_Type */
	EDITOR_TEXT6_FLAGS,	/* td_Flags */
	EDITOR_TEXT6_LEFT_EDGE,	/* td_LeftEdge */
	EDITOR_TEXT6_TOP_EDGE,	/* td_TopEdge */
	EDITOR_TEXT6_TEXT,	/* *td_Text */
	EDITOR_TEXT6_TEXT_ATTR	/* *td_TextAttr */
   }, {
	INTUISUP_DATA_END	/* mark end of text data array */
   }
};
	/* Defines for information texts */

#define INFO_TEXT_LINES		4

#define INFO_TEXT_LEFT_EDGE	(EDITOR_TEXT3_LEFT_EDGE + 8)
#define INFO_TEXT_TOP_EDGE	EDITOR_TEXT3_TOP_EDGE
#define INFO_TEXT_TYPE		TEXT_DATA_TYPE_TEXT
#define INFO_TEXT_FLAGS		TEXT_DATA_FLAG_COLOR2
#define INFO_TEXT_ATTR		EDITOR_TEXT3_TEXT_ATTR

#define INFO_TEXT_MAX_WIDTH	(EDITOR_WINDOW_WIDTH - INFO_TEXT_LEFT_EDGE)
#define INFO_TEXT_YOFFSET	(EDITOR_TEXT4_TOP_EDGE - EDITOR_TEXT3_TOP_EDGE)

#define INFO_LINE_NAME	0
#define INFO_LINE_TYPE	1
#define INFO_LINE_POS	2
#define INFO_LINE_SIZE	3

	/* Defines for menu */

#define MENU_TEXT_ATTR	&topaz60_attr

#define MENU_PROJECT				0
#define    ITEM_PROJECT_WINDOW			0
#define    ITEM_PROJECT_NEW			1
#define    ITEM_PROJECT_LOAD			2
#define    ITEM_PROJECT_APPEND			3
#define    ITEM_PROJECT_SAVE			4
#define    ITEM_PROJECT_SAVE_C			5
#define    ITEM_PROJECT_ABOUT			6
#define    ITEM_PROJECT_QUIT			7

#define MENU_EDIT				1
#define    ITEM_EDIT_LOCK			0		
#define    ITEM_EDIT_UNLOCK			1		
#define       SUBITEM_EDIT_UNLOCK_SINGLE	0		
#define       SUBITEM_EDIT_UNLOCK_ALL		1		
#define    ITEM_EDIT_SNAP			2		

	/* Statics for menu */

STATIC struct MenuData  editor_menu_data[] = {
   {
	MENU_DATA_TYPE_TITLE, 0, "Project"
   }, {
	   MENU_DATA_TYPE_ITEM, 0, "Window", "W", 0
   }, {
	   MENU_DATA_TYPE_ITEM, 0, "New", "N", 0
   }, {
	   MENU_DATA_TYPE_ITEM, 0, "Load", "L", 0
   }, {
	   MENU_DATA_TYPE_ITEM, 0, "Append", "P", 0
   }, {
	   MENU_DATA_TYPE_ITEM, 0, "Save", "S", 0
   }, {
	   MENU_DATA_TYPE_ITEM, 0, "Save as C", "C", 0
   }, {
	   MENU_DATA_TYPE_ITEM, 0, "About", "A", 0
   }, {
	   MENU_DATA_TYPE_ITEM, MENU_DATA_FLAG_EMPTY_LINE, "Quit", "Q", 0
   }, {
	MENU_DATA_TYPE_TITLE, 0, "Edit"
   }, {
	   MENU_DATA_TYPE_ITEM, MENU_DATA_FLAG_DISABLED, "Lock", "L", 0
   }, {
	   MENU_DATA_TYPE_ITEM, MENU_DATA_FLAG_DISABLED, "Unlock", NULL, 0
   }, {
	      MENU_DATA_TYPE_SUBITEM, 0, "Single", "U", 0
   }, {
	      MENU_DATA_TYPE_SUBITEM, 0, "All", NULL, 0
   }, {
	   MENU_DATA_TYPE_ITEM, 0, "Snap", NULL, 0
   }, {
	      MENU_DATA_TYPE_SUBITEM, MENU_DATA_FLAG_ATTRIBUTE | MENU_DATA_FLAG_SELECTED, "1", NULL, ~(1 << 0)
   }, {
	      MENU_DATA_TYPE_SUBITEM, MENU_DATA_FLAG_ATTRIBUTE, "2", NULL, ~(1 << 1)
   }, {
	      MENU_DATA_TYPE_SUBITEM, MENU_DATA_FLAG_ATTRIBUTE, "4", NULL, ~(1 << 2)
   }, {
	      MENU_DATA_TYPE_SUBITEM, MENU_DATA_FLAG_ATTRIBUTE, "6", NULL, ~(1 << 3)
   }, {
	      MENU_DATA_TYPE_SUBITEM, MENU_DATA_FLAG_ATTRIBUTE, "8", NULL, ~(1 << 4)
   }, {
	      MENU_DATA_TYPE_SUBITEM, MENU_DATA_FLAG_ATTRIBUTE, "10", NULL, ~(1 << 5)
   }, {
	      MENU_DATA_TYPE_SUBITEM, MENU_DATA_FLAG_ATTRIBUTE, "12", NULL, ~(1 << 6)
   }, {
	      MENU_DATA_TYPE_SUBITEM, MENU_DATA_FLAG_ATTRIBUTE, "14", NULL, ~(1 << 7)
   }, {
	      MENU_DATA_TYPE_SUBITEM, MENU_DATA_FLAG_ATTRIBUTE, "16", NULL, ~(1 << 8)
   }, {
	      MENU_DATA_TYPE_SUBITEM, MENU_DATA_FLAG_ATTRIBUTE, "18", NULL, ~(1 << 9)
   }, {
	      MENU_DATA_TYPE_SUBITEM, MENU_DATA_FLAG_ATTRIBUTE, "20", NULL, ~(1 << 10)
   }, {
	INTUISUP_DATA_END	/* mark end of menu data */
   }
};
	/* Editor action loop */

   SHORT
editor_action_loop(VOID)
{
   BOOL  keepon = TRUE;
   SHORT status = EDITOR_STATUS_NORMAL;

   if (!(egl = ICreateGadgets(eri, &editor_gadget_data[0], 0, 0, NULL))) {
      status = EDITOR_ERROR_OUT_OF_MEM;
   } else {
      if (!(eml = ICreateMenu(eri, ewin, &editor_menu_data[0],
						   MENU_TEXT_ATTR, NULL))) {
	 status = EDITOR_ERROR_OUT_OF_MEM;
      } else {
	 struct TemplateList  *tl = &template_list;
	 struct MsgPort       *eup = ewin->UserPort,
			      *pup = pwin->UserPort;
	 LONG emask = 1L << eup->mp_SigBit, pmask = 1L << pup->mp_SigBit,
	      full_mask = emask | pmask;

	 /* Init file requesters */
	 project_file_requester->fr_Flags2 |= FR2F_LongPath;
	 strcpy(project_file_requester->fr_File, DEFAULT_PROJECT_FILE_NAME);

	 csource_file_requester->fr_FuncFlags |= FRF_DoColor;
	 csource_file_requester->fr_Flags2    |= FR2F_LongPath;
	 csource_file_requester->fr_Hail       = CSOURCE_SAVE_HAIL_TEXT;
	 strcpy(csource_file_requester->fr_File, DEFAULT_CSOURCE_FILE_NAME);

	 /* Init template list */
	 NewList((struct List *)&tl->tl_Fonts);
	 NewList((struct List *)&tl->tl_Templates);
	 strcpy(&tl->tl_ProjectID[0], DEFAULT_PROJECT_ID);
	 change_project_name(tl, DEFAULT_PROJECT_NAME, -1);

	 /* Set gadget defaults */
	 ISetGadgetAttributes(egl, EDITOR_GADGET_TYPE, 0L, 0L,
			     USE_CURRENT_VALUE, (LONG)DEFAULT_TEMPLATE_TYPE,
						 (VOID *)USE_CURRENT_VALUE);
	 ISetGadgetAttributes(egl, FIRST_MODE_GADGET + DEFAULT_EDITOR_MODE,
		  0L, 0L, 1L, USE_CURRENT_VALUE, (VOID *)USE_CURRENT_VALUE);
	 print_project_window_title();

	 /* Editor main loop */
	 do {
	    struct Message  *msg;
	    SHORT last_status;

	    /* Remove all IDCMP events from editor and project window */
	    while (msg = GetMsg(eup)) {
	       ReplyMsg(msg);
	    }
	    while (msg = GetMsg(pup)) {
	       ReplyMsg(msg);
	    }

	    /* Display editor texts and gadgets, start use mode and attach menu */
	    IClearWindow(eri, ewin, 0, 0, EDITOR_WINDOW_WIDTH,
						   EDITOR_WINDOW_HEIGHT, 0);
	    IDisplayTexts(eri, ewin, &editor_text_data[0], 0, 0, NULL);
	    IDisplayGadgets(ewin, egl);
	    IAttachMenu(ewin, eml);
	    if (editor_mode == EDITOR_MODE_USE) {
	       start_gadget_use_mode();
	    }

	    /* Editor IDCMP loop */
	    do {
	       LONG signals = Wait(full_mask);

	       if (signals & emask) {
		  status = perform_editor_action();
	       }
	       if (signals & pmask) {
		  status = perform_project_action();
	       }
	    } while (status <= EDITOR_STATUS_NORMAL);

	    /* Remove editor gadgets, end gadget use mode and remove menu */
	    IRemoveGadgets(egl);
	    if (editor_mode == EDITOR_MODE_USE) {
	       end_gadget_use_mode();
	    }
	    IRemoveMenu(eml);

	    /* Perform special action given by status */
	    switch (last_status = status) {
	       case EDITOR_STATUS_EDIT :
		  if (selected_template) {
		     status = edit_template_data();
		  }
		  break;

	       case EDITOR_STATUS_WINDOW :
		  status = change_project_window();
		  break;

	       case EDITOR_STATUS_NEW :
		  if (!(tl->tl_Flags & TEMPLATE_LIST_FLAG_CHANGED) ||
			  ok_cancel_requester(" New ", "Project changed and"
					    " not saved.\\n\\nDo you really"
				 " want to start a new project?") == TRUE) {
		     status = new_project(tl,
					 TEMPLATE_LIST_FLAG_DEFAULT_WINDOW);
		  }
		  break;

	       case EDITOR_STATUS_LOAD :
		  if (!(tl->tl_Flags & TEMPLATE_LIST_FLAG_CHANGED) ||
			 ok_cancel_requester(" Load ", "Project changed and"
					    " not saved.\\n\\nDo you really"
				  " want to load a new project?") == TRUE) {
		     status = load_project(LOAD_MODE_NORMAL);
		  }
		  break;

	       case EDITOR_STATUS_APPEND :
		  if (!(tl->tl_Flags & TEMPLATE_LIST_FLAG_CHANGED) ||
		       ok_cancel_requester(" Append ", "Project changed and"
					    " not saved.\\n\\nDo you really"
				" want to append a new project?") == TRUE) {
		     status = load_project(LOAD_MODE_APPEND);
		  }
		  break;

	       case EDITOR_STATUS_SAVE :
		  status = save_project();
		  break;

	       case EDITOR_STATUS_SAVE_C :
		  status = save_c_source();
		  break;

	       case EDITOR_STATUS_ABOUT :
		  about_requester();
		  break;

	       case EDITOR_ERROR_NO_WINDOW :
		  keepon = FALSE;
		  break;
	    }
	    if (last_status == EDITOR_STATUS_WINDOW || last_status ==
		   EDITOR_STATUS_NEW || last_status == EDITOR_STATUS_LOAD) {

	       /* Get user port data from new project window */
	       pup       = pwin->UserPort;
	       pmask     = 1L << pup->mp_SigBit;
	       full_mask = emask | pmask;
	    }
	    if (status == EDITOR_STATUS_QUIT) {
	       if (!(tl->tl_Flags & TEMPLATE_LIST_FLAG_CHANGED) ||
		     ok_cancel_requester(" Quit ", "Project changed and not"
		      " saved.\\n\\nDo you really want to quit?") == TRUE) {
		  keepon = FALSE;
	       }
	    }
	 } while (keepon == TRUE);

	 /* Free resources */
	 free_template_list(tl);
	 IFreeMenu(eml);
      }
      IFreeGadgets(egl);
   }
   return(status);
}
	/* Perform editor action */

   STATIC SHORT
perform_editor_action(VOID)
{
   struct MsgPort       *up = ewin->UserPort;
   struct IntuiMessage  *msg;
   SHORT  status = EDITOR_STATUS_NORMAL;

   while (msg = IGetMsg(up)) {
      USHORT entry, code = msg->Code;
      LONG   value;

      switch (msg->Class) {
	 case CLOSEWINDOW :
	    status = EDITOR_STATUS_QUIT;
	    break;

	 case ISUP_ID :
	    entry = code;
	    value = (LONG)msg->IAddress;
	    switch (entry) {
	       case EDITOR_GADGET_TYPE :
		  template_type = value;
		  print_project_window_title();
		  break;

	       case EDITOR_GADGET_TEMPLATES :
		  clear_template_info();
		  if (select_template_by_num(value)) {
		     print_template_info();
		  }
		  break;

	       default :
		  change_editor_mode(entry, value);
		  break;
	    }
	    break;

	 case MENUPICK :
	    while (code != MENUNULL) {
	       switch (MENUNUM(code)) {
		  case MENU_PROJECT :
		     switch (ITEMNUM(code)) {
			case ITEM_PROJECT_WINDOW :
			   status = EDITOR_STATUS_WINDOW;
			   break;

			case ITEM_PROJECT_NEW :
			   status = EDITOR_STATUS_NEW;
			   break;

			case ITEM_PROJECT_LOAD :
			   status = EDITOR_STATUS_LOAD;
			   break;

			case ITEM_PROJECT_APPEND :
			   status = EDITOR_STATUS_APPEND;
			   break;

			case ITEM_PROJECT_SAVE :
			   status = EDITOR_STATUS_SAVE;
			   break;

			case ITEM_PROJECT_SAVE_C :
			   status = EDITOR_STATUS_SAVE_C;
			   break;

			case ITEM_PROJECT_ABOUT :
			   status = EDITOR_STATUS_ABOUT;
			   break;

			case ITEM_PROJECT_QUIT :
			   status = EDITOR_STATUS_QUIT;
			   break;
		     }
		     break;
		  case MENU_EDIT :
		     switch (ITEMNUM(code)) {
			case ITEM_EDIT_SNAP :
			   snap_offset = snap_offset_table[SUBNUM(code)];
			   break;
		     }
		     break;
	       }
	       code = IMenuItemAddress(eml, code)->NextSelect;
	    }
	    break;
      }
      IReplyMsg(msg);
   }
   return(status);
}
	/* Change editor mode */

   STATIC VOID
change_editor_mode(USHORT gadget, LONG value)
{
   UBYTE  new_mode = gadget - FIRST_MODE_GADGET;
   USHORT active_mode_gadget = editor_mode + FIRST_MODE_GADGET;

   if (gadget == active_mode_gadget) {

      /* If active mode gadget deselected so select it again */
      ISetGadgetAttributes(egl, gadget, 0L, 0L, 1L, USE_CURRENT_VALUE,
						 (VOID *)USE_CURRENT_VALUE);
   } else {

      /* Deselect active mode gadget and set new editor mode */
      ISetGadgetAttributes(egl, active_mode_gadget, 0L, 0L, 0L,
			      USE_CURRENT_VALUE, (VOID *)USE_CURRENT_VALUE);

      /* Toggle gadget use mode */
      if (new_mode == EDITOR_MODE_USE) {
	 start_gadget_use_mode();
      } else {
	 if (editor_mode == EDITOR_MODE_USE) {
	    end_gadget_use_mode();
	 }
      }
      editor_mode = new_mode;
      print_project_window_title();
   }
}
	/* Print infos about current selected template info */

   VOID
print_template_info(VOID)
{
   struct Template  *tp = selected_template;
   USHORT i, top_edge = INFO_TEXT_TOP_EDGE;

   /* First clear old info texts */
   if (info_displayed == TRUE) {
      IClearWindow(eri, ewin, INFO_TEXT_LEFT_EDGE, INFO_TEXT_TOP_EDGE,
		 INFO_TEXT_MAX_WIDTH, INFO_TEXT_TOP_EDGE + INFO_TEXT_LINES *
						  INFO_TEXT_YOFFSET - 1, 0);
      draw_box_with_border(pwin, &info_template->tp_Box);
   }

   /* Now print new info texts */
   for (i = 0; i < INFO_TEXT_LINES; i++) {
      BYTE text[80];

      switch (i) {
	 case INFO_LINE_NAME :
	    SPrintf(&text[0], "%s", &tp->tp_TemplateName[0]);
	    break;

	 case INFO_LINE_TYPE :
	    SPrintf(&text[0], "%s", template_type_text_array[tp->tp_Type]);
	    break;

	 case INFO_LINE_POS :
	    SPrintf(&text[0], "%d,%d", tp->tp_Box.bo_X1, tp->tp_Box.bo_Y1);
	    break;

	 case INFO_LINE_SIZE :
	    SPrintf(&text[0], "%dx%d", tp->tp_Box.bo_X2 - tp->tp_Box.bo_X1 +
				1, tp->tp_Box.bo_Y2 - tp->tp_Box.bo_Y1 + 1);
	    break;
      }
      IPrintText(eri, ewin, &text[0], INFO_TEXT_LEFT_EDGE, top_edge,
			   INFO_TEXT_TYPE, INFO_TEXT_FLAGS, INFO_TEXT_ATTR);
      top_edge += INFO_TEXT_YOFFSET;
   }
   draw_box_with_border(pwin, &tp->tp_Box);
   info_template  = tp;
   info_displayed = TRUE;
}
	/* Clear template info */

   VOID
clear_template_info(VOID)
{
   if (info_displayed == TRUE) {
      IClearWindow(eri, ewin, INFO_TEXT_LEFT_EDGE, INFO_TEXT_TOP_EDGE,
		 INFO_TEXT_MAX_WIDTH, INFO_TEXT_TOP_EDGE + INFO_TEXT_LINES *
						  INFO_TEXT_YOFFSET - 1, 0);
      draw_box_with_border(pwin, &info_template->tp_Box);
      info_template  = NULL;
      info_displayed = FALSE;
   }
}
	/* Start gadget use mode */

   VOID
start_gadget_use_mode(VOID)
{
   struct TemplateList  *tl = &template_list;
   USHORT num = tl->tl_GadgetTemplates;
   SHORT  status = EDITOR_STATUS_NORMAL;

   if (num) {
      struct GadgetData  *gd;

      if (!(use_gd = gd = AllocMem((LONG)(num + 1) *
			   sizeof(struct GadgetData), (LONG)MEMF_PUBLIC))) {
	 status = EDITOR_ERROR_OUT_OF_MEM;
      } else {
	 struct Template  *tp;

	 for (tp = get_head((struct List *)&tl->tl_Templates); tp;
					      tp = get_succ(&tp->tp_Node)) {
	    if (TEMPLATE_GROUP(tp) == TEMPLATE_GROUP_GADGET) {
	       CopyMem((BYTE *)&tp->tp_Data.tp_GadgetData, (BYTE *)gd++,
					   (LONG)sizeof(struct GadgetData));
	    }
	 }
	 gd->gd_Type = INTUISUP_DATA_END;   /* mark end of gadget data array */
	 if (!(use_gl = ICreateGadgets(pri, use_gd, 0, 0, NULL))) {
	    FreeMem(use_gd, (LONG)(num + 1) * sizeof(struct GadgetData));
	    status = EDITOR_ERROR_OUT_OF_MEM;
         } else {
	    IDisplayGadgets(pwin, use_gl);
         }
      }
      show_error(status);
   }
}
	/* End gadget use mode */

   VOID
end_gadget_use_mode(VOID)
{
   USHORT num = template_list.tl_GadgetTemplates;

   if (num) {
      IRemoveGadgets(use_gl);
      IFreeGadgets(use_gl);
      FreeMem(use_gd, (LONG)(num + 1) * sizeof(struct GadgetData));
   }
}
