From 68928213dfaac772ee5baf7adbc0264df9639fc7 Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Tue, 31 Mar 2009 15:07:27 +0000 Subject: [PATCH] Use g_slist_nth_data() with sorted_filetypes list instead of filetypes_find() as it is neater. git-svn-id: https://geany.svn.sourceforge.net/svnroot/geany/branches/reorder-filetypes@3673 ea778897-0a13-0410-b9d1-a72fbfd435f5 --- ChangeLog | 3 +++ src/dialogs.c | 42 ++++++++++++++---------------------------- src/filetypes.c | 4 +++- 3 files changed, 20 insertions(+), 29 deletions(-) diff --git a/ChangeLog b/ChangeLog index bd151af5..b9b2cb14 100644 --- a/ChangeLog +++ b/ChangeLog @@ -7,6 +7,9 @@ Make sorted_filetypes list public, and include None filetype first. Rename filetypes_foreach_sorted() to filetypes_foreach_named() to show it doesn't include the None filetype. + * src/dialogs.c, src/filetypes.c: + Use g_slist_nth_data() with sorted_filetypes list instead of + filetypes_find() as it is neater. 2009-03-27 Nick Treleaven diff --git a/src/dialogs.c b/src/dialogs.c index 66d36841..7124e933 100644 --- a/src/dialogs.c +++ b/src/dialogs.c @@ -71,14 +71,6 @@ static GtkWidget *add_file_open_extra_widget(void); #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, @@ -89,8 +81,8 @@ on_file_open_dialog_response (GtkDialog *dialog, if (response == GTK_RESPONSE_ACCEPT || response == GEANY_RESPONSE_VIEW) { GSList *filelist; - GtkComboBox *filecombo = GTK_COMBO_BOX( - ui_lookup_widget(GTK_WIDGET(dialog), "filetype_combo")); + gint filetype_idx = gtk_combo_box_get_active(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; @@ -98,12 +90,8 @@ on_file_open_dialog_response (GtkDialog *dialog, gboolean ro = (response == GEANY_RESPONSE_VIEW); /* View clicked */ /* ignore detect from file item */ - 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 (filetype_idx > 0 && filetype_idx < GEANY_MAX_BUILT_IN_FILETYPES) + ft = g_slist_nth_data(sorted_filetypes, filetype_idx); if (encoding_idx >= 0 && encoding_idx < GEANY_ENCODINGS_MAX) charset = encodings[encoding_idx].charset; @@ -183,23 +171,14 @@ on_file_open_check_hidden_toggled (GtkToggleButton *togglebutton, #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; GtkWidget *viewbtn; guint i; gchar *encoding_string; + GeanyFiletype *ft; + GSList *node; ui_widgets.open_filesel = gtk_file_chooser_dialog_new(_("Open File"), GTK_WINDOW(main_widgets.window), GTK_FILE_CHOOSER_ACTION_OPEN, NULL, NULL); @@ -237,7 +216,14 @@ static void create_open_file_dialog(void) /* now create meta filter "All Source" */ gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(ui_widgets.open_filesel), filetypes_create_file_filter_all_source()); - filetypes_foreach_named(add_opendlg_filetype, filetype_combo); + foreach_slist(ft, node, sorted_filetypes) + { + if (ft->id == GEANY_FILETYPES_NONE) + continue; + 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)); + } gtk_combo_box_set_active(GTK_COMBO_BOX(filetype_combo), 0); /* fill encoding combo box */ diff --git a/src/filetypes.c b/src/filetypes.c index 764abe68..0e8cd01c 100644 --- a/src/filetypes.c +++ b/src/filetypes.c @@ -70,7 +70,9 @@ GPtrArray *filetypes_array = NULL; /* Dynamic array of filetype pointers */ static GHashTable *filetypes_hash = NULL; /* Hash of filetype pointers based on name keys */ /* List of filetype pointers sorted by name, with ft[GEANY_FILETYPES_NONE] first, as this - * is usually treated specially. */ + * is usually treated specially. + * The list does not change after filetypes have been initialized, so you can use + * @code g_slist_nth_data(sorted_filetypes, n) @endcode and expect the same result at different times. */ GSList *sorted_filetypes = NULL;