SetTextAlignment (All OS)

Share your advanced PureBasic knowledge/code with the community.
User avatar
mk-soft
Always Here
Always Here
Posts: 6201
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

SetTextAlignment (All OS)

Post by mk-soft »

I found it again with me (since v5.73)


Update

Code: Select all

;-TOP mk-soft and Unknown, v1.01.1, 12.06.2025
; https://www.purebasic.fr/english/viewtopic.php?t=87074

EnableExplicit

Enumeration Windows
  #Main
EndEnumeration

Enumeration Gadgets
  #MainString
  #MainText
  #MainButton_Left
  #MainButton_Center
  #MainButton_Right
EndEnumeration

Enumeration Status
  #MainStatusBar
EndEnumeration

Enumeration
  #TextLeft
  #TextCenter
  #TextRight
EndEnumeration

; ----

CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_Windows
    Procedure SetTextAlignment(Gadget, Type)
      Protected handle = GadgetID(Gadget)
      Protected style = GetWindowLongPtr_(handle, #GWL_STYLE)
      Select Type
        Case #TextLeft
          style = style & ~#ES_CENTER & ~#ES_RIGHT
        Case #TextCenter
          style = style & ~#ES_RIGHT | #ES_CENTER
        Case #TextRight
          style = style & ~#ES_CENTER | #ES_RIGHT
      EndSelect
      SetWindowLongPtr_(handle, #GWL_STYLE, style)
      InvalidateRect_(handle, 0, #True)
    EndProcedure
    
  CompilerCase #PB_OS_Linux
    ImportC ""
      gtk_entry_set_alignment(*entry, xalign.f)
      gtk_label_set_xalign(*label, xalign.f)
      gtk_misc_set_alignment(*label, xalign.f, yalign.f)
    EndImport
    
    Procedure SetTextAlignment(Gadget, Type)
      Protected widget = GadgetID(Gadget)
      Select GadgetType(Gadget)
        Case #PB_GadgetType_String
          Select Type
            Case #TextLeft
              gtk_entry_set_alignment(widget, 0.0)
            Case #TextCenter
              gtk_entry_set_alignment(widget, 0.5)
            Case #TextRight
              gtk_entry_set_alignment(widget, 1.0)
          EndSelect
        CompilerIf Subsystem("gtk2")
          Case #PB_GadgetType_Text
            Select Type
              Case #TextLeft
                gtk_misc_set_alignment(widget, 0.0, 0.0)
                gtk_label_set_justify_(widget, #GTK_JUSTIFY_LEFT)
              Case #TextCenter
                gtk_misc_set_alignment(widget, 0.5, 0.0)
                gtk_label_set_justify_(widget, #GTK_JUSTIFY_CENTER)
              Case #TextRight
                gtk_misc_set_alignment(widget, 1.0, 0.0)
                gtk_label_set_justify_(widget, #GTK_JUSTIFY_RIGHT)
            EndSelect
        CompilerElse
          Case #PB_GadgetType_Text
            Select Type
              Case #TextLeft
                gtk_label_set_xalign(widget, 0.0)
                gtk_label_set_justify_(widget, #GTK_JUSTIFY_LEFT)
              Case #TextCenter
                gtk_label_set_xalign(widget, 0.5)
                gtk_label_set_justify_(widget, #GTK_JUSTIFY_CENTER)
              Case #TextRight
                gtk_label_set_xalign(widget, 1.0)
                gtk_label_set_justify_(widget, #GTK_JUSTIFY_RIGHT)
            EndSelect
        CompilerEndIf    
      EndSelect
      
    EndProcedure
    
  CompilerCase #PB_OS_MacOS
    Procedure SetTextAlignment(Gadget, Type)
      Select Type
        Case #TextLeft
          CocoaMessage(0, GadgetID(Gadget), "setAlignment:", #NSLeftTextAlignment)
        Case #TextCenter
          CocoaMessage(0, GadgetID(Gadget), "setAlignment:", #NSCenterTextAlignment)
        Case #TextRight
          CocoaMessage(0, GadgetID(Gadget), "setAlignment:", #NSRightTextAlignment)
      EndSelect
    EndProcedure
    
CompilerEndSelect

; ----

Procedure Main()
  Protected Text.s
  #MainStyle = #PB_Window_SystemMenu | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
  
  Text = "PureBasic is a native 32-bit and 64-bit programming" + #LF$ + "language based on established BASIC rules." + #LF$ +
         "The key features of PureBasic are portability" + #LF$ + "(Windows, Linux And MacOS X are currently supported)," + #LF$ +
         "the production of very fast And highly optimized executables" + #LF$ + "And, of course, the very simple BASIC syntax."
  
  If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 420, 240, "Window" , #MainStyle)
    CreateStatusBar(#MainStatusBar, WindowID(#Main))
    AddStatusBarField(#PB_Ignore)
    
    StringGadget(#MainString, 10, 10, 400, 25, "StringGadget")
    TextGadget(#MainText, 10, 45, 400, 100, Text);, #PB_Text_Center)
    ButtonGadget(#MainButton_Left, 10, 180, 100, 25, "Left")
    ButtonGadget(#MainButton_Center, 120, 180, 100, 25, "Center")
    ButtonGadget(#MainButton_Right, 230, 180, 100, 25, "Right")
    
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Break
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #MainButton_Left
              SetTextAlignment(#MainString, #TextLeft)
              SetTextAlignment(#MainText, #TextLeft)
            Case #MainButton_Center
              SetTextAlignment(#MainString, #TextCenter)
              SetTextAlignment(#MainText, #TextCenter)
            Case #MainButton_Right
              SetTextAlignment(#MainString, #TextRight)
              SetTextAlignment(#MainText, #TextRight)
          EndSelect
          
      EndSelect
    ForEver
    
  EndIf
  
EndProcedure : Main()
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
idle
Always Here
Always Here
Posts: 5834
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: SetTextAlignment (All OS)

Post by idle »

Thanks that's good to have.
User avatar
kenmo
Addict
Addict
Posts: 2032
Joined: Tue Dec 23, 2003 3:54 am

Re: SetTextAlignment (All OS)

Post by kenmo »

Thanks for this!!!
User avatar
Mijikai
Addict
Addict
Posts: 1517
Joined: Sun Sep 11, 2016 2:17 pm

Re: SetTextAlignment (All OS)

Post by Mijikai »

Thanks nice to have :)
SMaag
Enthusiast
Enthusiast
Posts: 301
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: SetTextAlignment (All OS)

Post by SMaag »

Greate! Thanks for sharing! Another missing command in PB!
I will integrated it next time in my PB-Command extention Module!

https://github.com/Maagic7/PureBasicFra ... dule_PB.pb
Post Reply