DestroyGadget Event

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
idle
Always Here
Always Here
Posts: 5844
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

DestroyGadget Event

Post 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.
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: DestroyGadget Event

Post 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...
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
idle
Always Here
Always Here
Posts: 5844
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: DestroyGadget Event

Post 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
Windows 11, Manjaro, Raspberry Pi OS
Image
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: DestroyGadget Event

Post by PB »

Why can't you just watch for #PB_Event_CloseWindow and then execute your cleanup code?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: DestroyGadget Event

Post 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.
User avatar
hallodri
Enthusiast
Enthusiast
Posts: 208
Joined: Tue Nov 08, 2005 7:59 am
Location: Germany
Contact:

Re: DestroyGadget Event

Post 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()
Last edited by hallodri on Thu Oct 31, 2013 9:52 pm, edited 1 time in total.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: DestroyGadget Event

Post by ts-soft »

@hallodri

crashes on line 37 with x64 compiler!
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Post Reply