It is currently Sat Dec 14, 2019 1:24 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Persistent Recent Files Menu
PostPosted: Wed Dec 26, 2018 3:53 am 
Offline
Addict
Addict

Joined: Fri Aug 28, 2015 6:10 pm
Posts: 1046
Location: Portugal
There are other ways of doing this just search the forum.

Just recently opened filkes added to the menu preseved in prefs file between sessions. As demo prgramme on how to use.

Code:
Global Window_0,MainMenu.i

;Declare List To Hold Recent Filenames
Global NewList RecentFiles.s()

;Add recentfiles To the menu Enumeration
Enumeration FormMenu
  #mnuOpen
  #mnuRecentFiles
  #mnuRecentFile01
  #mnuRecentFile02
  #mnuRecentFile03
  #mnuRecentFile04
  #mnuExit
EndEnumeration

Procedure WriteRecentFileList()
 
  Define iLoop.i
 
    If OpenPreferences(GetCurrentDirectory() + "MyApp") = 0
   
    CreatePreferences(GetCurrentDirectory() + "MyApp")

    PreferenceGroup("RecentFiles")
   
    ClosePreferences()
   
  Else
 
      PreferenceGroup("RecentFiles")
      iLoop = 1
      ;Delete Old Group and Values
      RemovePreferenceGroup("RecentFiles")
      PreferenceGroup("RecentFiles")     
      ForEach (RecentFiles())
        WritePreferenceString("RecentFile" + Str(iLoop), RecentFiles())
        iLoop = iLoop + 1 
      Next
 
  ClosePreferences()
 
  EndIf

EndProcedure

Procedure SetMenus()
 
  ;Separate procefure to add the menu to the application
 
  Define iLoop.i
 
  If IsMenu(MainMenu)
    FreeMenu(MainMenu)
  EndIf
 
  MainMenu = CreateMenu(#PB_Any, WindowID(Window_0))
  MenuTitle("Files")
  MenuItem(#mnuOpen,"Open")
 
  ;Add Recent file list entries to menu If There are any
  If ListSize(RecentFiles()) > 0
    OpenSubMenu("Recent Files")
    FirstElement(RecentFiles())
    MenuItem(#mnuRecentFile01, RecentFiles())
   
    If NextElement(RecentFiles())
      MenuItem(#mnuRecentFile02, RecentFiles()) 
    EndIf
   
    If NextElement(RecentFiles())
      MenuItem(#mnuRecentFile03, RecentFiles()) 
    EndIf
   
    If NextElement(RecentFiles())
      MenuItem(#mnuRecentFile04, RecentFiles())     
    EndIf
     
    CloseSubMenu()
  Else
    ;No recent files so add place holder and disable
    MenuItem(#mnuRecentFiles,"Recent Files")
    DisableMenuItem(MainMenu, #mnuRecentFiles, #True)
  EndIf
  MenuItem(#mnuExit,"Exit")
     
EndProcedure

Procedure AddRecentFile(FileName.s)
 
  MaxFiles = 4
 
  ;Check for and remove duplicate entries
  ForEach recentfiles()
   
    If FileName = RecentFiles()
      DeleteElement(Recentfiles())     
    EndIf
   
  Next
 
  ;;Add this new entry at the start of the list
  FirstElement(Recentfiles())
  InsertElement(Recentfiles())
  RecentFiles() = FileName
 
  ;Remove last entry if required
  If ListSize(RecentFiles()) > MaxFiles
    LastElement(Recentfiles())
    DeleteElement(Recentfiles())
  EndIf 
 
EndProcedure

Procedure ReadRecentFileList()
 
  Define PrefRet.s
 
    If OpenPreferences(GetCurrentDirectory() + "MyApp") = 0
   
    CreatePreferences(GetCurrentDirectory() + "MyApp")

    PreferenceGroup("RecentFiles")
   
    ClosePreferences()
   
  Else
   
    PreferenceGroup("RecentFiles")

      PrefRet = ReadPreferenceString("RecentFile4", "Empty") 
      If PrefRet <> "Empty"   
        AddRecentFile(PrefRet)
      EndIf
      PrefRet = ReadPreferenceString("RecentFile3", "Empty")
       If PrefRet <> "Empty"
        AddRecentFile(PrefRet)
      EndIf     
      PrefRet = ReadPreferenceString("RecentFile2", "Empty")
      If PrefRet <> "Empty"
        AddRecentFile(PrefRet)
      EndIf       
      PrefRet = ReadPreferenceString("RecentFile1", "Empty")
      If PrefRet <> "Empty"
        AddRecentFile(PrefRet)
      EndIf       
   
  EndIf
 
 
 
EndProcedure

Procedure.s ChooseFile()
 
  Define FileName.s
 
  Filename = OpenFileRequester("Please choose file to load", "C:\", "All Files (*.*)|*.*;", 0)
  If Filename
   
    AddRecentfile(Filename)
    ProcedureReturn FileName
   
  Else
   
    ProcedureReturn ""
   
  EndIf
 
 
EndProcedure

  Window_0 = OpenWindow(#PB_Any, 0, 0, 600, 400, "", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  ReadRecentFileList()
  Setmenus()
 
  Repeat
     
    Event = WaitWindowEvent()
    Select Event
      Case #PB_Event_CloseWindow
           
        WriteRecentFileList()
        End
 
      Case #PB_Event_Menu
        Select EventMenu()
           
          Case #mnuOpen
            ChooseFile()

          Case #mnuRecentFile01
           
            Debug GetMenuItemText(MainMenu, #mnuRecentFile01)
           
          Case #mnuRecentFile02
 
            Debug GetMenuItemText(MainMenu, #mnuRecentFile02)
                       
          Case #mnuRecentFile03

            Debug GetMenuItemText(MainMenu, #mnuRecentFile03)                       
                       
          Case #mnuRecentFile04
 
            Debug GetMenuItemText(MainMenu, #mnuRecentFile04)                       
             
          Case #mnuExit
           
            WriteRecentFileList()
            End
           


Regards

CD

_________________
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.


Top
 Profile  
Reply with quote  
 Post subject: Re: Persistent Recent Files Menu
PostPosted: Wed Dec 26, 2018 4:08 am 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1904
Just a tip, the following code is prone to failure:

Code:
OpenPreferences(GetCurrentDirectory()+"MyApp")

This is because GetCurrentDirectory() doesn't always return the path to your app, as it will change when another app uses the SetCurrentDirectory API (be it your app, or another third-party app on your PC).

You need to use this instead if you want the actual path to your app:

Code:
Global appfolder$=GetPathPart(ProgramFilename())
OpenPreferences(appfolder$+"MyApp")

Then you can use appfolder$ everywhere in your app to point to your app's actual folder.


Top
 Profile  
Reply with quote  
 Post subject: Re: Persistent Recent Files Menu
PostPosted: Wed Dec 26, 2018 11:11 am 
Offline
Addict
Addict

Joined: Fri Aug 28, 2015 6:10 pm
Posts: 1046
Location: Portugal
Ah!

Does that work on Linux and MAC as well? Noticed some weird behaviour on Linux when creating a link.

Regards

CD

PS Just looked in help and it says it does. Will be using in future.

Thanks

_________________
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: mk-soft, Sirius-2337 and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye