]> Pileus Git - ~andy/gtk/blob - tests/testrecentchooser.c
Use gtk_button_box_new() instead gtk_[v|h]_button_box_new()
[~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, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "config.h"
21
22 #include <string.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <stdlib.h>
26 #include <time.h>
27 #ifdef HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30 #include <gtk/gtk.h>
31
32 #ifdef G_OS_WIN32
33 # include <io.h>
34 # define localtime_r(t,b) *(b) = localtime (t)
35 # ifndef S_ISREG
36 #  define S_ISREG(m) ((m) & _S_IFREG)
37 # endif
38 #endif
39
40 #include "prop-editor.h"
41
42 static void
43 print_current_item (GtkRecentChooser *chooser)
44 {
45   gchar *uri;
46
47   uri = gtk_recent_chooser_get_current_uri (chooser);
48   g_print ("Current item changed :\n  %s\n", uri ? uri : "null");
49   g_free (uri);
50 }
51
52 static void
53 print_selected (GtkRecentChooser *chooser)
54 {
55   gsize uris_len, i;
56   gchar **uris = gtk_recent_chooser_get_uris (chooser, &uris_len);
57
58   g_print ("Selection changed :\n");
59   for (i = 0; i < uris_len; i++)
60     g_print ("  %s\n", uris[i]);
61   g_print ("\n");
62
63   g_strfreev (uris);
64 }
65
66 static void
67 response_cb (GtkDialog *dialog,
68              gint       response_id)
69 {
70   if (response_id == GTK_RESPONSE_OK)
71     {
72     }
73   else
74     g_print ("Dialog was closed\n");
75
76   gtk_main_quit ();
77 }
78
79 static void
80 filter_changed (GtkRecentChooserDialog *dialog,
81                 gpointer                data)
82 {
83   g_print ("recent filter changed\n");
84 }
85
86 static void
87 notify_multiple_cb (GtkWidget  *dialog,
88                     GParamSpec *pspec,
89                     GtkWidget  *button)
90 {
91   gboolean multiple;
92
93   multiple = gtk_recent_chooser_get_select_multiple (GTK_RECENT_CHOOSER (dialog));
94
95   gtk_widget_set_sensitive (button, multiple);
96 }
97
98 static void
99 kill_dependent (GtkWindow *win,
100                 GtkWidget *dep)
101 {
102   gtk_widget_destroy (dep);
103   g_object_unref (dep);
104 }
105
106 int
107 main (int   argc,
108       char *argv[])
109 {
110   GtkWidget *control_window;
111   GtkWidget *vbbox;
112   GtkWidget *button;
113   GtkWidget *dialog;
114   GtkWidget *prop_editor;
115   GtkRecentFilter *filter;
116   gint i;
117   gboolean multiple = FALSE;
118   
119   gtk_init (&argc, &argv);
120
121   /* to test rtl layout, set RTL=1 in the environment */
122   if (g_getenv ("RTL"))
123     gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
124
125   for (i = 1; i < argc; i++)
126     {
127       if (!strcmp ("--multiple", argv[i]))
128         multiple = TRUE;
129     }
130
131   dialog = g_object_new (GTK_TYPE_RECENT_CHOOSER_DIALOG,
132                          "select-multiple", multiple,
133                          "show-tips", TRUE,
134                          "show-icons", TRUE,
135                          NULL);
136   gtk_window_set_title (GTK_WINDOW (dialog), "Select a file");
137   gtk_dialog_add_buttons (GTK_DIALOG (dialog),
138                           GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
139                           GTK_STOCK_OPEN, GTK_RESPONSE_OK,
140                           NULL);
141   
142   gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
143
144   g_signal_connect (dialog, "item-activated",
145                     G_CALLBACK (print_current_item), NULL);
146   g_signal_connect (dialog, "selection-changed",
147                     G_CALLBACK (print_selected), NULL);
148   g_signal_connect (dialog, "response",
149                     G_CALLBACK (response_cb), NULL);
150   
151   /* filters */
152   filter = gtk_recent_filter_new ();
153   gtk_recent_filter_set_name (filter, "All Files");
154   gtk_recent_filter_add_pattern (filter, "*");
155   gtk_recent_chooser_add_filter (GTK_RECENT_CHOOSER (dialog), filter);
156
157   filter = gtk_recent_filter_new ();
158   gtk_recent_filter_set_name (filter, "Only PDF Files");
159   gtk_recent_filter_add_mime_type (filter, "application/pdf");
160   gtk_recent_chooser_add_filter (GTK_RECENT_CHOOSER (dialog), filter);
161
162   g_signal_connect (dialog, "notify::filter",
163                     G_CALLBACK (filter_changed), NULL);
164
165   gtk_recent_chooser_set_filter (GTK_RECENT_CHOOSER (dialog), filter);
166
167   filter = gtk_recent_filter_new ();
168   gtk_recent_filter_set_name (filter, "PNG and JPEG");
169   gtk_recent_filter_add_mime_type (filter, "image/png");
170   gtk_recent_filter_add_mime_type (filter, "image/jpeg");
171   gtk_recent_chooser_add_filter (GTK_RECENT_CHOOSER (dialog), filter);
172
173   gtk_widget_show_all (dialog);
174
175   prop_editor = create_prop_editor (G_OBJECT (dialog), GTK_TYPE_RECENT_CHOOSER);
176
177   control_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
178
179   vbbox = gtk_button_box_new (GTK_ORIENTATION_VERTICAL);
180   gtk_container_add (GTK_CONTAINER (control_window), vbbox);
181
182   button = gtk_button_new_with_mnemonic ("_Select all");
183   gtk_widget_set_sensitive (button, multiple);
184   gtk_container_add (GTK_CONTAINER (vbbox), button);
185   g_signal_connect_swapped (button, "clicked",
186                             G_CALLBACK (gtk_recent_chooser_select_all), dialog);
187   g_signal_connect (dialog, "notify::select-multiple",
188                     G_CALLBACK (notify_multiple_cb), button);
189
190   button = gtk_button_new_with_mnemonic ("_Unselect all");
191   gtk_container_add (GTK_CONTAINER (vbbox), button);
192   g_signal_connect_swapped (button, "clicked",
193                             G_CALLBACK (gtk_recent_chooser_unselect_all), dialog);
194
195   gtk_widget_show_all (control_window);
196   
197   g_object_ref (control_window);
198   g_signal_connect (dialog, "destroy",
199                     G_CALLBACK (kill_dependent), control_window);
200   
201   g_object_ref (dialog);
202   gtk_main ();
203   gtk_widget_destroy (dialog);
204   g_object_unref (dialog);
205
206   return 0;
207 }