[C] [GTK] Creating a custom editable widget

Hi everybody, I’d like to create a custom entry widget that implement custom features.

  • The first feature, is that I want it to only accept digits. I know I can do that if I create a Widget class that use a classic GtkEntry field.

  • The second feature is a display feature, I want each digit entered to replace a line with each line representing a number, for example _ _ _ would be 5 _ _ than 5 5 _ and 5 5 5 or maybe combine both keeping the underscore when writing the number.

The issue is that, I don’t know how to create the second feature using a GtkEntry.
Thanks for the help !

You may connect to the changed signal, investigate the actual user input string, and set your desired string.

StackOverflow has a more complete answer, maybe it still works for GTK4:

gtk - GtkEntry change text on user input - Stack Overflow

Thank you for your answer, I’ve heard of this solution, but your link give me a concrete implementation.
I see how it can help me for the first feature, I can see, too, how it can help me for the second feature, but I’m not sure that it fits all my needs for the second feature.
Indeed, I don’t see how I can tell my widget to display simultaneously my digit and a “_” under it.

At least you can display an underscore as placeholder, like _ _ _ when the user has to type 3 digits. When you need the underscore under the actual digits, that may be possible with pango markup: Gtk.Entry:attributes

Yes, I’ll first make it work like that and I’ll see later if I find a way to do what I want.

Just in case anyone gets here, there is a blog that makes a tutorial of creating custom widget, in C with gtk 4
https://blog.gtk.org/2020/04/24/custom-widgets-in-gtk-4-drawing/
If it can help sombody one day !

This should be easy at the least in ruby-gtk3.

After a text-modification was done, I would then query the new
string, and do the code logic there, e. g. your requirement here:
“for example _ _ _ would be 5”

In ruby this would probably be as simple as: x.sub!(//, ‘5’) if x.include? '

Should be not too hard even in C either.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.