Page 1 of 1

Proper demo on gadget list usage

Posted: Thu Jan 13, 2011 8:32 pm
by ultralazor
The help doc is poorly worded on this and threads on it don't really exist, so I put this together. I haven't been using PB long and needed to swap gadgets on a single window.

NOTE:If you don't mind adapting API code to each of your intended platforms you can also share gadgets really easy with GTK API. There is a performance cost using pure PB cause you have to have duplicate elements per list. The hack(for PB not GTK) involves appending to Glist structs using the append call. It's in PB already just not used this way. The only pure PB method is heavily redundant freeing of the gadgets them creating them again.

For example here in pure PB it takes 9 gadgets to make 3 buttons that are identical in each list. It'd actually take more code and memory to do the free-then-reuse method, refined you'd just free and create them per-list-load and not on creation, which still take a lot of code. Using untapped PB resources(existing GTK API call) it can b done with just one API call with a pointer to the list..

Code: Select all

Enumeration
  #Editor_0
  #Editor_1
  #Editor_2
  #Button_0
  #Button_1
  #Button_2
  #Button_3
  #Button_4
  #Button_5
  #Button_6
  #Button_7
  #Button_8
  #Container_0
  #Container_1
  #Container_2
EndEnumeration

Global.l first,second,third

If OpenWindow(0,383,200,419,414,"Swap Gadget Test",#PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
  ContainerGadget(#Container_0,0,0,419,414,#PB_Container_BorderLess)
  EditorGadget(#Editor_0, 0, 0, 419, 350)
  SetGadgetColor(#Editor_0,#PB_Gadget_BackColor,RGB(0,0,0))
  SetGadgetColor(#Editor_0,#PB_Gadget_FrontColor,RGB(252,144,3))
  ButtonGadget(#Button_0, 10, 360, 90, 40, "Editor1")
  ButtonGadget(#Button_1, 100, 360, 90, 40, "Editor2")
  ButtonGadget(#Button_2, 190, 360, 90, 40, "Editor3")
  first=UseGadgetList(WindowID(0))
  CloseGadgetList()
  HideGadget(#Container_0,#True)
  ContainerGadget(#Container_1,0,0,419,414,#PB_Container_BorderLess)
  EditorGadget(#Editor_1, 0, 0, 419, 350)
  SetGadgetColor(#Editor_1,#PB_Gadget_BackColor,RGB(29,219,226))
  SetGadgetColor(#Editor_1,#PB_Gadget_FrontColor,RGB(0,0,0))
  ButtonGadget(#Button_3, 10, 360, 90, 40, "Editor1")
  ButtonGadget(#Button_4, 100, 360, 90, 40, "Editor2")
  ButtonGadget(#Button_5, 190, 360, 90, 40, "Editor3")
  second=UseGadgetList(WindowID(0))
  CloseGadgetList()
  HideGadget(#Container_1,#True)
  ContainerGadget(#Container_2,0,0,419,414,#PB_Container_BorderLess)
  EditorGadget(#Editor_2, 0, 0, 419, 350)
  SetGadgetColor(#Editor_2,#PB_Gadget_BackColor,RGB(255,0,0))
  SetGadgetColor(#Editor_2,#PB_Gadget_FrontColor,RGB(255,255,0))
  ButtonGadget(#Button_6, 10, 360, 90, 40, "Editor1")
  ButtonGadget(#Button_7, 100, 360, 90, 40, "Editor2")
  ButtonGadget(#Button_8, 190, 360, 90, 40, "Editor3")
  third=UseGadgetList(WindowID(0))
  CloseGadgetList()
  HideGadget(#Container_1,#True)
  HideGadget(#Container_2,#True)
  HideGadget(#Container_0,#False)
  UseGadgetList(first)
EndIf

Repeat
  Event = WaitWindowEvent()
  GadgetID = EventGadget()
  EventType = EventType()
  If Event = #PB_Event_Gadget
    If GadgetID = #Button_0 Or GadgetID = #Button_3 Or GadgetID = #Button_6
      If EventType = #PB_EventType_LeftClick
        HideGadget(#Container_1,#True)
        HideGadget(#Container_2,#True)
        HideGadget(#Container_0,#False)
        UseGadgetList(first)
      EndIf
    ElseIf GadgetID = #Button_1 Or GadgetID = #Button_4 Or GadgetID = #Button_7
      If EventType = #PB_EventType_LeftClick
        HideGadget(#Container_0,#True)
        HideGadget(#Container_2,#True)
        HideGadget(#Container_1,#False)
        UseGadgetList(second)
      EndIf
    ElseIf GadgetID = #Button_2 Or GadgetID = #Button_5 Or GadgetID = #Button_8
      If EventType = #PB_EventType_LeftClick
        HideGadget(#Container_0,#True)
        HideGadget(#Container_1,#True)
        HideGadget(#Container_2,#False)
        UseGadgetList(third)
      EndIf
    EndIf
  EndIf
Until Event = #PB_Event_CloseWindow
End

Re: Proper demo on gadget list usage

Posted: Fri Jan 14, 2011 5:28 am
by Demivec
@ultralazor: Here's an alternative for the Repeat/Until loop that uses Select/Case instead of the If/ElseIf/Endif. Its only a minor change but I post it just to show what I think are improvements in the readability. Thanks for the Demo. :wink:

Code: Select all

Repeat
  event = WaitWindowEvent()
  GadgetID = EventGadget()
  EventType = EventType()
  If event = #PB_Event_Gadget
    Select GadgetID
      Case #Button_0, #Button_3, #Button_6
        If EventType = #PB_EventType_LeftClick
          HideGadget(#Container_1,#True)
          HideGadget(#Container_2,#True)
          HideGadget(#Container_0,#False)
          UseGadgetList(first)
        EndIf
      Case #Button_1, #Button_4, #Button_7
        If EventType = #PB_EventType_LeftClick
          HideGadget(#Container_0,#True)
          HideGadget(#Container_2,#True)
          HideGadget(#Container_1,#False)
          UseGadgetList(Second)
        EndIf
      Case #Button_2, #Button_5, #Button_8
        If EventType = #PB_EventType_LeftClick
          HideGadget(#Container_0,#True)
          HideGadget(#Container_1,#True)
          HideGadget(#Container_2,#False)
          UseGadgetList(third)
        EndIf
    EndSelect
  EndIf
Until event = #PB_Event_CloseWindow