Blitz Browse

BlitzMax Forums/BlitzMax Programming/Blitz Browse

To get use to MaxGUI I've been porting some of the old BlitzPlus examples. I thought I'd just post my conversion of Beaker's Blitz Browse found in the archives.
SuperStrict
'BLITZBROWSE by Beaker 2004 (updated)
'Rewritten using MaxGUI by Khomy Prime


'SETUP WINDOW
Global window:TGadget=CreateWindow( "Blitz Browse",0,0,800,600)
SetMinWindowSize window,200,0

'SETUP MENUS
Local filemenu:TGadget = CreateMenu ("  &File  ",0,WindowMenu(window))
	Local openmenuitem:TGadget = CreateMenu ("&Open...",1,filemenu)

CreateMenu ("",999,filemenu)
	Local exitmenuitem:TGadget = CreateMenu ("E&xit",9,filemenu)

Local bookmenu:TGadget = CreateMenu ("  &Bookmarks  ",0,WindowMenu(window))
	Local addbookmenuitem:TGadget = CreateMenu ("&Add bookmark",21,bookmenu)
	Local organisebookmenuitem:TGadget = CreateMenu ("&Organise bookmarks",22,bookmenu)
	DisableMenu organisebookmenuitem
	Local defaultmenuitem:TGadget = CreateMenu ("Set as &default homepage",23,bookmenu)
	CreateMenu ("",999,bookmenu)



If FileType("bookmark.txt") <> 1	'Then create a bookmark file with default bookmarks
	Local bookfile:TStream = WriteFile("bookmark.txt")
	RestoreData bookdata
	Local bookname:String
	Local bookURL:String
	ReadData bookname
	ReadData bookURL
	While bookname$ <> "END"
		WriteLine bookfile,bookname$
		WriteLine bookfile,bookURL$
		ReadData bookname$
		ReadData bookURL
	Wend
	CloseFile bookfile
EndIf


'read the bookmarks into the bookmark menu
Global bookf:Int=500
Local bookfile:TStream = ReadFile("bookmark.txt")
While Not Eof(bookfile)
	Local bookname:String = ReadLine(bookfile)
	Local bookURL:String = ReadLine(bookfile)
	If bookURL<>""
		CreateMenu (bookname,bookf,bookmenu)
		bookf = bookf +1
	EndIf
Wend
CloseFile bookfile
CreateMenu ("",999,bookmenu)

Local helpmenu:TGadget = CreateMenu("  &Help  ",0,WindowMenu(window))
Local aboutmenu:TGadget = CreateMenu("&About",1000,helpmenu)

UpdateWindowMenu window


If FileType("default.txt") <> 1	'Then create the default.txt file with the default homepage
	Local deffile:TStream = WriteFile("default.txt")
		WriteLine deffile,"http://www.blitzbasic.com"
	CloseFile deffile
EndIf

Local deffile:TStream = ReadFile("default.txt")
	Global defURL:String = ReadLine (deffile)
CloseFile deffile




'SETUP BUTTONS
Local panel:TGadget = CreatePanel (0,0,800,40,window,0)
	SetGadgetLayout panel,EDGE_ALIGNED,0,EDGE_ALIGNED,0
	Local backbutt:TGadget = CreateButton ("Back",5,5,60,30,panel)
	Local forebutt:TGadget = CreateButton ("Forward",70,5,60,30,panel)
	Local refreshbutt:TGadget = CreateButton ("Refresh",135,5,60,30,panel)
	Local homebutt:TGadget = CreateButton ("Home",200,5,60,30,panel)
	Global URLfield:TGadget = CreateTextField (265,10,350,20,panel)
	Local URLgo:TGadget = CreateButton ("GO!",620,10,60,20,panel)


'SETUP HTML VIEW
Global html:TGadget=CreateHTMLView( 0,45,ClientWidth(window),ClientHeight(window)-43,window )
SetGadgetLayout html,1,1,1,1


Global current:String
GoURL(defURL)


'MAIN LOOP
While WaitEvent()

	Select EventID()
		Case EVENT_MENUACTION	'MENU EVENTS
			Select EventData()
				Case 1	'Open local File
					Local localURL:String = RequestFile("Open local file","htm,html,jpg,gif,png")
					If FileType (localURL) = 1
						GoURL(localURL)
					EndIf
				Case 9	'Close program
					End
				Case 21	'Add bookmark
					Local bookfile:TStream = OpenFile("bookmark.txt")
					Local found:Int = False
					While Not Eof(bookfile)
						Local bookname:String = ReadLine(bookfile)
						Local bookURL:String = ReadLine(bookfile)
						If bookURL = current
							Notify "URL already in bookmarks"
							found = True
							Exit
						EndIf
					Wend
					If found = False
						WriteLine bookfile,current
						WriteLine bookfile,current
						CreateMenu (current,bookf,bookmenu)
						bookf = bookf +1
						UpdateWindowMenu window
					EndIf
					CloseFile bookfile
				Case 23	'Set as Default homepage
					Local deffile:TStream = WriteFile("default.txt")
						WriteLine deffile,current$
					CloseFile deffile
				Case 1000	'About
					Notify "Blitz Browse by Beaker 2004~n(bookmarks are in the bookmark.txt file)"
			End Select
			If EventData() >= 500	'Jump to a specific bookmark
				Local f:Int = 0
				Local bookfile:TStream = ReadFile("bookmark.txt")
				While Not Eof(bookfile)
					Local bookname:String = ReadLine (bookfile)
					Local bookURL:String = ReadLine (bookfile)
					If f = EventData()-500 Then
						GoURL(bookURL)
						Exit
					EndIf
					f = f +1
				Wend
			EndIf	
		Case EVENT_GADGETACTION
			Select EventSource()
				Case backbutt
						HtmlViewBack html
				Case forebutt
						HtmlViewForward html
				Case refreshbutt
						GoURL(HtmlViewCurrentURL(html))
				Case homebutt
					Local deffile:TStream = ReadFile("default.txt")
						GoURL(ReadLine (deffile))
					CloseFile deffile
				Case URLgo
					GoURL(TextFieldText(URLfield))
					
				Case URLfield
					If EventData() = 13
						GoURL(TextFieldText(URLfield))
					EndIf
				Case html
					SetGadgetText URLfield ,HtmlViewCurrentURL(html)
					
			End Select
		Case EVENT_WINDOWCLOSE
			Select EventSource()
				Case window
					End
			End Select
	End Select
	
	
Wend
End




Function GoURL(URL$)
	current = URL$
	SetGadgetText URLfield,current
	HtmlViewGo html,current
	SetGadgetText window,"Blitz Browse - "+current
End Function


#bookdata
DefData "Blitz Basic"
	DefData "http://www.blitzbasic.com"
DefData "Blitz Coder"
	DefData "http://www.blitzcoder.com"
DefData "FONText bitmap font creation"
	DefData "http://www.playerfactory.co.uk"
DefData "Redflame Games and Tools"
	DefData "http://www.redflame.net"
DefData "Blitz Base"
	DefData "http://www.blitzbase.de/"
DefData "Game Making Tools forum"
	DefData "http://www.playerfactory.co.uk"
DefData "END"
	DefData "END"


Here is my version. Not sure why it didn't make it into the official samples, maybe it works slightly differently on Max/Linux.

'MAX BROWSE by Beaker 2005

Strict 

Global window:TGadget
Global html:TGadget


'SETUP WINDOW
window=CreateWindow( "Max Browse",0,0,800,600)
SetMinWindowSize window,200,0


'SETUP MENUS
Local filemenu = CreateMenu ("  &File  ",0,WindowMenu(window))
	Local openmenuitem = CreateMenu ("&Open...",1,filemenu)

CreateMenu ("",999,filemenu)
	Local exitmenuitem = CreateMenu ("E&xit",9,filemenu)

Local bookmenu = CreateMenu ("  &Bookmarks  ",0,WindowMenu(window))
	Local addbookmenuitem = CreateMenu ("&Add bookmark",21,bookmenu)
	Local organisebookmenuitem = CreateMenu ("&Organise bookmarks",22,bookmenu)
	DisableMenu organisebookmenuitem
	Local defaultmenuitem = CreateMenu ("Set as &default homepage",23,bookmenu)
	CreateMenu ("",999,bookmenu)

	
Local bookname$, bookURL$, bookfile
If FileType("bookmark.txt") <> 1	'Then create a bookmark file with Default bookmarks
	bookfile = WriteFile("bookmark.txt")
	RestoreData bookdata
	ReadData bookname$
	ReadData bookURL$
	While bookname$ <> "END"
		WriteLine bookfile,bookname$
		WriteLine bookfile,bookURL$
		ReadData bookname$
		ReadData bookURL
	Wend
	CloseFile bookfile
EndIf

'read the bookmarks into the bookmark menu
Global bookf=500
bookfile = ReadFile("bookmark.txt")
While Not Eof(bookfile)
	bookname$ = ReadLine(bookfile)
	bookURL$ = ReadLine(bookfile)
	If bookURL<>""
		CreateMenu (bookname,bookf,bookmenu)
		bookf = bookf +1
	EndIf
Wend
CloseFile bookfile
CreateMenu ("",999,bookmenu)

Local helpmenu = CreateMenu("  &Help  ",0,WindowMenu(window))
Local aboutmenu = CreateMenu("&About",1000,helpmenu)

UpdateWindowMenu window


If FileType("default.txt") <> 1	'Then create the default.txt file with the Default homepage
	Local deffile = WriteFile("default.txt")
		WriteLine deffile,"http://www.blitzbasic.com"
	CloseFile deffile
EndIf

Local deffile = ReadFile("default.txt")
	Global defURL$ = ReadLine (deffile)
CloseFile deffile




'SETUP BUTTONS
Local panel = CreatePanel (0,0,800,40,window,0)
	SetGadgetLayout panel,1,0,1,0
	Local backbutt:TGadget = CreateButton ("Back",5,5,60,30,panel)
	Local forebutt:TGadget = CreateButton ("Forward",70,5,60,30,panel)
	Local refreshbutt:TGadget = CreateButton ("Refresh",135,5,60,30,panel)
	Local homebutt:TGadget = CreateButton ("Home",200,5,60,30,panel)
	Global URLfield:TGadget = CreateTextField (265,10,350,20,panel)
	Local URLgo:TGadget = CreateButton ("GO!",620,10,60,20,panel,BUTTON_OK)

	

'SETUP HTML VIEW
html=CreateHTMLView( 0,45,ClientWidth(window),ClientHeight(window)-50,window, HTMLVIEW_NONAVIGATE )
SetGadgetLayout html,1,1,1,1


Global current$
GoURL(defURL)

Local localURL$,found,url$

'MAIN LOOP
While WaitEvent()
	Print EventData()
	Select EventID()
		Case EVENT_GADGETDONE	'page loaded
			If EventSource() = html
				SetGadgetText URLfield,HtmlViewCurrentURL(html)			
			EndIf
		Case EVENT_MENUACTION	'MENU EVENTS
			Select EventData()
				Case 1	'Open Local File
					DebugLog "OPEN LOCAL"
					localURL$ = RequestFile("Open local file","Web files - *.htm,html,jpg,gif,png:htm,html,jpg,gif,png;All files:*")
					If FileType (localURL) = 1
						GoURL(localURL)
					EndIf
				Case 9	'Close program
					End
				Case 21	'Add bookmark
					bookfile = OpenFile("bookmark.txt")
					found = False
					While Not Eof(bookfile)
						bookname = ReadLine(bookfile)
						bookURL = ReadLine(bookfile)
						If bookURL = current
							Notify "URL already in bookmarks"
							found = True
							Exit
						EndIf
					Wend
					If found = False
						WriteLine bookfile,current
						WriteLine bookfile,current
						CreateMenu (current,bookf,bookmenu)
						bookf = bookf +1
						UpdateWindowMenu window
					EndIf
					CloseFile bookfile
				Case 23	'Set as Default homepage
					deffile = WriteFile("default.txt")
						WriteLine deffile,current$
					CloseFile deffile
				Case 1000	'About
					Notify "Max Browse by Beaker 2005"+Chr(13)+"(bookmarks are in the bookmark.txt file)"
			End Select
			If EventData() >= 500	'Jump To a specific bookmark
				Local f = 0
				bookfile = ReadFile("bookmark.txt")
				While Not Eof(bookfile)
					bookname$ = ReadLine (bookfile)
					bookURL = ReadLine (bookfile)
					If f = EventData()-500 Then
						GoURL(bookURL)
						Exit
					EndIf
					f = f +1
				Wend
			EndIf	
		Case EVENT_GADGETACTION	'BUTTON EVENTS
			Select EventSource()
				Case html	' Catch clicking on URL links
					goURL String(EventExtra())
				Case backbutt
						HtmlViewBack html
				Case forebutt
						HtmlViewForward html
				Case refreshbutt
						GoURL(HtmlViewCurrentURL(html))
				Case homebutt
					deffile = ReadFile("default.txt")
						GoURL(ReadLine (deffile))
					CloseFile deffile
				Case URLgo
					GoURL(TextFieldText(URLfield))					
			End Select
					
		Case EVENT_WINDOWCLOSE	'WINDOW CLOSED EVENT
			Select EventSource()
				Case window
					End
			End Select
				
	End Select
	

Wend
End



Function GoURL(URL$)
	current = URL$
	SetGadgetText URLfield,current
	DebugLog current
	HtmlViewGo html,current
	SetGadgetText window,"Max Browse - "+current
End Function




#bookdata
DefData "Blitz Basic"
	DefData "http://www.blitzbasic.com"
DefData "FonText bitmap font creation"
	DefData "http://www.playerfactory.co.uk"
DefData "gile[s] lightmapper"
	DefData "http://www.frecle.net/giles/"
DefData "GUIde GUI editor"
	DefData "http://members.home.nl/wdw/guide/"
DefData "Game Making Tools forum"
	DefData "http://playerfactory.proboards25.com"
DefData "END"
	DefData "END"