]> git.lyx.org Git - lyx.git/blob - development/Win32/pdfview/pdfview.nsi
pdfview.nsi: new version that fixes bug #9512 and some more
[lyx.git] / development / Win32 / pdfview / pdfview.nsi
1 /*
2
3 Windows PDF view helper
4 Author: Uwe Stöhr and 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 and Acrobat.
10
11 */
12
13 !include LogicLib.nsh
14 !include FileFunc.nsh
15
16 #--------------------------------
17 # Settings
18
19 Caption "PDF Viewer"
20 OutFile pdfview.exe
21 Icon "..\packaging\icons\lyx.ico"
22 SilentInstall silent
23
24 #--------------------------------
25 # Windows Vista (and later) settings
26
27 RequestExecutionLevel user
28
29 #--------------------------------
30 # Variables
31
32 Var Character
33 Var RunAppReturn
34
35 Var OriginalFile
36 Var OriginalFileName
37 Var OriginalDir
38
39 Var PDFFile
40 Var ViewerFileName
41 Var Viewer
42 Var ViewerHandle
43 Var ViewerVersion
44
45 #--------------------------------
46 # Macros
47
48 !macro SystemCall STACK
49
50   # Call a Windows API function
51
52   Push `${STACK}`
53   CallInstDLL "$EXEDIR\System.dll" Call
54
55 !macroend
56
57 !macro HideConsole COMMAND_LINE
58
59   # Run an application and hide console output
60
61   Push `${COMMAND_LINE}`
62   CallInstDLL "$EXEDIR\Console.dll" Exec
63   Pop $RunAppReturn
64   
65   ${If} $RunAppReturn == "error"
66     MessageBox MB_OK|MB_ICONSTOP "Error opening PDF file $PDFFile."
67   ${EndIf}
68
69 !macroend
70
71 #--------------------------------
72 # PDF viewing
73
74 Section "View PDF file"
75
76   InitPluginsDir # Temporary directory for PDF file
77
78   # Command line parameters
79   ${GetParameters} $OriginalFile
80
81   # Trim quotes
82   StrCpy $Character $OriginalFile 1
83   ${If} $Character == '"'
84     StrCpy $OriginalFile $OriginalFile "" 1
85   ${EndIf}
86   StrCpy $Character $OriginalFile 1 -1
87   ${If} $Character == '"'
88     StrCpy $OriginalFile $OriginalFile -1
89   ${EndIf}
90
91   GetFullPathName $OriginalFile $OriginalFile
92   ${GetFileName} $OriginalFile $OriginalFileName
93   ${GetParent} $OriginalFile $OriginalDir # tmpbuf
94   ${GetParent} $OriginalDir $OriginalDir # tmpdir
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   !insertmacro SystemCall "shell32::FindExecutable(t '$OriginalFile', t '', t .s)"
102   Pop $ViewerFileName
103   ${GetFileName} $ViewerFileName $Viewer
104
105   ${If} $Viewer == ""
106     MessageBox MB_OK|MB_ICONEXCLAMATION "No PDF viewer is installed. \
107         Please install a PDF viewer such as Adobe Reader."
108     Quit        
109   ${EndIf}
110   
111   ${if} $Viewer == "AcroRd32.exe"
112   ${orif} $Viewer == "AcroRd64.exe"
113   ${orif} $Viewer == "Acrobat.exe"
114     
115     # get the version of Acrobat - currenly not necessary
116     GetDLLVersion $ViewerFileName $R0 $R1
117     IntOp $R2 $R0 >> 16
118     IntOp $R2 $R2 & 0x0000FFFF ; $R2 now contains major version
119     #IntOp $R3 $R0 & 0x0000FFFF ; $R3 now contains minor version
120     #IntOp $R4 $R1 >> 16
121     #IntOp $R4 $R4 & 0x0000FFFF ; $R4 now contains release
122     #IntOp $R5 $R1 & 0x0000FFFF ; $R5 now contains build
123     StrCpy $ViewerVersion $R2
124     
125     # check if there is a windows open containing the PDF
126     ${if} $Viewer == "AcroRd32.exe"
127     ${orif} $Viewer == "AcroRd64.exe"
128      ${if} $ViewerVersion > 14
129       FindWindow $ViewerHandle "" "$OriginalFileName - Adobe Acrobat Reader DC"
130      ${else}
131       FindWindow $ViewerHandle "" "$OriginalFileName - Adobe Reader"
132      ${endif}
133     ${elseif} $Viewer == "Acrobat.exe"
134      FindWindow $ViewerHandle "" "$OriginalFileName - Adobe Acrobat"
135      ${if} $ViewerHandle == "0"
136       FindWindow $ViewerHandle "" "$OriginalFileName - Adobe Acrobat Pro"
137      ${endif}
138     ${endif}
139     ${if} $ViewerHandle <> "0" # close the window
140       SendMessage $ViewerHandle 16 0 0
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 '"$ViewerFileName" "$PDFFile"'
148     
149   ${Else}
150   
151     # Another PDF viewer like GSView is used
152     # No need for special actions, just forward to ShellExecute
153     ExecShell open $OriginalFile
154     
155   ${EndIf}
156     
157 SectionEnd