; minibrowser.pb
; ------------------------------------------------------------
; This is a minimalistic browser for admin purposes.
; It was derived from: MiniBrowser, (c) Fantaisie Software
; LICENSE  : GPL
; AUTHOR   : Michael H.G. Schmidt
; EMAIL    : michael@schmidt2.de
; DATE     : 20230214
; ------------------------------------------------------------
;


; VARIABLES
Global Security=#True
Global DefaultStartPage$="http://Bing.de"
Global StartPage$=DefaultStartPage$

Procedure ResizeWebWindow()
  ResizeGadget(10, #PB_Ignore, #PB_Ignore, WindowWidth(0), WindowHeight(0)-40)
  ResizeGadget(4, #PB_Ignore, #PB_Ignore, WindowWidth(0)-220, #PB_Ignore)
  ResizeGadget(5, WindowWidth(0)-30, #PB_Ignore, #PB_Ignore, #PB_Ignore)
  ResizeGadget(6, #PB_Ignore, #PB_Ignore, WindowWidth(0), #PB_Ignore)
EndProcedure

Procedure JavaScriptErrorMessages(WebGadget,MyState)
  Protected MyWebGadget.IWebBrowser2
  MyWebGadget=GetWindowLongPtr_(GadgetID(Webgadget), #GWL_USERDATA)
  MyWebGadget\put_Silent(MyState)
EndProcedure

Procedure toggleButton()
  
  BTN$ = GetGadgetText(1)
 
  If ( BTN$ = "Secure" )
    SetGadgetText(1, "INSECURE")
    Security = #False
  EndIf
  
  If ( BTN$ = "INSECURE" )
    SetGadgetText(1, "Secure")
    Security = #True
  EndIf
  
  JavaScriptErrorMessages(10,Security)
EndProcedure

Procedure Usage()
  MessageRequester("MiniBrowser",
    "usage: minibrowser < [/? | /I] [ URL ]" + Chr(13) + "  URL: webpage To show on start" + Chr(13) + "  /I = ignore security (SSL)" + Chr(13) + "  /? = show help",
    #PB_MessageRequester_Info)
	End 99
EndProcedure

; get arguments / check commandline ...
If ( CountProgramParameters() = 0 )
  StartPage$ = DefaultStartPage$
EndIf

If ( CountProgramParameters() >= 1 )
  If ( UCase(ProgramParameter(0)) = "/?" )
    Usage()
  ElseIf (UCase(ProgramParameter(0)) = "/I")
    StartPage$ = DefaultStartPage$
    Security = #False
  Else
    StartPage$ = ProgramParameter(0)
  EndIf
EndIf

If ( CountProgramParameters() = 2 )
  StartPage$ = ProgramParameter(1)  
EndIf

;
; MAIN
;

If OpenWindow(0, 100, 200, 800, 600, "MiniBrowser", #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget)
 
  LoadFont(1,"Courier",10, #PB_Font_Bold)
  SetGadgetFont(#PB_Default, FontID(1))
  If ( Security )
    ButtonGadget(1, 0, 3, 80, 25, "Secure")
  Else
    ButtonGadget(1, 0, 3, 80, 25, "INSECURE")
  EndIf
  SetGadgetFont(#PB_Default, #PB_Default)
  
  ButtonGadget(2, 80, 3, 50, 25, "Back")
  ButtonGadget(3, 130, 3, 50, 25, "Stop")
  
  StringGadget(4, 185, 5, 0, 20, StartPage$)
  ButtonGadget(5, 0, 3, 25, 25, "Go")
  FrameGadget(6, 0, 30, 0, 2, "", 2) ; Nice little separator
  
  WebGadget(10, 1, 37, 1278, 732,"")
  AddKeyboardShortcut(0, #PB_Shortcut_Return, 0)
  
  ; Use bindevent() to have a realtime window resize
  BindEvent(#PB_Event_SizeWindow, @ResizeWebWindow())
  ResizeWebWindow() ; Adjust the gadget to the current window size
  
  ; Use bindevent() to toggle the security button
  BindGadgetEvent(1, @toggleButton())
  
  SetGadgetState(10, #PB_Web_Refresh)
  JavaScriptErrorMessages(10,Security) ; Turn on/off Java Script Error Messages
  SetGadgetText(10, StartPage$)
  
  Repeat
    Event = WaitWindowEvent()
     
    Select Event
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1
            SetGadgetState(10, #PB_Web_Refresh)
          Case 2
            SetGadgetState(10, #PB_Web_Back)
          Case 3
            SetGadgetState(10, #PB_Web_Stop)
          Case 5
            SetGadgetText(10, GetGadgetText(4))
        EndSelect
        
      Case #PB_Event_Menu ; We only have one shortcut
        SetGadgetText(10, GetGadgetText(4)) 
    
    EndSelect
    
  Until Event = #PB_Event_CloseWindow

EndIf

; IDE Options = PureBasic 5.73 LTS (Windows - x64)
; CursorPosition = 98
; FirstLine = 77
; Folding = -
; EnableXP
; UseIcon = internet_free_icon.ico
; Executable = ..\tools\minibrowser.exe
; CompileSourceDirectory