-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWebNavigationBox.cs
177 lines (153 loc) · 5.35 KB
/
WebNavigationBox.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
namespace Xilium.CefGlue.Demo
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using WebBrowser = Xilium.CefGlue.Demo.Browser.WebBrowser;
public partial class WebNavigationBox : Control
{
private readonly Button _goBackButton;
private readonly Button _goForwardButton;
private readonly Button _stopButton;
private readonly Button _refreshButton;
private readonly Button _homeButton;
private readonly TextBox _addressTextBox;
private readonly Button _goButton;
private WebBrowser _browser;
public WebNavigationBox()
{
InitializeComponent();
Height = 36;
_goBackButton = CreateButton("Back", "appbar.arrow.left.png", OnGoBack);
_goForwardButton = CreateButton("Forward", "appbar.arrow.right.png", OnGoForward);
_stopButton = CreateButton("Stop", "appbar.close.png", OnStop);
_refreshButton = CreateButton("Refresh", "appbar.refresh.png", OnRefresh);
_homeButton = CreateButton("Home", "appbar.home.png", OnHome);
_goButton = CreateButton("Go", "go.png", OnGo, DockStyle.Right);
_addressTextBox = new TextBox();
_addressTextBox.Parent = this;
_addressTextBox.Dock = DockStyle.Fill;
_addressTextBox.BringToFront();
_addressTextBox.KeyPress += (s, e) =>
{
if (e.KeyChar == '\xD') { e.Handled = true; OnGo(s, EventArgs.Empty); }
};
CanGoBack = false;
CanGoForward = false;
Loading = false;
}
// TODO: fix button creation using ImageList, and resize images to other size, tooltips.
// Yes, we doesn't use any *Strip controls, 'cause from one side they are really buggy, and from other side - this code more similar to GTK port.
private Button CreateButton(string title, string stockImage, EventHandler action, DockStyle dockStyle = DockStyle.Left)
{
var button = new Button();
button.Image = Image.FromStream(GetImageStream(stockImage));
button.Width = 36; // button.Image.Width;
button.Height = 36; // button.Image.Height;
button.Text = "";
button.TextImageRelation = TextImageRelation.Overlay;
button.ImageAlign = ContentAlignment.MiddleCenter;
button.TextAlign = ContentAlignment.MiddleCenter;
button.Parent = this;
button.Dock = dockStyle;
button.BringToFront();
button.Click += action;
return button;
}
private static Stream GetImageStream(string name)
{
var type = typeof(WebNavigationBox);
return type.Assembly.GetManifestResourceStream(type.Namespace + ".Resources." + name);
}
public string HomeUrl { get; set; }
public string Address
{
get { return _addressTextBox.Text; }
set { _addressTextBox.Text = value; }
}
public bool CanGoBack
{
get { return _goBackButton.Enabled; }
set { _goBackButton.Enabled = value; }
}
public bool CanGoForward
{
get { return _goForwardButton.Enabled; }
set { _goForwardButton.Enabled = value; }
}
public bool Loading
{
get { return _stopButton.Visible; }
set
{
_stopButton.Visible = value;
_refreshButton.Visible = !value;
}
}
public void Attach(WebBrowser browser)
{
_browser = browser;
}
public void NavigateTo(string url)
{
if (_browser != null)
{
_browser.CefBrowser.StopLoad();
Address = url;
_browser.CefBrowser.GetMainFrame().LoadUrl(url);
}
}
private void OnGoBack(object sender, EventArgs e)
{
if (_browser != null)
{
_browser.CefBrowser.GoBack();
}
}
private void OnGoForward(object sender, EventArgs e)
{
if (_browser != null)
{
_browser.CefBrowser.GoForward();
}
}
private void OnStop(object sender, EventArgs e)
{
if (_browser != null)
{
_browser.CefBrowser.StopLoad();
}
}
private void OnRefresh(object sender, EventArgs e)
{
if (_browser != null)
{
_browser.CefBrowser.Reload();
}
}
private void OnHome(object sender, EventArgs e)
{
if (_browser != null)
{
_browser.CefBrowser.GetMainFrame().LoadUrl(HomeUrl);
}
}
private void OnGo(object sender, EventArgs e)
{
if (_browser != null)
{
_browser.CefBrowser.GetMainFrame().LoadUrl(_addressTextBox.Text);
}
}
public CefBrowser GetBrowser()
{
return _browser.CefBrowser;
}
}
}