"Fossies" - the Fresh Open Source Software Archive

Member "gentoo-0.20.7/src/hash_dialog.c" (23 Apr 2016, 4134 Bytes) of package /linux/misc/gentoo-0.20.7.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "hash_dialog.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.20.6_vs_0.20.7.

    1 /*
    2 ** 1998-07-13 - A more or less general helper module. Uses the dialog module to provide
    3 **      a clist showing the contents of a hash table. Very useful for selecting
    4 **      a command (native or user); originally written for the cfg_buttons module.
    5 ** 1998-08-01 - Discovered (and fixed) a weird bug; the clist's selection event handler was
    6 **      being called as the list was being populated, which caused a read-out of
    7 **      row data not yet installed. Seg-fault. Can't understand why it just popped
    8 **      up today... Fixed it with an ugly little flag (open).
    9 ** 1998-08-04 - Now handles immediate closing, without user clicking in list first. :)
   10 ** 1999-03-13 - Adapted for new dialog module, and generally touched up a little.
   11 ** 1999-06-19 - Changes due to rewritten dialog support module (again). Also changed the
   12 **      semantics: this is now a synchronous dialog.
   13 ** 2008-07-28 - Rewritten to use GtkTreeView and friends rather than GtkCList, which is
   14 **      deprecated in GTK+ 2.0. Feel the future.
   15 */
   16 
   17 #include "gentoo.h"
   18 #include "dialog.h"
   19 #include "hash_dialog.h"
   20 
   21 /* ----------------------------------------------------------------------------------------- */
   22 
   23 typedef struct {
   24     Dialog      *dlg;
   25     GtkListStore    *model;
   26     GtkWidget   *view;
   27 } HDlgInfo;
   28 
   29 /* ----------------------------------------------------------------------------------------- */
   30 
   31 /* 2008-07-28 - This gets called when the user double-clicks a row. Close the dialog. */
   32 static void evt_row_activated(GtkWidget *wid, GtkTreePath *path, GtkTreeViewColumn *col, gpointer user)
   33 {
   34     HDlgInfo    *hdl = user;
   35 
   36     dlg_dialog_sync_close(hdl->dlg, 0);
   37 }
   38 
   39 /* ----------------------------------------------------------------------------------------- */
   40 
   41 /* 2008-07-28 - This is a g_hash_table_foreach() callback, that populates a list store. */
   42 static void populate_model(gpointer key, gpointer value, gpointer user)
   43 {
   44     GtkListStore    *store = user;
   45     GtkTreeIter iter;
   46 
   47     /* FIXME: Here, we assume that the KEY is a string. */
   48     gtk_list_store_insert_with_values(store, &iter, -1, 0, key, 1, value, 2, key, -1);
   49 }
   50 
   51 /* 1999-06-19 - Main has dialog entry point. */
   52 const gchar * hdl_dialog_sync_new_wait(GHashTable *hash, const gchar *title)
   53 {
   54     static HDlgInfo     hdl;
   55     GtkWidget       *scwin;
   56     GtkCellRenderer     *cr;
   57     GtkTreeViewColumn   *vc;
   58     GtkTreeModel        *sm;        
   59 
   60     /* Throw each hashed object (value) into the list store. Our store has
   61      * three columns: one is a pointer to each KEY in the given hash, the
   62      * next is the key's VALUE, and the third is a string made from the two.
   63      * Currently, we assume that the KEY is also a string.
   64     */
   65     hdl.model = gtk_list_store_new(3, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_STRING);
   66     g_hash_table_foreach(hash, populate_model, hdl.model);
   67 
   68     /* Now, wrap the list model in a sortable model, and sort on that string column. */
   69     sm = gtk_tree_model_sort_new_with_model(GTK_TREE_MODEL(hdl.model));
   70     gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(sm), 2, GTK_SORT_ASCENDING);
   71 
   72     hdl.view = gtk_tree_view_new_with_model(sm);
   73     cr = gtk_cell_renderer_text_new();
   74     vc = gtk_tree_view_column_new_with_attributes("(title)", cr, "text", 2, NULL);
   75     gtk_tree_view_append_column(GTK_TREE_VIEW(hdl.view), vc);
   76     gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(hdl.view), FALSE);
   77     g_signal_connect(G_OBJECT(hdl.view), "row_activated", G_CALLBACK(evt_row_activated), &hdl);
   78 
   79     scwin = gtk_scrolled_window_new(NULL, NULL);
   80     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scwin), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
   81     gtk_container_add(GTK_CONTAINER(scwin), hdl.view);
   82 
   83     hdl.dlg = dlg_dialog_sync_new(scwin, title, NULL);
   84     gtk_window_set_default_size(GTK_WINDOW(dlg_dialog_get_dialog(hdl.dlg)), 320, 544);
   85     if(dlg_dialog_sync_wait(hdl.dlg) == DLG_POSITIVE)
   86     {
   87         GtkTreeSelection    *sel;
   88         GtkTreeIter     iter;
   89 
   90         /* Fish out the selection, and the selected row. */
   91         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(hdl.view));
   92         if(gtk_tree_selection_get_selected(sel, NULL, &iter))
   93         {
   94             gchar   *str;
   95 
   96             /* We have a string column, and need to return a string. Guess the next step. */
   97             gtk_tree_model_get(sm, &iter, 2, &str, -1);
   98             return str;
   99         }       
  100     }
  101     return NULL;
  102 }