SF.net SVN: geany:[5705] trunk

eht16 at users.sourceforge.net eht16 at xxxxx
Sun Apr 10 17:03:41 UTC 2011


Revision: 5705
          http://geany.svn.sourceforge.net/geany/?rev=5705&view=rev
Author:   eht16
Date:     2011-04-10 17:03:41 +0000 (Sun, 10 Apr 2011)

Log Message:
-----------
Add ui_menu_add_document_items_sorted() and document_sort_by_display_name() to the plugin API.

Modified Paths:
--------------
    trunk/ChangeLog
    trunk/plugins/geanyfunctions.h
    trunk/src/document.c
    trunk/src/document.h
    trunk/src/plugindata.h
    trunk/src/plugins.c
    trunk/src/ui_utils.c
    trunk/src/ui_utils.h

Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2011-04-10 14:52:57 UTC (rev 5704)
+++ trunk/ChangeLog	2011-04-10 17:03:41 UTC (rev 5705)
@@ -7,6 +7,10 @@
    Add and use signal "document-reload" to the plugin API.
  * src/symbols.c, tagmanager/sql.c:
    Fix mapping of SQL tags for Geany's symbol list (closes #3216474).
+ * src/ui_utils.h, src/plugindata.h, src/document.c, src/plugins.c,
+   src/document.h, src/ui_utils.c, plugins/geanyfunctions.h:
+   Add ui_menu_add_document_items_sorted() and
+   document_sort_by_display_name() to the plugin API.
 
 
 2011-04-08  Colomban Wendling  <colomban(at)geany(dot)org>

Modified: trunk/plugins/geanyfunctions.h
===================================================================
--- trunk/plugins/geanyfunctions.h	2011-04-10 14:52:57 UTC (rev 5704)
+++ trunk/plugins/geanyfunctions.h	2011-04-10 17:03:41 UTC (rev 5705)
@@ -72,6 +72,8 @@
 	geany_functions->p_document->document_get_basename_for_display
 #define document_get_notebook_page \
 	geany_functions->p_document->document_get_notebook_page
+#define document_sort_by_display_name \
+	geany_functions->p_document->document_sort_by_display_name
 #define editor_get_indent_prefs \
 	geany_functions->p_editor->editor_get_indent_prefs
 #define editor_create_widget \
@@ -292,6 +294,8 @@
 	geany_functions->p_ui->ui_get_gtk_settings_integer
 #define ui_combo_box_add_to_history \
 	geany_functions->p_ui->ui_combo_box_add_to_history
+#define ui_menu_add_document_items_sorted \
+	geany_functions->p_ui->ui_menu_add_document_items_sorted
 #define dialogs_show_question \
 	geany_functions->p_dialogs->dialogs_show_question
 #define dialogs_show_msgbox \

Modified: trunk/src/document.c
===================================================================
--- trunk/src/document.c	2011-04-10 14:52:57 UTC (rev 5704)
+++ trunk/src/document.c	2011-04-10 17:03:41 UTC (rev 5705)
@@ -3013,3 +3013,32 @@
 }
 
 
+/** GCompareFunc-like sort function to sort documents, e.g. in a GPtrArray by
+ *  their display names.
+ *  Display names means the base name of the document's file.
+ *
+ * @param a @c document a.
+ * @param b @c document b.
+ * @return negative value if a < b; zero if a = b; positive value if a > b.
+ *
+ *  @since 0.21
+ */
+gint document_sort_by_display_name(gconstpointer a, gconstpointer b)
+{
+	GeanyDocument *doc_a = (GeanyDocument*) ((GPtrArray*) a)->pdata;
+	GeanyDocument *doc_b = (GeanyDocument*) ((GPtrArray*) b)->pdata;
+	gchar *base_name_a, *base_name_b;
+	gint result;
+
+	base_name_a = g_path_get_basename(DOC_FILENAME(doc_a));
+	base_name_b = g_path_get_basename(DOC_FILENAME(doc_b));
+
+	result = strcmp(base_name_a, base_name_b);
+
+	g_free(base_name_a);
+	g_free(base_name_b);
+
+	return result;
+}
+
+

Modified: trunk/src/document.h
===================================================================
--- trunk/src/document.h	2011-04-10 14:52:57 UTC (rev 5704)
+++ trunk/src/document.h	2011-04-10 17:03:41 UTC (rev 5705)
@@ -259,4 +259,6 @@
 
 void document_apply_indent_settings(GeanyDocument *doc);
 
+gint document_sort_by_display_name(gconstpointer a, gconstpointer b);
+
 #endif

Modified: trunk/src/plugindata.h
===================================================================
--- trunk/src/plugindata.h	2011-04-10 14:52:57 UTC (rev 5704)
+++ trunk/src/plugindata.h	2011-04-10 17:03:41 UTC (rev 5705)
@@ -54,7 +54,7 @@
  * @warning You should not test for values below 200 as previously
  * @c GEANY_API_VERSION was defined as an enum value, not a macro.
  */
-#define GEANY_API_VERSION 206
+#define GEANY_API_VERSION 207
 
 /** The Application Binary Interface (ABI) version, incremented whenever
  * existing fields in the plugin data types have to be changed or reordered.
@@ -310,6 +310,7 @@
 	const GdkColor*	(*document_get_status_color) (struct GeanyDocument *doc);
 	gchar*		(*document_get_basename_for_display) (struct GeanyDocument *doc, gint length);
 	gint		(*document_get_notebook_page) (struct GeanyDocument *doc);
+	gint		(*document_sort_by_display_name) (gconstpointer a, gconstpointer b);
 }
 DocumentFuncs;
 
@@ -473,6 +474,8 @@
 	gint		(*ui_get_gtk_settings_integer) (const gchar *property_name, gint default_value);
 	void		(*ui_combo_box_add_to_history) (GtkComboBoxEntry *combo_entry,
 				const gchar *text, gint history_len);
+	void		(*ui_menu_add_document_items_sorted) (GtkMenu *menu, struct GeanyDocument *active,
+				GCallback callback, GCompareFunc sort_func);
 }
 UIUtilsFuncs;
 

Modified: trunk/src/plugins.c
===================================================================
--- trunk/src/plugins.c	2011-04-10 14:52:57 UTC (rev 5704)
+++ trunk/src/plugins.c	2011-04-10 17:03:41 UTC (rev 5705)
@@ -111,7 +111,8 @@
 	&document_rename_file,
 	&document_get_status_color,
 	&document_get_basename_for_display,
-	&document_get_notebook_page
+	&document_get_notebook_page,
+	&document_sort_by_display_name
 };
 
 static EditorFuncs editor_funcs = {
@@ -243,7 +244,8 @@
 	&ui_widget_modify_font_from_string,
 	&ui_is_keyval_enter_or_return,
 	&ui_get_gtk_settings_integer,
-	&ui_combo_box_add_to_history
+	&ui_combo_box_add_to_history,
+	&ui_menu_add_document_items_sorted
 };
 
 static DialogFuncs dialog_funcs = {

Modified: trunk/src/ui_utils.c
===================================================================
--- trunk/src/ui_utils.c	2011-04-10 14:52:57 UTC (rev 5704)
+++ trunk/src/ui_utils.c	2011-04-10 17:03:41 UTC (rev 5705)
@@ -2394,16 +2394,49 @@
  * @since 0.19 */
 void ui_menu_add_document_items(GtkMenu *menu, GeanyDocument *active, GCallback callback)
 {
+	ui_menu_add_document_items_sorted(menu, active, callback, NULL);
+}
+
+
+/** Adds a list of document items to @a menu.
+ *
+ * @a sort_func might be NULL to not sort the documents in the menu. In this case,
+ * the order of the document tabs is used.
+ *
+ * See document_sort_by_display_name() for an example sort function.
+ *
+ * @param menu Menu.
+ * @param active Which document to highlight, or @c NULL.
+ * @param callback is used for each menu item's @c "activate" signal and will be passed
+ * the corresponding document pointer as @c user_data.
+ * @param sort_func is used to sort the list. Might be @c NULL to not sort the list.
+ * @warning You should check @c doc->is_valid in the callback.
+ * @since 0.21 */
+void ui_menu_add_document_items_sorted(GtkMenu *menu, GeanyDocument *active,
+	GCallback callback, GCompareFunc sort_func)
+{
 	GtkWidget *menu_item, *menu_item_label, *image;
 	const GdkColor *color;
 	GeanyDocument *doc;
 	guint i, len;
 	gchar *base_name, *label;
+	GPtrArray *sorted_documents;
 
 	len = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
-	for (i = 0; i < len; i++)
+
+	sorted_documents = g_ptr_array_sized_new(len);
+	/* copy the documents_array into the new one */
+	foreach_document(i)
 	{
-		doc = document_get_from_page(i);
+		g_ptr_array_add(sorted_documents, documents[i]);
+	}
+	/* and now sort it */
+	if (sort_func != NULL)
+		g_ptr_array_sort(sorted_documents, sort_func);
+
+	for (i = 0; i < GEANY(sorted_documents)->len; i++)
+	{
+		doc = g_ptr_array_index(sorted_documents, i);
 		if (! DOC_VALID(doc))
 			continue;
 
@@ -2430,6 +2463,7 @@
 
 		g_free(base_name);
 	}
+	g_ptr_array_free(sorted_documents, TRUE);
 }
 
 

Modified: trunk/src/ui_utils.h
===================================================================
--- trunk/src/ui_utils.h	2011-04-10 14:52:57 UTC (rev 5704)
+++ trunk/src/ui_utils.h	2011-04-10 17:03:41 UTC (rev 5705)
@@ -238,6 +238,8 @@
 
 void ui_menu_add_document_items(GtkMenu *menu, GeanyDocument *active, GCallback callback);
 
+void ui_menu_add_document_items_sorted(GtkMenu *menu, GeanyDocument *active,
+		GCallback callback, GCompareFunc sort_func);
 
 void ui_set_statusbar(gboolean log, const gchar *format, ...) G_GNUC_PRINTF (2, 3);
 


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