Skip to content

Commit

Permalink
improved bar_sort configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
nadvornik committed Feb 24, 2009
1 parent da109aa commit b347730
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 76 deletions.
81 changes: 66 additions & 15 deletions src/bar_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "ui_fileops.h"
#include "ui_menu.h"
#include "ui_misc.h"
#include "rcfile.h"


/*
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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("");
}
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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: */
4 changes: 3 additions & 1 deletion src/bar_sort.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
#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: */
16 changes: 2 additions & 14 deletions src/layout.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "metadata.h"
#include "rcfile.h"
#include "bar.h"
#include "bar_sort.h"

#ifdef HAVE_LIRC
#include "lirc.h"
Expand Down Expand Up @@ -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);
}


Expand All @@ -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");
Expand Down Expand Up @@ -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);
}

Expand Down
68 changes: 43 additions & 25 deletions src/layout_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/*
*-----------------------------------------------------------------------------
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/layout_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 0 additions & 8 deletions src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions src/rcfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit b347730

Please sign in to comment.