Add --only-numerical

This commit is contained in:
TheBrokenRail 2022-04-25 16:55:41 -04:00
parent cdcc55e55f
commit 3dbcdbb34a
3 changed files with 46 additions and 0 deletions

View File

@ -21,6 +21,8 @@
* Authors: Glynn Foster <glynn.foster@sun.com>
*/
#include <ctype.h>
#include "util.h"
#include "zenity.h"
@ -45,6 +47,22 @@ zenity_entry_combo_activate_default (GtkEntry *entry, gpointer window) {
gtk_window_activate_default (GTK_WINDOW (window));
}
static void
zenity_entry_insert_text_event (GtkEditable *editable, const gchar *text,
gint length, gint *position, gpointer data) {
int i = 0;
if (gtk_editable_get_position (editable) == 0 && text[0] == '-' &&
gtk_entry_get_text (GTK_ENTRY (editable))[0] != '-') {
i++;
}
for (; i < length; i++) {
if (!isdigit (text[i])) {
g_signal_stop_emission_by_name (G_OBJECT (editable), "insert-text");
return;
}
}
}
void
zenity_entry (ZenityData *data, ZenityEntryData *entry_data) {
GtkBuilder *builder = NULL;
@ -133,6 +151,12 @@ zenity_entry (ZenityData *data, ZenityEntryData *entry_data) {
"activate",
G_CALLBACK (zenity_entry_combo_activate_default),
GTK_WINDOW (dialog));
if (entry_data->only_numerical)
g_signal_connect (G_OBJECT (gtk_bin_get_child (GTK_BIN (entry))),
"insert-text",
G_CALLBACK (zenity_entry_insert_text_event),
NULL);
} else {
entry = gtk_entry_new ();
@ -143,6 +167,12 @@ zenity_entry (ZenityData *data, ZenityEntryData *entry_data) {
if (entry_data->hide_text)
g_object_set (G_OBJECT (entry), "visibility", FALSE, NULL);
if (entry_data->only_numerical)
g_signal_connect (G_OBJECT (entry),
"insert-text",
G_CALLBACK (zenity_entry_insert_text_event),
NULL);
}
gtk_widget_show (entry);

View File

@ -48,6 +48,7 @@ static gboolean zenity_general_modal;
static gboolean zenity_entry_active;
static gchar *zenity_entry_entry_text;
static gboolean zenity_entry_hide_text;
static gboolean zenity_entry_only_numerical;
/* List Dialog Options */
static gboolean zenity_list_active;
@ -147,6 +148,13 @@ static GOptionEntry entry_options[] = {{"entry",
&zenity_entry_hide_text,
"Hide the entry text",
NULL},
{"only-numerical",
'\0',
0,
G_OPTION_ARG_NONE,
&zenity_entry_only_numerical,
"Only allow numerical input",
NULL},
{NULL}};
static GOptionEntry list_options[] = {{"list",
@ -347,6 +355,7 @@ zenity_entry_pre_callback (GOptionContext *context, GOptionGroup *group,
zenity_entry_active = FALSE;
zenity_entry_entry_text = NULL;
zenity_entry_hide_text = FALSE;
zenity_entry_only_numerical = FALSE;
return TRUE;
}
@ -394,6 +403,7 @@ zenity_entry_post_callback (GOptionContext *context, GOptionGroup *group,
results->entry_data->dialog_text = zenity_general_dialog_text;
results->entry_data->entry_text = zenity_entry_entry_text;
results->entry_data->hide_text = zenity_entry_hide_text;
results->entry_data->only_numerical = zenity_entry_only_numerical;
} else {
if (zenity_entry_entry_text)
zenity_option_error (zenity_option_get_name (
@ -404,6 +414,11 @@ zenity_entry_post_callback (GOptionContext *context, GOptionGroup *group,
zenity_option_error (
zenity_option_get_name (entry_options, &zenity_entry_hide_text),
ERROR_SUPPORT);
if (zenity_entry_only_numerical)
zenity_option_error (zenity_option_get_name (entry_options,
&zenity_entry_only_numerical),
ERROR_SUPPORT);
}
return TRUE;

View File

@ -31,6 +31,7 @@ typedef struct {
gchar *dialog_text;
gchar *entry_text;
gboolean hide_text;
gboolean only_numerical;
const gchar **data;
} ZenityEntryData;