]> git.lyx.org Git - lyx.git/blobdiff - development/Win32/pdfview/pdfview.nsi
Address #9368
[lyx.git] / development / Win32 / pdfview / pdfview.nsi
index 8bbdd470a7b362e0f7ab5120b51673204a2f4af3..5e49a1ee8d8e8f15b1a9af34dd84be27ffbe6004 100644 (file)
@@ -1,18 +1,17 @@
-/*
+/*
 
 Windows PDF view helper
 
 Windows PDF view helper
-Author: Joost Verburg
+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,
 
 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.
-
-Source code of pdfopen/pdfclose is available at:
-http://magic.aladdin.cs.cmu.edu/2005/07/15/pdfopen-and-pdfclose/
+but works around the file locking problems of Adobe Reader and Acrobat.
 
 */
 
 
 */
 
+Unicode true
+
 !include LogicLib.nsh
 !include FileFunc.nsh
 
 !include LogicLib.nsh
 !include FileFunc.nsh
 
@@ -25,69 +24,23 @@ Icon "..\packaging\icons\lyx.ico"
 SilentInstall silent
 
 #--------------------------------
 SilentInstall silent
 
 #--------------------------------
-# Windows Vista settings
+# Windows Vista (and later) settings
 
 RequestExecutionLevel user
 
 
 RequestExecutionLevel user
 
-#--------------------------------
-# Constants
-
-!define FALSE 0
-!define TRUE 1
-
-!define FILE_NOTIFY_CHANGE_LAST_WRITE 0x00000010
-!define WAIT_TIMEOUT 0x00000102
-
 #--------------------------------
 # Variables
 
 Var Character
 #--------------------------------
 # Variables
 
 Var Character
-Var RunAppReturn
 
 Var OriginalFile
 Var OriginalFileName
 
 Var OriginalFile
 Var OriginalFileName
-Var OriginalDir
 
 Var PDFFile
 Var ViewerFileName
 Var Viewer
 
 Var PDFFile
 Var ViewerFileName
 Var Viewer
+Var ViewerHandle
 Var ViewerVersion
 Var ViewerVersion
-Var DDEName
-
-Var ChangeNotification
-Var WaitReturn
-Var LockedFile
-
-Var OriginalTimeHigh
-Var OriginalTimeLow
-Var CurrentTimeHigh
-Var CurrentTimeLow
-
-#--------------------------------
-# Macros
-
-!macro SystemCall STACK
-
-  # Call a Windows API function
-
-  Push `${STACK}`
-  CallInstDLL "$EXEDIR\System.dll" Call
-
-!macroend
-
-!macro HideConsole COMMAND_LINE
-
-  # Run an application and hide console output
-
-  Push `${COMMAND_LINE}`
-  CallInstDLL "$EXEDIR\Console.dll" Exec
-  Pop $RunAppReturn
-  
-  ${If} $RunAppReturn == "error"
-    MessageBox MB_OK|MB_ICONSTOP "Error opening PDF file $PDFFile."
-  ${EndIf}
-
-!macroend
 
 #--------------------------------
 # PDF viewing
 
 #--------------------------------
 # PDF viewing
@@ -97,30 +50,32 @@ Section "View PDF file"
   InitPluginsDir # Temporary directory for PDF file
 
   # Command line parameters
   InitPluginsDir # Temporary directory for PDF file
 
   # Command line parameters
-  ${GetParameters} $OriginalFile
+  ${GetParameters} $OriginalFileName
 
   # Trim quotes
 
   # Trim quotes
-  StrCpy $Character $OriginalFile 1
+  StrCpy $Character $OriginalFileName 1
   ${If} $Character == '"'
   ${If} $Character == '"'
-    StrCpy $OriginalFile $OriginalFile "" 1
+    StrCpy $OriginalFileName $OriginalFileName "" 1
   ${EndIf}
   ${EndIf}
-  StrCpy $Character $OriginalFile 1 -1
+  StrCpy $Character $OriginalFileName 1 -1
   ${If} $Character == '"'
   ${If} $Character == '"'
-    StrCpy $OriginalFile $OriginalFile -1
+    StrCpy $OriginalFileName $OriginalFileName -1
   ${EndIf}
 
   ${EndIf}
 
-  GetFullPathName $OriginalFile $OriginalFile
-  ${GetFileName} $OriginalFile $OriginalFileName
-  ${GetParent} $OriginalFile $OriginalDir # tmpbuf
-  ${GetParent} $OriginalDir $OriginalDir # tmpdir
+  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
 
   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
-  !insertmacro SystemCall "shell32::FindExecutable(t '$OriginalFile', t '', t .s)"
-  Pop $ViewerFileName
+  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 == ""
   ${GetFileName} $ViewerFileName $Viewer
 
   ${If} $Viewer == ""
@@ -128,90 +83,44 @@ Section "View PDF file"
         Please install a PDF viewer such as Adobe Reader."
     Quit        
   ${EndIf}
         Please install a PDF viewer such as Adobe Reader."
     Quit        
   ${EndIf}
-
-  ${If} $Viewer == "AcroRd32.exe"
-    ${OrIf} $Viewer == "Acrobat.exe"
+  
+  ${if} $Viewer == "AcroRd32.exe"
+  ${orif} $Viewer == "AcroRd64.exe"
+  ${orif} $Viewer == "Acrobat.exe"
     
     
-    # Using Adobe viewer
-
+    # 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
     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"
-
-    ${If} $ViewerVersion < 10
-      StrCpy $DDEName "AcroView"
-    ${Else}
-      ${If} $Viewer == "AcroRd32.exe"
-        StrCpy $DDEName "AcroViewR$ViewerVersion"
-      ${ElseIf} $Viewer == "Acrobat.exe"
-        StrCpy $DDEName "AcroViewA$ViewerVersion"
-      ${EndIf}
-    ${EndIf}
-
-    # Close existing view
-    ${If} ${FileExists} $PDFFile
-      !insertmacro HideConsole '"$EXEDIR\pdfclose.exe" --reader "$ViewerFileName" --ddename "$DDEName" --file "$PDFFile"'
-    ${EndIf}
+    #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
     
     # Copy PDF to temporary file to allow LyX to overwrite the original
     CopyFiles /SILENT $OriginalFile $PDFFile
     
     # Open a new view
-    !insertmacro HideConsole '"$EXEDIR\pdfopen.exe" --reader "$ViewerFileName" --ddename "$DDEName" --back --file "$PDFFile"'
-    
-    # Monitor for updates of the original file
-    GetFileTime $OriginalFile $OriginalTimeHigh $OriginalTimeLow
-    !insertmacro SystemCall "kernel32::FindFirstChangeNotification(t '$OriginalDir', \
-      i 1, i ${FILE_NOTIFY_CHANGE_LAST_WRITE}) i.s"
-    Pop $ChangeNotification
-    
-    ${Do}
-    
-      !insertmacro SystemCall "kernel32::WaitForSingleObject(i $ChangeNotification, i 10000) i.s"
-      Pop $WaitReturn
-           
-      # Check whether the lock is still active (if not, Adobe Reader is closed)
-      
-      FileOpen $LockedFile $PDFFile a
-      
-      ${If} $LockedFile != ""
-        # Quit this application
-        FileClose $LockedFile
-        Delete $PDFFile
-        !insertmacro SystemCall "kernel32::FindCloseChangeNotification(i $ChangeNotification)"
-        Quit
-      ${EndIf}
-      
-      ${IfNot} $WaitReturn = ${WAIT_TIMEOUT}
-        
-        # The LyX temporary directory has been updated
-        # Check whether it's the PDF file that has been updated
-          
-        GetFileTime $OriginalFile $CurrentTimeHigh $CurrentTimeLow
-        
-        ${If} $OriginalTimeHigh != $CurrentTimeHigh
-          ${OrIf} $OriginalTimeLow != $CurrentTimeLow
-          # PDF has been modified, update view
-          !insertmacro HideConsole '"$EXEDIR\pdfclose.exe" --reader "$ViewerFileName" --ddename "$DDEName" --file "$PDFFile"'
-          CopyFiles /SILENT $OriginalFile $PDFFile
-          !insertmacro HideConsole '"$EXEDIR\pdfopen.exe" --reader "$ViewerFileName" --ddename "$DDEName" --back --file "$PDFFile"'
-          
-          # Time of new file
-          StrCpy $OriginalTimeHigh $CurrentTimeHigh
-          StrCpy $OriginalTimeLow  $CurrentTimeLow
-        ${EndIf}
-        
-        #Monitor again
-        !insertmacro SystemCall "kernel32::FindNextChangeNotification(i $ChangeNotification)"
-        
-      ${EndIf}
-    
-    ${Loop}
+    ExecWait '"$ViewerFileName" "$PDFFile"'
     
   ${Else}
   
     
   ${Else}