1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
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.
22 * SECTION:anjuta-save-prompt
23 * @short_description: Save dialog prompt for unsaved data
25 * @stability: Unstable
26 * @include: libanjuta/anjuta-save-prompt.h
30 #include <glib/gi18n.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 */
49 COL_ITEM_SAVE_FUNC_DATA
,
53 static GtkDialogClass
*parent_class
= NULL
;
56 anjuta_save_prompt_get_type()
58 static GType type
= 0;
61 static const GTypeInfo our_info
= {
62 sizeof (AnjutaSavePromptClass
),
65 (GClassInitFunc
)anjuta_save_prompt_class_init
,
68 sizeof (AnjutaSavePrompt
),
70 (GInstanceInitFunc
)anjuta_save_prompt_init
,
73 type
= g_type_register_static(GTK_TYPE_MESSAGE_DIALOG
,
74 "AnjutaSavePrompt", &our_info
, 0);
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
;
90 on_dialog_response (GtkWidget
*dlg
, gint response
, gpointer user_data
)
92 if (response
== ANJUTA_SAVE_PROMPT_RESPONSE_SAVE_CLOSE
)
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
))
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
,
111 COL_ITEM_SAVE_FUNC
, &item_save_func
,
112 COL_ITEM_SAVE_FUNC_DATA
, &user_data
,
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
,
124 g_signal_stop_emission_by_name (save_prompt
,
130 while (gtk_tree_model_iter_next (model
, &iter
));
136 anjuta_save_prompt_init(AnjutaSavePrompt
*obj
)
141 GtkTreeViewColumn
*col
;
142 GtkCellRenderer
*renderer
;
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_box_new (GTK_ORIENTATION_VERTICAL
, 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
),
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"),
191 "active", COL_SAVE_ENABLE
,
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"),
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
,
205 ANJUTA_SAVE_PROMPT_RESPONSE_CANCEL
,
207 ANJUTA_SAVE_PROMPT_RESPONSE_SAVE_CLOSE
,
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
),
216 obj
->priv
->treeview
= treeview
;
220 anjuta_save_prompt_finalize(GObject
*object
)
222 AnjutaSavePrompt
*cobj
;
223 cobj
= ANJUTA_SAVE_PROMPT(object
);
225 /* Free private members, etc. */
228 G_OBJECT_CLASS(parent_class
)->finalize(object
);
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
,
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
);
248 anjuta_save_prompt_get_items_count (AnjutaSavePrompt
*save_prompt
)
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
);
258 anjuta_save_prompt_add_item (AnjutaSavePrompt
*save_prompt
,
259 const gchar
*item_name
,
260 const gchar
*item_detail
,
262 AnjutaSavePromptSaveFunc item_save_func
,
267 gchar
*label
, *markup
;
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
);
275 label
= g_strdup_printf ("<b>%s</b>\n%s", item_name
, item_detail
);
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
),
283 COL_SAVE_ENABLE
, TRUE
,
286 COL_ITEM_SAVE_FUNC
, item_save_func
,
287 COL_ITEM_SAVE_FUNC_DATA
, user_data
,
291 items_count
= anjuta_save_prompt_get_items_count (save_prompt
);
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
);
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
);