Recent

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

ASBzone

  • Full Member
  • ***
  • Posts: 239
  • Automation leads to relaxation...
    • BrainWaveCC Utilities
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.0.5 r61666 / FPC v3.2.0-beta-r42593 (via FpcUpDeluxe) -- Windows 64-bit install w/32-bit cross-compile
Primary System: Windows 10 Pro x64, Version 1903 (Build 18362.295)
Other Systems: Windows 10 Pro x64, Version 1809 or greater

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2244
    • 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)
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

pixelink

  • Hero Member
  • *****
  • Posts: 1029
    • Pixelink Media
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!

Pixelink Media •  My LAZ Software
LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060