MaxGUI: Create a HTML page on the fly?

BlitzMax Forums/BlitzMax Programming/MaxGUI: Create a HTML page on the fly?

Hi!

Atm I'm displaying a simple plain textfile in a textarea of my app and then use FormatTextArea to highlight and color certain lines/words.

Could I do something similar by creating a HTML file out of the text on the fly and then display it with a HTMLView?

Are there any bmx modules for such operations? As I don't want to do that for about 200+ files by hand.

Thanks,
Grisu

A html file is just a text file. You don't need any conversion.

Just temporarily save the file to disk and have the htmlview display the file.

???

Yeah, but I need to write all functions to change the text itself and save it into the html-syntax.

So I was hoping for a module that makes this part easier.

if u use a hmtlview...it'll be displayed as webpage, so it'll formated the correct colors if u use the correct html tags when saying it to disk

I do this for Framework Assistant.
Just written a simple 'html' type specifically for generating the html source.
All it consists of is a TList and some Methods for creating the various parts of the HTML source.
Something like this:
htm.Clear
htm.WriteHeader
htm.AddText "whatever text here"
htm.AddText "more text blah .."
htm.WriteFooter
htm.Save

Then I use HTMLViewGo to display the page.

For colored text I do this:
htm.AddText "<font color='#0000FF'>I am blue</font>"


I also make use the following tags:

<p> .. </p>       for paragraphs
<br>              line break
<hr>              horizontal bar/line


Thanks Jim.

1.
I don't want to reinvent the wheel and I never spend time on html coding by hand. Could I use your type construct? :)


2.
Is it possible to search for a certain string inside the HTMLViewGo or even change the font color of a certain string (same as FormatTextAreaText)?

Is it possible to search for a certain string inside the HTMLViewGo or even change the font color of a certain string (same as FormatTextAreaText)?

yes, you could wrap the text in a span and set a style for it specifically.

i.e.

some text <span style="color:#ff0000">Your text</span> other text


you can specify color in hex values, named colors or RGB values with rgb(r,g,b)

Something quick to get you going ..

This will let you write text in different colors as well as BOLD , ITALICS, UNDERLINE

The example shows a simple HTML-generated file.
After clicking the NOTIFY button a modified version is shown.

I have used Blitz forum codes for text formatting.
See the ParseLine() function and example text.

The basics:
htm:HTML_Type

Methods/Functions
------------------------------------------------------------
htm.Clear               - clears the text (and creates a header)
htm.AddText t$          - adds an entry
htm.AddLink url$,desc$  - adds a link to url/file etc..
htm.Change f$,r$        - searches for f$ and replaces with r$
htm.Show htmviewgadget  - show results into a view gadget
htm.Cleanup             - removes temporary htm file and text



The source:
' Basic HTML source generator
' Jim Brown
' v0.01


Type HTML_Type
	Global htmlsource:TList=New TList
	Global templist:TList=New TList
	' add text entry
	Method New()
		Clear
	End Method
	' add a line if text
	Method AddText(t$,LineBreak%=True)
		Local lb$=""
		If LineBreak lb$="<br>"
		htmlsource.AddLast t$+lb$
	End Method
	' add a link to a url/file etc ..
	Method AddLink(url$,desc$,lb%=False)
		AddText "<a href=~q" + url$ + "~q>" + desc$ + "</a>" , lb
	End Method
	' create top header file for results.htm file
	Method Clear()
		htmlsource.Clear
		AddText "<html>",False
		AddText "<head>",False
		AddText "<title>HTML Generated Text</title>",False
		AddText "</head>",False
		AddText "<body bgcolor=~q#FFFFFF~q>",False
	End Method
	' search for and change a text entry
	Method Change(f$,r$)
		For Local l$=EachIn htmlsource
			l$=l.Replace(f$,r$)
			templist.AddLast l$
		Next
		htmlsource.Clear
		SwapLists htmlsource,templist
		templist.clear
	End Method
	' convert tags to HTML compatible tags
	Function ParseLine:String(a:String)
		a$=a.Replace("<i>","<em>")				' ITALICS ON
		a$=a.Replace("</i>","</em>")				' ITALICS OFF
		a$=a.Replace("<b>","<strong>")			' BOLD ON
		a$=a.Replace("</b>","</strong>")		' BOLD OFF
		a$=a.Replace("<u>","<u>")				' UNDERLINE ON
		a$=a.Replace("</u>","</u>")				' UNDERLINE OFF
		a$=a.Replace("[rgb]","<font color=~q#")	' COLOR ON
		a$=a.Replace("[/rgb]","~q>")				' COLOR OFF
		a$=a.Replace("[bar]","<hr>")				' HORIZONAL BAR
		't$=t.Replace("~q" , Chr$(34))
		Return a$
	End Function
	' show the resulting text into a HTML view gadget
	Method Show(htmviewgadget:TGadget)
		AddText "</body>",False
		AddText "</html>",False
		Local file:TStream=WriteFile("temp.htm")
		If file
			' write the file
			For Local t$=EachIn htmlsource
				WriteLine file,ParseLine(t$)
			Next
			CloseFile file
			' open the file for viewing
			Local htmfile$=AppDir$+"/temp.htm"
			htmfile$=htmfile.Replace("//","/")
			HtmlViewGo htmviewgadget,htmfile$
		Else
			Notify "HTML file error."
		EndIf
	End Method
	' remove text and temporary file
	Method Cleanup()
		htmlsource.Clear
		If FileSize("temp.htm")>-1 DeleteFile "temp.htm"
	End Method
End Type


' *********************************************************

' basic GUI frontend for viewing generated page

Global htmlview:TGadget
Const winW%=660 , WinH%=460
Const winflags%=WINDOW_RESIZABLE|WINDOW_TITLEBAR|WINDOW_STATUS
Global win:TGadget = CreateWindow("HTML Example",180,70,winW,winH,Null,winflags)
htmlview=CreateHTMLView(8,40,ClientWidth(win)-16,ClientHeight(win)-48,win)
SetGadgetLayout htmlview,1,1,1,1

' *********************************************************


' create a simple HTML test page
' Note: newly created pages are automatically
' initiallised with a header

Global htm:HTML_Type=New HTML_Type

htm.AddText "Hello World!"
htm.AddText "[rgb]8080ff[/rgb]This text should be in blue."
htm.AddText "[rgb]ee0000[/rgb]This line color will change shortly!!"
htm.AddText "[rgb]000000[/rgb]Black (default)"
htm.AddText "Example of <i>Italics</i> and <b>bold</b> too."
htm.AddText "[bar]"
htm.AddText "After clicking the NOTIFY button the RED text should change"
htm.AddText "to GREEN and the new results displayed."
' show the generated page into the htmlview gadget
htm.Show htmlview

Notify "Click me after reading the HTML code"

' search for and change the RED to GREEN
htm.Change "[rgb]ee0000[/rgb]","[rgb]00ff00[/rgb]"
htm.Change "will change shortly","has changed"
' show new results
htm.Show htmlview

Notify "Finally, how to clear the page and start again .."

htm.Clear
htm.AddText "Game over .. "
htm.AddText "Or, you might want to vist the ",False ' (False=suspend line break)
htm.AddLink "http://www.blitzmax.com","BlitzMax"
htm.AddText "site!!"
htm.Show htmlview



' *********************************************************

Repeat
	Local event%=WaitEvent()
	DebugLog CurrentEvent.ToString()
	Select event
		' closed via [X] or other shutdown method
		Case EVENT_WINDOWCLOSE , EVENT_APPTERMINATE
		Exit
	End Select
Forever

htm.Cleanup
End


This will keep me busy for a long while.
I'll add you to my credits window.

Thanks a lot.

P.S.: Could I also load incbined images to show up in the html file? Or do they have to be in extracted form on hdd?

they have to be on HD (html gadget reads everything from HD)

It is on hdd, it would be just incbined into the main exe.
But I guess its too much for this simple gadget.

actually, I'm not sure, but I think you probably could, you'd just need to find a way of telling the browser that you app is a web server and then dealing with the request for the image yourself (i.e. output headers then spew out the image data).

The browser object should handle it fine.

You could allso write it at runtime using javascript.

example:
HtmlViewGo htmlview, "about:" ' needed for a proper document object, can use any kind of html url though.
Delay 100 ' needed for the about page to load properly
HtmlViewGo htmlview, "javascript:document.write('<html><body>Hello World!</body></html>');"


Wow, nice find grable!

Is it possible to check if the user clicks a certain link url inside the htmlwindow?

If so I could check for these and display the images in a normal canavs. As a result I could incbin all media inside my main exe and no could just there go and grab it.

have you checked out the HTMLVIEW_NONAVIGATE style of CreateHTMLView? see the helpfile under CreateHTMLView

Thanks assari!

So in theory I could readout the Evendata via EventText$()
And get the url = my Image link and make bmx to load it into the canavas. Now THATS COOL. :)