我需要编写一个函数来验证用户在 gtk 输入字段中键入的设备名称。
设备必须采用以下形式:
"/dev/video + one digits" ( e.g. /dev/video1 )
为此,我创建了函数,但我现在不知道如何编写条件
void validate_device_cb (GtkEntry* entry, const gchar* text,
gint length, gint* position, gpointer data)
{
GtkEditable *editable = GTK_EDITABLE (entry);
int i, count = 0;
gchar *result = g_new (gchar, length);
for (i = 0; i < length; i++) {
/* insert condition here */
continue;
result[count++] = text[i];
}
if (count > 0) {
g_signal_handlers_block_by_func (G_OBJECT (editable),
G_CALLBACK (validate_device_cb), data);
gtk_editable_insert_text (editable, result, count, position);
g_signal_handlers_unblock_by_func (G_OBJECT (editable),
G_CALLBACK (validate_device_cb), data);
}
g_signal_stop_emission_by_name (G_OBJECT (editable), "insert_text");
g_free (result);
}
欢迎任何帮助
更新(更多解释)
gtk 条目必须只接受输入的这个字符:
first typed: '/'
second 'd'
third 'e'
fourth 'v'
fifth '/'
sixth 'v'
seventh 'i'
eight 'd'
.........
eleven digit
twelve digit
设置设备入口:
video_device = gtk_entry_new_with_max_length(12);
gtk_editable_set_editable(GTK_EDITABLE(video_device),TRUE);
g_signal_connect(G_OBJECT(video_device), "insert_text" ,
G_CALLBACK(validate_device_cb), NULL);