Module: XmlGadget

Share your advanced knowledge/code with the community.
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Module: XmlGadget

Post by Peter »

Hello,

this is my first attempt for a XmlGadget Module. XmlGadget allows to render DialogLibrary functionality into a ContainerGadget.

Example:

Code: Select all

OpenWindow(#MainWindow, 0, 0, 200, 200, "XmlGadget", #PB_Window_ScreenCentered | #PB_Window_SizeGadget)

Xml = "<vbox>" +
      " <button name='myButton1' text='Button1' onevent='ButtonEvent1()' />" +
      " <button name='myButton2' text='Button2' onevent='ButtonEvent2()' />" +
      "</vbox>"

XmlGadget(#ContainerGadget, 10, 10, WindowWidth(#MainWindow) - 20, WindowHeight(#MainWindow) - 20, Xml, #PB_Container_Flat)
Image

Feel free to ask, if you have any questions. :)

Greetings ... Peter

P.S.: I am not entirely happy with the naming of this module. If you have a better name, feel free to suggest it.

Module:

Code: Select all

DeclareModule XmlGadget
  
  EnableExplicit
  
  Declare XmlGadget(Gadget, x, y, Width, Height, Xml.s, Flags = 0)
  Declare ResizeXmlGadget(Gadget, x, y, Width, Height)
  Declare XmlDialogGadget(Gadget, Name.s)
  
EndDeclareModule

Module XmlGadget
  
  EnableExplicit
  
  Procedure ResizeXmlGadget(Gadget, x, y, Width, Height)
    
    If IsGadget(Gadget)
      
      ResizeGadget(Gadget, x, y, Width, Height)
      
      Protected oDLG
      
      Protected GID = GadgetID(Gadget)      
      
      ! v_odlg = $(v_gid).data("oDLG");
      
      ResizeWindow(DialogWindow(oDLG), 0, 0, Width, Height)
      
      RefreshDialog(oDLG)
      
    EndIf
    
  EndProcedure
  
  Procedure XmlGadget(Gadget, x, y, Width, Height, Xml.s, Flags = 0)
    
    If Gadget = #PB_Any
      Gadget = ContainerGadget(Gadget, x, y, Width, Height, Flags)
    Else
      ContainerGadget(Gadget, x, y, Width, Height, Flags)
    EndIf
    
    CloseGadgetList()
    
    Protected WindowName.s = "window_" + Str(ElapsedMilliseconds())
    
    Xml = "<window name='" + WindowName + "' flags='#PB_Window_BorderLess'>" + Xml + "</window>"
    
    Protected oXML, oDLG
    
    oXML = ParseXML(#PB_Any, Xml)
    
    If oXML And XMLStatus(oXML) = #PB_XML_Success
      
      oDLG = CreateDialog(#PB_Any)
      
      If OpenXMLDialog(oDLG, oXML, WindowName)
        
        Protected WID = WindowID(DialogWindow(oDLG))
        Protected GID = GadgetID(Gadget)
        
        ! var e = $(v_wid.window).detach()
        
        ! $(v_gid.div.children[0]).append(e);
        
        ! $(v_gid).data("oXML", v_oxml);
        ! $(v_gid).data("oDLG", v_odlg);
        
        ResizeWindow(DialogWindow(oDLG), 0, 0, Width, Height)
        
        RefreshDialog(oDLG)
        
        ProcedureReturn Gadget
        
      Else
        
        Debug "Dialog error: " + DialogError(oDLG)
        
      EndIf
      
    Else
      
      Debug "XML error: " + XMLError(oXML)
      
    EndIf
    
    ProcedureReturn -1
    
  EndProcedure
  
  Procedure XmlDialogGadget(Gadget, Name.s)
    
    If IsGadget(Gadget)
      
      Protected oDLG
      
      Protected GID = GadgetID(Gadget)
      
      ! v_odlg = $(v_gid).data("oDLG");
      
      ProcedureReturn DialogGadget(oDLG, Name)
      
    EndIf    
    
  EndProcedure
  
EndModule

CompilerIf #PB_Compiler_IsMainFile
  
  EnableExplicit
  
  ; Example:
  
  Enumeration 
    #MainWindow
    #ContainerGadget
  EndEnumeration
  
  Runtime Procedure ButtonEvent1()
    Debug "ButtonEvent1()"
  EndProcedure
  
  Runtime Procedure ButtonEvent2()
    Debug "ButtonEvent2()"
  EndProcedure
  
  Procedure SizeWindowEvent()
    
    XmlGadget::ResizeXmlGadget(#ContainerGadget, #PB_Ignore, #PB_Ignore, WindowWidth(#MainWindow) - 20, WindowHeight(#MainWindow) - 20)
    
  EndProcedure
  
  Define Xml.s
  
  UseModule XmlGadget
    
    OpenWindow(#MainWindow, 0, 0, 200, 200, "XmlGadget", #PB_Window_ScreenCentered | #PB_Window_SizeGadget)
    
    Xml = "<vbox>" +
          " <button name='myButton1' text='Button1' onevent='ButtonEvent1()' />" +
          " <button name='myButton2' text='Button2' onevent='ButtonEvent2()' />" +
          "</vbox>"
    
    XmlGadget(#ContainerGadget, 10, 10, WindowWidth(#MainWindow) - 20, WindowHeight(#MainWindow) - 20, Xml, #PB_Container_Flat)
    
    SetGadgetText(XmlDialogGadget(#ContainerGadget, "myButton1"), "myNewButtonText")
    
    BindEvent(#PB_Event_SizeWindow, @SizeWindowEvent(), #MainWindow)
    
  UnuseModule XmlGadget
  
CompilerEndIf
ljgww
Posts: 26
Joined: Thu Mar 26, 2020 5:47 pm

Re: Module: XmlGadget

Post by ljgww »

Since there is (Open)XMLDialog already it makes sense to be called XMLGadget - will go along with xxxxxGadget naming as it is in Pure/Spider basic already.

Now....

In JavaFX this is called HBox or VBox layout.

In GTK 2/3 this is called "Interface" and it is default method for building GUI interfaces (see: Glade)

in QT it is called 'Dynamic layout'. Many other GUI's do not have this method at all AFAIK (TCL/TK, WxWidgets etc)

Did not find anything of the kind in 'Windows Forms'. Perhaps there is some method in Metro UI (MDL). Perhaps there was something in WPF once upon a time.

In essence it is 'layout' it renders in the 'box', it is an 'interface', it is inherently 'dynamic' and it is a 'gadget'... 'builder' may also be applicable.

So there are some naming ideas....

Speculation: could be called DynamicBoxGadget or InterfaceGadget or DynamicGadget or LayoutBoxGadget or something else. XMLGadget is shortest though and intuitive.

At any rate this is something I was looking for, and I believe it is great what ever name it has!

Cheers!
Fred
Site Admin
Posts: 1506
Joined: Mon Feb 24, 2014 10:51 am

Re: Module: XmlGadget

Post by Fred »

Nice trick :)
Post Reply