Microsoft Small Basic

Program Listing: RQD767
' Calendar with Flickr Pictures
' Copyright © 2018 Nonki Takahashi. The MIT License.

Init()
While "True"
CalcMonth()
DrawCalendar()
WaitButton()
EndWhile

Sub CalcMonth
If Math.Remainder(year, 4) = 0 And Math.Remainder(year, 100) > 0 Or Math.Remainder(year, 400) = 0 Then
dom[2] = 29
Else
dom[2] = 28
Endif
nol = Math.Floor((year - 1) / 4) - Math.Floor((year - 1) / 100) + Math.Floor((year - 1) / 400) ' number of leap year
woy = Math.Remainder((year + nol), 7) ' week of year
doy = 0
For _m = 1 To mm - 1
doy = doy + dom[_m]
EndFor
w = Math.Remainder((doy + woy), 7)
EndSub

Sub DrawCalendar
url = Flickr.GetRandomPicture(month[mm])
img = ImageList.LoadImage(url)
iw = ImageList.GetWidthOfImage(img)
ih = ImageList.GetHeightOfImage(img)
If (iw / ih) < (gw / gh) Then
width = gw
height = gw / iw * ih
x = 0
y = (gh - height) / 2
Else
width = gh / ih * iw
height = gh
x = (gw - width) / 2
y = 0
EndIf
GraphicsWindow.Title = month[mm] + " " + year
GraphicsWindow.DrawResizedImage(img, x, y, width, height)
x = 24
y = 14
dx = (gw - 20) / 7
GraphicsWindow.FontSize = 14
For _w = 0 To 6
GraphicsWindow.BrushColor = sc
GraphicsWindow.DrawText(x + 2, y + 2, week[_w])
GraphicsWindow.BrushColor = fc
GraphicsWindow.DrawText(x, y, week[_w])
x = x + dx
EndFor
y = y + 32
fs = 40
GraphicsWindow.FontSize = fs
fw = fs * 0.5
For d = 1 To dom[mm]
x = 24 + w * dx
GraphicsWindow.BrushColor = sc
GraphicsWindow.DrawText(x + 2 + fw, y + 2, d)
GraphicsWindow.BrushColor = fc
GraphicsWindow.DrawText(x + fw, y, d)
If d = 9 Then
fw = 0
EndIf
w = w + 1
If 6 < w Then
w = 0
y = y + fs * 1.6
EndIf
EndFor
EndSub

Sub Init
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
month = "1=January;2=February;3=March;4=April;"
month = month + "5=May;6=June;7=July;8=August;9=September;"
month = month + "10=October;11=November;12=December;"
week = "0=Sunday;1=Monday;2=Tuesday;3=Wednesday;"
week = week + "4=Thursday;5=Friday;6=Saturday;"
dom = "1=31;2=28;3=31;4=30;5=31;6=30;7=31;8=31;9=30;10=31;11=30;12=31;"
sc = "#99000000" ' shadow color
fc = "White" ' font color
GraphicsWindow.FontName = "Trebuchet MS"
GraphicsWindow.BrushColor = "Black"
Controls.AddButton("<", gw - 58, gh - 40)
Controls.AddButton(">", gw - 30, gh - 40)
year = Clock.Year
mm = Clock.Month
Controls.ButtonClicked = OnButtonClicked
EndSub

Sub OnButtonClicked
wait = "False"
EndSub

Sub WaitButton
wait = "True"
While wait
Program.Delay(200)
EndWhile
If Controls.GetButtonCaption(Controls.LastClickedButton) = "<" Then
mm = mm - 1
If mm < 1 Then
mm = 12
year = year - 1
EndIf
Else
mm = mm + 1
If 12 < mm Then
mm = 1
year = year + 1
EndIf
EndIf
EndSub