Combine PDFs 3.0 Sourcecode

Class MainWindow
Inherits Window
// Controls
ControlInstance
Function KeyDown(Key As String) As Boolean Handles Event
if asc(key)=8 then
if me.SelCount>0 then
Remove

Return true
end if
end if
End Function
Sub Change() Handles Event
EnableMenuItems

UpdateButton

if PreviewVisible then
ShowCurrentPreview
end if

End Sub
End ControlInstance
ControlInstance
End ControlInstance
ControlInstance
End ControlInstance
ControlInstance
End ControlInstance
ControlInstance
End ControlInstance
ControlInstance
End ControlInstance
ControlInstance
Sub Open() Handles Event
me.text=UsernameMBS
End Sub
End ControlInstance
ControlInstance
End ControlInstance
ControlInstance
End ControlInstance
ControlInstance
End ControlInstance
ControlInstance
Sub Action() Handles Event
pref.DoLaunch=me.Value
pref.save
End Sub
End ControlInstance
ControlInstance
Sub Action() Handles Event
pref.DoBeep=me.Value
pref.save
End Sub
End ControlInstance
ControlInstance
Sub Action() Handles Event
'DebugMessageMBS "LaunchTimer.Action start"

if w<>nil then
if w.f<>nil then
w.f.Launch
w.f=nil
w=nil
end if
end if

'DebugMessageMBS "LaunchTimer.End start"

End Sub
End ControlInstance
ControlInstance
Sub Action() Handles Event
addfiles
End Sub
End ControlInstance
ControlInstance
Sub Action() Handles Event
remove
End Sub
End ControlInstance
ControlInstance
Sub Action() Handles Event
me.Enabled=false
savefile

End Sub
End ControlInstance
ControlInstance
Sub Action() Handles Event
if me.Value then
ShowPreview
else
ClosePreview
end if

End Sub
End ControlInstance
ControlInstance
Sub Action() Handles Event
ShowURL "http://www.monkeybreadsoftware.de/Freeware/CombinePDFs.shtml"

End Sub
End ControlInstance
ControlInstance
End ControlInstance

// Properties
Dim items(0) as item



Dim itemID as integer



Dim w as workthread



Dim PreviewVisible As boolean

// Event implementations
Sub EnableMenuItems()
dim w,i,c as integer
dim b1,b2,b3,b4,b5 as integer
dim s as string

c=List.ListCount-1
for i=0 to c
if List.Selected(i) then
s=List.Cell(i,3)
Select case left(s,1)
case "A"
b1=b1+1
case "B"
b2=b2+1
case "C"
b3=b3+1
case "M"
b4=b4+1
case "T"
b5=b5+1
end Select
end if
next

FileAddfiles.Enable

if List.ListIndex>=0 then
FileRemove.Enable
end if

dim pw,ph as integer
if getEmptyPageSize(pw,ph) then
FileAddemptypage.Enable
end if

if List.ListCount>0 then
FileSave.enable
EditSortpages.Enable
EditSortpagesbetter.Enable
EditSelectall.Enable
end if

if List.SelCount>0 then
PDFOptionsNoRotation.Enable
PDFOptionsRotateleft.Enable
PDFOptionsRotateright.Enable

PDFOptionsArtBox.Enable
PDFOptionsBleedBox.Enable
PDFOptionsCropBox.Enable
PDFOptionsMediaBox.Enable
PDFOptionsTrimBox.Enable
PDFOptionsArtBox.Checked=b1>0
PDFOptionsBleedBox.Checked=b2>0
PDFOptionsCropBox.Checked=b3>0
PDFOptionsMediaBox.Checked=b4>0
PDFOptionsTrimBox.Checked=b5>0
end if

FileRemoveallfiles.Enable
End Sub
Sub DropObject(obj As DragItem, action As Integer)
do
if obj.FolderItemAvailable then
add obj.FolderItem
end if
loop until not obj.NextItem
End Sub
Sub Open()
me.AcceptFileDrop "application/pdf"
me.AcceptFileDrop "special/any"
me.AcceptFileDrop "special/folder"

CheckOpen.Value=pref.DoLaunch
CheckBeep.Value=pref.DoBeep

updateInfo
End Sub

// Methods
Sub additemtolist(i as item)
dim l,w,h as integer
dim r as CGRectMBS
dim s as string

List.AddRow i.name
l=List.LastIndex

List.cell(l,1)=Format(i.page,"0")


w=i.width
h=i.height
s=Format(w,"0")+" x "+Format(h,"0")
if i.pdf<>nil then
r=i.pdf.MediaBox(i.page)
if r<>nil then
w=r.Width
h=r.Height
s="M "+Format(w,"0")+" x "+Format(h,"0")
end if
end if
List.cell(l,3)=s
List.cell(l,4)=Format(i.id,"0")


End Sub
Sub savefile()
dim f as FolderItem
dim o as SaveAsDialog
dim c as CGContextMBS

'DebugMessageMBS "Savefile start"

o=new SaveAsDialog
if iTitle.text="" then
o.SuggestedFileName="untitled.pdf"
else
o.SuggestedFileName=iTitle.text+".pdf"
end if

o.Filter="Application/pdf"

f=o.ShowModal

if f<>Nil then
c=f.NewCGPDFDocumentMBS(CGMakeRectMBS(0,0,100,100),iTitle.Text,iAuthor.text,iCreator.text)

if c<>nil then
run c,f
end if
end if

'DebugMessageMBS "Savefile end"


End Sub
Sub processitems(c as cgcontextMBS)
dim n,count as integer
dim i as item
dim r as CGRectMBS
dim id,j,itemcount as integer
dim cc as CGContextMBS
dim f as Double
dim ci as CGImageMBS
dim s as string
dim d as Double
dim rectpage as CGRectMBS
dim pages as integer
dim rectpict as CGRectMBS

'DebugMessageMBS "Processitems start"

itemcount=UBound(items)
count=List.ListCount-1

ProgressWindow.bar.Value=0
ProgressWindow.bar.Maximum=count+1
ProgressWindow.Visible=true

for n=0 to count
ProgressWindow.StaticText1.text=List.Cell(n,0)

if ProgressWindow.cancel then
Return
end if

s=List.cell(n,4)
id=val(s)
i=FindItemByID(id)

if i<>nil then
if i.pdf<>nil then
r=i.Rect

Select case i.rotate
case 0
rectpage=CGMakeRectMBS(0,0,r.Width,r.Height)
rectpict=CGMakeRectMBS(0,0,r.Width,r.Height)
case 90
rectpage=CGMakeRectMBS(0,0,r.Height,r.Width)
rectpict=CGMakeRectMBS(0,0,r.Width,r.Height)
case 180
rectpage=CGMakeRectMBS(0,0,r.Width,r.Height)
rectpict=CGMakeRectMBS(0,0,r.Width,r.Height)
case 270
rectpage=CGMakeRectMBS(0,0,r.Height,r.Width)
rectpict=CGMakeRectMBS(0,0,r.Width,r.Height)
end Select

c.BeginPage rectpage
c.SaveGState

Select case i.rotate
case 0
case 90
c.RotateCTM pi*1.5
c.TranslateCTM -r.width,0
case 180
c.RotateCTM pi*1.0
c.TranslateCTM -r.Width,-r.Height
case 270
c.RotateCTM pi*0.5
c.TranslateCTM 0,-r.Height
end Select

c.DrawCGPDFDocument i.pdf,rectpict,i.page
c.RestoreGState
c.EndPage
pages=pages+1
elseif i.pic<>nil then
r=CGMakeRectMBS(0,0,i.width*72.0/i.wres,i.height*72.0/i.hres)


Select case i.rotate
case 0
rectpage=CGMakeRectMBS(0,0,r.Width,r.Height)
rectpict=CGMakeRectMBS(0,0,r.Width,r.Height)
case 90
rectpage=CGMakeRectMBS(0,0,r.Height,r.Width)
rectpict=CGMakeRectMBS(0,0,r.Width,r.Height)
case 180
rectpage=CGMakeRectMBS(0,0,r.Width,r.Height)
rectpict=CGMakeRectMBS(0,0,r.Width,r.Height)
case 270
rectpage=CGMakeRectMBS(0,0,r.Height,r.Width)
rectpict=CGMakeRectMBS(0,0,r.Width,r.Height)
end Select

c.BeginPage rectpage
c.SaveGState

Select case i.rotate
case 0
case 90
c.RotateCTM pi*1.5
c.TranslateCTM -r.width,0
case 180
c.RotateCTM pi*1.0
c.TranslateCTM -r.Width,-r.Height
case 270
c.RotateCTM pi*0.5
c.TranslateCTM 0,-r.Height
end Select

ci=i.GetCGImage
c.DrawPicture ci,rectpict
c.RestoreGState
c.EndPage
i.ClearCIImage
pages=pages+1
else
r=CGMakeRectMBS(0,0,i.width,i.height)

Select case i.rotate
case 0
rectpage=CGMakeRectMBS(0,0,r.Width,r.Height)
case 90
rectpage=CGMakeRectMBS(0,0,r.Height,r.Width)
case 180
rectpage=CGMakeRectMBS(0,0,r.Width,r.Height)
case 270
rectpage=CGMakeRectMBS(0,0,r.Height,r.Width)
end Select

c.BeginPage rectpage
c.EndPage
pages=pages+1
end if
end if

ProgressWindow.bar.Value=ProgressWindow.bar.Value+1
app.DoEvents

if ProgressWindow.cancel then
Return
end if
next

pref.PagesCombined=pref.PagesCombined+pages
pref.PDFsCreated=pref.PDFsCreated+1
pref.save


updateInfo



End Sub
Sub Remove()
dim l,id as integer
dim lc,n,c,cc as integer

cc=List.SelCount
lc=List.ListCount-1

if cc>=0 then
for l=lc downto 0
if List.Selected(l) then
id=val(List.Cell(l,4))

c=UBound(items)
for n=1 to c
if items(n).id=id then
items.Remove n
List.RemoveRow l
exit
end if
next
end if
next
end if

UpdateButton
End Sub
Sub addfiles()
dim o as OpenDialogMBS
dim f as FolderItem
dim t as OpenDialogFileTypeMBS
dim c,i as integer


o=new OpenDialogMBS
o.MultipleSelection=True
if Keyboard.AsyncOptionKey then
o.ShowHiddenFiles=true
end if

o.ShowDialog

c=o.FileCount-1
for i=0 to c
add o.Files(i)
next

End Sub
Sub add(f as folderItem)
dim i as item
dim p as CGPDFDocumentMBS
dim n,c as integer
dim r as CGRectMBS
dim g as FolderItem
dim pic as Picture

if f.Directory then
c=f.Count

for n=1 to c
g=f.TrueItem(n)
if g.Visible and left(g.Name,1)<>"." then
add g
end if
next
else

p=f.OpenAsCGPDFDocumentMBS

if p<>nil then
c=p.PageCount
end if

if c>=1 then
for n=1 to c

Itemid=Itemid+1
i=new item
i.id=Itemid
i.pdf=p
i.name=f.DisplayName
i.file=f
i.page=n
i.rotate=0
r=p.MediaBox(n)
i.wres=72
i.hres=72
i.width=r.Width
i.height=r.Height

items.Append i

additemtolist i
next
else
pic=f.OpenAsPicture

if pic<>Nil then
Itemid=Itemid+1
i=new item
i.id=Itemid
i.pic=pic
i.name=f.DisplayName
i.file=f
i.page=1
i.wres=pic.VerticalResolution
i.hres=pic.HorizontalResolution
i.width=pic.Width
i.height=pic.Height

items.Append i

additemtolist i

end if
end if
end if

UpdateButton

Exception
End Sub
Sub SetRotation(dw as integer)
dim w,i,c,id,j,itemcount as integer
dim it as item

itemcount=UBound(items)

c=List.ListCount-1
for i=0 to c
if list.Selected(i) then
id=val(List.Cell(i,4))
it=FindItemByID(id)

if it<>nil then
if dw=0 then
w=0
else
w=it.rotate+dw

if w>270 then
w=0
elseif w<0 then
w=270
end if
end if

it.rotate=w

end if
end if
next

ShowCurrentPreview

End Sub
Function FindItemByID(id as integer) As item
dim j as integer
dim itemcount as integer
dim it as item

itemcount=UBound(Items)

for j=1 to itemcount
it=items(j)
if it<>nil and id=it.id then
Return it
end if
next

'failed
'RB returns nil
End Function
Sub setpdfbox(n as integer, t as string)
dim w,h,i,c,id,j,itemcount as integer
dim it as item
dim s as string
dim r as CGRectMBS

itemcount=UBound(items)

c=List.ListCount-1
for i=0 to c
if list.Selected(i) then
id=val(List.Cell(i,4))
it=FindItemByID(id)

if it<>nil then
it.box=n
w=it.width
h=it.height
s=Format(w,"0")+" x "+Format(h,"0")
if it.pdf<>nil then
r=it.Rect
if r<>nil then
w=r.Width
h=r.Height
s=t+" "+Format(w,"0")+" x "+Format(h,"0")
end if
end if
List.Cell(i,3)=S
end if
end if
next

ShowCurrentPreview


End Sub
Sub run(c as cgcontextMBS, f as folderitem)
'DebugMessageMBS "Run start"

w=new WorkThread
w.c=c
w.f=f

'w.run // threading enabled
w.Start // no threading

'DebugMessageMBS "Run end"


End Sub
Sub runThreaded(c as cgcontextMBS, f as folderitem)
'DebugMessageMBS "RunThreaded start"

ProgressWindow.cancel=false

processitems c

c.Flush
c=nil

f.MacType="PDF "
'f.MacCreator="prvw"

ProgressWindow.close

if pref.DoLaunch then
LaunchTimer.Mode=1 // launch does not work from thread
end if
if pref.DoBeep then
beep
end if

MergeButton.Enabled=true

'DebugMessageMBS "RunThreaded end"


End Sub
Protected Sub UpdateButton()
if List.ListCount>0 then
MergeButton.Enabled=true // save
else
MergeButton.Enabled=false
end if

if List.ListIndex<0 then
RemoveButton.Enabled=False
else
RemoveButton.Enabled=true
end if
End Sub
Sub ShowPreview()
if PreviewVisible=False then
PreviewVisible=True
PreviewWindow.Left=me.Left+me.Width+10
PreviewWindow.top=me.top
ShowCurrentPreview
PreviewWindow.Show
end if

End Sub
Sub ClosePreview()
if PreviewVisible then
PreviewWindow.close
PreviewVisible=False
end if

End Sub
Sub ShowCurrentPreview()
if list.ListIndex>=0 then
dim id as integer
dim it as item

id=val(List.Cell(list.ListIndex,4))

it=FindItemByID(id)

if it<>Nil then
PreviewWindow.showitem it
end if

end if
End Sub
Sub updateInfo()
if pref.PagesCombined>0 then
if pref.PDFsCreated=1 then
info.text="You combined "+str(pref.PagesCombined)+" pages to one PDF file."
else
info.text="You combined "+str(pref.PagesCombined)+" pages to "+str(pref.PDFsCreated)+" PDF files."
end if
else
info.text="Welcome to Combine PDFs"
end if

End Sub
Function Compare(x as string, y as string, better as boolean) As integer
if better then
dim cx,cy as string

cx=left(x,1)
cy=left(y,1)

while len(x)>0 and len(y)>0 and cx=cy
x=mid(x,2)
y=mid(y,2)

cx=left(x,1)
cy=left(y,1)

wend

if IsNumber(x) and IsNumber(y) then
Return val(x)-val(y)
else
Return StrComp(x,y,1)
end if
else
Return StrComp(x,y,1)
end if

End Function
Sub Sort(better as boolean)
dim n,i,j,c,d as integer
dim a0,a1,a2,a3,a4 as string
dim b0,b1,b2,b3,b4 as string
dim an,bn as integer

c=List.ListCount-1
d=c-1

for i=0 to d
a0=List.cell(i,0)
a1=List.cell(i,1)
an=val(a1)
for j=i+1 to c
b0=List.cell(j,0)
b1=List.cell(j,1)
bn=val(b1)
if (Compare(b0,a0,better)=0 and bn<an) or Compare(b0,a0,better)<0 then
a2=List.cell(i,2)
a3=List.cell(i,3)
a4=List.cell(i,4)
b2=List.cell(j,2)
b3=List.cell(j,3)
b4=List.cell(j,4)

List.cell(i,0)=b0
List.cell(i,1)=b1
List.cell(i,2)=b2
List.cell(i,3)=b3
List.cell(i,4)=b4

List.cell(j,0)=a0
List.cell(j,1)=a1
List.cell(j,2)=a2
List.cell(j,3)=a3
List.cell(j,4)=a4

a0=b0
a1=b1
end if
next
Next

End Sub
Function IsNumber(s as string) As boolean
if asc(s)<asc("0") then
Return false
elseif asc(s)>asc("9") then
Return false
else
Return true
end if

End Function
Function getEmptyPageSize(byref pw as integer, byref ph as integer) As boolean

if List.ListIndex>=0 then
dim id as integer
dim it as item

id=val(List.Cell(List.ListIndex,4))
it=FindItemByID(id)

if it<>nil then
pw=it.width
ph=it.height

Return true
end if
end if

if List.ListCount>0 then
dim id as integer
dim it as item

id=val(List.Cell(0,4))
it=FindItemByID(id)

if it<>nil then
pw=it.width
ph=it.height

Return true
end if
end if


End Function
End Class

Class Item
// Properties
Dim pdf as cgpdfDocumentMBS



Dim name as string



Dim file as folderItem



Dim width as integer



Dim id as integer



Dim page as integer



Dim height as integer



Dim pic as picture



Dim mymask as picture



Dim rotate as integer



Dim box as integer



Dim wres as integer



Dim hres as integer



Dim data As CGDataProviderMBS

// Methods
Function Mask() As picture
if mymask<>nil then
Return mymask
end if

mymask=NewPicture(pic.Width,pic.Height,32)
mymask.Graphics.ForeColor=rgb(0,0,0)
mymask.Graphics.FillRect 0,0,pic.Width,pic.Height

Return mymask
End Function
Function Rect() As cgrectMBS
dim r as CGRectMBS

select case box
case 0
r=pdf.ArtBox(page)
case 1
r=pdf.BleedBox(page)
case 2
r=pdf.CropBox(page)
case 3
r=pdf.MediaBox(page)
case 4
r=pdf.TrimBox(page)
else
r=pdf.MediaBox(page)
end Select

Return r
End Function
Function GetCGImage() As CGImageMBS
dim b as BinaryStream
dim s as string
dim url as CFURLMBS
dim ci as CGImageMBS

if file.MacType="JPEG" or Right(file.Name,4)=".jpg" then
url=NewCFURLMBSFile(file)
if url<>nil then
data=CGCreateDataProviderWithCFURLMBS(url)
if data<>nil then
ci=CGCreateImageFromJPEGDataProviderMBS(data,nil,false,0)
if ci<>Nil then
Return ci
end if
end if
end if
elseif file.MacType="PNGf" or Right(file.Name,4)=".png" then
url=NewCFURLMBSFile(file)
if url<>nil then
data=CGCreateDataProviderWithCFURLMBS(url)
if data<>nil then
ci=CGCreateImageFromPNGDataProviderMBS(data,nil,false,0)
if ci<>Nil then
Return ci
end if
end if
end if
end if

return CGCreateImageMBS(pic.BitmapMBS,mask.BitmapMBS)

End Function
Sub ClearCIImage()
data=nil

End Sub
End Class

Class AboutDialog
Inherits Window
// Controls
ControlInstance
End ControlInstance
ControlInstance
Sub Paint(g As Graphics) Handles Event
dim f as FolderItem

f=app.ExecutableFile.Parent.Parent.Parent
f.DrawIconMBS g,me.left,me.top

Exception
End Sub
End ControlInstance
ControlInstance
End ControlInstance
ControlInstance
End ControlInstance
ControlInstance
Sub Open() Handles Event
me.text="Realbasic "+rbVersionString
End Sub
End ControlInstance
ControlInstance
Sub Open() Handles Event
me.text=mbspluginversion
End Sub
End ControlInstance
ControlInstance
Sub Action() Handles Event
close
End Sub
End ControlInstance
ControlInstance
End ControlInstance
ControlInstance
Sub MouseUp(X As Integer, Y As Integer) Handles Event
me.TextColor=rgb(0,0,255)
ShowURL "http://www.monkeybreadsoftware.de"
End Sub
Function MouseDown(X As Integer, Y As Integer) As Boolean Handles Event
me.TextColor=rgb(255,0,0)
Return true
End Function
End ControlInstance

// Event implementations
Sub Open()
#if TargetWin32
title="Fullscreen Movie Player"
#else
title=app.longVersion
#endif

StaticText1.text=Title
End Sub
End Class

Class App
Inherits Application
// Event implementations
Sub Close()
pref.save
End Sub
Sub EnableMenuItems()
AppleAboutthisapplication.Enable

End Sub
Sub OpenDocument(item As FolderItem)
MainWindow.add item
End Sub
Sub Open()
RegisterPlugins // remove this line
UseMBSCFPlugin

'DebugMessageEnableMBS true

pref=new Preferences
pref.load
End Sub
End Class

Class Preferences
// Properties
Dim DoLaunch as boolean



Dim DoBeep as boolean



Dim FirstLaunchDay As integer
Dim PDFsCreated As Integer
Dim FirstLaunchMonth As Integer
Dim FirstLaunchYear As Integer
Dim PagesCombined As Integer

// Methods
Sub save()
dim p as CFPreferencesMBS

p=new CFPreferencesMBS

p.SetAppValue NewCFStringMBS("DoBeep"), NewCFBooleanMBS(DoBeep),p.kCFPreferencesCurrentApplication
p.SetAppValue NewCFStringMBS("DoLaunch"), NewCFBooleanMBS(DoLaunch),p.kCFPreferencesCurrentApplication
p.SetAppValue NewCFStringMBS("FirstLaunchDay"), NewCFNumberMBSInteger(FirstLaunchDay),p.kCFPreferencesCurrentApplication
p.SetAppValue NewCFStringMBS("FirstLaunchMonth"), NewCFNumberMBSInteger(FirstLaunchMonth),p.kCFPreferencesCurrentApplication
p.SetAppValue NewCFStringMBS("FirstLaunchYear"), NewCFNumberMBSInteger(FirstLaunchYear),p.kCFPreferencesCurrentApplication
p.SetAppValue NewCFStringMBS("PagesCombined"), NewCFNumberMBSInteger(PagesCombined),p.kCFPreferencesCurrentApplication
p.SetAppValue NewCFStringMBS("PDFsCreated"), NewCFNumberMBSInteger(PDFsCreated),p.kCFPreferencesCurrentApplication

End Sub
Sub load()
dim p as CFPreferencesMBS
dim o as CFObjectMBS
dim s as CFStringMBS
dim b as CFBooleanMBS
dim n as CFNumberMBS

p=new CFPreferencesMBS

o=p.CopyAppValue(NewCFStringMBS("DoLaunch"),p.kCFPreferencesCurrentApplication)
if o isa CFBooleanMBS then
b=CFBooleanMBS(o)
DoLaunch=b.Value
end if

o=p.CopyAppValue(NewCFStringMBS("DoBeep"),p.kCFPreferencesCurrentApplication)
if o isa CFBooleanMBS then
b=CFBooleanMBS(o)
DoBeep=b.Value
end if

o=p.CopyAppValue(NewCFStringMBS("FirstLaunchDay"),p.kCFPreferencesCurrentApplication)
if o isa CFNumberMBS then
n=CFNumberMBS(o)
FirstLaunchDay=n.integervalue
end if

o=p.CopyAppValue(NewCFStringMBS("FirstLaunchMonth"),p.kCFPreferencesCurrentApplication)
if o isa CFNumberMBS then
n=CFNumberMBS(o)
FirstLaunchMonth=n.integervalue
end if

o=p.CopyAppValue(NewCFStringMBS("FirstLaunchYear"),p.kCFPreferencesCurrentApplication)
if o isa CFNumberMBS then
n=CFNumberMBS(o)
FirstLaunchYear=n.integervalue
end if

o=p.CopyAppValue(NewCFStringMBS("PagesCombined"),p.kCFPreferencesCurrentApplication)
if o isa CFNumberMBS then
n=CFNumberMBS(o)
PagesCombined=n.integervalue
end if

o=p.CopyAppValue(NewCFStringMBS("PDFsCreated"),p.kCFPreferencesCurrentApplication)
if o isa CFNumberMBS then
n=CFNumberMBS(o)
PDFsCreated=n.integervalue
end if

End Sub
Sub pref()
DoLaunch=true
DoBeep=False
End Sub
End Class

Module Util
// Constants
Const pi = 3.14159265359

// Properties
Dim pref as Preferences



End Module

Class ProgressWindow
Inherits Window
// Controls
ControlInstance
End ControlInstance
ControlInstance
Sub Action() Handles Event
cancel=true
End Sub
End ControlInstance
ControlInstance
End ControlInstance

// Properties
Dim cancel as boolean



End Class

Class WorkThread
Inherits Thread
// Properties
Dim c as cgcontextMBS



Dim f as folderitem




// Event implementations
Sub Run()
Start
End Sub

// Methods
Sub Start()
MainWindow.runThreaded c,f
c=nil

End Sub
End Class

Class PreviewWindow
Inherits Window
// Properties
Dim current As item

// Event implementations
Sub Paint(g As Graphics)
if Current=nil then Return

dim p as Picture

p=current.pic
if p<>nil then
dim f as double
dim x,y,w,h as integer
dim c as CGContextMBS
dim r,rectpict as CGRectMBS
dim ci as CGImageMBS

c=self.CGContextMBS

if Current.rotate=0 or Current.rotate=180 then
f=min(height/current.Height,width/current.width)
else
f=min(width/current.Height,height/current.width)
end if
w=current.width*f
h=current.Height*f
x=(width-w)/2
y=(height-h)/2

c.SaveGState

Select case current.rotate
case 0
case 90
c.TranslateCTM Width/2.0,Height/2.0
c.RotateCTM pi*1.5
c.TranslateCTM -Width/2.0,-Height/2.0
case 180
c.TranslateCTM Width/2.0,Height/2.0
c.RotateCTM pi*1.0
c.TranslateCTM -Width/2.0,-Height/2.0
case 270
c.TranslateCTM Width/2.0,Height/2.0
c.RotateCTM pi*0.5
c.TranslateCTM -Width/2.0,-Height/2.0
end Select

ci=current.GetCGImage
c.DrawPicture ci,CGMakeRectMBS(x,y,w,h)
c.RestoreGState
current.ClearCIImage
end if

if current.pdf<>Nil then
dim r,rectpict as CGRectMBS
dim f as double
dim x,y,w,h as integer

dim c as CGContextMBS

c=self.CGContextMBS
c.SaveGState

r=current.Rect

if Current.rotate=0 or Current.rotate=180 then
f=min(height/r.Height,width/r.width)
else
f=min(width/r.Height,height/r.width)
end if

w=r.width*f
h=r.Height*f
x=(width-w)/2
y=(height-h)/2

Select case current.rotate
case 0
case 90
c.TranslateCTM Width/2.0,Height/2.0
c.RotateCTM pi*1.5
c.TranslateCTM -Width/2.0,-Height/2.0
case 180
c.TranslateCTM Width/2.0,Height/2.0
c.RotateCTM pi*1.0
c.TranslateCTM -Width/2.0,-Height/2.0
case 270
c.TranslateCTM Width/2.0,Height/2.0
c.RotateCTM pi*0.5
c.TranslateCTM -Width/2.0,-Height/2.0
end Select

c.DrawCGPDFDocument current.pdf,CGMakeRectMBS(x,y,w,h),current.page
c.RestoreGState

end if


End Sub
Function CancelClose(appQuitting as Boolean) As Boolean
MainWindow.PreviewVisible=false
End Function

// Methods
Sub showitem(it as item)
Current=it

if Visible then Refresh

End Sub
End Class





Links
MBS FileMaker Plugins