hythmbox-2.98/widgets/rb-dialog.c

No issues found

  1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
  2 /*
  3  *  Copyright (C) 2002 Jorn Baayen
  4  *  Copyright (C) 2004 Colin Walters <walters@redhat.com>
  5  *
  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, or (at your option)
  9  *  any later version.
 10  *
 11  *  The Rhythmbox authors hereby grant permission for non-GPL compatible
 12  *  GStreamer plugins to be used and distributed together with GStreamer
 13  *  and Rhythmbox. This permission is above and beyond the permissions granted
 14  *  by the GPL license by which Rhythmbox is covered. If you modify this code
 15  *  you may extend this exception to your version of the code, but you are not
 16  *  obligated to do so. If you do not wish to do so, delete this exception
 17  *  statement from your version.
 18  *
 19  *  This program is distributed in the hope that it will be useful,
 20  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 21  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 22  *  GNU General Public License for more details.
 23  *
 24  *  You should have received a copy of the GNU General Public License
 25  *  along with this program; if not, write to the Free Software
 26  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
 27  *
 28  */
 29 
 30 #include <config.h>
 31 
 32 #include <glib/gi18n.h>
 33 
 34 #include <gtk/gtk.h>
 35 #include <glib.h>
 36 #include <glib/gprintf.h>
 37 #include <stdio.h>
 38 #include <string.h>
 39 #include <stdarg.h>
 40 
 41 #include "rb-dialog.h"
 42 #include "rb-file-helpers.h"
 43 #include "rb-stock-icons.h"
 44 
 45 /**
 46  * SECTION:rb-dialog
 47  * @short_description: helper functions for creating gtk+ dialog windows
 48  *
 49  * This provides a couple of shortcuts for creating dialogs.  If you want
 50  * to annoy the user by displaying an error message, this is the way to do it.
 51  */
 52 
 53 /**
 54  * rb_error_dialog:
 55  * @parent: parent #GtkWindow for the dialog
 56  * @primary: main error message
 57  * @secondary: secondary error message (printf-style format string)
 58  * @...: any substitution values for the secondary message
 59  *
 60  * Creates and displays a simple error dialog box containing a primary
 61  * message in bold, larger type and a secondary message in the regular
 62  * font.  Both the primary and secondary message strings should be
 63  * translated.
 64  *
 65  * Care should be taken to avoid opening multiple error dialog boxes
 66  * when a single error message (such as 'out of disk space') affects
 67  * multiple items.
 68  */
 69 void
 70 rb_error_dialog (GtkWindow *parent,
 71 		 const char *primary,
 72 		 const char *secondary,
 73 		 ...)
 74 {
 75 	char *text = "";
 76 	va_list args;
 77 	GtkWidget *dialog;
 78 
 79 	va_start (args, secondary);
 80 	g_vasprintf (&text, secondary, args);
 81 	va_end (args);
 82 
 83 	dialog = gtk_message_dialog_new (parent,
 84 					 GTK_DIALOG_DESTROY_WITH_PARENT,
 85 					 GTK_MESSAGE_ERROR,
 86 					 GTK_BUTTONS_CLOSE,
 87 					 "%s", primary);
 88 
 89 	gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
 90 						  "%s", text);
 91 
 92 	gtk_window_set_title (GTK_WINDOW (dialog), "");
 93 
 94 	gtk_container_set_border_width (GTK_CONTAINER (dialog), 6);
 95 
 96 	g_signal_connect (dialog, "response", G_CALLBACK (gtk_widget_destroy), NULL);
 97 
 98 	gtk_widget_show (dialog);
 99 
100 	g_free (text);
101 }
102 
103 /**
104  * rb_file_chooser_new:
105  * @title: title for the file chooser
106  * @parent: parent #GtkWindow for the file chooser
107  * @action: the #GtkFileChooserAction
108  * @local_only: if TRUE, don't show network locations
109  *
110  * Creates and shows a regular gtk+ file chooser dialog with
111  * a given title.  The user's music directory (typically ~/Music) is
112  * added as a shortcut.
113  * 
114  * For consistency, this should be used anywhere a file chooser is required.
115  *
116  * After creating the dialog, the caller should connect a handler to its 
117  * 'response' signal to process the user's selected files or folders.
118  *
119  * Return value: (transfer full): the file chooser #GtkWidget
120  */
121 GtkWidget *
122 rb_file_chooser_new (const char *title,
123 		     GtkWindow *parent,
124 		     GtkFileChooserAction action,
125 		     gboolean local_only)
126 {
127 	GtkWidget *dialog;
128 
129 	if (action == GTK_FILE_CHOOSER_ACTION_OPEN	    ||
130 	    action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
131 	    action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER) {
132 		dialog = gtk_file_chooser_dialog_new (title, parent,
133 						      action,
134 						      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
135 						      GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
136 						      NULL);
137 		gtk_dialog_set_default_response (GTK_DIALOG (dialog),
138 						 GTK_RESPONSE_ACCEPT);
139 	} else if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
140 		dialog = gtk_file_chooser_dialog_new (title, parent,
141 						      action,
142 						      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
143 						      GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
144 						      NULL);
145 		gtk_dialog_set_default_response (GTK_DIALOG (dialog),
146 						 GTK_RESPONSE_ACCEPT);
147 		gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE);
148 	} else {
149 		g_assert_not_reached ();
150 		return NULL;
151 	}
152 
153 	gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (dialog), local_only);
154 	gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (dialog),
155 					      rb_music_dir (),
156 					      NULL);
157 
158 	if (parent != NULL) {
159 		gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent));
160 		gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
161 	}
162 
163 	gtk_widget_show_all (dialog);
164 
165 	return dialog;
166 }