diff src/gtkimhtml.c @ 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 a1d913ecd0f6
children 9f6dc7b4fc57
line wrap: on
line diff
--- 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));