Updated v3.0:TextGadget Vl & Hl Centered Aligned

Share your advanced PureBasic knowledge/code with the community.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Updated v3.0:TextGadget Vl & Hl Centered Aligned

Post 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
Last edited by RASHAD on Wed Jul 07, 2010 3:23 pm, edited 3 times in total.
Egypt my love
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Re: TextGadget Vl & Hl Centered Aligned

Post by gnozal »

RASHAD wrote:I hope it is cross-platform
Hi RASHAD, #ES_CENTER, #WS_SYSMENU and #WS_CAPTION are Windows constants.
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: TextGadget Vl & Hl Centered Aligned

Post 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
Egypt my love
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: TextGadget Vl & Hl Centered Aligned

Post by rsts »

Pretty nice for us Windows users.

Thanks, Mr Rashad. :)

cheers
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: TextGadget Vl & Hl Centered Aligned

Post 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
Egypt my love
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: TextGadget Vl & Hl Centered Aligned

Post 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.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

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

Post 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

Egypt my love
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

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

Post 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

Egypt my love
Nico
Enthusiast
Enthusiast
Posts: 274
Joined: Sun Jan 11, 2004 11:34 am
Location: France

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

Post by Nico »

the text is not vertically centered correctly in this version
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

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

Post 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)?
Egypt my love
Perkin
Enthusiast
Enthusiast
Posts: 504
Joined: Thu Jul 03, 2008 10:13 pm
Location: Kent, UK

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

Post 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.
%101010 = $2A = 42
Nico
Enthusiast
Enthusiast
Posts: 274
Joined: Sun Jan 11, 2004 11:34 am
Location: France

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

Post by Nico »

No, still does not work, put a long text and you'll see!
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

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

Post 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
Egypt my love
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

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

Post 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
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

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

Post 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
Egypt my love
Post Reply