Codepage messages related translated & other stuff...
[midnight-commander.git] / gnome / gcustom-layout.c
blobbcf4235f959da300e4f6a3576dced3c3e5e7de2b
1 /* Custom layout preferences box for the Midnight Commander
3 * Copyright (C) 1999 The Free Software Foundation
5 * Author: Owen Taylor <otaylor@redhat.com>
6 */
8 #include <config.h>
9 #include "x.h"
10 #include <ctype.h>
11 #include "panel.h"
12 #include "gcustom-layout.h"
13 #include "gscreen.h"
15 typedef struct _ColumnInfo ColumnInfo;
17 struct _ColumnInfo {
18 gchar *name;
19 gchar *value;
22 struct _GCustomLayout {
23 GtkWidget *table;
24 GtkWidget *srcList;
25 GtkWidget *destList;
26 GtkWidget *addButton;
27 GtkWidget *delButton;
28 GHashTable *hash;
29 GnomePropertyBox *prop_box;
30 WPanel *panel;
33 ColumnInfo possible_columns[] = {
34 { N_("Access Time"), "atime" },
35 { N_("Creation Time"), "ctime" },
36 { N_("Group"), "group" },
37 { N_("Group ID"), "ngid" },
38 { N_("Inode Number"), "inode" },
39 { N_("Mode"), "mode" },
40 { N_("Modification Time"), "mtime" },
41 { N_("Name"), "name" },
42 { N_("Number of Hard Links"), "nlink" },
43 { N_("Owner"), "owner" },
44 { N_("Permission"), "perm" },
45 { N_("Size"), "size" },
46 { N_("Size (short)"), "bsize" },
47 { N_("Type"), "type" },
48 { N_("User ID"), "nuid" }
50 gint n_possible_columns = sizeof(possible_columns) / sizeof(possible_columns[0]);
52 static void
53 custom_layout_add_clicked (GtkWidget *widget, GCustomLayout *layout)
55 gint row, new_row;
56 ColumnInfo *info;
57 gchar *tmp_name;
59 if (GTK_CLIST (layout->srcList)->selection) {
60 row = GPOINTER_TO_UINT (GTK_CLIST (layout->srcList)->selection->data);
62 info = gtk_clist_get_row_data (GTK_CLIST (layout->srcList), row);
64 tmp_name = gettext (info->name);
65 new_row = gtk_clist_append (GTK_CLIST (layout->destList), &tmp_name);
66 gtk_clist_set_row_data (GTK_CLIST (layout->destList),
67 new_row,
68 info);
69 gtk_clist_select_row (GTK_CLIST (layout->destList), new_row, 0);
71 gtk_widget_set_sensitive (layout->addButton, FALSE);
72 gnome_property_box_changed (layout->prop_box);
74 if (GTK_CLIST (layout->destList)->rows >= 2)
75 gtk_widget_set_sensitive (layout->delButton, TRUE);
78 static void
79 custom_layout_del_clicked (GtkWidget *widget, GCustomLayout *layout)
81 gint row;
83 if (GTK_CLIST (layout->destList)->selection) {
84 row = GPOINTER_TO_UINT (GTK_CLIST (layout->destList)->selection->data);
85 gtk_clist_remove (GTK_CLIST (layout->destList), row);
86 gnome_property_box_changed (layout->prop_box);
88 if (GTK_CLIST (layout->destList)->rows <= 1)
89 gtk_widget_set_sensitive (layout->delButton, FALSE);
92 static void
93 custom_layout_select_row (GtkWidget *widget, gint row, gint col, GdkEvent *event,
94 GCustomLayout *layout)
96 gint i;
97 ColumnInfo *info, *tmp_info;
99 info = gtk_clist_get_row_data (GTK_CLIST (layout->srcList), row);
101 for (i=0; i<GTK_CLIST (layout->destList)->rows; i++) {
102 tmp_info = gtk_clist_get_row_data (GTK_CLIST (layout->destList), i);
103 if (tmp_info == info) {
104 gtk_widget_set_sensitive (layout->addButton, FALSE);
105 return;
109 gtk_widget_set_sensitive (layout->addButton, TRUE);
112 static void
113 custom_layout_row_move (GtkWidget *widget,
114 gint source_row, gint dest_row,
115 GCustomLayout *layout)
117 gnome_property_box_changed (layout->prop_box);
120 static void
121 custom_layout_destroy (GtkWidget *widget, GCustomLayout *layout)
123 g_hash_table_destroy (layout->hash);
124 g_free (layout);
127 static void
128 custom_layout_create(GCustomLayout *layout, ColumnInfo *columns, gint ncolumns)
130 GtkWidget *vbox2;
131 GtkWidget *scrollwin;
132 GtkWidget *label;
133 GtkWidget *align;
134 gint i;
135 gchar *tmp_name;
137 layout->table = gtk_table_new(3, 1, FALSE);
138 gtk_container_set_border_width (GTK_CONTAINER (layout->table), GNOME_PAD_SMALL);
140 /* make list of possible columns to add */
141 vbox2 = gtk_vbox_new(FALSE, GNOME_PAD_SMALL);
142 label = gtk_label_new (_("Possible Columns"));
143 gtk_box_pack_start (GTK_BOX (vbox2), label, FALSE, FALSE, 0);
144 scrollwin = gtk_scrolled_window_new(NULL, NULL);
145 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollwin),
146 GTK_POLICY_AUTOMATIC,
147 GTK_POLICY_AUTOMATIC);
148 gtk_box_pack_start (GTK_BOX (vbox2), scrollwin, TRUE, TRUE, 0);
150 layout->srcList = gtk_clist_new(1);
151 gtk_container_add(GTK_CONTAINER(scrollwin), layout->srcList);
152 gtk_widget_set_usize(layout->srcList, 150, 200);
153 gtk_clist_set_selection_mode(GTK_CLIST(layout->srcList),
154 GTK_SELECTION_BROWSE);
155 gtk_clist_set_column_auto_resize(GTK_CLIST(layout->srcList), 0, 1);
156 gtk_table_attach(GTK_TABLE(layout->table), vbox2, 0, 1, 0, 1,
157 GTK_FILL | GTK_EXPAND | GTK_SHRINK,
158 GTK_FILL | GTK_EXPAND | GTK_SHRINK, 0, 0);
160 /* make list of currently displayed column types */
161 vbox2 = gtk_vbox_new(FALSE, GNOME_PAD_SMALL);
162 label = gtk_label_new (_("Displayed Columns"));
163 gtk_box_pack_start (GTK_BOX (vbox2), label, FALSE, FALSE, 0);
164 scrollwin = gtk_scrolled_window_new(NULL, NULL);
165 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollwin),
166 GTK_POLICY_AUTOMATIC,
167 GTK_POLICY_AUTOMATIC);
168 gtk_box_pack_start (GTK_BOX (vbox2), scrollwin, TRUE, TRUE, 0);
170 layout->destList = gtk_clist_new(1);
171 gtk_container_add(GTK_CONTAINER(scrollwin), layout->destList);
172 gtk_widget_set_usize(layout->destList, 150, 200);
173 gtk_clist_set_selection_mode(GTK_CLIST(layout->destList),
174 GTK_SELECTION_BROWSE);
175 gtk_clist_set_column_auto_resize(GTK_CLIST(layout->destList), 0, 1);
176 gtk_clist_set_reorderable (GTK_CLIST (layout->destList), TRUE);
178 gtk_table_attach(GTK_TABLE(layout->table), vbox2, 2, 3, 0, 1,
179 GTK_FILL | GTK_EXPAND | GTK_SHRINK,
180 GTK_FILL | GTK_EXPAND | GTK_SHRINK, 0, 0);
182 /* add add/remove buttons in center */
183 layout->addButton = gtk_button_new_with_label(_("Add"));
184 layout->delButton = gtk_button_new_with_label(_("Remove"));
186 align = gtk_alignment_new(0.0, 0.5, 0, 0);
187 vbox2 = gtk_vbox_new(TRUE, 0);
188 gtk_box_pack_start(GTK_BOX(vbox2), layout->addButton, FALSE, FALSE, 0);
189 gtk_box_pack_start(GTK_BOX(vbox2), layout->delButton, FALSE, FALSE, 0);
190 gtk_container_add(GTK_CONTAINER(align), vbox2);
191 gtk_table_attach(GTK_TABLE(layout->table), align, 1, 2, 0, 1, 0, 0, 5, 5);
193 layout->hash = g_hash_table_new (g_str_hash, g_str_equal);
195 for (i = 0; i < ncolumns; i++) {
196 tmp_name = gettext(columns[i].name);
197 gtk_clist_append (GTK_CLIST (layout->srcList), &tmp_name);
198 gtk_clist_set_row_data (GTK_CLIST (layout->srcList),
200 &columns[i]);
202 g_hash_table_insert (layout->hash,
203 columns[i].value,
204 &columns[i]);
207 gtk_signal_connect(GTK_OBJECT(layout->addButton), "clicked",
208 GTK_SIGNAL_FUNC(custom_layout_add_clicked),
209 layout);
210 gtk_signal_connect(GTK_OBJECT(layout->delButton), "clicked",
211 GTK_SIGNAL_FUNC(custom_layout_del_clicked),
212 layout);
213 gtk_signal_connect(GTK_OBJECT(layout->srcList), "select_row",
214 GTK_SIGNAL_FUNC(custom_layout_select_row),
215 layout);
216 gtk_signal_connect(GTK_OBJECT(layout->destList), "row_move",
217 GTK_SIGNAL_FUNC(custom_layout_row_move),
218 layout);
220 gtk_widget_show_all (layout->table);
222 gtk_signal_connect(GTK_OBJECT(layout->table), "destroy",
223 GTK_SIGNAL_FUNC(custom_layout_destroy), layout);
226 static void
227 custom_layout_set (GCustomLayout *layout, gchar *string)
229 gint new_row;
230 gint i;
231 gchar **strings;
232 ColumnInfo *info;
233 gchar *tmp_name;
235 gtk_clist_clear (GTK_CLIST (layout->destList));
237 /* skip over initial half or full */
238 while (*string && !isspace(*string))
239 string++;
240 while (*string && isspace(*string))
241 string++;
243 strings = g_strsplit (string, ",", -1);
245 for (i=0; strings[i]; i++) {
246 info = g_hash_table_lookup (layout->hash, strings[i]);
247 if (info) {
248 tmp_name = gettext (info->name);
249 new_row = gtk_clist_append (GTK_CLIST (layout->destList), &tmp_name);
250 gtk_clist_set_row_data (GTK_CLIST (layout->destList),
251 new_row, info);
255 g_strfreev (strings);
257 /* Set the status of the "Add" button correctly */
259 if (GTK_CLIST (layout->srcList)->selection) {
260 gint row = GPOINTER_TO_UINT (GTK_CLIST (layout->srcList)->selection->data);
261 custom_layout_select_row (NULL, row, 0, NULL, layout);
263 /* Set the status of the "Remove" button correctly */
264 if (GTK_CLIST (layout->destList)->rows <= 1)
265 gtk_widget_set_sensitive (layout->delButton, FALSE);
269 static gchar *
270 custom_layout_get (GCustomLayout *layout)
272 gint i;
273 GString *result;
274 gchar *string;
275 ColumnInfo *info;
277 result = g_string_new ("full ");
279 for (i=0; i<GTK_CLIST (layout->destList)->rows; i++) {
280 if (i != 0)
281 g_string_append_c (result, ',');
282 info = gtk_clist_get_row_data (GTK_CLIST (layout->destList), i);
283 g_string_append (result, info->value);
286 string = result->str;
287 g_string_free (result, FALSE);
289 return string;
292 GCustomLayout *
293 custom_layout_create_page (GnomePropertyBox *prop_box, WPanel *panel)
295 GCustomLayout *layout;
297 layout = g_new (GCustomLayout, 1);
298 custom_layout_create (layout, possible_columns, n_possible_columns);
299 layout->prop_box = prop_box;
300 layout->panel = panel;
302 gnome_property_box_append_page (GNOME_PROPERTY_BOX (prop_box),
303 layout->table,
304 gtk_label_new (_("Custom View")));
306 custom_layout_set (layout, panel->user_format);
308 return layout;
311 void
312 custom_layout_apply (GCustomLayout *layout)
314 gchar *format;
315 GList *tmp_list;
316 PanelContainer *container;
318 format = custom_layout_get (layout);
320 tmp_list = containers;
321 while (tmp_list) {
322 container = tmp_list->data;
324 g_free (container->panel->user_format);
325 container->panel->user_format = g_strdup (format);
327 g_free (default_user_format);
328 default_user_format = g_strdup (format);
330 set_panel_formats (container->panel);
332 tmp_list = tmp_list->next;
335 g_free (format);