docutil/dochtml.py
changeset 814 5743cbdff669
child 1490 f03bc6c9c146
equal deleted inserted replaced
813:1460273f40ed 814:5743cbdff669
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
       
     5 #based on the plcopen standard. 
       
     6 #
       
     7 #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
       
     8 #
       
     9 #See COPYING file for copyrights details.
       
    10 #
       
    11 #This library is free software; you can redistribute it and/or
       
    12 #modify it under the terms of the GNU General Public
       
    13 #License as published by the Free Software Foundation; either
       
    14 #version 2.1 of the License, or (at your option) any later version.
       
    15 #
       
    16 #This library is distributed in the hope that it will be useful,
       
    17 #but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    18 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    19 #General Public License for more details.
       
    20 #
       
    21 #You should have received a copy of the GNU General Public
       
    22 #License along with this library; if not, write to the Free Software
       
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    24 
       
    25 import wx, os, wx.html, subprocess
       
    26 
       
    27 HtmlFrameOpened = []
       
    28 
       
    29 def OpenHtmlFrame(self, title, file, size):
       
    30         if title not in HtmlFrameOpened:
       
    31             HtmlFrameOpened.append(title)
       
    32             window = HtmlFrame(self, HtmlFrameOpened)
       
    33             window.SetTitle(title)
       
    34             window.SetHtmlPage(file)
       
    35             window.SetClientSize(size)
       
    36             window.Show()
       
    37 
       
    38 [ID_HTMLFRAME, ID_HTMLFRAMEHTMLCONTENT] = [wx.NewId() for _init_ctrls in range(2)]
       
    39 EVT_HTML_URL_CLICK = wx.NewId()
       
    40 
       
    41 class HtmlWindowUrlClick(wx.PyEvent):
       
    42     def __init__(self, linkinfo):
       
    43         wx.PyEvent.__init__(self)
       
    44         self.SetEventType(EVT_HTML_URL_CLICK)
       
    45         self.linkinfo = (linkinfo.GetHref(), linkinfo.GetTarget())
       
    46         
       
    47 class UrlClickHtmlWindow(wx.html.HtmlWindow):
       
    48     """ HTML window that generates and OnLinkClicked event.
       
    49 
       
    50     Use this to avoid having to override HTMLWindow
       
    51     """
       
    52     def OnLinkClicked(self, linkinfo):
       
    53         wx.PostEvent(self, HtmlWindowUrlClick(linkinfo))
       
    54     
       
    55     def Bind(self, event, handler, source=None, id=wx.ID_ANY, id2=wx.ID_ANY):
       
    56         if event == HtmlWindowUrlClick:
       
    57             self.Connect(-1, -1, EVT_HTML_URL_CLICK, handler)
       
    58         else:
       
    59             wx.html.HtmlWindow.Bind(event, handler, source=source, id=id, id2=id2)
       
    60 
       
    61 class HtmlFrame(wx.Frame):
       
    62         def _init_ctrls(self, prnt):
       
    63             wx.Frame.__init__(self, id=ID_HTMLFRAME, name='HtmlFrame',
       
    64                   parent=prnt, pos=wx.Point(320, 231), size=wx.Size(853, 616),
       
    65                   style=wx.DEFAULT_FRAME_STYLE, title='')
       
    66             self.Bind(wx.EVT_CLOSE, self.OnCloseFrame)
       
    67             
       
    68             self.HtmlContent = UrlClickHtmlWindow(id=ID_HTMLFRAMEHTMLCONTENT,
       
    69                   name='HtmlContent', parent=self, pos=wx.Point(0, 0),
       
    70                   size=wx.Size(-1, -1), style=wx.html.HW_SCROLLBAR_AUTO|wx.html.HW_NO_SELECTION)
       
    71             self.HtmlContent.Bind(HtmlWindowUrlClick, self.OnLinkClick)
       
    72 
       
    73         def __init__(self, parent, opened):
       
    74             self._init_ctrls(parent)
       
    75             self.HtmlFrameOpened = opened
       
    76             
       
    77         def SetHtmlCode(self, htmlcode):
       
    78             self.HtmlContent.SetPage(htmlcode)
       
    79             
       
    80         def SetHtmlPage(self, htmlpage):
       
    81             self.HtmlContent.LoadPage(htmlpage)
       
    82             
       
    83         def OnCloseFrame(self, event):
       
    84             self.HtmlFrameOpened.remove(self.GetTitle())
       
    85             event.Skip()
       
    86         
       
    87         def OnLinkClick(self, event):
       
    88             url = event.linkinfo[0]
       
    89             try:
       
    90                 if wx.Platform == '__WXMSW__':
       
    91                     import webbrowser
       
    92                     webbrowser.open(url)
       
    93                 elif subprocess.call("firefox %s"%url, shell=True) != 0:
       
    94                     wx.MessageBox("""Firefox browser not found.\nPlease point your browser at :\n%s""" % url)
       
    95             except ImportError:
       
    96                 wx.MessageBox('Please point your browser at: %s' % url)