]> Pileus Git - ~andy/gtk/blobdiff - tests/testnotebookdnd.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / tests / testnotebookdnd.c
index 4c48af5649e7d84cf38600c5a0494311426515b0..a347148d4d428bdfba321155a4f26c48495037b7 100644 (file)
@@ -1,4 +1,5 @@
-/* 
+/* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
+/*
  * GTK - The GIMP Toolkit
  * Copyright (C) 2006  Carlos Garnacho Parro <carlosg@gnome.org>
  *
  * 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.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  */
 #include <gtk/gtk.h>
 
-enum {
-  PACK_START,
-  PACK_END,
-  PACK_ALTERNATE
-};
-
-enum {
-  GROUP_A,
-  GROUP_B
-};
+static gpointer GROUP_A = "GROUP_A";
+static gpointer GROUP_B = "GROUP_B";
 
 gchar *tabs1 [] = {
-  "a",
-  "b",
-  "c",
-  "d",
+  "aaaaaaaaaa",
+  "bbbbbbbbbb",
+  "cccccccccc",
+  "dddddddddd",
   NULL
 };
 
@@ -45,7 +36,7 @@ gchar *tabs2 [] = {
   "2",
   "3",
   "4",
-  "5",
+  "55555",
   NULL
 };
 
@@ -64,20 +55,26 @@ gchar *tabs4 [] = {
   NULL
 };
 
+static const GtkTargetEntry button_targets[] = {
+  { "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, 0 },
+};
+
 static GtkNotebook*
 window_creation_function (GtkNotebook *source_notebook,
-                         GtkWidget   *child,
-                         gint         x,
-                         gint         y,
-                         gpointer     data)
+                          GtkWidget   *child,
+                          gint         x,
+                          gint         y,
+                          gpointer     data)
 {
   GtkWidget *window, *notebook;
 
   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
   notebook = gtk_notebook_new ();
+  g_signal_connect (notebook, "create-window",
+                    G_CALLBACK (window_creation_function), NULL);
 
-  gtk_notebook_set_group_id (GTK_NOTEBOOK (notebook),
-                            gtk_notebook_get_group_id (source_notebook));
+  gtk_notebook_set_group_name (GTK_NOTEBOOK (notebook),
+                               gtk_notebook_get_group_name (source_notebook));
 
   gtk_container_add (GTK_CONTAINER (window), notebook);
 
@@ -94,21 +91,67 @@ on_page_reordered (GtkNotebook *notebook, GtkWidget *child, guint page_num, gpoi
   g_print ("page %d reordered\n", page_num);
 }
 
+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));
+
+  if (page_num > 2)
+    {
+      pixbuf = gtk_widget_render_icon_pixbuf (widget,
+                                              (page_num % 2) ? GTK_STOCK_HELP : GTK_STOCK_STOP,
+                                              GTK_ICON_SIZE_DND);
+
+      gtk_drag_set_icon_pixbuf (context, pixbuf, 0, 0);
+      g_object_unref (pixbuf);
+    }
+}
+
+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*) gtk_selection_data_get_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);
+}
+
 static GtkWidget*
 create_notebook (gchar           **labels,
-                gint              group_id,
-                gint              packing,
-                GtkPositionType   pos)
+                 const gchar      *group,
+                 GtkPositionType   pos)
 {
   GtkWidget *notebook, *title, *page;
   gint count = 0;
 
   notebook = gtk_notebook_new ();
+  gtk_widget_set_vexpand (notebook, TRUE);
+  gtk_widget_set_hexpand (notebook, TRUE);
+  g_signal_connect (notebook, "create-window",
+                    G_CALLBACK (window_creation_function), NULL);
 
   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);
+  gtk_notebook_set_group_name (GTK_NOTEBOOK (notebook), group);
 
   while (*labels)
     {
@@ -121,49 +164,105 @@ create_notebook (gchar           **labels,
       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++;
     }
 
   g_signal_connect (GTK_NOTEBOOK (notebook), "page-reordered",
-                   G_CALLBACK (on_page_reordered), NULL);
+                    G_CALLBACK (on_page_reordered), NULL);
+  g_signal_connect_after (G_OBJECT (notebook), "drag-begin",
+                          G_CALLBACK (on_notebook_drag_begin), NULL);
+  return notebook;
+}
+
+static GtkWidget*
+create_notebook_with_notebooks (gchar           **labels,
+                                const gchar      *group,
+                                GtkPositionType   pos)
+{
+  GtkWidget *notebook, *title, *page;
+  gint count = 0;
+
+  notebook = gtk_notebook_new ();
+  g_signal_connect (notebook, "create-window",
+                    G_CALLBACK (window_creation_function), NULL);
+
+  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_name (GTK_NOTEBOOK (notebook), group);
+
+  while (*labels)
+    {
+      page = create_notebook (labels, group, pos);
+      gtk_notebook_popup_enable (GTK_NOTEBOOK (page));
+
+      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);
 
+      count++;
+      labels++;
+    }
+
+  g_signal_connect (GTK_NOTEBOOK (notebook), "page-reordered",
+                    G_CALLBACK (on_page_reordered), NULL);
+  g_signal_connect_after (G_OBJECT (notebook), "drag-begin",
+                          G_CALLBACK (on_notebook_drag_begin), NULL);
   return notebook;
 }
 
+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;
+}
+
 gint
 main (gint argc, gchar *argv[])
 {
-  GtkWidget *window, *table;
+  GtkWidget *window, *grid;
 
   gtk_init (&argc, &argv);
 
   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
-  table = gtk_table_new (2, 2, TRUE);
+  grid = gtk_grid_new ();
 
-  gtk_notebook_set_window_creation_hook (window_creation_function, NULL);
+  gtk_grid_attach (GTK_GRID (grid),
+                   create_notebook (tabs1, GROUP_A, GTK_POS_TOP),
+                   0, 0, 1, 1);
 
-  gtk_table_attach_defaults (GTK_TABLE (table),
-                            create_notebook (tabs1, GROUP_A, PACK_START, GTK_POS_TOP),
-                            0, 1, 0, 1);
+  gtk_grid_attach (GTK_GRID (grid),
+                   create_notebook (tabs2, GROUP_B, GTK_POS_BOTTOM),
+                   0, 1, 1, 1);
 
-  gtk_table_attach_defaults (GTK_TABLE (table),
-                            create_notebook (tabs2, GROUP_B, PACK_ALTERNATE, GTK_POS_BOTTOM),
-                            0, 1, 1, 2);
+  gtk_grid_attach (GTK_GRID (grid),
+                   create_notebook (tabs3, GROUP_B, GTK_POS_LEFT),
+                   1, 0, 1, 1);
 
-  gtk_table_attach_defaults (GTK_TABLE (table),
-                            create_notebook (tabs3, GROUP_B, PACK_END, GTK_POS_LEFT),
-                            1, 2, 0, 1);
+  gtk_grid_attach (GTK_GRID (grid),
+                   create_notebook_with_notebooks (tabs4, GROUP_A, GTK_POS_RIGHT),
+                   1, 1, 1, 1);
 
-  gtk_table_attach_defaults (GTK_TABLE (table),
-                            create_notebook (tabs4, GROUP_A, PACK_ALTERNATE, GTK_POS_RIGHT),
-                            1, 2, 1, 2);
+  gtk_grid_attach (GTK_GRID (grid),
+                   create_trash_button (),
+                   1, 2, 1, 1);
 
-  gtk_container_add (GTK_CONTAINER (window), table);
+  gtk_container_add (GTK_CONTAINER (window), grid);
   gtk_window_set_default_size (GTK_WINDOW (window), 400, 400);
   gtk_widget_show_all (window);