Gosh, some little cosmetics here...
[midnight-commander.git] / gnome / gprefs.c
blob9eab3c99eed15d9cddf87b2a8a58d3e736b89ac3
1 /*
2 * Preferences configuration page for the GNU Midnight Commander
4 * Author:
5 * Jonathan Blandford (jrb@redhat.com)
6 */
7 #include <config.h>
8 #include "x.h"
9 #include <stdio.h>
10 #include <sys/stat.h>
11 #include "dir.h"
12 #include "panel.h"
13 #include "gscreen.h"
14 #include "main.h"
15 #include "gmain.h"
16 #include "cmd.h"
17 #include "boxes.h"
18 #include "profile.h"
19 #include "setup.h"
20 #include "panelize.h"
21 #include "dialog.h"
22 #include "layout.h"
23 #include "gcustom-layout.h"
24 #include "gdesktop-prefs.h"
25 #include "../vfs/vfs.h"
26 #include "gprefs.h"
28 /* Orphan confirmation options */
29 /* Auto save setup */
30 /* use internal edit */ /* maybe have a mime-type capplet for these */
31 /* use internal view */
32 /* complete: show all */
33 /* Animation??? */
34 /* advanced chown */
35 /* cd follows links */
36 /* safe delete */
38 extern int vfs_timeout;
39 extern int ftpfs_always_use_proxy;
40 extern char* ftpfs_anonymous_passwd;
41 extern int file_op_compute_totals;
42 extern int we_can_afford_the_speed;
43 typedef enum
45 PROPERTY_NONE,
46 PROPERTY_BOOL,
47 PROPERTY_STRING,
48 PROPERTY_INT,
49 PROPERTY_CUSTOM
50 } PropertyType_e;
52 typedef struct
54 gchar *label;
55 PropertyType_e type;
56 gpointer property_variable;
57 gpointer extra_data1;
58 gpointer extra_data2;
60 GtkWidget *widget;
61 } Property;
63 typedef struct
65 gchar *title;
66 Property *props;
67 } PrefsPage;
69 typedef struct
71 WPanel *panel;
72 GtkWidget *prop_box;
73 PrefsPage *prefs_pages;
75 GDesktopPrefs *desktop_prefs;
76 gint desktop_prefs_page;
78 GCustomLayout *custom_layout;
79 gint custom_layout_page;
80 } PrefsDlg;
83 #define PROPERTIES_DONE { NULL, PROPERTY_NONE, NULL, NULL, NULL, NULL }
84 #define PREFSPAGES_DONE { NULL, NULL }
86 typedef GtkWidget* (*CustomCreateFunc) (PrefsDlg *dlg, Property *prop);
87 typedef void (*CustomApplyFunc) (PrefsDlg *dlg, Property *prop);
89 static Property file_display_props [] =
92 N_("Show backup files"), PROPERTY_BOOL,
93 &show_backups, NULL, NULL, NULL
96 N_("Show hidden files"), PROPERTY_BOOL,
97 &show_dot_files, NULL, NULL, NULL
100 N_("Mix files and directories"), PROPERTY_BOOL,
101 &mix_all_files, NULL, NULL, NULL
104 N_("Use shell patterns instead of regular expressions"), PROPERTY_BOOL,
105 &easy_patterns, NULL, NULL, NULL
108 N_("Determine file types from file content instead of extensions"), PROPERTY_BOOL,
109 &use_magic, NULL, NULL, NULL
111 PROPERTIES_DONE
114 static Property confirmation_props [] =
117 N_("Confirm when deleting file"), PROPERTY_BOOL,
118 &confirm_delete, NULL, NULL, NULL
121 N_("Confirm when overwriting files"), PROPERTY_BOOL,
122 &confirm_overwrite, NULL, NULL, NULL
125 N_("Confirm when executing files"), PROPERTY_BOOL,
126 &confirm_execute, NULL, NULL, NULL
129 N_("Show progress while operations are being performed"), PROPERTY_BOOL,
130 &verbose, NULL, NULL, NULL
132 PROPERTIES_DONE
135 #ifdef USE_VFS
136 static Property vfs_props [] =
139 N_("VFS Timeout:"), PROPERTY_INT,
140 &vfs_timeout, N_("Seconds"), NULL, NULL
143 N_("Anonymous FTP password:"), PROPERTY_STRING,
144 &ftpfs_anonymous_passwd, NULL, NULL, NULL
147 N_("Always use FTP proxy"), PROPERTY_BOOL,
148 &ftpfs_always_use_proxy, NULL, NULL, NULL
150 PROPERTIES_DONE
152 #endif /* !USE_VFS */
154 static Property caching_and_optimization_props [] =
157 N_("Fast directory reload"), PROPERTY_BOOL,
158 &fast_reload, NULL, NULL, NULL
161 N_("Compute totals before copying files"), PROPERTY_BOOL,
162 &file_op_compute_totals, NULL, NULL, NULL
164 #ifdef USE_VFS
166 N_("FTP directory cache timeout :"), PROPERTY_INT,
167 &ftpfs_directory_timeout, N_("Seconds"), NULL, NULL
169 #endif /* !USE_VFS */
171 N_("Allow customization of icons in icon view"), PROPERTY_BOOL,
172 &we_can_afford_the_speed, NULL, NULL, NULL
174 PROPERTIES_DONE
177 static PrefsPage prefs_pages [] =
180 N_("File display"),
181 file_display_props
184 N_("Confirmation"),
185 confirmation_props
187 #ifdef USE_VFS
189 N_("VFS"),
190 vfs_props
192 #endif /* !USE_VFS */
194 N_("Caching"),
195 caching_and_optimization_props
197 PREFSPAGES_DONE
200 static void
201 apply_changes_bool (PrefsDlg *dlg, Property *cur_prop)
203 GtkWidget *checkbox;
205 checkbox = cur_prop->widget;
207 if (GTK_TOGGLE_BUTTON (checkbox)->active)
208 *( (int*) cur_prop->property_variable) = TRUE;
209 else
210 *( (int*) cur_prop->property_variable) = FALSE;
213 static void
214 apply_changes_string (PrefsDlg *dlg, Property *cur_prop)
216 GtkWidget *entry;
217 gchar *text;
219 entry = cur_prop->widget;
221 text = gtk_entry_get_text (GTK_ENTRY (gnome_entry_gtk_entry (GNOME_ENTRY (entry))));
223 *( (char**) cur_prop->property_variable) = g_strdup (text);
226 static void
227 apply_changes_int (PrefsDlg *dlg, Property *cur_prop)
229 GtkWidget *entry;
230 gdouble val;
231 gchar *num;
233 entry = cur_prop->widget;
234 num = gtk_entry_get_text (GTK_ENTRY (gnome_entry_gtk_entry (GNOME_ENTRY (entry))));
235 sscanf(num,"%lg",&val);
237 *( (int*) cur_prop->property_variable) = (gint) val;
240 static void
241 apply_changes_custom (PrefsDlg *dlg, Property *cur_prop)
243 CustomApplyFunc apply = (CustomApplyFunc) cur_prop->extra_data2;
245 apply (dlg, cur_prop);
248 static void
249 apply_page_changes (PrefsDlg *dlg, gint pagenum)
251 Property *props;
252 Property cur_prop;
253 gint i;
255 props = dlg->prefs_pages [pagenum].props;
256 i = 0;
257 cur_prop = props [i];
258 while (cur_prop.label != NULL) {
259 switch (cur_prop.type) {
260 case PROPERTY_NONE :
261 g_warning ("Invalid case in gprefs.c: apply_page_changes");
262 break;
263 case PROPERTY_BOOL :
264 apply_changes_bool (dlg, &cur_prop);
265 break;
266 case PROPERTY_STRING :
267 apply_changes_string (dlg, &cur_prop);
268 break;
269 case PROPERTY_INT :
270 apply_changes_int (dlg, &cur_prop);
271 break;
272 case PROPERTY_CUSTOM :
273 apply_changes_custom (dlg, &cur_prop);
274 break;
276 cur_prop = props[++i];
280 static void
281 apply_callback (GtkWidget *prop_box, gint pagenum, PrefsDlg *dlg)
283 if (pagenum == dlg->desktop_prefs_page)
284 desktop_prefs_apply (dlg->desktop_prefs);
285 else if (pagenum == dlg->custom_layout_page)
286 custom_layout_apply (dlg->custom_layout);
287 else if (pagenum != -1)
288 apply_page_changes (dlg, pagenum);
289 else {
290 update_panels (UP_RELOAD, UP_KEEPSEL);
291 save_setup ();
295 static void
296 changed_callback (GtkWidget *widget, PrefsDlg *dlg)
298 if (dlg->prop_box)
299 gnome_property_box_changed (GNOME_PROPERTY_BOX (dlg->prop_box));
302 static GtkWidget*
303 create_prop_bool (PrefsDlg *dlg, Property *prop)
305 GtkWidget *checkbox;
307 checkbox = gtk_check_button_new_with_label (_(prop->label));
309 if (*((int*) prop->property_variable)) {
310 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (checkbox),
311 TRUE);
312 } else {
313 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (checkbox),
314 FALSE);
317 gtk_signal_connect (GTK_OBJECT (checkbox), "clicked",
318 changed_callback, (gpointer) dlg);
320 prop->widget = checkbox;
321 gtk_widget_show_all (checkbox);
322 return checkbox;
325 static GtkWidget*
326 create_prop_string (PrefsDlg *dlg, Property *prop)
328 GtkWidget *entry;
329 GtkWidget *gtk_entry;
330 GtkWidget *label;
331 GtkWidget *hbox;
332 gint max_length;
334 hbox = gtk_hbox_new (FALSE, GNOME_PAD_SMALL);
336 label = gtk_label_new (_(prop->label));
337 gtk_box_pack_start (GTK_BOX (hbox), label,
338 FALSE, FALSE, 0);
340 entry = gnome_entry_new (_(prop->label));
341 gtk_entry = gnome_entry_gtk_entry (GNOME_ENTRY (entry));
343 max_length = (int)prop->extra_data1;
344 if (max_length != 0) {
345 gtk_entry_set_max_length (GTK_ENTRY (gtk_entry),
346 max_length);
349 gtk_entry_set_text (GTK_ENTRY (gtk_entry),
350 (gchar*) *( (gchar**) prop->property_variable));
351 gtk_signal_connect_while_alive (GTK_OBJECT (gtk_entry),
352 "changed",
353 GTK_SIGNAL_FUNC (changed_callback),
354 (gpointer) dlg,
355 GTK_OBJECT (dlg->prop_box));
357 gtk_box_pack_start (GTK_BOX (hbox), entry,
358 FALSE, FALSE, 0);
360 prop->widget = entry;
361 gtk_widget_show_all (hbox);
362 return hbox;
365 static GtkWidget*
366 create_prop_int (PrefsDlg *dlg, Property *prop)
368 GtkWidget *entry;
369 GtkWidget *label;
370 GtkWidget *hbox;
371 gchar buffer [10];
373 hbox = gtk_hbox_new (FALSE, GNOME_PAD_SMALL);
375 label = gtk_label_new (_(prop->label));
376 gtk_box_pack_start (GTK_BOX (hbox), label,
377 FALSE, FALSE, 0);
379 entry = gnome_entry_new (_(prop->label));
381 g_snprintf (buffer, 9, "%d", *( (int*) prop->property_variable));
383 gtk_entry_set_text (GTK_ENTRY (gnome_entry_gtk_entry (GNOME_ENTRY (entry))),
384 buffer);
386 gtk_signal_connect_while_alive (GTK_OBJECT (gnome_entry_gtk_entry (GNOME_ENTRY (entry))),
387 "changed",
388 GTK_SIGNAL_FUNC (changed_callback),
389 (gpointer) dlg,
390 GTK_OBJECT (dlg->prop_box));
392 gtk_box_pack_start (GTK_BOX (hbox), entry,
393 FALSE, FALSE, 0);
394 if (prop->extra_data1) {
395 label = gtk_label_new (_((gchar *)prop->extra_data1));
396 gtk_box_pack_start (GTK_BOX (hbox), label,
397 FALSE, FALSE, 0);
400 prop->widget = entry;
401 gtk_widget_show_all (hbox);
402 return hbox;
405 static GtkWidget*
406 create_prop_custom (PrefsDlg *dlg, Property *prop)
408 CustomCreateFunc create = (CustomCreateFunc) prop->extra_data1;
410 if (!create)
411 return create_prop_bool (dlg, prop);
413 return create (dlg, prop);
416 static GtkWidget*
417 create_prop_widget (PrefsDlg *dlg, Property *prop)
419 switch (prop->type) {
420 case PROPERTY_NONE :
421 g_warning ("Invalid case in gprefs.c: create_prop_widget");
422 break;
423 case PROPERTY_BOOL :
424 return create_prop_bool (dlg, prop);
425 case PROPERTY_STRING :
426 return create_prop_string (dlg, prop);
427 case PROPERTY_INT :
428 return create_prop_int (dlg, prop);
429 case PROPERTY_CUSTOM :
430 return create_prop_custom (dlg, prop);
431 break;
433 return NULL;
436 static void
437 create_page (PrefsDlg *dlg, PrefsPage *page)
439 GtkWidget *vbox;
440 GtkWidget *prop_widget;
441 Property *cur_prop;
442 gint i;
444 vbox = gtk_vbox_new (FALSE, GNOME_PAD_SMALL);
445 gtk_container_set_border_width (GTK_CONTAINER (vbox), GNOME_PAD_SMALL);
446 gtk_widget_show (vbox);
447 i = 0;
448 cur_prop = &(page->props [i]);
449 while (cur_prop->label != NULL) {
450 cur_prop = &(page->props [i]);
451 prop_widget = create_prop_widget (dlg, cur_prop);
452 gtk_box_pack_start (GTK_BOX (vbox), prop_widget,
453 FALSE, FALSE, 0);
454 i++;
455 cur_prop = &(page->props [i]);
458 gnome_property_box_append_page (GNOME_PROPERTY_BOX (dlg->prop_box),
459 vbox,
460 gtk_label_new (_(page->title)));
463 /* Help callback for the preferences dialog */
464 static void
465 help_callback (GnomePropertyBox *pb, gint page_num, gpointer data)
467 static GnomeHelpMenuEntry entry = {
468 "gmc",
469 "gmcprefs.html"
472 gnome_help_display (NULL, &entry);
475 static void
476 create_prop_box (PrefsDlg *dlg)
478 gint i;
479 PrefsPage *cur_page;
481 dlg->prop_box = gnome_property_box_new ();
482 if (dlg->panel)
483 gnome_dialog_set_parent (GNOME_DIALOG (dlg->prop_box),
484 GTK_WINDOW (dlg->panel->xwindow));
485 gtk_window_set_modal (GTK_WINDOW (dlg->prop_box), TRUE);
486 gtk_window_set_title (GTK_WINDOW (dlg->prop_box), _("Preferences"));
488 i = 0;
489 cur_page = &(dlg->prefs_pages [i]);
490 while (cur_page->title != NULL) {
491 create_page (dlg, cur_page);
492 i++;
493 cur_page = &(dlg->prefs_pages [i]);
496 dlg->desktop_prefs = desktop_prefs_new (GNOME_PROPERTY_BOX (dlg->prop_box));
497 dlg->desktop_prefs_page = i++;
499 if (dlg->panel)
500 dlg->custom_layout = custom_layout_create_page (
501 GNOME_PROPERTY_BOX (dlg->prop_box), dlg->panel);
503 dlg->custom_layout_page = i;
505 gtk_signal_connect (GTK_OBJECT (dlg->prop_box), "apply",
506 GTK_SIGNAL_FUNC (apply_callback), dlg);
507 gtk_signal_connect (GTK_OBJECT (dlg->prop_box), "help",
508 GTK_SIGNAL_FUNC (help_callback), dlg);
511 void
512 gnome_configure_box_with_desktop (GtkWidget *widget, WPanel *panel,
513 gboolean desktop)
515 static PrefsDlg dlg;
517 dlg.panel = panel;
518 dlg.prefs_pages = prefs_pages;
520 create_prop_box (&dlg);
522 if (desktop)
523 gtk_notebook_set_page (
524 GTK_NOTEBOOK (GNOME_PROPERTY_BOX (
525 dlg.prop_box)->notebook),
526 dlg.desktop_prefs_page);
527 gtk_widget_show (dlg.prop_box);
530 void
531 gnome_configure_box (GtkWidget *widget, WPanel *panel)
533 gnome_configure_box_with_desktop (widget, panel, FALSE);