how to get width & height of gadget text?

Just starting out? Need help? Post your questions and find answers here.
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

how to get width & height of gadget text?

Post by Mythros »

Hi all! I am trying to make my gadgets ( specifically button gadget in this context ) have a width of the text that is on the button + an x/y offset of +10 / +10. How can I get the width / height of ANY gadget's text?

Any help is as always, GREATLY appreciated!

Thank You! <3
User avatar
Demivec
Addict
Addict
Posts: 4280
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: how to get width & height of gadget text?

Post by Demivec »

The trick is to use the flag #PB_Gadget_RequiredSize with GadgetWidth() and GadgetHeight(). Here is an example with a Button Gadget and random text. I leave it as a homework assignment for you to test this with the gadget types you need to see how well this works for them. :)

Code: Select all

EnableExplicit
#MainWindow = 1

Enumeration gadgets
  #MyGadget = 1
  #Switch_Btn
EndEnumeration

Define wordChoice$ = "hello\good-bye\world\lucky\beautiful\mad\intriguing\blue\green\red"
  

OpenWindow(#MainWindow, 0, 0, 600, 300, "Test Window", #PB_Window_SystemMenu)


ButtonGadget(#Switch_Btn, 10, 10, 68, 25, "Press Me")
ButtonGadget(#MyGadget, 80, 100, 50, 20, "Fixed")

Define switched = #False, quit = #False, event, newText$, wordCount
Define wordChoice$ = "hello\good-bye\world\lucky\beautiful\mad\inriguing\blue\green\red"
Repeat
  event = WaitWindowEvent()
  
  Select event
    Case #PB_Event_CloseWindow
      quit = #True
    Case #PB_Event_Gadget
      
      Select EventGadget()
        Case #Switch_Btn
          If switched
            ;set a fixed size, sometimes too big, sometimes too small
            ResizeGadget(#MyGadget, #PB_Ignore, #PB_Ignore, 50, 20)
            newText$ = ""
            wordCount = Random(10, 1)
            While wordCount
              newText$ + StringField(wordChoice$, Random(10, 1), "\") + " "
              wordCount - 1
            Wend
            SetGadgetText(#MyGadget, RTrim(newText$))
          Else
            ;resize to fit minimum size to see text.
            ResizeGadget(#MyGadget, #PB_Ignore, #PB_Ignore,
                         GadgetWidth(#MyGadget, #PB_Gadget_RequiredSize), GadgetHeight(#MyGadget, #PB_Gadget_RequiredSize))
          EndIf
          switched = switched ! 1
      EndSelect

  EndSelect
    
Until quit
Last edited by Demivec on Wed May 27, 2020 10:10 am, edited 1 time in total.
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: how to get width & height of gadget text?

Post by Saki »

Hi Demivec
This is a very nice little test code.

It's the right method,
but with DPI aware it does not work properly.

You have to consider enough padding, but it rarely fits exactly.

Then there is the problem with the extended scaling under Windows,
e.g. 120% up to 125% (Not the OS defalult scaling 125%, this work),
then it fails even without DPI aware,
because the gadgets will then be scaled ever to 100%.

Please make a try

viewtopic.php?f=4&t=75363

Best regards Saki
地球上の平和
User avatar
Demivec
Addict
Addict
Posts: 4280
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: how to get width & height of gadget text?

Post by Demivec »

@Saki: I am not sure of the problem here. I have posted some test code in the thread you linked to. I'm not certain if both threads here are discussing the same thing. I will continue any discussion there on that problem.
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: how to get width & height of gadget text?

Post by Saki »

Hi, Demivec,
Many thanks for taking care of it.
It's very important that this be cleared up.
The bug is obviously real and certified on multiple systems.
This is the reason why some users complain about scaling problems that others can't confirm.
We have already had heated discussions about this.
The problem is that many don't understand what DPI aware does and doesn't do.

Best regards Saki
地球上の平和
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

Re: how to get width & height of gadget text?

Post by Mythros »

@saki : Thank you VERY much! I was going to post on this today but you saved me the trouble! :) For some reason, the width & height of the gadget sometimes does not match the size of the string.
User avatar
Demivec
Addict
Addict
Posts: 4280
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: how to get width & height of gadget text?

Post by Demivec »

Mythros wrote:@saki : Thank you VERY much! I was going to post on this today but you saved me the trouble! :) For some reason, the width & height of the gadget sometimes does not match the size of the string.
Your welcome.
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

Re: how to get width & height of gadget text?

Post by Mythros »

Thank you, @Demivec! My apologies. I was in a rush earlier yesterday. :)
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: how to get width & height of gadget text?

Post by Saki »

Many thanks Demivec and Mythros for your friendly words.

Together we are strong !

Best regards Saki
地球上の平和
Post Reply