Page 1 of 1

How to make StringGadget right justified?

Posted: Mon Jun 21, 2010 12:28 am
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.

Re: How to make StringGadget right justified?

Posted: Mon Jun 21, 2010 2:56 pm
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

Re: How to make StringGadget right justified?

Posted: Thu Sep 16, 2010 9:27 pm
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

Re: How to make StringGadget right justified?

Posted: Thu Sep 16, 2010 10:15 pm
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.

Re: How to make StringGadget right justified?

Posted: Mon Aug 01, 2011 11:02 pm
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