Page 1 of 1

RequiredSize for the Width of an Editor Gadget?

Posted: Sat Oct 17, 2020 10:25 pm
by Dr Soong
I am trying to programmatically determine the minimum width to resize an Editor Gadget in order to display the widest text line, so PB won't need to generate a horizontal scrollbar. The #PB_Gadget_RequiredSize option of GadgetWidth on my Editor Gadget always returns 0 - which would indicate to me that RequiredSize is not an available option for Editor Gadgets.

I can manually resize my Editor Gadget until I hit the "sweet width" where the horizontal scrollbar disappears. But I would prefer to initially present the user with a wide-enough Editor Gadget so that the user will never need to scroll right to see off-the-screen text.

Is there a Windows function that can provide the "required" gadget width in pixels? :?

Re: RequiredSize for the Width of an Editor Gadget?

Posted: Sat Oct 17, 2020 11:12 pm
by infratec
Hm...

find the longest line,
create an image,
use startdrawing
use TextWidth(Text$) to get the required pixels.
You have to use the same font :wink:

Re: RequiredSize for the Width of an Editor Gadget?

Posted: Sat Oct 17, 2020 11:20 pm
by Zach
Why not just enable wordwrap ?

Re: RequiredSize for the Width of an Editor Gadget?

Posted: Sun Oct 18, 2020 12:24 am
by Dr Soong
infratec wrote:Hm...

find the longest line,
create an image,
use startdrawing
use TextWidth(Text$) to get the required pixels.
You have to use the same font :wink:
Well... not the cleanest of solutions, but may be a fallback. Thanks!

Re: RequiredSize for the Width of an Editor Gadget?

Posted: Sun Oct 18, 2020 12:26 am
by Dr Soong
Zach wrote:Why not just enable wordwrap ?
My particular project is highlighting line-by-line for website population. For my users, it is cleaner if all of the data is contained on a single line.

Re: RequiredSize for the Width of an Editor Gadget?

Posted: Sun Oct 18, 2020 3:06 am
by RASHAD
Hi
Determine the longest line first
Use the proper font and font size to both the dummy TextGadget() and the EditorGadget()

Code: Select all

Global reqwidth

Procedure GetWIDTH(Text$)
  dummy = TextGadget(#PB_Any,0,0,2000,24,text$)
  SetGadgetFont(dummy,FontID(0))
  reqwidth = GadgetWidth(dummy,#PB_Gadget_RequiredSize)+4
  FreeGadget(dummy)
  ProcedureReturn reqwidth
EndProcedure

LoadFont(0,"Tahoma",20)
Text$ = "yui ioiooooo ioooyyyoy oio o yoyo oyoyoioi oyioi"

If OpenWindow(0, 0, 0, 600, 300, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  GetWIDTH(Text$)
  EditorGadget(1, 10, 10, reqwidth,130)
  SetGadgetFont(1,FontID(0))
  AddGadgetItem(1,0, Text$)
  
Repeat
  Select WaitWindowEvent(10)
        Case #PB_Event_CloseWindow
            Quit = 1
                 
        Case #PB_Event_Gadget
            Select EventGadget()
              Case 1
                                 
            EndSelect
  EndSelect
Until Quit = 1
EndIf

Re: RequiredSize for the Width of an Editor Gadget?

Posted: Sun Oct 18, 2020 10:56 pm
by Dr Soong
Thanks Rashad! I was thinking about going down this direction after the reply from infratec. Thanks for fleshing this out!

Re: RequiredSize for the Width of an Editor Gadget?

Posted: Sun Oct 18, 2020 11:58 pm
by RASHAD
You are welcome
If you still in need for API solution

Code: Select all

LoadFont(0,"Tahoma",12)
Text$ = "Wui ioiooooo ioooyyyoy oio o yoyo oyoyoioi oyioi"

If OpenWindow(0, 0, 0, 600, 300, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  EditorGadget(1, 10, 10, 100,100)
  GetWindowRect_(GadgetID(1),r.RECT)
  GetClientRect_(GadgetID(1),c.RECT)
  border = r\right-r\left-c\right+c\left+2
  
  dc = GetDC_(0)
  sz.Size  
  SelectObject_(dc, FontID(0))
  GetTextExtentPoint32_(dc, text$, Len(text$), sz)
  ReleaseDC_(0, dc)  
  
  reqwidth = sz\cx + border 
  ResizeGadget(1,#PB_Ignore,#PB_Ignore,reqwidth,130)
  SetGadgetFont(1,FontID(0))
  AddGadgetItem(1,0, Text$)
  
Repeat
  Select WaitWindowEvent(10)
        Case #PB_Event_CloseWindow
            Quit = 1
                 
        Case #PB_Event_Gadget
            Select EventGadget()
              Case 1
                                 
            EndSelect
  EndSelect
Until Quit = 1
EndIf