John Labenski | 3 Feb 19:31
Picon

Re: about mainloop

On Mon, Jan 2, 2012 at 5:51 AM, fra III <ilterzouomo@...> wrote:
> Hello All,
> when i run a lua script using wxLua, either mine that some of the
> examples in distribution, closing application do not exit automatically
> from the Lua interpreter, i.e. after closing main window i have to use
> ctrl-C to stop all. This happens either in terminal that using Scite
> editor.
> Is this normal?

No and I don't see that behaviour here in Fedora or MSW. If you simply run it as

$ wxLua minimal.wx.lua

then it should immediately exit when the minimal's wxFrame is closed.
Does it still happen when you run that sample?

> Also when i require wx the Lua function print is no working anymore
> (seems there is another recent post about this).

This was an unfortunate choice by me and the next version will not do
that when run using require.
A simple fix is to add this code that replaces the wxLua print
function back with the Lua print function.

print_wx = print -- save old print in case you want it...
print = print_lua

> I'm on Ubuntu 10.04 with wxwidget 2.8.12 from repository and
> wxLua from recent (?) git.

(Continue reading)

Milind Gupta | 15 Feb 04:09
Picon

Event handling function inside a module

Hi,

       I wonder if this is a known issue or something intentional. I have made a module that creates a frame with some buttons and associates an event handling function for the button click, the function resides in the module itself. The frame also is visible in the module. Now when the event handler is called the event handler function is executed in the global environment rather than the module environment hence it is not able to see the variables and functions of the module.
       Does this mean event handler functions cannot be in a module?

Thanks,
Milind
------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing 
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
wxlua-users mailing list
wxlua-users@...
https://lists.sourceforge.net/lists/listinfo/wxlua-users
John Labenski | 15 Feb 05:37
Picon

Re: Event handling function inside a module

On Tue, Feb 14, 2012 at 10:09 PM, Milind Gupta
<milind.gupta@...> wrote:
> Hi,
>        I wonder if this is a known issue or something intentional. I have
> made a module that creates a frame with some buttons and associates an event
> handling function for the button click, the function resides in the module
> itself. The frame also is visible in the module. Now when the event handler
> is called the event handler function is executed in the global environment
> rather than the module environment hence it is not able to see the variables
> and functions of the module.
>        Does this mean event handler functions cannot be in a module?

It should be able to work and the environment is saved when the
event:Connect() is handled. Upvalues work for sure.

Do you have some simple example code of this for me to try.

Regards,
    John

------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing 
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
Milind Gupta | 15 Feb 09:14
Picon

Re: Event handling function inside a module

Hello John, thanks for the reply. Here is an example code that does not work (Pasted below and attached the file as well). A function from the main wxlua application calls filterFormActivate which sets up a simple frame with 2 buttons and one static text and associates the button click to SelTaskPress. But when the button is clicked the test() statement gives an error. If that is removed then it cannot recognize the frame varaiable in the wx.wxFrame(frame line after test(). Pleas have a look at it and let me know what is wrong.
Thanks, Milind
local print = print local wx = wx local bit = bit local GUI = GUI local tostring = tostring module(...) filter = {} function test() print("In test "..tostring(frame)) end local function SelTaskPress(event) test() local frame = wx.wxFrame(frame, wx.wxID_ANY, "Select Task", wx.wxDefaultPosition, wx.wxSize(GUI.initFrameW, GUI.initFrameH), wx.wxDEFAULT_FRAME_STYLE) frame:SetSizer(MainSizer) MainSizer:SetSizeHints(frame) frame:Layout() frame:Show(true) end function filterFormActivate(parent) frame = wx.wxFrame(parent, wx.wxID_ANY, "Filter Form", wx.wxDefaultPosition, wx.wxSize(GUI.initFrameW, GUI.initFrameH), wx.wxDEFAULT_FRAME_STYLE) local MainSizer = wx.wxBoxSizer(wx.wxVERTICAL) MainBook = wx.wxNotebook(frame, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize, wx.wxNB_TOP) TandC = wx.wxPanel(MainBook, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize, wx.wxTAB_TRAVERSAL) local TandCSizer = wx.wxBoxSizer(wx.wxVERTICAL) local TaskSizer = wx.wxBoxSizer(wx.wxHORIZONTAL) SelTaskButton = wx.wxButton(TandC, wx.wxID_ANY, "Select Task", wx.wxDefaultPosition, wx.wxDefaultSize, 0, wx.wxDefaultValidator) TaskSizer:Add(SelTaskButton, 0, bit.bor(wx.wxALL,wx.wxALIGN_CENTER_HORIZONTAL,wx.wxALIGN_CENTER_VERTICAL), 1) FilterTask = wx.wxStaticText(TandC, wx.wxID_ANY, "No Task Selected", wx.wxDefaultPosition, wx.wxDefaultSize, wx.wxALIGN_CENTRE) TaskSizer:Add(FilterTask, 1, bit.bor(wx.wxALL,wx.wxALIGN_CENTER_HORIZONTAL,wx.wxALIGN_CENTER_VERTICAL), 1) ClearTaskButton = wx.wxButton(TandC, wx.wxID_ANY, "Clear Task", wx.wxDefaultPosition, wx.wxDefaultSize, 0, wx.wxDefaultValidator) TaskSizer:Add(ClearTaskButton, 0, bit.bor(wx.wxALL,wx.wxALIGN_CENTER_HORIZONTAL,wx.wxALIGN_CENTER_VERTICAL), 1) TandCSizer:Add(TaskSizer, 0, bit.bor(wx.wxALL,wx.wxEXPAND,wx.wxALIGN_CENTER_HORIZONTAL,wx.wxALIGN_CENTER_VERTICAL), 1) TandC:SetSizer(TandCSizer) TandCSizer:SetSizeHints(TandC) MainBook:AddPage(TandC, "T and C") MainSizer:Add(MainBook, 1, bit.bor(wx.wxALL,wx.wxEXPAND,wx.wxALIGN_CENTER_HORIZONTAL,wx.wxALIGN_CENTER_VERTICAL), 1) frame:SetSizer(MainSizer) MainSizer:SetSizeHints(frame) SelTaskButton:Connect(wx.wxEVT_COMMAND_BUTTON_CLICKED, SelTaskPress) ClearTaskButton:Connect(wx.wxEVT_COMMAND_BUTTON_CLICKED, function (event) filter.Tasks = nil FilterTask:SetLabel("No Task Selected") end ) frame:Layout() -- help sizing the windows before being shown frame:Show(true) end

On Tue, Feb 14, 2012 at 10:09 PM, Milind Gupta <milind.gupta <at> ...> wrote: > Hi, >        I wonder if this is a known issue or something intentional. I have > made a module that creates a frame with some buttons and associates an event > handling function for the button click, the function resides in the module > itself. The frame also is visible in the module. Now when the event handler > is called the event handler function is executed in the global environment > rather than the module environment hence it is not able to see the variables > and functions of the module. >        Does this mean event handler functions cannot be in a module? It should be able to work and the environment is saved when the event:Connect() is handled. Upvalues work for sure. Do you have some simple example code of this for me to try. Regards, John
Attachment (filterform.lua): application/octet-stream, 2547 bytes
------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing 
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
wxlua-users mailing list
wxlua-users@...
https://lists.sourceforge.net/lists/listinfo/wxlua-users
John Labenski | 16 Feb 03:38
Picon

Re: Event handling function inside a module

On Wed, Feb 15, 2012 at 3:14 AM, Milind Gupta <milind.gupta@...> wrote:
> Hello John,
>
>           thanks for the reply. Here is an example code that does not work

Please try to provide enough code to make it run out of the box... I
had to add back GUI.initFrameH and rem out the usage of MainSizer to
make it even possible to run without error.

> (Pasted below and attached the file as well). A function from the main wxlua
> application calls filterFormActivate which sets up a simple frame with 2
> buttons and one static text and associates the button click to SelTaskPress.

I made the help menu callback function of the minimal.wx.lua sample
call filterform.filterFormActivate(frame).

> But when the button is clicked the test() statement gives an error. If that
> is removed then it cannot recognize the frame varaiable in the
> wx.wxFrame(frame line after test().

Please always include these errors in the future to help diagnose the problem.

In order to solve your problem I made test() a local function so that
it will be an upvalue of the Connect() function that calls it. Note
that the 'frame' that is used in the SelTaskPress() function is the
frame from the minimal.wx.lua and not the frame from
filterFormActivate().

To be honest, I have not done too much with modules and require in Lua
since I'm not sure I fully understand its behavior.

I just came across this which clears some things up for me. Note the
sentence "This means that any global functions are not visible by
default, which is why we had to create a local alias to print." This
is probably also why making test() local above fixed it. See also
package.seeall which might be useful to you.
http://www.luafaq.org/#T1.37

Hope this helps,
    John

------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing 
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
Milind Gupta | 16 Feb 09:14
Picon

Re: Event handling function inside a module

Hello John,
           Sorry for the carelessness. Here is a full set with minimal.lua which is the main file to run and the module function is called by the Help->About call back. That is true making test a local function makes it work although I am not totally clear why but if you press the other button 'Clear Task' it will give an error complaining about filter which again is not declared local. Actually declaring local  anything in a module only makes it private. Whether it is declared local or not it always resides in the module environment. So even if test or filter are not declared local they should be identified in the module environment. Here is the Programming in Lua Modules and Packages chapter that clarifies it:


At least that is how I understood it. So I think it should work without being declared local. In your previous email you said that the environment is saved as an upvalue with the execution of Connect. I am not sure how that would be done since the module itself does not know what environment  to Global it will be in. Like if you see in minimal I placed it in the environment newPackage and all my functions tables declared in the module i.e. test, filter, etc. are in the table newPackage in the global environment but the module code has no way of knowing that.
    One last point is that unless its an event callback function normal functions in the module run just fine whether test is local or not. As an example I just added the test() call as the first line in filterFormActivate which is a non event function so when the Help-> About callback calls filterFormActivate, test() just runs fine even though it is not local but now if you press the Select Task button it will say it could not find test().
 

Milind


On Wed, Feb 15, 2012 at 3:14 AM, Milind Gupta <milind.gupta <at> ...> wrote:
> Hello John,
>
>           thanks for the reply. Here is an example code that does not work

Please try to provide enough code to make it run out of the box... I
had to add back GUI.initFrameH and rem out the usage of MainSizer to
make it even possible to run without error.

> (Pasted below and attached the file as well). A function from the main wxlua
> application calls filterFormActivate which sets up a simple frame with 2
> buttons and one static text and associates the button click to SelTaskPress.

I made the help menu callback function of the minimal.wx.lua sample
call filterform.filterFormActivate(frame).

> But when the button is clicked the test() statement gives an error. If that
> is removed then it cannot recognize the frame varaiable in the
> wx.wxFrame(frame line after test().

Please always include these errors in the future to help diagnose the problem.

In order to solve your problem I made test() a local function so that
it will be an upvalue of the Connect() function that calls it. Note
that the 'frame' that is used in the SelTaskPress() function is the
frame from the minimal.wx.lua and not the frame from
filterFormActivate().

To be honest, I have not done too much with modules and require in Lua
since I'm not sure I fully understand its behavior.

I just came across this which clears some things up for me. Note the
sentence "This means that any global functions are not visible by
default, which is why we had to create a local alias to print." This
is probably also why making test() local above fixed it. See also
package.seeall which might be useful to you.

Hope this helps,
    John

Attachment (filterform.lua): application/octet-stream, 2378 bytes
Attachment (minimal.lua): application/octet-stream, 3673 bytes
------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing 
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
wxlua-users mailing list
wxlua-users@...
https://lists.sourceforge.net/lists/listinfo/wxlua-users
Mike Richards | 16 Feb 21:06
Picon
Favicon

Re: Event handling function inside a module

John and Milind,

function test() is declared in the current scope, and globally as filterform.test

After fiddling around for a bit, it looks like wx.wxEvtHandler:Connect saves upvalues, but not the current function environment (scope), which was set in the call to module(...)
When the event function is called, the function environment is reset to the global scope, so if you want to reference things you declared in the module scope in the event callback, you have a couple of options:
1. Declare everything as local, which to me is just good practice.
2. Use a fully qualified name (e.g. filterform.test())
3. Wrap all your calls to wx.wxEvtHandler:Connect like this:

function Connect(evthandler, id, func)
    local environment = getfenv(1)
    evthandler:Connect(id,
        function(event)
            setfenv(1, environment)
            func(event)
        end
    )
end

[...]

local button = wx.Button([...])
Connect(button, wx.wxEVT_COMMAND_BUTTON_CLICKED, function(event) end)

John -- It seems like wx.wxEvtHandler:Connect should do this anyways . . . that is, save the current function environment, and then apply it before calling the callback.

Mike

On 2/16/2012 3:14 AM, Milind Gupta wrote:
Hello John,
           Sorry for the carelessness. Here is a full set with minimal.lua which is the main file to run and the module function is called by the Help->About call back. That is true making test a local function makes it work although I am not totally clear why but if you press the other button 'Clear Task' it will give an error complaining about filter which again is not declared local. Actually declaring local  anything in a module only makes it private. Whether it is declared local or not it always resides in the module environment. So even if test or filter are not declared local they should be identified in the module environment. Here is the Programming in Lua Modules and Packages chapter that clarifies it:


At least that is how I understood it. So I think it should work without being declared local. In your previous email you said that the environment is saved as an upvalue with the execution of Connect. I am not sure how that would be done since the module itself does not know what environment  to Global it will be in. Like if you see in minimal I placed it in the environment newPackage and all my functions tables declared in the module i.e. test, filter, etc. are in the table newPackage in the global environment but the module code has no way of knowing that.
    One last point is that unless its an event callback function normal functions in the module run just fine whether test is local or not. As an example I just added the test() call as the first line in filterFormActivate which is a non event function so when the Help-> About callback calls filterFormActivate, test() just runs fine even though it is not local but now if you press the Select Task button it will say it could not find test().
 

Milind


On Wed, Feb 15, 2012 at 3:14 AM, Milind Gupta <milind.gupta <at> ...> wrote:
> Hello John,
>
>           thanks for the reply. Here is an example code that does not work

Please try to provide enough code to make it run out of the box... I
had to add back GUI.initFrameH and rem out the usage of MainSizer to
make it even possible to run without error.

> (Pasted below and attached the file as well). A function from the main wxlua
> application calls filterFormActivate which sets up a simple frame with 2
> buttons and one static text and associates the button click to SelTaskPress.

I made the help menu callback function of the minimal.wx.lua sample
call filterform.filterFormActivate(frame).

> But when the button is clicked the test() statement gives an error. If that
> is removed then it cannot recognize the frame varaiable in the
> wx.wxFrame(frame line after test().

Please always include these errors in the future to help diagnose the problem.

In order to solve your problem I made test() a local function so that
it will be an upvalue of the Connect() function that calls it. Note
that the 'frame' that is used in the SelTaskPress() function is the
frame from the minimal.wx.lua and not the frame from
filterFormActivate().

To be honest, I have not done too much with modules and require in Lua
since I'm not sure I fully understand its behavior.

I just came across this which clears some things up for me. Note the
sentence "This means that any global functions are not visible by
default, which is why we had to create a local alias to print." This
is probably also why making test() local above fixed it. See also
package.seeall which might be useful to you.

Hope this helps,
    John



------------------------------------------------------------------------------ Virtualization & Cloud Management Using Capacity Planning Cloud computing makes use of virtualization - but cloud computing also focuses on allowing computing to be delivered as a service. http://www.accelacomm.com/jaw/sfnl/114/51521223/

_______________________________________________ wxlua-users mailing list wxlua-users-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org https://lists.sourceforge.net/lists/listinfo/wxlua-users
------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing 
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
wxlua-users mailing list
wxlua-users@...
https://lists.sourceforge.net/lists/listinfo/wxlua-users
Milind Gupta | 16 Feb 22:42
Picon

Re: Event handling function inside a module

Hi Mike,

       I agree a good practice is to declare things local, only for things we want to access from outside unrestricted its easier not to declare them local. So anyway what I ended up doing is having the first statement in my event handler function as setfenv(1,package.loaded[modname]) where modname is the name of the module passed by lua to the module. Since require loads the module function there so it just picks it up from there. 

Thanks,
Milind

On Thu, Feb 16, 2012 at 12:06 PM, Mike Richards <mrichards42-KK0ffGbhmjU@public.gmane.org> wrote:
John and Milind,

function test() is declared in the current scope, and globally as filterform.test

After fiddling around for a bit, it looks like wx.wxEvtHandler:Connect saves upvalues, but not the current function environment (scope), which was set in the call to module(...)
When the event function is called, the function environment is reset to the global scope, so if you want to reference things you declared in the module scope in the event callback, you have a couple of options:
1. Declare everything as local, which to me is just good practice.
2. Use a fully qualified name (e.g. filterform.test())
3. Wrap all your calls to wx.wxEvtHandler:Connect like this:

function Connect(evthandler, id, func)
    local environment = getfenv(1)
    evthandler:Connect(id,
        function(event)
            setfenv(1, environment)
            func(event)
        end
    )
end

[...]

local button = wx.Button([...])
Connect(button, wx.wxEVT_COMMAND_BUTTON_CLICKED, function(event) end)

John -- It seems like wx.wxEvtHandler:Connect should do this anyways . . . that is, save the current function environment, and then apply it before calling the callback.

Mike


On 2/16/2012 3:14 AM, Milind Gupta wrote:
Hello John,
           Sorry for the carelessness. Here is a full set with minimal.lua which is the main file to run and the module function is called by the Help->About call back. That is true making test a local function makes it work although I am not totally clear why but if you press the other button 'Clear Task' it will give an error complaining about filter which again is not declared local. Actually declaring local  anything in a module only makes it private. Whether it is declared local or not it always resides in the module environment. So even if test or filter are not declared local they should be identified in the module environment. Here is the Programming in Lua Modules and Packages chapter that clarifies it:


At least that is how I understood it. So I think it should work without being declared local. In your previous email you said that the environment is saved as an upvalue with the execution of Connect. I am not sure how that would be done since the module itself does not know what environment  to Global it will be in. Like if you see in minimal I placed it in the environment newPackage and all my functions tables declared in the module i.e. test, filter, etc. are in the table newPackage in the global environment but the module code has no way of knowing that.
    One last point is that unless its an event callback function normal functions in the module run just fine whether test is local or not. As an example I just added the test() call as the first line in filterFormActivate which is a non event function so when the Help-> About callback calls filterFormActivate, test() just runs fine even though it is not local but now if you press the Select Task button it will say it could not find test().
 

Milind


On Wed, Feb 15, 2012 at 3:14 AM, Milind Gupta <milind.gupta <at> ...> wrote:
> Hello John,
>
>           thanks for the reply. Here is an example code that does not work

Please try to provide enough code to make it run out of the box... I
had to add back GUI.initFrameH and rem out the usage of MainSizer to
make it even possible to run without error.

> (Pasted below and attached the file as well). A function from the main wxlua
> application calls filterFormActivate which sets up a simple frame with 2
> buttons and one static text and associates the button click to SelTaskPress.

I made the help menu callback function of the minimal.wx.lua sample
call filterform.filterFormActivate(frame).

> But when the button is clicked the test() statement gives an error. If that
> is removed then it cannot recognize the frame varaiable in the
> wx.wxFrame(frame line after test().

Please always include these errors in the future to help diagnose the problem.

In order to solve your problem I made test() a local function so that
it will be an upvalue of the Connect() function that calls it. Note
that the 'frame' that is used in the SelTaskPress() function is the
frame from the minimal.wx.lua and not the frame from
filterFormActivate().

To be honest, I have not done too much with modules and require in Lua
since I'm not sure I fully understand its behavior.

I just came across this which clears some things up for me. Note the
sentence "This means that any global functions are not visible by
default, which is why we had to create a local alias to print." This
is probably also why making test() local above fixed it. See also
package.seeall which might be useful to you.

Hope this helps,
    John



------------------------------------------------------------------------------ Virtualization & Cloud Management Using Capacity Planning Cloud computing makes use of virtualization - but cloud computing also focuses on allowing computing to be delivered as a service. http://www.accelacomm.com/jaw/sfnl/114/51521223/

_______________________________________________ wxlua-users mailing list wxlua-users-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org https://lists.sourceforge.net/lists/listinfo/wxlua-users

------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
wxlua-users mailing list
wxlua-users-5NWGOfrQmnd4wTydcyPnfg@public.gmane.orgceforge.net
https://lists.sourceforge.net/lists/listinfo/wxlua-users


------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing 
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
wxlua-users mailing list
wxlua-users@...
https://lists.sourceforge.net/lists/listinfo/wxlua-users
Mike Richards | 16 Feb 22:53
Picon
Favicon

Re: Event handling function inside a module

Milind,
You could also accomplish the same with a little less typing if you want:

module(...)
local _M = _M -- save the module table

then in your event handler function it's just setfenv(1, _M)

Mike

On 2/16/2012 4:42 PM, Milind Gupta wrote:
Hi Mike,
       I agree a good practice is to declare things local, only for things we want to access from outside unrestricted its easier not to declare them local. So anyway what I ended up doing is having the first statement in my event handler function as setfenv(1,package.loaded[modname]) where modname is the name of the module passed by lua to the module. Since require loads the module function there so it just picks it up from there. 

Thanks,
Milind

On Thu, Feb 16, 2012 at 12:06 PM, Mike Richards <mrichards42-KK0ffGbhmjU@public.gmane.org> wrote:
John and Milind,

function test() is declared in the current scope, and globally as filterform.test

After fiddling around for a bit, it looks like wx.wxEvtHandler:Connect saves upvalues, but not the current function environment (scope), which was set in the call to module(...)
When the event function is called, the function environment is reset to the global scope, so if you want to reference things you declared in the module scope in the event callback, you have a couple of options:
1. Declare everything as local, which to me is just good practice.
2. Use a fully qualified name (e.g. filterform.test())
3. Wrap all your calls to wx.wxEvtHandler:Connect like this:

function Connect(evthandler, id, func)
    local environment = getfenv(1)
    evthandler:Connect(id,
        function(event)
            setfenv(1, environment)
            func(event)
        end
    )
end

[...]

local button = wx.Button([...])
Connect(button, wx.wxEVT_COMMAND_BUTTON_CLICKED, function(event) end)

John -- It seems like wx.wxEvtHandler:Connect should do this anyways . . . that is, save the current function environment, and then apply it before calling the callback.

Mike


On 2/16/2012 3:14 AM, Milind Gupta wrote:
Hello John,
           Sorry for the carelessness. Here is a full set with minimal.lua which is the main file to run and the module function is called by the Help->About call back. That is true making test a local function makes it work although I am not totally clear why but if you press the other button 'Clear Task' it will give an error complaining about filter which again is not declared local. Actually declaring local  anything in a module only makes it private. Whether it is declared local or not it always resides in the module environment. So even if test or filter are not declared local they should be identified in the module environment. Here is the Programming in Lua Modules and Packages chapter that clarifies it:


At least that is how I understood it. So I think it should work without being declared local. In your previous email you said that the environment is saved as an upvalue with the execution of Connect. I am not sure how that would be done since the module itself does not know what environment  to Global it will be in. Like if you see in minimal I placed it in the environment newPackage and all my functions tables declared in the module i.e. test, filter, etc. are in the table newPackage in the global environment but the module code has no way of knowing that.
    One last point is that unless its an event callback function normal functions in the module run just fine whether test is local or not. As an example I just added the test() call as the first line in filterFormActivate which is a non event function so when the Help-> About callback calls filterFormActivate, test() just runs fine even though it is not local but now if you press the Select Task button it will say it could not find test().
 

Milind


On Wed, Feb 15, 2012 at 3:14 AM, Milind Gupta <milind.gupta <at> ...> wrote:
> Hello John,
>
>           thanks for the reply. Here is an example code that does not work

Please try to provide enough code to make it run out of the box... I
had to add back GUI.initFrameH and rem out the usage of MainSizer to
make it even possible to run without error.

> (Pasted below and attached the file as well). A function from the main wxlua
> application calls filterFormActivate which sets up a simple frame with 2
> buttons and one static text and associates the button click to SelTaskPress.

I made the help menu callback function of the minimal.wx.lua sample
call filterform.filterFormActivate(frame).

> But when the button is clicked the test() statement gives an error. If that
> is removed then it cannot recognize the frame varaiable in the
> wx.wxFrame(frame line after test().

Please always include these errors in the future to help diagnose the problem.

In order to solve your problem I made test() a local function so that
it will be an upvalue of the Connect() function that calls it. Note
that the 'frame' that is used in the SelTaskPress() function is the
frame from the minimal.wx.lua and not the frame from
filterFormActivate().

To be honest, I have not done too much with modules and require in Lua
since I'm not sure I fully understand its behavior.

I just came across this which clears some things up for me. Note the
sentence "This means that any global functions are not visible by
default, which is why we had to create a local alias to print." This
is probably also why making test() local above fixed it. See also
package.seeall which might be useful to you.

Hope this helps,
    John



------------------------------------------------------------------------------ Virtualization & Cloud Management Using Capacity Planning Cloud computing makes use of virtualization - but cloud computing also focuses on allowing computing to be delivered as a service. http://www.accelacomm.com/jaw/sfnl/114/51521223/

_______________________________________________ wxlua-users mailing list wxlua-users-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org https://lists.sourceforge.net/lists/listinfo/wxlua-users

------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
wxlua-users mailing list
wxlua-users-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
https://lists.sourceforge.net/lists/listinfo/wxlua-users




------------------------------------------------------------------------------ Virtualization & Cloud Management Using Capacity Planning Cloud computing makes use of virtualization - but cloud computing also focuses on allowing computing to be delivered as a service. http://www.accelacomm.com/jaw/sfnl/114/51521223/

_______________________________________________ wxlua-users mailing list wxlua-users-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org https://lists.sourceforge.net/lists/listinfo/wxlua-users
------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing 
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
wxlua-users mailing list
wxlua-users@...
https://lists.sourceforge.net/lists/listinfo/wxlua-users
Milind Gupta | 16 Feb 23:17
Picon

Re: Event handling function inside a module

Thanks,

Milind

On Thu, Feb 16, 2012 at 1:53 PM, Mike Richards <mrichards42-KK0ffGbhmjU@public.gmane.org> wrote:
Milind,
You could also accomplish the same with a little less typing if you want:

module(...)
local _M = _M -- save the module table

then in your event handler function it's just setfenv(1, _M)

Mike

On 2/16/2012 4:42 PM, Milind Gupta wrote:
Hi Mike,
       I agree a good practice is to declare things local, only for things we want to access from outside unrestricted its easier not to declare them local. So anyway what I ended up doing is having the first statement in my event handler function as setfenv(1,package.loaded[modname]) where modname is the name of the module passed by lua to the module. Since require loads the module function there so it just picks it up from there. 

Thanks,
Milind

On Thu, Feb 16, 2012 at 12:06 PM, Mike Richards <mrichards42-KK0ffGbhmjU@public.gmane.org> wrote:
John and Milind,

function test() is declared in the current scope, and globally as filterform.test

After fiddling around for a bit, it looks like wx.wxEvtHandler:Connect saves upvalues, but not the current function environment (scope), which was set in the call to module(...)
When the event function is called, the function environment is reset to the global scope, so if you want to reference things you declared in the module scope in the event callback, you have a couple of options:
1. Declare everything as local, which to me is just good practice.
2. Use a fully qualified name (e.g. filterform.test())
3. Wrap all your calls to wx.wxEvtHandler:Connect like this:

function Connect(evthandler, id, func)
    local environment = getfenv(1)
    evthandler:Connect(id,
        function(event)
            setfenv(1, environment)
            func(event)
        end
    )
end

[...]

local button = wx.Button([...])
Connect(button, wx.wxEVT_COMMAND_BUTTON_CLICKED, function(event) end)

John -- It seems like wx.wxEvtHandler:Connect should do this anyways . . . that is, save the current function environment, and then apply it before calling the callback.

Mike


On 2/16/2012 3:14 AM, Milind Gupta wrote:
Hello John,
           Sorry for the carelessness. Here is a full set with minimal.lua which is the main file to run and the module function is called by the Help->About call back. That is true making test a local function makes it work although I am not totally clear why but if you press the other button 'Clear Task' it will give an error complaining about filter which again is not declared local. Actually declaring local  anything in a module only makes it private. Whether it is declared local or not it always resides in the module environment. So even if test or filter are not declared local they should be identified in the module environment. Here is the Programming in Lua Modules and Packages chapter that clarifies it:


At least that is how I understood it. So I think it should work without being declared local. In your previous email you said that the environment is saved as an upvalue with the execution of Connect. I am not sure how that would be done since the module itself does not know what environment  to Global it will be in. Like if you see in minimal I placed it in the environment newPackage and all my functions tables declared in the module i.e. test, filter, etc. are in the table newPackage in the global environment but the module code has no way of knowing that.
    One last point is that unless its an event callback function normal functions in the module run just fine whether test is local or not. As an example I just added the test() call as the first line in filterFormActivate which is a non event function so when the Help-> About callback calls filterFormActivate, test() just runs fine even though it is not local but now if you press the Select Task button it will say it could not find test().
 

Milind


On Wed, Feb 15, 2012 at 3:14 AM, Milind Gupta <milind.gupta <at> ...> wrote:
> Hello John,
>
>           thanks for the reply. Here is an example code that does not work

Please try to provide enough code to make it run out of the box... I
had to add back GUI.initFrameH and rem out the usage of MainSizer to
make it even possible to run without error.

> (Pasted below and attached the file as well). A function from the main wxlua
> application calls filterFormActivate which sets up a simple frame with 2
> buttons and one static text and associates the button click to SelTaskPress.

I made the help menu callback function of the minimal.wx.lua sample
call filterform.filterFormActivate(frame).

> But when the button is clicked the test() statement gives an error. If that
> is removed then it cannot recognize the frame varaiable in the
> wx.wxFrame(frame line after test().

Please always include these errors in the future to help diagnose the problem.

In order to solve your problem I made test() a local function so that
it will be an upvalue of the Connect() function that calls it. Note
that the 'frame' that is used in the SelTaskPress() function is the
frame from the minimal.wx.lua and not the frame from
filterFormActivate().

To be honest, I have not done too much with modules and require in Lua
since I'm not sure I fully understand its behavior.

I just came across this which clears some things up for me. Note the
sentence "This means that any global functions are not visible by
default, which is why we had to create a local alias to print." This
is probably also why making test() local above fixed it. See also
package.seeall which might be useful to you.

Hope this helps,
    John



------------------------------------------------------------------------------ Virtualization & Cloud Management Using Capacity Planning Cloud computing makes use of virtualization - but cloud computing also focuses on allowing computing to be delivered as a service. http://www.accelacomm.com/jaw/sfnl/114/51521223/

_______________________________________________ wxlua-users mailing list wxlua-users-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org https://lists.sourceforge.net/lists/listinfo/wxlua-users

------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
wxlua-users mailing list
wxlua-users-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
https://lists.sourceforge.net/lists/listinfo/wxlua-users




------------------------------------------------------------------------------ Virtualization & Cloud Management Using Capacity Planning Cloud computing makes use of virtualization - but cloud computing also focuses on allowing computing to be delivered as a service. http://www.accelacomm.com/jaw/sfnl/114/51521223/

_______________________________________________ wxlua-users mailing list wxlua-users-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org https://lists.sourceforge.net/lists/listinfo/wxlua-users

------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
wxlua-users mailing list
wxlua-users-5NWGOfrQmnd4wTydcyPnfg@public.gmane.orgceforge.net
https://lists.sourceforge.net/lists/listinfo/wxlua-users


------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing 
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
wxlua-users mailing list
wxlua-users@...
https://lists.sourceforge.net/lists/listinfo/wxlua-users

Gmane