gtk-rs / gtk

DEPRECATED, use https://github.com/gtk-rs/gtk3-rs repository instead!
https://gtk-rs.org/
MIT License
1.25k stars 82 forks source link

Block/Unblock signal in entry -> connect_insert_text() #1014

Closed mezeipetister closed 4 years ago

mezeipetister commented 4 years ago

Hi Guys, Could you help me, how to block and unblock signal inside a connect_insert_text()? I would like to write a custom entry validator, that can access and even modify the entry inserted text. But when I use entry.insert_text(..) I got an event loop, and with overflow it exit. I did read in a gtk-py tutorial, that I need to block the signal before the insert_text(..), and unblock it after. But for block_signal(..) I need the SignalHandlerId, which is not available inside that closure.

Here is the code:

my_entry.connect_insert_text(|entry, text, _| {
            if text.parse::<u64>().is_ok() 
            {
                entry.block_signal(???);
                e.insert_text(&"1", position);
                entry.unblock_signal(???);
            }
            entry.stop_signal_emission("insert_text");
        });

So in the block_signal part, how can I know the signalHandlerId?

Thank you in advance!

Best wishes, Peter Mezei.

U.I.: GTK-RS is AWESOME!

sdroege commented 4 years ago

You need to store the return value of connect_insert_text() somehow and pass it into the closure. One way of doing that is to store it in a Rc<RefCell<Option<SignalHandlerId>>>. You would initialize that with None, pass a clone into the closure and after connecting fill in the value. Not very beautiful but the signal handler does not know its signal handler id by itself.