Page 1 of 2

Updated v3.0:TextGadget Vl & Hl Centered Aligned

Posted: Thu Jul 01, 2010 2:37 pm
by RASHAD
It is Not Perfect ,but not bad as a start
I hope it is cross-platform

Code: Select all

Global Text$,FSize,Gwd,Ght

Procedure TextAligen(Gadget,GWd,GHt)
    StartDrawing(WindowOutput(0))
    DrawingFont(FontID(0))
    TW = TextWidth(Text$) 
    TH = TextHeight("W")
    StopDrawing()
    NLines = (TW * FSize / 16 / Gwd) + 1
    NGLines = Gwd / (TH + FSize)
    NCRLF = (NGLines - NLines) / 2
    For i = 1 To NCRLF
       Result$ = Result$ + #CRLF$
    Next
    Result$ = Result$ + Text$
    SetGadgetFont(Gadget,FontID(0))
    SetGadgetText(Gadget,Result$)
EndProcedure


FSize = 18
LoadFont(0,"Arial",FSize)
Text$ = "Test Vertical and Horizontal Centered Text in Multiline StringGadget For MyTrial"
OpenWindow(0,0,0,300,200,"Edit Control VCenter",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
TextGadget(0,10,10,280,180,"",#PB_Text_Center)
TextAligen(0,280,180)
SetGadgetColor(0,#PB_Gadget_FrontColor,$0103FE)
SetGadgetColor(0,#PB_Gadget_BackColor,$D0FFFF)

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow

Re: TextGadget Vl & Hl Centered Aligned

Posted: Thu Jul 01, 2010 2:41 pm
by gnozal
RASHAD wrote:I hope it is cross-platform
Hi RASHAD, #ES_CENTER, #WS_SYSMENU and #WS_CAPTION are Windows constants.

Re: TextGadget Vl & Hl Centered Aligned

Posted: Thu Jul 01, 2010 2:55 pm
by RASHAD
Hi gnozal
Thank you for your reply
The first post updated
Sorry again gnozal but remember I said it is not bad as a start

Have a good day sir

Re: TextGadget Vl & Hl Centered Aligned

Posted: Thu Jul 01, 2010 3:19 pm
by rsts
Pretty nice for us Windows users.

Thanks, Mr Rashad. :)

cheers

Re: TextGadget Vl & Hl Centered Aligned

Posted: Thu Jul 01, 2010 3:26 pm
by RASHAD
@rsts Hi
Ya ,We should not bother ourselves with Linux and Mac hehe
rsts I just updated the first post can you check if it is OK now with, you know who

Have a good day sir

Re: TextGadget Vl & Hl Centered Aligned

Posted: Thu Jul 01, 2010 3:38 pm
by rsts
RASHAD wrote: Ya ,We should not bother ourselves with Linux and Mac hehe
Have a good day sir
:lol: I almost said the same thing, but didn't want to start a flame war while merely making a joke. :)

The code looks just fine for my use.

Good day to you sir.

Re: Updated v2.0:TextGadget Vl & Hl Centered Aligned

Posted: Thu Jul 01, 2010 5:58 pm
by RASHAD
Ver 2.0
More precise
Native commands for sure

Code: Select all


Global Text$,FSize,x,y,Gwd,Ght,FColor,BColor

Procedure TextGadgetEx(Gadget,x,y,GWd,GHt,FColor,BColor)
    StartDrawing(WindowOutput(0))
    DrawingFont(FontID(0))
    TW = TextWidth(Text$) 
    TH = TextHeight("W")
    StopDrawing()
    NLines = (TW * FSize / 16 / Gwd) + 1
    LH = NLines * TH
    mLH = (GHt - LH)/2
    UseGadgetList(0)
    result = ContainerGadget(#PB_Any, x, y,GWd,GHt)
    SetGadgetColor(result,#PB_Gadget_BackColor,BColor)
    TextGadget(Gadget,0,mLH,Gwd,LH,Text$,#PB_Text_Center)
    SetGadgetColor(Gadget,#PB_Gadget_FrontColor,FColor)
    SetGadgetColor(Gadget,#PB_Gadget_BackColor,BColor)    
    SetGadgetFont(Gadget,FontID(0))
    CloseGadgetList()
EndProcedure

FSize = 18
LoadFont(0,"Broadway",FSize)
Text$ = "Test Vertical and Horizontal Centered Text in Multiline TextGadget For MyTrial"
OpenWindow(0,0,0,300,200,"Edit Control VCenter",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
TextGadgetEx(0,10,10,280,180,$0002FF,$D0FFFF)

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow


Re: Updated v3.0:TextGadget Vl & Hl Centered Aligned

Posted: Wed Jul 07, 2010 3:26 pm
by RASHAD
V 3.0
I am always not satisfied with what I am doing
Next is more logic more precise
Hope you like it

Code: Select all

Global Text$,FSize,x,y,GW,GH,FColor,BColor

Procedure TextGadgetEx(Gadget,x,y,GW,GH,FColor,BColor)
    StartDrawing(WindowOutput(0))
      DrawingFont(FontID(0))
      CW = TextWidth("W")
      CH = TextHeight("W")
    StopDrawing()
    CompilerIf #PB_Compiler_Unicode 
        NLines = (StringByteLength(Text$) * CW ) / 2 / GW
    CompilerElse
        NLines = (StringByteLength(Text$) * CW ) / GW
    CompilerEndIf
    LH = (NLines * CH)
    mLH = (GH - LH)/2 + (NLines * 2)
    UseGadgetList(0)
      result = ContainerGadget(#PB_Any, x, y,GW,GH,#PB_Container_Single)
      SetGadgetColor(result,#PB_Gadget_BackColor,BColor)
      TextGadget(Gadget,0,mLH,GW,LH,Text$,#PB_Text_Center)
      SetGadgetColor(Gadget,#PB_Gadget_FrontColor,FColor)
      SetGadgetColor(Gadget,#PB_Gadget_BackColor,BColor)    
      SetGadgetFont(Gadget,FontID(0))
    CloseGadgetList()
EndProcedure


FSize = 18
LoadFont(0,"Broadway",FSize)
Text$ = "Test Vertical and Horizontal Centered Text in Multiline TextGadget For MyTrial"
OpenWindow(0,0,0,300,200,"Edit Control VCenter",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
TextGadgetEx(0,10,10,280,180,$0002FF,$D0FFFF)             ;#Gadget,x,y,Width,Height,FrontColor,BackColor

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow


Re: Updated v3.0:TextGadget Vl & Hl Centered Aligned

Posted: Wed Jul 07, 2010 10:34 pm
by Nico
the text is not vertically centered correctly in this version

Re: Updated v3.0:TextGadget Vl & Hl Centered Aligned

Posted: Wed Jul 07, 2010 11:25 pm
by RASHAD
Hi Nico
It is so difficult to make it good in the global
I mean when we do not know the text and the dimensions of the TextGadget
Now which OS you are using(do not tell me Linux or Mac I know a little about both)?

Re: Updated v3.0:TextGadget Vl & Hl Centered Aligned

Posted: Thu Jul 08, 2010 8:55 am
by Perkin
@Nico
I think I've sorted the vertical alignment
Change the CompilerIf lines to following

Code: Select all

    CompilerIf #PB_Compiler_Unicode
        NLines = (StringByteLength(Text$) * CW ) / 2 / GW -1
    CompilerElse
        NLines = (StringByteLength(Text$) * CW ) / GW -1
    CompilerEndIf
The alignment should now be correct.
Edit:
Simply added '-1' to ends of code lines.

Re: Updated v3.0:TextGadget Vl & Hl Centered Aligned

Posted: Thu Jul 08, 2010 6:21 pm
by Nico
No, still does not work, put a long text and you'll see!

Re: Updated v3.0:TextGadget Vl & Hl Centered Aligned

Posted: Fri Jul 09, 2010 8:53 pm
by RASHAD
Still cross-platform
For test

Code: Select all

Global Text$,FSize,x,y,GW,GH,FColor,BColor,count
Global Dim A$(0)

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
    k = 1
    NLines = 1
    While k <> count
       TW = TW + TextWidth(A$(k))
       If TW < GW
          k = k + 1
       Else 
          TW = 0
          NLines = NLines + 1
       EndIf
       If (TW = (GW - 1) And FSize&1=1) Or (TW = (GW - 2) And FSize&1=0)
          NLines = NLines + 1
       EndIf     
    Wend 
    TH = TextHeight("W") 
    StopDrawing()
    LH = NLines * TH
    mLH = (GH - LH)/2
    UseGadgetList(0)
    result = ContainerGadget(#PB_Any, x, y,GW,GH,#PB_Container_Single)
    SetGadgetColor(result,#PB_Gadget_BackColor,BColor)
    TextGadget(Gadget,0,mLH-2,GW,LH,Text$,#PB_Text_Center)
    SetGadgetColor(Gadget,#PB_Gadget_FrontColor,FColor)
    SetGadgetColor(Gadget,#PB_Gadget_BackColor,BColor)    
    SetGadgetFont(Gadget,FontID(0))
    CloseGadgetList()
EndProcedure


FSize = 18
LoadFont(0,"Broadway",FSize)
Text$ = "Test Vertical and Horizontal Centered Text in Multiline TextGadget For MyTrial"
count = CountString(Text$," ") + 2
ReDim A$(count)
OpenWindow(0,0,0,300,200,"Edit Control VCenter",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
TextGadgetEx(0,10,10,280,180,$0002FF,$D0FFFF)             ;#Gadget,x,y,Width,Height,FrontColor,BackColor

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow

Re: Updated v3.0:TextGadget Vl & Hl Centered Aligned

Posted: Sat Jul 10, 2010 7:38 pm
by Vera
Hi RASHAD,

I've been following your thread with much interest :) and always testing the codes on Linux ('cos from here I surf. else I'm on Win).
I find the latest code works best now. Still one thing never worked - there's no wrap. Today I searched, found a way
Inf0Byt3 wrote:As you probably know the text gadget doesn't wrap the text by itself, like on Windows.
Here's a snippet for enabling it
and implemented it:

Code: Select all

Global Text$,FSize,x,y,GW,GH,FColor,BColor,count
Global Dim A$(0)

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
;       Debug count
;       Debug i
    k = 1
    NLines = 1
    While k <> count
       TW = TW + TextWidth(A$(k))
       If TW < GW
          k = k + 1
       Else
          TW = 0
          NLines = NLines + 1
       EndIf
       If (TW = (GW - 1) And FSize&1=1) Or (TW = (GW - 2) And FSize&1=0)
          NLines = NLines + 1
       EndIf     
     Wend
;     Debug NLines
    TH = TextHeight("W")
    StopDrawing()
    LH = NLines * TH
;    Debug GW
;    Debug LH
    mLH = (GH - LH)/2
    UseGadgetList(0)
    result = ContainerGadget(#PB_Any, x, y,GW,GH,#PB_Container_Single)
    SetGadgetColor(result,#PB_Gadget_BackColor, $006666)
    TextGadget(Gadget,0,mLH-2,GW,LH,Text$,#PB_Text_Center)
    SetGadgetColor(Gadget,#PB_Gadget_BackColor,BColor)   
    SetGadgetColor(Gadget,#PB_Gadget_FrontColor,FColor)
    SetGadgetFont(Gadget,FontID(0))
    
CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_Linux    
    gtk_label_set_line_wrap_(GadgetID(Gadget),1)
    gtk_widget_set_size_request_(GadgetID(Gadget), GW, LH)
    gtk_label_set_justify_(GadgetID(Gadget), #GTK_JUSTIFY_CENTER) 
CompilerEndSelect
    
    CloseGadgetList()
EndProcedure


FSize = 18
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,300,200,"Edit Control VCenter",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
TextGadgetEx(0,10,10,280,180,$0002FF,$00ccff) 

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
It might not be the best or proper solution, but within limits of fontsizes and textamount it works fine. Maybe one of our Linux-pros might have a look over it.
(btw: I changed the colors to verify if and where they are supplied)
Two more hints:
- the font Georgia is available on both plattforms
- starting from fontsize 40 you get broken results

regards ~ Vera

Re: Updated v3.0:TextGadget Vl & Hl Centered Aligned

Posted: Sat Jul 10, 2010 8:38 pm
by RASHAD
@Vera Hi and thank you
Thank you for:
1- Testing
2- Adding more enhancements
3- Adding Linux support

I Just fixed the code and added again yours so test it again please

Code: Select all

;Coded by RASHAD
;Tested ,Modified and added Linux wrap support by Vera


Global Text$,FSize,x,y,GW,GH,FColor,BColor,count
Global Dim A$(0)

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
    While k <> count
       If run = 0
          TW = TW + TextWidth(A$(k))
       Else
          TW = TW + TextWidth(" "+A$(k))
       EndIf
       If TW < GW
          run = 1
          k = k + 1
       Else
          run = 0       
          TW = 0
          NLines = NLines + 1
       EndIf  
    Wend 
    TH = TextHeight("W") 
    StopDrawing()
    LH = NLines * TH
    mLH = (GH - LH)/2
    UseGadgetList(0)
    result = ContainerGadget(#PB_Any, x, y,GW,GH,#PB_Container_Single)
    SetGadgetColor(result,#PB_Gadget_BackColor,$006666)
    TextGadget(Gadget,0,mLH-2,GW,LH,Text$,#PB_Text_Center)
    SetGadgetColor(Gadget,#PB_Gadget_FrontColor,FColor)
    SetGadgetColor(Gadget,#PB_Gadget_BackColor,BColor)    
    SetGadgetFont(Gadget,FontID(0))
    
    CompilerSelect #PB_Compiler_OS
      CompilerCase #PB_OS_Linux    
        gtk_label_set_line_wrap_(GadgetID(Gadget),1)
        gtk_widget_set_size_request_(GadgetID(Gadget), GW, LH)
        gtk_label_set_justify_(GadgetID(Gadget), #GTK_JUSTIFY_CENTER) 
    CompilerEndSelect
    
    CloseGadgetList()
EndProcedure


FSize = 18
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,300,200,"Edit Control VCenter",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
TextGadgetEx(0,10,10,280,180,$0002FF,$D0FFFF)             ;#Gadget,x,y,Width,Height,FrontColor,BackColor

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow