| 1 |
#include <config.h> |
| 2 |
|
| 3 |
#include "entry-completion.h" |
| 4 |
|
| 5 |
#include <libgupnp-av/gupnp-av.h> |
| 6 |
|
| 7 |
static void |
| 8 |
entry_completion_constructed (GObject *self); |
| 9 |
|
| 10 |
struct _EntryCompletion |
| 11 |
{ |
| 12 |
GtkEntryCompletion parent_instance; |
| 13 |
|
| 14 |
GtkListStore *store; |
| 15 |
}; |
| 16 |
|
| 17 |
|
| 18 |
G_DEFINE_TYPE (EntryCompletion, entry_completion, GTK_TYPE_ENTRY_COMPLETION) |
| 19 |
|
| 20 |
GtkEntryCompletion * |
| 21 |
entry_completion_new (void) |
| 22 |
{ |
| 23 |
return g_object_new (ENTRY_TYPE_COMPLETION, NULL); |
| 24 |
} |
| 25 |
|
| 26 |
static void |
| 27 |
entry_completion_finalize (GObject *object) |
| 28 |
{ |
| 29 |
G_OBJECT_CLASS (entry_completion_parent_class)->finalize (object); |
| 30 |
} |
| 31 |
|
| 32 |
static void |
| 33 |
entry_completion_get_property (GObject *object, |
| 34 |
guint prop_id, |
| 35 |
GValue *value, |
| 36 |
GParamSpec *pspec) |
| 37 |
{ |
| 38 |
switch (prop_id) |
| 39 |
{ |
| 40 |
default: |
| 41 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
static void |
| 46 |
entry_completion_set_property (GObject *object, |
| 47 |
guint prop_id, |
| 48 |
const GValue *value, |
| 49 |
GParamSpec *pspec) |
| 50 |
{ |
| 51 |
switch (prop_id) |
| 52 |
{ |
| 53 |
default: |
| 54 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
static gboolean |
| 59 |
entry_completion_on_match_selected (GtkEntryCompletion *self, |
| 60 |
GtkTreeModel *model, |
| 61 |
GtkTreeIter *iter) |
| 62 |
{ |
| 63 |
char *match = NULL; |
| 64 |
GtkEntry *entry; |
| 65 |
|
| 66 |
entry = GTK_ENTRY (gtk_entry_completion_get_entry (self)); |
| 67 |
gtk_tree_model_get (model, iter, 0, &match, -1); |
| 68 |
|
| 69 |
char *old_text = g_strdup (gtk_entry_get_text (entry)); |
| 70 |
char *needle = strrchr (old_text, ' '); |
| 71 |
if (needle != NULL) { |
| 72 |
needle++; |
| 73 |
*needle = '\0'; |
| 74 |
} |
| 75 |
|
| 76 |
char *new_text = g_strconcat (needle == NULL ? "" |
| 77 |
: old_text, |
| 78 |
match, |
| 79 |
NULL); |
| 80 |
gtk_entry_set_text (entry, new_text); |
| 81 |
gtk_editable_set_position (GTK_EDITABLE (entry), strlen (new_text)); |
| 82 |
g_free (new_text); |
| 83 |
g_free (old_text); |
| 84 |
|
| 85 |
return TRUE; |
| 86 |
} |
| 87 |
|
| 88 |
static void |
| 89 |
entry_completion_class_init (EntryCompletionClass *klass) |
| 90 |
{ |
| 91 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 92 |
|
| 93 |
object_class->constructed = entry_completion_constructed; |
| 94 |
object_class->finalize = entry_completion_finalize; |
| 95 |
object_class->get_property = entry_completion_get_property; |
| 96 |
object_class->set_property = entry_completion_set_property; |
| 97 |
|
| 98 |
GtkEntryCompletionClass *entry_class = GTK_ENTRY_COMPLETION_CLASS (klass); |
| 99 |
entry_class->match_selected = entry_completion_on_match_selected; |
| 100 |
} |
| 101 |
|
| 102 |
static gboolean |
| 103 |
entry_completion_on_match (GtkEntryCompletion *self, const char *key, GtkTreeIter *iter, gpointer user_data) |
| 104 |
{ |
| 105 |
GtkTreeModel *model = gtk_entry_completion_get_model (self); |
| 106 |
char *candidate; |
| 107 |
GtkWidget *entry; |
| 108 |
gboolean retval = FALSE; |
| 109 |
|
| 110 |
entry = gtk_entry_completion_get_entry (self); |
| 111 |
gtk_tree_model_get (model, iter, 0, &candidate, -1); |
| 112 |
|
| 113 |
const char *needle = strrchr (key, ' '); |
| 114 |
if (needle != NULL) { |
| 115 |
if ((needle - key)> gtk_editable_get_position (GTK_EDITABLE (entry))) { |
| 116 |
goto out; |
| 117 |
} |
| 118 |
needle++; |
| 119 |
} else { |
| 120 |
needle = key; |
| 121 |
} |
| 122 |
|
| 123 |
if (strlen(needle) == 0) { |
| 124 |
goto out; |
| 125 |
} |
| 126 |
|
| 127 |
retval = g_str_has_prefix (candidate, needle); |
| 128 |
|
| 129 |
out: |
| 130 |
g_free (candidate); |
| 131 |
|
| 132 |
return retval; |
| 133 |
} |
| 134 |
|
| 135 |
static void |
| 136 |
entry_completion_init (EntryCompletion *self) |
| 137 |
{ |
| 138 |
} |
| 139 |
|
| 140 |
static void |
| 141 |
entry_completion_constructed (GObject *object) |
| 142 |
{ |
| 143 |
EntryCompletion *self = ENTRY_COMPLETION (object); |
| 144 |
|
| 145 |
if (G_OBJECT_CLASS (entry_completion_parent_class)->constructed != NULL) { |
| 146 |
G_OBJECT_CLASS (entry_completion_parent_class)->constructed (G_OBJECT (self)); |
| 147 |
} |
| 148 |
|
| 149 |
gtk_entry_completion_set_match_func (GTK_ENTRY_COMPLETION (self), |
| 150 |
entry_completion_on_match, |
| 151 |
NULL, |
| 152 |
NULL); |
| 153 |
|
| 154 |
self->store = gtk_list_store_new (1, G_TYPE_STRING); |
| 155 |
|
| 156 |
gtk_entry_completion_set_model (GTK_ENTRY_COMPLETION (self), GTK_TREE_MODEL (self->store)); |
| 157 |
gtk_entry_completion_set_text_column (GTK_ENTRY_COMPLETION (self), 0); |
| 158 |
} |
| 159 |
|
| 160 |
static const char *keywords[] = { |
| 161 |
"and", |
| 162 |
"or", |
| 163 |
"contains", |
| 164 |
"doesNotContain", |
| 165 |
"derivedFrom", |
| 166 |
"exists", |
| 167 |
"true", |
| 168 |
"false", |
| 169 |
">", ">=", "<", "<=", "=", "!=", "*", |
| 170 |
NULL |
| 171 |
}; |
| 172 |
|
| 173 |
void |
| 174 |
entry_completion_set_search_criteria (EntryCompletion *self, char const * const * criteria) |
| 175 |
{ |
| 176 |
GtkTreeIter iter; |
| 177 |
gtk_list_store_clear (self->store); |
| 178 |
|
| 179 |
// Prefill ListStore with the search expression keywords |
| 180 |
char const * const *it = keywords; |
| 181 |
while (*it != NULL) { |
| 182 |
gtk_list_store_insert_with_values (self->store, |
| 183 |
&iter, |
| 184 |
-1, |
| 185 |
0, |
| 186 |
*it, |
| 187 |
-1); |
| 188 |
it++; |
| 189 |
} |
| 190 |
|
| 191 |
// Add the properties supported by the search criteria |
| 192 |
it = criteria; |
| 193 |
while (*it != NULL) { |
| 194 |
gtk_list_store_insert_with_values (self->store, |
| 195 |
&iter, |
| 196 |
-1, |
| 197 |
0, |
| 198 |
*it, |
| 199 |
-1); |
| 200 |
it++; |
| 201 |
} |
| 202 |
} |