Page 1 of 1

GadgetFit(gadget) command

Posted: Mon Sep 29, 2025 3:22 pm
by marcoagpinto
Heya, @Fred,

Could this be implemented into PB?

I based the code on the forum from 2018 or so.

I have an include file for all my apps to share functions.

This procedure makes the gadgets fit into the contents, for example: text gadgets which have too much or too little pixels for the text.

Here is the code:

Code: Select all

  ; Changes the width and height of a gadget automatically to fits its text
  ;
  ; V1.0 - 25/JUL/2018
  ;        27/JUL/2018
  ; V1.1 - 24/OCT/2019
  ;        o Changed variable names to unique
  ;
  Procedure GadgetFit(MARCOAGPINTO_gadget)
  
    MARCOAGPINTO_x=GadgetX(MARCOAGPINTO_gadget)
    MARCOAGPINTO_y=GadgetY(MARCOAGPINTO_gadget)
    MARCOAGPINTO_w=GadgetWidth(MARCOAGPINTO_gadget)
    MARCOAGPINTO_h=GadgetHeight(MARCOAGPINTO_gadget)
    MARCOAGPINTO_w_new=GadgetWidth(MARCOAGPINTO_gadget,#PB_Gadget_RequiredSize)
    MARCOAGPINTO_h_new=GadgetHeight(MARCOAGPINTO_gadget,#PB_Gadget_RequiredSize)
  
    If MARCOAGPINTO_w<>MARCOAGPINTO_w_new Or MARCOAGPINTO_h<>MARCOAGPINTO_h_new
      ResizeGadget(MARCOAGPINTO_gadget,MARCOAGPINTO_x,MARCOAGPINTO_y,MARCOAGPINTO_w_new,MARCOAGPINTO_h_new)      
    EndIf
  
  EndProcedure
This useful command would benefit all users.

Thanks!

Re: GadgetFit(gadget) command

Posted: Mon Sep 29, 2025 4:14 pm
by Little John
This can be done much simpler:

Code: Select all

Procedure GadgetFit (gadget.i)
   Protected.i w_req, h_req
   
   w_req = GadgetWidth (gadget, #PB_Gadget_RequiredSize)
   h_req = GadgetHeight(gadget, #PB_Gadget_RequiredSize)
   
   If w_req <> GadgetWidth(gadget) Or h_req <> GadgetHeight(gadget)
      ResizeGadget(gadget, #PB_Ignore, #PB_Ignore, w_req, h_req)      
   EndIf
EndProcedure

Re: GadgetFit(gadget) command

Posted: Mon Sep 29, 2025 4:17 pm
by marcoagpinto
Little John wrote: Mon Sep 29, 2025 4:14 pm This can be done much simpler:

Code: Select all

Procedure GadgetFit (gadget.i)
   Protected.i w_req, h_req
   
   w_req = GadgetWidth (gadget, #PB_Gadget_RequiredSize)
   h_req = GadgetHeight(gadget, #PB_Gadget_RequiredSize)
   
   If w_req <> GadgetWidth(gadget) Or h_req <> GadgetHeight(gadget)
      ResizeGadget(gadget, #PB_Ignore, #PB_Ignore, w_req, h_req)      
   EndIf
EndProcedure
Heya,

Thank you so much for all the help you and the other developers have been giving me for 14+ years.

There is still a long way to go.