' Gambas class file

Private hDBResult As Result


Public Sub Report_Open()

    Dim rtlText As ReportTextLabel
    Dim H, iWidth As Integer
    Dim sSummary As String = File.Load(".../data/texts/swr-report.txt")
    Dim ss As String[]
    Dim s, sSQLStatement As String

'-- Only for controls in the testing of the report project
  ' Report.Debug = True

'-- Returns the result of a database query as a database result.
    sSQLStatement = Subst("SELECT frequenz, fb3320, yagi20, loop20 FROM &1", DB.Quote("testseries1"))
    hDBResult = DBCS.DBConnection.Exec(sSQLStatement)
  
'-- The value `hDBResult.Count` determines the number of repetitions of the database rows.
    rhboxDBTableDataRows.DataCount = hDBResult.Count

'-- Data for the cover page  ----------------------------------------------------------------------
    rlEditorName.Text = "Dr. Hans-Joachim Lehmann"
    rlDatabaseName.Text = DBCS.DBName
    rlDBTableName.Text = "testseries1"

'-- Selected data for the content pages  ----------------------------------------------------------
    rlblCaption.Text = Subst("&1  »  &2", ("Database Report"), ("Antenna tests"))
 '                            [XOffset, Yoffset, Color, Spread, Blur, Inset]
    rhboxCaption.BoxShadow.Add("1mm", "1mm", Color.LightGray, "3px", "8px", False)
    rhboxDBTableHaeder.Margin.Top = "5mm"
    rlPrintingDate.Text = Format(Now(), "dddd, d. mmmm yyyy - hh:nn")

'-- Data from the DB table is drawn in a diagram and inserted into the report as an image.
    rImageChart.Alignment = Align.Center
    rImageChart.Stretch = Report.Proportional
    rImageChart.Height = "11cm"    
    rImageChart.Image = Image.Load("/tmp/chart-picture.png")
    
  ' rImageChart.Image = FChart.ChartPicture.Image
  
    FChart.Close()
'-- Data for the Summary (Formatting)  ------------------------------------------------------------
    Report1.Font = Font["Noto Sans,10"]

    iWidth = ReturnWidth()
    H = Report1.Font.H
    ss = Split(sSummary, "\n", Null, False)
    For Each s In ss
      rtlText = New ReportTextLabel(rvboxTextSummery)
      rtlText.Font = Font["Noto Sans,10"]
      rtlText.Alignment = Align.Justify
      rtlText.Text = s
      rtlText.Height = CStr(Rows(s, iWidth) * H) & "px"
      rtlText.Margin.Bottom = CStr(H / 5) & "px"
    Next

'-- Inserting page numbers with format. The cover sheet is NOT counted.  ------------------------
    rlblFooterPages.Text = ("=\"Page \" & (page -1) & \" von \" & (pages -1)")

    Catch 
      Print Error.Text 
      Print Error.Where
End

'-- Display the contents of the database fields (iteration)  ------------------------------------

Public Sub rlblDBField_Frequenz_Data(Index As Integer)

    hDBResult.MoveTo(Index)
    Last.Data = Format(hDBResult["frequenz"], "#.#00")

End

Public Sub rlblDBField_FB3320_Data(Index As Integer)

    hDBResult.MoveTo(Index)
    Last.Data = Format(hDBResult["fb3320"], "#.00")

End

Public Sub rlblDBField_Yagi20_Data(Index As Integer)
 
    hDBResult.MoveTo(Index)
    Last.Data = Format(hDBResult["yagi20"], "#.00")

End

Public Sub rlblDBField_Loop20_Data(Index As Integer)

    hDBResult.MoveTo(Index)
    Last.Data = Format(hDBResult["loop20"], "#.00")

End


'-- The following 4 procedures are only required to display the summary (option)  ---------------

Private Function ReturnWidth() As Integer
'   If we use only the report padding, it is correct to subtract only the
'   left and right padding of the book as here.
'   But if we also use margins and/or padding of other containers and/or
'   borders, these too must will be subtracted.
    
    Dim fReport, fLeft, fRight As Float

    fReport = Report1.UnitToInch(ReturnMeasure(Report1.Width), ReturnUnit(Report1.Width))
    fLeft = Report1.UnitToInch(ReturnMeasure(Report1.Padding.Left), ReturnUnit(Report1.Padding.Left))
    fRight = Report1.UnitToInch(ReturnMeasure(Report1.Padding.Right), ReturnUnit(Report1.Padding.Left))
    Return CInt(Round((fReport - (fLeft + fRight)) * Desktop.Resolution))
  
    Catch
      Error.Raise(("The value of measure can't be extracted"))

End

Private Function Rows(argValue As String, argWidth As Integer) As Integer

    Dim i As Integer
    Dim ss As String[]
    Dim sRow As String

    If Report1.Font.TextWidth(argValue) < argWidth Then Return 1
  
    ss = Split(argValue, " ")
    For Each s As String In ss
      sRow &= " " & s
      If Report1.Font.TextWidth(sRow) > argWidth Then
         sRow = s
         Inc i
      Endif
    Next
  
    Return i + 1

End

Private Function ReturnUnit(argValue As String) As String

    Dim s As String

    Try s = Right(argValue, 2)
    If Error Then Error.Raise(("The value of the unit can't be extracted"))
    If s = "ft" Then Error.Raise(("Margins can't be set up in feet"))
  
    Return s

End

Fast Private Function ReturnMeasure(argValue As String) As Float

    Return CFloat(Left(argValue, Len(argValue) - 2))
    Catch
      Error.Raise(("The value of measure can't be extracted"))

End
