| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <string.h> |
| 5 |
|
| 6 |
#include "widget-text-entry.h" |
| 7 |
|
| 8 |
struct text_entry { |
| 9 |
struct alsa_elem *elem; |
| 10 |
GtkWidget *entry; |
| 11 |
}; |
| 12 |
|
| 13 |
|
| 14 |
static void text_entry_changed(GtkEditable *editable, struct text_entry *data) { |
| 15 |
const char *text = gtk_editable_get_text(editable); |
| 16 |
size_t len = strlen(text); |
| 17 |
|
| 18 |
|
| 19 |
alsa_set_elem_bytes(data->elem, text, len); |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
static void text_entry_updated( |
| 24 |
struct alsa_elem *elem, |
| 25 |
void *private |
| 26 |
) { |
| 27 |
struct text_entry *data = private; |
| 28 |
|
| 29 |
int is_writable = alsa_get_elem_writable(elem); |
| 30 |
gtk_widget_set_sensitive(data->entry, is_writable); |
| 31 |
|
| 32 |
size_t size; |
| 33 |
const void *bytes = alsa_get_elem_bytes(elem, &size); |
| 34 |
|
| 35 |
|
| 36 |
const char *current_text = gtk_editable_get_text(GTK_EDITABLE(data->entry)); |
| 37 |
|
| 38 |
|
| 39 |
if (bytes && size > 0) { |
| 40 |
|
| 41 |
size_t str_len = strnlen((const char *)bytes, size); |
| 42 |
|
| 43 |
|
| 44 |
gboolean is_valid = g_utf8_validate((const char *)bytes, str_len, NULL); |
| 45 |
if (str_len > 0 && is_valid) { |
| 46 |
|
| 47 |
if (strlen(current_text) != str_len || |
| 48 |
memcmp(current_text, bytes, str_len) != 0) { |
| 49 |
char *str = g_strndup((const char *)bytes, str_len); |
| 50 |
gtk_editable_set_text(GTK_EDITABLE(data->entry), str); |
| 51 |
g_free(str); |
| 52 |
} |
| 53 |
} else { |
| 54 |
if (*current_text != '\0') |
| 55 |
gtk_editable_set_text(GTK_EDITABLE(data->entry), ""); |
| 56 |
} |
| 57 |
} else { |
| 58 |
if (*current_text != '\0') |
| 59 |
gtk_editable_set_text(GTK_EDITABLE(data->entry), ""); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
static void on_destroy(struct text_entry *data) { |
| 65 |
g_free(data); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
GtkWidget *make_text_entry_alsa_elem(struct alsa_elem *elem) { |
| 70 |
struct text_entry *data = g_malloc0(sizeof(struct text_entry)); |
| 71 |
data->elem = elem; |
| 72 |
data->entry = gtk_entry_new(); |
| 73 |
gtk_widget_set_size_request(data->entry, 120, -1); |
| 74 |
|
| 75 |
|
| 76 |
g_signal_connect( |
| 77 |
data->entry, "changed", G_CALLBACK(text_entry_changed), data |
| 78 |
); |
| 79 |
|
| 80 |
|
| 81 |
alsa_elem_add_callback(elem, text_entry_updated, data, NULL); |
| 82 |
|
| 83 |
|
| 84 |
text_entry_updated(elem, data); |
| 85 |
|
| 86 |
|
| 87 |
g_object_weak_ref(G_OBJECT(data->entry), (GWeakNotify)on_destroy, data); |
| 88 |
|
| 89 |
return data->entry; |
| 90 |
} |