]> git.lyx.org Git - lyx.git/blob - development/Win32/pdfview/pdfview.nsi
1179fd8713e95582f4406f2980666b8913af780e
[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 !insertmacro GetParameters
19 !insertmacro GetFileName
20
21 ;--------------------------------
22 ;Settings
23
24 Caption "PDF Viewer"
25 OutFile pdfview.exe
26 Icon "..\packaging\icons\lyx_32x32.ico"
27 SilentInstall silent
28
29 ;--------------------------------
30 ;Constants
31
32 !define FALSE 0
33 !define TRUE 1
34
35 ;--------------------------------
36 ;Variables
37
38 Var Dummy
39 Var OriginalFile
40 Var OriginalFileName
41 Var PDFFile
42 Var Viewer
43 Var OriginalTimeHigh
44 Var OriginalTimeLow
45 Var CurrentTimeHigh
46 Var CurrentTimeLow
47
48 ;--------------------------------
49 ;Macros
50
51 !macro SystemCall STACK
52
53   Push `${STACK}`
54   CallInstDLL "$EXEDIR\System.dll" Call
55
56 !macroend
57
58 !macro HideConsole COMMAND_LINE
59
60   Push `${COMMAND_LINE}`
61   CallInstDLL "$EXEDIR\Console.dll" Exec
62   Pop $Dummy
63   
64   ${if} $Dummy == "error"
65     MessageBox MB_OK|MB_ICONSTOP "Error opening PDF file $R0."
66   ${endif}
67
68 !macroend
69
70 ;--------------------------------
71 ;PDF vieweing
72
73 Section "View PDF file"
74
75   InitPluginsDir ;Temporary directory for PDF file
76
77   ;Command line parameters
78   Call GetParameters
79   Pop $OriginalFile
80
81   ;Trim quotes
82   StrCpy $Dummy $OriginalFile 1
83   ${if} $Dummy == '"'
84     StrCpy $OriginalFile $OriginalFile "" 1
85   ${endif}
86   StrCpy $Dummy $OriginalFile 1 -1
87   ${if} $Dummy == '"'
88     StrCpy $OriginalFile $OriginalFile -1
89   ${endif}
90
91   GetFullPathName $OriginalFile $OriginalFile
92   Push $OriginalFile
93   Call GetFileName
94   Pop $OriginalFileName
95
96   SetOutPath $TEMP ;The LyX tmpbuf should not be locked
97
98   StrCpy $PDFFile $PLUGINSDIR\$OriginalFileName
99
100   ;Check whether the file will be opened with Adobe Reader or Adobe Acrobat
101   Push $OriginalFile
102   !insertmacro SystemCall "shell32::FindExecutable(t s, t '', t .s)"
103   Call GetFileName
104   Pop $Viewer
105
106   ${if} $Viewer == ""
107     MessageBox MB_OK|MB_ICONEXCLAMATION "No PDF viewer is installed. \
108         Please install a PDF viewer such as Adobe Reader."
109     Quit        
110   ${endif}
111
112   ${if} $Viewer == "AcroRd32.exe"
113     ${orif} $Viewer == "Acrobat.exe"
114     
115     ;Using Adobe viewer
116     
117     ;Close existing view
118     ${if} ${fileexists} $PDFFile
119       !insertmacro HideConsole '"$EXEDIR\pdfclose.exe" --file "$PDFFile"'
120     ${endif}
121     
122     ;Copy PDF to temporary file to allow LyX to overwrite the original
123     CopyFiles /SILENT $OriginalFile $PDFFile
124     
125     ;Open a new view
126     !insertmacro HideConsole '"$EXEDIR\pdfopen.exe" --back --file "$PDFFile"'
127     
128     ;Monitor for updates of the original file
129     
130     GetFileTime $OriginalFile $OriginalTimeHigh $OriginalTimeLow
131     
132     ${do}
133     
134       Sleep 500
135       
136       FileOpen $Dummy $PDFFile a
137       
138       ${if} $Dummy != ""
139         ;File no longer locked, reader closed
140         FileClose $Dummy
141         Delete $PDFFile
142         Quit
143       ${endif}
144       
145       ${if} ${fileexists} $OriginalFile
146         
147         GetFileTime $OriginalFile $CurrentTimeHigh $CurrentTimeLow
148         
149         ${if} $OriginalTimeHigh != $CurrentTimeHigh
150           ${orif} $OriginalTimeLow != $CurrentTimeLow
151           
152           ;Original has been modified, update!
153           
154           StrCpy $OriginalTimeHigh $CurrentTimeHigh
155           StrCpy $OriginalTimeLow  $CurrentTimeLow
156           !insertmacro HideConsole '"$EXEDIR\pdfclose.exe" --file "$PDFFile"'
157           CopyFiles /SILENT $OriginalFile $PDFFile
158           !insertmacro HideConsole '"$EXEDIR\pdfopen.exe" --back --file "$PDFFile"'
159           
160         ${endif}
161         
162       ${endif}
163     
164     ${loop}
165     
166   ${else}
167   
168     ;Another PDF viewer like GSView is used
169     ;No need for special actions, just forward to ShellExecute
170     ExecShell open $OriginalFile
171     
172   ${endif}
173     
174 SectionEnd