//ライセンス情報はAgeOnフォルダのLISENCEフォルダを参照してください

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;

using nispi.Menu;

namespace nispi.controlclass
{
    class SimpleBrowserParts:ControlBase
    {
        public MyWebBrowser control = new MyWebBrowser();

        //マウス押下時に得る文字列
        //string mouseDownText = "";
        
        public bool nonTextMenuOn;
        public bool textMenuOn;

        public FormParts ownerForm;

        public SimpleBrowserParts()
        {
            control.Location = new Point(1, 27);
            control.Name = "webBrowser1";
            control.TabIndex = 0;
            control.ScrollBarsEnabled = true;
            control.Font = new Font("Meiryo UI", 10.75F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(128)));
            control.ScriptErrorsSuppressed = true;
            BrowserClear();
            control.IsWebBrowserContextMenuEnabled = false;
            control.Document.Window.Name = "Simple";

            control.Document.MouseOver += new HtmlElementEventHandler(Document_MouseOver);

            this.controlBase = (Control)control;
        }
        public void Document_MouseOver(object sender, HtmlElementEventArgs e)
        {
            HtmlDocument htmlDocument = (HtmlDocument)sender;
            GetFocus(htmlDocument);
        }
        public void GetFocus(HtmlDocument htmlDocument)
        {
            if ((ownerForm != null) && (ownerForm.ContainsFocus == true))
            {
                htmlDocument.Focus();

            }
        }
        //ドキュメント
        public HtmlDocument Document
        {
            /*set
            {
                this.control.Document = value;
            }*/
            get
            {
                return this.control.Document;
            }
        }
        public bool AllowNavigation
        {
            set
            {
                this.control.AllowNavigation = value;
            }
            get
            {
                return this.control.AllowNavigation;
            }
        }
        public void BrowserClear()
        {
            try
            {
                if (control.Document != null)
                {
                    control.Document.OpenNew(true);
                }
                else
                {
                    control.Navigate("about:blank");
                }
            }
            catch { ;}
        }
        //ドキュメントを開く
        public void OpenNew(bool type)
        {
            this.control.Document.OpenNew(type);
        }
        
        public void Navigate(string text)
        {
            this.control.Navigate(text);
        }

        public void Write(string text)
        {
            this.control.Document.Write(text);
        }

        public Point GetScrollRectangle()
        {
            HtmlElement body = this.control.Document.Body;
            Point scrollpos = new Point(body.ScrollRectangle.X, body.ScrollRectangle.Y);
            return scrollpos;
        }
        
        public void ResetControl()
        {
            //this.control.Anchor = AnchorStyles.None;
            //this.control.Dock = DockStyle.None;
            this.Location = new Point(1, 1);
            this.Size = new Size(100, 100);
        }
    }
}