Fix typo: Similar Artist -> Similar Artists
[gmpc.git] / src / egg / eggcolumnchooserdialog.c
blob53cfe8a62f11e1c8f643f79df930d2b3680ff858
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* egg-column-chooser-dialog.c
3 * Copyright (C) 2001 Anders Carlsson
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #include "eggcolumnchooserdialog.h"
23 #include <gtk/gtk.h>
24 #include "eggcolumnmodel.h"
26 static void egg_column_chooser_dialog_class_init (EggColumnChooserDialogClass *klass);
27 static void egg_column_chooser_dialog_init (EggColumnChooserDialog *dialog);
29 static GtkDialogClass *parent_class = NULL;
31 GType
32 egg_column_chooser_dialog_get_type (void)
34 static GType object_type = 0;
36 if (!object_type) {
37 static const GTypeInfo object_info = {
38 sizeof (EggColumnChooserDialogClass),
39 NULL,
40 NULL,
41 (GClassInitFunc) egg_column_chooser_dialog_class_init,
42 NULL,
43 NULL,
44 sizeof (EggColumnChooserDialog),
46 (GInstanceInitFunc) egg_column_chooser_dialog_init
49 object_type = g_type_register_static (GTK_TYPE_DIALOG, "EggColumnChooserDialog", &object_info, (GTypeFlags)0);
53 return object_type;
56 static void
57 update_button_states (EggColumnChooserDialog *dialog, GtkTreeIter *iter)
59 if (egg_column_model_get_column_visible (dialog->column_model, iter)) {
60 gtk_widget_set_sensitive (dialog->hide_button, TRUE);
61 gtk_widget_set_sensitive (dialog->show_button, FALSE);
63 else {
64 gtk_widget_set_sensitive (dialog->hide_button, FALSE);
65 gtk_widget_set_sensitive (dialog->show_button, TRUE);
68 if (egg_column_model_is_column_first (dialog->column_model, iter)) {
69 gtk_widget_set_sensitive (dialog->move_up_button, FALSE);
71 else {
72 gtk_widget_set_sensitive (dialog->move_up_button, TRUE);
75 if (egg_column_model_is_column_last (dialog->column_model, iter)) {
76 gtk_widget_set_sensitive (dialog->move_down_button, FALSE);
78 else {
79 gtk_widget_set_sensitive (dialog->move_down_button, TRUE);
84 static void
85 selection_changed (GtkTreeSelection *selection, EggColumnChooserDialog *dialog)
87 GtkTreeIter iter;
89 if(gtk_tree_selection_get_selected (selection, NULL, &iter))
91 update_button_states (dialog, &iter);
95 static void
96 visible_toggled (GtkCellRendererToggle *cell, gchar *path_str, EggColumnChooserDialog *dialog)
98 GtkTreePath *path;
99 GtkTreeIter iter;
100 GValue value = {0, };
101 GtkTreeModel *model = GTK_TREE_MODEL (dialog->column_model);
103 path = gtk_tree_path_new_from_string (path_str);
104 gtk_tree_model_get_iter (model, &iter, path);
106 gtk_tree_model_get_value (model,
107 &iter,
109 &value);
111 egg_column_model_set_column_visible (dialog->column_model, &iter, !g_value_get_boolean (&value));
113 gtk_tree_path_free (path);
114 update_button_states (dialog, &iter);
117 static void
118 show_button_clicked (GtkWidget *button, EggColumnChooserDialog *dialog)
120 GtkTreeIter iter;
122 gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->tree_view)), NULL, &iter);
124 egg_column_model_set_column_visible (dialog->column_model, &iter, TRUE);
126 update_button_states (dialog, &iter);
129 static void
130 hide_button_clicked (GtkWidget *button, EggColumnChooserDialog *dialog)
132 GtkTreeIter iter;
134 gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->tree_view)), NULL, &iter);
136 egg_column_model_set_column_visible (dialog->column_model, &iter, FALSE);
138 update_button_states (dialog, &iter);
141 static void
142 move_down_button_clicked (GtkWidget *button, EggColumnChooserDialog *dialog)
144 GtkTreeIter iter;
146 gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->tree_view)), NULL, &iter);
148 egg_column_model_move_down_column (dialog->column_model, &iter);
150 /* We need to do this again since the model structure has changed */
151 gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->tree_view)), NULL, &iter);
152 update_button_states (dialog, &iter);
155 static void
156 move_up_button_clicked (GtkWidget *button, EggColumnChooserDialog *dialog)
158 GtkTreeIter iter;
160 gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->tree_view)), NULL, &iter);
162 egg_column_model_move_up_column (dialog->column_model, &iter);
164 /* We need to do this again since the model structure has changed */
165 gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->tree_view)), NULL, &iter);
166 update_button_states (dialog, &iter);
169 static void
170 egg_column_chooser_dialog_finalize (GObject *object)
172 EggColumnChooserDialog *dialog;
174 dialog = EGG_COLUMN_CHOOSER_DIALOG (object);
176 g_object_unref (G_OBJECT (dialog->column_model));
178 G_OBJECT_CLASS (parent_class)->finalize (object);
181 static void
182 egg_column_chooser_dialog_response (GtkDialog *dialog, gint response_id)
184 gtk_widget_destroy (GTK_WIDGET (dialog));
187 static void
188 egg_column_chooser_dialog_class_init (EggColumnChooserDialogClass *klass)
190 GObjectClass *object_class;
191 GtkDialogClass *dialog_class;
193 object_class = (GObjectClass *)klass;
194 dialog_class = (GtkDialogClass *)klass;
196 parent_class = (GtkDialogClass *)g_type_class_peek_parent (klass);
198 object_class->finalize = egg_column_chooser_dialog_finalize;
200 dialog_class->response = egg_column_chooser_dialog_response;
203 static void
204 egg_column_chooser_dialog_init (EggColumnChooserDialog *dialog)
206 GtkWidget *hbox, *vbox, *vbox2, *label, *button;
207 GtkWidget *scrolled_window;
208 GtkTreeSelection *selection;
209 GtkCellRenderer *toggle_renderer;
211 gtk_window_set_title (GTK_WINDOW (dialog), "Columns");
213 gtk_dialog_add_button (GTK_DIALOG (dialog),
214 GTK_STOCK_CLOSE,
215 GTK_RESPONSE_CLOSE);
217 vbox = gtk_vbox_new (FALSE, 12);
218 gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
220 label = gtk_label_new ("Check the columns that you would like to be visible in this view. "
221 "Use the Move Up and Move Down buttons to reorder the columns "
222 "however you like.");
223 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
224 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
226 gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (dialog)), vbox, TRUE, TRUE, 0);
227 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 8);
230 hbox = gtk_hbox_new (FALSE, 12);
231 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
233 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
234 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window),
235 GTK_SHADOW_IN);
236 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
237 GTK_POLICY_AUTOMATIC,
238 GTK_POLICY_AUTOMATIC);
239 gtk_box_pack_start (GTK_BOX (hbox), scrolled_window, TRUE, TRUE, 0);
241 dialog->tree_view = gtk_tree_view_new ();
242 g_object_set_data (G_OBJECT (dialog->tree_view), "foo-data", GINT_TO_POINTER (TRUE));
243 gtk_tree_view_set_reorderable (GTK_TREE_VIEW (dialog->tree_view), FALSE);
244 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->tree_view));
245 g_signal_connect (G_OBJECT (selection), "changed",
246 G_CALLBACK (selection_changed), dialog);
248 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (dialog->tree_view), FALSE);
249 gtk_container_add (GTK_CONTAINER (scrolled_window), dialog->tree_view);
251 toggle_renderer = (GtkCellRenderer *)g_object_new (GTK_TYPE_CELL_RENDERER_TOGGLE,
252 "activatable", TRUE,
253 NULL);
254 g_signal_connect (G_OBJECT (toggle_renderer), "toggled",
255 G_CALLBACK (visible_toggled), dialog);
257 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (dialog->tree_view),
258 -1, NULL,
259 toggle_renderer,
260 "active", 0,
261 NULL);
263 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (dialog->tree_view),
264 -1, NULL,
265 gtk_cell_renderer_text_new (),
266 "text", 1,
267 NULL);
269 vbox2 = gtk_vbox_new (FALSE, 8);
270 gtk_box_pack_start (GTK_BOX (hbox), vbox2, FALSE, FALSE, 0);
272 dialog->move_up_button = gtk_button_new_with_mnemonic ("Move _Up");
273 g_signal_connect (G_OBJECT (dialog->move_up_button), "clicked",
274 G_CALLBACK (move_up_button_clicked), dialog);
275 gtk_box_pack_start (GTK_BOX (vbox2), dialog->move_up_button, FALSE, FALSE, 0);
277 dialog->move_down_button = gtk_button_new_with_mnemonic ("Move _Down");
278 g_signal_connect (G_OBJECT (dialog->move_down_button), "clicked",
279 G_CALLBACK (move_down_button_clicked), dialog);
280 gtk_box_pack_start (GTK_BOX (vbox2), dialog->move_down_button, FALSE, FALSE, 0);
282 dialog->show_button = gtk_button_new_with_mnemonic ("_Show");
283 g_signal_connect (G_OBJECT (dialog->show_button), "clicked",
284 G_CALLBACK (show_button_clicked), dialog);
286 gtk_box_pack_start (GTK_BOX (vbox2), dialog->show_button, FALSE, FALSE, 0);
288 dialog->hide_button = gtk_button_new_with_mnemonic ("_Hide");
289 g_signal_connect (G_OBJECT (dialog->hide_button), "clicked",
290 G_CALLBACK (hide_button_clicked), dialog);
291 gtk_box_pack_start (GTK_BOX (vbox2), dialog->hide_button, FALSE, FALSE, 0);
293 button = gtk_button_new_with_label ("Reset");
294 gtk_widget_set_sensitive (button, FALSE);
295 gtk_box_pack_start (GTK_BOX (vbox2), button, FALSE, FALSE, 0);
299 static void
300 rows_reordered (GtkTreeModel *tree_model, GtkTreePath *path, GtkTreeIter *iter, gint *new_order, EggColumnChooserDialog *dialog)
302 GtkTreeIter my_iter;
304 gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->tree_view)), NULL, &my_iter);
305 update_button_states (dialog, &my_iter);
308 static void
309 egg_column_chooser_dialog_construct (EggColumnChooserDialog *dialog, GtkTreeView *tree_view)
311 dialog->column_model = egg_column_model_new (tree_view);
312 g_signal_connect_after (G_OBJECT (dialog->column_model), "rows_reordered",
313 G_CALLBACK (rows_reordered), dialog);
315 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->tree_view), GTK_TREE_MODEL (dialog->column_model));
319 GtkWidget *
320 egg_column_chooser_dialog_new (GtkTreeView *tree_view)
322 EggColumnChooserDialog *dialog;
323 dialog = (EggColumnChooserDialog *)g_object_new (EGG_TYPE_COLUMN_CHOOSER_DIALOG, NULL);
325 egg_column_chooser_dialog_construct (dialog, tree_view);
327 /* Select the first element in the list */
328 /* path = gtk_tree_path_new ();
329 gtk_tree_path_append_index (path, 0);
330 gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->tree_view)), path);
331 gtk_tree_path_free (path);
333 return GTK_WIDGET (dialog);