changeset 14213:28269422838f

[gaim-migrate @ 16894] Keybindings for GntEntry. committer: Tailor Script <tailor@pidgin.im>
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Sat, 19 Aug 2006 23:51:43 +0000
parents 96a21828d3d4
children a3f9ebb0babd
files console/libgnt/gntentry.c console/libgnt/gntkeys.h
diffstat 2 files changed, 151 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/console/libgnt/gntentry.c	Sat Aug 19 21:41:25 2006 +0000
+++ b/console/libgnt/gntentry.c	Sat Aug 19 23:51:43 2006 +0000
@@ -156,6 +156,83 @@
 	gnt_widget_queue_update(widget);
 }
 
+static void
+move_back(GntEntry *entry)
+{
+	if (entry->cursor <= entry->start)
+		return;
+	entry->cursor = g_utf8_find_prev_char(entry->start, entry->cursor);
+	if (entry->cursor < entry->scroll)
+		entry->scroll = entry->cursor;
+	entry_redraw(GNT_WIDGET(entry));
+}
+
+static void
+move_forward(GntEntry *entry)
+{
+	if (entry->cursor >= entry->end)
+		return;
+	entry->cursor = g_utf8_find_next_char(entry->cursor, NULL);
+	if (g_utf8_pointer_to_offset(entry->scroll, entry->cursor) >= GNT_WIDGET(entry)->priv.width)
+		entry->scroll = g_utf8_find_next_char(entry->scroll, NULL);
+	entry_redraw(GNT_WIDGET(entry));
+}
+
+static void
+backspace(GntEntry *entry)
+{
+	int len;
+
+	if (entry->cursor <= entry->start)
+		return;
+	
+	len = entry->cursor - g_utf8_find_prev_char(entry->start, entry->cursor);
+	entry->cursor -= len;
+	memmove(entry->cursor, entry->cursor + len, entry->end - entry->cursor);
+	entry->end -= len;
+
+	if (entry->scroll > entry->start)
+		entry->scroll = g_utf8_find_prev_char(entry->start, entry->scroll);
+
+	entry_redraw(GNT_WIDGET(entry));
+	if (entry->ddown)
+		show_suggest_dropdown(entry);
+}
+
+static void
+delkey(GntEntry *entry)
+{
+	int len;
+
+	if (entry->cursor >= entry->end)
+		return;
+	
+	len = g_utf8_find_next_char(entry->cursor, NULL) - entry->cursor;
+	memmove(entry->cursor, entry->cursor + len, entry->end - entry->cursor - len + 1);
+	entry->end -= len;
+	entry_redraw(GNT_WIDGET(entry));
+
+	if (entry->ddown)
+		show_suggest_dropdown(entry);
+}
+
+static void
+move_start(GntEntry *entry)
+{
+	entry->scroll = entry->cursor = entry->start;
+	entry_redraw(GNT_WIDGET(entry));
+}
+
+static void
+move_end(GntEntry *entry)
+{
+	entry->cursor = entry->end;
+	/* This should be better than this */
+	while (g_utf8_pointer_to_offset(entry->scroll, entry->cursor) >= GNT_WIDGET(entry)->priv.width)
+		entry->scroll = g_utf8_find_next_char(entry->scroll, NULL);
+	entry_redraw(GNT_WIDGET(entry));
+}
+
 static gboolean
 gnt_entry_key_pressed(GntWidget *widget, const char *text)
 {
@@ -165,32 +242,17 @@
 	{
 		if (strcmp(text + 1, GNT_KEY_DEL) == 0 && entry->cursor < entry->end)
 		{
-			int len = g_utf8_find_next_char(entry->cursor, NULL) - entry->cursor;
-			memmove(entry->cursor, entry->cursor + len, entry->end - entry->cursor - len + 1);
-			entry->end -= len;
-			entry_redraw(widget);
-
-			if (entry->ddown)
-				show_suggest_dropdown(entry);
-
+			delkey(entry);
 			return TRUE;
 		}
-		else if (strcmp(text + 1, GNT_KEY_LEFT) == 0 && entry->cursor > entry->start)
+		else if (strcmp(text + 1, GNT_KEY_LEFT) == 0)
 		{
-			entry->cursor = g_utf8_find_prev_char(entry->start, entry->cursor);
-			if (entry->cursor < entry->scroll)
-				entry->scroll = entry->cursor;
-			entry_redraw(widget);
-
+			move_back(entry);
 			return TRUE;
 		}
-		else if (strcmp(text + 1, GNT_KEY_RIGHT) == 0 && entry->cursor < entry->end)
+		else if (strcmp(text + 1, GNT_KEY_RIGHT) == 0)
 		{
-			entry->cursor = g_utf8_find_next_char(entry->cursor, NULL);
-			if (g_utf8_pointer_to_offset(entry->scroll, entry->cursor) >= widget->priv.width)
-				entry->scroll = g_utf8_find_next_char(entry->scroll, NULL);
-			entry_redraw(widget);
-
+			move_forward(entry);
 			return TRUE;
 		}
 		else if (strcmp(text + 1, GNT_KEY_CTRL_DOWN) == 0 && entry->histlength)
@@ -322,20 +384,55 @@
 		}
 		else
 		{
-			/* Backspace is here */
 			if (strcmp(text, GNT_KEY_BACKSPACE) == 0 && entry->cursor > entry->start)
 			{
-				int len = entry->cursor - g_utf8_find_prev_char(entry->start, entry->cursor);
-				entry->cursor -= len;
-				memmove(entry->cursor, entry->cursor + len, entry->end - entry->cursor);
-				entry->end -= len;
-
-				if (entry->scroll > entry->start)
-					entry->scroll = g_utf8_find_prev_char(entry->start, entry->scroll);
-
+				backspace(entry);
+				return TRUE;
+			}
+			else if (strcmp(text, GNT_KEY_CTRL_A) == 0)
+			{
+				move_start(entry);
+				return TRUE;
+			}
+			else if (strcmp(text, GNT_KEY_CTRL_B) == 0)
+			{
+				move_back(entry);
+				return TRUE;
+			}
+			else if (strcmp(text, GNT_KEY_CTRL_D) == 0)
+			{
+				delkey(entry);
+				return TRUE;
+			}
+			else if (strcmp(text, GNT_KEY_CTRL_E) == 0)
+			{
+				move_end(entry);
+				return TRUE;
+			}
+			else if (strcmp(text, GNT_KEY_CTRL_F) == 0)
+			{
+				move_forward(entry);
+				return TRUE;
+			}
+			else if (strcmp(text, GNT_KEY_CTRL_H) == 0)
+			{
+				backspace(entry);
+				return TRUE;
+			}
+			else if (strcmp(text, GNT_KEY_CTRL_K) == 0)
+			{
+				entry->end = entry->cursor;
+				memset(entry->end, '\0', entry->buffer - (entry->end - entry->start));
 				entry_redraw(widget);
-				if (entry->ddown)
-					show_suggest_dropdown(entry);
+				return TRUE;
+			}
+			else if (strcmp(text, GNT_KEY_CTRL_U) == 0)
+			{
+				memmove(entry->start, entry->cursor, entry->end - entry->cursor);
+				entry->end -= (entry->cursor - entry->start);
+				entry->cursor = entry->scroll = entry->start;
+				memset(entry->end, '\0', entry->buffer - (entry->end - entry->start));
+				entry_redraw(widget);
 				return TRUE;
 			}
 		}
--- a/console/libgnt/gntkeys.h	Sat Aug 19 21:41:25 2006 +0000
+++ b/console/libgnt/gntkeys.h	Sat Aug 19 23:51:43 2006 +0000
@@ -22,6 +22,29 @@
 #define GNT_KEY_BACKSPACE "\177"
 #define GNT_KEY_DEL    "[3~"
 
+#define GNT_KEY_CTRL_A     "\001"
+#define GNT_KEY_CTRL_B     "\002"
+#define GNT_KEY_CTRL_D     "\004"
+#define GNT_KEY_CTRL_E     "\005"
+#define GNT_KEY_CTRL_F     "\006"
+#define GNT_KEY_CTRL_G     "\007"
+#define GNT_KEY_CTRL_H     "\010"
+#define GNT_KEY_CTRL_I     "\011"
+#define GNT_KEY_CTRL_J     "\012"
+#define GNT_KEY_CTRL_K     "\013"
+#define GNT_KEY_CTRL_L     "\014"
+#define GNT_KEY_CTRL_M     "\012"
+#define GNT_KEY_CTRL_N     "\016"
+#define GNT_KEY_CTRL_O     "\017"
+#define GNT_KEY_CTRL_P     "\020"
+#define GNT_KEY_CTRL_R     "\022"
+#define GNT_KEY_CTRL_T     "\024"
+#define GNT_KEY_CTRL_U     "\025"
+#define GNT_KEY_CTRL_V     "\026"
+#define GNT_KEY_CTRL_W     "\027"
+#define GNT_KEY_CTRL_X     "\030"
+#define GNT_KEY_CTRL_Y     "\031"
+
 /**
  * This will do stuff with the terminal settings and stuff.
  */