unit ViewMail;

interface

uses
  commctrl,
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Variants,
  StdCtrls, PMComboBox, ExtCtrls, OleCtrls, DHTMLEDLib_TLB, MSHTML_TLB,RxCombos,
  ComCtrls, ToolWin,ActiveX,ShellAPI, Menus, TB97, TB97Tlbr, TB97Ctls,FileCtrl,
  ActnList,PXStuff, ImgList,SaveAttach;

type
  TGetInputArg = procedure(Sender : TObject; var pVarIn : OleVariant; var FuncResult : boolean) of object;
  TClearInputArg = procedure(Sender : TObject; var pVarIn : OleVariant) of object;
  TViewMailFrm = class(TForm)
    Panel1: TPanel;
    Label3: TLabel;
    Label2: TLabel;
    Label1: TLabel;
    fldFromHeader: TPMComboBox;
    fldAddresses: TPMComboBox;
    fldSubject: TPMComboBox;
    StandardToolBar: TToolBar;
    ToolButton11: TToolButton;
    TEFontNameBox: TComboBox;
    ToolButton2: TToolButton;
    BoldBtn: TToolButton;
    ItalicBtn: TToolButton;
    UnderlineBtn: TToolButton;
    ToolButton16: TToolButton;
    LeftAlign: TToolButton;
    CenterAlign: TToolButton;
    RightAlign: TToolButton;
    ToolButton20: TToolButton;
    ColorComboBox1: TColorComboBox;
    TEFontSizeBox: TComboBox;
    AttachPnl: TPanel;
    Label4: TLabel;
    ListView3: TListView;
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    menSave: TMenuItem;
    CancelOnlineOperation1: TMenuItem;
    N20: TMenuItem;
    menPrint: TMenuItem;
    menMessage: TMenuItem;
    Help1: TMenuItem;
    Edit2: TMenuItem;
    menPaste: TMenuItem;
    menCopy: TMenuItem;
    menCut: TMenuItem;
    menSelectAll: TMenuItem;
    menUndo: TMenuItem;
    N3: TMenuItem;
    N8: TMenuItem;
    N9: TMenuItem;
    PnlHtml: TPanel;
    OpenDialog1: TOpenDialog;
    StatusBar1: TStatusBar;
    menSaveAttach: TMenuItem;
    menMoveTo: TMenuItem;
    N6: TMenuItem;
    menDelete: TMenuItem;
    N15: TMenuItem;
    N16: TMenuItem;
    N17: TMenuItem;
    PopupMenu3: TPopupMenu;
    open1: TMenuItem;
    saveall1: TMenuItem;
    add1: TMenuItem;
    delete1: TMenuItem;
    N21: TMenuItem;
    N22: TMenuItem;
    saveas1: TMenuItem;
    N26: TMenuItem;
    N27: TMenuItem;
    N18: TMenuItem;
    menFind: TMenuItem;
    N1: TMenuItem;
    N2: TMenuItem;
    N10: TMenuItem;
    N14: TMenuItem;
    N19: TMenuItem;
    N23: TMenuItem;
    N29: TMenuItem;
    menSendMail: TMenuItem;
    N32: TMenuItem;
    menSendLater: TMenuItem;
    ShowPhoenixMail1: TMenuItem;
    N33: TMenuItem;
    N34: TMenuItem;
    ImageListCanEdit: TImageList;
    ImageListCannotEdit: TImageList;
    Panel2: TPanel;
    Toolbar1: TToolBar;
    ToolbarButton9730: TToolButton;
    ToolbarButton976: TToolButton;
    ToolbarButton9736: TToolButton;
    ToolButton10: TToolButton;
    ToolbarButton972: TToolButton;
    ToolbarButton974: TToolButton;
    ToolButton12: TToolButton;
    ToolbarButton977: TToolButton;
    ToolbarButton9740: TToolButton;
    ToolButton13: TToolButton;
    ToolbarButton973: TToolButton;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton8: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    ToolButton9: TToolButton;
    ToolButton7: TToolButton;
    Bevel1: TBevel;
    menSetPriority: TMenuItem;
    menHigh: TMenuItem;
    menNormal: TMenuItem;
    menLow: TMenuItem;
    menRequest: TMenuItem;
    ToolButton14: TToolButton;
    N7: TPopupMenu;
    PopMenuHigh: TMenuItem;
    PopMenuNormal: TMenuItem;
    PopMenuLow: TMenuItem;
    DHTMLEdit1: TDHTMLEdit;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);

    procedure DHTMLEdit1DisplayChanged(Sender: TObject);
    procedure DHTMLEdit1DocumentComplete(Sender: TObject);
    procedure BoldBtnClick(Sender: TObject);
    procedure ItalicBtnClick(Sender: TObject);
    procedure UnderlineBtnClick(Sender: TObject);
    procedure LeftAlignClick(Sender: TObject);
    procedure CenterAlignClick(Sender: TObject);
    procedure RightAlignClick(Sender: TObject);
    procedure ColorComboBox1Change(Sender: TObject);
    procedure TEFontNameBoxChange(Sender: TObject);
    procedure TEFontSizeBoxChange(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ListView3DblClick(Sender: TObject);
    procedure ListView3KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure ListView3KeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure ListView3MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure menUndoClick(Sender: TObject);
    procedure menSelectAllClick(Sender: TObject);
    procedure menCutClick(Sender: TObject);
    procedure menCopyClick(Sender: TObject);
    procedure menPasteClick(Sender: TObject);
    procedure ToolbarButton9737Click(Sender: TObject);
    procedure menSaveClick(Sender: TObject);
    procedure fldFromHeaderChange(Sender: TObject);
    procedure ToolbarButton9734Click(Sender: TObject);
    procedure ToolbarButton9733Click(Sender: TObject);
    procedure ToolbarButton9735Click(Sender: TObject);
    procedure ToolbarButton9728Click(Sender: TObject);
    procedure ToolbarButton9738Click(Sender: TObject);
    procedure ToolbarButton9731Click(Sender: TObject);
    procedure ToolbarButton971Click(Sender: TObject);
    procedure ToolbarButton974Click(Sender: TObject);
    procedure ToolbarButton9730Click(Sender: TObject);
    procedure ToolbarButton976Click(Sender: TObject);
    procedure ToolbarButton9736Click(Sender: TObject);
    procedure ToolbarButton977Click(Sender: TObject);
    procedure ToolbarButton9740Click(Sender: TObject);
    procedure ToolbarButton973Click(Sender: TObject);
    procedure CancelOnlineOperation1Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure actMessageAttOpenExecute(Sender: TObject);
    procedure actMessageAttOpenUpdate(Sender: TObject);
    procedure actMessageAttDeleteExecute(Sender: TObject);
    procedure actMessageAttDeleteUpdate(Sender: TObject);
    procedure open1Click(Sender: TObject);
    procedure PopupMenu3Popup(Sender: TObject);
    procedure saveall1Click(Sender: TObject);
    procedure delete1Click(Sender: TObject);
    procedure saveas1Click(Sender: TObject);
    procedure menMoveToClick(Sender: TObject);
    procedure N6Click(Sender: TObject);
    procedure N17Click(Sender: TObject);
    procedure menPrintClick(Sender: TObject);
    procedure ToolbarButton972Click(Sender: TObject);
    procedure menFindClick(Sender: TObject);
    procedure New3Click(Sender: TObject);
    procedure ToolbarButton975Click(Sender: TObject);
    procedure N10Click(Sender: TObject);
    procedure menSendLaterClick(Sender: TObject);
    procedure ShowPhoenixMail1Click(Sender: TObject);
    procedure N34Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure ListView3Change(Sender: TObject; Item: TListItem;
      Change: TItemChange);
    procedure FormResize(Sender: TObject);
    procedure menNormalClick(Sender: TObject);
    procedure menHighClick(Sender: TObject);
    procedure menLowClick(Sender: TObject);
    procedure menRequestClick(Sender: TObject);
    procedure menSendMailClick(Sender: TObject);
    procedure PopMenuHighClick(Sender: TObject);
    procedure PopMenuNormalClick(Sender: TObject);
    procedure PopMenuLowClick(Sender: TObject);
  private
    { Private declarations }
    DragDropDefWndProc, DragDropWndProcInstance: Pointer;
    bSendLater:Boolean;
    DocComplete : boolean;
    DOMInterface : IHtmlDocument2;
    CommandTarget : IOleCommandTarget;

    procedure UpdateViewMail;
    procedure DragDropWndProc(var Message: TMessage);
    procedure DragDropFiles(hDropHandle: HDrop);
    procedure WMTaskIcon(var Message: TMessage); message WM_TASKICON;
    procedure WMSysCommand(var Message: TMessage); message WM_SYSCOMMAND;
  public
    HeaderChanged : Boolean;


    procedure SaveAttach;
    procedure AddAttachment(aFilename: string);
    procedure DoSaveMessage;
    procedure ExecCommand(cmdID, cmdExecOpt, pVar: OleVariant);
    function QueryStatus(cmdID: OleVariant): OleVariant;
    function HrExecCommand(ucmdID : cardinal; const pVarIn : OleVariant;
                              var pVarOut : OleVariant; bPromptUser : boolean;
                              bTriEditCmdGroup : boolean) : HResult;
    procedure GetAvailableStyles;
    function TridentCommand(Cmd : Cardinal; hasInputArg : boolean; promptUser : boolean;
                                 editModeOnly : boolean; bTriEditCommandGroup : boolean;
                                 getInputArg : TGetInputArg; clearInputArg : TClearInputArg) : HRESULT;

    function GetCommandStatus(ucmdID : Cardinal; bTriEditCmdGroup : boolean) : DWORD;
    procedure GetTridentCommandState(cmdID : Cardinal; bTrieditCommandGroup : boolean;
               var bEnabled, bChecked : boolean);

    { Public declarations }
  end;

var
  ViewMailFrm: TViewMailFrm;

implementation

uses FMAddress, Main, ParserSup, FMMailOptions, MailParser,
  hedform, IEConst, DropSource, FMOnline, FMTemplate;
var
     NilVariant : OleVariant;

{$R *.DFM}

procedure TViewMailFrm.FormCreate(Sender: TObject);
var
  FAutoCompleteList : TStringList;
  si: TSHFILEINFO;
  ImageListHandle: THandle;
begin
  ImageListHandle := SHGetFileInfo('C:\',
                           0,
                           SI,
                           SizeOf(SI),
                           SHGFI_SYSICONINDEX or SHGFI_SMALLICON);
  SendMessage(ListView3.Handle, LVM_SETIMAGELIST, LVSIL_SMALL, ImageListHandle);
  FAutoCompleteList := TStringList.create;
  FAutoCompleteList.Assign(MailOptionsForm.MRU1.Items);
  FAutoCompleteList.Sort;
  fldAddresses.Items.Assign(FAutoCompleteList);
  fldAddresses.SelStart:=0;
  FAutoCompleteList.free;

  TEFontNameBox.Items.Assign(Screen.Fonts);
end;

procedure TViewMailFrm.FormShow(Sender: TObject);
begin
  UpdateViewMail;
  SaveMessage:=false;
end;




procedure TViewMailFrm.DHTMLEdit1DisplayChanged(Sender: TObject);
var
   FontNameStatus : DWORD;
   FontSizeStatus : DWORD;
   StyleNameStatus : DWORD;
   ColorNameStatus : DWORD;
   vo : OleVariant;
begin
     BoldBtn.Down := ((QueryStatus(DECMD_BOLD) and DECMDF_LATCHED) = DECMDF_LATCHED);
     ItalicBtn.Down := ((QueryStatus(DECMD_ITALIC) and DECMDF_LATCHED) = DECMDF_LATCHED);
     UnderlineBtn.Down := ((QueryStatus(DECMD_UNDERLINE) and DECMDF_LATCHED) = DECMDF_LATCHED);
     LeftAlign.Down :=((QueryStatus(DECMD_JUSTIFYLEFT) and DECMDF_LATCHED) = DECMDF_LATCHED);
     RightAlign.Down :=((QueryStatus(DECMD_JUSTIFYRIGHT) and DECMDF_LATCHED) = DECMDF_LATCHED);
     CenterAlign.Down :=((QueryStatus(DECMD_JUSTIFYCENTER) and DECMDF_LATCHED) = DECMDF_LATCHED);
     FontNameStatus := GetCommandStatus(IDM_FONTNAME, False);
     FontSizeStatus := GetCommandStatus(IDM_FONTSIZE, False);
     ColorNameStatus:= GetCommandStatus(IDM_FORECOLOR, False);


     if ((FontNameStatus and OleCmdf_Enabled) <> 0) then begin
           HrExecCommand(IDM_FONTNAME, NilVariant, vo, false, false);
        if (VarType(vo) = VarOleStr) then
           TEFontNameBox.ItemIndex := TEFontNameBox.Items.IndexOf(Vo);
     end;
     VarClear(vo);

     if ((FontSizeStatus and OleCmdf_Enabled) <> 0) then begin
        HrExecCommand(IDM_FONTSIZE, NilVariant, vo, false, false);
        if (VarType(vo) = VarInteger) then
           TEFontSizeBox.ItemIndex := TEFontSizeBox.Items.IndexOf(vo);
     end;
     varClear(vo);


     if ((ColorNameStatus and OleCmdf_Enabled) <> 0) then begin
          HrExecCommand(IDM_FORECOLOR, NilVariant, vo, false, false);
        if (VarType(vo) = VarInteger) then
           ColorComboBox1.ItemIndex := ColorComboBox1.Items.IndexOfObject(POINTER(TCOLOR(vo)));
     end;
     varClear(vo);




     StyleNameStatus := GetCommandStatus(IDM_BLOCKFMT, False);
     if ((StyleNameStatus and OleCmdf_Enabled) <> 0) then begin
        TVariantArg(vo).vt := vt_bstr;
        TVariantArg(vo).bStrVal := nil;
        HrExecCommand(IDM_BLOCKFMT, NilVariant, vo, false, false);
        if (VarType(vo) = VarOleStr) then
//           TEStylesBox.ItemIndex := TEStylesBox.Items.IndexOf(vo);
     end;
     varClear(vo);
end;

procedure TViewMailFrm.DHTMLEdit1DocumentComplete(Sender: TObject);
begin
     DocComplete := True;
     DOMInterface := DHTMLEdit1.Dom as IHtmlDocument2;
     CommandTarget := DOMInterface as IOleCommandTarget;
end;

procedure TViewMailFrm.BoldBtnClick(Sender: TObject);
begin
  TridentCommand(IDM_BOLD, false, false, true, false, Nil, Nil);
end;

procedure TViewMailFrm.ItalicBtnClick(Sender: TObject);
begin
  ExecCommand(DECMD_Italic, OLECMDEXECOPT_DONTPROMPTUSER, Null);
end;

procedure TViewMailFrm.UnderlineBtnClick(Sender: TObject);
begin
       ExecCommand(DECMD_Underline, OLECMDEXECOPT_DONTPROMPTUSER, Null);
end;

procedure TViewMailFrm.LeftAlignClick(Sender: TObject);
begin
  TridentCommand(IDM_JUSTIFYLEFT, false, false, true, false, Nil, Nil);
end;

procedure TViewMailFrm.CenterAlignClick(Sender: TObject);
begin
  TridentCommand(IDM_JUSTIFYCENTER, false, false, true, false, Nil, Nil);
end;

procedure TViewMailFrm.RightAlignClick(Sender: TObject);
begin
  TridentCommand(IDM_JUSTIFYRIGHT, false, false, true, false, Nil, Nil);
end;

procedure TViewMailFrm.ColorComboBox1Change(Sender: TObject);
begin
  ExecCommand(DECMD_SETFORECOLOR, OLECMDEXECOPT_DONTPROMPTUSER, ColorComboBox1.ColorValue);
end;

procedure TViewMailFrm.TEFontNameBoxChange(Sender: TObject);
var
   vi, vo : OleVariant;
begin
  vi := TEFontNameBox.Text;
  HrExecCommand(IDM_FONTNAME, vi, vo, false, false);
end;

procedure TViewMailFrm.ExecCommand(cmdID, cmdExecOpt, pVar: OleVariant);
begin
   if (DocComplete) then
      if ((QueryStatus(CmdID) and DECMDF_ENABLED) = DECMDF_ENABLED) then
         DHTMLEdit1.ExecCommand(cmdID, cmdExecOpt, pVar);

end;

procedure TViewMailFrm.GetAvailableStyles;
var
   varRange : OleVariant;
   b : TBStr;
   a : PSafeArray;
   l, h, i : longint;
   hr : HRESULT;
begin
   TVariantArg(VarRange).VT := VT_ARRAY; // VarArray or VarOleStr;
   TVariantArg(VarRange).ppArray := nil;
   hr := HrExecCommand(IDM_GETBLOCKFMTS, NilVariant, VarRange, false, false);
   // OleCheck(hr);
   if (hr = 0) then begin
      l := VarArrayLowBound(VarRange, 1);
      h := VarArrayHighBound(VarRange, 1);
      a := TVariantArg(VarRange).pArray;
      for i := l to h do begin
         SafeArrayGetElement(a, i, b);
//         TEStylesBox.Items.Add(OleStrToString(b));
      end;
   end;
end;



function TViewMailFrm.GetCommandStatus(ucmdID: Cardinal;
  bTriEditCmdGroup: boolean): DWORD;
var
   FMsoCmd : TOleCmd;
begin
   result := 0;
   if (Not Assigned(CommandTarget)) then
      Exit;
   FMsoCmd.CmdID := ucmdID;
   FMsoCmd.cmdf := 0;
   if (bTriEditCmdGroup) then begin
      CommandTarget.QueryStatus(@GUID_TriEditCommandGroup, 1, @FMsoCmd, nil);
   end else begin
      CommandTarget.QueryStatus(@CMDSETID_Forms3, 1, @FMsoCmd, nil);
   end;
   result := FMsoCmd.cmdf;
end;

procedure TViewMailFrm.GetTridentCommandState(cmdID: Cardinal;
  bTrieditCommandGroup: boolean; var bEnabled, bChecked: boolean);
var
   dwStatus : DWORD;
begin
   dwStatus := GetCommandStatus(cmdID, bTriEditCommandGroup);
   bEnabled := ((dwStatus and OLECMDF_ENABLED) <> 0);
   bChecked := ((dwStatus and OLECMDF_LATCHED) <> 0);
end;

function TViewMailFrm.HrExecCommand(ucmdID: cardinal; const pVarIn: OleVariant;
  var pVarOut: OleVariant; bPromptUser,
  bTriEditCmdGroup: boolean): HResult;
var
   dwCmdOpt : DWORD;
begin
   result := S_OK;
   if (Not Assigned(CommandTarget)) then
      Exit;
   if (bPromptUser) then
      dwCmdOpt := MSOCMDEXECOPT_PROMPTUSER
   else
      dwCmdOpt := MSOCMDEXECOPT_DONTPROMPTUSER;

   if (bTriEditCmdGroup) then begin
      result := CommandTarget.Exec(@GUID_TriEditCommandGroup, 
							ucmdID,
							dwCmdOpt,
							pVarIn,
							pVarOut);
   end else begin
      result := CommandTarget.Exec(@CMDSETID_Forms3,
							ucmdID,
							dwCmdOpt,
							pVarIn,
							pVarOut);
   end;
end;

function TViewMailFrm.QueryStatus(cmdID: OleVariant): OleVariant;
begin
   Result := DHTMLEdit1.QueryStatus(cmdID);
end;

function TViewMailFrm.TridentCommand(Cmd: Cardinal; hasInputArg, promptUser,
  editModeOnly, bTriEditCommandGroup: boolean; getInputArg: TGetInputArg;
  clearInputArg: TClearInputArg): HRESULT;
var
   pVarIn : OleVariant;
   bContinue : boolean;
begin
    result := S_OK;
    if (hasInputArg) then begin
       getInputArg(Self, pVarIn, bContinue);
       if (not bContinue) then
          exit;
       result := HrExecCommand(Cmd, pVarIn, nilVariant, promptUser, bTriEditCommandGroup)
    end else begin
       result := HrExecCommand(Cmd, nilVariant, nilVariant, promptUser, bTriEditCommandGroup);
    end;
    if (HasInputArg and Assigned(ClearInputArg)) then
       ClearInputArg(Self, pVarIn);
end;

procedure TViewMailFrm.TEFontSizeBoxChange(Sender: TObject);
var
   vi, vo : OleVariant;
begin
//  if (TEIgnoreChange) then
//     exit;
  vi := TEFontSizeBox.ItemIndex + 1;
  HrExecCommand(IDM_FONTSIZE, vi, vo, false, false);

end;

procedure TViewMailFrm.Button1Click(Sender: TObject);
var
  htmlStr:string;
begin
//  DHTMLEdit1.LoadDocument(sBackGround);

//htmlStr:='d:\3D.jpg';
//DHTMLEdit1.DOM.body.setAttribute('background',htmlStr,0);
//htmlStr:=sBackGround;
DHTMLEdit1.DOM.body.setAttribute('background',sBackGround,0);

end;

procedure TViewMailFrm.Button2Click(Sender: TObject);
begin
  fldFromHeader.SelStart:=0;
end;

procedure TViewMailFrm.ListView3DblClick(Sender: TObject);
begin
  PopupMenu3.Items[iDoubleClickAction].Action.Execute;
end;

procedure TViewMailFrm.ListView3KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Ord(Key) = 13 then
     PopupMenu3.Items[iDoubleClickAction].Action.Execute;

end;

procedure TViewMailFrm.ListView3KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  i: integer;
begin
  if Ord(Key) = 46 then
    actMessageAttDeleteExecute(Self)
  else if (Shift = [ssCtrl]) and (Ord(Key) = 65) then begin
    with ListView3 do
      for i := 0 to Items.Count - 1 do
        Items[i].Selected := true;
  end;
end;

procedure TViewMailFrm.ListView3MouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
var
  Result: HRESULT;
  OKEffect: LongInt;
  Effect: LongInt;
  DropSource: TDropFileSource;
  i: integer;
  Filename: string;
begin
  if ([ssLeft, ssRight] * Shift = []) or (ListView3.SelCount = 0) then
    exit;

  DropSource := TDropFileSource.Create;
  try
    for i := 0 to ListView3.Items.Count - 1 do
      if (ListView3.Items[I].Selected) then begin
        Filename := sTemporaryFolder + ListView3.Items[I].Caption;
        while (Pos('\\', FileName) > 0) do
          Delete(FileName, Pos('\\', FileName), 1);
        DropSource.Files.Add(Filename);
      end;
    OKEffect := DROPEFFECT_NONE;
    //if (CheckBoxCopy.Checked) then
    OKEffect := OKEffect or DROPEFFECT_COPY;
    //if (CheckBoxMove.Checked) then
    //  OKEffect := OKEffect OR DROPEFFECT_MOVE;
    //if (CheckBoxLink.Checked) then
    //  OKEffect := OKEffect OR DROPEFFECT_LINK;

    Result := DoDragDrop(DropSource as IDataObject, DropSource as IDropSource,
      OKEffect, Effect);
  except
    DropSource.Free;
    raise;
  end;

  case Result of
    DRAGDROP_S_DROP:
      ; //ShowMessage('Drop OK');
    DRAGDROP_S_CANCEL:
      ShowMessage('Drop cancel');
    E_OUTOFMEMORY:
      ShowMessage('Drop out of memory');
    //$800703E6: // E_UNSPEC:
    //  ShowMessage('Drop unexpected');
  else
    ShowMessage('Drop unknown:' + IntToHex(Result, 8));
  end;
end;




procedure TViewMailFrm.FormClose(Sender: TObject;
  var Action: TCloseAction);
var
  iResult:integer;
begin

  if (SaveMessage or DHTMLEdit1.IsDirty) and bEdit then
  begin
    with Application do
    begin
      iResult:=MessageBox('要保存对此邮件所做的修改吗？', Pchar(Application.Title), MB_YESNOCANCEL or MB_ICONEXCLAMATION);
      case  iResult of
      7:close;
      2:Action:=caNone;
      else
      begin
        DHTMLEdit1.DOM.charset:= 'gb2312';
        DoSaveMessage;
      end;
    end;
  end;
  bNewDoc:=false;
  bEdit:=true;
  sBackGround:='';
end;

end;
procedure TViewMailFrm.menUndoClick(Sender: TObject);
begin
     ExecCommand(DECMD_UNDO, OLECMDEXECOPT_DODEFAULT, Null);
end;

procedure TViewMailFrm.menSelectAllClick(Sender: TObject);
begin
  TridentCommand(IDM_SELECTALL, false, false, true, false, Nil, Nil);
end;

procedure TViewMailFrm.menCutClick(Sender: TObject);
begin
   TridentCommand(IDM_CUT, false, false, false, false, nil, nil);
end;

procedure TViewMailFrm.menCopyClick(Sender: TObject);
begin
  ExecCommand(DECMD_COPY, OLECMDEXECOPT_DODEFAULT, Null);
end;

procedure TViewMailFrm.menPasteClick(Sender: TObject);
begin
     ExecCommand(DECMD_PASTE, OLECMDEXECOPT_DODEFAULT, Null);
end;

procedure TViewMailFrm.ToolbarButton9737Click(Sender: TObject);
var op, nf : string;
    Item : TListItem;
begin
  if MainForm.ListView1.SelCount > 0 then
  begin
    if SaveMessage then DoSaveMessage;
    Item := MainForm.ListView1.Selected;
    while Item <> nil do
    begin
      op := MakeValidDirName(ExtractFilePath(Item.SubItems[ciFileName]));
      if Item.ImageIndex <> 5 then
      begin
        nf := ChangeFileExt(ExtractFileName(Item.SubItems[ciFileName]), '')
          + '+';
        nf := nf + '.msg';

        if not FileExists(op + nf) then
        begin
          Item.ImageIndex := 5;
          RenameFile(Item.SubItems[ciFileName], op + nf);
          MainForm.DoSynchSearch(Item.SubItems[ciFileName], op + nf);
          Item.SubItems[ciFileName] := op + nf;
        end;
      end;
      Item := MainForm.ListView1.GetNextItem(Item, sdAll, [isSelected]);
    end;

    SetPXTreeNodeName(MainForm.TreeView1.Selected);
 //   actUpdateStatusbarUpdate(Self);
    MainForm.OnlineSession(otSendSelected);
  end;
  close;
end;

procedure TViewMailFrm.menSaveClick(Sender: TObject);
begin
  DoSaveMessage;
  SaveMessage := false;
  Close;
end;

procedure TViewMailFrm.fldFromHeaderChange(Sender: TObject);
  var ms : TStringList;
begin
  SaveMessage := true;
  HeaderChanged:=true;
  if Sender = fldFromHeader then
  begin
    CurrentEmail.From := fldFromHeader.Text;
    exit;
  end;
  if Sender = fldSubject then
  begin
    CurrentEmail.Subject := fldSubject.Text;
    caption:=  fldSubject.Text;
    exit;
  end;
  if Sender = fldAddresses then
  begin
    ms := MainForm.CurrentAddressField;
    if ms <> nil then
    begin
      ms.Clear;
      HeaderAddressToStringList(fldAddresses.Text, ms);
    end;
  end;
end;


procedure TViewMailFrm.DoSaveMessage;
var
  ss:olevariant;
  IndexData:TIndexData;
  F,FromF, ToF: file of byte;
  i: Integer;
  FileName:string;
  buf:array[1..2048] of Char;
  buf2:Pchar;
  NumRead, NumWritten: Integer;
  sPosition,sSize:int64;
begin
  ss:=sTempMessageFile;

  if HeaderChanged then
  begin
    CurrentEmail.From := fldFromHeader.Text;
    case AddressMode of
      0 :
        begin
          CurrentEmail.ToReceiver.Clear;
          HeaderAddressToStringList(fldAddresses.Text, CurrentEmail.ToReceiver);
        end;
      1 :
        begin
          CurrentEmail.CC.Clear;
          HeaderAddressToStringList( fldAddresses.Text, CurrentEmail.CC);
        end;
      2 :
        begin
          CurrentEmail.BCC.Clear;
          HeaderAddressToStringList(fldAddresses.Text, CurrentEmail.BCC);
        end;
    end;
    CurrentEmail.Subject := fldSubject.Text;
  end;
  DHTMLEdit1.SaveDocument(ss);

  if not bNewDoc then begin
       CurrentEMail.CompileMsgFileOnDisk(ActualMailFile);
      iPosition:=StrToInt(MainForm.ListView1.Selected.SubItems[6]);
      iPosition:=(iPosition-1)*Sizeof(TIndexData);
      AssignFile(IndexFile,sMailIndexFile);
      Reset(IndexFile);
      Seek(IndexFile,iPosition);
      BlockRead(IndexFile, IndexData, SizeOf(TIndexData));
      Seek(IndexFile,iPosition);

//////////////////////////find mail file and write new mail file at the end////////////////////
      AssignFile(TempMailFile,ActualMailFile);
      Reset(TempMailFile);
      AssignFile(MailFile,sMailFile);
      Reset(MailFile);
      Seek(MailFile,FileSize(MailFile));
      iSize:=FileSize(TempMailFile);
      buf2 := StrAlloc(iSize);
      BlockRead(TempMailFile,buf2^,iSize);
      BlockWrite(MailFile,'****************',16);
      sPosition:=FilePos(MailFile);
      BlockWrite(MailFile,buf2^,iSize);
      CloseFile(MailFile);
      CloseFile(TempMailFile);
//////////////////////////find mail file and write new mail file at the end////////////////////

      IndexData.Position:=sPosition;
      IndexData.Size:=iSize;
      IndexData.Address:=CurrentEMail.ToReceiver[0];
      IndexData.Subject:=CurrentEMail.Subject;
      IndexData.SDate:=CurrentEMail.ADate;
      IndexData.STime:=CurrentEMail.ATime;
      IndexData.Priority:=CurrentEMail.Priority;
      IndexData.Attach:=CurrentEMail.cntAttachments;
      IndexData.Edit:=true;
      BlockWrite(IndexFile, IndexData,sizeof(TIndexData));
      CloseFile(IndexFile);
      StrDispose(buf2);
      SaveMessage := false;
      bNewDoc:=false;
      MainForm.RefreshListview;

      exit;
  end;

  Dir:=PAccountData(ActiveAccount.Data)^.Path;
  if bSendLater then FileName:=Dir+'发件箱'+'.SML' else  FileName:=Dir+'草稿'+'.SML';
  ////////////////////////////////////////////////////////////write mail file////////////////////////
//  FileName:=sMailFile;
  CurrentEMail.CompileMsgFileOnDisk(ActualMailFile);
  AssignFile(FromF,ActualMailFile);
  Reset(FromF);
  AssignFile(ToF,FileName);
  if FileExists(FileName) then Reset(ToF) else Rewrite(ToF);
  Seek(ToF,FileSize(ToF));
  BlockWrite(ToF,'****************',16);
  sPosition:=FilePos(ToF);
  repeat
    BlockRead(FromF, Buf, SizeOf(Buf), NumRead);
    BlockWrite(ToF, Buf, NumRead,NumWritten );
    sSize:=sSize+ NumWritten;
  until (NumRead = 0) or (NumWritten <> NumRead);
    CloseFile(FromF);
    CloseFile(ToF);
  DeleteFile(ActualMailFile);
////////////////////////////////////////////////////////////write mail file////////////////////////

  if bSendLater then FileName:=Dir+'发件箱'+'.IND' else FileName:=Dir+'草稿'+'.IND';
  bSendLater:=false;

////////////////////////////////////////////////////////////write index file////////////////////////
//  FileName:=sMailIndexFile;
  AssignFile(F, FileName);
  if FileExists(FileName) then
  begin
    Reset(F);
    seek(F,FileSize(F));
  end
  else
    Rewrite(F);
  IndexData.Index:=(FileSize(F) div sizeof(TIndexData))+1;
  IndexData.delete:=false;
  IndexData.Address:=CurrentEMail.ToReceiver[0];
  IndexData.Subject:=CurrentEMail.Subject;
  IndexData.SDate:=CurrentEMail.ADate;
  IndexData.STime:=CurrentEMail.ATime;
  IndexData.Priority:=CurrentEMail.Priority;
  IndexData.Read:=true;
  IndexData.Attach:=CurrentEMail.cntAttachments;
  IndexData.Position:=sPosition;
  IndexData.Size:=sSize;
  IndexData.Edit:=true;
  BlockWrite(f, IndexData,sizeof(TIndexData));
  CloseFile(f);
////////////////////////////////////////////////////////////write index file////////////////////////

  SaveMessage := false;
  bNewDoc:=false;
  bEdit:=false;
  MainForm.RefreshListview;
end;

procedure TViewMailFrm.AddAttachment(aFilename: string);
var
  Item: TListItem;
  SHFileInfo: TSHFileInfo;
  FileTitle: string;
begin
  FileTitle := ExtractFileName(GetLongFileName(aFilename));

  if not FileExists(sTemporaryFolder + FileTitle) then
  begin

    CurrentEmail.ContentType := '';
    CurrentEmail.ContentEncoding := '';

    Item := ListView3.Items.Add;
    Item.Caption := FileTitle;
    SHGetFileInfo(PChar(aFilename), 0, SHFileInfo, SizeOf(SHFileInfo),
      SHGFI_SYSICONINDEX or SHGFI_SMALLICON);
    Item.ImageIndex := SHFileInfo.iIcon;
  end;
  CopyFile(PChar(aFilename), PChar(sTemporaryFolder + FileTitle), False);
  SaveMessage := True;
end;

procedure TViewMailFrm.ToolbarButton9734Click(Sender: TObject);
var
  I: Integer;
begin
  if (MainForm.ListView1.SelCount <> 1) or (MainForm.ListView1.Selected = nil) then
    Exit;
  if DirectoryExists(sLastAttOpenDir) then
    OpenDialog1.InitialDir := sLastAttOpenDir
  else
    OpenDialog1.InitialDir := ExtractFileDir(Application.Exename);
  if OpenDialog1.Execute then
  begin
    CurrentEmail.ContentType := '';
    CurrentEmail.ContentEncoding := '';
    sLastAttOpenDir := ExtractFileDir(OpenDialog1.Filename);
    for I := 0 to OpenDialog1.Files.Count - 1 do
    begin
       AddAttachment(OpenDialog1.Files[I]);
    end;
    AttachPnl.Visible:=true;
    DoSaveMessage;
//    ListView3.Selected.SubItems[ciSize] :=
//      FormatByteText(GetFileSize(MainForm.ListView1.Selected.SubItems[ciFileName]));
  end;

end;

procedure TViewMailFrm.UpdateViewMail;
var
  shtml:olevariant;
  Found: Integer;
  SearchRec: TSearchRec;
  Item: TListItem;
  SHFileInfo: TSHFileInfo;
  LstTemp:TStringList;
begin
  if CurrentEmail.Priority= 'High' then
  begin
    menHigh.Checked:=true;
    PopMenuHigh.Checked:=true;
    PopMenuNormal.Checked:=false;
    PopMenuLow.Checked:=false;
  end
  else if CurrentEmail.Priority= 'Low' then
  begin
    menLow.Checked:=true;
    PopMenuLow.Checked:=true;
    PopMenuHigh.Checked:=false;
    PopMenuNormal.Checked:=false;
  end else
  begin

  end;


  if  bNewDoc or bEdit then
  begin
    fldFromHeader.ReadOnly:=false;
    fldFromHeader.Border3D:=true;
    fldAddresses.ReadOnly:=false;
    fldAddresses.Border3D:=true;
    fldSubject.ReadOnly:=false;
    fldSubject.Border3D:=true;
    StandardToolBar.Visible:=true;
    Toolbar1.Visible:=false;
    Toolbar2.Visible:=true;
    DHTMLEdit1.BrowseMode:=false;

    menMoveTo.Enabled:=true;
    menDelete.Enabled:=true;
    menPrint.Enabled:=true;
    menMessage.Visible:=false;
    menSetPriority.Visible:=true;
  end
  else
  begin
    fldFromHeader.Style:=csSimple;
    fldFromHeader.Color:=clBtnFace;
    fldFromHeader.ReadOnly:=true;
    fldFromHeader.BorderColor:=clBtnFace;

    fldAddresses.Style:=csSimple;
    fldAddresses.Color:=clBtnFace;
    fldAddresses.ReadOnly:=true;
    fldAddresses.BorderColor:=clBtnFace;

    fldSubject.Style:=csSimple;
    fldSubject.Color:=clBtnFace;
    fldSubject.ReadOnly:=true;
    fldSubject.BorderColor:=clBtnFace;

    Toolbar2.Visible:=false;
    Toolbar1.Visible:=true;
    StandardToolBar.Visible:=false;
    DHTMLEdit1.BrowseMode:=true;
    fldAddresses.SetFocus;
    fldAddresses.SelLength:=0;

  end;


  MainForm.CollectAccountsEmail(fldFromHeader.Items);
  fldFromHeader.Text := CurrentEmail.From;
  fldFromHeader.ItemIndex :=fldFromHeader.Items.IndexOf(CurrentEmail.From);


  if sLinkMail='' then  fldAddresses.Text := StringListToCommataString( MainForm.CurrentAddressField,
        False) else fldAddresses.Text :=sLinkMail;
  sLinkMail:='';
  fldSubject.Text := CurrentEmail.Subject;

  Caption:=CurrentEmail.Subject;
      if FileExists(sTempMessageFile) then begin
      shtml:=sTempMessageFile;
        try
//        if CurrentEMail.MsgType = mtText then
//            DHTMLEdit1.DOM.body.innerText:=MainForm.ReadTxtFile(sTempMessageFile)
//          else
            DHTMLEdit1.LoadDocument(shtml);
    except
    end;

    end;


    AttachPnl.Visible:=false;
    ListView3.Items.Clear;
    Found := FindFirst(sTemporaryFolder + '*.*', faReadOnly + faHidden + faArchive
      + faSysFile, SearchRec);
    while Found = 0 do begin
      if sTemporaryFolder + SearchRec.Name <> sTempMessageFile then begin
        Item := ListView3.Items.Add;
        Item.Caption := SearchRec.Name;
        SHGetFileInfo(PChar(sTemporaryFolder + SearchRec.Name), 0, SHFileInfo,SizeOf(SHFileInfo), SHGFI_SYSICONINDEX or SHGFI_SMALLICON);
        Item.ImageIndex := SHFileInfo.iIcon;
        AttachPnl.Visible:=true;
      end;
      Found := FindNext(SearchRec);
    end;
    FindClose(SearchRec);
    if ListView3.Items.Count>0 then menSaveAttach.Enabled:=true;
end;

procedure TViewMailFrm.ToolbarButton9733Click(Sender: TObject);
begin
  MainForm.actMessageDeleteExecute(MainForm.ListView1);
  UpdateViewMail;
end;

procedure TViewMailFrm.ToolbarButton9735Click(Sender: TObject);
begin
//  TridentCommand(IDM_PRINT, false, false, false, false, nil, nil);
end;

procedure TViewMailFrm.ToolbarButton9728Click(Sender: TObject);
begin
  TridentCommand(IDM_COPY, false, false, false, false, nil, nil);
end;

procedure TViewMailFrm.ToolbarButton9738Click(Sender: TObject);
begin
  TridentCommand(IDM_PASTE, false, false, false, false, nil, nil);
end;

procedure TViewMailFrm.ToolbarButton9731Click(Sender: TObject);
begin
   TridentCommand(IDM_Undo, false, false, true, false, Nil, Nil);
end;

procedure TViewMailFrm.ToolbarButton971Click(Sender: TObject);
begin
  TridentCommand(IDM_CUT, false, false, false, false, nil, nil);
end;

procedure TViewMailFrm.ToolbarButton974Click(Sender: TObject);
begin
  MainForm.actMessageDeleteExecute(MainForm.ListView1);
  UpdateViewMail;

end;

procedure TViewMailFrm.ToolbarButton9730Click(Sender: TObject);
begin
  bNewDoc:=true;
  MainForm.actMessageReply1.Execute;
  UpdateViewMail;
end;

procedure TViewMailFrm.ToolbarButton976Click(Sender: TObject);
begin
  bNewDoc:=true;
  MainForm.actMessageReplyQuoted.Execute;
  UpdateViewMail;
end;

procedure TViewMailFrm.ToolbarButton9736Click(Sender: TObject);
begin
  bNewDoc:=true;
  MainForm.actMessageForward1.Execute;
  UpdateViewMail;
end;

procedure TViewMailFrm.ToolbarButton977Click(Sender: TObject);
begin
  MainForm.actNavigationUp.Execute;
  UpdateViewMail;
end;

procedure TViewMailFrm.ToolbarButton9740Click(Sender: TObject);
begin
  MainForm.actNavigationDown.Execute;
  UpdateViewMail;

end;

procedure TViewMailFrm.ToolbarButton973Click(Sender: TObject);
begin
  MainForm.actEnhancedAddessBook.Execute;
end;

procedure TViewMailFrm.CancelOnlineOperation1Click(Sender: TObject);
begin
  MainForm.actFileSaveAs.Execute;
end;

procedure TViewMailFrm.N4Click(Sender: TObject);
begin
  MainForm.actMessageAttSaveAs.Execute;
end;

procedure TViewMailFrm.actMessageAttOpenExecute(Sender: TObject);
var
  I: Integer;
begin
  if ListView3.SelCount = 0 then Exit;
  for I := 0 to ListView3.Items.Count - 1 do
    if ListView3.Items[I].Selected then
    begin
      if not MainForm.OpenFileWithDoubleExtension(sTemporaryFolder +
        ListView3.Items[I].Caption) then exit;
      FileMightBeInUse := True;
      if ShellExecute(Handle, 'open', PChar(sTemporaryFolder +
        ListView3.Items[I].Caption), '', PChar(sTemporaryFolder), SW_SHOWNORMAL)
        <= 32 then
      begin
        ShellExecute(Handle, 'open', PChar(sQuickViewer), PChar(sTemporaryFolder
          + ListView3.Items[I].Caption), PChar(sTemporaryFolder), SW_SHOWNORMAL);
      end;
    end;
end;

procedure TViewMailFrm.actMessageAttOpenUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled :=(ListView3.SelCount > 0);
end;

procedure TViewMailFrm.actMessageAttDeleteExecute(Sender: TObject);
var
  I: Integer;
begin
  if ListView3.SelCount = 0 then
    Exit;
  // AHRK95 -- CLean up of label..
  i := 0;
  while i < ListView3.Items.Count do
  begin
    if ListView3.Items[i].Selected then
    begin
      case MessageDlg(Format(GetMessageString(14),
        [ListView3.Items[I].Caption]),
        mtWarning, [mbYes, mbNo, mbCancel], 0) of
        mrYes: begin
            DeleteFile(PChar(sTemporaryFolder + ListView3.Items[I].Caption));
            ListView3.Items[I].Delete;
            SaveMessage := true;
          end;
        mrNo: ; //nothing
        mrCancel: Exit;
      end;
    end;
    inc(i);
  end;

  if SaveMessage then
    DoSaveMessage;
  MainForm.ListView1.Selected.SubItems[ciSize] :=
    FormatByteText(GetFileSize(MainForm.ListView1.Selected.SubItems[ciFileName]));
end;

procedure TViewMailFrm.actMessageAttDeleteUpdate(Sender: TObject);
begin
 (Sender as TAction).Enabled := not DHTMLEdit1.BrowseMode and
   (MainForm.ListView1.Selected <> nil);

end;

procedure TViewMailFrm.open1Click(Sender: TObject);
var
  I: Integer;
begin
  if ListView3.SelCount = 0 then Exit;
  for I := 0 to ListView3.Items.Count - 1 do
    if ListView3.Items[I].Selected then
    begin
      if not MainForm.OpenFileWithDoubleExtension(sTemporaryFolder +
        ListView3.Items[I].Caption) then exit;
      FileMightBeInUse := True;
      if ShellExecute(Handle, 'open', PChar(sTemporaryFolder +
        ListView3.Items[I].Caption), '', PChar(sTemporaryFolder), SW_SHOWNORMAL)
        <= 32 then
      begin
        ShellExecute(Handle, 'open', PChar(sQuickViewer), PChar(sTemporaryFolder
          + ListView3.Items[I].Caption), PChar(sTemporaryFolder), SW_SHOWNORMAL);
      end;
    end;
end;

procedure TViewMailFrm.PopupMenu3Popup(Sender: TObject);
begin
   open1.Enabled:=(ListView3.SelCount >0) and
                (ListView3.Selected <> nil);
   saveall1.Enabled:=open1.Enabled;
   saveas1.Enabled:=open1.Enabled;
   add1.Enabled:=not DHTMLEdit1.BrowseMode;
   delete1.Enabled:=not DHTMLEdit1.BrowseMode;
end;

procedure TViewMailFrm.saveall1Click(Sender: TObject);
begin
  MainForm.actFileSaveAs.Execute;
end;

procedure TViewMailFrm.delete1Click(Sender: TObject);
var
  I: Integer;
begin
  if ListView3.SelCount = 0 then
    Exit;
  // AHRK95 -- CLean up of label..
  i := 0;
  while i < ListView3.Items.Count do
  begin
    if ListView3.Items[i].Selected then
    begin
      case MessageDlg(Format(GetMessageString(14),
        [ListView3.Items[I].Caption]),
        mtWarning, [mbYes, mbNo, mbCancel], 0) of
        mrYes: begin
            DeleteFile(PChar(sTemporaryFolder + ListView3.Items[I].Caption));
            ListView3.Items[I].Delete;
            SaveMessage := true;
          end;
        mrNo: ; //nothing
        mrCancel: Exit;
      end;
    end;
    inc(i);
  end;

  if SaveMessage then
    DoSaveMessage;
  MainForm.ListView1.Selected.SubItems[ciSize] :=
    FormatByteText(GetFileSize(MainForm.ListView1.Selected.SubItems[ciFileName]));
end;

procedure TViewMailFrm.saveas1Click(Sender: TObject);
begin
  SaveAttach;
end;

procedure TViewMailFrm.menMoveToClick(Sender: TObject);
begin
  MainForm.actMessageMove.Execute;
  close;
end;

procedure TViewMailFrm.N6Click(Sender: TObject);
begin
  MainForm.actMessageCopy.Execute;
  close;
end;

procedure TViewMailFrm.N17Click(Sender: TObject);
begin
  close;
end;

procedure TViewMailFrm.menPrintClick(Sender: TObject);
var
  withui: OleVariant;
begin
  withui := true;
  DHTMLEdit1.PrintDocument(withui);
end;

procedure TViewMailFrm.ToolbarButton972Click(Sender: TObject);
var
  withui: OleVariant;
begin
  withui := true;
  DHTMLEdit1.PrintDocument(withui);
end;

procedure TViewMailFrm.menFindClick(Sender: TObject);
begin
  TridentCommand(IDM_FIND, false, true, false, false, nil, nil);
end;

procedure TViewMailFrm.New3Click(Sender: TObject);
begin
  MainForm.actMessageNew.Execute;
end;

procedure TViewMailFrm.ToolbarButton975Click(Sender: TObject);
begin
   TridentCommand(IDM_CAPTIONINSERT, false, false, false, false, nil, nil);
end;

procedure TViewMailFrm.N10Click(Sender: TObject);
begin
  MainForm.actMessageAddToAddrbook.Execute;
end;

procedure TViewMailFrm.menSendLaterClick(Sender: TObject);
var
  Node: TTreeNode;
begin
  bSendLater:=true;
  DoSaveMessage;
  SaveMessage := false;
  Close;
end;

procedure TViewMailFrm.DragDropFiles(hDropHandle: HDrop);
var
  PFilename: PChar;
  I, Count: Integer;
begin
  if (MainForm.ListView1.SelCount <> 1) or (MainForm.ListView1.Selected = nil) then
    Exit;
  GetMem(PFilename, 256);
  Count := DragQueryFile(hDropHandle, $FFFFFFFF, nil, 255);
  for I := 0 to Count - 1 do begin
    DragQueryFile(hDropHandle, I, PFilename, 255);
    AddAttachment(StrPas(PFilename));
  end;
  FreeMem(PFilename, 256);
end;

procedure TViewMailFrm.DragDropWndProc(var Message: TMessage);
begin
  with Message do begin
    if Msg = WM_DROPFILES then
      DragDropFiles(HDrop(wParam))
    else
      Result := CallWindowProc(DragDropDefWndProc, ListView3.Handle, Msg,
        WParam, LParam);
  end;
end;

procedure TViewMailFrm.WMTaskIcon(var Message: TMessage);
var
  P: TPoint;
begin
  case Message.lParam of
    WM_LBUTTONDBLCLK: begin
      end;
    WM_LBUTTONUP: begin
        ShowPhoenixMail1Click(Self);
      end;
    WM_RBUTTONUP: begin
        GetCursorPos(P);
        MainForm.PopUpMenu6.PopUp(P.X, P.Y);
      end;
  end;
end;

procedure TViewMailFrm.WMSysCommand(var Message: TMessage);
begin
  if (Message.WParam = SC_MINIMIZE) then begin
    ShowWindow(Application.Handle, SW_HIDE);
    Visible := False;
  end
  else
    inherited;
end;


procedure TViewMailFrm.ShowPhoenixMail1Click(Sender: TObject);
begin
  Visible := True;
  ShowWindow(Application.Handle, SW_RESTORE);
  SetForegroundWindow(Application.Handle);
  SetForegroundWindow(Handle);
  if OnlineForm <> nil then try
    SetForegroundWindow(OnlineForm.Handle);
  except end;

end;

procedure TViewMailFrm.N34Click(Sender: TObject);
var
  G: Integer;
  IsNormalFolder : boolean;
  sTemp:string;
begin
  if MainForm.TreeView1.Selected <> nil then
  begin
    IsNormalFolder := (MainForm.TreeView1.Selected.StateIndex = tnFolder) and
                      (not (PFolderData(MainForm.TreeView1.Selected.Data)^.Inbox));

    TemplateForm := TTemplateForm.Create(Self);
    if Assigned(LangHolder) then
    begin
      if LangHolder.Language <> '' then
        LangHolder.Translate([TemplateForm]);
    end;

    with TemplateForm do
    begin
      DoMode(1);
      if (MainForm.ListView1.SelCount = 1) and (IsNormalFolder) then
        CanAttach := True
      else begin
        Button4.Visible := False;
        Button3.Default := True;
        CanAttach := False;
      end;
      if ShowModal = mrOK then begin
         Memo1.SelectAll;
         Memo1.CopyToClipboard;
         Free;
         ExecCommand(DECMD_PASTE, OLECMDEXECOPT_DODEFAULT, Null);
      end;
    end;
  end;
end;

procedure TViewMailFrm.ToolButton1Click(Sender: TObject);
var op, nf : string;
    Item : TListItem;
begin
  DoSaveMessage;
  {Item := MainForm.ListView1.Selected;
  while Item <> nil do
  begin
    op := MakeValidDirName(ExtractFilePath(Item.SubItems[ciFileName]));
    if Item.ImageIndex <> 5 then
    begin
      nf := ChangeFileExt(ExtractFileName(Item.SubItems[ciFileName]), '')
        + '+';
      nf := nf + '.msg';

      if not FileExists(op + nf) then
      begin
        Item.ImageIndex := 5;
        RenameFile(Item.SubItems[ciFileName], op + nf);
        MainForm.DoSynchSearch(Item.SubItems[ciFileName], op + nf);
        Item.SubItems[ciFileName] := op + nf;
      end;
    end;
    Item := MainForm.ListView1.GetNextItem(Item, sdAll, [isSelected]);
  end;
  MainForm.actUpdateStatusbar.Execute;
  SetPXTreeNodeName(MainForm.TreeView1.Selected);}
  MainForm.OnlineSession(otSendSelected);
end;

procedure TViewMailFrm.ToolButton3Click(Sender: TObject);
begin
  TridentCommand(IDM_CUT, false, false, false, false, nil, nil);
end;

procedure TViewMailFrm.ToolButton4Click(Sender: TObject);
begin
  TridentCommand(IDM_COPY, false, false, false, false, nil, nil);
end;

procedure TViewMailFrm.ToolButton5Click(Sender: TObject);
begin
  TridentCommand(IDM_PASTE, false, false, false, false, nil, nil);
end;

procedure TViewMailFrm.ToolButton7Click(Sender: TObject);
var
  I: Integer;
begin
  if DirectoryExists(sLastAttOpenDir) then
    OpenDialog1.InitialDir := sLastAttOpenDir
  else
    OpenDialog1.InitialDir := ExtractFileDir(Application.Exename);
  if OpenDialog1.Execute then
  begin
    CurrentEmail.ContentType := '';
    CurrentEmail.ContentEncoding := '';
    sLastAttOpenDir := ExtractFileDir(OpenDialog1.Filename);
    for I := 0 to OpenDialog1.Files.Count - 1 do
    begin
       AddAttachment(OpenDialog1.Files[I]);
    end;
    AttachPnl.Visible:=true;
//    DoSaveMessage;
  end;

end;

procedure TViewMailFrm.ToolButton6Click(Sender: TObject);
begin
   TridentCommand(IDM_Undo, false, false, true, false, Nil, Nil);
end;

procedure TViewMailFrm.FormPaint(Sender: TObject);
begin
  if bNewPic and (sBackGround<>'') then  DHTMLEdit1.DOM.body.setAttribute('background',sBackGround,0);
end;

procedure TViewMailFrm.SaveAttach;
var
  frmSaveAttach:TfrmSaveAttach;
begin
  frmSaveAttach:=TfrmSaveAttach.Create(self);
  frmSaveAttach.ShowModal;
end;

procedure TViewMailFrm.ListView3Change(Sender: TObject; Item: TListItem;
  Change: TItemChange);
begin
  SaveMessage:=true;
end;

procedure TViewMailFrm.FormResize(Sender: TObject);
begin
  fldFromHeader.Width:=panel1.Width-72-10;
  fldFromHeader.Left:=72;
  fldAddresses.Width:=panel1.Width-72-10;
  fldAddresses.Left:=72;
  fldSubject.Width:=panel1.Width-72-10;
  fldSubject.Left:=72;
  ListView3.Width:=panel1.Width-72-10;
  ListView3.Left:=72;
end;

procedure TViewMailFrm.menNormalClick(Sender: TObject);
begin
  CurrentEmail.Priority:='normal';
  CurrentEmail.Priority[1]:=UpCase(CurrentEmail.Priority[1]);
  menNormal.Checked:=true;
end;


procedure TViewMailFrm.menHighClick(Sender: TObject);
begin
  CurrentEmail.Priority:='high';
  CurrentEmail.Priority[1]:=UpCase(CurrentEmail.Priority[1]);
  menHigh.Checked:=true;
end;

procedure TViewMailFrm.menLowClick(Sender: TObject);
begin
  CurrentEmail.Priority:='low';
  CurrentEmail.Priority[1]:=UpCase(CurrentEmail.Priority[1]);
  menLow.Checked:=true;
end;

procedure TViewMailFrm.menRequestClick(Sender: TObject);
begin
  menRequest.Checked:=not menRequest.Checked;
  CurrentEmail.RequestReply:=menRequest.Checked;
end;

procedure TViewMailFrm.menSendMailClick(Sender: TObject);
var
  ss:olevariant;
begin
  ss:=sTempMessageFile;
  DHTMLEdit1.SaveDocument(ss);
  CurrentEMail.CompileMsgFileOnDisk(ActualMailFile);
  FileCopy(nil,ActualMailFile,sTemplateFolder+'Temp.SML');
  DeleteFile(ActualMailFile);
  MainForm.actUpdateStatusbarUpdate(Self);
  MainForm.OnlineSession(otSendSelected);
  bEdit:=false;
  bNewDoc:=false;
  sBackGround:='';    
  close;
end;

procedure TViewMailFrm.PopMenuHighClick(Sender: TObject);
begin
  CurrentEmail.Priority:='high';
  CurrentEmail.Priority[1]:=UpCase(CurrentEmail.Priority[1]);
  PopMenuHigh.Checked:=true;
  PopMenuNormal.Checked:=false;
  PopMenuLow.Checked:=false;
end;

procedure TViewMailFrm.PopMenuNormalClick(Sender: TObject);
begin
  CurrentEmail.Priority:='normal';
  CurrentEmail.Priority[1]:=UpCase(CurrentEmail.Priority[1]);
  PopMenuNormal.Checked:=true;
  PopMenuHigh.Checked:=false;
  PopMenuLow.Checked:=false;

end;

procedure TViewMailFrm.PopMenuLowClick(Sender: TObject);
begin
  CurrentEmail.Priority:='low';
  CurrentEmail.Priority[1]:=UpCase(CurrentEmail.Priority[1]);
  PopMenuLow.Checked:=true;
  PopMenuHigh.Checked:=false;
  PopMenuNormal.Checked:=false;

end;

initialization
  NilVariant  := 0;
end.

