How to make StringGadget right justified?

Mac OSX specific forum
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

How to make StringGadget right justified?

Post by WilliamL »

Is there a way to do this? Using the API? I've been formatting the output with blanks to the left to push the data to the right but it would be much easier to just have the field be right-justified.
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: How to make StringGadget right justified?

Post by skywalk »

Hi,
Don't have a Mac, but mixing the Text and String Flags works for me on XP Pro.
That's why I posted to the Help Manual page.

http://www.purebasic.fr/english/viewtop ... 84#p322884
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: How to make StringGadget right justified?

Post by Shardik »

Code: Select all

; Converted from Steve Christensen's C code
; http://lists.apple.com/archives/carbon-development/2003/Feb/msg01997.html

ImportC ""
  GetControlData(Control.L, ControlPartCode.L, TagName.L, BufferSize.L, *Buffer, *ActualSize)
  SetControlFontStyle(Control.L, *ControlFontStyleRec)
  TESetAlignment(just.W, TEHandle.L)
EndImport

#kControlEditTextPart = 5
#kControlUseJustMask = $40
#noErr = 0
#teFlushRight = -1

Structure RGBColor
  red.U
  green.U
  blue.U
EndStructure

Structure ControlFontStyleRec
  flags.W
  font.W
  size.W
  style.W
  mode.W
  just.W
  foreColor.RGBColor
  backColor.RGBColor
EndStructure

Define Result.L
Define Style.ControlFontStyleRec
Define *TEHandle

OpenWindow(0, 0, 0, 280, 30, "Right justified StringGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
StringGadget(0, 5, 5, 270, 20, "1234567")

Style\flags = #kControlUseJustMask
Style\just = #teFlushRight

Result = SetControlFontStyle(GadgetID(0), @Style) = #noErr

If Result = #noErr And (Style\flags & #kControlUseJustMask)
  If GetControlData(GadgetID(0), #kControlEditTextPart, 'than', SizeOf(*TEHandle), *TEHandle, 0) = #noErr
    TESetAlignment(Style\just, *TEHandle)
  EndIf
EndIf

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: How to make StringGadget right justified?

Post by WilliamL »

Wow, thanks Shardik, you just made my day. :D

I really think this is a useful addition to the StringGadget.

I hope you don't mind if I add it to my 'API list for Mac' thread.
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: How to make StringGadget right justified?

Post by Shardik »

I have simplified my above example and expanded it to toggle between
left, center and right justification of the text:

Code: Select all

ImportC ""
  SetControlData(ControlRef.L, ControlPartCode.L, TagName.L, BufferSize.L, *Buffer)
  SetControlFontStyle(ControlRef.L, *ControlFontStyleRec)
EndImport

#kControlEditTextPart = 5
#kControlEditTextSelectionTag = 'sele'
#kControlUseJustMask = $40

Structure RGBColor
  Red.U
  Green.U
  Blue.U
EndStructure

Structure ControlFontStyleRec
  Flags.W
  Font.W
  Size.W
  Style.W
  Mode.W
  Just.W
  ForeColor.RGBColor
  BackColor.RGBColor
EndStructure

Structure ControlEditTextSelectionRec
  SelStart.W
  SelEnd.W
EndStructure

Define Style.ControlFontStyleRec
Define TextSelection.ControlEditTextSelectionRec

OpenWindow(0, 100, 100, 270, 130, "Justify text in StringGadget")
StringGadget(0, 5, 10, 260, 20, "1234567")
ButtonGadget(1, (WindowWidth(0) - 100) / 2, 40, 100, 20, "Justify right")
ButtonGadget(2, (WindowWidth(0) - 100) / 2, 70, 100, 20, "Justify left")
ButtonGadget(3, (WindowWidth(0) - 100) / 2, 100, 100, 20, "Justify center")

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventType() = #PB_EventType_LeftClick
        GadgetID = EventGadget()

        If GadgetID >= 1 And GadgetID <= 3
          Style\Flags = #kControlUseJustMask
          Style\Just = GadgetID - 2

          ; ----- For right justification the cursor has to be positioned at the
          ;       right end because otherwise the text will be shifted out of view
          If GadgetID = 1
            TextSelection\SelStart = Len(GetGadgetText(0))
            TextSelection\SelEnd = TextSelection\SelStart
            SetControlData(GadgetID(0), #kControlEditTextPart, #kControlEditTextSelectionTag, SizeOf(ControlEditTextSelectionRec), @TextSelection)
            SetControlFontStyle(GadgetID(0), @Style)
          EndIf

          SetControlFontStyle(GadgetID(0), @Style)
        EndIf
      EndIf
  EndSelect
ForEver
Post Reply