Automatic resizing of gadgets

Share your advanced knowledge/code with the community.
pf shadoko
Posts: 74
Joined: Thu May 26, 2016 11:09 am

Automatic resizing of gadgets

Post by pf shadoko »

Hello,

I wrapped the functions relating to gadgets to enable them automatically resize when you resize the window
(There is not all gadgets, but you'll see it's very easy to add more)
it also works in PB

Code: Select all

EnableExplicit

;############################################################################################################################
;                             Automatic resizing of gadgets - pf shadoko - 2016
;
;   operation:
;   Instructions: OpenWindow, ButtonGadget, TextGadget, ..., CloseGadgetList
;   must be used With the suffix 'R'
;   2 additional parameters: rx And ry match the type of resizing:
;   rx / ry = 0: no change
;   rx / ry = 1: modification of the x / y position
;   rx / ry = 2: changing the width / height
;   rx / ry = 3: proportional positioning
;  (Rx / ry = 4: proportional positioning one side)
;  (Rx / ry = 5: proportional positioning of the other side)
;############################################################################################################################

Structure GadgetInfo
  g.i             ;gadgetID
  Map Glist.s()   ;list of contained gadgets  (for containers)
  x.w:  y.w       ;position of origin
  dx.w: dy.w      ;dimension of origin
  rx.b : ry.b     ;type of resize
EndStructure

Global Dim GadgetCont.s(256), GadgetConti
Global NewMap GadgetList.GadgetInfo()

Procedure Redimensioner(c, nx, ny, ndx, ndy, t.s="G")
  Protected.w ox1,oy1, ox2,oy2, x1,y1, x2,y2
  Protected gi.GadgetInfo, o.GadgetInfo,  adx,ady,  r.f,d.w 
  Macro RedimensionerD(t, v1, V2, oV1, oV2, adV, ndV)
    d = ndV - adV
    r = ndV / adV
    Select t
      Case 0: v1 = oV1:       V2 = oV2
      Case 1: v1 = oV1 + d:   V2 = oV2 + d
      Case 2: v1 = oV1:       V2 = oV2 + d
      Case 3: v1 = oV1 * r:   V2 = oV2 * r
      Case 4: v1 = oV1:       V2 = oV2 * r
      Case 5: v1 = oV1 * r:   V2 = oV2 + d
    EndSelect
  EndMacro
  gi=GadgetList(t+Str(c))
  adx = gi\dx
  ady = gi\dy
  ForEach gi\Glist():o=GadgetList(gi\Glist())
       If (o\rx Or o\ry)
           ox1 = o\x: ox2 = ox1 + o\dx: RedimensionerD (o\rx, x1, x2, ox1, ox2, adx, ndx)
           oy1 = o\y: oy2 = oy1 + o\dy: RedimensionerD (o\ry, y1, y2, oy1, oy2, ady, ndy)
           Redimensioner (o\g, x1, y1, x2 - x1, y2 - y1)
       EndIf
  Next
  If t="G":ResizeGadget(c,nx, ny, ndx, ndy):EndIf
EndProcedure

Procedure GadgetInfo(na,n,x.w,y.w,dx.w,dy.w,rx.b,ry.b,iscontainer=0)
  Protected gi.GadgetInfo, tg.s
  With gi
    If n=-1:\g=na:Else:\g=n:EndIf
    \x=x: \dx=dx: \rx=rx
    \y=y: \dy=dy: \ry=ry
    If iscontainer=-1
      tg="W"+Str(\g):GadgetConti=0 
    Else
      tg="G"+Str(\g):GadgetList(GadgetCont(GadgetConti))\Glist(tg)=tg
    EndIf
    GadgetList(tg)=gi
    If iscontainer:GadgetConti+1:GadgetCont(GadgetConti)=tg :EndIf
    ProcedureReturn \g
  EndWith
EndProcedure

;--------------------------------- gadgets
Procedure ButtonGadgetR(n,x,y,dx,dy,txt.s,f=0,rx=0,ry=0)
  Protected na=ButtonGadget(n,x,y,dx,dy,txt,f)
  ProcedureReturn GadgetInfo(na,n,x,y,dx,dy,rx,ry)
EndProcedure

Procedure TextGadgetR(n,x,y,dx,dy,txt.s,f=0,rx=0,ry=0)
  Protected na=TextGadget(n,x,y,dx,dy,txt,f)
  ProcedureReturn GadgetInfo(na,n,x,y,dx,dy,rx,ry)
EndProcedure

Procedure CheckBoxGadgetR(n,x,y,dx,dy,txt.s,f=0,rx=0,ry=0)
  Protected na=CheckBoxGadget(n,x,y,dx,dy,txt,f)
  ProcedureReturn GadgetInfo(na,n,x,y,dx,dy,rx,ry)
EndProcedure

Procedure StringGadgetR(n,x,y,dx,dy,txt.s,f=0,rx=0,ry=0)
  Protected na=StringGadget(n,x,y,dx,dy,txt,f)
  ProcedureReturn GadgetInfo(na,n,x,y,dx,dy,rx,ry)
EndProcedure

Procedure EditorGadgetR(n,x,y,dx,dy,f=0,rx=0,ry=0)
  Protected na=EditorGadget(n,x,y,dx,dy,f)
  ProcedureReturn GadgetInfo(na,n,x,y,dx,dy,rx,ry)
EndProcedure

Procedure ButtonImageGadgetR(n,x,y,dx,dy,imageID,f=0,rx=0,ry=0)
  Protected na=ButtonImageGadget(n,x,y,dx,dy,imageID,f)
  ProcedureReturn GadgetInfo(na,n,x,y,dx,dy,rx,ry)
EndProcedure

;--------------------------------- containers
Procedure ContainerGadgetR(n,x,y,dx,dy,f=0,rx=0,ry=0)
  Protected na=ContainerGadget(n,x,y,dx,dy,f)
  ProcedureReturn GadgetInfo(na,n,x,y,dx,dy,rx,ry,1)
EndProcedure

Procedure PanelGadgetR(n,x,y,dx,dy,rx=0,ry=0)
  Protected na=PanelGadget(n,x,y,dx,dy)
  ProcedureReturn GadgetInfo(na,n,x,y,dx,dy,rx,ry,1)
EndProcedure

Procedure ScrollAreaGadgetR(n,x,y,dx,dy,scrolldx,scrolldy,scrollstep,f=0,rx=0,ry=0)
  Protected na=ScrollAreaGadget(n,x,y,dx,dy,scrolldx,scrolldy,scrollstep,f)
  ProcedureReturn GadgetInfo(na,n,x,y,dx,dy,rx,ry,1)
EndProcedure

;--------------------------------- window
Procedure WindowResizeEvent()
  Protected n=EventWindow()
  Redimensioner(n,0,0,WindowWidth(n), WindowHeight(n),"W")
EndProcedure

Procedure OpenWindowR(n,x,y,dx,dy,txt.s,f=#PB_Window_SystemMenu,pid=0)
  Protected na=OpenWindow(n,x,y,dx,dy,txt,f,pid)
  Protected nw=GadgetInfo(na,n,x,y,dx,dy,2,2,-1)
  BindEvent(#PB_Event_SizeWindow,@WindowResizeEvent(), nw)
  ProcedureReturn nw
EndProcedure

;---------------------------------
Procedure CloseGadgetListR() 
  CloseGadgetList()
  GadgetConti-1
EndProcedure

;############################################################################################################################
;                                                 Exemple
;############################################################################################################################


CreateImage(0,200,60):StartDrawing(ImageOutput(0)):Define i:For i=0 To 200:Circle(100,30,200-i,(i+50)*$010101):Next:StopDrawing()

OpenWindowr(0, 0, 0,512, 200, "Resize gadget", #PB_Window_Background) 
;OpenWindowR(0, 0, 0, 512, 200, "Resize gadget",#PB_Window_ScreenCentered | #PB_Window_SizeGadget) 


  TextGadgetR(1, 10,  10, 200, 50, "Resize the window, the gadgets will be automatically resized",#PB_Text_Center)
  ButtonImageGadgetR(3, 10, 70, 200, 60, ImageID(0),0,0,0)
    EditorGadgetR(2, 10,  140, 200, 20, 0,0,2):SetGadgetText(2,"Editor")
    ButtonGadgetR(4, 10, 170, 490, 20, "Button / toggle", #PB_Button_Toggle,2,1)
   TextGadgetr(5,220,10,190,20,"Text",#PB_Text_Center,2,0):SetGadgetColor(5, #PB_Gadget_BackColor, $00FFFF)
   ContainerGadgetR(6, 220, 30, 190, 100,#PB_Container_Single,2,2):SetGadgetColor(6, #PB_Gadget_BackColor, $cccccc) 
     EditorGadgetR(7, 10,  10, 170, 50, 0,2,2):SetGadgetText(7,"Editor")
     ButtonGadgetR(8, 10, 70, 80, 20, "Button",0,4,1) 
     ButtonGadgetR(9, 100, 70, 80, 20, "Button",0,5,1) 
   CloseGadgetListR() 
   StringGadgetR(10, 220,  140, 190, 20, "String",0,2,1)
   ButtonGadgetR(11, 420,  10, 80, 80, "Bouton",0,1,2)
   CheckBoxGadgetR(12, 420,  90, 200, 20, "CheckBox",0,1,1)
   CheckBoxGadgetR(13, 420,  110, 200, 20, "CheckBox",0,1,1)
   CheckBoxGadgetR(14, 420,  130, 200, 20, "CheckBox",0,1,1)
   CheckBoxGadgetR(15, 420,  150, 200, 20, "CheckBox",0,1,1)

WindowResizeEvent()



Fred
Site Admin
Posts: 1510
Joined: Mon Feb 24, 2014 10:51 am

Re: Automatic resizing of gadgets

Post by Fred »

You could alos use the dialog lib for this :)
tj1010
Posts: 201
Joined: Wed May 27, 2015 1:36 pm
Contact:

Re: Automatic resizing of gadgets

Post by tj1010 »

Dialog lib is good except where you have images because it doesn't maintain aspect ratio for image gadgets or even have a auto parameter for image or gridbox gadget dimensions. It just crops.

I too messed with dynamic UI in this thread. I hit a wall where I had to write a algorithm to maintain aspect ratio without collision and without forcing the window size. I eventually went with adaptive versions like web designs do since there were failure cases with such an algorithm.
Post Reply