Page 1 of 1

Gadget position/scale by percentage of parent window

Posted: Mon Jan 18, 2010 3:37 pm
by Innesoft
Not sure if this has been asked before, but I'll post anyway..

How about the ability to scale/position a gadget by percentage of the parent window/element, or auto-aligned to one side, so that it auto-updates when you (the end-user) resize the window. Would be useful to be able to do this in the VD too.

I know this is something that can be hand-coded as a procedure, but making it part of the language would be even better.

Re: Gadget position/scale by percentage of parent window

Posted: Mon Jan 18, 2010 3:59 pm
by Kaeru Gaman
well I think ...
this would require a built-in callback, what definitely is not the way PureBasic is designed.
PureBasic is a procedural language and can handle events, but is not event-driven like OOP languages.

so, maybe that sounds like a nice feature, but it is completely off the philosophy of this language.

Re: Gadget position/scale by percentage of parent window

Posted: Mon Jan 18, 2010 4:03 pm
by Fred
You don't necessarily need callback for that. A layout library would be enough to handle that.

Re: Gadget position/scale by percentage of parent window

Posted: Mon Jan 18, 2010 4:08 pm
by Kaeru Gaman
oookaaaay.... :o

that sounds as if you're already on it...

Re: Gadget position/scale by percentage of parent window

Posted: Mon Jan 18, 2010 4:11 pm
by Innesoft
Kaeru Gaman wrote:..so, maybe that sounds like a nice feature, but it is completely off the philosophy of this language.
I understand where you're coming from, but I'm not sure I agree that it goes against what the language should be. It's just a way of handling a gadget property dynamically, however that would be handled behind the scenes. I mean, there's already a WindowEvent() for #PB_Event_SizeWindow. Not much of a deviation from the language, other than the way gadgets are handled, and this could be done with a flag like #PB_Position_Percentage (or something) and a command to update gadgets with that flag on a #PB_Event_SizeWindow event.

Anyway, just a thought.

Re: Gadget position/scale by percentage of parent window

Posted: Tue Jan 19, 2010 2:47 pm
by bembulak

Re: Gadget position/scale by percentage of parent window

Posted: Wed Jan 20, 2010 4:13 am
by Innesoft
I've knocked up a quick solution, which can probably be improved, but it's literally 5 minutes work thrown together. At least until Fred is kind enough to integrate a better solution into the language. :wink:

(Works with all gadgets)

Step 1. The Procedures...

Code: Select all

Structure dyn_element
  ID.i
  PAR.i
  lockwh.i
  lockx.i
  locky.i
  min_x.i
  min_y.i
  x_perc.f
  y_perc.f
  min_w.i
  min_h.i
  w_perc.f
  h_perc.f
EndStructure

Dim dyn.dyn_element(99)
Global dyn_cnt.i
Global dyn_statusbar_adjust.i = 24  ;adjust inner-height for status bar (set to zero to disable)
Procedure DynamicElement(parent,ID,lockx.i,locky.i,lockwh.i)
  Shared dyn.dyn_element()
  dyn_cnt.i=dyn_cnt.i+1
  dyn(dyn_cnt.i)\ID.i=ID
  dyn(dyn_cnt.i)\PAR.i=parent
  dyn(dyn_cnt.i)\lockwh.i=lockwh.i
  dyn(dyn_cnt.i)\lockx.i=lockx.i
  dyn(dyn_cnt.i)\locky.i=locky.i
  If dyn(dyn_cnt.i)\lockwh.i = 0
    dyn(dyn_cnt.i)\min_w.i=GadgetWidth(ID)
    dyn(dyn_cnt.i)\min_h.i=GadgetHeight(ID)
    dyn(dyn_cnt.i)\w_perc.f=(dyn(dyn_cnt.i)\min_w.i/(WindowWidth(parent)-dyn_statusbar_adjust.i))*100
    dyn(dyn_cnt.i)\h_perc.f=(dyn(dyn_cnt.i)\min_h.i/(WindowHeight(parent)-dyn_statusbar_adjust.i))*100
  EndIf
  dyn(dyn_cnt.i)\min_x.i=GadgetX(ID)
  dyn(dyn_cnt.i)\min_y.i=GadgetY(ID)
  dyn(dyn_cnt.i)\x_perc.f=(dyn(dyn_cnt.i)\min_x.i/(WindowWidth(parent)-dyn_statusbar_adjust.i))*100
  dyn(dyn_cnt.i)\y_perc.f=(dyn(dyn_cnt.i)\min_y.i/(WindowHeight(parent)-dyn_statusbar_adjust.i))*100
EndProcedure

Procedure UpdateDynamicElements()
  Shared dyn.dyn_element()
  For a.i=1 To dyn_cnt.i
    pw.f = WindowWidth(dyn(a.i)\PAR.i)-dyn_statusbar_adjust.i
    ph.f = WindowHeight(dyn(a.i)\PAR.i)-dyn_statusbar_adjust.i
    If dyn(dyn_cnt.i)\lockwh.i = 0
      gw.f = ((pw.f/100) * dyn(a.i)\w_perc.f)
      gh.f = ((ph.f/100) * dyn(a.i)\h_perc.f)
      If gw.f < dyn(a.i)\min_w.i
        gw.f = dyn(a.i)\min_w.i
      EndIf
      If gh.f < dyn(a.i)\min_h.i
        gh.f = dyn(a.i)\min_h.i
      EndIf
    Else
      gw.f = GadgetWidth(dyn(a.i)\ID.i)
      gh.f = GadgetHeight(dyn(a.i)\ID.i)
    EndIf
    If dyn(dyn_cnt.i)\lockx.i = 0
      gx.f = ((pw.f/100) * dyn(a.i)\x_perc.f)
      If gx.f < dyn(a.i)\min_x.i
        gx.f = dyn(a.i)\min_x.i
      EndIf
    Else
      gx.f = dyn(a.i)\min_x.i
    EndIf
    If dyn(dyn_cnt.i)\locky.i = 0
      gy.f = ((ph.f/100) * dyn(a.i)\y_perc.f)
      If gy.f < dyn(a.i)\min_y.i
        gy.f = dyn(a.i)\min_y.i
      EndIf
    Else
      gy.f = dyn(a.i)\min_y.i
    EndIf
    ResizeGadget(dyn(a.i)\ID.i,gx.f,gy.f,gw.f,gh.f)
  Next
EndProcedure
Step 2. Make Your Gadgets Dynamic...
The initial x,y,w,h of the gadget is automatically the minimum it will be allowed when you resize. This allows you to design your layout at the minimum with scope for maximizing the window.

Code: Select all

      ;Usage: DynamicElement(parent_window,gadgetID,lockx,locky,lockscale)
      DynamicElement(#Window_0,#Panel_0,1,0,0) ;dynamic but lock x position
      DynamicElement(#Window_0,#Panel_1,0,1,0) ;dynamic but lock y position
      DynamicElement(#Window_0,#Panel_2,0,0,1) ;dynamic but lock scale
Step 3. Put This in Your Main Loop

Code: Select all

  Event = WaitWindowEvent() ; you probably already have this line in your loop
  If Event = #PB_Event_SizeWindow Or Event = #PB_Event_MaximizeWindow Or Event = #PB_Event_RestoreWindow
    UpdateDynamicElements()
  EndIf