ReadMe (application help window)

Share your advanced PureBasic knowledge/code with the community.
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

ReadMe (application help window)

Post by Lunasole »

I've made following simple stuff to avoid taking external help files with programs (and to have simple solution of displaying some "about" info).

@wilbert also has trick to embed nicely formatted RTF-files this way, but I found regular txt-files "enough for everyone" for now ^^

Code: Select all

EnableExplicit

;{ ReadMe window }

	; This code packs readme.txt into executable on compilation stage
	; and shows it's content when function is called while program running
	Global WndAbout, TxtAbout
	; separated ReadMe window callbacks
	Procedure ReadmeSize()
		ResizeGadget(TxtAbout, 0, 0, WindowWidth(WndAbout), WindowHeight(WndAbout))
	EndProcedure
	Procedure ReadmeClose()
		UnbindEvent(#PB_Event_CloseWindow, @ReadmeClose(), WndAbout)
		UnbindEvent(#PB_Event_SizeWindow, @ReadmeSize(), WndAbout)
		CloseWindow(WndAbout)
	EndProcedure
	
	; Use following function to display ReadMe window
	; W, H						window sizes
	; FontName$, FontSize		font params to use
	; RETURN:					none, ReadMe window is created or activated if already exists
	Procedure ReadMe(Title$, W = 640, H = 480, FontName$ = "Consolas", FontSize = 9)
		DataSection
			lblAboutMe:
			IncludeBinary "res\ReadMe.txt" ; set here path to your readme.txt
			Data.i 0
		EndDataSection
		
		; only activate window if About dialog already exists
		If IsWindow(WndAbout)
			SetActiveWindow(WndAbout)
			ProcedureReturn 
		EndIf
		
		Protected AboutData$ = PeekS(?lblAboutMe + 3, -1, #PB_UTF8) ; for utf-8 ignoring 1st byte
		WndAbout = OpenWindow(#PB_Any, 0, 0, W, H, Title$, #PB_Window_SystemMenu | #PB_Window_SizeGadget |  #PB_Window_WindowCentered)
		TxtAbout = EditorGadget(#PB_Any, 0, 0, W, H, #PB_Editor_ReadOnly | #PB_Editor_WordWrap)
	
		Protected hFont = LoadFont(#PB_Any, FontName$, FontSize)
		If IsFont(hFont)
			SetGadgetFont(TxtAbout, FontID(hFont))
			FreeFont(hFont)
		EndIf
		
		ClearGadgetItems(TxtAbout)
		AddGadgetItem(TxtAbout, -1, AboutData$)
	
		BindEvent(#PB_Event_CloseWindow, @ReadmeClose(), WndAbout)
		BindEvent(#PB_Event_SizeWindow, @ReadmeSize(), WndAbout)
	EndProcedure

;}


; usage:
;	1. Set path to your readme file at data section
;	2. Add code to your program
;	3. Call ReadMe() function to display content of readme file in separated window
;
; ReadMe window doesn't interact with other windows, just do the EventWindow() check
;	inside you main loop, to avoid messages of ReadMe window. And use CloseWindow (#PB_All) 
; 	at program quit (that's important for DLLs)

; example
OpenWindow(1, 0, 0, 800, 80, "THIS IS MAIN PROGRAM WINDOW")
ReadMe("About")
Repeat
	Define Event = WaitWindowEvent(1)
	If EventWindow() = 1 ; do this to ignore ReadMe window events
		If Event = #PB_Event_CloseWindow
			Break
		EndIf
	EndIf
ForEver

CloseWindow(#PB_All)
Last edited by Lunasole on Fri Sep 16, 2016 2:58 am, edited 1 time in total.
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: ReadMe (application help window)

Post by Demivec »

Thanks for the code. I have one question.

Code: Select all

PeekS(?lblAboutMe + 1, -1, #PB_UTF8) ; for utf-8 ignoring 1st byte
Why do you ignore the first byte of the text information?

A BOM for utf-8 would be 3 bytes. If using ASCII or UTF-8 you lose the first character.

I suppose unicode is out of the question too.
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: ReadMe (application help window)

Post by Lunasole »

Demivec wrote: Why do you ignore the first byte of the text information?

A BOM for utf-8 would be 3 bytes. If using ASCII or UTF-8 you lose the first character.

I suppose unicode is out of the question too.
Yes, should be 3 for UTF-8, fixed in example. I just have tested it on UTF8-files (text files created by windows 7 explorer are utf-8 by default). +1 was enough for editor control to not show BOM (on windows) ^^

Of course it is needed to adjust PeekS() call if using ASCII or unicode formatting (adding built-in detection is unjustifiable as for me, don't want to bloat code because of this).
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
Post Reply