]> git.lyx.org Git - lyx.git/blob - development/Win32/pdfview/pdfview.nsi
move GuiImage::loadableFormats() to support/imagetools.{h,cpp} and simplify this...
[lyx.git] / development / Win32 / pdfview / pdfview.nsi
1 /*
2
3 Windows PDF view helper
4 Author: Joost Verburg
5
6 This will be installed as pdfview.exe.
7
8 The application will launch the default PDF viewer to display the PDF file,
9 but works around the file locking problems of Adobe Reader.
10
11 Source code of pdfopen/pdfclose is available at:
12 http://magic.aladdin.cs.cmu.edu/2005/07/15/pdfopen-and-pdfclose/
13
14 */
15
16 !include LogicLib.nsh
17 !include FileFunc.nsh
18
19 # Functions from FileFunc.nsh
20 !insertmacro GetParameters
21 !insertmacro GetFileName
22 !insertmacro GetParent
23
24 #--------------------------------
25 # Settings
26
27 Caption "PDF Viewer"
28 OutFile pdfview.exe
29 Icon "..\packaging\icons\lyx.ico"
30 SilentInstall silent
31
32 #--------------------------------
33 # Windows Vista settings
34
35 RequestExecutionLevel user
36
37 #--------------------------------
38 # Constants
39
40 !define FALSE 0
41 !define TRUE 1
42
43 !define FILE_NOTIFY_CHANGE_LAST_WRITE 0x00000010
44 !define WAIT_TIMEOUT 0x00000102
45
46 #--------------------------------
47 # Variables
48
49 Var Character
50 Var RunAppReturn
51
52 Var OriginalFile
53 Var OriginalFileName
54 Var OriginalDir
55
56 Var PDFFile
57 Var Viewer
58
59 Var ChangeNotification
60 Var WaitReturn
61 Var LockedFile
62
63 Var OriginalTimeHigh
64 Var OriginalTimeLow
65 Var CurrentTimeHigh
66 Var CurrentTimeLow
67
68 #--------------------------------
69 # Macros
70
71 !macro SystemCall STACK
72
73   # Call a Windows API function
74
75   Push `${STACK}`
76   CallInstDLL "$EXEDIR\System.dll" Call
77
78 !macroend
79
80 !macro HideConsole COMMAND_LINE
81
82   # Run an application and hide console output
83
84   Push `${COMMAND_LINE}`
85   CallInstDLL "$EXEDIR\Console.dll" Exec
86   Pop $RunAppReturn
87   
88   ${If} $RunAppReturn == "error"
89     MessageBox MB_OK|MB_ICONSTOP "Error opening PDF file $PDFFile."
90   ${EndIf}
91
92 !macroend
93
94 #--------------------------------
95 # PDF viewing
96
97 Section "View PDF file"
98
99   InitPluginsDir # Temporary directory for PDF file
100
101   # Command line parameters
102   ${GetParameters} $OriginalFile
103
104   # Trim quotes
105   StrCpy $Character $OriginalFile 1
106   ${If} $Character == '"'
107     StrCpy $OriginalFile $OriginalFile "" 1
108   ${EndIf}
109   StrCpy $Character $OriginalFile 1 -1
110   ${If} $Character == '"'
111     StrCpy $OriginalFile $OriginalFile -1
112   ${EndIf}
113
114   GetFullPathName $OriginalFile $OriginalFile
115   ${GetFileName} $OriginalFile $OriginalFileName
116   ${GetParent} $OriginalFile $OriginalDir # tmpbuf
117   ${GetParent} $OriginalDir $OriginalDir # tmpdir
118
119   SetOutPath $TEMP # The LyX tmpbuf should not be locked
120
121   StrCpy $PDFFile $PLUGINSDIR\$OriginalFileName
122
123   # Check whether the file will be opened with Adobe Reader or Adobe Acrobat
124   !insertmacro SystemCall "shell32::FindExecutable(t '$OriginalFile', t '', t .s)"
125   Call GetFileName
126   Pop $Viewer
127
128   ${If} $Viewer == ""
129     MessageBox MB_OK|MB_ICONEXCLAMATION "No PDF viewer is installed. \
130         Please install a PDF viewer such as Adobe Reader."
131     Quit        
132   ${EndIf}
133
134   ${If} $Viewer == "AcroRd32.exe"
135     ${OrIf} $Viewer == "Acrobat.exe"
136     
137     # Using Adobe viewer
138     
139     # Close existing view
140     ${If} ${FileExists} $PDFFile
141       !insertmacro HideConsole '"$EXEDIR\pdfclose.exe" --file "$PDFFile"'
142     ${EndIf}
143     
144     # Copy PDF to temporary file to allow LyX to overwrite the original
145     CopyFiles /SILENT $OriginalFile $PDFFile
146     
147     # Open a new view
148     !insertmacro HideConsole '"$EXEDIR\pdfopen.exe" --back --file "$PDFFile"'
149     
150     # Monitor for updates of the original file
151     GetFileTime $OriginalFile $OriginalTimeHigh $OriginalTimeLow
152     !insertmacro SystemCall "kernel32::FindFirstChangeNotification(t '$OriginalDir', \
153       i 1, i ${FILE_NOTIFY_CHANGE_LAST_WRITE}) i.s"
154     Pop $ChangeNotification
155     
156     ${Do}
157     
158       !insertmacro SystemCall "kernel32::WaitForSingleObject(i $ChangeNotification, i 10000) i.s"
159       Pop $WaitReturn
160            
161       # Check whether the lock is still active (if not, Adobe Reader is closed)
162       
163       FileOpen $LockedFile $PDFFile a
164       
165       ${If} $LockedFile != ""
166         # Quit this application
167         FileClose $LockedFile
168         Delete $PDFFile
169         !insertmacro SystemCall "kernel32::FindCloseChangeNotification(i $ChangeNotification)"
170         Quit
171       ${EndIf}
172       
173       ${IfNot} $WaitReturn = ${WAIT_TIMEOUT}
174         
175         # The LyX temporary directory has been updated
176         # Check whether it's the PDF file that has been updated
177           
178         GetFileTime $OriginalFile $CurrentTimeHigh $CurrentTimeLow
179         
180         ${If} $OriginalTimeHigh != $CurrentTimeHigh
181           ${OrIf} $OriginalTimeLow != $CurrentTimeLow
182           # PDF has been modified, update view
183           !insertmacro HideConsole '"$EXEDIR\pdfclose.exe" --file "$PDFFile"'
184           CopyFiles /SILENT $OriginalFile $PDFFile
185           !insertmacro HideConsole '"$EXEDIR\pdfopen.exe" --back --file "$PDFFile"'
186           
187           # Time of new file
188           StrCpy $OriginalTimeHigh $CurrentTimeHigh
189           StrCpy $OriginalTimeLow  $CurrentTimeLow
190         ${EndIf}
191         
192         #Monitor again
193         !insertmacro SystemCall "kernel32::FindNextChangeNotification(i $ChangeNotification)"
194         
195       ${EndIf}
196     
197     ${Loop}
198     
199   ${Else}
200   
201     # Another PDF viewer like GSView is used
202     # No need for special actions, just forward to ShellExecute
203     ExecShell open $OriginalFile
204     
205   ${EndIf}
206     
207 SectionEnd