Introspection non-fixes.
[gnumeric.git] / src / complete-sheet.c
blob9ac19e72586dfb4456bce3ec904621c60b202a9f
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * complete-sheet.c: Auto completes values from a sheet.
5 * Author:
6 * Miguel de Icaza (miguel@gnu.org)
8 * This is a pretty simple implementation, should be helpful
9 * to find performance hot-spots (if you bump SEARCH_STEPS to gnm_sheet_get_max_rows (sheet)/2)
11 * (C) 2000-2001 Ximian, Inc.
13 #include <gnumeric-config.h>
14 #include "gnumeric.h"
15 #include "complete-sheet.h"
16 #include "gnumeric-conf.h"
18 #include "sheet.h"
19 #include "cell.h"
20 #include "value.h"
21 #include "parse-util.h"
23 #include <gsf/gsf-impl-utils.h>
24 #include <string.h>
26 #define SEARCH_STEPS 50
28 #define PARENT_TYPE GNM_COMPLETE_TYPE
30 static GObjectClass *parent_class;
33 static void
34 search_strategy_reset_search (GnmCompleteSheet *cs)
36 cs->current.col = cs->entry.col;
37 cs->current.row = cs->entry.row;
38 cs->cell = NULL;
42 * Very simple search strategy: up until blank.
44 static gboolean
45 search_strategy_next (GnmCompleteSheet *cs)
47 cs->current.row--;
48 if (cs->current.row < 0)
49 return FALSE;
51 cs->cell = sheet_cell_get (cs->sheet, cs->current.col, cs->current.row);
52 return cs->cell != NULL;
56 static void
57 complete_sheet_finalize (GObject *object)
59 GnmCompleteSheet *cs = GNM_COMPLETE_SHEET (object);
60 g_free (cs->current_text);
61 parent_class->finalize (object);
64 static gboolean
65 text_matches (GnmCompleteSheet const *cs)
67 char const *text;
68 GnmComplete const *complete = &cs->parent;
70 if (cs->cell->value == NULL ||
71 !VALUE_IS_STRING (cs->cell->value) ||
72 gnm_cell_has_expr (cs->cell))
73 return FALSE;
75 text = value_peek_string (cs->cell->value);
76 if (strncmp (text, complete->text, strlen (complete->text)) != 0)
77 return FALSE;
79 (*complete->notify)(text, complete->notify_closure);
80 return TRUE;
83 static gboolean
84 complete_sheet_search_iteration (GnmComplete *complete)
86 GnmCompleteSheet *cs = GNM_COMPLETE_SHEET (complete);
87 int i;
89 if ((int)strlen (complete->text) <
90 gnm_conf_get_core_gui_editing_autocomplete_min_chars ())
91 return FALSE;
93 if (strncmp (cs->current_text, complete->text, strlen (cs->current_text)) != 0)
94 search_strategy_reset_search (cs);
96 for (i = 0; i < SEARCH_STEPS; i++) {
97 if (!search_strategy_next (cs))
98 return FALSE;
100 if (text_matches (cs))
101 return FALSE;
104 return TRUE;
107 static void
108 complete_sheet_class_init (GObjectClass *object_class)
110 GnmCompleteClass *auto_complete_class = (GnmCompleteClass *) object_class;
112 parent_class = g_type_class_peek (PARENT_TYPE);
113 object_class->finalize = complete_sheet_finalize;
114 auto_complete_class->search_iteration = complete_sheet_search_iteration;
118 * gnm_complete_sheet_new:
119 * @sheet: #Sheet
120 * @col: column
121 * @row: row
122 * @notify: (scope async): #GnmCompleteMatchNotifyFn
123 * @notify_closure: user data
125 * Returns: (transfer full): the new #GnmComplete.
127 GnmComplete *
128 gnm_complete_sheet_new (Sheet *sheet, int col, int row, GnmCompleteMatchNotifyFn notify, void *notify_closure)
131 * Somehow every time I pronounce this, I feel like something is not quite right.
133 GnmCompleteSheet *cs;
135 cs = g_object_new (GNM_COMPLETE_SHEET_TYPE, NULL);
136 gnm_complete_construct (GNM_COMPLETE (cs), notify, notify_closure);
138 cs->sheet = sheet;
139 cs->entry.col = col;
140 cs->entry.row = row;
141 cs->current_text = g_strdup ("");
142 search_strategy_reset_search (cs);
144 return GNM_COMPLETE (cs);
147 GSF_CLASS (GnmCompleteSheet, gnm_complete_sheet,
148 complete_sheet_class_init, NULL, PARENT_TYPE)