44 static void quickfill_insert_recursive (
QuickFill *qf,
const char *text,
45 int depth, QuickFillSort sort);
47 static void gnc_quickfill_remove_recursive (
QuickFill *qf,
const gchar *text,
48 gint depth, QuickFillSort sort);
57 gnc_quickfill_new (
void)
61 if (
sizeof (guint) <
sizeof (gunichar))
63 PWARN (
"Can't use quickfill");
72 qf->matches = g_hash_table_new (g_direct_hash, g_direct_equal);
81 destroy_helper (gpointer key, gpointer value, gpointer data)
83 gnc_quickfill_destroy (value);
93 g_hash_table_foreach (qf->matches, (GHFunc)destroy_helper, NULL);
94 g_hash_table_destroy (qf->matches);
98 CACHE_REMOVE(qf->text);
111 g_hash_table_foreach_remove (qf->matches, destroy_helper, NULL);
114 CACHE_REMOVE (qf->text);
137 guint key = g_unichar_toupper (uc);
139 if (NULL == qf)
return NULL;
141 DEBUG (
"xaccGetQuickFill(): index = %u\n", key);
143 return g_hash_table_lookup (qf->matches, GUINT_TO_POINTER (key));
151 const char *str,
int len)
156 if (NULL == qf)
return NULL;
157 if (NULL == str)
return NULL;
160 while (*c && (len > 0))
165 uc = g_utf8_get_char (c);
168 c = g_utf8_next_char (c);
181 if (NULL == qf)
return NULL;
182 if (NULL == str)
return NULL;
191 unique_len_helper (gpointer key, gpointer value, gpointer data)
211 count = g_hash_table_size (qf->matches);
216 g_hash_table_foreach (qf->matches, unique_len_helper, &qf);
231 gchar *normalized_str;
233 if (NULL == qf)
return;
234 if (NULL == text)
return;
237 normalized_str = g_utf8_normalize (text, -1, G_NORMALIZE_NFC);
238 quickfill_insert_recursive (qf, normalized_str, 0, sort);
239 g_free (normalized_str);
246 quickfill_insert_recursive (
QuickFill *qf,
const char *text,
int depth,
254 gunichar key_char_uc;
259 if ((text == NULL) || (g_utf8_strlen (text, -1) <= depth))
262 key_char = g_utf8_offset_to_pointer (text, depth);
264 key_char_uc = g_utf8_get_char (key_char);
265 key = g_unichar_toupper (key_char_uc);
267 match_qf = g_hash_table_lookup (qf->matches, GUINT_TO_POINTER (key));
268 if (match_qf == NULL)
270 match_qf = gnc_quickfill_new ();
271 g_hash_table_insert (qf->matches, GUINT_TO_POINTER (key), match_qf);
274 old_text = match_qf->text;
278 case QUICKFILL_ALPHA:
279 if (old_text && (g_utf8_collate (text, old_text) >= 0))
285 len = g_utf8_strlen (text, -1);
288 if (old_text == NULL)
290 match_qf->text = CACHE_INSERT((gpointer) text);
296 if ((len > match_qf->len) &&
297 (strncmp(text, old_text, strlen(old_text)) == 0))
300 CACHE_REMOVE(old_text);
301 match_qf->text = CACHE_INSERT((gpointer) text);
306 quickfill_insert_recursive (match_qf, text, ++depth, sort);
313 gnc_quickfill_remove (
QuickFill *qf,
const gchar *text, QuickFillSort sort)
315 gchar *normalized_str;
317 if (qf == NULL)
return;
318 if (text == NULL)
return;
320 normalized_str = g_utf8_normalize (text, -1, G_NORMALIZE_NFC);
321 gnc_quickfill_remove_recursive (qf, normalized_str, 0, sort);
322 g_free (normalized_str);
335 best_text_helper (gpointer key, gpointer value, gpointer user_data)
340 if (best->text == NULL)
343 best->text = qf->text;
346 else if (best->text == QUICKFILL_LIFO)
352 else if (g_utf8_collate (qf->text, best->text) < 0)
355 best->text = qf->text;
362 gnc_quickfill_remove_recursive (
QuickFill *qf,
const gchar *text, gint depth,
372 if (depth < g_utf8_strlen (text, -1))
377 gunichar key_char_uc;
380 key_char = g_utf8_offset_to_pointer (text, depth);
381 key_char_uc = g_utf8_get_char (key_char);
382 key = g_unichar_toupper (key_char_uc);
384 match_qf = g_hash_table_lookup (qf->matches, GUINT_TO_POINTER (key));
388 gnc_quickfill_remove_recursive (match_qf, text, depth + 1, sort);
390 if (match_qf->text == NULL)
393 g_hash_table_remove (qf->matches, GUINT_TO_POINTER (key));
394 gnc_quickfill_destroy (match_qf);
400 child_text = match_qf->text;
401 child_len = match_qf->len;
406 if (qf->text == NULL)
409 if (strcmp (text, qf->text) == 0)
413 gchar *best_text = NULL;
416 if (child_text != NULL)
419 best_text = child_text;
420 best_len = child_len;
425 if (g_hash_table_size (qf->matches) != 0)
432 g_hash_table_foreach (qf->matches, (GHFunc) best_text_helper, &bts);
433 best_text = bts.text;
434 best_len = (best_text == NULL) ? 0 : g_utf8_strlen (best_text, -1);
439 CACHE_REMOVE(qf->text);
440 if (best_text != NULL)
442 qf->text = CACHE_INSERT((gpointer) best_text);
void gnc_quickfill_insert(QuickFill *qf, const char *text, QuickFillSort sort)
QuickFill * gnc_quickfill_get_char_match(QuickFill *qf, gunichar uc)
utility functions for the GnuCash UI
#define DEBUG(format, args...)
#define PWARN(format, args...)
QuickFill * gnc_quickfill_get_string_len_match(QuickFill *qf, const char *str, int len)
QuickFill * gnc_quickfill_get_string_match(QuickFill *qf, const char *str)
All type declarations for the whole Gnucash engine.
const char * gnc_quickfill_string(QuickFill *qf)
QuickFill is used to auto-complete typed user entries.
QuickFill * gnc_quickfill_get_unique_len_match(QuickFill *qf, int *length)
const gchar * QofLogModule