User Tools

Site Tools


k13:k13.3:k13.3.2:start

13.3.2 Dynamic menu

The development environment (IDE) of Gambas sets in the menu bar under 'File' in the menu 'Recently opened' a menu list of k menus, which changes dynamically at runtime of the development environment. The list can be empty or contain entries whose number is limited. A menu with such properties is called dynamic menu.

IDE Gambas
Figure 13.3.2.1: Menu with menu list in the Gambas IDE

Planning the use of a dynamic menu requires, among other things, clear answers to the following questions:

(F1) Which menu in the menu structure should be equipped with a dynamic menu?
(F2) What will be saved in the menu list - recognizable by the sign >?
(F3) In which form should the entries in the menu list of the menu selected with (F1) be saved?
(F4) Is the selected menu not displayed or only deactivated if the menu list is empty?
(F5) What is the maximum number of entries to include in the menu list?
(F6) Should the entries in the menu list be sorted?

13.3.2.1 Preliminary considerations for the dynamic menu

The above 6 questions are answered project-related in the following project DynMenu:

MenüListe
Figure 13.3.2.1.1.1: Menu with menu list (project DynMenu)

  • The menu with the label 'Recently opened table' is to be assigned a menu list that changes dynamically. The name of the menu is mnuLastFiles. This menu is created in the same way as all other menus of the menu bar in the menu editor.
  • The menu list will contain a maximum of 5 menus. You enter the file names that are extracted from the paths of the most recently opened table files as labels. All menus in the list have the same object name and event name and are created dynamically.
  • Externally, the paths of the most recently opened table files are stored in a file lastfiles. conf. The methods of a Settings object store and read out the maximum of 5 entries in the menu list of the menu selected with (F1).
  • Internally, the paths of the last opened table files are stored in an array and another array synchronously records only the file names of the last opened table files.
  • If the menu list is empty - and this is guaranteed at the first start of the program - then the menu labeled 'Last opened table' will not be displayed.

Programmstart

  • The menu list should contain a maximum of 5 menus.
  • The entries in the menu list are not displayed sorted, because you will then no longer be able to recognize the name of the table file that was last opened. Only the order of opening determines the ranking in the menu list.
  • The menu list for the dynamic menu contains a maximum of 5 menus with the names of the last opened table files as labels. Whether the opened file has also been converted - i. e. edited - cannot be guaranteed. However, since a backup copy with the original file name but the extension.bak is created for each edited file, the starting point would be considerations for program extensions.

13.3.2.2 Project DynMenu

In a project, the preliminary considerations described in Section 13.3.2.1 are implemented when using a dynamic menu. This program has been in use since the beginning of the first publications of chapters of the online book on gambas-buch. de by the authors, when extensive tables have to be converted from a document - written with LibreOffice Writer - into the DokuWiki format.

13.3.2.3 Source Code Module MS. module

The procedures that create an instance of the Settings class and configure the Settings object, as well as importing and saving the paths of the recently opened table files, are stored in a module MS.module.

[1] ' Gambas module file
[2] ' MS = Module & Settings
[3]
[4] Private hFile As File
[5] Public pSettings As Settings
[6] Public Matrix As New String[]
[7] Public ShortMatrix As New String[]
[8] Public Const iLastNumber As Integer = 5
[9]
[10] Public Sub Init()
[11]   Dim sSettingsPath, sSlotName, sSettingsFileName As String
[12]
[13]   sSlotName = "LastFiles"
[14]   sSettingsPath = Application.Path &/ sSlotName
[15]   sSettingsFileName = "lastfiles.conf"
[16]
[17]   If Not Exist(sSettingsPath) Then
[18]      Mkdir sSettingsPath
[19]   Endif
[20]   If Not Exist(sSettingsPath &/ sSettingsFileName) Then
[21]      hFile = Open (sSettingsPath &/ sSettingsFileName) For Create
[22]      hFile.Close
[23]   Endif ' Not Exist(..)
[24]
[25] ' A Settings object sees the light in the memory ...
[26]   pSettings = New Settings(sSettingsPath &/ sSettingsFileName, "Filelist")
[27]
[28] End ' Init()
[29]
[30] Public Sub GetLastListFromSettings()
[31]   Dim iCount As Integer
[32]
[33]   pSettings.Reload() ' optional
[34]   Matrix.Clear
[35]   ShortMatrix.Clear
[36]   iCount = 0
[37]
[38]   While pSettings["FileList" & "/File" & Str(iCount)] <> Zero
[39]     If Exist(pSettings["FileList" & "/File" & Str(iCount)]) Then ' Does this file exist?
[40]        Matrix.Add(pSettings["FileList" & "/File" & Str(iCount)]) ' Save file path in matrix
[41]      ' Save file name in ShortMatrix
[42]        ShortMatrix.Add(File.Name(pSettings["FileList" & "/File" & Str(iCount)]))
[43]     Endif ' File.Exist?
[44]     Inc iCount
[45]   Wend ' <> Zero
[46]
[47] End ' GetLastListFromSettings()
[48]
[49] Public Sub SetLastListToSettings()
[50]   Dim iCount As Integer
[51]
[52]   pSettings.Clear
[53]     For iCount = 0 To Matrix.Max
[54]         pSettings[sSlotName & "/File" & Str(iCount)] = Matrix[iCount]
[55]       ' The maximum of 5 key-value pairs under the "LastFiles" slot are generated.
[56]     Next ' iCount
[57]   pSettings.Save ' Update the configuration file 'lastfiles.conf'.
[58]
[59] End ' SetLastListToSettings()

13.3.2.4 Source code FMain. class

The complete project can be found in the download area. Here, the source code - stored in the file FMain. class - is only played back and commented on in excerpts:

[1] ' Gambas class file
[2]
[3] Public fFile As File
[4] Public sCurrentFilePath As String
[5]
[6] Public Sub Form_Open()
[7]
[8]   FMain.Center
[9]   FMain.Resizable = False
[10]   mnu1ConvertTable.Enabled = False
[11]   mnu1SaveTable.Enabled = False
[12]   mnu2CenteredTable.Checked = False
[13]   btnReset.Enabled = False
[14]
[15]   MS.Init()
[16]   MS.GetLastListFromSettings()
[17]   UpdateMenu()
[18]
[19] End ' Form_Open()
[20]
[21] Public Sub UpdateMenu()
[22]   Dim MenuItem As Menu
[23]   Dim sFileName As String
[24]
[25]   mnu1LastFiles.Children.Clear ' Delete all old entries in the menu list of mnu1LastFiles.
[26] If MS.ShortMatrix.Count = 0 Then ' If the menu list is empty ...
[27] mnu1LastFiles.Visible = False ' then the menu 'mnu1LastFiles' is not visible
[28]   Else
[29]    ' Create menu list for the menu 'mnu1LastFiles'.
[30]      For Each sFileName In MS.ShortMatrix
[31]          MenuItem = New Menu(mnu1LastFiles) As "mnuLast" ' EventName is 'mnuLast'!
[32]          MenuItem.Text =  sFileName
[33]          MenuItem.Picture = Picture["icon:/16/insert-text"]
[34]      Next ' sFileName
[35]   Endif ' MS.ShortMatrix.Count = 0?
[36]
[37] End ' UpdateMenu
[38]
[39] Public Sub mnuLast_Click()
[40]   Dim iIndex As Integer
[41]
[42]   If MS.ShortMatrix.Find(Last.Text) <> -1 Then
[43]      iIndex = MS.ShortMatrix.Find(Last.Text)
[44]      sCurrentFilePath = MS.Matrix[iIndex]
[45]    ' Alternative: sCurrentFilePath = MS.Matrix[MS.ShortMatrix.Find(Last.Text)]
[46]      fFile = Open sCurrentFilePath For Input
[47]      Reset()
[48]      mnu1ConvertTable.Enabled = True
[49]      ShowTable()
[50]   Endif ' Find?
[51]
[52] End ' mnu1Last_Click
[53]
[54] Public Function OpenTable() As Boolean
[55]
[56]  Dialog.Title = "Import a table text file!"
[57] Dialog.Filter = ["*.txt", "Text Files"]
[58]
[59]  If Dialog.OpenFile(False) = True Then ' Multiselect = False (Standard)
[60]     Message.Info("The opening of the table file was cancelled!")
[61] Return False ' Cancel button pressed
[62]  Else
[63]    fFile = Open Dialog.Path For Input
[64]    If Lof(fFile) = 0 Then
[65]       Message.Info("The Text file is empty!")
[66]       fFile.Close
[67]       Return False
[68]    Endif ' Lof(fFile) = 0?
[69]  Endif ' Dialog.OpenFile(False) = True
[70]
[71]  sCurrentFilePath = Dialog.Path ' The DialogPath is assigned to the variable 'sCurrentFilePath'.
[72]
[73]    If Not MS.Matrix.Exist(Dialog.Path) Then ' If this file does not exist in the matrix ...
[74]       If MS.Matrix.Count = MS.iLastNumber Then ' and there are already 5 elements in the array Matrix,
[75]       MS.Matrix.Remove(0) ' then delete the 1st element in the array matrix
[76]          MS.ShortMatrix.Remove(0) ' then delete the 1st element in the array ShortMatrix.
[77]       Endif ' MS.Matrix.Count = MS.iLastNumber?
[78]       MS.Matrix.Add(Dialog.Path) ' The file path is inserted at the end of the array 'Matrix'.
[79]       MS.ShortMatrix.Add(File.Name(Dialog.Path)) ' File name is inserted at the end of 'ShortMatrix
[80]       UpdateMenu() ' The menu list is updated.
[81]   Endif ' Matrix.Exist?
[82]   Wait
[83]   Reset()
[84]   mnu1ConvertTable.Enabled = True
[85]   Return True
[86]
[87] End ' OpenTable()
[88]
[89] ...
[90]
[91] Public Sub Form_Close()
[92]   MS.SetLastListToSettings()
[93]   Try fFile.Close
[94] End ' Form_Close

Additional comments:

  • In lines 15 to 17, a Settings object is instantiated whose methods are used to read out the key value pairs (key value pairs) stored in the configuration file lastfiles. conf under the LastFiles slot and store them as elements (table file path) in the array matrix. Only the corresponding file names are stored in the ShortMatrix array. The menu list for the mnu1LastFiles menu is then generated from the contents of the ShortMatrix array. If this list is empty, the mnu1LastFiles menu is not visible.
  • The OpenTable() function in rows 54 to 87 is already adequately commented. In the file selection dialog only text files are displayed due to the filter setting. Within the function, line 80 calls the procedure UpDateMenu(), which causes the menu list of the dynamic menu to be refreshed.
  • When the program is terminated, the list of recently opened table files - stored in array matrix - is saved in the configuration file lastfiles.conf under the LastFiles slot (line 92). This means that the list is now available at the next program start.
  • Please note: If some of the last 5 table files opened have been deleted, moved or renamed before the next program startup, the following lines from the source code of the procedure GetLastListFromSettings() will provide the correct menu list.

Extract from the source code of the procedure GetLastListFromSettings():

[1] While pSettings["FileList" & "/File" & Str(icount)] <> Zero
[2]     If Exist(pSettings["FileList" & "/File" & Str(icount)]) Then ' Does this file exist?
[3] Matrix.Add(pSettings["FileList" & "/File" & Str(icount)]) ' Save file path in Matrix
[4]      ' Save file name in ShortMatrix
[5]        ShortMatrix.Add(File.Name(pSettings["FileList" & "/File" & Str(icount)]))
[6]     Endif ' File.Exist?
[7]     Inc icount
[8]   Wend ' <> ZERO

This is the content of the configuration file lastfiles.conf at a certain point in time:

# Dateiliste
[FileList]
File0="/home/hans/k12.3.4_tab.txt"
File1="/home/hans/k22.3.2_tab.txt"
File2="/home/hans/k3.7_tab.txt"

13.3.2.5 Download

Project

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k13/k13.3/k13.3.2/start.txt · Last modified: 02.02.2022 (external edit)

Page Tools