--!movie
--****************************************************************************
-- @file PDF Viewer Demo
-- @author Valentin Schmidt
-- @version 1.0
--
-- This demo uses SumatraPDF 2.5.2 (included in folder "bin") to display PDF documents
-- (as well as various ebook formats) in a Director window.
--****************************************************************************
global $
global hwnd_stage
global hwnd_sumatra
----------------------------------------
--
----------------------------------------
on startMovie
--_player.debugPlaybackEnabled = 1
$.import("drop") -- wraps xtra("DropXtra")
$.import("menu")
$.import("msg") -- wraps xtra("Msg")
-- movie/window settings
_movie.stage.title = "PDF Viewer"
_movie.stage.titlebarOptions.visible = TRUE
_movie.stage.titlebarOptions.maximizebox = TRUE
_movie.stage.resizable = TRUE
_movie.stage.rect = rect(0,0,800,600)
_movie.stage.bgColor = rgb("#999999")
_movie.centerStage = TRUE
_movie.puppetTempo(30)
-- create application menu
$.menu.init()
$.menu.addMenu("File")
$.menu.addItem("File", ["name":"Open...", "handler":#slotOpen, "letter":"O"])
$.menu.addItem("File", ["name":"Close", "handler":#slotClose, "letter":"W", "enabled":FALSE])
$.menu.addSep("File")
$.menu.addItem("File", ["name":"Exit", "handler":#halt])
$.menu.addMenu("Help")
$.menu.addItem("Help", ["name":"About PDF Viewer", "handler":#slotAbout])
$.menu.install()
hwnd_stage = $.msg.stage_handle()
_movie.stage.visible = TRUE
-- activate drop support
$.drop.setCallback(#slotFilesDropped)
$.drop.start()
end
----------------------------------------
--
----------------------------------------
on resizeWindow
if voidP(hwnd_sumatra) then
hwnd_sumatra = $.msg.find_win("SUMATRA_PDF_FRAME", "", hwnd_stage)
if voidP(hwnd_sumatra) then return
end if
-- resize SumatraPDF frame to stage
r = rect(0, 0, _movie.stage.rect.width, _movie.stage.rect.height)
$.msg.set_win_rect(hwnd_sumatra, r)
end
----------------------------------------
--
----------------------------------------
on zoomWindow
resizeWindow()
end
----------------------------------------
--
----------------------------------------
on loadFile (fn)
slotClose() -- quit previous instance of SumatraPDF
_movie.stage.title = _getFileName(fn) & " - PDF Viewer"
if fn contains SPACE then fn = QUOTE & fn & QUOTE
open "-plugin" && hwnd_stage && fn, _movie.path & "bin\SumatraPDF.exe"
$.menu.setItemEnabled("File", "Close", TRUE)
end
----------------------------------------
-- Extracts filename from path
-- @param {string} tPath
-- @return {string}
----------------------------------------
on _getFileName (tPath)
od = _player.itemDelimiter
_player.itemDelimiter = $.PD
tFileName = the last item of tPath
_player.itemDelimiter = od
return tFileName
end
----------------------------------------
-- @callback
----------------------------------------
on slotOpen
fltr = "All supported files,*.chm;*.djv;*.djvu;*.eps;*.epub;*.mobi;*.pdf;*.ps;*.xps"
put ",PDF files (.pdf),*.pdf" after fltr
put ",CHM files (.chm),*.chm" after fltr
put ",EPUB files (.epub),*.epub" after fltr
put ",PostScript files (.ps .eps),*.ps;*.eps" after fltr
put ",XPS files (.xps),*.xps" after fltr
put ",Mobi files (.mobi),*.mobi" after fltr
put ",DjVu files (.djv),*.djv" after fltr
fio = xtra("fileIO").new()
fio.setFilterMask(fltr)
fn = fio.displayOpen()
if string(fn)<>"" then loadFile(fn)
end
----------------------------------------
-- @callback
----------------------------------------
on slotClose
if voidP(hwnd_sumatra) then hwnd_sumatra = $.msg.find_win("SUMATRA_PDF_FRAME", "", hwnd_stage)
if not voidP(hwnd_sumatra) then
-- quit the running instance of SumatraPDF
$.msg.send_msg(hwnd_sumatra, $.msg.WM_DESTROY, 0, 0)
hwnd_sumatra = VOID
$.menu.setItemEnabled("File", "Close", FALSE)
end if
_movie.stage.title = "PDF Viewer"
end
----------------------------------------
-- @callback
----------------------------------------
on slotAbout
-- implemented by CommandLine xtra
-- _player.alert() could be used instead, but the dialog would then have a warning icon
MB_ICONINFORMATION = 64
messageBoxDialog("PDF Viewer v1.0"&$.LF&$.LF&"A simple PDF viewer based on SumatraPDF", "About PDF Viewer", MB_ICONINFORMATION)
end
----------------------------------------
-- @callback
----------------------------------------
on slotFilesDropped (data)
if data.files.count then loadFile(data.files[1])
end