1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
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.
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.
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
{
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
,
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
;
82 ((GObjectClass
*) anjuta_preferences_dialog_parent_class
)->finalize (obj
);
86 anjuta_preferences_dialog_class_init (AnjutaPreferencesDialogClass
*class)
88 GObjectClass
*object_class
= G_OBJECT_CLASS (class);
90 object_class
->finalize
= anjuta_preferences_dialog_finalize
;
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"),
109 gtk_tree_view_append_column (GTK_TREE_VIEW (dlg
->priv
->treeview
),
114 selection_changed_cb (GtkTreeSelection
*selection
,
115 AnjutaPreferencesDialog
*dlg
)
119 if (gtk_tree_selection_get_selected (selection
, NULL
, &iter
)) {
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
),
134 compare_pref_page_func (GtkTreeModel
*model
,
135 GtkTreeIter
*a
, GtkTreeIter
*b
, gpointer user_data
)
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)
147 if (strcmp (name2
, _("General")) == 0)
150 val
= strcmp (name1
, name2
);
158 anjuta_preferences_dialog_init (AnjutaPreferencesDialog
*dlg
)
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
),
173 dlg
->priv
->store
= gtk_list_store_new (LAST_COL
,
179 sortable
= GTK_TREE_SORTABLE (dlg
->priv
->store
);
180 gtk_tree_sortable_set_sort_column_id (sortable
, COL_TITLE
,
182 gtk_tree_sortable_set_sort_func (sortable
, COL_TITLE
,
183 compare_pref_page_func
,
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
,
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
),
208 gtk_container_set_border_width (GTK_CONTAINER (dlg
->priv
->notebook
),
211 gtk_box_pack_start (GTK_BOX (hbox
), dlg
->priv
->notebook
,
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
,
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.
239 anjuta_preferences_dialog_new (void)
241 return g_object_new (ANJUTA_TYPE_PREFERENCES_DIALOG
,
242 "title", _("Anjuta Preferences"),
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.
257 anjuta_preferences_dialog_add_page (AnjutaPreferencesDialog
*dlg
,
264 GtkTreeSelection
* selection
=
265 gtk_tree_view_get_selection (GTK_TREE_VIEW (dlg
->priv
->treeview
));
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
,
280 gtk_tree_model_get_iter_first (GTK_TREE_MODEL (dlg
->priv
->store
),
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.
293 anjuta_preferences_dialog_remove_page (AnjutaPreferencesDialog
*dlg
,
296 GtkTreeModel
* model
= GTK_TREE_MODEL(dlg
->priv
->store
);
300 if (gtk_tree_model_get_iter_first(model
, &iter
))
305 GObject
* page_widget
;
307 gtk_tree_model_get(model
, &iter
,
308 COL_TITLE
, &page_title
,
309 COL_WIDGET
, &page_widget
,
312 if (g_str_equal(page_title
, title
))
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
),
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
);
332 while (gtk_tree_model_iter_next(model
, &iter
));
334 g_warning("Could not find page to remove");