changeset 28602:a169c5c81ef5

Remove a bunch of code duplication.
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Fri, 20 Nov 2009 09:11:10 +0000
parents c84a82b014ec
children 8356e2f2486a
files pidgin/gtkimhtml.c
diffstat 1 files changed, 68 insertions(+), 113 deletions(-) [+]
line wrap: on
line diff
--- a/pidgin/gtkimhtml.c	Fri Nov 20 09:06:35 2009 +0000
+++ b/pidgin/gtkimhtml.c	Fri Nov 20 09:11:10 2009 +0000
@@ -472,6 +472,24 @@
 	parent_style_set(widget, prev_style);
 }
 
+static gboolean
+imhtml_get_iter_bounds(GtkIMHtml *imhtml, GtkTextIter *start, GtkTextIter *end)
+{
+	if (imhtml->wbfo) {
+		gtk_text_buffer_get_bounds(imhtml->text_buffer, start, end);
+		return TRUE;
+	} else if (imhtml->editable) {
+		if (!gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, start, end)) {
+			GtkTextMark *mark = gtk_text_buffer_get_insert(imhtml->text_buffer);
+			gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, start, mark);
+			*end = *start;
+		}
+		return TRUE;
+	}
+
+	return FALSE;
+}
+
 static void
 gtk_imhtml_set_link_color(GtkIMHtml *imhtml, GtkTextTag *tag)
 {
@@ -4304,13 +4322,8 @@
 	if (!imhtml->editable)
 		return;
 
-	if (imhtml->wbfo)
-		gtk_text_buffer_get_bounds(imhtml->text_buffer, &start, &end);
-	else if (!gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end)) {
-		GtkTextMark *mark = gtk_text_buffer_get_insert(imhtml->text_buffer);
-		gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &start, mark);
-		end = start;
-	}
+	if (!imhtml_get_iter_bounds(imhtml, &start, &end))
+		return;
 
 	gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "BOLD", &start, &end);
 	gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "ITALICS", &start, &end);
@@ -4653,19 +4666,13 @@
 
 	imhtml->edit.bold = !imhtml->edit.bold;
 
-	if (imhtml->wbfo) {
-		gtk_text_buffer_get_bounds(imhtml->text_buffer, &start, &end);
-		if (imhtml->edit.bold)
-			gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "BOLD", &start, &end);
-		else
-			gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "BOLD", &start, &end);
-	} else if (imhtml->editable && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end)) {
-		if (imhtml->edit.bold)
-			gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "BOLD", &start, &end);
-		else
-			gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "BOLD", &start, &end);
-
-	}
+	if (!imhtml_get_iter_bounds(imhtml, &start, &end))
+		return;
+
+	if (imhtml->edit.bold)
+		gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "BOLD", &start, &end);
+	else
+		gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "BOLD", &start, &end);
 }
 
 void gtk_imhtml_toggle_bold(GtkIMHtml *imhtml)
@@ -4679,18 +4686,13 @@
 
 	imhtml->edit.italic = !imhtml->edit.italic;
 
-	if (imhtml->wbfo) {
-		gtk_text_buffer_get_bounds(imhtml->text_buffer, &start, &end);
-		if (imhtml->edit.italic)
-			gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "ITALICS", &start, &end);
-		else
-			gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "ITALICS", &start, &end);
-	} else if (imhtml->editable && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end)) {
-		if (imhtml->edit.italic)
-			gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "ITALICS", &start, &end);
-		else
-			gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "ITALICS", &start, &end);
-	}
+	if (!imhtml_get_iter_bounds(imhtml, &start, &end))
+		return;
+
+	if (imhtml->edit.italic)
+		gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "ITALICS", &start, &end);
+	else
+		gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "ITALICS", &start, &end);
 }
 
 void gtk_imhtml_toggle_italic(GtkIMHtml *imhtml)
@@ -4704,18 +4706,13 @@
 
 	imhtml->edit.underline = !imhtml->edit.underline;
 
-	if (imhtml->wbfo) {
-		gtk_text_buffer_get_bounds(imhtml->text_buffer, &start, &end);
-		if (imhtml->edit.underline)
-			gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "UNDERLINE", &start, &end);
-		else
-			gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "UNDERLINE", &start, &end);
-	} else if (imhtml->editable && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end)) {
-		if (imhtml->edit.underline)
-			gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "UNDERLINE", &start, &end);
-		else
-			gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "UNDERLINE", &start, &end);
-	}
+	if (!imhtml_get_iter_bounds(imhtml, &start, &end))
+		return;
+
+	if (imhtml->edit.underline)
+		gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "UNDERLINE", &start, &end);
+	else
+		gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "UNDERLINE", &start, &end);
 }
 
 void gtk_imhtml_toggle_underline(GtkIMHtml *imhtml)
@@ -4729,18 +4726,13 @@
 
 	imhtml->edit.strike = !imhtml->edit.strike;
 
-	if (imhtml->wbfo) {
-		gtk_text_buffer_get_bounds(imhtml->text_buffer, &start, &end);
-		if (imhtml->edit.strike)
-			gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "STRIKE", &start, &end);
-		else
-			gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "STRIKE", &start, &end);
-	} else if (imhtml->editable && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end)) {
-		if (imhtml->edit.strike)
-			gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "STRIKE", &start, &end);
-		else
-			gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "STRIKE", &start, &end);
-	}
+	if (!imhtml_get_iter_bounds(imhtml, &start, &end))
+		return;
+
+	if (imhtml->edit.strike)
+		gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "STRIKE", &start, &end);
+	else
+		gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "STRIKE", &start, &end);
 }
 
 void gtk_imhtml_toggle_strike(GtkIMHtml *imhtml)
@@ -4755,16 +4747,12 @@
 
 	imhtml->edit.fontsize = size;
 
-	if (imhtml->wbfo) {
-		gtk_text_buffer_get_bounds(imhtml->text_buffer, &start, &end);
-		remove_font_size(imhtml, &start, &end, TRUE);
-		gtk_text_buffer_apply_tag(imhtml->text_buffer,
-		                                  find_font_size_tag(imhtml, imhtml->edit.fontsize), &start, &end);
-	} else if (imhtml->editable && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end)) {
-		remove_font_size(imhtml, &start, &end, FALSE);
-		gtk_text_buffer_apply_tag(imhtml->text_buffer,
-		                                  find_font_size_tag(imhtml, imhtml->edit.fontsize), &start, &end);
-	}
+	if (!imhtml_get_iter_bounds(imhtml, &start, &end))
+		return;
+
+	remove_font_size(imhtml, &start, &end, imhtml->wbfo);
+	gtk_text_buffer_apply_tag(imhtml->text_buffer,
+			find_font_size_tag(imhtml, imhtml->edit.fontsize), &start, &end);
 
 	object = g_object_ref(G_OBJECT(imhtml));
 	g_signal_emit(object, signals[TOGGLE_FORMAT], 0, GTK_IMHTML_SHRINK | GTK_IMHTML_GROW);
@@ -4783,16 +4771,11 @@
 	else
 		imhtml->edit.fontsize--;
 
-	if (imhtml->wbfo) {
-		gtk_text_buffer_get_bounds(imhtml->text_buffer, &start, &end);
-		remove_font_size(imhtml, &start, &end, TRUE);
-		gtk_text_buffer_apply_tag(imhtml->text_buffer,
+	if (!imhtml_get_iter_bounds(imhtml, &start, &end))
+		return;
+	remove_font_size(imhtml, &start, &end, imhtml->wbfo);
+	gtk_text_buffer_apply_tag(imhtml->text_buffer,
 		                                  find_font_size_tag(imhtml, imhtml->edit.fontsize), &start, &end);
-	} else if (imhtml->editable && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end)) {
-		remove_font_size(imhtml, &start, &end, FALSE);
-		gtk_text_buffer_apply_tag(imhtml->text_buffer,
-		                                  find_font_size_tag(imhtml, imhtml->edit.fontsize), &start, &end);
-	}
 }
 
 void gtk_imhtml_font_shrink(GtkIMHtml *imhtml)
@@ -4812,16 +4795,11 @@
 	else
 		imhtml->edit.fontsize++;
 
-	if (imhtml->wbfo) {
-		gtk_text_buffer_get_bounds(imhtml->text_buffer, &start, &end);
-		remove_font_size(imhtml, &start, &end, TRUE);
-		gtk_text_buffer_apply_tag(imhtml->text_buffer,
-		                                  find_font_size_tag(imhtml, imhtml->edit.fontsize), &start, &end);
-	} else if (imhtml->editable && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end)) {
-		remove_font_size(imhtml, &start, &end, FALSE);
-		gtk_text_buffer_apply_tag(imhtml->text_buffer,
-		                                  find_font_size_tag(imhtml, imhtml->edit.fontsize), &start, &end);
-	}
+	if (!imhtml_get_iter_bounds(imhtml, &start, &end))
+		return;
+	remove_font_size(imhtml, &start, &end, imhtml->wbfo);
+	gtk_text_buffer_apply_tag(imhtml->text_buffer,
+			find_font_size_tag(imhtml, imhtml->edit.fontsize), &start, &end);
 }
 
 void gtk_imhtml_font_grow(GtkIMHtml *imhtml)
@@ -4844,39 +4822,16 @@
 	{
 		*edit_field = g_strdup(value);
 
-		if (imhtml->wbfo)
-		{
-			gtk_text_buffer_get_bounds(imhtml->text_buffer, &start, &end);
-			remove_func(imhtml, &start, &end, TRUE);
+		if (imhtml_get_iter_bounds(imhtml, &start, &end)) {
+			remove_func(imhtml, &start, &end, imhtml->wbfo);
 			gtk_text_buffer_apply_tag(imhtml->text_buffer,
-		                              find_func(imhtml, *edit_field), &start, &end);
-		}
-		else
-		{
-			gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &start,
-			                                 gtk_text_buffer_get_mark(imhtml->text_buffer, "insert"));
-			if (imhtml->editable && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end))
-			{
-				remove_func(imhtml, &start, &end, FALSE);
-				gtk_text_buffer_apply_tag(imhtml->text_buffer,
-				                          find_func(imhtml,
-				                                    *edit_field),
-				                                    &start, &end);
-			}
+					find_func(imhtml, *edit_field), &start, &end);
 		}
 	}
 	else
 	{
-		if (imhtml->wbfo)
-		{
-			gtk_text_buffer_get_bounds(imhtml->text_buffer, &start, &end);
-			remove_func(imhtml, &start, &end, TRUE);
-		}
-		else
-		{
-			if (imhtml->editable && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end))
-				remove_func(imhtml, &start, &end, TRUE);
-		}
+		if (imhtml_get_iter_bounds(imhtml, &start, &end))
+			remove_func(imhtml, &start, &end, TRUE);	/* 'TRUE' or 'imhtml->wbfo'? */
 	}
 
 	object = g_object_ref(G_OBJECT(imhtml));