File: py-command-line.c

package info (click to toggle)
gnumeric 1.10.8-1squeeze5
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 90,968 kB
  • ctags: 23,303
  • sloc: ansic: 248,235; xml: 51,894; sh: 10,491; makefile: 2,822; perl: 2,466; yacc: 1,272; python: 205
file content (172 lines) | stat: -rw-r--r-- 4,371 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * py-command-line.c: Simple wrapper around GtkEntry with history support
 *
 * Author: Zbigniew Chyla (cyba@gnome.pl)
 */

#include <gnumeric-config.h>
#include "py-command-line.h"
#include "gnm-python.h"
#include <gnumeric.h>
#include <gutils.h>
#include <goffice/goffice.h>
#include <goffice/app/module-plugin-defs.h>
#include <gsf/gsf-impl-utils.h>
#include <glib-object.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <string.h>

#define MAX_HISTORY_SIZE  100

struct _GnmPyCommandLine {
	GtkEntry parent;

	GList *history, *history_tail, *history_cur;
	gboolean editing;
	int history_size;
};

typedef struct {
	GtkEntryClass parent_class;

	void (*entered) (GnmPyCommandLine *cline);
} GnmPyCommandLineClass;

enum {
	ENTERED_SIGNAL,
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };
static GObjectClass *parent_class = NULL;

static void
gnm_py_command_line_changed (GnmPyCommandLine *cline)
{
	cline->editing = TRUE;
}

static gint
gnm_py_command_line_keypress (GnmPyCommandLine *cline, GdkEventKey *event, gpointer user_data)
{
	switch (event->keyval) {
	case GDK_Return: {
		const char *text;

		text = gtk_entry_get_text (GTK_ENTRY (cline));
		if (cline->history_tail == NULL) {
			cline->history = g_list_append (NULL, g_strdup (text));
			cline->history_tail = cline->history;
		} else if (text[0] != '\0' && strcmp (text, cline->history_tail->data) != 0) {
			cline->history_tail =
				g_list_append (cline->history_tail,
					       g_strdup (text))->next;
		}
		if (cline->history_size == MAX_HISTORY_SIZE) {
			g_free (cline->history->data);
			cline->history = g_list_delete_link (cline->history, cline->history);
		} else {
			cline->history_size++;
		}
		g_signal_emit (cline, signals[ENTERED_SIGNAL], 0);
		gtk_entry_set_text (GTK_ENTRY (cline), "");
		cline->editing = TRUE;
		break;
	}
	case GDK_Up:
		if (cline->editing) {
			if (cline->history_tail != NULL) {
				cline->history_cur = cline->history_tail;
				gtk_entry_set_text (GTK_ENTRY (cline), cline->history_cur->data);
				gtk_editable_set_position (
					GTK_EDITABLE (cline), strlen (cline->history_cur->data));
				cline->editing = FALSE;
			}
		} else {
			if (cline->history_cur->prev != NULL) {
				cline->history_cur = cline->history_cur->prev;
				gtk_entry_set_text (GTK_ENTRY (cline), cline->history_cur->data);
				gtk_editable_set_position (
					GTK_EDITABLE (cline), strlen (cline->history_cur->data));
				cline->editing = FALSE;
			}
		}
		break;
	case GDK_Down:
		if (!cline->editing) {
			if (cline->history_cur->next != NULL) {
				cline->history_cur = cline->history_cur->next;
				gtk_entry_set_text (GTK_ENTRY (cline), cline->history_cur->data);
				gtk_editable_set_position (
					GTK_EDITABLE (cline), strlen (cline->history_cur->data));
				cline->editing = FALSE;
			} else {
				gtk_entry_set_text (GTK_ENTRY (cline), "");
				cline->editing = TRUE;
			}
		}
		break;
	default:
		return FALSE;
	}

	g_signal_stop_emission_by_name (cline, "key_press_event");
	return TRUE;
}

static void
gnm_py_command_line_init (GnmPyCommandLine *cline)
{
	g_signal_connect (
		cline, "key_press_event",
		G_CALLBACK (gnm_py_command_line_keypress), NULL);
	g_signal_connect (
		cline, "changed",
		G_CALLBACK (gnm_py_command_line_changed), NULL);
	cline->history = cline->history_tail = NULL;
	cline->history_cur = NULL;
	cline->editing = TRUE;
	cline->history_size = 0;
}

static void
gnm_py_command_line_finalize (GObject *obj)
{
	GnmPyCommandLine *cline = GNM_PY_COMMAND_LINE (obj);

	go_list_free_custom (cline->history, g_free);
	cline->history = NULL;

	parent_class->finalize (obj);
}

static void
gnm_py_command_line_class_init (GObjectClass *gobject_class)
{
	parent_class = g_type_class_peek_parent (gobject_class);

	gobject_class->finalize = gnm_py_command_line_finalize;

	signals[ENTERED_SIGNAL] =
	g_signal_new (
		"entered",
		G_TYPE_FROM_CLASS (gobject_class),
		G_SIGNAL_RUN_FIRST,
		G_STRUCT_OFFSET (GnmPyCommandLineClass, entered),
		NULL, NULL,
		g_cclosure_marshal_VOID__VOID,
		G_TYPE_NONE, 0);
}

GtkWidget *
gnm_py_command_line_new (void)
{
	return g_object_new (GNM_PY_COMMAND_LINE_TYPE, NULL);
}

GSF_DYNAMIC_CLASS (GnmPyCommandLine, gnm_py_command_line,
	gnm_py_command_line_class_init, gnm_py_command_line_init,
	GTK_TYPE_ENTRY)