Revision: 3658 http://geany.svn.sourceforge.net/geany/?rev=3658&view=rev Author: ntrel Date: 2009-03-27 14:06:11 +0000 (Fri, 27 Mar 2009)
Log Message: ----------- Sort open dialog filetypes in combo boxes by title. Add filetypes_foreach_sorted(), filetypes_find().
Modified Paths: -------------- branches/reorder-filetypes/ChangeLog branches/reorder-filetypes/src/dialogs.c branches/reorder-filetypes/src/filetypes.c branches/reorder-filetypes/src/filetypes.h
Modified: branches/reorder-filetypes/ChangeLog =================================================================== --- branches/reorder-filetypes/ChangeLog 2009-03-26 23:48:12 UTC (rev 3657) +++ branches/reorder-filetypes/ChangeLog 2009-03-27 14:06:11 UTC (rev 3658) @@ -1,3 +1,10 @@ +2009-03-27 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com> + + * src/dialogs.c, src/filetypes.c, src/filetypes.h: + Sort open dialog filetypes in combo boxes by title. + Add filetypes_foreach_sorted(), filetypes_find(). + + 2009-03-20 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
* src/highlighting.c, src/dialogs.c, src/plugindata.h,
Modified: branches/reorder-filetypes/src/dialogs.c =================================================================== --- branches/reorder-filetypes/src/dialogs.c 2009-03-26 23:48:12 UTC (rev 3657) +++ branches/reorder-filetypes/src/dialogs.c 2009-03-27 14:06:11 UTC (rev 3658) @@ -71,6 +71,14 @@
#if ! GEANY_USE_WIN32_DIALOG +static gboolean cmp_ft_title(gconstpointer pft, gconstpointer user_data) +{ + const GeanyFiletype *ft = pft; + + return (strcmp(ft->title, user_data) == 0); +} + + static void on_file_open_dialog_response (GtkDialog *dialog, gint response, @@ -81,8 +89,8 @@ if (response == GTK_RESPONSE_ACCEPT || response == GEANY_RESPONSE_VIEW) { GSList *filelist; - gint filetype_idx = gtk_combo_box_get_active(GTK_COMBO_BOX( - ui_lookup_widget(GTK_WIDGET(dialog), "filetype_combo"))); + GtkComboBox *filecombo = GTK_COMBO_BOX( + ui_lookup_widget(GTK_WIDGET(dialog), "filetype_combo")); gint encoding_idx = gtk_combo_box_get_active(GTK_COMBO_BOX( ui_lookup_widget(GTK_WIDGET(dialog), "encoding_combo"))); GeanyFiletype *ft = NULL; @@ -90,8 +98,12 @@ gboolean ro = (response == GEANY_RESPONSE_VIEW); /* View clicked */
/* ignore detect from file item */ - if (filetype_idx > 0 && filetype_idx < GEANY_MAX_BUILT_IN_FILETYPES) - ft = filetypes[filetype_idx]; + if (gtk_combo_box_get_active(filecombo) > 0) + { + gchar *title = gtk_combo_box_get_active_text(filecombo); + ft = filetypes_find(cmp_ft_title, title); + g_free(title); + } if (encoding_idx >= 0 && encoding_idx < GEANY_ENCODINGS_MAX) charset = encodings[encoding_idx].charset;
@@ -171,6 +183,17 @@
#if ! GEANY_USE_WIN32_DIALOG +static void add_opendlg_filetype(gpointer pft, gpointer filetype_combo) +{ + GeanyFiletype *ft = pft; + + gtk_combo_box_append_text(GTK_COMBO_BOX(filetype_combo), ft->title); + + gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(ui_widgets.open_filesel), + filetypes_create_file_filter(ft)); +} + + static void create_open_file_dialog(void) { GtkWidget *filetype_combo, *encoding_combo; @@ -214,12 +237,7 @@ /* now create meta filter "All Source" */ gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(ui_widgets.open_filesel), filetypes_create_file_filter_all_source()); - for (i = 1; i < filetypes_array->len; i++) - { - gtk_combo_box_append_text(GTK_COMBO_BOX(filetype_combo), filetypes[i]->title); - gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(ui_widgets.open_filesel), - filetypes_create_file_filter(filetypes[i])); - } + filetypes_foreach_sorted(add_opendlg_filetype, filetype_combo); gtk_combo_box_set_active(GTK_COMBO_BOX(filetype_combo), 0);
/* fill encoding combo box */
Modified: branches/reorder-filetypes/src/filetypes.c =================================================================== --- branches/reorder-filetypes/src/filetypes.c 2009-03-26 23:48:12 UTC (rev 3657) +++ branches/reorder-filetypes/src/filetypes.c 2009-03-27 14:06:11 UTC (rev 3658) @@ -712,11 +712,8 @@ }
-typedef gboolean FileTypesPredicate(GeanyFiletype *ft, gpointer user_data); - /* Find a filetype that predicate returns TRUE for, otherwise return NULL. */ -static GeanyFiletype *filetypes_find(gboolean source_only, - FileTypesPredicate predicate, gpointer user_data) +GeanyFiletype *filetypes_find(GCompareFunc predicate, gpointer user_data) { guint i;
@@ -724,9 +721,6 @@ { GeanyFiletype *ft = filetypes[i];
- if (source_only && i == GEANY_FILETYPES_NONE) - continue; /* None is not for source files */ - if (predicate(ft, user_data)) return ft; } @@ -734,12 +728,16 @@ }
-static gboolean match_basename(GeanyFiletype *ft, gpointer user_data) +static gboolean match_basename(gconstpointer pft, gconstpointer user_data) { + const GeanyFiletype *ft = pft; const gchar *base_filename = user_data; gint j; gboolean ret = FALSE;
+ if (ft->id == GEANY_FILETYPES_NONE) + return FALSE; + for (j = 0; ft->pattern[j] != NULL; j++) { GPatternSpec *pattern = g_pattern_spec_new(ft->pattern[j]); @@ -770,7 +768,7 @@ setptr(base_filename, g_utf8_strdown(base_filename, -1)); #endif
- ft = filetypes_find(TRUE, match_basename, base_filename); + ft = filetypes_find(match_basename, base_filename); if (ft == NULL) ft = filetypes[GEANY_FILETYPES_NONE];
@@ -1480,3 +1478,31 @@ }
+static gint cmp_filetype(gconstpointer pft1, gconstpointer pft2) +{ + const GeanyFiletype *ft1 = pft1, *ft2 = pft2; + + return utils_str_casecmp(ft1->name, ft2->name); +} + + +/* TODO: insert when adding filetypes properly */ +static GSList *sorted_filetypes = NULL; + +/* Does not include GEANY_FILETYPES_NONE, as this is usually treated specially. */ +void filetypes_foreach_sorted(GFunc callback, gpointer user_data) +{ + guint i; + GSList *item; + + if (sorted_filetypes == NULL) + { + for (i = 1; i < filetypes_array->len; i++) + sorted_filetypes = g_slist_insert_sorted(sorted_filetypes, filetypes[i], + cmp_filetype); + } + + for (item = sorted_filetypes; item != NULL; item = g_slist_next(item)) + callback(item->data, user_data); +} +
Modified: branches/reorder-filetypes/src/filetypes.h =================================================================== --- branches/reorder-filetypes/src/filetypes.h 2009-03-26 23:48:12 UTC (rev 3657) +++ branches/reorder-filetypes/src/filetypes.h 2009-03-27 14:06:11 UTC (rev 3658) @@ -146,7 +146,11 @@
GeanyFiletype *filetypes_lookup_by_name(const gchar *name);
+void filetypes_foreach_sorted(GFunc callback, gpointer user_data);
+GeanyFiletype *filetypes_find(GCompareFunc predicate, gpointer user_data); + + void filetypes_init(void);
void filetypes_init_types(void);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.