1#!/usr/bin/env python 2 3import sys 4import wx 5 6if wx.Platform == '__WXMSW__': 7 from wx.lib.pdfwin import PDFWindow 8 9 10#---------------------------------------------------------------------- 11 12class TestPanel(wx.Panel): 13 def __init__(self, parent, log): 14 wx.Panel.__init__(self, parent, -1) 15 16 mainsizer = wx.BoxSizer(wx.HORIZONTAL) 17 leftsizer = wx.BoxSizer(wx.VERTICAL) 18 self.pdf = PDFWindow(self, style=wx.SUNKEN_BORDER) 19 leftsizer.Add(self.pdf, proportion=1, flag=wx.EXPAND) 20 21 box = wx.StaticBox(self, wx.ID_ANY, "" ) 22 buttonsizer = wx.StaticBoxSizer(box, wx.HORIZONTAL ) 23 24 b1 = wx.Button(self, wx.ID_ANY, "First") 25 buttonsizer.Add(b1, proportion=0, flag=wx.ALIGN_CENTER|wx.ALL, border=5) 26 self.Bind(wx.EVT_BUTTON, self.OnFirstPageButton, b1) 27 28 b2 = wx.Button(self, wx.ID_ANY, "Previous") 29 buttonsizer.Add(b2, proportion=0, flag=wx.ALIGN_CENTER|wx.ALL, border=5) 30 self.Bind(wx.EVT_BUTTON, self.OnPreviousPageButton, b2) 31 32 tx1 = wx.StaticText(self, wx.ID_ANY, " Go to page" ) 33 buttonsizer.Add(tx1, proportion=0, flag=wx.ALIGN_CENTER|wx.ALL, border=5) 34 tc1 = wx.TextCtrl(self, wx.ID_ANY, "0", size=[30,-1]) 35 buttonsizer.Add( tc1, proportion=0, flag=wx.ALIGN_CENTER|wx.ALL, border=5) 36 self.Bind(wx.EVT_TEXT, self.OnGotoPage, tc1) 37 38 b3 = wx.Button(self, wx.ID_ANY, "Next") 39 buttonsizer.Add(b3, proportion=0, flag=wx.ALIGN_CENTER|wx.ALL, border=5) 40 self.Bind(wx.EVT_BUTTON, self.OnNextPageButton, b3) 41 42 b4 = wx.Button(self, wx.ID_ANY, "Last") 43 buttonsizer.Add(b4, proportion=0, flag=wx.ALIGN_CENTER|wx.ALL, border=5) 44 self.Bind(wx.EVT_BUTTON, self.OnLastPageButton, b4) 45 46 tx2 = wx.StaticText(self, wx.ID_ANY, " Zoom") 47 buttonsizer.Add(tx2, proportion=0, flag=wx.ALIGN_CENTER|wx.ALL, border=5) 48 49 ch1 = wx.Choice(self, wx.ID_ANY, 50 choices=["Default", "Fit", "FitH", "FitV", 51 "25%", "50%", "75%", "100%", "125%", "200%", "400%"]) 52 ch1.SetSelection(0) 53 buttonsizer.Add(ch1, proportion=0, flag=wx.ALIGN_CENTER|wx.ALL, border=5) 54 self.Bind(wx.EVT_CHOICE, self.OnZoom, ch1) 55 56 leftsizer.Add(buttonsizer, proportion=0) 57 mainsizer.Add(leftsizer, proportion=1, flag=wx.GROW|wx.ALIGN_CENTER_HORIZONTAL|wx.ALL, border=5) 58 59 box = wx.StaticBox(self, wx.ID_ANY, "" ) 60 rightsizer = wx.StaticBoxSizer(box, wx.VERTICAL) 61 62 b5 = wx.Button(self, wx.ID_ANY, "Load PDF") 63 rightsizer.Add(b5, proportion=0, flag=wx.ALIGN_CENTER_VERTICAL|wx.ALL, border=5) 64 self.Bind(wx.EVT_BUTTON, self.OnLoadButton, b5) 65 66 b6 = wx.Button(self, wx.ID_ANY, "Print") 67 rightsizer.Add(b6, proportion=0, flag=wx.ALIGN_CENTER_VERTICAL|wx.ALL, border=5) 68 self.Bind(wx.EVT_BUTTON, self.OnPrintButton, b6) 69 70 tx3 = wx.StaticText(self, wx.ID_ANY, "Page mode:") 71 rightsizer.Add(tx3, proportion=0, flag=wx.ALIGN_CENTER_VERTICAL|wx.ALL, border=5) 72 73 ch2 = wx.Choice(self, wx.ID_ANY,size=[100,-1], 74 choices=["None", "Bookmarks", "Thumbs"]) 75 ch2.SetSelection(0) 76 rightsizer.Add(ch2, proportion=0, flag=wx.ALIGN_CENTER_VERTICAL|wx.ALL, border=5) 77 self.Bind(wx.EVT_CHOICE, self.OnPageMode, ch2) 78 79 tx4 = wx.StaticText(self, wx.ID_ANY, "Layout mode:") 80 rightsizer.Add(tx4, proportion=0, flag=wx.ALIGN_CENTER_VERTICAL|wx.ALL, border=5) 81 82 ch3 = wx.Choice(self, wx.ID_ANY,size=[100,-1], 83 choices=["DontCare", "SinglePage", 84 "OneColumn", "TwoColumnLeft", "TwoColumnRight" ]) 85 ch3.SetSelection(0) 86 rightsizer.Add(ch3, proportion=0, flag=wx.ALIGN_CENTER_VERTICAL|wx.ALL, border=5) 87 self.Bind(wx.EVT_CHOICE, self.OnLayoutMode, ch3) 88 89 cx1 = wx.CheckBox(self, wx.ID_ANY, "Toolbar") 90 cx1.SetValue( True ) 91 rightsizer.Add( cx1,proportion=0, flag=wx.ALIGN_CENTER_VERTICAL|wx.ALL, border=5) 92 self.Bind(wx.EVT_CHECKBOX, self.OnToolbar, cx1) 93 94 cx2 = wx.CheckBox(self, wx.ID_ANY, "Scrollbars") 95 cx2.SetValue( True ) 96 rightsizer.Add( cx2,proportion=0, flag=wx.ALIGN_CENTER_VERTICAL|wx.ALL, border=5) 97 self.Bind(wx.EVT_CHECKBOX, self.OnScrollbars, cx2) 98 99 mainsizer.Add( rightsizer, proportion=0, flag=wx.ALL, border=15) 100 self.SetSizer(mainsizer) 101 self.SetAutoLayout(True) 102 103 def OnFirstPageButton(self, event): 104 self.pdf.gotoFirstPage() 105 106 def OnPreviousPageButton(self, event): 107 self.pdf.gotoPreviousPage() 108 109 def OnNextPageButton(self, event): 110 self.pdf.gotoNextPage() 111 112 def OnLastPageButton(self, event): 113 self.pdf.gotoLastPage() 114 115 def OnGotoPage(self, event): 116 npage = event.GetEventObject().GetValue() 117 try: 118 self.pdf.setCurrentPage(int(npage)) 119 except ValueError: 120 pass 121 122 def OnZoom(self, event): 123 astring = event.GetEventObject().GetStringSelection() 124 if astring.startswith('Fit'): 125 self.pdf.setView(astring) 126 else: 127 try: 128 percent = float(astring.replace('%','')) 129 self.pdf.setZoom(percent) 130 except ValueError: 131 pass 132 133 def OnLoadButton(self, event): 134 dlg = wx.FileDialog(self, wildcard="*.pdf") 135 if dlg.ShowModal() == wx.ID_OK: 136 wx.BeginBusyCursor() 137 self.pdf.LoadFile(dlg.GetPath()) 138 wx.EndBusyCursor() 139 dlg.Destroy() 140 141 def OnPrintButton(self, event): 142 self.pdf.Print() 143 144 def OnPageMode(self, event): 145 astring = event.GetEventObject().GetStringSelection() 146 self.pdf.setPageMode(astring.lower()) 147 148 def OnLayoutMode(self, event): 149 astring = event.GetEventObject().GetStringSelection() 150 self.pdf.setLayoutMode(astring) 151 152 def OnToolbar(self, event): 153 on = event.GetEventObject().GetValue() 154 self.pdf.setShowToolbar(on) 155 156 def OnScrollbars(self, event): 157 on = event.GetEventObject().GetValue() 158 self.pdf.setShowScrollbars(on) 159 160#---------------------------------------------------------------------- 161 162def runTest(frame, nb, log): 163 if wx.Platform == '__WXMSW__': 164 win = TestPanel(nb, log) 165 return win 166 else: 167 from wx.lib.msgpanel import MessagePanel 168 win = MessagePanel(nb, 'This demo only works on Microsoft Windows.', 169 'Sorry', wx.ICON_WARNING) 170 return win 171 172 173overview = """\ 174<html><body> 175<h2>wx.lib.pdfwin.PDFWindow</h2> 176 177The wx.lib.pdfwin.PDFWindow class is another example of using ActiveX 178controls from wxPython using the new wx.activex module. This allows 179you to use an ActiveX control as if it is a wx.Window, you can call 180its methods, set/get properties, and receive events from the ActiveX 181control in a very intuitive way. 182 183<p> Using this class is simpler than ActiveXWrapper, doesn't rely on 184the win32all extensions, and is more "wx\'ish", meaning that it uses 185events and etc. as would be expected from any other wx window. 186 187<p> This demo embeds the Adobe Acrobat Reader, and gives you some 188buttons for opening a PDF file, changing pages, etc. that show how to 189call methods on the COM object. If you don't have Acrobat Reader 4.0 190(or greater) installed it won't work. 191 192</body></html> 193""" 194 195#---------------------------------------------------------------------- 196 197 198 199if __name__ == '__main__': 200 import sys,os 201 import run 202 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) 203 204 205