Recent

Author Topic: [SOLVED] RichMemo Bold/Italic Mechanism Q  (Read 3036 times)

ASBzone

  • Hero Member
  • *****
  • Posts: 678
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: RichMemo Bold/Italic Mechanism Q
« Reply #15 on: April 26, 2019, 01:26:59 am »
Got this to work.
Code: Pascal  [Select][+][-]
  1.  
  2.  if (fsBold in fp.Style) then
  3.      fp.Style := fp.Style - [fsBold]
  4.   else
  5.     fp.Style := [fsBold];
  6.  
  7.  txtRTF.SetTextAttributes(txtRTF.SelStart, txtRTF.SelLength, fp);
  8.  
  9.  

Now, let see if I can take italics into account now.

What about the following:

Code: Pascal  [Select][+][-]
  1.  
  2.  if (fsBold in fp.Style) then
  3.      fp.Style := fp.Style - [fsBold]
  4.   else
  5.     fp.Style := fp.Style + [fsBold];
  6.  
  7.  txtRTF.SetTextAttributes(txtRTF.SelStart, txtRTF.SelLength, fp);
  8.  
-ASB: https://www.BrainWaveCC.com/

Lazarus v2.2.7-ada7a90186 / FPC v3.2.3-706-gaadb53e72c
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: RichMemo Bold/Italic Mechanism Q
« Reply #16 on: April 26, 2019, 04:12:09 am »
What about the following:

Code: Pascal  [Select][+][-]
  1.  
  2.  if (fsBold in fp.Style) then
  3.      fp.Style := fp.Style - [fsBold]
  4.   else
  5.     fp.Style := fp.Style + [fsBold];
  6.  
  7.  txtRTF.SetTextAttributes(txtRTF.SelStart, txtRTF.SelLength, fp);
  8.  
don't.

The use of SetRangeParams is the most intuitive for implementing an editor.
One doesn't want just ASSIGN a style. But instead add or remove a particular style element, keeping all other styles intact.

Typically that means that the text range needs to be scanned for different styles and the adjustment to be applied properly.
But some system native API provide a convenient method to avoid the hardship of styles scan.

(this was a missing functionality in Delphi's TRichEdit. And that's why TRichMemo doesn't replicate  TRichEdit's API)

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: RichMemo Bold/Italic Mechanism Q
« Reply #17 on: April 26, 2019, 11:57:57 am »
Just to re-post...
I already found a solution that works....

This takes into consideration if text is Bold or italic or both.
It doesn't remove it if it is so.

BOLD BUTTON CODE

Code: Pascal  [Select][+][-]
  1.  
  2.  
  3. procedure TForm5.btnBoldClick(Sender: TObject);
  4. var
  5.  fp   :  TFontParams;
  6. begin
  7.  Caption := Format('sel start %d,  sel length %d', [txtRTF.SelStart, txtRTF.SelLength]);
  8.  txtRTF.GetTextAttributes(txtRTF.SelStart, fp);
  9.  
  10.    if (fsBold in fp.Style) then
  11.       txtRTF.SetRangeParams (txtRTF.SelStart, txtRTF.SelLength,[tmm_Styles, tmm_Color],
  12.       ''{font},0{fontsize},clBlack{color},[]{make bold/italic},[fsBold]{remove bold/italic})
  13.    else
  14.       txtRTF.SetRangeParams (txtRTF.SelStart, txtRTF.SelLength,[tmm_Styles, tmm_Color],
  15.       ''{font},0{fontsize},clBlack{color},[fsBold]{make bold/italic},[]{remove bold/italic});
  16.  
  17. end;
  18.  
  19.  


ITALIC BUTTON

Code: Pascal  [Select][+][-]
  1.  
  2.  
  3. procedure TForm5.btnBoldClick(Sender: TObject);
  4. var
  5.  fp   :  TFontParams;
  6. begin
  7.  Caption := Format('sel start %d,  sel length %d', [txtRTF.SelStart, txtRTF.SelLength]);
  8.  txtRTF.GetTextAttributes(txtRTF.SelStart, fp);
  9.  
  10.    if (fsBold in fp.Style) then
  11.       txtRTF.SetRangeParams (txtRTF.SelStart, txtRTF.SelLength,[tmm_Styles, tmm_Color],
  12.       ''{font},0{fontsize},clBlack{color},[]{make bold/italic},[fsItalic]{remove bold/italic})
  13.    else
  14.       txtRTF.SetRangeParams (txtRTF.SelStart, txtRTF.SelLength,[tmm_Styles, tmm_Color],
  15.       ''{font},0{fontsize},clBlack{color},[fsItalic]{make bold/italic},[]{remove bold/italic});
  16.  
  17. end;
  18.  
  19.  
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

 

TinyPortal © 2005-2018