Fruit basket program in Lua

  • From: Jamal Mazrui <empower@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Tue, 6 Nov 2007 12:11:23 -0500 (EST)

From the zip archive at
http://www.EmpowermentZone.com/lua_fruit.zip

This fruit basket program is written in Lua:  an open source, dynamic
language developed at a university in Portugal (Lua means moon in
Portuguese).  Designed to be lightweight and portable, Lua is written in
ANSI C and may be used either stand-alone or as an embedded scripting
language for applications.  It is often deployed as a scripting engine for
games or portable devices.  Mobile Speak, the screen reader for smart
phones, uses Lua for scripting.

The Lua home page is at
http://lua.org

The fruit basket program uses wrappers for the WxWidgets GUI library,
available at
http://WxLua.SourceForge.net

This package includes a utility for creating a stand-alone executable that
runs an embedded Lua script.  A batch file included with this archive,
build.bat, shows how lua_fruit.exe is built.  To run the executable,
remember to include its .exe extension on the command line -- I found this
not to be optional, as is usually the case.

If one has a C compiler, WxLua may be selectively included in an
executable.  Otherwise, the resulting size is large:  about 6 megabytes.
The executable may be reduced to a quarter of that size, however, using an
open source utility called Ultimate Packer for Executables, available at
http://upx.sourceforge.net

The batch file invokes this utility so that lua_fruit.exe is about 1.5
megs.  The archive includes all components needed to rebuild the
executable.  Its source code is below.  I have also prepared a collection
of Lua documentation in structured text format at
http://www.EmpowermentZone.com/lua_doc.zip

Jamal

-- Content of the file lua_fruit.lua
-- Fruit basket program in Lua
-- Public domain by Jamal Mazrui

dlg = wx.wxDialog(wx.NULL, wx.wxID_ANY, "Fruit Basket",
wx.wxDefaultPosition, wx.wxSize(513, 176))

lblFruit = wx.wxStaticText(dlg, wx.wxID_ANY, "&Fruit:", wx.wxPoint(14,
14), wx.wxDefaultSize)
txtFruit = wx.wxTextCtrl(dlg, wx.wxID_ANY, "", wx.wxPoint(43, 14),
wx.wxDefaultSize, wx.wxTE_LEFT)

lblBasket = wx.wxStaticText(dlg, wx.wxID_ANY, "&Basket:", wx.wxPoint(251,
14), wx.wxDefaultSize)
lstBasket = wx.wxListBox(dlg, wx.wxID_ANY, wx.wxPoint(293,14),
wx.wxDefaultSize, {})

btnAdd = wx.wxButton(dlg, wx.wxID_ANY, "&Add", wx.wxPoint(190, 121),
wx.wxDefaultSize)
btnAdd:SetDefault()
dlg:Connect(btnAdd:GetId(), wx.wxEVT_COMMAND_BUTTON_CLICKED,
function(event)
sValue = txtFruit:GetValue()
if sValue == "" then
wx.wxMessageBox("No fruit to add!", "Alert", wx.wxICON_EXCLAMATION, dlg)
return
end

lstBasket:Append(sValue)
txtFruit:Clear()
iCount = lstBasket:GetCount()
iIndex = iCount - 1
lstBasket:SetSelection(iIndex)
end)

btnDelete = wx.wxButton(dlg, wx.wxID_ANY, "&Delete", wx.wxPoint(217, 121),
wx.wxDefaultSize)
dlg:Connect(btnDelete:GetId(), wx.wxEVT_COMMAND_BUTTON_CLICKED,
function(event)
iIndex = lstBasket:GetSelection()
if iIndex == -1 then
wx.wxMessageBox("No fruit to delete!", "Alert", wx.wxICON_EXCLAMATION,
dlg)
return
end

lstBasket:Delete(iIndex)
iCount = lstBasket:GetCount()
if iIndex == iCount then
iIndex = iCount - 1
end

lstBasket:SetSelection(iIndex)
end)

dlg:Connect(wx.wxEVT_CLOSE_WINDOW,
function (event)
if wx.wxMessageBox("Exit program?", "Confirm", wx.wxYES_NO, dlg) ==
wx.wxYES then
event:Skip()
dlg:Destroy()
end
end)

dlg:Centre()
dlg:Show(true)

-- End of lua_fruit.lua

__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

Other related posts: