Microsoft Small Basic

Program Listing: CRV802
' Image Viewer
' Version 0.1
' Copyright © 2016 Nonki Takahashi. The MIT License.
' Last update 2016-05-26
'
GraphicsWindow.Title = "Image Viewer 0.1"
Init()
Form()
While "True"
If keyDown Then
key = GraphicsWindow.LastKey
If key = "Up" Then
Previous()
ElseIf key = "Down" Then
Next()
ElseIf key = "Right" Then
Child()
ElseIf key = "Left" Then
Parent()
EndIf
keyDown = "False"
Else
Program.Delay(200)
EndIf
EndWhile
Sub Child
If 0 < iDirs Then
If dirs[iDirs] = ".." Then
Parent()
Else
cd = cd + "\" + dirs[iDirs]
Shapes.SetText(txtDir, cd)
child = ""
GetImageFiles()
EndIf
EndIf
EndSub
Sub Form
gw = GraphicsWindow.Width
gh = GraphicsWindow.Height
GraphicsWindow.BackgroundColor = "#111111"
GraphicsWindow.BrushColor = "White"
txtDir = Shapes.AddText(cd)
Shapes.Move(txtDir, 10, 10)
txtFile = Shapes.AddText(cf)
Shapes.Move(txtFile, 10, 30)
txtSize = Shapes.AddText(size)
Shapes.Move(txtSize, gw - 100, 30)
GraphicsWindow.KeyDown = OnKeyDown
EndSub
Sub OnKeyDown
keyDown = "True"
EndSub
Sub GetImageFiles
' param child - child directory
' param cd - current directory
' return nDirs - number of items in dirs array
' return nFiles - number of items in files array
' The following line could be harmful and has been automatically commented.
' files = File.GetFiles(cd)
n = Array.GetItemCount(files)
For i = 1 To n
len = Text.GetLength(files[i])
ext = Text.ConvertToLowerCase(Text.GetSubTextToEnd(files[i], len - 2))
If Array.ContainsValue(exts, ext) Then
For p = len To 1 Step -1
If Text.GetSubText(files[i], p, 1) = "\" Then
files[i] = Text.GetSubTextToEnd(files[i], p + 1)
p = 1 ' exit For
EndIf
EndFor
Else
files[i] = "" ' not image file
EndIf
EndFor
nFiles = Array.GetItemCount(files)
index = Array.GetAllIndices(files)
' The following line could be harmful and has been automatically commented.
' dirs = File.GetDirectories(cd)
n = Array.GetItemCount(dirs)
For i = 1 To n
len = Text.GetLength(dirs[i])
For p = len To 1 Step -1
If Text.GetSubText(dirs[i], p, 1) = "\" Then
dirs[i] = Text.GetSubTextToEnd(dirs[i], p + 1)
If child = dirs[i] Then
iDirs = i ' index for child directory
EndIf
p = 1 ' exit For
EndIf
EndFor
EndFor
If Text.IsSubText(cd, "\") Then
dirs[Array.GetItemCount(dirs) + 1] = ".."
EndIf
nDirs = Array.GetItemCount(dirs)
If child <> "" Then
iFiles = 0
cf = "[" + dirs[iDirs] + "]"
size = ""
ElseIf 0 < nFiles Then
iFiles = 1
iDirs = 0
cf = files[index[iFiles]]
Else
iDirs = 1
iFiles = 0
cf = "[" + dirs[iDirs] + "]"
size = ""
EndIf
ShowImageFile()
EndSub
Sub Init
exts = "1=bmp;2=gif;3=ico;4=jpg;5=png;6=tif;"
cd = Program.Directory
child = ""
GetImageFiles()
EndSub
Sub Next
If 0 < iFiles And iFiles < nFiles Or iDirs = nDirs And 0 < nFiles Then
iDirs = 0
iFiles = iFiles + 1
cf = files[index[iFiles]]
ElseIf iDirs < nDirs Then
iFiles = 0
iDirs = iDirs + 1
cf = "[" + dirs[iDirs] + "]"
ElseIf nFiles = 0 And 1 < nDirs And iDirs = nDirs Then
iDirs = 1
cf = "[" + dirs[iDirs] + "]"
EndIf
ShowImageFile()
EndSub
Sub Parent
len = Text.GetLength(cd)
pSlash = len
For p = len To 1 Step -1
If Text.GetSubText(cd, p, 1) = "\" Then
pSlash = p
p = 1 ' exit For
EndIf
EndFor
If pSlash < len Then
child = Text.GetSubTextToEnd(cd, pSlash + 1)
cd = Text.GetSubText(cd, 1, pSlash - 1)
Shapes.SetText(txtDir, cd)
GetImageFiles()
EndIf
EndSub
Sub Previous
If iDirs = 1 And 0 < nFiles Then
iDirs = 0
iFiles = nFiles
cf = files[index[iFiles]]
ElseIf 1 < iFiles Then
iFiles = iFiles - 1
cf = files[index[iFiles]]
ElseIf iFiles = 1 Or (iFiles = 0 And iDirs = 1 And 1 < nDirs) Then
iFiles = 0
iDirs = nDirs
cf = "[" + dirs[iDirs] + "]"
ElseIf 1 < iDirs Or 0 < nFiles Then
iFiles = 0
iDirs = iDirs - 1
cf = "[" + dirs[iDirs] + "]"
EndIf
ShowImageFile()
EndSub
Sub ShowImageFile
' param cf - current file
GraphicsWindow.BrushColor = "#111111"
GraphicsWindow.FillRectangle(0, 0, gw, gh)
If Text.EndsWith(cf, "]") Then
size = ""
Else
path = cd + "\" + cf
img = ImageList.LoadImage(path)
iw = ImageList.GetWidthOfImage(img)
ih = ImageList.GetHeightOfImage(img)
size = iw + "×" + ih
w = iw
h = ih
If gw - 20 < w Then
w = gw - 20
h = ih * (gw - 20) / iw
EndIf
If gh - 70 < h Then
w = w * (gh - 70) / h
h = gh - 70
EndIf
GraphicsWindow.DrawResizedImage(img, 10, 60, w, h)
EndIf
Shapes.SetText(txtFile, cf)
Shapes.SetText(txtSize, size)
EndSub