


' Gambas class file

Public aLines As String[]
Public aData As String[]
Public picPicture As Picture
Public imgWatchFace As Image
Public curTimeOffset As Integer
Public curLocation As String

Public Sub Form_Open()
  
  Dim sLine As String
  Dim picFlag As Picture
  Dim dDateTime, dUTC As Date
  Dim iTimeOffset As Integer
  
  FMain.Center
  FMain.Resizable = False  
  pboxLocal.Border = Border.None
  pboxWorld.Border = Border.None

' AnalogUhr-Takt 
  Timer.Delay = 1000 ' 1-Sekunden-Takt
  Timer.Start
  Timer.Trigger
  
  aLines = New String[]
  aLines = Split(File.Load(Application.Path &/ "zeitzone.txt"), gb.NewLine) ' Information: http://www.worldtimeserver.com
  
  For Each sLine In aLines
    aData = New String[]
    aData = Split(sLine, ",")
   
    picFlag = Picture.Load(Application.Path &/ "Flags" &/ aData[4])
    
    dUTC = DateAdd(Now, gb.Hour, -2)
    iTimeOffset = 60 * CInt(Scan(aData[2], "*:*")[0]) + CInt(Scan(aData[2], "*:*")[1]) 
    
    dDateTime = DateAdd(dUTC, gb.Minute, iTimeOffset) ' Lokale Zeit

    livData.Add(aData[0], "", PicFlag, Null)
    livData.Item.RichText = aData[0] & "<br/>"
    livData.Item.RichText &= "<b><font size=+1 face=FreeMono color=Black>"
    livData.Item.RichText &= Format(dDateTime, "hh:nn")
    livData.Item.RichText &= "</font></b><br/>"
    livData.Item.RichText &= Format(dDateTime, "dddd dd.mm.yyyy")
  Next
  
  livData["Berlin"].Selected = True
  livData.MoveTo("Berlin")
  livData.Item.EnsureVisible() 
  
  imgWatchFace = Image.Load("Images" &/ "zbs.png")
  
  curLocation = "UTC"
  curTimeOffset = -60 * 60 ' Einheit: Minuten
  
End ' Form_Open()

Private Sub UpdateListView()
  
  Dim sLine As String
  Dim dDateTime, dUTC As Date
  Dim iTimeOffset As Integer
  
  livData.MoveFirst()
  For Each sLine In aLines
    
    aData = New String[]
    aData = Split(sLine, ",")
    
    livData.MoveTo(aData[0])
    
    dUTC = DateAdd(Now, gb.Hour, -2)
    iTimeOffset = 60 * CInt(Scan(aData[2], "*:*")[0]) + CInt(Scan(aData[2], "*:*")[1]) 
    
    dDateTime = DateAdd(dUTC, gb.Minute, iTimeOffset) ' Lokale Zeit
    
    livData.Item.RichText = aData[0] & "<br/>"
    livData.Item.RichText &= "<b><font size=+1 face=FreeMono color=Black>"
    livData.Item.RichText &= Format(dDateTime, "hh:nn")
    livData.Item.RichText &= "</font></b><br/>"
    livData.Item.RichText &= Format(dDateTime, "dddd dd.mm.yyyy")
  Next

End ' UpateListView(...)

Public Sub Timer_Timer() ' Der Timer wird jede Sekunde aufgerufen 

  Dim dUTC As Date
  
' Es müssen die Ortszeiten für Berlin und für die in der ListView ausgewählte Stadt bereitgestellt werden
   
  PaintClock(Now(), True)         ' Ortszeit Berlin
  pboxLocal.Picture = picPicture
  
  lblWord.Text = curLocation      ' Ausgewählte Stadt in der ListView ...
  dUTC = DateAdd(Now, gb.Hour, -2) 
  PaintClock(DateAdd(dUTC, gb.Minute, curTimeOffset), True) ' ... und lokale Zeit in der ausgewählten Stadt
  pboxWorld.Picture = picPicture
  
  If Second(Now) = 59 Then
     Wait 1
     UpdateListView()
  Endif

End ' Timer_Timer()

Public Sub PaintClock(DateTime As Date, ShowSeconds As Boolean) ' Zeichnet Ziffernblatt und Zeiger
  
  Dim iRadius As Integer
  Dim fAngle As Float
  Dim x, y, W, H, fHourOffset, fMinuteOffset As Float

  W = imgWatchFace.Width - 1 ' = 119 und damit ungerade für exakten Mittelpunkt der Fläche
  H = imgWatchFace.Height - 1 
  
  picPicture = New Picture(W, H, True) ' Es wird eine leere Zeichenfläche von WxH erzeugt
  
  Paint.Begin(picPicture)
  
  ' Zeichnet das Ziffernblatt-Image auf die Zeichenfläche in die linke obere Ecke
    Paint.DrawImage(imgWatchFace, 0, 0, W, H)
  
    iRadius = 50 ' Basis-Länge eines Zeigers festlegen
  
  ' Stundenzeiger auf die Zeichenfläche zeichnen (über das Zifferblatt!)
    fAngle = Hour(DateTime) * 30 - 90 + 0.5 * Minute(DateTime) ' Der Winkel des Stunden-Zeigers in Grad   
    fHourOffset = 18
    x = (iRadius - fHourOffset) * Cos(fAngle * Pi / 180) ' Koordiante x, Umrechnung fAngle in Bogenmaß
    y = (iRadius - fHourOffset) * Sin(Rad(fAngle))       ' Koordiante y, Umrechnung fAngle in Bogenmaß
    PaintPointer(x, y, Color.White, 3.5, W, H)           ' Aufruf der PaintPointer()-Methode
 
  ' Minutenzeiger auf die Zeichenfläche zeichnen (über das Zifferblatt!)
    fAngle = Minute(DateTime) * 6 - 90
    fMinuteOffset = 12
    x = (iRadius - fMinuteOffset) * Cos(Rad(fAngle))
    y = (iRadius - fMinuteOffset) * Sin(Rad(fAngle))
    PaintPointer(x, y, Color.White, 2, W, H)
    
  ' Sekundenzeiger auf die Zeichenfläche zeichnen (optiona) (über das Zifferblatt!)
    If ShowSeconds = True Then 
       fAngle = Second(DateTime) * 6 - 90 ' -90°, da bei 3 Uhr der fAngle 0° liegt -> Bezugsachse!
       x = iRadius * Cos(Rad(fAngle))
       y = iRadius * Sin(Rad(fAngle))  
       PaintPointer(x, y, Color.Red, 0.9, W, H)
    Endif

  ' Kreis um Mittelpunkt in Hintergrundfarbe &H48484A (dunkelgrau) auf die Zeichenfläche zeichnen
    Paint.Brush = Paint.Color(&H48484A)
    Paint.Arc(W / 2, H / 2, 6) ' Radius = 3
    Paint.Fill
  
  ' Mittelpunkt-Kreis (weiß) auf die Zeichenfläche zeichnen
    Paint.Brush = Paint.Color(Color.White)
    Paint.Arc(W / 2, H / 2, 3) ' Radius = 3
    Paint.Fill()

  Paint.End()

End ' PaintClock(...)

Private Sub PaintPointer(x As Float, y As Float, hColor As Integer, fLineWidth As Float, W As Float, H As Float)
  
' Ohne Paint.Begin(...) und Paint.End(), weil die Prozedur PaintPointer in PaintClock(...) eingebunden wird!
' Zeiger zeichnen
  Paint.Brush = Paint.Color(hColor)
  Paint.LineWidth = fLineWidth
  Paint.LineCap = Paint.LineCapRound 
  Paint.MoveTo(W / 2, H / 2) ' Bild-Mitte
  Paint.RelLineTo(x, y) 
  Paint.Stroke()

End ' PaintPointer(...)

Public Sub livData_Click()

  Dim sLine As String
  
  curLocation = Scan(livData.Current.RichText, "*<br/>*")[0]

  For Each sLine In aLines
    aData = New String[]
    aData = Split(sLine, ",")
    If aData[0] = curLocation Then
       If curLocation = "Berlin" Then
          curLocation = "UTC"
          curTimeOffset = -3600
       Else
          curTimeOffset = 60 * CInt(Scan(aData[2], "*:*")[0]) + CInt(Scan(aData[2], "*:*")[1]) 
       Endif
    Endif
  Next

End ' livData_Click()
