Page 1 of 1
DestroyGadget Event
Posted: Wed Oct 30, 2013 8:20 pm
by idle
Would be useful to add a Destroy Gadget Event so when a window is closed the event can fire
so custom gadget can clean up eg free heap memory.
Re: DestroyGadget Event
Posted: Wed Oct 30, 2013 11:35 pm
by Bisonte
If you have custom gadgetdata stored in a list/map you may use
Code: Select all
Procedure EventCloseWindow()
Protected Window = EventWindow()
If WindowInMyList
; free gadgets memory etc
; maybe a map/list hold the data
; UnBindGadgetEvents.... etc
UnbindEvent(#PB_Event_CloseWindow, @EventCloseWindow(), Window)
EndIf
EndProcedure
Or miss I something ?
I wish a command to send an event directly to a gadget...
With PostEvent() I can only send an event to a window...
Re: DestroyGadget Event
Posted: Thu Oct 31, 2013 2:58 am
by idle
Yes you can do it like that but we shouldn't have to store custom gadgets in a list or map
and should be free to heap allocate them and set a callback to clean them up
Re: DestroyGadget Event
Posted: Thu Oct 31, 2013 11:50 am
by PB
Why can't you just watch for #PB_Event_CloseWindow and then execute your cleanup code?
Re: DestroyGadget Event
Posted: Thu Oct 31, 2013 6:19 pm
by Danilo
PB wrote:Why can't you just watch for #PB_Event_CloseWindow and then execute your cleanup code?
FreeGadget() does not fire #PB_Event_CloseWindow event. Gadgets are also freed/destroyed automatically
when the parent of the gadget (ContainerGadget(), PanelGadget() etc.) is freed, see help for FreeGadget().
So #PB_EventType_Destroy should be send for:
Code: Select all
- FreeGadget() is used with the gadget
- The window that contains the gadget is closed.
- The parent of the gadget (ContainerGadget(), PanelGadget() etc.) is freed.
- The program ends.
Re: DestroyGadget Event
Posted: Thu Oct 31, 2013 7:19 pm
by hallodri
i have a little workaround
Code: Select all
Structure GadgetVT Align 4
GadgetType.l
SizeOf.l
GadgetCallback.i
FreeGadget.i
EndStructure
Structure Gadget
Gadget.i
*vt.GadgetVT
EndStructure
Prototype CallFreeProc(Gadget)
Structure FreeProcInfo
OldProc.CallFreeProc
NewProc.CallFreeProc
GadgedNR.i
EndStructure
Global NewMap OldFreeProc.FreeProcInfo()
;-------------------------------------------
Procedure FreeProc(Gadget)
Protected GadgedNR
If Gadget
GadgedNR = OldFreeProc(Str(gadget))\GadgedNR
OldFreeProc(Str(gadget))\NewProc(GadgedNR)
DeleteMapElement(OldFreeProc(), Str(gadget))
If OldFreeProc(Str(gadget))\OldProc
ProcedureReturn OldFreeProc(Str(gadget))\OldProc(GadgedNR)
EndIf
EndIf
EndProcedure
;-------------------------------------------
Procedure SetFreeProc(Gadget, Proc)
Protected *g.Gadget
*g = IsGadget(gadget)
If *g
OldFreeProc(Str(*g))\GadgedNR = Gadget
OldFreeProc(Str(*g))\OldProc = *g\vt\FreeGadget
OldFreeProc(Str(*g))\NewProc = Proc
*g\vt\FreeGadget = @FreeProc()
EndIf
EndProcedure
;######################################################
Procedure FreeButton(Gadget)
Debug "Free ButtonGadget"
EndProcedure
Procedure FreeString(Gadget)
Debug "Free StringGadget"
EndProcedure
Procedure Main()
Protected event
If OpenWindow(0, #PB_Ignore, #PB_Ignore, 300, 300, "") And InitScintilla()
ButtonGadget(0, 0, 0, 100, 25, "Blub")
ButtonGadget(1, 0, 0, 100, 25, "Blub")
StringGadget(2, 0, 30, 100, 25, "Blub")
ButtonGadget(3, 0, 0, 100, 25, "Blub")
ButtonGadget(4, 0, 0, 100, 25, "Blub")
CanvasGadget(5, 0, 30, 100, 25)
SetFreeProc(0, @FreeButton())
SetFreeProc(1, @FreeButton())
SetFreeProc(2, @FreeString())
SetFreeProc(3, @FreeString())
SetFreeProc(4, @FreeString())
SetFreeProc(5, @FreeButton())
Repeat
event = WaitWindowEvent()
If event = #PB_Event_CloseWindow
Break
EndIf
ForEver
EndIf
EndProcedure: End main()
Re: DestroyGadget Event
Posted: Thu Oct 31, 2013 8:09 pm
by ts-soft
@hallodri
crashes on line 37 with x64 compiler!