/*
 * Econ - Release 1 - Common header includes/defines/etc...
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

#include <exec/libraries.h>
#include <exec/types.h>
#include <exec/memory.h>
#include <exec/nodes.h>
#include <exec/lists.h>

#include <devices/keymap.h>

#include <graphics/gfxbase.h>
#include <graphics/displayinfo.h>

#include <intuition/intuitionbase.h>
#include <intuition/intuition.h>

#include <dos/dos.h>
#include <dos/rdargs.h>
#include <dos/dostags.h>

#include <graphics/displayinfo.h>
#include <graphics/gfxbase.h>

#include <libraries/asl.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <libraries/gadtools.h>
#include <libraries/mui.h>

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

#include <clib/alib_protos.h>

#include <proto/asl.h>
#include <proto/diskfont.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/gadtools.h>
#include <proto/graphics.h>
#include <proto/icon.h>
#include <proto/intuition.h>
#include <proto/muimaster.h>
#include <proto/utility.h>
#include <proto/wb.h>

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <dos.h>
#include <math.h>

#include "finances.h"


#define REG(x) register __ ## x
#define ASM    __asm
#define SAVEDS __saveds

#define SimpleHelpButton(name,helpnode)\
	TextObject,\
		ButtonFrame,\
		MUIA_Text_Contents, name,\
		MUIA_Text_PreParse, "\33c",\
		MUIA_InputMode    , MUIV_InputMode_RelVerify,\
		MUIA_Background   , MUII_ButtonBack,\
      MUIA_HelpNode     , helpnode,\
		End

#define KeyHelpButton(name,key,helpnode)\
	TextObject,\
		ButtonFrame,\
		MUIA_Text_Contents, name,\
		MUIA_Text_PreParse, "\33c",\
		MUIA_Text_HiChar  , key,\
		MUIA_ControlChar  , key,\
		MUIA_InputMode    , MUIV_InputMode_RelVerify,\
		MUIA_Background   , MUII_ButtonBack,\
      MUIA_HelpNode     , helpnode,\
		End


/*
 * MESSAGE IDs -----------------------------
 */
#define ID_CLOSEMAINWINDOW        101
#define ID_CLOSEIFILWINDOW        102
#define ID_CLOSELFILWINDOW        103
#define ID_CLOSEMACROWINDOW       104
#define ID_CLOSEDENTWINDOW        105


/*
 * OBJECT IDs ------------------------------
 */
#define ID_MENU_NEW              1001
#define ID_MENU_OPEN             1002
#define ID_MENU_SAVE             1003
#define ID_MENU_SAVEAS           1004
#define ID_MENU_PRINT            1005
#define ID_MENU_PRINTAS          1006
#define ID_MENU_ABOUT            1007
#define ID_MENU_QUIT             1008
#define ID_MENU_CUT              1101
#define ID_MENU_COPY             1102
#define ID_MENU_PASTE            1103
#define ID_MENU_ERASE            1104
#define ID_MENU_ASSIGNMACRO      1201

#define ID_LIS_EDITLISTDBLCLICK     2001
#define ID_CYC_MAINVIEWBYCYCLE      2002
#define ID_BUT_MAININVFVINVEST      2007
#define ID_BUT_MAININVFVREGDEPOSIT  2008
#define ID_BUT_MAININVDESIREDFV     2009
#define ID_BUT_MAININVREGWD         2010
#define ID_BUT_MAININVWDINITINVEST  2011
#define ID_BUT_MAININVWDMININVEST   2012
#define ID_BUT_MAININVAPRONINVEST   2013
#define ID_BUT_MAINLOANPRINCIPAL    2014
#define ID_BUT_MAINLOANPAYMENT      2015
#define ID_BUT_MAINLOANTERM         2016
#define ID_BUT_MAINLOANAPRONLOAN    2017
#define ID_BUT_MAINLOANLOANBALANCE  2018
#define ID_LIS_MAINLISTCHANGE       2019

#define ID_DENT1                 6001
#define ID_DENT2                 6002
#define ID_DENT3                 6003
#define ID_DENT4                 6004
#define ID_DENT5                 6005
#define ID_BUT_DENTCALCULATE     6006
#define ID_BUT_DENTCANCEL        6007


/*
 * GENERAL DEFINES ----------------------------------
 */
#define  MAX( a,b )   ( ( (a) > (b) ) ? (a) : (b) )
#define  MIN( a,b )   ( ( (a) < (b) ) ? (a) : (b) )
#define MAKE_ID(a,b,c,d) ((ULONG) (a)<<24 | (ULONG) (b)<<16 | (ULONG) (c)<<8 | (ULONG) (d))

#define ENDWINDOW_OK       1
#define ENDWINDOW_CANCEL   0

#define SUCCESS 0L
#define FAILURE 1L
#define STOPRUN -1

#define FILEID_HEADER   "MUIECON_JMH2"


/*
 * APP-TEMPLATE FUNCTION PROTOTYPES -----------------
 */
void  JMH_About( void );
LONG  JMH_Error( char *, APTR, BOOL, char *, char *, LONG );
void  JMH_InitVars( void );
void  JMH_Startup( int, char ** );
char *JMH_CStrip( char *, char * );
void  JMH_Quit( char * );
void  JMH_WindowTitle( void );
BOOL  JMH_Write( BPTR, APTR, ULONG, BOOL );


/*
 * STRUCTURE DEFINITIONS ----------------------------
 */
struct MyGlobals {
   APTR  App;
   APTR  WIN_Main,
         WIN_Dent;

   struct DiskObject *dobj_App;
   char               szProjectSpec[ FMSIZE+1 ];

   UBYTE  ubCurrentEntryMode;
   BOOL   fReplacementMode;

   APTR   pCCPNode;  // cut, copy, paste node (internal)
   BOOL   running;
};

struct LVNode {
   UBYTE  ubDataEntryMode;
   UBYTE  ubContComp;
   char  *pszName;
   char  *pszResult;
   char  *pszText1;
   char  *pszText2;
   char  *pszText3;
   char  *pszText4;
   char  *pszText5;
};

struct MainWindow {
   APTR  CYC_ViewBy,
         LIS_EditList,
         TXT_TotalSelected,
         TXT_TotalAll,
         BUT_InvFVInvest,
         BUT_InvFVRegDeposit,
         BUT_InvDesiredFV,
         BUT_InvRegWD,
         BUT_InvWDInitInvest,
         BUT_InvWDMinInvest,
         BUT_InvAPRonInvest,
         BUT_LoanPrincipal,
         BUT_LoanPayment,
         BUT_LoanTerm,
         BUT_LoanAPRonLoan,
         BUT_LoanLoanBalance;
};

struct DataEntryWindow {
   APTR  STR_Name,
         TXT_DataText1,
         STR_DataLine1,
         TXT_DataText2,
         STR_DataLine2,
         TXT_DataText3,
         STR_DataLine3,
         TXT_DataText4,
         STR_DataLine4,
         TXT_DataText5,
         STR_DataLine5,
         CHE_ContComp,
         BUT_Calculate,
         BUT_Cancel;
};

/*
 * APPLICATION FUNCTION PROTOTYPES ------------------
 */
BOOL fnConstructMainWindow( void );
BOOL fnConstructDentWindow( void );
BOOL fnInitMainWindow( void );
BOOL fnInitDentWindow( UBYTE, BOOL, BOOL );
BOOL fnDestructDentWindow( BOOL );

ULONG  fnCheckDENT( APTR, UBYTE, BOOL );
double fnCalculate( ULONG * );
LONG   fnDoCalculation( BOOL, BOOL );
ULONG  fnPrint( ULONG, BOOL );
ULONG  fnPrintAs( BOOL );
ULONG  fnReadData( char *, BOOL );
ULONG  fnWriteData( BOOL, BOOL );
