| 1 | |
| 2 | import os |
| 3 | import sys |
| 4 | |
| 5 | import wx |
| 6 | import wx.html as html |
| 7 | import wx.lib.wxpTag |
| 8 | |
| 9 | from Main import opj |
| 10 | |
| 11 | #---------------------------------------------------------------------- |
| 12 | |
| 13 | # This shows how to catch the OnLinkClicked non-event. (It's a virtual |
| 14 | # method in the C++ code...) |
| 15 | class MyHtmlWindow(html.HtmlWindow): |
| 16 | def __init__(self, parent, id, log): |
| 17 | html.HtmlWindow.__init__(self, parent, id, style=wx.NO_FULL_REPAINT_ON_RESIZE) |
| 18 | self.log = log |
| 19 | self.Bind(wx.EVT_SCROLLWIN, self.OnScroll ) |
| 20 | if "gtk2" in wx.PlatformInfo: |
| 21 | self.SetStandardFonts() |
| 22 | |
| 23 | def OnScroll( self, event ): |
| 24 | #print 'event.GetOrientation()',event.GetOrientation() |
| 25 | #print 'event.GetPosition()',event.GetPosition() |
| 26 | event.Skip() |
| 27 | |
| 28 | def OnLinkClicked(self, linkinfo): |
| 29 | self.log.WriteText('OnLinkClicked: %s\n' % linkinfo.GetHref()) |
| 30 | |
| 31 | # Virtuals in the base class have been renamed with base_ on the front. |
| 32 | self.base_OnLinkClicked(linkinfo) |
| 33 | |
| 34 | |
| 35 | def OnSetTitle(self, title): |
| 36 | self.log.WriteText('OnSetTitle: %s\n' % title) |
| 37 | self.base_OnSetTitle(title) |
| 38 | |
| 39 | def OnCellMouseHover(self, cell, x, y): |
| 40 | self.log.WriteText('OnCellMouseHover: %s, (%d %d)\n' % (cell, x, y)) |
| 41 | self.base_OnCellMouseHover(cell, x, y) |
| 42 | |
| 43 | def OnCellClicked(self, cell, x, y, evt): |
| 44 | self.log.WriteText('OnCellClicked: %s, (%d %d)\n' % (cell, x, y)) |
| 45 | self.base_OnCellClicked(cell, x, y, evt) |
| 46 | |
| 47 | |
| 48 | # This filter doesn't really do anything but show how to use filters |
| 49 | class MyHtmlFilter(html.HtmlFilter): |
| 50 | def __init__(self, log): |
| 51 | html.HtmlFilter.__init__(self) |
| 52 | self.log = log |
| 53 | |
| 54 | # This method decides if this filter is able to read the file |
| 55 | def CanRead(self, fsfile): |
| 56 | self.log.write("CanRead: %s\n" % fsfile.GetMimeType()) |
| 57 | return False |
| 58 | |
| 59 | # If CanRead returns True then this method is called to actually |
| 60 | # read the file and return the contents. |
| 61 | def ReadFile(self, fsfile): |
| 62 | return "" |
| 63 | |
| 64 | |
| 65 | class TestHtmlPanel(wx.Panel): |
| 66 | def __init__(self, parent, frame, log): |
| 67 | wx.Panel.__init__(self, parent, -1, style=wx.NO_FULL_REPAINT_ON_RESIZE) |
| 68 | self.log = log |
| 69 | self.frame = frame |
| 70 | self.cwd = os.path.split(sys.argv[0])[0] |
| 71 | |
| 72 | if not self.cwd: |
| 73 | self.cwd = os.getcwd() |
| 74 | if frame: |
| 75 | self.titleBase = frame.GetTitle() |
| 76 | |
| 77 | html.HtmlWindow_AddFilter(MyHtmlFilter(log)) |
| 78 | |
| 79 | self.html = MyHtmlWindow(self, -1, log) |
| 80 | self.html.SetRelatedFrame(frame, self.titleBase + " -- %s") |
| 81 | self.html.SetRelatedStatusBar(0) |
| 82 | |
| 83 | self.printer = html.HtmlEasyPrinting() |
| 84 | |
| 85 | self.box = wx.BoxSizer(wx.VERTICAL) |
| 86 | self.box.Add(self.html, 1, wx.GROW) |
| 87 | |
| 88 | subbox = wx.BoxSizer(wx.HORIZONTAL) |
| 89 | |
| 90 | btn = wx.Button(self, -1, "Load File") |
| 91 | self.Bind(wx.EVT_BUTTON, self.OnLoadFile, btn) |
| 92 | subbox.Add(btn, 1, wx.GROW | wx.ALL, 2) |
| 93 | |
| 94 | btn = wx.Button(self, -1, "Load URL") |
| 95 | self.Bind(wx.EVT_BUTTON, self.OnLoadURL, btn) |
| 96 | subbox.Add(btn, 1, wx.GROW | wx.ALL, 2) |
| 97 | |
| 98 | btn = wx.Button(self, -1, "With Widgets") |
| 99 | self.Bind(wx.EVT_BUTTON, self.OnWithWidgets, btn) |
| 100 | subbox.Add(btn, 1, wx.GROW | wx.ALL, 2) |
| 101 | |
| 102 | btn = wx.Button(self, -1, "Back") |
| 103 | self.Bind(wx.EVT_BUTTON, self.OnBack, btn) |
| 104 | subbox.Add(btn, 1, wx.GROW | wx.ALL, 2) |
| 105 | |
| 106 | btn = wx.Button(self, -1, "Forward") |
| 107 | self.Bind(wx.EVT_BUTTON, self.OnForward, btn) |
| 108 | subbox.Add(btn, 1, wx.GROW | wx.ALL, 2) |
| 109 | |
| 110 | btn = wx.Button(self, -1, "Print") |
| 111 | self.Bind(wx.EVT_BUTTON, self.OnPrint, btn) |
| 112 | subbox.Add(btn, 1, wx.GROW | wx.ALL, 2) |
| 113 | |
| 114 | btn = wx.Button(self, -1, "View Source") |
| 115 | self.Bind(wx.EVT_BUTTON, self.OnViewSource, btn) |
| 116 | subbox.Add(btn, 1, wx.GROW | wx.ALL, 2) |
| 117 | |
| 118 | self.box.Add(subbox, 0, wx.GROW) |
| 119 | self.SetSizer(self.box) |
| 120 | self.SetAutoLayout(True) |
| 121 | |
| 122 | # A button with this ID is created on the widget test page. |
| 123 | self.Bind(wx.EVT_BUTTON, self.OnOk, id=wx.ID_OK) |
| 124 | |
| 125 | self.OnShowDefault(None) |
| 126 | |
| 127 | |
| 128 | def ShutdownDemo(self): |
| 129 | # put the frame title back |
| 130 | if self.frame: |
| 131 | self.frame.SetTitle(self.titleBase) |
| 132 | |
| 133 | |
| 134 | def OnShowDefault(self, event): |
| 135 | name = os.path.join(self.cwd, opj('data/test.htm')) |
| 136 | self.html.LoadPage(name) |
| 137 | |
| 138 | |
| 139 | def OnLoadFile(self, event): |
| 140 | dlg = wx.FileDialog(self, wildcard = '*.htm*', style=wx.OPEN) |
| 141 | |
| 142 | if dlg.ShowModal(): |
| 143 | path = dlg.GetPath() |
| 144 | self.html.LoadPage(path) |
| 145 | |
| 146 | dlg.Destroy() |
| 147 | |
| 148 | |
| 149 | def OnLoadURL(self, event): |
| 150 | dlg = wx.TextEntryDialog(self, "Enter a URL") |
| 151 | |
| 152 | if dlg.ShowModal(): |
| 153 | url = dlg.GetValue() |
| 154 | self.html.LoadPage(url) |
| 155 | |
| 156 | dlg.Destroy() |
| 157 | |
| 158 | |
| 159 | def OnWithWidgets(self, event): |
| 160 | os.chdir(self.cwd) |
| 161 | name = os.path.join(self.cwd, opj('data/widgetTest.htm')) |
| 162 | self.html.LoadPage(name) |
| 163 | |
| 164 | |
| 165 | def OnOk(self, event): |
| 166 | self.log.WriteText("It works!\n") |
| 167 | |
| 168 | def OnBack(self, event): |
| 169 | if not self.html.HistoryBack(): |
| 170 | wx.MessageBox("No more items in history!") |
| 171 | |
| 172 | |
| 173 | def OnForward(self, event): |
| 174 | if not self.html.HistoryForward(): |
| 175 | wx.MessageBox("No more items in history!") |
| 176 | |
| 177 | |
| 178 | def OnViewSource(self, event): |
| 179 | import wx.lib.dialogs |
| 180 | |
| 181 | source = self.html.GetParser().GetSource() |
| 182 | |
| 183 | dlg = wx.lib.dialogs.ScrolledMessageDialog(self, source, 'HTML Source') |
| 184 | dlg.ShowModal() |
| 185 | dlg.Destroy() |
| 186 | |
| 187 | |
| 188 | def OnPrint(self, event): |
| 189 | self.printer.GetPrintData().SetPaperId(wx.PAPER_LETTER) |
| 190 | self.printer.PrintFile(self.html.GetOpenedPage()) |
| 191 | |
| 192 | #---------------------------------------------------------------------- |
| 193 | |
| 194 | def runTest(frame, nb, log): |
| 195 | win = TestHtmlPanel(nb, frame, log) |
| 196 | return win |
| 197 | |
| 198 | |
| 199 | #---------------------------------------------------------------------- |
| 200 | |
| 201 | |
| 202 | overview = """<html><body> |
| 203 | <h2>wx.HtmlWindow</h2> |
| 204 | |
| 205 | <p>wx.HtmlWindow is capable of parsing and rendering most |
| 206 | simple HTML tags. |
| 207 | |
| 208 | <p>It is not intended to be a high-end HTML browser. If you're |
| 209 | looking for something like that see the IEHtmlWin class, which |
| 210 | wraps the core MSIE HTML viewer. |
| 211 | |
| 212 | </body></html> |
| 213 | """ |
| 214 | |
| 215 | |
| 216 | |
| 217 | if __name__ == '__main__': |
| 218 | import sys,os |
| 219 | import run |
| 220 | run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |