Tips window

Share your advanced PureBasic knowledge/code with the community.
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Tips window

Post by Tenaja »

If you are making an application, and want a Tip of the Day window when you start the application, this may come in handy. This also demonstrates a neat trick to attach a plain text file inside your code, so you don't have the messy data strings. I am certainly open to a new method of reading the strings with pointers, but my tests showed the include location was merely a pointer, so I ended up with this--and it works well.

Have fun!

TipsWindow.pbi:

Code: Select all

EnableExplicit

CompilerIf Defined(Win1, #PB_Constant)
	Enumeration #LastGadgetInWin1
CompilerElse
	Global.i TipsNumber = 0
	Global.i TipsShow = 1
	InitScintilla()
	Enumeration
		#Window_Tips
	EndEnumeration
	
	Enumeration
		#return_key = 1
		#Escape_Key
		#quit_event
		#button_return
	EndEnumeration
	Declare OpenWindow_Tips()
	OpenWindow_Tips()
	Enumeration
 CompilerEndIf
		#Editor_Tips
		#CheckBox_Tips
		#Button_TipsNext
		#Button_TipsClose
	EndEnumeration

#TipsMaxCount = 50							;Edit as needed.
#TipsPath = ""								;edit as needed for your file management.
	
; ------------------------------
DataSection
	TipFileStart:
	IncludeBinary #TipsPath + "Tips.txt"	; Use your file here.
	Data.b 0
EndDataSection 

; --------------------------

Procedure.s NextIncFileString(exitchar.c)
	Protected.c c
	Protected.s s = ""
	
	Repeat
		Read.c c
	Until c <> #LF And c <> #CR			;discard remaining EOL's
	
	If c = exitchar							; If we want to terminate with a beginning character.
		ProcedureReturn ""
	EndIf
	
	While c <> 0 And c <> #LF And c <> #CR
		s + Chr(c)
		Read.c c
	Wend
	
	ProcedureReturn s
EndProcedure


; --------------------------

Procedure OpenWindow_Tips()
	Protected.i i, TipFile, TipGadget
	Protected Dim TipList.s(#TipsMaxCount)
	Protected.s s
	
	If Not OpenWindow(#Window_Tips, 405, 216, 320, 225, "Tip of the Day", #PB_Window_TitleBar|#PB_Window_SystemMenu|#PB_Window_WindowCentered)
		ProcedureReturn
	EndIf
	
;	EditorGadget(#Editor_Tips, 10, 10, 300, 165, #PB_Editor_ReadOnly)	; can use EditorGadget, but scrollbars are inconvenient.
	StickyWindow(#Window_Tips, 1)	; On top of other windows.
	
	If Not  ScintillaGadget(#Editor_Tips, 10, 10, 300, 165, 0)
		MessageRequester("Error", "Scintilla failed to Init for Tips")
		CloseWindow(#Window_Tips)
		ProcedureReturn
	EndIf
	
	CheckBoxGadget(#CheckBox_Tips, 10, 193, 120, 20, "&Show Tips at Startup")
	ButtonGadget(#Button_TipsNext, 145, 190, 70, 25, "&Next Tip")
	ButtonGadget(#Button_TipsClose, 240, 190, 70, 25, "Close")
	AddKeyboardShortcut(#Window_Tips, #PB_Shortcut_Escape, #Escape_Key)
	
	Restore TipFileStart
	For i = 0 To #TipsMaxCount
		TipList(i) = NextIncFileString(';')
		If TipList(i) = ""
			Break
		EndIf
	Next
	
	
	If TipsNumber >= i : TipsNumber = 0 : EndIf		; in case you are entering from outside, with the value saved in a pref. file
	
	; Set up Scintilla gadget for displaying the text.
	ScintillaSendMessage(#Editor_Tips, #SCI_SETWRAPMODE, #True)
	ScintillaSendMessage(#Editor_Tips, #SCI_SETTEXT, 0, @TipList(TipsNumber))
	ScintillaSendMessage(#Editor_Tips, #SCI_SETREADONLY, #True)
	TipsNumber + 1
	
	SetGadgetState(#CheckBox_Tips, TipsShow)
	SetActiveGadget(#Button_TipsNext)
	
	Repeat
		Select WaitWindowEvent()
			Case #PB_Event_Gadget
					Select EventGadget()
						Case #Button_TipsNext
								If TipsNumber >= i : TipsNumber = 0 : EndIf
								ScintillaSendMessage(#Editor_Tips, #SCI_SETREADONLY, #False)
								ScintillaSendMessage(#Editor_Tips, #SCI_SETTEXT, 0, @TipList(TipsNumber))
								ScintillaSendMessage(#Editor_Tips, #SCI_SETREADONLY, #True)
								TipsNumber + 1
						Case #Button_TipsClose
								Break
					EndSelect
			Case #PB_Event_Menu
					If EventMenu() = #Escape_Key	; Escape to close.
						Break
					EndIf
			Case #PB_Event_CloseWindow
					Select EventWindow()
						Case #Window_Tips
								Break
					EndSelect
		EndSelect
	ForEver
	
	
	; Exit the window:
	TipsShow = GetGadgetState(#CheckBox_Tips)
	CloseWindow(#Window_Tips)
	
EndProcedure
Save this as Tips.txt in the same folder (or edit as needed.)

Code: Select all

Save TipsNumber in your application Preference file.
Save TipsShow in your Preference file, too.
I used Scintilla for the scrollbars, and it is already in the app.
You may convert to EditorGadget if you wish; that is up to you.
This also shows a way to include a text file and convert it to strings.
; This is the end of the displayed tips.
; For this comment feature to work, you must remove leading tabs inserted by browser.


These tips are not displayed.
The semicolon at the start of the line indicates the start of comments.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Tips window

Post by Kwai chang caine »

Cool it is like a real :D :wink:
Thanks to sharing 8)
ImageThe happiness is a road...
Not a destination
+18
Enthusiast
Enthusiast
Posts: 228
Joined: Fri Oct 24, 2008 2:07 pm

Re: Tips window

Post by +18 »

nice job, thanks really.
:D :D :D
Post Reply