VCLua v0.3.2

Creating VCLua application step by step

Tutorial 2

Actions

 tut02.lua

 

With VCLua actions you can improve your applications functionality. Actions can be used both by menus and toolbars, and changing an action property immediately effects these objects.

First you should create an ActionList, and load actions from a lua table

mainActions = VCL.ActionList(mainform,"mainActions")
mainActions:LoadFromTable({
    {name="fileOpenAction", caption="&Open...", shortcut="Ctrl+O", onexecute="onOpenAction", imageindex=0},
    {name="fileExitAction", caption="&Exit", shortcut="Alt+F4", onexecute="onExitAction", imageindex=1},
    {name="helpAboutAction", caption="&About", shortcut="Alt+F1", onexecute="onAboutAction", imageindex=2},
})

If the OnExecute method not assigned, then the action will be disabled. To enable/disable the action just assign the onexecute method

mainActions:Get("fileOpenAction").onexecute="onOpenAction" -- enabled
mainActions:Get("fileOpenAction").onexecute=nil -- disabled

Of course the action in itself do nothing. You need reference it from menu or from toolbar.

As you see the actions contains refrence to imageindexes. To continue our development we will need add an IimageList to the application.

mainImages = VCL.ImageList(mainForm,"mainImages")
mainImages:LoadFromTable({
    "images/open.png", "images/exit.png", "images/help.png"
})

Now you can build the action based menu and toolbar.

mainMenu = VCL.MainMenu(mainForm,"mainMenu")
mainMenu.Images = mainImages
mainMenu:LoadFromTable({
  {name="mmfile", caption="&File",
    submenu={
        {action=mainActions:Get("fileOpenAction")},
        {caption="-",},
        {action=mainActions:Get("fileExitAction")},
    }
  },
  {name="mmhelp", caption="&Help", RightJustify=true,
    submenu = {
        {action=mainActions:Get("helpAboutAction")},
    }
  }
})

mainToolbar = VCL.ToolBar(mainForm,"mainToolbar")
mainToolbar._ = { borderwidth =0, edgeborders = "[ebLeft,ebTop,ebRight,ebBottom]", edgeinner = "esRaised", edgeouter=  "esLowered", autosize = true, buttonwidth = 24, buttonheight = 24, align = alTop, flat = true }
mainToolbar.Images = mainImages
mainToolbar:LoadFromTable({
    {action=mainActions:Get("fileOpenAction")},
    {action=mainActions:Get("fileExitAction")},
    {style="tbsDivider"},
    {action=mainActions:Get("helpAboutAction")},
})