SF.net SVN: geany:[3922] trunk

ntrel at users.sourceforge.net ntrel at xxxxx
Tue Jul 7 11:57:51 UTC 2009


Revision: 3922
          http://geany.svn.sourceforge.net/geany/?rev=3922&view=rev
Author:   ntrel
Date:     2009-07-07 11:57:50 +0000 (Tue, 07 Jul 2009)

Log Message:
-----------
Fix removing underscores in translated string using no_underscore()
macro.
Set a tooltip for the Split Window plugin's Show Current tool button.
Add utils_strdupa() macro.

Modified Paths:
--------------
    trunk/ChangeLog
    trunk/plugins/splitwindow.c
    trunk/src/highlighting.c
    trunk/src/utils.h

Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2009-07-07 07:50:27 UTC (rev 3921)
+++ trunk/ChangeLog	2009-07-07 11:57:50 UTC (rev 3922)
@@ -1,3 +1,12 @@
+2009-07-06  Nick Treleaven  <nick(dot)treleaven(at)btinternet(dot)com>
+
+ * src/highlighting.c, src/utils.h, plugins/splitwindow.c:
+   Fix removing underscores in translated string using no_underscore()
+   macro.
+   Set a tooltip for the Split Window plugin's Show Current tool button.
+   Add utils_strdupa() macro.
+
+
 2009-07-04  Enrico Tröger  <enrico(dot)troeger(at)uvena(dot)de>
 
  * src/build.c:

Modified: trunk/plugins/splitwindow.c
===================================================================
--- trunk/plugins/splitwindow.c	2009-07-07 07:50:27 UTC (rev 3921)
+++ trunk/plugins/splitwindow.c	2009-07-07 11:57:50 UTC (rev 3922)
@@ -25,6 +25,8 @@
 /* Split Window plugin. */
 
 #include "geany.h"
+#include <string.h>
+
 #include "support.h"
 #include "Scintilla.h"
 #include "ScintillaWidget.h"
@@ -35,6 +37,8 @@
 #include "editor.h"
 #include "plugindata.h"
 #include "keybindings.h"
+#include "utils.h"
+
 #include "geanyfunctions.h"
 
 
@@ -225,14 +229,86 @@
 }
 
 
-static GtkWidget *create_tool_button(const gchar *label, const gchar *stock_id)
+/* Avoid adding new string translations which are the same but without an underscore.
+ * @warning Heavy use may cause stack exhaustion. */
+#define no_underscore(str)\
+	utils_str_remove_chars(utils_strdupa(str), "_")
+
+/* Like strcpy, but can handle overlapping src and dest. */
+static gchar *utils_str_copy(gchar *dest, const gchar *src)
 {
+	gchar *cpy;
+
+	/* strcpy might not handle overlaps, so make a copy */
+	cpy = utils_strdupa(src);
+	return strcpy(dest, cpy);
+}
+
+
+/* Remove characters from a string, in place.
+ * @param chars Characters to remove.
+ * @return @a str */
+/* Note: Could be more efficient by writing non-chars into new string then using only one strcpy. */
+static gchar *utils_str_remove_chars(gchar *str, const gchar *chars)
+{
+	gchar *ptr;
+	gsize len;
+
+	g_return_val_if_fail(str, NULL);
+
+	len = strlen(str);
+	ptr = str;
+
+	while (ptr < str + len)
+	{
+		if (strchr(chars, *ptr))
+		{
+			utils_str_copy(ptr, ptr + 1);
+			len--;
+		}
+		else
+			ptr++;
+	}
+	return str;
+}
+
+
+static const gchar *ui_get_stock_label(const gchar *stock_id)
+{
+	GtkStockItem item;
+
+	if (gtk_stock_lookup(stock_id, &item))
+		return item.label;
+
+	g_warning("No stock id '%s'!", stock_id);
+	return "";
+}
+
+
+/* Create a GtkToolButton with stock icon and tooltip.
+ * @param label can be NULL to use stock label text, without underscores.
+ * @param tooltip can be NULL to use label text (useful for GTK_TOOLBAR_ICONS). */
+static GtkWidget *ui_tool_button_new(const gchar *stock_id, const gchar *label, const gchar *tooltip)
+{
 	GtkToolItem *item;
+	gchar *dup = NULL;
 
+	if (stock_id && !label)
+	{
+		label = ui_get_stock_label(stock_id);
+		dup = g_strdup(label);
+		label = utils_str_remove_chars(dup, "_");
+	}
 	item = gtk_tool_button_new(NULL, label);
-	gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(item), stock_id);
-	ui_widget_set_tooltip_text(GTK_WIDGET(item), label);
+	if (stock_id)
+		gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(item), stock_id);
 
+	if (!tooltip)
+		tooltip = label;
+	if (tooltip)
+		ui_widget_set_tooltip_text(GTK_WIDGET(item), tooltip);
+
+	g_free(dup);
 	return GTK_WIDGET(item);
 }
 
@@ -248,18 +324,6 @@
 }
 
 
-/* avoid adding new strings which are the same but without a leading underscore */
-static const gchar *after_underscore(const gchar *str)
-{
-	const gchar *u = g_strstr_len(str, -1, "_");
-
-	if (u)
-		return ++u;
-	else
-		return str;
-}
-
-
 static GtkWidget *create_toolbar(void)
 {
 	GtkWidget *toolbar, *item;
@@ -269,7 +333,8 @@
 	gtk_toolbar_set_icon_size(GTK_TOOLBAR(toolbar), GTK_ICON_SIZE_MENU);
 	gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);
 
-	item = (GtkWidget*)gtk_tool_button_new_from_stock(GTK_STOCK_REFRESH);
+	item = ui_tool_button_new(GTK_STOCK_JUMP_TO, "",
+		_("Show the current document"));
 	gtk_container_add(GTK_CONTAINER(toolbar), item);
 	g_signal_connect(item, "clicked", G_CALLBACK(on_refresh), NULL);
 
@@ -282,7 +347,7 @@
 	gtk_container_add(GTK_CONTAINER(tool_item), item);
 	edit_window.name_label = item;
 
-	item = create_tool_button(after_underscore(_("_Unsplit")), GTK_STOCK_CLOSE);
+	item = ui_tool_button_new(GTK_STOCK_CLOSE, no_underscore(_("_Unsplit")), NULL);
 	gtk_container_add(GTK_CONTAINER(toolbar), item);
 	g_signal_connect(item, "clicked", G_CALLBACK(on_unsplit), NULL);
 

Modified: trunk/src/highlighting.c
===================================================================
--- trunk/src/highlighting.c	2009-07-07 07:50:27 UTC (rev 3921)
+++ trunk/src/highlighting.c	2009-07-07 11:57:50 UTC (rev 3922)
@@ -39,6 +39,7 @@
 #include "filetypes.h"
 #include "symbols.h"
 #include "ui_utils.h"
+#include "utils.h"
 
 
 /* Note: Avoid using SSM in files not related to scintilla, use sciwrappers.h instead. */
@@ -178,12 +179,6 @@
 }
 
 
-/* Like glibc's strdupa, but portable.
- * Don't use for long strings. */
-#define local_strdup(dest, src) \
-	dest = g_alloca(strlen(src) + 1); \
-	strcpy(dest, src);
-
 static void read_named_style(const gchar *named_style, GeanyLexerStyle *style)
 {
 	GeanyLexerStyle *cs;
@@ -192,7 +187,7 @@
 	const gchar *italic = NULL;
 
 	g_return_if_fail(named_style);
-	local_strdup(name, named_style);
+	name = utils_strdupa(named_style);	/* named_style must not be written to, may be a static string */
 
 	comma = strstr(name, ",");
 	if (comma)

Modified: trunk/src/utils.h
===================================================================
--- trunk/src/utils.h	2009-07-07 07:50:27 UTC (rev 3921)
+++ trunk/src/utils.h	2009-07-07 11:57:50 UTC (rev 3922)
@@ -49,6 +49,14 @@
 		g_free(setptr_tmp);\
 	}
 
+/** Like glibc's @c strdupa(), but portable.
+ * Duplicate a string on the stack using @c g_alloca().
+ * @note You must include @c string.h yourself.
+ * @warning Don't use excessively or for long strings otherwise there may be stack exhaustion -
+ *          see the GLib docs for @c g_alloca(). */
+#define utils_strdupa(str) \
+	strcpy(g_alloca(strlen(str) + 1), str)
+
 #define foreach_c_array(item, array, len) \
 	for (item = array; item < &array[len]; item++)
 


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.



More information about the Commits mailing list