TMRU - Most Recently Used File List

Source: source/classes/mru.prg

TMRU manages a Most Recently Used (MRU) file list, typically displayed in the File menu of applications. It persists the list to an INI file (via TIni) and automatically updates a popup menu. When a user opens a file, the MRU list is updated with the new entry at the top, and older entries shift down or are removed if the list exceeds the maximum size. Selecting an MRU menu item fires the associated bAction codeblock.

DATA Members

DATATypeDescription
aItemsArrayArray of file path strings in MRU order (most recent first)
nMaxItemsNumericMaximum number of items in the MRU list (default 4)
cIniFileCharacterPath to the INI file for persisting the MRU list
cSectionCharacterINI section name where MRU entries are stored
oMenuObjectPopup menu object that displays the MRU entries
bActionCodeblockCodeblock executed when an MRU item is selected, receives the file path

Methods

MethodDescription
New( cIni, cSec, nMax, cMsg, bAction, oMenu, nPos )Create MRU list. Loads from INI, creates popup menu items, and attaches them to oMenu at position nPos
Save( uNew )Add a new file to the top of the MRU list. If it already exists, move it to the top. Persists to INI. uNew can be a single file or an array of files
Clear()Clear the MRU list, remove all menu items, and delete the INI section

Example: MRU Menu in File Menu

#include "FiveWin.ch"

function Main()

   local oWnd, oMenu, oMru

   DEFINE WINDOW oWnd TITLE "MRU Demo"

   MENU oMenu
      MENUITEM "~File"
      POPUP
         MENUITEM "~Open..."   ACTION OpenFile( oWnd, oMru )
         MENUITEM "~Save"      ACTION MsgInfo( "Save" )
         SEPARATOR
         // MRU list will be inserted here at position 4
      ENDPOPUP
   ENDMENU

   oWnd:oMenu := oMenu

   // Create MRU with max 5 items, attached to File menu at position 4
   oMru := TMRU():New( "C:\MyApp\settings.ini", "MRU", 5, ;
                       "Open recent file", {|cFile| OpenRecent( cFile ) }, ;
                       oMenu, 4 )

   ACTIVATE WINDOW oWnd

return nil

function OpenFile( oWnd, oMru )

   local cFile := cGetFile( "All files (*.*)|*.*" )

   if ! Empty( cFile )
      oMru:Save( cFile )   // Add to MRU and update menu
      // ... open the file
   endif

return nil

function OpenRecent( cFile )

   MsgInfo( "Opening recent: " + cFile )
   // ... open the file

return nil

See Also