]> git.lyx.org Git - lyx.git/blobdiff - development/Win32/pdfview/pdfview.nsi
Address #9368
[lyx.git] / development / Win32 / pdfview / pdfview.nsi
index 2d86bd5bd11081f80ac2d76058ddfb8dd4d10746..5e49a1ee8d8e8f15b1a9af34dd84be27ffbe6004 100644 (file)
-/*\r
-\r
-Windows PDF view helper\r
-Author: Joost Verburg\r
-\r
-This will be installed as pdfview.exe.\r
-\r
-The application will launch the default PDF viewer to display the PDF file,\r
-but works around the file locking problems of Adobe Reader.\r
-\r
-Source code of pdfopen/pdfclose is available at:\r
-http://magic.aladdin.cs.cmu.edu/2005/07/15/pdfopen-and-pdfclose/\r
-\r
-*/\r
-\r
-!include "LogicLib.nsh"\r
-!include "FileFunc.nsh"\r
-!insertmacro GetParameters\r
-!insertmacro GetFileName\r
-\r
-;--------------------------------\r
-;Settings\r
-\r
-Caption "PDF Viewer"\r
-OutFile pdfview.exe\r
-Icon "..\packaging\icons\lyx_32x32.ico"\r
-SilentInstall silent\r
-\r
-;--------------------------------\r
-;Constants\r
-\r
-!define FALSE 0\r
-!define TRUE 1\r
-\r
-;--------------------------------\r
-;Variables\r
-\r
-Var Dummy\r
-Var OriginalFile\r
-Var OriginalFileName\r
-Var PDFFile\r
-Var Viewer\r
-Var OriginalTimeHigh\r
-Var OriginalTimeLow\r
-Var CurrentTimeHigh\r
-Var CurrentTimeLow\r
-\r
-;--------------------------------\r
-;Macros\r
-\r
-!macro SystemCall STACK\r
-\r
-  Push `${STACK}`\r
-  CallInstDLL "$EXEDIR\System.dll" Call\r
-\r
-!macroend\r
-\r
-!macro HideConsole COMMAND_LINE\r
-\r
-  Push `${COMMAND_LINE}`\r
-  CallInstDLL "$EXEDIR\Console.dll" Exec\r
-  Pop $Dummy\r
-  \r
-  ${if} $Dummy == "error"\r
-    MessageBox MB_OK|MB_ICONSTOP "Error opening PDF file $R0."\r
-  ${endif}\r
-\r
-!macroend\r
-\r
-;--------------------------------\r
-;PDF vieweing\r
-\r
-Section "View PDF file"\r
-\r
-  InitPluginsDir ;Temporary directory for PDF file\r
-\r
-  ;Command line parameters\r
-  Call GetParameters\r
-  Pop $OriginalFile\r
-\r
-  ;Trim quotes\r
-  StrCpy $Dummy $OriginalFile 1\r
-  ${if} $Dummy == '"'\r
-    StrCpy $OriginalFile $OriginalFile "" 1\r
-  ${endif}\r
-  StrCpy $Dummy $OriginalFile 1 -1\r
-  ${if} $Dummy == '"'\r
-    StrCpy $OriginalFile $OriginalFile -1\r
-  ${endif}\r
-\r
-  GetFullPathName $OriginalFile $OriginalFile\r
-  Push $OriginalFile\r
-  Call GetFileName\r
-  Pop $OriginalFileName\r
-\r
-  SetOutPath $TEMP ;The LyX tmpbuf should not be locked\r
-\r
-  StrCpy $PDFFile $PLUGINSDIR\$OriginalFileName\r
-\r
-  ;Check whether the file will be opened with Adobe Reader or Adobe Acrobat\r
-  Push $OriginalFile\r
-  !insertmacro SystemCall "shell32::FindExecutable(t s, t '', t .s)"\r
-  Call GetFileName\r
-  Pop $Viewer\r
-\r
-  ${if} $Viewer == ""\r
-    MessageBox MB_OK|MB_ICONEXCLAMATION "No PDF viewer is installed. \\r
-        Please install a PDF viewer such as Adobe Reader."\r
-    Quit        \r
-  ${endif}\r
-\r
-  ${if} $Viewer == "AcroRd32.exe"\r
-    ${orif} $Viewer == "Acrobat.exe"\r
-    \r
-    ;Using Adobe viewer\r
-    \r
-    ;Close existing view\r
-    ${if} ${fileexists} $PDFFile\r
-      !insertmacro HideConsole '"$EXEDIR\pdfclose.exe" --file "$PDFFile"'\r
-    ${endif}\r
-    \r
-    ;Copy PDF to temporary file to allow LyX to overwrite the original\r
-    CopyFiles /SILENT $OriginalFile $PDFFile\r
-    \r
-    ;Open a new view\r
-    !insertmacro HideConsole '"$EXEDIR\pdfopen.exe" --back --file "$PDFFile"'\r
-    \r
-    ;Monitor for updates of the original file\r
-    \r
-    GetFileTime $OriginalFile $OriginalTimeHigh $OriginalTimeLow\r
-    \r
-    ${do}\r
-    \r
-      Sleep 500\r
-      \r
-      FileOpen $Dummy $PDFFile a\r
-      \r
-      ${if} $Dummy != ""\r
-        ;File no longer locked, reader closed\r
-        FileClose $Dummy\r
-        Delete $PDFFile\r
-        Quit\r
-      ${endif}\r
-      \r
-      ${if} ${fileexists} $OriginalFile\r
-        \r
-        GetFileTime $OriginalFile $CurrentTimeHigh $CurrentTimeLow\r
-        \r
-        ${if} $OriginalTimeHigh != $CurrentTimeHigh\r
-          ${orif} $OriginalTimeLow != $CurrentTimeLow\r
-          \r
-          ;Original has been modified, update!\r
-          \r
-          StrCpy $OriginalTimeHigh $CurrentTimeHigh\r
-          StrCpy $OriginalTimeLow  $CurrentTimeLow\r
-          !insertmacro HideConsole '"$EXEDIR\pdfclose.exe" --file "$PDFFile"'\r
-          CopyFiles /SILENT $OriginalFile $PDFFile\r
-          !insertmacro HideConsole '"$EXEDIR\pdfopen.exe" --back --file "$PDFFile"'\r
-          \r
-        ${endif}\r
-        \r
-      ${endif}\r
-    \r
-    ${loop}\r
-    \r
-  ${else}\r
-  \r
-    ;Another PDF viewer like GSView is used\r
-    ;No need for special actions, just forward to ShellExecute\r
-    ExecShell open $OriginalFile\r
-    \r
-  ${endif}\r
-    \r
-SectionEnd\r
+/*
+
+Windows PDF view helper
+Author: Uwe Stöhr and Joost Verburg
+
+This will be installed as pdfview.exe.
+
+The application will launch the default PDF viewer to display the PDF file,
+but works around the file locking problems of Adobe Reader and Acrobat.
+
+*/
+
+Unicode true
+
+!include LogicLib.nsh
+!include FileFunc.nsh
+
+#--------------------------------
+# Settings
+
+Caption "PDF Viewer"
+OutFile pdfview.exe
+Icon "..\packaging\icons\lyx.ico"
+SilentInstall silent
+
+#--------------------------------
+# Windows Vista (and later) settings
+
+RequestExecutionLevel user
+
+#--------------------------------
+# Variables
+
+Var Character
+
+Var OriginalFile
+Var OriginalFileName
+
+Var PDFFile
+Var ViewerFileName
+Var Viewer
+Var ViewerHandle
+Var ViewerVersion
+
+#--------------------------------
+# PDF viewing
+
+Section "View PDF file"
+
+  InitPluginsDir # Temporary directory for PDF file
+
+  # Command line parameters
+  ${GetParameters} $OriginalFileName
+
+  # Trim quotes
+  StrCpy $Character $OriginalFileName 1
+  ${If} $Character == '"'
+    StrCpy $OriginalFileName $OriginalFileName "" 1
+  ${EndIf}
+  StrCpy $Character $OriginalFileName 1 -1
+  ${If} $Character == '"'
+    StrCpy $OriginalFileName $OriginalFileName -1
+  ${EndIf}
+
+  GetFullPathName $OriginalFile $OriginalFileName
+
+  SetOutPath $TEMP # The LyX tmpbuf should not be locked
+
+  StrCpy $PDFFile $PLUGINSDIR\$OriginalFileName
+
+  # Check whether the file will be opened with Adobe Reader or Adobe Acrobat
+  FileOpen $R0 "$PLUGINSDIR\a.pdf" "w" #create a temp pdf file with a simple name
+  FileClose $R0
+  
+  #find stadard executable for "a.pdf", writes result in $0 (".r0" below)
+  System::Call "shell32::FindExecutable(t '$PLUGINSDIR\a.pdf', t '', t .r0)"
+  StrCpy $ViewerFileName $0
+  ${GetFileName} $ViewerFileName $Viewer
+
+  ${If} $Viewer == ""
+    MessageBox MB_OK|MB_ICONEXCLAMATION "No PDF viewer is installed. \
+        Please install a PDF viewer such as Adobe Reader."
+    Quit        
+  ${EndIf}
+  
+  ${if} $Viewer == "AcroRd32.exe"
+  ${orif} $Viewer == "AcroRd64.exe"
+  ${orif} $Viewer == "Acrobat.exe"
+    
+    # get the version of Acrobat - currenly not necessary
+    GetDLLVersion $ViewerFileName $R0 $R1
+    IntOp $R2 $R0 >> 16
+    IntOp $R2 $R2 & 0x0000FFFF ; $R2 now contains major version
+    #IntOp $R3 $R0 & 0x0000FFFF ; $R3 now contains minor version
+    #IntOp $R4 $R1 >> 16
+    #IntOp $R4 $R4 & 0x0000FFFF ; $R4 now contains release
+    #IntOp $R5 $R1 & 0x0000FFFF ; $R5 now contains build
+    StrCpy $ViewerVersion $R2
+    
+    # check if there is a windows open containing the PDF
+    ${if} $Viewer == "AcroRd32.exe"
+    ${orif} $Viewer == "AcroRd64.exe"
+     ${if} $ViewerVersion > 14
+      FindWindow $ViewerHandle "" "$OriginalFileName - Adobe Acrobat Reader DC"
+     ${else}
+      FindWindow $ViewerHandle "" "$OriginalFileName - Adobe Reader"
+     ${endif}
+    ${elseif} $Viewer == "Acrobat.exe"
+     FindWindow $ViewerHandle "" "$OriginalFileName - Adobe Acrobat"
+     ${if} $ViewerHandle == "0"
+      FindWindow $ViewerHandle "" "$OriginalFileName - Adobe Acrobat Pro"
+     ${endif}
+    ${endif}
+    ${if} $ViewerHandle <> "0" # close the window
+      SendMessage $ViewerHandle 16 0 0
+    ${endif}
+    
+    # Copy PDF to temporary file to allow LyX to overwrite the original
+    CopyFiles /SILENT $OriginalFile $PDFFile
+    
+    # Open a new view
+    ExecWait '"$ViewerFileName" "$PDFFile"'
+    
+  ${Else}
+  
+    # Another PDF viewer like GSView is used
+    # No need for special actions, just forward to ShellExecute
+    ExecShell open $OriginalFile
+    
+  ${EndIf}
+    
+SectionEnd