2008-05-05 Paolo Borelli <pborelli@katamail.com>
[nautilus.git] / src / nautilus-location-dialog.c
blob464057b0f660535b1bd2bd25f2e2be3049d9be92
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
3 /*
4 * Nautilus
6 * Copyright (C) 2003 Ximian, Inc.
8 * Nautilus is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * Nautilus is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public
19 * License along with this program; see the file COPYING. If not,
20 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
24 #include <config.h>
25 #include "nautilus-location-dialog.h"
27 #include <eel/eel-gtk-macros.h>
28 #include <eel/eel-stock-dialogs.h>
29 #include <gtk/gtkhbox.h>
30 #include <gtk/gtklabel.h>
31 #include <gtk/gtkstock.h>
32 #include <libgnomeui/gnome-help.h>
33 #include <libnautilus-private/nautilus-file-utilities.h>
34 #include "nautilus-location-entry.h"
35 #include "nautilus-desktop-window.h"
37 struct _NautilusLocationDialogDetails {
38 GtkWidget *entry;
39 NautilusWindow *window;
42 static void nautilus_location_dialog_class_init (NautilusLocationDialogClass *class);
43 static void nautilus_location_dialog_init (NautilusLocationDialog *dialog);
45 EEL_CLASS_BOILERPLATE (NautilusLocationDialog,
46 nautilus_location_dialog,
47 GTK_TYPE_DIALOG)
48 enum {
49 RESPONSE_OPEN
50 };
52 static void
53 nautilus_location_dialog_finalize (GObject *object)
55 NautilusLocationDialog *dialog;
57 dialog = NAUTILUS_LOCATION_DIALOG (object);
59 g_free (dialog->details);
61 EEL_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
64 static void
65 nautilus_location_dialog_destroy (GtkObject *object)
67 NautilusLocationDialog *dialog;
69 dialog = NAUTILUS_LOCATION_DIALOG (object);
71 EEL_CALL_PARENT (GTK_OBJECT_CLASS, destroy, (object));
74 static void
75 open_current_location (NautilusLocationDialog *dialog)
77 GFile *location;
78 char *user_location;
80 user_location = gtk_editable_get_chars (GTK_EDITABLE (dialog->details->entry), 0, -1);
81 location = g_file_parse_name (user_location);
82 nautilus_window_go_to (dialog->details->window, location);
83 g_object_unref (location);
84 g_free (user_location);
87 static void
88 response_callback (NautilusLocationDialog *dialog,
89 int response_id,
90 gpointer data)
92 GError *error;
94 switch (response_id) {
95 case RESPONSE_OPEN :
96 open_current_location (dialog);
97 gtk_widget_destroy (GTK_WIDGET (dialog));
98 break;
99 case GTK_RESPONSE_NONE :
100 case GTK_RESPONSE_DELETE_EVENT :
101 case GTK_RESPONSE_CANCEL :
102 gtk_widget_destroy (GTK_WIDGET (dialog));
103 break;
104 case GTK_RESPONSE_HELP :
105 error = NULL;
106 gnome_help_display_desktop_on_screen (NULL, "user-guide", "user-guide.xml",
107 "nautilus-open-location",
108 gtk_window_get_screen (GTK_WINDOW (dialog)),
109 &error);
110 if (error) {
111 eel_show_error_dialog (_("There was an error displaying help."), error->message,
112 GTK_WINDOW (dialog));
113 g_error_free (error);
115 break;
116 default :
117 g_assert_not_reached ();
121 static void
122 entry_activate_callback (GtkEntry *entry,
123 gpointer user_data)
125 NautilusLocationDialog *dialog;
127 dialog = NAUTILUS_LOCATION_DIALOG (user_data);
128 gtk_dialog_response (GTK_DIALOG (dialog), RESPONSE_OPEN);
131 static void
132 nautilus_location_dialog_class_init (NautilusLocationDialogClass *class)
134 GObjectClass *gobject_class;
135 GtkObjectClass *object_class;
137 gobject_class = G_OBJECT_CLASS (class);
138 gobject_class->finalize = nautilus_location_dialog_finalize;
140 object_class = GTK_OBJECT_CLASS (class);
141 object_class->destroy = nautilus_location_dialog_destroy;
144 static void
145 nautilus_location_dialog_init (NautilusLocationDialog *dialog)
147 GtkWidget *box;
148 GtkWidget *label;
150 dialog->details = g_new0 (NautilusLocationDialogDetails, 1);
152 gtk_window_set_title (GTK_WINDOW (dialog), _("Open Location"));
153 gtk_window_set_default_size (GTK_WINDOW (dialog), 300, -1);
154 gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
155 gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
156 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
157 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 2);
159 box = gtk_hbox_new (FALSE, 12);
160 gtk_container_set_border_width (GTK_CONTAINER (box), 5);
161 gtk_widget_show (box);
163 label = gtk_label_new_with_mnemonic (_("_Location:"));
164 gtk_widget_show (label);
165 gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
167 dialog->details->entry = nautilus_location_entry_new ();
168 gtk_entry_set_width_chars (GTK_ENTRY (dialog->details->entry), 30);
169 g_signal_connect (dialog->details->entry,
170 "activate",
171 G_CALLBACK (entry_activate_callback),
172 dialog);
174 gtk_widget_show (dialog->details->entry);
176 gtk_label_set_mnemonic_widget (GTK_LABEL (label), dialog->details->entry);
178 gtk_box_pack_start (GTK_BOX (box), dialog->details->entry,
179 TRUE, TRUE, 0);
181 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
182 box, FALSE, TRUE, 0);
184 gtk_dialog_add_button (GTK_DIALOG (dialog),
185 GTK_STOCK_HELP,
186 GTK_RESPONSE_HELP);
187 gtk_dialog_add_button (GTK_DIALOG (dialog),
188 GTK_STOCK_CANCEL,
189 GTK_RESPONSE_CANCEL);
190 gtk_dialog_add_button (GTK_DIALOG (dialog),
191 GTK_STOCK_OPEN,
192 RESPONSE_OPEN);
193 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
194 RESPONSE_OPEN);
196 g_signal_connect (dialog, "response",
197 G_CALLBACK (response_callback),
198 dialog);
201 GtkWidget *
202 nautilus_location_dialog_new (NautilusWindow *window)
204 GtkWidget *dialog;
205 GFile *location;
206 char *formatted_location;
208 dialog = gtk_widget_new (NAUTILUS_TYPE_LOCATION_DIALOG, NULL);
210 if (window) {
211 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (window));
212 gtk_window_set_screen (GTK_WINDOW (dialog),
213 gtk_window_get_screen (GTK_WINDOW (window)));
214 NAUTILUS_LOCATION_DIALOG (dialog)->details->window = window;
218 location = nautilus_window_get_location (window);
219 if (location != NULL) {
220 if (NAUTILUS_IS_DESKTOP_WINDOW (window)) {
221 formatted_location = g_strdup_printf ("%s/", g_get_home_dir ());
222 } else {
223 formatted_location = g_file_get_parse_name (location);
225 nautilus_entry_set_text (NAUTILUS_ENTRY (NAUTILUS_LOCATION_DIALOG (dialog)->details->entry), formatted_location);
226 gtk_editable_select_region (GTK_EDITABLE (NAUTILUS_LOCATION_DIALOG (dialog)->details->entry), 0, -1);
227 gtk_editable_set_position (GTK_EDITABLE (NAUTILUS_LOCATION_DIALOG (dialog)->details->entry), -1);
228 g_free (formatted_location);
229 g_object_unref (location);
232 gtk_widget_grab_focus (NAUTILUS_LOCATION_DIALOG (dialog)->details->entry);
234 return dialog;
237 void
238 nautilus_location_dialog_set_location (NautilusLocationDialog *dialog,
239 const char *location)
241 nautilus_entry_set_text (NAUTILUS_ENTRY (NAUTILUS_LOCATION_DIALOG (dialog)->details->entry), location);
242 gtk_editable_select_region (GTK_EDITABLE (NAUTILUS_LOCATION_DIALOG (dialog)->details->entry), 0, 0);
243 gtk_editable_set_position (GTK_EDITABLE (NAUTILUS_LOCATION_DIALOG (dialog)->details->entry), -1);