File: w_main.srw
Size: 6753
Date: Tue, 02 Aug 2022 15:45:34 +0200
$PBExportHeader$w_main.srw
forward
global type w_main from window
end type
type mdi_1 from mdiclient within w_main
end type
type point from structure within w_main
end type
type minmaxinfo from structure within w_main
end type
end forward

type point from structure
   long     lx
   long     ly
end type

type minmaxinfo from structure
   point    ptreserved
   point    ptmaxsize
   point    ptmaxposition
   point    ptmintracksize
   point    ptmaxtracksize
end type

global type w_main from window
integer width = 4174
integer height = 2424
boolean titlebar = true
string title = "PSR Viewer"
string menuname = "m_main"
boolean controlmenu = true
boolean minbox = true
boolean maxbox = true
boolean resizable = true
windowtype windowtype = mdihelp!
long backcolor = 67108864
string icon = "AppIcon!"
event getminmaxinfo pbm_getminmaxinfo
event syscommand pbm_syscommand
mdi_1 mdi_1
end type
global w_main w_main

type prototypes
Subroutine GetMinMaxInfo ( &
   Ref MINMAXINFO Destination, &
   longptr Source, &
   long Length &
   ) Library "kernel32.dll" Alias For "RtlMoveMemory"

Subroutine SetMinMaxInfo ( &
   longptr Destination, &
   MINMAXINFO Source, &
   long Length &
   ) Library "kernel32.dll" Alias For "RtlMoveMemory"

end prototypes

type variables
Integer MinHeight = 2500
Integer MinWidth  = 1650

end variables

forward prototypes
public subroutine wf_restorewindowstate ()
public subroutine wf_savewindowstate ()
public subroutine wf_recentfiles ()
public subroutine wf_dynamicmenus (integer ai_index)
end prototypes

event getminmaxinfo;MINMAXINFO lstr_minmaxinfo

// copy the data to local structure
GetMinMaxInfo(lstr_minmaxinfo, minmaxinfo, 40)

// set the minimum size for our window
lstr_minmaxinfo.ptMinTrackSize.lx = UnitsToPixels(MinHeight, XUnitsToPixels!)
lstr_minmaxinfo.ptMinTrackSize.ly = UnitsToPixels(MinWidth, YUnitsToPixels!)

// copy the structure back into memory
SetMinMaxInfo(minmaxinfo, lstr_minmaxinfo, 40)

// important: must return 0
Return 0

end event

event syscommand;Constant ulong SC_RESTORE = 61728 // 0xF120
Integer li_xpos, li_ypos, li_hght, li_wdth

li_xpos = Integer(gn_app.of_GetReg("Window", "XPos", String(this.X)))
li_ypos = Integer(gn_app.of_GetReg("Window", "YPos", String(this.Y)))
li_hght = Integer(gn_app.of_GetReg("Window", "Width", String(this.Width)))
li_wdth = Integer(gn_app.of_GetReg("Window", "Height", String(this.Height)))

// restore window to previous size after Restore (maximized button was clicked)
If commandtype = SC_RESTORE Then
   this.Post Move(li_xpos, li_ypos)
   this.Post Resize(li_hght, li_wdth)
End If

end event

public subroutine wf_restorewindowstate ();Integer li_xpos, li_ypos, li_width, li_height
String ls_value

// restore window position
li_xpos   = Integer(gn_app.of_GetReg("Window", "XPos", String(this.X)))
li_ypos   = Integer(gn_app.of_GetReg("Window", "YPos", String(this.Y)))
Move(li_xpos, li_ypos)

// restore window size
li_width  = Integer(gn_app.of_GetReg("Window", "Width", String(this.Width)))
li_height = Integer(gn_app.of_GetReg("Window", "Height", String(this.Height)))
Resize(li_width, li_height)

// restore windowstate
ls_value = gn_app.of_GetReg("Window", "WindowState", "Normal")
If Lower(ls_value) = "maximized" Then
   // maximize window
   gn_app.of_ShowWindow(this, Maximized!)
Else
   // normalize window
   gn_app.of_ShowWindow(this, Normal!)
End If

this.Visible = True

end subroutine

public subroutine wf_savewindowstate ();// save window position/size
If this.WindowState = Normal! Then
   gn_app.of_SetReg("Window", "XPos", String(this.X))
   gn_app.of_SetReg("Window", "YPos", String(this.Y))
   gn_app.of_SetReg("Window", "Width", String(this.Width))
   gn_app.of_SetReg("Window", "Height", String(this.Height))
   gn_app.of_SetReg("Window", "WindowState", "Normal")
Else
   gn_app.of_SetReg("Window", "WindowState", "Maximized")
End If

end subroutine

public subroutine wf_recentfiles ();// populate the recent files menu

Application la_app
m_main lm_menu
Menu lm_item
String ls_value

// reset the menu
this.ChangeMenu(m_main)

// get reference to the menu
lm_menu = this.MenuID

// get reference to the menu item
lm_item = lm_menu.m_file.m_recentfiles

// set the parent window
gn_dyn.of_SetParent(this)

// update recent files menu item
gn_mru.of_RecentFiles(lm_item)

// update toolbar size
ls_value = gn_app.of_GetReg("Window", "ToolbarText", "false")
If ls_value = "true" Then
   lm_menu.mf_BigToolbarButtons(True)
Else
   lm_menu.mf_BigToolbarButtons(False)
End If

end subroutine

public subroutine wf_dynamicmenus (integer ai_index);// Dynamic Menu Item Clicked

w_sheet lw_sheet
String ls_filename

// get filename from tag
ls_filename = gn_dyn.of_GetItemString(ai_index, "tag")

If Not FileExists(ls_filename) Then
   // update the MRU List
   gn_mru.of_RemoveFromList(ls_filename)
   
   // reset recent files on all sheets
   lw_sheet = gw_frame.GetFirstSheet()
   do while IsValid(lw_sheet)
      lw_sheet.wf_RecentFiles()
      lw_sheet = gw_frame.GetNextSheet(lw_sheet)
   loop

   MessageBox("Recent Files", &
         "The file you chose does not exist!", StopSign!)
   Return
End If

// update the MRU List
gn_mru.of_AddToList(ls_filename)

// update the menu items
wf_RecentFiles()

// open the file
OpenSheetWithParm(lw_sheet, ls_filename, "w_sheet", gw_frame, 0, Layered!)

end subroutine

on w_main.create
if this.MenuName = "m_main" then this.MenuID = create m_main
this.mdi_1=create mdi_1
this.Control[]={this.mdi_1}
end on

on w_main.destroy
if IsValid(MenuID) then destroy(MenuID)
destroy(this.mdi_1)
end on

event open;w_sheet lw_sheet
String ls_cmdline, ls_filename

gw_frame = this

// load list from registry
gn_mru.of_LoadList()

// add items to menu
wf_RecentFiles()

// restore window state
wf_RestoreWindowState()

// check for passed filename
ls_cmdline = CommandParm()
If ls_cmdline <> "" Then
   ls_filename = Trim(ls_cmdline)
   If Left(ls_filename, 1) = "~"" And &
      Right(ls_filename, 1) = "~"" Then
      ls_filename = Mid(ls_filename, 2, Len(ls_filename) - 2)
   End If
   OpenSheetWithParm(lw_sheet, ls_filename, "w_sheet", gw_frame, 0, Layered!)
End If

end event

event activate;// move if positioned outside the boundaries of any monitor
If Not gn_app.of_IsOnMonitor(this) Then
   this.Post Move(10, 10)
End If

end event

event close;// save list to registry
gn_mru.of_SaveList()

// save window state
wf_SaveWindowState()

end event

event resize;// save window state
If sizetype = 0 Then
   wf_SaveWindowState()
End If

end event

type mdi_1 from mdiclient within w_main
long BackColor=268435456
end type