texteditor functions

You can post your code snippets here for others to use and learn from
Post Reply
ilbarbax
Posts: 172
Joined: Thu Apr 01, 2010 6:41 pm

texteditor functions

Post by ilbarbax »

I want to share some of my functions I developed to handle texteditor contents.
Despite the simple appearance, they cost a lot of efforts mainly due to the fact that documentation is lacking examples (sorry for this critic but RapGui manual is very pour of examples)


This function colors a specific text into a specific line

Code: Select all

Function p_te_color_txt(id$,l_n,txt$,clr)
		Local line_pos=moai.DoMethod(id$, "GetPosition", "0", ToString(l_n-1))
		Local line_len=moai.DoMethod(id$, "GetLineLength", ToString(l_n-1))
		Local t$ = moai.DoMethod(id$, "GetText", line_pos, line_pos+line_len)
		Local pz=FindStr(t$,txt$)
		Local tt$=LeftStr(t$,pz);only the color code before txt$ needs to be counted
		Local bkw=CountStr(tt$,Chr(27).."P[")*(-10)+CountStr(t$,Chr(27).."b")*(-2)
		If pz>=0 Then moai.DoMethod(id$, "SetColor", line_pos+pz+bkw, line_pos+pz+bkw+StrLen(txt$), clr)
		moai.Set(id$,"CursorPos",line_pos)
EndFunction 
This function does the opposite, remove specific colored text from a line

Code: Select all

Function p_te_uncolor_txt(id$,l_n,txt$)
		Local line_pos=moai.DoMethod(id$, "GetPosition", "0", ToString(l_n-1))
		Local line_len=moai.DoMethod(id$, "GetLineLength", ToString(l_n-1))
		Local t$ = moai.DoMethod(id$, "GetText", line_pos, line_pos+line_len)
		Local pz=FindStr(t$,txt$)
		If pz>=0 
			ntxt$=MidStr(t$,pz-10,StrLen(txt$)+20); my text including evenctual color escape chr
			If StartsWith(ntxt$,Chr(27).."P[") ;txt$ is colored
				t$=UnmidStr(t$, pz+StrLen(txt$), 10) ;remove after esc chr
				t$=UnmidStr(t$, pz-10, 10);remove before esc chr
				moai.Set(id$,"CursorPos",line_pos)
				moai.DoMethod(id$, "Mark", line_pos, line_pos+line_len) ;select the line
				moai.DoMethod(id$, "Cut") ;cut out the line
				moai.Set(id$,"CursorPos",line_pos)
				moai.DoMethod(id$, "Insert", t$, "Cursor"); replace the line without the txt$ uncolored.
			EndIf
		EndIf		
EndFunction 
This function will mark a specific line (note that I got problems in case of readonly text editor, therefore I used SetBold to show the selected line.

Code: Select all

Function p_te_markline(id$,l_n,flg)
		Local line_st=moai.DoMethod(id$, "GetPosition", "0", ToString(l_n))
		Local line_en=line_st+moai.DoMethod(id$, "GetLineLength", ToString(l_n))
		moai.DoMethod(id$, "SetBold", line_st, line_en, flg)
;		moai.DoMethod(id$, "Mark", line_st, line_en); use this if the texteditor is not readonly
		moai.Set(id$,"CursorPos",line_st)
EndFunction 
This function is quite simple but is useful anyway.

Code: Select all

Function p_te_getlinetext(id$,l_n)
		Local line_st=moai.DoMethod(id$, "GetPosition", "0", ToString(l_n))
		Local line_en=line_st+moai.DoMethod(id$, "GetLineLength", ToString(l_n))
		Return(moai.DoMethod(id$, "GetText",  line_st, line_en))
EndFunction
Post Reply