﻿version
6.50
4
Form2
Form4
Form3
Form1
Form5
238
268
C:\USB\Projects\bb4ppc_Projects\Pocket Quotes\stockmarket.ico
5
BinaryFile.dll
Collections.dll
HTTP.dll
ListviewDesktop.dll
StringsEx.dll
5
HTTP.dll
StringsEx.dll
Collections.dll
ListViewDevice.dll
BinaryFile.dll
9
response:WebResponse
ct:ColumnType
strb:StringBuilder
Writer:BinaryFile
stre:StringsEx
Reader:BinaryFile
sl:SortedList
lv:Listview
request:WebRequest
0
Sub designer
addform(Form2,"Loading...","",220,220,220)@
addform(Form4,"Form4","",220,220,220)@
addbutton(form4,btnOK,150,90,75,23,"OK",212,208,200,0,0,0,True,True,9)@
addbutton(form4,btnCancel,10,90,75,25,"Cancel",212,208,200,0,0,0,True,True,9)@
addtextbox(form4,txtStockSymbol,150,55,75,22,"",255,255,255,0,0,0,True,True,False,9)@
addlabel(form4,Label5,10,55,125,25,"Enter Stock Symbol",220,220,220,0,0,0,True,True,9)@
addform(Form3,"Form3","",255,255,255)@
addlabel(form3,Label4,10,240,220,25,"Label4",255,255,255,0,0,0,True,True,8)@
addlabel(form3,Label3,10,10,220,25,"Label3",255,255,255,0,0,0,True,True,14)@
addlabel(form3,Label2,10,225,225,25,"Label2",255,255,255,0,0,0,True,True,9)@
addlabel(form3,Label1,10,195,220,25,"Label1",255,255,255,0,0,0,True,True,9)@
addimage(form3,imgChart,10,40,220,140,"cStretchImage","",245,245,220,True,True)@
addform(Form1,"Form1","",220,220,220)@
addlabel(form1,lblChart,25,5,150,35,"",255,255,255,0,0,0,False,False,12)@
addtimer(form1,Timer1,5,245,500)@
addarraylist(form1,AL2,95,235,80,25)@
addarraylist(form1,AL1,5,235,80,25)@
addmenuitem(form1,mnuFile,"File",True,False)@
addmenuitem(mnufile,mnuSettings,"Settings",True,False)@
addmenuitem(mnufile,mnuHistory,"History",True,False)@
addmenuitem(mnufile,mnuAbout,"About",True,False)@
addmenuitem(mnufile,mnuQuit,"Exit",True,False)@
addmenuitem(form1,mnuUpdate,"Update",True,False)@
addform(Form5,"Settings","",220,220,220)@
addlabel(form5,Label8,15,100,75,25,"Color 2",220,220,220,0,0,0,True,True,9)@
addlabel(form5,Label7,15,70,75,25,"Color 1",220,220,220,0,0,0,True,True,9)@
addtextbox(form5,txtColor2,135,100,75,22,"TextBox2",255,255,255,0,0,0,True,True,False,9)@
addtextbox(form5,txtColor1,135,70,75,22,"TextBox1",255,255,255,0,0,0,True,True,False,9)@
addbutton(form5,btnSave,135,155,75,23,"Save",212,208,200,0,0,0,True,True,9)@
addlabel(form5,Label6,15,40,115,25,"Refresh Interval",220,220,220,0,0,0,True,True,10)@
addtextbox(form5,txtRefresh,135,40,75,22,"TextBox1",255,255,255,0,0,0,True,True,False,9)@
End Sub
@EndOfDesignText@
Sub Globals
      Dim Buffer(0) As byte
	  Dim StockData(0) As string
	 Dim Cells(4)
	  Dim Cats(10)
		Dim Controls(0)
		
		Dim arrColorValues(3) As byte
		
		scale=1
		SortAscending=True
	  '<config>
	  updateInterval=10
	  cColor1="255,255,255"
	  cColor2="128,255,128" 
	  
	  stockSymbols="GM|F|HMC|TM"
	  stockHistory=AppPath & "\stockhistory.txt"
	  '</config>
	  
	  
	  timerCount=updateInterval
	  'tickertape=""
	  
	  programName="Pocket Quotes"
	  programVersion="D.19"
	  programAuthor="Don Graham"
	  
	  '.11 - implemented listview
	  '12 - fix vga
	  '13 - add splash screen, fix add symbol sort
	  '14 - fix timer
	  '15 - add chart, download splash
	  '16 - 
	  '17
	  '18
	  '19 - add settings editor
	  
	  'based on this ASP Alliance article: http://aspalliance.com/112_Building_a_Yahoo_stock_quote_ticker.all
	  
	  '//todo
	  '1 - stock history log - done
	  '2 - config editor - not needed (you can add/edit delete stock symbols)
	  '3 - sort stock symbol list - done
	  '4 - vga - done
	  '5 - landscape
	  '6 - additional stock detail + charts
	  
End Sub


Sub App_Start

form1.Show


	form2.LoadPicture(AppPath & "\gfx\background.jpg")
    form2.Show
	form2.Refresh
	
	  Form1.Text=programName
	  'textbox1.Text=""
	  
	 ' SetDisplay
	  
	   If form1.Height > 400 Then
			scale=2
			ChangeToVGA
		End If
	  
	  SetDisplay
	  
	  If cPPC=True Then
	  	lv.New1("Form1",0,0,form1.Width, form1.Height-(26*scale))
	Else
	  
	  lv.New1("Form1",0,0,form1.Width, form1.Height)
	
	End If
	'  If form1.Height+52 > =form1.Width Then 'portrait
	'		  lv.New1("Form1",0,0,form1.Width,label3.top- 2)
	 '	 Else
	'		lv.New1("Form1",0,0,form1.Width, form1.Height-2)
	'	End If
	  ct.New1
    lv.AddColumn("Stock",(form1.Width /4.5) ,0 ,False, ct.NText )
  '  lv.AddColumn("Price",(form1.Width /3.5) ,1 ,True, coltype.cDecimal)
	lv.AddColumn("Price",(form1.Width/4.5), 1, False, ct.NText)
	lv.AddColumn("$ +/-",(form1.Width /4.5) ,1,False, ct.NText)
	lv.AddColumn("% +/-", (form1.Width / 4.5), 1, False, ct.NText)
    Lv.Fontsize=10
	lv.Visible=True
	lv.FullRowSelect=True
  '  Lv.SetRowColor(cBeige,cSilver)
  lv.SetRowColor(SetColor(cColor1), SetColor(cColor2))
	  lv.AddContextMenu("Add")
	  'lv.AddContextMenu("Edit")
	  lv.AddContextMenu("Delete")
	 ' Form1.Show
	  
	  
	  'LoadTheme
	  
	  Timer1.Interval =  60000' 'milliseconds
      Timer1.Enabled = True

	  CheckMeOut
	  LoadINI
	  lv.SetRowColor(SetColor(cColor1), SetColor(cColor2))
	  LoadStockSymbols
      UpdateStockData
	  If al2.Count > 0 Then
		DisplayStockData 
		
		lv.SelectRow(0)
		lv_SelectionChanged
	  Else
	  	'Msgbox("Unable to download stock data","Http quotes")
	  End If
	'  If form1.Height > 400 Then
		'	scale=2
	'		ChangeToVGA
	'	End If
	form2.Close
	form1.Show
End Sub

Sub SetColor(StrColor)
	arrColorValues()=StrSplit(strColor,",")
	ColorVal= Rgb(arrColorValues(0),arrColorValues(1), arrColorValues(2))
	Return ColorVal
End Sub

Sub SetDisplay
'set display for 320x320
If form1.Width=320 Then
	hscale=1.25
	wscale=1.25
Else
	hscale=1
	wscale=1
End If

If form1.Height > 480 Then
	hscale=2
	wscale=2
Else
	hscale=1
	wscale=1
End If

'on PPC - add for menu bar at bottom
If cPPC=True Then
	basefactor=26*hscale
	'basefactor=26
Else
	basefactor=2
End If

'////////////////////////////////
'FORM1 controls
'////////////////////////////////
	label4.Height=12 * hscale
	label1.Height=48* hscale
	label2.Height=48 * hscale
'	label3.Height=22* hscale
	
	label4.Width=230* wscale
	label4.Left=5* wscale
	
	label1.Width=115* wscale
	label1.Left=5
	
	label2.Width=115* wscale
	label2.Left=label1.Left+label1.Width+2
	
	'label3.Width=230 * wscale
	'label3.Left=5
	
	
	  label4.Top=form1.Height-label4.Height - basefactor
	  
	  
	  label1.Top=label4.Top-label1.Height -(4 * hscale)
	  label2.Top=label4.Top-label2.Height - (4 * hscale)
	 ' label3.Top=label2.Top-label3.Height '- (4* hscale)
	
	
	
End Sub
Sub CheckMeOut
	If FileExist(AppPath & "\pquote.ini") = False Then
		FileOpen(c1,AppPath & "\pquote.ini", cWrite,,cASCII)
		FileWrite(c1, "updateInterval=" & updateInterval)
		FileWrite(c1, "stockSymbols=" & stockSymbols)
		FileWrite(c1, "stockHistory=" & stockHistory)
		FileClose(c1)
	End If
	If DirExist(AppPath & "\cache") = False Then
		DirCreate(AppPath & "\cache")
	End If
		
End Sub

Sub LoadINI
	FileOpen(c1, AppPath & "\pquote.ini",cRead,,cASCII)
	stre.New1
	
	r=FileRead(c1)
	Do Until r= EOF
	x=StrIndexOf(r,"=",0)
	If x >0 Then
		ini=SubString(r,0,x)
		
		value=SubString(r,x+1, StrLength(r)-x-1)
		stre.Trim(value)
		
		Select ini
			Case "updateInterval"
				updateInterval=value
			Case "stockSymbols"
				stockSymbols=value
			Case "stockHistory"
				stockHistory=value
			Case "color1"
				cColor1=value
			Case "color2"
				cColor2=value
		End Select
	End If
	r=FileRead(c1)
	Loop
	FileClose(c1)


End Sub
Sub LoadStockSymbols
'Msgbox(stockSymbols)
	sl.New1
al1.Clear
	StockData()=StrSplit(stockSymbols,"|")
	For i = 0 To ArrayLen(StockData())-1 
		If StockData(i) <> "" Then
			'AL1.Add(StockData(i) )
			' msgbox(StockData(i))
			sl.Add(StockData(i),StockData(i) )
		End If
	Next 
	For x=0 To sl.Count -1
		al1.Add(sl.GetKeyAt(x) )
	Next
	
	
End Sub

Sub UpdateStockData 
	AL2.Clear
	For x=0 To AL1.Count-1
		symbol=AL1.Item(x)
		'Msgbox(symbol)
		symbol=StrReplace(symbol,"^","%5E")
		URL="http://quote.yahoo.com/d/quotes.csv?s=" & symbol & "&d=t&f=sl1d1t1c1ohgvj1pp2wern"
		'Msgbox(URL)
		data=GetText(URL)
		If data="" Then
			data=readCache(symbol)
			If data="" Then
			''Msgbox("No data from http request","Http error")
			Else
				al2.Add(data)
			End If
			
		Else
				'AL2.Add(GetText(URL) )
				
				data=StrReplace(data,"%5E","^")
				al2.Add(data)
				SaveCache(symbol, data)
			If mnuHistory.Checked=True Then
				WriteStockHistory(AL2.Item(x) )
			End If
		End If
	Next

End Sub
Sub saveCache (symbol,data)
	FileOpen(c1, AppPath & "\cache\" & symbol & ".cache",cWrite,,cASCII)
	FileWrite(c1, data)
	FileClose(c1)
End Sub
Sub readCache(symbol)
	If FileExist(AppPath & "\cache\" & symbol & "\.cache") = True Then
		FileOpen(c1,AppPath & "\cache\" & symbol & "\.cache",cRead,,cASCII)
		r=FileRead(c1)
		FileClose(c1)
	Else
		r=""
	End If
	
	Return r
End Sub

Sub DisplayStockData
'function stub
ErrorLabel (DisplayStockDataError)

If al2.Count > 0 Then


'listbox1.Clear
lv.RemoveAllRows


	For x=0 To AL2.Count -1
	
		StockData()=StrSplit(AL2.Item(x), ",")
		
		'get rid of quotes
		strb.New1(50)
		strb.Append(StockData(0) )
		strb.Replace(Chr(34), "")
		symbol=strb.ToString
		
		strb.New1(50)
		strb.Append(stockdata(11) )
		strb.Replace(Chr(34), "")
		pct=strb.ToString
		
		cells(0)=symbol
		cells(1)=StockData(1)
		cells(2)=StockData(4)
		cells(3)=pct
		lv.AddRow(cells() )
		
		
		'listbox1.Add( symbol & cTab & StockData(1) & cTab & StockData(4) )
	Next

	' lv.SelectedRow(0)
	'listbox1.SelectedIndex=0

End If

DisplayStockDataError:

End Sub

Sub lv_SelectionChanged



End Sub

Sub StockDetail
ErrorLabel(StockDetail_error)
     'label1.text="Selected Row: " & lv.SelectedRow
	 StockData()=StrSplit(AL2.Item(lv.SelectedRow), "," )
	
	
	
	strb.New1(50)
	strb.Append(StockData(15) )
	strb.Replace(Chr(34), "")
	company=strb.ToString
	
	x =StrIndexOf(company,CRLF,0)
	If x > 0 Then
		company=SubString(company,0,x)
	End If
	
	strb.New1(50)
	strb.Append(StockData(3))
	strb.Replace(Chr(34),"")
	stkTime=strb.ToString
	
	strb.New1(50)
	strb.Append(StockData(2) )
	strb.Replace(Chr(34), "")
    stkDate=strb.ToString
	
	strb.New1(50)
	strb.Append(StockData(12) )
	strb.Replace(Chr(34), "")
	'history="52 week: " & strb.ToString
	
	traded="Last trade:" & stkTime & " est on " & stkDate 
	
		
	'fluctuation="Highest: " & StockData(6) & " Lowest: " & StockData(7)
	hval=StockData(6)
	lval=StockData(7)
	
	h1=StrIndexOf(hval,".",0)
	If h1>-1 Then
		hval=SubString(hval,0,h1) & "." & SubString(hval,h1+1,2)
	End If
	
	l1=StrIndexOf(lval,".",0)
	If l1>-1 Then
		lval=SubString(lval,0,l1) & "." & SubString(lval,l1+1,2)
	End If

	fluctL="Highest:" & stre.PadLeft(hval, 15, Chr(32))
	fluctR="Lowest:" & stre.PadLeft(lval,15, Chr(32))
	
	
	'fluctL="Highest:" & stre.PadLeft(StockData(6), 15, Chr(32))
	'fluctR="Lowest :" & stre.PadLeft(StockData(7),15, Chr(32))
	fluctuation=fluctL & CRLF  & fluctR


	'TextBox1.Text=  company   & CRLF & recent & CRLF &  fluctuation & CRLF &  history & CRLF & traded
	
	'label1.Text=company
	
	recentL="Close:" & stre.PadLeft(StockData(10),15,Chr(32))
	recentR="Open:" & stre.PadLeft(StockData(5),15, Chr(32))
	recent=recentL & CRLF & recentR
	
	
	
	
	
	'tickertape=" * Symbol: " & lv.Cellget("Stock", lv.SelectedRow) & " * " & recent & " *  " & fluctuation & " *  " & history & " *  " & traded
	label1.Text=CRLF & recent 
	label2.Text=  CRLF & fluctuation
	label3.Text=company
	label4.Text=traded
	StockDetail_error:
	

End Sub


Sub RefreshStockData (mode)

'//TODO: use mode to determine whether to get updates from Internet


	imgChart.Image=AppPath & "\gfx\download.jpg"
	label1.Text=""
	label2.Text=""
	label3.Text=""
	label4.Text=""
	Form3.Text="Downloading..."
	imgChart.Enabled=True
	imgChart.Visible=True
	
	
	
	'form1.Refresh
	Form3.Show
	Form3.Refresh
	
		UpdateStockData
	
	Form3.Close
	
	If al2.Count > 0 Then
		DisplayStockData
		lv.SelectRow(0)
	   lv_SelectionChanged
	End If
	
	
	form1.Refresh

End Sub


Sub mnuUpdate_Click
	
	RefreshStockData(True)
	 
End Sub
Sub Lv_DoubleClick
 'Msgbox("Future screen: show information for " & lv.Cellget("Stock",lv.SelectedRow),"Program TODO")
	GetChart(lv.Cellget("Stock",lv.SelectedRow))

End Sub

Sub lv_ColumnClick
	'mnuUpdate_Click
'lv.TableSort("colFamilyName ASC")

  'SortAscending=Not(SortAscending)
 ' lv.SortColumn(lv.SelectedColumn,SortAscending)
End Sub
Sub GetText (URL)
ErrorLabel (GetTextError)
      Response.New1
	  
      Request.New1(URL)
	  request.TimeOut=30000
      Response.Value = Request.GetResponse 'This line calls the server and gets the response.
      string = Response.GetString 'Get the Response string.
      Response.Close
      Return string
GetTextError:
	  Return ""
End Sub
Sub DownloadFile (LocalFile,URL)

ErrorLabel(DownloadFileError)     
   Response.New1
      Request.New1(URL)
      Response.Value = Request.GetResponse 'Launch the request and get the response.
      'Msgbox("Download size: " & Response.ContentLength) 'Show the file size.
      Reader.New1(Response.GetStream,True) 'Use a BinaryFile object to read the data from the Response stream.
      FileOpen(c1,LocalFile,cRandom)
      Writer.New1(c1,False)
      Dim buffer(4096) As byte
	 ' ProgressBar.Maximum = Response.ContentLength
	 ' ProgressBar.value = 0
      'ProgressBar.refresh
      count = Reader.ReadBytes(buffer(),4096)
      Do While count > 0
            Writer.WriteBytes2(buffer(),0,count)
            count = Reader.ReadBytes(buffer(),4096)
		'	ProgressBar.value = Progressbar.value + count
           ' progressBar.refresh
      Loop
      FileClose(c1)     
      Response.Close 'Close the Response stream.
	  
    ' Msgbox("File saved.")
DownloadFileError:
'' DNG
FileClose(c1)
End Sub

Sub GetChart(symbol)
url="http://ichart.finance.yahoo.com/t?s=" & symbol
localfile=AppPath & "\cache\" & symbol & ".jpg"
DownloadFile(localfile,url)
chartImage=AppPath & "\cache\" & symbol & ".jpg"
If FileExist(chartImage) = False Then
	chartImage=AppPath & "\gfx\missing_image.jpg"
End If

'Msgbox(chartImage)
'imgChart.Image=AppPath & "\cache\" & symbol & ".jpg"
imgChart.Image=chartImage
StockDetail
form3.Text="Stock Info"
form3.Show
form3.Refresh
End Sub
Sub WriteStockHistory(str)
	FileOpen(c1,stockHistory, cWrite, cAppend, cASCII)
	FileWrite(c1, str)
	FileClose(c1)

End Sub


Sub mnuQuit_Click
	AppClose
End Sub

Sub Timer1_Tick



	 timerCount=timerCount-1
     If timerCount < = 0 Then 
	' msgbox("timer reached 0")
	 	timerCount=updateInterval
		If DateDayOfweek(Now)="Saturday" OR DateDayOfweek(Now)="Sunday" Then
			Return
		Else
			mnuUpdate_Click
		End If
	
		
	 End If
	 
End Sub

Sub mnuAbout_Click
	msg=programName & CRLF & "Version: " & programVersion & CRLF & "Author: " &programAuthor
	
	Msgbox(msg, "About")
End Sub

Sub mnuHistory_Click
	If mnuHistory.Checked=True Then
		mnuHistory.Checked=False
	Else
		mnuHistory.Checked=True
	End If
End Sub

Sub Lv_Contextclick
    ' Select Lv.SelectedContextMenu
	Select lv.SelectedContextText
	
       Case "Delete"
           'Do something
		   stk=lv.Cellget(lv.Colname(0), lv.SelectedRow)
		   al1.Remove(stk)
		   strb.New1(50)
		   strb.append(stocksymbols)
		   strb.Replace(stk & "|", "")
		   strb.Replace("|" & stk, "")
		   strb.Replace(stk, "")
		   stocksymbols=strb.ToString
		   
		   lv.RemoveRow(lv.SelectedRow)
		   'Msgbox(stocksymbols)
		   SaveINI
		   '/dng
		   RefreshStockData (False)
		   
		   
       Case "Edit"
            'Do something
			'Msgbox(lv.SelectedRow)
	Case "Add"
		'Do Something
		AddStockSymbol
	'	LoadStockSymbols
	'	mnuUpdate_Click
	End Select
End Sub

Sub ChangeToVGA
    Controls() = GetControls("")
    For i = 0 To ArrayLen(Controls())-1
        Select ControlType(Controls(i))
            Case "ListBox","NumUpDown","Button","TextBox","Label","ComboBox","Panel","RadioBtn","Table","ImageButton","CheckBox","Image"
                Control(Controls(i)).Left = 2 * Control(Controls(i)).Left
                Control(Controls(i)).Top = 2 * Control(Controls(i)).Top
                Control(Controls(i)).Height = 2 * Control(Controls(i)).Height
                Control(Controls(i)).Width = 2 * Control(Controls(i)).Width
'*** Uncomment these lines if your application includes Tables.
'                If ControlType(Controls(i)) = "Table" Then
'                    tbl = Controls(i)
'                    For i2 = 0 To Control(tbl,Table).ColCount-1
'                        col = Control(tbl,Table).ColName(i2)
'                        Control(tbl,Table).ColWidth(col) = Control(tbl,Table).ColWidth(col) * 2
'                    Next
'                End If
        End Select
    Next
End Sub

Sub btnCancel_Click
	'pnlAddStock.Visible=False
	Form4.Close
End Sub

Sub AddStockSymbol
	txtStockSymbol.Text=""
	'pnlAddStock.Visible=True
	If cPPC=True Then
		Sip(True)
	End If
	Form4.Text="Add Stock"
	Form4.Show
	Form4.Refresh

End Sub

Sub btnOK_Click
	'pnlAddStock.Visible=False
	If cPPC=True Then
		Sip(False)
	End If
	Form4.Close
	' code to save to file
	al1.add(txtStockSymbol.Text)
	
	al1.Sort(cCaseInsensitive)
	
	stocksymbols=stocksymbols & "|" & txtStockSymbol.text
	'Msgbox(stocksymbols)
	SaveINI
	LoadStockSymbols
	RefreshStockData (True)
	 
End Sub


Sub SaveINI

cColor1=txtColor1.Text
cColor2=txtColor2.Text

FileOpen(c1, AppPath & "\pquote.ini",cWrite,,cASCII)
FileWrite(c1,"updateInterval=" & updateinterval)
FileWrite(c1,"stockSymbols=" & stocksymbols)
FileWrite(c1, "stockHistory=" & stockhistory)
FileWrite(c1, "color1=" & cColor1)
FileWrite(c1, "color2=" & cColor2)
FileClose(c1)

'update colors
 lv.SetRowColor(SetColor(cColor1), SetColor(cColor2))
 RefreshStockData(True)
End Sub


Sub mnuSettings_Click
	txtRefresh.Text=updateInterval
	txtColor1.Text=cColor1
	txtColor2.Text=cColor2
	form5.Show
End Sub

Sub btnSave_Click
	updateInterval=txtRefresh.Text
	SaveINI
	form5.Close
End Sub
