Change Editor Gadget Text Justification on the fly

Windows specific forum
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Change Editor Gadget Text Justification on the fly

Post by IdeasVacuum »

The Text justification can be pre-set thus:

Code: Select all

EditorGadget(#GADGET_Text,10,10,380,380,#ES_RIGHT)
However, if the justification needs to change at run-time, how is that accomplished?

I have tried:

Code: Select all

SetWindowLongPtr_(GadgetID(#GADGET_Text),#GWL_STYLE,GetWindowLongPtr_(GadgetID(#GADGET_Text),#GWL_STYLE)|#ES_CENTER)
This does not work. From MSDN, seems it is not possible to make this type of change at run time, which is strange to me.

My work around is to have 3 Editors, one for each justification style, and swap them using HideGadget().

http://www.purebasic.fr/english/viewtop ... 48#p367348

Another related issue is that I would like to suppress the Editor Gadget's auto-scrollbar setting.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4997
Joined: Sun Apr 12, 2009 6:27 am

Re: Change Editor Gadget Text Justification on the fly

Post by RASHAD »

Hi IdeasVacuum
I hope it fulfill your needs

Code: Select all

#PFM_ALL2            =  $0fffdff

Procedure RangeFormat(Gadget,SLIndex,ELIndex,InFlag,BFlag,SpFlag,Align)
  sel.CHARRANGE
  sel\cpMin = SendMessage_(GadgetID(Gadget), #EM_LINEINDEX,SLIndex, 0)
  sel\cpMax = SendMessage_(GadgetID(Gadget), #EM_LINEINDEX,ELIndex, 0)
  SendMessage_(GadgetID(Gadget), #EM_EXSETSEL, 0, @sel)  
  para.PARAFORMAT2
  para\cbSize = SizeOf(PARAFORMAT2)  
  para\dwMask = #PFM_ALL2
  If InFlag = 1
    para\dxStartIndent = LeftInd 
    para\dxRightIndent = RightInd
  EndIf
  If BFlag = 1 
    para\wNumbering = BFormat
    para\wNumberingStart = BStart
    para\wNumberingStyle = BStyle
  EndIf
  If SpFlag = 1
    para\bLineSpacingRule = SpRule
    para\dyLineSpacing = LSp       
    para\dySpaceBefore = SpBefore 
    para\dySpaceAfter =  SpAfter
  EndIf 
  para\wAlignment = Align
  SendMessage_(GadgetID(Gadget), #EM_SETPARAFORMAT, 0, @para)
  sel\cpMin = SendMessage_(GadgetID(Gadget), #EM_LINEINDEX, 0, 0)
  sel\cpMax = SendMessage_(GadgetID(Gadget), #EM_LINEINDEX, 0, 0)
  SendMessage_(GadgetID(Gadget), #EM_EXSETSEL, 0, @sel)
EndProcedure

Procedure AddText(Gadget,Text$,Eolf)
  If Eolf = 1
     Text$ = Text$ + Chr(10)
  EndIf
   SendMessage_(GadgetID(Gadget), #EM_REPLACESEL, #True, @Text$)
EndProcedure

LoadFont(0, "Arial", 16)

If OpenWindow(0, 0, 0,500,500, "EditorGadget", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  EditorGadget(1,5,5,490,400)
  SetGadgetFont(1, FontID(0))
  SetGadgetColor(1, #PB_Gadget_BackColor, $E7FEFD)
  SetGadgetColor(1, #PB_Gadget_FrontColor, $FD1618)
  
  ButtonGadget(2,5,410,60,22,"Left")
  ButtonGadget(3,70,410,60,22,"Center")
  ButtonGadget(4,135,410,60,22,"Right")
  
Repeat
  Select WaitWindowEvent()
      
      Case #PB_Event_CloseWindow
            Quit = 1       
      
      Case #PB_Event_Gadget
          Select EventGadget()
           Case 2
              ClearGadgetItems(1)
              AddText(1,"",1)
              RangeFormat(1,0,0,0,0,0,#PFA_LEFT)
              SetActiveGadget(1)
           
           Case 3
              ClearGadgetItems(1)
              AddText(1,"",1)
              RangeFormat(1,0,0,0,0,0,#PFA_CENTER)
              SetActiveGadget(1)           
           
           Case 4
              ClearGadgetItems(1)
              AddText(1,"",1)
              RangeFormat(1,0,0,0,0,0,#PFA_RIGHT)
              SetActiveGadget(1)           
                       
          EndSelect
          
  EndSelect
  
  Until Quit = 1
EndIf

Egypt my love
User avatar
Kiffi
Addict
Addict
Posts: 1510
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Change Editor Gadget Text Justification on the fly

Post by Kiffi »

IdeasVacuum wrote:My work around is to have 3 Editors, one for each justification style, and swap them using HideGadget().
no need for three EditorGadgets:

Code: Select all

Enumeration 
  #MainWindow
EndEnumeration

Enumeration 
  #EditorGadget
  #Button_AlignLeft
  #Button_AlignCenter
  #Button_AlignRight
EndEnumeration

Procedure SetAlignment(Alignment)
  Protected Dummy.s
  Dummy = GetGadgetText(#EditorGadget)
  EditorGadget(#EditorGadget, 5, 40, 265, 205, Alignment)
  SetGadgetText(#EditorGadget, Dummy)
EndProcedure

OpenWindow(#MainWindow, #PB_Ignore, #PB_Ignore, 278, 253, "")

EditorGadget(#EditorGadget, 5, 40, 265, 205, #ES_LEFT)

SetGadgetText(#EditorGadget, "Feel the ..Pure.. Power")

ButtonGadget(#Button_AlignLeft, 5, 5, 85, 30, "Align left")
ButtonGadget(#Button_AlignCenter, 95, 5, 85, 30, "Align center")
ButtonGadget(#Button_AlignRight, 185, 5, 85, 30, "Align right")

Repeat
  
  WWE = WaitWindowEvent()
  
  Select WWE
      
    Case #PB_Event_Gadget
      
      Select EventGadget()
          
        Case #Button_AlignLeft   : SetAlignment(#ES_LEFT)
        Case #Button_AlignCenter : SetAlignment(#ES_CENTER)
        Case #Button_AlignRight  : SetAlignment(#ES_RIGHT)
          
      EndSelect
      
  EndSelect
  
Until WWE = #PB_Event_CloseWindow
Greetings ... Kiffi
Hygge
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Change Editor Gadget Text Justification on the fly

Post by IdeasVacuum »

Wow guys - there I am thinking that it can't be done and bang - two different methods! Thank you very much for your help :mrgreen:
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
skywalk
Addict
Addict
Posts: 4242
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Change Editor Gadget Text Justification on the fly

Post by skywalk »

I use #PB_Any to create my gadgets and store relevant stuff in a structured array.
In this case, Kiffi's approach requires FreeGadget(gadgetID)...

Code: Select all

Enumeration
  #MainWindow
EndEnumeration

Enumeration
  #EditorGadget
  #Button_AlignLeft
  #Button_AlignCenter
  #Button_AlignRight
EndEnumeration
Global Dim gad.i(3)  ; Simple array here, but can be array of structure holding all sorts of gadget info.

Procedure SetAlignment(gID.i,Alignment)
  Protected Dummy.s
  Dummy = GetGadgetText(gID)
  If IsGadget(gID)
    FreeGadget(gID)
    gad(#EditorGadget) = EditorGadget(#PB_Any, 5, 40, 265, 205, Alignment)
    SetGadgetText(gad(#EditorGadget), Dummy)
  EndIf
EndProcedure

OpenWindow(#MainWindow, #PB_Ignore, #PB_Ignore, 278, 253, "")
gad(#EditorGadget) = EditorGadget(#PB_Any, 5, 40, 265, 205, #ES_LEFT)
SetGadgetText(gad(#EditorGadget), "Feel the ..Pure.. Power")
gad(#Button_AlignLeft) = ButtonGadget(#PB_Any, 5, 5, 85, 30, "Align left")
gad(#Button_AlignCenter) = ButtonGadget(#PB_Any, 95, 5, 85, 30, "Align center")
gad(#Button_AlignRight) = ButtonGadget(#PB_Any, 185, 5, 85, 30, "Align right")
Repeat
  WWE = WaitWindowEvent()
  Select WWE
  Case #PB_Event_Gadget
    Select EventGadget()
    Case gad(#Button_AlignLeft)   : SetAlignment(gad(#EditorGadget), #ES_LEFT)
    Case gad(#Button_AlignCenter) : SetAlignment(gad(#EditorGadget), #ES_CENTER)
    Case gad(#Button_AlignRight)  : SetAlignment(gad(#EditorGadget), #ES_RIGHT)
    EndSelect
  EndSelect
Until WWE = #PB_Event_CloseWindow
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4997
Joined: Sun Apr 12, 2009 6:27 am

Re: Change Editor Gadget Text Justification on the fly

Post by RASHAD »

With this approach :
- You do not need to create the gadget again and again
- You can have the three justification styles in the same gadget
- You can have the RTF text as the Plain text intact

Code: Select all


#PFM_ALL2            =  $0fffdff

Procedure RangeFormat(Gadget,SLIndex,ELIndex,InFlag,BFlag,SpFlag,Align)
  sel.CHARRANGE
  sel\cpMin = SendMessage_(GadgetID(Gadget), #EM_LINEINDEX,SLIndex, 0)
  sel\cpMax = SendMessage_(GadgetID(Gadget), #EM_LINEINDEX,ELIndex, 0)
  SendMessage_(GadgetID(Gadget), #EM_EXSETSEL, 0, @sel)  
  para.PARAFORMAT2
  para\cbSize = SizeOf(PARAFORMAT2)  
  para\dwMask = #PFM_ALL2
  If InFlag = 1
    para\dxStartIndent = LeftInd 
    para\dxRightIndent = RightInd
  EndIf
  If BFlag = 1 
    para\wNumbering = BFormat
    para\wNumberingStart = BStart
    para\wNumberingStyle = BStyle
  EndIf
  If SpFlag = 1
    para\bLineSpacingRule = SpRule
    para\dyLineSpacing = LSp       
    para\dySpaceBefore = SpBefore 
    para\dySpaceAfter =  SpAfter
  EndIf 
  para\wAlignment = Align
  SendMessage_(GadgetID(Gadget), #EM_SETPARAFORMAT, 0, @para)
  sel\cpMin = SendMessage_(GadgetID(Gadget), #EM_LINEINDEX, 0, 0)
  sel\cpMax = SendMessage_(GadgetID(Gadget), #EM_LINEINDEX, 0, 0)
  SendMessage_(GadgetID(Gadget), #EM_EXSETSEL, 0, @sel)
EndProcedure

Procedure AddText(Gadget,Text$,Eolf)
  If Eolf = 1
     Text$ = Text$ + Chr(10)
  EndIf
   SendMessage_(GadgetID(Gadget), #EM_REPLACESEL, #True, @Text$)
EndProcedure

LoadFont(0, "Arial", 16)

If OpenWindow(0, 0, 0,500,500, "EditorGadget", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  EditorGadget(1,5,5,490,400)
  SetGadgetFont(1, FontID(0))
  SetGadgetColor(1, #PB_Gadget_BackColor, $E7FEFD)
  SetGadgetColor(1, #PB_Gadget_FrontColor, $FD1618)
  
  SendMessage_(GadgetID(1), #EM_LIMITTEXT, -1, 0)

  a.s="{\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fswiss\fcharset0 Arial;}}{\colortbl ;\red255\green0\blue0;\red0\green0\blue0;}"
  a.s=a.s+"{\*\generator Msftedit 5.41.15.1503;}\viewkind4\uc1\pard\f0\fs20 Hello, this is \cf1\b\fs32 RTF\cf2\b0\fs20 direct!\cf0\par}"
  *MemoryBuffer = AllocateMemory(Len(a.s)+1)
  PokeS(*MemoryBuffer, a.s, Len(a.s)+1,#PB_Ascii)
  SetGadgetText(1,PeekS(*MemoryBuffer ,Len(a.s)+1))
  
  ButtonGadget(2,5,410,60,22,"Left")
  ButtonGadget(3,70,410,60,22,"Center")
  ButtonGadget(4,135,410,60,22,"Right")
  
Repeat
  Select WaitWindowEvent()
      
      Case #PB_Event_CloseWindow
            Quit = 1       
      
      Case #PB_Event_Gadget
          Select EventGadget()
           Case 2
              ;ClearGadgetItems(1)
              ;AddText(1,"",1)
              RangeFormat(1,0,3,0,0,0,#PFA_LEFT)
              SetActiveGadget(1)
           
           Case 3
;               ClearGadgetItems(1)
;               AddText(1,"",1)
              RangeFormat(1,0,3,0,0,0,#PFA_CENTER)
              SetActiveGadget(1)           
           
           Case 4
              ;ClearGadgetItems(1)
              ;AddText(1,"",1)
              RangeFormat(1,0,3,0,0,0,#PFA_RIGHT)
              SetActiveGadget(1)           
                       
          EndSelect
          
  EndSelect
  
  Until Quit = 1
EndIf
Egypt my love
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Change Editor Gadget Text Justification on the fly

Post by IdeasVacuum »

...Thanks for the free image tip Skywalk. I had actually surpassed myself and remembered that requirement - must have changed the coffee brand recently or something.

I chose Kiffi's method over Rashad's this time because it was easier to understand (and to implement).

....got a bonus effect too - the bug where the text did not show-up in the saved image vanished! :mrgreen:
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
skywalk
Addict
Addict
Posts: 4242
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Change Editor Gadget Text Justification on the fly

Post by skywalk »

RASHAD - you are my Windows API hero 8)
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply