Hi RASHAD,
while trying to hunt that center-prob I played around a bit and added a horizontal modulation. I like it
(though it's far from being finished) and it removes the left- alignment in most cases.
Have a look - maybe it's good for a clue
Code: Select all
Global Text$,FSize,x,y,GW,GH,FColor,BColor,count
Global Dim A$(0)
fs = 4 ; only relevant when testing the loop
Procedure TextGadgetEx(Gadget,x,y,GW,GH,FColor,BColor)
StartDrawing(WindowOutput(0))
DrawingFont(FontID(0))
For i = 1 To count
A$(i) = StringField(Text$,i, " ")
Next
run = 0
k = 1
NLines = 1
; xG = 35 ; to apply a bit left indention (absolut value: ok for WIN)
xG = (GW/100)*(3+(FSize/2)) ; any percentage of GW is better -> gives better results on Linux
GW2 = GW - 2*xG ; to apply according right indention
; xG = 0 ; just to use for quick toggling
; GW2 = GW ; to the original values
While k <> count
If run = 0
TW = TW + TextWidth(A$(k))
Else
TW = TW + TextWidth(" "+A$(k))
EndIf
If TW < GW2 ; use applied gadget-width
run = 1
k = k + 1
Else
run = 0
TW = 0
NLines = NLines + 1
EndIf
Wend
TH = TextHeight("W")
StopDrawing()
LH = NLines * TH
Debug NLines
Debug xG
Debug GW2
mLH = (GH - LH)/2
UseGadgetList(0)
result = ContainerGadget(#PB_Any, x, y,GW,GH,#PB_Container_Single)
SetGadgetColor(result,#PB_Gadget_BackColor, $666600) ; BColor a different color shows the frame
TextGadget(Gadget,xG,mLH-2,GW2,LH,Text$,#PB_Text_Center) ; set applied gadget-width
SetGadgetColor(Gadget,#PB_Gadget_FrontColor,FColor)
SetGadgetColor(Gadget,#PB_Gadget_BackColor,BColor)
SetGadgetFont(Gadget,FontID(0))
; CompilerIf #PB_Compiler_OS = #PB_OS_Linux
; ; CompilerSelect #PB_Compiler_OS ; CompilerSelect is obsolete if only one exception is needed
; ; CompilerCase #PB_OS_Linux
; gtk_label_set_line_wrap_(GadgetID(Gadget),1)
; gtk_widget_set_size_request_(GadgetID(Gadget), GW2, LH) ; set applied gadget-width
; gtk_label_set_justify_(GadgetID(Gadget), #GTK_JUSTIFY_CENTER)
; ; CompilerEndSelect
; CompilerEndIf
CloseGadgetList()
EndProcedure
; While n <= 3 ; just for fun
; n+1 ; trial to loop procedure with dif. fontsizes
; FSize = fs + 4 ; but doesn't work fully yet ;)
FSize = 18 ; comment out when testing the loop
LoadFont(0,"Georgia",FSize)
Text$ = "Test Vertical and Horizontal Centered Text in Multiline TextGadget For MyTrial and more"
count = CountString(Text$," ") + 2
ReDim A$(count)
OpenWindow(0,0,0,400,300,"Edit Control VCenter",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
TextGadgetEx(0,10,10,380,280,$0002FF,$00ccff) ;#Gadget,x,y,Width,Height,FrontColor,BackColor
; fs = FSize
; Debug fs
; Delay(3000)
; Wend
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
With different font-sizes you do not only have to consider the textamount and needed lines by a given width, but also consider the presentation on account of the available gadgetdimension. I think it somehow has to be calculated twice. First what you already do and then check the positioning. This way you may also retain missfitting oversizes.
This is only loud thinking ~ I hope I don't disturb you
greetings ~ Vera