]> Pileus Git - ~andy/gtk/blob - tests/testnotebookdnd.c
9092f74ac67606f029d44ce9ad91833adfe10b3b
[~andy/gtk] / tests / testnotebookdnd.c
1 /* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
2 /* 
3  * GTK - The GTK+ Toolkit
4  * Copyright (C) 2006  Carlos Garnacho Parro <carlosg@gnome.org>
5  *
6  * All rights reserved.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 #include <gtk/gtk.h>
24
25 enum {
26   PACK_START,
27   PACK_END,
28   PACK_ALTERNATE
29 };
30
31 static gpointer GROUP_A = "GROUP_A";
32 static gpointer GROUP_B = "GROUP_B";
33
34 gchar *tabs1 [] = {
35   "aaaaaaaaaa",
36   "bbbbbbbbbb",
37   "cccccccccc",
38   "dddddddddd",
39   NULL
40 };
41
42 gchar *tabs2 [] = {
43   "1",
44   "2",
45   "3",
46   "4",
47   "55555",
48   NULL
49 };
50
51 gchar *tabs3 [] = {
52   "foo",
53   "bar",
54   NULL
55 };
56
57 gchar *tabs4 [] = {
58   "beer",
59   "water",
60   "lemonade",
61   "coffee",
62   "tea",
63   NULL
64 };
65
66 static const GtkTargetEntry button_targets[] = {
67   { "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, 0 },
68 };
69
70 static GtkNotebook*
71 window_creation_function (GtkNotebook *source_notebook,
72                           GtkWidget   *child,
73                           gint         x,
74                           gint         y,
75                           gpointer     data)
76 {
77   GtkWidget *window, *notebook;
78
79   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
80   notebook = gtk_notebook_new ();
81
82   gtk_notebook_set_group (GTK_NOTEBOOK (notebook),
83                           gtk_notebook_get_group (source_notebook));
84
85   gtk_container_add (GTK_CONTAINER (window), notebook);
86
87   gtk_window_set_default_size (GTK_WINDOW (window), 300, 300);
88   gtk_window_move (GTK_WINDOW (window), x, y);
89   gtk_widget_show_all (window);
90
91   return GTK_NOTEBOOK (notebook);
92 }
93
94 static void
95 on_page_reordered (GtkNotebook *notebook, GtkWidget *child, guint page_num, gpointer data)
96 {
97   g_print ("page %d reordered\n", page_num);
98 }
99
100 static void
101 on_notebook_drag_begin (GtkWidget      *widget,
102                         GdkDragContext *context,
103                         gpointer        data)
104 {
105   GdkPixbuf *pixbuf;
106   guint page_num;
107
108   page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (widget));
109
110   if (page_num > 2)
111     {
112       pixbuf = gtk_widget_render_icon (widget,
113                                    (page_num % 2) ? GTK_STOCK_HELP : GTK_STOCK_STOP,
114                                    GTK_ICON_SIZE_DND, NULL);
115
116       gtk_drag_set_icon_pixbuf (context, pixbuf, 0, 0);
117       g_object_unref (pixbuf);
118     }
119 }
120
121 static void
122 on_button_drag_data_received (GtkWidget        *widget,
123                               GdkDragContext   *context,
124                               gint              x,
125                               gint              y,
126                               GtkSelectionData *data,
127                               guint             info,
128                               guint             time,
129                               gpointer          user_data)
130 {
131   GtkWidget *source, *tab_label;
132   GtkWidget **child;
133
134   source = gtk_drag_get_source_widget (context);
135   child = (void*) data->data;
136
137   tab_label = gtk_notebook_get_tab_label (GTK_NOTEBOOK (source), *child);
138   g_print ("Removing tab: %s\n", gtk_label_get_text (GTK_LABEL (tab_label)));
139
140   gtk_container_remove (GTK_CONTAINER (source), *child);
141 }
142
143 static GtkWidget*
144 create_notebook (gchar           **labels,
145                  gpointer          group,
146                  gint              packing,
147                  GtkPositionType   pos)
148 {
149   GtkWidget *notebook, *title, *page;
150   gint count = 0;
151
152   notebook = gtk_notebook_new ();
153
154   gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), pos);
155   gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
156   gtk_container_set_border_width (GTK_CONTAINER (notebook), 6);
157   gtk_notebook_set_group (GTK_NOTEBOOK (notebook), group);
158
159   while (*labels)
160     {
161       page = gtk_entry_new ();
162       gtk_entry_set_text (GTK_ENTRY (page), *labels);
163
164       title = gtk_label_new (*labels);
165
166       gtk_notebook_append_page (GTK_NOTEBOOK (notebook), page, title);
167       gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (notebook), page, TRUE);
168       gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (notebook), page, TRUE);
169
170       if (packing == PACK_END ||
171           (packing == PACK_ALTERNATE && count % 2 == 1))
172         gtk_container_child_set (GTK_CONTAINER (notebook), page, "tab-pack", GTK_PACK_END, NULL);
173
174       count++;
175       labels++;
176     }
177
178   g_signal_connect (GTK_NOTEBOOK (notebook), "page-reordered",
179                     G_CALLBACK (on_page_reordered), NULL);
180   g_signal_connect_after (G_OBJECT (notebook), "drag-begin",
181                           G_CALLBACK (on_notebook_drag_begin), NULL);
182   return notebook;
183 }
184
185 static GtkWidget*
186 create_notebook_with_notebooks (gchar           **labels,
187                                 gpointer          group,
188                                 gint              packing,
189                                 GtkPositionType   pos)
190 {
191   GtkWidget *notebook, *title, *page;
192   gint count = 0;
193
194   notebook = gtk_notebook_new ();
195
196   gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), pos);
197   gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
198   gtk_container_set_border_width (GTK_CONTAINER (notebook), 6);
199   gtk_notebook_set_group (GTK_NOTEBOOK (notebook), group);
200
201   while (*labels)
202     {
203       page = create_notebook (labels, group, packing, pos);
204       gtk_notebook_popup_enable (GTK_NOTEBOOK (page));
205       
206       title = gtk_label_new (*labels);
207
208       gtk_notebook_append_page (GTK_NOTEBOOK (notebook), page, title);
209       gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (notebook), page, TRUE);
210       gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (notebook), page, TRUE);
211       
212       if (packing == PACK_END ||
213           (packing == PACK_ALTERNATE && count % 2 == 1))
214         gtk_container_child_set (GTK_CONTAINER (notebook), page, "tab-pack", GTK_PACK_END, NULL);
215
216       count++;
217       labels++;
218     }
219
220   g_signal_connect (GTK_NOTEBOOK (notebook), "page-reordered",
221                     G_CALLBACK (on_page_reordered), NULL);
222   g_signal_connect_after (G_OBJECT (notebook), "drag-begin",
223                           G_CALLBACK (on_notebook_drag_begin), NULL);
224   return notebook;
225 }
226
227 static GtkWidget*
228 create_trash_button (void)
229 {
230   GtkWidget *button;
231
232   button = gtk_button_new_from_stock (GTK_STOCK_DELETE);
233
234   gtk_drag_dest_set (button,
235                      GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP,
236                      button_targets,
237                      G_N_ELEMENTS (button_targets),
238                      GDK_ACTION_MOVE);
239
240   g_signal_connect_after (G_OBJECT (button), "drag-data-received",
241                           G_CALLBACK (on_button_drag_data_received), NULL);
242   return button;
243 }
244
245 gint
246 main (gint argc, gchar *argv[])
247 {
248   GtkWidget *window, *table;
249
250   gtk_init (&argc, &argv);
251
252   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
253   table = gtk_table_new (3, 2, FALSE);
254
255   gtk_notebook_set_window_creation_hook (window_creation_function, NULL, NULL);
256
257   gtk_table_attach_defaults (GTK_TABLE (table),
258                              create_notebook (tabs1, GROUP_A, PACK_ALTERNATE, GTK_POS_TOP),
259                              0, 1, 0, 1);
260
261   gtk_table_attach_defaults (GTK_TABLE (table),
262                              create_notebook (tabs2, GROUP_B, PACK_ALTERNATE, GTK_POS_BOTTOM),
263                              0, 1, 1, 2);
264
265   gtk_table_attach_defaults (GTK_TABLE (table),
266                              create_notebook (tabs3, GROUP_B, PACK_END, GTK_POS_LEFT),
267                              1, 2, 0, 1);
268
269   gtk_table_attach_defaults (GTK_TABLE (table),
270                              create_notebook_with_notebooks (tabs4, GROUP_A, PACK_ALTERNATE, GTK_POS_RIGHT),
271                              1, 2, 1, 2);
272
273   gtk_table_attach (GTK_TABLE (table),
274                     create_trash_button (), 1, 2, 2, 3,
275                     GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);
276
277   gtk_container_add (GTK_CONTAINER (window), table);
278   gtk_window_set_default_size (GTK_WINDOW (window), 400, 400);
279   gtk_widget_show_all (window);
280
281   gtk_main ();
282
283   return 0;
284 }