File: n_mrulist.sru
Size: 3719
Date: Fri, 01 Feb 2019 02:42:56 +0100
$PBExportHeader$n_mrulist.sru
forward
global type n_mrulist from datastore
end type
end forward

global type n_mrulist from datastore
string dataobject = "d_mrulist"
end type
global n_mrulist n_mrulist

type variables
Long MaxEntries = 8

end variables

forward prototypes
public subroutine of_savelist ()
public subroutine of_addtolist (string as_filename)
public subroutine of_loadlist ()
public subroutine of_recentfiles (ref menu am_menuitem)
public subroutine of_removefromlist (string as_filename)
public function string of_mostrecentfile ()
end prototypes

public subroutine of_savelist ();// save MRU List in the registry

Long ll_row, ll_max
String ls_FileName, ls_data, ls_name
DateTime ldt_WhenAccessed

gn_app.of_DelRegkey("MRUList")

ll_max = this.RowCount()
For ll_row = 1 To ll_max
   ldt_WhenAccessed = this.GetItemDateTime(ll_row, "whenaccessed")
   ls_FileName = this.GetItemString(ll_row, "filename")
   ls_data = String(ldt_WhenAccessed) + "~t" + ls_FileName
   ls_name = "File" + String(ll_row)
   gn_app.of_SetReg("MRUList", ls_name, ls_data)
Next

end subroutine

public subroutine of_addtolist (string as_filename);// add a file to the MRU List

Long ll_row
String ls_find

// remove current entry for this file
ls_find = "lower(filename) = '" + Lower(as_filename) + "'"
ll_row = this.Find(ls_find, 1, this.RowCount())
If ll_row > 0 Then
   this.DeleteRow(ll_row)
End If

// add file to the list
ll_row = this.InsertRow(1)
this.SetItem(ll_row, "whenaccessed", DateTime(Today(), Now()))
this.SetItem(ll_row, "filename", as_filename)
this.Sort()

// delete excess
this.RowsDiscard(MaxEntries + 1, this.RowCount(), Primary!)

end subroutine

public subroutine of_loadlist ();// load MRU List from the registry

Long ll_row, ll_max
String ls_regkey, ls_Values[], ls_data

ls_regkey = gn_app.REGISTRYKEY + "\MRUList"
RegistryValues(ls_regkey, ls_Values)

ll_max = UpperBound(ls_Values)
For ll_row = 1 To ll_max
   ls_data = gn_app.of_GetReg("MRUList", ls_Values[ll_row], "")
   this.ImportString(ls_data)
Next

this.Sort()

end subroutine

public subroutine of_recentfiles (ref menu am_menuitem);// populate the recent files menu

Integer li_item
Long ll_row, ll_max
String ls_filename, ls_menutext

// add the items
ll_max = this.RowCount()
For ll_row = 1 To ll_max
   ls_filename = this.GetItemString(ll_row, "filename")
   If FileExists(ls_filename) Then
      // add menu item
      ls_menutext = "&" + String(ll_row) + " " + ls_filename
      li_item = gn_dyn.of_AddItem(am_menuitem, ls_menutext)
      // save filename in tag
      gn_dyn.of_SetItem(li_item, "tag", ls_filename)
      // save filename in microhelp
      ls_menutext = "Open " + ls_filename
      gn_dyn.of_SetItem(li_item, "microhelp", ls_menutext)
   End If
Next

end subroutine

public subroutine of_removefromlist (string as_filename);// remove a file from the MRU List

Long ll_row
String ls_find

// remove current entry for this file
ls_find = "lower(filename) = '" + Lower(as_filename) + "'"
ll_row = this.Find(ls_find, 1, this.RowCount())
If ll_row > 0 Then
   this.DeleteRow(ll_row)
End If

end subroutine

public function string of_mostrecentfile ();// return the most recent filename

Long ll_row, ll_max
String ls_filename

ll_max = this.RowCount()
For ll_row = 1 To ll_max
   ls_filename = this.GetItemString(ll_row, "filename")
   If FileExists(ls_filename) Then
      Return ls_filename
   End If
Next

SetNull(ls_filename)

Return ls_filename

end function

on n_mrulist.create
call super::create
TriggerEvent( this, "constructor" )
end on

on n_mrulist.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on