DocumentView: Fix some bugs and add ShowPage and Desaturate properties.

[GB.FORM]
* NEW: DocumentView: The default background color is now a mix of the default text foreground and background colors.
* BUG: DocumentView: Use Paint.Begin() instead of Draw.Begin(), so that preview uses anti-aliasing.
* BUG: DocumentView: Fix some cases of page drawing not correctly triggered.
* NEW: DocumentView: ShowPage is new property that displays the current page and the number of pages inside the preview.
* NEW: DocumentView: Desaturate is a new property that desaturates the preview. It's slow when the zoom is huge.
* NEW: DocumentView: Allow zoom up to 800%.
This commit is contained in:
gambas 2019-07-09 19:43:15 +02:00
parent 63b503ca24
commit f44b12f14b

View file

@ -3,11 +3,10 @@
Export
Inherits UserControl
Public Const _Properties As String = "*,Border,Arrangement{Arrange.Row;Column;Vertical;Horizontal;Fill}=Row,Padding=10,Spacing=10,Column,AutoCenter,ShowShadow"
Public Const _Properties As String = "*,Border,Arrangement{Arrange.Row;Column;Vertical;Horizontal;Fill}=Row,Padding=10,Spacing=10,Column,AutoCenter,ShowShadow,ShowPage"
Public Const _Group As String = "View"
Public Const _Similar As String = "GridView"
Public _ShowPageNumber As Boolean
Public Enum Portrait, Paysage
'Public Enum None, PagesWidth, PageHeight
Property DocWidth, PageWidth As Integer
@ -21,11 +20,13 @@ Property Arrangement As Integer
Property Padding As Integer
Property Spacing As Integer
Property ShowShadow As Boolean
Property ShowPage As Boolean
Property Read Item As _DocumentItem
Property Read FirstVisibleDocument As Integer
Property Read LastVisibleDocument As Integer
Property AutoCenter As Boolean
Property Border As Boolean
Property Desaturate As Boolean
Private $bAutoCenter As Boolean
Private $iColumn As Integer
@ -72,17 +73,19 @@ Private $iSX As Integer
Private $iSY As Integer
Private $MX As Integer
Private $MY As Integer
Private $bShowPage As Boolean
Private $hObs As Observer
Event Layout(Page As Integer)
Event Draw(Page As Integer, Width As Integer, Height As Integer)
Event Finished
Event Zoom
Private $bDesaturate As Boolean
Public Sub _new()
$hView = New ScrollArea(Me) As "View"
$hView.Background = Color.DarkGray
$hView.Background = Color.Merge(Color.TextBackground, Color.TextForeground)
'$tmrLoad.Delay = 100
Me.Proxy = $hView
$hDocItem = New _DocumentItem As "Doc"
@ -114,12 +117,13 @@ Public Sub tmrLoad_Timer()
'Raise Layout(iPage)
hImg = New Image(iDocWidth * $fZoom, iDocHeight * $fZoom, Color.White)
Draw.Begin(hImg)
Paint.Begin(hImg)
'Paint.Scale($fZoom, $fZoom)
Raise Draw(iPage, iDocWidth * $fZoom, iDocHeight * $fZoom)
Draw.End
Paint.End
$colImage[iPage] = hImg
$aImageLoaded.Add(iPage)
$tmrLoad.Trigger
$tmrRefresh.Trigger
End
@ -127,8 +131,12 @@ End
Public Sub View_Draw()
Dim i, j, k As Integer
Dim sText As String
Dim W As Integer
Dim D As Integer
If $iCount = 0 Then Return
Select Case $iArrangement
Case Arrange.Horizontal
k = $iFirstVisibleDocument
@ -145,8 +153,8 @@ Public Sub View_Draw()
DrawDoc(k, i, j)
Inc k
If k > $iLastVisibleDocument Then Return
If k >= $iCount Then Return
If k > $iLastVisibleDocument Then Goto SHOW_PAGE
If k >= $iCount Then Goto SHOW_PAGE
Next
Next
@ -157,8 +165,8 @@ Public Sub View_Draw()
For i = 0 To $iNbreLines - 1
DrawDoc(k, i, j)
Inc k
If K > $iLastVisibleDocument Then Return
If K >= $iCount Then Return
If K > $iLastVisibleDocument Then Goto SHOW_PAGE
If K >= $iCount Then Goto SHOW_PAGE
Next
Next
@ -177,12 +185,29 @@ Public Sub View_Draw()
Next
End Select
SHOW_PAGE:
If $bShowPage Then
D = Desktop.Scale
sText = CStr($iFirstVisibleDocument + 1) & " / " & CStr($iCount)
W = Paint.Font.TextWidth(sText) + D * 2
Paint.FillRect(Paint.W - W - D, D, W, Paint.Font.H + D, Color.SetAlpha(Color.TextForeground, 128))
Paint.Background = Color.SetAlpha(Color.TextBackground, 128)
Paint.DrawText(sText, Paint.W - W - D + 1, D, W, Paint.Font.H + D, Align.Center)
Paint.Background = Color.TextBackground
Paint.DrawText(sText, Paint.W - W - D, D, W, Paint.Font.H + D, Align.Center)
Endif
End
Private Sub DrawDoc(Page As Integer, iRow As Integer, iCol As Integer)
Dim iDocX, iDocY As Integer
Dim fShad As Float = $fZoom * ($iSpacing / 4)
Dim hImage As Image
'Dim fFullContentWidth As Float
If $colLayout.Exist(Page) Then
@ -225,7 +250,9 @@ Private Sub DrawDoc(Page As Integer, iRow As Integer, iCol As Integer)
Endif
If $colImage.Exist(Page) Then
Paint.DrawImage($colImage[Page], iDocX, iDocY, $fScaledDocWidth, $fScaledDocHeight)
hImage = $colImage[Page]
If $bDesaturate Then hImage = hImage.Copy().Desaturate()
Paint.DrawImage(hImage, iDocX, iDocY, $fScaledDocWidth, $fScaledDocHeight)
Paint.Rectangle(iDocX, iDocY, $fScaledDocWidth, $fScaledDocHeight)
Paint.Brush = Paint.Color(Color.Black)
Paint.Stroke
@ -243,11 +270,6 @@ Private Sub DrawDoc(Page As Integer, iRow As Integer, iCol As Integer)
Paint.Fill()
Endif
If _ShowPageNumber Then
Paint.Font.Grade = 20 * $fZoom
Paint.DrawText(Page, iDocX, iDocY, $fScaledDocWidth, $fScaledDocHeight, Align.Center)
Endif
End
Private Sub RefreshLayoutNeeds()
@ -540,7 +562,7 @@ Private Sub Zoom_Write(Value As Float)
Select Case $iArrangement
Case Arrange.None, Arrange.Row, Arrange.Column, Arrange.LeftRight, Arrange.TopBottom
SetZoom(Min(Max(Value, 0.1), 4))
SetZoom(Min(Max(Value, 0.1), 8))
'$iArrangement = None
'$fZoom = $fZoom
'just refresh image in the cache don't clear them
@ -994,3 +1016,31 @@ Private Sub Border_Write(Value As Boolean)
$hView.Border = Value
End
Private Function ShowPage_Read() As Boolean
Return $bShowPage
End
Private Sub ShowPage_Write(Value As Boolean)
If $bShowPage = Value Then Return
$bShowPage = Value
$hView.Refresh
End
Private Function Desaturate_Read() As Boolean
Return $bDesaturate
End
Private Sub Desaturate_Write(Value As Boolean)
If $bDesaturate = Value Then Return
$bDesaturate = Value
$hView.refresh
End