testnotebookdnd.c 6.07 KB
Newer Older
1
/* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
Matthias Clasen's avatar
Matthias Clasen committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
/* 
 * GTK - The GIMP Toolkit
 * Copyright (C) 2006  Carlos Garnacho Parro <carlosg@gnome.org>
 *
 * All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#include <gtk/gtk.h>

enum {
  PACK_START,
  PACK_END,
  PACK_ALTERNATE
};

enum {
  GROUP_A,
  GROUP_B
};

gchar *tabs1 [] = {
  "a",
  "b",
  "c",
  "d",
  NULL
};

gchar *tabs2 [] = {
  "1",
  "2",
  "3",
  "4",
  "5",
  NULL
};

gchar *tabs3 [] = {
  "foo",
  "bar",
  NULL
};

gchar *tabs4 [] = {
  "beer",
  "water",
  "lemonade",
  "coffee",
  "tea",
  NULL
};

68 69 70 71
static const GtkTargetEntry button_targets[] = {
  { "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, 0 },
};

Matthias Clasen's avatar
Matthias Clasen committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
static GtkNotebook*
window_creation_function (GtkNotebook *source_notebook,
			  GtkWidget   *child,
			  gint         x,
			  gint         y,
			  gpointer     data)
{
  GtkWidget *window, *notebook;

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  notebook = gtk_notebook_new ();

  gtk_notebook_set_group_id (GTK_NOTEBOOK (notebook),
			     gtk_notebook_get_group_id (source_notebook));

  gtk_container_add (GTK_CONTAINER (window), notebook);

  gtk_window_set_default_size (GTK_WINDOW (window), 300, 300);
  gtk_window_move (GTK_WINDOW (window), x, y);
  gtk_widget_show_all (window);

  return GTK_NOTEBOOK (notebook);
}

static void
97
on_page_reordered (GtkNotebook *notebook, GtkWidget *child, guint page_num, gpointer data)
Matthias Clasen's avatar
Matthias Clasen committed
98
{
99
  g_print ("page %d reordered\n", page_num);
Matthias Clasen's avatar
Matthias Clasen committed
100 101
}

102 103 104 105 106 107 108 109 110 111
static void
on_notebook_drag_begin (GtkWidget      *widget,
			GdkDragContext *context,
			gpointer        data)
{
  GdkPixbuf *pixbuf;
  guint page_num;

  page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (widget));

Matthias Clasen's avatar
Matthias Clasen committed
112 113 114 115
  if (page_num > 2)
    {
      pixbuf = gtk_widget_render_icon (widget,
  				   (page_num % 2) ? GTK_STOCK_HELP : GTK_STOCK_STOP,
116 117
				   GTK_ICON_SIZE_DND, NULL);

Matthias Clasen's avatar
Matthias Clasen committed
118 119 120
      gtk_drag_set_icon_pixbuf (context, pixbuf, 0, 0);
      g_object_unref (pixbuf);
    }
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
}

static void
on_button_drag_data_received (GtkWidget        *widget,
			      GdkDragContext   *context,
			      gint              x,
			      gint              y,
			      GtkSelectionData *data,
			      guint             info,
			      guint             time,
			      gpointer          user_data)
{
  GtkWidget *source, *tab_label;
  GtkWidget **child;

  source = gtk_drag_get_source_widget (context);
  child = (void*) data->data;

  tab_label = gtk_notebook_get_tab_label (GTK_NOTEBOOK (source), *child);
  g_print ("Removing tab: %s\n", gtk_label_get_text (GTK_LABEL (tab_label)));

  gtk_container_remove (GTK_CONTAINER (source), *child);
}

Matthias Clasen's avatar
Matthias Clasen committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
static GtkWidget*
create_notebook (gchar           **labels,
		 gint              group_id,
		 gint              packing,
		 GtkPositionType   pos)
{
  GtkWidget *notebook, *title, *page;
  gint count = 0;

  notebook = gtk_notebook_new ();

  gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), pos);
  gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
  gtk_container_set_border_width (GTK_CONTAINER (notebook), 6);
  gtk_notebook_set_group_id (GTK_NOTEBOOK (notebook), group_id);

  while (*labels)
    {
      page = gtk_entry_new ();
      gtk_entry_set_text (GTK_ENTRY (page), *labels);

      title = gtk_label_new (*labels);

      gtk_notebook_append_page (GTK_NOTEBOOK (notebook), page, title);
      gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (notebook), page, TRUE);
      gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (notebook), page, TRUE);

      if (packing == PACK_END ||
	  (packing == PACK_ALTERNATE && count % 2 == 1))
	gtk_container_child_set (GTK_CONTAINER (notebook), page, "tab-pack", GTK_PACK_END, NULL);

      count++;
      labels++;
    }

180 181
  g_signal_connect (GTK_NOTEBOOK (notebook), "page-reordered",
		    G_CALLBACK (on_page_reordered), NULL);
182 183
  g_signal_connect_after (G_OBJECT (notebook), "drag-begin",
			  G_CALLBACK (on_notebook_drag_begin), NULL);
Matthias Clasen's avatar
Matthias Clasen committed
184 185 186
  return notebook;
}

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
static GtkWidget*
create_trash_button (void)
{
  GtkWidget *button;

  button = gtk_button_new_from_stock (GTK_STOCK_DELETE);

  gtk_drag_dest_set (button,
		     GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP,
		     button_targets,
		     G_N_ELEMENTS (button_targets),
		     GDK_ACTION_MOVE);

  g_signal_connect_after (G_OBJECT (button), "drag-data-received",
			  G_CALLBACK (on_button_drag_data_received), NULL);
  return button;
}

Matthias Clasen's avatar
Matthias Clasen committed
205 206 207 208 209 210 211 212
gint
main (gint argc, gchar *argv[])
{
  GtkWidget *window, *table;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
213
  table = gtk_table_new (3, 2, FALSE);
Matthias Clasen's avatar
Matthias Clasen committed
214

Matthias Clasen's avatar
Matthias Clasen committed
215
  gtk_notebook_set_window_creation_hook (window_creation_function, NULL, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232

  gtk_table_attach_defaults (GTK_TABLE (table),
			     create_notebook (tabs1, GROUP_A, PACK_START, GTK_POS_TOP),
			     0, 1, 0, 1);

  gtk_table_attach_defaults (GTK_TABLE (table),
			     create_notebook (tabs2, GROUP_B, PACK_ALTERNATE, GTK_POS_BOTTOM),
			     0, 1, 1, 2);

  gtk_table_attach_defaults (GTK_TABLE (table),
			     create_notebook (tabs3, GROUP_B, PACK_END, GTK_POS_LEFT),
			     1, 2, 0, 1);

  gtk_table_attach_defaults (GTK_TABLE (table),
			     create_notebook (tabs4, GROUP_A, PACK_ALTERNATE, GTK_POS_RIGHT),
			     1, 2, 1, 2);

233 234 235 236
  gtk_table_attach (GTK_TABLE (table),
		    create_trash_button (), 1, 2, 2, 3,
		    GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);

Matthias Clasen's avatar
Matthias Clasen committed
237 238 239 240 241 242 243 244
  gtk_container_add (GTK_CONTAINER (window), table);
  gtk_window_set_default_size (GTK_WINDOW (window), 400, 400);
  gtk_widget_show_all (window);

  gtk_main ();

  return 0;
}