It is currently Sun Jun 16, 2019 3:49 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Verticaly center text in string gadget.
PostPosted: Mon Jun 10, 2019 9:42 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Oct 06, 2006 3:57 pm
Posts: 519
Location: England
Hi,

Is it possible?

Is there an option like #SS_CENTERIMAGE for textgadget?

Any help appreciated.

Thanks


Top
 Profile  
Reply with quote  
 Post subject: Re: Verticaly center text in string gadget.
PostPosted: Mon Jun 10, 2019 9:58 am 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 1743
Location: Germany
The operator system does not support them.
Possibly create it yourself with the CanvasGadget

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / Event-Designer
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: Verticaly center text in string gadget.
PostPosted: Mon Jun 10, 2019 9:58 am 
Offline
Moderator
Moderator
User avatar

Joined: Thu Dec 31, 2009 11:05 pm
Posts: 956
Location: Germany::Berlin()
viewtopic.php?p=285352#p285352

_________________
ImageImageImageImageImage


Top
 Profile  
Reply with quote  
 Post subject: Re: Verticaly center text in string gadget.
PostPosted: Mon Jun 10, 2019 10:18 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3317
Simple & cross platform
You can play around it
Code:
LoadFont(0,"Tahoma",14)

OpenWindow(0, 0, 0, 600, 400, "StringGadget ", #PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_SizeGadget)
SetWindowColor(0,#White)
StringGadget(1, 10,10,580,40,"ABCDEJQ")
SetGadgetFont(1,FontID(0))
h = GadgetHeight(1,#PB_Gadget_RequiredSize)
ResizeGadget(1,10,10,#PB_Ignore,h)

Repeat

Until WaitWindowEvent() = #PB_Event_CloseWindow

_________________
Egypt my love


Top
 Profile  
Reply with quote  
 Post subject: Re: Verticaly center text in string gadget.
PostPosted: Mon Jun 10, 2019 10:30 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Oct 06, 2006 3:57 pm
Posts: 519
Location: England
Thanks for the quick reponses - gotta love this forum.

Rashad's code from this thread works fine for me.

FYI - Rashad's code from the other thread, that RSBasic pointed, to works in ordinary mode but fails in DPI aware mode.

I really am not understanding the DPI awareness commands - I thought I had all this working and then tried it on a windows client @ 150% and nearly melted my eyes :mrgreen:


Top
 Profile  
Reply with quote  
 Post subject: Re: Verticaly center text in string gadget.
PostPosted: Mon Jun 10, 2019 9:20 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Apr 21, 2005 2:38 pm
Posts: 1621
Location: Germany
RASHAD wrote:
Simple & cross platform

Simple yes but not cross-platform. It's working nice on Windows and Linux but not on MacOS:

Image

Another drawback is that you can't freely choose the vertical height of the TextGadget but having to use the fixed #PB_Gadget_RequiredSize.

My example is able to toggle the vertical text alignment and uses the respective API functions for the different operating systems. Unfortunately on Windows it seems that SS_CENTERIMAGE can only be used when creating the TextGadget. Using
Code:
SetWindowLongPtr_(GadgetID(0), #GWL_STYLE, GetWindowLongPtr_(GadgetID(0), #GWL_STYLE) ! #SS_CENTERIMAGE)
UpdateWindow_GadgetID(0)
to dynamically change the vertical text alignment doesn't work. So on Windows it's necessary to always newly create the TextGadget with or without SS_CENTERIMAGE. On Linux and MacOS the dynamic changing of vertical alignment is no problem.

I have tested my example successfully on these operating systems:
- Linux Mint 19.1 x64 'Tessa' with Cinnamon and GTK3
- MacOS 10.6.8 'Snow Leopard'
- MacOS 10.14.5 'Mojave'
- Windows 7 x64 SP1.
Code:
EnableExplicit

Define CenterVertically.I

CompilerIf #PB_Compiler_OS = #PB_OS_Linux
  ImportC ""
    gtk_label_set_yalign(*Label.GtkLabel, xalign.F)
  EndImport
CompilerEndIf

OpenWindow(0, 270, 100, 310, 100, "Center text vertically")
TextGadget(0, 10, 10, 290, 40, "The quick brown fox jumps over the lazy dog.",
  #PB_Text_Border | #PB_Text_Center)
ButtonGadget(1, 65, 60, 180, 25, "Toggle vertical centering")

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 1
        CenterVertically ! 1

        CompilerSelect #PB_Compiler_OS
          CompilerCase #PB_OS_Linux
            gtk_label_set_yalign(GadgetID(0), CenterVertically * 0.5)
          CompilerCase #PB_OS_MacOS
            CocoaMessage(0, CocoaMessage(0, GadgetID(0), "cell"),
              "_setVerticallyCentered:", CenterVertically)
            CocoaMessage(0, GadgetID(0), "setNeedsDisplay:", #True)
          CompilerCase #PB_OS_Windows
            If CenterVertically
              TextGadget(0, 10, 10, 290, 40,
                "The quick brown fox jumps over the lazy dog.",
                #PB_Text_Border | #PB_Text_Center | #SS_CENTERIMAGE)
            Else
              TextGadget(0, 10, 10, 290, 40,
                "The quick brown fox jumps over the lazy dog.",
                #PB_Text_Border | #PB_Text_Center)
            EndIf

         CompilerEndSelect
      EndIf
  EndSelect
ForEver


Edit: I have removed this debug line twice
Code:
           Debug "GWL_STYLE = $" + Hex(GetWindowLongPtr_(GadgetID(0), #GWL_STYLE))
because it's not cross-platform since using Windows API code.


Last edited by Shardik on Mon Jun 10, 2019 10:40 pm, edited 2 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Verticaly center text in string gadget.
PostPosted: Mon Jun 10, 2019 10:00 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3317
Hi Shardik
StringGadget() is the requested not the TextGadget()
There is a big difference you know
If is GadgetHeight(gadget,#PB_Gadget_RequiredSize) does not work with Mac so Fred should be informed

_________________
Egypt my love


Top
 Profile  
Reply with quote  
 Post subject: Re: Verticaly center text in string gadget.
PostPosted: Mon Jun 10, 2019 10:01 pm 
Online
Enthusiast
Enthusiast

Joined: Sat Apr 26, 2003 2:49 pm
Posts: 598
On Windows you have to use InvalidateRect_() to update the control.
This works:
Code:

EnableExplicit

Define CenterVertically.I

CompilerIf #PB_Compiler_OS = #PB_OS_Linux
  ImportC ""
    gtk_label_set_yalign(*Label.GtkLabel, xalign.F)
  EndImport
CompilerEndIf

OpenWindow(0, 270, 100, 310, 100, "Center text vertically")
TextGadget(0, 10, 10, 290, 40, "The quick brown fox jumps over the lazy dog.",
  #PB_Text_Border | #PB_Text_Center)
ButtonGadget(1, 65, 60, 180, 25, "Toggle vertical centering")
Debug "GWL_STYLE = $" + Hex(GetWindowLongPtr_(GadgetID(0), #GWL_STYLE))

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 1
        CenterVertically ! 1

        CompilerSelect #PB_Compiler_OS
          CompilerCase #PB_OS_Linux
            gtk_label_set_yalign(GadgetID(0), CenterVertically * 0.5)
          CompilerCase #PB_OS_MacOS
            CocoaMessage(0, CocoaMessage(0, GadgetID(0), "cell"),
              "_setVerticallyCentered:", CenterVertically)
            CocoaMessage(0, GadgetID(0), "setNeedsDisplay:", #True)
           
          CompilerCase #PB_OS_Windows
            If CenterVertically
               SetWindowLongPtr_(GadgetID(0), #GWL_STYLE, GetWindowLongPtr_(GadgetID(0), #GWL_STYLE) | #SS_CENTERIMAGE)
;   
            Else
                    SetWindowLongPtr_(GadgetID(0), #GWL_STYLE, GetWindowLongPtr_(GadgetID(0), #GWL_STYLE) & ~#SS_CENTERIMAGE)

            EndIf
            InvalidateRect_(GadgetID(0), #Null, #True)

            Debug "GWL_STYLE = $" + Hex(GetWindowLongPtr_(GadgetID(0), #GWL_STYLE))
        CompilerEndSelect
      EndIf
  EndSelect
ForEver


Top
 Profile  
Reply with quote  
 Post subject: Re: Verticaly center text in string gadget.
PostPosted: Mon Jun 10, 2019 10:34 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Apr 21, 2005 2:38 pm
Posts: 1621
Location: Germany
RASHAD wrote:
StringGadget() is the requested not the TextGadget()

Sorry, you are right, RASHAD. I simply overread the "like" in
captain_skank wrote:
Is there an option like #SS_CENTERIMAGE for textgadget?

But I tested your code with StringGadget and took the snapshot for MacOS...

Tomorrow I will try to find a cross-platform solution to vertically align text in a StringGadget...

Justin wrote:
On Windows you have to use InvalidateRect_() to update the control.

Thank you for finding out how to align the text vertically in the TextGadget dynamically even on Windows, Justin! That's much more elegant than always recreating the TextGadget. I had exactly tested the same Windows code as you, but only tested with UpdateWindow_() instead of InvalidateRect_().


Top
 Profile  
Reply with quote  
 Post subject: Re: Verticaly center text in string gadget.
PostPosted: Tue Jun 11, 2019 5:43 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3317
Hi Shardik
Can you check ?
https://www.purebasic.fr/english/viewtopic.php?f=12&t=72996

_________________
Egypt my love


Top
 Profile  
Reply with quote  
 Post subject: Re: Verticaly center text in string gadget.
PostPosted: Tue Jun 11, 2019 9:32 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Apr 21, 2005 2:38 pm
Posts: 1621
Location: Germany
RASHAD wrote:
StringGadget() is the requested not the TextGadget()
There is a big difference you know

Yes, after realizing to have posted an example for the wrong Gadget, I found the following Windows examples for the StringGadget in my example collection (one posted by you):
- Sparkie (2005)
- TerryHough (2006)
- Fluid Byte (2007)
- IdeasVacuum (2007)
- RASHAD (2008)
The examples from FluidByte, TerryHough and you seem to be derived from Sparkie's because they use the same variable name eRect.RECT... :wink:

RASHAD wrote:
Hi Shardik
Can you check ?
viewtopic.php?f=12&t=72996

I have posted directly in your linked thread.

Shardik wrote:
Tomorrow I will try to find a cross-platform solution to vertically align text in a StringGadget...

I had to find out that on Linux PureBasic utilizes the GtkEntry which is only for single line text input and which doesn't allow vertical alignment at all. So on Linux the text in a StringGadget is always vertically centered by default.

Nevertheless, this is my cross-platform solution for toggling the vertical position of text in the StringGadget (on Linux only an information is displayed) :
Code:
EnableExplicit

Define CenterVertically.I

Procedure AlignTextVertically(StringGadgetID, CenterVertically.I)
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux
      MessageRequester("Info",
        "On Linux the text in a StringGadget (GtkEntry) is always " +
        "vertically centered by default and this cannot be changed!")
    CompilerCase #PB_OS_MacOS
      CocoaMessage(0, CocoaMessage(0, GadgetID(StringGadgetID), "cell"),
        "_setVerticallyCentered:", CenterVertically)
      CocoaMessage(0, GadgetID(StringGadgetID), "setNeedsDisplay:", #True)
    CompilerCase #PB_OS_Windows
      Protected ClientRect.RECT
      Protected DCHandle.I
      Protected Text.S = GetGadgetText(StringGadgetID)
      Protected TextXY.SIZE
 
      DCHandle = GetDC_(GadgetID(StringGadgetID))
      SelectObject_(DCHandle, GetGadgetFont(StringGadgetID))
      GetTextExtentPoint32_(DCHandle, Text, Len(Text), @TextXY)
      ReleaseDC_(GadgetID(StringGadgetID), DCHandle)
      GetClientRect_(GadgetID(StringGadgetID), ClientRect)

      If CenterVertically
        ClientRect\top = (ClientRect\bottom - TextXY\cy) / 2 - 1
      EndIf

      SendMessage_(GadgetID(StringGadgetID), #EM_SETRECT, 0, ClientRect)
      InvalidateRect_(GadgetID(0), 0, #True)
  CompilerEndSelect
EndProcedure

OpenWindow(0, 270, 100, 330, 100, "Center text vertically")

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  StringGadget(0, 10, 10, 310, 40,
    "The quick brown fox jumps over the lazy dog.", #ES_MULTILINE)
CompilerElse
  StringGadget(0, 10, 10, 310, 40,
    "The quick brown fox jumps over the lazy dog.")
CompilerEndIf

ButtonGadget(1, 70, 60, 180, 25, "Toggle vertical centering")

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 1
        CenterVertically ! 1
        AlignTextVertically(0, CenterVertically)
      EndIf
  EndSelect
ForEver

Edit 1: In Windows the StringGadget is now created with #ES_MULTILINE.
Edit 2: I have added the following line in the Windows-specific code to correctly handle different font sizes:
Code:
SelectObject_(DCHandle, GetGadgetFont(StringGadgetID))


Last edited by Shardik on Wed Jun 12, 2019 12:56 pm, edited 4 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Verticaly center text in string gadget.
PostPosted: Tue Jun 11, 2019 10:09 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3317
1- You forgot that srod snippet based on Sparkie's too
2- Your link for my snippet is wrong it links to this thread :)
3- Your last code does not work with Windows

_________________
Egypt my love


Top
 Profile  
Reply with quote  
 Post subject: Re: Verticaly center text in string gadget.
PostPosted: Wed Jun 12, 2019 7:23 am 
Offline
Addict
Addict
User avatar

Joined: Thu Apr 21, 2005 2:38 pm
Posts: 1621
Location: Germany
RASHAD wrote:
1- You forgot that srod snippet based on Sparkie's too
I am sure that I have still missed some other examples too... :wink:

RASHAD wrote:
2- Your link for my snippet is wrong it links to this thread :)
Sorry, but the link to your snippet correctly links to your example in the thread "Set Center Align in StringGadget : Vertical 'n' Horizontal" from 2018. It doesn't link to this thread named "Verticaly center text in string gadget." started by captain_skank.

RASHAD wrote:
3- Your last code does not work with Windows
I should never post cross-platform examples after having made short changes when testing the code on other operating systems and being too tired... :wink:
I had forgotten to remark that on Windows the StringGadget has to be set into multiline mode (#ES_MULTILINE) in order to center the text vertically. In my Windows specific code I had removed the flag #ES_MULTILINE from the StringGadget() definition line (because it's not cross-platform) and moved it to the Windows-specific code. But in Windows it seems that you can't change a StringGadget's style after its declaration to be multilined:
Code:
SetWindowLongPtr_(GadgetID(0), #GWL_STYLE, GetWindowLongPtr_(GadgetID(0), #GWL_STYLE) ! #ES_MULTILINE)
Even a following InvalidateRect_() and UpdateWindow_() doesn't help...

Thank you for testing and reporting! I have modified my example above.


Top
 Profile  
Reply with quote  
 Post subject: Re: Verticaly center text in string gadget.
PostPosted: Wed Jun 12, 2019 9:40 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Oct 06, 2006 3:57 pm
Posts: 519
Location: England
This is making my head hurt.

Testing with the code Shardik provided the results look fine with the default font, but off center with any other font

Code:
    EnableExplicit

    Define CenterVertically.I
   
    ;Global f_12.i = LoadFont(#PB_Any,"Calibri Light", 12, #PB_Font_HighQuality)
    Global f_12.i = LoadFont(#PB_Any,"Arial", 12, #PB_Font_HighQuality)
   
    Procedure AlignTextVertically(StringGadgetID, CenterVertically.I)

      Protected ClientRect.RECT
      Protected DCHandle.I
      Protected Text.S = GetGadgetText(StringGadgetID)
      Protected TextXY.SIZE
     
      DCHandle = GetDC_(GadgetID(StringGadgetID))
      GetTextExtentPoint32_(DCHandle, Text, Len(Text), @TextXY)
      ReleaseDC_(GadgetID(StringGadgetID), DCHandle)
      GetClientRect_(GadgetID(StringGadgetID), ClientRect)
     
      If CenterVertically
        ClientRect\top = (ClientRect\bottom - TextXY\cy) / 2
      EndIf
     
      SendMessage_(GadgetID(StringGadgetID), #EM_SETRECT, 0, ClientRect)
      InvalidateRect_(GadgetID(0), 0, #True)

    EndProcedure

    OpenWindow(0, 270, 100, 330, 200, "Center text vertically")
   
    StringGadget(0, 10, 10, 310, 40, "The quick brown fox jumps over the lazy dog.", #ES_MULTILINE)
   
    StringGadget(1, 10, 60, 310, 40, "The quick brown fox jumps over the lazy dog.", #ES_MULTILINE)
      SetGadgetFont(1, FontID(f_12))
   
    ButtonGadget(2, 70, 110, 180, 25, "Toggle vertical centering")

    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Break
        Case #PB_Event_Gadget
          If EventGadget() = 2
            CenterVertically ! 1
            AlignTextVertically(0, CenterVertically)
            AlignTextVertically(1, CenterVertically)
          EndIf
      EndSelect
    ForEver


I got rid of the non windows code for my purposes.


Top
 Profile  
Reply with quote  
 Post subject: Re: Verticaly center text in string gadget.
PostPosted: Wed Jun 12, 2019 12:31 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Apr 21, 2005 2:38 pm
Posts: 1621
Location: Germany
Sorry, I didn't take the font size into account. I had to add
Code:
      SelectObject_(DCHandle, GetGadgetFont(StringGadgetID))
in my example above. This is your modified Windows example:
Code:
    EnableExplicit

    Define CenterVertically.I
   
    ;Global f_12.i = LoadFont(#PB_Any,"Calibri Light", 12, #PB_Font_HighQuality)
    Global f_12.i = LoadFont(#PB_Any,"Arial", 12, #PB_Font_HighQuality)
   
    Procedure AlignTextVertically(StringGadgetID, CenterVertically.I)

      Protected ClientRect.RECT
      Protected DCHandle.I
      Protected Text.S = GetGadgetText(StringGadgetID)
      Protected TextXY.SIZE
     
      DCHandle = GetDC_(GadgetID(StringGadgetID))
      SelectObject_(DCHandle, GetGadgetFont(StringGadgetID))
      GetTextExtentPoint32_(DCHandle, Text, Len(Text), @TextXY)
      ReleaseDC_(GadgetID(StringGadgetID), DCHandle)
      GetClientRect_(GadgetID(StringGadgetID), ClientRect)

      If CenterVertically
        ClientRect\top = (ClientRect\bottom - TextXY\cy) / 2 - 1
      EndIf
     
      SendMessage_(GadgetID(StringGadgetID), #EM_SETRECT, 0, ClientRect)
      InvalidateRect_(GadgetID(StringGadgetID), 0, #True)

    EndProcedure

    OpenWindow(0, 270, 100, 330, 200, "Center text vertically")
   
    StringGadget(0, 10, 10, 310, 40, "The quick brown fox jumps over the lazy dog.", #ES_MULTILINE)
   
    StringGadget(1, 10, 60, 310, 40, "The quick brown fox jumps over the lazy dog.", #ES_MULTILINE)
      SetGadgetFont(1, FontID(f_12))
   
    ButtonGadget(2, 70, 110, 180, 25, "Toggle vertical centering")

    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Break
        Case #PB_Event_Gadget
          If EventGadget() = 2
            CenterVertically ! 1
            AlignTextVertically(0, CenterVertically)
            AlignTextVertically(1, CenterVertically)
          EndIf
      EndSelect
    ForEver


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 13 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye