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