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 # Functions from FileFunc.nsh
20 !insertmacro GetParameters
21 !insertmacro GetFileName
22 !insertmacro GetParent
24 #--------------------------------
29 Icon "..\packaging\icons\lyx.ico"
32 #--------------------------------
33 # Windows Vista settings
35 RequestExecutionLevel user
37 #--------------------------------
43 !define FILE_NOTIFY_CHANGE_LAST_WRITE 0x00000010
44 !define WAIT_TIMEOUT 0x00000102
46 #--------------------------------
59 Var ChangeNotification
68 #--------------------------------
71 !macro SystemCall STACK
73 # Call a Windows API function
76 CallInstDLL "$EXEDIR\System.dll" Call
80 !macro HideConsole COMMAND_LINE
82 # Run an application and hide console output
84 Push `${COMMAND_LINE}`
85 CallInstDLL "$EXEDIR\Console.dll" Exec
88 ${If} $RunAppReturn == "error"
89 MessageBox MB_OK|MB_ICONSTOP "Error opening PDF file $PDFFile."
94 #--------------------------------
97 Section "View PDF file"
99 InitPluginsDir # Temporary directory for PDF file
101 # Command line parameters
102 ${GetParameters} $OriginalFile
105 StrCpy $Character $OriginalFile 1
106 ${If} $Character == '"'
107 StrCpy $OriginalFile $OriginalFile "" 1
109 StrCpy $Character $OriginalFile 1 -1
110 ${If} $Character == '"'
111 StrCpy $OriginalFile $OriginalFile -1
114 GetFullPathName $OriginalFile $OriginalFile
115 ${GetFileName} $OriginalFile $OriginalFileName
116 ${GetParent} $OriginalFile $OriginalDir
118 SetOutPath $TEMP # The LyX tmpbuf should not be locked
120 StrCpy $PDFFile $PLUGINSDIR\$OriginalFileName
122 # Check whether the file will be opened with Adobe Reader or Adobe Acrobat
123 !insertmacro SystemCall "shell32::FindExecutable(t '$OriginalFile', t '', t .s)"
128 MessageBox MB_OK|MB_ICONEXCLAMATION "No PDF viewer is installed. \
129 Please install a PDF viewer such as Adobe Reader."
133 ${If} $Viewer == "AcroRd32.exe"
134 ${OrIf} $Viewer == "Acrobat.exe"
138 # Close existing view
139 ${If} ${FileExists} $PDFFile
140 !insertmacro HideConsole '"$EXEDIR\pdfclose.exe" --file "$PDFFile"'
143 # Copy PDF to temporary file to allow LyX to overwrite the original
144 CopyFiles /SILENT $OriginalFile $PDFFile
147 !insertmacro HideConsole '"$EXEDIR\pdfopen.exe" --back --file "$PDFFile"'
149 # Monitor for updates of the original file
150 GetFileTime $OriginalFile $OriginalTimeHigh $OriginalTimeLow
151 !insertmacro SystemCall "kernel32::FindFirstChangeNotification(t '$OriginalDir', \
152 i 0, i ${FILE_NOTIFY_CHANGE_LAST_WRITE}) i.s"
153 Pop $ChangeNotification
157 !insertmacro SystemCall "kernel32::WaitForSingleObject(i $ChangeNotification, i 10000) i.s"
160 # Check whether the PDF still exists (if not, LyX is being closed)
162 ${IfNot} ${FileExists} $LockedFile
163 # Quit this application
164 !insertmacro SystemCall "kernel32::FindCloseChangeNotification(i $ChangeNotification)"
168 # Check whether the lock is still active (if not, Adobe Reader being closed)
170 FileOpen $LockedFile $PDFFile a
172 ${If} $LockedFile != ""
173 # Quit this application
174 FileClose $LockedFile
176 !insertmacro SystemCall "kernel32::FindCloseChangeNotification(i $ChangeNotification)"
180 ${IfNot} $WaitReturn = ${WAIT_TIMEOUT}
181 # The LyX tmpbuf has been updated
182 # Check whether it's the PDF file that has been updated
184 GetFileTime $OriginalFile $CurrentTimeHigh $CurrentTimeLow
186 ${if} $OriginalTimeHigh != $CurrentTimeHigh
187 ${orif} $OriginalTimeLow != $CurrentTimeLow
188 # PDF has been modified, update view
189 !insertmacro HideConsole '"$EXEDIR\pdfclose.exe" --file "$PDFFile"'
190 CopyFiles /SILENT $OriginalFile $PDFFile
191 !insertmacro HideConsole '"$EXEDIR\pdfopen.exe" --back --file "$PDFFile"'
194 StrCpy $OriginalTimeHigh $CurrentTimeHigh
195 StrCpy $OriginalTimeLow $CurrentTimeLow
199 !insertmacro SystemCall "kernel32::FindNextChangeNotification(i $ChangeNotification)"
207 # Another PDF viewer like GSView is used
208 # No need for special actions, just forward to ShellExecute
209 ExecShell open $OriginalFile