示例#1
0
        protected void AddToolbarAndBrowserToTab(TabPage tabPage, GeckoWebBrowser browser)
        {
            TextBox urlbox = new TextBox();

            urlbox.Top   = 0;
            urlbox.Width = 200;

            Button nav = new Button();

            nav.Text = "Go";
            nav.Left = urlbox.Width;

            Button newTab = new Button();

            newTab.Text = "NewTab";
            newTab.Left = nav.Left + nav.Width;

            Button stop = new Button
            {
                Text = "Stop",
                Left = newTab.Left + newTab.Width
            };

            Button closeTab = new Button();

            closeTab.Text = "GC.Collect";
            closeTab.Left = stop.Left + stop.Width;

            Button closeWithDisposeTab = new Button();

            closeWithDisposeTab.Text = "Close";
            closeWithDisposeTab.Left = closeTab.Left + closeTab.Width;

            Button open = new Button();

            open.Text = "FileOpen";
            open.Left = closeWithDisposeTab.Left + closeWithDisposeTab.Width;

            Button print = new Button();

            print.Text = "Print";
            print.Left = open.Left + open.Width;

            Button scrollDown = new Button {
                Text = "Down", Left = closeWithDisposeTab.Left + 250
            };
            Button scrollUp = new Button {
                Text = "Up", Left = closeWithDisposeTab.Left + 330
            };

            scrollDown.Click += (s, e) => { browser.Window.ScrollByPages(1); };
            scrollUp.Click   += (s, e) => { browser.Window.ScrollByPages(-1); };

            nav.Click += delegate
            {
                // use javascript to warn if url box is empty.
                if (string.IsNullOrEmpty(urlbox.Text.Trim()))
                {
                    browser.Navigate("javascript:alert('hey try typing a url!');");
                }
                browser.Navigate(urlbox.Text);
            };

            newTab.Click += delegate { AddTab(); };

            stop.Click += delegate { browser.Stop(); };

            closeTab.Click += delegate {
                GC.Collect();
                GC.WaitForPendingFinalizers();
            };

            closeWithDisposeTab.Click += delegate
            {
                m_tabControl.Controls.Remove(tabPage);
                tabPage.Dispose();
            };

            open.Click += (s, a) =>
            {
                nsIFilePicker filePicker = Xpcom.CreateInstance <nsIFilePicker>("@mozilla.org/filepicker;1");
#if PORTFF60
                filePicker.Init(browser.Window.DomWindow, new nsAString("hello"), nsIFilePickerConsts.modeOpen);
                filePicker.AppendFilter(new nsAString("png"), new nsAString("*.png"));
                filePicker.AppendFilter(new nsAString("html"), new nsAString("*.html"));
                if (nsIFilePickerConsts.returnOK == filePicker.Show())
                {
                    using (nsACString str = new nsACString())
                    {
                        filePicker.GetFileAttribute().GetNativePathAttribute(str);
                        browser.Navigate(str.ToString());
                    }
                }
#endif
                throw new NotImplementedException();
            };
            //url in Navigating event may be the mapped version,
            //e.g. about:config in Navigating event is jar:file:///<xulrunner>/omni.ja!/chrome/toolkit/content/global/config.xul
            browser.Navigating += (s, e) =>
            {
                Console.WriteLine("Navigating: url: " + e.Uri + ", top: " + e.DomWindowTopLevel);

                // ContextFlagAttribute is arbitrary value
                if (browser.GetContextFlagsAttribute() == 1)
                {
                    // UKAC - basic popup blocker after CreateWindow
                    if (MessageBox.Show($"Block popup from: {e.Uri}?", "Popup Blocker", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        e.Cancel = true;
                        m_tabControl.TabPages.Remove(tabPage);
                        return;
                    }
                    else
                    {
                        tabPage.Show();
                        m_tabControl.SelectedTab = tabPage;
                    }
                }
            };
            browser.Navigated += (s, e) =>
            {
                if (e.DomWindowTopLevel)
                {
                    urlbox.Text = e.Uri.ToString();
                }
                Console.WriteLine("Navigated: url: " + e.Uri + ", top: " + e.DomWindowTopLevel, ", errorPage: " + e.IsErrorPage);
            };

            browser.Retargeted += (s, e) =>
            {
                var ch = e.Request as Gecko.Net.Channel;
                Console.WriteLine("Retargeted: url: " + e.Uri + ", contentType: " + ch.ContentType + ", top: " + e.DomWindowTopLevel);
            };
            browser.DocumentCompleted += (s, e) =>
            {
                Console.WriteLine("DocumentCompleted: url: " + e.Uri + ", top: " + e.IsTopLevel);
            };

            print.Click += delegate { browser.Window.Print(); };

            tabPage.Controls.Add(urlbox);
            tabPage.Controls.Add(nav);
            tabPage.Controls.Add(newTab);
            tabPage.Controls.Add(stop);
            tabPage.Controls.Add(closeTab);
            tabPage.Controls.Add(closeWithDisposeTab);
            tabPage.Controls.Add(open);
            tabPage.Controls.Add(print);
            tabPage.Controls.Add(browser);
            tabPage.Controls.Add(scrollDown);
            tabPage.Controls.Add(scrollUp);
        }