]> git.lyx.org Git - lyx.git/blob - development/Win32/pdfview/pdfview.nsi
03d1966205098ca4d607ea1799a967fa005b572c
[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_32x32.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 vieweing
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
117
118   SetOutPath $TEMP # The LyX tmpbuf should not be locked
119
120   StrCpy $PDFFile $PLUGINSDIR\$OriginalFileName
121
122   # Check whether the file will be opened with Adobe Reader or Adobe Acrobat
123   !insertmacro SystemCall "shell32::FindExecutable(t '$OriginalFile', t '', t .s)"
124   Call GetFileName
125   Pop $Viewer
126
127   ${If} $Viewer == ""
128     MessageBox MB_OK|MB_ICONEXCLAMATION "No PDF viewer is installed. \
129         Please install a PDF viewer such as Adobe Reader."
130     Quit        
131   ${EndIf}
132
133   ${If} $Viewer == "AcroRd32.exe"
134     ${OrIf} $Viewer == "Acrobat.exe"
135     
136     # Using Adobe viewer
137     
138     # Close existing view
139     ${If} ${FileExists} $PDFFile
140       !insertmacro HideConsole '"$EXEDIR\pdfclose.exe" --file "$PDFFile"'
141     ${EndIf}
142     
143     # Copy PDF to temporary file to allow LyX to overwrite the original
144     CopyFiles /SILENT $OriginalFile $PDFFile
145     
146     # Open a new view
147     !insertmacro HideConsole '"$EXEDIR\pdfopen.exe" --back --file "$PDFFile"'
148     
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
154     
155     ${Do}
156     
157       !insertmacro SystemCall "kernel32::WaitForSingleObject(i $ChangeNotification, i 10000) i.s"
158       Pop $WaitReturn    
159          
160       # Check whether a lock is still active.
161       # If not, Adode Reader is closed and we can close this application as well
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         # The LyX tmpbuf has been updated
175         # Check whether it's the PDF file that has been updated
176           
177         GetFileTime $OriginalFile $CurrentTimeHigh $CurrentTimeLow
178         
179         ${if} $OriginalTimeHigh != $CurrentTimeHigh
180           ${orif} $OriginalTimeLow != $CurrentTimeLow
181           # PDF has been modified, update view
182           !insertmacro HideConsole '"$EXEDIR\pdfclose.exe" --file "$PDFFile"'
183           CopyFiles /SILENT $OriginalFile $PDFFile
184           !insertmacro HideConsole '"$EXEDIR\pdfopen.exe" --back --file "$PDFFile"'
185           
186           # Time of new file
187           StrCpy $OriginalTimeHigh $CurrentTimeHigh
188           StrCpy $OriginalTimeLow  $CurrentTimeLow
189         ${EndIf}
190         
191         #Monitor again
192         !insertmacro SystemCall "kernel32::FindNextChangeNotification(i $ChangeNotification)"
193         
194       ${EndIf}
195     
196     ${Loop}
197     
198   ${Else}
199   
200     # Another PDF viewer like GSView is used
201     # No need for special actions, just forward to ShellExecute
202     ExecShell open $OriginalFile
203     
204   ${EndIf}
205     
206 SectionEnd