improved bar_sort configuration
authorVladimir Nadvornik <nadvornik@suse.cz>
Tue, 24 Feb 2009 22:21:28 +0000 (22:21 +0000)
committerVladimir Nadvornik <nadvornik@suse.cz>
Tue, 24 Feb 2009 22:21:28 +0000 (22:21 +0000)
src/bar_sort.c
src/bar_sort.h
src/layout.c
src/layout_util.c
src/layout_util.h
src/options.c
src/rcfile.c
src/typedefs.h

index 12f0ac3..0bd5681 100644 (file)
@@ -26,6 +26,7 @@
 #include "ui_fileops.h"
 #include "ui_menu.h"
 #include "ui_misc.h"
+#include "rcfile.h"
 
 
 /*
@@ -171,7 +172,6 @@ static void bar_sort_mode_cb(GtkWidget *combo, gpointer data)
                {
                bar_sort_mode_sync(sd, BAR_SORT_MODE_COLLECTION);
                }
-       options->layout.panels.sort.mode_state = sd->mode;
 }
 
 /* this takes control of src_list */
@@ -351,19 +351,15 @@ static void bar_sort_bookmark_select(const gchar *path, gpointer data)
 
 static void bar_sort_set_action(SortData *sd, SortActionType action, const gchar *filter_key)
 {
-       options->layout.panels.sort.action_state = sd->action = action;
+       sd->action = action;
        if (action == BAR_SORT_FILTER)
                {
                if (!filter_key) filter_key = "";
                sd->filter_key = filter_key;
-               g_free(options->layout.panels.sort.action_filter);
-               options->layout.panels.sort.action_filter = g_strdup(filter_key);
                }
        else
                {
                sd->filter_key = NULL;
-               g_free(options->layout.panels.sort.action_filter);
-               options->layout.panels.sort.action_filter = g_strdup("");
                }
 }
 
@@ -393,7 +389,7 @@ static void bar_sort_set_filter_cb(GtkWidget *button, gpointer data)
 
 static void bar_sort_set_selection(SortData *sd, SortSelectionType selection)
 {
-       options->layout.panels.sort.selection_state = sd->selection = selection;
+       sd->selection = selection;
 }
 
 static void bar_sort_set_selection_image_cb(GtkWidget *button, gpointer data)
@@ -555,14 +551,13 @@ static void bar_sort_destroy(GtkWidget *widget, gpointer data)
        g_free(sd);
 }
 
-GtkWidget *bar_sort_new(LayoutWindow *lw)
+static GtkWidget *bar_sort_new(LayoutWindow *lw, SortActionType action, SortModeType mode, SortSelectionType selection, const gchar *filter_key)
 {
        SortData *sd;
        GtkWidget *buttongrp;
        GtkWidget *label;
        GtkWidget *tbar;
        GtkWidget *combo;
-       SortModeType mode;
        GList *editors_list, *work;
        gboolean have_filter;
 
@@ -572,14 +567,14 @@ GtkWidget *bar_sort_new(LayoutWindow *lw)
 
        sd->lw = lw;
 
-       mode = CLAMP(options->layout.panels.sort.mode_state, 0, BAR_SORT_MODE_COUNT - 1);
-       sd->action = CLAMP(options->layout.panels.sort.action_state, 0, BAR_SORT_ACTION_COUNT - 1);
+       sd->action = action;
        
-       if (sd->action == BAR_SORT_FILTER && 
-           (!options->layout.panels.sort.action_filter || !options->layout.panels.sort.action_filter[0]))
+       if (sd->action == BAR_SORT_FILTER && (!filter_key || !filter_key[0]))
+               {
                sd->action = BAR_SORT_COPY;
+               }
        
-       sd->selection = CLAMP(options->layout.panels.sort.selection_state, 0, BAR_SORT_SELECTION_COUNT - 1);
+       sd->selection = selection;
        sd->undo_src = NULL;
        sd->undo_dest = NULL;
 
@@ -625,7 +620,7 @@ GtkWidget *bar_sort_new(LayoutWindow *lw)
                
                if (!editor_is_filter(editor->key)) continue;
 
-               if (sd->action == BAR_SORT_FILTER && strcmp(editor->key, options->layout.panels.sort.action_filter) == 0)
+               if (sd->action == BAR_SORT_FILTER && strcmp(editor->key, filter_key) == 0)
                        {
                        bar_sort_set_action(sd, sd->action, editor->key);
                        select = TRUE;
@@ -671,4 +666,60 @@ GtkWidget *bar_sort_new(LayoutWindow *lw)
 
        return sd->vbox;
 }
+
+GtkWidget *bar_sort_new_from_config(LayoutWindow *lw, const gchar **attribute_names, const gchar **attribute_values)
+{
+       GtkWidget *bar;
+       
+       gboolean enabled = TRUE;
+       gint action = 0;
+       gint mode = 0;
+       gint selection = 0;
+       gchar *filter_key = NULL;
+
+       while (attribute_names && *attribute_names)
+               {
+               const gchar *option = *attribute_names++;
+               const gchar *value = *attribute_values++;
+
+               if (READ_BOOL_FULL("enabled", enabled)) continue;
+               if (READ_INT_CLAMP_FULL("action", action, 0, BAR_SORT_ACTION_COUNT - 1)) continue;
+               if (READ_INT_CLAMP_FULL("mode", mode, 0, BAR_SORT_MODE_COUNT - 1)) continue;
+               if (READ_INT_CLAMP_FULL("selection", selection, 0, BAR_SORT_SELECTION_COUNT - 1)) continue;
+               if (READ_CHAR_FULL("filter_key", filter_key)) continue;
+
+               DEBUG_1("unknown attribute %s = %s", option, value);
+               }
+       bar = bar_sort_new(lw, action, mode, selection, filter_key);
+
+       g_free(filter_key);
+       if (enabled) gtk_widget_show(bar);
+       return bar;
+}
+
+GtkWidget *bar_sort_new_default(LayoutWindow *lw)
+{
+       return bar_sort_new_from_config(lw, NULL, NULL);
+}
+
+void bar_sort_write_config(GtkWidget *bar, GString *outstr, gint indent)
+{
+       SortData *sd;
+
+       if (!bar) return;
+       sd = g_object_get_data(G_OBJECT(bar), "bar_sort_data");
+       if (!sd) return;
+
+       WRITE_STRING("<bar_sort\n");
+       indent++;
+       write_bool_option(outstr, indent, "enabled", GTK_WIDGET_VISIBLE(bar));
+       WRITE_INT(*sd, mode);
+       WRITE_INT(*sd, action);
+       WRITE_INT(*sd, selection);
+       WRITE_CHAR(*sd, filter_key);
+       indent--;
+       WRITE_STRING("/>\n");
+}
+
+
 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */
index 2f928d3..4dffac3 100644 (file)
 #define BAR_SORT_H
 
 
-GtkWidget *bar_sort_new(LayoutWindow *lw);
+GtkWidget *bar_sort_new_default(LayoutWindow *lw);
+GtkWidget *bar_sort_new_from_config(LayoutWindow *lw, const gchar **attribute_names, const gchar **attribute_values);
 void bar_sort_close(GtkWidget *bar);
 
+void bar_sort_write_config(GtkWidget *bar, GString *outstr, gint indent);
 
 #endif
 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */
index 43c58db..3f14157 100644 (file)
@@ -35,6 +35,7 @@
 #include "metadata.h"
 #include "rcfile.h"
 #include "bar.h"
+#include "bar_sort.h"
 
 #ifdef HAVE_LIRC
 #include "lirc.h"
@@ -2157,12 +2158,6 @@ void layout_write_attributes(LayoutOptions *layout, GString *outstr, gint indent
        WRITE_SEPARATOR();
 
        WRITE_BOOL(*layout, toolbar_hidden);
-
-       WRITE_BOOL(*layout, panels.sort.enabled);
-       WRITE_INT(*layout, panels.sort.action_state);
-       WRITE_INT(*layout, panels.sort.mode_state);
-       WRITE_INT(*layout, panels.sort.selection_state);
-       WRITE_CHAR(*layout, panels.sort.action_filter);
 }
 
 
@@ -2173,6 +2168,7 @@ void layout_write_config(LayoutWindow *lw, GString *outstr, gint indent)
        layout_write_attributes(&lw->options, outstr, indent + 1);
        WRITE_STRING(">\n");
 
+       bar_sort_write_config(lw->bar_sort, outstr, indent + 1);
        bar_write_config(lw->bar, outstr, indent + 1);
        
        WRITE_STRING("</layout>\n");
@@ -2224,14 +2220,6 @@ void layout_load_attributes(LayoutOptions *layout, const gchar **attribute_names
                if (READ_BOOL(*layout, tools_restore_state)) continue;
                if (READ_BOOL(*layout, toolbar_hidden)) continue;
 
-               /* panels */
-               if (READ_BOOL(*layout, panels.sort.enabled)) continue;
-               if (READ_INT(*layout, panels.sort.action_state)) continue;
-               if (READ_INT(*layout, panels.sort.mode_state)) continue;
-               if (READ_INT(*layout, panels.sort.selection_state)) continue;
-               if (READ_CHAR(*layout, panels.sort.action_filter)) continue;
-
-
                DEBUG_1("unknown attribute %s = %s", option, value);
                }
 
index 95aa3d9..ba6d702 100644 (file)
@@ -50,6 +50,7 @@
 #define MENU_EDIT_ACTION_OFFSET 16
 
 static gboolean layout_bar_enabled(LayoutWindow *lw);
+static gboolean layout_bar_sort_enabled(LayoutWindow *lw);
 
 /*
  *-----------------------------------------------------------------------------
@@ -688,7 +689,7 @@ static void layout_menu_bar_sort_cb(GtkToggleAction *action, gpointer data)
 {
        LayoutWindow *lw = data;
 
-       if (lw->options.panels.sort.enabled == gtk_toggle_action_get_active(action)) return;
+       if (layout_bar_sort_enabled(lw) == gtk_toggle_action_get_active(action)) return;
 
        layout_exit_fullscreen(lw);
        layout_bar_sort_toggle(lw);
@@ -1867,7 +1868,7 @@ static void layout_util_sync_views(LayoutWindow *lw)
        gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), layout_bar_enabled(lw));
 
        action = gtk_action_group_get_action(lw->action_group, "SBarSort");
-       gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), lw->options.panels.sort.enabled);
+       gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), layout_bar_sort_enabled(lw));
 
        action = gtk_action_group_get_action(lw->action_group, "HideToolbar");
        gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), lw->options.toolbar_hidden);
@@ -2040,32 +2041,35 @@ static void layout_bar_new_selection(LayoutWindow *lw, gint count)
 //     bar_info_selection(lw->bar_info, count - 1);
 }
 
+static gboolean layout_bar_sort_enabled(LayoutWindow *lw)
+{
+       return lw->bar_sort && GTK_WIDGET_VISIBLE(lw->bar_sort);
+}
+
+
 static void layout_bar_sort_destroyed(GtkWidget *widget, gpointer data)
 {
        LayoutWindow *lw = data;
 
        lw->bar_sort = NULL;
 
-       if (lw->utility_box)
-               {
-               /* destroyed from within itself */
-               lw->options.panels.sort.enabled = FALSE;
+/* 
+    do not call layout_util_sync_views(lw) here
+    this is called either when whole layout is destroyed - no need for update
+    or when the bar is replaced - sync is called by upper function at the end of whole operation
 
-               layout_util_sync_views(lw);
-               }
+*/
 }
 
-static void layout_bar_sort_new(LayoutWindow *lw)
+static void layout_bar_sort_set_default(LayoutWindow *lw)
 {
+       GtkWidget *bar;
+       
        if (!lw->utility_box) return;
 
-       lw->bar_sort = bar_sort_new(lw);
-       g_signal_connect(G_OBJECT(lw->bar_sort), "destroy",
-                        G_CALLBACK(layout_bar_sort_destroyed), lw);
-       lw->options.panels.sort.enabled = TRUE;
-
-       gtk_box_pack_end(GTK_BOX(lw->utility_box), lw->bar_sort, FALSE, FALSE, 0);
-       gtk_widget_show(lw->bar_sort);
+       bar = bar_sort_new_default(lw);
+       
+       layout_bar_sort_set(lw, bar);
 }
 
 static void layout_bar_sort_close(LayoutWindow *lw)
@@ -2075,19 +2079,38 @@ static void layout_bar_sort_close(LayoutWindow *lw)
                bar_sort_close(lw->bar_sort);
                lw->bar_sort = NULL;
                }
-       lw->options.panels.sort.enabled = FALSE;
+}
+
+void layout_bar_sort_set(LayoutWindow *lw, GtkWidget *bar)
+{
+       if (!lw->utility_box) return;
+
+       layout_bar_sort_close(lw); /* if any */
+
+       if (!bar) return;
+       lw->bar_sort = bar;
+
+       g_signal_connect(G_OBJECT(lw->bar_sort), "destroy",
+                        G_CALLBACK(layout_bar_sort_destroyed), lw);
+
+       gtk_box_pack_end(GTK_BOX(lw->utility_box), lw->bar_sort, FALSE, FALSE, 0);
 }
 
 void layout_bar_sort_toggle(LayoutWindow *lw)
 {
-       if (lw->options.panels.sort.enabled)
+       if (layout_bar_sort_enabled(lw))
                {
-               layout_bar_sort_close(lw);
+               gtk_widget_hide(lw->bar_sort);
                }
        else
                {
-               layout_bar_sort_new(lw);
+               if (!lw->bar_sort)
+                       {
+                       layout_bar_sort_set_default(lw);
+                       }
+               gtk_widget_show(lw->bar_sort);
                }
+       layout_util_sync_views(lw);
 }
 
 void layout_bars_new_image(LayoutWindow *lw)
@@ -2112,11 +2135,6 @@ GtkWidget *layout_bars_prepare(LayoutWindow *lw, GtkWidget *image)
        gtk_box_pack_start(GTK_BOX(lw->utility_box), image, TRUE, TRUE, 0);
        gtk_widget_show(image);
 
-       if (lw->options.panels.sort.enabled)
-               {
-               layout_bar_sort_new(lw);
-               }
-
        return lw->utility_box;
 }
 
index a118cd2..7fa3798 100644 (file)
@@ -47,8 +47,8 @@ void folder_icons_free(PixmapFolders *pf);
 void layout_bar_toggle(LayoutWindow *lw);
 void layout_bar_set(LayoutWindow *lw, GtkWidget *bar);
 
-void layout_bar_exif_toggle(LayoutWindow *lw);
 void layout_bar_sort_toggle(LayoutWindow *lw);
+void layout_bar_sort_set(LayoutWindow *lw, GtkWidget *bar);
 
 void layout_bars_new_image(LayoutWindow *lw);
 void layout_bars_new_selection(LayoutWindow *lw, gint count);
index 871229d..c442a61 100644 (file)
@@ -120,12 +120,6 @@ ConfOptions *init_options(ConfOptions *options)
        options->open_recent_list_maxsize = 10;
        options->place_dialogs_under_mouse = FALSE;
 
-       options->layout.panels.sort.action_state = 0;
-       options->layout.panels.sort.enabled = FALSE;
-       options->layout.panels.sort.mode_state = 0;
-       options->layout.panels.sort.selection_state = 0;
-       options->layout.panels.sort.action_filter = NULL;
-       
        options->progressive_key_scrolling = TRUE;
        
        options->metadata.enable_metadata_dirs = FALSE;
@@ -205,14 +199,12 @@ void copy_layout_options(LayoutOptions *dest, const LayoutOptions *src)
        *dest = *src;
        dest->order = g_strdup(src->order);
        dest->home_path = g_strdup(src->home_path);
-       dest->panels.sort.action_filter = g_strdup(src->panels.sort.action_filter);
 }
 
 void free_layout_options_content(LayoutOptions *dest)
 {
        if (dest->order) g_free(dest->order);
        if (dest->home_path) g_free(dest->home_path);
-       if (dest->panels.sort.action_filter) g_free(dest->panels.sort.action_filter);
 }
 
 static void sync_options_with_current_state(ConfOptions *options)
index f8b480b..12cace5 100644 (file)
@@ -21,6 +21,7 @@
 #include "bar_exif.h"
 #include "bar_histogram.h"
 #include "bar_keywords.h"
+#include "bar_sort.h"
 #include "editors.h"
 #include "filefilter.h"
 #include "misc.h"
@@ -881,6 +882,12 @@ static void options_parse_layout(GQParserData *parser_data, GMarkupParseContext
                layout_bar_set(lw, bar);
                options_parse_func_push(parser_data, options_parse_bar, NULL, lw->bar);
                }
+       else if (g_ascii_strcasecmp(element_name, "bar_sort") == 0)
+               {
+               GtkWidget *bar = bar_sort_new_from_config(lw, attribute_names, attribute_values);
+               layout_bar_sort_set(lw, bar);
+               options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
+               }
        else
                {
                DEBUG_1("unexpected in <layout>: <%s>", element_name);
index 57a5e11..27d1933 100644 (file)
@@ -513,18 +513,6 @@ struct _LayoutOptions
        gboolean toolbar_hidden;
        
        gchar *home_path;
-
-       /* panels */
-       struct {
-               struct {
-                       gboolean enabled;
-                       gint mode_state;
-                       gint action_state;
-                       gint selection_state;
-                       gchar *action_filter;
-               } sort;
-       } panels;
-
 };
 
 struct _LayoutWindow