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