Set gadget height to multiline text (Windows)
Posted: Sat Dec 07, 2024 8:55 am
I am searching for a routine which calculates the needed gadget height for text precisely. I saw that there a some hickups depending if a text or editor gadget is used (#TextGadget=1/0).
So I am using a constant #TextHeightHack=6 for now which seems to work - but I fear this is not correct for all cases. So maybe there's a better solution available?
So I am using a constant #TextHeightHack=6 for now which seems to work - but I fear this is not correct for all cases. So maybe there's a better solution available?
Code: Select all
; Define
#TextGadget=1
Enumeration
#Win
#Editor
#BtnExit
;
#GadgetLineHeight
EndEnumeration
EnableExplicit
Global Font=LoadFont(0,"Microsoft Sans Serif",Random(20)+5,#PB_Font_HighQuality)
Global Font=LoadFont(0,"Segoe UI",20,#PB_Font_HighQuality)
; EndDefine
Procedure.i GetLineHeight(Window,Font)
Protected LineHeight
If StartDrawing(WindowOutput(Window))
DrawingMode(#PB_2DDrawing_Default)
DrawingFont(Font)
LineHeight=TextHeight(".")
StopDrawing()
EndIf
ProcedureReturn(LineHeight)
EndProcedure
Procedure.i GetTextHeigt(Window,Gadget,Font,Text.s)
#TextHeightHack=6
Protected Magic=Bool(GadgetType(Gadget)=#PB_GadgetType_Text)*#TextHeightHack
ResizeGadget(#GadgetLineHeight,#PB_Ignore,#PB_Ignore,GadgetWidth(Gadget)+Magic,#PB_Ignore)
SetGadgetFont(#GadgetLineHeight,Font)
SetGadgetText(#GadgetLineHeight,Text)
ProcedureReturn CountGadgetItems(#GadgetLineHeight) * GetLineHeight(Window,Font) + (Magic!#TextHeightHack)
EndProcedure
Procedure Main()
Protected Text.s
Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
Text+"Nullam molestie commodo magna, id rutrum enim suscipit ac. "
Text+"Nunc tempor faucibus magna, vel volutpat tellus luctus et."
;Text="TEST"
If OpenWindow(#Win, 0, 0, 400, 500, "", #PB_Window_Invisible|#PB_Window_ScreenCentered)
SetWindowColor(#Win, RGB(192,192,192))
EditorGadget(#GadgetLineHeight,200,0,0,1000,#PB_Editor_WordWrap)
HideGadget(#GadgetLineHeight,1)
If #TextGadget
TextGadget(#Editor, 0,0, 200,0,"")
Else
EditorGadget(#Editor,0,0, 200,0, #PB_Editor_WordWrap)
EndIf
ButtonGadget(#BtnExit,300,0,100,26, "Exit")
SetGadgetFont(#Editor,Font)
ResizeGadget(#Editor,#PB_Ignore,#PB_Ignore,#PB_Ignore, GetTextHeigt(#Win,#Editor,Font,Text))
SetGadgetText(#Editor,Text)
HideWindow(#Win,#False)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
End
Case #PB_Event_Gadget
If EventGadget()=#BtnExit
End
EndIf
EndSelect
ForEver
EndIf
EndProcedure
Main()