2005-07-12 16:02:19 +00:00
|
|
|
/* Icon View/Icon View Basics
|
2004-07-04 20:35:35 +00:00
|
|
|
*
|
2008-08-11 19:12:58 +00:00
|
|
|
* The GtkIconView widget is used to display and manipulate icons.
|
|
|
|
* It uses a GtkTreeModel for data storage, so the list store
|
2005-07-12 16:02:19 +00:00
|
|
|
* example might be helpful.
|
2004-07-04 20:35:35 +00:00
|
|
|
*/
|
|
|
|
|
2013-06-21 17:46:54 +00:00
|
|
|
#include <glib/gi18n.h>
|
2004-07-04 20:35:35 +00:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
static GtkWidget *window = NULL;
|
|
|
|
|
2013-01-29 16:21:19 +00:00
|
|
|
#define FOLDER_NAME "/iconview/gnome-fs-directory.png"
|
|
|
|
#define FILE_NAME "/iconview/gnome-fs-regular.png"
|
2004-07-04 20:35:35 +00:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
COL_PATH,
|
|
|
|
COL_DISPLAY_NAME,
|
|
|
|
COL_PIXBUF,
|
|
|
|
COL_IS_DIRECTORY,
|
|
|
|
NUM_COLS
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static GdkPixbuf *file_pixbuf, *folder_pixbuf;
|
|
|
|
gchar *parent;
|
2020-02-03 03:52:15 +00:00
|
|
|
GtkWidget *up_button;
|
2004-07-04 20:35:35 +00:00
|
|
|
|
|
|
|
/* Loads the images for the demo and returns whether the operation succeeded */
|
2013-01-29 16:21:19 +00:00
|
|
|
static void
|
|
|
|
load_pixbufs (void)
|
2004-07-04 20:35:35 +00:00
|
|
|
{
|
|
|
|
if (file_pixbuf)
|
2013-01-29 16:21:19 +00:00
|
|
|
return; /* already loaded earlier */
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2013-01-29 16:21:19 +00:00
|
|
|
file_pixbuf = gdk_pixbuf_new_from_resource (FILE_NAME, NULL);
|
|
|
|
/* resources must load successfully */
|
|
|
|
g_assert (file_pixbuf);
|
2004-07-04 20:35:35 +00:00
|
|
|
|
2013-01-29 16:21:19 +00:00
|
|
|
folder_pixbuf = gdk_pixbuf_new_from_resource (FOLDER_NAME, NULL);
|
|
|
|
g_assert (folder_pixbuf);
|
2004-07-04 20:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fill_store (GtkListStore *store)
|
|
|
|
{
|
|
|
|
GDir *dir;
|
|
|
|
const gchar *name;
|
|
|
|
GtkTreeIter iter;
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2004-07-04 20:35:35 +00:00
|
|
|
/* First clear the store */
|
|
|
|
gtk_list_store_clear (store);
|
|
|
|
|
|
|
|
/* Now go through the directory and extract all the file
|
|
|
|
* information */
|
|
|
|
dir = g_dir_open (parent, 0, NULL);
|
|
|
|
if (!dir)
|
|
|
|
return;
|
|
|
|
|
|
|
|
name = g_dir_read_name (dir);
|
|
|
|
while (name != NULL)
|
|
|
|
{
|
|
|
|
gchar *path, *display_name;
|
|
|
|
gboolean is_dir;
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2004-07-04 20:35:35 +00:00
|
|
|
/* We ignore hidden files that start with a '.' */
|
|
|
|
if (name[0] != '.')
|
2011-09-02 03:55:47 +00:00
|
|
|
{
|
|
|
|
path = g_build_filename (parent, name, NULL);
|
2004-07-04 20:35:35 +00:00
|
|
|
|
2011-09-02 03:55:47 +00:00
|
|
|
is_dir = g_file_test (path, G_FILE_TEST_IS_DIR);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2011-09-02 03:55:47 +00:00
|
|
|
display_name = g_filename_to_utf8 (name, -1, NULL, NULL, NULL);
|
2004-07-04 20:35:35 +00:00
|
|
|
|
2011-09-02 03:55:47 +00:00
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter,
|
|
|
|
COL_PATH, path,
|
|
|
|
COL_DISPLAY_NAME, display_name,
|
|
|
|
COL_IS_DIRECTORY, is_dir,
|
|
|
|
COL_PIXBUF, is_dir ? folder_pixbuf : file_pixbuf,
|
|
|
|
-1);
|
|
|
|
g_free (path);
|
|
|
|
g_free (display_name);
|
|
|
|
}
|
2004-07-04 20:35:35 +00:00
|
|
|
|
2008-08-11 19:12:58 +00:00
|
|
|
name = g_dir_read_name (dir);
|
2004-07-04 20:35:35 +00:00
|
|
|
}
|
2011-02-17 20:54:50 +00:00
|
|
|
g_dir_close (dir);
|
2004-07-04 20:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
|
|
|
sort_func (GtkTreeModel *model,
|
2011-09-02 03:55:47 +00:00
|
|
|
GtkTreeIter *a,
|
|
|
|
GtkTreeIter *b,
|
|
|
|
gpointer user_data)
|
2004-07-04 20:35:35 +00:00
|
|
|
{
|
|
|
|
gboolean is_dir_a, is_dir_b;
|
|
|
|
gchar *name_a, *name_b;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* We need this function because we want to sort
|
|
|
|
* folders before files.
|
|
|
|
*/
|
|
|
|
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2004-07-04 20:35:35 +00:00
|
|
|
gtk_tree_model_get (model, a,
|
2011-09-02 03:55:47 +00:00
|
|
|
COL_IS_DIRECTORY, &is_dir_a,
|
|
|
|
COL_DISPLAY_NAME, &name_a,
|
|
|
|
-1);
|
2004-07-04 20:35:35 +00:00
|
|
|
|
|
|
|
gtk_tree_model_get (model, b,
|
2011-09-02 03:55:47 +00:00
|
|
|
COL_IS_DIRECTORY, &is_dir_b,
|
|
|
|
COL_DISPLAY_NAME, &name_b,
|
|
|
|
-1);
|
2004-07-04 20:35:35 +00:00
|
|
|
|
|
|
|
if (!is_dir_a && is_dir_b)
|
|
|
|
ret = 1;
|
|
|
|
else if (is_dir_a && !is_dir_b)
|
|
|
|
ret = -1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = g_utf8_collate (name_a, name_b);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (name_a);
|
|
|
|
g_free (name_b);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-07-12 16:02:19 +00:00
|
|
|
static GtkListStore *
|
2004-07-04 20:35:35 +00:00
|
|
|
create_store (void)
|
|
|
|
{
|
|
|
|
GtkListStore *store;
|
|
|
|
|
|
|
|
store = gtk_list_store_new (NUM_COLS,
|
2011-09-02 03:55:47 +00:00
|
|
|
G_TYPE_STRING,
|
|
|
|
G_TYPE_STRING,
|
|
|
|
GDK_TYPE_PIXBUF,
|
|
|
|
G_TYPE_BOOLEAN);
|
2004-07-04 20:35:35 +00:00
|
|
|
|
2008-08-11 19:12:58 +00:00
|
|
|
/* Set sort column and function */
|
2004-07-04 20:35:35 +00:00
|
|
|
gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (store),
|
2011-09-02 03:55:47 +00:00
|
|
|
sort_func,
|
|
|
|
NULL, NULL);
|
2004-07-04 20:35:35 +00:00
|
|
|
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
|
2011-09-02 03:55:47 +00:00
|
|
|
GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,
|
|
|
|
GTK_SORT_ASCENDING);
|
2004-07-04 20:35:35 +00:00
|
|
|
|
|
|
|
return store;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
item_activated (GtkIconView *icon_view,
|
2011-09-02 03:55:47 +00:00
|
|
|
GtkTreePath *tree_path,
|
|
|
|
gpointer user_data)
|
2004-07-04 20:35:35 +00:00
|
|
|
{
|
|
|
|
GtkListStore *store;
|
|
|
|
gchar *path;
|
|
|
|
GtkTreeIter iter;
|
|
|
|
gboolean is_dir;
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2004-07-04 20:35:35 +00:00
|
|
|
store = GTK_LIST_STORE (user_data);
|
|
|
|
|
|
|
|
gtk_tree_model_get_iter (GTK_TREE_MODEL (store),
|
2011-09-02 03:55:47 +00:00
|
|
|
&iter, tree_path);
|
2004-07-04 20:35:35 +00:00
|
|
|
gtk_tree_model_get (GTK_TREE_MODEL (store), &iter,
|
2011-09-02 03:55:47 +00:00
|
|
|
COL_PATH, &path,
|
|
|
|
COL_IS_DIRECTORY, &is_dir,
|
|
|
|
-1);
|
2004-07-04 20:35:35 +00:00
|
|
|
|
|
|
|
if (!is_dir)
|
|
|
|
{
|
|
|
|
g_free (path);
|
|
|
|
return;
|
|
|
|
}
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2004-07-04 20:35:35 +00:00
|
|
|
/* Replace parent with path and re-fill the model*/
|
|
|
|
g_free (parent);
|
|
|
|
parent = path;
|
|
|
|
|
|
|
|
fill_store (store);
|
|
|
|
|
|
|
|
/* Sensitize the up button */
|
|
|
|
gtk_widget_set_sensitive (GTK_WIDGET (up_button), TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-03 04:42:34 +00:00
|
|
|
up_clicked (GtkButton *item,
|
|
|
|
gpointer user_data)
|
2004-07-04 20:35:35 +00:00
|
|
|
{
|
|
|
|
GtkListStore *store;
|
|
|
|
gchar *dir_name;
|
|
|
|
|
|
|
|
store = GTK_LIST_STORE (user_data);
|
|
|
|
|
|
|
|
dir_name = g_path_get_dirname (parent);
|
|
|
|
g_free (parent);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2004-07-04 20:35:35 +00:00
|
|
|
parent = dir_name;
|
|
|
|
|
|
|
|
fill_store (store);
|
|
|
|
|
|
|
|
/* Maybe de-sensitize the up button */
|
|
|
|
gtk_widget_set_sensitive (GTK_WIDGET (up_button),
|
2011-09-02 03:55:47 +00:00
|
|
|
strcmp (parent, "/") != 0);
|
2004-07-04 20:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-03 04:42:34 +00:00
|
|
|
home_clicked (GtkButton *item,
|
|
|
|
gpointer user_data)
|
2004-07-04 20:35:35 +00:00
|
|
|
{
|
|
|
|
GtkListStore *store;
|
|
|
|
|
|
|
|
store = GTK_LIST_STORE (user_data);
|
|
|
|
|
|
|
|
g_free (parent);
|
|
|
|
parent = g_strdup (g_get_home_dir ());
|
|
|
|
|
|
|
|
fill_store (store);
|
|
|
|
|
|
|
|
/* Sensitize the up button */
|
|
|
|
gtk_widget_set_sensitive (GTK_WIDGET (up_button),
|
2011-09-02 03:55:47 +00:00
|
|
|
TRUE);
|
2004-07-04 20:35:35 +00:00
|
|
|
}
|
|
|
|
|
2006-12-23 02:39:45 +00:00
|
|
|
static void close_window(void)
|
|
|
|
{
|
2020-05-09 14:26:22 +00:00
|
|
|
gtk_window_destroy (GTK_WINDOW (window));
|
2006-12-23 02:39:45 +00:00
|
|
|
window = NULL;
|
|
|
|
|
|
|
|
g_object_unref (file_pixbuf);
|
|
|
|
file_pixbuf = NULL;
|
|
|
|
|
|
|
|
g_object_unref (folder_pixbuf);
|
|
|
|
folder_pixbuf = NULL;
|
|
|
|
}
|
|
|
|
|
2004-07-04 20:35:35 +00:00
|
|
|
GtkWidget *
|
|
|
|
do_iconview (GtkWidget *do_widget)
|
|
|
|
{
|
|
|
|
if (!window)
|
|
|
|
{
|
2013-01-29 16:21:19 +00:00
|
|
|
GtkWidget *sw;
|
|
|
|
GtkWidget *icon_view;
|
|
|
|
GtkListStore *store;
|
|
|
|
GtkWidget *vbox;
|
|
|
|
GtkWidget *tool_bar;
|
2020-02-03 03:52:15 +00:00
|
|
|
GtkWidget *home_button;
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2020-02-14 19:55:36 +00:00
|
|
|
window = gtk_window_new ();
|
2004-07-04 20:35:35 +00:00
|
|
|
gtk_window_set_default_size (GTK_WINDOW (window), 650, 400);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2017-10-31 06:41:15 +00:00
|
|
|
gtk_window_set_display (GTK_WINDOW (window),
|
|
|
|
gtk_widget_get_display (do_widget));
|
2015-06-20 01:59:15 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Icon View Basics");
|
2004-07-04 20:35:35 +00:00
|
|
|
|
|
|
|
g_signal_connect (window, "destroy",
|
2011-09-02 03:55:47 +00:00
|
|
|
G_CALLBACK (close_window), NULL);
|
2004-07-04 20:35:35 +00:00
|
|
|
|
2013-01-29 16:21:19 +00:00
|
|
|
load_pixbufs ();
|
2004-07-04 20:35:35 +00:00
|
|
|
|
2013-01-29 16:21:19 +00:00
|
|
|
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
|
2020-05-02 21:26:54 +00:00
|
|
|
gtk_window_set_child (GTK_WINDOW (window), vbox);
|
2004-07-04 20:35:35 +00:00
|
|
|
|
2020-02-03 03:52:15 +00:00
|
|
|
tool_bar = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (vbox), tool_bar);
|
2004-07-04 20:35:35 +00:00
|
|
|
|
2020-02-03 03:52:15 +00:00
|
|
|
up_button = gtk_button_new_with_mnemonic ("_Up");
|
2013-01-29 16:21:19 +00:00
|
|
|
gtk_widget_set_sensitive (GTK_WIDGET (up_button), FALSE);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (tool_bar), up_button);
|
2020-02-03 03:52:15 +00:00
|
|
|
|
|
|
|
home_button = gtk_button_new_with_mnemonic ("_Home");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (tool_bar), home_button);
|
2013-01-29 16:21:19 +00:00
|
|
|
|
|
|
|
|
2020-06-24 15:25:09 +00:00
|
|
|
sw = gtk_scrolled_window_new ();
|
2020-04-17 03:25:06 +00:00
|
|
|
gtk_scrolled_window_set_has_frame (GTK_SCROLLED_WINDOW (sw), TRUE);
|
2013-01-29 16:21:19 +00:00
|
|
|
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
|
|
|
|
GTK_POLICY_AUTOMATIC,
|
|
|
|
GTK_POLICY_AUTOMATIC);
|
2017-04-21 16:59:59 +00:00
|
|
|
gtk_widget_set_vexpand (sw, TRUE);
|
2013-01-29 16:21:19 +00:00
|
|
|
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (vbox), sw);
|
2013-01-29 16:21:19 +00:00
|
|
|
|
|
|
|
/* Create the store and fill it with the contents of '/' */
|
|
|
|
parent = g_strdup ("/");
|
|
|
|
store = create_store ();
|
|
|
|
fill_store (store);
|
|
|
|
|
|
|
|
icon_view = gtk_icon_view_new_with_model (GTK_TREE_MODEL (store));
|
|
|
|
gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (icon_view),
|
|
|
|
GTK_SELECTION_MULTIPLE);
|
|
|
|
g_object_unref (store);
|
|
|
|
|
|
|
|
/* Connect to the "clicked" signal of the "Up" tool button */
|
|
|
|
g_signal_connect (up_button, "clicked",
|
|
|
|
G_CALLBACK (up_clicked), store);
|
|
|
|
|
|
|
|
/* Connect to the "clicked" signal of the "Home" tool button */
|
|
|
|
g_signal_connect (home_button, "clicked",
|
|
|
|
G_CALLBACK (home_clicked), store);
|
|
|
|
|
|
|
|
/* We now set which model columns that correspond to the text
|
|
|
|
* and pixbuf of each item
|
|
|
|
*/
|
|
|
|
gtk_icon_view_set_text_column (GTK_ICON_VIEW (icon_view), COL_DISPLAY_NAME);
|
|
|
|
gtk_icon_view_set_pixbuf_column (GTK_ICON_VIEW (icon_view), COL_PIXBUF);
|
|
|
|
|
|
|
|
/* Connect to the "item-activated" signal */
|
|
|
|
g_signal_connect (icon_view, "item-activated",
|
|
|
|
G_CALLBACK (item_activated), store);
|
2020-05-02 04:51:20 +00:00
|
|
|
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), icon_view);
|
2013-01-29 16:21:19 +00:00
|
|
|
|
|
|
|
gtk_widget_grab_focus (icon_view);
|
2004-07-04 20:35:35 +00:00
|
|
|
}
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2010-03-01 06:47:38 +00:00
|
|
|
if (!gtk_widget_get_visible (window))
|
2017-01-19 09:02:04 +00:00
|
|
|
gtk_widget_show (window);
|
2004-07-04 20:35:35 +00:00
|
|
|
else
|
2020-05-09 14:26:22 +00:00
|
|
|
gtk_window_destroy (GTK_WINDOW (window));
|
2004-07-04 20:35:35 +00:00
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|