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