mp3splt-gtk
combo_helper.c
Go to the documentation of this file.
00001 /**********************************************************
00002  *
00003  * mp3splt-gtk -- utility based on mp3splt,
00004  *                for mp3/ogg splitting without decoding
00005  *
00006  * Copyright: (C) 2005-2011 Alexandru Munteanu
00007  * Contact: io_fx@yahoo.fr
00008  *
00009  * http://mp3splt.sourceforge.net/
00010  *
00011  *********************************************************/
00012 
00013 /**********************************************************
00014  *
00015  * This program is free software; you can redistribute it and/or
00016  * modify it under the terms of the GNU General Public License
00017  * as published by the Free Software Foundation; either version 2
00018  * of the License, or (at your option) any later version.
00019  *
00020  * This program is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  * GNU General Public License for more details.
00024  *
00025  * You should have received a copy of the GNU General Public License
00026  * along with this program; if not, write to the Free Software
00027  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
00028  * USA.
00029  *
00030  *********************************************************/
00031 
00032 /*!********************************************************
00033  * \file 
00034  *
00035  * this file contains the code for the combo helpers.
00036  ********************************************************/
00037 
00038 #include <string.h>
00039 
00040 #include "combo_helper.h"
00041 
00042 GtkComboBox *ch_new_combo()
00043 {
00044   GtkListStore *store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
00045   GtkComboBox *combo = GTK_COMBO_BOX(
00046       gtk_combo_box_new_with_model(GTK_TREE_MODEL(store)));
00047 
00048   GtkCellRenderer *cell = gtk_cell_renderer_text_new();
00049   gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), cell, TRUE);
00050   gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), cell, "text", 0, NULL);
00051 
00052   return combo;
00053 }
00054 
00055 void ch_append_to_combo(GtkComboBox *combo, const gchar *text, gint value)
00056 {
00057   GtkTreeIter iter;
00058   GtkListStore *store = GTK_LIST_STORE(gtk_combo_box_get_model(combo));
00059   gtk_list_store_append(store, &iter);
00060   gtk_list_store_set(store, &iter, 0, text, 1, value, -1); 
00061 }
00062 
00063 gchar *ch_get_active_str_value(GtkComboBox *combo)
00064 {
00065   gchar *value = NULL;
00066 
00067   GtkTreeIter iter;
00068   gboolean has_selection = gtk_combo_box_get_active_iter(combo, &iter);
00069 
00070   if (has_selection)
00071   {
00072     GtkTreeModel *store = gtk_combo_box_get_model(combo);
00073     gtk_tree_model_get(store, &iter, 0, &value, -1);
00074   }
00075  
00076   return value;
00077 }
00078 
00079 gint ch_get_active_value(GtkComboBox *combo)
00080 {
00081   gint value = -1;
00082 
00083   GtkTreeIter iter;
00084   gboolean has_selection = gtk_combo_box_get_active_iter(combo, &iter);
00085 
00086   if (has_selection)
00087   {
00088     GtkTreeModel *store = gtk_combo_box_get_model(combo);
00089     gtk_tree_model_get(store, &iter, 1, &value, -1);
00090   }
00091  
00092   return value;
00093 }
00094 
00095 void ch_set_active_str_value(GtkComboBox *combo, gchar *new_value)
00096 {
00097   GtkTreeIter iter;
00098   GtkTreeModel *store = gtk_combo_box_get_model(combo);
00099 
00100   gboolean valid_row = gtk_tree_model_get_iter_first(store, &iter);
00101   while (valid_row)
00102   {
00103     gchar *value;
00104     gtk_tree_model_get(store, &iter, 0, &value, -1);
00105 
00106     if (strcmp(value, new_value) == 0)
00107     {
00108       gtk_combo_box_set_active_iter(combo, &iter);
00109       return;
00110     }
00111 
00112     valid_row = gtk_tree_model_iter_next(store, &iter);
00113   }
00114 }
00115 
00116 void ch_set_active_value(GtkComboBox *combo, gint new_value)
00117 {
00118   GtkTreeIter iter;
00119   GtkTreeModel *store = gtk_combo_box_get_model(combo);
00120 
00121   gboolean valid_row = gtk_tree_model_get_iter_first(store, &iter);
00122   while (valid_row)
00123   {
00124     gint value;
00125     gtk_tree_model_get(store, &iter, 1, &value, -1);
00126 
00127     if (value == new_value)
00128     {
00129       gtk_combo_box_set_active_iter(combo, &iter);
00130       return;
00131     }
00132 
00133     valid_row = gtk_tree_model_iter_next(store, &iter);
00134   }
00135 }
00136