]> Pileus Git - ~andy/gtk/blob - tests/testnotebookdnd.c
Update callers
[~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 enum {
32   GROUP_A,
33   GROUP_B
34 };
35
36 gchar *tabs1 [] = {
37   "a",
38   "b",
39   "c",
40   "d",
41   NULL
42 };
43
44 gchar *tabs2 [] = {
45   "1",
46   "2",
47   "3",
48   "4",
49   "5",
50   NULL
51 };
52
53 gchar *tabs3 [] = {
54   "foo",
55   "bar",
56   NULL
57 };
58
59 gchar *tabs4 [] = {
60   "beer",
61   "water",
62   "lemonade",
63   "coffee",
64   "tea",
65   NULL
66 };
67
68 static const GtkTargetEntry button_targets[] = {
69   { "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, 0 },
70 };
71
72 static GtkNotebook*
73 window_creation_function (GtkNotebook *source_notebook,
74                           GtkWidget   *child,
75                           gint         x,
76                           gint         y,
77                           gpointer     data)
78 {
79   GtkWidget *window, *notebook;
80
81   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
82   notebook = gtk_notebook_new ();
83
84   gtk_notebook_set_group_id (GTK_NOTEBOOK (notebook),
85                              gtk_notebook_get_group_id (source_notebook));
86
87   gtk_container_add (GTK_CONTAINER (window), notebook);
88
89   gtk_window_set_default_size (GTK_WINDOW (window), 300, 300);
90   gtk_window_move (GTK_WINDOW (window), x, y);
91   gtk_widget_show_all (window);
92
93   return GTK_NOTEBOOK (notebook);
94 }
95
96 static void
97 on_page_reordered (GtkNotebook *notebook, GtkWidget *child, guint page_num, gpointer data)
98 {
99   g_print ("page %d reordered\n", page_num);
100 }
101
102 static void
103 on_notebook_drag_begin (GtkWidget      *widget,
104                         GdkDragContext *context,
105                         gpointer        data)
106 {
107   GdkPixbuf *pixbuf;
108   guint page_num;
109
110   page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (widget));
111
112   if (page_num > 2)
113     {
114       pixbuf = gtk_widget_render_icon (widget,
115                                    (page_num % 2) ? GTK_STOCK_HELP : GTK_STOCK_STOP,
116                                    GTK_ICON_SIZE_DND, NULL);
117
118       gtk_drag_set_icon_pixbuf (context, pixbuf, 0, 0);
119       g_object_unref (pixbuf);
120     }
121 }
122
123 static void
124 on_button_drag_data_received (GtkWidget        *widget,
125                               GdkDragContext   *context,
126                               gint              x,
127                               gint              y,
128                               GtkSelectionData *data,
129                               guint             info,
130                               guint             time,
131                               gpointer          user_data)
132 {
133   GtkWidget *source, *tab_label;
134   GtkWidget **child;
135
136   source = gtk_drag_get_source_widget (context);
137   child = (void*) data->data;
138
139   tab_label = gtk_notebook_get_tab_label (GTK_NOTEBOOK (source), *child);
140   g_print ("Removing tab: %s\n", gtk_label_get_text (GTK_LABEL (tab_label)));
141
142   gtk_container_remove (GTK_CONTAINER (source), *child);
143 }
144
145 static GtkWidget*
146 create_notebook (gchar           **labels,
147                  gint              group_id,
148                  gint              packing,
149                  GtkPositionType   pos)
150 {
151   GtkWidget *notebook, *title, *page;
152   gint count = 0;
153
154   notebook = gtk_notebook_new ();
155
156   gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), pos);
157   gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
158   gtk_container_set_border_width (GTK_CONTAINER (notebook), 6);
159   gtk_notebook_set_group_id (GTK_NOTEBOOK (notebook), group_id);
160
161   while (*labels)
162     {
163       page = gtk_entry_new ();
164       gtk_entry_set_text (GTK_ENTRY (page), *labels);
165
166       title = gtk_label_new (*labels);
167
168       gtk_notebook_append_page (GTK_NOTEBOOK (notebook), page, title);
169       gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (notebook), page, TRUE);
170       gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (notebook), page, TRUE);
171
172       if (packing == PACK_END ||
173           (packing == PACK_ALTERNATE && count % 2 == 1))
174         gtk_container_child_set (GTK_CONTAINER (notebook), page, "tab-pack", GTK_PACK_END, NULL);
175
176       count++;
177       labels++;
178     }
179
180   g_signal_connect (GTK_NOTEBOOK (notebook), "page-reordered",
181                     G_CALLBACK (on_page_reordered), NULL);
182   g_signal_connect_after (G_OBJECT (notebook), "drag-begin",
183                           G_CALLBACK (on_notebook_drag_begin), NULL);
184   return notebook;
185 }
186
187 static GtkWidget*
188 create_trash_button (void)
189 {
190   GtkWidget *button;
191
192   button = gtk_button_new_from_stock (GTK_STOCK_DELETE);
193
194   gtk_drag_dest_set (button,
195                      GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP,
196                      button_targets,
197                      G_N_ELEMENTS (button_targets),
198                      GDK_ACTION_MOVE);
199
200   g_signal_connect_after (G_OBJECT (button), "drag-data-received",
201                           G_CALLBACK (on_button_drag_data_received), NULL);
202   return button;
203 }
204
205 gint
206 main (gint argc, gchar *argv[])
207 {
208   GtkWidget *window, *table;
209
210   gtk_init (&argc, &argv);
211
212   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
213   table = gtk_table_new (3, 2, FALSE);
214
215   gtk_notebook_set_window_creation_hook (window_creation_function, NULL, NULL);
216
217   gtk_table_attach_defaults (GTK_TABLE (table),
218                              create_notebook (tabs1, GROUP_A, PACK_START, GTK_POS_TOP),
219                              0, 1, 0, 1);
220
221   gtk_table_attach_defaults (GTK_TABLE (table),
222                              create_notebook (tabs2, GROUP_B, PACK_ALTERNATE, GTK_POS_BOTTOM),
223                              0, 1, 1, 2);
224
225   gtk_table_attach_defaults (GTK_TABLE (table),
226                              create_notebook (tabs3, GROUP_B, PACK_END, GTK_POS_LEFT),
227                              1, 2, 0, 1);
228
229   gtk_table_attach_defaults (GTK_TABLE (table),
230                              create_notebook (tabs4, GROUP_A, PACK_ALTERNATE, GTK_POS_RIGHT),
231                              1, 2, 1, 2);
232
233   gtk_table_attach (GTK_TABLE (table),
234                     create_trash_button (), 1, 2, 2, 3,
235                     GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);
236
237   gtk_container_add (GTK_CONTAINER (window), table);
238   gtk_window_set_default_size (GTK_WINDOW (window), 400, 400);
239   gtk_widget_show_all (window);
240
241   gtk_main ();
242
243   return 0;
244 }