Skip to content
stevedonovan edited this page Feb 21, 2013 · 3 revisions

wxLua is a comprehensive binding to the wxWidgets cross-platform GUI framework.

It is straightforward to write Moonscript programs that use this API (provided they are on the Lua module binary path). The result is definitely cleaner than the equivalent Lua, not to mention the original C++!

-- tree.wx.moon
require "wx"

-- create a table to store any extra information for each node like this
-- you don't have to store the id in the table, but it might be useful
-- treedata[id] = { id=wx.wxTreeCtrlId, data="whatever data we want" }

treedata = {}

treedata.put = (id,str) ->
    val = id\GetValue!
    treedata[val] = id:val, data:str

any = wx.wxID_ANY
defpos = wx.wxDefaultPosition
defsize = wx.wxDefaultSize

main = ->
    frame = wx.wxFrame wx.NULL, any, "wxLua wxTreeCtrl Sample",
                        defpos, wx.wxSize(600, 400),
                        wx.wxDEFAULT_FRAME_STYLE

    -- create the menubar and attach it
    fileMenu = wx.wxMenu!
    fileMenu\Append wx.wxID_EXIT, "E&xit", "Quit the program"
    helpMenu = wx.wxMenu!
    helpMenu\Append wx.wxID_ABOUT, "&About", "About the wxLua wxTreeCtrl Sample"

    menuBar = wx.wxMenuBar!
    menuBar\Append fileMenu, "&File"
    menuBar\Append helpMenu, "&Help"
    frame\SetMenuBar menuBar

    frame\Connect wx.wxID_EXIT, wx.wxEVT_COMMAND_MENU_SELECTED,->
        frame\Close true

    frame\Connect wx.wxID_ABOUT, wx.wxEVT_COMMAND_MENU_SELECTED,->
        wx.wxMessageBox 'This is the "About" dialog of the wxLua wxTreeCtrl sample.\n'..wxlua.wxLUA_VERSION_STRING.." built with "..wx.wxVERSION_STRING,
                        "About wxLua",
                        wx.wxOK + wx.wxICON_INFORMATION,
                        frame

    splitter = wx.wxSplitterWindow frame,any,defpos,defsize

    -- create our treectrl
    tree = wx.wxTreeCtrl splitter, any, defpos, defsize,
                        wx.wxTR_LINES_AT_ROOT + wx.wxTR_HAS_BUTTONS


    -- create our log window

    textCtrl = wx.wxTextCtrl splitter, any, "", defpos, defsize,
                        wx.wxTE_READONLY + wx.wxTE_MULTILINE

    splitter\SetMinimumPaneSize 50
    splitter\SplitVertically tree, textCtrl, 200


    root_id = tree\AddRoot "Root"
    treedata.put root_id, "I'm the root item"

    for idx = 0, 10
        parent_id = tree\AppendItem root_id, "Parent (#{idx})"
        treedata.put parent_id, "I'm the data for Parent (#{idx})"
        for jdx = 0, 5
            child_id = tree\AppendItem parent_id, "Child (#{idx},#{jdx})"
            treedata.put child_id, "I'm the child data for Parent (#{idx},#{jdx}))"
        if idx == 2 or idx == 5
            tree\Expand parent_id

    tree_event = (evnt,name) ->
        tree\Connect evnt,(event) ->
            value = event\GetItem!\GetValue!
            textCtrl\AppendText "Item #{name}: #{tostring(value)} '#{treedata[value].data}'\n"

    -- connect to some events from the wxTreeCtrl
    tree_event wx.wxEVT_COMMAND_TREE_ITEM_EXPANDING,"expanding"

    tree_event wx.wxEVT_COMMAND_TREE_ITEM_COLLAPSING, "collapsing"

    tree_event wx.wxEVT_COMMAND_TREE_ITEM_ACTIVATED, "activated"

    tree_event wx.wxEVT_COMMAND_TREE_SEL_CHANGED, "sel changed"

    tree\Expand root_id

    frame\Show true

main!

wx.wxGetApp!\MainLoop!

Clone this wiki locally