/***************************************************************************
 *                                                                         *
 *  Filename : BuildGUI.c                                                  *
 *                                                                         *
 *  Description : Routines for creating the program's GUI.                 *
 *                                                                         *
 ***************************************************************************
 *                                                                         *
 *                         Modification History                            *
 *                                                                         *
 *  Date      Author       Comments                                        *
 * ----------------------------------------------------------------------  *
 * 10.6.93    R.Bödi       Created.                                        *
 * 2.11.93    R.Bödi       Changed structure TeXDriver, see TeXPrt.h       *
 * 21.2.94    R.Bödi       Changed GUI building to MUI.                    *
 * 22.2.94    R.Bödi       Added AppMsgFunc() hook function for appwindow  *
 *                         message handling.                               *
 *                                                                         *
 ***************************************************************************
 *                                                                         *
 * Copyright © 1993 Richard Bödi,  All rights reserved.                    *
 *                                                                         *
 ***************************************************************************/

/*----------------------------- INCLUDES ----------------------------------*/

#include "TeXPrt.h"
#include "TexPrt_protos.h"
#include "TexPrt_globals.h"

/*----------------------------- DEFINES -----------------------------------*/

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

/*-------------------------- GLOBAL DATA ----------------------------------*/

static char	*STR_RAD_FromTo [] = 	{ 	"Whole text", 	"From/To", 	NULL 	};
static char	*STR_RAD_OddEven [] = 	{ 	"All", 	"Odd", 	"Even", 	NULL 	};
static char	*STR_RAD_Orientation [] = 	{ 	"Portrait", 	"Landscape", 	NULL 	};

static const struct Hook AppMsgHook = { { NULL,NULL },(void *)AppMsgFunc,NULL,NULL };

/*------------------------------- CODE ------------------------------------*/

/***************************************************************************
 *                                                                         *
 *  Function name : AppMsgFunc                                             *
 *                                                                         *
 *  Description : App message callback hook which is called when an icon   *
 *                is dropped on an MUI object.                             *
 *                                                                         *
 ***************************************************************************
 *                                                                         *
 *  Synopsis : AppMsgFunc (Object, AppMessage)                             *
 *                                                                         *
 *  Parameters :                                                           *
 *    (APTR) Object:                                                       *
 *       The MUI object which is affected by dropping the icon onto.       *
 *    (struct AppMessage **) AppMsg:                                       *
 *       The message from the icon which has beedn dropped on the object.  *
 *                                                                         *
 *  Return value : None                                                    *
 *                                                                         *
 ***************************************************************************/

void __saveds __asm AppMsgFunc (register __a2 APTR Object, register __a1 struct AppMessage **AppMsg)

{
int i;
struct WBArg        *WBArg;
struct AppMessage   *TempAppMsg = *AppMsg;
static char          Buffer[FMSIZE];

for (WBArg = TempAppMsg->am_ArgList, i = 0; i < TempAppMsg->am_NumArgs; i++, WBArg++)
   {
	NameFromLock (WBArg->wa_Lock, Buffer, sizeof (Buffer));
	AddPart (Buffer, WBArg->wa_Name, sizeof (Buffer));
   SetAttrs(Object, MUIA_String_Contents, Buffer, TAG_DONE);
   }
}


/***************************************************************************
 *                                                                         *
 *  Function name : BuildMUIApp                                            *
 *                                                                         *
 *  Description : Creates the MUI GUI.                                     *
 *                                                                         *
 ***************************************************************************
 *                                                                         *
 *  Synopsis : BuildMUIApp (Pointers)                                      *
 *                                                                         *
 *  Parameters :                                                           *
 *    (struct MUIPointers *) Pointers:                                     *
 *       A structure consisting of APTRs to MUI elements.                  *
 *    (struct NewMenu *) Menu:                                             *
 *       The window's menu.                                                *
 *                                                                         *
 *  Return value : (BOOL)                                                  *
 *      TRUE, if successful,                                               *
 *     FALSE, if not.                                                      *
 *                                                                         *
 ***************************************************************************/

BOOL  BuildMUIApp (struct MUIPointers *Pointers, struct NewMenu *Menu)

{
Pointers->app = ApplicationObject,
	MUIA_Application_Author, "Richard A. Bödi",
	MUIA_Application_Base, AppName,
	MUIA_Application_Title, AppName,
	MUIA_Application_Version, Version,
	MUIA_Application_Copyright, "© 1994 Richard A. Bödi",
	MUIA_Application_Description, "A front end for DVI printer drivers",
   MUIA_Application_Commands, ARexxCommandList,
	MUIA_Application_Menu, Menu,
	SubWindow, Pointers->MainWindow = WindowObject,
		MUIA_Window_Title, &Version[6],
		MUIA_Window_ID, (ULONG)(MAKE_ID ('T','e','X','P')),
   	MUIA_Window_AppWindow, TRUE,
		MUIA_HelpNode, "MainWindow",
		WindowContents, GroupObject,
			MUIA_Group_Horiz, 1,
			MUIA_Group_HorizSpacing, 10,
			MUIA_Group_VertSpacing, 10,
			Child, GroupObject,
				Child, GroupObject,
					GroupFrameT( "I/O" ),
					Child, GroupObject,
						MUIA_Group_Horiz, 1,
						Child, ColGroup( 2 ),
							Child, KeyLabel2("DVI File",'D'),
      					Child, Pointers->IMG_In = PopaslObject,
      						MUIA_Popstring_String, Pointers->STR_DVIFile = KeyString("",FMSIZE,'d'),
      						MUIA_Popstring_Button, PopButton(MUII_PopFile),
      						ASLFR_TitleText, "Please select a DVI file...",
      						End,
						End,
					End,
					Child, GroupObject,
						MUIA_Group_Horiz, 1,
						Child, ColGroup( 2 ),
							Child, KeyLabel2( "Output to",'O' ),
      					Child, Pointers->IMG_Out = PopaslObject,
      						MUIA_Popstring_String, Pointers->STR_OutFile = KeyString("",FMSIZE,'o'),
      						MUIA_Popstring_Button, PopButton(MUII_PopFile),
      						ASLFR_TitleText, "Please select an output file or a device...",
      						End,
						End,
					End,
				End,
				Child, GroupObject,
					GroupFrameT( "Page Selection" ),
					MUIA_Group_Horiz, 1,
					Child, GroupObject,
						Child, Pointers->RAD_FromTo = RadioObject,
							GroupFrameT( "" ),
							MUIA_Radio_Entries, STR_RAD_FromTo,
						End,
						Child, ColGroup( 2 ),
							Child, KeyLabel2( "From",'F' ),
							Child, Pointers->STR_From = StringObject,
								StringFrame,
								MUIA_ControlChar, 'f',
								MUIA_String_Contents, "1",
								MUIA_String_Accept, "-0123456789",
								MUIA_String_Format, 2,
							End,
						End,
						Child, ColGroup( 2 ),
							Child, KeyLabel2( "  To",'T' ),
							Child, Pointers->STR_To = StringObject,
								StringFrame,
								MUIA_ControlChar, 't',
								MUIA_String_Contents, "1",
								MUIA_String_Accept, "-0123456789",
								MUIA_String_Format, 2,
							End,
						End,
					End,
					Child, HVSpace,
					Child, GroupObject,
						Child, Pointers->RAD_OddEven = RadioObject,
							GroupFrameT( "" ),
							MUIA_Radio_Entries, STR_RAD_OddEven,
						End,
						Child, HVSpace,
						Child, ColGroup( 2 ),
							Child, KeyLabel2( "Copies",'C' ),
							Child, Pointers->STR_Copies = StringObject,
								StringFrame,
								MUIA_ControlChar, 'c',
								MUIA_String_Contents, "1",
								MUIA_String_Accept, "0123456789",
								MUIA_String_Format, 2,
							End,
						End,
					End,
				End,
				Child, GroupObject,
					GroupFrameT( "Page Setup" ),
					MUIA_Group_Horiz, 1,
					Child, GroupObject,
						Child, Pointers->RAD_Orientation = RadioObject,
							GroupFrameT( "Orientation" ),
							MUIA_Radio_Entries, STR_RAD_Orientation,
						End,
						Child, HVSpace,
					End,
					Child, GroupObject,
						GroupFrameT( "Offsets" ),
						Child, GroupObject,
							MUIA_Group_Horiz, 1,
							Child, ColGroup( 2 ),
								Child, KeyLabel2( "X",'X' ),
								Child, Pointers->STR_X = StringObject,
									StringFrame,
									MUIA_ControlChar, 'x',
									MUIA_String_Contents, "0",
									MUIA_String_Accept, "-0123456789",
									MUIA_String_Format, 2,
								End,
							End,
							Child, Pointers->TXT_X = TextObject,
								MUIA_Background, 128,
								MUIA_Text_SetMax, 1,
								MUIA_Text_SetMin, 1,
								MUIA_Frame, 0,
							End,
						End,
						Child, GroupObject,
							MUIA_Group_Horiz, 1,
							Child, ColGroup( 2 ),
								Child, KeyLabel2( "Y",'Y' ),
								Child, Pointers->STR_Y = StringObject,
									StringFrame,
									MUIA_ControlChar, 'y',
									MUIA_String_Contents, "0",
									MUIA_String_Accept, "-0123456789",
									MUIA_String_Format, 2,
								End,
							End,
							Child, Pointers->TXT_Y = TextObject,
								MUIA_Background, 128,
								MUIA_Text_SetMax, 1,
								MUIA_Text_SetMin, 1,
								MUIA_Frame, 0,
							End,
						End,
					End,
				End,
			End,
			Child, GroupObject,
				Child, GroupObject,
					GroupFrameT( "Options" ),
					MUIA_Group_Horiz, 1,
					Child, GroupObject,
						MUIA_Group_VertSpacing, 2,
						Child, Pointers->CHM_Option[0] = CheckMark( 0 ),
						Child, Pointers->CHM_Option[1] = CheckMark( 0 ),
						Child, Pointers->CHM_Option[2] = CheckMark( 0 ),
						Child, Pointers->CHM_Option[3] = CheckMark( 0 ),
						Child, Pointers->CHM_Option[4] = CheckMark( 0 ),
						Child, Pointers->CHM_Option[5] = CheckMark( 0 ),
						Child, Pointers->CHM_Option[6] = CheckMark( 0 ),
						Child, Pointers->CHM_Option[7] = CheckMark( 0 ),
						Child, Pointers->CHM_Option[8] = CheckMark( 0 ),
						Child, Pointers->CHM_Option[9] = CheckMark( 0 ),
					End,
					Child, GroupObject,
						MUIA_Group_VertSpacing, 5,
						Child, Pointers->TXT_Option[0] = TextObject,
							MUIA_Background, 128,
							MUIA_Text_SetMax, 1,
							MUIA_Text_SetMin, 1,
							MUIA_Frame, 0,
						End,
						Child, Pointers->TXT_Option[1] = TextObject,
							MUIA_Background, 128,
							MUIA_Text_SetMax, 1,
							MUIA_Text_SetMin, 1,
							MUIA_Frame, 0,
						End,
						Child, Pointers->TXT_Option[2] = TextObject,
							MUIA_Background, 128,
							MUIA_Text_SetMax, 1,
							MUIA_Text_SetMin, 1,
							MUIA_Frame, 0,
						End,
						Child, Pointers->TXT_Option[3] = TextObject,
							MUIA_Background, 128,
							MUIA_Text_SetMax, 1,
							MUIA_Text_SetMin, 1,
							MUIA_Frame, 0,
						End,
						Child, Pointers->TXT_Option[4] = TextObject,
							MUIA_Background, 128,
							MUIA_Text_SetMax, 1,
							MUIA_Text_SetMin, 1,
							MUIA_Frame, 0,
						End,
						Child, Pointers->TXT_Option[5] = TextObject,
							MUIA_Background, 128,
							MUIA_Text_SetMax, 1,
							MUIA_Text_SetMin, 1,
							MUIA_Frame, 0,
						End,
						Child, Pointers->TXT_Option[6] = TextObject,
							MUIA_Background, 128,
							MUIA_Text_SetMax, 1,
							MUIA_Text_SetMin, 1,
							MUIA_Frame, 0,
						End,
						Child, Pointers->TXT_Option[7] = TextObject,
							MUIA_Background, 128,
							MUIA_Text_SetMax, 1,
							MUIA_Text_SetMin, 1,
							MUIA_Frame, 0,
						End,
						Child, Pointers->TXT_Option[8] = TextObject,
							MUIA_Background, 128,
							MUIA_Text_SetMax, 1,
							MUIA_Text_SetMin, 1,
							MUIA_Frame, 0,
						End,
						Child, Pointers->TXT_Option[9] = TextObject,
							MUIA_Background, 128,
							MUIA_Text_SetMax, 1,
							MUIA_Text_SetMin, 1,
							MUIA_Frame, 0,
						End,
					End,
				End,
				Child, HVSpace,
				Child, GroupObject,
					GroupFrameT( "Operate" ),
					MUIA_Group_Horiz, 1,
					Child, Pointers->BUT_Quit = KeyButton( "Quit",'q' ),
					Child, Pointers->BUT_Print = KeyButton( "Print",'p' ),
				End,
			End,
		End,
	End,
End;

// Set callback hook for appwindow
DoMethod (Pointers->MainWindow, MUIM_Notify, MUIA_AppMessage, MUIV_EveryTime,
		    Pointers->STR_DVIFile, 3, MUIM_CallHook, &AppMsgHook, MUIV_TriggerValue);
// Set cycle chain for MUI objects
DoMethod (Pointers->MainWindow, MUIM_Window_SetCycleChain,
          Pointers->RAD_FromTo, Pointers->STR_From, Pointers->STR_To,
          Pointers->RAD_OddEven, Pointers->STR_Copies, Pointers->RAD_Orientation,
          Pointers->STR_X, Pointers->STR_Y, NULL);
// Connect the Print and Quit button as well as the closing gadget
DoMethod (Pointers->MainWindow, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, Pointers->app, 2, MUIM_Application_ReturnID, ID_QUIT);
DoMethod (Pointers->STR_From,   MUIM_Notify, MUIA_String_Contents, MUIV_EveryTime, Pointers->app, 2, MUIM_Application_ReturnID, ID_FROM_PAGE);
DoMethod (Pointers->STR_To,     MUIM_Notify, MUIA_String_Contents, MUIV_EveryTime, Pointers->app, 2, MUIM_Application_ReturnID, ID_TO_PAGE);
DoMethod (Pointers->BUT_Print,  MUIM_Notify, MUIA_Pressed, FALSE, Pointers->app, 2, MUIM_Application_ReturnID, ID_PRINT);
DoMethod (Pointers->BUT_Quit,   MUIM_Notify, MUIA_Pressed, FALSE, Pointers->app, 2, MUIM_Application_ReturnID, ID_QUIT);

return ((BOOL)(!set (Pointers->MainWindow, MUIA_Window_Open, TRUE)));
}



/***************************************************************************
 *                                                                         *
 *  Function name : BuildMenuStructure                                     *
 *                                                                         *
 *  Description : Allocates and builds a NewMenu structure according to    *
 *                the DVI printer drivers found by ScanDrivers.            *
 *                The environment variable TeXPrint is used to determine   *
 *                the default printer driver.                              *
 *                                                                         *
 ***************************************************************************
 *                                                                         *
 *  Synopsis : BuildMenuStructure (AvailDrivers, NewMenu)                  *
 *                                                                         *
 *  Parameters :                                                           *
 *       (struct AvailDrivers*) AvailDrivers :                             *
 *          Contains the names of the drivers found by ScanDrivers() in    *
 *          the DriverDrawer directory.                                    *
 *       (USHORT *) ActualDriver:                                          *
 *          Will be set to the number of the actual driver file as listed  *
 *          in the Driver menu.                                            *
 *                                                                         *
 *  Return value : (struct NewMenu *)                                      *
 *       Pointer to the NewMenu structure created or NULL if no memory     *
 *       was available.                                                    *
 *                                                                         *
 ***************************************************************************/

struct NewMenu   *BuildMenuStructure (struct AvailDrivers *AvailDrivers, USHORT *ActualDriver)

{
int             Item;
char            EnvVar[NAMELENGTH];
BOOL            EntryFound = FALSE;
struct NewMenu *NewMenu;


if (NewMenu = calloc (AvailDrivers->NoOfDrivers + STDITEMS + 2, sizeof (struct NewMenu)))
   {
   NewMenu[0].nm_Type      = NM_TITLE;
   NewMenu[0].nm_Label     = "Config";
   NewMenu[1].nm_Type      = NM_ITEM;
   NewMenu[1].nm_Label     = "Save";
   NewMenu[1].nm_CommKey   = "S";
   NewMenu[1].nm_UserData  = (APTR)ID_SAVE;
   NewMenu[2].nm_Type      = NM_ITEM;
   NewMenu[2].nm_Label     = "Save As";
   NewMenu[2].nm_CommKey   = "A";
   NewMenu[2].nm_UserData  = (APTR)ID_SAVEAS;
   NewMenu[3].nm_Type      = NM_ITEM;
   NewMenu[3].nm_Label     = NM_BARLABEL;
   NewMenu[4].nm_Type      = NM_ITEM;
   NewMenu[4].nm_Label     = "About";
   NewMenu[4].nm_UserData  = (APTR)ID_ABOUT;
   NewMenu[5].nm_Type      = NM_ITEM;
   NewMenu[5].nm_Label     = NM_BARLABEL;
   NewMenu[6].nm_Type      = NM_ITEM;
   NewMenu[6].nm_Label     = "Quit";
   NewMenu[6].nm_CommKey   = "Q";
   NewMenu[6].nm_UserData  = (APTR)ID_QUIT;
   NewMenu[7].nm_Type      = NM_TITLE;
   NewMenu[7].nm_Label     = "Drivers";

   GetVar (TeXPrintEnv, EnvVar, sizeof (EnvVar), 0);
   *ActualDriver = 0;

   for (Item = 0; Item < AvailDrivers->NoOfDrivers; Item++)
      {
      NewMenu[STDITEMS + 2 + Item].nm_Type           = NM_ITEM;
      NewMenu[STDITEMS + 2 + Item].nm_Label          = AvailDrivers->DriverNames + Item * NAMELENGTH;
      NewMenu[STDITEMS + 2 + Item].nm_Flags         |= CHECKIT;
      NewMenu[STDITEMS + 2 + Item].nm_MutualExclude  = ~(1 << Item);
      NewMenu[STDITEMS + 2 + Item].nm_UserData       = (APTR)(ID_DRIVER + Item);

      if ((!strcmp (EnvVar, NewMenu[STDITEMS + 2 + Item].nm_Label)) && (!EntryFound))
         {
         EntryFound = TRUE;
         NewMenu[STDITEMS + 2 + Item].nm_Flags |= CHECKED;
         *ActualDriver = Item;
         }
      }

   if (!EntryFound)
      NewMenu[STDITEMS + 2].nm_Flags |= CHECKED;

   NewMenu[STDITEMS + 2 + Item].nm_Type = NM_END;
   }

return (NewMenu);
}


/***************************************************************************
 *                                                                         *
 *  Function name : BuildAppIcon                                           *
 *                                                                         *
 *  Description : Creates an AppIcon.                                      *
 *                                                                         *
 ***************************************************************************
 *                                                                         *
 *  Synopsis : BuildAppIcon (AppMsgPort, AppIconObj, AppIcon, IconName)    *
 *                                                                         *
 *  Parameters :                                                           *
 *       (struct MsgPort **) AppMsgPort :                                  *
 *          The message port for arriving notifications on appicons and    *
 *          appwindows.                                                    *
 *       (struct DiskObject **) AppIconObj :                               *
 *          Icon represented as a DiskObject which specifies the appearance*
 *          of the AppIcon. AppIconName is used for the name of the icon   *
 *          file.                                                          *
 *       (struct AppIcon **) AppIcon :                                     *
 *          A pointer to AppIcon structure.                                *
 *          If NULL is supplied, no AppIcon will be created.               *
 *       (char *) IconName :                                               *
 *          A string pointer to the name of the AppIcon.                   *
 *                                                                         *
 *  Return value : (BOOL)                                                  *
 *       TRUE, if the AppIcon has been created.                            *
 *      FALSE, if not.                                                     *
 *                                                                         *
 ***************************************************************************/

BOOL  BuildAppIcon (struct MsgPort **AppMsgPort, struct DiskObject **AppIconObj,
                    struct AppIcon **AppIcon, char *IconName)

{
BOOL  Created = FALSE;
char  NameNode[FNSIZE];

if (*AppMsgPort = (CreateMsgPort ()))
   {
   if (AppIcon)
      {
      if (*AppIconObj = GetDiskObject (IconName))
         {
         (*AppIconObj)->do_Type = NULL;
         (*AppIconObj)->do_CurrentX = NO_ICON_POSITION;
         (*AppIconObj)->do_CurrentY = NO_ICON_POSITION;

         stcgfn (NameNode, IconName);

         if (*AppIcon = AddAppIconA (0, 0, NameNode, *AppMsgPort, NULL, *AppIconObj, NULL))
            Created = TRUE;
         else
            printf ("Couldn't create AppIcon.\n");
         }
      else
         PrintFault (IoErr (), "Couldn't access icon file for AppIcon.\n");
      }
   }
else
   printf ("Couldn't create message port for AppIcon/AppWindow.\n");

return (Created);
}


/*---------------------------- END OF FILE --------------------------------*/
