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