Merge branch '54_dlg_mouse_handling'
[midnight-commander.git] / src / screen.c
blob132916e24cee2830800658dc25b3304c5805e068
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"
63 #include "cmddef.h" /* CK_ cmd name const */
64 #include "keybind.h" /* global_key_map_t */
66 #define ELEMENTS(arr) ( sizeof(arr) / sizeof((arr)[0]) )
68 #define NORMAL 0
69 #define SELECTED 1
70 #define MARKED 2
71 #define MARKED_SELECTED 3
72 #define STATUS 5
75 * This describes a format item. The parse_display_format routine parses
76 * the user specified format and creates a linked list of format_e structures.
78 typedef struct format_e {
79 struct format_e *next;
80 int requested_field_len;
81 int field_len;
82 align_crt_t just_mode;
83 int expand;
84 const char *(*string_fn)(file_entry *, int len);
85 const char *title;
86 const char *id;
87 } format_e;
89 /* If true, show the mini-info on the panel */
90 int show_mini_info = 1;
92 /* If true, then use stat() on the cwd to determine directory changes */
93 int fast_reload = 0;
95 /* If true, use some usability hacks by Torben */
96 int torben_fj_mode = 0;
98 /* If true, up/down keys scroll the pane listing by pages */
99 int panel_scroll_pages = 1;
101 /* If 1, we use permission hilighting */
102 int permission_mode = 0;
104 /* If 1 - then add per file type hilighting */
105 int filetype_mode = 1;
107 /* The hook list for the select file function */
108 Hook *select_file_hook = 0;
110 const global_key_map_t *panel_map;
112 static cb_ret_t panel_callback (Widget *, widget_msg_t msg, int parm);
113 static int panel_event (Gpm_Event *event, void *);
114 static void paint_frame (WPanel *panel);
115 static const char *panel_format (WPanel *panel);
116 static const char *mini_status_format (WPanel *panel);
118 /* This macro extracts the number of available lines in a panel */
119 #define llines(p) (p->widget.lines-3 - (show_mini_info ? 2 : 0))
121 static void
122 set_colors (WPanel *panel)
124 (void) panel;
125 tty_set_normal_attrs ();
126 tty_setcolor (NORMAL_COLOR);
129 /* Delete format string, it is a linked list */
130 static void
131 delete_format (format_e *format)
133 format_e *next;
135 while (format){
136 next = format->next;
137 g_free (format);
138 format = next;
142 /* This code relies on the default justification!!! */
143 static void
144 add_permission_string (char *dest, int width, file_entry *fe, int attr, int color, int is_octal)
146 int i, r, l;
148 l = get_user_permissions (&fe->st);
150 if (is_octal){
151 /* Place of the access bit in octal mode */
152 l = width + l - 3;
153 r = l + 1;
154 } else {
155 /* The same to the triplet in string mode */
156 l = l * 3 + 1;
157 r = l + 3;
160 for(i = 0; i < width; i++){
161 if (i >= l && i < r){
162 if (attr == SELECTED || attr == MARKED_SELECTED)
163 tty_setcolor (MARKED_SELECTED_COLOR);
164 else
165 tty_setcolor (MARKED_COLOR);
166 } else if (color >= 0)
167 tty_setcolor (color);
168 else
169 tty_lowlevel_setcolor (-color);
171 tty_print_char (dest[i]);
175 /* String representations of various file attributes */
176 /* name */
177 static const char *
178 string_file_name (file_entry *fe, int len)
180 static char buffer [MC_MAXPATHLEN * MB_LEN_MAX + 1];
182 (void) len;
183 g_strlcpy (buffer, fe->fname, sizeof(buffer));
184 return buffer;
187 static unsigned int ilog10(dev_t n)
189 unsigned int digits = 0;
190 do {
191 digits++, n /= 10;
192 } while (n != 0);
193 return digits;
196 static void format_device_number (char *buf, size_t bufsize, dev_t dev)
198 dev_t major_dev = major(dev);
199 dev_t minor_dev = minor(dev);
200 unsigned int major_digits = ilog10(major_dev);
201 unsigned int minor_digits = ilog10(minor_dev);
203 g_assert(bufsize >= 1);
204 if (major_digits + 1 + minor_digits + 1 <= bufsize) {
205 g_snprintf(buf, bufsize, "%lu,%lu", (unsigned long) major_dev,
206 (unsigned long) minor_dev);
207 } else {
208 g_strlcpy(buf, _("[dev]"), bufsize);
212 /* size */
213 static const char *
214 string_file_size (file_entry *fe, int len)
216 static char buffer [BUF_TINY];
218 /* Don't ever show size of ".." since we don't calculate it */
219 if (!strcmp (fe->fname, "..")) {
220 return _("UP--DIR");
223 #ifdef HAVE_STRUCT_STAT_ST_RDEV
224 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
225 format_device_number (buffer, len + 1, fe->st.st_rdev);
226 else
227 #endif
229 size_trunc_len (buffer, (unsigned int) len, fe->st.st_size, 0);
231 return buffer;
234 /* bsize */
235 static const char *
236 string_file_size_brief (file_entry *fe, int len)
238 if (S_ISLNK (fe->st.st_mode) && !fe->f.link_to_dir) {
239 return _("SYMLINK");
242 if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && strcmp (fe->fname, "..")) {
243 return _("SUB-DIR");
246 return string_file_size (fe, len);
249 /* This functions return a string representation of a file entry */
250 /* type */
251 static const char *
252 string_file_type (file_entry *fe, int len)
254 static char buffer[2];
256 (void) len;
257 if (S_ISDIR (fe->st.st_mode))
258 buffer[0] = PATH_SEP;
259 else if (S_ISLNK (fe->st.st_mode)) {
260 if (fe->f.link_to_dir)
261 buffer[0] = '~';
262 else if (fe->f.stale_link)
263 buffer[0] = '!';
264 else
265 buffer[0] = '@';
266 } else if (S_ISCHR (fe->st.st_mode))
267 buffer[0] = '-';
268 else if (S_ISSOCK (fe->st.st_mode))
269 buffer[0] = '=';
270 else if (S_ISDOOR (fe->st.st_mode))
271 buffer[0] = '>';
272 else if (S_ISBLK (fe->st.st_mode))
273 buffer[0] = '+';
274 else if (S_ISFIFO (fe->st.st_mode))
275 buffer[0] = '|';
276 else if (S_ISNAM (fe->st.st_mode))
277 buffer[0] = '#';
278 else if (!S_ISREG (fe->st.st_mode))
279 buffer[0] = '?'; /* non-regular of unknown kind */
280 else if (is_exe (fe->st.st_mode))
281 buffer[0] = '*';
282 else
283 buffer[0] = ' ';
284 buffer[1] = '\0';
285 return buffer;
288 /* mtime */
289 static const char *
290 string_file_mtime (file_entry *fe, int len)
292 (void) len;
293 if (!strcmp (fe->fname, "..")) {
294 return "";
296 return file_date (fe->st.st_mtime);
299 /* atime */
300 static const char *
301 string_file_atime (file_entry *fe, int len)
303 (void) len;
304 if (!strcmp (fe->fname, "..")) {
305 return "";
307 return file_date (fe->st.st_atime);
310 /* ctime */
311 static const char *
312 string_file_ctime (file_entry *fe, int len)
314 (void) len;
315 if (!strcmp (fe->fname, "..")) {
316 return "";
318 return file_date (fe->st.st_ctime);
321 /* perm */
322 static const char *
323 string_file_permission (file_entry *fe, int len)
325 (void) len;
326 return string_perm (fe->st.st_mode);
329 /* mode */
330 static const char *
331 string_file_perm_octal (file_entry *fe, int len)
333 static char buffer [10];
335 (void) len;
336 g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode);
337 return buffer;
340 /* nlink */
341 static const char *
342 string_file_nlinks (file_entry *fe, int len)
344 static char buffer[BUF_TINY];
346 (void) len;
347 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
348 return buffer;
351 /* inode */
352 static const char *
353 string_inode (file_entry *fe, int len)
355 static char buffer [10];
357 (void) len;
358 g_snprintf (buffer, sizeof (buffer), "%lu",
359 (unsigned long) fe->st.st_ino);
360 return buffer;
363 /* nuid */
364 static const char *
365 string_file_nuid (file_entry *fe, int len)
367 static char buffer [10];
369 (void) len;
370 g_snprintf (buffer, sizeof (buffer), "%lu",
371 (unsigned long) fe->st.st_uid);
372 return buffer;
375 /* ngid */
376 static const char *
377 string_file_ngid (file_entry *fe, int len)
379 static char buffer [10];
381 (void) len;
382 g_snprintf (buffer, sizeof (buffer), "%lu",
383 (unsigned long) fe->st.st_gid);
384 return buffer;
387 /* owner */
388 static const char *
389 string_file_owner (file_entry *fe, int len)
391 (void) len;
392 return get_owner (fe->st.st_uid);
395 /* group */
396 static const char *
397 string_file_group (file_entry *fe, int len)
399 (void) len;
400 return get_group (fe->st.st_gid);
403 /* mark */
404 static const char *
405 string_marked (file_entry *fe, int len)
407 (void) len;
408 return fe->f.marked ? "*" : " ";
411 /* space */
412 static const char *
413 string_space (file_entry *fe, int len)
415 (void) fe;
416 (void) len;
417 return " ";
420 /* dot */
421 static const char *
422 string_dot (file_entry *fe, int len)
424 (void) fe;
425 (void) len;
426 return ".";
429 #define GT 1
431 static struct {
432 const char *id;
433 int min_size;
434 int expands;
435 align_crt_t default_just;
436 const char *title;
437 int use_in_gui;
438 const char *(*string_fn)(file_entry *, int);
439 sortfn *sort_routine; /* This field is currently unused. */
440 } formats [] = {
441 { "name", 12, 1, J_LEFT_FIT, N_("Name"), 1, string_file_name, (sortfn *) sort_name },
442 { "size", 7, 0, J_RIGHT, N_("Size"), 1, string_file_size, (sortfn *) sort_size },
443 { "bsize", 7, 0, J_RIGHT, N_("Size"), 1, string_file_size_brief, (sortfn *) sort_size },
444 { "type", GT, 0, J_LEFT, "", 2, string_file_type, NULL },
445 { "mtime", 12, 0, J_RIGHT, N_("MTime"), 1, string_file_mtime, (sortfn *) sort_time },
446 { "atime", 12, 0, J_RIGHT, N_("ATime"), 1, string_file_atime, (sortfn *) sort_atime },
447 { "ctime", 12, 0, J_RIGHT, N_("CTime"), 1, string_file_ctime, (sortfn *) sort_ctime },
448 { "perm", 10, 0, J_LEFT, N_("Permission"),1,string_file_permission, NULL },
449 { "mode", 6, 0, J_RIGHT, N_("Perm"), 1, string_file_perm_octal, NULL },
450 { "nlink", 2, 0, J_RIGHT, N_("Nl"), 1, string_file_nlinks, NULL },
451 { "inode", 5, 0, J_RIGHT, N_("Inode"), 1, string_inode, (sortfn *) sort_inode },
452 { "nuid", 5, 0, J_RIGHT, N_("UID"), 1, string_file_nuid, NULL },
453 { "ngid", 5, 0, J_RIGHT, N_("GID"), 1, string_file_ngid, NULL },
454 { "owner", 8, 0, J_LEFT_FIT, N_("Owner"), 1, string_file_owner, NULL },
455 { "group", 8, 0, J_LEFT_FIT, N_("Group"), 1, string_file_group, NULL },
456 { "mark", 1, 0, J_RIGHT, " ", 1, string_marked, NULL },
457 { "|", 1, 0, J_RIGHT, " ", 0, NULL, NULL },
458 { "space", 1, 0, J_RIGHT, " ", 0, string_space, NULL },
459 { "dot", 1, 0, J_RIGHT, " ", 0, string_dot, NULL },
462 static int
463 file_compute_color (int attr, file_entry *fe)
465 switch (attr) {
466 case SELECTED:
467 return (SELECTED_COLOR);
468 case MARKED:
469 return (MARKED_COLOR);
470 case MARKED_SELECTED:
471 return (MARKED_SELECTED_COLOR);
472 case STATUS:
473 return (NORMAL_COLOR);
474 case NORMAL:
475 default:
476 if (!filetype_mode)
477 return (NORMAL_COLOR);
480 return mc_fhl_get_color (mc_filehighlight, fe);
483 /* Formats the file number file_index of panel in the buffer dest */
484 static void
485 format_file (char *dest, int limit, WPanel *panel, int file_index, int width, int attr, int isstatus)
487 int color, length, empty_line;
488 const char *txt;
489 format_e *format, *home;
490 file_entry *fe;
492 (void) dest;
493 (void) limit;
494 length = 0;
495 empty_line = (file_index >= panel->count);
496 home = (isstatus) ? panel->status_format : panel->format;
497 fe = &panel->dir.list [file_index];
499 if (!empty_line)
500 color = file_compute_color (attr, fe);
501 else
502 color = NORMAL_COLOR;
504 for (format = home; format; format = format->next){
505 if (length == width)
506 break;
508 if (format->string_fn){
509 int len, perm;
510 char *preperad_text;
512 if (empty_line)
513 txt = " ";
514 else
515 txt = (*format->string_fn)(fe, format->field_len);
517 len = format->field_len;
518 if (len + length > width)
519 len = width - length;
520 if (len <= 0)
521 break;
523 perm = 0;
524 if (permission_mode) {
525 if (!strcmp(format->id, "perm"))
526 perm = 1;
527 else if (!strcmp(format->id, "mode"))
528 perm = 2;
531 if (color >= 0)
532 tty_setcolor (color);
533 else
534 tty_lowlevel_setcolor (-color);
536 preperad_text = (char*) str_fit_to_term(txt, len, format->just_mode);
537 if (perm)
538 add_permission_string (preperad_text, format->field_len, fe,
539 attr, color, perm - 1);
540 else
541 tty_print_string (preperad_text);
543 length+= len;
544 } else {
545 if (attr == SELECTED || attr == MARKED_SELECTED)
546 tty_setcolor (SELECTED_COLOR);
547 else
548 tty_setcolor (NORMAL_COLOR);
549 tty_print_one_vline ();
550 length++;
554 if (length < width)
555 tty_draw_hline (-1, -1, ' ', width - length);
558 static void
559 repaint_file (WPanel *panel, int file_index, int mv, int attr, int isstatus)
561 int second_column = 0;
562 int width;
563 int offset = 0;
564 char buffer [BUF_MEDIUM];
566 gboolean panel_is_split = !isstatus && panel->split;
568 width = panel->widget.cols - 2;
570 if (panel_is_split) {
571 second_column = (file_index - panel->top_file) / llines (panel);
572 width = width/2 - 1;
574 if (second_column != 0) {
575 offset = 1 + width;
576 /*width = (panel->widget.cols-2) - (panel->widget.cols-2)/2 - 1;*/
577 width = panel->widget.cols - offset - 2;
581 /* Nothing to paint */
582 if (width <= 0)
583 return;
585 if (mv){
586 if (panel_is_split)
587 widget_move (&panel->widget,
588 (file_index - panel->top_file) % llines (panel) + 2,
589 offset + 1);
590 else
591 widget_move (&panel->widget, file_index - panel->top_file + 2, 1);
594 format_file (buffer, sizeof(buffer), panel, file_index, width, attr, isstatus);
596 if (panel_is_split) {
597 if (second_column)
598 tty_print_char (' ');
599 else {
600 tty_setcolor (NORMAL_COLOR);
601 tty_print_one_vline ();
606 static void
607 display_mini_info (WPanel *panel)
609 widget_move (&panel->widget, llines (panel)+3, 1);
611 if (panel->searching){
612 tty_setcolor (INPUT_COLOR);
613 tty_print_char ('/');
614 tty_print_string (str_fit_to_term (panel->search_buffer,
615 panel->widget.cols - 3, J_LEFT));
616 return;
619 /* Status resolves links and show them */
620 set_colors (panel);
622 if (S_ISLNK (panel->dir.list [panel->selected].st.st_mode)){
623 char *link, link_target [MC_MAXPATHLEN];
624 int len;
626 link = concat_dir_and_file (panel->cwd, panel->dir.list [panel->selected].fname);
627 len = mc_readlink (link, link_target, MC_MAXPATHLEN - 1);
628 g_free (link);
629 if (len > 0){
630 link_target[len] = 0;
631 tty_print_string ("-> ");
632 tty_print_string (str_fit_to_term (link_target, panel->widget.cols - 5,
633 J_LEFT_FIT));
634 } else
635 tty_print_string (str_fit_to_term (_("<readlink failed>"),
636 panel->widget.cols - 2, J_LEFT));
637 } else if (strcmp (panel->dir.list [panel->selected].fname, "..") == 0) {
638 /* FIXME:
639 * while loading directory (do_load_dir() and do_reload_dir()),
640 * the actual stat info about ".." directory isn't got;
641 * so just don't display incorrect info about ".." directory */
642 tty_print_string (str_fit_to_term (_("UP--DIR"), panel->widget.cols - 2, J_LEFT));
643 } else
644 /* Default behavior */
645 repaint_file (panel, panel->selected, 0, STATUS, 1);
648 static void
649 paint_dir (WPanel *panel)
651 int i;
652 int color; /* Color value of the line */
653 int items; /* Number of items */
655 items = llines (panel) * (panel->split ? 2 : 1);
657 for (i = 0; i < items; i++){
658 if (i+panel->top_file >= panel->count)
659 color = 0;
660 else {
661 color = 2 * (panel->dir.list [i+panel->top_file].f.marked);
662 color += (panel->selected==i+panel->top_file && panel->active);
664 repaint_file (panel, i+panel->top_file, 1, color, 0);
666 tty_set_normal_attrs ();
669 static void
670 display_total_marked_size (WPanel *panel, int y, int x, gboolean size_only)
672 char buffer[BUF_SMALL], b_bytes[BUF_SMALL], *buf;
673 int cols;
675 if (panel->marked <= 0)
676 return;
678 buf = size_only ? b_bytes : buffer;
679 cols = panel->widget.cols - 2;
682 * This is a trick to use two ngettext() calls in one sentence.
683 * First make "N bytes", then insert it into "X in M files".
685 g_snprintf (b_bytes, sizeof (b_bytes),
686 ngettext("%s byte", "%s bytes", (unsigned long) panel->total),
687 size_trunc_sep (panel->total));
688 if (!size_only)
689 g_snprintf (buffer, sizeof (buffer),
690 ngettext("%s in %d file", "%s in %d files", panel->marked),
691 b_bytes, panel->marked);
693 /* don't forget spaces around buffer content */
694 buf = (char *) str_trunc (buf, cols - 4);
696 if (x < 0)
697 /* center in panel */
698 x = (panel->widget.cols - str_term_width1 (buf)) / 2 - 1;
701 * y == llines (panel) + 2 for mini_info_separator
702 * y == panel->widget.lines - 1 for panel bottom frame
704 widget_move (&panel->widget, y, x);
705 tty_setcolor (MARKED_COLOR);
706 tty_printf (" %s ", buf);
709 static void
710 mini_info_separator (WPanel *panel)
712 const int y = llines (panel) + 2;
714 tty_setcolor (NORMAL_COLOR);
715 tty_draw_hline (panel->widget.y + y, panel->widget.x + 1,
716 ACS_HLINE, panel->widget.cols - 2);
717 /* Status displays total marked size.
718 * Centered in panel, full format. */
719 display_total_marked_size (panel, y, -1, FALSE);
722 static void
723 show_free_space (WPanel *panel)
725 /* Used to figure out how many free space we have */
726 static struct my_statfs myfs_stats;
727 /* Old current working directory for displaying free space */
728 static char *old_cwd = NULL;
730 /* Don't try to stat non-local fs */
731 if (!vfs_file_is_local (panel->cwd) || !free_space)
732 return;
734 if (old_cwd == NULL || strcmp (old_cwd, panel->cwd) != 0) {
735 char rpath[PATH_MAX];
737 init_my_statfs ();
738 g_free (old_cwd);
739 old_cwd = g_strdup (panel->cwd);
741 if (mc_realpath (panel->cwd, rpath) == NULL)
742 return;
744 my_statfs (&myfs_stats, rpath);
747 if (myfs_stats.avail > 0 || myfs_stats.total > 0) {
748 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
749 size_trunc_len (buffer1, sizeof(buffer1) - 1, myfs_stats.avail, 1);
750 size_trunc_len (buffer2, sizeof(buffer2) - 1, myfs_stats.total, 1);
751 g_snprintf (tmp, sizeof(tmp), " %s/%s (%d%%) ", buffer1, buffer2,
752 myfs_stats.total > 0 ?
753 (int)(100 * (double)myfs_stats.avail / myfs_stats.total) : 0);
754 widget_move (&panel->widget, panel->widget.lines - 1,
755 panel->widget.cols - 2 - (int) strlen (tmp));
756 tty_setcolor (NORMAL_COLOR);
757 tty_print_string (tmp);
761 static void
762 show_dir (WPanel *panel)
764 set_colors (panel);
765 draw_box (panel->widget.parent,
766 panel->widget.y, panel->widget.x,
767 panel->widget.lines, panel->widget.cols);
769 if (show_mini_info) {
770 widget_move (&panel->widget, llines (panel) + 2, 0);
771 tty_print_alt_char (ACS_LTEE);
772 widget_move (&panel->widget, llines (panel) + 2,
773 panel->widget.cols - 1);
774 tty_print_alt_char (ACS_RTEE);
777 if (panel->active)
778 tty_setcolor (REVERSE_COLOR);
780 widget_move (&panel->widget, 0, 3);
782 tty_printf (" %s ",
783 str_term_trim (strip_home_and_password (panel->cwd),
784 min (max (panel->widget.cols - 9, 0),
785 panel->widget.cols)));
787 widget_move (&panel->widget, 0, 1);
788 tty_print_string ("<");
789 widget_move (&panel->widget, 0, panel->widget.cols - 2);
790 tty_print_string (">");
791 widget_move (&panel->widget, 0, panel->widget.cols - 3);
792 tty_print_string ("v");
794 if (!show_mini_info) {
795 if (panel->marked == 0) {
796 /* Show size of curret file in the bottom of panel */
797 if (S_ISREG (panel->dir.list [panel->selected].st.st_mode)) {
798 char buffer[BUF_SMALL];
800 g_snprintf (buffer, sizeof (buffer), " %s ",
801 size_trunc_sep (panel->dir.list [panel->selected].st.st_size));
802 tty_setcolor (NORMAL_COLOR);
803 widget_move (&panel->widget, panel->widget.lines - 1, 2);
804 tty_print_string (buffer);
806 } else {
807 /* Show total size of marked files
808 * In the bottom of panel, display size only. */
809 display_total_marked_size (panel, panel->widget.lines - 1, 2, TRUE);
813 show_free_space (panel);
815 if (panel->active)
816 tty_set_normal_attrs ();
819 /* To be used only by long_frame and full_frame to adjust top_file */
820 static void
821 adjust_top_file (WPanel *panel)
823 int old_top = panel->top_file;
825 if (panel->selected - old_top > llines (panel))
826 panel->top_file = panel->selected;
827 if (old_top - panel->count > llines (panel))
828 panel->top_file = panel->count - llines (panel);
831 /* Repaint everything, including frame and separator */
832 static void
833 paint_panel (WPanel *panel)
835 paint_frame (panel); /* including show_dir */
836 paint_dir (panel);
838 if (show_mini_info) {
839 mini_info_separator (panel);
840 display_mini_info (panel);
843 panel->dirty = 0;
846 /* add "#enc:encodning" to end of path */
847 /* if path end width a previous #enc:, only encoding is changed no additional
848 * #enc: is appended
849 * retun new string
851 static char
852 *add_encoding_to_path (const char *path, const char *encoding)
854 char *result;
855 char *semi;
856 char *slash;
858 semi = g_strrstr (path, "#enc:");
860 if (semi != NULL) {
861 slash = strchr (semi, PATH_SEP);
862 if (slash != NULL) {
863 result = g_strconcat (path, "/#enc:", encoding, NULL);
864 } else {
865 *semi = 0;
866 result = g_strconcat (path, "/#enc:", encoding, NULL);
867 *semi = '#';
869 } else {
870 result = g_strconcat (path, "/#enc:", encoding, NULL);
873 return result;
876 char *
877 remove_encoding_from_path (const char *path)
879 GString *ret;
880 GString *tmp_path, *tmp_conv;
881 char *tmp, *tmp2;
882 const char *enc;
883 GIConv converter;
885 ret = g_string_new("");
886 tmp_conv = g_string_new("");
888 tmp_path = g_string_new(path);
890 while ((tmp = g_strrstr (tmp_path->str, "/#enc:")) != NULL){
891 enc = vfs_get_encoding ((const char *) tmp);
892 converter = enc ? str_crt_conv_to (enc): str_cnv_to_term;
893 if (converter == INVALID_CONV) converter = str_cnv_to_term;
895 tmp2=tmp+1;
896 while (*tmp2 && *tmp2 != '/')
897 tmp2++;
899 if (*tmp2){
900 str_vfs_convert_from (converter, tmp2, tmp_conv);
901 g_string_prepend(ret, tmp_conv->str);
902 g_string_set_size(tmp_conv,0);
904 g_string_set_size(tmp_path,tmp - tmp_path->str);
905 str_close_conv (converter);
907 g_string_prepend(ret, tmp_path->str);
908 g_string_free(tmp_path,TRUE);
909 g_string_free(tmp_conv,TRUE);
911 tmp = ret->str;
912 g_string_free(ret, FALSE);
913 return tmp;
917 * Repaint the contents of the panels without frames. To schedule panel
918 * for repainting, set panel->dirty to 1. There are many reasons why
919 * the panels need to be repainted, and this is a costly operation, so
920 * it's done once per event.
922 void
923 update_dirty_panels (void)
925 if (current_panel->dirty)
926 paint_panel (current_panel);
928 if ((get_other_type () == view_listing) && other_panel->dirty)
929 paint_panel (other_panel);
932 static void
933 do_select (WPanel *panel, int i)
935 if (i != panel->selected) {
936 panel->dirty = 1;
937 panel->selected = i;
938 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
939 if (panel->top_file < 0)
940 panel->top_file = 0;
944 static void
945 do_try_to_select (WPanel *panel, const char *name)
947 int i;
948 char *subdir;
950 if (!name) {
951 do_select(panel, 0);
952 return;
955 /* We only want the last component of the directory,
956 * and from this only the name without suffix. */
957 subdir = vfs_strip_suffix_from_filename (x_basename(name));
959 /* Search that subdirectory, if found select it */
960 for (i = 0; i < panel->count; i++){
961 if (strcmp (subdir, panel->dir.list [i].fname) == 0) {
962 do_select (panel, i);
963 g_free (subdir);
964 return;
968 /* Try to select a file near the file that is missing */
969 if (panel->selected >= panel->count)
970 do_select (panel, panel->count-1);
971 g_free (subdir);
974 void
975 try_to_select (WPanel *panel, const char *name)
977 do_try_to_select (panel, name);
978 select_item (panel);
981 void
982 panel_update_cols (Widget *widget, int frame_size)
984 int cols, origin;
986 if (horizontal_split){
987 widget->cols = COLS;
988 return;
991 if (frame_size == frame_full){
992 cols = COLS;
993 origin = 0;
994 } else {
995 if (widget == get_panel_widget (0)){
996 cols = first_panel_size;
997 origin = 0;
998 } else {
999 cols = COLS-first_panel_size;
1000 origin = first_panel_size;
1004 widget->cols = cols;
1005 widget->x = origin;
1008 static char *
1009 panel_save_name (WPanel *panel)
1011 extern int saving_setup;
1013 /* If the program is shuting down */
1014 if ((midnight_shutdown && auto_save_setup) || saving_setup)
1015 return g_strdup (panel->panel_name);
1016 else
1017 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1020 void
1021 panel_clean_dir (WPanel *panel)
1023 int count = panel->count;
1025 panel->count = 0;
1026 panel->top_file = 0;
1027 panel->selected = 0;
1028 panel->marked = 0;
1029 panel->dirs_marked = 0;
1030 panel->total = 0;
1031 panel->searching = 0;
1032 panel->is_panelized = 0;
1033 panel->dirty = 1;
1035 clean_dir (&panel->dir, count);
1038 static void
1039 panel_destroy (WPanel *p)
1041 int i;
1043 char *name = panel_save_name (p);
1045 panel_save_setup (p, name);
1046 panel_clean_dir (p);
1048 /* save and clean history */
1049 if (p->dir_history) {
1050 history_put (p->hist_name, p->dir_history);
1052 p->dir_history = g_list_first (p->dir_history);
1053 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1054 g_list_free (p->dir_history);
1057 g_free (p->hist_name);
1059 delete_format (p->format);
1060 delete_format (p->status_format);
1062 g_free (p->user_format);
1063 for (i = 0; i < LIST_TYPES; i++)
1064 g_free (p->user_status_format[i]);
1065 g_free (p->dir.list);
1066 g_free (p->panel_name);
1067 g_free (name);
1070 static void
1071 panel_format_modified (WPanel *panel)
1073 panel->format_modified = 1;
1076 /* Panel creation */
1077 /* The parameter specifies the name of the panel for setup retieving */
1078 WPanel *
1079 panel_new (const char *panel_name)
1081 return panel_new_with_dir(panel_name, NULL);
1084 /* Panel creation for specified directory */
1085 /* The parameter specifies the name of the panel for setup retieving */
1086 /* and the path of working panel directory. If path is NULL then */
1087 /* panel will be created for current directory */
1088 WPanel *
1089 panel_new_with_dir (const char *panel_name, const char *wpath)
1091 WPanel *panel;
1092 char *section;
1093 int i, err;
1094 char curdir[MC_MAXPATHLEN];
1096 panel = g_new0 (WPanel, 1);
1098 /* No know sizes of the panel at startup */
1099 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
1101 /* We do not want the cursor */
1102 widget_want_cursor (panel->widget, 0);
1104 if (wpath) {
1105 g_strlcpy(panel->cwd, wpath, sizeof (panel->cwd));
1106 mc_get_current_wd (curdir, sizeof (curdir) - 2);
1107 } else
1108 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
1110 strcpy (panel->lwd, ".");
1112 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
1113 panel->dir_history = history_get (panel->hist_name);
1114 directory_history_add (panel, panel->cwd);
1116 panel->dir.list = g_new (file_entry, MIN_FILES);
1117 panel->dir.size = MIN_FILES;
1118 panel->active = 0;
1119 panel->filter = 0;
1120 panel->split = 0;
1121 panel->top_file = 0;
1122 panel->selected = 0;
1123 panel->marked = 0;
1124 panel->total = 0;
1125 panel->reverse = 0;
1126 panel->dirty = 1;
1127 panel->searching = 0;
1128 panel->dirs_marked = 0;
1129 panel->is_panelized = 0;
1130 panel->format = 0;
1131 panel->status_format = 0;
1132 panel->format_modified = 1;
1134 panel->panel_name = g_strdup (panel_name);
1135 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
1137 for (i = 0; i < LIST_TYPES; i++)
1138 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
1140 panel->search_buffer[0] = 0;
1141 panel->frame_size = frame_half;
1142 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1143 if (!mc_config_has_group (mc_main_config, section)) {
1144 g_free (section);
1145 section = g_strdup (panel->panel_name);
1147 panel_load_setup (panel, section);
1148 g_free (section);
1150 /* Load format strings */
1151 err = set_panel_formats (panel);
1152 if (err) {
1153 set_panel_formats (panel);
1157 /* Because do_load_dir lists files in current directory */
1158 if (wpath)
1159 mc_chdir(wpath);
1161 /* Load the default format */
1162 panel->count =
1163 do_load_dir (panel->cwd, &panel->dir, panel->sort_type,
1164 panel->reverse, panel->case_sensitive,
1165 panel->exec_first, panel->filter);
1167 /* Restore old right path */
1168 if (wpath)
1169 mc_chdir(curdir);
1171 return panel;
1174 void
1175 panel_reload (WPanel *panel)
1177 struct stat current_stat;
1179 if (fast_reload && !stat (panel->cwd, &current_stat)
1180 && current_stat.st_ctime == panel->dir_stat.st_ctime
1181 && current_stat.st_mtime == panel->dir_stat.st_mtime)
1182 return;
1184 while (mc_chdir (panel->cwd) == -1) {
1185 char *last_slash;
1187 if (panel->cwd[0] == PATH_SEP && panel->cwd[1] == 0) {
1188 panel_clean_dir (panel);
1189 panel->count = set_zero_dir (&panel->dir);
1190 return;
1192 last_slash = strrchr (panel->cwd, PATH_SEP);
1193 if (!last_slash || last_slash == panel->cwd)
1194 strcpy (panel->cwd, PATH_SEP_STR);
1195 else
1196 *last_slash = 0;
1197 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
1198 show_dir (panel);
1201 panel->count =
1202 do_reload_dir (panel->cwd, &panel->dir, panel->sort_type,
1203 panel->count, panel->reverse, panel->case_sensitive,
1204 panel->exec_first, panel->filter);
1206 panel->dirty = 1;
1207 if (panel->selected >= panel->count)
1208 do_select (panel, panel->count - 1);
1210 recalculate_panel_summary (panel);
1213 static void
1214 paint_frame (WPanel *panel)
1216 int side, width;
1217 char *txt = NULL;
1219 if (!panel->split)
1220 adjust_top_file (panel);
1222 widget_erase (&panel->widget);
1223 show_dir (panel);
1225 widget_move (&panel->widget, 1, 1);
1227 for (side = 0; side <= panel->split; side++){
1228 format_e *format;
1230 if (side){
1231 tty_setcolor (NORMAL_COLOR);
1232 tty_print_one_vline ();
1233 width = panel->widget.cols - panel->widget.cols/2 - 1;
1234 } else if (panel->split)
1235 width = panel->widget.cols/2 - 3;
1236 else
1237 width = panel->widget.cols - 2;
1239 for (format = panel->format; format; format = format->next){
1240 if (format->string_fn){
1241 if (panel->filter && !strcmp (format->id, "name")) {
1242 txt = g_strdup_printf ("%s [%s]", format->title, panel->filter);
1243 } else {
1244 txt = g_strdup (format->title);
1247 tty_setcolor (MARKED_COLOR);
1248 tty_print_string (str_fit_to_term (format->title, format->field_len,
1249 J_CENTER_LEFT));
1250 g_free(txt);
1251 width -= format->field_len;
1252 } else {
1253 tty_setcolor (NORMAL_COLOR);
1254 tty_print_one_vline ();
1255 width--;
1259 if (width > 0)
1260 tty_draw_hline (-1, -1, ' ', width);
1264 static const char *
1265 parse_panel_size (WPanel *panel, const char *format, int isstatus)
1267 int frame = frame_half;
1268 format = skip_separators (format);
1270 if (!strncmp (format, "full", 4)){
1271 frame = frame_full;
1272 format += 4;
1273 } else if (!strncmp (format, "half", 4)){
1274 frame = frame_half;
1275 format += 4;
1278 if (!isstatus){
1279 panel->frame_size = frame;
1280 panel->split = 0;
1283 /* Now, the optional column specifier */
1284 format = skip_separators (format);
1286 if (*format == '1' || *format == '2'){
1287 if (!isstatus)
1288 panel->split = *format == '2';
1289 format++;
1292 if (!isstatus)
1293 panel_update_cols (&(panel->widget), panel->frame_size);
1295 return skip_separators (format);
1298 /* Format is:
1300 all := panel_format? format
1301 panel_format := [full|half] [1|2]
1302 format := one_format_e
1303 | format , one_format_e
1305 one_format_e := just format.id [opt_size]
1306 just := [<=>]
1307 opt_size := : size [opt_expand]
1308 size := [0-9]+
1309 opt_expand := +
1313 static format_e *
1314 parse_display_format (WPanel *panel, const char *format, char **error, int isstatus, int *res_total_cols)
1316 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1317 int total_cols = 0; /* Used columns by the format */
1318 int set_justify; /* flag: set justification mode? */
1319 align_crt_t justify = J_LEFT; /* Which mode. */
1320 int items = 0; /* Number of items in the format */
1321 size_t i;
1323 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1325 *error = 0;
1327 if (i18n_timelength == 0) {
1328 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1330 for (i = 0; i < ELEMENTS(formats); i++)
1331 if (strcmp ("time", formats [i].id+1) == 0)
1332 formats [i].min_size = i18n_timelength;
1336 * This makes sure that the panel and mini status full/half mode
1337 * setting is equal
1339 format = parse_panel_size (panel, format, isstatus);
1341 while (*format){ /* format can be an empty string */
1342 int found = 0;
1344 darr = g_new (format_e, 1);
1346 /* I'm so ugly, don't look at me :-) */
1347 if (!home)
1348 home = old = darr;
1350 old->next = darr;
1351 darr->next = 0;
1352 old = darr;
1354 format = skip_separators (format);
1356 if (strchr ("<=>", *format)){
1357 set_justify = 1;
1358 switch (*format)
1360 case '<':
1361 justify = J_LEFT;
1362 break;
1363 case '=':
1364 justify = J_CENTER;
1365 break;
1366 case '>':
1367 default:
1368 justify = J_RIGHT;
1369 break;
1371 format = skip_separators (format+1);
1372 } else
1373 set_justify = 0;
1375 for (i = 0; i < ELEMENTS(formats); i++){
1376 size_t klen = strlen (formats [i].id);
1378 if (strncmp (format, formats [i].id, klen) != 0)
1379 continue;
1381 format += klen;
1383 if (formats [i].use_in_gui)
1384 items++;
1386 darr->requested_field_len = formats [i].min_size;
1387 darr->string_fn = formats [i].string_fn;
1388 if (formats [i].title [0])
1389 darr->title = _(formats [i].title);
1390 else
1391 darr->title = "";
1392 darr->id = formats [i].id;
1393 darr->expand = formats [i].expands;
1394 darr->just_mode = formats [i].default_just;
1396 if (set_justify) {
1397 if (IS_FIT(darr->just_mode))
1398 darr->just_mode = MAKE_FIT(justify);
1399 else
1400 darr->just_mode = justify;
1402 found = 1;
1404 format = skip_separators (format);
1406 /* If we have a size specifier */
1407 if (*format == ':'){
1408 int req_length;
1410 /* If the size was specified, we don't want
1411 * auto-expansion by default
1413 darr->expand = 0;
1414 format++;
1415 req_length = atoi (format);
1416 darr->requested_field_len = req_length;
1418 format = skip_numbers (format);
1420 /* Now, if they insist on expansion */
1421 if (*format == '+'){
1422 darr->expand = 1;
1423 format++;
1428 break;
1430 if (!found){
1431 char *tmp_format = g_strdup (format);
1433 int pos = min (8, strlen (format));
1434 delete_format (home);
1435 tmp_format [pos] = 0;
1436 *error = g_strconcat (_("Unknown tag on display format: "), tmp_format, (char *) NULL);
1437 g_free (tmp_format);
1438 return 0;
1440 total_cols += darr->requested_field_len;
1443 *res_total_cols = total_cols;
1444 return home;
1447 static format_e *
1448 use_display_format (WPanel *panel, const char *format, char **error, int isstatus)
1450 #define MAX_EXPAND 4
1451 int expand_top = 0; /* Max used element in expand */
1452 int usable_columns; /* Usable columns in the panel */
1453 int total_cols = 0;
1454 int i;
1455 format_e *darr, *home;
1457 if (!format)
1458 format = DEFAULT_USER_FORMAT;
1460 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1462 if (*error)
1463 return 0;
1465 panel->dirty = 1;
1467 /* Status needn't to be split */
1468 usable_columns = ((panel->widget.cols-2)/((isstatus)
1470 : (panel->split+1))) - (!isstatus && panel->split);
1472 /* Look for the expandable fields and set field_len based on the requested field len */
1473 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next){
1474 darr->field_len = darr->requested_field_len;
1475 if (darr->expand)
1476 expand_top++;
1479 /* If we used more columns than the available columns, adjust that */
1480 if (total_cols > usable_columns){
1481 int pdif, dif = total_cols - usable_columns;
1483 while (dif){
1484 pdif = dif;
1485 for (darr = home; darr; darr = darr->next){
1486 if (dif && darr->field_len - 1){
1487 darr->field_len--;
1488 dif--;
1492 /* avoid endless loop if num fields > 40 */
1493 if (pdif == dif)
1494 break;
1496 total_cols = usable_columns; /* give up, the rest should be truncated */
1499 /* Expand the available space */
1500 if ((usable_columns > total_cols) && expand_top){
1501 int spaces = (usable_columns - total_cols) / expand_top;
1502 int extra = (usable_columns - total_cols) % expand_top;
1504 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1505 if (darr->expand){
1506 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1507 i++;
1510 return home;
1513 /* Switches the panel to the mode specified in the format */
1514 /* Seting up both format and status string. Return: 0 - on success; */
1515 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
1517 set_panel_formats (WPanel *p)
1519 format_e *form;
1520 char *err;
1521 int retcode = 0;
1523 form = use_display_format (p, panel_format (p), &err, 0);
1525 if (err){
1526 g_free (err);
1527 retcode = 1;
1529 else {
1530 if (p->format)
1531 delete_format (p->format);
1533 p->format = form;
1536 if (show_mini_info){
1538 form = use_display_format (p, mini_status_format (p), &err, 1);
1540 if (err){
1541 g_free (err);
1542 retcode += 2;
1544 else {
1545 if (p->status_format)
1546 delete_format (p->status_format);
1548 p->status_format = form;
1552 panel_format_modified (p);
1553 panel_update_cols (&(p->widget), p->frame_size);
1555 if (retcode)
1556 message (D_ERROR, _("Warning" ), _( "User supplied format looks invalid, reverting to default." ) );
1557 if (retcode & 0x01){
1558 g_free (p->user_format);
1559 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
1561 if (retcode & 0x02){
1562 g_free (p->user_status_format [p->list_type]);
1563 p->user_status_format [p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
1566 return retcode;
1569 /* Given the panel->view_type returns the format string to be parsed */
1570 static const char *
1571 panel_format (WPanel *panel)
1573 switch (panel->list_type){
1575 case list_long:
1576 return "full perm space nlink space owner space group space size space mtime space name";
1578 case list_brief:
1579 return "half 2 type name";
1581 case list_user:
1582 return panel->user_format;
1584 default:
1585 case list_full:
1586 return "half type name | size | mtime";
1590 static const char *
1591 mini_status_format (WPanel *panel)
1593 if (panel->user_mini_status)
1594 return panel->user_status_format [panel->list_type];
1596 switch (panel->list_type){
1598 case list_long:
1599 return "full perm space nlink space owner space group space size space mtime space name";
1601 case list_brief:
1602 return "half type name space bsize space perm space";
1604 case list_full:
1605 return "half type name";
1607 default:
1608 case list_user:
1609 return panel->user_format;
1613 /* */
1614 /* Panel operation commands */
1615 /* */
1617 /* Used to emulate Lynx's entering leaving a directory with the arrow keys */
1618 static cb_ret_t
1619 maybe_cd (int move_up_dir)
1621 if (navigate_with_arrows) {
1622 if (!cmdline->buffer[0]) {
1623 if (move_up_dir) {
1624 do_cd ("..", cd_exact);
1625 return MSG_HANDLED;
1627 if (S_ISDIR (selection (current_panel)->st.st_mode)
1628 || link_isdir (selection (current_panel))) {
1629 do_cd (selection (current_panel)->fname, cd_exact);
1630 return MSG_HANDLED;
1634 return MSG_NOT_HANDLED;
1637 /* Returns the number of items in the given panel */
1638 static int
1639 ITEMS (WPanel *p)
1641 if (p->split)
1642 return llines (p) * 2;
1643 else
1644 return llines (p);
1647 /* Select current item and readjust the panel */
1648 void
1649 select_item (WPanel *panel)
1651 int items = ITEMS (panel);
1653 /* Although currently all over the code we set the selection and
1654 top file to decent values before calling select_item, I could
1655 forget it someday, so it's better to do the actual fitting here */
1657 if (panel->top_file < 0)
1658 panel->top_file = 0;
1660 if (panel->selected < 0)
1661 panel->selected = 0;
1663 if (panel->selected > panel->count - 1)
1664 panel->selected = panel->count - 1;
1666 if (panel->top_file > panel->count - 1)
1667 panel->top_file = panel->count - 1;
1669 if ((panel->count - panel->top_file) < items) {
1670 panel->top_file = panel->count - items;
1671 if (panel->top_file < 0)
1672 panel->top_file = 0;
1675 if (panel->selected < panel->top_file)
1676 panel->top_file = panel->selected;
1678 if ((panel->selected - panel->top_file) >= items)
1679 panel->top_file = panel->selected - items + 1;
1681 panel->dirty = 1;
1683 execute_hooks (select_file_hook);
1686 /* Clears all files in the panel, used only when one file was marked */
1687 void
1688 unmark_files (WPanel *panel)
1690 int i;
1692 if (!panel->marked)
1693 return;
1694 for (i = 0; i < panel->count; i++)
1695 file_mark (panel, i, 0);
1697 panel->dirs_marked = 0;
1698 panel->marked = 0;
1699 panel->total = 0;
1702 static void
1703 unselect_item (WPanel *panel)
1705 repaint_file (panel, panel->selected, 1, 2*selection (panel)->f.marked, 0);
1708 static void
1709 move_down (WPanel *panel)
1711 if (panel->selected+1 == panel->count)
1712 return;
1714 unselect_item (panel);
1715 panel->selected++;
1716 if (panel->selected - panel->top_file == ITEMS (panel) &&
1717 panel_scroll_pages) {
1718 /* Scroll window half screen */
1719 panel->top_file += ITEMS (panel)/2;
1720 if (panel->top_file > panel->count - ITEMS (panel))
1721 panel->top_file = panel->count - ITEMS (panel);
1722 paint_dir (panel);
1724 select_item (panel);
1727 static void
1728 move_up (WPanel *panel)
1730 if (panel->selected == 0)
1731 return;
1733 unselect_item (panel);
1734 panel->selected--;
1735 if (panel->selected < panel->top_file && panel_scroll_pages) {
1736 /* Scroll window half screen */
1737 panel->top_file -= ITEMS (panel)/2;
1738 if (panel->top_file < 0)
1739 panel->top_file = 0;
1740 paint_dir (panel);
1742 select_item (panel);
1745 /* Changes the selection by lines (may be negative) */
1746 static void
1747 move_selection (WPanel *panel, int lines)
1749 int new_pos;
1750 int adjust = 0;
1752 new_pos = panel->selected + lines;
1753 if (new_pos >= panel->count)
1754 new_pos = panel->count-1;
1756 if (new_pos < 0)
1757 new_pos = 0;
1759 unselect_item (panel);
1760 panel->selected = new_pos;
1762 if (panel->selected - panel->top_file >= ITEMS (panel)){
1763 panel->top_file += lines;
1764 adjust = 1;
1767 if (panel->selected - panel->top_file < 0){
1768 panel->top_file += lines;
1769 adjust = 1;
1772 if (adjust){
1773 if (panel->top_file > panel->selected)
1774 panel->top_file = panel->selected;
1775 if (panel->top_file < 0)
1776 panel->top_file = 0;
1777 paint_dir (panel);
1779 select_item (panel);
1782 static cb_ret_t
1783 move_left (WPanel *panel)
1785 if (panel->split) {
1786 move_selection (panel, -llines (panel));
1787 return MSG_HANDLED;
1788 } else
1789 return maybe_cd (1); /* cd .. */
1792 static int
1793 move_right (WPanel *panel)
1795 if (panel->split) {
1796 move_selection (panel, llines (panel));
1797 return MSG_HANDLED;
1798 } else
1799 return maybe_cd (0); /* cd (selection) */
1802 static void
1803 prev_page (WPanel *panel)
1805 int items;
1807 if (!panel->selected && !panel->top_file)
1808 return;
1809 unselect_item (panel);
1810 items = ITEMS (panel);
1811 if (panel->top_file < items)
1812 items = panel->top_file;
1813 if (!items)
1814 panel->selected = 0;
1815 else
1816 panel->selected -= items;
1817 panel->top_file -= items;
1819 /* This keeps the selection in a reasonable place */
1820 if (panel->selected < 0)
1821 panel->selected = 0;
1822 if (panel->top_file < 0)
1823 panel->top_file = 0;
1824 select_item (panel);
1825 paint_dir (panel);
1828 static void
1829 ctrl_prev_page (WPanel *panel)
1831 (void) panel;
1832 do_cd ("..", cd_exact);
1835 static void
1836 next_page (WPanel *panel)
1838 int items;
1840 if (panel->selected == panel->count - 1)
1841 return;
1842 unselect_item (panel);
1843 items = ITEMS (panel);
1844 if (panel->top_file > panel->count - 2 * items)
1845 items = panel->count - items - panel->top_file;
1846 if (panel->top_file + items < 0)
1847 items = -panel->top_file;
1848 if (!items)
1849 panel->selected = panel->count - 1;
1850 else
1851 panel->selected += items;
1852 panel->top_file += items;
1854 /* This keeps the selection in it's relative position */
1855 if (panel->selected >= panel->count)
1856 panel->selected = panel->count - 1;
1857 if (panel->top_file >= panel->count)
1858 panel->top_file = panel->count - 1;
1859 select_item (panel);
1860 paint_dir (panel);
1863 static void
1864 ctrl_next_page (WPanel *panel)
1866 if ((S_ISDIR (selection (panel)->st.st_mode)
1867 || link_isdir (selection (panel)))) {
1868 do_cd (selection (panel)->fname, cd_exact);
1872 static void
1873 goto_top_file (WPanel *panel)
1875 unselect_item (panel);
1876 panel->selected = panel->top_file;
1877 select_item (panel);
1880 static void
1881 goto_middle_file (WPanel *panel)
1883 unselect_item (panel);
1884 panel->selected = panel->top_file + (ITEMS (panel)/2);
1885 if (panel->selected >= panel->count)
1886 panel->selected = panel->count - 1;
1887 select_item (panel);
1890 static void
1891 goto_bottom_file (WPanel *panel)
1893 unselect_item (panel);
1894 panel->selected = panel->top_file + ITEMS (panel)-1;
1895 if (panel->selected >= panel->count)
1896 panel->selected = panel->count - 1;
1897 select_item (panel);
1900 static void
1901 move_home (WPanel *panel)
1903 if (panel->selected == 0)
1904 return;
1905 unselect_item (panel);
1907 if (torben_fj_mode){
1908 int middle_pos = panel->top_file + (ITEMS (panel)/2);
1910 if (panel->selected > middle_pos){
1911 goto_middle_file (panel);
1912 return;
1914 if (panel->selected != panel->top_file){
1915 goto_top_file (panel);
1916 return;
1920 panel->top_file = 0;
1921 panel->selected = 0;
1923 paint_dir (panel);
1924 select_item (panel);
1927 static void
1928 move_end (WPanel *panel)
1930 if (panel->selected == panel->count-1)
1931 return;
1932 unselect_item (panel);
1933 if (torben_fj_mode){
1934 int middle_pos = panel->top_file + (ITEMS (panel)/2);
1936 if (panel->selected < middle_pos){
1937 goto_middle_file (panel);
1938 return;
1940 if (panel->selected != (panel->top_file + ITEMS(panel)-1)){
1941 goto_bottom_file (panel);
1942 return;
1946 panel->selected = panel->count-1;
1947 paint_dir (panel);
1948 select_item (panel);
1951 /* Recalculate the panels summary information, used e.g. when marked
1952 files might have been removed by an external command */
1953 void
1954 recalculate_panel_summary (WPanel *panel)
1956 int i;
1958 panel->marked = 0;
1959 panel->dirs_marked = 0;
1960 panel->total = 0;
1962 for (i = 0; i < panel->count; i++)
1963 if (panel->dir.list [i].f.marked){
1964 /* do_file_mark will return immediately if newmark == oldmark.
1965 So we have to first unmark it to get panel's summary information
1966 updated. (Norbert) */
1967 panel->dir.list [i].f.marked = 0;
1968 do_file_mark (panel, i, 1);
1972 /* This routine marks a file or a directory */
1973 void
1974 do_file_mark (WPanel *panel, int idx, int mark)
1976 if (panel->dir.list[idx].f.marked == mark)
1977 return;
1979 /* Only '..' can't be marked, '.' isn't visible */
1980 if (!strcmp (panel->dir.list[idx].fname, ".."))
1981 return;
1983 file_mark (panel, idx, mark);
1984 if (panel->dir.list[idx].f.marked) {
1985 panel->marked++;
1986 if (S_ISDIR (panel->dir.list[idx].st.st_mode)) {
1987 if (panel->dir.list[idx].f.dir_size_computed)
1988 panel->total += panel->dir.list[idx].st.st_size;
1989 panel->dirs_marked++;
1990 } else
1991 panel->total += panel->dir.list[idx].st.st_size;
1992 set_colors (panel);
1993 } else {
1994 if (S_ISDIR (panel->dir.list[idx].st.st_mode)) {
1995 if (panel->dir.list[idx].f.dir_size_computed)
1996 panel->total -= panel->dir.list[idx].st.st_size;
1997 panel->dirs_marked--;
1998 } else
1999 panel->total -= panel->dir.list[idx].st.st_size;
2000 panel->marked--;
2004 static void
2005 do_mark_file (WPanel *panel, int do_move)
2007 do_file_mark (panel, panel->selected,
2008 selection (panel)->f.marked ? 0 : 1);
2009 if (mark_moves_down && do_move)
2010 move_down (panel);
2013 static void
2014 mark_file (WPanel *panel)
2016 do_mark_file (panel, 1);
2019 /* Incremental search of a file name in the panel */
2020 static void
2021 do_search (WPanel *panel, int c_code)
2023 size_t l, max, buf_max;
2024 int i, sel;
2025 int wrapped = 0;
2026 char *act;
2028 l = strlen (panel->search_buffer);
2029 if (c_code == KEY_BACKSPACE) {
2030 if (l != 0) {
2031 act = panel->search_buffer + l;
2032 str_prev_noncomb_char (&act, panel->search_buffer);
2033 act[0] = '\0';
2035 panel->search_chpoint = 0;
2036 } else {
2037 if (c_code && (gsize) panel->search_chpoint < sizeof (panel->search_char)) {
2038 panel->search_char[panel->search_chpoint] = c_code;
2039 panel->search_chpoint++;
2042 if (panel->search_chpoint > 0) {
2043 switch (str_is_valid_char (panel->search_char, panel->search_chpoint)) {
2044 case -2:
2045 return;
2046 case -1:
2047 panel->search_chpoint = 0;
2048 return;
2049 default:
2050 if (l + panel->search_chpoint < sizeof (panel->search_buffer)) {
2051 memcpy (panel->search_buffer + l, panel->search_char,
2052 panel->search_chpoint);
2053 l+= panel->search_chpoint;
2054 (panel->search_buffer + l)[0] = '\0';
2055 panel->search_chpoint = 0;
2061 buf_max = panel->case_sensitive ?
2062 str_prefix (panel->search_buffer, panel->search_buffer) :
2063 str_caseprefix (panel->search_buffer, panel->search_buffer);
2064 max = 0;
2065 sel = panel->selected;
2066 for (i = panel->selected; !wrapped || i != panel->selected; i++) {
2067 if (i >= panel->count) {
2068 i = 0;
2069 if (wrapped)
2070 break;
2071 wrapped = 1;
2073 l = panel->case_sensitive ?
2074 str_prefix (panel->dir.list[i].fname, panel->search_buffer) :
2075 str_caseprefix (panel->dir.list[i].fname, panel->search_buffer);
2076 if (l > max) {
2077 max = l;
2078 sel = i;
2079 if (max == buf_max) break;
2083 unselect_item (panel);
2084 panel->selected = sel;
2085 select_item (panel);
2087 act = panel->search_buffer + strlen (panel->search_buffer);
2088 while (max < buf_max) {
2089 str_prev_char_safe (&act);
2090 act[0] = '\0';
2091 buf_max = panel->case_sensitive ?
2092 str_prefix (panel->search_buffer, panel->search_buffer) :
2093 str_caseprefix (panel->search_buffer, panel->search_buffer);
2096 paint_panel (panel);
2099 static void
2100 start_search (WPanel *panel)
2102 if (panel->searching){
2103 if (panel->selected+1 == panel->count)
2104 panel->selected = 0;
2105 else
2106 move_down (panel);
2107 do_search (panel, 0);
2108 } else {
2109 panel->searching = 1;
2110 panel->search_buffer[0] = '\0';
2111 panel->search_char[0] = '\0';
2112 panel->search_chpoint = 0;
2113 display_mini_info (panel);
2114 mc_refresh ();
2118 /* Return 1 if the Enter key has been processed, 0 otherwise */
2119 static int
2120 do_enter_on_file_entry (file_entry *fe)
2122 char *full_name;
2125 * Directory or link to directory - change directory.
2126 * Try the same for the entries on which mc_lstat() has failed.
2128 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe)
2129 || (fe->st.st_mode == 0)) {
2130 if (!do_cd (fe->fname, cd_exact))
2131 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2132 return 1;
2135 /* Try associated command */
2136 if (regex_command (fe->fname, "Open", 0) != 0)
2137 return 1;
2139 /* Check if the file is executable */
2140 full_name = concat_dir_and_file (current_panel->cwd, fe->fname);
2141 if (!is_exe (fe->st.st_mode) || !if_link_is_exe (full_name, fe)) {
2142 g_free (full_name);
2143 return 0;
2145 g_free (full_name);
2147 if (confirm_execute) {
2148 if (query_dialog
2149 (_(" The Midnight Commander "),
2150 _(" Do you really want to execute? "), D_NORMAL, 2, _("&Yes"),
2151 _("&No")) != 0)
2152 return 1;
2154 #ifdef USE_VFS
2155 if (!vfs_current_is_local ()) {
2156 char *tmp;
2157 int ret;
2159 tmp = concat_dir_and_file (vfs_get_current_dir (), fe->fname);
2160 ret = mc_setctl (tmp, VFS_SETCTL_RUN, NULL);
2161 g_free (tmp);
2162 /* We took action only if the dialog was shown or the execution
2163 * was successful */
2164 return confirm_execute || (ret == 0);
2166 #endif
2169 char *tmp = name_quote (fe->fname, 0);
2170 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2171 g_free (tmp);
2172 shell_execute (cmd, 0);
2173 g_free (cmd);
2176 return 1;
2179 static int
2180 do_enter (WPanel *panel)
2182 return do_enter_on_file_entry (selection (panel));
2185 static void
2186 chdir_other_panel (WPanel *panel)
2188 char *new_dir;
2189 char *sel_entry = NULL;
2191 if (get_other_type () != view_listing) {
2192 set_display_type (get_other_index (), view_listing);
2195 if (!S_ISDIR (panel->dir.list [panel->selected].st.st_mode)) {
2196 new_dir = concat_dir_and_file (panel->cwd, "..");
2197 sel_entry = strrchr(panel->cwd, PATH_SEP);
2198 } else
2199 new_dir = concat_dir_and_file (panel->cwd, panel->dir.list [panel->selected].fname);
2201 change_panel ();
2202 do_cd (new_dir, cd_exact);
2203 if (sel_entry)
2204 try_to_select (current_panel, sel_entry);
2205 change_panel ();
2207 move_down (panel);
2209 g_free (new_dir);
2213 * Make the current directory of the current panel also the current
2214 * directory of the other panel. Put the other panel to the listing
2215 * mode if needed. If the current panel is panelized, the other panel
2216 * doesn't become panelized.
2218 static void
2219 sync_other_panel (WPanel *panel)
2221 if (get_other_type () != view_listing) {
2222 set_display_type (get_other_index (), view_listing);
2225 do_panel_cd (other_panel, current_panel->cwd, cd_exact);
2227 /* try to select current filename on the other panel */
2228 if (!panel->is_panelized) {
2229 try_to_select (other_panel, selection (panel)->fname);
2233 static void
2234 chdir_to_readlink (WPanel *panel)
2236 char *new_dir;
2238 if (get_other_type () != view_listing)
2239 return;
2241 if (S_ISLNK (panel->dir.list [panel->selected].st.st_mode)) {
2242 char buffer [MC_MAXPATHLEN], *p;
2243 int i;
2244 struct stat st;
2246 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2247 if (i < 0)
2248 return;
2249 if (mc_stat (selection (panel)->fname, &st) < 0)
2250 return;
2251 buffer [i] = 0;
2252 if (!S_ISDIR (st.st_mode)) {
2253 p = strrchr (buffer, PATH_SEP);
2254 if (p && !p[1]) {
2255 *p = 0;
2256 p = strrchr (buffer, PATH_SEP);
2258 if (!p)
2259 return;
2260 p[1] = 0;
2262 if (*buffer == PATH_SEP)
2263 new_dir = g_strdup (buffer);
2264 else
2265 new_dir = concat_dir_and_file (panel->cwd, buffer);
2267 change_panel ();
2268 do_cd (new_dir, cd_exact);
2269 change_panel ();
2271 move_down (panel);
2273 g_free (new_dir);
2277 typedef void (*panel_key_callback) (WPanel *);
2279 static void cmd_do_enter(WPanel *wp) { (void) do_enter(wp); }
2280 static void cmd_view_simple(WPanel *wp) { (void) wp; view_simple_cmd(); }
2281 static void cmd_edit_new(WPanel *wp) { (void) wp; edit_cmd_new(); }
2282 static void cmd_copy_local(WPanel *wp) { (void) wp;copy_cmd_local(); }
2283 static void cmd_rename_local(WPanel *wp) { (void) wp;ren_cmd_local(); }
2284 static void cmd_delete_local(WPanel *wp) { (void) wp;delete_cmd_local(); }
2285 static void cmd_select(WPanel *wp) { (void) wp;select_cmd(); }
2286 static void cmd_unselect(WPanel *wp) { (void) wp;unselect_cmd(); }
2287 static void cmd_reverse_selection(WPanel *wp) { (void) wp;reverse_selection_cmd(); }
2289 static cb_ret_t
2290 panel_execute_cmd (WPanel *panel, int command)
2292 int res = MSG_HANDLED;
2294 switch (command) {
2295 case CK_PanelChdirOtherPanel:
2296 chdir_other_panel (panel);
2297 break;
2298 case CK_PanelChdirToReadlink:
2299 chdir_to_readlink (panel);
2300 break;
2301 case CK_PanelCmdCopyLocal:
2302 cmd_copy_local (panel);
2303 break;
2304 case CK_PanelCmdDeleteLocal:
2305 cmd_delete_local (panel);
2306 break;
2307 case CK_PanelCmdDoEnter:
2308 cmd_do_enter (panel);
2309 break;
2310 case CK_PanelCmdViewSimple:
2311 cmd_view_simple (panel);
2312 break;
2313 case CK_PanelCmdEditNew:
2314 cmd_edit_new (panel);
2315 break;
2316 case CK_PanelCmdRenameLocal:
2317 cmd_rename_local (panel);
2318 break;
2319 case CK_PanelCmdReverseSelection:
2320 cmd_reverse_selection (panel);
2321 break;
2322 case CK_PanelCmdSelect:
2323 cmd_select (panel);
2324 break;
2325 case CK_PanelCmdUnselect:
2326 cmd_unselect (panel);
2327 break;
2328 case CK_PanelNextPage:
2329 next_page (panel);
2330 break;
2331 case CK_PanelPrevPage:
2332 prev_page (panel);
2333 break;
2334 case CK_PanelCtrlNextPage:
2335 ctrl_next_page (panel);
2336 break;
2337 case CK_PanelCtrlPrevPage:
2338 ctrl_prev_page (panel);
2339 break;
2340 case CK_PanelDirectoryHistoryList:
2341 directory_history_list (panel);
2342 break;
2343 case CK_PanelDirectoryHistoryNext:
2344 directory_history_next (panel);
2345 break;
2346 case CK_PanelDirectoryHistoryPrev:
2347 directory_history_prev (panel);
2348 break;
2349 case CK_PanelGotoBottomFile:
2350 goto_bottom_file (panel);
2351 break;
2352 case CK_PanelGotoMiddleFile:
2353 goto_middle_file (panel);
2354 break;
2355 case CK_PanelGotoTopFile:
2356 goto_top_file (panel);
2357 break;
2358 case CK_PanelMarkFile:
2359 mark_file (panel);
2360 break;
2361 case CK_PanelMoveUp:
2362 move_up (panel);
2363 break;
2364 case CK_PanelMoveDown:
2365 move_down (panel);
2366 break;
2367 case CK_PanelMoveLeft:
2368 res = move_left (panel);
2369 break;
2370 case CK_PanelMoveRight:
2371 res = move_right (panel);
2372 break;
2373 case CK_PanelMoveEnd:
2374 move_end (panel);
2375 break;
2376 case CK_PanelMoveHome:
2377 move_home (panel);
2378 break;
2379 case CK_PanelSetPanelEncoding:
2380 set_panel_encoding (panel);
2381 break;
2382 case CK_PanelStartSearch:
2383 start_search (panel);
2384 break;
2385 case CK_PanelSyncOtherPanel:
2386 sync_other_panel (panel);
2387 break;
2389 return res;
2392 static cb_ret_t
2393 panel_key (WPanel *panel, int key)
2395 int i;
2396 int res, command;
2398 for (i = 0; panel_map[i].key; i++) {
2399 if (key == panel_map[i].key) {
2400 int old_searching = panel->searching;
2402 if (panel_map[i].command != CK_PanelStartSearch)
2403 panel->searching = 0;
2405 command = panel_map[i].command;
2406 res = panel_execute_cmd (panel, command);
2408 if (res == MSG_NOT_HANDLED)
2409 return res;
2411 if (panel->searching != old_searching)
2412 display_mini_info (panel);
2413 return MSG_HANDLED;
2417 if (torben_fj_mode && key == ALT ('h')) {
2418 goto_middle_file (panel);
2419 return MSG_HANDLED;
2422 if (is_abort_char (key)) {
2423 panel->searching = 0;
2424 display_mini_info (panel);
2425 return MSG_HANDLED;
2428 /* Do not eat characters not meant for the panel below ' ' (e.g. C-l). */
2429 if ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE) {
2430 if (panel->searching) {
2431 do_search (panel, key);
2432 return MSG_HANDLED;
2435 if (!command_prompt) {
2436 start_search (panel);
2437 do_search (panel, key);
2438 return MSG_HANDLED;
2442 return MSG_NOT_HANDLED;
2445 static cb_ret_t
2446 panel_callback (Widget *w, widget_msg_t msg, int parm)
2448 WPanel *panel = (WPanel *) w;
2449 Dlg_head *h = panel->widget.parent;
2451 switch (msg) {
2452 case WIDGET_DRAW:
2453 paint_panel (panel);
2454 return MSG_HANDLED;
2456 case WIDGET_FOCUS:
2457 current_panel = panel;
2458 panel->active = 1;
2459 if (mc_chdir (panel->cwd) != 0) {
2460 char *cwd = strip_password (g_strdup (panel->cwd), 1);
2461 message (D_ERROR, MSG_ERROR, _(" Cannot chdir to \"%s\" \n %s "),
2462 cwd, unix_error_string (errno));
2463 g_free(cwd);
2464 } else
2465 subshell_chdir (panel->cwd);
2467 update_xterm_title_path ();
2468 select_item (panel);
2469 show_dir (panel);
2470 paint_dir (panel);
2471 panel->dirty = 0;
2473 buttonbar_set_label (h, 1, _("Help"), help_cmd);
2474 buttonbar_set_label (h, 2, _("Menu"), user_file_menu_cmd);
2475 buttonbar_set_label (h, 3, _("View"), view_cmd);
2476 buttonbar_set_label (h, 4, _("Edit"), edit_cmd);
2477 buttonbar_set_label (h, 5, _("Copy"), copy_cmd);
2478 buttonbar_set_label (h, 6, _("RenMov"), ren_cmd);
2479 buttonbar_set_label (h, 7, _("Mkdir"), mkdir_cmd);
2480 buttonbar_set_label (h, 8, _("Delete"), delete_cmd);
2481 buttonbar_redraw (h);
2482 return MSG_HANDLED;
2484 case WIDGET_UNFOCUS:
2485 /* Janne: look at this for the multiple panel options */
2486 if (panel->searching){
2487 panel->searching = 0;
2488 display_mini_info (panel);
2490 panel->active = 0;
2491 show_dir (panel);
2492 unselect_item (panel);
2493 return MSG_HANDLED;
2495 case WIDGET_KEY:
2496 return panel_key (panel, parm);
2498 case WIDGET_DESTROY:
2499 panel_destroy (panel);
2500 return MSG_HANDLED;
2502 default:
2503 return default_proc (msg, parm);
2507 void
2508 file_mark (WPanel *panel, int index, int val)
2510 if (panel->dir.list[index].f.marked != val) {
2511 panel->dir.list[index].f.marked = val;
2512 panel->dirty = 1;
2516 /* */
2517 /* Panel mouse events support routines */
2518 /* */
2519 static int mouse_marking = 0;
2521 static void
2522 mouse_toggle_mark (WPanel *panel)
2524 do_mark_file (panel, 0);
2525 mouse_marking = selection (panel)->f.marked;
2528 static void
2529 mouse_set_mark (WPanel *panel)
2531 if (mouse_marking && !(selection (panel)->f.marked))
2532 do_mark_file (panel, 0);
2533 else if (!mouse_marking && (selection (panel)->f.marked))
2534 do_mark_file (panel, 0);
2537 static int
2538 mark_if_marking (WPanel *panel, Gpm_Event *event)
2540 if (event->buttons & GPM_B_RIGHT){
2541 if (event->type & GPM_DOWN)
2542 mouse_toggle_mark (panel);
2543 else
2544 mouse_set_mark (panel);
2545 return 1;
2547 return 0;
2551 * Mouse callback of the panel minus repainting.
2552 * If the event is redirected to the menu, *redir is set to 1.
2554 static int
2555 do_panel_event (Gpm_Event *event, WPanel *panel, int *redir)
2557 const int lines = llines (panel);
2558 const gboolean is_active = dlg_widget_active (panel);
2559 const gboolean mouse_down = (event->type & GPM_DOWN) != 0;
2561 /* "<" button */
2562 if (mouse_down && event->y == 1 && event->x == 2) {
2563 directory_history_prev (panel);
2564 return MOU_NORMAL;
2567 /* ">" button */
2568 if (mouse_down && event->y == 1 && event->x == panel->widget.cols - 1) {
2569 directory_history_next (panel);
2570 return MOU_NORMAL;
2573 /* "v" button */
2574 if (mouse_down && event->y == 1 && event->x == panel->widget.cols - 2) {
2575 directory_history_list (panel);
2576 return MOU_NORMAL;
2579 /* rest of the upper frame, the menu is invisible - call menu */
2580 if (mouse_down && event->y == 1 && !menubar_visible) {
2581 *redir = 1;
2582 event->x += panel->widget.x;
2583 return the_menubar->widget.mouse (event, the_menubar);
2586 /* Mouse wheel events */
2587 if (mouse_down && (event->buttons & GPM_B_UP)) {
2588 if (is_active) {
2589 if (panel->top_file > 0)
2590 prev_page (panel);
2591 else /* We are in first page */
2592 move_up (panel);
2594 return MOU_NORMAL;
2597 if (mouse_down && (event->buttons & GPM_B_DOWN)) {
2598 if (is_active) {
2599 if (panel->top_file + ITEMS (panel) < panel->count)
2600 next_page (panel);
2601 else /* We are in last page */
2602 move_down (panel);
2604 return MOU_NORMAL;
2607 event->y -= 2;
2608 if ((event->type & (GPM_DOWN | GPM_DRAG))) {
2609 int my_index;
2611 if (!is_active)
2612 change_panel ();
2614 if (event->y <= 0) {
2615 mark_if_marking (panel, event);
2616 if (mouse_move_pages)
2617 prev_page (panel);
2618 else
2619 move_up (panel);
2620 return MOU_REPEAT;
2623 if (!((panel->top_file + event->y <= panel->count) && event->y <= lines)) {
2624 mark_if_marking (panel, event);
2625 if (mouse_move_pages)
2626 next_page (panel);
2627 else
2628 move_down (panel);
2629 return MOU_REPEAT;
2632 my_index = panel->top_file + event->y - 1;
2633 if (panel->split && (event->x > ((panel->widget.cols - 2) / 2)))
2634 my_index += llines (panel);
2636 if (my_index >= panel->count)
2637 my_index = panel->count - 1;
2639 if (my_index != panel->selected) {
2640 unselect_item (panel);
2641 panel->selected = my_index;
2642 select_item (panel);
2645 /* This one is new */
2646 mark_if_marking (panel, event);
2647 } else if ((event->type & (GPM_UP | GPM_DOUBLE)) == (GPM_UP | GPM_DOUBLE)) {
2648 if (event->y > 0 && event->y <= lines)
2649 do_enter (panel);
2651 return MOU_NORMAL;
2654 /* Mouse callback of the panel */
2655 static int
2656 panel_event (Gpm_Event *event, void *data)
2658 WPanel *panel = data;
2659 int ret;
2660 int redir = MOU_NORMAL;
2662 ret = do_panel_event (event, panel, &redir);
2663 if (!redir)
2664 paint_panel (panel);
2666 return ret;
2669 void
2670 panel_re_sort (WPanel *panel)
2672 char *filename;
2673 int i;
2675 if (panel == NULL)
2676 return;
2678 filename = g_strdup (selection (panel)->fname);
2679 unselect_item (panel);
2680 do_sort (&panel->dir, panel->sort_type, panel->count-1, panel->reverse,
2681 panel->case_sensitive, panel->exec_first);
2682 panel->selected = -1;
2683 for (i = panel->count; i; i--){
2684 if (!strcmp (panel->dir.list [i-1].fname, filename)){
2685 panel->selected = i-1;
2686 break;
2689 g_free (filename);
2690 panel->top_file = panel->selected - ITEMS (panel)/2;
2691 if (panel->top_file < 0)
2692 panel->top_file = 0;
2693 select_item (panel);
2694 panel->dirty = 1;
2697 void
2698 panel_set_sort_order (WPanel *panel, sortfn *sort_order)
2700 if (sort_order == 0)
2701 return;
2703 panel->sort_type = sort_order;
2705 /* The directory is already sorted, we have to load the unsorted stuff */
2706 if (sort_order == (sortfn *) unsorted){
2707 char *current_file;
2709 current_file = g_strdup (panel->dir.list [panel->selected].fname);
2710 panel_reload (panel);
2711 try_to_select (panel, current_file);
2712 g_free (current_file);
2714 panel_re_sort (panel);
2717 void
2718 set_panel_encoding (WPanel *panel)
2720 const char *encoding = NULL;
2721 char *cd_path;
2722 #ifdef HAVE_CHARSET
2723 const char *errmsg;
2724 int offset;
2725 int r;
2727 if (horizontal_split) {
2728 offset = (get_current_index () != 0) ? panel->widget.lines : -panel->widget.lines;
2729 r = select_charset (0, offset, source_codepage, FALSE);
2730 } else {
2731 offset = (panel->widget.cols == COLS) ? 0
2732 : (get_current_index () != 0) ? panel->widget.cols
2733 : -panel->widget.cols;
2734 r = select_charset (offset, 0, source_codepage, FALSE);
2737 if (r == SELECT_CHARSET_CANCEL)
2738 return; /* Cancel */
2740 if (r == SELECT_CHARSET_NO_TRANSLATE) {
2741 /* No translation */
2742 errmsg = init_translation_table (display_codepage, display_codepage);
2743 cd_path = remove_encoding_from_path (panel->cwd);
2744 do_panel_cd (panel, cd_path, 0);
2745 g_free (cd_path);
2746 return;
2749 source_codepage = r;
2751 errmsg = init_translation_table (source_codepage, display_codepage);
2752 if (errmsg) {
2753 message (D_ERROR, MSG_ERROR, "%s", errmsg);
2754 return;
2757 encoding = get_codepage_id (source_codepage);
2758 #endif
2759 if (encoding != NULL) {
2760 cd_path = add_encoding_to_path (panel->cwd, encoding);
2761 if (!do_panel_cd (panel, cd_path, 0))
2762 message (D_ERROR, MSG_ERROR, _(" Cannot chdir to %s "), cd_path);
2763 g_free (cd_path);
2767 static void
2768 reload_panelized (WPanel *panel)
2770 int i, j;
2771 dir_list *list = &panel->dir;
2773 if (panel != current_panel)
2774 mc_chdir (panel->cwd);
2776 for (i = 0, j = 0; i < panel->count; i++) {
2777 if (list->list[i].f.marked) {
2778 /* Unmark the file in advance. In case the following mc_lstat
2779 * fails we are done, else we have to mark the file again
2780 * (Note: do_file_mark depends on a valid "list->list [i].buf").
2781 * IMO that's the best way to update the panel's summary status
2782 * -- Norbert
2784 do_file_mark (panel, i, 0);
2786 if (mc_lstat (list->list[i].fname, &list->list[i].st)) {
2787 g_free (list->list[i].fname);
2788 continue;
2790 if (list->list[i].f.marked)
2791 do_file_mark (panel, i, 1);
2792 if (j != i)
2793 list->list[j] = list->list[i];
2794 j++;
2796 if (j == 0)
2797 panel->count = set_zero_dir (list);
2798 else
2799 panel->count = j;
2801 if (panel != current_panel)
2802 mc_chdir (current_panel->cwd);
2805 static void
2806 update_one_panel_widget (WPanel *panel, int force_update,
2807 const char *current_file)
2809 int free_pointer;
2810 char *my_current_file = NULL;
2812 if (force_update & UP_RELOAD) {
2813 panel->is_panelized = 0;
2814 mc_setctl (panel->cwd, VFS_SETCTL_FLUSH, 0);
2815 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
2818 /* If current_file == -1 (an invalid pointer) then preserve selection */
2819 if (current_file == UP_KEEPSEL) {
2820 free_pointer = 1;
2821 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
2822 current_file = my_current_file;
2823 } else
2824 free_pointer = 0;
2826 if (panel->is_panelized)
2827 reload_panelized (panel);
2828 else
2829 panel_reload (panel);
2831 try_to_select (panel, current_file);
2832 panel->dirty = 1;
2834 if (free_pointer)
2835 g_free (my_current_file);
2838 static void
2839 update_one_panel (int which, int force_update, const char *current_file)
2841 WPanel *panel;
2843 if (get_display_type (which) != view_listing)
2844 return;
2846 panel = (WPanel *) get_panel_widget (which);
2847 update_one_panel_widget (panel, force_update, current_file);
2850 /* This routine reloads the directory in both panels. It tries to
2851 * select current_file in current_panel and other_file in other_panel.
2852 * If current_file == -1 then it automatically sets current_file and
2853 * other_file to the currently selected files in the panels.
2855 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
2856 * will not reload the other panel.
2858 void
2859 update_panels (int force_update, const char *current_file)
2861 int reload_other = !(force_update & UP_ONLY_CURRENT);
2862 WPanel *panel;
2864 update_one_panel (get_current_index (), force_update, current_file);
2865 if (reload_other)
2866 update_one_panel (get_other_index (), force_update, UP_KEEPSEL);
2868 if (get_current_type () == view_listing)
2869 panel = (WPanel *) get_panel_widget (get_current_index ());
2870 else
2871 panel = (WPanel *) get_panel_widget (get_other_index ());
2873 mc_chdir (panel->cwd);