Debian Codegraph @

traitsui/wx/extra/windows/ie_html_editor.py

raw
1 # (C) Copyright 2004-2023 Enthought, Inc., Austin, TX
2 # All rights reserved.
3 #
4 # This software is provided without warranty under the terms of the BSD
5 # license included in LICENSE.txt and may be redistributed only under
6 # the conditions described in the aforementioned license. The license
7 # is also available online at http://www.enthought.com/licenses/BSD.txt
8 #
9 # Thanks for using Enthought open source!
10
11 """ Traits UI MS Internet Explorer editor.
12 """
13
14
15 import re
16 import webbrowser
17
18 import wx
19
20
21 if wx.Platform == "__WXMSW__":
22 # The new version of IEHTMLWindow (wx 2.8.8.0) is mostly compatible with
23 # the old one, but it has changed the API for handling COM events, so we
24 # cannot use it.
25 try:
26 import wx.lib.iewin_old as iewin
27 except ImportError:
28 import wx.lib.iewin as iewin
29
30 from traits.api import Bool, Event, Property, Str
31
32 from traitsui.wx.editor import Editor
33
34 from traitsui.basic_editor_factory import BasicEditorFactory
35
36 # -------------------------------------------------------------------------
37 # Constants
38 # -------------------------------------------------------------------------
39
40 RELATIVE_OBJECTS_PATTERN = re.compile(
41 r'src=["\'](?!https?:)([\s\w/\.]+?)["\']', re.IGNORECASE
42 )
43
44
45 class _IEHTMLEditor(Editor):
46 """Traits UI MS Internet Explorer editor."""
47
48 # -------------------------------------------------------------------------
49 # Trait definitions:
50 # -------------------------------------------------------------------------
51
52 #: Is the table editor is scrollable? This value overrides the default.
53 scrollable = True
54
55 #: External objects referenced in the HTML are relative to this url
56 base_url = Str()
57
58 #: Event fired when the browser home page should be displayed:
59 home = Event()
60
61 #: Event fired when the browser should show the previous page:
62 back = Event()
63
64 #: Event fired when the browser should show the next page:
65 forward = Event()
66
67 #: Event fired when the browser should stop loading the current page:
68 stop = Event()
69
70 #: Event fired when the browser should refresh the current page:
71 refresh = Event()
72
73 #: Event fired when the browser should search the current page:
74 search = Event()
75
76 #: The current browser status:
77 status = Str()
78
79 #: The current browser page title:
80 title = Str()
81
82 #: The URL of the page that just finished loading:
83 page_loaded = Str()
84
85 #: The current page content as HTML:
86 html = Property()
87
88 def init(self, parent):
89 """Finishes initializing the editor by creating the underlying toolkit
90 widget.
91 """
92 self.control = ie = iewin.IEHtmlWindow(
93 parent, -1, style=wx.NO_FULL_REPAINT_ON_RESIZE
94 )
95 self.set_tooltip()
96
97 factory = self.factory
98 self.base_url = factory.base_url
99 self.sync_value(factory.home, "home", "from")
100 self.sync_value(factory.back, "back", "from")
101 self.sync_value(factory.forward, "forward", "from")
102 self.sync_value(factory.stop, "stop", "from")
103 self.sync_value(factory.refresh, "refresh", "from")
104 self.sync_value(factory.search, "search", "from")
105 self.sync_value(factory.status, "status", "to")
106 self.sync_value(factory.title, "title", "to")
107 self.sync_value(factory.page_loaded, "page_loaded", "to")
108 self.sync_value(factory.html, "html", "to")
109 self.sync_value(factory.base_url_name, "base_url", "from")
110
111 parent.Bind(iewin.EVT_StatusTextChange, ie, id=self._status_modified)
112 parent.Bind(iewin.EVT_TitleChange, ie, id=self._title_modified)
113 parent.Bind(
114 iewin.EVT_DocumentComplete, ie, id=self._page_loaded_modified
115 )
116 parent.Bind(iewin.EVT_NewWindow2, ie, id=self._new_window_modified)
117 parent.Bind(iewin.EVT_BeforeNavigate2, ie, id=self._navigate_requested)
118
119 def update_editor(self):
120 """Updates the editor when the object trait changes externally to the
121 editor.
122 """
123 value = self.str_value.strip()
124
125 # We can correct URLs via the BeforeNavigate Event, but the COM
126 # interface provides no such option for images. Sadly, we are forced
127 # to take a more brute force approach.
128 if self.base_url:
129 rep = lambda m: r'src="%s%s"' % (self.base_url, m.group(1))
130 value = re.sub(RELATIVE_OBJECTS_PATTERN, rep, value)
131
132 if value == "":
133 self.control.LoadString("<html><body></body></html>")
134
135 elif value[:1] == "<":
136 self.control.LoadString(value)
137
138 elif (value[:4] != "http") or (value.find("://") < 0):
139 try:
140 with open(value, "rb") as file:
141 self.control.LoadStream(file)
142 except:
143 pass
144
145 else:
146 self.control.Navigate(value)
147
148 # -- Property Implementations ---------------------------------------------
149
150 def _get_html(self):
151 return self.control.GetText()
152
153 def _set_html(self, value):
154 self.control.LoadString(value)
155
156 # -- Event Handlers -------------------------------------------------------
157
158 def _home_changed(self):
159 self.control.GoHome()
160
161 def _back_changed(self):
162 self.control.GoBack()
163
164 def _forward_changed(self):
165 self.control.GoForward()
166
167 def _stop_changed(self):
168 self.control.Stop()
169
170 def _search_changed(self):
171 self.control.GoSearch()
172
173 def _refresh_changed(self):
174 self.control.Refresh(iewin.REFRESH_COMPLETELY)
175
176 def _status_modified(self, event):
177 self.status = event.Text
178
179 def _title_modified(self, event):
180 self.title = event.Text
181
182 def _page_loaded_modified(self, event):
183 self.page_loaded = event.URL
184 self.trait_property_changed("html", "", self.html)
185
186 def _new_window_modified(self, event):
187 # If the event is cancelled, new windows can be disabled.
188 # At this point we've opted to allow new windows
189 pass
190
191 def _navigate_requested(self, event):
192 # The way NavigateToString works is to navigate to about:blank then
193 # load the supplied HTML into the document property. This borks
194 # relative URLs.
195 if event.URL.startswith("about:"):
196 base = self.base_url
197 if not base.endswith("/"):
198 base += "/"
199 event.URL = base + event.URL[6:]
200
201 if self.factory.open_externally:
202 event.Cancel = True
203 webbrowser.get("windows-default").open_new(event.URL)
204
205
206 # -------------------------------------------------------------------------
207 # Create the editor factory object:
208 # -------------------------------------------------------------------------
209
210 # wxPython editor factory for MS Internet Explorer editors:
211
212
213 class IEHTMLEditor(BasicEditorFactory):
214
215 #: The editor class to be created:
216 klass = _IEHTMLEditor
217
218 #: External objects referenced in the HTML are relative to this url
219 base_url = Str()
220
221 #: The object trait containing the base URL
222 base_url_name = Str()
223
224 #: Should links be opened in an external browser?
225 open_externally = Bool(False)
226
227 #: Optional name of trait used to tell browser to show Home page:
228 home = Str()
229
230 #: Optional name of trait used to tell browser to view the previous page:
231 back = Str()
232
233 #: Optional name of trait used to tell browser to view the next page:
234 forward = Str()
235
236 #: Optional name of trait used to tell browser to stop loading page:
237 stop = Str()
238
239 #: Optional name of trait used to tell browser to refresh the current page:
240 refresh = Str()
241
242 #: Optional name of trait used to tell browser to search the current page:
243 search = Str()
244
245 #: Optional name of trait used to contain the current browser status:
246 status = Str()
247
248 #: Optional name of trait used to contain the current browser page title:
249 title = Str()
250
251 #: Optional name of trait used to contain the URL of the page that just
252 #: completed loading:
253 page_loaded = Str()
254
255 #: Optional name of trait used to get/set the page content as HTML:
256 html = Str()