|
2 | 2 | // |
3 | 3 | // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. |
4 | 4 |
|
5 | | -using CefSharp.Example; |
6 | | -using CefSharp.WinForms.Example.Controls; |
7 | 5 | using System; |
8 | | -using System.Threading.Tasks; |
9 | 6 | using System.Windows.Forms; |
| 7 | +using CefSharp.Example; |
10 | 8 |
|
11 | 9 | namespace CefSharp.WinForms.Example |
12 | 10 | { |
13 | 11 | public partial class BrowserForm : Form |
14 | 12 | { |
15 | | - private readonly ChromiumWebBrowser browser; |
| 13 | + private const string DefaultUrlForAddedTabs = "https://www.google.com"; |
16 | 14 |
|
17 | 15 | public BrowserForm() |
18 | 16 | { |
19 | 17 | InitializeComponent(); |
20 | 18 |
|
21 | | - Load += BrowserFormLoad; |
22 | | - |
23 | 19 | Text = "CefSharp"; |
24 | 20 | WindowState = FormWindowState.Maximized; |
25 | 21 |
|
26 | | - browser = new ChromiumWebBrowser(CefExample.DefaultUrl) |
| 22 | + AddTab(CefExample.DefaultUrl); |
| 23 | + } |
| 24 | + |
| 25 | + private void AddTab(string url, int? insertIndex = null) |
| 26 | + { |
| 27 | + browserTabControl.SuspendLayout(); |
| 28 | + |
| 29 | + var browser = new BrowserTabUserControl(url) |
27 | 30 | { |
28 | 31 | Dock = DockStyle.Fill, |
29 | 32 | }; |
30 | | - toolStripContainer.ContentPanel.Controls.Add(browser); |
31 | 33 |
|
32 | | - browser.DownloadHandler = new DownloadHandler(); |
33 | | - browser.MenuHandler = new MenuHandler(); |
34 | | - browser.NavStateChanged += OnBrowserNavStateChanged; |
35 | | - browser.ConsoleMessage += OnBrowserConsoleMessage; |
36 | | - browser.StatusMessage += OnBrowserStatusMessage; |
37 | | - browser.TitleChanged += OnBrowserTitleChanged; |
38 | | - browser.AddressChanged += OnBrowserAddressChanged; |
| 34 | + var tabPage = new TabPage(url) |
| 35 | + { |
| 36 | + Dock = DockStyle.Fill |
| 37 | + }; |
| 38 | + |
| 39 | + tabPage.Controls.Add(browser); |
39 | 40 |
|
40 | | - var version = String.Format("Chromium: {0}, CEF: {1}, CefSharp: {2}", Cef.ChromiumVersion, Cef.CefVersion, Cef.CefSharpVersion); |
41 | | - DisplayOutput(version); |
42 | | - } |
| 41 | + if (insertIndex == null) |
| 42 | + { |
| 43 | + browserTabControl.TabPages.Add(tabPage); |
| 44 | + } |
| 45 | + else |
| 46 | + { |
| 47 | + browserTabControl.TabPages.Insert(insertIndex.Value, tabPage); |
| 48 | + } |
43 | 49 |
|
44 | | - private void BrowserFormLoad(object sender, EventArgs e) |
45 | | - { |
46 | | - ToggleBottomToolStrip(); |
47 | | - } |
| 50 | + //Make newly created tab active |
| 51 | + browserTabControl.SelectedTab = tabPage; |
48 | 52 |
|
49 | | - private void OnBrowserConsoleMessage(object sender, ConsoleMessageEventArgs args) |
50 | | - { |
51 | | - DisplayOutput(string.Format("Line: {0}, Source: {1}, Message: {2}", args.Line, args.Source, args.Message)); |
| 53 | + browserTabControl.ResumeLayout(true); |
52 | 54 | } |
53 | 55 |
|
54 | | - private void OnBrowserStatusMessage(object sender, StatusMessageEventArgs args) |
| 56 | + private void ExitMenuItemClick(object sender, EventArgs e) |
55 | 57 | { |
56 | | - this.InvokeOnUiThreadIfRequired(() => statusLabel.Text = args.Value); |
| 58 | + ExitApplication(); |
57 | 59 | } |
58 | 60 |
|
59 | | - private void OnBrowserNavStateChanged(object sender, NavStateChangedEventArgs args) |
| 61 | + private void ExitApplication() |
60 | 62 | { |
61 | | - SetCanGoBack(args.CanGoBack); |
62 | | - SetCanGoForward(args.CanGoForward); |
63 | | - |
64 | | - this.InvokeOnUiThreadIfRequired(() => SetIsLoading(!args.CanReload)); |
| 63 | + Cef.Shutdown(); |
| 64 | + Close(); |
65 | 65 | } |
66 | 66 |
|
67 | | - private void OnBrowserTitleChanged(object sender, TitleChangedEventArgs args) |
| 67 | + private void AboutToolStripMenuItemClick(object sender, EventArgs e) |
68 | 68 | { |
69 | | - this.InvokeOnUiThreadIfRequired(() => Text = args.Title); |
| 69 | + new AboutBox().ShowDialog(); |
70 | 70 | } |
71 | 71 |
|
72 | | - private void OnBrowserAddressChanged(object sender, AddressChangedEventArgs args) |
| 72 | + private void FindMenuItemClick(object sender, EventArgs e) |
73 | 73 | { |
74 | | - this.InvokeOnUiThreadIfRequired(() => urlTextBox.Text = args.Address); |
| 74 | + var control = GetCurrentTabControl(); |
| 75 | + if (control != null) |
| 76 | + { |
| 77 | + control.ShowFind(); |
| 78 | + } |
75 | 79 | } |
76 | 80 |
|
77 | | - private void SetCanGoBack(bool canGoBack) |
| 81 | + private void CopySourceToClipBoardAsyncClick(object sender, EventArgs e) |
78 | 82 | { |
79 | | - this.InvokeOnUiThreadIfRequired(() => backButton.Enabled = canGoBack); |
| 83 | + var control = GetCurrentTabControl(); |
| 84 | + if (control != null) |
| 85 | + { |
| 86 | + control.CopySourceToClipBoardAsync(); |
| 87 | + } |
80 | 88 | } |
81 | 89 |
|
82 | | - private void SetCanGoForward(bool canGoForward) |
| 90 | + private BrowserTabUserControl GetCurrentTabControl() |
83 | 91 | { |
84 | | - this.InvokeOnUiThreadIfRequired(() => forwardButton.Enabled = canGoForward); |
85 | | - } |
| 92 | + if (browserTabControl.SelectedIndex == -1) |
| 93 | + { |
| 94 | + return null; |
| 95 | + } |
86 | 96 |
|
87 | | - private void SetIsLoading(bool isLoading) |
88 | | - { |
89 | | - goButton.Text = isLoading ? |
90 | | - "Stop" : |
91 | | - "Go"; |
92 | | - goButton.Image = isLoading ? |
93 | | - Properties.Resources.nav_plain_red : |
94 | | - Properties.Resources.nav_plain_green; |
| 97 | + var tabPage = browserTabControl.Controls[browserTabControl.SelectedIndex]; |
| 98 | + var control = (BrowserTabUserControl)tabPage.Controls[0]; |
95 | 99 |
|
96 | | - HandleToolStripLayout(); |
| 100 | + return control; |
97 | 101 | } |
98 | 102 |
|
99 | | - public void ExecuteScript(string script) |
| 103 | + private void NewTabToolStripMenuItemClick(object sender, EventArgs e) |
100 | 104 | { |
101 | | - browser.ExecuteScriptAsync(script); |
| 105 | + AddTab(DefaultUrlForAddedTabs); |
102 | 106 | } |
103 | 107 |
|
104 | | - public object EvaluateScript(string script) |
| 108 | + private void CloseTabToolStripMenuItemClick(object sender, EventArgs e) |
105 | 109 | { |
106 | | - return browser.EvaluateScript(script); |
107 | | - } |
| 110 | + if(browserTabControl.Controls.Count == 0) |
| 111 | + { |
| 112 | + return; |
| 113 | + } |
108 | 114 |
|
109 | | - public void DisplayOutput(string output) |
110 | | - { |
111 | | - this.InvokeOnUiThreadIfRequired(() => outputLabel.Text = output); |
112 | | - } |
| 115 | + var currentIndex = browserTabControl.SelectedIndex; |
113 | 116 |
|
114 | | - private void HandleToolStripLayout(object sender, LayoutEventArgs e) |
115 | | - { |
116 | | - HandleToolStripLayout(); |
117 | | - } |
| 117 | + var tabPage = browserTabControl.Controls[currentIndex]; |
118 | 118 |
|
119 | | - private void HandleToolStripLayout() |
120 | | - { |
121 | | - var width = toolStrip1.Width; |
122 | | - foreach (ToolStripItem item in toolStrip1.Items) |
| 119 | + var control = GetCurrentTabControl(); |
| 120 | + if (control != null) |
123 | 121 | { |
124 | | - if (item != urlTextBox) |
125 | | - { |
126 | | - width -= item.Width - item.Margin.Horizontal; |
127 | | - } |
| 122 | + control.Dispose(); |
128 | 123 | } |
129 | | - urlTextBox.Width = Math.Max(0, width - urlTextBox.Margin.Horizontal - 18); |
130 | | - } |
131 | 124 |
|
132 | | - private void ExitMenuItemClick(object sender, EventArgs e) |
133 | | - { |
134 | | - browser.Dispose(); |
135 | | - Cef.Shutdown(); |
136 | | - Close(); |
137 | | - } |
138 | | - |
139 | | - private void GoButtonClick(object sender, EventArgs e) |
140 | | - { |
141 | | - LoadUrl(urlTextBox.Text); |
142 | | - } |
143 | | - |
144 | | - private void BackButtonClick(object sender, EventArgs e) |
145 | | - { |
146 | | - browser.Back(); |
147 | | - } |
| 125 | + browserTabControl.Controls.Remove(tabPage); |
148 | 126 |
|
149 | | - private void ForwardButtonClick(object sender, EventArgs e) |
150 | | - { |
151 | | - browser.Forward(); |
152 | | - } |
| 127 | + browserTabControl.SelectedIndex = currentIndex - 1; |
153 | 128 |
|
154 | | - private void UrlTextBoxKeyUp(object sender, KeyEventArgs e) |
155 | | - { |
156 | | - if (e.KeyCode != Keys.Enter) |
| 129 | + if (browserTabControl.Controls.Count == 0) |
157 | 130 | { |
158 | | - return; |
| 131 | + ExitApplication(); |
159 | 132 | } |
160 | | - |
161 | | - LoadUrl(urlTextBox.Text); |
162 | 133 | } |
163 | 134 |
|
164 | | - private void LoadUrl(string url) |
| 135 | + private void UndoMenuItemClick(object sender, EventArgs e) |
165 | 136 | { |
166 | | - if (Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute)) |
| 137 | + var control = GetCurrentTabControl(); |
| 138 | + if (control != null) |
167 | 139 | { |
168 | | - browser.Load(url); |
| 140 | + control.Browser.Undo(); |
169 | 141 | } |
170 | 142 | } |
171 | 143 |
|
172 | | - private void AboutToolStripMenuItemClick(object sender, EventArgs e) |
| 144 | + private void RedoMenuItemClick(object sender, EventArgs e) |
173 | 145 | { |
174 | | - new AboutBox().ShowDialog(); |
175 | | - } |
176 | | - |
177 | | - private void FindCloseButtonClick(object sender, EventArgs e) |
178 | | - { |
179 | | - ToggleBottomToolStrip(); |
180 | | - } |
181 | | - |
182 | | - private void FindMenuItemClick(object sender, EventArgs e) |
183 | | - { |
184 | | - ToggleBottomToolStrip(); |
| 146 | + var control = GetCurrentTabControl(); |
| 147 | + if (control != null) |
| 148 | + { |
| 149 | + control.Browser.Redo(); |
| 150 | + } |
185 | 151 | } |
186 | 152 |
|
187 | | - private void ToggleBottomToolStrip() |
| 153 | + private void CutMenuItemClick(object sender, EventArgs e) |
188 | 154 | { |
189 | | - if (toolStripContainer.BottomToolStripPanelVisible) |
| 155 | + var control = GetCurrentTabControl(); |
| 156 | + if (control != null) |
190 | 157 | { |
191 | | - browser.StopFinding(true); |
192 | | - toolStripContainer.BottomToolStripPanelVisible = false; |
193 | | - } |
194 | | - else |
195 | | - { |
196 | | - toolStripContainer.BottomToolStripPanelVisible = true; |
197 | | - findTextBox.Focus(); |
| 158 | + control.Browser.Cut(); |
198 | 159 | } |
199 | 160 | } |
200 | 161 |
|
201 | | - private void FindNextButtonClick(object sender, EventArgs e) |
| 162 | + private void CopyMenuItemClick(object sender, EventArgs e) |
202 | 163 | { |
203 | | - Find(true); |
| 164 | + var control = GetCurrentTabControl(); |
| 165 | + if (control != null) |
| 166 | + { |
| 167 | + control.Browser.Copy(); |
| 168 | + } |
204 | 169 | } |
205 | 170 |
|
206 | | - private void FindPreviousButtonClick(object sender, EventArgs e) |
| 171 | + private void PasteMenuItemClick(object sender, EventArgs e) |
207 | 172 | { |
208 | | - Find(false); |
| 173 | + var control = GetCurrentTabControl(); |
| 174 | + if (control != null) |
| 175 | + { |
| 176 | + control.Browser.Paste(); |
| 177 | + } |
209 | 178 | } |
210 | 179 |
|
211 | | - private void Find(bool next) |
| 180 | + private void DeleteMenuItemClick(object sender, EventArgs e) |
212 | 181 | { |
213 | | - if (!string.IsNullOrEmpty(findTextBox.Text)) |
| 182 | + var control = GetCurrentTabControl(); |
| 183 | + if (control != null) |
214 | 184 | { |
215 | | - browser.Find(0, findTextBox.Text, next, false, false); |
| 185 | + control.Browser.Delete(); |
216 | 186 | } |
217 | 187 | } |
218 | 188 |
|
219 | | - private void FindTextBoxKeyDown(object sender, KeyEventArgs e) |
| 189 | + private void SelectAllMenuItemClick(object sender, EventArgs e) |
220 | 190 | { |
221 | | - if (e.KeyCode != Keys.Enter) |
| 191 | + var control = GetCurrentTabControl(); |
| 192 | + if (control != null) |
222 | 193 | { |
223 | | - return; |
| 194 | + control.Browser.SelectAll(); |
224 | 195 | } |
225 | | - |
226 | | - Find(true); |
227 | 196 | } |
228 | 197 |
|
229 | | - private void CopySourceToClipBoardAsyncClick(object sender, EventArgs e) |
| 198 | + private void PrintToolStripMenuItemClick(object sender, EventArgs e) |
230 | 199 | { |
231 | | - var task = browser.GetSourceAsync(); |
232 | | - |
233 | | - task.ContinueWith(t => |
| 200 | + var control = GetCurrentTabControl(); |
| 201 | + if (control != null) |
234 | 202 | { |
235 | | - if (!t.IsFaulted) |
236 | | - { |
237 | | - Clipboard.SetText(t.Result); |
238 | | - DisplayOutput("HTML Source copied to clipboard"); |
239 | | - } |
240 | | - }, |
241 | | - TaskScheduler.FromCurrentSynchronizationContext()); |
| 203 | + control.Browser.Print(); |
| 204 | + } |
242 | 205 | } |
243 | 206 |
|
244 | 207 | private void ShowDevToolsMenuItemClick(object sender, EventArgs e) |
245 | 208 | { |
246 | | - browser.ShowDevTools(); |
| 209 | + var control = GetCurrentTabControl(); |
| 210 | + if (control != null) |
| 211 | + { |
| 212 | + control.Browser.ShowDevTools(); |
| 213 | + } |
247 | 214 | } |
248 | 215 |
|
249 | 216 | private void CloseDevToolsMenuItemClick(object sender, EventArgs e) |
250 | 217 | { |
251 | | - browser.CloseDevTools(); |
| 218 | + var control = GetCurrentTabControl(); |
| 219 | + if (control != null) |
| 220 | + { |
| 221 | + control.Browser.CloseDevTools(); |
| 222 | + } |
252 | 223 | } |
253 | 224 | } |
254 | 225 | } |
0 commit comments