' The MIT License (MIT)
'
' Copyright (C) 2026 Scott Duensing
'
' Permission is hereby granted, free of charge, to any person obtaining a copy
' of this software and associated documentation files (the "Software"), to
' deal in the Software without restriction, including without limitation the
' rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
' sell copies of the Software, and to permit persons to whom the Software is
' furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in
' all copies or substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
' FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
' IN THE SOFTWARE.

' dynform.bas -- Dynamic Form Demo
'
' Demonstrates building a complete form entirely in code
' with no .frm file needed:
'   CreateForm      - create a form programmatically
'   CreateControl   - add controls with optional parent
'   SetEvent        - bind events to arbitrary handlers
'   RemoveControl   - remove a control at runtime
'   Optional params - language extension for optional arguments
'
' Add commdlg.bas to your project, then click Run.

DIM frm AS LONG
DIM lbl AS LONG
DIM btnBar AS LONG

' Create a form entirely from code
SET frm = CreateForm("DynDemo", 350, 200)
frm.Caption = "Dynamic Form Demo"

' Add a label at the top
SET lbl = CreateControl(frm, "Label", "StatusLabel")
StatusLabel.Caption = "Built entirely in code!"

' Add a horizontal button bar container
SET btnBar = CreateControl(frm, "HBox", "ButtonBar")

' Add buttons inside the HBox, using the optional parent parameter
DIM btnHello AS LONG
SET btnHello = CreateControl(frm, "CommandButton", "BtnHello", btnBar)
BtnHello.Caption = "Hello"

DIM btnFile AS LONG
SET btnFile = CreateControl(frm, "CommandButton", "BtnFile", btnBar)
BtnFile.Caption = "Open File..."

DIM btnRemove AS LONG
SET btnRemove = CreateControl(frm, "CommandButton", "BtnRemove", btnBar)
BtnRemove.Caption = "Remove Me"

' Wire events to custom handler names using SetEvent
SetEvent btnHello, "Click", "OnHelloClick"
SetEvent btnFile, "Click", "OnFileClick"
SetEvent btnRemove, "Click", "OnRemoveClick"

frm.Show

PRINT "Dynamic form created. Try the buttons!"


SUB OnHelloClick
    DIM name AS STRING
    name = basInputBox2("Greeting", "What is your name?", "World")
    IF name <> "" THEN
        StatusLabel.Caption = "Hello, " + name + "!"
        PRINT "Greeted: " + name
    END IF
END SUB


SUB OnFileClick
    DIM path AS STRING
    path = basFileOpen("Open a File", "All Files (*.*)")
    IF path <> "" THEN
        StatusLabel.Caption = "Selected: " + path
        PRINT "File: " + path
    ELSE
        StatusLabel.Caption = "No file selected."
    END IF
END SUB


SUB OnRemoveClick
    StatusLabel.Caption = "Button removed!"
    RemoveControl frm, "BtnRemove"
    PRINT "BtnRemove removed from form."
END SUB
