/*************************************************************
                  PROTOTYPES
*************************************************************/

 int PASCAL  WinMain(HINSTANCE, HINSTANCE, LPSTR, int) ;
BOOL InitApplication(HANDLE);
BOOL    InitInstance(HANDLE, int) ;

// Window Procs
long CALLBACK       CtrlWndProc(HWND, UINT, WPARAM, LPARAM) ;
long CALLBACK        BudWndProc(HWND, UINT, WPARAM, LPARAM) ;
long CALLBACK       ScanWndProc(HWND, UINT, WPARAM, LPARAM) ;
long CALLBACK       ResultsProc(HWND, UINT, WPARAM, LPARAM) ;
long CALLBACK       CollateProc(HWND, UINT, WPARAM, LPARAM) ;
long CALLBACK     BannerWndProc(HWND, UINT, WPARAM, LPARAM) ;
long CALLBACK       OverlayProc(HWND, UINT, WPARAM, LPARAM) ;
long CALLBACK   ArchiveWorkProc(HWND, UINT, WPARAM, LPARAM) ;
long CALLBACK       ImageryProc(HWND, UINT, WPARAM, LPARAM)	;
long CALLBACK         GraphProc(HWND, UINT, WPARAM, LPARAM) ;
long CALLBACK   PrintLayoutProc(HWND, UINT, WPARAM, LPARAM)	;
long CALLBACK   SelectivityProc(HWND, UINT, WPARAM, LPARAM)	;
long CALLBACK		 ViewerProc(HWND, UINT, WPARAM, LPARAM) ;
long CALLBACK        RasterProc(HWND, UINT, WPARAM, LPARAM) ;
long CALLBACK       PreviewProc(HWND, UINT, WPARAM, LPARAM) ;
long CALLBACK      ListingsProc(HWND, UINT, WPARAM, LPARAM) ;

// Dialog box Procs
BOOL CALLBACK        CtlPanelDlgProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK       TabSourceDlgProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK   TabSelectBodyDlgProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK       TabEpochsDlgProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK      TabDisplayDlgProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK     TabSystemicDlgProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK   TabSystemicIIDlgProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK      TabFourierDlgProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK        TabTunedDlgProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK      TabSpectraDlgProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK	      TabMiscDlgProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK TabViewSettingsDlgProc(HWND, UINT, WPARAM, LPARAM) ;

BOOL CALLBACK  RasterisedDipsDlgProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK       PickSuiteDlgProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK         PickArcDlgProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK     SearchStatsDlgProc(HWND, UINT, WPARAM, LPARAM) ;

BOOL CALLBACK        HytDlgProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK     DetailDlgProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK      PrefsDlgProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK      CalibDlgProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK      AlignDlgProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK    CollateDlgProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK  ArchivistDlgProc(HWND, UINT, WPARAM, LPARAM) ;

BOOL CALLBACK     RotateDlgProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK     ResizeDlgProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK     BriConDlgProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK    TestBudDlgProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK     TkParmDlgProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK        GPSDlgProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK         AboutProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK          SigPatch(HWND, UINT, WPARAM, LPARAM) ;

BOOL CALLBACK      GrabSetUpDlg(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK  SelCaptDriverDlg(HWND, UINT, WPARAM, LPARAM) ;

//BOOL CALLBACK  NameSuiteDlgProc(HWND, UINT, WPARAM, LPARAM) ;
//BOOL CALLBACK     UpdateDlgProc(HWND, UINT, WPARAM, LPARAM) ; // Proc removed to "OldUpdateProc.txt"

  void                DirectionalPlots( HDC, int, int ) ;
  void                        DirPlots( HDC, int, int ) ;
  void                       DataPlots( HDC, int ) ;
  void        PlotSynthRawLambdaSeries( void ) ;
  void       PlotSynthMeanLambdaSeries( void ) ;
  void  PlotForcedSynthRawLambdaSeries( void ) ;
  void PlotForcedSynthMeanLambdaSeries( void ) ;
  void   PlotTunedSynthRawLambdaSeries( void ) ;
  void  PlotTunedSynthMeanLambdaSeries( void ) ;
  void             PlotRawLambdaSeries( void ) ;
  void            PlotMeanLambdaSeries( void ) ;

  BOOL             ivrc( HWND, LPRECT, BOOL ) ;	// InvalidateRect() wrapper
  HWND   CreateAToolBar( HWND ) ; 
  BOOL       GetBudFile( HWND ) ;
  BOOL      SaveBudFile( HWND ) ;
  BOOL  GetSearchFolder( HWND ) ;
   int         MakeRoom( void )	;
   int    CheckFileType( void ) ;
   int     LoadBMformat( void ) ;
   int     LoadJPformat( void )	;
  void     LoadBGformat( void ) ;
  void     LoadBLformat( void ) ;
  void	       BMPtoJPG( char * infile, char * ofile ) ;
  void        GrabFrame( void ) ;

  BOOL     SaveCollFile( void )	;
  void       ProcessDIB( void )	;
HBITMAP         Capture( HWND, LPPOINT ) ;
  HWND     SelectWindow( HWND ) ;
  HWND		   PickTree( LPPOINT ) ;
  void      ScanDIBline( int ) ;
  int CheckLumPeakLimit( int ) ;
  void      ScanRGBLine( BYTE *, UINT32, UINT32, int ) ;
  void             Draw( HWND, int, int, int, int, HPEN ) ;
  void        DrawCross( HWND, int, int, int, int, HPEN ) ;
  void       DrawCircle( HWND, int, int, int, int, HPEN ) ;
  void         DrawMark( HWND, int, int, int, HPEN ) ;
  void            DrawX( HWND, int, int, int, HPEN ) ;
  void         DrawRect( HWND, int, int, int, int, int, HPEN ) ;
  void       RubberLine( HWND, int, int, int, int ) ;

  void      DrawControl( HWND, LPDRAWITEMSTRUCT ) ;
  void      MeasureGrid( LPMEASURE ) ;
  void         SubText1( HWND, UINT32, UINT32, UINT32, int, UINT32, UINT32 ) ;
  void   InitBmControls( void ) ;
  void   AdjustDisplays( void ) ;
  void   GetLineStorage( void )	;
  void    ShowSmoothing( HWND ) ;
  void     ResetMeasure( HWND, int *, int * );
  void ResetCalibration( void ) ;

   int       ParseBudID( char *, LPBUDID ) ;
   int GetFirstandFinal( LPBUDSEARCH, int ) ;
  char * MakeFriendlyDate( int, int, int, int ) ;
  char      * ParseLine( char *, int ) ;
  char      * GetMyPath( char * ) ;
  char       * DropPath( char * ) ;
  char        * DropExt( char * ) ;
  char         * AddExt( char *, char * ) ;
  char         * GetExt( char * ) ;

   int         checkeps( void ) ;		  // "check ee pees"
  void          PutText( HDC, LPTXT, char * ) ;
  void           Lambda( int ) ;
  void        PathCurve( HWND, int, int, HPEN ) ;
  void           TrialPC( HWND, double, double, double, double, double, double, HPEN )	;
   int    RecordLambdas( void ) ;
   int RecordDimensions( void ) ;

   int        Inventory( LPBUDSEARCH ) ;
  void      SaveAllAway( void ) ;
  void			   Note( int, char * ) ;
  void             WPPS( int, int ) ;
 DWORD             gpps( char *, char *, char *, char *, DWORD, char * ) ;
  void             wpps( char *, char *, char *, char * ) ;

  void		  ResetScan( void ) ;
   int Fname_is_on_Path( char *, char * ) ;
   int         GetDepth( char * ) ;
  void       GetNewPath( char *, char *, char * ) ;

// int       FindDrives( HWND ) ;					// find all drives on the system

   int      FindFolders( HWND, char * ) ;			// find all paths to all folders on system
   int           incpth( char * ) ;

  void        Rearrange( void ) ;	// copy and rearrange the archive

  char       * DupSlash( char * ) ;  // double up contained backslashes
  char    * getlastcomp( char * ) ;					// parse out last item of a path ("tail")
   int      InspectFile( HWND, char *, char * ) ;	// inspect a file for prefix, type etc.,
   int  Is_A_known_to_B( int, int, int, int, int ) ;// establish associations (is A known to B?)
   int        MaybeHere( HWND, char *, int ) ;		// valid file(s) may be in this directory 
   int		InspectDirs( int, int, int) ;			// inspect a dir for file of possibly valid type  
   int		   checkABR( int ) ;
  void     GetIDsperIMG( void ) ;					// get all the IDs associated with all the images
  char          * PreWrt(int, int, char *) ;


   int         GetPthID( char *, int ) ;			// have we  a PTH to match this path name? Get its ID.
   int         GetImgID( char *, int ) ;			// have we an IMG to match this inst name? Get its ID.
   int         GetIniID( char *, int ) ;			// have we an INI to match this inst name? Get its ID.
   int		   GetTrkID( char *, int ) ;			// have we  a TRK to match this inst name? Get its ID.
   int		   GetDatID( char *, int ) ;			// have we  a DAT to match this inst name? Get its ID.

   
  HWND         PvwPopup( HWND, int, int, int, int ) ;
   int         AdjPVwnd( HWND, int ) ;
   int      OpenPreview( HWND, char * ) ;

   int  FreeTrackMemory( LPTRKINFO ) ;
  void   AllocTrackHyts( LPTRKINFO, int ) ;
   int  LinkTrackPoints( LPTRKINFO, int ) ;
   int  TrackRGBProfile( int, int ) ;
 BOOL       StandardBud( void ) ;
 BOOL MeasureTrackedBud( TRKINFO ) ;
 void         Construct( int, int ) ;
 void		 convertDAT( void ) ;
 void     InterpretData( int ) ;

  int       TestTransit( double, double, int ) ; 

 void             Aflag( HWND ) ;
 void             Gflag( HWND ) ;
 void     SetTimeFlags2( void ) ;
 void      SetFouFlags2( void ) ;
 void      SetAgrFlags2( void ) ;

  int	        PlotSeg( HWND, int, int, int, int ) ;
 void			   plot( HDC , int, int, int *, int *, int, int, int ) ;
  int       GetPlotMode( void ) ;
 void           Connect( HDC, int, int, int, int) ; // plot line between points

 BOOL Video_cap(HWND hWnd, int index) ;
  int EnumCapDrv() ;

BOOL CreateGPSPort(char * port, int baudrate) ;

 void DoTunedSynth(
				  LPFOURIER fou,	 // data structure to use
				        int N,		 // number of data points
						int sFactor, // multiplier for number of points to synthesize
					 double lo_order,// least order of band
					 double hi_order,// largest order of band
					 double * ostep, // order step
						int onbuds,	 // whether on bud series or general dates
				        int Type	 // lambda type to use (raw/mean)
				 ) ;

 void           Sampler( LPFOURIER, LPFOURIER, int, int ) ;	// regularise irregular data by sampling
 void   EnableMnDspCtls( BOOL ) ;
 void   EnableRwDspCtls( BOOL ) ;

  int  GetLambdaSpectrum( int ) ;		// get Lambda spectrum
  int GetLambdaSynthesis( int, int ) ;	// get Lambda synthesis
  int            updisp( void ) ;
 void           Refresh( void ) ;
 void       ShowArchBar( HWND, char * ) ;
 void        ClearCombo( HWND, int ) ;
 void        PutInCombo( HWND, int, char * ) ;
 void        InsInCombo( HWND, int, char *, int ) ;
 void        PutCBEdBox( HWND, int, char * ) ;
 void    SetDragHandles( int  )	;
 void   DrawDragHandles( HWND ) ;
 void RestoreExclusions( void ) ;
  int        LineCursor( HWND, int, int *, int *, RECT, POINT ) ;
  int             Flyer( HWND, char *, HFONT, int, int, int ) ;
 void			 IsRast( void ) ;

 void          ImageSig( UINT32, UINT32 ) ;

 void          CliToImg(DPOINT * I,  POINT * S ) ;
 void          ImgToCli( POINT * S, DPOINT * I ) ;
 void         CliToBuff( POINT * B,  POINT * S ) ;
 void        CliToInset( POINT * B,  POINT * S ) ;
 void         BuffToCli( POINT * S,  POINT * B ) ;
 void        InsetToCli( POINT * S,  POINT * B ) ;
 void         BuffToImg(DPOINT * I,  POINT * B ) ;
 void         ImgToBuff( POINT * B, DPOINT * I ) ;
 void        InsetToImg(DPOINT * I,  POINT * B ) ;
 void        ImgToInset( POINT * B, DPOINT * I ) ;

 double           xp2ep( int ) ;
  int			  ep2xp( double ) ;

  int         FullPhase(
			     HDC hdc,
			    HPEN pen,
			  double oang,
			  double ang,
			     int Sp,
				 int xp,
				 int yp,
				 int oxp,
				 int oyp,
				 int top,
				 int bot
			  ) ;

 BOOL		     DLLShowWindow( HWND ) ; // this is my very first DLL!


HBITMAP PutPatch
(
    HWND hWnd,
 HBITMAP hBm,
     int w,
	 int h,
     int x,
     int y
) ;

HBITMAP GrabPatch
(
HWND hWnd,
 int w,
 int h,
 int x,
 int y
) ;

HBITMAP GetZoomPatch
(
HWND hWnd,
int x,	// cursor position
int y,
int w,	// sides of region round cursor
int h
) ;

HWND    GrabWindow( HWND ) ; // Get the window under the mouse
HBITMAP    GrabWnd( HWND ) ;

double	   PickDate( int, char * ) ; // Get date from graph by mouse click
double  PickMseDate( int, char *) ;  // Get date from graph on mouse move
void	  ComposeEp( void ) ;

void			   Polar( HDC, int, int, int, int, double, HPEN, SYSTEMIC ) ;
void	   bSysDirection( HDC, int, int, int, int, double, HPEN, SYSTEMIC ) ;
void bSysDirectionbyMass( HDC, int, int, int, int, double, HPEN ) ;
void	  bSysDispersion( HDC, int, int, int, int, double, HPEN ) ;

void       ClearBodyIncList( HWND ) ;
void    ClearNonBodyIncList( HWND ) ;
void       ClearBodyDirList( HWND ) ;
void     ClearBodyShiftList( HWND ) ; 
void    ClearBodySysDirList( HWND ) ;
void    ClearBodySysAccList( HWND ) ;
void    ClearBodySysDspList( HWND ) ;
void         SetBodyIncList( HWND ) ;
void      SetNonBodyIncList( HWND ) ;
void         SetBodyDirList( HWND ) ;
void       SetBodyShiftList( HWND ) ; 
void      SetBodySysDirList( HWND ) ;
void      SetBodySysAccList( HWND ) ;
void      SetBodySysAlnList( HWND ) ;
void      SetBodySysDspList( HWND ) ;
void	    KillDipControls( void ) ;
void	 KillArcDipControls( void ) ;
void	KillRastDipControls( void ) ;
BOOL      CheckRastBodyList( void ) ;

void				 ChkDrv( HWND ) ;

void         VelsOn( void ) ;				// turn on VELS if off, and count calls to use VELS
void        VelsOff( void ) ;				// if count of calls to use VELS is non-zero, decrement the count. If count is 1, turn VELS off
BOOL           ivrc( HWND, LPRECT, BOOL ) ;	// Invalidate window rectangle
BOOL            vrc( HWND, LPRECT ) ;		//   Validate window rectangle

VOID WINAPI OnTabbedDialogInit( HWND ) ;
VOID WINAPI       OnSelChanged( HWND ) ;
VOID WINAPI  OnChildDialogInit( HWND ) ;