2005-07-12 16:02:19 +00:00
|
|
|
/* Icon View/Editing and Drag-and-Drop
|
|
|
|
*
|
|
|
|
* The GtkIconView widget supports Editing and Drag-and-Drop.
|
|
|
|
* This example also demonstrates using the generic GtkCellLayout
|
|
|
|
* interface to set up cell renderers in an icon view.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
COL_TEXT,
|
|
|
|
NUM_COLS
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
fill_store (GtkListStore *store)
|
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
const gchar *text[] = { "Red", "Green", "Blue", "Yellow" };
|
|
|
|
gint i;
|
2005-07-13 23:04:04 +00:00
|
|
|
|
2005-07-12 16:02:19 +00:00
|
|
|
/* First clear the store */
|
|
|
|
gtk_list_store_clear (store);
|
2005-07-13 23:04:04 +00:00
|
|
|
|
2005-07-12 16:02:19 +00:00
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, COL_TEXT, text[i], -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkListStore *
|
|
|
|
create_store (void)
|
|
|
|
{
|
|
|
|
GtkListStore *store;
|
|
|
|
|
|
|
|
store = gtk_list_store_new (NUM_COLS, G_TYPE_STRING);
|
2005-07-13 23:04:04 +00:00
|
|
|
|
2005-07-12 16:02:19 +00:00
|
|
|
return store;
|
|
|
|
}
|
|
|
|
|
2005-07-13 23:04:04 +00:00
|
|
|
static void
|
2005-07-12 16:02:19 +00:00
|
|
|
set_cell_color (GtkCellLayout *cell_layout,
|
2011-09-02 03:55:47 +00:00
|
|
|
GtkCellRenderer *cell,
|
|
|
|
GtkTreeModel *tree_model,
|
|
|
|
GtkTreeIter *iter,
|
|
|
|
gpointer data)
|
2005-07-12 16:02:19 +00:00
|
|
|
{
|
|
|
|
gchar *text;
|
2014-05-22 13:05:58 +00:00
|
|
|
GdkRGBA color;
|
2005-07-12 16:02:19 +00:00
|
|
|
guint32 pixel = 0;
|
|
|
|
GdkPixbuf *pixbuf;
|
|
|
|
|
|
|
|
gtk_tree_model_get (tree_model, iter, COL_TEXT, &text, -1);
|
2014-05-22 13:05:58 +00:00
|
|
|
if (!text)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (gdk_rgba_parse (&color, text))
|
2005-07-13 23:04:04 +00:00
|
|
|
pixel =
|
2014-05-22 13:05:58 +00:00
|
|
|
((gint)(color.red * 255)) << 24 |
|
|
|
|
((gint)(color.green * 255)) << 16 |
|
|
|
|
((gint)(color.blue * 255)) << 8 |
|
|
|
|
((gint)(color.alpha * 255));
|
2005-07-12 16:02:19 +00:00
|
|
|
|
2005-12-07 19:50:39 +00:00
|
|
|
g_free (text);
|
|
|
|
|
2014-05-22 13:05:58 +00:00
|
|
|
pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
|
2005-07-12 16:02:19 +00:00
|
|
|
gdk_pixbuf_fill (pixbuf, pixel);
|
2005-07-13 23:04:04 +00:00
|
|
|
|
2005-07-12 16:02:19 +00:00
|
|
|
g_object_set (cell, "pixbuf", pixbuf, NULL);
|
|
|
|
|
|
|
|
g_object_unref (pixbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
edited (GtkCellRendererText *cell,
|
2011-09-02 03:55:47 +00:00
|
|
|
gchar *path_string,
|
|
|
|
gchar *text,
|
|
|
|
gpointer data)
|
2005-07-12 16:02:19 +00:00
|
|
|
{
|
|
|
|
GtkTreeModel *model;
|
|
|
|
GtkTreeIter iter;
|
|
|
|
GtkTreePath *path;
|
|
|
|
|
|
|
|
model = gtk_icon_view_get_model (GTK_ICON_VIEW (data));
|
|
|
|
path = gtk_tree_path_new_from_string (path_string);
|
|
|
|
|
|
|
|
gtk_tree_model_get_iter (model, &iter, path);
|
2005-07-13 23:04:04 +00:00
|
|
|
gtk_list_store_set (GTK_LIST_STORE (model), &iter,
|
2011-09-02 03:55:47 +00:00
|
|
|
COL_TEXT, text, -1);
|
2005-07-12 16:02:19 +00:00
|
|
|
|
|
|
|
gtk_tree_path_free (path);
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
do_iconview_edit (GtkWidget *do_widget)
|
|
|
|
{
|
2015-06-28 13:23:28 +00:00
|
|
|
static GtkWidget *window = NULL;
|
|
|
|
|
2005-07-12 16:02:19 +00:00
|
|
|
if (!window)
|
|
|
|
{
|
|
|
|
GtkWidget *icon_view;
|
|
|
|
GtkListStore *store;
|
|
|
|
GtkCellRenderer *renderer;
|
2005-07-13 23:04:04 +00:00
|
|
|
|
2005-07-12 16:02:19 +00:00
|
|
|
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
2005-07-13 23:04:04 +00:00
|
|
|
|
2005-07-12 16:02:19 +00:00
|
|
|
gtk_window_set_screen (GTK_WINDOW (window),
|
2011-09-02 03:55:47 +00:00
|
|
|
gtk_widget_get_screen (do_widget));
|
2005-07-12 16:02:19 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Editing and Drag-and-Drop");
|
|
|
|
|
|
|
|
g_signal_connect (window, "destroy",
|
2011-09-02 03:55:47 +00:00
|
|
|
G_CALLBACK (gtk_widget_destroyed), &window);
|
2005-07-12 16:02:19 +00:00
|
|
|
|
|
|
|
store = create_store ();
|
|
|
|
fill_store (store);
|
2005-07-13 23:04:04 +00:00
|
|
|
|
2005-07-12 16:02:19 +00:00
|
|
|
icon_view = gtk_icon_view_new_with_model (GTK_TREE_MODEL (store));
|
|
|
|
g_object_unref (store);
|
|
|
|
|
|
|
|
gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (icon_view),
|
2011-09-02 03:55:47 +00:00
|
|
|
GTK_SELECTION_SINGLE);
|
2010-09-02 03:03:48 +00:00
|
|
|
gtk_icon_view_set_item_orientation (GTK_ICON_VIEW (icon_view),
|
2011-09-02 03:55:47 +00:00
|
|
|
GTK_ORIENTATION_HORIZONTAL);
|
2005-07-12 16:02:19 +00:00
|
|
|
gtk_icon_view_set_columns (GTK_ICON_VIEW (icon_view), 2);
|
|
|
|
gtk_icon_view_set_reorderable (GTK_ICON_VIEW (icon_view), TRUE);
|
2005-07-13 23:04:04 +00:00
|
|
|
|
2005-07-12 16:02:19 +00:00
|
|
|
renderer = gtk_cell_renderer_pixbuf_new ();
|
|
|
|
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_view),
|
2011-09-02 03:55:47 +00:00
|
|
|
renderer, TRUE);
|
2005-07-12 16:02:19 +00:00
|
|
|
gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (icon_view),
|
2011-09-02 03:55:47 +00:00
|
|
|
renderer,
|
|
|
|
set_cell_color,
|
|
|
|
NULL, NULL);
|
2005-07-12 16:02:19 +00:00
|
|
|
|
|
|
|
renderer = gtk_cell_renderer_text_new ();
|
|
|
|
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_view),
|
2011-09-02 03:55:47 +00:00
|
|
|
renderer, TRUE);
|
2005-07-12 16:02:19 +00:00
|
|
|
g_object_set (renderer, "editable", TRUE, NULL);
|
|
|
|
g_signal_connect (renderer, "edited", G_CALLBACK (edited), icon_view);
|
|
|
|
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
|
2011-09-02 03:55:47 +00:00
|
|
|
renderer,
|
|
|
|
"text", COL_TEXT,
|
|
|
|
NULL);
|
2005-07-13 23:04:04 +00:00
|
|
|
|
2005-07-12 16:02:19 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (window), icon_view);
|
2005-07-13 23:04:04 +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);
|
2005-07-12 16:02:19 +00:00
|
|
|
else
|
2015-06-28 13:23:28 +00:00
|
|
|
gtk_widget_destroy (window);
|
2005-07-13 23:04:04 +00:00
|
|
|
|
2005-07-12 16:02:19 +00:00
|
|
|
return window;
|
|
|
|
}
|