Merge branch 'DEV_mcview2'
[midnight-commander.git] / src / screen.c
blobcfee8a975714ab63d78ba996fbf3df5a648b55dc
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/tty/color.h"
38 #include "../src/tty/mouse.h" /* For Gpm_Event */
39 #include "../src/tty/key.h" /* XCTRL and ALT macros */
41 #include "dir.h"
42 #include "panel.h"
43 #include "tree.h"
44 #include "ext.h" /* regexp_command */
45 #include "layout.h" /* Most layout variables are here */
46 #include "wtools.h" /* for message (...) */
47 #include "cmd.h"
48 #include "command.h" /* cmdline */
49 #include "setup.h" /* For loading/saving panel options */
50 #include "user.h"
51 #include "../src/mcconfig/mcconfig.h"
52 #include "execute.h"
53 #include "widget.h"
54 #include "menu.h" /* menubar_visible */
55 #include "main-widgets.h"
56 #include "main.h"
57 #include "unixcompat.h"
58 #include "mountlist.h" /* my_statfs */
59 #include "selcodepage.h" /* select_charset () */
60 #include "charsets.h" /* get_codepage_id () */
61 #include "strutil.h"
63 #define ELEMENTS(arr) ( sizeof(arr) / sizeof((arr)[0]) )
65 #define NORMAL 0
66 #define SELECTED 1
67 #define MARKED 2
68 #define MARKED_SELECTED 3
69 #define STATUS 5
72 * This describes a format item. The parse_display_format routine parses
73 * the user specified format and creates a linked list of format_e structures.
75 typedef struct format_e {
76 struct format_e *next;
77 int requested_field_len;
78 int field_len;
79 align_crt_t just_mode;
80 int expand;
81 const char *(*string_fn)(file_entry *, int len);
82 const char *title;
83 const char *id;
84 } format_e;
86 /* If true, show the mini-info on the panel */
87 int show_mini_info = 1;
89 /* If true, then use stat() on the cwd to determine directory changes */
90 int fast_reload = 0;
92 /* If true, use some usability hacks by Torben */
93 int torben_fj_mode = 0;
95 /* If true, up/down keys scroll the pane listing by pages */
96 int panel_scroll_pages = 1;
98 /* If 1, we use permission hilighting */
99 int permission_mode = 0;
101 /* If 1 - then add per file type hilighting */
102 int filetype_mode = 1;
104 /* The hook list for the select file function */
105 Hook *select_file_hook = 0;
107 static cb_ret_t panel_callback (Widget *, widget_msg_t msg, int parm);
108 static int panel_event (Gpm_Event *event, void *);
109 static void paint_frame (WPanel *panel);
110 static const char *panel_format (WPanel *panel);
111 static const char *mini_status_format (WPanel *panel);
113 /* This macro extracts the number of available lines in a panel */
114 #define llines(p) (p->widget.lines-3 - (show_mini_info ? 2 : 0))
116 static void
117 set_colors (WPanel *panel)
119 (void) panel;
120 tty_set_normal_attrs ();
121 tty_setcolor (NORMAL_COLOR);
124 /* Delete format string, it is a linked list */
125 static void
126 delete_format (format_e *format)
128 format_e *next;
130 while (format){
131 next = format->next;
132 g_free (format);
133 format = next;
137 /* This code relies on the default justification!!! */
138 static void
139 add_permission_string (char *dest, int width, file_entry *fe, int attr, int color, int is_octal)
141 int i, r, l;
143 l = get_user_permissions (&fe->st);
145 if (is_octal){
146 /* Place of the access bit in octal mode */
147 l = width + l - 3;
148 r = l + 1;
149 } else {
150 /* The same to the triplet in string mode */
151 l = l * 3 + 1;
152 r = l + 3;
155 for(i = 0; i < width; i++){
156 if (i >= l && i < r){
157 if (attr == SELECTED || attr == MARKED_SELECTED)
158 tty_setcolor (MARKED_SELECTED_COLOR);
159 else
160 tty_setcolor (MARKED_COLOR);
161 } else
162 tty_setcolor (color);
164 tty_print_char (dest[i]);
168 /* String representations of various file attributes */
169 /* name */
170 static const char *
171 string_file_name (file_entry *fe, int len)
173 static char buffer [MC_MAXPATHLEN * MB_LEN_MAX + 1];
175 (void) len;
176 g_strlcpy (buffer, fe->fname, sizeof(buffer));
177 return buffer;
180 static inline unsigned int ilog10(dev_t n)
182 unsigned int digits = 0;
183 do {
184 digits++, n /= 10;
185 } while (n != 0);
186 return digits;
189 static void format_device_number (char *buf, size_t bufsize, dev_t dev)
191 dev_t major_dev = major(dev);
192 dev_t minor_dev = minor(dev);
193 unsigned int major_digits = ilog10(major_dev);
194 unsigned int minor_digits = ilog10(minor_dev);
196 g_assert(bufsize >= 1);
197 if (major_digits + 1 + minor_digits + 1 <= bufsize) {
198 g_snprintf(buf, bufsize, "%lu,%lu", (unsigned long) major_dev,
199 (unsigned long) minor_dev);
200 } else {
201 g_strlcpy(buf, _("[dev]"), bufsize);
205 /* size */
206 static const char *
207 string_file_size (file_entry *fe, int len)
209 static char buffer [BUF_TINY];
211 /* Don't ever show size of ".." since we don't calculate it */
212 if (!strcmp (fe->fname, "..")) {
213 return _("UP--DIR");
216 #ifdef HAVE_STRUCT_STAT_ST_RDEV
217 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
218 format_device_number (buffer, len + 1, fe->st.st_rdev);
219 else
220 #endif
222 size_trunc_len (buffer, len, fe->st.st_size, 0);
224 return buffer;
227 /* bsize */
228 static const char *
229 string_file_size_brief (file_entry *fe, int len)
231 if (S_ISLNK (fe->st.st_mode) && !fe->f.link_to_dir) {
232 return _("SYMLINK");
235 if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && strcmp (fe->fname, "..")) {
236 return _("SUB-DIR");
239 return string_file_size (fe, len);
242 /* This functions return a string representation of a file entry */
243 /* type */
244 static const char *
245 string_file_type (file_entry *fe, int len)
247 static char buffer[2];
249 (void) len;
250 if (S_ISDIR (fe->st.st_mode))
251 buffer[0] = PATH_SEP;
252 else if (S_ISLNK (fe->st.st_mode)) {
253 if (fe->f.link_to_dir)
254 buffer[0] = '~';
255 else if (fe->f.stale_link)
256 buffer[0] = '!';
257 else
258 buffer[0] = '@';
259 } else if (S_ISCHR (fe->st.st_mode))
260 buffer[0] = '-';
261 else if (S_ISSOCK (fe->st.st_mode))
262 buffer[0] = '=';
263 else if (S_ISDOOR (fe->st.st_mode))
264 buffer[0] = '>';
265 else if (S_ISBLK (fe->st.st_mode))
266 buffer[0] = '+';
267 else if (S_ISFIFO (fe->st.st_mode))
268 buffer[0] = '|';
269 else if (S_ISNAM (fe->st.st_mode))
270 buffer[0] = '#';
271 else if (!S_ISREG (fe->st.st_mode))
272 buffer[0] = '?'; /* non-regular of unknown kind */
273 else if (is_exe (fe->st.st_mode))
274 buffer[0] = '*';
275 else
276 buffer[0] = ' ';
277 buffer[1] = '\0';
278 return buffer;
281 /* mtime */
282 static const char *
283 string_file_mtime (file_entry *fe, int len)
285 (void) len;
286 if (!strcmp (fe->fname, "..")) {
287 return "";
289 return file_date (fe->st.st_mtime);
292 /* atime */
293 static const char *
294 string_file_atime (file_entry *fe, int len)
296 (void) len;
297 if (!strcmp (fe->fname, "..")) {
298 return "";
300 return file_date (fe->st.st_atime);
303 /* ctime */
304 static const char *
305 string_file_ctime (file_entry *fe, int len)
307 (void) len;
308 if (!strcmp (fe->fname, "..")) {
309 return "";
311 return file_date (fe->st.st_ctime);
314 /* perm */
315 static const char *
316 string_file_permission (file_entry *fe, int len)
318 (void) len;
319 return string_perm (fe->st.st_mode);
322 /* mode */
323 static const char *
324 string_file_perm_octal (file_entry *fe, int len)
326 static char buffer [10];
328 (void) len;
329 g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode);
330 return buffer;
333 /* nlink */
334 static const char *
335 string_file_nlinks (file_entry *fe, int len)
337 static char buffer[BUF_TINY];
339 (void) len;
340 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
341 return buffer;
344 /* inode */
345 static const char *
346 string_inode (file_entry *fe, int len)
348 static char buffer [10];
350 (void) len;
351 g_snprintf (buffer, sizeof (buffer), "%lu",
352 (unsigned long) fe->st.st_ino);
353 return buffer;
356 /* nuid */
357 static const char *
358 string_file_nuid (file_entry *fe, int len)
360 static char buffer [10];
362 (void) len;
363 g_snprintf (buffer, sizeof (buffer), "%lu",
364 (unsigned long) fe->st.st_uid);
365 return buffer;
368 /* ngid */
369 static const char *
370 string_file_ngid (file_entry *fe, int len)
372 static char buffer [10];
374 (void) len;
375 g_snprintf (buffer, sizeof (buffer), "%lu",
376 (unsigned long) fe->st.st_gid);
377 return buffer;
380 /* owner */
381 static const char *
382 string_file_owner (file_entry *fe, int len)
384 (void) len;
385 return get_owner (fe->st.st_uid);
388 /* group */
389 static const char *
390 string_file_group (file_entry *fe, int len)
392 (void) len;
393 return get_group (fe->st.st_gid);
396 /* mark */
397 static const char *
398 string_marked (file_entry *fe, int len)
400 (void) len;
401 return fe->f.marked ? "*" : " ";
404 /* space */
405 static const char *
406 string_space (file_entry *fe, int len)
408 (void) fe;
409 (void) len;
410 return " ";
413 /* dot */
414 static const char *
415 string_dot (file_entry *fe, int len)
417 (void) fe;
418 (void) len;
419 return ".";
422 #define GT 1
424 static struct {
425 const char *id;
426 int min_size;
427 int expands;
428 align_crt_t default_just;
429 const char *title;
430 int use_in_gui;
431 const char *(*string_fn)(file_entry *, int);
432 sortfn *sort_routine; /* This field is currently unused. */
433 } formats [] = {
434 { "name", 12, 1, J_LEFT_FIT, N_("Name"), 1, string_file_name, (sortfn *) sort_name },
435 { "size", 7, 0, J_RIGHT, N_("Size"), 1, string_file_size, (sortfn *) sort_size },
436 { "bsize", 7, 0, J_RIGHT, N_("Size"), 1, string_file_size_brief, (sortfn *) sort_size },
437 { "type", GT, 0, J_LEFT, "", 2, string_file_type, NULL },
438 { "mtime", 12, 0, J_RIGHT, N_("MTime"), 1, string_file_mtime, (sortfn *) sort_time },
439 { "atime", 12, 0, J_RIGHT, N_("ATime"), 1, string_file_atime, (sortfn *) sort_atime },
440 { "ctime", 12, 0, J_RIGHT, N_("CTime"), 1, string_file_ctime, (sortfn *) sort_ctime },
441 { "perm", 10, 0, J_LEFT, N_("Permission"),1,string_file_permission, NULL },
442 { "mode", 6, 0, J_RIGHT, N_("Perm"), 1, string_file_perm_octal, NULL },
443 { "nlink", 2, 0, J_RIGHT, N_("Nl"), 1, string_file_nlinks, NULL },
444 { "inode", 5, 0, J_RIGHT, N_("Inode"), 1, string_inode, (sortfn *) sort_inode },
445 { "nuid", 5, 0, J_RIGHT, N_("UID"), 1, string_file_nuid, NULL },
446 { "ngid", 5, 0, J_RIGHT, N_("GID"), 1, string_file_ngid, NULL },
447 { "owner", 8, 0, J_LEFT_FIT, N_("Owner"), 1, string_file_owner, NULL },
448 { "group", 8, 0, J_LEFT_FIT, N_("Group"), 1, string_file_group, NULL },
449 { "mark", 1, 0, J_RIGHT, " ", 1, string_marked, NULL },
450 { "|", 1, 0, J_RIGHT, " ", 0, NULL, NULL },
451 { "space", 1, 0, J_RIGHT, " ", 0, string_space, NULL },
452 { "dot", 1, 0, J_RIGHT, " ", 0, string_dot, NULL },
455 static int
456 file_compute_color (int attr, file_entry *fe)
458 switch (attr) {
459 case SELECTED:
460 return (SELECTED_COLOR);
461 case MARKED:
462 return (MARKED_COLOR);
463 case MARKED_SELECTED:
464 return (MARKED_SELECTED_COLOR);
465 case STATUS:
466 return (NORMAL_COLOR);
467 case NORMAL:
468 default:
469 if (!filetype_mode)
470 return (NORMAL_COLOR);
473 /* if filetype_mode == true */
474 if (S_ISDIR (fe->st.st_mode))
475 return (DIRECTORY_COLOR);
476 else if (S_ISLNK (fe->st.st_mode)) {
477 if (fe->f.link_to_dir)
478 return (DIRECTORY_COLOR);
479 else if (fe->f.stale_link)
480 return (STALE_LINK_COLOR);
481 else
482 return (LINK_COLOR);
483 } else if (S_ISSOCK (fe->st.st_mode))
484 return (SPECIAL_COLOR);
485 else if (S_ISCHR (fe->st.st_mode))
486 return (DEVICE_COLOR);
487 else if (S_ISBLK (fe->st.st_mode))
488 return (DEVICE_COLOR);
489 else if (S_ISNAM (fe->st.st_mode))
490 return (DEVICE_COLOR);
491 else if (S_ISFIFO (fe->st.st_mode))
492 return (SPECIAL_COLOR);
493 else if (S_ISDOOR (fe->st.st_mode))
494 return (SPECIAL_COLOR);
495 else if (!S_ISREG (fe->st.st_mode))
496 return (STALE_LINK_COLOR); /* non-regular file of unknown kind */
497 else if (is_exe (fe->st.st_mode))
498 return (EXECUTABLE_COLOR);
499 else if (fe->fname && (!strcmp (fe->fname, "core")
500 || !strcmp (extension (fe->fname), "core")))
501 return (CORE_COLOR);
503 return (NORMAL_COLOR);
506 /* Formats the file number file_index of panel in the buffer dest */
507 static void
508 format_file (char *dest, int limit, WPanel *panel, int file_index, int width, int attr, int isstatus)
510 int color, length, empty_line;
511 const char *txt;
512 format_e *format, *home;
513 file_entry *fe;
515 (void) dest;
516 (void) limit;
517 length = 0;
518 empty_line = (file_index >= panel->count);
519 home = (isstatus) ? panel->status_format : panel->format;
520 fe = &panel->dir.list [file_index];
522 if (!empty_line)
523 color = file_compute_color (attr, fe);
524 else
525 color = NORMAL_COLOR;
527 for (format = home; format; format = format->next){
528 if (length == width)
529 break;
531 if (format->string_fn){
532 int len, perm;
533 char *preperad_text;
535 if (empty_line)
536 txt = " ";
537 else
538 txt = (*format->string_fn)(fe, format->field_len);
540 len = format->field_len;
541 if (len + length > width)
542 len = width - length;
543 if (len <= 0)
544 break;
546 perm = 0;
547 if (permission_mode) {
548 if (!strcmp(format->id, "perm"))
549 perm = 1;
550 else if (!strcmp(format->id, "mode"))
551 perm = 2;
554 tty_setcolor (color);
556 preperad_text = (char*) str_fit_to_term(txt, len, format->just_mode);
557 if (perm)
558 add_permission_string (preperad_text, format->field_len, fe,
559 attr, color, perm - 1);
560 else
561 tty_print_string (preperad_text);
563 length+= len;
564 } else {
565 if (attr == SELECTED || attr == MARKED_SELECTED)
566 tty_setcolor (SELECTED_COLOR);
567 else
568 tty_setcolor (NORMAL_COLOR);
569 tty_print_one_vline ();
570 length++;
574 if (length < width)
575 tty_draw_hline (-1, -1, ' ', width - length);
578 static void
579 repaint_file (WPanel *panel, int file_index, int mv, int attr, int isstatus)
581 int second_column = 0;
582 int width;
583 int offset = 0;
584 char buffer [BUF_MEDIUM];
586 gboolean panel_is_split = !isstatus && panel->split;
588 width = panel->widget.cols - 2;
590 if (panel_is_split) {
591 second_column = (file_index - panel->top_file) / llines (panel);
592 width = width/2 - 1;
594 if (second_column != 0) {
595 offset = 1 + width;
596 /*width = (panel->widget.cols-2) - (panel->widget.cols-2)/2 - 1;*/
597 width = panel->widget.cols - offset - 2;
601 /* Nothing to paint */
602 if (width <= 0)
603 return;
605 if (mv){
606 if (panel_is_split)
607 widget_move (&panel->widget,
608 (file_index - panel->top_file) % 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 (panel_is_split) {
617 if (second_column)
618 tty_print_char (' ');
619 else {
620 tty_setcolor (NORMAL_COLOR);
621 tty_print_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 tty_setcolor (INPUT_COLOR);
633 tty_print_char ('/');
634 tty_print_string (str_fit_to_term (panel->search_buffer,
635 panel->widget.cols - 3, J_LEFT));
636 return;
639 /* Status resolves links and show them */
640 set_colors (panel);
642 if (S_ISLNK (panel->dir.list [panel->selected].st.st_mode)){
643 char *link, link_target [MC_MAXPATHLEN];
644 int len;
646 link = concat_dir_and_file (panel->cwd, panel->dir.list [panel->selected].fname);
647 len = mc_readlink (link, link_target, MC_MAXPATHLEN - 1);
648 g_free (link);
649 if (len > 0){
650 link_target[len] = 0;
651 tty_print_string ("-> ");
652 tty_print_string (str_fit_to_term (link_target, panel->widget.cols - 5,
653 J_LEFT_FIT));
654 } else
655 tty_print_string (str_fit_to_term (_("<readlink failed>"),
656 panel->widget.cols - 2, J_LEFT));
657 } else if (strcmp (panel->dir.list [panel->selected].fname, "..") == 0) {
658 /* FIXME:
659 * while loading directory (do_load_dir() and do_reload_dir()),
660 * the actual stat info about ".." directory isn't got;
661 * so just don't display incorrect info about ".." directory */
662 tty_print_string (str_fit_to_term (_("UP--DIR"), panel->widget.cols - 2, J_LEFT));
663 } else
664 /* Default behavior */
665 repaint_file (panel, panel->selected, 0, STATUS, 1);
668 static void
669 paint_dir (WPanel *panel)
671 int i;
672 int color; /* Color value of the line */
673 int items; /* Number of items */
675 items = llines (panel) * (panel->split ? 2 : 1);
677 for (i = 0; i < items; i++){
678 if (i+panel->top_file >= panel->count)
679 color = 0;
680 else {
681 color = 2 * (panel->dir.list [i+panel->top_file].f.marked);
682 color += (panel->selected==i+panel->top_file && panel->active);
684 repaint_file (panel, i+panel->top_file, 1, color, 0);
686 tty_set_normal_attrs ();
689 static void
690 display_total_marked_size (WPanel *panel, int y, int x, gboolean size_only)
692 char buffer[BUF_SMALL], b_bytes[BUF_SMALL], *buf;
693 int cols;
694 size_t blen;
696 if (panel->marked <= 0)
697 return;
699 buf = size_only ? b_bytes : buffer;
700 cols = panel->widget.cols - 2;
703 * This is a trick to use two ngettext() calls in one sentence.
704 * First make "N bytes", then insert it into "X in M files".
706 g_snprintf (b_bytes, sizeof (b_bytes),
707 ngettext("%s byte", "%s bytes", (unsigned long) panel->total),
708 size_trunc_sep (panel->total));
709 if (!size_only)
710 g_snprintf (buffer, sizeof (buffer),
711 ngettext("%s in %d file", "%s in %d files", panel->marked),
712 b_bytes, panel->marked);
714 blen = strlen (buf);
716 /* don't forget spaces around buffer content */
717 if ((int) blen > cols - 2) {
718 buf[cols - 2] = '\0';
719 blen = (size_t) (cols - 2);
722 if (x < 0)
723 /* center in panel */
724 x = (panel->widget.cols - (int) blen) / 2 - 1;
727 * y == llines (panel) + 2 for mini_info_separator
728 * y == panel->widget.lines - 1 for panel bottom frame
730 widget_move (&panel->widget, y, x);
731 tty_setcolor (MARKED_COLOR);
732 tty_printf (" %s ", buf);
735 static void
736 mini_info_separator (WPanel *panel)
738 const int y = llines (panel) + 2;
740 tty_setcolor (NORMAL_COLOR);
741 tty_draw_hline (panel->widget.y + y, panel->widget.x + 1,
742 tty_is_slow () ? '-' : ACS_HLINE,
743 panel->widget.cols - 2);
744 /* Status displays total marked size.
745 * Centered in panel, full format. */
746 display_total_marked_size (panel, y, -1, FALSE);
749 static void
750 show_free_space (WPanel *panel)
752 /* Used to figure out how many free space we have */
753 static struct my_statfs myfs_stats;
754 /* Old current working directory for displaying free space */
755 static char *old_cwd = NULL;
757 /* Don't try to stat non-local fs */
758 if (!vfs_file_is_local (panel->cwd) || !free_space)
759 return;
761 if (old_cwd == NULL || strcmp (old_cwd, panel->cwd) != 0) {
762 char rpath[PATH_MAX];
764 init_my_statfs ();
765 g_free (old_cwd);
766 old_cwd = g_strdup (panel->cwd);
768 if (mc_realpath (panel->cwd, rpath) == NULL)
769 return;
771 my_statfs (&myfs_stats, rpath);
774 if (myfs_stats.avail > 0 || myfs_stats.total > 0) {
775 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
776 size_trunc_len (buffer1, sizeof(buffer1) - 1, myfs_stats.avail, 1);
777 size_trunc_len (buffer2, sizeof(buffer2) - 1, myfs_stats.total, 1);
778 g_snprintf (tmp, sizeof(tmp), " %s/%s (%d%%) ", buffer1, buffer2,
779 myfs_stats.total > 0 ?
780 (int)(100 * (double)myfs_stats.avail / myfs_stats.total) : 0);
781 widget_move (&panel->widget, panel->widget.lines - 1,
782 panel->widget.cols - 2 - (int) strlen (tmp));
783 tty_setcolor (NORMAL_COLOR);
784 tty_print_string (tmp);
788 static void
789 show_dir (WPanel *panel)
791 set_colors (panel);
792 draw_box (panel->widget.parent,
793 panel->widget.y, panel->widget.x,
794 panel->widget.lines, panel->widget.cols);
796 if (show_mini_info && !tty_is_slow ()) {
797 widget_move (&panel->widget, llines (panel) + 2, 0);
798 tty_print_alt_char (ACS_LTEE);
799 widget_move (&panel->widget, llines (panel) + 2,
800 panel->widget.cols - 1);
801 tty_print_alt_char (ACS_RTEE);
804 if (panel->active)
805 tty_setcolor (REVERSE_COLOR);
807 widget_move (&panel->widget, 0, 3);
809 tty_printf (" %s ",
810 str_term_trim (strip_home_and_password (panel->cwd),
811 min (max (panel->widget.cols - 9, 0),
812 panel->widget.cols)));
814 widget_move (&panel->widget, 0, 1);
815 tty_print_string ("<");
816 widget_move (&panel->widget, 0, panel->widget.cols - 2);
817 tty_print_string (">");
818 widget_move (&panel->widget, 0, panel->widget.cols - 3);
819 tty_print_string ("v");
821 if (!show_mini_info) {
822 if (panel->marked == 0) {
823 /* Show size of curret file in the bottom of panel */
824 if (S_ISREG (panel->dir.list [panel->selected].st.st_mode)) {
825 char buffer[BUF_SMALL];
827 g_snprintf (buffer, sizeof (buffer), " %s ",
828 size_trunc_sep (panel->dir.list [panel->selected].st.st_size));
829 tty_setcolor (NORMAL_COLOR);
830 widget_move (&panel->widget, panel->widget.lines - 1, 2);
831 tty_print_string (buffer);
833 } else {
834 /* Show total size of marked files
835 * In the bottom of panel, display size only. */
836 display_total_marked_size (panel, panel->widget.lines - 1, 2, TRUE);
840 show_free_space (panel);
842 if (panel->active)
843 tty_set_normal_attrs ();
846 /* To be used only by long_frame and full_frame to adjust top_file */
847 static void
848 adjust_top_file (WPanel *panel)
850 int old_top = panel->top_file;
852 if (panel->selected - old_top > llines (panel))
853 panel->top_file = panel->selected;
854 if (old_top - panel->count > llines (panel))
855 panel->top_file = panel->count - llines (panel);
858 /* Repaint everything, including frame and separator */
859 static void
860 paint_panel (WPanel *panel)
862 paint_frame (panel); /* including show_dir */
863 paint_dir (panel);
865 if (show_mini_info) {
866 mini_info_separator (panel);
867 display_mini_info (panel);
870 panel->dirty = 0;
873 /* add "#enc:encodning" to end of path */
874 /* if path end width a previous #enc:, only encoding is changed no additional
875 * #enc: is appended
876 * retun new string
878 static char
879 *add_encoding_to_path (const char *path, const char *encoding)
881 char *result;
882 char *semi;
883 char *slash;
885 semi = g_strrstr (path, "#enc:");
887 if (semi != NULL) {
888 slash = strchr (semi, PATH_SEP);
889 if (slash != NULL) {
890 result = g_strconcat (path, "/#enc:", encoding, NULL);
891 } else {
892 *semi = 0;
893 result = g_strconcat (path, "/#enc:", encoding, NULL);
894 *semi = '#';
896 } else {
897 result = g_strconcat (path, "/#enc:", encoding, NULL);
900 return result;
903 char *
904 remove_encoding_from_path (const char *path)
906 GString *ret;
907 GString *tmp_path, *tmp_conv;
908 char *tmp, *tmp2;
909 const char *enc;
910 GIConv converter;
912 ret = g_string_new("");
913 tmp_conv = g_string_new("");
915 tmp_path = g_string_new(path);
917 while ((tmp = g_strrstr (tmp_path->str, "/#enc:")) != NULL){
918 enc = vfs_get_encoding ((const char *) tmp);
919 converter = enc ? str_crt_conv_to (enc): str_cnv_to_term;
920 if (converter == INVALID_CONV) converter = str_cnv_to_term;
922 tmp2=tmp+1;
923 while (*tmp2 && *tmp2 != '/')
924 tmp2++;
926 if (*tmp2){
927 str_vfs_convert_from (converter, tmp2, tmp_conv);
928 g_string_prepend(ret, tmp_conv->str);
929 g_string_set_size(tmp_conv,0);
931 g_string_set_size(tmp_path,tmp - tmp_path->str);
932 str_close_conv (converter);
934 g_string_prepend(ret, tmp_path->str);
935 g_string_free(tmp_path,TRUE);
936 g_string_free(tmp_conv,TRUE);
938 tmp = ret->str;
939 g_string_free(ret, FALSE);
940 return tmp;
944 * Repaint the contents of the panels without frames. To schedule panel
945 * for repainting, set panel->dirty to 1. There are many reasons why
946 * the panels need to be repainted, and this is a costly operation, so
947 * it's done once per event.
949 void
950 update_dirty_panels (void)
952 if (current_panel->dirty)
953 paint_panel (current_panel);
955 if ((get_other_type () == view_listing) && other_panel->dirty)
956 paint_panel (other_panel);
959 static void
960 do_select (WPanel *panel, int i)
962 if (i != panel->selected) {
963 panel->dirty = 1;
964 panel->selected = i;
965 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
966 if (panel->top_file < 0)
967 panel->top_file = 0;
971 static inline void
972 do_try_to_select (WPanel *panel, const char *name)
974 int i;
975 char *subdir;
977 if (!name) {
978 do_select(panel, 0);
979 return;
982 /* We only want the last component of the directory,
983 * and from this only the name without suffix. */
984 subdir = vfs_strip_suffix_from_filename (x_basename(name));
986 /* Search that subdirectory, if found select it */
987 for (i = 0; i < panel->count; i++){
988 if (strcmp (subdir, panel->dir.list [i].fname) == 0) {
989 do_select (panel, i);
990 g_free (subdir);
991 return;
995 /* Try to select a file near the file that is missing */
996 if (panel->selected >= panel->count)
997 do_select (panel, panel->count-1);
998 g_free (subdir);
1001 void
1002 try_to_select (WPanel *panel, const char *name)
1004 do_try_to_select (panel, name);
1005 select_item (panel);
1008 void
1009 panel_update_cols (Widget *widget, int frame_size)
1011 int cols, origin;
1013 if (horizontal_split){
1014 widget->cols = COLS;
1015 return;
1018 if (frame_size == frame_full){
1019 cols = COLS;
1020 origin = 0;
1021 } else {
1022 if (widget == get_panel_widget (0)){
1023 cols = first_panel_size;
1024 origin = 0;
1025 } else {
1026 cols = COLS-first_panel_size;
1027 origin = first_panel_size;
1031 widget->cols = cols;
1032 widget->x = origin;
1035 static char *
1036 panel_save_name (WPanel *panel)
1038 extern int saving_setup;
1040 /* If the program is shuting down */
1041 if ((midnight_shutdown && auto_save_setup) || saving_setup)
1042 return g_strdup (panel->panel_name);
1043 else
1044 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1047 void
1048 panel_clean_dir (WPanel *panel)
1050 int count = panel->count;
1052 panel->count = 0;
1053 panel->top_file = 0;
1054 panel->selected = 0;
1055 panel->marked = 0;
1056 panel->dirs_marked = 0;
1057 panel->total = 0;
1058 panel->searching = 0;
1059 panel->is_panelized = 0;
1060 panel->dirty = 1;
1062 clean_dir (&panel->dir, count);
1065 static void
1066 panel_destroy (WPanel *p)
1068 int i;
1070 char *name = panel_save_name (p);
1072 panel_save_setup (p, name);
1073 panel_clean_dir (p);
1075 /* save and clean history */
1076 if (p->dir_history) {
1077 history_put (p->hist_name, p->dir_history);
1079 p->dir_history = g_list_first (p->dir_history);
1080 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1081 g_list_free (p->dir_history);
1084 g_free (p->hist_name);
1086 delete_format (p->format);
1087 delete_format (p->status_format);
1089 g_free (p->user_format);
1090 for (i = 0; i < LIST_TYPES; i++)
1091 g_free (p->user_status_format[i]);
1092 g_free (p->dir.list);
1093 g_free (p->panel_name);
1094 g_free (name);
1097 static void
1098 panel_format_modified (WPanel *panel)
1100 panel->format_modified = 1;
1103 /* Panel creation */
1104 /* The parameter specifies the name of the panel for setup retieving */
1105 WPanel *
1106 panel_new (const char *panel_name)
1108 return panel_new_with_dir(panel_name, NULL);
1111 /* Panel creation for specified directory */
1112 /* The parameter specifies the name of the panel for setup retieving */
1113 /* and the path of working panel directory. If path is NULL then */
1114 /* panel will be created for current directory */
1115 WPanel *
1116 panel_new_with_dir (const char *panel_name, const char *wpath)
1118 WPanel *panel;
1119 char *section;
1120 int i, err;
1121 char curdir[MC_MAXPATHLEN];
1123 panel = g_new0 (WPanel, 1);
1125 /* No know sizes of the panel at startup */
1126 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
1128 /* We do not want the cursor */
1129 widget_want_cursor (panel->widget, 0);
1131 if (wpath) {
1132 g_strlcpy(panel->cwd, wpath, sizeof (panel->cwd));
1133 mc_get_current_wd (curdir, sizeof (curdir) - 2);
1134 } else
1135 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
1137 strcpy (panel->lwd, ".");
1139 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
1140 panel->dir_history = history_get (panel->hist_name);
1141 directory_history_add (panel, panel->cwd);
1143 panel->dir.list = g_new (file_entry, MIN_FILES);
1144 panel->dir.size = MIN_FILES;
1145 panel->active = 0;
1146 panel->filter = 0;
1147 panel->split = 0;
1148 panel->top_file = 0;
1149 panel->selected = 0;
1150 panel->marked = 0;
1151 panel->total = 0;
1152 panel->reverse = 0;
1153 panel->dirty = 1;
1154 panel->searching = 0;
1155 panel->dirs_marked = 0;
1156 panel->is_panelized = 0;
1157 panel->format = 0;
1158 panel->status_format = 0;
1159 panel->format_modified = 1;
1161 panel->panel_name = g_strdup (panel_name);
1162 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
1164 for (i = 0; i < LIST_TYPES; i++)
1165 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
1167 panel->search_buffer[0] = 0;
1168 panel->frame_size = frame_half;
1169 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1170 if (!mc_config_has_group (mc_main_config, section)) {
1171 g_free (section);
1172 section = g_strdup (panel->panel_name);
1174 panel_load_setup (panel, section);
1175 g_free (section);
1177 /* Load format strings */
1178 err = set_panel_formats (panel);
1179 if (err) {
1180 set_panel_formats (panel);
1184 /* Because do_load_dir lists files in current directory */
1185 if (wpath)
1186 mc_chdir(wpath);
1188 /* Load the default format */
1189 panel->count =
1190 do_load_dir (panel->cwd, &panel->dir, panel->sort_type,
1191 panel->reverse, panel->case_sensitive,
1192 panel->exec_first, panel->filter);
1194 /* Restore old right path */
1195 if (wpath)
1196 mc_chdir(curdir);
1198 return panel;
1201 void
1202 panel_reload (WPanel *panel)
1204 struct stat current_stat;
1206 if (fast_reload && !stat (panel->cwd, &current_stat)
1207 && current_stat.st_ctime == panel->dir_stat.st_ctime
1208 && current_stat.st_mtime == panel->dir_stat.st_mtime)
1209 return;
1211 while (mc_chdir (panel->cwd) == -1) {
1212 char *last_slash;
1214 if (panel->cwd[0] == PATH_SEP && panel->cwd[1] == 0) {
1215 panel_clean_dir (panel);
1216 panel->count = set_zero_dir (&panel->dir);
1217 return;
1219 last_slash = strrchr (panel->cwd, PATH_SEP);
1220 if (!last_slash || last_slash == panel->cwd)
1221 strcpy (panel->cwd, PATH_SEP_STR);
1222 else
1223 *last_slash = 0;
1224 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
1225 show_dir (panel);
1228 panel->count =
1229 do_reload_dir (panel->cwd, &panel->dir, panel->sort_type,
1230 panel->count, panel->reverse, panel->case_sensitive,
1231 panel->exec_first, panel->filter);
1233 panel->dirty = 1;
1234 if (panel->selected >= panel->count)
1235 do_select (panel, panel->count - 1);
1237 recalculate_panel_summary (panel);
1240 static void
1241 paint_frame (WPanel *panel)
1243 int side, width;
1244 char *txt = NULL;
1246 if (!panel->split)
1247 adjust_top_file (panel);
1249 widget_erase (&panel->widget);
1250 show_dir (panel);
1252 widget_move (&panel->widget, 1, 1);
1254 for (side = 0; side <= panel->split; side++){
1255 format_e *format;
1257 if (side){
1258 tty_setcolor (NORMAL_COLOR);
1259 tty_print_one_vline ();
1260 width = panel->widget.cols - panel->widget.cols/2 - 1;
1261 } else if (panel->split)
1262 width = panel->widget.cols/2 - 3;
1263 else
1264 width = panel->widget.cols - 2;
1266 for (format = panel->format; format; format = format->next){
1267 if (format->string_fn){
1268 if (panel->filter && !strcmp (format->id, "name")) {
1269 txt = g_strdup_printf ("%s [%s]", format->title, panel->filter);
1270 } else {
1271 txt = g_strdup (format->title);
1274 tty_setcolor (MARKED_COLOR);
1275 tty_print_string (str_fit_to_term (format->title, format->field_len,
1276 J_CENTER_LEFT));
1277 g_free(txt);
1278 width -= format->field_len;
1279 } else {
1280 tty_setcolor (NORMAL_COLOR);
1281 tty_print_one_vline ();
1282 width--;
1286 if (width > 0)
1287 tty_draw_hline (-1, -1, ' ', width);
1291 static const char *
1292 parse_panel_size (WPanel *panel, const char *format, int isstatus)
1294 int frame = frame_half;
1295 format = skip_separators (format);
1297 if (!strncmp (format, "full", 4)){
1298 frame = frame_full;
1299 format += 4;
1300 } else if (!strncmp (format, "half", 4)){
1301 frame = frame_half;
1302 format += 4;
1305 if (!isstatus){
1306 panel->frame_size = frame;
1307 panel->split = 0;
1310 /* Now, the optional column specifier */
1311 format = skip_separators (format);
1313 if (*format == '1' || *format == '2'){
1314 if (!isstatus)
1315 panel->split = *format == '2';
1316 format++;
1319 if (!isstatus)
1320 panel_update_cols (&(panel->widget), panel->frame_size);
1322 return skip_separators (format);
1325 /* Format is:
1327 all := panel_format? format
1328 panel_format := [full|half] [1|2]
1329 format := one_format_e
1330 | format , one_format_e
1332 one_format_e := just format.id [opt_size]
1333 just := [<=>]
1334 opt_size := : size [opt_expand]
1335 size := [0-9]+
1336 opt_expand := +
1340 static format_e *
1341 parse_display_format (WPanel *panel, const char *format, char **error, int isstatus, int *res_total_cols)
1343 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1344 int total_cols = 0; /* Used columns by the format */
1345 int set_justify; /* flag: set justification mode? */
1346 align_crt_t justify = J_LEFT; /* Which mode. */
1347 int items = 0; /* Number of items in the format */
1348 size_t i;
1350 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1352 *error = 0;
1354 if (i18n_timelength == 0) {
1355 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1357 for (i = 0; i < ELEMENTS(formats); i++)
1358 if (strcmp ("time", formats [i].id+1) == 0)
1359 formats [i].min_size = i18n_timelength;
1363 * This makes sure that the panel and mini status full/half mode
1364 * setting is equal
1366 format = parse_panel_size (panel, format, isstatus);
1368 while (*format){ /* format can be an empty string */
1369 int found = 0;
1371 darr = g_new (format_e, 1);
1373 /* I'm so ugly, don't look at me :-) */
1374 if (!home)
1375 home = old = darr;
1377 old->next = darr;
1378 darr->next = 0;
1379 old = darr;
1381 format = skip_separators (format);
1383 if (strchr ("<=>", *format)){
1384 set_justify = 1;
1385 switch (*format)
1387 case '<':
1388 justify = J_LEFT;
1389 break;
1390 case '=':
1391 justify = J_CENTER;
1392 break;
1393 case '>':
1394 default:
1395 justify = J_RIGHT;
1396 break;
1398 format = skip_separators (format+1);
1399 } else
1400 set_justify = 0;
1402 for (i = 0; i < ELEMENTS(formats); i++){
1403 size_t klen = strlen (formats [i].id);
1405 if (strncmp (format, formats [i].id, klen) != 0)
1406 continue;
1408 format += klen;
1410 if (formats [i].use_in_gui)
1411 items++;
1413 darr->requested_field_len = formats [i].min_size;
1414 darr->string_fn = formats [i].string_fn;
1415 if (formats [i].title [0])
1416 darr->title = _(formats [i].title);
1417 else
1418 darr->title = "";
1419 darr->id = formats [i].id;
1420 darr->expand = formats [i].expands;
1421 darr->just_mode = formats [i].default_just;
1423 if (set_justify) {
1424 if (IS_FIT(darr->just_mode))
1425 darr->just_mode = MAKE_FIT(justify);
1426 else
1427 darr->just_mode = justify;
1429 found = 1;
1431 format = skip_separators (format);
1433 /* If we have a size specifier */
1434 if (*format == ':'){
1435 int req_length;
1437 /* If the size was specified, we don't want
1438 * auto-expansion by default
1440 darr->expand = 0;
1441 format++;
1442 req_length = atoi (format);
1443 darr->requested_field_len = req_length;
1445 format = skip_numbers (format);
1447 /* Now, if they insist on expansion */
1448 if (*format == '+'){
1449 darr->expand = 1;
1450 format++;
1455 break;
1457 if (!found){
1458 char *tmp_format = g_strdup (format);
1460 int pos = min (8, strlen (format));
1461 delete_format (home);
1462 tmp_format [pos] = 0;
1463 *error = g_strconcat (_("Unknown tag on display format: "), tmp_format, (char *) NULL);
1464 g_free (tmp_format);
1465 return 0;
1467 total_cols += darr->requested_field_len;
1470 *res_total_cols = total_cols;
1471 return home;
1474 static format_e *
1475 use_display_format (WPanel *panel, const char *format, char **error, int isstatus)
1477 #define MAX_EXPAND 4
1478 int expand_top = 0; /* Max used element in expand */
1479 int usable_columns; /* Usable columns in the panel */
1480 int total_cols = 0;
1481 int i;
1482 format_e *darr, *home;
1484 if (!format)
1485 format = DEFAULT_USER_FORMAT;
1487 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1489 if (*error)
1490 return 0;
1492 panel->dirty = 1;
1494 /* Status needn't to be split */
1495 usable_columns = ((panel->widget.cols-2)/((isstatus)
1497 : (panel->split+1))) - (!isstatus && panel->split);
1499 /* Look for the expandable fields and set field_len based on the requested field len */
1500 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next){
1501 darr->field_len = darr->requested_field_len;
1502 if (darr->expand)
1503 expand_top++;
1506 /* If we used more columns than the available columns, adjust that */
1507 if (total_cols > usable_columns){
1508 int pdif, dif = total_cols - usable_columns;
1510 while (dif){
1511 pdif = dif;
1512 for (darr = home; darr; darr = darr->next){
1513 if (dif && darr->field_len - 1){
1514 darr->field_len--;
1515 dif--;
1519 /* avoid endless loop if num fields > 40 */
1520 if (pdif == dif)
1521 break;
1523 total_cols = usable_columns; /* give up, the rest should be truncated */
1526 /* Expand the available space */
1527 if ((usable_columns > total_cols) && expand_top){
1528 int spaces = (usable_columns - total_cols) / expand_top;
1529 int extra = (usable_columns - total_cols) % expand_top;
1531 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1532 if (darr->expand){
1533 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1534 i++;
1537 return home;
1540 /* Switches the panel to the mode specified in the format */
1541 /* Seting up both format and status string. Return: 0 - on success; */
1542 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
1544 set_panel_formats (WPanel *p)
1546 format_e *form;
1547 char *err;
1548 int retcode = 0;
1550 form = use_display_format (p, panel_format (p), &err, 0);
1552 if (err){
1553 g_free (err);
1554 retcode = 1;
1556 else {
1557 if (p->format)
1558 delete_format (p->format);
1560 p->format = form;
1563 if (show_mini_info){
1565 form = use_display_format (p, mini_status_format (p), &err, 1);
1567 if (err){
1568 g_free (err);
1569 retcode += 2;
1571 else {
1572 if (p->status_format)
1573 delete_format (p->status_format);
1575 p->status_format = form;
1579 panel_format_modified (p);
1580 panel_update_cols (&(p->widget), p->frame_size);
1582 if (retcode)
1583 message (D_ERROR, _("Warning" ), _( "User supplied format looks invalid, reverting to default." ) );
1584 if (retcode & 0x01){
1585 g_free (p->user_format);
1586 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
1588 if (retcode & 0x02){
1589 g_free (p->user_status_format [p->list_type]);
1590 p->user_status_format [p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
1593 return retcode;
1596 /* Given the panel->view_type returns the format string to be parsed */
1597 static const char *
1598 panel_format (WPanel *panel)
1600 switch (panel->list_type){
1602 case list_long:
1603 return "full perm space nlink space owner space group space size space mtime space name";
1605 case list_brief:
1606 return "half 2 type name";
1608 case list_user:
1609 return panel->user_format;
1611 default:
1612 case list_full:
1613 return "half type name | size | mtime";
1617 static const char *
1618 mini_status_format (WPanel *panel)
1620 if (panel->user_mini_status)
1621 return panel->user_status_format [panel->list_type];
1623 switch (panel->list_type){
1625 case list_long:
1626 return "full perm space nlink space owner space group space size space mtime space name";
1628 case list_brief:
1629 return "half type name space bsize space perm space";
1631 case list_full:
1632 return "half type name";
1634 default:
1635 case list_user:
1636 return panel->user_format;
1640 /* */
1641 /* Panel operation commands */
1642 /* */
1644 /* Used to emulate Lynx's entering leaving a directory with the arrow keys */
1645 static cb_ret_t
1646 maybe_cd (int move_up_dir)
1648 if (navigate_with_arrows) {
1649 if (!cmdline->buffer[0]) {
1650 if (move_up_dir) {
1651 do_cd ("..", cd_exact);
1652 return MSG_HANDLED;
1654 if (S_ISDIR (selection (current_panel)->st.st_mode)
1655 || link_isdir (selection (current_panel))) {
1656 do_cd (selection (current_panel)->fname, cd_exact);
1657 return MSG_HANDLED;
1661 return MSG_NOT_HANDLED;
1664 /* Returns the number of items in the given panel */
1665 static int
1666 ITEMS (WPanel *p)
1668 if (p->split)
1669 return llines (p) * 2;
1670 else
1671 return llines (p);
1674 /* Select current item and readjust the panel */
1675 void
1676 select_item (WPanel *panel)
1678 int items = ITEMS (panel);
1680 /* Although currently all over the code we set the selection and
1681 top file to decent values before calling select_item, I could
1682 forget it someday, so it's better to do the actual fitting here */
1684 if (panel->top_file < 0)
1685 panel->top_file = 0;
1687 if (panel->selected < 0)
1688 panel->selected = 0;
1690 if (panel->selected > panel->count - 1)
1691 panel->selected = panel->count - 1;
1693 if (panel->top_file > panel->count - 1)
1694 panel->top_file = panel->count - 1;
1696 if ((panel->count - panel->top_file) < items) {
1697 panel->top_file = panel->count - items;
1698 if (panel->top_file < 0)
1699 panel->top_file = 0;
1702 if (panel->selected < panel->top_file)
1703 panel->top_file = panel->selected;
1705 if ((panel->selected - panel->top_file) >= items)
1706 panel->top_file = panel->selected - items + 1;
1708 panel->dirty = 1;
1710 execute_hooks (select_file_hook);
1713 /* Clears all files in the panel, used only when one file was marked */
1714 void
1715 unmark_files (WPanel *panel)
1717 int i;
1719 if (!panel->marked)
1720 return;
1721 for (i = 0; i < panel->count; i++)
1722 file_mark (panel, i, 0);
1724 panel->dirs_marked = 0;
1725 panel->marked = 0;
1726 panel->total = 0;
1729 static void
1730 unselect_item (WPanel *panel)
1732 repaint_file (panel, panel->selected, 1, 2*selection (panel)->f.marked, 0);
1735 static void
1736 move_down (WPanel *panel)
1738 if (panel->selected+1 == panel->count)
1739 return;
1741 unselect_item (panel);
1742 panel->selected++;
1743 if (panel->selected - panel->top_file == ITEMS (panel) &&
1744 panel_scroll_pages) {
1745 /* Scroll window half screen */
1746 panel->top_file += ITEMS (panel)/2;
1747 if (panel->top_file > panel->count - ITEMS (panel))
1748 panel->top_file = panel->count - ITEMS (panel);
1749 paint_dir (panel);
1751 select_item (panel);
1754 static void
1755 move_up (WPanel *panel)
1757 if (panel->selected == 0)
1758 return;
1760 unselect_item (panel);
1761 panel->selected--;
1762 if (panel->selected < panel->top_file && panel_scroll_pages) {
1763 /* Scroll window half screen */
1764 panel->top_file -= ITEMS (panel)/2;
1765 if (panel->top_file < 0)
1766 panel->top_file = 0;
1767 paint_dir (panel);
1769 select_item (panel);
1772 /* Changes the selection by lines (may be negative) */
1773 static void
1774 move_selection (WPanel *panel, int lines)
1776 int new_pos;
1777 int adjust = 0;
1779 new_pos = panel->selected + lines;
1780 if (new_pos >= panel->count)
1781 new_pos = panel->count-1;
1783 if (new_pos < 0)
1784 new_pos = 0;
1786 unselect_item (panel);
1787 panel->selected = new_pos;
1789 if (panel->selected - panel->top_file >= ITEMS (panel)){
1790 panel->top_file += lines;
1791 adjust = 1;
1794 if (panel->selected - panel->top_file < 0){
1795 panel->top_file += lines;
1796 adjust = 1;
1799 if (adjust){
1800 if (panel->top_file > panel->selected)
1801 panel->top_file = panel->selected;
1802 if (panel->top_file < 0)
1803 panel->top_file = 0;
1804 paint_dir (panel);
1806 select_item (panel);
1809 static cb_ret_t
1810 move_left (WPanel *panel, int c_code)
1812 (void) c_code;
1813 if (panel->split) {
1814 move_selection (panel, -llines (panel));
1815 return MSG_HANDLED;
1816 } else
1817 return maybe_cd (1); /* cd .. */
1820 static int
1821 move_right (WPanel *panel, int c_code)
1823 (void) c_code;
1824 if (panel->split) {
1825 move_selection (panel, llines (panel));
1826 return MSG_HANDLED;
1827 } else
1828 return maybe_cd (0); /* cd (selection) */
1831 static void
1832 prev_page (WPanel *panel)
1834 int items;
1836 if (!panel->selected && !panel->top_file)
1837 return;
1838 unselect_item (panel);
1839 items = ITEMS (panel);
1840 if (panel->top_file < items)
1841 items = panel->top_file;
1842 if (!items)
1843 panel->selected = 0;
1844 else
1845 panel->selected -= items;
1846 panel->top_file -= items;
1848 /* This keeps the selection in a reasonable place */
1849 if (panel->selected < 0)
1850 panel->selected = 0;
1851 if (panel->top_file < 0)
1852 panel->top_file = 0;
1853 select_item (panel);
1854 paint_dir (panel);
1857 static void
1858 ctrl_prev_page (WPanel *panel)
1860 (void) panel;
1861 do_cd ("..", cd_exact);
1864 static void
1865 next_page (WPanel *panel)
1867 int items;
1869 if (panel->selected == panel->count - 1)
1870 return;
1871 unselect_item (panel);
1872 items = ITEMS (panel);
1873 if (panel->top_file > panel->count - 2 * items)
1874 items = panel->count - items - panel->top_file;
1875 if (panel->top_file + items < 0)
1876 items = -panel->top_file;
1877 if (!items)
1878 panel->selected = panel->count - 1;
1879 else
1880 panel->selected += items;
1881 panel->top_file += items;
1883 /* This keeps the selection in it's relative position */
1884 if (panel->selected >= panel->count)
1885 panel->selected = panel->count - 1;
1886 if (panel->top_file >= panel->count)
1887 panel->top_file = panel->count - 1;
1888 select_item (panel);
1889 paint_dir (panel);
1892 static void
1893 ctrl_next_page (WPanel *panel)
1895 if ((S_ISDIR (selection (panel)->st.st_mode)
1896 || link_isdir (selection (panel)))) {
1897 do_cd (selection (panel)->fname, cd_exact);
1901 static void
1902 goto_top_file (WPanel *panel)
1904 unselect_item (panel);
1905 panel->selected = panel->top_file;
1906 select_item (panel);
1909 static void
1910 goto_middle_file (WPanel *panel)
1912 unselect_item (panel);
1913 panel->selected = panel->top_file + (ITEMS (panel)/2);
1914 if (panel->selected >= panel->count)
1915 panel->selected = panel->count - 1;
1916 select_item (panel);
1919 static void
1920 goto_bottom_file (WPanel *panel)
1922 unselect_item (panel);
1923 panel->selected = panel->top_file + ITEMS (panel)-1;
1924 if (panel->selected >= panel->count)
1925 panel->selected = panel->count - 1;
1926 select_item (panel);
1929 static void
1930 move_home (WPanel *panel)
1932 if (panel->selected == 0)
1933 return;
1934 unselect_item (panel);
1936 if (torben_fj_mode){
1937 int middle_pos = panel->top_file + (ITEMS (panel)/2);
1939 if (panel->selected > middle_pos){
1940 goto_middle_file (panel);
1941 return;
1943 if (panel->selected != panel->top_file){
1944 goto_top_file (panel);
1945 return;
1949 panel->top_file = 0;
1950 panel->selected = 0;
1952 paint_dir (panel);
1953 select_item (panel);
1956 static void
1957 move_end (WPanel *panel)
1959 if (panel->selected == panel->count-1)
1960 return;
1961 unselect_item (panel);
1962 if (torben_fj_mode){
1963 int middle_pos = panel->top_file + (ITEMS (panel)/2);
1965 if (panel->selected < middle_pos){
1966 goto_middle_file (panel);
1967 return;
1969 if (panel->selected != (panel->top_file + ITEMS(panel)-1)){
1970 goto_bottom_file (panel);
1971 return;
1975 panel->selected = panel->count-1;
1976 paint_dir (panel);
1977 select_item (panel);
1980 /* Recalculate the panels summary information, used e.g. when marked
1981 files might have been removed by an external command */
1982 void
1983 recalculate_panel_summary (WPanel *panel)
1985 int i;
1987 panel->marked = 0;
1988 panel->dirs_marked = 0;
1989 panel->total = 0;
1991 for (i = 0; i < panel->count; i++)
1992 if (panel->dir.list [i].f.marked){
1993 /* do_file_mark will return immediately if newmark == oldmark.
1994 So we have to first unmark it to get panel's summary information
1995 updated. (Norbert) */
1996 panel->dir.list [i].f.marked = 0;
1997 do_file_mark (panel, i, 1);
2001 /* This routine marks a file or a directory */
2002 void
2003 do_file_mark (WPanel *panel, int idx, int mark)
2005 if (panel->dir.list[idx].f.marked == mark)
2006 return;
2008 /* Only '..' can't be marked, '.' isn't visible */
2009 if (!strcmp (panel->dir.list[idx].fname, ".."))
2010 return;
2012 file_mark (panel, idx, mark);
2013 if (panel->dir.list[idx].f.marked) {
2014 panel->marked++;
2015 if (S_ISDIR (panel->dir.list[idx].st.st_mode)) {
2016 if (panel->dir.list[idx].f.dir_size_computed)
2017 panel->total += panel->dir.list[idx].st.st_size;
2018 panel->dirs_marked++;
2019 } else
2020 panel->total += panel->dir.list[idx].st.st_size;
2021 set_colors (panel);
2022 } else {
2023 if (S_ISDIR (panel->dir.list[idx].st.st_mode)) {
2024 if (panel->dir.list[idx].f.dir_size_computed)
2025 panel->total -= panel->dir.list[idx].st.st_size;
2026 panel->dirs_marked--;
2027 } else
2028 panel->total -= panel->dir.list[idx].st.st_size;
2029 panel->marked--;
2033 static void
2034 do_mark_file (WPanel *panel, int do_move)
2036 do_file_mark (panel, panel->selected,
2037 selection (panel)->f.marked ? 0 : 1);
2038 if (mark_moves_down && do_move)
2039 move_down (panel);
2042 static void
2043 mark_file (WPanel *panel)
2045 do_mark_file (panel, 1);
2048 /* Incremental search of a file name in the panel */
2049 static void
2050 do_search (WPanel *panel, int c_code)
2052 size_t l, max, buf_max;
2053 int i, sel;
2054 int wrapped = 0;
2055 char *act;
2057 l = strlen (panel->search_buffer);
2058 if (c_code == KEY_BACKSPACE) {
2059 if (l != 0) {
2060 act = panel->search_buffer + l;
2061 str_prev_noncomb_char (&act, panel->search_buffer);
2062 act[0] = '\0';
2064 panel->search_chpoint = 0;
2065 } else {
2066 if (c_code && (gsize) panel->search_chpoint < sizeof (panel->search_char)) {
2067 panel->search_char[panel->search_chpoint] = c_code;
2068 panel->search_chpoint++;
2071 if (panel->search_chpoint > 0) {
2072 switch (str_is_valid_char (panel->search_char, panel->search_chpoint)) {
2073 case -2:
2074 return;
2075 case -1:
2076 panel->search_chpoint = 0;
2077 return;
2078 default:
2079 if (l + panel->search_chpoint < sizeof (panel->search_buffer)) {
2080 memcpy (panel->search_buffer + l, panel->search_char,
2081 panel->search_chpoint);
2082 l+= panel->search_chpoint;
2083 (panel->search_buffer + l)[0] = '\0';
2084 panel->search_chpoint = 0;
2090 buf_max = panel->case_sensitive ?
2091 str_prefix (panel->search_buffer, panel->search_buffer) :
2092 str_caseprefix (panel->search_buffer, panel->search_buffer);
2093 max = 0;
2094 sel = panel->selected;
2095 for (i = panel->selected; !wrapped || i != panel->selected; i++) {
2096 if (i >= panel->count) {
2097 i = 0;
2098 if (wrapped)
2099 break;
2100 wrapped = 1;
2102 l = panel->case_sensitive ?
2103 str_prefix (panel->dir.list[i].fname, panel->search_buffer) :
2104 str_caseprefix (panel->dir.list[i].fname, panel->search_buffer);
2105 if (l > max) {
2106 max = l;
2107 sel = i;
2108 if (max == buf_max) break;
2112 unselect_item (panel);
2113 panel->selected = sel;
2114 select_item (panel);
2116 act = panel->search_buffer + strlen (panel->search_buffer);
2117 while (max < buf_max) {
2118 str_prev_char_safe (&act);
2119 act[0] = '\0';
2120 buf_max = panel->case_sensitive ?
2121 str_prefix (panel->search_buffer, panel->search_buffer) :
2122 str_caseprefix (panel->search_buffer, panel->search_buffer);
2125 paint_panel (panel);
2128 static void
2129 start_search (WPanel *panel)
2131 if (panel->searching){
2132 if (panel->selected+1 == panel->count)
2133 panel->selected = 0;
2134 else
2135 move_down (panel);
2136 do_search (panel, 0);
2137 } else {
2138 panel->searching = 1;
2139 panel->search_buffer[0] = '\0';
2140 panel->search_char[0] = '\0';
2141 panel->search_chpoint = 0;
2142 display_mini_info (panel);
2143 mc_refresh ();
2147 /* Return 1 if the Enter key has been processed, 0 otherwise */
2148 static int
2149 do_enter_on_file_entry (file_entry *fe)
2151 char *full_name;
2154 * Directory or link to directory - change directory.
2155 * Try the same for the entries on which mc_lstat() has failed.
2157 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe)
2158 || (fe->st.st_mode == 0)) {
2159 if (!do_cd (fe->fname, cd_exact))
2160 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2161 return 1;
2164 /* Try associated command */
2165 if (regex_command (fe->fname, "Open", 0) != 0)
2166 return 1;
2168 /* Check if the file is executable */
2169 full_name = concat_dir_and_file (current_panel->cwd, fe->fname);
2170 if (!is_exe (fe->st.st_mode) || !if_link_is_exe (full_name, fe)) {
2171 g_free (full_name);
2172 return 0;
2174 g_free (full_name);
2176 if (confirm_execute) {
2177 if (query_dialog
2178 (_(" The Midnight Commander "),
2179 _(" Do you really want to execute? "), D_NORMAL, 2, _("&Yes"),
2180 _("&No")) != 0)
2181 return 1;
2183 #ifdef USE_VFS
2184 if (!vfs_current_is_local ()) {
2185 char *tmp;
2186 int ret;
2188 tmp = concat_dir_and_file (vfs_get_current_dir (), fe->fname);
2189 ret = mc_setctl (tmp, VFS_SETCTL_RUN, NULL);
2190 g_free (tmp);
2191 /* We took action only if the dialog was shown or the execution
2192 * was successful */
2193 return confirm_execute || (ret == 0);
2195 #endif
2198 char *tmp = name_quote (fe->fname, 0);
2199 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2200 g_free (tmp);
2201 shell_execute (cmd, 0);
2202 g_free (cmd);
2205 return 1;
2208 static int
2209 do_enter (WPanel *panel)
2211 return do_enter_on_file_entry (selection (panel));
2214 static void
2215 chdir_other_panel (WPanel *panel)
2217 char *new_dir;
2218 char *sel_entry = NULL;
2220 if (get_other_type () != view_listing) {
2221 set_display_type (get_other_index (), view_listing);
2224 if (!S_ISDIR (panel->dir.list [panel->selected].st.st_mode)) {
2225 new_dir = concat_dir_and_file (panel->cwd, "..");
2226 sel_entry = strrchr(panel->cwd, PATH_SEP);
2227 } else
2228 new_dir = concat_dir_and_file (panel->cwd, panel->dir.list [panel->selected].fname);
2230 change_panel ();
2231 do_cd (new_dir, cd_exact);
2232 if (sel_entry)
2233 try_to_select (current_panel, sel_entry);
2234 change_panel ();
2236 move_down (panel);
2238 g_free (new_dir);
2242 * Make the current directory of the current panel also the current
2243 * directory of the other panel. Put the other panel to the listing
2244 * mode if needed. If the current panel is panelized, the other panel
2245 * doesn't become panelized.
2247 static void
2248 sync_other_panel (WPanel *panel)
2250 if (get_other_type () != view_listing) {
2251 set_display_type (get_other_index (), view_listing);
2254 do_panel_cd (other_panel, current_panel->cwd, cd_exact);
2256 /* try to select current filename on the other panel */
2257 if (!panel->is_panelized) {
2258 try_to_select (other_panel, selection (panel)->fname);
2262 static void
2263 chdir_to_readlink (WPanel *panel)
2265 char *new_dir;
2267 if (get_other_type () != view_listing)
2268 return;
2270 if (S_ISLNK (panel->dir.list [panel->selected].st.st_mode)) {
2271 char buffer [MC_MAXPATHLEN], *p;
2272 int i;
2273 struct stat st;
2275 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2276 if (i < 0)
2277 return;
2278 if (mc_stat (selection (panel)->fname, &st) < 0)
2279 return;
2280 buffer [i] = 0;
2281 if (!S_ISDIR (st.st_mode)) {
2282 p = strrchr (buffer, PATH_SEP);
2283 if (p && !p[1]) {
2284 *p = 0;
2285 p = strrchr (buffer, PATH_SEP);
2287 if (!p)
2288 return;
2289 p[1] = 0;
2291 if (*buffer == PATH_SEP)
2292 new_dir = g_strdup (buffer);
2293 else
2294 new_dir = concat_dir_and_file (panel->cwd, buffer);
2296 change_panel ();
2297 do_cd (new_dir, cd_exact);
2298 change_panel ();
2300 move_down (panel);
2302 g_free (new_dir);
2306 typedef void (*panel_key_callback) (WPanel *);
2307 typedef struct {
2308 int key_code;
2309 panel_key_callback fn;
2310 } panel_key_map;
2312 static void cmd_do_enter(WPanel *wp) { (void) do_enter(wp); }
2313 static void cmd_view_simple(WPanel *wp) { (void) wp; view_simple_cmd(); }
2314 static void cmd_edit_new(WPanel *wp) { (void) wp; edit_cmd_new(); }
2315 static void cmd_copy_local(WPanel *wp) { (void) wp;copy_cmd_local(); }
2316 static void cmd_rename_local(WPanel *wp) { (void) wp;ren_cmd_local(); }
2317 static void cmd_delete_local(WPanel *wp) { (void) wp;delete_cmd_local(); }
2318 static void cmd_select(WPanel *wp) { (void) wp;select_cmd(); }
2319 static void cmd_unselect(WPanel *wp) { (void) wp;unselect_cmd(); }
2320 static void cmd_reverse_selection(WPanel *wp) { (void) wp;reverse_selection_cmd(); }
2322 static const panel_key_map panel_keymap [] = {
2323 { KEY_DOWN, move_down },
2324 { KEY_UP, move_up },
2326 /* The action button :-) */
2327 { '\n', cmd_do_enter },
2328 { KEY_ENTER, cmd_do_enter },
2330 { KEY_IC, mark_file },
2331 { KEY_HOME, move_home },
2332 { KEY_A1, move_home },
2333 { ALT ('<'), move_home },
2334 { KEY_C1, move_end },
2335 { KEY_END, move_end },
2336 { ALT ('>'), move_end },
2337 { KEY_NPAGE, next_page },
2338 { KEY_PPAGE, prev_page },
2339 { KEY_NPAGE | KEY_M_CTRL, ctrl_next_page },
2340 { KEY_PPAGE | KEY_M_CTRL, ctrl_prev_page },
2342 /* To quickly move in the panel */
2343 { ALT('g'), goto_top_file },
2344 { ALT('r'), goto_middle_file }, /* M-r like emacs */
2345 { ALT('j'), goto_bottom_file },
2347 /* Emacs-like bindings */
2348 { XCTRL('v'), next_page }, /* C-v like emacs */
2349 { ALT('v'), prev_page }, /* M-v like emacs */
2350 { XCTRL('p'), move_up }, /* C-p like emacs */
2351 { XCTRL('n'), move_down }, /* C-n like emacs */
2352 { XCTRL('s'), start_search }, /* C-s like emacs */
2353 { ALT('s'), start_search }, /* M-s not like emacs */
2354 /* { XCTRL('t'), mark_file },*/
2355 { XCTRL('t'), set_panel_encoding },
2356 { ALT('o'), chdir_other_panel },
2357 { ALT('i'), sync_other_panel },
2358 { ALT('l'), chdir_to_readlink },
2359 { ALT('H'), directory_history_list },
2360 { KEY_F(13), cmd_view_simple },
2361 { KEY_F(14), cmd_edit_new },
2362 { KEY_F(15), cmd_copy_local },
2363 { KEY_F(16), cmd_rename_local },
2364 { KEY_F(18), cmd_delete_local },
2365 { ALT('y'), directory_history_prev },
2366 { ALT('u'), directory_history_next },
2367 { ALT('+'), cmd_select },
2368 { KEY_KP_ADD, cmd_select },
2369 { ALT('\\'), cmd_unselect },
2370 { ALT('-'), cmd_unselect },
2371 { KEY_KP_SUBTRACT, cmd_unselect },
2372 { ALT('*'), cmd_reverse_selection },
2373 { KEY_KP_MULTIPLY, cmd_reverse_selection },
2374 { 0, 0 }
2377 static inline cb_ret_t
2378 panel_key (WPanel *panel, int key)
2380 int i;
2382 for (i = 0; panel_keymap[i].key_code; i++) {
2383 if (key == panel_keymap[i].key_code) {
2384 int old_searching = panel->searching;
2386 if (panel_keymap[i].fn != start_search)
2387 panel->searching = 0;
2389 (*panel_keymap[i].fn) (panel);
2391 if (panel->searching != old_searching)
2392 display_mini_info (panel);
2393 return MSG_HANDLED;
2396 if (torben_fj_mode && key == ALT ('h')) {
2397 goto_middle_file (panel);
2398 return MSG_HANDLED;
2401 /* We do not want to take a key press if nothing can be done with it */
2402 /* The command line widget may do something more useful */
2403 if (key == KEY_LEFT)
2404 return move_left (panel, key);
2406 if (key == KEY_RIGHT)
2407 return move_right (panel, key);
2409 if (is_abort_char (key)) {
2410 panel->searching = 0;
2411 display_mini_info (panel);
2412 return MSG_HANDLED;
2415 /* Do not eat characters not meant for the panel below ' ' (e.g. C-l). */
2416 if ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE) {
2417 if (panel->searching) {
2418 do_search (panel, key);
2419 return MSG_HANDLED;
2422 if (!command_prompt) {
2423 start_search (panel);
2424 do_search (panel, key);
2425 return MSG_HANDLED;
2429 return MSG_NOT_HANDLED;
2432 static cb_ret_t
2433 panel_callback (Widget *w, widget_msg_t msg, int parm)
2435 WPanel *panel = (WPanel *) w;
2436 Dlg_head *h = panel->widget.parent;
2438 switch (msg) {
2439 case WIDGET_DRAW:
2440 paint_panel (panel);
2441 return MSG_HANDLED;
2443 case WIDGET_FOCUS:
2444 current_panel = panel;
2445 panel->active = 1;
2446 if (mc_chdir (panel->cwd) != 0) {
2447 char *cwd = strip_password (g_strdup (panel->cwd), 1);
2448 message (D_ERROR, MSG_ERROR, _(" Cannot chdir to \"%s\" \n %s "),
2449 cwd, unix_error_string (errno));
2450 g_free(cwd);
2451 } else
2452 subshell_chdir (panel->cwd);
2454 update_xterm_title_path ();
2455 select_item (panel);
2456 show_dir (panel);
2457 paint_dir (panel);
2458 panel->dirty = 0;
2460 buttonbar_set_label (h, 1, _("Help"), help_cmd);
2461 buttonbar_set_label (h, 2, _("Menu"), user_file_menu_cmd);
2462 buttonbar_set_label (h, 3, _("View"), view_cmd);
2463 buttonbar_set_label (h, 4, _("Edit"), edit_cmd);
2464 buttonbar_set_label (h, 5, _("Copy"), copy_cmd);
2465 buttonbar_set_label (h, 6, _("RenMov"), ren_cmd);
2466 buttonbar_set_label (h, 7, _("Mkdir"), mkdir_cmd);
2467 buttonbar_set_label (h, 8, _("Delete"), delete_cmd);
2468 buttonbar_redraw (h);
2469 return MSG_HANDLED;
2471 case WIDGET_UNFOCUS:
2472 /* Janne: look at this for the multiple panel options */
2473 if (panel->searching){
2474 panel->searching = 0;
2475 display_mini_info (panel);
2477 panel->active = 0;
2478 show_dir (panel);
2479 unselect_item (panel);
2480 return MSG_HANDLED;
2482 case WIDGET_KEY:
2483 return panel_key (panel, parm);
2485 case WIDGET_DESTROY:
2486 panel_destroy (panel);
2487 return MSG_HANDLED;
2489 default:
2490 return default_proc (msg, parm);
2494 void
2495 file_mark (WPanel *panel, int index, int val)
2497 if (panel->dir.list[index].f.marked != val) {
2498 panel->dir.list[index].f.marked = val;
2499 panel->dirty = 1;
2503 /* */
2504 /* Panel mouse events support routines */
2505 /* */
2506 static int mouse_marking = 0;
2508 static void
2509 mouse_toggle_mark (WPanel *panel)
2511 do_mark_file (panel, 0);
2512 mouse_marking = selection (panel)->f.marked;
2515 static void
2516 mouse_set_mark (WPanel *panel)
2518 if (mouse_marking && !(selection (panel)->f.marked))
2519 do_mark_file (panel, 0);
2520 else if (!mouse_marking && (selection (panel)->f.marked))
2521 do_mark_file (panel, 0);
2524 static inline int
2525 mark_if_marking (WPanel *panel, Gpm_Event *event)
2527 if (event->buttons & GPM_B_RIGHT){
2528 if (event->type & GPM_DOWN)
2529 mouse_toggle_mark (panel);
2530 else
2531 mouse_set_mark (panel);
2532 return 1;
2534 return 0;
2538 * Mouse callback of the panel minus repainting.
2539 * If the event is redirected to the menu, *redir is set to 1.
2541 static int
2542 do_panel_event (Gpm_Event *event, WPanel *panel, int *redir)
2544 const int lines = llines (panel);
2546 int my_index;
2548 /* Mouse wheel events */
2549 if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN)) {
2550 prev_page (panel);
2551 return MOU_NORMAL;
2553 if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN)) {
2554 next_page (panel);
2555 return MOU_NORMAL;
2558 /* "<" button */
2559 if (event->type & GPM_DOWN && event->x == 2 && event->y == 1) {
2560 directory_history_prev (panel);
2561 return MOU_NORMAL;
2564 /* ">" button */
2565 if (event->type & GPM_DOWN && event->x == panel->widget.cols - 1
2566 && event->y == 1) {
2567 directory_history_next (panel);
2568 return MOU_NORMAL;
2571 /* "v" button */
2572 if (event->type & GPM_DOWN && event->x == panel->widget.cols - 2
2573 && event->y == 1) {
2574 directory_history_list (panel);
2575 return MOU_NORMAL;
2578 /* rest of the upper frame, the menu is invisible - call menu */
2579 if (event->type & GPM_DOWN && event->y == 1 && !menubar_visible) {
2580 *redir = 1;
2581 event->x += panel->widget.x;
2582 return (*(the_menubar->widget.mouse)) (event, the_menubar);
2585 event->y -= 2;
2586 if ((event->type & (GPM_DOWN | GPM_DRAG))) {
2588 if (!dlg_widget_active (panel))
2589 change_panel ();
2591 if (event->y <= 0) {
2592 mark_if_marking (panel, event);
2593 if (mouse_move_pages)
2594 prev_page (panel);
2595 else
2596 move_up (panel);
2597 return MOU_REPEAT;
2600 if (!((panel->top_file + event->y <= panel->count)
2601 && event->y <= lines)) {
2602 mark_if_marking (panel, event);
2603 if (mouse_move_pages)
2604 next_page (panel);
2605 else
2606 move_down (panel);
2607 return MOU_REPEAT;
2609 my_index = panel->top_file + event->y - 1;
2610 if (panel->split) {
2611 if (event->x > ((panel->widget.cols - 2) / 2))
2612 my_index += llines (panel);
2615 if (my_index >= panel->count)
2616 my_index = panel->count - 1;
2618 if (my_index != panel->selected) {
2619 unselect_item (panel);
2620 panel->selected = my_index;
2621 select_item (panel);
2624 /* This one is new */
2625 mark_if_marking (panel, event);
2627 } else if ((event->type & (GPM_UP | GPM_DOUBLE)) ==
2628 (GPM_UP | GPM_DOUBLE)) {
2629 if (event->y > 0 && event->y <= lines)
2630 do_enter (panel);
2632 return MOU_NORMAL;
2635 /* Mouse callback of the panel */
2636 static int
2637 panel_event (Gpm_Event *event, void *data)
2639 WPanel *panel = data;
2640 int ret;
2641 int redir = 0;
2643 ret = do_panel_event (event, panel, &redir);
2644 if (!redir)
2645 paint_panel (panel);
2647 return ret;
2650 void
2651 panel_re_sort (WPanel *panel)
2653 char *filename;
2654 int i;
2656 if (panel == NULL)
2657 return;
2659 filename = g_strdup (selection (panel)->fname);
2660 unselect_item (panel);
2661 do_sort (&panel->dir, panel->sort_type, panel->count-1, panel->reverse,
2662 panel->case_sensitive, panel->exec_first);
2663 panel->selected = -1;
2664 for (i = panel->count; i; i--){
2665 if (!strcmp (panel->dir.list [i-1].fname, filename)){
2666 panel->selected = i-1;
2667 break;
2670 g_free (filename);
2671 panel->top_file = panel->selected - ITEMS (panel)/2;
2672 if (panel->top_file < 0)
2673 panel->top_file = 0;
2674 select_item (panel);
2675 panel->dirty = 1;
2678 void
2679 panel_set_sort_order (WPanel *panel, sortfn *sort_order)
2681 if (sort_order == 0)
2682 return;
2684 panel->sort_type = sort_order;
2686 /* The directory is already sorted, we have to load the unsorted stuff */
2687 if (sort_order == (sortfn *) unsorted){
2688 char *current_file;
2690 current_file = g_strdup (panel->dir.list [panel->selected].fname);
2691 panel_reload (panel);
2692 try_to_select (panel, current_file);
2693 g_free (current_file);
2695 panel_re_sort (panel);
2698 void
2699 set_panel_encoding (WPanel *panel)
2701 const char *encoding = NULL;
2702 char *cd_path;
2703 #ifdef HAVE_CHARSET
2704 const char *errmsg;
2705 int r;
2706 int width = (panel->widget.x)? panel->widget.cols : panel->widget.cols * (-1);
2708 r = select_charset (width, 0, source_codepage, FALSE);
2710 if (r == SELECT_CHARSET_CANCEL)
2711 return; /* Cancel */
2713 if (r == SELECT_CHARSET_NO_TRANSLATE) {
2714 /* No translation */
2715 errmsg = init_translation_table (display_codepage, display_codepage);
2716 cd_path = remove_encoding_from_path (panel->cwd);
2717 do_panel_cd (panel, cd_path, 0);
2718 g_free (cd_path);
2719 return;
2722 source_codepage = r;
2724 errmsg = init_translation_table (source_codepage, display_codepage);
2725 if (errmsg) {
2726 message (D_ERROR, MSG_ERROR, "%s", errmsg);
2727 return;
2730 encoding = get_codepage_id (source_codepage);
2731 #endif
2732 if (encoding != NULL) {
2733 cd_path = add_encoding_to_path (panel->cwd, encoding);
2734 if (!do_panel_cd (panel, cd_path, 0))
2735 message (D_ERROR, MSG_ERROR, _(" Cannot chdir to %s "), cd_path);
2736 g_free (cd_path);
2740 static void
2741 reload_panelized (WPanel *panel)
2743 int i, j;
2744 dir_list *list = &panel->dir;
2746 if (panel != current_panel)
2747 mc_chdir (panel->cwd);
2749 for (i = 0, j = 0; i < panel->count; i++) {
2750 if (list->list[i].f.marked) {
2751 /* Unmark the file in advance. In case the following mc_lstat
2752 * fails we are done, else we have to mark the file again
2753 * (Note: do_file_mark depends on a valid "list->list [i].buf").
2754 * IMO that's the best way to update the panel's summary status
2755 * -- Norbert
2757 do_file_mark (panel, i, 0);
2759 if (mc_lstat (list->list[i].fname, &list->list[i].st)) {
2760 g_free (list->list[i].fname);
2761 continue;
2763 if (list->list[i].f.marked)
2764 do_file_mark (panel, i, 1);
2765 if (j != i)
2766 list->list[j] = list->list[i];
2767 j++;
2769 if (j == 0)
2770 panel->count = set_zero_dir (list);
2771 else
2772 panel->count = j;
2774 if (panel != current_panel)
2775 mc_chdir (current_panel->cwd);
2778 static void
2779 update_one_panel_widget (WPanel *panel, int force_update,
2780 const char *current_file)
2782 int free_pointer;
2783 char *my_current_file = NULL;
2785 if (force_update & UP_RELOAD) {
2786 panel->is_panelized = 0;
2787 mc_setctl (panel->cwd, VFS_SETCTL_FLUSH, 0);
2788 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
2791 /* If current_file == -1 (an invalid pointer) then preserve selection */
2792 if (current_file == UP_KEEPSEL) {
2793 free_pointer = 1;
2794 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
2795 current_file = my_current_file;
2796 } else
2797 free_pointer = 0;
2799 if (panel->is_panelized)
2800 reload_panelized (panel);
2801 else
2802 panel_reload (panel);
2804 try_to_select (panel, current_file);
2805 panel->dirty = 1;
2807 if (free_pointer)
2808 g_free (my_current_file);
2811 static void
2812 update_one_panel (int which, int force_update, const char *current_file)
2814 WPanel *panel;
2816 if (get_display_type (which) != view_listing)
2817 return;
2819 panel = (WPanel *) get_panel_widget (which);
2820 update_one_panel_widget (panel, force_update, current_file);
2823 /* This routine reloads the directory in both panels. It tries to
2824 * select current_file in current_panel and other_file in other_panel.
2825 * If current_file == -1 then it automatically sets current_file and
2826 * other_file to the currently selected files in the panels.
2828 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
2829 * will not reload the other panel.
2831 void
2832 update_panels (int force_update, const char *current_file)
2834 int reload_other = !(force_update & UP_ONLY_CURRENT);
2835 WPanel *panel;
2837 update_one_panel (get_current_index (), force_update, current_file);
2838 if (reload_other)
2839 update_one_panel (get_other_index (), force_update, UP_KEEPSEL);
2841 if (get_current_type () == view_listing)
2842 panel = (WPanel *) get_panel_widget (get_current_index ());
2843 else
2844 panel = (WPanel *) get_panel_widget (get_other_index ());
2846 mc_chdir (panel->cwd);