libanjuta: set the info for the default value of a property
[anjuta.git] / libanjuta / anjuta-save-prompt.c
blob5a362927cba992676fb06b7e1d5f51992c3d4920
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * anjuta-save-prompt.c
4 * Copyright (C) 2000 Naba Kumar <naba@gnome.org>
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 Library 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 /**
22 * SECTION:anjuta-save-prompt
23 * @short_description: Save dialog prompt for unsaved data
24 * @see_also:
25 * @stability: Unstable
26 * @include: libanjuta/anjuta-save-prompt.h
30 #include <glib/gi18n.h>
31 #include <gtk/gtk.h>
33 #include "anjuta-save-prompt.h"
35 static void anjuta_save_prompt_class_init(AnjutaSavePromptClass *klass);
36 static void anjuta_save_prompt_init(AnjutaSavePrompt *sp);
37 static void anjuta_save_prompt_finalize(GObject *object);
39 struct _AnjutaSavePromptPrivate {
40 /* Place Private Members Here */
41 GtkWidget *treeview;
44 enum {
45 COL_SAVE_ENABLE,
46 COL_LABEL,
47 COL_ITEM,
48 COL_ITEM_SAVE_FUNC,
49 COL_ITEM_SAVE_FUNC_DATA,
50 N_COLS
53 static GtkDialogClass *parent_class = NULL;
55 GType
56 anjuta_save_prompt_get_type()
58 static GType type = 0;
60 if(type == 0) {
61 static const GTypeInfo our_info = {
62 sizeof (AnjutaSavePromptClass),
63 NULL,
64 NULL,
65 (GClassInitFunc)anjuta_save_prompt_class_init,
66 NULL,
67 NULL,
68 sizeof (AnjutaSavePrompt),
70 (GInstanceInitFunc)anjuta_save_prompt_init,
73 type = g_type_register_static(GTK_TYPE_MESSAGE_DIALOG,
74 "AnjutaSavePrompt", &our_info, 0);
77 return type;
80 static void
81 anjuta_save_prompt_class_init(AnjutaSavePromptClass *klass)
83 GObjectClass *object_class = G_OBJECT_CLASS(klass);
85 parent_class = g_type_class_peek_parent(klass);
86 object_class->finalize = anjuta_save_prompt_finalize;
89 static void
90 on_dialog_response (GtkWidget *dlg, gint response, gpointer user_data)
92 if (response == ANJUTA_SAVE_PROMPT_RESPONSE_SAVE_CLOSE)
94 GtkWidget *treeview;
95 GtkTreeModel *model;
96 GtkTreeIter iter;
98 AnjutaSavePrompt *save_prompt = ANJUTA_SAVE_PROMPT (dlg);
99 treeview = save_prompt->priv->treeview;
100 model = gtk_tree_view_get_model (GTK_TREE_VIEW (treeview));
101 if(gtk_tree_model_get_iter_first (model, &iter))
103 do {
104 gboolean save_enable;
105 gpointer item, user_data;
106 AnjutaSavePromptSaveFunc item_save_func;
108 gtk_tree_model_get (model, &iter,
109 COL_SAVE_ENABLE, &save_enable,
110 COL_ITEM, &item,
111 COL_ITEM_SAVE_FUNC, &item_save_func,
112 COL_ITEM_SAVE_FUNC_DATA, &user_data,
113 -1);
114 if (save_enable)
116 if (item_save_func(save_prompt, item, user_data))
118 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
119 COL_SAVE_ENABLE, FALSE,
120 -1);
122 else
124 g_signal_stop_emission_by_name (save_prompt,
125 "response");
126 break;
130 while (gtk_tree_model_iter_next (model, &iter));
135 static void
136 anjuta_save_prompt_init(AnjutaSavePrompt *obj)
138 GtkWidget *vbox;
139 GtkWidget *treeview;
140 GtkListStore *store;
141 GtkTreeViewColumn *col;
142 GtkCellRenderer *renderer;
143 GtkWidget *label;
144 GtkWidget *scrolledwindow;
146 obj->priv = g_new0(AnjutaSavePromptPrivate, 1);
148 gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (obj), "Uninitialized");
149 gtk_window_set_resizable (GTK_WINDOW (obj), TRUE);
150 gtk_window_set_default_size (GTK_WINDOW (obj), 400, 300);
152 vbox = gtk_vbox_new (FALSE, 0);
153 gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (obj))),
154 vbox, TRUE, TRUE, 0);
155 gtk_widget_show (vbox);
157 label = gtk_label_new (_("Select the items to save:"));
158 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
159 gtk_misc_set_padding (GTK_MISC (label), 10, -1);
160 gtk_misc_set_alignment (GTK_MISC (label), 0, -1);
161 gtk_widget_show (label);
163 store = gtk_list_store_new (N_COLS, G_TYPE_BOOLEAN,
164 G_TYPE_STRING, G_TYPE_POINTER,
165 G_TYPE_POINTER, G_TYPE_POINTER);
167 scrolledwindow = gtk_scrolled_window_new (NULL, NULL);
168 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow),
169 GTK_POLICY_AUTOMATIC,
170 GTK_POLICY_AUTOMATIC);
171 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolledwindow),
172 GTK_SHADOW_IN);
173 gtk_box_pack_start (GTK_BOX (vbox), scrolledwindow, TRUE, TRUE, 0);
174 gtk_container_set_border_width (GTK_CONTAINER (scrolledwindow), 10);
175 gtk_widget_show (scrolledwindow);
177 treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
178 g_object_unref (store);
179 gtk_container_add (GTK_CONTAINER (scrolledwindow), treeview);
180 gtk_widget_show (treeview);
182 label = gtk_label_new (_("If you do not save, all your changes will be lost."));
183 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
184 gtk_misc_set_padding (GTK_MISC (label), 10, -1);
185 gtk_misc_set_alignment (GTK_MISC (label), 0, -1);
186 gtk_widget_show (label);
188 renderer = gtk_cell_renderer_toggle_new ();
189 col = gtk_tree_view_column_new_with_attributes (_("Save"),
190 renderer,
191 "active", COL_SAVE_ENABLE,
192 NULL);
193 gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), col);
195 renderer = gtk_cell_renderer_text_new ();
196 col = gtk_tree_view_column_new_with_attributes (_("Item"),
197 renderer,
198 "markup", COL_LABEL,
199 NULL);
200 gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), col);
202 gtk_dialog_add_buttons (GTK_DIALOG (obj), _("_Discard changes"),
203 ANJUTA_SAVE_PROMPT_RESPONSE_DISCARD,
204 GTK_STOCK_CANCEL,
205 ANJUTA_SAVE_PROMPT_RESPONSE_CANCEL,
206 GTK_STOCK_SAVE,
207 ANJUTA_SAVE_PROMPT_RESPONSE_SAVE_CLOSE,
208 NULL);
209 /* TRUE is the default and Gtk warns that this has no effect */
210 /* gtk_dialog_set_has_separator (GTK_DIALOG (obj), TRUE); */
211 gtk_dialog_set_default_response (GTK_DIALOG (obj),
212 ANJUTA_SAVE_PROMPT_RESPONSE_SAVE_CLOSE);
213 g_signal_connect (obj, "response",
214 G_CALLBACK (on_dialog_response),
215 obj);
216 obj->priv->treeview = treeview;
219 static void
220 anjuta_save_prompt_finalize(GObject *object)
222 AnjutaSavePrompt *cobj;
223 cobj = ANJUTA_SAVE_PROMPT(object);
225 /* Free private members, etc. */
227 g_free(cobj->priv);
228 G_OBJECT_CLASS(parent_class)->finalize(object);
231 AnjutaSavePrompt *
232 anjuta_save_prompt_new (GtkWindow *parent)
234 AnjutaSavePrompt *obj;
236 g_return_val_if_fail (GTK_IS_WINDOW (parent), NULL);
238 obj = ANJUTA_SAVE_PROMPT(g_object_new(ANJUTA_TYPE_SAVE_PROMPT,
239 "message-type", GTK_MESSAGE_QUESTION,
240 NULL));
241 /* Gtk warns that this has no effect
242 gtk_dialog_set_has_separator (GTK_DIALOG (obj), TRUE); */
243 gtk_window_set_transient_for (GTK_WINDOW (obj), parent);
244 return obj;
247 gint
248 anjuta_save_prompt_get_items_count (AnjutaSavePrompt *save_prompt)
250 GtkTreeModel *model;
251 g_return_val_if_fail (ANJUTA_IS_SAVE_PROMPT (save_prompt), -1);
252 model = gtk_tree_view_get_model (GTK_TREE_VIEW (save_prompt->priv->treeview));
253 return gtk_tree_model_iter_n_children (model, NULL);
257 void
258 anjuta_save_prompt_add_item (AnjutaSavePrompt *save_prompt,
259 const gchar *item_name,
260 const gchar *item_detail,
261 gpointer item,
262 AnjutaSavePromptSaveFunc item_save_func,
263 gpointer user_data)
265 GtkTreeModel *model;
266 GtkTreeIter iter;
267 gchar *label, *markup;
268 gint items_count;
270 g_return_if_fail (ANJUTA_IS_SAVE_PROMPT (save_prompt));
271 g_return_if_fail (item_name != NULL);
272 g_return_if_fail (item_save_func != NULL);
274 if (item_detail)
275 label = g_strdup_printf ("<b>%s</b>\n%s", item_name, item_detail);
276 else
277 label = g_strdup (item_name);
279 model = gtk_tree_view_get_model (GTK_TREE_VIEW (save_prompt->priv->treeview));
280 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
281 gtk_list_store_set (GTK_LIST_STORE (model),
282 &iter,
283 COL_SAVE_ENABLE, TRUE,
284 COL_LABEL, label,
285 COL_ITEM, item,
286 COL_ITEM_SAVE_FUNC, item_save_func,
287 COL_ITEM_SAVE_FUNC_DATA, user_data,
288 -1);
289 g_free (label);
291 items_count = anjuta_save_prompt_get_items_count (save_prompt);
293 if (items_count > 1)
295 label = g_strdup_printf(
296 ngettext ("There is %d item with unsaved changes. Save changes before closing?",
297 "There are %d items with unsaved changes. Save changes before closing?",
298 items_count), items_count);
299 markup = g_strdup_printf ("<b>%s</b>", label);
300 g_free (label);
302 else
304 markup = g_strdup_printf ("<b>%s</b>",
305 _("There is an item with unsaved changes. Save changes before closing?"));
308 gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (save_prompt), markup);
309 g_free (markup);