Logo Search packages:      
Sourcecode: tea version File versions  Download package

tea_tree_view.c

/***************************************************************************
                          tea_tree_view.c  -  description
                             -------------------
    begin                : 2005
    copyright            : (C)2005 by Peter 'Roxton' Semiletov
    email                : peter.semiletov@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/


#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <glib.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
#include "tea_defs.h"


enum
{
  COL_TEXT = 0,
  NUM_COLS
};

static GList *tv_temp_list = NULL;


static void tv_simple_cb (GtkTreeModel *model,
                          GtkTreePath *path,
                          GtkTreeIter *iter,
                          gpointer data)
{
  gchar *name;
  gtk_tree_model_get (model, iter, COL_TEXT, &name, -1);
  tv_temp_list = g_list_prepend (tv_temp_list, name);
}


GList* tv_get_selected_multiply (GtkTreeView *tree_view) 
{
  if (tv_temp_list)
     g_list_free (tv_temp_list);
  
  tv_temp_list = NULL;  

  GtkTreeSelection *sl = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view));
  
  if (gtk_tree_selection_get_mode (sl) != GTK_SELECTION_MULTIPLE)
     return NULL;
  
  gtk_tree_selection_selected_foreach (sl, tv_simple_cb, NULL);

  return g_list_reverse (tv_temp_list);
}


static void tv_add_new_item (GtkTreeView *tree_view, gchar *s)
{
  if (! tree_view || ! s)
     return;

  GtkTreeModel *model;
  GtkTreeIter newrow;
  model = gtk_tree_view_get_model (GTK_TREE_VIEW (tree_view));
  gtk_list_store_append (GTK_LIST_STORE (model), &newrow);
  gtk_list_store_set (GTK_LIST_STORE (model), &newrow, COL_TEXT, s, -1);
}


void tv_fill_with_glist (GtkTreeView *tree_view, GList *l)
{
  if (! tree_view || ! l)
     return;

  GtkListStore *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (tree_view));    

  if (liststore)
     gtk_list_store_clear (liststore);

  GList *t = g_list_first (l);
  while (t)
       {
        tv_add_new_item (tree_view, t->data);
        t = g_list_next (t);
       }
}


gchar* tv_get_selected_single (GtkTreeView *tree_view)
{
  if (! tree_view)
     return NULL;

  GtkTreeSelection *sl = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view));

  if (gtk_tree_selection_get_mode (sl) != GTK_SELECTION_SINGLE)
     return NULL;

  GtkTreeModel *model;
  GtkTreeIter iter; 
  gchar *name = NULL;

  if (gtk_tree_selection_get_selected (sl, &model, &iter))
      gtk_tree_model_get (model, &iter, COL_TEXT, &name, -1);
     
  return name;
}


GtkTreeView* tv_create (GtkSelectionMode mode)
{
  GtkTreeView *tree_view;
  GtkCellRenderer *renderer;
  GtkTreeViewColumn *col;
  GtkTreeSelection *sel;
  GtkListStore *list_store;

  list_store = gtk_list_store_new (NUM_COLS, G_TYPE_STRING);
  tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (list_store));
  renderer = gtk_cell_renderer_text_new();
       
  col = gtk_tree_view_column_new ();

  gtk_tree_view_column_pack_start (col, renderer, TRUE);
  gtk_tree_view_column_add_attribute (col, renderer, "text", COL_TEXT);
  gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), col);

  sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view));
  gtk_tree_selection_set_mode (sel, mode);
  gtk_tree_view_set_headers_visible (tree_view, FALSE);

  return tree_view;
}


GtkTreeView* tv_create_framed (GtkContainer *c, gchar *caption, GtkSelectionMode mode)
{
  GtkWidget *wnd = gtk_scrolled_window_new (NULL, NULL);
  gtk_widget_show (wnd);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (wnd), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

  GtkWidget *f = gtk_frame_new (caption);
  gtk_widget_show (f);
 
  GtkTreeView *tv = tv_create (mode);
  gtk_widget_show (tv);

  gtk_container_add (c, f); 

  gtk_scrolled_window_add_with_viewport (wnd, tv);
  gtk_container_add (f, wnd); 

  return tv;
}

Generated by  Doxygen 1.6.0   Back to index