* examples/hildon-bread-crumb-trail-example.c
[hildon] / examples / hildon-bread-crumb-trail-example.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2007 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
7  * Author: Xan Lopez <xan.lopez@nokia.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; version 2.1 of
12  * the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22  * 02110-1301 USA
23  *
24  */
25
26 #include <gtk/gtk.h>
27 #include "hildon-bread-crumb-trail.h"
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31
32 #if 1
33 #include <hildon/hildon.h>
34 #endif
35
36 GtkWidget *treeview;
37 gchar *current_root;
38
39 enum {
40   ICON_COL,
41   STRING_COL,
42   IS_DIR_COL,
43   N_COLS
44 };
45
46 static void
47 populate_store (GtkListStore *store,
48                 const gchar *path)
49 {
50   GDir *dir;
51   GError *error = NULL;
52   gchar *item;
53   GtkTreeIter iter;
54   struct stat stat_info;
55   GdkPixbuf *pixbuf = NULL;
56
57   dir = g_dir_open (path, 0, &error);
58   if (error)
59     {
60       g_debug ("Error populating store: %s", error->message);
61       g_error_free (error);
62       return;
63     }
64
65   while ((item = (gchar*)g_dir_read_name (dir)) != NULL)
66     {
67       gchar *file_path = g_strconcat (path, "/", item, NULL);
68
69       if (stat (file_path, &stat_info) == -1)
70         {
71           g_debug ("error retrieving stat info for %s", item);
72           continue;
73         }
74       g_free (file_path);
75
76       gtk_list_store_append (store, &iter);
77
78       if (S_ISDIR (stat_info.st_mode))
79         pixbuf = gdk_pixbuf_new_from_file ("./gnome-fs-directory.png", NULL);
80       else
81         pixbuf = gdk_pixbuf_new_from_file ("./gnome-mime-text.png", NULL);
82
83       gtk_list_store_set (store, &iter,
84                           ICON_COL, pixbuf,
85                           STRING_COL, item,
86                           IS_DIR_COL, S_ISDIR (stat_info.st_mode) ? TRUE : FALSE,
87                           -1);
88       if (pixbuf)
89         g_object_unref (pixbuf);
90     }
91
92   g_dir_close (dir);
93
94   return;
95 }
96
97 static void
98 free_id (gpointer data)
99 {
100   g_debug ("Freeing ID data");
101   g_free (data);
102 }
103
104 static void
105 row_activated_cb (GtkTreeView *treeview,
106                   GtkTreePath *path,
107                   GtkTreeViewColumn *column,
108                   HildonBreadCrumbTrail *bct)
109 {
110   gchar *text = NULL, *new_root;
111   GtkTreeIter iter;
112   GtkTreeModel *model;
113   gboolean is_dir;
114
115   model = gtk_tree_view_get_model (treeview);
116   gtk_tree_model_get_iter (model, &iter, path);
117   gtk_tree_model_get (model, &iter,
118                       STRING_COL, &text,
119                       IS_DIR_COL, &is_dir,
120                       -1);
121
122   if (is_dir == FALSE) goto out;
123
124   g_debug ("Clicked %s", text);
125
126   new_root = g_strconcat (g_str_equal (current_root, "/")? "" : current_root, "/", text, NULL);
127   gtk_list_store_clear (GTK_LIST_STORE (model));
128   populate_store (GTK_LIST_STORE (model), new_root);
129
130   hildon_bread_crumb_trail_push_text (bct, text, new_root, (GDestroyNotify)free_id);
131   
132   if (current_root)
133     {
134       g_free (current_root);
135     }
136
137   current_root = g_strdup (new_root);
138
139  out:
140   g_free (text);
141 }
142
143 static void
144 crumb_clicked_cb (HildonBreadCrumbTrail *bct, gpointer id)
145 {
146   GtkTreeModel *model;
147   gchar *text = (gchar*)id;
148
149   g_debug ("item %s clicked", text);
150   model = gtk_tree_view_get_model (GTK_TREE_VIEW (treeview));
151   gtk_list_store_clear (GTK_LIST_STORE (model));
152   populate_store (GTK_LIST_STORE (model), text);
153   if (current_root)
154     g_free (current_root);
155   current_root = g_strdup (text);
156 }
157
158 int main (int argc, char **argv)
159 {
160 #if 1
161   HildonProgram *program;
162 #endif
163   GtkListStore *store;
164   GtkWidget *window, *scrolled_window, *vbox, *bct;
165   GtkCellRenderer *renderer;
166   GtkTreeViewColumn *column;
167
168   gtk_init (&argc, &argv);
169
170   /* Main window */
171 #if 1
172   program = hildon_program_get_instance ();
173   window = hildon_window_new ();
174   hildon_program_add_window (program, HILDON_WINDOW (window));
175 #else
176   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
177 #endif
178   gtk_container_set_border_width (GTK_CONTAINER (window), 2);
179   gtk_window_set_default_size (GTK_WINDOW (window), 400, 600);
180   g_signal_connect (window, "delete-event", gtk_main_quit, NULL);
181
182   vbox = gtk_vbox_new (FALSE, 3);
183   gtk_container_add (GTK_CONTAINER (window), vbox);
184   gtk_widget_show (vbox);
185
186   current_root = g_strdup ("/");
187
188   bct = hildon_bread_crumb_trail_new ();
189   g_signal_connect (bct, "crumb-clicked", G_CALLBACK (crumb_clicked_cb), NULL);
190   gtk_box_pack_start (GTK_BOX (vbox), bct, FALSE, FALSE, 0);
191   gtk_widget_show (bct);
192
193   hildon_bread_crumb_trail_push_text (HILDON_BREAD_CRUMB_TRAIL (bct), "/",
194                                       g_strdup ("/"), (GDestroyNotify)free_id);
195
196   /* Treeview */
197   scrolled_window = gtk_scrolled_window_new (NULL, NULL);
198   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
199                                   GTK_POLICY_AUTOMATIC,
200                                   GTK_POLICY_AUTOMATIC);
201   store = gtk_list_store_new (N_COLS, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_BOOLEAN);
202   populate_store (store, "/");
203   treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
204   g_signal_connect (treeview, "row-activated", G_CALLBACK (row_activated_cb), bct);
205
206   renderer = gtk_cell_renderer_pixbuf_new ();
207   column = gtk_tree_view_column_new_with_attributes ("Icon",
208                                                      renderer,
209                                                      "pixbuf", ICON_COL,
210                                                      NULL);
211   gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
212
213   renderer = gtk_cell_renderer_text_new ();
214   column = gtk_tree_view_column_new_with_attributes ("Name",
215                                                      renderer,
216                                                      "text", STRING_COL,
217                                                      NULL);
218   gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
219
220   gtk_container_add (GTK_CONTAINER (scrolled_window), treeview);
221   gtk_widget_show (treeview);
222   gtk_box_pack_start (GTK_BOX (vbox), scrolled_window, TRUE, TRUE, 0);
223   gtk_widget_show (scrolled_window);
224
225   gtk_widget_show (window);
226
227   gtk_main ();
228
229   return 0;
230 }