#ifndef MGET_LISTVIEW2_C
#define MGET_LISTVIEW2_C

/*
 *  Source machine generated by GadToolsBox V2.0b
 *  which is (c) Copyright 1991-1993 Jaba Development
 *
 *  GUI Designed by : Jeremy Friesner
 */

#include <exec/types.h>
#include <exec/libraries.h>
#include <intuition/intuition.h>g
#include <intuition/classes.h>
#include <intuition/classusr.h>
#include <intuition/imageclass.h>
#include <intuition/gadgetclass.h>
#include <libraries/gadtools.h>
#include <graphics/displayinfo.h>
#include <graphics/gfxbase.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/graphics_protos.h>
#include <clib/utility_protos.h>
#include <clib/wb_protos.h>

#include <workbench/workbench.h>
#include <workbench/startup.h>
#include <string.h>

#include "mget_listview2.h"
#include "Gadmget.h"

#define BACKSPACE_KEYCODE 8
#define UP_KEYCODE        76
#define DOWN_KEYCODE      77
#define LEFT_KEYCODE      79
#define RIGHT_KEYCODE     78

extern char pnamestring[];
extern struct List *filelist;
extern struct List *getlist;
extern struct Gadget *findgadlist;
extern void *findvi;
extern int appwindow;
extern char *pubscreenname;
extern int screentype;
extern UWORD windowwidth;
extern UWORD windowheight;
extern UWORD windowtop;
extern UWORD windowleft;
extern char windowtitle[];
extern int windowisopen;
extern int screenisopen;
extern char sListFontName[];
extern char sOutFileName[];
extern int nListFontSize;
extern FILE *fpOut;
extern struct TextFont *listfontdata;  /* pointer to font data */
extern struct GfxBase *GraphicsBase;   /* needed to determine default system font */
extern BOOL BStartedFromWB;
extern int nTopListTop;
extern int nTopListHeight;
extern int nBottomListHeight;
extern int filelist_length;
extern int getlist_length;
	
int screenwidth,screenheight;

struct Screen        *Scr = NULL;
APTR                 VisualInfo = NULL;
struct Window        *mgetWnd = NULL;
struct Window  	     *findwindow = NULL;
struct AppWindow     *appwin = NULL;
struct AppMessage    *amsg = NULL;
struct MsgPort 	     *awport = NULL;
struct Gadget        *mgetGList = NULL;
struct Menu          *mgetMenus = NULL;
struct IntuiMessage  mgetMsg;
struct Gadget        *mgetGadgets[2];
UWORD                mgetLeft = 0;
UWORD                mgetTop = 12;
UWORD lastcode;
ULONG appwinsig = 0L;

/* If non-negative, these were the pre-iconify values */
int nPrevLeft = -1, nPrevTop = -1, nPrevWidth = -1, nPrevHeight = -1;

struct TextAttr topaz8 = {
	( STRPTR )"topaz.font", 8, 0x00, 0x00 };

struct TextAttr listfont = {
	NULL, 0 , 0x00, 0x00 };

/* Used right-amiga key letters:

out of: ABCDEFGHIJKLMNOPQRSTUVWXYZ
used:   ABCDEFG IJKLMNOPQRSTUVWXY

*/
struct NewMenu mgetNewMenu[] = {
 NM_TITLE, "Project", 		NULL, 	0L, 	 NULL, NULL,
 NM_ITEM, "Open...", 		"O", 	0L, 	 0L, (APTR)mgetopenfile,
 NM_ITEM, "Append", 		"A", 	0L, 	 0L, (APTR)mgetappend,
 NM_ITEM, "Save",		"S",	0L,	 0L, (APTR)mgetsave, 
 NM_ITEM, "Save As...",		"W",	0L,	 0L, (APTR)mgetsaveas, 
 NM_ITEM, "Clear",             	NULL, 	0L,      NULL, NULL,
 NM_SUB,  "Files List",       	"U",  	0L,      0L, (APTR)mgetclearfiles,
 NM_SUB,  "Get List",         	"G",  	0L,      0L, (APTR)mgetclearget,
 NM_ITEM, "About", 		NULL, 	0L, 	 0L, (APTR)mgetabout,
 NM_ITEM, (STRPTR)NM_BARLABEL, 	NULL, 	0L, 	 0L, NULL,
 NM_ITEM, "Iconify",		"I", 	0L, 	 0L, (APTR)mgeticonify,
 NM_ITEM, "Output & Quit", 	"X", 	0L, 	 0L, (APTR)mgetquit,
 NM_ITEM, "Save & Quit",	"E", 	0L, 	 0L, (APTR)mgetmarkexit,
 NM_ITEM, "Quit", 		"Q", 	0L, 	 0L, (APTR)mgetfastquit,
 NM_TITLE,"Find", 		NULL, 	0L, 	 NULL, NULL,
 NM_ITEM, "Find Forward",	"F", 	0L, 	 0L, (APTR)mgetfind,
 NM_ITEM, "Find Backward", 	"K", 	0L, 	 0L, (APTR)mgetfindback,
 NM_ITEM, "Find from Top", 	"T", 	0L, 	 0L, (APTR)mgetfindtop,
 NM_ITEM, "Find from Bottom", 	"B", 	0L, 	 0L, (APTR)mgetfindbottom,
 NM_ITEM, "Find Next", 		"N", 	0L, 	 0L, (APTR)mgetfindnext,
 NM_ITEM, "Find Previous", 	"P", 	0L, 	 0L, (APTR)mgetfindprevious, 
 NM_ITEM, "Find Current",	"J",  	0L,	 0L, (APTR)mgetfindcurrent,
 NM_ITEM, "Select Multiple",   	"M",  	0L,	 NULL, (APTR)mgetmultiselect,
 NM_ITEM, "Deselect Multiple",	"D",  	0L,	 NULL, (APTR)mgetmultideselect,
 NM_ITEM, "Case Sensitive", 	"C", 	CHECKIT, 0L, (APTR)mgetcase,
 NM_TITLE,"Sort", 		NULL, 	0L, 	 NULL, NULL,
 NM_ITEM, "Files List", 	NULL, 	0L, 	 NULL, NULL,
 NM_SUB,  "By FileName", 	"!", 	0L, 	 NULL, (APTR)mgetfilesbyname,
 NM_SUB,  "By Directory", 	"@", 	0L, 	 NULL, (APTR)mgetfilesbydir,
 NM_SUB,  "By Size", 		"#", 	0L, 	 NULL, (APTR)mgetfilesbysize,
 NM_SUB,  "By Age",		"$",	0L,	 NULL, (APTR)mgetfilesbyage,
 NM_SUB,  "By Description", 	"%", 	0L, 	 NULL, (APTR)mgetfilesbydesc,
 NM_SUB,  "Keep Sorted", 	"-", 	CHECKIT, 0L, (APTR) mgetfileskeep,
 NM_ITEM, "Get List", 		NULL, 	0L, 	 NULL, NULL,
 NM_SUB,  "By FileName", 	"1", 	0L, 	 NULL, (APTR)mgetgetbyname,
 NM_SUB,  "By Directory", 	"2", 	0L, 	 NULL, (APTR)mgetgetbydir,
 NM_SUB,  "By Size", 		"3", 	0L, 	 NULL, (APTR)mgetgetbysize,
 NM_SUB,  "By Age",		"4",	0L,	 NULL, (APTR)mgetgetbyage,
 NM_SUB,  "By Description", 	"5", 	0L, 	 NULL, (APTR)mgetgetbydesc,
 NM_SUB,  "Keep Sorted", 	"=",	CHECKIT, 0L, (APTR) mgetgetkeep,
 NM_ITEM, "Sort Algorithm", 	NULL, 	0L, 	 NULL, NULL,
 NM_SUB,  "Tree Sort", 		NULL, 	CHECKIT, 0L, (APTR)mgettreesort,
 NM_SUB,  "Bubble Sort", 	NULL, 	CHECKIT, 0L, (APTR)mgetbubblesort,
 NM_ITEM, "Case Sensitive", 	"V", 	CHECKIT, 0L, (APTR)mgetsortcase,
 NM_TITLE,"Output", 		NULL, 	0L, 	 NULL, NULL,
 NM_ITEM, "Aminet Parse", 	NULL, 	CHECKIT, 0L, (APTR)mgetnoparse,
 NM_ITEM, "Get Files", 		NULL, 	CHECKIT, 0L, (APTR)mgetfiles,
 NM_ITEM, "Get Readmes", 	NULL, 	CHECKIT, 0L, (APTR)mgetreadmes,
 NM_ITEM, "Split Commands", 	NULL, 	CHECKIT, 0L, (APTR)mgetsplit,
 NM_ITEM, "Simple Paths", 	NULL, 	CHECKIT, 0L, (APTR)mgetCD,
 NM_ITEM, "Line Length...", 	"L", 	0, 	 0L, (APTR)mgetlinelength,
 NM_ITEM, "Output Now", 	"Y", 	0, 	 0L, (APTR)mgetoutput,
 NM_ITEM, "Output To...", 	"R", 	0, 	 0L, (APTR)mgetoutputfile,
 NM_END,  NULL, 		NULL, 	0, 	 0L, NULL };

struct NewGadget mgetNGad[] = {
	0, 0, 0, 0, (UBYTE *)"Selectable Files", NULL, 0, PLACETEXT_ABOVE, NULL, (APTR)listviewgadgetClicked,
	0, 0, 0, 0, (UBYTE *)"Selected Files",   NULL, 1, PLACETEXT_BELOW, NULL, (APTR)list2Clicked
};

ULONG mgetGTags[] = {
	(GT_Underscore), '_', 
	(TAG_DONE)
};

UWORD DriPens[] = {
	~0 };

int SetupScreen( void )
{
	ULONG lflag = 0L;
	struct Screen *sTemp = NULL;
	struct DrawInfo *screen_drawinfo = NULL;
	
	Scr = NULL;
		
	switch (screentype)
		{
		case USE_PUBSCREEN:
			Scr = LockPubScreen(pubscreenname);
			if (Scr == NULL)
				{
				MakeReq("Couldn't lock public screen!",NULL,"Huh?"); 
				return(1L);
				}
			screenwidth 	= Scr->Width;
			screenheight	= Scr->Height;
			break;
			
		case USE_CUSTOMSCREEN:
		 if ((sTemp = LockPubScreen("Workbench")) != NULL)
			{
			lflag = GetVPModeID(&(sTemp->ViewPort));	
			screenwidth  = sTemp->Width;
			screenheight = sTemp->Height;
			}
			else
			{
			screenwidth  = 640;
			screenheight = 400;
			lflag = HIRESLACE_KEY;
			}
		   
		 if (sTemp != NULL) UnlockPubScreen(NULL,sTemp);
			
		 if ( ! ( Scr = OpenScreenTags(NULL, 
				SA_Left,	0,
				SA_Top,		0,
				SA_Width,	screenwidth,
				SA_Height,	screenheight,
				SA_Depth,	2,
				SA_Font,	NULL /* &GraphicsBase->DefaultFont */,
				SA_Type,	CUSTOMSCREEN,
				SA_DisplayID,	lflag,
				SA_AutoScroll,	TRUE,
				SA_Overscan,	OSCAN_TEXT,
				SA_Pens,	&DriPens[0],
				SA_Title,	pnamestring,
				TAG_DONE ))) 
					return( 1L );
			break;
			
		case USE_WBSCREEN:
			Scr = LockPubScreen("Workbench");
			if (Scr == NULL) {MakeReq("Couldn't lock Workbench Screen!",NULL,"Where is it?"); return(1L);}
			screenwidth = Scr->Width;
			screenheight = Scr->Height;
			break;
		}
		
	if ( ! ( VisualInfo = GetVisualInfo( Scr, TAG_DONE )))
		return( 2L );

	screenisopen = 1;
	return( 0L );
}







void CloseDownScreen( void )
{
	if ( VisualInfo ) 
		{
		FreeVisualInfo( VisualInfo );
		VisualInfo = NULL;
		}

	switch(screentype)
		{
		case USE_CUSTOMSCREEN:
			CloseScreen( Scr );
			Scr = NULL;
			break;
		case USE_PUBSCREEN:
			UnlockPubScreen(NULL,Scr);
			break;
		case USE_WBSCREEN:
			UnlockPubScreen(NULL,Scr);
			break;
		}
	
	screenisopen = 0;		
}	









int HandlemgetIDCMP( struct IntuiMessage * overRideMsg )
{
	struct IntuiMessage	*m;
	struct MenuItem		*n;
	int			(*func)();
	int 	index1, index2, nMoveIncrement = 1;
	BOOL			running = TRUE, BMoveTopBottom = FALSE, BLeaveNow = FALSE;
	WORD		wtop = -1 , wleft = -1, wheight = -1 , wwidth= -1;
	
	struct NewGadget ng;
	struct Gadget	*g;
	int FilesWindowHeightLines = nTopListHeight/(listfont.ta_YSize);
	int GetWindowHeightLines = nBottomListHeight/(listfont.ta_YSize)-2;
	int nTemp;
	
	if (GetWindowHeightLines < 1) GetWindowHeightLines = 1;  /* a little less due to round-off error in Gadget */
	
	if (overRideMsg == NULL) m = GT_GetIMsg(mgetWnd->UserPort);
			    else m = overRideMsg;

	while( m ) {

		CopyMem(( char * )m, ( char * )&mgetMsg, (long)sizeof( struct IntuiMessage ));
		lastcode = m->Code;
		
		GT_ReplyIMsg( m );

		if ((mgetMsg.Qualifier & IEQUALIFIER_LSHIFT)||
			 (mgetMsg.Qualifier & IEQUALIFIER_RSHIFT)) nMoveIncrement = FilesWindowHeightLines;
			 
		if (mgetMsg.Qualifier & IEQUALIFIER_CONTROL)  nMoveIncrement = 10;
		
		if ((mgetMsg.Qualifier & IEQUALIFIER_LALT)||
		    (mgetMsg.Qualifier & IEQUALIFIER_RALT)) BMoveTopBottom = TRUE;
		
		switch ( mgetMsg.Class ) {
			case IDCMP_RAWKEY:
				/* printf("[%i:%c]\n", mgetMsg.Code, mgetMsg.Code); */
					
				if (BMoveTopBottom == FALSE)			
				{
					switch ( mgetMsg.Code ) {
						case UP_KEYCODE:
							nTemp = ListViewTop((struct Gadget *) mgetGadgets[0], filelist);
							if (nTemp > (nMoveIncrement-1)) nTemp -= nMoveIncrement; else nTemp = 0;
							GT_SetGadgetAttrs((struct Gadget *) mgetGadgets[0], mgetWnd, NULL, GTLV_Top, nTemp, TAG_END);				
							break;
						
						case DOWN_KEYCODE:
							GT_SetGadgetAttrs((struct Gadget *) mgetGadgets[0], mgetWnd, NULL, GTLV_Top, 
							 		ListViewTop((struct Gadget *) mgetGadgets[0], filelist) + nMoveIncrement, TAG_END);				
							break;
						
						case LEFT_KEYCODE:
							nTemp = ListViewTop((struct Gadget *) mgetGadgets[1], getlist);
							if (nMoveIncrement == FilesWindowHeightLines) nMoveIncrement = GetWindowHeightLines;
							if (nTemp > (nMoveIncrement-1)) nTemp -= nMoveIncrement; else nTemp = 0;
							GT_SetGadgetAttrs((struct Gadget *) mgetGadgets[1], mgetWnd, NULL, GTLV_Top, nTemp, TAG_END);				
							break;
		
						case RIGHT_KEYCODE:
							if (nMoveIncrement == FilesWindowHeightLines) nMoveIncrement = GetWindowHeightLines;
							GT_SetGadgetAttrs((struct Gadget *) mgetGadgets[1], mgetWnd, NULL, GTLV_Top, 
							 						ListViewTop((struct Gadget *) mgetGadgets[1], getlist) + nMoveIncrement
														, TAG_END);				
							break;				
						}
				}
				else
				{
					/* ALT held down--go to top or bottom of appropriate list */
					switch ( mgetMsg.Code ) {
						case UP_KEYCODE:
							GT_SetGadgetAttrs((struct Gadget *) mgetGadgets[0], mgetWnd, NULL, GTLV_Top, 0, TAG_END);				
							break;

						case DOWN_KEYCODE:
							GT_SetGadgetAttrs((struct Gadget *) mgetGadgets[0], mgetWnd, NULL, GTLV_Top, filelist_length, TAG_END);	
							break;

						case LEFT_KEYCODE:
							GT_SetGadgetAttrs((struct Gadget *) mgetGadgets[1], mgetWnd, NULL, GTLV_Top, 0, TAG_END);				
							break;

						case RIGHT_KEYCODE:
							GT_SetGadgetAttrs((struct Gadget *) mgetGadgets[1], mgetWnd, NULL, GTLV_Top, getlist_length, TAG_END);
							break;
					}
				}
				break;
				
			case IDCMP_VANILLAKEY:
				/* printf("{%i:%c}\n", mgetMsg.Code, mgetMsg.Code); */
								
				switch ( mgetMsg.Code ) {
					case ' ':
						GT_SetGadgetAttrs((struct Gadget *) mgetGadgets[0], mgetWnd, NULL, GTLV_Top, 
							ListViewTop((struct Gadget *) mgetGadgets[0], filelist) + FilesWindowHeightLines, TAG_END);
					break;
					
					case BACKSPACE_KEYCODE:
						nTemp = ListViewTop((struct Gadget *) mgetGadgets[0], filelist) - FilesWindowHeightLines;
						if (nTemp < 0) nTemp = 0;
						GT_SetGadgetAttrs((struct Gadget *) mgetGadgets[0], mgetWnd, NULL, GTLV_Top, nTemp, TAG_END);				
					break;					
				}
				break;
				
			case IDCMP_REFRESHWINDOW:
				GT_BeginRefresh( mgetWnd );
				GT_EndRefresh( mgetWnd, TRUE );
				break;

			case IDCMP_NEWSIZE:
				windowtop = mgetWnd->TopEdge;
				windowleft = mgetWnd->LeftEdge;
				windowheight = mgetWnd->Height;
				windowwidth = mgetWnd->Width;
				
				index1 = ListViewTop ((struct Gadget *) mgetGadgets[0], mgetWnd);
				index2 = ListViewTop ((struct Gadget *) mgetGadgets[1], mgetWnd);

				RemoveGList(mgetWnd, mgetGList, -1);
				FreeGadgets(mgetGList);
				
				EraseRect(mgetWnd->RPort,mgetWnd->BorderLeft, mgetWnd->BorderTop,
						    mgetWnd->Width - mgetWnd->BorderRight - 1,
						    mgetWnd->Height - mgetWnd->BorderBottom - 1);
						    
				if (SetupGadgets(g, &ng) == 2L) 
					{
					MakeReq("ListView Gadget Setup Error",NULL,"I wish Jeremy would debug better");
					CleanUp();
					exit(10);
					}
				AddGList(mgetWnd, mgetGList, -1, -1, NULL);
				RefreshGList(mgetGList, mgetWnd, NULL, -1);
						
				GT_RefreshWindow(mgetWnd, NULL);
				RefreshWindowFrame(mgetWnd);
				
				ChangeWindowTitle();				
				
				GT_SetGadgetAttrs((struct Gadget *) mgetGadgets[0], mgetWnd, NULL, GTLV_Labels, filelist, GTLV_Top, index1, TAG_END); 
				GT_SetGadgetAttrs((struct Gadget *) mgetGadgets[1], mgetWnd, NULL, GTLV_Labels, getlist, GTLV_Top, index2, TAG_END); 
				break;
				
			case	IDCMP_CLOSEWINDOW:
				running = mgetCloseWindow();
				break;

			case	IDCMP_GADGETUP:
			case	IDCMP_GADGETDOWN:
				func = ( void * )(( struct Gadget * )mgetMsg.IAddress )->UserData;
				running = func();
				break;

			case	IDCMP_MENUPICK:
				while( mgetMsg.Code != MENUNULL ) {
					n = ItemAddress( mgetMenus, mgetMsg.Code );
					func = (void *)(GTMENUITEM_USERDATA( n ));
					if (func == (int(*)()) &mgeticonify) BLeaveNow = TRUE;
					running = func();
					if (BLeaveNow == FALSE) mgetMsg.Code = n->NextSelect; else mgetMsg.Code = MENUNULL;
				}
				break;
		}
		if (BLeaveNow == FALSE) m = GT_GetIMsg( mgetWnd->UserPort ); else m = NULL;
	}
	return( running );
}





int SetupGadgets(struct Gadget *gadlist, struct NewGadget *ng)
{
 UWORD		lc, tc;
 UWORD		offx = Scr->WBorLeft, offy = Scr->WBorTop + Scr->RastPort.TxHeight + 1;
 UWORD		wheight = windowheight - offy;
 UWORD		wwidth  = windowwidth  - offx;
 UWORD		topedge[2], height[2];
 BYTE		   bSizeGadHeight = 8;		
 struct List	  *thislist = NULL; 
  		 
 topedge[0]    = offy;
 height[0]     = ((int) (.7 * wheight));
 topedge[1]    = topedge[0] + height[0] + 1;
 height[1]     = windowheight - topedge[1] - bSizeGadHeight - 2;

  /* rearrange sizes if height of lower list is too small */
  if (height[1] < nListFontSize * 3)
  {
  	height[1] = nListFontSize * 3;
  	topedge[1] = windowheight - height[1] - bSizeGadHeight - 2;
  	height[0] = topedge[1] - topedge[0];
  }
	
		   
  nTopListTop = topedge[0];
  nTopListHeight = height[0];
  nBottomListHeight = height[1];


  if ( ! ( gadlist = CreateContext( &mgetGList ))) return( 1L );

  for( lc = 0, tc = 0; lc < mget_CNT; lc++ ) 
  {	
     if (lc == 1) thislist = getlist; else thislist = filelist;
			
     CopyMem((char * )&mgetNGad[ lc ], (char * )ng, (long)sizeof( struct NewGadget ));

     ng->ng_VisualInfo = VisualInfo;
     ng->ng_TextAttr   = &listfont;
     ng->ng_LeftEdge   = offx;
     ng->ng_TopEdge    = topedge[lc];
     ng->ng_Width      = wwidth - offx;
     ng->ng_Height     = height[lc];	
     ng->ng_GadgetText = NULL;
     ng->ng_Flags      = NULL;
     ng->ng_GadgetID   = lc + 50;

     mgetGadgets[ lc ] = gadlist = CreateGadget(LISTVIEW_KIND, gadlist, ng, GTLV_Top, 0, TAG_END);
	
     while( mgetGTags[ tc ] ) tc += 2;

     tc++;

     if ( NOT gadlist) return( 2L );
  }
}









int OpenmgetWindow( void)
{
	struct NewGadget ng;
	struct Gadget	*g;
	int titleheight = (Scr->WBorTop + Scr->RastPort.TxHeight + 1);
	int nMinHeight;

 	/* Put in correct font */
 	listfont.ta_Name  = sListFontName;
 	if (nListFontSize > 0) listfont.ta_YSize = nListFontSize; else listfont.ta_YSize = 12;

 	/* a reasonable default, hopefully! */
 	listfontdata = OpenDiskFont(&listfont);	
 	if (listfontdata == NULL)
 	{
 		/* load system default font */
		AskFont(&Scr->RastPort,&listfont);
		listfontdata = OpenDiskFont(&listfont);
		if (listfontdata == NULL)
		{
			CleanUp();
			exit(5);
		}
		if (strlen(sListFontName) > 0) 
		{
			MakeReq("Couldn't find specified font, Sorry!",NULL,"Um, Ok");
			strncpy(sListFontName,NUL,1);
			nListFontSize = listfont.ta_YSize;
		}
	}	
	if (nListFontSize == 0) nListFontSize = listfont.ta_YSize;	/* get default setting */
	
	nMinHeight = (8 * (nListFontSize + 2));
	if (nMinHeight < 90) nMinHeight = 90;
	nMinHeight += titleheight;
  
	if ((windowwidth <= 0)||(windowwidth > screenwidth)) windowwidth  = screenwidth;
	if (windowheight < nMinHeight) windowheight = nMinHeight;
	
	if ((windowheight <= 0)||(windowheight > screenheight)) windowheight = screenheight - titleheight;
			
	if ((windowleft <= 0)||(windowleft > screenwidth))        windowleft   = 0;
	if ((windowleft+windowwidth) > screenwidth)	windowwidth -= windowleft;
	if ((windowtop <= 0)||(windowtop > screenheight))       windowtop    = titleheight;
	if ((windowtop+windowheight) > screenheight) windowheight -= windowtop;
		
	if ((windowwidth <= 0)||(windowwidth > screenwidth))    
		{
		windowwidth  = screenwidth;
		windowleft = 0;
		}
		
	if ((windowheight <= 0)||(windowheight > screenheight)) 
		{
		windowheight = screenheight - titleheight;
		windowtop = titleheight;
		}
		
	
	if (SetupGadgets(g, &ng) == 2L) return(2L);	
	
	if ( ! ( mgetMenus = CreateMenus( mgetNewMenu, GTMN_FrontPen, 0L, TAG_DONE )))
		return( 3L );
	
	LayoutMenus( mgetMenus, VisualInfo, TAG_DONE );	
		
	if (nPrevLeft > 0)
	{
		/* If non-negative, these were the pre-iconify values */
		windowleft = nPrevLeft; 
		windowtop = nPrevTop; 
		windowwidth = nPrevWidth;
		windowheight = nPrevHeight;
	}
	
	
	switch (screentype)
		{
		case USE_PUBSCREEN:
			if ( ! ( mgetWnd = OpenWindowTags( NULL,
				WA_Left,	windowleft,
				WA_Top,		windowtop,
				WA_Width,	windowwidth,
				WA_Height,	windowheight,
				WA_PubScreen,	Scr,
				WA_PubScreenFallBack, TRUE,
				WA_IDCMP,	LISTVIEWIDCMP|IDCMP_MENUPICK|IDCMP_CLOSEWINDOW|IDCMP_REFRESHWINDOW|IDCMP_NEWSIZE|IDCMP_RAWKEY|IDCMP_VANILLAKEY,
				WA_Flags,	WFLG_SIZEGADGET|WFLG_SIZEBBOTTOM|WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH|WFLG_ACTIVATE|WFLG_NEWLOOKMENUS,
				WA_Gadgets,	mgetGList,
				WA_Title,	windowtitle,
				WA_ScreenTitle,pnamestring,
				WA_MinWidth,	155,
				WA_MinHeight,	nMinHeight,
				WA_MaxWidth,	-1,
				WA_MaxHeight,	-1,
				TAG_DONE )))	
					return( 4L );
			break;
			
		case USE_CUSTOMSCREEN:
			if ( ! ( mgetWnd = OpenWindowTags( NULL,
				WA_Left,	windowleft,
				WA_Top,		windowtop,
				WA_Width,	windowwidth,
				WA_Height,	windowheight,
				WA_CustomScreen,Scr,
				WA_IDCMP,	LISTVIEWIDCMP|IDCMP_MENUPICK|IDCMP_CLOSEWINDOW|IDCMP_REFRESHWINDOW|IDCMP_NEWSIZE|IDCMP_RAWKEY|IDCMP_VANILLAKEY,
				WA_Flags,	WFLG_SIZEGADGET|WFLG_SIZEBBOTTOM|WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH|WFLG_ACTIVATE|WFLG_NEWLOOKMENUS|WFLG_NEWLOOKMENUS|WFLG_NEWLOOKMENUS|WFLG_NEWLOOKMENUS,
				WA_Gadgets,	mgetGList,
				WA_Title,	windowtitle,
				WA_ScreenTitle,pnamestring,
				WA_MinWidth,	155,
				WA_MinHeight,	nMinHeight,
				WA_MaxWidth,	-1,
				WA_MaxHeight,	-1,
				TAG_DONE )))	
					return( 4L );
			break;
			
		case USE_WBSCREEN:
			if ( ! ( mgetWnd = OpenWindowTags( NULL,
				WA_Left,	windowleft,
				WA_Top,		windowtop,
				WA_Width,	windowwidth,
				WA_Height,	windowheight,
				WA_IDCMP,	LISTVIEWIDCMP|IDCMP_MENUPICK|IDCMP_CLOSEWINDOW|IDCMP_REFRESHWINDOW|IDCMP_NEWSIZE|IDCMP_RAWKEY|IDCMP_VANILLAKEY,
				WA_Flags,	WFLG_SIZEGADGET|WFLG_SIZEBBOTTOM|WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH|WFLG_ACTIVATE|WFLG_NEWLOOKMENUS,
				WA_Gadgets,	mgetGList,
				WA_Title,	windowtitle,
				WA_ScreenTitle,pnamestring,
				WA_MinWidth,	155,
				WA_MinHeight,	nMinHeight,
				WA_MaxWidth,	-1,
				WA_MaxHeight,	-1,
				TAG_DONE )))	
					return( 4L );
					
			if (appwindow == 1)
			{
				awport = CreateMsgPort();
				if (awport == NULL) 
				{
					appwindow = 0;
					return(4L);
				}

				appwin = AddAppWindow(1, 0, mgetWnd, awport, NULL);
				
				if (appwin == NULL) {appwindow = 0;}
				  else appwinsig = 1L << awport->mp_SigBit;
			}	
			break;
		}	

	TurnOnAllMenuItems(mgetMenus);
	SetMenuStrip( mgetWnd, mgetMenus ); 
	GT_RefreshWindow( mgetWnd, NULL );
	windowisopen = 1;
return( 0L );
}







void ClosemgetWindow( void )
{
	/* safety first! */
	if (mgetWnd == NULL) return;
	
	/* Save values for de-iconify, maybe */
	nPrevLeft   = mgetWnd->LeftEdge; 
	nPrevTop    = mgetWnd->TopEdge; 
	nPrevWidth  = mgetWnd->Width; 
	nPrevHeight = mgetWnd->Height; 
	
	if ( mgetMenus      ) 
	{
		ClearMenuStrip( mgetWnd );
		FreeMenus( mgetMenus );
		mgetMenus = NULL;	
	}

	if ( findwindow     ) 
	{ 
	 	CloseWindow( findwindow );
	 	FreeGadgets(findgadlist);
	 	FreeVisualInfo(findvi);
	 	findwindow = NULL;
	}
 	
 	if (appwin) 
 	{
 		RemoveAppWindow(appwin);
 		
 		/* throw out any leftover messages */
 		while(amsg = (struct AppMessage *) GetMsg(awport))
 				ReplyMsg((struct Message *) amsg);
 		DeleteMsgPort(awport);
 	}
		
	if ( mgetWnd        ) 
	{
		CloseWindow( mgetWnd );
		windowisopen = 0;
		mgetWnd = NULL;
	}

	if ( mgetGList      ) 
	{
		FreeGadgets( mgetGList );
		mgetGList = NULL;
	}
}


#endif

