iconview_edit.c 4.2 KB
Newer Older
Matthias Clasen's avatar
Matthias Clasen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/* 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>

static GtkWidget *window = NULL;

enum
{
  COL_TEXT,
  NUM_COLS
};


static void
fill_store (GtkListStore *store)
{
  GtkTreeIter iter;
  const gchar *text[] = { "Red", "Green", "Blue", "Yellow" };
  gint i;
Manish Singh's avatar
Manish Singh committed
26

Matthias Clasen's avatar
Matthias Clasen committed
27 28
  /* First clear the store */
  gtk_list_store_clear (store);
Manish Singh's avatar
Manish Singh committed
29

Matthias Clasen's avatar
Matthias Clasen committed
30 31 32 33 34 35 36 37 38 39 40 41 42
  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);
Manish Singh's avatar
Manish Singh committed
43

Matthias Clasen's avatar
Matthias Clasen committed
44 45 46
  return store;
}

Manish Singh's avatar
Manish Singh committed
47
static void
Matthias Clasen's avatar
Matthias Clasen committed
48
set_cell_color (GtkCellLayout   *cell_layout,
Matthias Clasen's avatar
Matthias Clasen committed
49 50 51 52
                GtkCellRenderer *cell,
                GtkTreeModel    *tree_model,
                GtkTreeIter     *iter,
                gpointer         data)
Matthias Clasen's avatar
Matthias Clasen committed
53 54 55 56 57 58 59 60
{
  gchar *text;
  GdkColor color;
  guint32 pixel = 0;
  GdkPixbuf *pixbuf;

  gtk_tree_model_get (tree_model, iter, COL_TEXT, &text, -1);
  if (gdk_color_parse (text, &color))
Manish Singh's avatar
Manish Singh committed
61
    pixel =
Matthias Clasen's avatar
Matthias Clasen committed
62 63 64 65
      (color.red   >> 8) << 24 |
      (color.green >> 8) << 16 |
      (color.blue  >> 8) << 8;

Matthias Clasen's avatar
Matthias Clasen committed
66 67
  g_free (text);

Matthias Clasen's avatar
Matthias Clasen committed
68 69
  pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 24, 24);
  gdk_pixbuf_fill (pixbuf, pixel);
Manish Singh's avatar
Manish Singh committed
70

Matthias Clasen's avatar
Matthias Clasen committed
71 72 73 74 75 76 77
  g_object_set (cell, "pixbuf", pixbuf, NULL);

  g_object_unref (pixbuf);
}

static void
edited (GtkCellRendererText *cell,
Matthias Clasen's avatar
Matthias Clasen committed
78 79 80
        gchar               *path_string,
        gchar               *text,
        gpointer             data)
Matthias Clasen's avatar
Matthias Clasen committed
81 82 83 84 85 86 87 88 89
{
  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);
Manish Singh's avatar
Manish Singh committed
90
  gtk_list_store_set (GTK_LIST_STORE (model), &iter,
Matthias Clasen's avatar
Matthias Clasen committed
91
                      COL_TEXT, text, -1);
Matthias Clasen's avatar
Matthias Clasen committed
92 93 94 95 96 97 98 99 100 101 102 103

  gtk_tree_path_free (path);
}

GtkWidget *
do_iconview_edit (GtkWidget *do_widget)
{
  if (!window)
    {
      GtkWidget *icon_view;
      GtkListStore *store;
      GtkCellRenderer *renderer;
Manish Singh's avatar
Manish Singh committed
104

Matthias Clasen's avatar
Matthias Clasen committed
105
      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
Manish Singh's avatar
Manish Singh committed
106

Matthias Clasen's avatar
Matthias Clasen committed
107
      gtk_window_set_screen (GTK_WINDOW (window),
Matthias Clasen's avatar
Matthias Clasen committed
108
                             gtk_widget_get_screen (do_widget));
Matthias Clasen's avatar
Matthias Clasen committed
109 110 111
      gtk_window_set_title (GTK_WINDOW (window), "Editing and Drag-and-Drop");

      g_signal_connect (window, "destroy",
Matthias Clasen's avatar
Matthias Clasen committed
112
                        G_CALLBACK (gtk_widget_destroyed), &window);
Matthias Clasen's avatar
Matthias Clasen committed
113 114 115

      store = create_store ();
      fill_store (store);
Manish Singh's avatar
Manish Singh committed
116

Matthias Clasen's avatar
Matthias Clasen committed
117 118 119 120
      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),
Matthias Clasen's avatar
Matthias Clasen committed
121
                                        GTK_SELECTION_SINGLE);
122
      gtk_icon_view_set_item_orientation (GTK_ICON_VIEW (icon_view),
Matthias Clasen's avatar
Matthias Clasen committed
123
                                          GTK_ORIENTATION_HORIZONTAL);
Matthias Clasen's avatar
Matthias Clasen committed
124 125
      gtk_icon_view_set_columns (GTK_ICON_VIEW (icon_view), 2);
      gtk_icon_view_set_reorderable (GTK_ICON_VIEW (icon_view), TRUE);
Manish Singh's avatar
Manish Singh committed
126

Matthias Clasen's avatar
Matthias Clasen committed
127 128
      renderer = gtk_cell_renderer_pixbuf_new ();
      gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_view),
Matthias Clasen's avatar
Matthias Clasen committed
129
                                  renderer, TRUE);
Matthias Clasen's avatar
Matthias Clasen committed
130
      gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (icon_view),
Matthias Clasen's avatar
Matthias Clasen committed
131 132 133
                                          renderer,
                                          set_cell_color,
                                          NULL, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
134 135 136

      renderer = gtk_cell_renderer_text_new ();
      gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_view),
Matthias Clasen's avatar
Matthias Clasen committed
137
                                  renderer, TRUE);
Matthias Clasen's avatar
Matthias Clasen committed
138 139 140
      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),
Matthias Clasen's avatar
Matthias Clasen committed
141 142 143
                                      renderer,
                                      "text", COL_TEXT,
                                      NULL);
Manish Singh's avatar
Manish Singh committed
144

Matthias Clasen's avatar
Matthias Clasen committed
145
      gtk_container_add (GTK_CONTAINER (window), icon_view);
Manish Singh's avatar
Manish Singh committed
146 147
    }

148
  if (!gtk_widget_get_visible (window))
Matthias Clasen's avatar
Matthias Clasen committed
149 150 151 152 153 154
    gtk_widget_show_all (window);
  else
    {
      gtk_widget_destroy (window);
      window = NULL;
    }
Manish Singh's avatar
Manish Singh committed
155

Matthias Clasen's avatar
Matthias Clasen committed
156 157
  return window;
}