From: John L. <jr...@us...> - 2005-06-07 05:57:23
|
Update of /cvsroot/wxlua/wxLua/samples/calculator In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30590/wxLua/samples/calculator Added Files: calculator.wx.lua calculator.wxr calculator.xrc Log Message: added calculator wxIcon has CopyFromBitmap in GTK since 2.5 --- NEW FILE: calculator.xrc --- <?xml version="1.0" ?> <resource> <object class="wxDialog" name="dialog1"> <pos>10,10d</pos> <size>67,106d</size> <style>wxRAISED_BORDER|wxDEFAULT_DIALOG_STYLE|wxCAPTION|wxTHICK_FRAME|wxRESIZE_BORDER|wxSYSTEM_MENU</style> <font> <size>8</size> <face>swiss</face> </font> <title>Calc</title> <object class="wxButton" name="button0"> <pos>4,91d</pos> <size>12,12d</size> <font> <size>8</size> <face>swiss</face> </font> <label>0</label> </object> <object class="wxButton" name="button1"> <pos>4,75d</pos> <size>12,12d</size> <font> <size>8</size> <face>swiss</face> </font> <label>1</label> </object> <object class="wxButton" name="button2"> <pos>20,75d</pos> <size>12,12d</size> <font> <size>8</size> <face>swiss</face> </font> <label>2</label> </object> <object class="wxButton" name="button3"> <pos>36,75d</pos> <size>12,12d</size> <font> <size>8</size> <face>swiss</face> </font> <label>3</label> </object> <object class="wxButton" name="button4"> <pos>4,59d</pos> <size>12,12d</size> <font> <size>8</size> <face>swiss</face> </font> <label>4</label> </object> <object class="wxButton" name="button5"> <pos>20,59d</pos> <size>12,12d</size> <font> <size>8</size> <face>swiss</face> </font> <label>5</label> </object> <object class="wxButton" name="button6"> <pos>36,59d</pos> <size>12,12d</size> <font> <size>8</size> <face>swiss</face> </font> <label>6</label> </object> <object class="wxButton" name="button7"> <pos>4,43d</pos> <size>12,12d</size> <font> <size>8</size> <face>swiss</face> </font> <label>7</label> </object> <object class="wxButton" name="button8"> <pos>20,43d</pos> <size>12,12d</size> <font> <size>8</size> <face>swiss</face> </font> <label>8</label> </object> <object class="wxButton" name="button9"> <pos>36,43d</pos> <size>12,12d</size> <font> <size>8</size> <face>swiss</face> </font> <label>9</label> </object> <object class="wxButton" name="button_decimal"> <pos>20,91d</pos> <size>12,12d</size> <font> <size>8</size> <face>swiss</face> </font> <label>.</label> </object> <object class="wxButton" name="button_equals"> <pos>36,91d</pos> <size>12,12d</size> <font> <size>8</size> <face>swiss</face> </font> <label>=</label> </object> <object class="wxButton" name="button_divide"> <pos>52,43d</pos> <size>12,12d</size> <font> <size>8</size> <face>swiss</face> </font> <label>/</label> </object> <object class="wxButton" name="button_multiply"> <pos>52,59d</pos> <size>12,12d</size> <font> <size>8</size> <face>swiss</face> </font> <label>x</label> </object> <object class="wxButton" name="button_minus"> <pos>52,75d</pos> <size>12,12d</size> <font> <size>8</size> <face>swiss</face> </font> <label>-</label> </object> <object class="wxButton" name="button_plus"> <pos>52,91d</pos> <size>12,12d</size> <font> <size>8</size> <face>swiss</face> </font> <label>+</label> </object> <object class="wxButton" name="button_clear"> <pos>4,28d</pos> <size>29,12d</size> <font> <size>8</size> <face>swiss</face> </font> <label>C</label> </object> <object class="wxButton" name="button_off"> <pos>36,28d</pos> <size>28,12d</size> <font> <size>8</size> <face>swiss</face> </font> <label>OFF</label> </object> <object class="wxStaticText" name="statictext1"> <pos>4,9d</pos> <size>56,16d</size> <font> <size>10</size> <face>swiss</face> </font> <label>0</label> </object> <object class="wxStaticBox" name="staticbox1"> <pos>2,2d</pos> <size>64,18d</size> <font> <size>10</size> <face>swiss</face> </font> <label></label> </object> </object> </resource> --- NEW FILE: calculator.wx.lua --- -------------------------------------------------------------------------=--- -- Name: Calculator.wx.lua -- Purpose: Calculator wxLua sample -- Author: J Winwood -- Based on the wxWindows sample by Marco Ghislanzoni -- Created: March 2002 -- Updated January 2003 to use XML resources -- Copyright: (c) 2002-2003 Lomtick Software. All rights reserved. -- Licence: wxWindows licence -------------------------------------------------------------------------=--- ID_0 = 102 ID_1 = 103 ID_2 = 104 ID_3 = 105 ID_4 = 106 ID_5 = 107 ID_6 = 108 ID_7 = 109 ID_8 = 110 ID_9 = 111 ID_DECIMAL = 112 ID_EQUALS = 113 ID_PLUS = 114 ID_MINUS = 115 ID_MULTIPLY = 116 ID_DIVIDE = 117 ID_CLEAR = 118 ID_OFF = 119 local xpmdata = { "16 16 6 1", " s None c None", ". c #000000", "+ c #c0c0c0", "@ c #808080", "# c #ffff00", "$ c #ffffff", " ", " @@@@@ ", " @#+#+#@ ", " @#+#+#+#@@@@@@ ", " @$$$$$$$$$$$$@.", " @$#+#+#+#+#+#@.", " @$+#+#+@+#+#+@.", " @$#+#+@+@+#+#@.", " @$+#+@+#+@+#+@.", " @$#+@+#+#+@+#@.", " @$+#+#+#+#+#+@.", " @$#+#+#+#+#+#@.", " @@@@@@@@@@@@@@.", " ..............", " ", " " } bitmap = wx.wxBitmapFromXPMData(xpmdata) icon = wx.wxDefaultIcon() icon:CopyFromBitmap(bitmap) -- return the path part of the currently executing file local strAt = string.byte('@') function getPath() function findLast(filePath) local lastOffset = nil local offset repeat offset = string.find(filePath, "\\") if offset == nil then offset = string.find(filePath, "/") end if offset then if not lastOffset then lastOffset = offset else lastOffset = lastOffset + offset end filePath = string.sub(filePath, offset + 1) end until not offset return lastOffset end local filePath = debug.getinfo(1, "S").source if string.byte(filePath) == strAt then local offset = findLast(filePath) if offset ~= nil then -- remove the @ at the front up to just before the path separator filePath = string.sub(filePath, 2, offset - 1) else filePath = "." end else filePath = wx.wxGetCwd().."/Examples" end return filePath end dialog = nil res = nil txtDisplay = nil -- old style resources (if present) if wx.wxResourceParseFile then wx.wxResourceParseFile(getPath().."/calculator.wxr") -- Create main dialog using default constructor dialog = wx.wxEmptyDialog() if dialog:LoadFromResource(wx.wxNull, "dialog1") == wx.FALSE then wx.wxMessageBox("Error loading wxr resources!", "Calculator", wx.wxOK + wx.wxICON_EXCLAMATION, dialog) wx.wxExit(0) end txtDisplay = wx.wxFindWindowByName("statictext1", dialog); if not txtDisplay then wx.wxError([[Unable to find window "statictext1" in the dialog]]) end else -- xml style resources (if present) res = wx.wxXmlResourceGetDefault() res:InitAllHandlers() res:Load(getPath().."/calculator.xrc") dialog = wx.wxEmptyDialog() if res:LoadDialogCreate(dialog, wx.wxNull, "dialog1") == wx.FALSE then wx.wxMessageBox("Error loading xrc resources!", "Calculator", wx.wxOK + wx.wxICON_EXCLAMATION, wx.wxNull) wx.wxExit(0) end txtDisplay = dialog:FindWindow(res:GetXRCID("statictext1")) if not txtDisplay then wx.wxError([[Unable to find window "statictext1" in the dialog]]) end ID_0 = res:GetXRCID("button0") ID_1 = res:GetXRCID("button1") ID_2 = res:GetXRCID("button2") ID_3 = res:GetXRCID("button3") ID_4 = res:GetXRCID("button4") ID_5 = res:GetXRCID("button5") ID_6 = res:GetXRCID("button6") ID_7 = res:GetXRCID("button7") ID_8 = res:GetXRCID("button8") ID_9 = res:GetXRCID("button9") ID_DECIMAL = res:GetXRCID("button_decimal") ID_EQUALS = res:GetXRCID("button_equals") ID_PLUS = res:GetXRCID("button_plus") ID_MINUS = res:GetXRCID("button_minus") ID_MULTIPLY = res:GetXRCID("button_multiply") ID_DIVIDE = res:GetXRCID("button_divide") ID_OFF = res:GetXRCID("button_off") ID_CLEAR = res:GetXRCID("button_clear") end dialog:SetIcon(icon) if not txtDisplay:DynamicCast("wxStaticText") then wx.wxError([[window "statictext1" is not a "wxStaticText" or is not derived from it"]]) end txtDisplay:SetLabel("0") lastNumber = 0 lastOperation = ID_PLUS clearDisplay = nil function OnClear(event) txtDisplay:SetLabel("0") lastNumber = 0 lastOperation = ID_PLUS end function OnNumber(event) local number = event:GetId() local displayString = txtDisplay:GetLabel() if displayString == "0" or clearDisplay then displayString = "" end clearDisplay = nil -- Limit string length to 12 chars if string.len(displayString) < 12 then if number == ID_DECIMAL then -- If the first pressed char is "." then we want "0." if string.len(displayString) == 0 then displayString = displayString.."0." else displayString = displayString.."." end else number = number - ID_0 -- If first character entered is 0 we reject it if number == 0 and string.len(displayString) == 0 then displayString = "0" elseif displayString == "" then displayString = number else displayString = displayString..number end end txtDisplay:SetLabel(displayString) end end function DoOperation(a, b, operation) local result = a if operation == ID_PLUS then result = b + a elseif operation == ID_MINUS then result = b - a elseif operation == ID_MULTIPLY then result = b * a elseif operation == ID_DIVIDE then if a == 0 then result = "Error" else result = b / a end end return result end function OnOperator(event) -- Get display content local displayString = txtDisplay:GetLabel() local currentOperation = tonumber(displayString) -- Get the required lastOperation local operation = event:GetId() displayString = DoOperation(currentOperation, lastNumber, lastOperation) lastNumber = tonumber(displayString) if lastOperation ~= ID_EQUALS or operation == ID_EQUALS then txtDisplay:SetLabel(displayString) end clearDisplay = 1 lastOperation = operation end function OnQuit(event) -- Hide main dialog dialog:Show(wx.FALSE) wx.wxMessageBox("Calculator sample based on the calc sample written by Marco Ghislanzoni", "Calculator", wx.wxOK + wx.wxICON_INFORMATION) dialog:Destroy() end dialog:ConnectEvent(ID_0, wx.wxEVT_COMMAND_BUTTON_CLICKED, OnNumber) dialog:ConnectEvent(ID_1, wx.wxEVT_COMMAND_BUTTON_CLICKED, OnNumber) dialog:ConnectEvent(ID_2, wx.wxEVT_COMMAND_BUTTON_CLICKED, OnNumber) dialog:ConnectEvent(ID_3, wx.wxEVT_COMMAND_BUTTON_CLICKED, OnNumber) dialog:ConnectEvent(ID_4, wx.wxEVT_COMMAND_BUTTON_CLICKED, OnNumber) dialog:ConnectEvent(ID_5, wx.wxEVT_COMMAND_BUTTON_CLICKED, OnNumber) dialog:ConnectEvent(ID_6, wx.wxEVT_COMMAND_BUTTON_CLICKED, OnNumber) dialog:ConnectEvent(ID_7, wx.wxEVT_COMMAND_BUTTON_CLICKED, OnNumber) dialog:ConnectEvent(ID_8, wx.wxEVT_COMMAND_BUTTON_CLICKED, OnNumber) dialog:ConnectEvent(ID_9, wx.wxEVT_COMMAND_BUTTON_CLICKED, OnNumber) dialog:ConnectEvent(ID_DECIMAL, wx.wxEVT_COMMAND_BUTTON_CLICKED, OnNumber) dialog:ConnectEvent(ID_EQUALS, wx.wxEVT_COMMAND_BUTTON_CLICKED, OnOperator) dialog:ConnectEvent(ID_PLUS, wx.wxEVT_COMMAND_BUTTON_CLICKED, OnOperator) dialog:ConnectEvent(ID_MINUS, wx.wxEVT_COMMAND_BUTTON_CLICKED, OnOperator) dialog:ConnectEvent(ID_MULTIPLY, wx.wxEVT_COMMAND_BUTTON_CLICKED, OnOperator) dialog:ConnectEvent(ID_DIVIDE, wx.wxEVT_COMMAND_BUTTON_CLICKED, OnOperator) dialog:ConnectEvent(ID_OFF, wx.wxEVT_COMMAND_BUTTON_CLICKED, OnQuit) dialog:ConnectEvent(ID_CLEAR, wx.wxEVT_COMMAND_BUTTON_CLICKED, OnClear) dialog:ConnectEvent(wx.wxEVT_CLOSE_WINDOW, function(event) OnQuit(event) event:Skip() end) dialog:Centre() dialog:Show(wx.TRUE) accelTable = wx.wxAcceleratorTable{ { wx.wxACCEL_NORMAL, string.byte('0'), ID_0 }, { wx.wxACCEL_NORMAL, wx.VXK_NUMPAD0, ID_0 }, { wx.wxACCEL_NORMAL, string.byte('1'), ID_1 }, { wx.wxACCEL_NORMAL, wx.VXK_NUMPAD1, ID_1 }, { wx.wxACCEL_NORMAL, string.byte('2'), ID_2 }, { wx.wxACCEL_NORMAL, wx.VXK_NUMPAD2, ID_2 }, { wx.wxACCEL_NORMAL, string.byte('3'), ID_3 }, { wx.wxACCEL_NORMAL, wx.VXK_NUMPAD3, ID_3 }, { wx.wxACCEL_NORMAL, string.byte('4'), ID_4 }, { wx.wxACCEL_NORMAL, wx.VXK_NUMPAD4, ID_4 }, { wx.wxACCEL_NORMAL, string.byte('5'), ID_5 }, { wx.wxACCEL_NORMAL, wx.VXK_NUMPAD5, ID_5 }, { wx.wxACCEL_NORMAL, string.byte('6'), ID_6 }, { wx.wxACCEL_NORMAL, wx.VXK_NUMPAD6, ID_6 }, { wx.wxACCEL_NORMAL, string.byte('7'), ID_7 }, { wx.wxACCEL_NORMAL, wx.VXK_NUMPAD7, ID_7 }, { wx.wxACCEL_NORMAL, string.byte('8'), ID_8 }, { wx.wxACCEL_NORMAL, wx.VXK_NUMPAD8, ID_8 }, { wx.wxACCEL_NORMAL, string.byte('9'), ID_9 }, { wx.wxACCEL_NORMAL, wx.VXK_NUMPAD9, ID_9 }, { wx.wxACCEL_NORMAL, string.byte('.'), ID_DECIMAL }, { wx.wxACCEL_NORMAL, wx.VXK_NUMPAD_DECIMAL, ID_DECIMAL }, { wx.wxACCEL_NORMAL, string.byte('='), ID_EQUALS }, { wx.wxACCEL_NORMAL, wx.VXK_NUMPAD_ENTER, ID_EQUALS }, { wx.wxACCEL_NORMAL, 13, ID_EQUALS }, { wx.wxACCEL_NORMAL, string.byte('+'), ID_PLUS }, { wx.wxACCEL_NORMAL, wx.VXK_NUMPAD_ADD, ID_PLUS }, { wx.wxACCEL_NORMAL, string.byte('-'), ID_MINUS }, { wx.wxACCEL_NORMAL, wx.VXK_NUMPAD_SUBTRACT, ID_MINUS }, { wx.wxACCEL_NORMAL, string.byte('*'), ID_MULTIPLY }, { wx.wxACCEL_NORMAL, wx.VXK_NUMPAD_MULTIPLY, ID_MULTIPLY }, { wx.wxACCEL_NORMAL, string.byte('/'), ID_DIVIDE }, { wx.wxACCEL_NORMAL, wx.VXK_NUMPAD_DIVIDE, ID_DIVIDE }, { wx.wxACCEL_NORMAL, string.byte('C'), ID_CLEAR }, { wx.wxACCEL_NORMAL, string.byte('c'), ID_CLEAR }, { wx.wxACCEL_NORMAL, wx.WXK_ESCAPE, ID_OFF } } dialog:SetAcceleratorTable(accelTable) --- NEW FILE: calculator.wxr --- static char *dialog1 = "dialog(name = 'dialog1',\ style = 'wxRAISED_BORDER | wxCAPTION | wxTHICK_FRAME | wxSYSTEM_MENU',\ title = 'Calc',\ id = 100,\ x = 10, y = 10, width = 67, height = 106,\ background_colour = 'C0C0C0',\ use_dialog_units = 1,\ use_system_defaults = 0,\ font = [8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif'],\ control = [102, wxButton, '0', '0', 'button1', 4, 91, 12, 12, '',\ [8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\ control = [103, wxButton, '1', '0', 'button2', 4, 75, 12, 12, '',\ [8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\ control = [104, wxButton, '2', '0', 'button3', 20, 75, 12, 12, '',\ [8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\ control = [105, wxButton, '3', '0', 'button4', 36, 75, 12, 12, '',\ [8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\ control = [106, wxButton, '4', '0', 'button5', 4, 59, 12, 12, '',\ [8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\ control = [107, wxButton, '5', '0', 'button6', 20, 59, 12, 12, '',\ [8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\ control = [108, wxButton, '6', '0', 'button7', 36, 59, 12, 12, '',\ [8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\ control = [109, wxButton, '7', '0', 'button8', 4, 43, 12, 12, '',\ [8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\ control = [110, wxButton, '8', '0', 'button9', 20, 43, 12, 12, '',\ [8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\ control = [111, wxButton, '9', '0', 'button10', 36, 43, 12, 12, '',\ [8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\ control = [112, wxButton, '.', '0', 'button11', 20, 91, 12, 12, '',\ [8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\ control = [113, wxButton, '=', '0', 'button12', 36, 91, 12, 12, '',\ [8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\ control = [117, wxButton, '/', '0', 'button13', 52, 43, 12, 12, '',\ [8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\ control = [116, wxButton, 'x', '0', 'button14', 52, 59, 12, 12, '',\ [8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\ control = [115, wxButton, '-', '0', 'button15', 52, 75, 12, 12, '',\ [8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\ control = [114, wxButton, '+', '0', 'button16', 52, 91, 12, 12, '',\ [8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\ control = [118, wxButton, 'C', '0', 'button17', 4, 28, 29, 12, '',\ [8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\ control = [119, wxButton, 'OFF', '0', 'button18', 36, 28, 28, 12, '',\ [8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\ control = [101, wxStaticText, '0', 'wxALIGN_RIGHT', 'statictext1', 4, 9, 56, 16, '',\ [10, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 'MS Sans Serif']],\ control = [120, wxStaticBox, '', '0', 'staticbox1', 2, 2, 64, 18,\ [10, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']])."; |