VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 1  'vbSimpleBound
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "sGGlobals"
Attribute VB_GlobalNameSpace = True
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

'*/////////////////////////////////////////
'#REGION: CMDDLG_WITH_API [AutoUnfold]
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Declare Function CHOOSECOLOR Lib "comdlg32.dll" Alias "ChooseColorA" (pChoosecolor As TCHOOSECOLOR) As Long
Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long

Private Type TCHOOSECOLOR
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    rgbResult As Long
    lpCustColors As String
    flags As Long
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type

Private Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type

Public Function CMDLG_FILTER_IMAGE() As String
    CMDLG_FILTER_IMAGE = "Imagenes|*.png;*.jpg;*.bmp;*.gif|Todos los archivos (*.*)|*.*"
End Function

'=================================================================
'Choose Color Dialog
'=================================================================
'Return -1 when canceled
'Example:
'    Dim SelColor&
'    SelectedColor = CMDLG_GetColor(me.hwnd)
'    If (SelectedColor <> -1) Then Form1.BackColor = SelectedColor
Public Property Get CMDLG_GetColor(FrmHwnd&, Optional ShowColor& = &HC0C0C0) As Long
Dim Color As TCHOOSECOLOR
    Color.hwndOwner = FrmHwnd& 'when skipped program will keep focus
    Color.flags = 3 'CC_RGBINIT=1  CC_FULLOPEN = &H2
    'Color.flags = &H80 'CC_SOLIDCOLOR: Only list of colors (no full rgb selector)
    Color.lStructSize = Len(Color)
    Color.lpCustColors = 100
    Color.rgbResult = ShowColor&
    If CHOOSECOLOR(Color) <> 0 Then
        CMDLG_GetColor = Color.rgbResult 'Set GetColor to the long value despite it saying rgb
    Else
        CMDLG_GetColor = -1 'Not 0 because then it would be though black not an error
    End If
End Property

'=================================================================
'Open File Dialog
'=================================================================
'Return "" when canceled
'Example:
'    filename$ = CMDLG_ShowOpen(Me.hWnd,"Select image",CMDLG_FILTER_IMAGE)
'    filename$ = CMDLG_ShowOpen(Me.hWnd,"Select document","Word Files|*.doc;*.docx")
Public Function CMDLG_ShowOpen(FrmHwnd&, Title$, Optional Filter$ = "Todos los archivos|*.*") As String
On Error GoTo ErrorLoc
Dim OpenF As OPENFILENAME
    OpenF.flags = &H4 ' no open as readonly box
    OpenF.hwndOwner = FrmHwnd& 'set the window handle
    
    OpenF.lpstrFile = String(500, Chr(0))
    OpenF.lpstrFileTitle = String(500, Chr(0))
    OpenF.lpstrFilter = Replace(Filter, "|", Chr(0))
    OpenF.lpstrTitle = Title
    OpenF.lStructSize = Len(OpenF)
    OpenF.nMaxFile = 501
    OpenF.nMaxFileTitle = 501
    If GetOpenFileName(OpenF) Then
        CMDLG_ShowOpen = Replace(OpenF.lpstrFile, Chr(0), "")
    Else
ErrorLoc:
        CMDLG_ShowOpen = "" 'No file error
    End If
End Function

'=================================================================
'Save File Dialog
'=================================================================
'Return "" when canceled
'Example:
'    filename$ = CMDLG_ShowSave(Me.hWnd,"Save image",CMDLG_FILTER_IMAGE)
'    filename$ = CMDLG_ShowSave(Me.hWnd,"Save as document","Word Files|*.doc;*.docx")
Public Function CMDLG_ShowSave(FrmHwnd&, Title$, Optional Filter$ = "Todos los archivos|*.*") As String
On Error GoTo ErrorLoc
    Dim SaveF As OPENFILENAME
    SaveF.flags = &H2 Or &H4 'Prompt on overwrite, no read only box
    SaveF.hwndOwner = FrmHwnd&
    SaveF.lpstrFile = String(500, Chr(0))
    SaveF.lpstrFileTitle = String(500, Chr(0))
    SaveF.lpstrFilter = Replace(Filter, "|", Chr(0))
    SaveF.lpstrTitle = Title
    SaveF.lStructSize = Len(SaveF)
    SaveF.nMaxFile = 501
    SaveF.nMaxFileTitle = 501
    If GetSaveFileName(SaveF) Then
        CMDLG_ShowSave = Replace(SaveF.lpstrFile, Chr(0), "")
    Else
ErrorLoc:
        CMDLG_ShowSave = "" 'Error no file
    End If
End Function
'Common dialogs via API
'#END REGION
'*/////////////////////////////////////////




'Set relative path resolution for sG controls in OCX
Public Sub SetAppPath(sAppPath$)
    BaseAppPath$ = sAppPath
End Sub

'======================================
'Check value is in [min-max] range
'if not return the nearest valid one
'======================================
Public Function Range(ByRef value!, Min!, Max!) As Single
    If (value <= Min!) Then value! = Min!
    If (value >= Max!) Then value = Max!
    Range = value!
End Function
'Idem Long version
Public Function RangeL(ByRef value&, Min&, Max&) As Long
    If (value <= Min) Then value = Min
    If (value >= Max) Then value = Max
    RangeL = value
End Function

'===================================
'Random number generation in range
'===================================
'Note: Max is never reached
Public Function RN(Min!, Max!) As Single
    RN = (Max - Min) * Rnd + Min
End Function

'===================================
'Random number generation in range
'===================================
'Note: include min and máx
Public Function RNL(Min&, Max&) As Long
    RNL = Fix(((Max + 1) - Min) * Rnd + Min)
End Function

'===============================================
'Return a random option from a serie of strings
'===============================================
Public Function RNS(ByRef Options$, Optional delimiter$ = "|") As String
Dim v As Variant
    v = Split(Options, delimiter)
    RNS = v(RNL(LBound(v), UBound(v)))
End Function

'===============================================
'Scale X,Y from pixels to Twips
'===============================================
Public Function SCX(value&) As Long
    SCX = (value * Screen.TwipsPerPixelX)
End Function
Public Function SCY(value&) As Long
    SCY = (value * Screen.TwipsPerPixelY)
End Function

'===============================================
'Scale X,Y from Form in Twips to pixel scale
'===============================================
Public Function SCPX(value&) As Long
    SCPX = (value / Screen.TwipsPerPixelX)
End Function
Public Function SCPY(value&) As Long
    SCPY = (value / Screen.TwipsPerPixelY)
End Function

'NO USAR! Solo se inicializa desde el Ejecutable. Dentro de sgControls hay que instanciarla
Private Sub Class_Initialize()
    'Debug.Print "SgGlobals.Initialize"
End Sub
