r1644: Don't close the Set Run Action box after showing the usage message if the...
[rox-filer.git] / ROX-Filer / src / abox.c
blob278293e0811dad4c9c56d8f4a7b170fd8113813e
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2002, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* abox.c - the dialog box widget used for filer operations.
24 * The actual code for specific operations is in action.c.
27 #include "config.h"
29 #include <string.h>
31 #include "global.h"
33 #include "main.h"
34 #include "abox.h"
35 #include "gui_support.h"
36 #include "filer.h"
37 #include "display.h"
39 #define RESPONSE_QUIET 1
41 /* Static prototypes */
42 static void abox_class_init(GObjectClass *gclass, gpointer data);
43 static void abox_init(GTypeInstance *object, gpointer gclass);
44 static void response(GtkDialog *dialog, gint response_id);
45 static void abox_finalise(GObject *object);
46 static void shade(ABox *abox);
48 GType abox_get_type(void)
50 static GType type = 0;
52 if (!type)
54 static const GTypeInfo info =
56 sizeof (ABoxClass),
57 NULL, /* base_init */
58 NULL, /* base_finalise */
59 (GClassInitFunc) abox_class_init,
60 NULL, /* class_finalise */
61 NULL, /* class_data */
62 sizeof(ABox),
63 0, /* n_preallocs */
64 (GInstanceInitFunc) abox_init
67 type = g_type_register_static(GTK_TYPE_DIALOG,
68 "ABox", &info, 0);
71 return type;
74 GtkWidget* abox_new(const gchar *title, gboolean quiet)
76 GtkWidget *widget;
77 ABox *abox;
79 widget = GTK_WIDGET(gtk_widget_new(abox_get_type(), NULL));
80 abox = (ABox *) widget;
82 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(abox->quiet), quiet);
84 gtk_window_set_title(GTK_WINDOW(widget), title);
85 gtk_dialog_set_has_separator(GTK_DIALOG(widget), FALSE);
87 return widget;
90 static void abox_class_init(GObjectClass *gclass, gpointer data)
92 GtkDialogClass *dialog = (GtkDialogClass *) gclass;
94 dialog->response = response;
96 g_signal_new("flag_toggled", G_TYPE_FROM_CLASS(gclass),
97 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET(ABoxClass, flag_toggled),
98 NULL, NULL, g_cclosure_marshal_VOID__INT,
99 G_TYPE_NONE, 1, G_TYPE_INT);
101 gclass->finalize = abox_finalise;
104 static void abox_init(GTypeInstance *object, gpointer gclass)
106 GtkWidget *frame, *text, *scrollbar, *button;
107 ABox *abox = ABOX(object);
108 GtkDialog *dialog = GTK_DIALOG(object);
110 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
112 abox->dir_label = gtk_label_new(_("<dir>"));
113 gtk_widget_set_size_request(abox->dir_label, 8, -1);
114 abox->results = NULL;
115 abox->entry = NULL;
116 abox->next_dir = NULL;
117 abox->next_timer = 0;
118 abox->question = FALSE;
119 gtk_misc_set_alignment(GTK_MISC(abox->dir_label), 0.5, 0.5);
120 gtk_box_pack_start(GTK_BOX(dialog->vbox),
121 abox->dir_label, FALSE, TRUE, 0);
123 abox->log_hbox = gtk_hbox_new(FALSE, 0);
124 gtk_box_pack_start(GTK_BOX(dialog->vbox),
125 abox->log_hbox, TRUE, TRUE, 4);
127 frame = gtk_frame_new(NULL);
128 gtk_box_pack_start_defaults(GTK_BOX(abox->log_hbox), frame);
130 text = gtk_text_view_new();
131 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
132 gtk_container_add(GTK_CONTAINER(frame), text);
134 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
135 abox->log = text;
137 scrollbar = gtk_vscrollbar_new(NULL);
138 gtk_widget_set_scroll_adjustments(text, NULL,
139 gtk_range_get_adjustment(GTK_RANGE(scrollbar)));
140 gtk_text_buffer_create_tag(
141 gtk_text_view_get_buffer(GTK_TEXT_VIEW(abox->log)),
142 "error", "foreground", "red",
143 NULL);
144 gtk_text_buffer_create_tag(
145 gtk_text_view_get_buffer(GTK_TEXT_VIEW(abox->log)),
146 "question", "weight", "bold",
147 NULL);
148 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
149 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(text), FALSE);
150 gtk_widget_set_size_request(text, 400, 100);
152 gtk_box_pack_start(GTK_BOX(abox->log_hbox), scrollbar, FALSE, TRUE, 0);
154 gtk_dialog_add_buttons(dialog,
155 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
156 GTK_STOCK_NO, GTK_RESPONSE_NO,
157 GTK_STOCK_YES, GTK_RESPONSE_YES,
158 NULL);
160 abox->flag_box = gtk_hbox_new(FALSE, 16);
161 gtk_box_pack_end(GTK_BOX(dialog->vbox),
162 abox->flag_box, FALSE, TRUE, 2);
164 button = button_new_mixed(GTK_STOCK_GOTO_LAST, _("_Quiet"));
165 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
166 gtk_dialog_add_action_widget(dialog, button, RESPONSE_QUIET);
167 gtk_dialog_set_default_response(dialog, RESPONSE_QUIET);
169 gtk_widget_show_all(dialog->vbox);
171 abox->quiet = abox_add_flag(abox,
172 _("Quiet"), _("Don't confirm every operation"),
173 'Q', FALSE);
175 shade(abox);
178 static void flag_toggled(GtkToggleButton *toggle, ABox *abox)
180 gint code;
182 code = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(toggle),
183 "abox-response"));
184 if (code == 'Q')
185 shade(abox);
187 g_signal_emit_by_name(abox, "flag_toggled", code);
191 GtkWidget *abox_add_flag(ABox *abox, const gchar *label, const gchar *tip,
192 gint response, gboolean default_value)
194 GtkWidget *check;
196 check = gtk_check_button_new_with_label(label);
197 gtk_tooltips_set_tip(tooltips, check, tip, NULL);
198 g_object_set_data(G_OBJECT(check), "abox-response",
199 GINT_TO_POINTER(response));
200 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), default_value);
201 g_signal_connect(check, "toggled", G_CALLBACK(flag_toggled), abox);
202 gtk_box_pack_end(GTK_BOX(abox->flag_box), check, FALSE, TRUE, 0);
203 gtk_widget_show(check);
205 return check;
208 static void response(GtkDialog *dialog, gint response_id)
210 ABox *abox = ABOX(dialog);
212 if (response_id == GTK_RESPONSE_CANCEL)
213 gtk_widget_destroy(GTK_WIDGET(dialog));
214 else if (response_id == RESPONSE_QUIET)
216 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(abox->quiet),
217 TRUE);
218 gtk_dialog_response(dialog, GTK_RESPONSE_YES);
220 else if (response_id == GTK_RESPONSE_YES ||
221 response_id == GTK_RESPONSE_NO)
223 abox->question = FALSE;
224 shade(abox);
228 /* Display the question. Unshade the Yes, No and entry box (if any).
229 * Will send a response signal when the user makes a choice.
231 void abox_ask(ABox *abox, const gchar *question)
233 g_return_if_fail(abox != NULL);
234 g_return_if_fail(question != NULL);
235 g_return_if_fail(IS_ABOX(abox));
237 abox_log(abox, question, "question");
239 abox->question = TRUE;
240 shade(abox);
243 void abox_cancel_ask(ABox *abox)
245 g_return_if_fail(abox != NULL);
246 g_return_if_fail(IS_ABOX(abox));
248 abox->question = FALSE;
249 shade(abox);
252 void abox_log(ABox *abox, const gchar *message, const gchar *style)
254 GtkTextIter end;
255 GtkTextBuffer *text_buffer;
256 GtkTextView *log = GTK_TEXT_VIEW(abox->log);
258 text_buffer = gtk_text_view_get_buffer(log);
260 gtk_text_buffer_get_end_iter(text_buffer, &end);
261 gtk_text_buffer_insert_with_tags_by_name(text_buffer,
262 &end, message, -1, style, NULL);
263 gtk_text_view_scroll_to_mark(
264 log,
265 gtk_text_buffer_get_mark(text_buffer, "insert"),
266 0.0, FALSE, 0, 0);
269 static void abox_finalise(GObject *object)
271 GObjectClass *parent_class;
272 ABox *abox = ABOX(object);
274 if (abox->next_dir)
276 g_free(abox->next_dir);
277 abox->next_dir = NULL;
278 gtk_timeout_remove(abox->next_timer);
281 parent_class = gtk_type_class(GTK_TYPE_DIALOG);
283 if (G_OBJECT_CLASS(parent_class)->finalize)
284 (*G_OBJECT_CLASS(parent_class)->finalize)(object);
287 static gboolean show_next_dir(gpointer data)
289 ABox *abox = (ABox *) data;
291 gtk_label_set_text(GTK_LABEL(abox->dir_label), abox->next_dir);
292 g_free(abox->next_dir);
293 abox->next_dir = NULL;
295 return FALSE;
298 /* Display this message in the current-object area.
299 * The display won't update too fast, even if you call this very often.
301 void abox_set_current_object(ABox *abox, const gchar *message)
303 g_return_if_fail(abox != NULL);
304 g_return_if_fail(message != NULL);
305 g_return_if_fail(IS_ABOX(abox));
307 /* If a string is already set then replace it, but assume the
308 * timer is already running...
311 if (abox->next_dir)
312 g_free(abox->next_dir);
313 else
315 gtk_label_set_text(GTK_LABEL(abox->dir_label), message);
316 abox->next_timer = gtk_timeout_add(500, show_next_dir, abox);
319 abox->next_dir = g_strdup(message);
322 static void lost_preview(GtkWidget *window, ABox *abox)
324 abox->preview = NULL;
327 static void select_row_callback(GtkTreeView *treeview,
328 GtkTreePath *path,
329 GtkTreeViewColumn *col,
330 ABox *abox)
332 GtkTreeModel *model;
333 GtkTreeIter iter;
334 char *leaf, *dir;
336 model = gtk_tree_view_get_model(GTK_TREE_VIEW(abox->results));
337 gtk_tree_model_get_iter(model, &iter, path);
338 gtk_tree_model_get(model, &iter, 0, &leaf, 1, &dir, -1);
340 if (abox->preview)
342 if (strcmp(abox->preview->real_path, dir) == 0)
343 display_set_autoselect(abox->preview, leaf);
344 else
345 filer_change_to(abox->preview, dir, leaf);
346 goto out;
349 abox->preview = filer_opendir(dir, NULL);
350 if (abox->preview)
352 display_set_autoselect(abox->preview, leaf);
353 g_signal_connect_object(abox->preview->window, "destroy",
354 G_CALLBACK(lost_preview), abox, 0);
357 out:
358 g_free(dir);
359 g_free(leaf);
362 /* Add a list-of-results area. You must use this before adding files
363 * with abox_add_filename().
365 void abox_add_results(ABox *abox)
367 GtkTreeViewColumn *column;
368 GtkWidget *scroller, *frame;
369 GtkListStore *model;
370 GtkCellRenderer *cell_renderer;
372 g_return_if_fail(abox != NULL);
373 g_return_if_fail(IS_ABOX(abox));
374 g_return_if_fail(abox->results == NULL);
376 scroller = gtk_scrolled_window_new(NULL, NULL);
377 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroller),
378 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
380 frame = gtk_frame_new(NULL);
381 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
382 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(abox)->vbox),
383 frame, TRUE, TRUE, 4);
385 gtk_container_add(GTK_CONTAINER(frame), scroller);
387 model = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING);
388 abox->results = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
389 g_object_unref(G_OBJECT(model));
391 cell_renderer = gtk_cell_renderer_text_new();
392 column = gtk_tree_view_column_new_with_attributes(
393 _("Name"), cell_renderer, "text", 0, NULL);
394 gtk_tree_view_column_set_resizable(column, TRUE);
395 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
396 gtk_tree_view_append_column(GTK_TREE_VIEW(abox->results), column);
397 gtk_tree_view_insert_column_with_attributes(
398 GTK_TREE_VIEW(abox->results),
399 1, (gchar *) _("Directory"), cell_renderer,
400 "text", 1, NULL);
402 gtk_container_add(GTK_CONTAINER(scroller), abox->results);
404 gtk_widget_set_size_request(abox->results, 100, 100);
405 gtk_box_set_child_packing(GTK_BOX(GTK_DIALOG(abox)->vbox),
406 abox->log_hbox, FALSE, TRUE, 4, GTK_PACK_START);
408 g_signal_connect(abox->results, "row-activated",
409 G_CALLBACK(select_row_callback), abox);
412 void abox_add_filename(ABox *abox, const gchar *path)
414 GtkTreeModel *model;
415 GtkTreeIter iter;
416 gchar *dir;
418 model = gtk_tree_view_get_model(GTK_TREE_VIEW(abox->results));
420 gtk_list_store_append(GTK_LIST_STORE(model), &iter);
422 dir = g_dirname(path);
423 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
424 0, g_basename(path),
425 1, dir, -1);
426 g_free(dir);
429 /* Clear search results area */
430 void abox_clear_results(ABox *abox)
432 GtkTreeModel *model;
434 g_return_if_fail(abox != NULL);
435 g_return_if_fail(IS_ABOX(abox));
437 model = gtk_tree_view_get_model(GTK_TREE_VIEW(abox->results));
439 gtk_list_store_clear(GTK_LIST_STORE(model));
442 void abox_add_combo(ABox *abox, GList *presets,
443 const gchar *text, GtkWidget *help_button)
445 GtkWidget *hbox, *label, *combo;
447 g_return_if_fail(abox != NULL);
448 g_return_if_fail(IS_ABOX(abox));
449 g_return_if_fail(abox->entry == NULL);
451 hbox = gtk_hbox_new(FALSE, 0);
452 label = gtk_label_new(_("Command:"));
453 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, TRUE, 4);
455 combo = gtk_combo_new();
456 gtk_combo_disable_activate(GTK_COMBO(combo));
457 gtk_combo_set_use_arrows_always(GTK_COMBO(combo), TRUE);
458 gtk_combo_set_popdown_strings(GTK_COMBO(combo), presets);
459 abox->entry = GTK_COMBO(combo)->entry;
460 gtk_entry_set_activates_default(GTK_ENTRY(abox->entry), TRUE);
462 gtk_entry_set_text(GTK_ENTRY(abox->entry), text);
463 gtk_box_pack_start(GTK_BOX(hbox), combo, TRUE, TRUE, 4);
465 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(abox)->vbox), hbox,
466 FALSE, TRUE, 0);
467 gtk_box_pack_start(GTK_BOX(hbox), help_button, FALSE, TRUE, 4);
469 gtk_widget_show_all(hbox);
471 shade(abox);
474 void abox_add_entry(ABox *abox, const gchar *text, GtkWidget *help_button)
476 GtkWidget *hbox, *label;
478 g_return_if_fail(abox != NULL);
479 g_return_if_fail(IS_ABOX(abox));
480 g_return_if_fail(abox->entry == NULL);
482 hbox = gtk_hbox_new(FALSE, 0);
483 label = gtk_label_new(_("Expression:"));
484 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, TRUE, 4);
485 abox->entry = gtk_entry_new();
486 gtk_widget_set_name(abox->entry, "fixed-style");
487 gtk_entry_set_text(GTK_ENTRY(abox->entry), text);
488 gtk_box_pack_start(GTK_BOX(hbox), abox->entry, TRUE, TRUE, 4);
489 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(abox)->vbox),
490 hbox, FALSE, TRUE, 4);
491 gtk_box_pack_start(GTK_BOX(hbox), help_button,
492 FALSE, TRUE, 4);
494 gtk_entry_set_activates_default(GTK_ENTRY(abox->entry), TRUE);
496 gtk_widget_show_all(hbox);
498 shade(abox);
501 static void shade(ABox *abox)
503 GtkDialog *dialog = (GtkDialog *) abox;
504 gboolean quiet, on = abox->question;
506 quiet = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(abox->quiet));
508 gtk_dialog_set_response_sensitive(dialog, GTK_RESPONSE_YES, on);
509 gtk_dialog_set_response_sensitive(dialog, GTK_RESPONSE_NO, on);
511 if (on && !quiet)
512 gtk_dialog_set_response_sensitive(dialog, RESPONSE_QUIET, TRUE);
513 else
514 gtk_dialog_set_response_sensitive(dialog,
515 RESPONSE_QUIET, FALSE);
517 /* Unsetting the focus means that set_default will put it in the
518 * right place...
520 gtk_window_set_focus(GTK_WINDOW(abox), NULL);
521 /* Note: Gtk+-2.0.0 will segfault on Return if an insensitive
522 * widget is the default.
524 if (quiet)
525 gtk_dialog_set_default_response(dialog, GTK_RESPONSE_YES);
526 else
527 gtk_dialog_set_default_response(dialog, RESPONSE_QUIET);
529 if (abox->entry)
531 gtk_widget_set_sensitive(abox->entry, on);
532 if (on)
533 gtk_widget_grab_focus(abox->entry);