Syntax
Success = WidgetFree(Widget)
Description
Removes the specified widget from its parent layout and frees all associated resources. The widget will no longer be visible or interactive, and its handle becomes invalid.
Parameters
Widget
The handle of the ProGUI widget to free.
Return Value
Returns #True if the widget was successfully marked for deletion, or #False if the
handle was invalid.
Remarks
This function queues the widget for deletion. The actual removal from the layout and freeing of memory happens
during the layout update cycle. If the widget had its own layout assigned (WidgetSetLayout()), that layout and all its contained widgets will also be
freed recursively. The #PG_Event_Destroy event is dispatched to the widget just before its
resources are released, allowing for custom cleanup.
Example
IncludeFile "ProGUI_PB.pbi"
StartProGUI()
Global WidgetToFree
; Event handler for button click
Procedure ButtonHandler(Widget, EventType, *EventData, *UserData)
If EventType = #PG_Event_MouseLeftButtonUp
If WidgetToFree
Debug "Freeing the widget..."
WidgetFree(WidgetToFree)
Debug "Widget marked for deletion."
WidgetToFree = #Null ; Clear the global handle
Else
Debug "Widget already freed."
EndIf
EndIf
EndProcedure
; Event handler for the widget being freed
Procedure DestroyHandler(Widget, EventType, *EventData, *UserData)
If EventType = #PG_Event_Destroy
Debug "Widget Destroy event received!"
; Perform any cleanup related to this specific widget here
EndIf
EndProcedure
MyWindow = CreateWindow(0, 0, 300, 200, "WidgetFree Example")
RootLayout = WindowGetLayout(MyWindow)
LayoutSetType(RootLayout, #PG_Layout_Type_Flex)
LayoutFlexSetDirection(RootLayout, #PG_Flex_Direction_Column)
LayoutSetPadding(RootLayout, 10)
If MyWindow
; The widget we will free
WidgetToFree = CreateWidget(0, 0, 200, 50)
WidgetSetClass(WidgetToFree, "status-label") ; Assume skin exists
WidgetSetMargin(WidgetToFree, 5)
AddEventHandler(WidgetToFree, #PG_Event_Destroy, @DestroyHandler())
; Add drawing handler if needed
; A button to trigger freeing the widget
FreeButton = CreateWidget(0, 0, 100, 30)
WidgetSetClass(FreeButton, "button") ; Assume skin exists
WidgetSetMargin(FreeButton, 5)
AddEventHandler(FreeButton, #PG_Event_MouseLeftButtonUp, @ButtonHandler())
; Add drawing handler if needed
WindowShow(MyWindow, #True, #PG_Window_ScreenCentered)
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
EndIf
StopProGUI()
See Also
CreateWidget, CreateOsWidget, #PG_Event_Destroy, AddEventHandler
Supported OS
Windows, Linux