trichview.com

trichview.support.examples




Example: emotions icons


Return to index


Author

Message

Sergey Tkachenko

Posted: 03/14/2004 13:56:10


There is a demo in Demos\Delphi\Assorted\Graphics\Emoticons\ showing several

approaches for adding icons in chat windows

a) using search and replace

b) copying from plain text editor, replacing icons codes in plain text

c) copying from TRichViewEdit, replacing icons codes in RVF text.


This example shows another approach: icons codes are detected not at the

moment of copying but at the moment of typing.

You can add the code below in the last page of that demo (add all these

events to rve4)



1) OnKeyPress replaces ':)', ':|' and ':(' with bullets.

2) OnKeyDown. If there is no selection, and user presses Backspace key when

the caret is to the right of emotion icon, the icon is disassembled back to

its text code.


procedure TForm1.rve4KeyPress(Sender: TObject; var Key: Char);

var

  rve: TCustomRichViewEdit;

  ItemNo, Offs: Integer;

  s: String;


  function GetImageIndex(mouth: Char): Integer;

  begin

    case mouth of

      ')': Result := 0;

      '|': Result := 1;

      else Result := 2;

    end;

  end;


begin

  if not (Key in [')', '(', '|']) then

    exit;

  rve := (Sender as TCustomRichViewEdit).TopLevelEditor;

  ItemNo := rve.CurItemNo;

  if rve.GetItemStyle(ItemNo)<0 then

    exit;

  Offs := rve.OffsetInCurItem;

  s := rve.GetItemTextA(ItemNo);

  if (s='') or (Offs=1) then

    exit;

  if s[Offs-1]=':' then begin

    rve.SetSelectionBounds(ItemNo, Offs-1, ItemNo, Offs);

    rve.InsertBullet(GetImageIndex(Key), ImageList1);

    Key := #0;

  end;

end;


procedure TForm1.rve4KeyDown(Sender: TObject; var Key: Word;

  Shift: TShiftState);

var

  rve: TCustomRichViewEdit;

  ItemNo, Offs: Integer;


  function GetBulletImageIndex: Integer;

  var s: String;

      tag: Integer;

      il: TCustomImageList;

  begin

    rve.GetBulletInfo(ItemNo, s, Result, il, tag);

  end;


  function GetSmile(ImageIndex: Integer): String;

  begin

    case ImageIndex of

      0: Result := ':)';

      1: Result := ':|';

      else Result := ':(';

    end;

  end;


begin

  if Key<>VK_BACK then

    exit;

  rve := (Sender as TCustomRichViewEdit).TopLevelEditor;

  if rve.SelectionExists then

    exit;

  ItemNo := rve.CurItemNo;

  Offs := rve.OffsetInCurItem;

  if (rve.GetItemStyle(ItemNo)=rvsBullet) and (Offs=1) then begin

    Key := 0;

    rve.SetSelectionBounds(ItemNo, 0, ItemNo, 1);

    rve.InsertText(GetSmile(GetBulletImageIndex));

  end;

end;


3) You need to modify code for copying from rve4 to the chat window (rv3)


procedure TForm1.Button3Click(Sender: TObject);

var Stream: TMemoryStream;

begin

  Stream := TMemoryStream.Create;

  rve4.SaveRVFToStream(Stream, False);

  Stream.Position := 0;

  rv3.InsertRVFFromStream(Stream, rv3.ItemCount);

  Stream.Free;

  rv3.FormatTail;

end;


and process rv3.OnRVFImageListNeeded:


procedure TForm1.rv3RVFImageListNeeded(Sender: TCustomRichView;

  ImageListTag: Integer; var il: TCustomImageList);

begin

  il := ImageList1;

end;





Powered by ABC Amber Outlook Express Converter