* gcustom-layout.c, gprefs.c: Make the preferences dialog
[midnight-commander.git] / gnome / gcustom-layout.c
blob37925eae307772f5f266dd01ab8d894672c5f209
1 /* GNU Midnight Commander -- GNOME edition
3 * Preferences page for custom list view
5 * Copyright (C) 1999 The Free Software Foundation
7 * Author: Owen Taylor
8 */
10 #include <config.h>
11 #include "x.h"
12 #include <ctype.h>
13 #include "panel.h"
14 #include "gcustom-layout.h"
15 #include "gscreen.h"
17 typedef struct _ColumnInfo ColumnInfo;
19 struct _ColumnInfo {
20 gchar *name;
21 gchar *value;
24 struct _GCustomLayout {
25 GtkWidget *table;
26 GtkWidget *srcList;
27 GtkWidget *destList;
28 GtkWidget *addButton;
29 GHashTable *hash;
30 GnomePropertyBox *prop_box;
31 WPanel *panel;
34 ColumnInfo possible_columns[] = {
35 { N_("Access Time"), "atime" },
36 { N_("Creation Time"), "ctime" },
37 { N_("Group"), "group" },
38 { N_("Group ID"), "ngid" },
39 { N_("Inode Number"), "inode" },
40 { N_("Mode"), "mode" },
41 { N_("Modification Time"), "mtime" },
42 { N_("Name"), "name" },
43 { N_("Number of Hard Links"), "nlink" },
44 { N_("Owner"), "owner" },
45 { N_("Permission"), "perm" },
46 { N_("Size"), "size" },
47 { N_("Size (short)"), "bsize" },
48 { N_("Type"), "type" },
49 { N_("User ID"), "nuid" }
51 gint n_possible_columns = sizeof(possible_columns) / sizeof(possible_columns[0]);
53 static void
54 custom_layout_add_clicked (GtkWidget *widget, GCustomLayout *layout)
56 gint row, new_row;
57 ColumnInfo *info;
58 gchar *tmp_name;
60 if (GTK_CLIST (layout->srcList)->selection) {
61 row = GPOINTER_TO_UINT (GTK_CLIST (layout->srcList)->selection->data);
63 info = gtk_clist_get_row_data (GTK_CLIST (layout->srcList), row);
65 tmp_name = gettext (info->name);
66 new_row = gtk_clist_append (GTK_CLIST (layout->destList), &tmp_name);
67 gtk_clist_set_row_data (GTK_CLIST (layout->destList),
68 new_row,
69 info);
70 gtk_clist_select_row (GTK_CLIST (layout->destList), new_row, 0);
72 gtk_widget_set_sensitive (layout->addButton, FALSE);
73 gnome_property_box_changed (layout->prop_box);
77 static void
78 custom_layout_del_clicked (GtkWidget *widget, GCustomLayout *layout)
80 gint row;
82 if (GTK_CLIST (layout->destList)->selection) {
83 row = GPOINTER_TO_UINT (GTK_CLIST (layout->destList)->selection->data);
84 gtk_clist_remove (GTK_CLIST (layout->destList), row);
85 gnome_property_box_changed (layout->prop_box);
89 static void
90 custom_layout_select_row (GtkWidget *widget, gint row, gint col, GdkEvent *event,
91 GCustomLayout *layout)
93 gint i;
94 ColumnInfo *info, *tmp_info;
96 info = gtk_clist_get_row_data (GTK_CLIST (layout->srcList), row);
98 for (i=0; i<GTK_CLIST (layout->destList)->rows; i++) {
99 tmp_info = gtk_clist_get_row_data (GTK_CLIST (layout->destList), i);
100 if (tmp_info == info) {
101 gtk_widget_set_sensitive (layout->addButton, FALSE);
102 return;
106 gtk_widget_set_sensitive (layout->addButton, TRUE);
109 static void
110 custom_layout_row_move (GtkWidget *widget,
111 gint source_row, gint dest_row,
112 GCustomLayout *layout)
114 gnome_property_box_changed (layout->prop_box);
117 static void
118 custom_layout_destroy (GtkWidget *widget, GCustomLayout *layout)
120 g_hash_table_destroy (layout->hash);
121 g_free (layout);
124 static void
125 custom_layout_create(GCustomLayout *layout, ColumnInfo *columns, gint ncolumns)
127 GtkWidget *vbox2;
128 GtkWidget *scrollwin;
129 GtkWidget *delButton;
130 GtkWidget *label;
131 GtkWidget *align;
132 gint i;
133 gchar *tmp_name;
135 layout->table = gtk_table_new(3, 1, FALSE);
136 gtk_container_set_border_width (GTK_CONTAINER (layout->table), GNOME_PAD_SMALL);
138 /* make list of possible columns to add */
139 vbox2 = gtk_vbox_new(FALSE, GNOME_PAD_SMALL);
140 label = gtk_label_new (_("Possible Columns"));
141 gtk_box_pack_start (GTK_BOX (vbox2), label, FALSE, FALSE, 0);
142 scrollwin = gtk_scrolled_window_new(NULL, NULL);
143 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollwin),
144 GTK_POLICY_AUTOMATIC,
145 GTK_POLICY_AUTOMATIC);
146 gtk_box_pack_start (GTK_BOX (vbox2), scrollwin, TRUE, TRUE, 0);
148 layout->srcList = gtk_clist_new(1);
149 gtk_container_add(GTK_CONTAINER(scrollwin), layout->srcList);
150 gtk_widget_set_usize(layout->srcList, 150, 200);
151 gtk_clist_set_selection_mode(GTK_CLIST(layout->srcList),
152 GTK_SELECTION_BROWSE);
153 gtk_clist_set_column_auto_resize(GTK_CLIST(layout->srcList), 0, 1);
154 gtk_table_attach(GTK_TABLE(layout->table), vbox2, 0, 1, 0, 1,
155 GTK_FILL | GTK_EXPAND | GTK_SHRINK,
156 GTK_FILL | GTK_EXPAND | GTK_SHRINK, 0, 0);
158 /* make list of currently displayed column types */
159 vbox2 = gtk_vbox_new(FALSE, GNOME_PAD_SMALL);
160 label = gtk_label_new (_("Displayed Columns"));
161 gtk_box_pack_start (GTK_BOX (vbox2), label, FALSE, FALSE, 0);
162 scrollwin = gtk_scrolled_window_new(NULL, NULL);
163 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollwin),
164 GTK_POLICY_AUTOMATIC,
165 GTK_POLICY_AUTOMATIC);
166 gtk_box_pack_start (GTK_BOX (vbox2), scrollwin, TRUE, TRUE, 0);
168 layout->destList = gtk_clist_new(1);
169 gtk_container_add(GTK_CONTAINER(scrollwin), layout->destList);
170 gtk_widget_set_usize(layout->destList, 150, 200);
171 gtk_clist_set_selection_mode(GTK_CLIST(layout->destList),
172 GTK_SELECTION_BROWSE);
173 gtk_clist_set_column_auto_resize(GTK_CLIST(layout->destList), 0, 1);
174 gtk_clist_set_reorderable (GTK_CLIST (layout->destList), TRUE);
176 gtk_table_attach(GTK_TABLE(layout->table), vbox2, 2, 3, 0, 1,
177 GTK_FILL | GTK_EXPAND | GTK_SHRINK,
178 GTK_FILL | GTK_EXPAND | GTK_SHRINK, 0, 0);
180 /* add add/remove buttons in center */
181 layout->addButton = gtk_button_new_with_label(_("Add"));
182 delButton = gtk_button_new_with_label(_("Remove"));
184 align = gtk_alignment_new(0.0, 0.5, 0, 0);
185 vbox2 = gtk_vbox_new(TRUE, 0);
186 gtk_box_pack_start(GTK_BOX(vbox2), layout->addButton, FALSE, FALSE, 0);
187 gtk_box_pack_start(GTK_BOX(vbox2), delButton, FALSE, FALSE, 0);
188 gtk_container_add(GTK_CONTAINER(align), vbox2);
189 gtk_table_attach(GTK_TABLE(layout->table), align, 1, 2, 0, 1, 0, 0, 5, 5);
191 layout->hash = g_hash_table_new (g_str_hash, g_str_equal);
193 for (i = 0; i < ncolumns; i++) {
194 tmp_name = gettext(columns[i].name);
195 gtk_clist_append (GTK_CLIST (layout->srcList), &tmp_name);
196 gtk_clist_set_row_data (GTK_CLIST (layout->srcList),
198 &columns[i]);
200 g_hash_table_insert (layout->hash,
201 columns[i].value,
202 &columns[i]);
205 gtk_signal_connect(GTK_OBJECT(layout->addButton), "clicked",
206 GTK_SIGNAL_FUNC(custom_layout_add_clicked),
207 layout);
208 gtk_signal_connect(GTK_OBJECT(delButton), "clicked",
209 GTK_SIGNAL_FUNC(custom_layout_del_clicked),
210 layout);
211 gtk_signal_connect(GTK_OBJECT(layout->srcList), "select_row",
212 GTK_SIGNAL_FUNC(custom_layout_select_row),
213 layout);
214 gtk_signal_connect(GTK_OBJECT(layout->destList), "row_move",
215 GTK_SIGNAL_FUNC(custom_layout_row_move),
216 layout);
218 gtk_widget_show_all (layout->table);
220 gtk_signal_connect(GTK_OBJECT(layout->table), "destroy",
221 GTK_SIGNAL_FUNC(custom_layout_destroy), layout);
224 static void
225 custom_layout_set (GCustomLayout *layout, gchar *string)
227 gint new_row;
228 gint i;
229 gchar **strings;
230 ColumnInfo *info;
231 gchar *tmp_name;
233 gtk_clist_clear (GTK_CLIST (layout->destList));
235 /* skip over initial half or full */
236 while (*string && !isspace(*string))
237 string++;
238 while (*string && isspace(*string))
239 string++;
241 strings = g_strsplit (string, ",", -1);
243 for (i=0; strings[i]; i++) {
244 info = g_hash_table_lookup (layout->hash, strings[i]);
245 if (info) {
246 tmp_name = gettext (info->name);
247 new_row = gtk_clist_append (GTK_CLIST (layout->destList), &tmp_name);
248 gtk_clist_set_row_data (GTK_CLIST (layout->destList),
249 new_row, info);
253 g_strfreev (strings);
255 /* Set the status of the "Add" button correctly */
257 if (GTK_CLIST (layout->srcList)->selection) {
258 gint row = GPOINTER_TO_UINT (GTK_CLIST (layout->srcList)->selection->data);
259 custom_layout_select_row (NULL, row, 0, NULL, layout);
264 static gchar *
265 custom_layout_get (GCustomLayout *layout)
267 gint i;
268 GString *result;
269 gchar *string;
270 ColumnInfo *info;
272 result = g_string_new ("full ");
274 for (i=0; i<GTK_CLIST (layout->destList)->rows; i++) {
275 if (i != 0)
276 g_string_append_c (result, ',');
277 info = gtk_clist_get_row_data (GTK_CLIST (layout->destList), i);
278 g_string_append (result, info->value);
281 string = result->str;
282 g_string_free (result, FALSE);
284 return string;
287 GCustomLayout *
288 custom_layout_create_page (GnomePropertyBox *prop_box, WPanel *panel)
290 GCustomLayout *layout;
292 layout = g_new (GCustomLayout, 1);
293 custom_layout_create (layout, possible_columns, n_possible_columns);
294 layout->prop_box = prop_box;
295 layout->panel = panel;
297 gnome_property_box_append_page (GNOME_PROPERTY_BOX (prop_box),
298 layout->table,
299 gtk_label_new (_("Custom View")));
301 custom_layout_set (layout, panel->user_format);
303 return layout;
306 void
307 custom_layout_apply (GCustomLayout *layout)
309 gchar *format;
310 GList *tmp_list;
311 PanelContainer *container;
313 format = custom_layout_get (layout);
315 tmp_list = containers;
316 while (tmp_list) {
317 container = tmp_list->data;
319 g_free (container->panel->user_format);
320 container->panel->user_format = g_strdup (format);
322 set_panel_formats (container->panel);
324 tmp_list = tmp_list->next;
327 g_free (format);