function PrintSetup(Instruction, p1, p2, p3)
#pragma precomp SRP_PreCompiler

******************************************************************************
*
* 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
* Revelation Technologies, Inc.
*
* Product    :  OpenInsight Works
*
* History    :  (date, initials, notes)
*   01/23/96  cp   Updating for Hoffa/3.1
*   02/22/96  cp   Added help
*
******************************************************************************

declare function   Get_Property, SRP_Get_Printers, SRP_Array, Database_Services, Get_Printer
declare subroutine Set_Property, SendMessage, Dialog_Box, Start_Window
declare subroutine WritePrivateProfileString, DefPrtr, SetupP
declare subroutine GetPrivateProfileSection, GetPrivateProfileString
declare subroutine Utility

$insert Logical
$insert PrintSetup_Equates

equ DLG_NAME$         to  "PRINTSETUP"
equ INI_FILE$         to  "win.ini": \00\
equ PRN_SECTION$      to  "devices": \00\
equ DFT_SECTION$      to  "windows": \00\
equ DFT_KEY$          to  "device" : \00\
equ WM_WININICHANGE$  to  26
equ HWND_BROADCAST$   to  65535
equ MAX_SIZE$         to  4096

if assigned(Instruction) then
    if len(Instruction) and num(Instruction) else
        Instruction = 1
    end
end else
    Instruction = 1
end

Ret = ""
if Instruction then
    on Instruction gosub Setup, Get, Set, Create, SetDefault, Options, Help, SelChange, Setup
end
return Ret


*******************************
* open the printer setup window
*******************************
Setup:
    Parent = @window
    Exists = Get_Property(Parent, "HANDLE")
    if len(Parent) and Exists then
        Frame = Get_Property(Parent, "MDIFRAME")
        if len(Frame) then
            Parent = Frame
        end
        Dialog_Box(DLG_NAME$, Parent)
    end else
        Start_Window(DLG_NAME$)
    end
return


**************************
* get the list of printers
**************************
Get:

    UseSRPGetPrinters = Database_Services('ReadDataRow', 'APP_INFO', 'USE_SRP_GET_PRINTERS')
    
    If UseSRPGetPrinters then
        result   = SRP_Get_Printers('Local')
        result2  = SRP_Get_Printers('Remote')
        result3  = SRP_Get_Printers('Connect')
        result4  = SRP_Get_Printers('Default')
        List     = ''
        Current  = ''
        Printers = result:@fm:result2:@fm:result3:@fm:result4
        Printers = SRP_Array('Clean', Printers, 'TrimAndMakeUnique', @FM)
        Printers = SRP_Array('SortSimpleList', Printers, 'AscendingText', @FM)
        Ret      = count(Printers, @fm) + (Printers # "")
        Default  = result4
        transfer Printers to p2
        transfer Default  to p3
    end else
        * get list of installed printer types
        Models = str(\00\, MAX_SIZE$)
        GetPrivateProfileSection(PRN_SECTION$, 0, \00\, Models, len(Models), INI_FILE$)
        convert \0020\ to \2000\ in Models
        Models = trimb(Models)
        convert \0020\ to \20FE\ in Models
        * get the list of installed printers
        cModels  = count(Models, @fm) + (Models # "")
        Printers = ""
        for iModel = 1 to cModels
            Printer = str(\00\, MAX_SIZE$) 
            GetPrivateProfileString(PRN_SECTION$, Models<iModel>: \00\, \00\, Printer, len(Printer), INI_FILE$)
            Printer   = Printer [1,\00\]
            cPrinters = count(Printer, ",")
            for iPrinter = 1 to cPrinters
                Printers<-1> = Models<iModel>: ",": field(Printer, ",", 1): ",": field(Printer, ",", iPrinter + 1)
            next iPrinter
        next iModel
        
        * get the default printer
        Default = str(\00\, MAX_SIZE$) 
        GetPrivateProfileString(DFT_SECTION$, DFT_KEY$, \00\, Default, len(Default), INI_FILE$)
        Default = Default [1, \00\]
        
        Ret = count(Printers, @fm) + (Printers # "")
        transfer Models   to p1
        transfer Printers to p2
        transfer Default  to p3
    end
    
return

*************************
* set the default printer
*************************
Set:
    if assigned(p1) then
        Current = p1
        WritePrivateProfileString(DFT_SECTION$, DFT_KEY$, Current: \00\, INI_FILE$)
        SendMessage(HWND_BROADCAST$, WM_WININICHANGE$, 0, 0)
        Ret = TRUE$
    end else
        Ret = FALSE$
    end
return


*********************
* window create event
*********************
Create:
    * get list of printers and the current (ie. default) printer
        result = SRP_Get_Printers('Local')
        result2 = SRP_Get_Printers('Remote')
        result3 = SRP_Get_Printers('Connect')
        result4 = SRP_Get_Printers('Default')
        
        List = ''
        Current = ''
        cPrinters = result:@fm:result2:@fm:result3:@fm:result4
        
    for i = 1 to cPrinters
        List<i> = field(List<i>, ",", 1): " on ": field(List<i>, ",", 3)
    next i
    
    * highlight current printer in printer list
    if len(Current) then
        locate Current in Printers using @fm setting Pos then
            Current = List<Pos>
        end else
            Pos = ""
        end
    end else
        Pos = ""
    end
    
    Ctrls = @window: ".LB_PRINTER": @rm: @window: ".LB_PRINTER": @rm: @window: ".ST_DEFAULT": @rm: @window: ".CB_OPTIONS": @rm: @window    : @rm: @window
    Props = "LIST"                : @rm: "SELPOS"              : @rm: "TEXT"                : @rm: "ENABLED"             : @rm: "@PRINTERS": @rm: "VISIBLE"
    Vals  = List                  : @rm: Pos                   : @rm: Current               : @rm: (Pos > 0)             : @rm: Printers   : @rm: TRUE$
    
    Set_Property(Ctrls, Props, Vals)
return


****************************
* user chose default printer
****************************
SetDefault:
    Ctrls    = @window: ".LB_PRINTER": @rm: @window: ".LB_PRINTER": @rm: @window
    Props    = "TEXT"                : @rm: "SELPOS"              : @rm: "@PRINTERS"
    Vals     = Get_Property(Ctrls, Props)
    Current  = field(Vals, @rm, 1)
    Pos      = field(Vals, @rm, 2)
    Printers = field(Vals, @rm, 3)
    
    if PrintSetup(PRN_SET$, Printers<Pos>) then
        Set_Property(@window: ".ST_DEFAULT", "TEXT", Current)
        Set_Property(@window: ".CB_OPTIONS", "ENABLED", TRUE$)
    end
return


*******************************
* printer configuration options
*******************************
Options:
    hWnd = Get_Property(@window, "HANDLE")
    Set_Property("SYSTEM", "BLOCK_EVENTS", TRUE$)
    SetupP(hWnd)
    Set_Property("SYSTEM", "BLOCK_EVENTS", FALSE$)
return


***************************
* call help for print setup
***************************
Help:
    Utility("RUNHELP", "OINSIGHT.HLP", @window: @fm: 1: @fm: 9010)
return


********************************
* user changes highlight in list
********************************
SelChange:
    Ctrls    = @window: ".LB_PRINTER": @rm: @window: ".ST_DEFAULT"
    Props    = "TEXT"                : @rm: "TEXT"
    Vals     = Get_Property(Ctrls, Props)
    Printer  = field(Vals, @rm, 1)
    Current  = field(Vals, @rm, 2)
    
    Set_Property(@window: ".CB_OPTIONS", "ENABLED", Printer = Current)
return

