RequiredSize for the Width of an Editor Gadget?

Just starting out? Need help? Post your questions and find answers here.
Dr Soong
User
User
Posts: 14
Joined: Tue Nov 17, 2015 11:20 pm

RequiredSize for the Width of an Editor Gadget?

Post 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? :?
PB: 6.00 LTS (x86) OS: Windows 10
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: RequiredSize for the Width of an Editor Gadget?

Post 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:
Zach
Addict
Addict
Posts: 1656
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Re: RequiredSize for the Width of an Editor Gadget?

Post by Zach »

Why not just enable wordwrap ?
Image
Dr Soong
User
User
Posts: 14
Joined: Tue Nov 17, 2015 11:20 pm

Re: RequiredSize for the Width of an Editor Gadget?

Post 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!
PB: 6.00 LTS (x86) OS: Windows 10
Dr Soong
User
User
Posts: 14
Joined: Tue Nov 17, 2015 11:20 pm

Re: RequiredSize for the Width of an Editor Gadget?

Post 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.
PB: 6.00 LTS (x86) OS: Windows 10
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: RequiredSize for the Width of an Editor Gadget?

Post 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
Egypt my love
Dr Soong
User
User
Posts: 14
Joined: Tue Nov 17, 2015 11:20 pm

Re: RequiredSize for the Width of an Editor Gadget?

Post by Dr Soong »

Thanks Rashad! I was thinking about going down this direction after the reply from infratec. Thanks for fleshing this out!
PB: 6.00 LTS (x86) OS: Windows 10
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: RequiredSize for the Width of an Editor Gadget?

Post 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

Egypt my love
Post Reply