]>
Commit | Line | Data |
---|---|---|
f6bcfd97 BP |
1 | """ |
2 | <html><body> | |
3 | This demo shows how to embed an ActiveX control in a wxPython application, (Win32 only.) | |
4 | <p> | |
5 | The MakeActiveXClass function dynamically builds a new Class on the fly, that has the | |
6 | same signature and semantics as wxWindow. This means that when you call the function | |
7 | you get back a new class that you can use just like wxWindow, (set the size and position, | |
8 | use in a sizer, etc.) except its contents will be the COM control. | |
9 | <p> | |
10 | This demo embeds the Adobe Acrobat Reader, and gives you some buttons for opening a PDF | |
11 | file, changing pages, etc. that show how to call methods on the COM object. If you don't | |
12 | have Acrobat Reader 4.0 installed it won't work. | |
13 | </body></html> | |
14 | """ | |
15 | ||
8fa876ca RD |
16 | # 11/24/2003 - Jeff Grimmett (grimmtooth@softhome.net) |
17 | # | |
18 | # o Updated for 2.5 | |
19 | # | |
f6bcfd97 | 20 | |
8fa876ca RD |
21 | import sys |
22 | import wx | |
23 | ||
24 | if wx.Platform == '__WXMSW__': | |
25 | import wx.lib.activexwrapper as ax | |
26 | import win32com.client.gencache | |
f6bcfd97 BP |
27 | |
28 | try: | |
8fa876ca RD |
29 | acrobat = win32com.client.gencache.EnsureModule( |
30 | '{CA8A9783-280D-11CF-A24D-444553540000}', 0x0, 1, 3 | |
31 | ) | |
f6bcfd97 BP |
32 | except: |
33 | raise ImportError("Can't load PDF.OCX, install Acrobat 4.0") | |
34 | ||
35 | ||
f6bcfd97 BP |
36 | #---------------------------------------------------------------------- |
37 | ||
8fa876ca | 38 | class TestPanel(wx.Panel): |
f3d9dc1d | 39 | def __init__(self, parent, log): |
8fa876ca | 40 | wx.Panel.__init__(self, parent, -1) |
f6bcfd97 BP |
41 | self.pdf = None |
42 | ||
8fa876ca RD |
43 | sizer = wx.BoxSizer(wx.VERTICAL) |
44 | btnSizer = wx.BoxSizer(wx.HORIZONTAL) | |
f6bcfd97 BP |
45 | |
46 | # this function creates a new class that can be used as | |
8fa876ca RD |
47 | # a wx.Window, but contains the given ActiveX control. |
48 | ActiveXWrapper = ax.MakeActiveXClass(acrobat.Pdf) | |
f6bcfd97 BP |
49 | |
50 | # create an instance of the new class | |
8fa876ca | 51 | self.pdf = ActiveXWrapper( self, -1, style=wx.SUNKEN_BORDER) |
f6bcfd97 | 52 | |
8fa876ca | 53 | sizer.Add(self.pdf, proportion=1, flag=wx.EXPAND) |
f6bcfd97 | 54 | |
8fa876ca RD |
55 | btn = wx.Button(self, wx.NewId(), "Open PDF File") |
56 | self.Bind(wx.EVT_BUTTON, self.OnOpenButton) | |
57 | btnSizer.Add(btn, proportion=1, flag=wx.EXPAND|wx.ALL, border=5) | |
f6bcfd97 | 58 | |
8fa876ca RD |
59 | btn = wx.Button(self, wx.NewId(), "<-- Previous Page") |
60 | self.Bind(wx.EVT_BUTTON, self.OnPrevPageButton, id=btn.GetId()) | |
61 | btnSizer.Add(btn, proportion=1, flag=wx.EXPAND|wx.ALL, border=5) | |
f6bcfd97 | 62 | |
8fa876ca RD |
63 | btn = wx.Button(self, wx.NewId(), "Next Page -->") |
64 | self.Bind(wx.EVT_BUTTON, self.OnNextPageButton, id=btn.GetId()) | |
65 | btnSizer.Add(btn, proportion=1, flag=wx.EXPAND|wx.ALL, border=5) | |
f6bcfd97 BP |
66 | |
67 | ||
8fa876ca RD |
68 | btnSizer.Add((50,-1), proportion=2, flag=wx.EXPAND) |
69 | sizer.Add(btnSizer, proportion=0, flag=wx.EXPAND) | |
f6bcfd97 BP |
70 | |
71 | self.SetSizer(sizer) | |
1e4a197e | 72 | self.SetAutoLayout(True) |
f6bcfd97 | 73 | |
8fa876ca | 74 | self.Bind(wx.EVT_WINDOW_DESTROY, self.OnDestroy) |
63b6646e RD |
75 | |
76 | ||
77 | def OnDestroy(self, evt): | |
f6bcfd97 BP |
78 | if self.pdf: |
79 | self.pdf.Cleanup() | |
80 | self.pdf = None | |
81 | ||
82 | ||
83 | ||
84 | def OnOpenButton(self, event): | |
8fa876ca RD |
85 | dlg = wx.FileDialog(self, wildcard="*.pdf") |
86 | ||
87 | if dlg.ShowModal() == wx.ID_OK: | |
88 | wx.BeginBusyCursor() | |
f6bcfd97 | 89 | self.pdf.LoadFile(dlg.GetPath()) |
8fa876ca | 90 | wx.EndBusyCursor() |
f6bcfd97 BP |
91 | |
92 | dlg.Destroy() | |
93 | ||
94 | ||
95 | def OnPrevPageButton(self, event): | |
96 | self.pdf.gotoPreviousPage() | |
97 | ||
98 | ||
99 | def OnNextPageButton(self, event): | |
100 | self.pdf.gotoNextPage() | |
101 | ||
102 | ||
103 | ||
104 | #---------------------------------------------------------------------- | |
105 | ||
106 | def runTest(frame, nb, log): | |
8fa876ca | 107 | if wx.Platform == '__WXMSW__': |
f3d9dc1d | 108 | win = TestPanel(nb, log) |
f6bcfd97 BP |
109 | return win |
110 | else: | |
8fa876ca RD |
111 | dlg = wx.MessageDialog(frame, 'This demo only works on MSW.', |
112 | 'Sorry', wx.OK | wx.ICON_INFORMATION) | |
f6bcfd97 BP |
113 | dlg.ShowModal() |
114 | dlg.Destroy() | |
115 | ||
116 | ||
117 | overview = __doc__ | |
118 | ||
119 | #---------------------------------------------------------------------- | |
120 | ||
121 | ||
122 | if __name__ == '__main__': | |
8fa876ca | 123 | class TestFrame(wx.Frame): |
f6bcfd97 | 124 | def __init__(self): |
8fa876ca RD |
125 | wx.Frame.__init__( |
126 | self, None, -1, "ActiveX test -- Acrobat", size=(640, 480), | |
127 | style=wx.DEFAULT_FRAME_STYLE|wx.NO_FULL_REPAINT_ON_RESIZE | |
128 | ) | |
129 | ||
f6bcfd97 | 130 | self.tp = TestPanel(self, sys.stdout) |
f6bcfd97 BP |
131 | |
132 | ||
8fa876ca | 133 | app = wx.PySimpleApp() |
f6bcfd97 | 134 | frame = TestFrame() |
1e4a197e | 135 | frame.Show(True) |
f6bcfd97 BP |
136 | app.MainLoop() |
137 | ||
138 |