Ticket #305 (dont work rename/copy on some chars)
[midnight-commander.git] / src / screen.c
blobfd7a043be89ed79016595894b95da014e3ee333c
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"
35 #include "tty.h"
36 #include "dir.h"
37 #include "panel.h"
38 #include "color.h"
39 #include "tree.h"
40 #include "win.h"
41 #include "ext.h" /* regexp_command */
42 #include "mouse.h" /* For Gpm_Event */
43 #include "layout.h" /* Most layout variables are here */
44 #include "wtools.h" /* for message (...) */
45 #include "cmd.h"
46 #include "key.h" /* XCTRL and ALT macros */
47 #include "setup.h" /* For loading/saving panel options */
48 #include "user.h"
49 #include "../src/mcconfig/mcconfig.h"
50 #include "execute.h"
51 #include "widget.h"
52 #include "menu.h" /* menubar_visible */
53 #include "main-widgets.h"
54 #include "main.h" /* the_menubar */
55 #include "unixcompat.h"
56 #include "mountlist.h" /* my_statfs */
57 #include "selcodepage.h" /* do_select_codepage () */
58 #include "charsets.h" /* get_codepage_id () */
59 #include "strutil.h"
61 #define ELEMENTS(arr) ( sizeof(arr) / sizeof((arr)[0]) )
63 #define NORMAL 0
64 #define SELECTED 1
65 #define MARKED 2
66 #define MARKED_SELECTED 3
67 #define STATUS 5
70 * This describes a format item. The parse_display_format routine parses
71 * the user specified format and creates a linked list of format_e structures.
73 typedef struct format_e {
74 struct format_e *next;
75 int requested_field_len;
76 int field_len;
77 align_crt_t just_mode;
78 int expand;
79 const char *(*string_fn)(file_entry *, int len);
80 const char *title;
81 const char *id;
82 } format_e;
84 /* If true, show the mini-info on the panel */
85 int show_mini_info = 1;
87 /* If true, then use stat() on the cwd to determine directory changes */
88 int fast_reload = 0;
90 /* If true, use some usability hacks by Torben */
91 int torben_fj_mode = 0;
93 /* If true, up/down keys scroll the pane listing by pages */
94 int panel_scroll_pages = 1;
96 /* If 1, we use permission hilighting */
97 int permission_mode = 0;
99 /* If 1 - then add per file type hilighting */
100 int filetype_mode = 1;
102 /* The hook list for the select file function */
103 Hook *select_file_hook = 0;
105 static cb_ret_t panel_callback (Widget *, widget_msg_t msg, int parm);
106 static int panel_event (Gpm_Event *event, void *);
107 static void paint_frame (WPanel *panel);
108 static const char *panel_format (WPanel *panel);
109 static const char *mini_status_format (WPanel *panel);
111 /* This macro extracts the number of available lines in a panel */
112 #define llines(p) (p->widget.lines-3 - (show_mini_info ? 2 : 0))
114 static void
115 set_colors (WPanel *panel)
117 (void) panel;
118 standend ();
119 attrset (NORMAL_COLOR);
122 /* Delete format string, it is a linked list */
123 static void
124 delete_format (format_e *format)
126 format_e *next;
128 while (format){
129 next = format->next;
130 g_free (format);
131 format = next;
135 /* This code relies on the default justification!!! */
136 static void
137 add_permission_string (char *dest, int width, file_entry *fe, int attr, int color, int is_octal)
139 int i, r, l;
141 l = get_user_permissions (&fe->st);
143 if (is_octal){
144 /* Place of the access bit in octal mode */
145 l = width + l - 3;
146 r = l + 1;
147 } else {
148 /* The same to the triplet in string mode */
149 l = l * 3 + 1;
150 r = l + 3;
153 for(i = 0; i < width; i++){
154 if (i >= l && i < r){
155 if (attr == SELECTED || attr == MARKED_SELECTED)
156 attrset (MARKED_SELECTED_COLOR);
157 else
158 attrset (MARKED_COLOR);
159 } else
160 attrset (color);
162 addch (dest[i]);
166 /* String representations of various file attributes */
167 /* name */
168 static const char *
169 string_file_name (file_entry *fe, int len)
171 static char buffer [MC_MAXPATHLEN * MB_LEN_MAX + 1];
173 (void) len;
174 g_strlcpy (buffer, fe->fname, sizeof(buffer));
175 return buffer;
178 static inline unsigned int ilog10(dev_t n)
180 unsigned int digits = 0;
181 do {
182 digits++, n /= 10;
183 } while (n != 0);
184 return digits;
187 static void format_device_number (char *buf, size_t bufsize, dev_t dev)
189 dev_t major_dev = major(dev);
190 dev_t minor_dev = minor(dev);
191 unsigned int major_digits = ilog10(major_dev);
192 unsigned int minor_digits = ilog10(minor_dev);
194 g_assert(bufsize >= 1);
195 if (major_digits + 1 + minor_digits + 1 <= bufsize) {
196 g_snprintf(buf, bufsize, "%lu,%lu", (unsigned long) major_dev,
197 (unsigned long) minor_dev);
198 } else {
199 g_strlcpy(buf, _("[dev]"), bufsize);
203 /* size */
204 static const char *
205 string_file_size (file_entry *fe, int len)
207 static char buffer [BUF_TINY];
209 /* Don't ever show size of ".." since we don't calculate it */
210 if (!strcmp (fe->fname, "..")) {
211 return _("UP--DIR");
214 #ifdef HAVE_STRUCT_STAT_ST_RDEV
215 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
216 format_device_number (buffer, len + 1, fe->st.st_rdev);
217 else
218 #endif
220 size_trunc_len (buffer, len, fe->st.st_size, 0);
222 return buffer;
225 /* bsize */
226 static const char *
227 string_file_size_brief (file_entry *fe, int len)
229 if (S_ISLNK (fe->st.st_mode) && !fe->f.link_to_dir) {
230 return _("SYMLINK");
233 if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && strcmp (fe->fname, "..")) {
234 return _("SUB-DIR");
237 return string_file_size (fe, len);
240 /* This functions return a string representation of a file entry */
241 /* type */
242 static const char *
243 string_file_type (file_entry *fe, int len)
245 static char buffer[2];
247 (void) len;
248 if (S_ISDIR (fe->st.st_mode))
249 buffer[0] = PATH_SEP;
250 else if (S_ISLNK (fe->st.st_mode)) {
251 if (fe->f.link_to_dir)
252 buffer[0] = '~';
253 else if (fe->f.stale_link)
254 buffer[0] = '!';
255 else
256 buffer[0] = '@';
257 } else if (S_ISCHR (fe->st.st_mode))
258 buffer[0] = '-';
259 else if (S_ISSOCK (fe->st.st_mode))
260 buffer[0] = '=';
261 else if (S_ISDOOR (fe->st.st_mode))
262 buffer[0] = '>';
263 else if (S_ISBLK (fe->st.st_mode))
264 buffer[0] = '+';
265 else if (S_ISFIFO (fe->st.st_mode))
266 buffer[0] = '|';
267 else if (S_ISNAM (fe->st.st_mode))
268 buffer[0] = '#';
269 else if (!S_ISREG (fe->st.st_mode))
270 buffer[0] = '?'; /* non-regular of unknown kind */
271 else if (is_exe (fe->st.st_mode))
272 buffer[0] = '*';
273 else
274 buffer[0] = ' ';
275 buffer[1] = '\0';
276 return buffer;
279 /* mtime */
280 static const char *
281 string_file_mtime (file_entry *fe, int len)
283 (void) len;
284 if (!strcmp (fe->fname, "..")) {
285 return "";
287 return file_date (fe->st.st_mtime);
290 /* atime */
291 static const char *
292 string_file_atime (file_entry *fe, int len)
294 (void) len;
295 if (!strcmp (fe->fname, "..")) {
296 return "";
298 return file_date (fe->st.st_atime);
301 /* ctime */
302 static const char *
303 string_file_ctime (file_entry *fe, int len)
305 (void) len;
306 if (!strcmp (fe->fname, "..")) {
307 return "";
309 return file_date (fe->st.st_ctime);
312 /* perm */
313 static const char *
314 string_file_permission (file_entry *fe, int len)
316 (void) len;
317 return string_perm (fe->st.st_mode);
320 /* mode */
321 static const char *
322 string_file_perm_octal (file_entry *fe, int len)
324 static char buffer [10];
326 (void) len;
327 g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode);
328 return buffer;
331 /* nlink */
332 static const char *
333 string_file_nlinks (file_entry *fe, int len)
335 static char buffer[BUF_TINY];
337 (void) len;
338 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
339 return buffer;
342 /* inode */
343 static const char *
344 string_inode (file_entry *fe, int len)
346 static char buffer [10];
348 (void) len;
349 g_snprintf (buffer, sizeof (buffer), "%lu",
350 (unsigned long) fe->st.st_ino);
351 return buffer;
354 /* nuid */
355 static const char *
356 string_file_nuid (file_entry *fe, int len)
358 static char buffer [10];
360 (void) len;
361 g_snprintf (buffer, sizeof (buffer), "%lu",
362 (unsigned long) fe->st.st_uid);
363 return buffer;
366 /* ngid */
367 static const char *
368 string_file_ngid (file_entry *fe, int len)
370 static char buffer [10];
372 (void) len;
373 g_snprintf (buffer, sizeof (buffer), "%lu",
374 (unsigned long) fe->st.st_gid);
375 return buffer;
378 /* owner */
379 static const char *
380 string_file_owner (file_entry *fe, int len)
382 (void) len;
383 return get_owner (fe->st.st_uid);
386 /* group */
387 static const char *
388 string_file_group (file_entry *fe, int len)
390 (void) len;
391 return get_group (fe->st.st_gid);
394 /* mark */
395 static const char *
396 string_marked (file_entry *fe, int len)
398 (void) len;
399 return fe->f.marked ? "*" : " ";
402 /* space */
403 static const char *
404 string_space (file_entry *fe, int len)
406 (void) fe;
407 (void) len;
408 return " ";
411 /* dot */
412 static const char *
413 string_dot (file_entry *fe, int len)
415 (void) fe;
416 (void) len;
417 return ".";
420 #define GT 1
422 static struct {
423 const char *id;
424 int min_size;
425 int expands;
426 align_crt_t default_just;
427 const char *title;
428 int use_in_gui;
429 const char *(*string_fn)(file_entry *, int);
430 sortfn *sort_routine; /* This field is currently unused. */
431 } formats [] = {
432 { "name", 12, 1, J_LEFT_FIT, N_("Name"), 1, string_file_name, (sortfn *) sort_name },
433 { "size", 7, 0, J_RIGHT, N_("Size"), 1, string_file_size, (sortfn *) sort_size },
434 { "bsize", 7, 0, J_RIGHT, N_("Size"), 1, string_file_size_brief, (sortfn *) sort_size },
435 { "type", GT, 0, J_LEFT, "", 2, string_file_type, NULL },
436 { "mtime", 12, 0, J_RIGHT, N_("MTime"), 1, string_file_mtime, (sortfn *) sort_time },
437 { "atime", 12, 0, J_RIGHT, N_("ATime"), 1, string_file_atime, (sortfn *) sort_atime },
438 { "ctime", 12, 0, J_RIGHT, N_("CTime"), 1, string_file_ctime, (sortfn *) sort_ctime },
439 { "perm", 10, 0, J_LEFT, N_("Permission"),1,string_file_permission, NULL },
440 { "mode", 6, 0, J_RIGHT, N_("Perm"), 1, string_file_perm_octal, NULL },
441 { "nlink", 2, 0, J_RIGHT, N_("Nl"), 1, string_file_nlinks, NULL },
442 { "inode", 5, 0, J_RIGHT, N_("Inode"), 1, string_inode, (sortfn *) sort_inode },
443 { "nuid", 5, 0, J_RIGHT, N_("UID"), 1, string_file_nuid, NULL },
444 { "ngid", 5, 0, J_RIGHT, N_("GID"), 1, string_file_ngid, NULL },
445 { "owner", 8, 0, J_LEFT_FIT, N_("Owner"), 1, string_file_owner, NULL },
446 { "group", 8, 0, J_LEFT_FIT, N_("Group"), 1, string_file_group, NULL },
447 { "mark", 1, 0, J_RIGHT, " ", 1, string_marked, NULL },
448 { "|", 1, 0, J_RIGHT, " ", 0, NULL, NULL },
449 { "space", 1, 0, J_RIGHT, " ", 0, string_space, NULL },
450 { "dot", 1, 0, J_RIGHT, " ", 0, string_dot, NULL },
453 static int
454 file_compute_color (int attr, file_entry *fe)
456 switch (attr) {
457 case SELECTED:
458 return (SELECTED_COLOR);
459 case MARKED:
460 return (MARKED_COLOR);
461 case MARKED_SELECTED:
462 return (MARKED_SELECTED_COLOR);
463 case STATUS:
464 return (NORMAL_COLOR);
465 case NORMAL:
466 default:
467 if (!filetype_mode)
468 return (NORMAL_COLOR);
471 /* if filetype_mode == true */
472 if (S_ISDIR (fe->st.st_mode))
473 return (DIRECTORY_COLOR);
474 else if (S_ISLNK (fe->st.st_mode)) {
475 if (fe->f.link_to_dir)
476 return (DIRECTORY_COLOR);
477 else if (fe->f.stale_link)
478 return (STALE_LINK_COLOR);
479 else
480 return (LINK_COLOR);
481 } else if (S_ISSOCK (fe->st.st_mode))
482 return (SPECIAL_COLOR);
483 else if (S_ISCHR (fe->st.st_mode))
484 return (DEVICE_COLOR);
485 else if (S_ISBLK (fe->st.st_mode))
486 return (DEVICE_COLOR);
487 else if (S_ISNAM (fe->st.st_mode))
488 return (DEVICE_COLOR);
489 else if (S_ISFIFO (fe->st.st_mode))
490 return (SPECIAL_COLOR);
491 else if (S_ISDOOR (fe->st.st_mode))
492 return (SPECIAL_COLOR);
493 else if (!S_ISREG (fe->st.st_mode))
494 return (STALE_LINK_COLOR); /* non-regular file of unknown kind */
495 else if (is_exe (fe->st.st_mode))
496 return (EXECUTABLE_COLOR);
497 else if (fe->fname && (!strcmp (fe->fname, "core")
498 || !strcmp (extension (fe->fname), "core")))
499 return (CORE_COLOR);
501 return (NORMAL_COLOR);
504 /* Formats the file number file_index of panel in the buffer dest */
505 static void
506 format_file (char *dest, int limit, WPanel *panel, int file_index, int width, int attr, int isstatus)
508 int color, length, empty_line;
509 const char *txt;
510 format_e *format, *home;
511 file_entry *fe;
513 (void) dest;
514 (void) limit;
515 length = 0;
516 empty_line = (file_index >= panel->count);
517 home = (isstatus) ? panel->status_format : panel->format;
518 fe = &panel->dir.list [file_index];
520 if (!empty_line)
521 color = file_compute_color (attr, fe);
522 else
523 color = NORMAL_COLOR;
525 for (format = home; format; format = format->next){
527 if (length == width)
528 break;
530 if (format->string_fn){
531 int len, perm;
532 char *preperad_text;
534 if (empty_line)
535 txt = " ";
536 else
537 txt = (*format->string_fn)(fe, format->field_len);
539 len = format->field_len;
540 if (len + length > width)
541 len = width - length;
542 if (len <= 0)
543 break;
545 perm = 0;
546 if (permission_mode) {
547 if (!strcmp(format->id, "perm"))
548 perm = 1;
549 else if (!strcmp(format->id, "mode"))
550 perm = 2;
553 attrset (color);
555 preperad_text = (char*) str_fit_to_term(txt, len, format->just_mode);
556 if (perm)
557 add_permission_string (preperad_text, format->field_len, fe,
558 attr, color, perm - 1);
559 else
560 addstr (preperad_text);
562 length+= len;
563 } else {
564 if (attr == SELECTED || attr == MARKED_SELECTED)
565 attrset (SELECTED_COLOR);
566 else
567 attrset (NORMAL_COLOR);
568 one_vline ();
569 length++;
573 if (length < width){
574 int still = width - length;
575 while (still--)
576 addch (' ');
580 static void
581 repaint_file (WPanel *panel, int file_index, int mv, int attr, int isstatus)
583 int second_column = 0;
584 int width, offset;
585 char buffer [BUF_MEDIUM];
587 offset = 0;
588 if (!isstatus && panel->split){
590 second_column = (file_index - panel->top_file) / llines (panel);
591 width = (panel->widget.cols - 2)/2 - 1;
593 if (second_column){
594 offset = 1 + width;
595 width = (panel->widget.cols-2) - (panel->widget.cols-2)/2 - 1;
597 } else
598 width = (panel->widget.cols - 2);
600 /* Nothing to paint */
601 if (width <= 0)
602 return;
604 if (mv){
605 if (!isstatus && panel->split){
606 widget_move (&panel->widget,
607 (file_index - panel->top_file) %
608 llines (panel) + 2,
609 (offset + 1));
610 } else
611 widget_move (&panel->widget, file_index - panel->top_file + 2, 1);
614 format_file (buffer, sizeof(buffer), panel, file_index, width, attr, isstatus);
616 if (!isstatus && panel->split){
617 if (second_column)
618 addch (' ');
619 else {
620 attrset (NORMAL_COLOR);
621 one_vline ();
626 static void
627 display_mini_info (WPanel *panel)
629 widget_move (&panel->widget, llines (panel)+3, 1);
631 if (panel->searching){
632 attrset (INPUT_COLOR);
633 addstr ("/");
634 addstr (str_fit_to_term (panel->search_buffer,
635 panel->widget.cols - 3, J_LEFT));
636 attrset (NORMAL_COLOR);
637 return;
640 /* Status resolves links and show them */
641 set_colors (panel);
643 if (S_ISLNK (panel->dir.list [panel->selected].st.st_mode)){
644 char *link, link_target [MC_MAXPATHLEN];
645 int len;
647 link = concat_dir_and_file (panel->cwd, panel->dir.list [panel->selected].fname);
648 len = mc_readlink (link, link_target, MC_MAXPATHLEN - 1);
649 g_free (link);
650 if (len > 0){
651 link_target[len] = 0;
652 addstr ("-> ");
653 addstr (str_fit_to_term (link_target, panel->widget.cols - 5,
654 J_LEFT_FIT));
655 } else
656 addstr (str_fit_to_term (_("<readlink failed>"),
657 panel->widget.cols - 2, J_LEFT));
658 } else if (strcmp (panel->dir.list [panel->selected].fname, "..") == 0) {
659 /* FIXME:
660 * while loading directory (do_load_dir() and do_reload_dir(),
661 * the actual stat info about ".." directory isn't got;
662 * so just don't display incorrect info about ".." directory */
663 addstr (str_fit_to_term (_("UP--DIR"), panel->widget.cols - 2, J_LEFT));
664 } else
665 /* Default behavior */
666 repaint_file (panel, panel->selected, 0, STATUS, 1);
669 static void
670 paint_dir (WPanel *panel)
672 int i;
673 int color; /* Color value of the line */
674 int items; /* Number of items */
676 items = llines (panel) * (panel->split ? 2 : 1);
678 for (i = 0; i < items; i++){
679 if (i+panel->top_file >= panel->count)
680 color = 0;
681 else {
682 color = 2 * (panel->dir.list [i+panel->top_file].f.marked);
683 color += (panel->selected==i+panel->top_file && panel->active);
685 repaint_file (panel, i+panel->top_file, 1, color, 0);
687 standend ();
690 static void
691 display_total_marked_size (WPanel *panel, int y, int x, gboolean size_only)
693 char buffer[BUF_SMALL], b_bytes[BUF_SMALL], *buf;
694 int cols;
695 size_t blen;
697 if (panel->marked <= 0)
698 return;
700 buf = size_only ? b_bytes : buffer;
701 cols = panel->widget.cols - 2;
704 * This is a trick to use two ngettext() calls in one sentence.
705 * First make "N bytes", then insert it into "X in M files".
707 g_snprintf (b_bytes, sizeof (b_bytes),
708 ngettext("%s byte", "%s bytes", (unsigned long) panel->total),
709 size_trunc_sep (panel->total));
710 if (!size_only)
711 g_snprintf (buffer, sizeof (buffer),
712 ngettext("%s in %d file", "%s in %d files", panel->marked),
713 b_bytes, panel->marked);
715 blen = strlen (buf);
717 /* don't forget spaces around buffer content */
718 if ((int) blen > cols - 2) {
719 buf[cols - 2] = '\0';
720 blen = (size_t) (cols - 2);
723 if (x < 0)
724 /* center in panel */
725 x = (panel->widget.cols - (int) blen) / 2 - 1;
728 * y == llines (panel) + 2 for mini_info_separator
729 * y == panel->widget.lines - 1 for panel bottom frame
731 widget_move (&panel->widget, y, x);
732 attrset (MARKED_COLOR);
733 tty_printf (" %s ", buf);
736 static void
737 mini_info_separator (WPanel *panel)
739 const int y = llines (panel) + 2;
741 standend ();
742 widget_move (&panel->widget, y, 1);
743 attrset (NORMAL_COLOR);
744 #ifdef HAVE_SLANG
745 hline (ACS_HLINE, panel->widget.cols - 2);
746 #else
747 hline ((slow_terminal ? '-' : ACS_HLINE) | NORMAL_COLOR,
748 panel->widget.cols - 2);
749 #endif /* !HAVE_SLANG */
751 /* Status displays total marked size.
752 * Centered in panel, full format. */
753 display_total_marked_size (panel, y, -1, FALSE);
756 static void
757 show_free_space (WPanel *panel)
759 /* Used to figure out how many free space we have */
760 static struct my_statfs myfs_stats;
761 /* Old current working directory for displaying free space */
762 static char *old_cwd = NULL;
764 /* Don't try to stat non-local fs */
765 if (!vfs_file_is_local (panel->cwd) || !free_space)
766 return;
768 if (old_cwd == NULL || strcmp (old_cwd, panel->cwd) != 0) {
769 char rpath[PATH_MAX];
771 init_my_statfs ();
772 g_free (old_cwd);
773 old_cwd = g_strdup (panel->cwd);
775 if (mc_realpath (panel->cwd, rpath) == NULL)
776 return;
778 my_statfs (&myfs_stats, rpath);
781 if (myfs_stats.avail > 0 || myfs_stats.total > 0) {
782 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
783 size_trunc_len (buffer1, sizeof(buffer1) - 1, myfs_stats.avail, 1);
784 size_trunc_len (buffer2, sizeof(buffer2) - 1, myfs_stats.total, 1);
785 g_snprintf (tmp, sizeof(tmp), " %s/%s (%d%%) ", buffer1, buffer2,
786 myfs_stats.total > 0 ?
787 (int)(100 * (double)myfs_stats.avail / myfs_stats.total) : 0);
788 widget_move (&panel->widget, panel->widget.lines - 1,
789 panel->widget.cols - 2 - (int) strlen (tmp));
790 attrset (NORMAL_COLOR);
791 addstr (tmp);
795 static void
796 show_dir (WPanel *panel)
798 int len;
800 set_colors (panel);
801 draw_double_box (panel->widget.parent,
802 panel->widget.y, panel->widget.x,
803 panel->widget.lines, panel->widget.cols);
805 if (show_mini_info) {
806 widget_move (&panel->widget, llines (panel) + 2, 0);
807 tty_print_alt_char (ACS_LTEE);
808 widget_move (&panel->widget, llines (panel) + 2,
809 panel->widget.cols - 1);
810 tty_print_alt_char (ACS_RTEE);
813 if (panel->active)
814 attrset (REVERSE_COLOR);
816 widget_move (&panel->widget, 0, 3);
818 addch (' ');
819 len = min (max (panel->widget.cols - 9, 0), panel->widget.cols);
820 addstr (str_term_trim (strip_home_and_password (panel->cwd), len));
821 addch (' ');
823 widget_move (&panel->widget, 0, 1);
824 addstr ("<");
825 widget_move (&panel->widget, 0, panel->widget.cols - 2);
826 addstr (">");
827 widget_move (&panel->widget, 0, panel->widget.cols - 3);
828 addstr ("v");
830 if (!show_mini_info) {
831 if (panel->marked == 0) {
832 /* Show size of curret file in the bottom of panel */
833 if (S_ISREG (panel->dir.list [panel->selected].st.st_mode)) {
834 char buffer[BUF_SMALL];
836 g_snprintf (buffer, sizeof (buffer), " %s ",
837 size_trunc_sep (panel->dir.list [panel->selected].st.st_size));
838 attrset (NORMAL_COLOR);
839 widget_move (&panel->widget, panel->widget.lines - 1, 2);
840 addstr (buffer);
842 } else {
843 /* Show total size of marked files
844 * In the bottom of panel, display size only. */
845 display_total_marked_size (panel, panel->widget.lines - 1, 2, TRUE);
849 show_free_space (panel);
851 if (panel->active)
852 standend ();
855 /* To be used only by long_frame and full_frame to adjust top_file */
856 static void
857 adjust_top_file (WPanel *panel)
859 int old_top = panel->top_file;
861 if (panel->selected - old_top > llines (panel))
862 panel->top_file = panel->selected;
863 if (old_top - panel->count > llines (panel))
864 panel->top_file = panel->count - llines (panel);
867 /* Repaint everything, including frame and separator */
868 static void
869 paint_panel (WPanel *panel)
871 paint_frame (panel); /* including show_dir */
872 paint_dir (panel);
874 if (show_mini_info) {
875 mini_info_separator (panel);
876 display_mini_info (panel);
879 panel->dirty = 0;
882 /* add "#enc:encodning" to end of path */
883 /* if path end width a previous #enc:, only encoding is changed no additional
884 * #enc: is appended
885 * retun new string
887 static char
888 *add_encoding_to_path (const char *path, const char *encoding)
890 char *result;
891 char *semi;
892 char *slash;
894 semi = g_strrstr (path, "#enc:");
896 if (semi != NULL) {
897 slash = strchr (semi, PATH_SEP);
898 if (slash != NULL) {
899 result = g_strconcat (path, "/#enc:", encoding, NULL);
900 } else {
901 *semi = 0;
902 result = g_strconcat (path, "/#enc:", encoding, NULL);
903 *semi = '#';
905 } else {
906 result = g_strconcat (path, "/#enc:", encoding, NULL);
909 return result;
912 char *
913 remove_encoding_from_path (const char *path)
915 GString *ret;
916 GString *tmp_path, *tmp_conv;
917 char *tmp, *tmp2;
918 const char *enc;
919 GIConv converter;
921 ret = g_string_new("");
922 tmp_conv = g_string_new("");
924 tmp_path = g_string_new(path);
926 while ((tmp = g_strrstr (tmp_path->str, "/#enc:")) != NULL){
927 enc = vfs_get_encoding ((const char *) tmp);
928 converter = enc ? str_crt_conv_to (enc): str_cnv_to_term;
929 if (converter == INVALID_CONV) converter = str_cnv_to_term;
931 tmp2=tmp+1;
932 while (*tmp2 && *tmp2 != '/')
933 tmp2++;
935 if (*tmp2){
936 str_vfs_convert_from (converter, tmp2, tmp_conv);
937 g_string_prepend(ret, tmp_conv->str);
938 g_string_set_size(tmp_conv,0);
940 g_string_set_size(tmp_path,tmp - tmp_path->str);
941 str_close_conv (converter);
943 g_string_prepend(ret, tmp_path->str);
944 g_string_free(tmp_path,TRUE);
945 g_string_free(tmp_conv,TRUE);
947 tmp = ret->str;
948 g_string_free(ret, FALSE);
949 return tmp;
953 * Repaint the contents of the panels without frames. To schedule panel
954 * for repainting, set panel->dirty to 1. There are many reasons why
955 * the panels need to be repainted, and this is a costly operation, so
956 * it's done once per event.
958 void
959 update_dirty_panels (void)
961 if (current_panel->dirty)
962 paint_panel (current_panel);
964 if ((get_other_type () == view_listing) && other_panel->dirty)
965 paint_panel (other_panel);
968 static void
969 do_select (WPanel *panel, int i)
971 if (i != panel->selected) {
972 panel->dirty = 1;
973 panel->selected = i;
974 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
975 if (panel->top_file < 0)
976 panel->top_file = 0;
980 static inline void
981 do_try_to_select (WPanel *panel, const char *name)
983 int i;
984 char *subdir;
986 if (!name) {
987 do_select(panel, 0);
988 return;
991 /* We only want the last component of the directory,
992 * and from this only the name without suffix. */
993 subdir = vfs_strip_suffix_from_filename (x_basename(name));
995 /* Search that subdirectory, if found select it */
996 for (i = 0; i < panel->count; i++){
997 if (strcmp (subdir, panel->dir.list [i].fname) == 0) {
998 do_select (panel, i);
999 g_free (subdir);
1000 return;
1004 /* Try to select a file near the file that is missing */
1005 if (panel->selected >= panel->count)
1006 do_select (panel, panel->count-1);
1007 g_free (subdir);
1010 void
1011 try_to_select (WPanel *panel, const char *name)
1013 do_try_to_select (panel, name);
1014 select_item (panel);
1017 void
1018 panel_update_cols (Widget *widget, int frame_size)
1020 int cols, origin;
1022 if (horizontal_split){
1023 widget->cols = COLS;
1024 return;
1027 if (frame_size == frame_full){
1028 cols = COLS;
1029 origin = 0;
1030 } else {
1031 if (widget == get_panel_widget (0)){
1032 cols = first_panel_size;
1033 origin = 0;
1034 } else {
1035 cols = COLS-first_panel_size;
1036 origin = first_panel_size;
1040 widget->cols = cols;
1041 widget->x = origin;
1044 static char *
1045 panel_save_name (WPanel *panel)
1047 extern int saving_setup;
1049 /* If the program is shuting down */
1050 if ((midnight_shutdown && auto_save_setup) || saving_setup)
1051 return g_strdup (panel->panel_name);
1052 else
1053 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1056 static void
1057 panel_destroy (WPanel *p)
1059 int i;
1061 char *name = panel_save_name (p);
1063 panel_save_setup (p, name);
1064 panel_clean_dir (p);
1066 /* save and clean history */
1067 if (p->dir_history) {
1068 history_put (p->hist_name, p->dir_history);
1070 p->dir_history = g_list_first (p->dir_history);
1071 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1072 g_list_free (p->dir_history);
1075 g_free (p->hist_name);
1077 delete_format (p->format);
1078 delete_format (p->status_format);
1080 g_free (p->user_format);
1081 for (i = 0; i < LIST_TYPES; i++)
1082 g_free (p->user_status_format[i]);
1083 g_free (p->dir.list);
1084 g_free (p->panel_name);
1085 g_free (name);
1088 static void
1089 panel_format_modified (WPanel *panel)
1091 panel->format_modified = 1;
1094 /* Panel creation */
1095 /* The parameter specifies the name of the panel for setup retieving */
1096 WPanel *
1097 panel_new (const char *panel_name)
1099 WPanel *panel;
1100 char *section;
1101 int i, err;
1103 panel = g_new0 (WPanel, 1);
1105 /* No know sizes of the panel at startup */
1106 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
1108 /* We do not want the cursor */
1109 widget_want_cursor (panel->widget, 0);
1111 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
1112 strcpy (panel->lwd, ".");
1114 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
1115 panel->dir_history = history_get (panel->hist_name);
1116 directory_history_add (panel, panel->cwd);
1118 panel->dir.list = g_new (file_entry, MIN_FILES);
1119 panel->dir.size = MIN_FILES;
1120 panel->active = 0;
1121 panel->filter = 0;
1122 panel->split = 0;
1123 panel->top_file = 0;
1124 panel->selected = 0;
1125 panel->marked = 0;
1126 panel->total = 0;
1127 panel->reverse = 0;
1128 panel->dirty = 1;
1129 panel->searching = 0;
1130 panel->dirs_marked = 0;
1131 panel->is_panelized = 0;
1132 panel->format = 0;
1133 panel->status_format = 0;
1134 panel->format_modified = 1;
1136 panel->panel_name = g_strdup (panel_name);
1137 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
1139 for (i = 0; i < LIST_TYPES; i++)
1140 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
1142 panel->search_buffer[0] = 0;
1143 panel->frame_size = frame_half;
1144 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1145 if (!mc_config_has_group (mc_main_config, section)) {
1146 g_free (section);
1147 section = g_strdup (panel->panel_name);
1149 panel_load_setup (panel, section);
1150 g_free (section);
1152 /* Load format strings */
1153 err = set_panel_formats (panel);
1154 if (err) {
1155 set_panel_formats (panel);
1158 /* Load the default format */
1159 panel->count =
1160 do_load_dir (panel->cwd, &panel->dir, panel->sort_type,
1161 panel->reverse, panel->case_sensitive,
1162 panel->exec_first, panel->filter);
1163 return panel;
1166 void
1167 panel_reload (WPanel *panel)
1169 struct stat current_stat;
1171 if (fast_reload && !stat (panel->cwd, &current_stat)
1172 && current_stat.st_ctime == panel->dir_stat.st_ctime
1173 && current_stat.st_mtime == panel->dir_stat.st_mtime)
1174 return;
1176 while (mc_chdir (panel->cwd) == -1) {
1177 char *last_slash;
1179 if (panel->cwd[0] == PATH_SEP && panel->cwd[1] == 0) {
1180 panel_clean_dir (panel);
1181 panel->count = set_zero_dir (&panel->dir);
1182 return;
1184 last_slash = strrchr (panel->cwd, PATH_SEP);
1185 if (!last_slash || last_slash == panel->cwd)
1186 strcpy (panel->cwd, PATH_SEP_STR);
1187 else
1188 *last_slash = 0;
1189 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
1190 show_dir (panel);
1193 panel->count =
1194 do_reload_dir (panel->cwd, &panel->dir, panel->sort_type,
1195 panel->count, panel->reverse, panel->case_sensitive,
1196 panel->exec_first, panel->filter);
1198 panel->dirty = 1;
1199 if (panel->selected >= panel->count)
1200 do_select (panel, panel->count - 1);
1202 recalculate_panel_summary (panel);
1205 static void
1206 paint_frame (WPanel *panel)
1208 int header_len;
1209 int side, width;
1211 const char *txt;
1212 if (!panel->split)
1213 adjust_top_file (panel);
1215 widget_erase (&panel->widget);
1216 show_dir (panel);
1218 widget_move (&panel->widget, 1, 1);
1220 for (side = 0; side <= panel->split; side++){
1221 format_e *format;
1223 if (side){
1224 attrset (NORMAL_COLOR);
1225 one_vline ();
1226 width = panel->widget.cols - panel->widget.cols/2 - 1;
1227 } else if (panel->split)
1228 width = panel->widget.cols/2 - 3;
1229 else
1230 width = panel->widget.cols - 2;
1232 for (format = panel->format; format; format = format->next){
1233 if (format->string_fn){
1234 txt = format->title;
1236 header_len = strlen (txt);
1237 if (header_len > format->field_len)
1238 header_len = format->field_len;
1240 attrset (MARKED_COLOR);
1241 addstr (str_fit_to_term (txt, format->field_len, J_CENTER_LEFT));
1242 width -= format->field_len;
1243 } else {
1244 attrset (NORMAL_COLOR);
1245 one_vline ();
1246 width --;
1247 continue;
1251 if (width > 0)
1252 tty_printf ("%*s", width, "");
1256 static const char *
1257 parse_panel_size (WPanel *panel, const char *format, int isstatus)
1259 int frame = frame_half;
1260 format = skip_separators (format);
1262 if (!strncmp (format, "full", 4)){
1263 frame = frame_full;
1264 format += 4;
1265 } else if (!strncmp (format, "half", 4)){
1266 frame = frame_half;
1267 format += 4;
1270 if (!isstatus){
1271 panel->frame_size = frame;
1272 panel->split = 0;
1275 /* Now, the optional column specifier */
1276 format = skip_separators (format);
1278 if (*format == '1' || *format == '2'){
1279 if (!isstatus)
1280 panel->split = *format == '2';
1281 format++;
1284 if (!isstatus)
1285 panel_update_cols (&(panel->widget), panel->frame_size);
1287 return skip_separators (format);
1290 /* Format is:
1292 all := panel_format? format
1293 panel_format := [full|half] [1|2]
1294 format := one_format_e
1295 | format , one_format_e
1297 one_format_e := just format.id [opt_size]
1298 just := [<=>]
1299 opt_size := : size [opt_expand]
1300 size := [0-9]+
1301 opt_expand := +
1305 static format_e *
1306 parse_display_format (WPanel *panel, const char *format, char **error, int isstatus, int *res_total_cols)
1308 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1309 int total_cols = 0; /* Used columns by the format */
1310 int set_justify; /* flag: set justification mode? */
1311 align_crt_t justify = J_LEFT; /* Which mode. */
1312 int items = 0; /* Number of items in the format */
1313 size_t i;
1315 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1317 *error = 0;
1319 if (i18n_timelength == 0) {
1320 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1322 for (i = 0; i < ELEMENTS(formats); i++)
1323 if (strcmp ("time", formats [i].id+1) == 0)
1324 formats [i].min_size = i18n_timelength;
1328 * This makes sure that the panel and mini status full/half mode
1329 * setting is equal
1331 format = parse_panel_size (panel, format, isstatus);
1333 while (*format){ /* format can be an empty string */
1334 int found = 0;
1336 darr = g_new (format_e, 1);
1338 /* I'm so ugly, don't look at me :-) */
1339 if (!home)
1340 home = old = darr;
1342 old->next = darr;
1343 darr->next = 0;
1344 old = darr;
1346 format = skip_separators (format);
1348 if (strchr ("<=>", *format)){
1349 set_justify = 1;
1350 switch (*format)
1352 case '<':
1353 justify = J_LEFT;
1354 break;
1355 case '=':
1356 justify = J_CENTER;
1357 break;
1358 case '>':
1359 default:
1360 justify = J_RIGHT;
1361 break;
1363 format = skip_separators (format+1);
1364 } else
1365 set_justify = 0;
1367 for (i = 0; i < ELEMENTS(formats); i++){
1368 int klen = strlen (formats [i].id);
1370 if (strncmp (format, formats [i].id, klen) != 0)
1371 continue;
1373 format += klen;
1375 if (formats [i].use_in_gui)
1376 items++;
1378 darr->requested_field_len = formats [i].min_size;
1379 darr->string_fn = formats [i].string_fn;
1380 if (formats [i].title [0])
1381 darr->title = _(formats [i].title);
1382 else
1383 darr->title = "";
1384 darr->id = formats [i].id;
1385 darr->expand = formats [i].expands;
1386 darr->just_mode = formats [i].default_just;
1388 if (set_justify) {
1389 if (IS_FIT(darr->just_mode))
1390 darr->just_mode = MAKE_FIT(justify);
1391 else
1392 darr->just_mode = justify;
1394 found = 1;
1396 format = skip_separators (format);
1398 /* If we have a size specifier */
1399 if (*format == ':'){
1400 int req_length;
1402 /* If the size was specified, we don't want
1403 * auto-expansion by default
1405 darr->expand = 0;
1406 format++;
1407 req_length = atoi (format);
1408 darr->requested_field_len = req_length;
1410 format = skip_numbers (format);
1412 /* Now, if they insist on expansion */
1413 if (*format == '+'){
1414 darr->expand = 1;
1415 format++;
1420 break;
1422 if (!found){
1423 char *tmp_format = g_strdup (format);
1425 int pos = min (8, strlen (format));
1426 delete_format (home);
1427 tmp_format [pos] = 0;
1428 *error = g_strconcat (_("Unknown tag on display format: "), tmp_format, (char *) NULL);
1429 g_free (tmp_format);
1430 return 0;
1432 total_cols += darr->requested_field_len;
1435 *res_total_cols = total_cols;
1436 return home;
1439 static format_e *
1440 use_display_format (WPanel *panel, const char *format, char **error, int isstatus)
1442 #define MAX_EXPAND 4
1443 int expand_top = 0; /* Max used element in expand */
1444 int usable_columns; /* Usable columns in the panel */
1445 int total_cols = 0;
1446 int i;
1447 format_e *darr, *home;
1449 if (!format)
1450 format = DEFAULT_USER_FORMAT;
1452 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1454 if (*error)
1455 return 0;
1457 panel->dirty = 1;
1459 /* Status needn't to be split */
1460 usable_columns = ((panel->widget.cols-2)/((isstatus)
1462 : (panel->split+1))) - (!isstatus && panel->split);
1464 /* Look for the expandable fields and set field_len based on the requested field len */
1465 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next){
1466 darr->field_len = darr->requested_field_len;
1467 if (darr->expand)
1468 expand_top++;
1471 /* If we used more columns than the available columns, adjust that */
1472 if (total_cols > usable_columns){
1473 int pdif, dif = total_cols - usable_columns;
1475 while (dif){
1476 pdif = dif;
1477 for (darr = home; darr; darr = darr->next){
1478 if (dif && darr->field_len - 1){
1479 darr->field_len--;
1480 dif--;
1484 /* avoid endless loop if num fields > 40 */
1485 if (pdif == dif)
1486 break;
1488 total_cols = usable_columns; /* give up, the rest should be truncated */
1491 /* Expand the available space */
1492 if ((usable_columns > total_cols) && expand_top){
1493 int spaces = (usable_columns - total_cols) / expand_top;
1494 int extra = (usable_columns - total_cols) % expand_top;
1496 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1497 if (darr->expand){
1498 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1499 i++;
1502 return home;
1505 /* Switches the panel to the mode specified in the format */
1506 /* Seting up both format and status string. Return: 0 - on success; */
1507 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
1509 set_panel_formats (WPanel *p)
1511 format_e *form;
1512 char *err;
1513 int retcode = 0;
1515 form = use_display_format (p, panel_format (p), &err, 0);
1517 if (err){
1518 g_free (err);
1519 retcode = 1;
1521 else {
1522 if (p->format)
1523 delete_format (p->format);
1525 p->format = form;
1528 if (show_mini_info){
1530 form = use_display_format (p, mini_status_format (p), &err, 1);
1532 if (err){
1533 g_free (err);
1534 retcode += 2;
1536 else {
1537 if (p->status_format)
1538 delete_format (p->status_format);
1540 p->status_format = form;
1544 panel_format_modified (p);
1545 panel_update_cols (&(p->widget), p->frame_size);
1547 if (retcode)
1548 message (D_ERROR, _("Warning" ), _( "User supplied format looks invalid, reverting to default." ) );
1549 if (retcode & 0x01){
1550 g_free (p->user_format);
1551 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
1553 if (retcode & 0x02){
1554 g_free (p->user_status_format [p->list_type]);
1555 p->user_status_format [p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
1558 return retcode;
1561 /* Given the panel->view_type returns the format string to be parsed */
1562 static const char *
1563 panel_format (WPanel *panel)
1565 switch (panel->list_type){
1567 case list_long:
1568 return "full perm space nlink space owner space group space size space mtime space name";
1570 case list_brief:
1571 return "half 2 type name";
1573 case list_user:
1574 return panel->user_format;
1576 default:
1577 case list_full:
1578 return "half type name | size | mtime";
1582 static const char *
1583 mini_status_format (WPanel *panel)
1585 if (panel->user_mini_status)
1586 return panel->user_status_format [panel->list_type];
1588 switch (panel->list_type){
1590 case list_long:
1591 return "full perm space nlink space owner space group space size space mtime space name";
1593 case list_brief:
1594 return "half type name space bsize space perm space";
1596 case list_full:
1597 return "half type name";
1599 default:
1600 case list_user:
1601 return panel->user_format;
1605 /* */
1606 /* Panel operation commands */
1607 /* */
1609 /* Returns the number of items in the given panel */
1610 static int
1611 ITEMS (WPanel *p)
1613 if (p->split)
1614 return llines (p) * 2;
1615 else
1616 return llines (p);
1619 /* Select current item and readjust the panel */
1620 void
1621 select_item (WPanel *panel)
1623 int items = ITEMS (panel);
1625 /* Although currently all over the code we set the selection and
1626 top file to decent values before calling select_item, I could
1627 forget it someday, so it's better to do the actual fitting here */
1629 if (panel->top_file < 0)
1630 panel->top_file = 0;
1632 if (panel->selected < 0)
1633 panel->selected = 0;
1635 if (panel->selected > panel->count - 1)
1636 panel->selected = panel->count - 1;
1638 if (panel->top_file > panel->count - 1)
1639 panel->top_file = panel->count - 1;
1641 if ((panel->count - panel->top_file) < items) {
1642 panel->top_file = panel->count - items;
1643 if (panel->top_file < 0)
1644 panel->top_file = 0;
1647 if (panel->selected < panel->top_file)
1648 panel->top_file = panel->selected;
1650 if ((panel->selected - panel->top_file) >= items)
1651 panel->top_file = panel->selected - items + 1;
1653 panel->dirty = 1;
1655 execute_hooks (select_file_hook);
1658 /* Clears all files in the panel, used only when one file was marked */
1659 void
1660 unmark_files (WPanel *panel)
1662 int i;
1664 if (!panel->marked)
1665 return;
1666 for (i = 0; i < panel->count; i++)
1667 file_mark (panel, i, 0);
1669 panel->dirs_marked = 0;
1670 panel->marked = 0;
1671 panel->total = 0;
1674 static void
1675 unselect_item (WPanel *panel)
1677 repaint_file (panel, panel->selected, 1, 2*selection (panel)->f.marked, 0);
1680 static void
1681 move_down (WPanel *panel)
1683 if (panel->selected+1 == panel->count)
1684 return;
1686 unselect_item (panel);
1687 panel->selected++;
1689 if (panel->selected - panel->top_file == ITEMS (panel) &&
1690 panel_scroll_pages){
1691 /* Scroll window half screen */
1692 panel->top_file += ITEMS (panel)/2;
1693 if (panel->top_file > panel->count - ITEMS (panel))
1694 panel->top_file = panel->count - ITEMS (panel);
1695 paint_dir (panel);
1696 select_item (panel);
1698 select_item (panel);
1701 static void
1702 move_up (WPanel *panel)
1704 if (panel->selected == 0)
1705 return;
1707 unselect_item (panel);
1708 panel->selected--;
1709 if (panel->selected < panel->top_file && panel_scroll_pages){
1710 /* Scroll window half screen */
1711 panel->top_file -= ITEMS (panel)/2;
1712 if (panel->top_file < 0) panel->top_file = 0;
1713 paint_dir (panel);
1715 select_item (panel);
1718 /* Changes the selection by lines (may be negative) */
1719 static void
1720 move_selection (WPanel *panel, int lines)
1722 int new_pos;
1723 int adjust = 0;
1725 new_pos = panel->selected + lines;
1726 if (new_pos >= panel->count)
1727 new_pos = panel->count-1;
1729 if (new_pos < 0)
1730 new_pos = 0;
1732 unselect_item (panel);
1733 panel->selected = new_pos;
1735 if (panel->selected - panel->top_file >= ITEMS (panel)){
1736 panel->top_file += lines;
1737 adjust = 1;
1740 if (panel->selected - panel->top_file < 0){
1741 panel->top_file += lines;
1742 adjust = 1;
1745 if (adjust){
1746 if (panel->top_file > panel->selected)
1747 panel->top_file = panel->selected;
1748 if (panel->top_file < 0)
1749 panel->top_file = 0;
1750 paint_dir (panel);
1752 select_item (panel);
1755 static cb_ret_t
1756 move_left (WPanel *panel, int c_code)
1758 (void) c_code;
1759 if (panel->split) {
1760 move_selection (panel, -llines (panel));
1761 return MSG_HANDLED;
1762 } else
1763 return maybe_cd (1); /* cd .. */
1766 static int
1767 move_right (WPanel *panel, int c_code)
1769 (void) c_code;
1770 if (panel->split) {
1771 move_selection (panel, llines (panel));
1772 return MSG_HANDLED;
1773 } else
1774 return maybe_cd (0); /* cd (selection) */
1777 static void
1778 prev_page (WPanel *panel)
1780 int items;
1782 if (!panel->selected && !panel->top_file)
1783 return;
1784 unselect_item (panel);
1785 items = ITEMS (panel);
1786 if (panel->top_file < items)
1787 items = panel->top_file;
1788 if (!items)
1789 panel->selected = 0;
1790 else
1791 panel->selected -= items;
1792 panel->top_file -= items;
1794 /* This keeps the selection in a reasonable place */
1795 if (panel->selected < 0)
1796 panel->selected = 0;
1797 if (panel->top_file < 0)
1798 panel->top_file = 0;
1799 select_item (panel);
1800 paint_dir (panel);
1803 static void
1804 ctrl_prev_page (WPanel *panel)
1806 (void) panel;
1807 do_cd ("..", cd_exact);
1810 static void
1811 next_page (WPanel *panel)
1813 int items;
1815 if (panel->selected == panel->count - 1)
1816 return;
1817 unselect_item (panel);
1818 items = ITEMS (panel);
1819 if (panel->top_file > panel->count - 2 * items)
1820 items = panel->count - items - panel->top_file;
1821 if (panel->top_file + items < 0)
1822 items = -panel->top_file;
1823 if (!items)
1824 panel->selected = panel->count - 1;
1825 else
1826 panel->selected += items;
1827 panel->top_file += items;
1829 /* This keeps the selection in it's relative position */
1830 if (panel->selected >= panel->count)
1831 panel->selected = panel->count - 1;
1832 if (panel->top_file >= panel->count)
1833 panel->top_file = panel->count - 1;
1834 select_item (panel);
1835 paint_dir (panel);
1838 static void
1839 ctrl_next_page (WPanel *panel)
1841 if ((S_ISDIR (selection (panel)->st.st_mode)
1842 || link_isdir (selection (panel)))) {
1843 do_cd (selection (panel)->fname, cd_exact);
1847 static void
1848 goto_top_file (WPanel *panel)
1850 unselect_item (panel);
1851 panel->selected = panel->top_file;
1852 select_item (panel);
1855 static void
1856 goto_middle_file (WPanel *panel)
1858 unselect_item (panel);
1859 panel->selected = panel->top_file + (ITEMS (panel)/2);
1860 if (panel->selected >= panel->count)
1861 panel->selected = panel->count - 1;
1862 select_item (panel);
1865 static void
1866 goto_bottom_file (WPanel *panel)
1868 unselect_item (panel);
1869 panel->selected = panel->top_file + ITEMS (panel)-1;
1870 if (panel->selected >= panel->count)
1871 panel->selected = panel->count - 1;
1872 select_item (panel);
1875 static void
1876 move_home (WPanel *panel)
1878 if (panel->selected == 0)
1879 return;
1880 unselect_item (panel);
1882 if (torben_fj_mode){
1883 int middle_pos = panel->top_file + (ITEMS (panel)/2);
1885 if (panel->selected > middle_pos){
1886 goto_middle_file (panel);
1887 return;
1889 if (panel->selected != panel->top_file){
1890 goto_top_file (panel);
1891 return;
1895 panel->top_file = 0;
1896 panel->selected = 0;
1898 paint_dir (panel);
1899 select_item (panel);
1902 static void
1903 move_end (WPanel *panel)
1905 if (panel->selected == panel->count-1)
1906 return;
1907 unselect_item (panel);
1908 if (torben_fj_mode){
1909 int middle_pos = panel->top_file + (ITEMS (panel)/2);
1911 if (panel->selected < middle_pos){
1912 goto_middle_file (panel);
1913 return;
1915 if (panel->selected != (panel->top_file + ITEMS(panel)-1)){
1916 goto_bottom_file (panel);
1917 return;
1921 panel->selected = panel->count-1;
1922 paint_dir (panel);
1923 select_item (panel);
1926 /* Recalculate the panels summary information, used e.g. when marked
1927 files might have been removed by an external command */
1928 void
1929 recalculate_panel_summary (WPanel *panel)
1931 int i;
1933 panel->marked = 0;
1934 panel->dirs_marked = 0;
1935 panel->total = 0;
1937 for (i = 0; i < panel->count; i++)
1938 if (panel->dir.list [i].f.marked){
1939 /* do_file_mark will return immediately if newmark == oldmark.
1940 So we have to first unmark it to get panel's summary information
1941 updated. (Norbert) */
1942 panel->dir.list [i].f.marked = 0;
1943 do_file_mark (panel, i, 1);
1947 /* This routine marks a file or a directory */
1948 void
1949 do_file_mark (WPanel *panel, int idx, int mark)
1951 if (panel->dir.list[idx].f.marked == mark)
1952 return;
1954 /* Only '..' can't be marked, '.' isn't visible */
1955 if (!strcmp (panel->dir.list[idx].fname, ".."))
1956 return;
1958 file_mark (panel, idx, mark);
1959 if (panel->dir.list[idx].f.marked) {
1960 panel->marked++;
1961 if (S_ISDIR (panel->dir.list[idx].st.st_mode)) {
1962 if (panel->dir.list[idx].f.dir_size_computed)
1963 panel->total += panel->dir.list[idx].st.st_size;
1964 panel->dirs_marked++;
1965 } else
1966 panel->total += panel->dir.list[idx].st.st_size;
1967 set_colors (panel);
1968 } else {
1969 if (S_ISDIR (panel->dir.list[idx].st.st_mode)) {
1970 if (panel->dir.list[idx].f.dir_size_computed)
1971 panel->total -= panel->dir.list[idx].st.st_size;
1972 panel->dirs_marked--;
1973 } else
1974 panel->total -= panel->dir.list[idx].st.st_size;
1975 panel->marked--;
1979 static void
1980 do_mark_file (WPanel *panel, int do_move)
1982 do_file_mark (panel, panel->selected,
1983 selection (panel)->f.marked ? 0 : 1);
1984 if (mark_moves_down && do_move)
1985 move_down (panel);
1988 static void
1989 mark_file (WPanel *panel)
1991 do_mark_file (panel, 1);
1994 /* Incremental search of a file name in the panel */
1995 static void
1996 do_search (WPanel *panel, int c_code)
1998 size_t l, max, buf_max;
1999 int i, sel;
2000 int wrapped = 0;
2001 char *act;
2003 l = strlen (panel->search_buffer);
2004 if (c_code == KEY_BACKSPACE) {
2005 if (l != 0) {
2006 act = panel->search_buffer + l;
2007 str_prev_noncomb_char (&act, panel->search_buffer);
2008 act[0] = '\0';
2010 panel->search_chpoint = 0;
2011 } else {
2012 if (c_code && (gsize) panel->search_chpoint < sizeof (panel->search_char)) {
2013 panel->search_char[panel->search_chpoint] = c_code;
2014 panel->search_chpoint++;
2017 if (panel->search_chpoint > 0) {
2018 switch (str_is_valid_char (panel->search_char, panel->search_chpoint)) {
2019 case -2:
2020 return;
2021 case -1:
2022 panel->search_chpoint = 0;
2023 return;
2024 default:
2025 if (l + panel->search_chpoint < sizeof (panel->search_buffer)) {
2026 memcpy (panel->search_buffer + l, panel->search_char,
2027 panel->search_chpoint);
2028 l+= panel->search_chpoint;
2029 (panel->search_buffer + l)[0] = '\0';
2030 panel->search_chpoint = 0;
2036 buf_max = panel->case_sensitive ?
2037 str_prefix (panel->search_buffer, panel->search_buffer) :
2038 str_caseprefix (panel->search_buffer, panel->search_buffer);
2039 max = 0;
2040 sel = panel->selected;
2041 for (i = panel->selected; !wrapped || i != panel->selected; i++) {
2042 if (i >= panel->count) {
2043 i = 0;
2044 if (wrapped)
2045 break;
2046 wrapped = 1;
2048 l = panel->case_sensitive ?
2049 str_prefix (panel->dir.list[i].fname, panel->search_buffer) :
2050 str_caseprefix (panel->dir.list[i].fname, panel->search_buffer);
2051 if (l > max) {
2052 max = l;
2053 sel = i;
2054 if (max == buf_max) break;
2058 unselect_item (panel);
2059 panel->selected = sel;
2060 select_item (panel);
2062 act = panel->search_buffer + strlen (panel->search_buffer);
2063 while (max < buf_max) {
2064 str_prev_char_safe (&act);
2065 act[0] = '\0';
2066 buf_max = panel->case_sensitive ?
2067 str_prefix (panel->search_buffer, panel->search_buffer) :
2068 str_caseprefix (panel->search_buffer, panel->search_buffer);
2071 paint_panel (panel);
2074 static void
2075 start_search (WPanel *panel)
2077 if (panel->searching){
2078 if (panel->selected+1 == panel->count)
2079 panel->selected = 0;
2080 else
2081 move_down (panel);
2082 do_search (panel, 0);
2083 } else {
2084 panel->searching = 1;
2085 panel->search_buffer[0] = '\0';
2086 panel->search_char[0] = '\0';
2087 panel->search_chpoint = 0;
2088 display_mini_info (panel);
2089 mc_refresh ();
2093 /* Return 1 if the Enter key has been processed, 0 otherwise */
2094 static int
2095 do_enter_on_file_entry (file_entry *fe)
2097 char *full_name;
2100 * Directory or link to directory - change directory.
2101 * Try the same for the entries on which mc_lstat() has failed.
2103 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe)
2104 || (fe->st.st_mode == 0)) {
2105 if (!do_cd (fe->fname, cd_exact))
2106 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2107 return 1;
2110 /* Try associated command */
2111 if (regex_command (fe->fname, "Open", 0) != 0)
2112 return 1;
2114 /* Check if the file is executable */
2115 full_name = concat_dir_and_file (current_panel->cwd, fe->fname);
2116 if (!is_exe (fe->st.st_mode) || !if_link_is_exe (full_name, fe)) {
2117 g_free (full_name);
2118 return 0;
2120 g_free (full_name);
2122 if (confirm_execute) {
2123 if (query_dialog
2124 (_(" The Midnight Commander "),
2125 _(" Do you really want to execute? "), D_NORMAL, 2, _("&Yes"),
2126 _("&No")) != 0)
2127 return 1;
2129 #ifdef USE_VFS
2130 if (!vfs_current_is_local ()) {
2131 char *tmp;
2132 int ret;
2134 tmp = concat_dir_and_file (vfs_get_current_dir (), fe->fname);
2135 ret = mc_setctl (tmp, VFS_SETCTL_RUN, NULL);
2136 g_free (tmp);
2137 /* We took action only if the dialog was shown or the execution
2138 * was successful */
2139 return confirm_execute || (ret == 0);
2141 #endif
2144 char *tmp = name_quote (fe->fname, 0);
2145 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2146 g_free (tmp);
2147 shell_execute (cmd, 0);
2148 g_free (cmd);
2151 return 1;
2154 static int
2155 do_enter (WPanel *panel)
2157 return do_enter_on_file_entry (selection (panel));
2160 static void
2161 chdir_other_panel (WPanel *panel)
2163 char *new_dir;
2164 char *sel_entry = NULL;
2166 if (get_other_type () != view_listing) {
2167 set_display_type (get_other_index (), view_listing);
2170 if (!S_ISDIR (panel->dir.list [panel->selected].st.st_mode)) {
2171 new_dir = concat_dir_and_file (panel->cwd, "..");
2172 sel_entry = strrchr(panel->cwd, PATH_SEP);
2173 } else
2174 new_dir = concat_dir_and_file (panel->cwd, panel->dir.list [panel->selected].fname);
2176 change_panel ();
2177 do_cd (new_dir, cd_exact);
2178 if (sel_entry)
2179 try_to_select (current_panel, sel_entry);
2180 change_panel ();
2182 move_down (panel);
2184 g_free (new_dir);
2188 * Make the current directory of the current panel also the current
2189 * directory of the other panel. Put the other panel to the listing
2190 * mode if needed. If the current panel is panelized, the other panel
2191 * doesn't become panelized.
2193 static void
2194 sync_other_panel (WPanel *panel)
2196 if (get_other_type () != view_listing) {
2197 set_display_type (get_other_index (), view_listing);
2200 do_panel_cd (other_panel, current_panel->cwd, cd_exact);
2202 /* try to select current filename on the other panel */
2203 if (!panel->is_panelized) {
2204 try_to_select (other_panel, selection (panel)->fname);
2208 static void
2209 chdir_to_readlink (WPanel *panel)
2211 char *new_dir;
2213 if (get_other_type () != view_listing)
2214 return;
2216 if (S_ISLNK (panel->dir.list [panel->selected].st.st_mode)) {
2217 char buffer [MC_MAXPATHLEN], *p;
2218 int i;
2219 struct stat st;
2221 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2222 if (i < 0)
2223 return;
2224 if (mc_stat (selection (panel)->fname, &st) < 0)
2225 return;
2226 buffer [i] = 0;
2227 if (!S_ISDIR (st.st_mode)) {
2228 p = strrchr (buffer, PATH_SEP);
2229 if (p && !p[1]) {
2230 *p = 0;
2231 p = strrchr (buffer, PATH_SEP);
2233 if (!p)
2234 return;
2235 p[1] = 0;
2237 if (*buffer == PATH_SEP)
2238 new_dir = g_strdup (buffer);
2239 else
2240 new_dir = concat_dir_and_file (panel->cwd, buffer);
2242 change_panel ();
2243 do_cd (new_dir, cd_exact);
2244 change_panel ();
2246 move_down (panel);
2248 g_free (new_dir);
2252 typedef void (*panel_key_callback) (WPanel *);
2253 typedef struct {
2254 int key_code;
2255 panel_key_callback fn;
2256 } panel_key_map;
2258 static void cmd_do_enter(WPanel *wp) { (void) do_enter(wp); }
2259 static void cmd_view_simple(WPanel *wp) { (void) wp; view_simple_cmd(); }
2260 static void cmd_edit_new(WPanel *wp) { (void) wp; edit_cmd_new(); }
2261 static void cmd_copy_local(WPanel *wp) { (void) wp;copy_cmd_local(); }
2262 static void cmd_rename_local(WPanel *wp) { (void) wp;ren_cmd_local(); }
2263 static void cmd_delete_local(WPanel *wp) { (void) wp;delete_cmd_local(); }
2264 static void cmd_select(WPanel *wp) { (void) wp;select_cmd(); }
2265 static void cmd_unselect(WPanel *wp) { (void) wp;unselect_cmd(); }
2266 static void cmd_reverse_selection(WPanel *wp) { (void) wp;reverse_selection_cmd(); }
2268 static const panel_key_map panel_keymap [] = {
2269 { KEY_DOWN, move_down },
2270 { KEY_UP, move_up },
2272 /* The action button :-) */
2273 { '\n', cmd_do_enter },
2274 { KEY_ENTER, cmd_do_enter },
2276 { KEY_IC, mark_file },
2277 { KEY_HOME, move_home },
2278 { KEY_A1, move_home },
2279 { ALT ('<'), move_home },
2280 { KEY_C1, move_end },
2281 { KEY_END, move_end },
2282 { ALT ('>'), move_end },
2283 { KEY_NPAGE, next_page },
2284 { KEY_PPAGE, prev_page },
2285 { KEY_NPAGE | KEY_M_CTRL, ctrl_next_page },
2286 { KEY_PPAGE | KEY_M_CTRL, ctrl_prev_page },
2288 /* To quickly move in the panel */
2289 { ALT('g'), goto_top_file },
2290 { ALT('r'), goto_middle_file }, /* M-r like emacs */
2291 { ALT('j'), goto_bottom_file },
2293 /* Emacs-like bindings */
2294 { XCTRL('v'), next_page }, /* C-v like emacs */
2295 { ALT('v'), prev_page }, /* M-v like emacs */
2296 { XCTRL('p'), move_up }, /* C-p like emacs */
2297 { XCTRL('n'), move_down }, /* C-n like emacs */
2298 { XCTRL('s'), start_search }, /* C-s like emacs */
2299 { ALT('s'), start_search }, /* M-s not like emacs */
2300 /* { XCTRL('t'), mark_file },*/
2301 { XCTRL('t'), set_panel_encoding },
2302 { ALT('o'), chdir_other_panel },
2303 { ALT('i'), sync_other_panel },
2304 { ALT('l'), chdir_to_readlink },
2305 { ALT('H'), directory_history_list },
2306 { KEY_F(13), cmd_view_simple },
2307 { KEY_F(14), cmd_edit_new },
2308 { KEY_F(15), cmd_copy_local },
2309 { KEY_F(16), cmd_rename_local },
2310 { KEY_F(18), cmd_delete_local },
2311 { ALT('y'), directory_history_prev },
2312 { ALT('u'), directory_history_next },
2313 { ALT('+'), cmd_select },
2314 { KEY_KP_ADD, cmd_select },
2315 { ALT('\\'), cmd_unselect },
2316 { ALT('-'), cmd_unselect },
2317 { KEY_KP_SUBTRACT, cmd_unselect },
2318 { ALT('*'), cmd_reverse_selection },
2319 { KEY_KP_MULTIPLY, cmd_reverse_selection },
2320 { 0, 0 }
2323 static inline cb_ret_t
2324 panel_key (WPanel *panel, int key)
2326 int i;
2328 for (i = 0; panel_keymap[i].key_code; i++) {
2329 if (key == panel_keymap[i].key_code) {
2330 int old_searching = panel->searching;
2332 if (panel_keymap[i].fn != start_search)
2333 panel->searching = 0;
2335 (*panel_keymap[i].fn) (panel);
2337 if (panel->searching != old_searching)
2338 display_mini_info (panel);
2339 return MSG_HANDLED;
2342 if (torben_fj_mode && key == ALT ('h')) {
2343 goto_middle_file (panel);
2344 return MSG_HANDLED;
2347 /* We do not want to take a key press if nothing can be done with it */
2348 /* The command line widget may do something more useful */
2349 if (key == KEY_LEFT)
2350 return move_left (panel, key);
2352 if (key == KEY_RIGHT)
2353 return move_right (panel, key);
2355 if (is_abort_char (key)) {
2356 panel->searching = 0;
2357 display_mini_info (panel);
2358 return MSG_HANDLED;
2361 /* Do not eat characters not meant for the panel below ' ' (e.g. C-l). */
2362 if ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE) {
2363 if (panel->searching) {
2364 do_search (panel, key);
2365 return MSG_HANDLED;
2368 if (!command_prompt) {
2369 start_search (panel);
2370 do_search (panel, key);
2371 return MSG_HANDLED;
2375 return MSG_NOT_HANDLED;
2378 static cb_ret_t
2379 panel_callback (Widget *w, widget_msg_t msg, int parm)
2381 WPanel *panel = (WPanel *) w;
2382 Dlg_head *h = panel->widget.parent;
2384 switch (msg) {
2385 case WIDGET_DRAW:
2386 paint_panel (panel);
2387 return MSG_HANDLED;
2389 case WIDGET_FOCUS:
2390 current_panel = panel;
2391 panel->active = 1;
2392 if (mc_chdir (panel->cwd) != 0) {
2393 char *cwd = strip_password (g_strdup (panel->cwd), 1);
2394 message (D_ERROR, MSG_ERROR, _(" Cannot chdir to \"%s\" \n %s "),
2395 cwd, unix_error_string (errno));
2396 g_free(cwd);
2397 } else
2398 subshell_chdir (panel->cwd);
2400 update_xterm_title_path ();
2401 select_item (panel);
2402 show_dir (panel);
2403 paint_dir (panel);
2404 panel->dirty = 0;
2406 buttonbar_set_label (h, 1, _("Help"), help_cmd);
2407 buttonbar_set_label (h, 2, _("Menu"), user_file_menu_cmd);
2408 buttonbar_set_label (h, 3, _("View"), view_cmd);
2409 buttonbar_set_label (h, 4, _("Edit"), edit_cmd);
2410 buttonbar_set_label (h, 5, _("Copy"), copy_cmd);
2411 buttonbar_set_label (h, 6, _("RenMov"), ren_cmd);
2412 buttonbar_set_label (h, 7, _("Mkdir"), mkdir_cmd);
2413 buttonbar_set_label (h, 8, _("Delete"), delete_cmd);
2414 buttonbar_redraw (h);
2415 return MSG_HANDLED;
2417 case WIDGET_UNFOCUS:
2418 /* Janne: look at this for the multiple panel options */
2419 if (panel->searching){
2420 panel->searching = 0;
2421 display_mini_info (panel);
2423 panel->active = 0;
2424 show_dir (panel);
2425 unselect_item (panel);
2426 return MSG_HANDLED;
2428 case WIDGET_KEY:
2429 return panel_key (panel, parm);
2431 case WIDGET_DESTROY:
2432 panel_destroy (panel);
2433 return MSG_HANDLED;
2435 default:
2436 return default_proc (msg, parm);
2440 void
2441 file_mark (WPanel *panel, int index, int val)
2443 if (panel->dir.list[index].f.marked != val) {
2444 panel->dir.list[index].f.marked = val;
2445 panel->dirty = 1;
2449 /* */
2450 /* Panel mouse events support routines */
2451 /* */
2452 static int mouse_marking = 0;
2454 static void
2455 mouse_toggle_mark (WPanel *panel)
2457 do_mark_file (panel, 0);
2458 mouse_marking = selection (panel)->f.marked;
2461 static void
2462 mouse_set_mark (WPanel *panel)
2464 if (mouse_marking && !(selection (panel)->f.marked))
2465 do_mark_file (panel, 0);
2466 else if (!mouse_marking && (selection (panel)->f.marked))
2467 do_mark_file (panel, 0);
2470 static inline int
2471 mark_if_marking (WPanel *panel, Gpm_Event *event)
2473 if (event->buttons & GPM_B_RIGHT){
2474 if (event->type & GPM_DOWN)
2475 mouse_toggle_mark (panel);
2476 else
2477 mouse_set_mark (panel);
2478 return 1;
2480 return 0;
2484 * Mouse callback of the panel minus repainting.
2485 * If the event is redirected to the menu, *redir is set to 1.
2487 static int
2488 do_panel_event (Gpm_Event *event, WPanel *panel, int *redir)
2490 const int lines = llines (panel);
2492 int my_index;
2494 /* Mouse wheel events */
2495 if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN)) {
2496 prev_page (panel);
2497 return MOU_NORMAL;
2499 if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN)) {
2500 next_page (panel);
2501 return MOU_NORMAL;
2504 /* "<" button */
2505 if (event->type & GPM_DOWN && event->x == 2 && event->y == 1) {
2506 directory_history_prev (panel);
2507 return MOU_NORMAL;
2510 /* ">" button */
2511 if (event->type & GPM_DOWN && event->x == panel->widget.cols - 1
2512 && event->y == 1) {
2513 directory_history_next (panel);
2514 return MOU_NORMAL;
2517 /* "v" button */
2518 if (event->type & GPM_DOWN && event->x == panel->widget.cols - 2
2519 && event->y == 1) {
2520 directory_history_list (panel);
2521 return MOU_NORMAL;
2524 /* rest of the upper frame, the menu is invisible - call menu */
2525 if (event->type & GPM_DOWN && event->y == 1 && !menubar_visible) {
2526 *redir = 1;
2527 event->x += panel->widget.x;
2528 return (*(the_menubar->widget.mouse)) (event, the_menubar);
2531 event->y -= 2;
2532 if ((event->type & (GPM_DOWN | GPM_DRAG))) {
2534 if (!dlg_widget_active (panel))
2535 change_panel ();
2537 if (event->y <= 0) {
2538 mark_if_marking (panel, event);
2539 if (mouse_move_pages)
2540 prev_page (panel);
2541 else
2542 move_up (panel);
2543 return MOU_REPEAT;
2546 if (!((panel->top_file + event->y <= panel->count)
2547 && event->y <= lines)) {
2548 mark_if_marking (panel, event);
2549 if (mouse_move_pages)
2550 next_page (panel);
2551 else
2552 move_down (panel);
2553 return MOU_REPEAT;
2555 my_index = panel->top_file + event->y - 1;
2556 if (panel->split) {
2557 if (event->x > ((panel->widget.cols - 2) / 2))
2558 my_index += llines (panel);
2561 if (my_index >= panel->count)
2562 my_index = panel->count - 1;
2564 if (my_index != panel->selected) {
2565 unselect_item (panel);
2566 panel->selected = my_index;
2567 select_item (panel);
2570 /* This one is new */
2571 mark_if_marking (panel, event);
2573 } else if ((event->type & (GPM_UP | GPM_DOUBLE)) ==
2574 (GPM_UP | GPM_DOUBLE)) {
2575 if (event->y > 0 && event->y <= lines)
2576 do_enter (panel);
2578 return MOU_NORMAL;
2581 /* Mouse callback of the panel */
2582 static int
2583 panel_event (Gpm_Event *event, void *data)
2585 WPanel *panel = data;
2586 int ret;
2587 int redir = 0;
2589 ret = do_panel_event (event, panel, &redir);
2590 if (!redir)
2591 paint_panel (panel);
2593 return ret;
2596 void
2597 panel_re_sort (WPanel *panel)
2599 char *filename;
2600 int i;
2602 if (panel == NULL)
2603 return;
2605 filename = g_strdup (selection (panel)->fname);
2606 unselect_item (panel);
2607 do_sort (&panel->dir, panel->sort_type, panel->count-1, panel->reverse,
2608 panel->case_sensitive, panel->exec_first);
2609 panel->selected = -1;
2610 for (i = panel->count; i; i--){
2611 if (!strcmp (panel->dir.list [i-1].fname, filename)){
2612 panel->selected = i-1;
2613 break;
2616 g_free (filename);
2617 panel->top_file = panel->selected - ITEMS (panel)/2;
2618 if (panel->top_file < 0)
2619 panel->top_file = 0;
2620 select_item (panel);
2621 panel->dirty = 1;
2624 void
2625 panel_set_sort_order (WPanel *panel, sortfn *sort_order)
2627 if (sort_order == 0)
2628 return;
2630 panel->sort_type = sort_order;
2632 /* The directory is already sorted, we have to load the unsorted stuff */
2633 if (sort_order == (sortfn *) unsorted){
2634 char *current_file;
2636 current_file = g_strdup (panel->dir.list [panel->selected].fname);
2637 panel_reload (panel);
2638 try_to_select (panel, current_file);
2639 g_free (current_file);
2641 panel_re_sort (panel);
2644 void
2645 set_panel_encoding (WPanel *panel)
2647 char *encoding = NULL;
2648 char *cd_path;
2649 #ifdef HAVE_CHARSET
2650 const char *errmsg;
2651 int r;
2652 int width = (panel->widget.x)? panel->widget.cols : panel->widget.cols * (-1);
2654 r = select_charset (width, 0, source_codepage, 0);
2656 if ( r == -2)
2657 return;
2660 if (r == -1){
2661 errmsg = init_translation_table (display_codepage, display_codepage);
2662 cd_path = remove_encoding_from_path (panel->cwd);
2663 do_panel_cd (panel, cd_path, 0);
2664 g_free (cd_path);
2665 return;
2668 source_codepage = r;
2670 errmsg = init_translation_table (source_codepage, display_codepage);
2671 if (errmsg) {
2672 message (D_ERROR, MSG_ERROR, "%s", errmsg);
2673 return;
2675 encoding = g_strdup( get_codepage_id ( source_codepage ) );
2676 #endif
2677 if (encoding) {
2678 cd_path = add_encoding_to_path (panel->cwd, encoding);
2679 if (!do_panel_cd (panel, cd_path, 0))
2680 message (1, MSG_ERROR, _(" Cannot chdir to %s "), cd_path);
2681 g_free (cd_path);
2682 g_free (encoding);