; thinclient.pb
; ------------------------------------------------------------
; wrapper for mstsc.exe & vnc.exe (tvnviewer.exe) 
; LICENSE  : GPL
; AUTHOR   : Michael H.G. Schmidt
; EMAIL    : michael@schmidt2.de
; DATE     : 20240201
; ------------------------------------------------------------
;

; preparations to use native "system" command ...
ImportC "msvcrt.lib"
  system(str.p-ascii)
EndImport

; Get system drive
Global SYS$=GetEnvironmentVariable("SystemDrive")

Procedure StartRDP()
  ; start RDP ...
  dummy=RunProgram(SYS$ + "\tools\rdp.exe","/kiosk:cup /ontop /nodrives /noprinters /span","")
EndProcedure

Procedure StartVNC()
  ; start VNC ...
  dummy=RunProgram(SYS$ + "\tools\tvnviewer.exe")
EndProcedure

Procedure Poweroff()
  ; Poweroff ...
  dummy=system("shutdown /s /f /t 0 /d p:0:0")
EndProcedure

; Button handler procedure
Procedure ButtonHandler()
  
  Select EventGadget()    
    Case 0
      StartRDP()
    Case 1
      StartVNC()
    Case 2
      Poweroff()
  EndSelect

EndProcedure

;;;;;;;
; MAIN
;;;;;;;

; open window to hide all other windows on desktop ...
OpenWindow(0, 0, 0, 0, 0, "", #PB_Window_BorderLess | #PB_Window_Maximize)
 
; set colour to windows blue ...
SetWindowColor(0, RGB($04,$92,$c2)) 

; Get window size
W = WindowWidth(0)
H = WindowHeight(0)

; Shrink factor for buttons
S=0.1

; position calculations for the buttons
X0=W/2-(W*S/2)

GAP=W*0.01
ButtonW=W*0.1
ButtonH=H*0.1

BX0=X0 - (ButtonW*2 + GAP)/2
BX1=BX0 + ButtonW + GAP
BX2=W - ButtonW - W/100

BY=H/2-ButtonH/2
BY2=H-ButtonH - ButtonH/10

; use a bigger font the buttons
LoadFont(0, "MS Shell Dlg", 16)
SetGadgetFont(#PB_Default, FontID(0))

; Create Buttons
ButtonGadget(0, BX0, BY, ButtonW, ButtonH, "Remote Desktop", #PB_Button_MultiLine)
BindGadgetEvent(0, @ButtonHandler())

ButtonGadget(1, BX1, BY, ButtonW, ButtonH, "VNC viewer", #PB_Button_MultiLine)
BindGadgetEvent(1, @ButtonHandler())

ButtonGadget(2, BX2, BY2, ButtonW, ButtonH, "Poweroff", #PB_Button_MultiLine)
BindGadgetEvent(2, @ButtonHandler())

; MAIN LOOP
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

; IDE Options = PureBasic 5.73 LTS (Windows - x64)
; CursorPosition = 28
; FirstLine = 17
; Folding = -
; EnableXP