3 Windows PDF view helper
6 This will be installed as pdfview.exe.
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.
11 Source code of pdfopen/pdfclose is available at:
12 http://magic.aladdin.cs.cmu.edu/2005/07/15/pdfopen-and-pdfclose/
19 #--------------------------------
24 Icon "..\packaging\icons\lyx.ico"
27 #--------------------------------
28 # Windows Vista settings
30 RequestExecutionLevel user
32 #--------------------------------
38 !define FILE_NOTIFY_CHANGE_LAST_WRITE 0x00000010
39 !define WAIT_TIMEOUT 0x00000102
41 #--------------------------------
57 Var ChangeNotification
66 #--------------------------------
69 !macro SystemCall STACK
71 # Call a Windows API function
74 CallInstDLL "$EXEDIR\System.dll" Call
78 !macro HideConsole COMMAND_LINE
80 # Run an application and hide console output
82 Push `${COMMAND_LINE}`
83 CallInstDLL "$EXEDIR\Console.dll" Exec
86 ${If} $RunAppReturn == "error"
87 MessageBox MB_OK|MB_ICONSTOP "Error opening PDF file $PDFFile."
92 #--------------------------------
95 Section "View PDF file"
97 InitPluginsDir # Temporary directory for PDF file
99 # Command line parameters
100 ${GetParameters} $OriginalFile
103 StrCpy $Character $OriginalFile 1
104 ${If} $Character == '"'
105 StrCpy $OriginalFile $OriginalFile "" 1
107 StrCpy $Character $OriginalFile 1 -1
108 ${If} $Character == '"'
109 StrCpy $OriginalFile $OriginalFile -1
112 GetFullPathName $OriginalFile $OriginalFile
113 ${GetFileName} $OriginalFile $OriginalFileName
114 ${GetParent} $OriginalFile $OriginalDir # tmpbuf
115 ${GetParent} $OriginalDir $OriginalDir # tmpdir
117 SetOutPath $TEMP # The LyX tmpbuf should not be locked
119 StrCpy $PDFFile $PLUGINSDIR\$OriginalFileName
121 # Check whether the file will be opened with Adobe Reader or Adobe Acrobat
122 !insertmacro SystemCall "shell32::FindExecutable(t '$OriginalFile', t '', t .s)"
124 ${GetFileName} $ViewerFileName $Viewer
127 MessageBox MB_OK|MB_ICONEXCLAMATION "No PDF viewer is installed. \
128 Please install a PDF viewer such as Adobe Reader."
132 ${If} $Viewer == "AcroRd32.exe"
133 ${OrIf} $Viewer == "Acrobat.exe"
137 GetDLLVersion $ViewerFileName $R0 $R1
139 IntOp $R2 $R2 & 0x0000FFFF ; $R2 now contains major version
140 IntOp $R3 $R0 & 0x0000FFFF ; $R3 now contains minor version
142 IntOp $R4 $R4 & 0x0000FFFF ; $R4 now contains release
143 IntOp $R5 $R1 & 0x0000FFFF ; $R5 now contains build
144 StrCpy $ViewerVersion "$R2"
146 ${If} $ViewerVersion < 10
147 StrCpy $DDEName "AcroView"
149 ${If} $Viewer == "AcroRd32.exe"
150 StrCpy $DDEName "AcroViewR$ViewerVersion"
151 ${ElseIf} $Viewer == "Acrobat.exe"
152 StrCpy $DDEName "AcroViewA$ViewerVersion"
156 # Close existing view
157 ${If} ${FileExists} $PDFFile
158 !insertmacro HideConsole '"$EXEDIR\pdfclose.exe" --reader "$ViewerFileName" --ddename "$DDEName" --file "$PDFFile"'
161 # Copy PDF to temporary file to allow LyX to overwrite the original
162 CopyFiles /SILENT $OriginalFile $PDFFile
165 !insertmacro HideConsole '"$EXEDIR\pdfopen.exe" --reader "$ViewerFileName" --ddename "$DDEName" --back --file "$PDFFile"'
167 # Monitor for updates of the original file
168 GetFileTime $OriginalFile $OriginalTimeHigh $OriginalTimeLow
169 !insertmacro SystemCall "kernel32::FindFirstChangeNotification(t '$OriginalDir', \
170 i 1, i ${FILE_NOTIFY_CHANGE_LAST_WRITE}) i.s"
171 Pop $ChangeNotification
175 !insertmacro SystemCall "kernel32::WaitForSingleObject(i $ChangeNotification, i 10000) i.s"
178 # Check whether the lock is still active (if not, Adobe Reader is closed)
180 FileOpen $LockedFile $PDFFile a
182 ${If} $LockedFile != ""
183 # Quit this application
184 FileClose $LockedFile
186 !insertmacro SystemCall "kernel32::FindCloseChangeNotification(i $ChangeNotification)"
190 ${IfNot} $WaitReturn = ${WAIT_TIMEOUT}
192 # The LyX temporary directory has been updated
193 # Check whether it's the PDF file that has been updated
195 GetFileTime $OriginalFile $CurrentTimeHigh $CurrentTimeLow
197 ${If} $OriginalTimeHigh != $CurrentTimeHigh
198 ${OrIf} $OriginalTimeLow != $CurrentTimeLow
199 # PDF has been modified, update view
200 !insertmacro HideConsole '"$EXEDIR\pdfclose.exe" --reader "$ViewerFileName" --ddename "$DDEName" --file "$PDFFile"'
201 CopyFiles /SILENT $OriginalFile $PDFFile
202 !insertmacro HideConsole '"$EXEDIR\pdfopen.exe" --reader "$ViewerFileName" --ddename "$DDEName" --back --file "$PDFFile"'
205 StrCpy $OriginalTimeHigh $CurrentTimeHigh
206 StrCpy $OriginalTimeLow $CurrentTimeLow
210 !insertmacro SystemCall "kernel32::FindNextChangeNotification(i $ChangeNotification)"
218 # Another PDF viewer like GSView is used
219 # No need for special actions, just forward to ShellExecute
220 ExecShell open $OriginalFile