Fix a Gtk warning when checking path input in the log viewer.
[anjuta-git-plugin.git] / libanjuta / anjuta-preferences-dialog.c
blob255ae49f301e5e571bc78522b47dfed85c0405f3
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * Anjuta
4 * Copyright (C) 2002 Dave Camp
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 /**
22 * SECTION:anjuta-preferences-dialog
23 * @short_description: Preferences dialog
24 * @see_also: #AnjutaPreferences
25 * @stability: Unstable
26 * @include: libanjuta/anjuta-preferences-dialog.h
28 * Plugins can added preferences page with anjuta_preferences_dialog_add_page().
29 * However, read #AnjutaPreferences for adding proper preferences pages.
32 #include <config.h>
34 #include <string.h>
35 #include <glib/gi18n.h>
36 #include <gtk/gtkhbox.h>
37 #include <gtk/gtkliststore.h>
38 #include <gtk/gtknotebook.h>
39 #include <gtk/gtkscrolledwindow.h>
40 #include <gtk/gtkstock.h>
41 #include <gtk/gtktreeview.h>
42 #include <gtk/gtktreeselection.h>
43 #include <gdk-pixbuf/gdk-pixbuf.h>
45 #include <libgnome/gnome-macros.h>
47 #include <cell-renderer-captioned-image.h>
48 #include <libanjuta/anjuta-preferences-dialog.h>
49 struct _AnjutaPreferencesDialogPrivate {
50 GtkWidget *treeview;
51 GtkListStore *store;
53 GtkWidget *notebook;
56 enum {
57 COL_NAME,
58 COL_TITLE,
59 COL_PIXBUF,
60 COL_WIDGET,
61 LAST_COL
64 static void anjuta_preferences_dialog_class_init (AnjutaPreferencesDialogClass *class);
65 static void anjuta_preferences_dialog_init (AnjutaPreferencesDialog *dlg);
67 G_DEFINE_TYPE (AnjutaPreferencesDialog, anjuta_preferences_dialog,
68 GTK_TYPE_DIALOG)
70 static void
71 anjuta_preferences_dialog_finalize (GObject *obj)
73 AnjutaPreferencesDialog *dlg = ANJUTA_PREFERENCES_DIALOG (obj);
75 if (dlg->priv->store) {
76 g_object_unref (dlg->priv->store);
77 dlg->priv->store = NULL;
80 g_free (dlg->priv);
82 ((GObjectClass *) anjuta_preferences_dialog_parent_class)->finalize (obj);
85 static void
86 anjuta_preferences_dialog_class_init (AnjutaPreferencesDialogClass *class)
88 GObjectClass *object_class = G_OBJECT_CLASS (class);
90 object_class->finalize = anjuta_preferences_dialog_finalize;
93 static void
94 add_category_columns (AnjutaPreferencesDialog *dlg)
96 GtkCellRenderer *renderer;
97 GtkTreeViewColumn *column;
99 renderer = anjuta_cell_renderer_captioned_image_new ();
100 g_object_ref_sink (renderer);
101 column = gtk_tree_view_column_new_with_attributes (_("Category"),
102 renderer,
103 "text",
104 COL_TITLE,
105 "pixbuf",
106 COL_PIXBUF,
107 NULL);
109 gtk_tree_view_append_column (GTK_TREE_VIEW (dlg->priv->treeview),
110 column);
113 static void
114 selection_changed_cb (GtkTreeSelection *selection,
115 AnjutaPreferencesDialog *dlg)
117 GtkTreeIter iter;
119 if (gtk_tree_selection_get_selected (selection, NULL, &iter)) {
120 GtkWidget *widget;
122 gtk_tree_model_get (GTK_TREE_MODEL (dlg->priv->store), &iter,
123 COL_WIDGET, &widget, -1);
125 gtk_notebook_set_current_page
126 (GTK_NOTEBOOK (dlg->priv->notebook),
127 gtk_notebook_page_num (GTK_NOTEBOOK (dlg->priv->notebook),
128 widget));
133 static gint
134 compare_pref_page_func (GtkTreeModel *model,
135 GtkTreeIter *a, GtkTreeIter *b, gpointer user_data)
137 gint val;
138 gchar *name1, *name2;
140 gtk_tree_model_get (model, a, COL_TITLE, &name1, -1);
141 gtk_tree_model_get (model, b, COL_TITLE, &name2, -1);
143 /* FIXME: Make the general page first */
144 if (strcmp (name1, _("General")) == 0)
145 return -1;
147 if (strcmp (name2, _("General")) == 0)
148 return 1;
150 val = strcmp (name1, name2);
151 g_free (name1);
152 g_free (name2);
154 return val;
157 static void
158 anjuta_preferences_dialog_init (AnjutaPreferencesDialog *dlg)
160 GtkWidget *hbox;
161 GtkWidget *scrolled_window;
162 GtkTreeSelection *selection;
163 GtkTreeSortable *sortable;
165 dlg->priv = g_new0 (AnjutaPreferencesDialogPrivate, 1);
167 hbox = gtk_hbox_new (FALSE, 0);
169 dlg->priv->treeview = gtk_tree_view_new ();
170 gtk_widget_show (dlg->priv->treeview);
171 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (dlg->priv->treeview),
172 FALSE);
173 dlg->priv->store = gtk_list_store_new (LAST_COL,
174 G_TYPE_STRING,
175 G_TYPE_STRING,
176 GDK_TYPE_PIXBUF,
177 GTK_TYPE_WIDGET,
178 G_TYPE_INT);
179 sortable = GTK_TREE_SORTABLE (dlg->priv->store);
180 gtk_tree_sortable_set_sort_column_id (sortable, COL_TITLE,
181 GTK_SORT_ASCENDING);
182 gtk_tree_sortable_set_sort_func (sortable, COL_TITLE,
183 compare_pref_page_func,
184 NULL, NULL);
186 gtk_tree_view_set_model (GTK_TREE_VIEW (dlg->priv->treeview),
187 GTK_TREE_MODEL (dlg->priv->store));
189 add_category_columns (dlg);
191 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
192 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
193 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
194 gtk_scrolled_window_set_shadow_type
195 (GTK_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_IN);
197 gtk_widget_show (scrolled_window);
198 gtk_container_add (GTK_CONTAINER (scrolled_window),
199 dlg->priv->treeview);
200 gtk_box_pack_start (GTK_BOX (hbox), scrolled_window,
201 FALSE, FALSE, 0);
203 dlg->priv->notebook = gtk_notebook_new ();
204 gtk_widget_show (dlg->priv->notebook);
205 gtk_notebook_set_show_tabs (GTK_NOTEBOOK (dlg->priv->notebook), FALSE);
206 gtk_notebook_set_show_border (GTK_NOTEBOOK (dlg->priv->notebook),
207 FALSE);
208 gtk_container_set_border_width (GTK_CONTAINER (dlg->priv->notebook),
211 gtk_box_pack_start (GTK_BOX (hbox), dlg->priv->notebook,
212 TRUE, TRUE, 0);
215 selection = gtk_tree_view_get_selection
216 (GTK_TREE_VIEW (dlg->priv->treeview));
218 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
220 g_signal_connect (selection, "changed",
221 G_CALLBACK (selection_changed_cb), dlg);
223 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), hbox,
224 TRUE, TRUE, 0);
226 gtk_dialog_add_button (GTK_DIALOG (dlg), GTK_STOCK_CLOSE, -7);
228 gtk_widget_show (hbox);
232 * anjuta_preferences_dialog_new:
234 * Creates a new #AnjutaPreferencesDialog object.
236 * Return value: a new #AnjutaPreferencesDialog object.
238 GtkWidget *
239 anjuta_preferences_dialog_new (void)
241 return g_object_new (ANJUTA_TYPE_PREFERENCES_DIALOG,
242 "title", _("Anjuta Preferences"),
243 NULL);
247 * anjuta_preferences_dialog_add_page:
248 * @dlg: A #AnjutaPreferencesDialog object.
249 * @name: Name of the preferences page.
250 * @icon: Icon file name.
251 * @page: page widget.
253 * Adds a widget page in preferences dialog. Name and icon appears
254 * on the left icon list where differnt pages are selected.
256 void
257 anjuta_preferences_dialog_add_page (AnjutaPreferencesDialog *dlg,
258 const gchar *name,
259 const gchar *title,
260 GdkPixbuf *icon,
261 GtkWidget *page)
263 GtkTreeIter iter;
264 GtkTreeSelection* selection =
265 gtk_tree_view_get_selection (GTK_TREE_VIEW (dlg->priv->treeview));
266 GtkTreeIter first;
267 gtk_widget_show (page);
269 gtk_notebook_append_page (GTK_NOTEBOOK (dlg->priv->notebook), page, NULL);
271 gtk_list_store_append (dlg->priv->store, &iter);
273 gtk_list_store_set (dlg->priv->store, &iter,
274 COL_NAME, name,
275 COL_TITLE, title,
276 COL_PIXBUF, icon,
277 COL_WIDGET, page,
278 -1);
280 gtk_tree_model_get_iter_first (GTK_TREE_MODEL (dlg->priv->store),
281 &first);
282 gtk_tree_selection_select_iter (selection, &first);
286 * anjuta_preferences_dialog_remove_page:
287 * @dlg: A #AnjutaPreferencesDialog object.g_signal_handler
288 * @name: Name of the preferences page.
290 * Removes a preferences page.
292 void
293 anjuta_preferences_dialog_remove_page (AnjutaPreferencesDialog *dlg,
294 const char *title)
296 GtkTreeModel* model = GTK_TREE_MODEL(dlg->priv->store);
297 GtkTreeIter iter;
298 GtkWidget *page;
300 if (gtk_tree_model_get_iter_first(model, &iter))
304 gchar* page_title;
305 GObject* page_widget;
307 gtk_tree_model_get(model, &iter,
308 COL_TITLE, &page_title,
309 COL_WIDGET, &page_widget,
310 -1);
312 if (g_str_equal(page_title, title))
314 int page_num;
316 page_num = gtk_notebook_page_num (
317 GTK_NOTEBOOK(dlg->priv->notebook),
318 GTK_WIDGET (page_widget));
320 page = gtk_notebook_get_nth_page (GTK_NOTEBOOK (dlg->priv->notebook),
321 page_num);
323 gtk_notebook_remove_page(
324 GTK_NOTEBOOK(dlg->priv->notebook), page_num);
326 gtk_widget_destroy (page);
328 gtk_list_store_remove(dlg->priv->store, &iter);
329 return;
332 while (gtk_tree_model_iter_next(model, &iter));
334 g_warning("Could not find page to remove");