~ubuntu-branches/ubuntu/utopic/python-traitsui/utopic

« back to all changes in this revision

Viewing changes to traitsui/wx/html_editor.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-07-09 13:57:39 UTC
  • Revision ID: james.westby@ubuntu.com-20110709135739-x5u20q86huissmn1
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#------------------------------------------------------------------------------
 
2
#
 
3
#  Copyright (c) 2005, Enthought, Inc.
 
4
#  All rights reserved.
 
5
#
 
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
 
10
#
 
11
#  Thanks for using Enthought open source!
 
12
#
 
13
#  Author: David C. Morrill
 
14
#  Date:   10/21/2004
 
15
#
 
16
#------------------------------------------------------------------------------
 
17
 
 
18
""" Defines the HTML "editor" for the wxPython user interface toolkit.
 
19
    HTML editors interpret and display HTML-formatted text, but do not
 
20
    modify it.
 
21
"""
 
22
 
 
23
#-------------------------------------------------------------------------------
 
24
#  Imports:
 
25
#-------------------------------------------------------------------------------
 
26
 
 
27
import os.path
 
28
import webbrowser
 
29
 
 
30
import wx.html as wh
 
31
 
 
32
from traits.api import Str
 
33
 
 
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
 
38
 
 
39
from editor import Editor
 
40
 
 
41
#-------------------------------------------------------------------------------
 
42
#  URLResolvingHtmlWindow class:
 
43
#-------------------------------------------------------------------------------
 
44
 
 
45
class URLResolvingHtmlWindow( wh.HtmlWindow ):
 
46
    """ Overrides OnOpeningURL method of HtmlWindow to append the base URL
 
47
        local links.
 
48
    """
 
49
 
 
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
 
54
 
 
55
    def OnLinkClicked ( self, link_info ):
 
56
        """ Handle the base url and opening in a new browser window for links.
 
57
        """
 
58
        if self.open_externally:
 
59
            url = link_info.GetHref()
 
60
            if (self.base_url and
 
61
                not url.startswith( ( 'http://', 'https://' ) )):
 
62
                url = self.base_url + url
 
63
            if not url.startswith( ( 'file://', 'http://', 'https://' ) ):
 
64
                url = 'file://' + url
 
65
            webbrowser.open_new( url )
 
66
 
 
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
 
71
            OnLinkClicked.
 
72
        """
 
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
 
76
        else:
 
77
            return wh.HTML_OPEN
 
78
 
 
79
#-------------------------------------------------------------------------------
 
80
#  'SimpleEditor' class:
 
81
#-------------------------------------------------------------------------------
 
82
 
 
83
class SimpleEditor ( Editor ):
 
84
    """ Simple style of editor for HTML, which displays interpreted HTML.
 
85
    """
 
86
    #---------------------------------------------------------------------------
 
87
    #  Trait definitions:
 
88
    #---------------------------------------------------------------------------
 
89
 
 
90
    # Is the HTML editor scrollable? This values override the default.
 
91
    scrollable = True
 
92
 
 
93
    # External objects referenced in the HTML are relative to this URL
 
94
    base_url = Str
 
95
 
 
96
    #---------------------------------------------------------------------------
 
97
    #  Finishes initializing the editor by creating the underlying toolkit
 
98
    #  widget:
 
99
    #---------------------------------------------------------------------------
 
100
 
 
101
    def init ( self, parent ):
 
102
        """ Finishes initializing the editor by creating the underlying toolkit
 
103
            widget.
 
104
        """
 
105
        self.control = URLResolvingHtmlWindow( parent ,
 
106
                                               self.factory.open_externally,
 
107
                                               self.base_url )
 
108
        self.control.SetBorders( 2 )
 
109
 
 
110
        self.base_url = self.factory.base_url
 
111
        self.sync_value( self.factory.base_url_name, 'base_url', 'from' )
 
112
 
 
113
    #---------------------------------------------------------------------------
 
114
    #  Updates the editor when the object trait changes external to the editor:
 
115
    #---------------------------------------------------------------------------
 
116
 
 
117
    def update_editor ( self ):
 
118
        """ Updates the editor when the object trait changes external to the
 
119
            editor.
 
120
        """
 
121
        text = self.str_value
 
122
        if self.factory.format_text:
 
123
            text = self.factory.parse_text( text )
 
124
        self.control.SetPage( text )
 
125
 
 
126
    #-- Event Handlers ---------------------------------------------------------
 
127
 
 
128
    def _base_url_changed(self):
 
129
        url = self.base_url
 
130
        if not url.endswith( '/' ):
 
131
            url += '/'
 
132
        self.control.base_url = url
 
133
        self.update_editor()
 
134
 
 
135
#--EOF-------------------------------------------------------------------------