unit MChatForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, RVScroll, RichView, RVEdit, Menus, RichViewActions, PtblRV, RVStyle,
  RVFontCombos, StdCtrls, ComCtrls, ToolWin, Buttons, jpeg, ExtCtrls,
  SRVTypeItems, SRVControl, SRVButton;

type
  TframeChatEditor = class(TFrame)
    RVAControlPanel1: TRVAControlPanel;
    RVStyle1: TRVStyle;
    RVPrint1: TRVPrint;
    RVAPopupMenu1: TRVAPopupMenu;
    ColorDialog1: TColorDialog;
    Panel6: TPanel;
    Splitter1: TSplitter;
    Panel1: TPanel;
    Panel9: TPanel;
    ImgUser: TImage;
    ListMessages: TRichView;
    Panel10: TPanel;
    SpeedButton13: TSpeedButton;
    Panel11: TPanel;
    Panel12: TPanel;
    SpeedButton14: TSpeedButton;
    Panel13: TPanel;
    ImgMy: TImage;
    btnSendMode: TSpeedButton;
    Panel14: TPanel;
    Panel15: TPanel;
    CoolBar1: TCoolBar;
    ToolBar1: TToolBar;
    ToolButton17: TToolButton;
    ToolButton18: TToolButton;
    ToolButton19: TToolButton;
    ToolButton5: TToolButton;
    ToolButton20: TToolButton;
    ToolButton21: TToolButton;
    ToolButton10: TToolButton;
    ToolButton46: TToolButton;
    ToolButton47: TToolButton;
    ToolBar2: TToolBar;
    ToolButton41: TToolButton;
    ToolButton42: TToolButton;
    ToolButton43: TToolButton;
    ToolButton44: TToolButton;
    ToolButton29: TToolButton;
    ToolButton6: TToolButton;
    ToolButton30: TToolButton;
    ToolButton31: TToolButton;
    ToolButton35: TToolButton;
    ToolButton36: TToolButton;
    ToolButton38: TToolButton;
    TextMessage: TRichViewEdit;
    btnSend: TSRVButton;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    procedure SpeedButton13Click(Sender: TObject);
    procedure SpeedButton14Click(Sender: TObject);
    procedure btnSendClick(Sender: TObject);
    procedure TextMessageKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure SendChatMessage();
    function  GetHypertextStyleNo(StyleNo : Integer; color : TColor) : Integer;
  end;

implementation

uses dmActions, MFChat;

{$R *.dfm}

procedure TframeChatEditor.SpeedButton13Click(Sender: TObject);
begin
   Panel9.Visible := not Panel9.Visible;
   if (Panel9.Visible) then
     SpeedButton13.Caption := '>'
   else
     SpeedButton13.Caption := '<';
end;

procedure TframeChatEditor.SpeedButton14Click(Sender: TObject);
begin
   Panel13.Visible := not  Panel13.Visible;
   if (Panel13.Visible) then
     SpeedButton14.Caption := '>'
   else
     SpeedButton14.Caption := '<';
end;

procedure TframeChatEditor.btnSendClick(Sender: TObject);
begin
  SendChatMessage();
end;

procedure TframeChatEditor.TextMessageKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (Key = VK_RETURN) then
    if btnSendMode.Down and (ssCtrl in Shift) then begin
      btnSendClick(nil);
      Key := 0;
    end else if not btnSendMode.Down and not (ssCtrl in Shift)  then begin
      btnSendClick(nil);
      Key := 0;
    end;
end;

procedure TframeChatEditor.SendChatMessage();
var
     stream : TMemoryStream;
     ItemNo, OffItem, ItemIndex : Integer;
     TitleMsg, s : String;
begin
   stream := TMemoryStream.Create();
   ItemNo := TextMessage.ItemCount-1;
   OffItem := TextMessage.RVData.GetOffsAfterItem(ItemNo);
   TextMessage.SetSelectionBounds(ItemNo, OffItem, ItemNo, OffItem);
   TextMessage.SaveRVFToStream(stream, False);

   TitleMsg := MFChat.MyUserName + ' to ';

   ItemIndex := frmChat.TabChat.TabIndex;
   TitleMsg := TitleMsg + frmChat.TabChat.Tabs[ItemIndex].Text;

   Application.ProcessMessages();

   s := FormatDateTime('hh:mm', Time());
   TitleMsg := TitleMsg + ' (' + s + '):';

   stream.Position := 0;
   ListMessages.AddNLTag(TitleMsg, GetHypertextStyleNo(0, clBlue), 0, 0);
   ListMessages.InsertRVFFromStream(stream, ListMessages.ItemCount);
   ListMessages.Format();
   ListMessages.VScrollPos := ListMessages.VScrollMax;
   TextMessage.Clear();
   TextMessage.Format();

   stream.Free;
end;

//---------------------------------------------------------------------------
function  TframeChatEditor.GetHypertextStyleNo(StyleNo : Integer; color : TColor) : Integer;
var
     fi : TFontInfo;
begin
  fi := TFontInfo.Create(nil);
  fi.Assign(RVStyle1.TextStyles.Items[StyleNo]);
  fi.Size := 10;
  fi.Color := color;
  fi.Style := fi.Style + [fsUnderline, fsBold];
  fi.Jump  := true;
  result := RVStyle1.TextStyles.FindSuchStyle(StyleNo, fi, RVAllFontInfoProperties);
  if (result<0) then begin
    RVStyle1.TextStyles.Add();
    result := RVStyle1.TextStyles.Count-1;
    RVStyle1.TextStyles.Items[result].Assign(fi);
    RVStyle1.TextStyles.Items[result].Standard := false;
  end;
end;

end.
