pidgin: e82b0d8f: Remove a bunch of code duplication.

sadrul at pidgin.im sadrul at pidgin.im
Fri Nov 20 04:10:22 EST 2009


-----------------------------------------------------------------
Revision: e82b0d8f02e0a92cc7e4e5dab9ebcb49d0f180e6
Ancestor: fc956b554f067ed7a616b17976e5f2a50747bc93
Author: sadrul at pidgin.im
Date: 2009-11-20T09:11:10
Branch: im.pidgin.pidgin
URL: http://d.pidgin.im/viewmtn/revision/info/e82b0d8f02e0a92cc7e4e5dab9ebcb49d0f180e6

Modified files:
        pidgin/gtkimhtml.c

ChangeLog: 

Remove a bunch of code duplication.

-------------- next part --------------
============================================================
--- pidgin/gtkimhtml.c	cd8a58a6cf45aab382e8ad6c46d7cf86cdd24e0b
+++ pidgin/gtkimhtml.c	5e93c64a73fc5627035a4e4a03d4c7b12946e7ed
@@ -472,6 +472,24 @@ gtk_imhtml_style_set(GtkWidget *widget, 
 	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 @@ imhtml_clear_formatting(GtkIMHtml *imhtm
 	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 @@ static void imhtml_toggle_bold(GtkIMHtml
 
 	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 @@ static void imhtml_toggle_italic(GtkIMHt
 
 	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 @@ static void imhtml_toggle_underline(GtkI
 
 	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 @@ static void imhtml_toggle_strike(GtkIMHt
 
 	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,17 +4747,13 @@ void gtk_imhtml_font_set_size(GtkIMHtml 
 
 	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);
 	g_object_unref(object);
@@ -4783,16 +4771,11 @@ static void imhtml_font_shrink(GtkIMHtml
 	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 @@ static void imhtml_font_grow(GtkIMHtml *
 	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 @@ static gboolean gtk_imhtml_toggle_str_ta
 	{
 		*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);
+					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);
-			}
-		}
 	}
 	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));


More information about the Commits mailing list