Lazarus

Programming => Packages and Libraries => RichMemo => Topic started by: snorkel on August 18, 2014, 08:28:22 pm

Title: Anyone using lzrichedit or trichmemo for a chat type app?
Post by: snorkel on August 18, 2014, 08:28:22 pm
I am trying to convert some delphi code that does stuff like this:

re1.SelAttributes.Color:=clred;
re1.SelText:='word 1';   //word 1 is red
re1.SelAttributes.Color:=clnavy;
re1.SelText:='word 2'  //word 2 is navy

This does not work at all(it just puts in plain text instead of coloured) in LzRichedit.




Title: Re: Anyone using lzrichedit or trichmemo for a chat type app?
Post by: typo on August 18, 2014, 08:58:48 pm
SelAtrributes property is read only.

Code: [Select]
property SelAttributes: TTextAttributes read FSelAttributes;

RichBox.pas, line 126 on my installation.
Title: Re: Anyone using lzrichedit or trichmemo for a chat type app?
Post by: snorkel on August 18, 2014, 09:06:21 pm
it's not read only in LzRichEdit, trichmemo it is.
Title: Re: Anyone using lzrichedit or trichmemo for a chat type app?
Post by: typo on August 18, 2014, 09:09:08 pm
lzRichEdit.

My installation is not up to date.

So why it does not work?
Title: Re: Anyone using lzrichedit or trichmemo for a chat type app?
Post by: snorkel on August 18, 2014, 09:13:14 pm
lzRichEdit.

My installation is not up to date.

So why it does not work?

oops sorry you are correct it is read only but properties it contains are read/write like style and color.
Title: Re: Anyone using lzrichedit or trichmemo for a chat type app?
Post by: typo on August 18, 2014, 09:15:22 pm
Sure, it should not be so. Not implemented yet.
Title: Re: Anyone using lzrichedit or trichmemo for a chat type app?
Post by: snorkel on August 18, 2014, 09:19:45 pm
this works in the demo that's included with lzrichedit:

lzRichEdit1.SelAttributes.Color:=

Typo, are you the author of LzRichEdit?  Just wondering.
Title: Re: Anyone using lzrichedit or trichmemo for a chat type app?
Post by: typo on August 18, 2014, 09:23:00 pm
No.
Title: Re: Anyone using lzrichedit or trichmemo for a chat type app?
Post by: snorkel on August 18, 2014, 11:08:29 pm
The problem I am having with lzrichedit is setting the text attributes in code.
for example if a new chat message comes in i want the the datetime in say clred, the username in clnavy and bold etc
Title: Re: Anyone using lzrichedit or trichmemo for a chat type app?
Post by: typo on August 18, 2014, 11:50:44 pm
This works, don't ask why:

Code: [Select]
  re.SelText:='word 1';   //word 1 is red
  re.setfocus;
  re.selstart := re.selstart - length('word 1');
  re.SelLength := length('word 1');
  re.setfocus;
  re.SelAttributes.Color:=clred;
  re.sellength := 0;
  re.selstart := re.selstart + length('word 1');
Title: Re: Anyone using lzrichedit or trichmemo for a chat type app?
Post by: taazz on August 19, 2014, 12:39:28 am
how about setting the attribute after a valid selection is made, ee add the new text select it and then set the color or other attributes moving the caret to end of the selection after everything is done to avoid replacing the selected text in the next iteration.
Title: Re: Anyone using lzrichedit or trichmemo for a chat type app?
Post by: typo on August 19, 2014, 12:46:46 am
Code: [Select]
  // Insert the unformatted text
  re.SelText:='word 1';   //word 1 is red
  re.setfocus;

  // Select the inserted text
  re.selstart := re.selstart - length('word 1');
  re.SelLength := length('word 1');
  re.setfocus;

  // Set attributes
  re.SelAttributes.Color:=clred;

  //  Unselect
  re.sellength := 0;
  re.selstart := re.selstart + length('word 1');
Title: Re: Anyone using lzrichedit or trichmemo for a chat type app?
Post by: snorkel on August 19, 2014, 09:27:41 pm
I came up with this using trichmemo:

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
 fp:TFontParams;
 current_user:string;
begin
     current_user:='Joe User';
     send_memo.Lines.BeginUpdate;
     try
        //get the send text
        send_memo.SelectAll;
        send_memo.CutToClipboard;
        //insert time to receive_memo;
        receive_memo.selText:=FormatDateTime('(hh:mm:ss AM/PM) ',now);
        //set time color
        fp.Color:=clnavy;
        fp.Style:=[];
        receive_memo.SetTextAttributes(receive_memo.SelStart,receive_memo.SelLength,fp);
        receive_memo.SelStart:=receive_memo.GetTextLen;
        //Add username
        fp.Style:=[fsbold];
        receive_memo.SelText:=format('%s ',[current_user]);
        receive_memo.SetTextAttributes(receive_memo.SelStart,receive_memo.SelLength,fp);
        receive_memo.SelStart:=receive_memo.GetTextLen;
        //add the send text to receive memo
        receive_memo.PasteFromClipboard;
        receive_memo.Lines.Append('');
        receive_memo.Lines.Delete(receive_memo.Lines.Count-1);
        fp.Style:=[];
        fp.Color:=clDefault;
     finally
            send_memo.Lines.endUpdate;
     end;
end;
Title: Re: Anyone using lzrichedit or trichmemo for a chat type app?
Post by: snorkel on August 19, 2014, 10:29:06 pm
in case anyone is interested this works in trichmemo and probably lzrichedit in win32/win64

http://www.swissdelphicenter.ch/en/showcode.php?id=1147
Title: Re: Anyone using lzrichedit or trichmemo for a chat type app?
Post by: arneolav on November 19, 2014, 10:23:23 pm
In a translating system i need ... 

Editor.SelAttributes.BackColor := ...   Set background color of a selected text.
Title: Re: Anyone using lzrichedit or trichmemo for a chat type app?
Post by: typo on November 20, 2014, 01:36:43 am
Just added. You can download it from here:

http://wiki.freepascal.org/lzRichEdit#Download

Title: Re: Anyone using lzrichedit or trichmemo for a chat type app?
Post by: avra on February 27, 2015, 02:19:33 pm
If you need mixed coloring then this is an example that will add a new line with several different colored words to TRichMemo:

Example:
Black, Green, Blue, Red
Black, Green, Blue, Red
Black, Green, Blue, Red

Since none of forum examples worked for me, I found this working on Windows:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
    procedure AddColorStr(s: string; const col: TColor = clBlack; const NewLine: boolean = true);
    begin
      with RichMemo1 do
      begin
        if NewLine then
        begin
          Lines.Add('');
          Lines.Delete(Lines.Count - 1); // avoid double line spacing
        end;
 
        SelStart  := Length(Text);
        SelText   := s;
        SelLength := Length(s);
        SetRangeColor(SelStart, SelLength, col);
 
        // deselect inserted string and position cursor at the end of the text
        SelStart  := Length(Text);
        SelText   := '';
      end;
    end;
  begin
    AddColorStr('Black, ');
    AddColorStr('Green, ', clGReen, false);
    AddColorStr('Blue, ', clBlue, false);
    AddColorStr('Red', clRed, false);
  end;

Hopefully someone having the same problem will find it, or decide to extend TRichMemo with a method that appends colored text...  ;)

Here is step by step tutorial: http://lazplanet.blogspot.com/2013/12/create-rich-text-editor-for-yourself.html

UPDATE1: I have just updated the Wiki. It would be nice if someone could test if this works on any other OS.
UPDATE2: Two more examples added: http://wiki.freepascal.org/RichMemo#Append_mixed_color_text_at_the_end_of_the_RichMemo
UPDATE3: Fixed scrolling error by changing example. Added link to blog tutorial.
Title: Re: Anyone using lzrichedit or trichmemo for a chat type app?
Post by: Dibo on February 27, 2015, 04:17:07 pm
I'm using TIpHTMLPanel or THTMLPort for chat type app. You can then do whatever you want with text formatting and also add emoticons inside text
Title: Re: Anyone using lzrichedit or trichmemo for a chat type app?
Post by: eldonfsr on February 28, 2015, 05:24:00 am
I try to use richmemo for save in database but when i restore the rtc text the image gone only text show me i don't know what kind of image is support for richmemo i try jpg, also  i dont know is save rtf in sqlite works or not  bur for now i don't get its works for me.
Title: http://www.swissdelphicenter.ch/en/showcode.php?id=1147
Post by: eldonfsr on March 10, 2015, 04:40:53 am
This don't works on trichmemo i tested if you tested can you put your code pls.

Title: Re: Anyone using lzrichedit or trichmemo for a chat type app?
Post by: typo on March 10, 2015, 05:02:01 am
lzRichEdit example attached.
Title: Re: Anyone using lzrichedit or trichmemo for a chat type app?
Post by: JD on March 26, 2015, 05:46:21 pm
I'm using TIpHTMLPanel or THTMLPort for chat type app. You can then do whatever you want with text formatting and also add emoticons inside text

Hi there Dibo. Would you mind showing me how you did that?

Thanks,

JD
TinyPortal © 2005-2018