Page 1 of 1

SetTextAlignment (All OS)

Posted: Thu Jun 12, 2025 9:27 pm
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()

Re: SetTextAlignment (All OS)

Posted: Sun Jun 15, 2025 9:47 am
by idle
Thanks that's good to have.

Re: SetTextAlignment (All OS)

Posted: Thu Jun 26, 2025 3:54 am
by kenmo
Thanks for this!!!

Re: SetTextAlignment (All OS)

Posted: Fri Jun 27, 2025 4:44 pm
by Mijikai
Thanks nice to have :)

Re: SetTextAlignment (All OS)

Posted: Fri Jun 27, 2025 9:11 pm
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