Function Init_OIPI(Params)

/************************************************************************************************
*
* This program is proprietary and is not to be used by or disclosed to others, nor is it to
* be copied without written permission from SRP Computer Solutions, Inc.
*
* Name       :  Init_OIPI
*
* Description:  Initializes the OIPI using the provided parameters. Meant to be a shell around
*				Set_Printer("INIT") that will use default parameters if none are passed. This
*				routine will also provide better error trapping than what normally occurs inside
*				report stored procedures.
*
* Notes:		Start_OIPI and Create_Custom_Form, and Delete_Custom_Form are used to facilite 
*				advanced management of the INIT statement.		
*
* Parameters:
*	Params				 [in] -- @FM delimited array of parameters for this routine. This array gets
*								 parsed into a matrixed array so variable equates can be used to represent
*								 each individual parameter. See list of equates below for an explanation
*
*		Preview			  (1) -- True/False flag if report should be previewed. Default is false
*		PrintJobTitle	  (2) -- The text that should appear in the printer dialog box and the preview titlebar
*		PrinterAssignment (3) -- Name of the printer assignment for the report. Default will be null, which means
*								 the workstation's default printer will be printed to
*		Landscape		  (4) -- True/False flag if report should print in landscape. Default is false
*		LeftMargin		  (5) -- Left margin for the report. Default is 0.500 inches
*		TopMargin		  (6) -- Top margin for the report. Default is 0.500 inches
*		RightMargin		  (7) -- Right margin for the report. Default is 0.500 inches
*		BottomMargin	  (8) -- Bottom margin for the report. Default is 0.500 inches
*		PageWidth		  (9) -- Page width for the report. Default is null and will use FormID instead
*		PageHeight		 (10) -- Page height for the report. Default is null and will use FormID instead
*		FormID			 (11) -- Form ID for the report. Default is LETTER (i.e. 1, see OIPRINT_EQUATES)
*		ExportType		 (12) -- Format that the report should export into (i.e. PDF, HTML, etc.) Default is no export
*		ExportFileName	 (13) -- Name of the file that the report will export into. Default is no filename
*		DisplayError	 (14) -- True/False flag if any OIPI error messages should be displayed. Default is true
*		Copies			 (15) -- Number of copies of the report, 1 if blank
*		Printer			 (16) -- Custom selection of a printer on the fly
*
*	Ans					[out] -- True if INIT is successful, False if it isn't
*
* History (Date, Initials, Notes)
*   02/20/03  dmb   Original programmer
*	02/25/03  dmb	Increase loop from 2 to 5 to stop/start OIPI. Add Set_Printer("TERM", 1)
*					statement before each Start_OIPI() command as it appears to close the
*					OIPI better
*
************************************************************************************************/

$insert SRP_APP_INSERTS
$insert OIPRINT_EQUATES

Equ FormDimensions$ to 8.5:"-":11 : @FM : 11:"-":17 : @FM : 17:"-":11 : @FM : 8.5:"-":14 : @FM : 4.13:"-":9.5
Equ FormIDs$		to LETTER	  : @FM : TABLOID	: @FM : LEDGER	  : @FM : LEGAL		 : @FM : ENV10

Declare function   Get_Printer_Port, Create_Custom_Form, Delete_Custom_Form, Start_OIPI
Declare subroutine OIPI_Errors, Set_Printer

If Assigned(Params)	else Params	= ""

Ans = False$ ; * Assume failure for now

Process = 0
Loop
Until Process GE 4
	Process += 1
	On Process GoSub Setup_Variables, Get_Form_ID, Prepare_INIT_Params, Initialize_OIPI
Repeat

Return Ans


Setup_Variables:
	Dim MatParams(16)	; * Initialize the matrix

	Equ Preview				to MatParams(1)
	Equ PrintJobTitle		to MatParams(2)
	Equ PrinterAssignment	to MatParams(3)
	Equ Landscape			to MatParams(4)
	Equ LeftMargin			to MatParams(5)
	Equ TopMargin			to MatParams(6)
	Equ RightMargin			to MatParams(7)
	Equ BottomMargin		to MatParams(8)
	Equ PageWidth			to MatParams(9)
	Equ PageHeight			to MatParams(10)
	Equ FormID				to MatParams(11)
	Equ ExportType			to MatParams(12)
	Equ ExportFileName		to MatParams(13)
	Equ DisplayError		to MatParams(14)
	Equ Copies				to MatParams(15)
	Equ Printer				to MatParams(16)
	
    MatParse Params into MatParams	; * Convert the dynamic array into the matrixed array

	If Preview EQ "" 		then Preview 		= False$
	If Landscape EQ "" 		then Landscape 		= False$
	If LeftMargin EQ "" 	then LeftMargin 	= 0.500
	If TopMargin EQ "" 		then TopMargin 		= 0.500
	If RightMargin EQ "" 	then RightMargin 	= 0.500
	If BottomMargin EQ "" 	then BottomMargin 	= 0.500
	If DisplayError EQ "" 	then DisplayError 	= True$
	If Copies EQ ""			then Copies			= 1

	If Len(PageWidth) AND Not(Num(PageWidth)) 	then PageWidth 	= "" 	; * PageWidth must be a number
	If Len(PageHeight) AND Not(Num(PageHeight)) then PageHeight = "" 	; * PageHeight must be a number
	If Len(FormID) AND Not(Num(FormID)) 		then FormID 	= "" 	; * FormID must be a number

	If ExportType GT 0 AND ExportFileName EQ "" then
		/*	An export filetype has been selected but no path and filename. Bring up the ChooseFile dialog  */
		
		FileType  = "" ; * Initialize the variable
		Extension = "" ; * Initialize the variable
		Begin Case
			Case ExportType EQ 1  ; FileType = "Rich Text Format"					; Extension = "rtf"
			Case ExportType EQ 2  ; FileType = "HyperText Markup Language"			; Extension = "html"
			Case ExportType EQ 3  ; FileType = "Dynamic HyperText Markup Language"	; Extension = "dhtml"
			Case ExportType EQ 4  ; FileType = "HyperText Markup Language"			; Extension = "html"
			Case ExportType EQ 5  ; FileType = "Dynamic HyperText Markup Language"	; Extension = "dhtml"
			Case ExportType EQ 6  ; FileType = "Portable Document Format"			; Extension = "pdf"
			Case ExportType EQ 10 ; FileType = "OIPI Print File"					; Extension = "opf"
		End Case
		
		If Len(Extension) then
			CFOpt = ""
			CFOpt<1> = 0
			CFOpt<2> = FileType:" (*.":Extension:")/*.":Extension:"/All Files (*.*)/*.*/"
			CFOpt<3> = 1
			ExportFileName = Utility("CHOOSEFILE", @Window, CFOpt)
			LenExt = Len(Extension)
			If ExportFileName EQ "" then
				ExportType = 0 ; * No export filename was selected, reset to printer output
			end else
				If ExportFileName[-LenExt, LenExt] NE Extension then ExportFileName := ".":Extension
			end
		end else
			ExportType = 0 ; * No valid export type was given, reset to printer output
		end
	end
return


Get_Form_ID:
	/*	The 32-bit OIPI works better with Form IDs rather than width and height values. Therefore, even if a width
		and height is specified, a match will be attempted against the common Form ID dimensions. If no match is found
		then a custom form will be created using Create_Custom_Form  */

FormID = ""
* 	Begin Case
* 
*		Case Len(FormID)
			* A valid FormID was specified. Nothing else needs to be done

*		Case Len(PageWidth) AND Len(PageHeight)
*			* A valid width and height was specified. See if these match the common dimensions list and use that
*			* Form ID if found
*			PageSize = (PageWidth + 0) : "-": (PageHeight + 0) ; * Create a lookup value
*			Locate PageSize in FormDimensions$ using @FM setting fPos then
*				* A match was found so use predefined Form ID
*				FormID = FormIDs$<fPos>
*			end else
*				* No match was found. Create a custom form
*				FormID = Create_Custom_Form(PageWidth, PageHeight)
*			end
*		
*		Case Otherwise$
*			// No values were specified so use Letter (8-1/2 x 11) as a default
*			FormID = LETTER

* 	End Case

 	// Clear the PageWidth and PageHeight variables since only the FormID will be used
* 	PageWidth = ""
* 	PageHeight = ""
return


Prepare_INIT_Params:
	FileName	= ""
	FileName<1>	= PrintJobTitle
	FileName<3> = ExportType
	FileName<4> = ExportFileName

	PageInfo 	= ""
	PageInfo<1> = LeftMargin
	PageInfo<2> = TopMargin
	PageInfo<3> = RightMargin
	PageInfo<4> = BottomMargin
	PageInfo<5> = PageWidth
	PageInfo<6> = PageHeight
	PageInfo<7> = FormID
	PageInfo<8> = Copies
	PageInfo<9> = 1 ; * Allow copy to clipboard

	PageSetup	 = ""
	PageSetup<1> = Landscape

	If Preview then
		Preview = 3 ; * Set preview to maximize
	end else
		Preview = 0 ; * Print using the WMF fix
	end

	PrintSetup = ""
	Begin Case
		Case ExportType GT 0
			PrintSetup<1, 1> = 5	; * Specific location
			PrintSetup<1, 3> = 1	; * Hide the Printing Window
		Case Preview
			PrintSetup<1, 1> = Preview
			PrintSetup<1, 2> = 0
			PrintSetup<1, 6> = 7	; * Keyboard, mouse, and wheel support
			PrintSetup<2>	 = -1	; * Zoom to page width
		Case Otherwise$
			PrintSetup<1, 1> = Preview
			PrintSetup<1, 2> = 0
			PrintSetup<1, 3> = 1	; * Hide the Printing Window
			PrintSetup<1, 4> = 3	; * Completely hidden
	End Case

	If Printer then
		Port = Printer
	end else
		Port = Get_Printer_Port(PrinterAssignment) ; * If PrinterAssignment is null, Get_Printer_Port will return the workstation's default printer
	end
return


Initialize_OIPI:
	Ans = Set_Printer("INIT", FileName, PrintJobTitle, PageInfo, PageSetup, PrintSetup, Port<1>:@FM:Port<3>)
	
	If (Ans LT 0) AND (DisplayError) then
		OIPI_Errors(Ans, @APPINFO<1>)
		Ans = False$ ; * Set return value to just false
	end
return
