]> Pileus Git - ~andy/gtk/blob - tests/testrecentchooser.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / tests / testrecentchooser.c
1 /* testrecentchooser.c
2  * Copyright (C) 2006  Emmanuele Bassi.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "config.h"
19
20 #include <string.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <stdlib.h>
24 #include <time.h>
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28 #include <gtk/gtk.h>
29
30 #ifdef G_OS_WIN32
31 # include <io.h>
32 # define localtime_r(t,b) *(b) = localtime (t)
33 # ifndef S_ISREG
34 #  define S_ISREG(m) ((m) & _S_IFREG)
35 # endif
36 #endif
37
38 #include "prop-editor.h"
39
40 static void
41 print_current_item (GtkRecentChooser *chooser)
42 {
43   gchar *uri;
44
45   uri = gtk_recent_chooser_get_current_uri (chooser);
46   g_print ("Current item changed :\n  %s\n", uri ? uri : "null");
47   g_free (uri);
48 }
49
50 static void
51 print_selected (GtkRecentChooser *chooser)
52 {
53   gsize uris_len, i;
54   gchar **uris = gtk_recent_chooser_get_uris (chooser, &uris_len);
55
56   g_print ("Selection changed :\n");
57   for (i = 0; i < uris_len; i++)
58     g_print ("  %s\n", uris[i]);
59   g_print ("\n");
60
61   g_strfreev (uris);
62 }
63
64 static void
65 response_cb (GtkDialog *dialog,
66              gint       response_id)
67 {
68   if (response_id == GTK_RESPONSE_OK)
69     {
70     }
71   else
72     g_print ("Dialog was closed\n");
73
74   gtk_main_quit ();
75 }
76
77 static void
78 filter_changed (GtkRecentChooserDialog *dialog,
79                 gpointer                data)
80 {
81   g_print ("recent filter changed\n");
82 }
83
84 static void
85 notify_multiple_cb (GtkWidget  *dialog,
86                     GParamSpec *pspec,
87                     GtkWidget  *button)
88 {
89   gboolean multiple;
90
91   multiple = gtk_recent_chooser_get_select_multiple (GTK_RECENT_CHOOSER (dialog));
92
93   gtk_widget_set_sensitive (button, multiple);
94 }
95
96 static void
97 kill_dependent (GtkWindow *win,
98                 GtkWidget *dep)
99 {
100   gtk_widget_destroy (dep);
101   g_object_unref (dep);
102 }
103
104 int
105 main (int   argc,
106       char *argv[])
107 {
108   GtkWidget *control_window;
109   GtkWidget *vbbox;
110   GtkWidget *button;
111   GtkWidget *dialog;
112   GtkRecentFilter *filter;
113   gint i;
114   gboolean multiple = FALSE;
115   
116   gtk_init (&argc, &argv);
117
118   /* to test rtl layout, set RTL=1 in the environment */
119   if (g_getenv ("RTL"))
120     gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
121
122   for (i = 1; i < argc; i++)
123     {
124       if (!strcmp ("--multiple", argv[i]))
125         multiple = TRUE;
126     }
127
128   dialog = g_object_new (GTK_TYPE_RECENT_CHOOSER_DIALOG,
129                          "select-multiple", multiple,
130                          "show-tips", TRUE,
131                          "show-icons", TRUE,
132                          NULL);
133   gtk_window_set_title (GTK_WINDOW (dialog), "Select a file");
134   gtk_dialog_add_buttons (GTK_DIALOG (dialog),
135                           GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
136                           GTK_STOCK_OPEN, GTK_RESPONSE_OK,
137                           NULL);
138   
139   gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
140
141   g_signal_connect (dialog, "item-activated",
142                     G_CALLBACK (print_current_item), NULL);
143   g_signal_connect (dialog, "selection-changed",
144                     G_CALLBACK (print_selected), NULL);
145   g_signal_connect (dialog, "response",
146                     G_CALLBACK (response_cb), NULL);
147   
148   /* filters */
149   filter = gtk_recent_filter_new ();
150   gtk_recent_filter_set_name (filter, "All Files");
151   gtk_recent_filter_add_pattern (filter, "*");
152   gtk_recent_chooser_add_filter (GTK_RECENT_CHOOSER (dialog), filter);
153
154   filter = gtk_recent_filter_new ();
155   gtk_recent_filter_set_name (filter, "Only PDF Files");
156   gtk_recent_filter_add_mime_type (filter, "application/pdf");
157   gtk_recent_chooser_add_filter (GTK_RECENT_CHOOSER (dialog), filter);
158
159   g_signal_connect (dialog, "notify::filter",
160                     G_CALLBACK (filter_changed), NULL);
161
162   gtk_recent_chooser_set_filter (GTK_RECENT_CHOOSER (dialog), filter);
163
164   filter = gtk_recent_filter_new ();
165   gtk_recent_filter_set_name (filter, "PNG and JPEG");
166   gtk_recent_filter_add_mime_type (filter, "image/png");
167   gtk_recent_filter_add_mime_type (filter, "image/jpeg");
168   gtk_recent_chooser_add_filter (GTK_RECENT_CHOOSER (dialog), filter);
169
170   gtk_widget_show_all (dialog);
171
172   create_prop_editor (G_OBJECT (dialog), GTK_TYPE_RECENT_CHOOSER);
173
174   control_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
175
176   vbbox = gtk_button_box_new (GTK_ORIENTATION_VERTICAL);
177   gtk_container_add (GTK_CONTAINER (control_window), vbbox);
178
179   button = gtk_button_new_with_mnemonic ("_Select all");
180   gtk_widget_set_sensitive (button, multiple);
181   gtk_container_add (GTK_CONTAINER (vbbox), button);
182   g_signal_connect_swapped (button, "clicked",
183                             G_CALLBACK (gtk_recent_chooser_select_all), dialog);
184   g_signal_connect (dialog, "notify::select-multiple",
185                     G_CALLBACK (notify_multiple_cb), button);
186
187   button = gtk_button_new_with_mnemonic ("_Unselect all");
188   gtk_container_add (GTK_CONTAINER (vbbox), button);
189   g_signal_connect_swapped (button, "clicked",
190                             G_CALLBACK (gtk_recent_chooser_unselect_all), dialog);
191
192   gtk_widget_show_all (control_window);
193   
194   g_object_ref (control_window);
195   g_signal_connect (dialog, "destroy",
196                     G_CALLBACK (kill_dependent), control_window);
197   
198   g_object_ref (dialog);
199   gtk_main ();
200   gtk_widget_destroy (dialog);
201   g_object_unref (dialog);
202
203   return 0;
204 }