@APPTITLE "Quite Tiny PDF Viewer" @APPAUTHOR "jPV/RNO" @APPCOPYRIGHT "(C) Copyright 2018 by RNO." @APPVERSION "$VER: Quite Tiny PDF Viewer 0.1 (11.10.18)" @APPDESCRIPTION "Quite tiny PDF viewer." @APPIDENTIFIER "com.wmhost.jpv.qtpdfv" @REQUIRE "polybios" @DISPLAY {Title="Quite Tiny PDF Viewer", Width=240, Height=320, Color=#WHITE, Sizeable=True, Menu=1} @MENU 1, { {"Project", { {"Open...", ID = "open", Hotkey = "O"}, {""}, {"About...", ID = "about"}, {""}, {"Quit", ID = "quit", Hotkey = "Q"}}}, {"Settings", { {"Resize automatically?", ID = "resize", Flags = #MENUITEM_TOGGLE}, {""}, {"Save", ID = "save"}}} } Const #VERSION = "0.1" Function p_Input(msg) Switch msg.Action Case "OnWheelDown": p_Control("next") Case "OnWheelUp": p_Control("prev") Case "OnKeyUp": Switch msg.Key Case "PAGEDOWN": FallThrough Case "RIGHT": p_Control("next") Case "PAGEUP": FallThrough Case "LEFT": p_Control("prev") Case "HOME": p_Control("first") Case "END": p_Control("last") EndSwitch Case "SizeWindow": p_Draw() Case "OnMidMouseDown": p_Zoom() Case "CloseWindow": End Case "OnMenuSelect": Switch msg.item Case "open": p_Open() Case "quit": End Case "about": SystemRequest("About", "Quite Tiny PDF Viewer "..#VERSION.."\n\nCode by jPV/RNO", "OK", #REQICON_INFORMATION) Case "save": settings.resize=IsMenuItemSelected(1,"resize") SavePrefs(settings) EndSwitch EndSwitch EndFunction Function p_Control(cmd$) If pdfopen Switch cmd$ Case "next": If pdfpage1 Then pdfpage=pdfpage-1 Case "first": pdfpage=1 Case "last": pdfpage=pdfpages EndSwitch FreeBrush(1) pdf.GetBrush(1, pdfpage, 1) SetDisplayAttributes({Title=deffile$.." ["..pdfpage.."/"..pdfpages.."]"}) p_Draw() EndIf EndFunction Function p_Open(f$) If Not f$ Then f$=FileRequest("Select a PDF file", "pdf", Nil, defdir$, deffile$) If f$ If pdfopen FreeBrush(1) pdf.CloseDocument(1) pdfopen=False EndIf ExitOnError(False) pdf.OpenDocument(1, f$) If GetLastError() ExitOnError(True) SystemRequest("Error", "Cannot open the file.", "OK", #REQICON_ERROR) Cls Return EndIf ExitOnError(True) PDF_DOCUMENT = pdf.GetObjectType() pdfpages = GetAttribute(PDF_DOCUMENT, 1, #PDFATTRPAGES) pdfopen=True defdir$=PathPart(f$) deffile$=FilePart(f$) pdfpage=1 SetDisplayAttributes({Title=deffile$.." ["..pdfpage.."/"..pdfpages.."]"}) pdf.GetBrush(1, pdfpage, 1) Cls If IsMenuItemSelected(1,"resize") Then ChangeDisplaySize(GetAttribute(#BRUSH,1,#ATTRWIDTH),GetAttribute(#BRUSH,1,#ATTRHEIGHT)) p_Draw() EndIf EndFunction Function p_Scale() Local t={} Local bw=GetAttribute(#BRUSH,1,#ATTRWIDTH) Local bh=GetAttribute(#BRUSH,1,#ATTRHEIGHT) Local dw=GetAttribute(#DISPLAY,0,#ATTRWIDTH) Local dh=GetAttribute(#DISPLAY,0,#ATTRHEIGHT) If dw/dh > bw/bh t.w=Int(bw*dh/bh) t.h=dh t.x=Int((dw-t.w)/2) t.y=0 Else t.w=dw t.h=Int(bh*dw/bw) t.y=Int((dh-t.h)/2) t.x=0 EndIf Return(t) EndFunction Function p_Draw() If HaveObject(#BRUSH,1) Local s=p_Scale() DisplayBrush(1,s.x,s.y,{Width=s.w,Height=s.h}) EndIf EndFunction Function p_Zoom() If HaveObject(#BRUSH,1) Local mx=0 Local my=0 Local bx,by Local dw=GetAttribute(#DISPLAY,0,#ATTRWIDTH) Local dh=GetAttribute(#DISPLAY,0,#ATTRHEIGHT) Local bw=GetAttribute(#BRUSH,1,#ATTRWIDTH) * 2 ; The zoom rate can be changed here Local bh=GetAttribute(#BRUSH,1,#ATTRHEIGHT) * 2 Local s=p_Scale() Local ztmp=CreateBrush(Nil,bw,bh) SelectBrush(ztmp) DisplayBrush(1,0,0,{Width=bw, Height=bh}) EndSelect() SetFillStyle(#FILLCOLOR) While IsMidMouse() If MouseX()<>mx Or MouseY()<>my mx=MouseX() my=MouseY() bx=mx-Int((mx-s.x)/s.w*bw) by=my-Int((my-s.y)/s.h*bh) BeginRefresh(True) ; Clearing only the needed area with boxes is about 2x faster than just using Cls If bx>0 Then Box(0,0,bx,dh,#WHITE) If by>0 Then Box(0,0,dw,by,#WHITE) If bx+bw 0 For Local k=0 To argcount-1 Switch UpperStr(args[k].arg) Case "FILE": p_Open(args[k].param) Case "DEFDIR": defdir$=args[k].param Default: Local helptxt$="Allowed arguments:\n".. " -file - Opens a file at startup.\n".. " -defdir - Sets the initial dir to open files.\n" StringToFile(helptxt$,"CONSOLE:") End EndSwitch Next EndIf InstallEventHandler({SizeWindow=p_Input, OnMidMouseDown=p_Input, CloseWindow=p_Input, OnKeyUp=p_Input, OnMenuSelect=p_Input, OnWheelDown=p_Input, OnWheelUp=p_Input, OnDropFile=Function(msg) p_Open(msg.DropFiles[0]) EndFunction}) Repeat WaitEvent Forever