Move plugin init code
[gnumeric.git] / src / complete-sheet.c
blob6bd074d60bda32a55253bcbf1e81f4174a69e294
1 /*
2 * complete-sheet.c: Auto completes values from a sheet.
4 * Author:
5 * Miguel de Icaza (miguel@gnu.org)
7 * This is a pretty simple implementation, should be helpful
8 * to find performance hot-spots (if you bump SEARCH_STEPS to gnm_sheet_get_max_rows (sheet)/2)
10 * (C) 2000-2001 Ximian, Inc.
12 #include <gnumeric-config.h>
13 #include <gnumeric.h>
14 #include <complete-sheet.h>
15 #include <gnumeric-conf.h>
17 #include <sheet.h>
18 #include <cell.h>
19 #include <value.h>
20 #include <parse-util.h>
22 #include <gsf/gsf-impl-utils.h>
23 #include <string.h>
25 #define SEARCH_STEPS 50
27 #define PARENT_TYPE GNM_COMPLETE_TYPE
29 static GObjectClass *parent_class;
32 static void
33 search_strategy_reset_search (GnmCompleteSheet *cs)
35 cs->current.col = cs->entry.col;
36 cs->current.row = cs->entry.row;
37 cs->cell = NULL;
41 * Very simple search strategy: up until blank.
43 static gboolean
44 search_strategy_next (GnmCompleteSheet *cs)
46 cs->current.row--;
47 if (cs->current.row < 0)
48 return FALSE;
50 cs->cell = sheet_cell_get (cs->sheet, cs->current.col, cs->current.row);
51 return cs->cell != NULL;
55 static void
56 complete_sheet_finalize (GObject *object)
58 GnmCompleteSheet *cs = GNM_COMPLETE_SHEET (object);
59 g_free (cs->current_text);
60 parent_class->finalize (object);
63 static gboolean
64 text_matches (GnmCompleteSheet const *cs)
66 char const *text;
67 GnmComplete const *complete = &cs->parent;
69 if (cs->cell->value == NULL ||
70 !VALUE_IS_STRING (cs->cell->value) ||
71 gnm_cell_has_expr (cs->cell))
72 return FALSE;
74 text = value_peek_string (cs->cell->value);
75 if (strncmp (text, complete->text, strlen (complete->text)) != 0)
76 return FALSE;
78 (*complete->notify)(text, complete->notify_closure);
79 return TRUE;
82 static gboolean
83 complete_sheet_search_iteration (GnmComplete *complete)
85 GnmCompleteSheet *cs = GNM_COMPLETE_SHEET (complete);
86 int i;
88 if ((int)strlen (complete->text) <
89 gnm_conf_get_core_gui_editing_autocomplete_min_chars ())
90 return FALSE;
92 if (strncmp (cs->current_text, complete->text, strlen (cs->current_text)) != 0)
93 search_strategy_reset_search (cs);
95 for (i = 0; i < SEARCH_STEPS; i++) {
96 if (!search_strategy_next (cs))
97 return FALSE;
99 if (text_matches (cs))
100 return FALSE;
103 return TRUE;
106 static void
107 complete_sheet_class_init (GObjectClass *object_class)
109 GnmCompleteClass *auto_complete_class = (GnmCompleteClass *) object_class;
111 parent_class = g_type_class_peek (PARENT_TYPE);
112 object_class->finalize = complete_sheet_finalize;
113 auto_complete_class->search_iteration = complete_sheet_search_iteration;
117 * gnm_complete_sheet_new:
118 * @sheet: #Sheet
119 * @col: column
120 * @row: row
121 * @notify: (scope async): #GnmCompleteMatchNotifyFn
122 * @notify_closure: user data
124 * Returns: (transfer full): the new #GnmComplete.
126 GnmComplete *
127 gnm_complete_sheet_new (Sheet *sheet, int col, int row, GnmCompleteMatchNotifyFn notify, void *notify_closure)
130 * Somehow every time I pronounce this, I feel like something is not quite right.
132 GnmCompleteSheet *cs;
134 cs = g_object_new (GNM_COMPLETE_SHEET_TYPE, NULL);
135 gnm_complete_construct (GNM_COMPLETE (cs), notify, notify_closure);
137 cs->sheet = sheet;
138 cs->entry.col = col;
139 cs->entry.row = row;
140 cs->current_text = g_strdup ("");
141 search_strategy_reset_search (cs);
143 return GNM_COMPLETE (cs);
146 GSF_CLASS (GnmCompleteSheet, gnm_complete_sheet,
147 complete_sheet_class_init, NULL, PARENT_TYPE)