Page 1 of 1

Center text in an editorgadget

Posted: Wed Jun 01, 2011 9:51 am
by captain_skank
Morning All,

Is there a way to center text in an editorgadget like #ss_centerimage for a textgadget ??

I'd like to center the text verticaly as well as horizontaly.

cheers

edit : it would would also be good to do this in a string gadget ;)

Re: Center text in an editorgadget

Posted: Wed Jun 01, 2011 3:00 pm
by RASHAD
Hi captain_skank

Not very much complete
But it is a start

For EditorGadget

Code: Select all

#CFM_BOLD            =  1
#CFM_ITALIC          =  2
#CFM_UNDERLINE       =  4
#CFM_STRIKEOUT       =  8
#CFM_PROTECTED       =  $10
#CFM_LINK            =  $20
#CFM_SMALLCAPS       =  $40
#CFM_ALLCAPS         =  $80
#CFM_HIDDEN          =  $100
#CFM_OUTLINE         =  $200
#CFM_SHADOW          =  $400
#CFM_EMBOSS          =  $800
#CFM_IMPRINT         =  $1000
#CFM_DISABLED        =  $2000
#CFM_REVISED         =  $4000
#CFM_REVAUTHOR       =  $8000
#CFM_SUPERSCRIPT     =  $10000
#CFM_SUBSCRIPT       =  $30000
#CFM_ANIMATION       =  $40000
#CFM_STYEL           =  $80000
#CFM_KERNING         =  $100000
#CFM_SPACING         =  $200000
#CFM_WEIGHT          =  $400000
#CFM_UNDERLINETYPE   =  $800000
#CFM_LCID            =  $2000000
#CFM_BACKCOLOR       =  $4000000
#CFM_CHARSET         =  $8000000
#CFM_OFFSET          =  $10000000
#CFM_FACE            =  $20000000
#CFM_COLOR           =  $40000000
#CFM_SIZE            =  $80000000

; #CFE_BOLD            = 1
; #CFE_ITALIC          = 2
; #CFE_UNDERLINE       = 4
; #CFE_STRIKEOUT       = 8
; #CFE_PROTECTED       = $10
; #CFE_LINK            = $20
; #CFE_SUBSCRIPT       = $10000
; #CFE_SUPERSCRIPT     = $20000
; #CFE_AUTOCOLOR       = #CFM_COLOR
; #CFE_SMALLCAPS       = #CFM_SMALLCAPS
; #CFE_ALLCAPS         = #CFM_ALLCAPS
; #CFE_HIDDEN          = #CFM_HIDDEN
; #CFE_OUTLINE         = #CFM_OUTLINE
; #CFE_SHADOW          = #CFM_SHADOW
; #CFE_EMBOSS          = #CFM_EMBOSS
; #CFE_IMPRINT         = #CFM_IMPRINT
; #CFE_DISABLED        = #CFM_DISABLED
; #CFE_REVISED         = #CFM_REVISED
; #CFE_AUTOBACKCOLOR   = #CFM_BACKCOLOR

#CFU_CF1UNDERLINE                = $FF
#CFU_INVERT                      = $FE
#CFU_UNDERLINETHICKLONGDASH      = 18
#CFU_UNDERLINETHICKDOTTED        = 17
#CFU_UNDERLINETHICKDASHDOTDOT    = 16
#CFU_UNDERLINETHICKDASHDOT       = 15
#CFU_UNDERLINETHICKDASH          = 14
#CFU_UNDERLINELONGDASH           = 13
#CFU_UNDERLINEHEAVYWAVE          = 12
#CFU_UNDERLINEDOUBLEWAVE         = 11
#CFU_UNDERLINEHAIRLINE           = 10
#CFU_UNDERLINETHICK              = 9
#CFU_UNDERLINEWAVE               = 8
#CFU_UNDERLINEDASHDOTDOT         = 7
#CFU_UNDERLINEDASHDOT            = 6
#CFU_UNDERLINEDASH               = 5
#CFU_UNDERLINEDOTTED             = 4
#CFU_UNDERLINEDOUBLE             = 3
#CFU_UNDERLINEWORD               = 2
#CFU_UNDERLINE                   = 1
#CFU_UNDERLINENONE               = 0


Procedure AutoresizeEG(Gadget)
  OldStyle = GetWindowLongPtr_(GadgetID(Gadget),#GWL_STYLE)
  For i = 12 To 500 Step 4
  MoveWindow_(GadgetID(Gadget),10,10,480,i,1)
  SendMessage_(GadgetID(Gadget),#WM_VSCROLL,#SB_TOP,0)
  SendMessage_(GadgetID(Gadget),#WM_VSCROLL,#SB_BOTTOM,0)  
  NewStyle = GetWindowLongPtr_(GadgetID(Gadget),#GWL_STYLE)
  If NewStyle <> OldStyle
     Break
  EndIf
  Next
EndProcedure


Procedure LineFormat(Gadget,FName$,FSize,FColor,BColor,FFlags)
  format.CHARFORMAT2
  format\cbSize = SizeOf(CHARFORMAT2)
  format\dwMask = #CFM_FACE|#CFM_COLOR|#CFM_BACKCOLOR|#CFM_SIZE|#CFM_ITALIC|
#CFM_BOLD|#CFM_STRIKEOUT|#CFM_UNDERLINE|#CFM_EMBOSS|
#CFM_OUTLINE|#CFM_SHADOW|#CFM_LINK|#CFM_SUBSCRIPT|
#CFM_SUPERSCRIPT|#CFM_PROTECTED
  ;format\dwEffects = #CFE_BOLD|#CFE_ITALIC|#CFE_LINK|#CFE_OUTLINE|#CFE_SHADOW|
#CFE_STRIKEOUT|#CFE_AUTOBACKCOLOR|#CFE_AUTOCOLOR|
#CFE_EMBOSS|#CFE_SUBSCRIPT|#CFE_SUPERSCRIPT|#CFE_UNDERLINE
  PokeS(@format\szFaceName, FName$)
  format\yHeight = FSize*1440/72
  format\crTextColor = FColor
  format\crBackColor = BColor
  format\dwEffects = FFlags
  SendMessage_(GadgetID(Gadget), #EM_SETCHARFORMAT, #SCF_SELECTION, @format)
EndProcedure

If OpenWindow(0, 0, 0, 500, 500, "EditorGadget", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  EditorGadget(1, 10, 10, 480,12,#PB_Editor_ReadOnly|1)                    ;0 Left aligned - 1 Center aligned - 2 Right aligned

    LineFormat(1,"Forte",52,$0102FE,$02FEFD,#CFM_UNDERLINE|
#CFU_UNDERLINETHICKDASH)                ;LineFormat(#Gadget,Font Name,Font Size,Font Color,Font Flags)
    AddGadgetItem(1, 0, "This is a RED") 

    LineFormat(1,"Broadway",24,$73E06D,$02FEFD,#CFM_BOLD)
    AddGadgetItem(1, 1, "Times new Roman")

    LineFormat(1,"Times new Roman",16,$FB1404,$C5C5C5,#CFM_ANIMATION)
    AddGadgetItem(1, 2, "This is Italic and Strikeout")    
   
    AddGadgetItem(1, 3, "This is usual Text")
    AutoresizeEG(1)
  
  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf


For StringGadget
Sparkie code I believe

Code: Select all

Procedure StringGadgetVCenter(GadgetID)
   hwndEdit = GadgetID(GadgetID)
   
   LineCount = SendMessage_(hwndEdit,#EM_GETLINECOUNT, 0, 0)

   hdc = GetDC_(hwndEdit)
   SelectObject_(hdc,GetGadgetFont(0))
   GetTextExtentPoint32_(hdc,"ABC",3,fsz.SIZE)
   ReleaseDC_(hwndEdit,hdc)   
   
   GetClientRect_(hwndEdit,eRect.RECT)
   eRect\top = (GadgetHeight(GadgetID) - fsz\cy * LineCount) / 2
   eRect\bottom = eRect\top + (fsz\cy * LineCount) + 4
   
   If eRect\bottom < GadgetHeight(GadgetID)
      SendMessage_(hwndEdit,#EM_SETRECT,0,eRect)
   EndIf
EndProcedure

Text$ = "Vertical and Horizontal" + #CRLF$ + "Centered Text in" + #CRLF$ + "Multiline StringGadget"

OpenWindow(0,0,0,300,200,"Edit Control VCenter",#WS_SYSMENU | #WS_CAPTION | 1)
StringGadget(0,10,10,280,180,Text$,#ES_MULTILINE | #ES_CENTER )
SetGadgetFont(0,LoadFont(0,"Arial",12))
StringGadgetVCenter(0)

LinesNum = SendMessage_(GadgetID(0), #EM_GETLINECOUNT,0,0)

Repeat
   EventID = WaitWindowEvent()
   
   Select EventGadget()
      Case 0
      If EventType() = #PB_EventType_Change And LinesNum <> SendMessage_(GadgetID(0),#EM_GETLINECOUNT,0,0)
        LinesNum = SendMessage_(GadgetID(0),#EM_GETLINECOUNT,0,0)
       
        StringGadgetVCenter(0)
      EndIf
   EndSelect
Until EventID = #PB_Event_CloseWindow


Re: Center text in an editorgadget

Posted: Wed Jun 01, 2011 3:35 pm
by captain_skank
Thanks Rashad, much appreciated. :lol:

Re: Center text in an editorgadget

Posted: Wed Jun 01, 2011 3:52 pm
by RASHAD
You are welcome

Much simpler EditorGadget

Code: Select all

#CFM_BOLD            =  1
#CFM_ITALIC          =  2
#CFM_UNDERLINE       =  4
#CFM_STRIKEOUT       =  8
#CFM_PROTECTED       =  $10
#CFM_LINK            =  $20
#CFM_SMALLCAPS       =  $40
#CFM_ALLCAPS         =  $80
#CFM_HIDDEN          =  $100
#CFM_OUTLINE         =  $200
#CFM_SHADOW          =  $400
#CFM_EMBOSS          =  $800
#CFM_IMPRINT         =  $1000
#CFM_DISABLED        =  $2000
#CFM_REVISED         =  $4000
#CFM_REVAUTHOR       =  $8000
#CFM_SUPERSCRIPT     =  $10000
#CFM_SUBSCRIPT       =  $30000
#CFM_ANIMATION       =  $40000
#CFM_STYEL           =  $80000
#CFM_KERNING         =  $100000
#CFM_SPACING         =  $200000
#CFM_WEIGHT          =  $400000
#CFM_UNDERLINETYPE   =  $800000
#CFM_LCID            =  $2000000
#CFM_BACKCOLOR       =  $4000000
#CFM_CHARSET         =  $8000000
#CFM_OFFSET          =  $10000000
#CFM_FACE            =  $20000000
#CFM_COLOR           =  $40000000
#CFM_SIZE            =  $80000000


Procedure LineFormat(Gadget,FName$,FSize,FColor,BColor,FFlags)
  format.CHARFORMAT2
  format\cbSize = SizeOf(CHARFORMAT2)
  format\dwMask = #CFM_FACE|#CFM_COLOR|#CFM_BACKCOLOR|#CFM_SIZE| #CFM_ITALIC|#CFM_BOLD|#CFM_STRIKEOUT|#CFM_UNDERLINE| #CFM_EMBOSS|#CFM_OUTLINE|#CFM_SHADOW|#CFM_LINK| #CFM_SUBSCRIPT|#CFM_SUPERSCRIPT|#CFM_PROTECTED
  PokeS(@format\szFaceName, FName$)
  format\yHeight = FSize*1440/72
  format\crTextColor = FColor
  format\crBackColor = BColor
  format\dwEffects = FFlags
  SendMessage_(GadgetID(Gadget), #EM_SETCHARFORMAT, #SCF_SELECTION, @format)
EndProcedure

If OpenWindow(0, 0, 0, 500, 500, "EditorGadget", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  EditorGadget(1, 10, 10, 480,200,#ES_CENTER)

    AddGadgetItem(1, 0, "")
       
    LineFormat(1,"Forte",52,$0102FE,$02FEFD,#CFM_UNDERLINE| #CFU_UNDERLINETHICKDASH)                ;LineFormat(#Gadget,Font Name,Font Size,Font Color,Font Flags)
    AddGadgetItem(1, 1, "This is a RED") 

    LineFormat(1,"Broadway",24,$73E06D,$02FEFD,#CFM_BOLD)
    AddGadgetItem(1, 2, "Times new Roman")

    LineFormat(1,"Times new Roman",16,$FB1404,$C5C5C5,#CFM_ANIMATION)
    AddGadgetItem(1, 3, "This is Italic and Strikeout")   
    
    AddGadgetItem(1, 4, "This is usual Text")
    
    space = (200 - 52 - 24 - 16 - 12 - 12)/2      ;space = (Editor Height - Font Size 1 - Font Size 2 - FontSize .....)/2
    
    LineFormat(1,"Arial",space,0,0,0)
    AddGadgetItem(1, 0, "")
  
  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf


Re: Center text in an editorgadget

Posted: Sun Aug 04, 2024 1:15 pm
by BarryG
Hi all. I can see from the code above that flag of #SS_CENTER for an EditorGadget will center it horizontally. But how to left-align it again without recreating the gadget? Thanks.

This doesn't work: SetWindowLongPtr_(eg,#GWL_STYLE,GetWindowLongPtr_(eg,#GWL_STYLE) ! #SS_CENTER)

Code: Select all

If OpenWindow(0, 0, 0, 322, 140, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  eg=EditorGadget(0, 8, 8, 306, 80, #SS_CENTER)
  SetGadgetText(0, #CRLF$ + "Want to toggle this being centered")
  ButtonGadget(1, 8, 100, 306, 30, "Toggle")
  Repeat
    ev=WaitWindowEvent()
    If ev=#PB_Event_Gadget And EventGadget()=1
      ; Insert code here to toggling centering. :)
    EndIf
  Until ev = #PB_Event_CloseWindow
EndIf

Re: Center text in an editorgadget

Posted: Sun Aug 04, 2024 2:06 pm
by Axolotl
There might be a better way, and unfortunately SetWindowLongPtr_() does not work here....
In the meantime you can start with this....

Code: Select all

; ---------------------------------------------------------------------------

Procedure RichEdit_SetSelection(Gadget, LineStart, CharStart, LineEnd, CharEnd)     
  Protected hGad, sel.CHARRANGE  

  hGad = GadgetID(Gadget) 
  sel\cpMin = SendMessage_(hGad, #EM_LINEINDEX, LineStart, 0)+CharStart-1  
  If LineEnd = -1  
    LineEnd = SendMessage_(hGad, #EM_GETLINECOUNT, 0, 0) - 1  
  EndIf  
  sel\cpMax = SendMessage_(hGad, #EM_LINEINDEX, LineEnd, 0)  
  If CharEnd = -1  
    sel\cpMax + SendMessage_(hGad, #EM_LINELENGTH, sel\cpMax, 0)  
  Else  
    sel\cpMax + CharEnd-1
  EndIf  
  SendMessage_(hGad, #EM_EXSETSEL, 0, @sel)  ;:: ???? @?
EndProcedure  

; ---------------------------------------------------------------------------

Macro RichEdit_SetSelAll(Gadget)
  RichEdit_SetSelection(Gadget, 0, 1, -1, -1)
EndMacro

Macro RichEdit_SetSelNothing(Gadget)
  RichEdit_SetSelection(Gadget, 0, 0, 0, 0)
EndMacro


; ---------------------------------------------------------------------------
;
;  Set paragraph justification. 
; 
;  Can be one of the following values: 
;    #PFA_LEFT    
;    #PFA_RIGHT    
;    #PFA_CENTER    

Procedure RichEdit_JustifyParagraph(Gadget, Justify) 
  Protected format.PARAFORMAT 

  format\cbSize = SizeOf(PARAFORMAT) 
  format\dwMask = #PFM_ALIGNMENT 
  format\wAlignment = Justify 
  SendMessage_(GadgetID(Gadget), #EM_SETPARAFORMAT, 0, @format) 
EndProcedure 

; ---------------------------------------------------------------------------

Procedure RichEditGadget(Gadget, X, Y, W, H, Flags=0) 
  Protected ret 

  ret = EditorGadget(Gadget, X, Y, W, H, Flags) 
  If Gadget = #PB_Any 
    Gadget = ret 
  EndIf 
  SendMessage_(GadgetID(Gadget), #EM_SETTEXTMODE, #TM_RICHTEXT, 0) 
  ProcedureReturn ret 
EndProcedure 

; ---------------------------------------------------------------------------

Global bToggle = 1

If OpenWindow(0, 0, 0, 322, 140, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
;   EditorGadget(0, 8, 8, 306, 80, 1) ; 1 for the flag means centered horizontally.
;   SendMessage_(GadgetID(0), #EM_SETTEXTMODE, #TM_RICHTEXT, 0)  ; 
  RichEditGadget(0, 8, 8, 306, 80, 1) ; 1 for the flag means centered horizontally.

  SetGadgetText(0, #CRLF$ + "Want to toggle this being centered")
  ButtonGadget(1, 8, 100, 306, 30, "Toggle")
  Repeat 
    ev=WaitWindowEvent()
    If ev=#PB_Event_Gadget And EventGadget()=1
      ; Insert code here to toggling centering. :) 
      RichEdit_SetSelAll(0)
      If bToggle 
        RichEdit_JustifyParagraph(0, #PFA_LEFT) 
      Else 
        RichEdit_JustifyParagraph(0, #PFA_CENTER) 
      EndIf 
      bToggle ! 1 
      RichEdit_SetSelNothing(0) 
    EndIf
  Until ev = #PB_Event_CloseWindow
EndIf


Re: Center text in an editorgadget

Posted: Sun Aug 04, 2024 3:53 pm
by RASHAD
Simple and concise

Code: Select all

If OpenWindow(0, 0, 0, 322, 140, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  eg=EditorGadget(0, 8, 8, 306, 80, #ES_CENTER)
  SendMessage_(eg, #EM_SETTEXTMODE, #TM_RICHTEXT, 0)
  SetGadgetText(0, #CRLF$ + "Want to toggle this being centered")
  ButtonGadget(1, 8, 100, 306, 30, "Toggle")
  Repeat
    ev=WaitWindowEvent()
    If ev=#PB_Event_Gadget And EventGadget()=1
          RUN ! 1
          pfmt.PARAFORMAT
          pfmt\cbSize = SizeOf(PARAFORMAT)
          pfmt\dwMask = #PFM_ALIGNMENT
          If Run = 1
            pfmt\wAlignment = #PFA_LEFT 
          Else
            pfmt\wAlignment = #PFA_CENTER
          EndIf       
          SendMessage_(eg,#EM_SETSEL,0,-1)
          SendMessage_(eg,#EM_SETPARAFORMAT,0,pfmt)
          SendMessage_(eg,#EM_SETSEL,0,0)
    EndIf
  Until ev = #PB_Event_CloseWindow
EndIf

Re: Center text in an editorgadget

Posted: Mon Aug 05, 2024 1:37 am
by BarryG
Thank you both. :) Do we really have to deselect the text, though? What if my user has text selected? They'll lose their selection.

Re: Center text in an editorgadget

Posted: Mon Aug 05, 2024 11:54 am
by Axolotl
Hi BarryG,

well, this was a small example to answer your question....
Now we have the next question.
Try the companion of EM_SETSEL called EM_GETSEL and you will store the selection do the adjustment and set the selection back.
For further information:
https://learn.microsoft.com/en-us/windo ... /em-getsel
Sorry, no running code this time, I am a little busy....

Re: Center text in an editorgadget

Posted: Mon Aug 05, 2024 12:46 pm
by Mindphazer
Something like this

Code: Select all

If OpenWindow(0, 0, 0, 322, 140, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  eg=EditorGadget(0, 8, 8, 306, 80, #ES_CENTER)
  SendMessage_(eg, #EM_SETTEXTMODE, #TM_RICHTEXT, 0)
  SetGadgetText(0, #CRLF$ + "Want to toggle this being centered")
  ButtonGadget(1, 8, 100, 306, 30, "Toggle")
  Repeat
    ev=WaitWindowEvent()
    If ev=#PB_Event_Gadget And EventGadget()=1
          RUN ! 1
          pfmt.PARAFORMAT
          pfmt\cbSize = SizeOf(PARAFORMAT)
          pfmt\dwMask = #PFM_ALIGNMENT
          If Run = 1
            pfmt\wAlignment = #PFA_LEFT 
          Else
            pfmt\wAlignment = #PFA_CENTER
          EndIf
          SendMessage_(eg, #EM_GETSEL, @Debut_Position, @Fin_position)    
          SendMessage_(eg,#EM_SETSEL,0,-1)
          SendMessage_(eg,#EM_SETPARAFORMAT,0,pfmt)
          SendMessage_(eg,#EM_SETSEL, Debut_Position, Fin_position)  
    EndIf
  Until ev = #PB_Event_CloseWindow
EndIf

Re: Center text in an editorgadget

Posted: Mon Aug 05, 2024 2:00 pm
by BarryG
Okay, thanks for the follow-up. Appreciated! :)

Re: Center text in an editorgadget

Posted: Tue Aug 06, 2024 9:39 am
by boddhi
RASHAD wrote: Much simpler EditorGadget
Hi Rashad,

I wanted to test your code but PB (6.11) reports an error with the value of the constant #CFM_SUPERSCRIPT already defined.
You assign it a value of $10000 and PB $30000 as #CFM_SUBSCRIPT

See the results after a test:
Image

Who is wrong? #CFM_SUBSCRIPT supersedes CFM_SUPERSCRIPT?

Thanks.

Re: Center text in an editorgadget

Posted: Wed Aug 07, 2024 10:28 am
by Axolotl
HI,
you can use this database for searching for any kind of values ....
The Magic Number Database

I tried to check this in some sources: (Best I could find) is this.

Code: Select all

/*
 *	RICHEDIT.H
 *	
 *	Purpose:
 *		RICHEDIT v2.0-v6.0 public definitions
 *	
 *	Copyright (c) Microsoft Corporation. All rights reserved.
 */
 // ... 
#define CFE_SUBSCRIPT		0x00010000		// Superscript and subscript are 
#define CFE_SUPERSCRIPT 	0x00020000		//	mutually exclusive			 
// ... 
#define CFM_SUBSCRIPT		(CFE_SUBSCRIPT | CFE_SUPERSCRIPT)
#define CFM_SUPERSCRIPT 	CFM_SUBSCRIPT
// ...