import httplib import sys, os from wxPython.wx import * from wxPython.html import * import wxPython.lib.wxpTag #h=httplib.HTTP("starport86331.htmlplanet.com") #h.putrequest('GET', '/index.html') #h.putheader('Accept', 'text/html') #h.putheader('Accept', 'text/plain') #h.endheaders() #h.set_debuglevel (1) #errcode, errmsg, headers = h.getreply() #print errcode,errmsg, headers # Should be 200 #f = h.getfile() #data = f.read() # Get the raw HTML #print errcode # Should be 200 #print data #f.close() #---------------------------------------------------------------------- # This shows how to catch the OnLinkClicked non-event. (It's a virtual # method in the C++ code...) class TomHtmlWindow(wxHtmlWindow): def __init__(self, parent, id): wxHtmlWindow.__init__(self, parent, id) #self.log = log def OnLinkClicked(self, link): #self.log.WriteText('OnLinkClicked: %s\n' % link) # Virtuals in the base class have been renamed with base_ on the font. self.base_OnLinkClicked(link) class TestHtmlPanel(wxPanel): def __init__(self, parent, frame): wxPanel.__init__(self, parent, -1) #self.log = log self.frame = frame self.cwd = os.path.split(sys.argv[0])[0] if not self.cwd: self.cwd = os.getcwd() self.html = TomHtmlWindow(self, -1) self.html.SetRelatedFrame(frame, "tommy") self.html.SetRelatedStatusBar(0) self.printer = wxHtmlEasyPrinting() self.box = wxBoxSizer(wxVERTICAL) self.box.Add(self.html, 1, wxGROW) subbox = wxBoxSizer(wxHORIZONTAL) btn = wxButton(self, 1201, "Show Default") EVT_BUTTON(self, 1201, self.OnShowDefault) subbox.Add(btn, 1, wxGROW | wxALL, 2) btn = wxButton(self, 1202, "Load File") EVT_BUTTON(self, 1202, self.OnLoadFile) subbox.Add(btn, 1, wxGROW | wxALL, 2) btn = wxButton(self, 1203, "With Widgets") EVT_BUTTON(self, 1203, self.OnWithWidgets) subbox.Add(btn, 1, wxGROW | wxALL, 2) btn = wxButton(self, 1204, "Back") EVT_BUTTON(self, 1204, self.OnBack) subbox.Add(btn, 1, wxGROW | wxALL, 2) btn = wxButton(self, 1205, "Forward") EVT_BUTTON(self, 1205, self.OnForward) subbox.Add(btn, 1, wxGROW | wxALL, 2) btn = wxButton(self, 1207, "Print") EVT_BUTTON(self, 1207, self.OnPrint) subbox.Add(btn, 1, wxGROW | wxALL, 2) btn = wxButton(self, 1206, "View Source") EVT_BUTTON(self, 1206, self.OnViewSource) subbox.Add(btn, 1, wxGROW | wxALL, 2) self.box.Add(subbox, 0, wxGROW) self.SetSizer(self.box) self.SetAutoLayout(true) # A button with this ID is created on the widget test page. EVT_BUTTON(self, wxID_OK, self.OnOk) self.OnShowDefault(None) def OnShowDefault(self, event): name = os.path.join(self.cwd, 'data/test.htm') self.html.LoadPage(name) def OnLoadFile(self, event): dlg = wxFileDialog(self, wildcard = '*.htm*', style=wxOPEN) if dlg.ShowModal(): path = dlg.GetPath() self.html.LoadPage(path) dlg.Destroy() def OnWithWidgets(self, event): os.chdir(self.cwd) name = os.path.join(self.cwd, 'data/widgetTest.htm') self.html.LoadPage(name) def OnOk(self, event): #self.log.WriteText("It works!\n") pass def OnBack(self, event): if not self.html.HistoryBack(): wxMessageBox("No more items in history!") def OnForward(self, event): if not self.html.HistoryForward(): wxMessageBox("No more items in history!") def OnViewSource(self, event): from wxPython.lib.dialogs import wxScrolledMessageDialog source = self.html.GetParser().GetSource() dlg = wxScrolledMessageDialog(self, source, 'HTML Source') dlg.ShowModal() dlg.Destroy() def OnPrint(self, event): self.printer.PrintFile(self.html.GetOpenedPage()) #---------------------------------------------------------------------- def runTest(frame, nb): win = TestHtmlPanel(nb, frame) return win #------------------- class TomFrame(wx.wxFrame): def __init__(self, parent, id, title): # First, call the base class' __init__ method to create the frame wx.wxFrame.__init__(self, parent, id, title,wxPoint(100, 100), wxSize(200, 600)) # Associate some events with methods of this class EVT_SIZE(self, self.OnSize) EVT_MOVE(self, self.OnMove) # Add a panel and some controls to display the size and position panel = TomHtmlWindow(self, -1) #wxStaticText(panel, -1, "Size:",wxDLG_PNT(panel, wxPoint(4, 4)), wxDefaultSize) #wxStaticText(panel, -1, "Pos:",wxDLG_PNT(panel, wxPoint(4, 16)), wxDefaultSize) #self.sizeCtrl = wxTextCtrl(panel, -1, "",wxDLG_PNT(panel, wxPoint(24, 4)),wxDLG_SZE(panel, wxSize(36, -1)),wxTE_READONLY) #self.posCtrl = wxTextCtrl(panel, -1, "",wxDLG_PNT(panel, wxPoint(24, 16)),wxDLG_SZE(panel, wxSize(36, -1)),wxTE_READONLY) #print wxDLG_PNT(panel, wxPoint(24, 4)), wxDLG_SZE(panel, wxSize(36, -1)) #print wxDLG_PNT(panel, wxPoint(24, 16)),wxDLG_SZE(panel, wxSize(36, -1)) # This method is called automatically when the CLOSE event is # sent to this window def OnCloseWindow(self, event): # tell the window to kill itself self.Destroy() # This method is called by the System when the window is resized, # because of the association above. def OnSize(self, event): size = event.GetSize() self.sizeCtrl.SetValue("%s, %s" % (size.width, size.height)) # tell the event system to continue looking for an event handler, # so the default handler will get called. event.Skip() # This method is called by the System when the window is moved, # because of the association above. def OnMove(self, event): pos = event.GetPosition() self.posCtrl.SetValue("%s, %s" % (pos.x, pos.y)) #--------------------------------------------------------------------------- #---------------------------------------------------------------------- # Every wxWindows application must have a class derived from wxApp class TomApp(wxApp): # wxWindows calls this method to initialize the application def OnInit(self): # Create an instance of our customized Frame class frame = TomFrame(NULL, -1, "HTML Vewer") frame.Show(true) # Tell wxWindows that this is our main window self.SetTopWindow(frame) # Return a success flag return true #--------------------------------------------------------------------------- app = TomApp(0) # Create an instance of the application class app.MainLoop() # Tell it to start processing events