trichview.com

trichview.support




Re: Inserting EmotionIcon while user is typing


Return to index


Author

Message

Sergey Tkachenko

Posted: 05/12/2005 11:42:20


For simplicity I assume you have array

Emoticons: array[0..20] of TEmoticon


TEmoticon = record

  Graphic: TGraphic;

  Shotcut, Descriton: String;

end;


First, create a set of the ending characters in shortcuts:


LastCharacters: set of char;


for i := 0 to 20 do

  Include(LastCharacters,

Emoticons[i].Shortcut[Length(Emoticons[i].Shortcut)]);



Processing OnKeyPress.

In this event, we detect if the user typed an emoticon's shortcut.

A special processing is for Key=#0.

Of course, this event is never called with this Key. But we will do it

ourself, to force emoticon detection.


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

var

  rve: TCustomRichViewEdit;

  ItemNo, Offs, Index: Integer;

  s: String;

begin

  // only do something if the last character in some emoticon code is

pressed

  // #0 is treated specially

  if not (Key in (LastCharacters+[#0])) then

    exit;

  // getting text before the caret

  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;

  s := Copy(s, 1, Offs-1);

  // if key<>#0, adding it to this text

  if Key<>#0 then

    s := s+Key;

  // checking, if there is an emoticon code at the end of it

  Index := GetEmoticonIndex(s);

  if Index<0 then

    exit;

  // if yes, selecting it and inserting the proper picture in its place

  rve.SetSelectionBounds(ItemNo, Offs-Length(Emoticons[Index].Shortcut),

ItemNo, Offs);

  rve.InsertPicture('', MakeEmoticonGraphic(Index), rvvaBaseline);

  Key := #0;

end;


We used 2 functions: GetEmoticonIndex and MakeEmoticonsGraphic.

Let's see how to implement them:


// if the string contains one of emoticons codes at its end, returns its

index.

// returns -1 otherwise

function TForm1.GetEmoticonIndex(const s: String): Integer;

var i, CodeLen: Integer;

begin

  for i := 0 to 20 do begin

    CodeLen := Length(Emoticons[i].Shortcut);

    if Length(s)< CodeLen then

      continue;

    if Copy(s, Length(s)-CodeLen+1, CodeLen)=Emoticons[i].Shortcut then

begin

      Result := i;

      exit;

    end;

  end;

  Result := -1;

end;


// makes graphic by the emoticon index

function TForm1.MakeEmoticonGraphic(Index: Integer): TGraphic;

begin

  Result :=

RV_CreateGraphics(TGraphicClass(Emoticons[Index].Graphic.ClassType));

  Result :=   Result.Assign(Emoticons[Index].Graphic);

end;


That's almost all.

One additional function - how to insert emoticon from code:


Option 1:

rveInput.InsertPicture('', MakeEmoticonGraphic(Index), rvvaBaseline);


Option 2:

var Key: Char;


rveInput.InsertText(Emoticons[i].Shortcut);

Key := #0;

rveInputKeyPress(rveInput, Key);




> Thaks Sergey but...

> How can i adapt that demo to support emotion icon shortcuts that can be

set

> by the user and have length more that 3 letters?

>

> In my case im storing emotion icons in a dataset with that structure, to

> understand how im building my application.

>

> Fields of dataset:

> ICON   TBlobField   //Gifs, Jpgs, etc

> SHORTCUT String(7)   //Shortcuts to emotion set by the user

> DESCRIPTION String(20)

>

> When user is typing a messange i do lookup in that dataset to get the icon

> if a word is equal to the shortcut.

>

> So any tips to that example?





Powered by ABC Amber Outlook Express Converter