#Region Activity Attributes
#FullScreen: False
#IncludeTitle: false
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim PageNumber As Int ' save page number during device rotation
' Dim paracount As Int : paracount =0
Dim LeftMargin As Int : LeftMargin = 5%x
Dim TopMargin As Int : TopMargin = 0%y
Dim LineSpacing As Int : LineSpacing = 1
Dim ft As Boolean=False
Dim alltext,all2 As List
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim PageTurner As PageTurnView
Dim pager As TextPaginator
Dim Font As Typeface
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
ft=True
End If
PageTurner.Initialize("PageTurner", 20)
Activity.AddView(PageTurner,0, 0, Activity.Width,Activity.Height )
PageTurner.TwoPages = False
' PageTurner.RenderLeftPage = False
pager.SetPageParameters(pager.ALIGN_CENTER, PageTurner.Width , LeftMargin, PageTurner.Height , TopMargin, LineSpacing)
PageTurner.SetMarginPixels(1dip, 1dip, 1dip, 1dip)
PageTurner.AllowLastPageCurl = False ' the default is true
Font=Typeface.LoadFromAssets("bold.ttf")
pager.SetPaintParameters( Font, 22, Colors.Black, True)
alltext.Initialize
all2.Initialize
alltext.AddAll(Array As String("ONe page text", _
"Another Page", _
"New page text", _
"And last page"))
For l= alltext.Size-1 To 0 Step -1
Dim ntxt As String =alltext.Get(l)
pager.Paginate((l)&CRLF&CRLF&CRLF&CRLF&CRLF&CRLF&ntxt& CRLF)
Next
End Sub
Sub Activity_Resume
PageTurner.CurrentPage = PageNumber
PageTurner.Color = Colors.RGB(245, 241, 222) ' otherwise it gets lost on Pause and Resume without a Create
PageTurner.OnResume
If ft = True Then
PageTurner.CurrentPage=pager.PageCount+1
ft=False
End If
End Sub
Sub Activity_Pause (UserClosed As Boolean)
PageNumber = PageTurner.CurrentPage
PageTurner.OnPause
If UserClosed Then
ft=True
End If
End Sub
' This is run on the main thread to display any exception in PageTurner_GetBitmap
Sub ShowPTError(title As String, msg As String)
ToastMessageShow(msg&":::" &title,False)
End Sub
' The PageTurnerView events run on a separate thread to the main thread.
' They therefore must not try to manipulate GUI elements.
' -------------------------------------------------------------------------------------------
' DO NOT ATTEMPT TO PAUSE THE DEBUGGER IN ANY OF SUBS BELOW AS IT MAY HAVE UNEXPECTED RESULTS
' -------------------------------------------------------------------------------------------
' This may be fixed in a future version of Basic4android.
Sub PageTurner_GetBitmap(width As Int, Height As Int, Page As Int) As Bitmap 'Called when the Bitmap for the given page number is required. Return the Bitmap
' As this is running on a separate thread exceptions will cause the application to force close
' and not report the exception as would happen on the main thread so we use a Try Catch block
' to trap any errors
Dim bmp As Bitmap
Dim cnv As Canvas
bmp.InitializeMutable(width, Height) ' do this here so we have a valid return in case of an exception
Try
'File.OpenInput("bad", "filename") ' this would cause an exception to be reported on the main thread
If Page = 0 Then
cnv.Initialize2(bmp)
cnv.DrawColor(Colors.RGB(245, 241, 222))
cnv.DrawText( "END", width/2,45%y, Typeface.LoadFromAssets("bold.ttf"), 24, Colors.Black, "CENTER")
Return bmp
Else If Page = pager.PageCount + 1 Then ' alltext.Size Then '
cnv.Initialize2(bmp)
cnv.DrawColor(Colors.RGB(245, 241, 222))
cnv.DrawText("", width/2, 1%y, Typeface.LoadFromAssets("arab2.ttf"), 24, Colors.Black, "CENTER")
cnv.DrawText( "Book Name", width/2, 45%y, Typeface.LoadFromAssets("bold.ttf"), 24, Colors.Black, "CENTER")
cnv.DrawText("", width/2, 98%y, Typeface.LoadFromAssets("arab2.ttf"), 24, Colors.Black, "CENTER")
Return bmp
Else
cnv.Initialize2(bmp)
cnv.DrawColor(Colors.RGB(245, 241, 222))
Return pager.GetPageBitmap(Page - 1,Colors.RGB(245, 241, 222))
End If
Catch
' catch and report any exceptions on the rendering thread to the main thread
PTException
End Try
Return bmp ' if we don't return something valid we will cause an exception on the rendering thread
End Sub
Sub PageTurner_GetPages() As Int 'Called when the number of pages is required. Return the number of pages
' This is running on a separate thread and I have seen a NullPointerException in here on closing the app
' Presumably the Pager object was destroyed before the OpenGL thread was stopped so we trap any error
Try
Return pager.PageCount + 1
Catch
Return 0 ' if we don't return something valid we will cause an exception on the rendering thread
End Try
End Sub
Sub PTException()
Dim Ex As ExceptionEx
Dim where As String
Ex = LastException
Dim args(2) As Object
args(0) = LastException.Message
where = Ex.StackTraceElement(2) ' get Java line of error
args(1) = where
PageTurner.RunOnGuiThread("ShowPTError", args)
End Sub