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