unit U_FrmMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ActnList, ToolWin, ActnMan, ActnCtrls, XPStyleActnCtrls,
  OleCtrls, SHDocVw, EmbeddedWB, ActnMenus, CustomizeDlg, ImgList,
  ComCtrls, StdCtrls, ExtCtrls, U_WebForm, U_WebToolBar, Menus,
  U_PublicInterface, Gauges, IEUtils, FavMenu, CommCtrl,
  IETravelLog, Buttons, U_SideBar, HttpApp, IEAddress, SecurityManager,
  ImportFavorites, ExportFavorites, U_UndoList, AppEvnts;

type
  TFrmIEMain = class(TForm, IMainUiHandler, IPageControl)
    imgToolBar: TImageList;
    StatusBar: TStatusBar;
    IETravelLog: TIETravelLog;
    ActionList: TActionList;
    Ac_Forward: TAction;
    Ac_Back: TAction;
    Ac_Stop: TAction;
    Ac_Refresh: TAction;
    Ac_Blank: TAction;
    Ac_Go: TAction;
    MainMenu: TMainMenu;
    F1: TMenuItem;
    N1: TMenuItem;
    E1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    N4: TMenuItem;
    N5: TMenuItem;
    N6: TMenuItem;
    CoolBar: TCoolBar;
    tblMainToolBar: TToolBar;
    ToolButton1: TToolButton;
    ToolButton6: TToolButton;
    btnBack: TToolButton;
    btnForward: TToolButton;
    ToolButton7: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    tblAddressBar: TToolBar;
    lblAddress: TLabel;
    btnAddress: TToolButton;
    pmnBack: TPopupMenu;
    pmnForward: TPopupMenu;
    N7: TMenuItem;
    Ac_Favorites: TAction;
    Ac_History: TAction;
    ToolButton9: TToolButton;
    btnFavorites: TToolButton;
    btnHistory: TToolButton;
    Splitter: TSplitter;
    pnlLeftDockBar: TPanel;
    cobAddress: TIEAddress;
    SecurityManager: TSecurityManager;
    Ac_AllowActivex: TAction;
    N8: TMenuItem;
    N9: TMenuItem;
    ACTIVEX1: TMenuItem;
    Ac_AllowScript: TAction;
    Ac_AllowImage: TAction;
    Ac_AllowVideo: TAction;
    Ac_AllowBkSound: TAction;
    Ac_AllowJava: TAction;
    N10: TMenuItem;
    N11: TMenuItem;
    JAVA1: TMenuItem;
    Script1: TMenuItem;
    AcAllowVideo1: TMenuItem;
    Opd: TOpenDialog;
    Sdl: TSaveDialog;
    ImportFavorite: TImportFavorite;
    ExportFavorite: TExportFavorite;
    Ac_About: TAction;
    N12: TMenuItem;
    SOEASY2: TMenuItem;
    Ac_Cut: TAction;
    Ac_Copy: TAction;
    Ac_Paste: TAction;
    N13: TMenuItem;
    N14: TMenuItem;
    N15: TMenuItem;
    N16: TMenuItem;
    N18: TMenuItem;
    Ac_SelectAll: TAction;
    Ac_Search: TAction;
    N17: TMenuItem;
    N19: TMenuItem;
    N20: TMenuItem;
    Ac_CurPage: TAction;
    N21: TMenuItem;
    N22: TMenuItem;
    Ac_Open: TAction;
    Ac_SaveAs: TAction;
    Ac_Print: TAction;
    Ac_PrintSet: TAction;
    Ac_Preview: TAction;
    Ac_OffLine: TAction;
    Ac_Close: TAction;
    Ac_CloseAll: TAction;
    Ac_Quit: TAction;
    N23: TMenuItem;
    N24: TMenuItem;
    N25: TMenuItem;
    N26: TMenuItem;
    N27: TMenuItem;
    N28: TMenuItem;
    N29: TMenuItem;
    N30: TMenuItem;
    N31: TMenuItem;
    N32: TMenuItem;
    N33: TMenuItem;
    N34: TMenuItem;
    N35: TMenuItem;
    N36: TMenuItem;
    Ac_Property: TAction;
    N37: TMenuItem;
    N38: TMenuItem;
    Ac_ViewSource: TAction;
    N39: TMenuItem;
    N40: TMenuItem;
    Ac_HighLight: TAction;
    N41: TMenuItem;
    Ac_IeOption: TAction;
    Internet1: TMenuItem;
    Ac_SendUrlEmail: TAction;
    Ac_SendPageEmail: TAction;
    Ac_SendShotCut: TAction;
    N42: TMenuItem;
    N43: TMenuItem;
    N44: TMenuItem;
    N45: TMenuItem;
    N46: TMenuItem;
    Ac_HomePage: TAction;
    N47: TMenuItem;
    CoolBarPageControl: TCoolBar;
    PageControl: TTabControl;
    imgPageControl: TImageList;
    FavoritesMenu: TFavoritesMenu;
    imgFavorite: TImageList;
    ToolButton2: TToolButton;
    Ac_UnDo: TAction;
    ToolButton3: TToolButton;
    ToolButton8: TToolButton;
    pmnUndo: TPopupMenu;
    ToolBar1: TToolBar;
    ApplicationEvents: TApplicationEvents;
    procedure Ac_ForwardExecute(Sender: TObject);
    procedure Ac_BackExecute(Sender: TObject);
    procedure Ac_StopExecute(Sender: TObject);
    procedure Ac_RefreshExecute(Sender: TObject);
    procedure Ac_BlankExecute(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure cobAddressNavigateUrl(Sender: TObject; Url: string);
    procedure FormShow(Sender: TObject);
    procedure StatusBarResize(Sender: TObject);
    procedure FavoritesMenuURLSelected(Sender: TObject; Url: string);
    procedure Ac_GoExecute(Sender: TObject);
    procedure tblAddressBarResize(Sender: TObject);
    procedure IETravelLogEntry(Title, Url: string; var Cancel: Boolean);
    procedure pmnBackPopup(Sender: TObject);
    procedure pmnForwardPopup(Sender: TObject);
    procedure Ac_FavoritesExecute(Sender: TObject);
    procedure FormGetSiteInfo(Sender: TObject; DockClient: TControl;
      var InfluenceRect: TRect; MousePos: TPoint; var CanDock: Boolean);
    procedure OnSideBarPageControlChange(Sender: TObject);
    procedure pnlLeftDockBarDockDrop(Sender: TObject;
      Source: TDragDockObject; X, Y: Integer);
    procedure pnlLeftDockBarDockOver(Sender: TObject;
      Source: TDragDockObject; X, Y: Integer; State: TDragState;
      var Accept: Boolean);
    procedure pnlLeftDockBarUnDock(Sender: TObject; Client: TControl;
      NewTarget: TWinControl; var Allow: Boolean);
    procedure Ac_AllowImageExecute(Sender: TObject);
    procedure FavoritesMenuExportClick(Sender: TObject;
      var LocalPath: string);
    procedure FavoritesMenuImportClick(Sender: TObject;
      var LocalPath: string);
    procedure Ac_AboutExecute(Sender: TObject);
    procedure Ac_OpenExecute(Sender: TObject);
    procedure Ac_CurPageExecute(Sender: TObject);
    procedure Ac_SaveAsExecute(Sender: TObject);
    procedure Ac_PrintSetExecute(Sender: TObject);
    procedure Ac_PrintExecute(Sender: TObject);
    procedure Ac_PreviewExecute(Sender: TObject);
    procedure Ac_OffLineExecute(Sender: TObject);
    procedure Ac_CloseExecute(Sender: TObject);
    procedure Ac_CloseAllExecute(Sender: TObject);
    procedure Ac_QuitExecute(Sender: TObject);
    procedure Ac_CutExecute(Sender: TObject);
    procedure Ac_CopyExecute(Sender: TObject);
    procedure Ac_PasteExecute(Sender: TObject);
    procedure Ac_SelectAllExecute(Sender: TObject);
    procedure Ac_SearchExecute(Sender: TObject);
    procedure Ac_PropertyExecute(Sender: TObject);
    procedure Ac_ViewSourceExecute(Sender: TObject);
    procedure Ac_HighLightExecute(Sender: TObject);
    procedure Ac_IeOptionExecute(Sender: TObject);
    procedure Ac_SendUrlEmailExecute(Sender: TObject);
    procedure Ac_SendPageEmailExecute(Sender: TObject);
    procedure Ac_SendShotCutExecute(Sender: TObject);
    procedure Ac_HomePageExecute(Sender: TObject);
    procedure PageControlChange(Sender: TObject);
    procedure PageControlMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure PageControlMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure FavoritesMenuItemImageIndex(Sender: TObject;
      MenuItem: TMenuItem; MenuType: TMenuType; var ImageIndex: Integer);
    procedure Ac_UnDoExecute(Sender: TObject);
  private
    FProgressBar: TGauge;

    PopUpItems: array[0..9] of TMenuItem;
    ItemsCounter: Integer;
    FSideBar: TFrmSideBar;
    FRowCount: Integer;
    FPageUndoList: IPageUndoList;
  protected
    {IMainUiHandler}
    procedure ActiveWebChange; stdcall;
    procedure ChangeAddress(Address: string); stdcall;
    procedure RefreshProgress(Progress: Integer); stdcall;
    procedure ShowStatus(StatusText: string); stdcall;
    procedure CommandStateChange(Command: Integer; Enable: WordBool); stdcall;
    function GetPageControl: IPageControl; stdcall;
    {IPageControl}
    function InsertPage(PageText: string; PageObject: TObject): Integer; stdcall;
    procedure DeletePage(PageObject: TObject); stdcall;
    procedure ChagePageImage(PageObject: TObject; ImageIndex: Integer); stdcall;
    function GetPageCount: Integer; stdcall;
    procedure SetPageText(PageObject: TObject; Text: string); stdcall;
    function GetPageText(PageObject: TObject): string; stdcall;
  public
    procedure AdjustPageControl;
    procedure CreateFavoritesBar;
    procedure CreateProgress;
    function CurrentWebForm: TFrmWeb;
    procedure LoadOptionForAll;
    function NewPage(UrlStr: string; IsActive: Boolean): TFrmWeb;
    procedure OnSideBarClose(Sender: TObject; var Action: TCloseAction);
    procedure OnSideBarGotoUrl(Sender: TObject; const Url: string);
    procedure OnSideBarURLSelected(Sender: TObject; Url: string);
    procedure OnTravelLogItemClick(Sender: TObject);
    procedure OnUndoItemClick(Sender: TObject);
    procedure ShowDockPanel(APanel: TPanel; MakeVisible: Boolean; Client: TControl);

    procedure Open();
    { Public declarations }
  end;

var
  FrmIEMain: TFrmIEMain;

implementation

uses Unicode, U_UserConfig, U_FrmAbout;



{$R *.dfm}
procedure TFrmIEMain.Open();
begin
  SetWindowPos(Handle, HWND_TOPMOST, Left, Top, Width, Height, SWP_SHOWWINDOW);
  show;
end;
procedure TFrmIEMain.FormCreate(Sender: TObject);
begin
  CreateProgress;
  tblAddressBarResize(nil);
  FRowCount := 1;

  FavoritesMenu.Images := imgFavorite;

  FPageUndoList := TPageUndoList.Create;
  FPageUndoList.MaxItemCount := 20;
end;


procedure TFrmIEMain.CreateFavoritesBar;
begin
  FSideBar := TFrmSideBar.Create(Self);
  FSideBar.OnClose := OnSideBarClose;
  FSideBar.FavoritesTree.OnNavigate := OnSideBarGotoUrl;
  FSideBar.HistoryListView.OnURLSelected := OnSideBarURLSelected;
  FSideBar.PageControl.OnChange := OnSideBarPageControlChange;
  FSideBar.ManualDock(pnlLeftDockBar);
  FSideBar.Visible := True;
end;

procedure TFrmIEMain.CreateProgress;
begin
  FProgressBar := TGauge.Create(Self);
  with FProgressBar do
  begin
    Parent := StatusBar;
    BorderStyle := bsNone;
    BackColor := StatusBar.Color;
    Left := StatusBar.Panels[0].Width + 3;
    Width := StatusBar.Panels[1].Width - 4;
    Top := 3;
    Height := StatusBar.Height - 4;

    MaxValue := 10000;
  end;
end;


procedure TFrmIEMain.FormDestroy(Sender: TObject);
begin
//
end;

procedure TFrmIEMain.Ac_ForwardExecute(Sender: TObject);
begin
  CurrentWebForm.GoForward;
end;

procedure TFrmIEMain.Ac_BackExecute(Sender: TObject);
begin
  if CurrentWebForm <> nil then
    CurrentWebForm.GoBack;
end;

procedure TFrmIEMain.Ac_StopExecute(Sender: TObject);
begin
  if CurrentWebForm <> nil then
    CurrentWebForm.Stop;
end;

procedure TFrmIEMain.Ac_RefreshExecute(Sender: TObject);
begin
  if CurrentWebForm <> nil then
    CurrentWebForm.Refresh;
end;

procedure TFrmIEMain.Ac_BlankExecute(Sender: TObject);
begin
  NewPage(C_BLANKADDR, True);
  cobAddress.SetFocus;
end;

function TFrmIEMain.CurrentWebForm: TFrmWeb;
begin
  Result := TFrmWeb(ActiveMDIChild);
end;

function TFrmIEMain.NewPage(UrlStr: string; IsActive: Boolean): TFrmWeb;
begin
  if MDIChildCount = 0 then
    IsActive := True;

  Result := TFrmWeb.Create(UrlStr, IsActive, Self);
end;


procedure TFrmIEMain.cobAddressNavigateUrl(Sender: TObject; Url: string);
begin
  Ac_Go.Execute;
end;

procedure TFrmIEMain.ChangeAddress(Address: string);
begin
  cobAddress.Url := Address;
end;

procedure TFrmIEMain.RefreshProgress(Progress: Integer);
begin
  FProgressBar.Progress := Progress;
end;

procedure TFrmIEMain.ShowStatus(StatusText: string);
begin
  StatusBar.Panels[0].Text := StatusText;
end;

procedure TFrmIEMain.FormShow(Sender: TObject);
begin
  cobAddress.SetFocus;
  Ac_Blank.Execute;
  FavoritesMenu.CreateMenu;
  CreateFavoritesBar;
end;

procedure TFrmIEMain.StatusBarResize(Sender: TObject);
begin
  StatusBar.Panels[0].Width := StatusBar.Width
    - StatusBar.Panels[1].Width - StatusBar.Panels[2].Width;
  FProgressBar.Left := StatusBar.Panels[0].Width + 3;
  FProgressBar.Width := StatusBar.Panels[1].Width - 4;
  FProgressBar.Top := 3;
  FProgressBar.Height := StatusBar.Height - 4;
end;


procedure TFrmIEMain.FavoritesMenuURLSelected(Sender: TObject; Url: string);
begin
  NewPage(Url, False);
end;

procedure TFrmIEMain.CommandStateChange(Command: Integer; Enable: WordBool);
begin
  if Command = CSC_NAVIGATEFORWARD then
    Ac_Forward.Enabled := Enable;

  if Command = CSC_NAVIGATEBACK then
    Ac_Back.Enabled := Enable;

end;

procedure TFrmIEMain.ActiveWebChange;
var
  WebForm: TFrmWeb;
begin
  WebForm := CurrentWebForm;
  if WebForm = nil then Exit;

  //改变地址栏
  cobAddress.Url := WebForm.WebUrl;

  PageControl.TabIndex := PageControl.Tabs.IndexOfObject(WebForm);
//  WebForm.ToolButton.Down := True;

  //刷新访问历史
//  IeTravelLog.Webbrowser := WebForm.WebBrowser;
//  IeTravelLog.Connect;

  FavoritesMenu.EmbeddedWB := WebForm.WebBrowser;

  //刷新进度条
  RefreshProgress(WebForm.Progress);

  WebForm.WebBrowser.SetFocusToDoc;
end;


procedure TFrmIEMain.Ac_GoExecute(Sender: TObject);
var
  eWebForm: TFrmWeb;
begin
  eWebForm := CurrentWebForm;

  if eWebForm = nil then
    eWebForm := NewPage(cobAddress.Url, True)
  else
    eWebForm.Go(cobAddress.Url, False);

  eWebForm.WebBrowser.SetFocusToDoc;
end;

procedure TFrmIEMain.tblAddressBarResize(Sender: TObject);
begin
  cobAddress.Width := tblAddressBar.Width - lblAddress.Width - btnAddress.Width;
end;

procedure TFrmIEMain.IETravelLogEntry(Title, Url: string;
  var Cancel: Boolean);
begin
  PopUpItems[ItemsCounter] := TMenuItem.Create(Self);
  PopUpItems[ItemsCounter].Caption := Title;
  PopUpItems[ItemsCounter].Hint := Url;
  PopUpItems[ItemsCounter].OnClick := OnTravelLogItemClick;

  if IETravelLog.IsBack then
    pmnBack.Items.Add(PopUpItems[ItemsCounter])
  else
    pmnForward.Items.Add(PopUpItems[ItemsCounter]);

  Inc(ItemsCounter);
  if ItemsCounter = 10 then Cancel := True;
end;

procedure TFrmIEMain.OnTravelLogItemClick(Sender: TObject);
var
  index: Integer;
begin
  with Sender as TMenuItem do
    if IETravelLog.IsBack then
      index := 0 - pmnBack.Items.IndexOf(Sender as TMenuItem) - 1
    else
      index := pmnForward.Items.IndexOf(Sender as TMenuItem) + 1;

  IETravelLog.TravelTo(index);
end;

procedure TFrmIEMain.pmnBackPopup(Sender: TObject);
var
  eWebForm: TFrmWeb;
begin
  eWebForm := CurrentWebForm;

  if eWebForm = nil then Exit;

  pmnBack.Items.Clear;
  //刷新访问历史
  IETravelLog.WebBrowser := eWebForm.WebBrowser;
  IETravelLog.Connect;

  IETravelLog.EnumerateBack;
end;

procedure TFrmIEMain.pmnForwardPopup(Sender: TObject);
var
  eWebForm: TFrmWeb;
begin
  eWebForm := CurrentWebForm;

  if eWebForm = nil then Exit;

  pmnForward.Items.Clear;
  //刷新访问历史
  IETravelLog.WebBrowser := eWebForm.WebBrowser;
  IETravelLog.Connect;

  IETravelLog.EnumerateForward;
end;


procedure TFrmIEMain.Ac_FavoritesExecute(Sender: TObject);
begin
  if btnFavorites.Down or btnHistory.Down then
    pnlLeftDockBar.Visible := True
  else
    pnlLeftDockBar.Visible := False;

  if not TAction(Sender).Checked then
    Exit;

  if Sender = Ac_Favorites then
    FSideBar.SideBarType := sbtFavorites
  else
    FSideBar.SideBarType := sbtHistory;
end;

procedure TFrmIEMain.FormGetSiteInfo(Sender: TObject; DockClient: TControl;
  var InfluenceRect: TRect; MousePos: TPoint; var CanDock: Boolean);
begin
  CanDock := DockClient is TFrmSideBar;
end;

procedure TFrmIEMain.OnSideBarClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caNone;
  if FSideBar.HostDockSite <> nil then
    FSideBar.HostDockSite.Hide;
  Ac_Favorites.Checked := False;
  Ac_History.Checked := False;
end;

procedure TFrmIEMain.OnSideBarGotoUrl(Sender: TObject; const Url: string);
var
  eWebForm: TFrmWeb;
begin
  eWebForm := CurrentWebForm;

  if eWebForm = nil then Exit;

  eWebForm.Go(Url, False);
end;

procedure TFrmIEMain.OnSideBarPageControlChange(Sender: TObject);
begin
  case FSideBar.SideBarType of
    sbtFavorites:
      begin
        Ac_Favorites.Checked := True;
        Ac_History.Checked := False;
      end;
    sbtHistory:
      begin
        Ac_Favorites.Checked := False;
        Ac_History.Checked := True;
      end;
  end;
end;

procedure TFrmIEMain.OnSideBarURLSelected(Sender: TObject; Url: string);
var
  eWebForm: TFrmWeb;
begin
  eWebForm := CurrentWebForm;

  if eWebForm = nil then Exit;

  eWebForm.Go(Url, False);
end;

procedure TFrmIEMain.pnlLeftDockBarDockDrop(Sender: TObject;
  Source: TDragDockObject; X, Y: Integer);
begin
  if (Sender as TPanel).DockClientCount = 1 then
    ShowDockPanel(Sender as TPanel, True, nil);
  (Sender as TPanel).DockManager.ResetBounds(True);
end;

procedure TFrmIEMain.pnlLeftDockBarDockOver(Sender: TObject;
  Source: TDragDockObject; X, Y: Integer; State: TDragState;
  var Accept: Boolean);
var
  ARect: TRect;
begin
  Accept := True;
  if Accept then
  begin
    //Modify the DockRect to preview dock area.
    ARect.TopLeft := pnlLeftDockBar.ClientToScreen(Point(0, 0));
    ARect.BottomRight := pnlLeftDockBar.ClientToScreen(
      Point(Self.ClientWidth div 3, pnlLeftDockBar.Height));
    Source.DockRect := ARect;
  end;
end;


procedure TFrmIEMain.pnlLeftDockBarUnDock(Sender: TObject; Client: TControl;
  NewTarget: TWinControl; var Allow: Boolean);
begin
  if (Sender as TPanel).DockClientCount = 0 then
    ShowDockPanel(Sender as TPanel, False, nil);
end;

procedure TFrmIEMain.ShowDockPanel(APanel: TPanel; MakeVisible: Boolean; Client:
  TControl);
begin
  if not MakeVisible and (APanel.VisibleDockClientCount > 1) then
    Exit;

  Splitter.Visible := MakeVisible;

  if MakeVisible then
  begin
//    APanel.Width := ClientWidth div 3;
    APanel.Width := ClientWidth div 4;
    Splitter.Left := APanel.Width + Splitter.Width;
  end else
    APanel.Width := 0;

  if MakeVisible and (Client <> nil) then Client.show;
end;

procedure TFrmIEMain.Ac_AllowImageExecute(Sender: TObject);
begin
  UserConfig.Down_AllowActivex := Ac_AllowActivex.Checked;
  UserConfig.Down_AllowImage := Ac_AllowImage.Checked;
  UserConfig.Down_AllowJava := Ac_AllowJava.Checked;
  UserConfig.Down_AllowVideo := Ac_AllowVideo.Checked;
  UserConfig.Down_AllowScript := Ac_AllowScript.Checked;
  UserConfig.Down_AllowBkSound := Ac_AllowBkSound.Checked;

  LoadOptionForAll;
end;

procedure TFrmIEMain.LoadOptionForAll;
var
  I: Integer;
begin
  for I := 0 to MDIChildCount - 1 do
    TFrmWeb(MdiChildren[I]).LoadOption;
end;

procedure TFrmIEMain.FavoritesMenuExportClick(Sender: TObject;
  var LocalPath: string);
begin
  Sdl.FileName := LocalPath;
  if not Sdl.Execute then
  begin
    LocalPath := '';
    Exit;
  end;

  LocalPath := Sdl.FileName;
  LocalPath := ChangeFileExt(LocalPath, '.htm');
end;

procedure TFrmIEMain.FavoritesMenuImportClick(Sender: TObject;
  var LocalPath: string);
begin
  Opd.FileName := LocalPath;
  if not Opd.Execute then begin
    LocalPath := '';
    Exit;
  end;

  LocalPath := Opd.FileName;
end;

procedure TFrmIEMain.Ac_AboutExecute(Sender: TObject);
begin
  with TAboutBox.Create(Self) do
  begin
    ShowModal;
    Free;
  end;
end;

procedure TFrmIEMain.Ac_OpenExecute(Sender: TObject);
begin
  if CurrentWebForm = nil then Exit;

  CurrentWebForm.WebBrowser.OpenDialog; ;
end;

procedure TFrmIEMain.Ac_CurPageExecute(Sender: TObject);
begin
  if CurrentWebForm = nil then Exit;

  CurrentWebForm.WebBrowser.OpenOtherWBFavorites;
end;

procedure TFrmIEMain.Ac_SaveAsExecute(Sender: TObject);
begin
  if CurrentWebForm = nil then Exit;

  CurrentWebForm.WebBrowser.SaveDialog;
end;

procedure TFrmIEMain.Ac_PrintSetExecute(Sender: TObject);
begin
  if CurrentWebForm = nil then Exit;

  CurrentWebForm.WebBrowser.PrintSetup;
end;

procedure TFrmIEMain.Ac_PrintExecute(Sender: TObject);
begin
  if CurrentWebForm = nil then Exit;

  CurrentWebForm.WebBrowser.Print;
end;

procedure TFrmIEMain.Ac_PreviewExecute(Sender: TObject);
begin
  if CurrentWebForm = nil then Exit;

  CurrentWebForm.WebBrowser.PrintPreView;

end;

procedure TFrmIEMain.Ac_OffLineExecute(Sender: TObject);
begin
  if CurrentWebForm = nil then Exit;

  if Ac_OffLine.Checked then
    CurrentWebForm.WebBrowser.WorkOffline
  else
    CurrentWebForm.WebBrowser.WorkOnline;
end;

procedure TFrmIEMain.Ac_CloseExecute(Sender: TObject);
begin
  if CurrentWebForm = nil then Exit;

  CurrentWebForm.Close;
end;

procedure TFrmIEMain.Ac_CloseAllExecute(Sender: TObject);
begin

  while MDIChildCount <> 0 do
    MdiChildren[MDIChildCount - 1].Free;
end;

procedure TFrmIEMain.Ac_QuitExecute(Sender: TObject);
begin
  Application.Terminate;
end;

procedure TFrmIEMain.Ac_CutExecute(Sender: TObject);
begin
  if not cobAddress.Focused then
    CurrentWebForm.WebBrowser.Cut
  else
    cobAddress.Perform(WM_CUT, 0, 0);
end;

procedure TFrmIEMain.Ac_CopyExecute(Sender: TObject);
begin
  if not cobAddress.Focused then
    CurrentWebForm.WebBrowser.Copy
  else
    cobAddress.Perform(WM_COPY, 0, 0);
end;

procedure TFrmIEMain.Ac_PasteExecute(Sender: TObject);
begin
  if not cobAddress.Focused then
    CurrentWebForm.WebBrowser.Paste
  else
    cobAddress.Perform(WM_PASTE, 0, 0);
end;

procedure TFrmIEMain.Ac_SelectAllExecute(Sender: TObject);
begin
  if CurrentWebForm = nil then Exit;

  CurrentWebForm.WebBrowser.SelectAll;
end;

procedure TFrmIEMain.Ac_SearchExecute(Sender: TObject);
begin
  if CurrentWebForm = nil then Exit;

  CurrentWebForm.WebBrowser.ShowFindDialog;
end;

procedure TFrmIEMain.Ac_PropertyExecute(Sender: TObject);
begin
  if CurrentWebForm = nil then Exit;

  CurrentWebForm.WebBrowser.ShowPageProperties;
end;

procedure TFrmIEMain.Ac_ViewSourceExecute(Sender: TObject);
begin
  if CurrentWebForm = nil then Exit;

  CurrentWebForm.WebBrowser.ViewPageSourceHtml;
end;

procedure TFrmIEMain.Ac_HighLightExecute(Sender: TObject);
var
  eText: string;
begin
  if CurrentWebForm = nil then Exit;

  if not InputQuery('高亮显示', '请输入搜索的字符：', eText) then Exit;

  CurrentWebForm.WebBrowser.SearchAndHighlight(eText);
end;

procedure TFrmIEMain.Ac_IeOptionExecute(Sender: TObject);
begin
  if CurrentWebForm = nil then Exit;

  CurrentWebForm.WebBrowser.ShowInternetOptions;
end;

procedure TFrmIEMain.Ac_SendUrlEmailExecute(Sender: TObject);
begin
  if CurrentWebForm = nil then Exit;

  CurrentWebForm.WebBrowser.SendURLInMail;
end;

procedure TFrmIEMain.Ac_SendPageEmailExecute(Sender: TObject);
begin
  if CurrentWebForm = nil then Exit;

  CurrentWebForm.WebBrowser.SendPageInMail(CurrentWebForm.WebBrowser.LocationName);
end;

procedure TFrmIEMain.Ac_SendShotCutExecute(Sender: TObject);
begin
  if CurrentWebForm = nil then Exit;

  CurrentWebForm.WebBrowser.CreateADesktopShortcut;
end;

procedure TFrmIEMain.Ac_HomePageExecute(Sender: TObject);
begin
  if CurrentWebForm = nil then Exit;

  CurrentWebForm.WebBrowser.GoHome;
end;

procedure TFrmIEMain.AdjustPageControl;
var
  eAlign: TAlign;
begin
  if PageControl.Tabs.Count = 0 then Exit;

  if FRowCount = PageControl.RowCount then Exit;

  FRowCount := PageControl.RowCount;
  eAlign := CoolBarPageControl.Align;
  CoolBarPageControl.Align := alNone;
  PageControl.Height := PageControl.RowCount * PageControl.TabHeight +
    PageControl.RowCount * 2;
  CoolBarPageControl.Align := eAlign;
end;

function TFrmIEMain.GetPageControl: IPageControl;
begin
  Result := Self;
end;

procedure TFrmIEMain.DeletePage(PageObject: TObject);
var
  index: Integer;
  UndoItem: TUndoItem;
  eUndoItem: PUndoItem;
  MenuItem: TMenuItem;
begin
  index := PageControl.Tabs.IndexOfObject(PageObject);
  if index = -1 then Exit;

  if FPageUndoList.ItemCount = FPageUndoList.MaxItemCount then
    pmnUndo.Items[pmnUndo.Items.Count - 1].Free;

  UndoItem.Name := TFrmWeb(PageObject).WebTitle;
  UndoItem.Url := TFrmWeb(PageObject).WebUrl;
  UndoItem.DateTime := Now;
  eUndoItem := FPageUndoList.Add(UndoItem);

  MenuItem := TMenuItem.Create(pmnUndo);
  MenuItem.Caption := UndoItem.Name;
  MenuItem.Hint := UndoItem.Url;
  MenuItem.Tag := Integer(eUndoItem);
  MenuItem.OnClick := OnUndoItemClick;
  pmnUndo.Items.Insert(0, MenuItem);


  PageControl.Tabs.Delete(index);

  AdjustPageControl;


  if (PageControl.TabIndex = -1) and (MDIChildCount = 1) then
  begin
    PageControl.TabIndex := 0;
    PageControlChange(nil);
  end;
end;

function TFrmIEMain.GetPageCount: Integer;
begin
  Result := PageControl.Tabs.Count;
end;

function TFrmIEMain.InsertPage(PageText: string; PageObject: TObject): Integer;
begin
  PageControl.Tabs.InsertObject(PageControl.TabIndex + 1, PageText, PageObject);
  Result := PageControl.TabIndex + 1;

  AdjustPageControl;
end;

procedure TFrmIEMain.SetPageText(PageObject: TObject; Text: string);
var
  index: Integer;
begin
  index := PageControl.Tabs.IndexOfObject(PageObject);
  if index = -1 then Exit;

  PageControl.Tabs.Strings[index] := Text;
end;

function TFrmIEMain.GetPageText(PageObject: TObject): string;
var
  index: Integer;
begin
  index := PageControl.Tabs.IndexOfObject(PageObject);
  if index = -1 then Exit;

  Result := PageControl.Tabs.Strings[index];
end;

procedure TFrmIEMain.ChagePageImage(PageObject: TObject; ImageIndex: Integer);
var
  index: Integer;
  TCItem: TTCItem;
begin
  index := PageControl.Tabs.IndexOfObject(PageObject);
  if index = -1 then Exit;

  TCItem.mask := TCIF_IMAGE;
  TCItem.iImage := ImageIndex;
  SendMessage(PageControl.Handle, TCM_SETITEM, index, Longint(@TCItem));

  PageControl.Refresh;
//  TabsChanged;
end;

procedure TFrmIEMain.PageControlChange(Sender: TObject);
begin
  if PageControl.TabIndex = -1 then Exit;

  TFrmWeb(PageControl.Tabs.Objects[PageControl.TabIndex]).ShowWebForm;
end;

procedure TFrmIEMain.PageControlMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  index: Integer;
begin
  if ssDouble in Shift then
  begin
    index := PageControl.IndexOfTabAt(X, Y);
    if index = -1 then Exit;

    TFrmWeb(PageControl.Tabs.Objects[index]).Close;
  end;
end;


procedure TFrmIEMain.PageControlMouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
var
  TabIndex: Integer;
begin
  TabIndex := PageControl.IndexOfTabAt(X, Y);
  if TabIndex = -1 then Exit;

  PageControl.Hint := PageControl.Tabs[TabIndex];
end;

procedure TFrmIEMain.FavoritesMenuItemImageIndex(Sender: TObject;
  MenuItem: TMenuItem; MenuType: TMenuType; var ImageIndex: Integer);
begin
  case MenuType of
    mtFolder, mtEmptyFolder: ImageIndex := 0;
    mtItem: ImageIndex := 1;
    mtAddFavorite: ImageIndex := 2;
    mtOrganizeFavorites: ImageIndex := 3;
    mtImportFavorites: ImageIndex := 4;
    mtExportFavorites: ImageIndex := 5;
  end;
end;

procedure TFrmIEMain.OnUndoItemClick(Sender: TObject);
var
  Url: string;
begin
  Url := TMenuItem(Sender).Hint;

  NewPage(Url, False);

  FPageUndoList.Delete(PUndoItem(TMenuItem(Sender).Tag));
  Sender.Free;
end;

procedure TFrmIEMain.Ac_UnDoExecute(Sender: TObject);
begin
  if pmnUndo.Items.Count = 0 then Exit;

  pmnUndo.Items[0].Click;
end;

end.

