Skip to content

Instantly share code, notes, and snippets.

@robertsanseries
Last active October 16, 2017 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertsanseries/728e504f85c8a7724e52ebc06b4b7ef9 to your computer and use it in GitHub Desktop.
Save robertsanseries/728e504f85c8a7724e52ebc06b4b7ef9 to your computer and use it in GitHub Desktop.
valac --pkg gtk+-3.0 EntryNumber.vala
using Gtk;
using Gdk;
public static int main (string[] args) {
Gtk.init (ref args);
Application app = new Application ();
app.show_all ();
Gtk.main ();
return 0;
}
public class Application : Gtk.Window {
public Application () {
this.title = "My Gtk.Window";
this.window_position = Gtk.WindowPosition.CENTER;
this.set_default_size (350, 300);
this.hide_titlebar_when_maximized = false;
EntryNumber entry_number = new EntryNumber (Gtk.InputPurpose.DIGITS);
entry_number.include_icon_clean ();
entry_number.changed.connect (() => {
entry_number.input_digits();
});
this.add (entry_number);
}
}
public class EntryNumber : Gtk.Entry {
private string old = "";
public EntryNumber (Gtk.InputPurpose purpose) {
this.input_purpose = purpose;
}
public void input_digits () {
try {
this.text = this.text.replace (" ", "");
var regex = new Regex ("""^[0-9]{1,}$""");
var regex_match = regex.match (this.text);
if (regex_match) {
this.old = this.text;
} else if (this.text != "") {
this.text = this.old;
}
} catch (Error e) {
message (e.message);
}
}
public void include_icon_clean () {
this.set_icon_from_icon_name (Gtk.EntryIconPosition.SECONDARY, "edit-clear");
this.icon_press.connect ((pos, event) => {
if (pos == Gtk.EntryIconPosition.SECONDARY) {
this.set_text ("");
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment