complete.c 2.58 KB
Newer Older
Miguel de Icaza's avatar
Miguel de Icaza committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * complete.c: Our auto completion engine.  This is an abstract class
 * that must be derived to implement its actual functionality.
 *
 * Author:
 *   Miguel de Icaza (miguel@gnu.org)
 *
 * Theory of operation:
 *
 *    Derived types of Complete provide the search function.
 *
 *    The search function should not take too long to run, and try to
 *    search on each step information on its data repository.  When the
 *    data repository information has been extenuated or if a match has
 *    been found, then the method should return FALSE and invoke the
 *    notification function that was provided to Complete.
17
 *
Miguel de Icaza's avatar
Miguel de Icaza committed
18
 *
19
 * (C) 2000-2001 Ximain Inc.
Miguel de Icaza's avatar
Miguel de Icaza committed
20
 */
21 22
#include <gnumeric-config.h>
#include "gnumeric.h"
Miguel de Icaza's avatar
Miguel de Icaza committed
23 24
#include "complete.h"

25 26
#include <gal/util/e-util.h>
#include <gtk/gtkmain.h>
27
#include <stdio.h>
28
#include <stdlib.h>
29

Miguel de Icaza's avatar
Miguel de Icaza committed
30
#define PARENT_TYPE (gtk_object_get_type ())
31
#define ACC(o) (COMPLETE_CLASS (G_OBJECT_GET_CLASS (o)))
Miguel de Icaza's avatar
Miguel de Icaza committed
32 33 34 35 36 37 38 39 40 41 42 43

static GtkObjectClass *parent_class;

void
complete_construct (Complete *complete,
		    CompleteMatchNotifyFn notify,
		    void *notify_closure)
{
	complete->notify = notify;
	complete->notify_closure = notify_closure;
}

44
static void
Miguel de Icaza's avatar
Miguel de Icaza committed
45 46 47
complete_destroy (GtkObject *object)
{
	Complete *complete = COMPLETE (object);
48

49
	if (complete->idle_tag) {
Miguel de Icaza's avatar
Miguel de Icaza committed
50 51 52 53
		gtk_idle_remove (complete->idle_tag);
		complete->idle_tag = 0;
	}

54
	if (complete->text) {
Miguel de Icaza's avatar
Miguel de Icaza committed
55
		g_free (complete->text);
56 57
		complete->text = 0;
	}
58

Miguel de Icaza's avatar
Miguel de Icaza committed
59
	if (parent_class->destroy)
60
		(parent_class->destroy) (object);
Miguel de Icaza's avatar
Miguel de Icaza committed
61 62 63 64 65 66 67 68 69 70
}

static gint
complete_idle (gpointer data)
{
	Complete *complete = data;

	if (complete->idle_tag == 0){
		abort ();
	}
71

72
	if (ACC(complete)->search_iteration (complete))
Miguel de Icaza's avatar
Miguel de Icaza committed
73 74 75
		return TRUE;

	complete->idle_tag = 0;
76

Miguel de Icaza's avatar
Miguel de Icaza committed
77 78 79 80 81 82 83 84 85
	return FALSE;
}

void
complete_start (Complete *complete, const char *text)
{
	g_return_if_fail (complete != NULL);
	g_return_if_fail (IS_COMPLETE (complete));
	g_return_if_fail (text != NULL);
86

87 88 89 90 91
	if (complete->text != text) {
		if (complete->text)
			g_free (complete->text);
		complete->text = g_strdup (text);
	}
92

93 94
	if (complete->idle_tag == 0)
		complete->idle_tag = gtk_idle_add (complete_idle, complete);
95 96 97

	if (ACC(complete)->start_over)
		ACC(complete)->start_over (complete);
Miguel de Icaza's avatar
Miguel de Icaza committed
98 99 100 101 102 103 104 105 106 107 108 109
}

static gboolean
default_search_iteration (Complete *complete)
{
	return FALSE;
}

static void
complete_class_init (GtkObjectClass *object_class)
{
	CompleteClass *complete_class = (CompleteClass *) object_class;
110

Miguel de Icaza's avatar
Miguel de Icaza committed
111 112 113 114 115 116
	object_class->destroy = complete_destroy;
	complete_class->search_iteration = default_search_iteration;

	parent_class = gtk_type_class (PARENT_TYPE);
}

117
E_MAKE_TYPE(complete, "Complete", Complete, &complete_class_init, NULL, PARENT_TYPE);