Home   Archive   Permalink



field face to update text face

field face to update text face
    
Trying to capture keys at the field level.
    
This works only for the first few characters.
    
Something obvious I am missing?
    
Thanks
    
R ebol[]
    
view layout [
    
    tface: text 'Test'
    
    entry: field teal 200 rate 100 feel [
    
        detect: func [face action event] [
    
        tface/text: face/text
        show tface
         ]
        
        [ engage: func [face action event] [
    event
        ]
            ]
     ]
]

posted by:   OneArb     2-Jan-2017/11:14:53-8:00



Could you please be more specific on what you are trying to achive? It is hard to understand by looking at the code.
    
Just some notes, you should use double quotes instead of single quotes for strings.
    
"rate 100" is a very high rate, why do you need that?
    
You have a engage function but it does nothing.
    
You are also overwriting the "field"s default feel object hence it doesn't behave like a field, so you cannot type in it anymore.
    
Here is a working example, when you type a text and press enter text will be shown in text:
    
     view layout [
         tface: text 100 "Test"
         entry: field 100 teal 200 [
             tface/text: copy face/text
             show tface
         ]
     ]


posted by:   Endo     2-Jan-2017/15:23:27-8:00



Also note that in Endo's response, he added 100 (width) to the TEXT object. Without that, the TEXT object is calculated to be as wide as it's original text--this is why you only see the first few characters. You can see this by adding a little colour:
    
     view layout [
         text "Test" black white
         text 200 "Test" black white
     ]
    
I'd wrap the original text ENGAGE function (which is contained within CTX-TEXT) and add an additional keypress handler:
    
     view layout [
         tface: text 200 "Test" black white
         entry: field 200 feel make ctx-text/edit [
             do-engage: :engage
             engage: func [face action event][
                 do-engage face action event
                 if event/type = 'key [
                     tface/text: entry/text
                     show tface
                 ]
                 event
             ]
         ]
     ]

posted by:   Chris     2-Jan-2017/17:10:50-8:00



Of course, you can shorten:
    
     tface/text: entry/text
     show tface
    
To the more Rebolish:
    
     set-face tface get-face entry
    
Incidentally, good overview of event handling here:
http://www.rebol.com/docs/view-face-events.html
    
(and 'it's' in my first answer should be 'its', d'oh!)

posted by:   Chris     2-Jan-2017/17:44:53-8:00



@Endo
    
Capturing keys, meaning capturing keystroke one at a time.
    
I'd like text face to be updated each time a key is pressed in field face.
    
My purpose is to do color and string transforms on the target text face.
    
There will be different color and background for txt face within a single line.
    
I was planning to create several txt face on the fly, one for each txt color and background to compose the entire line. I'll have to calculate the length of the txt to generate the adequate size.
    
'Text' was changed from "Text" when I copy pasted here.
    
I tried rate 100 because the text face is not updating fast enough.
    
I used engage: in an attempt to capture the time events rate I hypothesized would increase generate.
    
So you are saying field already has a feel object? Unless I add feel detect does nothing.
    
I am trying to figure the meaning of
    
     detect:
    
Am I setting a native object to a function?

posted by:   OneArb     2-Jan-2017/22:48:16-8:00



@Chris    
    
Your code does exact what I want and it runs fast!
    
http://www.rebol.com/docs/view-face-events.html
is the exact documentation I read that led to my code attempt.
    
When I changed the code from text to field I was surprised that it failed.
    
I could I have tried using a global insert-event-func instead.
    
The code
    
    do-engage: :engage
             engage: func [face action event][
                 do-engage face action event
    
is total foreign to me.
    


posted by:   OneArb     2-Jan-2017/23:23:41-8:00



"So you are saying field already has a feel object?" Yes some faces like field, area, button have feel object and engage function accordingly.
    
So when you put your own feel object with a new engage function you are overwriting the default one which prevents its default behavior.
    
And actually it is what Chris does in his answer, he create his feel object using the default feel for edit (feel make ctx-text/edit) and then gets the default engage function (do-engage: :engage), then calls the original function to keep the default behavior (do-engage face action event) and last puts his own stuff.
    
You can see the default engage function inside the field you've created:
    
     view layout [
         tface: text 200 "Test" black white
         entry: field 200
         do [
             probe get in entry/feel 'engage
         ]
     ]
    


posted by:   Endo     3-Jan-2017/2:25:45-8:00