Skip to content

Commit

Permalink
Merge few more vdlist/vdtree functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurent Monin committed Apr 16, 2008
1 parent 87388b5 commit 0313a63
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 145 deletions.
115 changes: 93 additions & 22 deletions src/view_dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "layout_image.h"
#include "layout_util.h"
#include "ui_fileops.h"
#include "ui_tree_edit.h"
#include "ui_menu.h"
#include "utilops.h"
#include "view_dir_list.h"
Expand Down Expand Up @@ -130,26 +131,106 @@ const gchar *vd_row_get_path(ViewDir *vd, gint row)
return ret;
}

void vd_color_set(ViewDir *vd, FileData *fd, gint color_set)
gint vd_find_row(ViewDir *vd, FileData *fd, GtkTreeIter *iter)
{
gint ret = FALSE;

switch(vd->type)
{
case DIRVIEW_LIST: ret = vdlist_find_row(vd, fd, iter); break;
case DIRVIEW_TREE: ret = vdtree_find_row(vd, fd, iter, NULL); break;
}

return ret;
}

static gint vd_rename_cb(TreeEditData *td, const gchar *old, const gchar *new, gpointer data)
{
ViewDir *vd = data;
GtkTreeModel *store;
GtkTreeIter iter;
FileData *fd;
gchar *old_path;
gchar *new_path;
gchar *base;

store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
if (!gtk_tree_model_get_iter(store, &iter, td->path)) return FALSE;

switch(vd->type)
{
case DIRVIEW_LIST:
gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1);
break;
case DIRVIEW_TREE:
{
if (vdlist_find_row(vd, fd, &iter) < 0) return;
store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
gtk_list_store_set(GTK_LIST_STORE(store), &iter, DIR_COLUMN_COLOR, color_set, -1);
NodeData *nd;
gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &nd, -1);
if (!nd) return FALSE;
fd = nd->fd;
};
break;
}

if (!fd) return FALSE;

old_path = g_strdup(fd->path);

base = remove_level_from_path(old_path);
new_path = concat_dir_and_file(base, new);
g_free(base);

if (file_util_rename_dir(fd, new_path, vd->view))
{

if (vd->type == DIRVIEW_TREE) vdtree_populate_path(vd, new_path, TRUE, TRUE);
if (vd->layout && strcmp(vd->path, old_path) == 0)
{
layout_set_path(vd->layout, new_path);
}
else
{
if (vd->type == DIRVIEW_LIST) vd_refresh(vd);
}
}

g_free(old_path);
g_free(new_path);

return FALSE;
}

static void vd_rename_by_data(ViewDir *vd, FileData *fd)
{
GtkTreeModel *store;
GtkTreePath *tpath;
GtkTreeIter iter;

if (!fd || vd_find_row(vd, fd, &iter) < 0) return;
store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
tpath = gtk_tree_model_get_path(store, &iter);

tree_edit_by_path(GTK_TREE_VIEW(vd->view), tpath, 0, fd->name,
vd_rename_cb, vd);
gtk_tree_path_free(tpath);
}


void vd_color_set(ViewDir *vd, FileData *fd, gint color_set)
{
GtkTreeModel *store;
GtkTreeIter iter;

if (vd_find_row(vd, fd, &iter) < 0) return;
store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));

switch(vd->type)
{
case DIRVIEW_LIST:
gtk_list_store_set(GTK_LIST_STORE(store), &iter, DIR_COLUMN_COLOR, color_set, -1);
break;
case DIRVIEW_TREE:
{
if (vdtree_find_row(vd, fd, &iter, NULL) < 0) return;
store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
gtk_tree_store_set(GTK_TREE_STORE(store), &iter, DIR_COLUMN_COLOR, color_set, -1);
}
break;
}
}
Expand Down Expand Up @@ -390,27 +471,21 @@ static void vd_pop_menu_new_cb(GtkWidget *widget, gpointer data)
}
else
{
FileData *fd = NULL;

switch(vd->type)
{
case DIRVIEW_LIST:
{
FileData *fd;

vd_refresh(vd);
fd = vdlist_row_by_path(vd, new_path, NULL);

vdlist_rename_by_row(vd, fd);
};
break;
case DIRVIEW_TREE:
{
FileData *fd;

fd = vdtree_populate_path(vd, new_path, TRUE, TRUE);
vdtree_rename_by_data(vd, fd);
};
break;
}
vd_rename_by_data(vd, fd);
}

g_free(new_path);
Expand All @@ -420,11 +495,7 @@ static void vd_pop_menu_rename_cb(GtkWidget *widget, gpointer data)
{
ViewDir *vd = data;

switch(vd->type)
{
case DIRVIEW_LIST: vdlist_rename_by_row(vd, vd->click_fd); break;
case DIRVIEW_TREE: vdtree_rename_by_data(vd, vd->click_fd); break;
}
vd_rename_by_data(vd, vd->click_fd);
}

GtkWidget *vd_pop_menu(ViewDir *vd, FileData *fd)
Expand Down
1 change: 1 addition & 0 deletions src/view_dir.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ void vd_set_layout(ViewDir *vdl, LayoutWindow *layout);

gint vd_set_path(ViewDir *vdl, const gchar *path);
void vd_refresh(ViewDir *vdl);
gint vd_find_row(ViewDir *vd, FileData *fd, GtkTreeIter *iter);

const gchar *vd_row_get_path(ViewDir *vdl, gint row);

Expand Down
58 changes: 3 additions & 55 deletions src/view_dir_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,58 +62,6 @@ gint vdlist_find_row(ViewDir *vd, FileData *fd, GtkTreeIter *iter)
return -1;
}

static gint vdlist_rename_row_cb(TreeEditData *td, const gchar *old, const gchar *new, gpointer data)
{
ViewDir *vd = data;
GtkTreeModel *store;
GtkTreeIter iter;
FileData *fd;
gchar *old_path;
gchar *new_path;
gchar *base;

store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
if (!gtk_tree_model_get_iter(store, &iter, td->path)) return FALSE;
gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1);
if (!fd) return FALSE;

old_path = g_strdup(fd->path);

base = remove_level_from_path(old_path);
new_path = concat_dir_and_file(base, new);
g_free(base);

if (file_util_rename_dir(fd, new_path, vd->view))
{
if (vd->layout && strcmp(vd->path, old_path) == 0)
{
layout_set_path(vd->layout, new_path);
}
else
{
vdlist_refresh(vd);
}
}

g_free(old_path);
g_free(new_path);
return FALSE;
}

void vdlist_rename_by_row(ViewDir *vd, FileData *fd)
{
GtkTreeModel *store;
GtkTreePath *tpath;
GtkTreeIter iter;

if (vdlist_find_row(vd, fd, &iter) < 0) return;
store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
tpath = gtk_tree_model_get_path(store, &iter);

tree_edit_by_path(GTK_TREE_VIEW(vd->view), tpath, 0, fd->name,
vdlist_rename_row_cb, vd);
gtk_tree_path_free(tpath);
}

FileData *vdlist_row_by_path(ViewDir *vd, const gchar *path, gint *row)
{
Expand Down Expand Up @@ -274,7 +222,7 @@ static gint vdlist_get_row_visibility(ViewDir *vd, FileData *fd)
GdkRectangle vrect;
GdkRectangle crect;

if (!fd || vdlist_find_row(vd, fd, &iter) < 0) return 0;
if (!fd || vd_find_row(vd, fd, &iter) < 0) return 0;

column = gtk_tree_view_get_column(GTK_TREE_VIEW(vd->view), 0);
store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
Expand All @@ -296,7 +244,7 @@ static void vdlist_scroll_to_row(ViewDir *vd, FileData *fd, gfloat y_align)
GtkTreeIter iter;

if (GTK_WIDGET_REALIZED(vd->view) &&
vdlist_find_row(vd, fd, &iter) >= 0)
vd_find_row(vd, fd, &iter) >= 0)
{
GtkTreeModel *store;
GtkTreePath *tpath;
Expand Down Expand Up @@ -613,7 +561,7 @@ static void vdlist_menu_position_cb(GtkMenu *menu, gint *x, gint *y, gboolean *p
GtkTreePath *tpath;
gint cw, ch;

if (vdlist_find_row(vd, vd->click_fd, &iter) < 0) return;
if (vd_find_row(vd, vd->click_fd, &iter) < 0) return;
store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
tpath = gtk_tree_model_get_path(store, &iter);
tree_view_get_cell_clamped(GTK_TREE_VIEW(vd->view), tpath, 0, TRUE, x, y, &cw, &ch);
Expand Down
75 changes: 7 additions & 68 deletions src/view_dir_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@ struct _PathData
FileData *node;
};

typedef struct _NodeData NodeData;
struct _NodeData
{
FileData *fd;
gint expanded;
time_t last_update;
};


static gint vdtree_populate_path_by_iter(ViewDir *vd, GtkTreeIter *iter, gint force, const gchar *target_path);
Expand Down Expand Up @@ -154,66 +147,12 @@ static void vdtree_expand_by_data(ViewDir *vd, FileData *fd, gint expand)
{
GtkTreeIter iter;

if (vdtree_find_row(vd, fd, &iter, NULL))
if (vd_find_row(vd, fd, &iter))
{
vdtree_expand_by_iter(vd, &iter, expand);
}
}

static gint vdtree_rename_row_cb(TreeEditData *td, const gchar *old, const gchar *new, gpointer data)
{
ViewDir *vd = data;
GtkTreeModel *store;
GtkTreeIter iter;
NodeData *nd;
gchar *old_path;
gchar *new_path;
gchar *base;

store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
if (!gtk_tree_model_get_iter(store, &iter, td->path)) return FALSE;
gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &nd, -1);
if (!nd) return FALSE;

old_path = g_strdup(nd->fd->path);

base = remove_level_from_path(old_path);
new_path = concat_dir_and_file(base, new);
g_free(base);

if (file_util_rename_dir(nd->fd, new_path, vd->view))
{
vdtree_populate_path(vd, new_path, TRUE, TRUE);

if (vd->layout && strcmp(vd->path, old_path) == 0)
{
layout_set_path(vd->layout, new_path);
}
}

g_free(old_path);
g_free(new_path);

return FALSE;
}

void vdtree_rename_by_data(ViewDir *vd, FileData *fd)
{
GtkTreeModel *store;
GtkTreePath *tpath;
GtkTreeIter iter;

if (!fd ||
!vdtree_find_row(vd, fd, &iter, NULL)) return;

store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
tpath = gtk_tree_model_get_path(store, &iter);

tree_edit_by_path(GTK_TREE_VIEW(vd->view), tpath, 0, fd->name,
vdtree_rename_row_cb, vd);
gtk_tree_path_free(tpath);
}

static void vdtree_node_free(NodeData *nd)
{
if (!nd) return;
Expand Down Expand Up @@ -347,7 +286,7 @@ static gint vdtree_dnd_drop_expand_cb(gpointer data)
GtkTreeIter iter;

if (vd->drop_fd &&
vdtree_find_row(vd, vd->drop_fd, &iter, NULL))
vd_find_row(vd, vd->drop_fd, &iter))
{
vdtree_populate_path_by_iter(vd, &iter, FALSE, vd->path);
vdtree_expand_by_data(vd, vd->drop_fd, TRUE);
Expand Down Expand Up @@ -900,7 +839,7 @@ FileData *vdtree_populate_path(ViewDir *vd, const gchar *path, gint expand, gint

parent_pd = work->prev->data;

if (!vdtree_find_row(vd, parent_pd->node, &parent_iter, NULL) ||
if (!vd_find_row(vd, parent_pd->node, &parent_iter) ||
!vdtree_populate_path_by_iter(vd, &parent_iter, force, path) ||
(nd = vdtree_find_iter_by_name(vd, &parent_iter, pd->name, &iter)) == NULL)
{
Expand All @@ -926,7 +865,7 @@ FileData *vdtree_populate_path(ViewDir *vd, const gchar *path, gint expand, gint
{
GtkTreeIter iter;

if (vdtree_find_row(vd, pd->node, &iter, NULL))
if (vd_find_row(vd, pd->node, &iter))
{
if (expand) vdtree_expand_by_iter(vd, &iter, TRUE);
vdtree_populate_path_by_iter(vd, &iter, force, path);
Expand Down Expand Up @@ -968,7 +907,7 @@ static void vdtree_select_row(ViewDir *vd, FileData *fd)
GtkTreeSelection *selection;
GtkTreeIter iter;

if (!vdtree_find_row(vd, fd, &iter, NULL)) return;
if (!vd_find_row(vd, fd, &iter)) return;
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vd->view));

/* hack, such that selection is only allowed to be changed from here */
Expand Down Expand Up @@ -1001,7 +940,7 @@ gint vdtree_set_path(ViewDir *vd, const gchar *path)

if (!fd) return FALSE;

if (vdtree_find_row(vd, fd, &iter, NULL))
if (vd_find_row(vd, fd, &iter))
{
GtkTreeModel *store;
GtkTreePath *tpath;
Expand Down Expand Up @@ -1051,7 +990,7 @@ static void vdtree_menu_position_cb(GtkMenu *menu, gint *x, gint *y, gboolean *p
GtkTreePath *tpath;
gint cw, ch;

if (vdtree_find_row(vd, vd->click_fd, &iter, NULL) < 0) return;
if (vd_find_row(vd, vd->click_fd, &iter) < 0) return;
store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
tpath = gtk_tree_model_get_path(store, &iter);
tree_view_get_cell_clamped(GTK_TREE_VIEW(vd->view), tpath, 0, TRUE, x, y, &cw, &ch);
Expand Down
Loading

0 comments on commit 0313a63

Please sign in to comment.