;
;************************************************************************
;Title      : pb-include.pbi (For all my projects)
;Auteur     : (C) 2017 CAGE
;Date       : 2017/09/10
;Version PB : PureBasic 5.60 (Windows - x86)
;Version PB : PureBasic 5.60 (Windows - x64)
;Compiler Options
;Compiler Options : Create threadsafe executable
;Compiler Options : Enable moderm theme support (for Windows X and above)
;Compile/Run      : Create temporary executable in the source directory
;
;Libairies:
;
;************************************************************************
;

EnableExplicit ; all the variables must be declared

Global Mutex
Global Message$
Global optConfirm
Global optMinimize

Global AppName$ = GetFilePart(ProgramFilename())
Global AppPath$ = GetPathPart(ProgramFilename())
Global AppDisk$ = Left(AppPath$,2)

; https://msdn.microsoft.com/en-us/library/windows/desktop/cc144179%28v=vs.85%29.aspx
Global TB_Message = RegisterWindowMessage_("TaskbarCreated")

Declare initSysTray()

Procedure taskbarCallback(hWnd, Msg, wParam, lParam)
  If Msg = TB_Message
    Debug Msg
    initSysTray()
  EndIf
  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

Procedure messageBox(request = #False)
  Protected flag = #PB_MessageRequester_Ok ;|#MB_ICONINFORMATION
  If request
    flag = #PB_MessageRequester_YesNo ; |#MB_ICONQUESTION
  EndIf
  ProcedureReturn MessageRequester(#TITLE$, Message$, flag | #MB_TOPMOST)
EndProcedure

CompilerIf Not Defined(TRIAL, #PB_Constant)
  #TRIAL = #False
CompilerEndIf

Procedure.a checkEndTrial()
  If #TRIAL
    If Date() < #MINDATE
      Message$ = "Your clock is not up to date."
      messageBox()
      ProcedureReturn #True
      End
    EndIf
    If Date() > #MAXDATE
      Message$ = "Your program has expired."
      messageBox()
      ProcedureReturn #True
      End
    EndIf
  EndIf
  ProcedureReturn #False
EndProcedure

CompilerIf Defined(TRIAL, #PB_Constant)
  If checkEndTrial()
    End
  EndIf
CompilerEndIf

CompilerIf Not Defined(FOLDER, #PB_Constant)
  #FOLDER = #False
CompilerEndIf

CompilerIf Not Defined(WRITE, #PB_Constant)
  #WRITE = #True
CompilerEndIf

CompilerSelect #PB_Compiler_Processor
  CompilerCase #PB_Processor_x86
    #OS$  = "x86"
    #BIT$ = "32"
  CompilerCase #PB_Processor_x64
    #OS$  = "x64"
    #BIT$ = "64"
  CompilerDefault
    End
CompilerEndSelect

CompilerSelect #BITEXE
  CompilerCase #False
  #PROGRAM$ = #MyApp$ + ".exe"
  CompilerCase #True
  #PROGRAM$ = #MyApp$ + #BIT$ + ".exe"
CompilerEndSelect

Global OSV$

Select OSVersion()
  Case #PB_OS_Windows_2000           : OSV$ = "W2K"
  Case #PB_OS_Windows_XP             : OSV$ = "WXP"
  Case #PB_OS_Windows_Vista          : OSV$ = "WVISTA"
  Case #PB_OS_Windows_7              : OSV$ = "W7"
  Case #PB_OS_Windows_8              : OSV$ = "W8"
  Case #PB_OS_Windows_8_1            : OSV$ = "W8.1"
  Case #PB_OS_Windows_Server_2003    : OSV$ = "WS2003"
  Case #PB_OS_Windows_Server_2008    : OSV$ = "WS2008"
  Case #PB_OS_Windows_Server_2008_R2 : OSV$ = "WS2008R2"
  Case #PB_OS_Windows_Server_2012    : OSV$ = "WS2012"
  Case #PB_OS_Windows_Server_2012_R2 : OSV$ = "WS2012R2"
  Case #PB_OS_Windows_10             : OSV$ = "W10"
  Default
    OSV$ = "W?"
EndSelect

CompilerIf Defined(TITLE$, #PB_Constant)
  #TITLE$ = #TITLE$
CompilerElse
  #TITLE$ = #MyApp$
CompilerEndIf

#WinAppTitle$ =  #TITLE$ + " (" + #OS$ + ") v" + #AppVer$

CompilerIf #PB_Compiler_Debugger
;   Message$ = "Currently, you are in the IDE."
;   MessageRequester(#MyApp$, Message$, #PB_MessageRequester_Ok | #MB_ICONINFORMATION | #MB_TOPMOST)
;   End
  Define Now$ = FormatDate("%yy.%mm.%dd", Date())
  If #AppVer$ <> Now$
    Message$ = "Changer la version "+#AppVer$+" en "+Now$
    messageBox()
    End
  EndIf
  Define Min = Date(Year(Date()),Month(Date()),01,0,0,1)
  Define Max = Date(Year(Date()),Month(Date()),28,0,0,1)
  If #MINDATE <> Min
    Message$ = "Changer #MINDATE "+#MINDATE+" en "+Str(Min)
    messageBox()
    End
  EndIf
  If #MAXDATE <= Min
    Message$ = "Changer #MAXDATE "+#MAXDATE+" en "+Str(Max)
    messageBox()
    End
  EndIf
CompilerElse
  If #BIT$ = "32" And Bool(GetEnvironmentVariable("PROGRAMW6432") <> #Null$)
    Message$ = "You must use the 64-bit version of this application."
  EndIf
  Define Disk.l
  Disk = GetDriveType_(AppDisk$)
  If Disk = 4 Or AppDisk$ = "\\"
    Message$ = "Disk " + AppDisk$ + " not supported"
  EndIf
  If #FOLDER And Right(AppPath$,Len(FOLDER$)+2) <> "\"+FOLDER$+"\"
    Message$ = "The installation Folder must be named "+FOLDER$
  EndIf
  If AppName$ <> LCase(#PROGRAM$)
    Message$ = "Rename " + AppName$ + " to " + #PROGRAM$ + " - Thanks."
  EndIf
  If #WRITE
    Define foo$ = "~"+#MyApp$+".tmp"
    SetFileAttributes(foo$, #PB_FileSystem_Normal)
    DeleteFile(foo$, #PB_FileSystem_Force)
    If CreateFile(0, foo$)
      CloseFile(0)
      DeleteFile(foo$, #PB_FileSystem_Force)
    Else
      Message$ = "You don't have write permissions to this folder:"+#CRLF$+#CRLF$
      Message$+LCase(AppPath$)+#CRLF$+#CRLF$
      Message$+"Failed to launch the application."
      messageBox()
      End
    EndIf
    foo$ = #Null$
  EndIf
  If TB_Message
    ;Mutex = CreateMutex_(0,1,#MyApp$)
    Mutex = CreateMutex_(#Null, #False, #MyApp$)
    If GetLastError_() = #ERROR_ALREADY_EXISTS And #RUNONE ; #MONO_INSTANCE
      ;;;
      ;;; Same Mutex in memory, that same app is running ???
      ;;; 
      Define hWnd = FindWindow_("WindowClass_"+Str(#WinApp), #NUL)
      If hWnd
        ;;;
        ;;; Same Mutex in memory and same ClassName, no doubt, is the same app.
        ;;;
        SendMessage_(hWnd,#WM_SYSCOMMAND, #SC_RESTORE,0)
        SetForegroundWindow_(hWnd)
        End
      Else
        Message$ = AppName$ + " is already running."
      EndIf
    EndIf
  Else
    ;; Desktop is not activated, then no TaskBar (TB)
    End
  EndIf
  If Message$ <> #Null$
    messageBox()
    End
  EndIf
CompilerEndIf

Procedure checkRequireFileExist(f$)
  If Not ReadFile(#PB_Any, f$)
    Message$ = "File " + f$ +" is required by this tool !" + #CRLF$ + #CRLF$
    Message$ + "Copy this file to the folder : " + #CRLF$ + #CRLF$
    Message$ + AppPath$
    messageBox()
    End
  Else
    CloseFile(#PB_All)
  EndIf
EndProcedure

Procedure appAbout()
  Message$ = "This tool is copyrighted © 2017 by CAGE" + Chr(10) + Chr(10)
  Message$ + "Thanks to PureBasic : www.purebasic.com"
  messageBox()
EndProcedure

Procedure appQuit()
  Protected confirm = #PB_MessageRequester_Yes
  If optConfirm
    Message$ = "Want to close application ! Are you sure ?"
    confirm = messageBox(#True)
  EndIf
  If confirm = #PB_MessageRequester_Yes
    ProcedureReturn #True
  Else
    ProcedureReturn #False
  EndIf
EndProcedure

Procedure hideAppWindow()
  If optMinimize
    HideWindow(#WinApp, #True)
  EndIf
  SetWindowState(#WinApp, #PB_Window_Minimize)
EndProcedure

Procedure showAppWindow()
  HideWindow(#WinApp, #False) ; , #PB_Window_ScreenCentered)
  SetWindowState(#WinApp, #PB_Window_Normal)
EndProcedure

Procedure.a fileExist(f$)
  If FileSize(f$) < 0
    ProcedureReturn #False
  Else
    ProcedureReturn #True
  EndIf
EndProcedure

Procedure.a folderExist(f$)
  If FileSize(f$) = -2
    ProcedureReturn #True
  Else
    ProcedureReturn #False
  EndIf
EndProcedure

Procedure.i getNamePID(ProcessName.s,*ptrPID)
  Protected HSnap.l,Prec.processentry32,result.l=#False
  HSnap=CreateToolhelp32Snapshot_(#TH32CS_SNAPPROCESS,0)
  If HSnap=#INVALID_HANDLE_VALUE
    ProcedureReturn result
  EndIf
  Prec\dwSize=SizeOf(prec)
  If Not Process32First_(HSnap,Prec)
    ProcedureReturn result
  EndIf
  Repeat
    If LCase(GetFilePart(PeekS(@Prec\szexefile)))=LCase(ProcessName)
      PokeL(*ptrPID,Prec\th32ProcessID)
      result=#True
      Break
    EndIf
  Until Not Process32Next_(HSnap,Prec)
  CloseHandle_(HSnap)
  ProcedureReturn result
EndProcedure

Procedure.i FindWindowTitle(tSearch.s,*ptrHW)
  Protected hWnd.i, tWnd.s
  hWnd = GetWindow_(GetDesktopWindow_(), #GW_CHILD)
  While hWnd
    tWnd = Space(GetWindowTextLength_(hWnd) + 2)
    GetWindowText_(hWnd, tWnd, Len(tWnd))
    If FindString(LCase(tWnd), LCase(tSearch))
      PokeL(*ptrHW,hWnd)
      ;SendMessage_(hWnd,#WM_SYSCOMMAND, #SC_RESTORE,0)
      ;SetForegroundWindow_(hWnd)
      ProcedureReturn #True
    EndIf
    hWnd = GetWindow_(hWnd, #GW_HWNDNEXT)
  Wend
  ProcedureReturn #False
EndProcedure

; IDE Options = PureBasic 5.60 (Windows - x64)
; CursorPosition = 16
; Folding = AAA1
; EnableXP
; CompileSourceDirectory
; EnablePurifier
; EnableUnicode