1. --!movie
  2.  
  3. --****************************************************************************
  4. -- @file PDF Viewer Demo
  5. -- @author Valentin Schmidt
  6. -- @version 1.0
  7. --
  8. -- This demo uses SumatraPDF 2.5.2 (included in folder "bin") to display PDF documents
  9. -- (as well as various ebook formats) in a Director window.
  10. --****************************************************************************
  11.  
  12. global $
  13.  
  14. global hwnd_stage
  15. global hwnd_sumatra
  16.  
  17. ----------------------------------------
  18. --
  19. ----------------------------------------
  20. on startMovie
  21.     --_player.debugPlaybackEnabled = 1
  22.  
  23.     $.import("drop") -- wraps xtra("DropXtra")
  24.     $.import("menu")
  25.     $.import("msg") -- wraps xtra("Msg")
  26.  
  27.     -- movie/window settings
  28.     _movie.stage.title = "PDF Viewer"
  29.     _movie.stage.titlebarOptions.visible = TRUE
  30.     _movie.stage.titlebarOptions.maximizebox = TRUE
  31.     _movie.stage.resizable = TRUE
  32.     _movie.stage.rect = rect(0,0,800,600)
  33.     _movie.stage.bgColor = rgb("#999999")
  34.     _movie.centerStage = TRUE
  35.     _movie.puppetTempo(30)
  36.  
  37.     -- create application menu
  38.     $.menu.init()
  39.     $.menu.addMenu("File")
  40.     $.menu.addItem("File", ["name":"Open...", "handler":#slotOpen, "letter":"O"])
  41.     $.menu.addItem("File", ["name":"Close", "handler":#slotClose, "letter":"W", "enabled":FALSE])
  42.     $.menu.addSep("File")
  43.     $.menu.addItem("File", ["name":"Exit", "handler":#halt])
  44.     $.menu.addMenu("Help")
  45.     $.menu.addItem("Help", ["name":"About PDF Viewer", "handler":#slotAbout])
  46.     $.menu.install()
  47.  
  48.     hwnd_stage = $.msg.stage_handle()
  49.     _movie.stage.visible = TRUE
  50.  
  51.     -- activate drop support
  52.     $.drop.setCallback(#slotFilesDropped)
  53.     $.drop.start()
  54. end
  55.  
  56. ----------------------------------------
  57. --
  58. ----------------------------------------
  59. on resizeWindow
  60.     if voidP(hwnd_sumatra) then
  61.         hwnd_sumatra = $.msg.find_win("SUMATRA_PDF_FRAME", "", hwnd_stage)
  62.         if voidP(hwnd_sumatra) then return
  63.     end if
  64.     -- resize SumatraPDF frame to stage
  65.     r = rect(0, 0, _movie.stage.rect.width, _movie.stage.rect.height)
  66.     $.msg.set_win_rect(hwnd_sumatra, r)
  67. end
  68.  
  69. ----------------------------------------
  70. --
  71. ----------------------------------------
  72. on zoomWindow
  73.     resizeWindow()
  74. end
  75.  
  76. ----------------------------------------
  77. --
  78. ----------------------------------------
  79. on loadFile (fn)
  80.     slotClose() -- quit previous instance of SumatraPDF
  81.     _movie.stage.title = _getFileName(fn) & " - PDF Viewer"
  82.     if fn contains SPACE then fn = QUOTE & fn & QUOTE
  83.     open "-plugin" && hwnd_stage && fn, _movie.path & "bin\SumatraPDF.exe"
  84.     $.menu.setItemEnabled("File", "Close", TRUE)
  85. end
  86.  
  87. ----------------------------------------
  88. -- Extracts filename from path
  89. -- @param {string} tPath
  90. -- @return {string}
  91. ----------------------------------------
  92. on _getFileName (tPath)
  93.     od = _player.itemDelimiter
  94.     _player.itemDelimiter = $.PD
  95.     tFileName = the last item of tPath
  96.     _player.itemDelimiter = od
  97.     return tFileName
  98. end
  99.  
  100. ----------------------------------------
  101. -- @callback
  102. ----------------------------------------
  103. on slotOpen
  104.     fltr = "All supported files,*.chm;*.djv;*.djvu;*.eps;*.epub;*.mobi;*.pdf;*.ps;*.xps"
  105.     put ",PDF files (.pdf),*.pdf" after fltr
  106.     put ",CHM files (.chm),*.chm" after fltr
  107.     put ",EPUB files (.epub),*.epub" after fltr
  108.     put ",PostScript files (.ps .eps),*.ps;*.eps" after fltr
  109.     put ",XPS files (.xps),*.xps" after fltr
  110.     put ",Mobi files (.mobi),*.mobi" after fltr
  111.     put ",DjVu files (.djv),*.djv" after fltr
  112.     fio = xtra("fileIO").new()
  113.     fio.setFilterMask(fltr)
  114.     fn = fio.displayOpen()
  115.     if string(fn)<>"" then loadFile(fn)
  116. end
  117.  
  118. ----------------------------------------
  119. -- @callback
  120. ----------------------------------------
  121. on slotClose
  122.     if voidP(hwnd_sumatra) then hwnd_sumatra = $.msg.find_win("SUMATRA_PDF_FRAME", "", hwnd_stage)
  123.     if not voidP(hwnd_sumatra) then
  124.         -- quit the running instance of SumatraPDF
  125.         $.msg.send_msg(hwnd_sumatra, $.msg.WM_DESTROY, 0, 0)
  126.         hwnd_sumatra = VOID
  127.         $.menu.setItemEnabled("File", "Close", FALSE)
  128.     end if
  129.     _movie.stage.title = "PDF Viewer"
  130. end
  131.  
  132. ----------------------------------------
  133. -- @callback
  134. ----------------------------------------
  135. on slotAbout
  136.     -- implemented by CommandLine xtra
  137.     -- _player.alert() could be used instead, but the dialog would then have a warning icon
  138.     MB_ICONINFORMATION = 64
  139.     messageBoxDialog("PDF Viewer v1.0"&$.LF&$.LF&"A simple PDF viewer based on SumatraPDF", "About PDF Viewer", MB_ICONINFORMATION)
  140. end
  141.  
  142. ----------------------------------------
  143. -- @callback
  144. ----------------------------------------
  145. on slotFilesDropped (data)
  146.     if data.files.count then loadFile(data.files[1])
  147. end
  148.  
[raw code]