Themabewertung:
  • 0 Bewertung(en) - 0 im Durchschnitt
  • 1
  • 2
  • 3
  • 4
  • 5
Syntax Highlightning in tuiTextBox
#1
Syntax Highlightning (or any other formatting of the text) in NTUI is called "styling".
This is how it works for now (Function names will change slightly in final NTUI), since don't want to expose newtypes.

[ab3]myStyleCallback:
Function.l myStyleCallback{*textBox.tuiTextBox,lpos.l,*tline.tline}

*tline\sclength = 0 ; reset the style information
cpos.l = 0 ; start at character position 0

While cpos<*tline\clength ; go through the whole line
c.b = Peek.b(*tline\text+cpos) : cpos+1 ; grab a character

Select c
Case @"a" ; if we see an "a"...

; make it bold and red
_tb_SetStyleCommand{*tline,cpos-1,#TUITBSC_BOLD,1}
_tb_SetStyleCommand{*tline,cpos-1,#TUITBSC_FGPEN,#TUITBPEN_RED}

; and return to normal
_tb_SetStyleCommand{*tline,cpos,#TUITBSC_BOLD,0}
_tb_SetStyleCommand{*tline,cpos,#TUITBSC_FGPEN,#TUITBPEN_TEXT}

Default
; nothing happens for other characters

End Select

Wend

*tline\flags|#TUITBLF_STYLED ; mark the line as styled
Function Return #TUISCB_DONE ; tell NTUI we are done

End Function
; this creates a global variable "*myStyleCallback" that points to our functions
; and tells AmiBlitz3 not to remove the function, since it is never called directly
!_MakeGlobalFuncPointer{myStyleCallback,{Null,0,Null}}

...
ntui_SetAttr{*textBox,#TUITBA_STYLECB,*myStyleCallback} ; attach the callback function to the textbox
...
ntui_SetAttr{*textBox,#TUITBA_STYLECB,Null} ; remove the callback function from the textbox[/ab3]

There are a few things to mention:
1. The example makes all "a"'s in the text bold and red.

2. You don't have to remove the callback when you free the textBox. This is only necessary if you want to make sure it will never called again, e.g. if your style callback allocated resources that you want to free before NTUI.

3. Between the label "myStyleCallback:" and the function declaration MUST NOT be any code.

4. It is allowed to write to the text buffer (Poke), but the length MUST be preserved. E.g. Casing can be altered. Note that you are actually changing the text buffer. If you just want to just format for display, you need "Style Commands" for this.

5. There are many "Style Commands", not only bold and colors. See TUITBSC_... for more. New commands can be easily added if necessary.

6. To style the text, you don't need a StyleCallback. You can also go through the whole text and style it. However, the style callback has so many advantages that you really want to use it. E.g., you don't need to parse the whole text, only the visible lines (=much faster). It also keeps track while editing the text line.

7. If you parse a line and the result may affect the next or previous line (e.g. multi-line comment), you need to return #TUISCB_NEXT or #TUISCB_PREV instead of #TUISCB_DONE. You can store the parser state as flags in *tline\flags. There are currently two flags, #TUITBLF_INCOMMENT and #TUITBLF_INTAG.

8. If the highlightning is more complex that just tracking a character, I highly recommend using a stack-based parser rather than a state machine.
99% of all programming languages can be parsed by a stack based parser with lookahead of 1 or 2 characters. I can give an example if there is interest.
Zitieren
#2
If i understood alright, this Callback is only for 1 Char ?

will i highlight a command like engine or id or title and other simple commands i must use a stack parser alright ?

Zitat:I can give an example if there is interest.

yes please...

and another question.
This type of Callback will also work with a Listview ?
Zitieren
#3
Blackbird schrieb:If i understood alright, this Callback is only for 1 Char ?
No, it is per line. See the "While" loop...

Zitat:will i highlight a command like engine or id or title and other simple commands i must use a stack parser alright ?
The "StyleCommands" are just short "instructions" to tell the text renderer how to change the colors and fonts. You can add them to a line by giving the line and character positions using _tb_SetStyleCommand.

Zitat:
Zitat:I can give an example if there is interest.

yes please...
Let me figure something out.

Zitat:and another question.
This type of Callback will also work with a Listview ?
Unfortunately no. But I plan to use the same mechanism of text formatting for all kinds of text output.
Just interface wise this is not so easy.
Zitieren
#4
@Blackbird

What kind of highlightning you are looking for?
Zitieren
#5
Thanks Wanderer.

For a Listview ?
Oh, i looking for 1 Char highlightning
An Example for the Output of my Copacabanaprogram:

I will get an output for the statuscommand from svn like

C 965 wc/bar.c
A 965 wc/qax.c

C is representet a Conflict and A is for Add.

I will highlight the Chars for better usual by a commit.
C should be red A should be blue ect....
Zitieren
#6
i found out that the Flag #TUITBPEN_RED doesn't exist in the svn-version i have.
will use another one for testing......token is my favorit Big Grin

;/* ntui TextBox Pens */
#TUITBPEN_BG = 0 ; pen for background
#TUITBPEN_TEXT = 1 ; pen for normal text
#TUITBPEN_MARKER = 2
#TUITBPEN_TOKEN = 3 ; pen for Tokens
#TUITBPEN_NUMBER = 4 ; pen for numeric values
#TUITBPEN_TYPE = 5 ; pen for structs
#TUITBPEN_CONSTANT = 6 ; pen for constants/macros
#TUITBPEN_COMMENT = 7 ; pen for comments
#TUITBPEN_MACRO = 8 ; pen for macros
#TUITBPEN_MEMBER = 9 ; pen for macros
#TUITBPEN_STRING = 10 ; pen for string constants
#TUITBPEN_FUNCTION = 11 ; pen for functions
#TUITBPEN_DIRECTIVE = 12 ; pen for directives
#TUITBPEN_LABEL = 13 ; pen for labels
#TUITBPEN_TAG = 14 ; pen for tag
#TUITBPEN_ATTR = 15 ; pen for tag attribute
#TUITBPEN_BGFOCUS = 16 ; pen for background with focus
#TUITBPEN_BGNOFOCUS = 17 ; pen for background without focus
#TUITBPEN_BGDISABLED = 18 ; pen for disabled background
#TUITBPEN_HINTTEXT = 19
#TUITBPEN_BG2 = 20
#TUITBPEN_MAX = 21
Zitieren
#7
Right, TUITBPEN's are semantic. There is not RED.

About the ListView: You can make multiple columns and set some formatting for individual cells, you might event want to replace A, C etc. with images like a warning sign or OK checkmark.

;/ Label formating rules: /
; | seperator of columns
; \x escape code: /
; l left align
; r right align
; b bold
; i italic
; s small
; u underlined
; f fixed font width
; d disabled
; h highlighted
; ~ gone
; | insert | sign
; \ inster \ sign
; t continue with text
; p picture name follows...must be ended with | or \


E.g. instead of "C Work:Source/file.ab2" you set the listview item to "\paltert \bWork:Source/file.ab2". This adds the AISS image "alert" and makes the text bold.
Zitieren
#8
hmmm, not that kind of what iam searching for...
I became the Output via Listener....
As i see that right, i must reread the Listview again and sort out the Items and replace them ?
Zitieren
#9
Oh, just realized that you cannot easily parse the data yet.
It will work like this, but TUIA_DISPATCHEVENTCB is still missing:
[ab3]myDispatchEvent:
Function.l myDispatchEvent{*obj.tuiObject,*ev.tuiEvent}

*listView.tuiListView = (.tuiListView)*obj ; make sure this cast is legal
ntui_GetEventAttr{*ev,#TUIEVA_ID,&id.l} ; get the id of the event

Select id
Case #TUILVEV_APPEND ; its an APPEND event sent from the FileListener
ntui_GetEventAttr{*ev,#TUIEVA_STRING,&*strP.b} ; get the string
If *strP Then string.s = Peek.s(*strP) ; copy it to a Blitz Stromg
; ... manipulate string.s
ntui_AddListItem{*listView,-1,string,0,Null,#TUI_NOFLAGS} ; and append it to the ListView
Function Return True ; tell NTUI we are done with this event, no further processing

End Select

Function Return _ntui_DispatchListViewEvent{*listView,*ev} ; all other events go to the regular dispatcher for ListView
End Function
!_MakeGlobalFuncPointer{myDispatchEvent,{Null,Null}}

...
ntui_SetAttr{*listView,#TUIA_DISPATCHEVENTCB,*myDispatchEvent} ; attach the callback function to the listview
...[/ab3]


I know that you are calling a private function at this point. This is why I want to make some more changes to the OOP model of NTUI, that should allow subclassing in a better and more flexible way. So this might change slightly in the future.
Zitieren
#10
Ok, no problem at all,
In the meanwhile i can use the Listener to fill a normal textfile and can parse the entrys then to the Listview ?

Thats all i can do at the Moment or ?
I hope i can fill the Listview async ?
Zitieren


Gehe zu:


Benutzer, die gerade dieses Thema anschauen: 3 Gast/Gäste