Recent

Author Topic: Setting text attributes  (Read 5871 times)

rick2691

  • Sr. Member
  • ****
  • Posts: 444
Setting text attributes
« on: August 15, 2016, 05:58:21 pm »
The following is combination of two methods for setting text attributes:

1) If a position is clicked and selection has been made it uses SetTextAttributes and inherets exisiting attributes.

2) If a selection has ben made it uses SetRangeParams and only modifies by the single attribute that is chosen.

I have learned both of these methods by working with the mediator of this forum, and I offer him my thanks.

I have also added scroll control so it will return to your editing position.

Code: Pascal  [Select][+][-]
  1. // global variables...
  2. var TxtHrzBar,TxtVrtBar: integer;  
  3.      SelFontFormat: TFontParams;
  4.      PageMemo: TRichMemo;
  5.  
  6. //  List of mask-constants for changing attribute-types in SetRangeParams...
  7. //  tmm_Color - font color would be modified (fnt.Color field must be populated)
  8. //  tmm_Name - font name would be modified (fnt.Name field must be populated)
  9. //  tmm_Size - font size would be modified (fnt.Size field must be populated)
  10. //  tmm_Styles - font styles modified per AddFontStyle and RemoveFontStyle parameters
  11. //                    (font styles: fsBold, fsItalic, fsUnderline, fsStrikeOut)
  12. //  tmm_BackColor - text highlight (background) color would be modified
  13. //                         (fnt.HasBkClr and fnt.BkColor fields must be populated).
  14.  
  15. procedure TCmdForm.btnBoldClick(Sender: TObject);
  16. begin
  17.           PageMemo.GetTextAttributes(PageMemo.SelStart, SelFontFormat);  
  18.           TxtHrzBar:= GetScrollPos(PageMemo.handle,SB_HORZ);
  19.           TxtVrtBar:= GetScrollPos(PageMemo.handle,SB_VERT);
  20.           if PageMemo.SelLength=0
  21.              then begin
  22.                     if (fsBold in SelFontFormat.Style = False)
  23.                         then SelFontFormat.Style:=SelFontFormat.Style + [fsBold]
  24.                         else SelFontFormat.Style:=SelFontFormat.Style - [fsBold];
  25.                    PageMemo.SetTextAttributes(PageMemo.SelStart, PageMemo.SelLength, SelFontFormat);
  26.                    end
  27.              else begin
  28.                    if (fsBold in SelFontFormat.Style=False)
  29.                        then begin
  30.                               PageMemo.SetRangeParams(PageMemo.SelStart,   //  int: begin position of selection, 0 sets start of file.
  31.                                                                    PageMemo.SelLength,  //  int: length of selection set.
  32.                                                                    [tmm_Styles],        //  cst: modify types, [] sets to empty, or [tmm_Styles,tmm_Color]
  33.                                                                    '',                  //  str: font name, '' sets to empty.
  34.                                                                    0,                   //  int: font size, 0 sets to empty.
  35.                                                                    0,                   //  int: color, 0 sets to empty.
  36.                                                                    [fsBold],            //  cst: adding styles, [] sets to empty, or [fsBold,fsItalic]
  37.                                                                    []                   //  cst: remove styles, [] sets to empty, or [fsBold,fsItalic]
  38.                                                                   );
  39.                             end
  40.                       else begin
  41.                             PageMemo.SetRangeParams (PageMemo.SelStart,  //  int: begin position of selection, 0 sets start of file.
  42.                                                                    PageMemo.SelLength,  //  int: length of selection characters, -1 sets end of file.
  43.                                                                    [tmm_Styles],        //  cst: modify types, [] sets to empty, or [tmm_Styles,tmm_Color]
  44.                                                                    '',                  //  str: font name, '' sets to empty.
  45.                                                                    0,                   //  int: font size, 0 sets to empty.
  46.                                                                    0,                   //  int: color, 0 sets to empty.
  47.                                                                    [],                  //  cst: adding styles, [] sets to empty, or [fsBold,fsItalic]
  48.                                                                    [fsBold]             //  cst: remove styles, [] sets to empty, or [fsBold,fsItalic]
  49.                                                                    );
  50.                            end;
  51.                   end;
  52.          SendMessage(PageMemo.handle, WM_HSCROLL, MakeLong(SB_THUMBPOSITION, TxtHrzBar), 0);
  53.          SendMessage(PageMemo.handle, WM_VSCROLL, MakeLong(SB_THUMBPOSITION, TxtVrtBar), 0);
  54.          PageMemo.SetFocus;
  55. end;
  56.  

I hope that any of you will be able benefit by it.

Rick
« Last Edit: November 29, 2016, 08:56:06 pm by rick2691 »
Windows 11, LAZ 2.0.10, FPC 3.2.0, SVN 63526, i386-win32-win32/win64, using windows unit

rick2691

  • Sr. Member
  • ****
  • Posts: 444
Re: Setting text attributes
« Reply #1 on: November 29, 2016, 08:25:46 pm »
By my last post I found a problem. It is the placement of

Code: Pascal  [Select][+][-]
  1. PageMemo.GetTextAttributes(PageMemo.SelStart, SelFontFormat);  

and an adjustment to its use. The following sets it in the "else" clause...

Code: Pascal  [Select][+][-]
  1. procedure TCmdForm.btnBoldClick(Sender: TObject);
  2. begin
  3.   if PageControl1.PageCount>0
  4.      then begin
  5.           TxtHrzBar:= GetScrollPos(PageMemo.handle,SB_HORZ);
  6.           TxtVrtBar:= GetScrollPos(PageMemo.handle,SB_VERT);
  7.           if PageMemo.SelLength=0
  8.              then begin
  9.                   if (fsBold in SelFontFormat.Style = False)
  10.                       then SelFontFormat.Style:=SelFontFormat.Style + [fsBold]
  11.                       else SelFontFormat.Style:=SelFontFormat.Style - [fsBold];
  12.                   PageMemo.SetTextAttributes(PageMemo.SelStart, PageMemo.SelLength, SelFontFormat);
  13.                   end
  14.              else begin
  15.                   if (CurrentLin=1) and (CurrentPos=1)
  16.                      then PageMemo.GetTextAttributes(PageMemo.SelStart, SelFontFormat)   // get in place
  17.                      else PageMemo.GetTextAttributes(PageMemo.SelStart-1, SelFontFormat);  // get from prior
  18.                   if (fsBold in SelFontFormat.Style = False)
  19.                       then begin // not in
  20.                            PageMemo.SetRangeParams (PageMemo.SelStart,   //  int: begin position of selection, 0 sets start of file.
  21.                                                     PageMemo.SelLength,  //  int: length of selection set.
  22.                                                     [tmm_Styles],        //  cst: modify types, [] sets to empty, or [tmm_Styles,tmm_Color]
  23.                                                     '',                  //  str: font name, '' sets to empty.
  24.                                                     0,                   //  int: font size, 0 sets to empty.
  25.                                                     0,                   //  int: color, 0 sets to empty.
  26.                                                     [fsBold],            //  cst: adding styles, [] sets to empty, or [fsBold,fsItalic]
  27.                                                     []                   //  cst: remove styles, [] sets to empty, or [fsBold,fsItalic]
  28.                                                     );
  29.                            end
  30.                       else begin  // is in
  31.                            PageMemo.SetRangeParams (PageMemo.SelStart,  //  int: begin position of selection, 0 sets start of file.
  32.                                                     PageMemo.SelLength,  //  int: length of selection characters, -1 sets end of file.
  33.                                                     [tmm_Styles],        //  cst: modify types, [] sets to empty, or [tmm_Styles,tmm_Color]
  34.                                                     '',                  //  str: font name, '' sets to empty.
  35.                                                     0,                   //  int: font size, 0 sets to empty.
  36.                                                     0,                   //  int: color, 0 sets to empty.
  37.                                                     [],                  //  cst: adding styles, [] sets to empty, or [fsBold,fsItalic]
  38.                                                     [fsBold]             //  cst: remove styles, [] sets to empty, or [fsBold,fsItalic]
  39.                                                     );
  40.                            end;
  41.                   end;
  42.          SendMessage(PageMemo.handle, WM_HSCROLL, MakeLong(SB_THUMBPOSITION, TxtHrzBar), 0);
  43.          SendMessage(PageMemo.handle, WM_VSCROLL, MakeLong(SB_THUMBPOSITION, TxtVrtBar), 0);
  44.          PageMemo.SetFocus;
  45.          PageMemo.Repaint;
  46.          end;
  47. end;
  48.  

I hope this useful for someone. Its purpose is to permit all attributes to be acquired when no selection has been employed.

CurrentLin and CurrentPos are integers set by

Code: Pascal  [Select][+][-]
  1. procedure TCmdForm.ReportPosition;
  2. var CrsC,CrsL,EndL,Wrp,Pst,Lng,Total: string;
  3.     cx,cy,cp,cl,ct: integer;
  4. begin
  5.   cp:= SendMessage(PageMemo.Handle,EM_LINEFROMCHAR,PageMemo.SelStart,0);
  6.   cy:= SendMessage(PageMemo.Handle,EM_LINEINDEX,cp,0);
  7.   cl:= SendMessage(PageMemo.handle,EM_LINELENGTH,cy,0);
  8.   cx:= PageMemo.SelStart-cy;
  9.   ct:= PageMemo.Lines.Count;
  10.   CurrentLin:= cp+1;      //global integer
  11.   CurrentPos:= cx+1;     //global integer
  12.   CurrentEnd:= cl;          //global integer
  13.   str(cx+1,CrsC);
  14.   str(cp+1,CrsL);
  15.   str(cl,EndL);
  16.   str(ct,Total);
  17.   if PageMemo.WordWrap=true
  18.      then Wrp:='Wrap ON'
  19.      else Wrp:='Wrap OFF';
  20.   if Clipboard.HasFormat(CF_TEXT)
  21.      then Pst:='Paste ON'
  22.      else Pst:='Paste OFF';
  23.   if Clipboard.HasPictureFormat
  24.      then Pst:='Paste ON';
  25.   if EngOn then Lng:= 'ENGLISH';
  26.   if HebOn then Lng:= 'HEBREW';
  27.   if SyrOn then Lng:= 'SYRIAC';
  28.   if GrkOn then Lng:= 'GREEK';
  29.   if CptOn then Lng:= 'COPTIC';
  30.   if PhnOn then Lng:= 'PHOENICIAN';
  31.   StatusBar2.SimpleText:= ' Line '+CrsL+' of '+Total+'    •    Place '+CrsC+' of '+EndL
  32.                           +'    •    '+Wrp+'    •    '+Pst+'    •    Mode '+Lng
  33.                           +'    •    '+DateToStr(Now);
  34. end;  
  35.  

Its primary function is post status information to a StatusBar.


Rick
« Last Edit: November 29, 2016, 08:56:42 pm by rick2691 »
Windows 11, LAZ 2.0.10, FPC 3.2.0, SVN 63526, i386-win32-win32/win64, using windows unit

 

TinyPortal © 2005-2018