Merge branch '44_more_functionally_u7z'
[pantumic.git] / src / screen.c
blob45c401460e863e5c9b0115f2a82ca6f399f53c50
1 /* Panel managing.
2 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 Written by: 1995 Miguel de Icaza
16 1997, 1999 Timur Bakeyev
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22 /** \file screen.c
23 * \brief Source: panel managin module
26 #include <config.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
34 #include "global.h"
36 #include "../src/tty/tty.h"
37 #include "../src/skin/skin.h"
38 #include "../src/tty/mouse.h" /* For Gpm_Event */
39 #include "../src/tty/key.h" /* XCTRL and ALT macros */
40 #include "../src/filehighlight/fhl.h"
42 #include "dir.h"
43 #include "panel.h"
44 #include "tree.h"
45 #include "ext.h" /* regexp_command */
46 #include "layout.h" /* Most layout variables are here */
47 #include "wtools.h" /* for message (...) */
48 #include "cmd.h"
49 #include "command.h" /* cmdline */
50 #include "setup.h" /* For loading/saving panel options */
51 #include "user.h"
52 #include "../src/mcconfig/mcconfig.h"
53 #include "execute.h"
54 #include "widget.h"
55 #include "menu.h" /* menubar_visible */
56 #include "main-widgets.h"
57 #include "main.h"
58 #include "unixcompat.h"
59 #include "mountlist.h" /* my_statfs */
60 #include "selcodepage.h" /* select_charset () */
61 #include "charsets.h" /* get_codepage_id () */
62 #include "strutil.h"
64 #define ELEMENTS(arr) ( sizeof(arr) / sizeof((arr)[0]) )
66 #define NORMAL 0
67 #define SELECTED 1
68 #define MARKED 2
69 #define MARKED_SELECTED 3
70 #define STATUS 5
73 * This describes a format item. The parse_display_format routine parses
74 * the user specified format and creates a linked list of format_e structures.
76 typedef struct format_e {
77 struct format_e *next;
78 int requested_field_len;
79 int field_len;
80 align_crt_t just_mode;
81 int expand;
82 const char *(*string_fn)(file_entry *, int len);
83 const char *title;
84 const char *id;
85 } format_e;
87 /* If true, show the mini-info on the panel */
88 int show_mini_info = 1;
90 /* If true, then use stat() on the cwd to determine directory changes */
91 int fast_reload = 0;
93 /* If true, use some usability hacks by Torben */
94 int torben_fj_mode = 0;
96 /* If true, up/down keys scroll the pane listing by pages */
97 int panel_scroll_pages = 1;
99 /* If 1, we use permission hilighting */
100 int permission_mode = 0;
102 /* If 1 - then add per file type hilighting */
103 int filetype_mode = 1;
105 /* The hook list for the select file function */
106 Hook *select_file_hook = 0;
108 static cb_ret_t panel_callback (Widget *, widget_msg_t msg, int parm);
109 static int panel_event (Gpm_Event *event, void *);
110 static void paint_frame (WPanel *panel);
111 static const char *panel_format (WPanel *panel);
112 static const char *mini_status_format (WPanel *panel);
114 /* This macro extracts the number of available lines in a panel */
115 #define llines(p) (p->widget.lines-3 - (show_mini_info ? 2 : 0))
117 static void
118 set_colors (WPanel *panel)
120 (void) panel;
121 tty_set_normal_attrs ();
122 tty_setcolor (NORMAL_COLOR);
125 /* Delete format string, it is a linked list */
126 static void
127 delete_format (format_e *format)
129 format_e *next;
131 while (format){
132 next = format->next;
133 g_free (format);
134 format = next;
138 /* This code relies on the default justification!!! */
139 static void
140 add_permission_string (char *dest, int width, file_entry *fe, int attr, int color, int is_octal)
142 int i, r, l;
144 l = get_user_permissions (&fe->st);
146 if (is_octal){
147 /* Place of the access bit in octal mode */
148 l = width + l - 3;
149 r = l + 1;
150 } else {
151 /* The same to the triplet in string mode */
152 l = l * 3 + 1;
153 r = l + 3;
156 for(i = 0; i < width; i++){
157 if (i >= l && i < r){
158 if (attr == SELECTED || attr == MARKED_SELECTED)
159 tty_setcolor (MARKED_SELECTED_COLOR);
160 else
161 tty_setcolor (MARKED_COLOR);
162 } else if (color >= 0)
163 tty_setcolor (color);
164 else
165 tty_lowlevel_setcolor (-color);
167 tty_print_char (dest[i]);
171 /* String representations of various file attributes */
172 /* name */
173 static const char *
174 string_file_name (file_entry *fe, int len)
176 static char buffer [MC_MAXPATHLEN * MB_LEN_MAX + 1];
178 (void) len;
179 g_strlcpy (buffer, fe->fname, sizeof(buffer));
180 return buffer;
183 static unsigned int ilog10(dev_t n)
185 unsigned int digits = 0;
186 do {
187 digits++, n /= 10;
188 } while (n != 0);
189 return digits;
192 static void format_device_number (char *buf, size_t bufsize, dev_t dev)
194 dev_t major_dev = major(dev);
195 dev_t minor_dev = minor(dev);
196 unsigned int major_digits = ilog10(major_dev);
197 unsigned int minor_digits = ilog10(minor_dev);
199 g_assert(bufsize >= 1);
200 if (major_digits + 1 + minor_digits + 1 <= bufsize) {
201 g_snprintf(buf, bufsize, "%lu,%lu", (unsigned long) major_dev,
202 (unsigned long) minor_dev);
203 } else {
204 g_strlcpy(buf, _("[dev]"), bufsize);
208 /* size */
209 static const char *
210 string_file_size (file_entry *fe, int len)
212 static char buffer [BUF_TINY];
214 /* Don't ever show size of ".." since we don't calculate it */
215 if (!strcmp (fe->fname, "..")) {
216 return _("UP--DIR");
219 #ifdef HAVE_STRUCT_STAT_ST_RDEV
220 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
221 format_device_number (buffer, len + 1, fe->st.st_rdev);
222 else
223 #endif
225 size_trunc_len (buffer, (unsigned int) len, fe->st.st_size, 0);
227 return buffer;
230 /* bsize */
231 static const char *
232 string_file_size_brief (file_entry *fe, int len)
234 if (S_ISLNK (fe->st.st_mode) && !fe->f.link_to_dir) {
235 return _("SYMLINK");
238 if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && strcmp (fe->fname, "..")) {
239 return _("SUB-DIR");
242 return string_file_size (fe, len);
245 /* This functions return a string representation of a file entry */
246 /* type */
247 static const char *
248 string_file_type (file_entry *fe, int len)
250 static char buffer[2];
252 (void) len;
253 if (S_ISDIR (fe->st.st_mode))
254 buffer[0] = PATH_SEP;
255 else if (S_ISLNK (fe->st.st_mode)) {
256 if (fe->f.link_to_dir)
257 buffer[0] = '~';
258 else if (fe->f.stale_link)
259 buffer[0] = '!';
260 else
261 buffer[0] = '@';
262 } else if (S_ISCHR (fe->st.st_mode))
263 buffer[0] = '-';
264 else if (S_ISSOCK (fe->st.st_mode))
265 buffer[0] = '=';
266 else if (S_ISDOOR (fe->st.st_mode))
267 buffer[0] = '>';
268 else if (S_ISBLK (fe->st.st_mode))
269 buffer[0] = '+';
270 else if (S_ISFIFO (fe->st.st_mode))
271 buffer[0] = '|';
272 else if (S_ISNAM (fe->st.st_mode))
273 buffer[0] = '#';
274 else if (!S_ISREG (fe->st.st_mode))
275 buffer[0] = '?'; /* non-regular of unknown kind */
276 else if (is_exe (fe->st.st_mode))
277 buffer[0] = '*';
278 else
279 buffer[0] = ' ';
280 buffer[1] = '\0';
281 return buffer;
284 /* mtime */
285 static const char *
286 string_file_mtime (file_entry *fe, int len)
288 (void) len;
289 if (!strcmp (fe->fname, "..")) {
290 return "";
292 return file_date (fe->st.st_mtime);
295 /* atime */
296 static const char *
297 string_file_atime (file_entry *fe, int len)
299 (void) len;
300 if (!strcmp (fe->fname, "..")) {
301 return "";
303 return file_date (fe->st.st_atime);
306 /* ctime */
307 static const char *
308 string_file_ctime (file_entry *fe, int len)
310 (void) len;
311 if (!strcmp (fe->fname, "..")) {
312 return "";
314 return file_date (fe->st.st_ctime);
317 /* perm */
318 static const char *
319 string_file_permission (file_entry *fe, int len)
321 (void) len;
322 return string_perm (fe->st.st_mode);
325 /* mode */
326 static const char *
327 string_file_perm_octal (file_entry *fe, int len)
329 static char buffer [10];
331 (void) len;
332 g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode);
333 return buffer;
336 /* nlink */
337 static const char *
338 string_file_nlinks (file_entry *fe, int len)
340 static char buffer[BUF_TINY];
342 (void) len;
343 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
344 return buffer;
347 /* inode */
348 static const char *
349 string_inode (file_entry *fe, int len)
351 static char buffer [10];
353 (void) len;
354 g_snprintf (buffer, sizeof (buffer), "%lu",
355 (unsigned long) fe->st.st_ino);
356 return buffer;
359 /* nuid */
360 static const char *
361 string_file_nuid (file_entry *fe, int len)
363 static char buffer [10];
365 (void) len;
366 g_snprintf (buffer, sizeof (buffer), "%lu",
367 (unsigned long) fe->st.st_uid);
368 return buffer;
371 /* ngid */
372 static const char *
373 string_file_ngid (file_entry *fe, int len)
375 static char buffer [10];
377 (void) len;
378 g_snprintf (buffer, sizeof (buffer), "%lu",
379 (unsigned long) fe->st.st_gid);
380 return buffer;
383 /* owner */
384 static const char *
385 string_file_owner (file_entry *fe, int len)
387 (void) len;
388 return get_owner (fe->st.st_uid);
391 /* group */
392 static const char *
393 string_file_group (file_entry *fe, int len)
395 (void) len;
396 return get_group (fe->st.st_gid);
399 /* mark */
400 static const char *
401 string_marked (file_entry *fe, int len)
403 (void) len;
404 return fe->f.marked ? "*" : " ";
407 /* space */
408 static const char *
409 string_space (file_entry *fe, int len)
411 (void) fe;
412 (void) len;
413 return " ";
416 /* dot */
417 static const char *
418 string_dot (file_entry *fe, int len)
420 (void) fe;
421 (void) len;
422 return ".";
425 #define GT 1
427 static struct {
428 const char *id;
429 int min_size;
430 int expands;
431 align_crt_t default_just;
432 const char *title;
433 int use_in_gui;
434 const char *(*string_fn)(file_entry *, int);
435 sortfn *sort_routine; /* This field is currently unused. */
436 } formats [] = {
437 { "name", 12, 1, J_LEFT_FIT, N_("Name"), 1, string_file_name, (sortfn *) sort_name },
438 { "size", 7, 0, J_RIGHT, N_("Size"), 1, string_file_size, (sortfn *) sort_size },
439 { "bsize", 7, 0, J_RIGHT, N_("Size"), 1, string_file_size_brief, (sortfn *) sort_size },
440 { "type", GT, 0, J_LEFT, "", 2, string_file_type, NULL },
441 { "mtime", 12, 0, J_RIGHT, N_("MTime"), 1, string_file_mtime, (sortfn *) sort_time },
442 { "atime", 12, 0, J_RIGHT, N_("ATime"), 1, string_file_atime, (sortfn *) sort_atime },
443 { "ctime", 12, 0, J_RIGHT, N_("CTime"), 1, string_file_ctime, (sortfn *) sort_ctime },
444 { "perm", 10, 0, J_LEFT, N_("Permission"),1,string_file_permission, NULL },
445 { "mode", 6, 0, J_RIGHT, N_("Perm"), 1, string_file_perm_octal, NULL },
446 { "nlink", 2, 0, J_RIGHT, N_("Nl"), 1, string_file_nlinks, NULL },
447 { "inode", 5, 0, J_RIGHT, N_("Inode"), 1, string_inode, (sortfn *) sort_inode },
448 { "nuid", 5, 0, J_RIGHT, N_("UID"), 1, string_file_nuid, NULL },
449 { "ngid", 5, 0, J_RIGHT, N_("GID"), 1, string_file_ngid, NULL },
450 { "owner", 8, 0, J_LEFT_FIT, N_("Owner"), 1, string_file_owner, NULL },
451 { "group", 8, 0, J_LEFT_FIT, N_("Group"), 1, string_file_group, NULL },
452 { "mark", 1, 0, J_RIGHT, " ", 1, string_marked, NULL },
453 { "|", 1, 0, J_RIGHT, " ", 0, NULL, NULL },
454 { "space", 1, 0, J_RIGHT, " ", 0, string_space, NULL },
455 { "dot", 1, 0, J_RIGHT, " ", 0, string_dot, NULL },
458 static int
459 file_compute_color (int attr, file_entry *fe)
461 switch (attr) {
462 case SELECTED:
463 return (SELECTED_COLOR);
464 case MARKED:
465 return (MARKED_COLOR);
466 case MARKED_SELECTED:
467 return (MARKED_SELECTED_COLOR);
468 case STATUS:
469 return (NORMAL_COLOR);
470 case NORMAL:
471 default:
472 if (!filetype_mode)
473 return (NORMAL_COLOR);
476 return mc_fhl_get_color (mc_filehighlight, fe);
479 /* Formats the file number file_index of panel in the buffer dest */
480 static void
481 format_file (char *dest, int limit, WPanel *panel, int file_index, int width, int attr, int isstatus)
483 int color, length, empty_line;
484 const char *txt;
485 format_e *format, *home;
486 file_entry *fe;
488 (void) dest;
489 (void) limit;
490 length = 0;
491 empty_line = (file_index >= panel->count);
492 home = (isstatus) ? panel->status_format : panel->format;
493 fe = &panel->dir.list [file_index];
495 if (!empty_line)
496 color = file_compute_color (attr, fe);
497 else
498 color = NORMAL_COLOR;
500 for (format = home; format; format = format->next){
501 if (length == width)
502 break;
504 if (format->string_fn){
505 int len, perm;
506 char *preperad_text;
508 if (empty_line)
509 txt = " ";
510 else
511 txt = (*format->string_fn)(fe, format->field_len);
513 len = format->field_len;
514 if (len + length > width)
515 len = width - length;
516 if (len <= 0)
517 break;
519 perm = 0;
520 if (permission_mode) {
521 if (!strcmp(format->id, "perm"))
522 perm = 1;
523 else if (!strcmp(format->id, "mode"))
524 perm = 2;
527 if (color >= 0)
528 tty_setcolor (color);
529 else
530 tty_lowlevel_setcolor (-color);
532 preperad_text = (char*) str_fit_to_term(txt, len, format->just_mode);
533 if (perm)
534 add_permission_string (preperad_text, format->field_len, fe,
535 attr, color, perm - 1);
536 else
537 tty_print_string (preperad_text);
539 length+= len;
540 } else {
541 if (attr == SELECTED || attr == MARKED_SELECTED)
542 tty_setcolor (SELECTED_COLOR);
543 else
544 tty_setcolor (NORMAL_COLOR);
545 tty_print_one_vline ();
546 length++;
550 if (length < width)
551 tty_draw_hline (-1, -1, ' ', width - length);
554 static void
555 repaint_file (WPanel *panel, int file_index, int mv, int attr, int isstatus)
557 int second_column = 0;
558 int width;
559 int offset = 0;
560 char buffer [BUF_MEDIUM];
562 gboolean panel_is_split = !isstatus && panel->split;
564 width = panel->widget.cols - 2;
566 if (panel_is_split) {
567 second_column = (file_index - panel->top_file) / llines (panel);
568 width = width/2 - 1;
570 if (second_column != 0) {
571 offset = 1 + width;
572 /*width = (panel->widget.cols-2) - (panel->widget.cols-2)/2 - 1;*/
573 width = panel->widget.cols - offset - 2;
577 /* Nothing to paint */
578 if (width <= 0)
579 return;
581 if (mv){
582 if (panel_is_split)
583 widget_move (&panel->widget,
584 (file_index - panel->top_file) % llines (panel) + 2,
585 offset + 1);
586 else
587 widget_move (&panel->widget, file_index - panel->top_file + 2, 1);
590 format_file (buffer, sizeof(buffer), panel, file_index, width, attr, isstatus);
592 if (panel_is_split) {
593 if (second_column)
594 tty_print_char (' ');
595 else {
596 tty_setcolor (NORMAL_COLOR);
597 tty_print_one_vline ();
602 static void
603 display_mini_info (WPanel *panel)
605 widget_move (&panel->widget, llines (panel)+3, 1);
607 if (panel->searching){
608 tty_setcolor (INPUT_COLOR);
609 tty_print_char ('/');
610 tty_print_string (str_fit_to_term (panel->search_buffer,
611 panel->widget.cols - 3, J_LEFT));
612 return;
615 /* Status resolves links and show them */
616 set_colors (panel);
618 if (S_ISLNK (panel->dir.list [panel->selected].st.st_mode)){
619 char *link, link_target [MC_MAXPATHLEN];
620 int len;
622 link = concat_dir_and_file (panel->cwd, panel->dir.list [panel->selected].fname);
623 len = mc_readlink (link, link_target, MC_MAXPATHLEN - 1);
624 g_free (link);
625 if (len > 0){
626 link_target[len] = 0;
627 tty_print_string ("-> ");
628 tty_print_string (str_fit_to_term (link_target, panel->widget.cols - 5,
629 J_LEFT_FIT));
630 } else
631 tty_print_string (str_fit_to_term (_("<readlink failed>"),
632 panel->widget.cols - 2, J_LEFT));
633 } else if (strcmp (panel->dir.list [panel->selected].fname, "..") == 0) {
634 /* FIXME:
635 * while loading directory (do_load_dir() and do_reload_dir()),
636 * the actual stat info about ".." directory isn't got;
637 * so just don't display incorrect info about ".." directory */
638 tty_print_string (str_fit_to_term (_("UP--DIR"), panel->widget.cols - 2, J_LEFT));
639 } else
640 /* Default behavior */
641 repaint_file (panel, panel->selected, 0, STATUS, 1);
644 static void
645 paint_dir (WPanel *panel)
647 int i;
648 int color; /* Color value of the line */
649 int items; /* Number of items */
651 items = llines (panel) * (panel->split ? 2 : 1);
653 for (i = 0; i < items; i++){
654 if (i+panel->top_file >= panel->count)
655 color = 0;
656 else {
657 color = 2 * (panel->dir.list [i+panel->top_file].f.marked);
658 color += (panel->selected==i+panel->top_file && panel->active);
660 repaint_file (panel, i+panel->top_file, 1, color, 0);
662 tty_set_normal_attrs ();
665 static void
666 display_total_marked_size (WPanel *panel, int y, int x, gboolean size_only)
668 char buffer[BUF_SMALL], b_bytes[BUF_SMALL], *buf;
669 int cols;
671 if (panel->marked <= 0)
672 return;
674 buf = size_only ? b_bytes : buffer;
675 cols = panel->widget.cols - 2;
678 * This is a trick to use two ngettext() calls in one sentence.
679 * First make "N bytes", then insert it into "X in M files".
681 g_snprintf (b_bytes, sizeof (b_bytes),
682 ngettext("%s byte", "%s bytes", (unsigned long) panel->total),
683 size_trunc_sep (panel->total));
684 if (!size_only)
685 g_snprintf (buffer, sizeof (buffer),
686 ngettext("%s in %d file", "%s in %d files", panel->marked),
687 b_bytes, panel->marked);
689 /* don't forget spaces around buffer content */
690 buf = (char *) str_trunc (buf, cols - 4);
692 if (x < 0)
693 /* center in panel */
694 x = (panel->widget.cols - str_term_width1 (buf)) / 2 - 1;
697 * y == llines (panel) + 2 for mini_info_separator
698 * y == panel->widget.lines - 1 for panel bottom frame
700 widget_move (&panel->widget, y, x);
701 tty_setcolor (MARKED_COLOR);
702 tty_printf (" %s ", buf);
705 static void
706 mini_info_separator (WPanel *panel)
708 const int y = llines (panel) + 2;
710 tty_setcolor (NORMAL_COLOR);
711 tty_draw_hline (panel->widget.y + y, panel->widget.x + 1,
712 ACS_HLINE, panel->widget.cols - 2);
713 /* Status displays total marked size.
714 * Centered in panel, full format. */
715 display_total_marked_size (panel, y, -1, FALSE);
718 static void
719 show_free_space (WPanel *panel)
721 /* Used to figure out how many free space we have */
722 static struct my_statfs myfs_stats;
723 /* Old current working directory for displaying free space */
724 static char *old_cwd = NULL;
726 /* Don't try to stat non-local fs */
727 if (!vfs_file_is_local (panel->cwd) || !free_space)
728 return;
730 if (old_cwd == NULL || strcmp (old_cwd, panel->cwd) != 0) {
731 char rpath[PATH_MAX];
733 init_my_statfs ();
734 g_free (old_cwd);
735 old_cwd = g_strdup (panel->cwd);
737 if (mc_realpath (panel->cwd, rpath) == NULL)
738 return;
740 my_statfs (&myfs_stats, rpath);
743 if (myfs_stats.avail > 0 || myfs_stats.total > 0) {
744 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
745 size_trunc_len (buffer1, sizeof(buffer1) - 1, myfs_stats.avail, 1);
746 size_trunc_len (buffer2, sizeof(buffer2) - 1, myfs_stats.total, 1);
747 g_snprintf (tmp, sizeof(tmp), " %s/%s (%d%%) ", buffer1, buffer2,
748 myfs_stats.total > 0 ?
749 (int)(100 * (double)myfs_stats.avail / myfs_stats.total) : 0);
750 widget_move (&panel->widget, panel->widget.lines - 1,
751 panel->widget.cols - 2 - (int) strlen (tmp));
752 tty_setcolor (NORMAL_COLOR);
753 tty_print_string (tmp);
757 static void
758 show_dir (WPanel *panel)
760 set_colors (panel);
761 draw_box (panel->widget.parent,
762 panel->widget.y, panel->widget.x,
763 panel->widget.lines, panel->widget.cols);
765 if (show_mini_info) {
766 widget_move (&panel->widget, llines (panel) + 2, 0);
767 tty_print_alt_char (ACS_LTEE);
768 widget_move (&panel->widget, llines (panel) + 2,
769 panel->widget.cols - 1);
770 tty_print_alt_char (ACS_RTEE);
773 if (panel->active)
774 tty_setcolor (REVERSE_COLOR);
776 widget_move (&panel->widget, 0, 3);
778 tty_printf (" %s ",
779 str_term_trim (strip_home_and_password (panel->cwd),
780 min (max (panel->widget.cols - 9, 0),
781 panel->widget.cols)));
783 widget_move (&panel->widget, 0, 1);
784 tty_print_string ("<");
785 widget_move (&panel->widget, 0, panel->widget.cols - 2);
786 tty_print_string (">");
787 widget_move (&panel->widget, 0, panel->widget.cols - 3);
788 tty_print_string ("v");
790 if (!show_mini_info) {
791 if (panel->marked == 0) {
792 /* Show size of curret file in the bottom of panel */
793 if (S_ISREG (panel->dir.list [panel->selected].st.st_mode)) {
794 char buffer[BUF_SMALL];
796 g_snprintf (buffer, sizeof (buffer), " %s ",
797 size_trunc_sep (panel->dir.list [panel->selected].st.st_size));
798 tty_setcolor (NORMAL_COLOR);
799 widget_move (&panel->widget, panel->widget.lines - 1, 2);
800 tty_print_string (buffer);
802 } else {
803 /* Show total size of marked files
804 * In the bottom of panel, display size only. */
805 display_total_marked_size (panel, panel->widget.lines - 1, 2, TRUE);
809 show_free_space (panel);
811 if (panel->active)
812 tty_set_normal_attrs ();
815 /* To be used only by long_frame and full_frame to adjust top_file */
816 static void
817 adjust_top_file (WPanel *panel)
819 int old_top = panel->top_file;
821 if (panel->selected - old_top > llines (panel))
822 panel->top_file = panel->selected;
823 if (old_top - panel->count > llines (panel))
824 panel->top_file = panel->count - llines (panel);
827 /* Repaint everything, including frame and separator */
828 static void
829 paint_panel (WPanel *panel)
831 paint_frame (panel); /* including show_dir */
832 paint_dir (panel);
834 if (show_mini_info) {
835 mini_info_separator (panel);
836 display_mini_info (panel);
839 panel->dirty = 0;
842 /* add "#enc:encodning" to end of path */
843 /* if path end width a previous #enc:, only encoding is changed no additional
844 * #enc: is appended
845 * retun new string
847 static char
848 *add_encoding_to_path (const char *path, const char *encoding)
850 char *result;
851 char *semi;
852 char *slash;
854 semi = g_strrstr (path, "#enc:");
856 if (semi != NULL) {
857 slash = strchr (semi, PATH_SEP);
858 if (slash != NULL) {
859 result = g_strconcat (path, "/#enc:", encoding, NULL);
860 } else {
861 *semi = 0;
862 result = g_strconcat (path, "/#enc:", encoding, NULL);
863 *semi = '#';
865 } else {
866 result = g_strconcat (path, "/#enc:", encoding, NULL);
869 return result;
872 char *
873 remove_encoding_from_path (const char *path)
875 GString *ret;
876 GString *tmp_path, *tmp_conv;
877 char *tmp, *tmp2;
878 const char *enc;
879 GIConv converter;
881 ret = g_string_new("");
882 tmp_conv = g_string_new("");
884 tmp_path = g_string_new(path);
886 while ((tmp = g_strrstr (tmp_path->str, "/#enc:")) != NULL){
887 enc = vfs_get_encoding ((const char *) tmp);
888 converter = enc ? str_crt_conv_to (enc): str_cnv_to_term;
889 if (converter == INVALID_CONV) converter = str_cnv_to_term;
891 tmp2=tmp+1;
892 while (*tmp2 && *tmp2 != '/')
893 tmp2++;
895 if (*tmp2){
896 str_vfs_convert_from (converter, tmp2, tmp_conv);
897 g_string_prepend(ret, tmp_conv->str);
898 g_string_set_size(tmp_conv,0);
900 g_string_set_size(tmp_path,tmp - tmp_path->str);
901 str_close_conv (converter);
903 g_string_prepend(ret, tmp_path->str);
904 g_string_free(tmp_path,TRUE);
905 g_string_free(tmp_conv,TRUE);
907 tmp = ret->str;
908 g_string_free(ret, FALSE);
909 return tmp;
913 * Repaint the contents of the panels without frames. To schedule panel
914 * for repainting, set panel->dirty to 1. There are many reasons why
915 * the panels need to be repainted, and this is a costly operation, so
916 * it's done once per event.
918 void
919 update_dirty_panels (void)
921 if (current_panel->dirty)
922 paint_panel (current_panel);
924 if ((get_other_type () == view_listing) && other_panel->dirty)
925 paint_panel (other_panel);
928 static void
929 do_select (WPanel *panel, int i)
931 if (i != panel->selected) {
932 panel->dirty = 1;
933 panel->selected = i;
934 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
935 if (panel->top_file < 0)
936 panel->top_file = 0;
940 static void
941 do_try_to_select (WPanel *panel, const char *name)
943 int i;
944 char *subdir;
946 if (!name) {
947 do_select(panel, 0);
948 return;
951 /* We only want the last component of the directory,
952 * and from this only the name without suffix. */
953 subdir = vfs_strip_suffix_from_filename (x_basename(name));
955 /* Search that subdirectory, if found select it */
956 for (i = 0; i < panel->count; i++){
957 if (strcmp (subdir, panel->dir.list [i].fname) == 0) {
958 do_select (panel, i);
959 g_free (subdir);
960 return;
964 /* Try to select a file near the file that is missing */
965 if (panel->selected >= panel->count)
966 do_select (panel, panel->count-1);
967 g_free (subdir);
970 void
971 try_to_select (WPanel *panel, const char *name)
973 do_try_to_select (panel, name);
974 select_item (panel);
977 void
978 panel_update_cols (Widget *widget, int frame_size)
980 int cols, origin;
982 if (horizontal_split){
983 widget->cols = COLS;
984 return;
987 if (frame_size == frame_full){
988 cols = COLS;
989 origin = 0;
990 } else {
991 if (widget == get_panel_widget (0)){
992 cols = first_panel_size;
993 origin = 0;
994 } else {
995 cols = COLS-first_panel_size;
996 origin = first_panel_size;
1000 widget->cols = cols;
1001 widget->x = origin;
1004 static char *
1005 panel_save_name (WPanel *panel)
1007 extern int saving_setup;
1009 /* If the program is shuting down */
1010 if ((midnight_shutdown && auto_save_setup) || saving_setup)
1011 return g_strdup (panel->panel_name);
1012 else
1013 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1016 void
1017 panel_clean_dir (WPanel *panel)
1019 int count = panel->count;
1021 panel->count = 0;
1022 panel->top_file = 0;
1023 panel->selected = 0;
1024 panel->marked = 0;
1025 panel->dirs_marked = 0;
1026 panel->total = 0;
1027 panel->searching = 0;
1028 panel->is_panelized = 0;
1029 panel->dirty = 1;
1031 clean_dir (&panel->dir, count);
1034 static void
1035 panel_destroy (WPanel *p)
1037 int i;
1039 char *name = panel_save_name (p);
1041 panel_save_setup (p, name);
1042 panel_clean_dir (p);
1044 /* save and clean history */
1045 if (p->dir_history) {
1046 history_put (p->hist_name, p->dir_history);
1048 p->dir_history = g_list_first (p->dir_history);
1049 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1050 g_list_free (p->dir_history);
1053 g_free (p->hist_name);
1055 delete_format (p->format);
1056 delete_format (p->status_format);
1058 g_free (p->user_format);
1059 for (i = 0; i < LIST_TYPES; i++)
1060 g_free (p->user_status_format[i]);
1061 g_free (p->dir.list);
1062 g_free (p->panel_name);
1063 g_free (name);
1066 static void
1067 panel_format_modified (WPanel *panel)
1069 panel->format_modified = 1;
1072 /* Panel creation */
1073 /* The parameter specifies the name of the panel for setup retieving */
1074 WPanel *
1075 panel_new (const char *panel_name)
1077 return panel_new_with_dir(panel_name, NULL);
1080 /* Panel creation for specified directory */
1081 /* The parameter specifies the name of the panel for setup retieving */
1082 /* and the path of working panel directory. If path is NULL then */
1083 /* panel will be created for current directory */
1084 WPanel *
1085 panel_new_with_dir (const char *panel_name, const char *wpath)
1087 WPanel *panel;
1088 char *section;
1089 int i, err;
1090 char curdir[MC_MAXPATHLEN];
1092 panel = g_new0 (WPanel, 1);
1094 /* No know sizes of the panel at startup */
1095 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
1097 /* We do not want the cursor */
1098 widget_want_cursor (panel->widget, 0);
1100 if (wpath) {
1101 g_strlcpy(panel->cwd, wpath, sizeof (panel->cwd));
1102 mc_get_current_wd (curdir, sizeof (curdir) - 2);
1103 } else
1104 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
1106 strcpy (panel->lwd, ".");
1108 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
1109 panel->dir_history = history_get (panel->hist_name);
1110 directory_history_add (panel, panel->cwd);
1112 panel->dir.list = g_new (file_entry, MIN_FILES);
1113 panel->dir.size = MIN_FILES;
1114 panel->active = 0;
1115 panel->filter = 0;
1116 panel->split = 0;
1117 panel->top_file = 0;
1118 panel->selected = 0;
1119 panel->marked = 0;
1120 panel->total = 0;
1121 panel->reverse = 0;
1122 panel->dirty = 1;
1123 panel->searching = 0;
1124 panel->dirs_marked = 0;
1125 panel->is_panelized = 0;
1126 panel->format = 0;
1127 panel->status_format = 0;
1128 panel->format_modified = 1;
1130 panel->panel_name = g_strdup (panel_name);
1131 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
1133 for (i = 0; i < LIST_TYPES; i++)
1134 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
1136 panel->search_buffer[0] = 0;
1137 panel->frame_size = frame_half;
1138 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1139 if (!mc_config_has_group (mc_main_config, section)) {
1140 g_free (section);
1141 section = g_strdup (panel->panel_name);
1143 panel_load_setup (panel, section);
1144 g_free (section);
1146 /* Load format strings */
1147 err = set_panel_formats (panel);
1148 if (err) {
1149 set_panel_formats (panel);
1153 /* Because do_load_dir lists files in current directory */
1154 if (wpath)
1155 mc_chdir(wpath);
1157 /* Load the default format */
1158 panel->count =
1159 do_load_dir (panel->cwd, &panel->dir, panel->sort_type,
1160 panel->reverse, panel->case_sensitive,
1161 panel->exec_first, panel->filter);
1163 /* Restore old right path */
1164 if (wpath)
1165 mc_chdir(curdir);
1167 return panel;
1170 void
1171 panel_reload (WPanel *panel)
1173 struct stat current_stat;
1175 if (fast_reload && !stat (panel->cwd, &current_stat)
1176 && current_stat.st_ctime == panel->dir_stat.st_ctime
1177 && current_stat.st_mtime == panel->dir_stat.st_mtime)
1178 return;
1180 while (mc_chdir (panel->cwd) == -1) {
1181 char *last_slash;
1183 if (panel->cwd[0] == PATH_SEP && panel->cwd[1] == 0) {
1184 panel_clean_dir (panel);
1185 panel->count = set_zero_dir (&panel->dir);
1186 return;
1188 last_slash = strrchr (panel->cwd, PATH_SEP);
1189 if (!last_slash || last_slash == panel->cwd)
1190 strcpy (panel->cwd, PATH_SEP_STR);
1191 else
1192 *last_slash = 0;
1193 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
1194 show_dir (panel);
1197 panel->count =
1198 do_reload_dir (panel->cwd, &panel->dir, panel->sort_type,
1199 panel->count, panel->reverse, panel->case_sensitive,
1200 panel->exec_first, panel->filter);
1202 panel->dirty = 1;
1203 if (panel->selected >= panel->count)
1204 do_select (panel, panel->count - 1);
1206 recalculate_panel_summary (panel);
1209 static void
1210 paint_frame (WPanel *panel)
1212 int side, width;
1213 char *txt = NULL;
1215 if (!panel->split)
1216 adjust_top_file (panel);
1218 widget_erase (&panel->widget);
1219 show_dir (panel);
1221 widget_move (&panel->widget, 1, 1);
1223 for (side = 0; side <= panel->split; side++){
1224 format_e *format;
1226 if (side){
1227 tty_setcolor (NORMAL_COLOR);
1228 tty_print_one_vline ();
1229 width = panel->widget.cols - panel->widget.cols/2 - 1;
1230 } else if (panel->split)
1231 width = panel->widget.cols/2 - 3;
1232 else
1233 width = panel->widget.cols - 2;
1235 for (format = panel->format; format; format = format->next){
1236 if (format->string_fn){
1237 if (panel->filter && !strcmp (format->id, "name")) {
1238 txt = g_strdup_printf ("%s [%s]", format->title, panel->filter);
1239 } else {
1240 txt = g_strdup (format->title);
1243 tty_setcolor (MARKED_COLOR);
1244 tty_print_string (str_fit_to_term (format->title, format->field_len,
1245 J_CENTER_LEFT));
1246 g_free(txt);
1247 width -= format->field_len;
1248 } else {
1249 tty_setcolor (NORMAL_COLOR);
1250 tty_print_one_vline ();
1251 width--;
1255 if (width > 0)
1256 tty_draw_hline (-1, -1, ' ', width);
1260 static const char *
1261 parse_panel_size (WPanel *panel, const char *format, int isstatus)
1263 int frame = frame_half;
1264 format = skip_separators (format);
1266 if (!strncmp (format, "full", 4)){
1267 frame = frame_full;
1268 format += 4;
1269 } else if (!strncmp (format, "half", 4)){
1270 frame = frame_half;
1271 format += 4;
1274 if (!isstatus){
1275 panel->frame_size = frame;
1276 panel->split = 0;
1279 /* Now, the optional column specifier */
1280 format = skip_separators (format);
1282 if (*format == '1' || *format == '2'){
1283 if (!isstatus)
1284 panel->split = *format == '2';
1285 format++;
1288 if (!isstatus)
1289 panel_update_cols (&(panel->widget), panel->frame_size);
1291 return skip_separators (format);
1294 /* Format is:
1296 all := panel_format? format
1297 panel_format := [full|half] [1|2]
1298 format := one_format_e
1299 | format , one_format_e
1301 one_format_e := just format.id [opt_size]
1302 just := [<=>]
1303 opt_size := : size [opt_expand]
1304 size := [0-9]+
1305 opt_expand := +
1309 static format_e *
1310 parse_display_format (WPanel *panel, const char *format, char **error, int isstatus, int *res_total_cols)
1312 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1313 int total_cols = 0; /* Used columns by the format */
1314 int set_justify; /* flag: set justification mode? */
1315 align_crt_t justify = J_LEFT; /* Which mode. */
1316 int items = 0; /* Number of items in the format */
1317 size_t i;
1319 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1321 *error = 0;
1323 if (i18n_timelength == 0) {
1324 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1326 for (i = 0; i < ELEMENTS(formats); i++)
1327 if (strcmp ("time", formats [i].id+1) == 0)
1328 formats [i].min_size = i18n_timelength;
1332 * This makes sure that the panel and mini status full/half mode
1333 * setting is equal
1335 format = parse_panel_size (panel, format, isstatus);
1337 while (*format){ /* format can be an empty string */
1338 int found = 0;
1340 darr = g_new (format_e, 1);
1342 /* I'm so ugly, don't look at me :-) */
1343 if (!home)
1344 home = old = darr;
1346 old->next = darr;
1347 darr->next = 0;
1348 old = darr;
1350 format = skip_separators (format);
1352 if (strchr ("<=>", *format)){
1353 set_justify = 1;
1354 switch (*format)
1356 case '<':
1357 justify = J_LEFT;
1358 break;
1359 case '=':
1360 justify = J_CENTER;
1361 break;
1362 case '>':
1363 default:
1364 justify = J_RIGHT;
1365 break;
1367 format = skip_separators (format+1);
1368 } else
1369 set_justify = 0;
1371 for (i = 0; i < ELEMENTS(formats); i++){
1372 size_t klen = strlen (formats [i].id);
1374 if (strncmp (format, formats [i].id, klen) != 0)
1375 continue;
1377 format += klen;
1379 if (formats [i].use_in_gui)
1380 items++;
1382 darr->requested_field_len = formats [i].min_size;
1383 darr->string_fn = formats [i].string_fn;
1384 if (formats [i].title [0])
1385 darr->title = _(formats [i].title);
1386 else
1387 darr->title = "";
1388 darr->id = formats [i].id;
1389 darr->expand = formats [i].expands;
1390 darr->just_mode = formats [i].default_just;
1392 if (set_justify) {
1393 if (IS_FIT(darr->just_mode))
1394 darr->just_mode = MAKE_FIT(justify);
1395 else
1396 darr->just_mode = justify;
1398 found = 1;
1400 format = skip_separators (format);
1402 /* If we have a size specifier */
1403 if (*format == ':'){
1404 int req_length;
1406 /* If the size was specified, we don't want
1407 * auto-expansion by default
1409 darr->expand = 0;
1410 format++;
1411 req_length = atoi (format);
1412 darr->requested_field_len = req_length;
1414 format = skip_numbers (format);
1416 /* Now, if they insist on expansion */
1417 if (*format == '+'){
1418 darr->expand = 1;
1419 format++;
1424 break;
1426 if (!found){
1427 char *tmp_format = g_strdup (format);
1429 int pos = min (8, strlen (format));
1430 delete_format (home);
1431 tmp_format [pos] = 0;
1432 *error = g_strconcat (_("Unknown tag on display format: "), tmp_format, (char *) NULL);
1433 g_free (tmp_format);
1434 return 0;
1436 total_cols += darr->requested_field_len;
1439 *res_total_cols = total_cols;
1440 return home;
1443 static format_e *
1444 use_display_format (WPanel *panel, const char *format, char **error, int isstatus)
1446 #define MAX_EXPAND 4
1447 int expand_top = 0; /* Max used element in expand */
1448 int usable_columns; /* Usable columns in the panel */
1449 int total_cols = 0;
1450 int i;
1451 format_e *darr, *home;
1453 if (!format)
1454 format = DEFAULT_USER_FORMAT;
1456 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1458 if (*error)
1459 return 0;
1461 panel->dirty = 1;
1463 /* Status needn't to be split */
1464 usable_columns = ((panel->widget.cols-2)/((isstatus)
1466 : (panel->split+1))) - (!isstatus && panel->split);
1468 /* Look for the expandable fields and set field_len based on the requested field len */
1469 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next){
1470 darr->field_len = darr->requested_field_len;
1471 if (darr->expand)
1472 expand_top++;
1475 /* If we used more columns than the available columns, adjust that */
1476 if (total_cols > usable_columns){
1477 int pdif, dif = total_cols - usable_columns;
1479 while (dif){
1480 pdif = dif;
1481 for (darr = home; darr; darr = darr->next){
1482 if (dif && darr->field_len - 1){
1483 darr->field_len--;
1484 dif--;
1488 /* avoid endless loop if num fields > 40 */
1489 if (pdif == dif)
1490 break;
1492 total_cols = usable_columns; /* give up, the rest should be truncated */
1495 /* Expand the available space */
1496 if ((usable_columns > total_cols) && expand_top){
1497 int spaces = (usable_columns - total_cols) / expand_top;
1498 int extra = (usable_columns - total_cols) % expand_top;
1500 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1501 if (darr->expand){
1502 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1503 i++;
1506 return home;
1509 /* Switches the panel to the mode specified in the format */
1510 /* Seting up both format and status string. Return: 0 - on success; */
1511 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
1513 set_panel_formats (WPanel *p)
1515 format_e *form;
1516 char *err;
1517 int retcode = 0;
1519 form = use_display_format (p, panel_format (p), &err, 0);
1521 if (err){
1522 g_free (err);
1523 retcode = 1;
1525 else {
1526 if (p->format)
1527 delete_format (p->format);
1529 p->format = form;
1532 if (show_mini_info){
1534 form = use_display_format (p, mini_status_format (p), &err, 1);
1536 if (err){
1537 g_free (err);
1538 retcode += 2;
1540 else {
1541 if (p->status_format)
1542 delete_format (p->status_format);
1544 p->status_format = form;
1548 panel_format_modified (p);
1549 panel_update_cols (&(p->widget), p->frame_size);
1551 if (retcode)
1552 message (D_ERROR, _("Warning" ), _( "User supplied format looks invalid, reverting to default." ) );
1553 if (retcode & 0x01){
1554 g_free (p->user_format);
1555 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
1557 if (retcode & 0x02){
1558 g_free (p->user_status_format [p->list_type]);
1559 p->user_status_format [p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
1562 return retcode;
1565 /* Given the panel->view_type returns the format string to be parsed */
1566 static const char *
1567 panel_format (WPanel *panel)
1569 switch (panel->list_type){
1571 case list_long:
1572 return "full perm space nlink space owner space group space size space mtime space name";
1574 case list_brief:
1575 return "half 2 type name";
1577 case list_user:
1578 return panel->user_format;
1580 default:
1581 case list_full:
1582 return "half type name | size | mtime";
1586 static const char *
1587 mini_status_format (WPanel *panel)
1589 if (panel->user_mini_status)
1590 return panel->user_status_format [panel->list_type];
1592 switch (panel->list_type){
1594 case list_long:
1595 return "full perm space nlink space owner space group space size space mtime space name";
1597 case list_brief:
1598 return "half type name space bsize space perm space";
1600 case list_full:
1601 return "half type name";
1603 default:
1604 case list_user:
1605 return panel->user_format;
1609 /* */
1610 /* Panel operation commands */
1611 /* */
1613 /* Used to emulate Lynx's entering leaving a directory with the arrow keys */
1614 static cb_ret_t
1615 maybe_cd (int move_up_dir)
1617 if (navigate_with_arrows) {
1618 if (!cmdline->buffer[0]) {
1619 if (move_up_dir) {
1620 do_cd ("..", cd_exact);
1621 return MSG_HANDLED;
1623 if (S_ISDIR (selection (current_panel)->st.st_mode)
1624 || link_isdir (selection (current_panel))) {
1625 do_cd (selection (current_panel)->fname, cd_exact);
1626 return MSG_HANDLED;
1630 return MSG_NOT_HANDLED;
1633 /* Returns the number of items in the given panel */
1634 static int
1635 ITEMS (WPanel *p)
1637 if (p->split)
1638 return llines (p) * 2;
1639 else
1640 return llines (p);
1643 /* Select current item and readjust the panel */
1644 void
1645 select_item (WPanel *panel)
1647 int items = ITEMS (panel);
1649 /* Although currently all over the code we set the selection and
1650 top file to decent values before calling select_item, I could
1651 forget it someday, so it's better to do the actual fitting here */
1653 if (panel->top_file < 0)
1654 panel->top_file = 0;
1656 if (panel->selected < 0)
1657 panel->selected = 0;
1659 if (panel->selected > panel->count - 1)
1660 panel->selected = panel->count - 1;
1662 if (panel->top_file > panel->count - 1)
1663 panel->top_file = panel->count - 1;
1665 if ((panel->count - panel->top_file) < items) {
1666 panel->top_file = panel->count - items;
1667 if (panel->top_file < 0)
1668 panel->top_file = 0;
1671 if (panel->selected < panel->top_file)
1672 panel->top_file = panel->selected;
1674 if ((panel->selected - panel->top_file) >= items)
1675 panel->top_file = panel->selected - items + 1;
1677 panel->dirty = 1;
1679 execute_hooks (select_file_hook);
1682 /* Clears all files in the panel, used only when one file was marked */
1683 void
1684 unmark_files (WPanel *panel)
1686 int i;
1688 if (!panel->marked)
1689 return;
1690 for (i = 0; i < panel->count; i++)
1691 file_mark (panel, i, 0);
1693 panel->dirs_marked = 0;
1694 panel->marked = 0;
1695 panel->total = 0;
1698 static void
1699 unselect_item (WPanel *panel)
1701 repaint_file (panel, panel->selected, 1, 2*selection (panel)->f.marked, 0);
1704 static void
1705 move_down (WPanel *panel)
1707 if (panel->selected+1 == panel->count)
1708 return;
1710 unselect_item (panel);
1711 panel->selected++;
1712 if (panel->selected - panel->top_file == ITEMS (panel) &&
1713 panel_scroll_pages) {
1714 /* Scroll window half screen */
1715 panel->top_file += ITEMS (panel)/2;
1716 if (panel->top_file > panel->count - ITEMS (panel))
1717 panel->top_file = panel->count - ITEMS (panel);
1718 paint_dir (panel);
1720 select_item (panel);
1723 static void
1724 move_up (WPanel *panel)
1726 if (panel->selected == 0)
1727 return;
1729 unselect_item (panel);
1730 panel->selected--;
1731 if (panel->selected < panel->top_file && panel_scroll_pages) {
1732 /* Scroll window half screen */
1733 panel->top_file -= ITEMS (panel)/2;
1734 if (panel->top_file < 0)
1735 panel->top_file = 0;
1736 paint_dir (panel);
1738 select_item (panel);
1741 /* Changes the selection by lines (may be negative) */
1742 static void
1743 move_selection (WPanel *panel, int lines)
1745 int new_pos;
1746 int adjust = 0;
1748 new_pos = panel->selected + lines;
1749 if (new_pos >= panel->count)
1750 new_pos = panel->count-1;
1752 if (new_pos < 0)
1753 new_pos = 0;
1755 unselect_item (panel);
1756 panel->selected = new_pos;
1758 if (panel->selected - panel->top_file >= ITEMS (panel)){
1759 panel->top_file += lines;
1760 adjust = 1;
1763 if (panel->selected - panel->top_file < 0){
1764 panel->top_file += lines;
1765 adjust = 1;
1768 if (adjust){
1769 if (panel->top_file > panel->selected)
1770 panel->top_file = panel->selected;
1771 if (panel->top_file < 0)
1772 panel->top_file = 0;
1773 paint_dir (panel);
1775 select_item (panel);
1778 static cb_ret_t
1779 move_left (WPanel *panel, int c_code)
1781 (void) c_code;
1782 if (panel->split) {
1783 move_selection (panel, -llines (panel));
1784 return MSG_HANDLED;
1785 } else
1786 return maybe_cd (1); /* cd .. */
1789 static int
1790 move_right (WPanel *panel, int c_code)
1792 (void) c_code;
1793 if (panel->split) {
1794 move_selection (panel, llines (panel));
1795 return MSG_HANDLED;
1796 } else
1797 return maybe_cd (0); /* cd (selection) */
1800 static void
1801 prev_page (WPanel *panel)
1803 int items;
1805 if (!panel->selected && !panel->top_file)
1806 return;
1807 unselect_item (panel);
1808 items = ITEMS (panel);
1809 if (panel->top_file < items)
1810 items = panel->top_file;
1811 if (!items)
1812 panel->selected = 0;
1813 else
1814 panel->selected -= items;
1815 panel->top_file -= items;
1817 /* This keeps the selection in a reasonable place */
1818 if (panel->selected < 0)
1819 panel->selected = 0;
1820 if (panel->top_file < 0)
1821 panel->top_file = 0;
1822 select_item (panel);
1823 paint_dir (panel);
1826 static void
1827 ctrl_prev_page (WPanel *panel)
1829 (void) panel;
1830 do_cd ("..", cd_exact);
1833 static void
1834 next_page (WPanel *panel)
1836 int items;
1838 if (panel->selected == panel->count - 1)
1839 return;
1840 unselect_item (panel);
1841 items = ITEMS (panel);
1842 if (panel->top_file > panel->count - 2 * items)
1843 items = panel->count - items - panel->top_file;
1844 if (panel->top_file + items < 0)
1845 items = -panel->top_file;
1846 if (!items)
1847 panel->selected = panel->count - 1;
1848 else
1849 panel->selected += items;
1850 panel->top_file += items;
1852 /* This keeps the selection in it's relative position */
1853 if (panel->selected >= panel->count)
1854 panel->selected = panel->count - 1;
1855 if (panel->top_file >= panel->count)
1856 panel->top_file = panel->count - 1;
1857 select_item (panel);
1858 paint_dir (panel);
1861 static void
1862 ctrl_next_page (WPanel *panel)
1864 if ((S_ISDIR (selection (panel)->st.st_mode)
1865 || link_isdir (selection (panel)))) {
1866 do_cd (selection (panel)->fname, cd_exact);
1870 static void
1871 goto_top_file (WPanel *panel)
1873 unselect_item (panel);
1874 panel->selected = panel->top_file;
1875 select_item (panel);
1878 static void
1879 goto_middle_file (WPanel *panel)
1881 unselect_item (panel);
1882 panel->selected = panel->top_file + (ITEMS (panel)/2);
1883 if (panel->selected >= panel->count)
1884 panel->selected = panel->count - 1;
1885 select_item (panel);
1888 static void
1889 goto_bottom_file (WPanel *panel)
1891 unselect_item (panel);
1892 panel->selected = panel->top_file + ITEMS (panel)-1;
1893 if (panel->selected >= panel->count)
1894 panel->selected = panel->count - 1;
1895 select_item (panel);
1898 static void
1899 move_home (WPanel *panel)
1901 if (panel->selected == 0)
1902 return;
1903 unselect_item (panel);
1905 if (torben_fj_mode){
1906 int middle_pos = panel->top_file + (ITEMS (panel)/2);
1908 if (panel->selected > middle_pos){
1909 goto_middle_file (panel);
1910 return;
1912 if (panel->selected != panel->top_file){
1913 goto_top_file (panel);
1914 return;
1918 panel->top_file = 0;
1919 panel->selected = 0;
1921 paint_dir (panel);
1922 select_item (panel);
1925 static void
1926 move_end (WPanel *panel)
1928 if (panel->selected == panel->count-1)
1929 return;
1930 unselect_item (panel);
1931 if (torben_fj_mode){
1932 int middle_pos = panel->top_file + (ITEMS (panel)/2);
1934 if (panel->selected < middle_pos){
1935 goto_middle_file (panel);
1936 return;
1938 if (panel->selected != (panel->top_file + ITEMS(panel)-1)){
1939 goto_bottom_file (panel);
1940 return;
1944 panel->selected = panel->count-1;
1945 paint_dir (panel);
1946 select_item (panel);
1949 /* Recalculate the panels summary information, used e.g. when marked
1950 files might have been removed by an external command */
1951 void
1952 recalculate_panel_summary (WPanel *panel)
1954 int i;
1956 panel->marked = 0;
1957 panel->dirs_marked = 0;
1958 panel->total = 0;
1960 for (i = 0; i < panel->count; i++)
1961 if (panel->dir.list [i].f.marked){
1962 /* do_file_mark will return immediately if newmark == oldmark.
1963 So we have to first unmark it to get panel's summary information
1964 updated. (Norbert) */
1965 panel->dir.list [i].f.marked = 0;
1966 do_file_mark (panel, i, 1);
1970 /* This routine marks a file or a directory */
1971 void
1972 do_file_mark (WPanel *panel, int idx, int mark)
1974 if (panel->dir.list[idx].f.marked == mark)
1975 return;
1977 /* Only '..' can't be marked, '.' isn't visible */
1978 if (!strcmp (panel->dir.list[idx].fname, ".."))
1979 return;
1981 file_mark (panel, idx, mark);
1982 if (panel->dir.list[idx].f.marked) {
1983 panel->marked++;
1984 if (S_ISDIR (panel->dir.list[idx].st.st_mode)) {
1985 if (panel->dir.list[idx].f.dir_size_computed)
1986 panel->total += panel->dir.list[idx].st.st_size;
1987 panel->dirs_marked++;
1988 } else
1989 panel->total += panel->dir.list[idx].st.st_size;
1990 set_colors (panel);
1991 } else {
1992 if (S_ISDIR (panel->dir.list[idx].st.st_mode)) {
1993 if (panel->dir.list[idx].f.dir_size_computed)
1994 panel->total -= panel->dir.list[idx].st.st_size;
1995 panel->dirs_marked--;
1996 } else
1997 panel->total -= panel->dir.list[idx].st.st_size;
1998 panel->marked--;
2002 static void
2003 do_mark_file (WPanel *panel, int do_move)
2005 do_file_mark (panel, panel->selected,
2006 selection (panel)->f.marked ? 0 : 1);
2007 if (mark_moves_down && do_move)
2008 move_down (panel);
2011 static void
2012 mark_file (WPanel *panel)
2014 do_mark_file (panel, 1);
2017 /* Incremental search of a file name in the panel */
2018 static void
2019 do_search (WPanel *panel, int c_code)
2021 size_t l, max, buf_max;
2022 int i, sel;
2023 int wrapped = 0;
2024 char *act;
2026 l = strlen (panel->search_buffer);
2027 if (c_code == KEY_BACKSPACE) {
2028 if (l != 0) {
2029 act = panel->search_buffer + l;
2030 str_prev_noncomb_char (&act, panel->search_buffer);
2031 act[0] = '\0';
2033 panel->search_chpoint = 0;
2034 } else {
2035 if (c_code && (gsize) panel->search_chpoint < sizeof (panel->search_char)) {
2036 panel->search_char[panel->search_chpoint] = c_code;
2037 panel->search_chpoint++;
2040 if (panel->search_chpoint > 0) {
2041 switch (str_is_valid_char (panel->search_char, panel->search_chpoint)) {
2042 case -2:
2043 return;
2044 case -1:
2045 panel->search_chpoint = 0;
2046 return;
2047 default:
2048 if (l + panel->search_chpoint < sizeof (panel->search_buffer)) {
2049 memcpy (panel->search_buffer + l, panel->search_char,
2050 panel->search_chpoint);
2051 l+= panel->search_chpoint;
2052 (panel->search_buffer + l)[0] = '\0';
2053 panel->search_chpoint = 0;
2059 buf_max = panel->case_sensitive ?
2060 str_prefix (panel->search_buffer, panel->search_buffer) :
2061 str_caseprefix (panel->search_buffer, panel->search_buffer);
2062 max = 0;
2063 sel = panel->selected;
2064 for (i = panel->selected; !wrapped || i != panel->selected; i++) {
2065 if (i >= panel->count) {
2066 i = 0;
2067 if (wrapped)
2068 break;
2069 wrapped = 1;
2071 l = panel->case_sensitive ?
2072 str_prefix (panel->dir.list[i].fname, panel->search_buffer) :
2073 str_caseprefix (panel->dir.list[i].fname, panel->search_buffer);
2074 if (l > max) {
2075 max = l;
2076 sel = i;
2077 if (max == buf_max) break;
2081 unselect_item (panel);
2082 panel->selected = sel;
2083 select_item (panel);
2085 act = panel->search_buffer + strlen (panel->search_buffer);
2086 while (max < buf_max) {
2087 str_prev_char_safe (&act);
2088 act[0] = '\0';
2089 buf_max = panel->case_sensitive ?
2090 str_prefix (panel->search_buffer, panel->search_buffer) :
2091 str_caseprefix (panel->search_buffer, panel->search_buffer);
2094 paint_panel (panel);
2097 static void
2098 start_search (WPanel *panel)
2100 if (panel->searching){
2101 if (panel->selected+1 == panel->count)
2102 panel->selected = 0;
2103 else
2104 move_down (panel);
2105 do_search (panel, 0);
2106 } else {
2107 panel->searching = 1;
2108 panel->search_buffer[0] = '\0';
2109 panel->search_char[0] = '\0';
2110 panel->search_chpoint = 0;
2111 display_mini_info (panel);
2112 mc_refresh ();
2116 /* Return 1 if the Enter key has been processed, 0 otherwise */
2117 static int
2118 do_enter_on_file_entry (file_entry *fe)
2120 char *full_name;
2123 * Directory or link to directory - change directory.
2124 * Try the same for the entries on which mc_lstat() has failed.
2126 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe)
2127 || (fe->st.st_mode == 0)) {
2128 if (!do_cd (fe->fname, cd_exact))
2129 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2130 return 1;
2133 /* Try associated command */
2134 if (regex_command (fe->fname, "Open", 0) != 0)
2135 return 1;
2137 /* Check if the file is executable */
2138 full_name = concat_dir_and_file (current_panel->cwd, fe->fname);
2139 if (!is_exe (fe->st.st_mode) || !if_link_is_exe (full_name, fe)) {
2140 g_free (full_name);
2141 return 0;
2143 g_free (full_name);
2145 if (confirm_execute) {
2146 if (query_dialog
2147 (_(" The Midnight Commander "),
2148 _(" Do you really want to execute? "), D_NORMAL, 2, _("&Yes"),
2149 _("&No")) != 0)
2150 return 1;
2152 #ifdef USE_VFS
2153 if (!vfs_current_is_local ()) {
2154 char *tmp;
2155 int ret;
2157 tmp = concat_dir_and_file (vfs_get_current_dir (), fe->fname);
2158 ret = mc_setctl (tmp, VFS_SETCTL_RUN, NULL);
2159 g_free (tmp);
2160 /* We took action only if the dialog was shown or the execution
2161 * was successful */
2162 return confirm_execute || (ret == 0);
2164 #endif
2167 char *tmp = name_quote (fe->fname, 0);
2168 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2169 g_free (tmp);
2170 shell_execute (cmd, 0);
2171 g_free (cmd);
2174 return 1;
2177 static int
2178 do_enter (WPanel *panel)
2180 return do_enter_on_file_entry (selection (panel));
2183 static void
2184 chdir_other_panel (WPanel *panel)
2186 char *new_dir;
2187 char *sel_entry = NULL;
2189 if (get_other_type () != view_listing) {
2190 set_display_type (get_other_index (), view_listing);
2193 if (!S_ISDIR (panel->dir.list [panel->selected].st.st_mode)) {
2194 new_dir = concat_dir_and_file (panel->cwd, "..");
2195 sel_entry = strrchr(panel->cwd, PATH_SEP);
2196 } else
2197 new_dir = concat_dir_and_file (panel->cwd, panel->dir.list [panel->selected].fname);
2199 change_panel ();
2200 do_cd (new_dir, cd_exact);
2201 if (sel_entry)
2202 try_to_select (current_panel, sel_entry);
2203 change_panel ();
2205 move_down (panel);
2207 g_free (new_dir);
2211 * Make the current directory of the current panel also the current
2212 * directory of the other panel. Put the other panel to the listing
2213 * mode if needed. If the current panel is panelized, the other panel
2214 * doesn't become panelized.
2216 static void
2217 sync_other_panel (WPanel *panel)
2219 if (get_other_type () != view_listing) {
2220 set_display_type (get_other_index (), view_listing);
2223 do_panel_cd (other_panel, current_panel->cwd, cd_exact);
2225 /* try to select current filename on the other panel */
2226 if (!panel->is_panelized) {
2227 try_to_select (other_panel, selection (panel)->fname);
2231 static void
2232 chdir_to_readlink (WPanel *panel)
2234 char *new_dir;
2236 if (get_other_type () != view_listing)
2237 return;
2239 if (S_ISLNK (panel->dir.list [panel->selected].st.st_mode)) {
2240 char buffer [MC_MAXPATHLEN], *p;
2241 int i;
2242 struct stat st;
2244 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2245 if (i < 0)
2246 return;
2247 if (mc_stat (selection (panel)->fname, &st) < 0)
2248 return;
2249 buffer [i] = 0;
2250 if (!S_ISDIR (st.st_mode)) {
2251 p = strrchr (buffer, PATH_SEP);
2252 if (p && !p[1]) {
2253 *p = 0;
2254 p = strrchr (buffer, PATH_SEP);
2256 if (!p)
2257 return;
2258 p[1] = 0;
2260 if (*buffer == PATH_SEP)
2261 new_dir = g_strdup (buffer);
2262 else
2263 new_dir = concat_dir_and_file (panel->cwd, buffer);
2265 change_panel ();
2266 do_cd (new_dir, cd_exact);
2267 change_panel ();
2269 move_down (panel);
2271 g_free (new_dir);
2275 typedef void (*panel_key_callback) (WPanel *);
2276 typedef struct {
2277 int key_code;
2278 panel_key_callback fn;
2279 } panel_key_map;
2281 static void cmd_do_enter(WPanel *wp) { (void) do_enter(wp); }
2282 static void cmd_view_simple(WPanel *wp) { (void) wp; view_simple_cmd(); }
2283 static void cmd_edit_new(WPanel *wp) { (void) wp; edit_cmd_new(); }
2284 static void cmd_copy_local(WPanel *wp) { (void) wp;copy_cmd_local(); }
2285 static void cmd_rename_local(WPanel *wp) { (void) wp;ren_cmd_local(); }
2286 static void cmd_delete_local(WPanel *wp) { (void) wp;delete_cmd_local(); }
2287 static void cmd_select(WPanel *wp) { (void) wp;select_cmd(); }
2288 static void cmd_unselect(WPanel *wp) { (void) wp;unselect_cmd(); }
2289 static void cmd_reverse_selection(WPanel *wp) { (void) wp;reverse_selection_cmd(); }
2291 static const panel_key_map panel_keymap [] = {
2292 { KEY_DOWN, move_down },
2293 { KEY_UP, move_up },
2295 /* The action button :-) */
2296 { '\n', cmd_do_enter },
2297 { KEY_ENTER, cmd_do_enter },
2299 { KEY_IC, mark_file },
2300 { KEY_HOME, move_home },
2301 { KEY_A1, move_home },
2302 { ALT ('<'), move_home },
2303 { KEY_C1, move_end },
2304 { KEY_END, move_end },
2305 { ALT ('>'), move_end },
2306 { KEY_NPAGE, next_page },
2307 { KEY_PPAGE, prev_page },
2308 { KEY_NPAGE | KEY_M_CTRL, ctrl_next_page },
2309 { KEY_PPAGE | KEY_M_CTRL, ctrl_prev_page },
2311 /* To quickly move in the panel */
2312 { ALT('g'), goto_top_file },
2313 { ALT('r'), goto_middle_file }, /* M-r like emacs */
2314 { ALT('j'), goto_bottom_file },
2316 /* Emacs-like bindings */
2317 { XCTRL('v'), next_page }, /* C-v like emacs */
2318 { ALT('v'), prev_page }, /* M-v like emacs */
2319 { XCTRL('p'), move_up }, /* C-p like emacs */
2320 { XCTRL('n'), move_down }, /* C-n like emacs */
2321 { XCTRL('s'), start_search }, /* C-s like emacs */
2322 { ALT('s'), start_search }, /* M-s not like emacs */
2323 /* { XCTRL('t'), mark_file },*/
2324 { XCTRL('t'), set_panel_encoding },
2325 { ALT('o'), chdir_other_panel },
2326 { ALT('i'), sync_other_panel },
2327 { ALT('l'), chdir_to_readlink },
2328 { ALT('H'), directory_history_list },
2329 { KEY_F(13), cmd_view_simple },
2330 { KEY_F(14), cmd_edit_new },
2331 { KEY_F(15), cmd_copy_local },
2332 { KEY_F(16), cmd_rename_local },
2333 { KEY_F(18), cmd_delete_local },
2334 { ALT('y'), directory_history_prev },
2335 { ALT('u'), directory_history_next },
2336 { ALT('+'), cmd_select },
2337 { KEY_KP_ADD, cmd_select },
2338 { ALT('\\'), cmd_unselect },
2339 { ALT('-'), cmd_unselect },
2340 { KEY_KP_SUBTRACT, cmd_unselect },
2341 { ALT('*'), cmd_reverse_selection },
2342 { KEY_KP_MULTIPLY, cmd_reverse_selection },
2343 { 0, 0 }
2346 static cb_ret_t
2347 panel_key (WPanel *panel, int key)
2349 int i;
2351 for (i = 0; panel_keymap[i].key_code; i++) {
2352 if (key == panel_keymap[i].key_code) {
2353 int old_searching = panel->searching;
2355 if (panel_keymap[i].fn != start_search)
2356 panel->searching = 0;
2358 (*panel_keymap[i].fn) (panel);
2360 if (panel->searching != old_searching)
2361 display_mini_info (panel);
2362 return MSG_HANDLED;
2365 if (torben_fj_mode && key == ALT ('h')) {
2366 goto_middle_file (panel);
2367 return MSG_HANDLED;
2370 /* We do not want to take a key press if nothing can be done with it */
2371 /* The command line widget may do something more useful */
2372 if (key == KEY_LEFT)
2373 return move_left (panel, key);
2375 if (key == KEY_RIGHT)
2376 return move_right (panel, key);
2378 if (is_abort_char (key)) {
2379 panel->searching = 0;
2380 display_mini_info (panel);
2381 return MSG_HANDLED;
2384 /* Do not eat characters not meant for the panel below ' ' (e.g. C-l). */
2385 if ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE) {
2386 if (panel->searching) {
2387 do_search (panel, key);
2388 return MSG_HANDLED;
2391 if (!command_prompt) {
2392 start_search (panel);
2393 do_search (panel, key);
2394 return MSG_HANDLED;
2398 return MSG_NOT_HANDLED;
2401 static cb_ret_t
2402 panel_callback (Widget *w, widget_msg_t msg, int parm)
2404 WPanel *panel = (WPanel *) w;
2405 Dlg_head *h = panel->widget.parent;
2407 switch (msg) {
2408 case WIDGET_DRAW:
2409 paint_panel (panel);
2410 return MSG_HANDLED;
2412 case WIDGET_FOCUS:
2413 current_panel = panel;
2414 panel->active = 1;
2415 if (mc_chdir (panel->cwd) != 0) {
2416 char *cwd = strip_password (g_strdup (panel->cwd), 1);
2417 message (D_ERROR, MSG_ERROR, _(" Cannot chdir to \"%s\" \n %s "),
2418 cwd, unix_error_string (errno));
2419 g_free(cwd);
2420 } else
2421 subshell_chdir (panel->cwd);
2423 update_xterm_title_path ();
2424 select_item (panel);
2425 show_dir (panel);
2426 paint_dir (panel);
2427 panel->dirty = 0;
2429 buttonbar_set_label (h, 1, _("Help"), help_cmd);
2430 buttonbar_set_label (h, 2, _("Menu"), user_file_menu_cmd);
2431 buttonbar_set_label (h, 3, _("View"), view_cmd);
2432 buttonbar_set_label (h, 4, _("Edit"), edit_cmd);
2433 buttonbar_set_label (h, 5, _("Copy"), copy_cmd);
2434 buttonbar_set_label (h, 6, _("RenMov"), ren_cmd);
2435 buttonbar_set_label (h, 7, _("Mkdir"), mkdir_cmd);
2436 buttonbar_set_label (h, 8, _("Delete"), delete_cmd);
2437 buttonbar_redraw (h);
2438 return MSG_HANDLED;
2440 case WIDGET_UNFOCUS:
2441 /* Janne: look at this for the multiple panel options */
2442 if (panel->searching){
2443 panel->searching = 0;
2444 display_mini_info (panel);
2446 panel->active = 0;
2447 show_dir (panel);
2448 unselect_item (panel);
2449 return MSG_HANDLED;
2451 case WIDGET_KEY:
2452 return panel_key (panel, parm);
2454 case WIDGET_DESTROY:
2455 panel_destroy (panel);
2456 return MSG_HANDLED;
2458 default:
2459 return default_proc (msg, parm);
2463 void
2464 file_mark (WPanel *panel, int index, int val)
2466 if (panel->dir.list[index].f.marked != val) {
2467 panel->dir.list[index].f.marked = val;
2468 panel->dirty = 1;
2472 /* */
2473 /* Panel mouse events support routines */
2474 /* */
2475 static int mouse_marking = 0;
2477 static void
2478 mouse_toggle_mark (WPanel *panel)
2480 do_mark_file (panel, 0);
2481 mouse_marking = selection (panel)->f.marked;
2484 static void
2485 mouse_set_mark (WPanel *panel)
2487 if (mouse_marking && !(selection (panel)->f.marked))
2488 do_mark_file (panel, 0);
2489 else if (!mouse_marking && (selection (panel)->f.marked))
2490 do_mark_file (panel, 0);
2493 static int
2494 mark_if_marking (WPanel *panel, Gpm_Event *event)
2496 if (event->buttons & GPM_B_RIGHT){
2497 if (event->type & GPM_DOWN)
2498 mouse_toggle_mark (panel);
2499 else
2500 mouse_set_mark (panel);
2501 return 1;
2503 return 0;
2507 * Mouse callback of the panel minus repainting.
2508 * If the event is redirected to the menu, *redir is set to 1.
2510 static int
2511 do_panel_event (Gpm_Event *event, WPanel *panel, int *redir)
2513 const int lines = llines (panel);
2514 const gboolean is_active = dlg_widget_active (panel);
2515 const gboolean mouse_down = (event->type & GPM_DOWN) != 0;
2517 /* rest of the upper frame, the menu is invisible - call menu */
2518 if (mouse_down && event->y == 1 && !menubar_visible) {
2519 *redir = 1;
2520 event->x += panel->widget.x;
2521 return (*(the_menubar->widget.mouse)) (event, the_menubar);
2524 /* "<" button */
2525 if (mouse_down && event->y == 1 && event->x == 2) {
2526 directory_history_prev (panel);
2527 return MOU_NORMAL;
2530 /* ">" button */
2531 if (mouse_down && event->y == 1 && event->x == panel->widget.cols - 1) {
2532 directory_history_next (panel);
2533 return MOU_NORMAL;
2536 /* "v" button */
2537 if (mouse_down && event->y == 1 && event->x == panel->widget.cols - 2) {
2538 directory_history_list (panel);
2539 return MOU_NORMAL;
2542 /* Mouse wheel events */
2543 if (mouse_down && (event->buttons & GPM_B_UP)) {
2544 if (is_active) {
2545 if (panel->top_file > 0)
2546 prev_page (panel);
2547 else /* We are in first page */
2548 move_up (panel);
2550 return MOU_NORMAL;
2552 if (mouse_down && (event->buttons & GPM_B_DOWN)) {
2553 if (is_active) {
2554 if (panel->top_file + ITEMS (panel) < panel->count)
2555 next_page (panel);
2556 else /* We are in last page */
2557 move_down (panel);
2559 return MOU_NORMAL;
2562 event->y -= 2;
2563 if ((event->type & (GPM_DOWN | GPM_DRAG))) {
2564 int my_index;
2566 if (!is_active)
2567 change_panel ();
2569 if (event->y <= 0) {
2570 mark_if_marking (panel, event);
2571 if (mouse_move_pages)
2572 prev_page (panel);
2573 else
2574 move_up (panel);
2575 return MOU_REPEAT;
2578 if (!((panel->top_file + event->y <= panel->count) && event->y <= lines)) {
2579 mark_if_marking (panel, event);
2580 if (mouse_move_pages)
2581 next_page (panel);
2582 else
2583 move_down (panel);
2584 return MOU_REPEAT;
2587 my_index = panel->top_file + event->y - 1;
2588 if (panel->split && (event->x > ((panel->widget.cols - 2) / 2)))
2589 my_index += llines (panel);
2591 if (my_index >= panel->count)
2592 my_index = panel->count - 1;
2594 if (my_index != panel->selected) {
2595 unselect_item (panel);
2596 panel->selected = my_index;
2597 select_item (panel);
2600 /* This one is new */
2601 mark_if_marking (panel, event);
2602 } else if ((event->type & (GPM_UP | GPM_DOUBLE)) == (GPM_UP | GPM_DOUBLE)) {
2603 if (event->y > 0 && event->y <= lines)
2604 do_enter (panel);
2606 return MOU_NORMAL;
2609 /* Mouse callback of the panel */
2610 static int
2611 panel_event (Gpm_Event *event, void *data)
2613 WPanel *panel = data;
2614 int ret;
2615 int redir = MOU_NORMAL;
2617 ret = do_panel_event (event, panel, &redir);
2618 if (!redir)
2619 paint_panel (panel);
2621 return ret;
2624 void
2625 panel_re_sort (WPanel *panel)
2627 char *filename;
2628 int i;
2630 if (panel == NULL)
2631 return;
2633 filename = g_strdup (selection (panel)->fname);
2634 unselect_item (panel);
2635 do_sort (&panel->dir, panel->sort_type, panel->count-1, panel->reverse,
2636 panel->case_sensitive, panel->exec_first);
2637 panel->selected = -1;
2638 for (i = panel->count; i; i--){
2639 if (!strcmp (panel->dir.list [i-1].fname, filename)){
2640 panel->selected = i-1;
2641 break;
2644 g_free (filename);
2645 panel->top_file = panel->selected - ITEMS (panel)/2;
2646 if (panel->top_file < 0)
2647 panel->top_file = 0;
2648 select_item (panel);
2649 panel->dirty = 1;
2652 void
2653 panel_set_sort_order (WPanel *panel, sortfn *sort_order)
2655 if (sort_order == 0)
2656 return;
2658 panel->sort_type = sort_order;
2660 /* The directory is already sorted, we have to load the unsorted stuff */
2661 if (sort_order == (sortfn *) unsorted){
2662 char *current_file;
2664 current_file = g_strdup (panel->dir.list [panel->selected].fname);
2665 panel_reload (panel);
2666 try_to_select (panel, current_file);
2667 g_free (current_file);
2669 panel_re_sort (panel);
2672 void
2673 set_panel_encoding (WPanel *panel)
2675 const char *encoding = NULL;
2676 char *cd_path;
2677 #ifdef HAVE_CHARSET
2678 const char *errmsg;
2679 int offset;
2680 int r;
2682 if (horizontal_split) {
2683 offset = (get_current_index () != 0) ? panel->widget.lines : -panel->widget.lines;
2684 r = select_charset (0, offset, source_codepage, FALSE);
2685 } else {
2686 offset = (panel->widget.cols == COLS) ? 0
2687 : (get_current_index () != 0) ? panel->widget.cols
2688 : -panel->widget.cols;
2689 r = select_charset (offset, 0, source_codepage, FALSE);
2692 if (r == SELECT_CHARSET_CANCEL)
2693 return; /* Cancel */
2695 if (r == SELECT_CHARSET_NO_TRANSLATE) {
2696 /* No translation */
2697 errmsg = init_translation_table (display_codepage, display_codepage);
2698 cd_path = remove_encoding_from_path (panel->cwd);
2699 do_panel_cd (panel, cd_path, 0);
2700 g_free (cd_path);
2701 return;
2704 source_codepage = r;
2706 errmsg = init_translation_table (source_codepage, display_codepage);
2707 if (errmsg) {
2708 message (D_ERROR, MSG_ERROR, "%s", errmsg);
2709 return;
2712 encoding = get_codepage_id (source_codepage);
2713 #endif
2714 if (encoding != NULL) {
2715 cd_path = add_encoding_to_path (panel->cwd, encoding);
2716 if (!do_panel_cd (panel, cd_path, 0))
2717 message (D_ERROR, MSG_ERROR, _(" Cannot chdir to %s "), cd_path);
2718 g_free (cd_path);
2722 static void
2723 reload_panelized (WPanel *panel)
2725 int i, j;
2726 dir_list *list = &panel->dir;
2728 if (panel != current_panel)
2729 mc_chdir (panel->cwd);
2731 for (i = 0, j = 0; i < panel->count; i++) {
2732 if (list->list[i].f.marked) {
2733 /* Unmark the file in advance. In case the following mc_lstat
2734 * fails we are done, else we have to mark the file again
2735 * (Note: do_file_mark depends on a valid "list->list [i].buf").
2736 * IMO that's the best way to update the panel's summary status
2737 * -- Norbert
2739 do_file_mark (panel, i, 0);
2741 if (mc_lstat (list->list[i].fname, &list->list[i].st)) {
2742 g_free (list->list[i].fname);
2743 continue;
2745 if (list->list[i].f.marked)
2746 do_file_mark (panel, i, 1);
2747 if (j != i)
2748 list->list[j] = list->list[i];
2749 j++;
2751 if (j == 0)
2752 panel->count = set_zero_dir (list);
2753 else
2754 panel->count = j;
2756 if (panel != current_panel)
2757 mc_chdir (current_panel->cwd);
2760 static void
2761 update_one_panel_widget (WPanel *panel, int force_update,
2762 const char *current_file)
2764 int free_pointer;
2765 char *my_current_file = NULL;
2767 if (force_update & UP_RELOAD) {
2768 panel->is_panelized = 0;
2769 mc_setctl (panel->cwd, VFS_SETCTL_FLUSH, 0);
2770 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
2773 /* If current_file == -1 (an invalid pointer) then preserve selection */
2774 if (current_file == UP_KEEPSEL) {
2775 free_pointer = 1;
2776 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
2777 current_file = my_current_file;
2778 } else
2779 free_pointer = 0;
2781 if (panel->is_panelized)
2782 reload_panelized (panel);
2783 else
2784 panel_reload (panel);
2786 try_to_select (panel, current_file);
2787 panel->dirty = 1;
2789 if (free_pointer)
2790 g_free (my_current_file);
2793 static void
2794 update_one_panel (int which, int force_update, const char *current_file)
2796 WPanel *panel;
2798 if (get_display_type (which) != view_listing)
2799 return;
2801 panel = (WPanel *) get_panel_widget (which);
2802 update_one_panel_widget (panel, force_update, current_file);
2805 /* This routine reloads the directory in both panels. It tries to
2806 * select current_file in current_panel and other_file in other_panel.
2807 * If current_file == -1 then it automatically sets current_file and
2808 * other_file to the currently selected files in the panels.
2810 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
2811 * will not reload the other panel.
2813 void
2814 update_panels (int force_update, const char *current_file)
2816 int reload_other = !(force_update & UP_ONLY_CURRENT);
2817 WPanel *panel;
2819 update_one_panel (get_current_index (), force_update, current_file);
2820 if (reload_other)
2821 update_one_panel (get_other_index (), force_update, UP_KEEPSEL);
2823 if (get_current_type () == view_listing)
2824 panel = (WPanel *) get_panel_widget (get_current_index ());
2825 else
2826 panel = (WPanel *) get_panel_widget (get_other_index ());
2828 mc_chdir (panel->cwd);