changeset 399:d071a3555011

Reduce code redundancy with the introduction of a new function named vd_get_fd_from_tree_path().
author zas_
date Thu, 17 Apr 2008 18:44:17 +0000
parents c4080362d619
children 35346871af88
files src/view_dir.c
diffstat 1 files changed, 30 insertions(+), 94 deletions(-) [+]
line wrap: on
line diff
--- a/src/view_dir.c	Thu Apr 17 17:44:54 2008 +0000
+++ b/src/view_dir.c	Thu Apr 17 18:44:17 2008 +0000
@@ -154,34 +154,40 @@
 	return ret;
 }
 
+FileData *vd_get_fd_from_tree_path(ViewDir *vd, GtkTreeView *tview, GtkTreePath *tpath)
+{
+	GtkTreeIter iter;
+	FileData *fd = NULL;
+	GtkTreeModel *store;
+
+	store = gtk_tree_view_get_model(tview);
+	gtk_tree_model_get_iter(store, &iter, tpath);
+	switch (vd->type)
+		{
+		case DIRVIEW_LIST:
+			gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1);
+			break;
+		case DIRVIEW_TREE:
+			{
+			NodeData *nd;
+			gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &nd, -1);
+			fd = (nd) ? nd->fd : NULL;
+			};
+			break;
+		}
+
+	return fd;
+}
+
 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:
-		{
-		NodeData *nd;
-		gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &nd, -1);
-		if (!nd) return FALSE;
-		fd = nd->fd;
-		};
-		break;
-	}
-
+	fd = vd_get_fd_from_tree_path(vd, GTK_TREE_VIEW(vd->view), td->path);
 	if (!fd) return FALSE;
 
 	old_path = g_strdup(fd->path);
@@ -669,7 +675,6 @@
 {
 	ViewDir *vd = data;
 	GtkTreePath *tpath;
-	GtkTreeIter iter;
 	FileData *fd = NULL;
 
 	vd->click_fd = NULL;
@@ -677,23 +682,7 @@
 	if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), x, y,
 					  &tpath, NULL, NULL, NULL))
 		{
-		GtkTreeModel *store;
-
-		store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
-		gtk_tree_model_get_iter(store, &iter, tpath);
-		switch (vd->type)
-			{
-			case DIRVIEW_LIST:
-				gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1);
-				break;
-			case DIRVIEW_TREE:
-				{
-				NodeData *nd;
-				gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &nd, -1);
-				fd = (nd) ? nd->fd : NULL;
-				};
-				break;
-			}
+		fd = vd_get_fd_from_tree_path(vd, GTK_TREE_VIEW(widget), tpath);
 		gtk_tree_path_free(tpath);
 		}
 
@@ -721,29 +710,12 @@
 static void vd_drop_update(ViewDir *vd, gint x, gint y)
 {
 	GtkTreePath *tpath;
-	GtkTreeIter iter;
 	FileData *fd = NULL;
 
 	if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vd->view), x, y,
 					  &tpath, NULL, NULL, NULL))
 		{
-		GtkTreeModel *store;
-	
-		store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
-		gtk_tree_model_get_iter(store, &iter, tpath);
-		switch (vd->type)
-			{
-			case DIRVIEW_LIST:
-				gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1);
-				break;
-			case DIRVIEW_TREE:
-				{
-				NodeData *nd;
-				gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &nd, -1);
-				fd = (nd) ? nd->fd : NULL;
-				};
-				break;
-			}
+		fd = vd_get_fd_from_tree_path(vd, GTK_TREE_VIEW(vd->view), tpath);
 		gtk_tree_path_free(tpath);
 		}
 
@@ -884,25 +856,7 @@
 void vd_activate_cb(GtkTreeView *tview, GtkTreePath *tpath, GtkTreeViewColumn *column, gpointer data)
 {
 	ViewDir *vd = data;
-	GtkTreeModel *store;
-	GtkTreeIter iter;
-	FileData *fd;
-
-	store = gtk_tree_view_get_model(tview);
-	gtk_tree_model_get_iter(store, &iter, tpath);
-	switch (vd->type)
-		{
-		case DIRVIEW_LIST:
-			gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1);
-			break;
-		case DIRVIEW_TREE:
-			{
-			NodeData *nd;
-			gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &nd, -1);
-			fd = (nd) ? nd->fd : NULL;
-			};
-			break;
-		}
+	FileData *fd = vd_get_fd_from_tree_path(vd, tview, tpath);
 
 	vd_select_row(vd, fd);
 }
@@ -941,7 +895,6 @@
 {
 	ViewDir *vd = data;
 	GtkTreePath *tpath;
-	GtkTreeIter iter;
 	FileData *fd = NULL;
 
 	vd_color_set(vd, vd->click_fd, FALSE);
@@ -952,24 +905,7 @@
 	    gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y,
 					  &tpath, NULL, NULL, NULL))
 		{
-		GtkTreeModel *store;
-
-		store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
-		gtk_tree_model_get_iter(store, &iter, tpath);
-		switch (vd->type)
-			{
-			case DIRVIEW_LIST:
-				gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1);
-				break;
-			case DIRVIEW_TREE:
-				{
-				NodeData *nd;
-				gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &nd, -1);
-				fd = (nd) ? nd->fd : NULL;
-				};
-				break;
-			}
-		
+		fd = vd_get_fd_from_tree_path(vd, GTK_TREE_VIEW(widget), tpath);
 		gtk_tree_path_free(tpath);
 		}