13.1.2 Source code - Module MM. module

Even with a simple menu on the form, the source code for a project quickly becomes confusing, because you have to distinguish between the procedures for generating the menus, the procedures for assigning the program actions to the individual menus, and the procedures that describe the program actions. It is a good idea to place the source code in a module for generating the individual menus.

The source code for the module and the project MenuQM are completely specified and commented in substantial parts.

[1] ' Gambas class file
[2]
[3] Private mnuMenu1 As Menu
[4] Private mnuMenu2 As Menu
[5] Private mnuMenu3 As Menu
[6]
[7] Private mnu11Open As Menu
[8] Private mnu12New As Menu
[9] Private mnu13Save As Menu
[10] Private mnu14SaveAs As Menu
[11] Private mnu15Print As Menu
[12] Private mnu16Close As Menu
[13] Private mnu21Optionen As Menu
[14] Private mnu22SelectColor As Menu
[15] Private mnu23PreView As Menu
[16] Private mnu221Palette1 As Menu
[17] Private mnu222Palette2 As Menu
[18] Private mnu31Help As Menu
[19] Private mnu32Information As Menu
[20] Private mnuSpace As Menu
[21]
[22] Public Sub ShowMenu()
[23]
[24]   mnuMenu1 = New Menu(FMain, False) ' 1. Entry in the menu bar , visible
[25]   mnuMenu1.Caption = "Image-File"
[26]
[27]   mnu11Open = New Menu(mnuMenu1) As "mnuOpen"
[28]   mnu11Open.Caption = "Open Image-File..."
[29]   mnu11Open.Shortcut = "CTRL+O"
[30]   mnu11Open.Picture = Picture["icon:/16/open"]
[31]   mnu12New = New Menu(mnuMenu1) As "mnuNew"
[32]   mnu12New.Caption = "Create new Image-File"
[33]   mnu12New.Shortcut = "CTRL+N"
[34]   mnu12New.Picture = Picture["icon:/16/new"]
[35]   mnu13Save = New Menu(mnuMenu1) As "mnuSave"
[36]   mnu13Save.Caption = "Save"
[37]   mnu13Save.Shortcut = "CTRL+S"
[38]   mnu13Save.Picture = Picture["icon:/16/save"]
[39]   mnu13Save.Enabled = False
[40]   mnu14SaveAs = New Menu(mnuMenu1) As "mnuSaveAs"
[41]   mnu14SaveAs.Caption = "Save file as..."
[42]   mnu14SaveAs.Shortcut = "CTRL+SHIFT+S"
[43]   mnu14SaveAs.Picture = Picture["icon:/16/save-as"]
[44]   mnu14SaveAs.Enabled = False
[45]   mnuSpace = New Menu(mnuMenu1)
[46]   mnuSpace.Caption = ""
[47]   mnu15Print = New Menu(mnuMenu1) As "mnuPrint"
[48]   mnu15Print.Caption = "Print picture"
[49]   mnu15Print.Picture = Picture["icon:/16/print"]
[50]   mnu15Print.Enabled = False
[51]   mnuSpace = New Menu(mnuMenu1)
[52]   mnuSpace.Caption = ""
[53]   mnu16Close = New Menu(mnuMenu1) As "mnuClose"
[54]   mnu16Close.Caption = "Close"
[55]   mnu16Close.Action = "FormClose"
[56]   mnu16Close.Picture = Picture["icon:/16/quit"]
[57] '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[58]   mnuMenu2 = New Menu(FMain, False) ' 2. Entry in the menu bar , visible
[59]   mnuMenu2.Caption = "Extras"
[60]
[61]   mnu21Optionen = New Menu(mnuMenu2) As "mnuOptionen"
[62]   mnu21Optionen.Caption = "Optiones"
[63]   mnu21Optionen.Picture = Picture["icon:/16/options"]
[64]   mnu22SelectColor = New Menu(mnuMenu2) As "mnuSelectColor"
[65]   mnu22SelectColor.Caption = "Color pallets"
[66]   mnu22SelectColor.Picture = Picture["icon:/16/fill"]
[67]   mnu221Palette1 = New Menu(mnu22SelectColor) As "mnuPalette1"
[68]   mnu221Palette1.Caption = "Color chart 1"
[69]   mnu221Palette1.Picture = Picture["icon:/16/pen"]
[70]   mnuSpace = New Menu(mnu22SelectColor)
[71]   mnuSpace.Caption = ""
[72]   mnu222Palette2 = New Menu(mnu22SelectColor) As "mnuPalette2"
[73]   mnu222Palette2.Caption = "Color chart2"
[74]   mnu222Palette2.Picture = Picture["icon:/16/color"]
[75]   mnu23PreView = New Menu(mnuMenu2) As "mnuPreView"
[76]   mnu23PreView.Caption = "Preview"
[77]   mnu23PreView.Checked = True
[78]   mnu23PreView.Toggle = True
[79] '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[80]   mnuMenu3 = New Menu(FMain, False) ' 3. Entry in the menu bar , visible
[81]   mnuMenu3.Caption = "Hilfe"
[82]
[83]   mnu31Help = New Menu(mnuMenu3) As "mnuHelpMe"
[84]   mnu31Help.Caption = "Help"
[85]   mnu31Help.Shortcut = "F1"
[86]   mnu31Help.Action = "HelpMe"
[87]   mnu31Help.Picture = Picture["icon:/16/help"]
[88]   mnu32Information = New Menu(mnuMenu3) As "mnuInformation"
[89]   mnu32Information.Caption = "Online-Hilfe"
[90]   mnu32Information.Picture = Picture["icon:/16/internet"]
[91]
[92] End ' ShowMenu()
[93]
[94] Public Sub mnuOpen_Click()
[95]   FMain.ImageOpen()
[96]   mnu15Print.Enabled = True
[97] End ' mnuOpen_Click()
[98]
[99] Public Sub mnuNew_Click()
[100]   mnu13Save.Enabled = False
[101]   mnu14SaveAs.Enabled = True
[102]   FMain.CreateImage
[103] End ' mnuNew_Click()
[104]
[105] Public Sub mnuSaveAs_Click()
[106]   FMain.SaveAsDialog
[107]   mnu13Save.Enabled = True
[108]   mnu14SaveAs.Enabled = False
[109] End '  mnuSaveAs_Click()
[110]
[111] Public Sub mnuSave_Click()
[112]   FMain.SaveImage
[113] End '  mnuSaveAs_Click()
[114]
[115] Public Sub mnuPrint_Click()
[116]   FMain.PrintImage
[117] End ' mnuPrint_Click()
[118]
[119] Public Sub mnuClose_Click()
[120]   FHelp.Close
[121]   FMain.Close
[122] End ' mnuClose_Click()
[123]
[124] Public Sub mnuOptionen_Click()
[125]  FMain.SetOptions
[126] End ' mnuOptionen_Click()
[127]
[128] Public Sub mnuPalette1_Click()
[129]   FMain.GetColor_1()
[130] End ' mnuPalette1_Click()
[131]
[132] Public Sub mnuPalette2_Click()
[133]   FMain.GetColor_2()
[134] End ' mnuPalette2_Click()
[135]
[136] Public Sub mnuPreView_Click()
[137]   If mnu23PreView.Checked = True Then
[138]      Message.Info("Show future preview image or" & gb.NewLine & "generate or...")
[139]   Endif ' Checked = True
[140] End ' mnu23PreView_Click()
[141]
[142] Public Sub mnuHelpMe_Click()
[143]   FHelp.Show
[144] End ' mnuHelpMe_Click()
[145]
[146] Public Sub mnuInformation_Click()
[147]   Desktop.Open("http://www.gambas-buch.de/dw/doku.php?id=k13:start")
[148] End ' mnu32Information_Click()