]> Pileus Git - ~andy/gtk/blob - tests/testnotebookdnd.c
missing #include <ctype.h>.
[~andy/gtk] / tests / testnotebookdnd.c
1 /* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
2 /* 
3  * GTK - The GIMP 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
205       title = gtk_label_new (*labels);
206
207       gtk_notebook_append_page (GTK_NOTEBOOK (notebook), page, title);
208       gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (notebook), page, TRUE);
209       gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (notebook), page, TRUE);
210
211       if (packing == PACK_END ||
212           (packing == PACK_ALTERNATE && count % 2 == 1))
213         gtk_container_child_set (GTK_CONTAINER (notebook), page, "tab-pack", GTK_PACK_END, NULL);
214
215       count++;
216       labels++;
217     }
218
219   g_signal_connect (GTK_NOTEBOOK (notebook), "page-reordered",
220                     G_CALLBACK (on_page_reordered), NULL);
221   g_signal_connect_after (G_OBJECT (notebook), "drag-begin",
222                           G_CALLBACK (on_notebook_drag_begin), NULL);
223   return notebook;
224 }
225
226 static GtkWidget*
227 create_trash_button (void)
228 {
229   GtkWidget *button;
230
231   button = gtk_button_new_from_stock (GTK_STOCK_DELETE);
232
233   gtk_drag_dest_set (button,
234                      GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP,
235                      button_targets,
236                      G_N_ELEMENTS (button_targets),
237                      GDK_ACTION_MOVE);
238
239   g_signal_connect_after (G_OBJECT (button), "drag-data-received",
240                           G_CALLBACK (on_button_drag_data_received), NULL);
241   return button;
242 }
243
244 gint
245 main (gint argc, gchar *argv[])
246 {
247   GtkWidget *window, *table;
248
249   gtk_init (&argc, &argv);
250
251   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
252   table = gtk_table_new (3, 2, FALSE);
253
254   gtk_notebook_set_window_creation_hook (window_creation_function, NULL, NULL);
255
256   gtk_table_attach_defaults (GTK_TABLE (table),
257                              create_notebook (tabs1, GROUP_A, PACK_ALTERNATE, GTK_POS_TOP),
258                              0, 1, 0, 1);
259
260   gtk_table_attach_defaults (GTK_TABLE (table),
261                              create_notebook (tabs2, GROUP_B, PACK_ALTERNATE, GTK_POS_BOTTOM),
262                              0, 1, 1, 2);
263
264   gtk_table_attach_defaults (GTK_TABLE (table),
265                              create_notebook (tabs3, GROUP_B, PACK_END, GTK_POS_LEFT),
266                              1, 2, 0, 1);
267
268   gtk_table_attach_defaults (GTK_TABLE (table),
269                              create_notebook_with_notebooks (tabs4, GROUP_A, PACK_ALTERNATE, GTK_POS_RIGHT),
270                              1, 2, 1, 2);
271
272   gtk_table_attach (GTK_TABLE (table),
273                     create_trash_button (), 1, 2, 2, 3,
274                     GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);
275
276   gtk_container_add (GTK_CONTAINER (window), table);
277   gtk_window_set_default_size (GTK_WINDOW (window), 400, 400);
278   gtk_widget_show_all (window);
279
280   gtk_main ();
281
282   return 0;
283 }