1
Mach4 General Discussion / Re: wxLua and wxWidgets questions
« on: December 17, 2017, 04:58:07 AM »
I thought I would take another stab at it and figured out what I was doing wrong in just a couple minutes of testing. I thought I would post some test code in case others would like to see what I was trying to explain.
Below is a complete Lua example. You should be able to copy it into the mcLuaEditor and run it without doing anything but saving it. After running it, hold the ctrl-key down while drag selecting a couple times and maybe a couple single selections. Then just type in a numeric value and hit enter. Keep in mind, it is a simple demo and does not check to make sure your selections are all the same data type. The XY columns are floating point only and all the others are text. You can cause errors in some selection cases that include text columns , but only because I didn't try to handle them appropriately for this demo.
BTW, the error was simply not realizing that the returned arrays are indexed from 0 to getcount-1 instead of 1 to getcount. So it blew-up whenever the index was out of range.
Below is a complete Lua example. You should be able to copy it into the mcLuaEditor and run it without doing anything but saving it. After running it, hold the ctrl-key down while drag selecting a couple times and maybe a couple single selections. Then just type in a numeric value and hit enter. Keep in mind, it is a simple demo and does not check to make sure your selections are all the same data type. The XY columns are floating point only and all the others are text. You can cause errors in some selection cases that include text columns , but only because I didn't try to handle them appropriately for this demo.
Code: [Select]
-----------------------------------------------------------------------------
-- Name: gridblocktest.wx.lua
-- Purpose: Show how to use the Grid Block Selections to fill all with data at once
-- Author: David Kenny, from original code by J Winwood
-----------------------------------------------------------------------------
-- Load the wxLua module, does nothing if running from wxLua, wxLuaFreeze, or wxLuaEdit
package.cpath = package.cpath..";./?.dll;./?.so;../lib/?.so;../lib/vc_dll/?.dll;../lib/bcc_dll/?.dll;../lib/mingw_dll/?.dll;"
require("wx")
local frame = wx.wxFrame(wx.NULL, wx.wxID_ANY, "Grid Block-Selection example",
wx.wxPoint(25, 25), wx.wxSize(450, 450))
local fileMenu = wx.wxMenu("", wx.wxMENU_TEAROFF)
fileMenu:Append(wx.wxID_EXIT, "E&xit\tCtrl-X", "Quit the program")
frame:Connect(wx.wxID_EXIT, wx.wxEVT_COMMAND_MENU_SELECTED,
function (event)
frame:Close()
end )
grid = wx.wxGrid(frame, wx.wxID_ANY)
grid:Connect( wx.wxEVT_KEY_DOWN, function(event)
--implements PartsLocGridOnKeyDown
BTL = (grid:GetSelectionBlockTopLeft())
BBR = (grid:GetSelectionBlockBottomRight())
SCs = (grid:GetSelectedCells())
--Have to grab the selections before the CELL_CHANGE as that cancels the selection
event:Skip()
end )
grid:Connect( wx.wxEVT_GRID_CELL_CHANGE, function(event)
--implements PartsLocGridOnGridCmdCellChange
Row = event:GetRow()
Col = event:GetCol()
if Col > 0 then
Num = tonumber(grid:GetCellValue(Row, Col)) -- Floating point
if Num == nil then Num = 0 end
Num = string.format("%04.2f", Num)
grid:SetCellValue( Row, Col, Num)
end
--Made no attempt (in this demo) to make sure the selected blocks are in columns with the same data type
--Now Copy the contents to all blocks and single selections using the selection arrays grabbed durint the KEY_DOWN event.
print("BTL Count:".. BTL:GetCount())
for i=0,BTL:GetCount()-1 do
local wxGridCellCoords topleft = (BTL:Item(i))
local wxGridCellCoords bottomright = (BBR:Item(i))
tlr = topleft:GetRow()
tlc = topleft:GetCol()
brr = bottomright:GetRow()
brc = bottomright:GetCol()
FillBlock(tlr, tlc, brr, brc, Num)
end
for i=0, SCs:GetCount()-1 do
local wxGridCellCoords selectedcells = (SCs:Item(i))
scr = selectedcells:GetRow()
scc = selectedcells:GetCol()
grid:SetCellValue(scr, scc, Num)
end event:Skip()
end )
grid:CreateGrid(10, 8)
grid:SetColSize(3, 200)
grid:SetColLabelValue(0,"Description" )
grid:SetColLabelValue(1,"Center x" )
grid:SetColLabelValue(2,"Center y" )
grid:SetColFormatFloat(1,4,2)
grid:SetColFormatFloat(2,4,2)
for i=0, 10 do --Create some rows to work with
--local str = "Hole #"..i
grid:SetCellValue(i,0,string.format("Hole #%s",i))
end
frame:Show(true)
BTL = nil
BBR = nil
SCs = nil
function FillBlock(tlr, tlc, brr, brc, val)
for i=tlr, brr do
for j=tlc, brc do
grid:SetCellValue(i, j, val)
end
end
end
-- Call wx.wxGetApp():MainLoop() last to start the wxWidgets event loop,
-- otherwise the wxLua program will exit immediately.
-- Does nothing if running from wxLua, wxLuaFreeze, or wxLuaEdit since the
-- MainLoop is already running or will be started by the C++ program.
wx.wxGetApp():MainLoop()
BTW, the error was simply not realizing that the returned arrays are indexed from 0 to getcount-1 instead of 1 to getcount. So it blew-up whenever the index was out of range.