changeset 7714:3d9d3d21e600

[gaim-migrate @ 8359] I rock so hard! This is just too easy. WYSIWYG foreground and background colors. The observant commit watcher will note that I'm not really paying too close attention to the toolbar UI yet. And as a result, it doesn't really work too great. The plan is to make the toolbar its own GtkWidget, which will attach to a GtkIMHtml. The GtkIMHtml will emit signals to it, telling it what state it should be in, and the toolbar will manipulate the GtkIMHtml accordingly. This way, anything that has text entry can have a toolbar with which to edit it. committer: Tailor Script <tailor@pidgin.im>
author Sean Egan <seanegan@gmail.com>
date Wed, 03 Dec 2003 04:17:42 +0000
parents 1adc71ed6d45
children 9f6dc7b4fc57
files src/dialogs.c src/gtkimhtml.c src/gtkimhtml.h
diffstat 3 files changed, 78 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/src/dialogs.c	Wed Dec 03 02:03:44 2003 +0000
+++ b/src/dialogs.c	Wed Dec 03 04:17:42 2003 +0000
@@ -1093,16 +1093,12 @@
 	gtkconv = GAIM_GTK_CONVERSATION(c);
 
 	gtkconv->fg_color = text_color;
-	g_snprintf(open_tag, 23, "<FONT COLOR=\"#%02X%02X%02X\">",
+	g_snprintf(open_tag, 23, "#%02X%02X%02X",
 			   text_color.red / 256,
 			   text_color.green / 256,
 			   text_color.blue / 256);
-	gaim_gtk_surround(gtkconv, open_tag, "</FONT>");
+	gtk_imhtml_toggle_forecolor(GTK_IMHTML(gtkconv->entry), open_tag);
 
-	gaim_debug(GAIM_DEBUG_MISC, "fgcolor dialog", "#%02X%02X%02X\n",
-			   text_color.red / 256,
-			   text_color.green / 256,
-			   text_color.blue / 256);
 	g_free(open_tag);
 	cancel_fgcolor(NULL, c);
 }
@@ -1124,16 +1120,12 @@
 	gtkconv = GAIM_GTK_CONVERSATION(c);
 
 	gtkconv->bg_color = text_color;
-	g_snprintf(open_tag, 25, "<BODY BGCOLOR=\"#%02X%02X%02X\">",
+	g_snprintf(open_tag, 25, "#%02X%02X%02X",
 			   text_color.red / 256,
 			   text_color.green / 256,
 			   text_color.blue / 256);
-	gaim_gtk_surround(gtkconv, open_tag, "</BODY>");
-	gaim_debug(GAIM_DEBUG_MISC, "bgcolor dialog", "#%02X%02X%02X\n",
-			   text_color.red / 256,
-			   text_color.green / 256,
-			   text_color.blue / 256);
-
+	gtk_imhtml_toggle_backcolor(GTK_IMHTML(gtkconv->entry), open_tag);
+	
 	g_free(open_tag);
 	cancel_bgcolor(NULL, c);
 }
--- a/src/gtkimhtml.c	Wed Dec 03 02:03:44 2003 +0000
+++ b/src/gtkimhtml.c	Wed Dec 03 04:17:42 2003 +0000
@@ -520,6 +520,9 @@
 	imhtml->edit.bold = NULL;
 	imhtml->edit.italic = NULL;
 	imhtml->edit.underline = NULL;
+	imhtml->edit.forecolor = NULL;
+	imhtml->edit.backcolor = NULL;
+
 	imhtml->format_spans = NULL;
 	
 	imhtml->scalables = NULL;
@@ -1975,13 +1978,20 @@
 		gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "ITALICS", &italic, iter);
 	}
 	
-		
-	if ((span = imhtml->edit.underline)) {
-		GtkTextIter underline;
-		gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &underline, span->start);
+	if ((span = imhtml->edit.forecolor)) {
+		GtkTextIter fore;
+		gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &fore, span->start);
 		gtk_text_iter_forward_chars(iter, len);
-		gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "UNDERLINE", &underline, iter);
+		gtk_text_buffer_apply_tag(imhtml->text_buffer, span->tag, &fore, iter);
 	}
+	
+	if ((span = imhtml->edit.backcolor)) {
+		GtkTextIter back;
+		gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &back, span->start);
+		gtk_text_iter_forward_chars(iter, len);
+		gtk_text_buffer_apply_tag(imhtml->text_buffer, span->tag, &back, iter);
+	}
+	
 }
 
 void gtk_imhtml_set_editable(GtkIMHtml *imhtml, gboolean editable) 
@@ -2009,6 +2019,7 @@
 		span->end = NULL;
 		span->end_tag = g_strdup("</b>");
 		span->buffer = imhtml->text_buffer;
+		span->tag =  gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(imhtml->text_buffer), "BOLD");
 		imhtml->edit.bold = span;
 		imhtml->format_spans = g_list_append(imhtml->format_spans, span);
 	} else {
@@ -2032,6 +2043,7 @@
 		span->end = NULL;
 		span->end_tag = g_strdup("</i>");
 		span->buffer = imhtml->text_buffer;
+		span->tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(imhtml->text_buffer), "ITALIC");
 		imhtml->edit.italic = span;
 		imhtml->format_spans = g_list_append(imhtml->format_spans, span);
 	} else {
@@ -2041,6 +2053,7 @@
 	}
 	return imhtml->edit.italic != NULL;
 }
+
 gboolean gtk_imhtml_toggle_underline(GtkIMHtml *imhtml)
 {
 	GtkIMHtmlFormatSpan *span;
@@ -2054,6 +2067,7 @@
 		span->end = NULL;
 		span->end_tag = g_strdup("</u>");
 		span->buffer = imhtml->text_buffer;
+		span->tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(imhtml->text_buffer), "UNDERLINE");
 		imhtml->edit.underline = span;
 		imhtml->format_spans = g_list_append(imhtml->format_spans, span);
 	} else {
@@ -2064,6 +2078,54 @@
 	return imhtml->edit.underline != NULL;
 }
 
+gboolean gtk_imhtml_toggle_forecolor(GtkIMHtml *imhtml, const char *color)
+{
+	GtkIMHtmlFormatSpan *span;
+	GtkTextMark *ins = gtk_text_buffer_get_insert(imhtml->text_buffer);
+	GtkTextIter iter;
+	gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &iter, ins);
+	if (!imhtml->edit.forecolor) {
+		span = g_malloc(sizeof(GtkIMHtmlFormatSpan));
+		span->start = gtk_text_buffer_create_mark(imhtml->text_buffer, NULL, &iter, TRUE);
+		span->start_tag = g_strdup_printf("<font color='%s'>", color);
+		span->end = NULL;
+		span->end_tag = g_strdup("</font>");
+		span->buffer = imhtml->text_buffer;
+		span->tag = gtk_text_buffer_create_tag(imhtml->text_buffer, NULL, "foreground", color, NULL);
+		imhtml->edit.forecolor = span;
+		imhtml->format_spans = g_list_append(imhtml->format_spans, span);
+	} else {
+		span = imhtml->edit.forecolor;
+		span->end = gtk_text_buffer_create_mark(imhtml->text_buffer, NULL, &iter, TRUE);
+		imhtml->edit.forecolor = NULL;
+	}
+	return imhtml->edit.forecolor != NULL;
+}
+
+gboolean gtk_imhtml_toggle_backcolor(GtkIMHtml *imhtml, const char *color)
+{
+	GtkIMHtmlFormatSpan *span;
+	GtkTextMark *ins = gtk_text_buffer_get_insert(imhtml->text_buffer);
+	GtkTextIter iter;
+	gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &iter, ins);
+	if (!imhtml->edit.backcolor) {
+		span = g_malloc(sizeof(GtkIMHtmlFormatSpan));
+		span->start = gtk_text_buffer_create_mark(imhtml->text_buffer, NULL, &iter, TRUE);
+		span->start_tag = g_strdup_printf("<body bgcolor='%s'>", color);
+		span->end = NULL;
+		span->end_tag = g_strdup("</font>");
+		span->buffer = imhtml->text_buffer;
+		span->tag = gtk_text_buffer_create_tag(imhtml->text_buffer, NULL, "background", color, NULL);
+		imhtml->edit.backcolor = span;
+		imhtml->format_spans = g_list_append(imhtml->format_spans, span);
+	} else {
+		span = imhtml->edit.backcolor;
+		span->end = gtk_text_buffer_create_mark(imhtml->text_buffer, NULL, &iter, TRUE);
+		imhtml->edit.backcolor = NULL;
+	}
+	return imhtml->edit.backcolor != NULL;
+}
+
 void gtk_imhtml_insert_link(GtkIMHtml *imhtml, const char *url, const char *text)
 {
 	GtkIMHtmlFormatSpan *span = g_malloc(sizeof(GtkIMHtmlFormatSpan));
--- a/src/gtkimhtml.h	Wed Dec 03 02:03:44 2003 +0000
+++ b/src/gtkimhtml.h	Wed Dec 03 04:17:42 2003 +0000
@@ -56,6 +56,7 @@
 	char *start_tag;
 	char *end_tag;
 	GtkTextBuffer *buffer;
+	GtkTextTag *tag;
 } GtkIMHtmlFormatSpan;
 
 struct _GtkIMHtml {
@@ -88,6 +89,8 @@
 		GtkIMHtmlFormatSpan *bold;
 		GtkIMHtmlFormatSpan *italic;
 		GtkIMHtmlFormatSpan *underline;
+		GtkIMHtmlFormatSpan *forecolor;
+		GtkIMHtmlFormatSpan *backcolor;
 	} edit;
 	GList *format_spans;
 };
@@ -206,6 +209,9 @@
 gboolean gtk_imhtml_toggle_bold(GtkIMHtml *imhtml);
 gboolean gtk_imhtml_toggle_italic(GtkIMHtml *imhtml);
 gboolean gtk_imhtml_toggle_underline(GtkIMHtml *imhtml);
+gboolean gtk_imhtml_toggle_forecolor(GtkIMHtml *imhtml, const char *color);
+gboolean gtk_imhtml_toggle_backcolor(GtkIMHtml *imhtml, const char *color);
+
 void gtk_imhtml_insert_link(GtkIMHtml *imhtml, const char *url, const char *text);
 
 char *gtk_imhtml_get_markup(GtkIMHtml *imhtml);