laurent@565: #!/usr/bin/env python laurent@565: # -*- coding: utf-8 -*- laurent@565: laurent@565: #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor laurent@565: #based on the plcopen standard. laurent@565: # laurent@565: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD laurent@565: # laurent@565: #See COPYING file for copyrights details. laurent@565: # laurent@565: #This library is free software; you can redistribute it and/or laurent@565: #modify it under the terms of the GNU General Public laurent@565: #License as published by the Free Software Foundation; either laurent@565: #version 2.1 of the License, or (at your option) any later version. laurent@565: # laurent@565: #This library is distributed in the hope that it will be useful, laurent@565: #but WITHOUT ANY WARRANTY; without even the implied warranty of laurent@565: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU laurent@565: #General Public License for more details. laurent@565: # laurent@565: #You should have received a copy of the GNU General Public laurent@565: #License along with this library; if not, write to the Free Software laurent@565: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA laurent@565: laurent@565: import wx, os, wx.html, subprocess laurent@565: laurent@565: HtmlFrameOpened = [] laurent@565: laurent@565: def OpenHtmlFrame(self, title, file, size): laurent@565: if title not in HtmlFrameOpened: laurent@565: HtmlFrameOpened.append(title) laurent@565: window = HtmlFrame(self, HtmlFrameOpened) laurent@565: window.SetTitle(title) laurent@565: window.SetHtmlPage(file) laurent@565: window.SetClientSize(size) laurent@565: window.Show() laurent@565: laurent@565: [ID_HTMLFRAME, ID_HTMLFRAMEHTMLCONTENT] = [wx.NewId() for _init_ctrls in range(2)] laurent@565: EVT_HTML_URL_CLICK = wx.NewId() laurent@565: laurent@565: class HtmlWindowUrlClick(wx.PyEvent): laurent@565: def __init__(self, linkinfo): laurent@565: wx.PyEvent.__init__(self) laurent@565: self.SetEventType(EVT_HTML_URL_CLICK) laurent@565: self.linkinfo = (linkinfo.GetHref(), linkinfo.GetTarget()) laurent@565: laurent@565: class UrlClickHtmlWindow(wx.html.HtmlWindow): laurent@565: """ HTML window that generates and OnLinkClicked event. laurent@565: laurent@565: Use this to avoid having to override HTMLWindow laurent@565: """ laurent@565: def OnLinkClicked(self, linkinfo): laurent@565: wx.PostEvent(self, HtmlWindowUrlClick(linkinfo)) laurent@565: laurent@565: def Bind(self, event, handler, source=None, id=wx.ID_ANY, id2=wx.ID_ANY): laurent@565: if event == HtmlWindowUrlClick: laurent@565: self.Connect(-1, -1, EVT_HTML_URL_CLICK, handler) laurent@565: else: laurent@565: wx.html.HtmlWindow.Bind(event, handler, source=source, id=id, id2=id2) laurent@565: laurent@565: class HtmlFrame(wx.Frame): laurent@565: def _init_ctrls(self, prnt): laurent@565: wx.Frame.__init__(self, id=ID_HTMLFRAME, name='HtmlFrame', laurent@565: parent=prnt, pos=wx.Point(320, 231), size=wx.Size(853, 616), laurent@565: style=wx.DEFAULT_FRAME_STYLE, title='') laurent@565: self.Bind(wx.EVT_CLOSE, self.OnCloseFrame) laurent@565: laurent@565: self.HtmlContent = UrlClickHtmlWindow(id=ID_HTMLFRAMEHTMLCONTENT, laurent@565: name='HtmlContent', parent=self, pos=wx.Point(0, 0), laurent@565: size=wx.Size(-1, -1), style=wx.html.HW_SCROLLBAR_AUTO|wx.html.HW_NO_SELECTION) laurent@565: self.HtmlContent.Bind(HtmlWindowUrlClick, self.OnLinkClick) laurent@565: laurent@565: def __init__(self, parent, opened): laurent@565: self._init_ctrls(parent) laurent@565: self.HtmlFrameOpened = opened laurent@565: laurent@565: def SetHtmlCode(self, htmlcode): laurent@565: self.HtmlContent.SetPage(htmlcode) laurent@565: laurent@565: def SetHtmlPage(self, htmlpage): laurent@565: self.HtmlContent.LoadPage(htmlpage) laurent@565: laurent@565: def OnCloseFrame(self, event): laurent@565: self.HtmlFrameOpened.remove(self.GetTitle()) laurent@565: event.Skip() laurent@565: laurent@565: def OnLinkClick(self, event): laurent@565: url = event.linkinfo[0] laurent@565: try: laurent@565: if wx.Platform == '__WXMSW__': laurent@565: import webbrowser laurent@565: webbrowser.open(url) laurent@565: elif subprocess.call("firefox %s"%url, shell=True) != 0: laurent@565: wx.MessageBox("""Firefox browser not found.\nPlease point your browser at :\n%s""" % url) laurent@565: except ImportError: laurent@565: wx.MessageBox('Please point your browser at: %s' % url)