1 /**********************************************************
2  *
3  * mp3splt-gtk -- utility based on mp3splt,
4  *
5  *                for mp3/ogg splitting without decoding
6  * Copyright: (C) 2005-2014 Alexandru Munteanu
7  * Contact: m@ioalex.net
8  *
9  * http://mp3splt.sourceforge.net/
10  *
11  *********************************************************/
12 
13 /**********************************************************
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
28  * USA.
29  *
30  *********************************************************/
31 
32 /*!********************************************************
33  * \file
34  *
35  * this file contains the code for the combo helpers.
36  ********************************************************/
37 
38 #include <string.h>
39 
40 #include "combo_helper.h"
41 
ch_new_combo()42 GtkComboBox *ch_new_combo()
43 {
44   GtkListStore *store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
45   GtkComboBox *combo = GTK_COMBO_BOX(
46       gtk_combo_box_new_with_model(GTK_TREE_MODEL(store)));
47 
48   GtkCellRenderer *cell = gtk_cell_renderer_text_new();
49   gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), cell, TRUE);
50   gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), cell, "text", 0, NULL);
51 
52   return combo;
53 }
54 
ch_append_to_combo(GtkComboBox * combo,const gchar * text,gint value)55 void ch_append_to_combo(GtkComboBox *combo, const gchar *text, gint value)
56 {
57   GtkTreeIter iter;
58   GtkListStore *store = GTK_LIST_STORE(gtk_combo_box_get_model(combo));
59   gtk_list_store_append(store, &iter);
60   gtk_list_store_set(store, &iter, 0, text, 1, value, -1);
61 }
62 
ch_get_active_str_value(GtkComboBox * combo)63 gchar *ch_get_active_str_value(GtkComboBox *combo)
64 {
65   gchar *value = NULL;
66 
67   GtkTreeIter iter;
68   gboolean has_selection = gtk_combo_box_get_active_iter(combo, &iter);
69 
70   if (has_selection)
71   {
72     GtkTreeModel *store = gtk_combo_box_get_model(combo);
73     gtk_tree_model_get(store, &iter, 0, &value, -1);
74   }
75 
76   return value;
77 }
78 
ch_get_active_value(GtkComboBox * combo)79 gint ch_get_active_value(GtkComboBox *combo)
80 {
81   gint value = -1;
82 
83   GtkTreeIter iter;
84   gboolean has_selection = gtk_combo_box_get_active_iter(combo, &iter);
85 
86   if (has_selection)
87   {
88     GtkTreeModel *store = gtk_combo_box_get_model(combo);
89     gtk_tree_model_get(store, &iter, 1, &value, -1);
90   }
91 
92   return value;
93 }
94 
ch_set_active_str_value(GtkComboBox * combo,gchar * new_value)95 void ch_set_active_str_value(GtkComboBox *combo, gchar *new_value)
96 {
97   GtkTreeIter iter;
98   GtkTreeModel *store = gtk_combo_box_get_model(combo);
99 
100   gboolean valid_row = gtk_tree_model_get_iter_first(store, &iter);
101   while (valid_row)
102   {
103     gchar *value;
104     gtk_tree_model_get(store, &iter, 0, &value, -1);
105 
106     if (strcmp(value, new_value) == 0)
107     {
108       gtk_combo_box_set_active_iter(combo, &iter);
109       g_free(value);
110       return;
111     }
112 
113     valid_row = gtk_tree_model_iter_next(store, &iter);
114     g_free(value);
115   }
116 }
117 
ch_set_active_value(GtkComboBox * combo,gint new_value)118 void ch_set_active_value(GtkComboBox *combo, gint new_value)
119 {
120   GtkTreeIter iter;
121   GtkTreeModel *store = gtk_combo_box_get_model(combo);
122 
123   gboolean valid_row = gtk_tree_model_get_iter_first(store, &iter);
124   while (valid_row)
125   {
126     gint value;
127     gtk_tree_model_get(store, &iter, 1, &value, -1);
128 
129     if (value == new_value)
130     {
131       gtk_combo_box_set_active_iter(combo, &iter);
132       return;
133     }
134 
135     valid_row = gtk_tree_model_iter_next(store, &iter);
136   }
137 }
138 
139