Recent

Author Topic: TRichMemo example for attributes via Keystroke?  (Read 2971 times)

ozznixon

  • Jr. Member
  • **
  • Posts: 85
    • http://www.modernpascal.com/
TRichMemo example for attributes via Keystroke?
« on: June 25, 2018, 01:40:00 am »
I am able to highlight words, CTRL-B, and viola, Bold wording... however, if I Clear the RichMemo, Ctrl-B, and start typing - it is not bold. If I set RichMemo1.Font.Style += [fsBold] it is bold, when I disable it, everything I typed is unbold.

I copied and applied Rich2xxx's Set Attribute code, it does the same - selections change, but, to do on the fly from keyboard (or via buttons that contain the same code) nada.

Is there an example of using Keyboard and Buttons to toggle attributes where SelLength == 0?

Also, i have downloaded 3 different RTF2HTML pascal units, none work with TRichMemo's RTF output. Does anyone have a SaveToHTML and LoadFromHTML for TRichMemo? Working on an Email like client - and the other end is dCEF based - so I have to send/receive HTML from their product.

PS. My app runs on Linux and Mac, so I cannot use all these Windows tricks.  >:D

Regards,
Ozz
---
Want to kick the tires to a Free Pascal like script engine? http://www.ModernPascal.com/

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2282
    • havefunsoft.com
Re: TRichMemo example for attributes via Keystroke?
« Reply #1 on: June 25, 2018, 03:47:58 am »
If I set RichMemo1.Font.Style += [fsBold] it is bold, when I disable it, everything I typed is unbold.
don't. Assigning Font to richmemo changes the whole text to the specified font.
instead you could use code like this:
Code: Pascal  [Select]
  1. var
  2.   fn : TFontParams;
  3. begin
  4.   RichMemo1.GetTextAttributes(RichMemo1.SelStart, fn);
  5.   fn.Style:=fn.Style+[fsBold];
  6.   RichMemo1.SetTextAttributes(RichMemo1.SelStart, 0, fn);
  7. end;
  8.  

Working on an Email like client
RichMemo is not suitable for the task. The reason is simple - no support for embedding images.
« Last Edit: June 25, 2018, 04:17:48 am by skalogryz »
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

ozznixon

  • Jr. Member
  • **
  • Posts: 85
    • http://www.modernpascal.com/
Re: TRichMemo example for attributes via Keystroke?
« Reply #2 on: June 25, 2018, 03:43:41 pm »
If only it was that easy... I am on Linux, and currently this *almost* works perfectly for toggling bold on/off.

Code: Pascal  [Select]
  1. procedure TForm1.ECSpeedBtn2Change(Sender: TObject);
  2. var
  3.   TxtHrzBar, TxtVrtBar: integer;
  4.   SelFontFormat: TFontParams;
  5.  
  6. begin
  7.   TxtHrzBar := GetScrollPos(RichMemo1.handle, SB_HORZ);
  8.   TxtVrtBar := GetScrollPos(RichMemo1.handle, SB_VERT);
  9.   if RichMemo1.SelLength = 0 then
  10.   begin
  11.     RichMemo1.GetTextAttributes(RichMemo1.SelStart, SelFontFormat);
  12.     if (fsBold in SelFontFormat.Style = False) then
  13.       SelFontFormat.Style := SelFontFormat.Style + [fsBold]
  14.     else
  15.       SelFontFormat.Style := SelFontFormat.Style - [fsBold];
  16.     RichMemo1.SetTextAttributes(RichMemo1.SelStart, 0, SelFontFormat);
  17.   end
  18.   else
  19.   begin
  20.     ReportPosition;
  21.     if (CurrentLin = 1) and (CurrentPos = 1) then
  22.       RichMemo1.GetTextAttributes(RichMemo1.SelStart, SelFontFormat)   // get in place
  23.     else
  24.       RichMemo1.GetTextAttributes(RichMemo1.SelStart - 1, SelFontFormat);  // get from prior
  25.     if (fsBold in SelFontFormat.Style = False) then
  26.     begin // not in
  27.       RichMemo1.SetRangeParams(RichMemo1.SelStart,
  28.         //  int: begin position of selection, 0 sets start of file.
  29.         RichMemo1.SelLength,
  30.         //  int: length of selection set.
  31.         [tmm_Styles],
  32.         //  cst: modify types, [] sets to empty, or [tmm_Styles,tmm_Color]
  33.         '',
  34.         //  str: font name, '' sets to empty.
  35.         0,
  36.         //  int: font size, 0 sets to empty.
  37.         0,
  38.         //  int: color, 0 sets to empty.
  39.         [fsBold],
  40.         //  cst: adding styles, [] sets to empty, or [fsBold,fsItalic]
  41.         []
  42.         //  cst: remove styles, [] sets to empty, or [fsBold,fsItalic]
  43.         );
  44.     end
  45.     else
  46.     begin  // is in
  47.       RichMemo1.SetRangeParams(RichMemo1.SelStart,
  48.         //  int: begin position of selection, 0 sets start of file.
  49.         RichMemo1.SelLength,
  50.         //  int: length of selection characters, -1 sets end of file.
  51.         [tmm_Styles],
  52.         //  cst: modify types, [] sets to empty, or [tmm_Styles,tmm_Color]
  53.         '',
  54.         //  str: font name, '' sets to empty.
  55.         0,
  56.         //  int: font size, 0 sets to empty.
  57.         0,
  58.         //  int: color, 0 sets to empty.
  59.         [],
  60.         //  cst: adding styles, [] sets to empty, or [fsBold,fsItalic]
  61.         [
  62.         fsBold]             //  cst: remove styles, [] sets to empty, or [fsBold,fsItalic]
  63.         );
  64.     end;
  65.   end;
  66.   SendMessage(RichMemo1.handle, WM_HSCROLL,
  67.     MakeLong(SB_THUMBPOSITION, TxtHrzBar), 0);
  68.   SendMessage(RichMemo1.handle, WM_VSCROLL,
  69.     MakeLong(SB_THUMBPOSITION, TxtVrtBar), 0);
  70.   RichMemo1.SetFocus;
  71.   RichMemo1.Repaint;
  72. end;
  73.  
---
Want to kick the tires to a Free Pascal like script engine? http://www.ModernPascal.com/

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2282
    • havefunsoft.com
Re: TRichMemo example for attributes via Keystroke?
« Reply #3 on: June 26, 2018, 04:15:09 am »
Why do you need SetFocus, Repaint and Scrollers?
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

ozznixon

  • Jr. Member
  • **
  • Posts: 85
    • http://www.modernpascal.com/
Re: TRichMemo example for attributes via Keystroke?
« Reply #4 on: June 26, 2018, 06:54:50 pm »
Why do you need SetFocus, Repaint and Scrollers?

That was based form others posting, and merged in your fix.

Tried your fix standalone, does not work if I call it from OnKeyUp Ctrl-B and keep typing and Ctrl-B again. Now, if I move my cursor, and select and click BOLD button (calls pasted code) and keep typing and will become bold. (mimicked here).

Ozz
---
Want to kick the tires to a Free Pascal like script engine? http://www.ModernPascal.com/