Einzelnen Beitrag anzeigen

tinibeenie

Registriert seit: 5. Feb 2005
Ort: Barsbüttel
34 Beiträge
 
Delphi XE6 Professional
 
#1

Smileys in JvRichEdit einfügen

  Alt 27. Jun 2005, 17:48
Hallo!

Um Smileys in ein JvRichEdit zu bekommen, habe ich mich am JVCL-Messenger-Beispiel orientiert. Es klappt auch größtenteils, nur verstehe ich nicht, wie das mit der Integer-Variable "State" funktioniert. Es ist doch nirgendwo definiert, was State=1,10,20 usw. ist, also woher weiß das Program, dass wenn z.B. State=1 ist, der ':' gemeint ist. Es steht ja nur im Kommentar, aber nirgendwo als "richtiger Code". Ist das irgendwie schon vordefiniert oder so?

Es wär toll, wenn ihr mir helfen könntet. Das ist die dazugehörige Prozedur:

Delphi-Quellcode:
procedure TMessengerMainForm.ParseString(const S: string);
var
  P, Q: PChar;
  State: Integer;
  procedure AddImage(const SmileIndex, SmileLength: Integer);
  begin
    AddTextToRichEdit(Q, P - Q - SmileLength + 1, FCurrentFont);
    AddImageToRichEdit(SmileIndex);
    State := 0;
    Q := P + 1;
  end;
begin
  P := PChar(S);
  Q := P;
  State := 0;

  // State = 1.. then looking at ":-A" with A = @x)( etc.
  // 10.. "LOL"
  // 20.. "O :-)"
  // 30.. "(blush)"
  // 40.. ":'-("
  // 50.. "8-)"
  // 60.. ";-)"
  // 70.. "readonly"
  // 80.. "read-only on" or "read-only off"
  //
  // State = 1 -> ":" read
  // State = 2 -> ":-" read
  // State = 11 -> "LO" read
  // State = 22 -> "O :" read
  // State = 23 -> "O :-" read
  //
  // etc.

  while P^ <> #0 do
  begin
    case P^ of
      '$':
        if State in [2, 23] then
          AddImage(9, 3) // :-$
        else
          State := 0;
      '&':
        if State in [2, 23] then
          AddImage(4, 3) // :-&
        else
          State := 0;
      '(':
        case State of
          1, 22: AddImage(5, 2); // :(
          2, 23: AddImage(5, 3); // :-(
          40: AddImage(2, 3); // :,(
          41: AddImage(2, 4); // :'-(
        else
          State := 30;
        end;
      ')':
        case State of
          1, 22: AddImage(11, 2); // :)
          2: AddImage(11, 3); // :-)
          23: AddImage(6, 5); // O :-)
          35: AddImage(3, 7); // (blush)
          51: AddImage(1, 3); // 8-)
          61: AddImage(15, 3); // ;-)
        else
          State := 0;
        end;
      '*':
        if State = 1 then
          AddImage(7, 2) // :*
        else
          State := 0;
      ',':
        if State in [1, 22] then
          State := 40
        else
          State := 0;
      '-':
        case State of
          1, 22, 40, 50, 60: Inc(State);
          73: State := 80;
        else
          State := 0;
        end;
      '8': State := 50;
      '"': State := -1; // to prevent "read.." etc will be triggered by the program
      ':':
        if State = 21 then
          State := 22
        else
          State := 1;
      ';': State := 60;
      '@':
        case State of
          1, 22: AddImage(0, 2); // :@
          2, 23: AddImage(0, 3) // :-@
        else
          State := 0;
        end;
      'D':
        case State of
          1, 22: AddImage(8, 2); // :D = LOL
          2, 23: AddImage(8, 3) // :-D = LOL
        else
          State := 0;
        end;
      'L':
        if State = 11 then
          AddImage(8, 3) // LOL
        else
          State := 10;
      'O':
        if State = 10 then
          State := 11
        else
          State := 20;
      '\', '/':
        if State in [2, 23] then
          AddImage(14, 3) // :-\
        else
          State := 0;
      'a':
        if State = 71 then
          State := 72
        else
          State := 0;
      'b':
        if State = 30 then
          State := 31
        else
          State := 0;
      'd':
        if State = 72 then
          State := 73
        else
          State := 0;
      'e':
        if State = 70 then
          State := 71
        else
          State := 0;
      'f':
        case State of
          86: State := 87;
          87: HandleReadOnlyOff;
        else
          State := 0;
        end;
      'h':
        if State = 34 then
          State := 35
        else
          State := 0;
      'l':
        if State in [31, 75, 82] then
          Inc(State)
        else
          State := 0;
      'n':
        case State of
          74, 81: Inc(State);
          86: HandleReadOnlyOn;
        else
          State := 0;
        end;
      'o':
        case State of
          2, 23: AddImage(12, 3); // :-o
          73, 80, 85: Inc(State);
        else
          State := 0;
        end;
      'p':
        if State in [2, 23] then
          AddImage(13, 3) // :-p
        else
          State := 0;
      'r':
        if State <> -1 then
          State := 70
        else
          State := 0;
      's':
        if State = 33 then
          State := 34
        else
          State := 0;
      'u':
        if State = 32 then
          State := 33
        else
          State := 0;
      'x', 'X':
        if State in [2, 23] then
          AddImage(10, 3) // :-x
        else
          State := 0;
      'y':
        case State of
          76: HandleReadOnly;
          83: State := 84;
        else
          State := 0;
        end;
      ' ':
        case State of
          20, 84: Inc(State);
          11: State := 21;
        else
          State := 0;
        end;
    end;
    Inc(P);
  end;

  if Q < P then
    AddTextToRichEdit(Q, P - Q, FCurrentFont);
end;
  Mit Zitat antworten Zitat