testnotebookdnd.c 7.87 KB
Newer Older
1
/* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
2
/*
Cody Russell's avatar
Cody Russell committed
3
 * GTK - The GIMP Toolkit
Matthias Clasen's avatar
Matthias Clasen committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * 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
Javier Jardón's avatar
Javier Jardón committed
19
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Matthias Clasen's avatar
Matthias Clasen committed
20 21 22
 */
#include <gtk/gtk.h>

23 24
static gpointer GROUP_A = "GROUP_A";
static gpointer GROUP_B = "GROUP_B";
Matthias Clasen's avatar
Matthias Clasen committed
25 26

gchar *tabs1 [] = {
27 28 29 30
  "aaaaaaaaaa",
  "bbbbbbbbbb",
  "cccccccccc",
  "dddddddddd",
Matthias Clasen's avatar
Matthias Clasen committed
31 32 33 34 35 36 37 38
  NULL
};

gchar *tabs2 [] = {
  "1",
  "2",
  "3",
  "4",
39
  "55555",
Matthias Clasen's avatar
Matthias Clasen committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  NULL
};

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

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

58 59 60 61
static const GtkTargetEntry button_targets[] = {
  { "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, 0 },
};

Matthias Clasen's avatar
Matthias Clasen committed
62 63
static GtkNotebook*
window_creation_function (GtkNotebook *source_notebook,
64 65 66 67
                          GtkWidget   *child,
                          gint         x,
                          gint         y,
                          gpointer     data)
Matthias Clasen's avatar
Matthias Clasen committed
68 69 70 71 72
{
  GtkWidget *window, *notebook;

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  notebook = gtk_notebook_new ();
73 74
  g_signal_connect (notebook, "create-window",
                    G_CALLBACK (window_creation_function), NULL);
Matthias Clasen's avatar
Matthias Clasen committed
75

76
  gtk_notebook_set_group_name (GTK_NOTEBOOK (notebook),
77
                               gtk_notebook_get_group_name (source_notebook));
Matthias Clasen's avatar
Matthias Clasen committed
78 79 80 81 82 83 84 85 86 87 88

  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
89
on_page_reordered (GtkNotebook *notebook, GtkWidget *child, guint page_num, gpointer data)
Matthias Clasen's avatar
Matthias Clasen committed
90
{
91
  g_print ("page %d reordered\n", page_num);
Matthias Clasen's avatar
Matthias Clasen committed
92 93
}

94 95
static void
on_notebook_drag_begin (GtkWidget      *widget,
96 97
                        GdkDragContext *context,
                        gpointer        data)
98 99 100 101 102 103
{
  GdkPixbuf *pixbuf;
  guint page_num;

  page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (widget));

Matthias Clasen's avatar
Matthias Clasen committed
104 105
  if (page_num > 2)
    {
106
      pixbuf = gtk_widget_render_icon_pixbuf (widget,
107 108
                                              (page_num % 2) ? GTK_STOCK_HELP : GTK_STOCK_STOP,
                                              GTK_ICON_SIZE_DND);
109

Matthias Clasen's avatar
Matthias Clasen committed
110 111 112
      gtk_drag_set_icon_pixbuf (context, pixbuf, 0, 0);
      g_object_unref (pixbuf);
    }
113 114 115 116
}

static void
on_button_drag_data_received (GtkWidget        *widget,
117 118 119 120 121 122 123
                              GdkDragContext   *context,
                              gint              x,
                              gint              y,
                              GtkSelectionData *data,
                              guint             info,
                              guint             time,
                              gpointer          user_data)
124 125 126 127 128
{
  GtkWidget *source, *tab_label;
  GtkWidget **child;

  source = gtk_drag_get_source_widget (context);
129
  child = (void*) gtk_selection_data_get_data (data);
130 131 132 133 134 135 136

  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
137 138
static GtkWidget*
create_notebook (gchar           **labels,
139 140
                 const gchar      *group,
                 GtkPositionType   pos)
Matthias Clasen's avatar
Matthias Clasen committed
141 142 143 144 145
{
  GtkWidget *notebook, *title, *page;
  gint count = 0;

  notebook = gtk_notebook_new ();
146 147
  gtk_widget_set_vexpand (notebook, TRUE);
  gtk_widget_set_hexpand (notebook, TRUE);
148 149
  g_signal_connect (notebook, "create-window",
                    G_CALLBACK (window_creation_function), NULL);
Matthias Clasen's avatar
Matthias Clasen committed
150 151 152 153

  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);
154
  gtk_notebook_set_group_name (GTK_NOTEBOOK (notebook), group);
Matthias Clasen's avatar
Matthias Clasen committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

  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);

      count++;
      labels++;
    }

171
  g_signal_connect (GTK_NOTEBOOK (notebook), "page-reordered",
172
                    G_CALLBACK (on_page_reordered), NULL);
173
  g_signal_connect_after (G_OBJECT (notebook), "drag-begin",
174
                          G_CALLBACK (on_notebook_drag_begin), NULL);
Matthias Clasen's avatar
Matthias Clasen committed
175 176 177
  return notebook;
}

178 179
static GtkWidget*
create_notebook_with_notebooks (gchar           **labels,
180 181
                                const gchar      *group,
                                GtkPositionType   pos)
182 183 184 185 186
{
  GtkWidget *notebook, *title, *page;
  gint count = 0;

  notebook = gtk_notebook_new ();
187 188
  g_signal_connect (notebook, "create-window",
                    G_CALLBACK (window_creation_function), NULL);
189 190 191 192

  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);
193
  gtk_notebook_set_group_name (GTK_NOTEBOOK (notebook), group);
194 195 196

  while (*labels)
    {
197
      page = create_notebook (labels, group, pos);
198
      gtk_notebook_popup_enable (GTK_NOTEBOOK (page));
199

200 201 202 203 204 205 206 207 208 209 210
      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",
211
                    G_CALLBACK (on_page_reordered), NULL);
212
  g_signal_connect_after (G_OBJECT (notebook), "drag-begin",
213
                          G_CALLBACK (on_notebook_drag_begin), NULL);
214 215 216
  return notebook;
}

217 218 219 220 221 222 223 224
static GtkWidget*
create_trash_button (void)
{
  GtkWidget *button;

  button = gtk_button_new_from_stock (GTK_STOCK_DELETE);

  gtk_drag_dest_set (button,
225 226 227 228
                     GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP,
                     button_targets,
                     G_N_ELEMENTS (button_targets),
                     GDK_ACTION_MOVE);
229 230

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

Matthias Clasen's avatar
Matthias Clasen committed
235 236 237
gint
main (gint argc, gchar *argv[])
{
238
  GtkWidget *window, *grid;
Matthias Clasen's avatar
Matthias Clasen committed
239 240 241 242

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
243
  grid = gtk_grid_new ();
Matthias Clasen's avatar
Matthias Clasen committed
244

245 246 247
  gtk_grid_attach (GTK_GRID (grid),
                   create_notebook (tabs1, GROUP_A, GTK_POS_TOP),
                   0, 0, 1, 1);
Matthias Clasen's avatar
Matthias Clasen committed
248

249 250 251
  gtk_grid_attach (GTK_GRID (grid),
                   create_notebook (tabs2, GROUP_B, GTK_POS_BOTTOM),
                   0, 1, 1, 1);
Matthias Clasen's avatar
Matthias Clasen committed
252

253 254 255
  gtk_grid_attach (GTK_GRID (grid),
                   create_notebook (tabs3, GROUP_B, GTK_POS_LEFT),
                   1, 0, 1, 1);
Matthias Clasen's avatar
Matthias Clasen committed
256

257 258 259
  gtk_grid_attach (GTK_GRID (grid),
                   create_notebook_with_notebooks (tabs4, GROUP_A, GTK_POS_RIGHT),
                   1, 1, 1, 1);
Matthias Clasen's avatar
Matthias Clasen committed
260

261 262 263
  gtk_grid_attach (GTK_GRID (grid),
                   create_trash_button (),
                   1, 2, 1, 1);
264

265
  gtk_container_add (GTK_CONTAINER (window), grid);
Matthias Clasen's avatar
Matthias Clasen committed
266 267 268 269 270 271 272
  gtk_window_set_default_size (GTK_WINDOW (window), 400, 400);
  gtk_widget_show_all (window);

  gtk_main ();

  return 0;
}