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*
window_creation_function(GtkNotebook * source_notebook,GtkWidget * child,gint x,gint y,gpointer data)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 g_signal_connect (notebook, "create-window",
82 G_CALLBACK (window_creation_function), NULL);
83
84 gtk_notebook_set_group_name (GTK_NOTEBOOK (notebook),
85 gtk_notebook_get_group_name (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
on_page_reordered(GtkNotebook * notebook,GtkWidget * child,guint page_num,gpointer data)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
on_notebook_drag_begin(GtkWidget * widget,GdkDragContext * context,gpointer data)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
on_button_drag_data_received(GtkWidget * widget,GdkDragContext * context,gint x,gint y,GtkSelectionData * data,guint info,guint time,gpointer user_data)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*
create_notebook(gchar ** labels,gpointer group,gint packing,GtkPositionType pos)146 create_notebook (gchar **labels,
147 gpointer group,
148 gint packing,
149 GtkPositionType pos)
150 {
151 GtkWidget *notebook, *title, *page;
152 gint count = 0;
153
154 notebook = gtk_notebook_new ();
155 g_signal_connect (notebook, "create-window",
156 G_CALLBACK (window_creation_function), NULL);
157
158 gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), pos);
159 gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
160 gtk_container_set_border_width (GTK_CONTAINER (notebook), 6);
161 gtk_notebook_set_group_name (GTK_NOTEBOOK (notebook), group);
162
163 while (*labels)
164 {
165 page = gtk_entry_new ();
166 gtk_entry_set_text (GTK_ENTRY (page), *labels);
167
168 title = gtk_label_new (*labels);
169
170 gtk_notebook_append_page (GTK_NOTEBOOK (notebook), page, title);
171 gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (notebook), page, TRUE);
172 gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (notebook), page, TRUE);
173
174 if (packing == PACK_END ||
175 (packing == PACK_ALTERNATE && count % 2 == 1))
176 gtk_container_child_set (GTK_CONTAINER (notebook), page, "tab-pack", GTK_PACK_END, NULL);
177
178 count++;
179 labels++;
180 }
181
182 g_signal_connect (GTK_NOTEBOOK (notebook), "page-reordered",
183 G_CALLBACK (on_page_reordered), NULL);
184 g_signal_connect_after (G_OBJECT (notebook), "drag-begin",
185 G_CALLBACK (on_notebook_drag_begin), NULL);
186 return notebook;
187 }
188
189 static GtkWidget*
create_notebook_with_notebooks(gchar ** labels,gpointer group,gint packing,GtkPositionType pos)190 create_notebook_with_notebooks (gchar **labels,
191 gpointer group,
192 gint packing,
193 GtkPositionType pos)
194 {
195 GtkWidget *notebook, *title, *page;
196 gint count = 0;
197
198 notebook = gtk_notebook_new ();
199 g_signal_connect (notebook, "create-window",
200 G_CALLBACK (window_creation_function), NULL);
201
202 gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), pos);
203 gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
204 gtk_container_set_border_width (GTK_CONTAINER (notebook), 6);
205 gtk_notebook_set_group_name (GTK_NOTEBOOK (notebook), group);
206
207 while (*labels)
208 {
209 page = create_notebook (labels, group, packing, pos);
210 gtk_notebook_popup_enable (GTK_NOTEBOOK (page));
211
212 title = gtk_label_new (*labels);
213
214 gtk_notebook_append_page (GTK_NOTEBOOK (notebook), page, title);
215 gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (notebook), page, TRUE);
216 gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (notebook), page, TRUE);
217
218 if (packing == PACK_END ||
219 (packing == PACK_ALTERNATE && count % 2 == 1))
220 gtk_container_child_set (GTK_CONTAINER (notebook), page, "tab-pack", GTK_PACK_END, NULL);
221
222 count++;
223 labels++;
224 }
225
226 g_signal_connect (GTK_NOTEBOOK (notebook), "page-reordered",
227 G_CALLBACK (on_page_reordered), NULL);
228 g_signal_connect_after (G_OBJECT (notebook), "drag-begin",
229 G_CALLBACK (on_notebook_drag_begin), NULL);
230 return notebook;
231 }
232
233 static GtkWidget*
create_trash_button(void)234 create_trash_button (void)
235 {
236 GtkWidget *button;
237
238 button = gtk_button_new_from_stock (GTK_STOCK_DELETE);
239
240 gtk_drag_dest_set (button,
241 GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP,
242 button_targets,
243 G_N_ELEMENTS (button_targets),
244 GDK_ACTION_MOVE);
245
246 g_signal_connect_after (G_OBJECT (button), "drag-data-received",
247 G_CALLBACK (on_button_drag_data_received), NULL);
248 return button;
249 }
250
251 gint
main(gint argc,gchar * argv[])252 main (gint argc, gchar *argv[])
253 {
254 GtkWidget *window, *table;
255
256 gtk_init (&argc, &argv);
257
258 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
259 table = gtk_table_new (3, 2, FALSE);
260
261 gtk_table_attach_defaults (GTK_TABLE (table),
262 create_notebook (tabs1, GROUP_A, PACK_ALTERNATE, GTK_POS_TOP),
263 0, 1, 0, 1);
264
265 gtk_table_attach_defaults (GTK_TABLE (table),
266 create_notebook (tabs2, GROUP_B, PACK_ALTERNATE, GTK_POS_BOTTOM),
267 0, 1, 1, 2);
268
269 gtk_table_attach_defaults (GTK_TABLE (table),
270 create_notebook (tabs3, GROUP_B, PACK_END, GTK_POS_LEFT),
271 1, 2, 0, 1);
272
273 gtk_table_attach_defaults (GTK_TABLE (table),
274 create_notebook_with_notebooks (tabs4, GROUP_A, PACK_ALTERNATE, GTK_POS_RIGHT),
275 1, 2, 1, 2);
276
277 gtk_table_attach (GTK_TABLE (table),
278 create_trash_button (), 1, 2, 2, 3,
279 GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);
280
281 gtk_container_add (GTK_CONTAINER (window), table);
282 gtk_window_set_default_size (GTK_WINDOW (window), 400, 400);
283 gtk_widget_show_all (window);
284
285 gtk_main ();
286
287 return 0;
288 }
289