Back to index

bamf  0.2.120
alt-tabber.c
Go to the documentation of this file.
00001 /*
00002  *
00003  * Copyright (C) 2009 - Canonical Ltd.
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 3 of the License
00008  *
00009  * Bamf is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin St, Fifth Floor,
00017  * Boston, MA  02110-1301  USA
00018  */
00019 
00020 #include <stdio.h>
00021 #include <gtk/gtk.h>
00022 #include <gdk/gdk.h>
00023 #include <glib.h>
00024 #include <libbamf/libbamf.h>
00025 
00026 GtkWidget *window;
00027 GtkWidget *treeView;
00028 guint timer;
00029 
00030 typedef struct _Entry {
00031   GtkTreeStore* store;
00032   GtkTreeIter   child;
00033 } Entry;
00034 
00035 void
00036 on_name_changed (BamfView* bamfview,
00037                  gchar*    old_name,
00038                  gchar*    new_name,
00039                  Entry*    entry)
00040 {
00041   gtk_tree_store_set (entry->store, &entry->child, 0, new_name, -1);
00042 }
00043 
00044 void populate_tree_store (GtkTreeStore *store)
00045 {
00046   GtkTreeIter position; 
00047   GtkTreeIter child;
00048   GList *children, *c; 
00049   GList *apps, *l;
00050   BamfApplication *app;
00051   BamfWindow *window;
00052   const char *filename;
00053   Entry* entry = NULL;
00054 
00055   apps = bamf_matcher_get_applications (bamf_matcher_get_default ());
00056 
00057   for (l = apps; l; l = l->next) 
00058   {
00059     app = BAMF_APPLICATION (l->data);
00060     
00061     if (!bamf_view_user_visible (BAMF_VIEW (app)))
00062       continue;
00063 
00064     gtk_tree_store_append (store, &position, NULL);
00065     filename = bamf_view_get_name (BAMF_VIEW (app));
00066     gtk_tree_store_set (store, &position, 0, filename, -1);
00067 
00068     children = bamf_view_get_children (BAMF_VIEW (app));
00069     
00070     g_print("%i    %s\n", g_list_length (children), filename);
00071 
00072     for (c = children; c; c = c->next) 
00073     {
00074       if (BAMF_IS_WINDOW (c->data))
00075         {
00076           window = BAMF_WINDOW (c->data);
00077 
00078           if (!bamf_view_user_visible (BAMF_VIEW (window)))
00079             continue;
00080 
00081           const gchar *name = bamf_view_get_name (BAMF_VIEW (window));
00082           gtk_tree_store_append (store, &child, &position);
00083           gtk_tree_store_set (store, &child, 0, name, -1);
00084 
00085           // the following is just to make the tree-store correctly update with
00086          // any changes happening to titles during tab-changes, the g_new0()
00087          // has no corresponding g_free() thus is leaking memory, don't do that
00088          // in production-level code, this is just a small test
00089          entry = g_new0 (Entry, 1); // this is leaking memory
00090           entry->store = store;
00091          entry->child = child;
00092           g_signal_connect (G_OBJECT (window),
00093                             "name-changed",
00094                             (GCallback) on_name_changed,
00095                             entry);
00096         }
00097       else if (BAMF_IS_INDICATOR (c->data))
00098         {
00099           const gchar *path = bamf_indicator_get_dbus_menu_path (BAMF_INDICATOR (c->data));
00100           gtk_tree_store_append (store, &child, &position);
00101           gtk_tree_store_set (store, &child, 0, path, -1);
00102         }
00103     }
00104   }
00105 }
00106 
00107 GtkWidget * make_tree_view ()
00108 {
00109   GtkWidget *treeView;
00110   GtkTreeStore *treeStore;
00111   GtkTreeViewColumn *column;
00112   GtkCellRenderer *renderer;
00113 
00114   treeView = gtk_tree_view_new ();
00115   treeStore = gtk_tree_store_new (1, G_TYPE_STRING);
00116 
00117   column = gtk_tree_view_column_new ();
00118   gtk_tree_view_column_set_title (column, "windows");
00119   gtk_tree_view_append_column (GTK_TREE_VIEW (treeView), column);
00120 
00121   renderer = gtk_cell_renderer_text_new ();
00122   gtk_tree_view_column_pack_start (column, renderer, TRUE);
00123   gtk_tree_view_column_add_attribute(column, renderer, "text", 0);
00124 
00125   gtk_tree_view_set_model (GTK_TREE_VIEW (treeView), GTK_TREE_MODEL (treeStore));
00126 
00127   populate_tree_store (treeStore);
00128   
00129   g_object_unref (treeStore);
00130 
00131   return treeView;
00132 }
00133 
00134 static void destroy (GtkWidget *widget, gpointer data)
00135 {
00136   gtk_main_quit ();
00137 }
00138 
00139 static void handle_view_opened (BamfMatcher *matcher, BamfView *view, gpointer data)
00140 {
00141   g_print ("VIEW OPENED\n");
00142 }
00143 
00144 /* This entire program is intentionally implemented in a slightly niave manner to allow it to show
00145  * the function of both of the primary calls to libbamf. There is usually no reason to use
00146  * both as one or the other can generally get the job done. Experienced coders should recognize how
00147  * to do this right away, however it is noted where it could be performed in the source.
00148  */
00149 int main (int argc, char **argv)
00150 {
00151   BamfMatcher *matcher;
00152   gtk_init (&argc, &argv);
00153 
00154   matcher = bamf_matcher_get_default ();
00155 
00156   g_signal_connect (G_OBJECT (matcher), "view-opened",
00157                    (GCallback) handle_view_opened, NULL);
00158 
00159   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
00160 
00161   g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (destroy), NULL);
00162 
00163   treeView = make_tree_view ();
00164   gtk_container_add (GTK_CONTAINER (window), treeView);
00165   
00166   gchar *favs [] = { "/usr/share/applications/evolution-mail.desktop", NULL };
00167   bamf_matcher_register_favorites (matcher, (const gchar **) favs);
00168 
00169   gtk_widget_show_all (window);
00170   gtk_main ();
00171   return 0;
00172 }
00173 
00174 
00175 
00176 
00177 
00178 
00179