1
#------------------------------------------------------------------------------
3
# Copyright (c) 2005, Enthought, Inc.
6
# This software is provided without warranty under the terms of the BSD
7
# license included in enthought/LICENSE.txt and may be redistributed only
8
# under the conditions described in the aforementioned license. The license
9
# is also available online at http://www.enthought.com/licenses/BSD.txt
11
# Thanks for using Enthought open source!
13
# Author: David C. Morrill
16
#------------------------------------------------------------------------------
18
""" Defines the HTML "editor" for the wxPython user interface toolkit.
19
HTML editors interpret and display HTML-formatted text, but do not
23
#-------------------------------------------------------------------------------
25
#-------------------------------------------------------------------------------
32
from traits.api import Str
34
# FIXME: ToolkitEditorFactory is a proxy class defined here just for backward
35
# compatibility. The class has been moved to the
36
# traitsui.editors.html_editor file.
37
from traitsui.editors.html_editor import ToolkitEditorFactory
39
from editor import Editor
41
#-------------------------------------------------------------------------------
42
# URLResolvingHtmlWindow class:
43
#-------------------------------------------------------------------------------
45
class URLResolvingHtmlWindow( wh.HtmlWindow ):
46
""" Overrides OnOpeningURL method of HtmlWindow to append the base URL
50
def __init__( self, parent, open_externally, base_url ):
51
wh.HtmlWindow.__init__( self, parent )
52
self.open_externally = open_externally
53
self.base_url = base_url
55
def OnLinkClicked ( self, link_info ):
56
""" Handle the base url and opening in a new browser window for links.
58
if self.open_externally:
59
url = link_info.GetHref()
61
not url.startswith( ( 'http://', 'https://' ) )):
62
url = self.base_url + url
63
if not url.startswith( ( 'file://', 'http://', 'https://' ) ):
65
webbrowser.open_new( url )
67
def OnOpeningURL( self, url_type, url ):
68
""" According to the documentation, this method is supposed to be called
69
for both images and link clicks, but it appears to only be called
70
for image loading, hence the base url handling code in
73
if (self.base_url and not os.path.isabs(url) and
74
not url.startswith( ( 'http://', 'https://', self.base_url ) )):
75
return self.base_url + url
79
#-------------------------------------------------------------------------------
80
# 'SimpleEditor' class:
81
#-------------------------------------------------------------------------------
83
class SimpleEditor ( Editor ):
84
""" Simple style of editor for HTML, which displays interpreted HTML.
86
#---------------------------------------------------------------------------
88
#---------------------------------------------------------------------------
90
# Is the HTML editor scrollable? This values override the default.
93
# External objects referenced in the HTML are relative to this URL
96
#---------------------------------------------------------------------------
97
# Finishes initializing the editor by creating the underlying toolkit
99
#---------------------------------------------------------------------------
101
def init ( self, parent ):
102
""" Finishes initializing the editor by creating the underlying toolkit
105
self.control = URLResolvingHtmlWindow( parent ,
106
self.factory.open_externally,
108
self.control.SetBorders( 2 )
110
self.base_url = self.factory.base_url
111
self.sync_value( self.factory.base_url_name, 'base_url', 'from' )
113
#---------------------------------------------------------------------------
114
# Updates the editor when the object trait changes external to the editor:
115
#---------------------------------------------------------------------------
117
def update_editor ( self ):
118
""" Updates the editor when the object trait changes external to the
121
text = self.str_value
122
if self.factory.format_text:
123
text = self.factory.parse_text( text )
124
self.control.SetPage( text )
126
#-- Event Handlers ---------------------------------------------------------
128
def _base_url_changed(self):
130
if not url.endswith( '/' ):
132
self.control.base_url = url
135
#--EOF-------------------------------------------------------------------------