# # This file is part of Documancer (http://documancer.sf.net) # # Copyright (C) 2002-2005 Vaclav Slavik # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # $Id: HtmlBrowser.py,v 1.24 2005/02/06 09:14:26 vaclavslavik Exp $ # # Abstract interface to various browsers (Mozilla, MSIE, wxHTML) # from wxPython import * from wxPython.wx import * from wxPython.html import * import sys, os, os.path import utils # Prefered browser: HtmlBrowser = None # Specific browsers: IEBrowserClass = None MozillaBrowserClass = None # ------------------------------------------------------------------------- # wxIEHtmlWindow # ------------------------------------------------------------------------- if sys.platform == 'win32': try: from wxPython.iewin import * class IEBrowser(wxIEHtmlWin): name = 'msie' def __init__(self, parent, id): wxIEHtmlWin.__init__(self, parent, id) self.OnPageChanged = lambda x: x self.OnStatusChanged = lambda x,y: x self.OnTitleChanged = lambda x: x self.OnProgress = lambda x: x self.url = '' self.title = '' EVT_MSHTML_BEFORENAVIGATE2(self, -1, self.__OnUrlChanged) EVT_MSHTML_STATUSTEXTCHANGE(self, -1, self.__OnStatusChanged) EVT_MSHTML_TITLECHANGE(self, -1, self.__OnTitleChanged) EVT_MSHTML_PROGRESSCHANGE(self, -1, self.__OnProgress) def __OnUrlChanged(self, event): self.url = event.URL self.OnPageChanged(self.url) def __OnStatusChanged(self, event): self.OnStatusChanged(event.Text, False) def __OnTitleChanged(self, event): self.title = event.Text self.OnTitleChanged(event.Text) def __OnProgress(self, event): if event.ProgressMax != -1: self.OnProgress(float(event.Progress) / float(event.ProgressMax)) def LoadURL(self, url): self.LoadUrl(url) def GetPageTitle(self): return self.GetTitle() def GetPageURL(self): return self.url def SavePrefs(self): pass #wxMozillaSettings_SavePrefs() IEBrowserClass = IEBrowser IEBrowserVersion = 'Internet Explorer' except: pass # ------------------------------------------------------------------------- # Mozilla # ------------------------------------------------------------------------- try: from wxPython.mozilla import * import wxPython.mozilla # is it new enough wxMozilla? try: MOZILLA_VERSION = wxPython.mozilla.__version__ except AttributeError: raise RuntimeError('Documancer needs wxMozilla >= 0.5.3') # Setting Mozilla path doesn't work if you run the documancer.py # source file on Win because DOCUMANCER_HOME will be pointing to # the src dir. So check to see if the Mozilla path exists before # adding it. This way the system Python's wxMozilla libs will be # used if mozpath is incorrect. mozpath = '%s\\win32\\mozilla' % os.environ['DOCUMANCER_HOME'] if sys.platform == 'win32' and os.path.exists(mozpath): wxMozillaSettings_SetMozillaPath(mozpath) wxMozillaSettings_SetProfilePath( os.path.join(utils.getConfigDir(), 'mozilla-profile')) wxMozillaSettings_SetIntPref("browser.downloadmanager.behavior", 1) class MozillaBrowser(wxMozillaBrowser): name = 'mozilla' def __init__(self, parent, id): wxMozillaBrowser.__init__(self, parent, id) self.OnPageChanged = lambda x: x self.OnStatusChanged = lambda x,y: x self.OnTitleChanged = lambda x: x self.OnProgress = lambda x: x self.url = '' EVT_MOZILLA_URL_CHANGED(self, -1, self.__OnUrlChanged) EVT_MOZILLA_STATUS_CHANGED(self, -1, self.__OnStatusChanged) EVT_MOZILLA_TITLE_CHANGED(self, -1, self.__OnTitleChanged) EVT_MOZILLA_PROGRESS(self, -1, self.__OnProgress) def __OnUrlChanged(self, event): self.url = event.GetNewURL() self.OnPageChanged(self.url) def __OnStatusChanged(self, event): self.OnStatusChanged(event.GetStatusText(), event.IsBusy()) def __OnTitleChanged(self, event): self.OnTitleChanged(event.GetTitle()) def __OnProgress(self, event): if event.GetTotalMaxProgress() != -1: self.OnProgress(float(event.GetTotalCurrentProgress()) / float(event.GetTotalMaxProgress())) elif event.GetSelfMaxProgress() != -1: self.OnProgress(float(event.GetSelfCurrentProgress()) / float(event.GetSelfMaxProgress())) def GetPageTitle(self): return self.GetTitle() def GetPageURL(self): return self.url def SavePrefs(self): wxMozillaSettings_SavePrefs() MozillaBrowserClass = MozillaBrowser MozillaBrowserVersion = 'Mozilla (wxMozilla %i.%i.%i)' % wxPython.mozilla.__version__ except ImportError: pass # ------------------------------------------------------------------------- # wxHtmlWindow (fallback) # ------------------------------------------------------------------------- # wxHTML is worst of all, don't bother with it if there's any other option: if MozillaBrowserClass == None and IEBrowserClass == None: print 'warning: using minimalistic HTML renderer (wxHTML),' print ' please install wxMozilla!' class wxHtmlBrowser(wxHtmlWindow): name = 'wxhtml' def __init__(self, parent, id): wxHtmlWindow.__init__(self, parent, id) self.OnPageChanged = lambda x: x def LoadURL(self, url): self.LoadPage(url) def GoBack(self): self.HistoryBack() def GoForward(self): self.HistoryForward() def GetPageTitle(self): return self.GetOpenedPageTitle() def GetPageURL(self): return self.GetOpenedPage() def CanGoBack(self): return self.HistoryCanBack() def CanGoForward(self): return self.HistoryCanForward() def Stop(self): pass def Reload(self): pass def SavePrefs(self): pass HtmlBrowser = wxHtmlBrowser import wxPython browserVersion = 'wxWidgets built-in (version %s)' % wxPython.wx.__version__ else: # choose preferred browser: if MozillaBrowserClass != None: HtmlBrowser = MozillaBrowserClass browserVersion = MozillaBrowserVersion elif IEBrowserClass != None: HtmlBrowser = IEBrowserClass browserVersion = IEBrowserVersion def create(parent, id, backend=None): """Creates browser instance, using provided backend, if available.""" if backend == 'msie' and IEBrowserClass != None: return IEBrowser(parent, id) elif backend == 'mozilla' and MozillaBrowserClass != None: return MozillaBrowser(parent, id) else: return HtmlBrowser(parent, id) def determineBrowser(backend): """Returns name of browser to use, provided the user would like to use specified one (which may be None or not supported by this build).""" if (backend == 'msie' and IEBrowserClass != None): return 'msie' if (backend == 'mozilla' and MozillaBrowserClass != None): return 'mozilla' return HtmlBrowser.name