root/src/filemanager/layout.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. max
  2. check_split
  3. update_split
  4. b_left_right_cback
  5. bplus_cback
  6. bminus_cback
  7. layout_bg_callback
  8. layout_callback
  9. layout_dlg_create
  10. panel_do_cols
  11. restore_into_right_dir_panel
  12. layout_save
  13. layout_restore
  14. layout_change
  15. layout_box
  16. panel_update_cols
  17. setup_panels
  18. panels_split_equal
  19. panels_split_more
  20. panels_split_less
  21. setup_cmdline
  22. use_dash
  23. set_hintbar
  24. rotate_dash
  25. get_nth_panel_name
  26. create_panel
  27. swap_panels
  28. get_panel_type
  29. get_panel_widget
  30. get_current_index
  31. get_other_index
  32. get_other_panel
  33. get_current_type
  34. get_other_type
  35. save_panel_dir
  36. get_panel_dir_for
  37. do_load_prompt
  38. load_prompt
  39. title_path_prepare
  40. update_xterm_title_path
  41. update_terminal_cwd

   1 /*
   2    Panel layout module for the Midnight Commander
   3 
   4    Copyright (C) 1995-2024
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Janne Kukonlehto, 1995
   9    Miguel de Icaza, 1995
  10    Andrew Borodin <aborodin@vmail.ru>, 2011-2022
  11    Slava Zanko <slavazanko@gmail.com>, 2013
  12    Avi Kelman <patcherton.fixesthings@gmail.com>, 2013
  13 
  14    This file is part of the Midnight Commander.
  15 
  16    The Midnight Commander is free software: you can redistribute it
  17    and/or modify it under the terms of the GNU General Public License as
  18    published by the Free Software Foundation, either version 3 of the License,
  19    or (at your option) any later version.
  20 
  21    The Midnight Commander is distributed in the hope that it will be useful,
  22    but WITHOUT ANY WARRANTY; without even the implied warranty of
  23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24    GNU General Public License for more details.
  25 
  26    You should have received a copy of the GNU General Public License
  27    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  28  */
  29 
  30 /** \file layout.c
  31  *  \brief Source: panel layout module
  32  */
  33 
  34 #include <config.h>
  35 
  36 #include <pwd.h>                /* for username in xterm title */
  37 #include <stdlib.h>
  38 #include <stdio.h>
  39 #include <string.h>
  40 #include <sys/types.h>
  41 #include <unistd.h>
  42 
  43 #include "lib/global.h"
  44 #include "lib/tty/tty.h"
  45 #include "lib/skin.h"
  46 #include "lib/tty/key.h"
  47 #include "lib/tty/mouse.h"
  48 #include "lib/mcconfig.h"
  49 #include "lib/vfs/vfs.h"        /* vfs_get_cwd () */
  50 #include "lib/strutil.h"
  51 #include "lib/widget.h"
  52 #include "lib/event.h"
  53 #include "lib/util.h"           /* mc_time_elapsed() */
  54 
  55 #include "src/consaver/cons.saver.h"
  56 #include "src/viewer/mcviewer.h"        /* The view widget */
  57 #include "src/setup.h"
  58 #ifdef ENABLE_SUBSHELL
  59 #include "src/subshell/subshell.h"
  60 #endif
  61 
  62 #include "command.h"
  63 #include "filemanager.h"
  64 #include "tree.h"
  65 /* Needed for the extern declarations of integer parameters */
  66 #include "dir.h"
  67 #include "layout.h"
  68 #include "info.h"               /* The Info widget */
  69 
  70 /*** global variables ****************************************************************************/
  71 
  72 panels_layout_t panels_layout = {
  73     /* Set if the panels are split horizontally */
  74     .horizontal_split = FALSE,
  75 
  76     /* vertical split */
  77     .vertical_equal = TRUE,
  78     .left_panel_size = 0,
  79 
  80     /* horizontal split */
  81     .horizontal_equal = TRUE,
  82     .top_panel_size = 0
  83 };
  84 
  85 /* Controls the display of the rotating dash on the verbose mode */
  86 gboolean nice_rotating_dash = TRUE;
  87 
  88 /* The number of output lines shown (if available) */
  89 int output_lines = 0;
  90 
  91 /* Set if the command prompt is to be displayed */
  92 gboolean command_prompt = TRUE;
  93 
  94 /* Set if the main menu is visible */
  95 gboolean menubar_visible = TRUE;
  96 
  97 /* Set to show current working dir in xterm window title */
  98 gboolean xterm_title = TRUE;
  99 
 100 /* Set to show free space on device assigned to current directory */
 101 gboolean free_space = TRUE;
 102 
 103 /* The starting line for the output of the subprogram */
 104 int output_start_y = 0;
 105 
 106 int ok_to_refresh = 1;
 107 
 108 /*** file scope macro definitions ****************************************************************/
 109 
 110 /* The maximum number of views managed by the create_panel routine */
 111 /* Must be at least two (for current and other).  Please note that until */
 112 /* Janne gets around this, we will only manage two of them :-) */
 113 #define MAX_VIEWS 2
 114 
 115 /* Width 12 for a wee Quick (Hex) View */
 116 #define MINWIDTH 12
 117 #define MINHEIGHT 5
 118 
 119 #define B_2LEFT B_USER
 120 #define B_2RIGHT (B_USER + 1)
 121 #define B_PLUS (B_USER + 2)
 122 #define B_MINUS (B_USER + 3)
 123 
 124 #define LAYOUT_OPTIONS_COUNT  G_N_ELEMENTS (check_options)
 125 
 126 /*** file scope type declarations ****************************************************************/
 127 
 128 typedef struct
 129 {
 130     gboolean menubar_visible;
 131     gboolean command_prompt;
 132     gboolean keybar_visible;
 133     gboolean message_visible;
 134     gboolean xterm_title;
 135     gboolean free_space;
 136     int output_lines;
 137 } layout_t;
 138 
 139 /*** forward declarations (file scope functions) *************************************************/
 140 
 141 /*** file scope variables ************************************************************************/
 142 
 143 static struct
 144 {
 145     panel_view_mode_t type;
 146     Widget *widget;
 147     char *last_saved_dir;       /* last view_list working directory */
 148 } panels[MAX_VIEWS] =
 149 {
 150     /* *INDENT-OFF* */
 151     /* init MAX_VIEWS items */
 152     { view_listing, NULL, NULL},
 153     { view_listing, NULL, NULL}
 154     /* *INDENT-ON* */
 155 };
 156 
 157 static layout_t old_layout;
 158 static panels_layout_t old_panels_layout;
 159 
 160 static gboolean equal_split;
 161 static int _output_lines;
 162 
 163 static int height;
 164 
 165 static WRadio *radio_widget;
 166 
 167 static struct
 168 {
 169     const char *text;
 170     gboolean *variable;
 171     WCheck *widget;
 172 } check_options[] =
 173 {
 174     /* *INDENT-OFF* */
 175     { N_("&Equal split"), &equal_split, NULL },
 176     { N_("&Menubar visible"), &menubar_visible, NULL },
 177     { N_("Command &prompt"), &command_prompt, NULL },
 178     { N_("&Keybar visible"), &mc_global.keybar_visible, NULL },
 179     { N_("H&intbar visible"), &mc_global.message_visible, NULL },
 180     { N_("&XTerm window title"), &xterm_title, NULL },
 181     { N_("&Show free space"), &free_space, NULL }
 182     /* *INDENT-ON* */
 183 };
 184 
 185 static const char *output_lines_label = NULL;
 186 static int output_lines_label_len;
 187 
 188 static WButton *bleft_widget, *bright_widget;
 189 
 190 /* --------------------------------------------------------------------------------------------- */
 191 /*** file scope functions ************************************************************************/
 192 /* --------------------------------------------------------------------------------------------- */
 193 
 194 /* don't use max() macro to avoid double call of str_term_width1() in widget width calculation */
 195 #undef max
 196 
 197 static int
 198 max (int a, int b)
     /* [previous][next][first][last][top][bottom][index][help]  */
 199 {
 200     return a > b ? a : b;
 201 }
 202 
 203 /* --------------------------------------------------------------------------------------------- */
 204 
 205 static void
 206 check_split (panels_layout_t * layout)
     /* [previous][next][first][last][top][bottom][index][help]  */
 207 {
 208     if (layout->horizontal_split)
 209     {
 210         if (layout->horizontal_equal)
 211             layout->top_panel_size = height / 2;
 212         else if (layout->top_panel_size < MINHEIGHT)
 213             layout->top_panel_size = MINHEIGHT;
 214         else if (layout->top_panel_size > height - MINHEIGHT)
 215             layout->top_panel_size = height - MINHEIGHT;
 216     }
 217     else
 218     {
 219         int md_cols = CONST_WIDGET (filemanager)->rect.cols;
 220 
 221         if (layout->vertical_equal)
 222             layout->left_panel_size = md_cols / 2;
 223         else if (layout->left_panel_size < MINWIDTH)
 224             layout->left_panel_size = MINWIDTH;
 225         else if (layout->left_panel_size > md_cols - MINWIDTH)
 226             layout->left_panel_size = md_cols - MINWIDTH;
 227     }
 228 }
 229 
 230 /* --------------------------------------------------------------------------------------------- */
 231 
 232 static void
 233 update_split (const WDialog * h)
     /* [previous][next][first][last][top][bottom][index][help]  */
 234 {
 235     /* Check split has to be done before testing if it changed, since
 236        it can change due to calling check_split() as well */
 237     check_split (&panels_layout);
 238 
 239     if (panels_layout.horizontal_split)
 240         check_options[0].widget->state = panels_layout.horizontal_equal;
 241     else
 242         check_options[0].widget->state = panels_layout.vertical_equal;
 243     widget_draw (WIDGET (check_options[0].widget));
 244 
 245     tty_setcolor (check_options[0].widget->state ? DISABLED_COLOR : COLOR_NORMAL);
 246 
 247     widget_gotoyx (h, 6, 5);
 248     if (panels_layout.horizontal_split)
 249         tty_printf ("%03d", panels_layout.top_panel_size);
 250     else
 251         tty_printf ("%03d", panels_layout.left_panel_size);
 252 
 253     widget_gotoyx (h, 6, 17);
 254     if (panels_layout.horizontal_split)
 255         tty_printf ("%03d", height - panels_layout.top_panel_size);
 256     else
 257         tty_printf ("%03d", CONST_WIDGET (filemanager)->rect.cols - panels_layout.left_panel_size);
 258 
 259     widget_gotoyx (h, 6, 12);
 260     tty_print_char ('=');
 261 }
 262 
 263 /* --------------------------------------------------------------------------------------------- */
 264 
 265 static int
 266 b_left_right_cback (WButton * button, int action)
     /* [previous][next][first][last][top][bottom][index][help]  */
 267 {
 268     (void) action;
 269 
 270     if (button == bright_widget)
 271     {
 272         if (panels_layout.horizontal_split)
 273             panels_layout.top_panel_size++;
 274         else
 275             panels_layout.left_panel_size++;
 276     }
 277     else
 278     {
 279         if (panels_layout.horizontal_split)
 280             panels_layout.top_panel_size--;
 281         else
 282             panels_layout.left_panel_size--;
 283     }
 284 
 285     update_split (DIALOG (WIDGET (button)->owner));
 286     layout_change ();
 287     do_refresh ();
 288     return 0;
 289 }
 290 
 291 /* --------------------------------------------------------------------------------------------- */
 292 
 293 static int
 294 bplus_cback (WButton * button, int action)
     /* [previous][next][first][last][top][bottom][index][help]  */
 295 {
 296     (void) button;
 297     (void) action;
 298 
 299     if (_output_lines < 99)
 300         _output_lines++;
 301     return 0;
 302 }
 303 
 304 /* --------------------------------------------------------------------------------------------- */
 305 
 306 static int
 307 bminus_cback (WButton * button, int action)
     /* [previous][next][first][last][top][bottom][index][help]  */
 308 {
 309     (void) button;
 310     (void) action;
 311 
 312     if (_output_lines > 0)
 313         _output_lines--;
 314     return 0;
 315 }
 316 
 317 /* --------------------------------------------------------------------------------------------- */
 318 
 319 static cb_ret_t
 320 layout_bg_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 321 {
 322     switch (msg)
 323     {
 324     case MSG_DRAW:
 325         frame_callback (w, NULL, MSG_DRAW, 0, NULL);
 326 
 327         old_layout.output_lines = -1;
 328 
 329         update_split (DIALOG (w->owner));
 330 
 331         if (old_layout.output_lines != _output_lines)
 332         {
 333             old_layout.output_lines = _output_lines;
 334             tty_setcolor (mc_global.tty.console_flag != '\0' ? COLOR_NORMAL : DISABLED_COLOR);
 335             widget_gotoyx (w, 9, 5);
 336             tty_print_string (output_lines_label);
 337             widget_gotoyx (w, 9, 5 + 3 + output_lines_label_len);
 338             tty_printf ("%02d", _output_lines);
 339         }
 340         return MSG_HANDLED;
 341 
 342     default:
 343         return frame_callback (w, sender, msg, parm, data);
 344     }
 345 }
 346 
 347 /* --------------------------------------------------------------------------------------------- */
 348 
 349 static cb_ret_t
 350 layout_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 351 {
 352     WDialog *h = DIALOG (w);
 353 
 354     switch (msg)
 355     {
 356     case MSG_POST_KEY:
 357         {
 358             const Widget *mw = CONST_WIDGET (filemanager);
 359             gboolean _menubar_visible, _command_prompt, _keybar_visible, _message_visible;
 360 
 361             _menubar_visible = check_options[1].widget->state;
 362             _command_prompt = check_options[2].widget->state;
 363             _keybar_visible = check_options[3].widget->state;
 364             _message_visible = check_options[4].widget->state;
 365 
 366             if (mc_global.tty.console_flag == '\0')
 367                 height =
 368                     mw->rect.lines - (_keybar_visible ? 1 : 0) - (_command_prompt ? 1 : 0) -
 369                     (_menubar_visible ? 1 : 0) - _output_lines - (_message_visible ? 1 : 0);
 370             else
 371             {
 372                 int minimum;
 373 
 374                 if (_output_lines < 0)
 375                     _output_lines = 0;
 376                 height =
 377                     mw->rect.lines - (_keybar_visible ? 1 : 0) - (_command_prompt ? 1 : 0) -
 378                     (_menubar_visible ? 1 : 0) - _output_lines - (_message_visible ? 1 : 0);
 379                 minimum = MINHEIGHT * (1 + (panels_layout.horizontal_split ? 1 : 0));
 380                 if (height < minimum)
 381                 {
 382                     _output_lines -= minimum - height;
 383                     height = minimum;
 384                 }
 385             }
 386 
 387             if (old_layout.output_lines != _output_lines)
 388             {
 389                 old_layout.output_lines = _output_lines;
 390                 tty_setcolor (mc_global.tty.console_flag != '\0' ? COLOR_NORMAL : DISABLED_COLOR);
 391                 widget_gotoyx (h, 9, 5 + 3 + output_lines_label_len);
 392                 tty_printf ("%02d", _output_lines);
 393             }
 394         }
 395         return MSG_HANDLED;
 396 
 397     case MSG_NOTIFY:
 398         if (sender == WIDGET (radio_widget))
 399         {
 400             if ((panels_layout.horizontal_split ? 1 : 0) == radio_widget->sel)
 401                 update_split (h);
 402             else
 403             {
 404                 int eq;
 405 
 406                 panels_layout.horizontal_split = radio_widget->sel != 0;
 407 
 408                 if (panels_layout.horizontal_split)
 409                 {
 410                     eq = panels_layout.horizontal_equal;
 411                     if (eq)
 412                         panels_layout.top_panel_size = height / 2;
 413                 }
 414                 else
 415                 {
 416                     eq = panels_layout.vertical_equal;
 417                     if (eq)
 418                         panels_layout.left_panel_size = CONST_WIDGET (filemanager)->rect.cols / 2;
 419                 }
 420 
 421                 widget_disable (WIDGET (bleft_widget), eq);
 422                 widget_disable (WIDGET (bright_widget), eq);
 423 
 424                 update_split (h);
 425                 layout_change ();
 426                 do_refresh ();
 427             }
 428 
 429             return MSG_HANDLED;
 430         }
 431 
 432         if (sender == WIDGET (check_options[0].widget))
 433         {
 434             gboolean eq;
 435 
 436             if (panels_layout.horizontal_split)
 437             {
 438                 panels_layout.horizontal_equal = check_options[0].widget->state;
 439                 eq = panels_layout.horizontal_equal;
 440             }
 441             else
 442             {
 443                 panels_layout.vertical_equal = check_options[0].widget->state;
 444                 eq = panels_layout.vertical_equal;
 445             }
 446 
 447             widget_disable (WIDGET (bleft_widget), eq);
 448             widget_disable (WIDGET (bright_widget), eq);
 449 
 450             update_split (h);
 451             layout_change ();
 452             do_refresh ();
 453 
 454             return MSG_HANDLED;
 455         }
 456 
 457         {
 458             gboolean ok = TRUE;
 459 
 460             if (sender == WIDGET (check_options[1].widget))
 461                 menubar_visible = check_options[1].widget->state;
 462             else if (sender == WIDGET (check_options[2].widget))
 463                 command_prompt = check_options[2].widget->state;
 464             else if (sender == WIDGET (check_options[3].widget))
 465                 mc_global.keybar_visible = check_options[3].widget->state;
 466             else if (sender == WIDGET (check_options[4].widget))
 467                 mc_global.message_visible = check_options[4].widget->state;
 468             else if (sender == WIDGET (check_options[5].widget))
 469                 xterm_title = check_options[5].widget->state;
 470             else if (sender == WIDGET (check_options[6].widget))
 471                 free_space = check_options[6].widget->state;
 472             else
 473                 ok = FALSE;
 474 
 475             if (ok)
 476             {
 477                 update_split (h);
 478                 layout_change ();
 479                 do_refresh ();
 480                 return MSG_HANDLED;
 481             }
 482         }
 483 
 484         return MSG_NOT_HANDLED;
 485 
 486     default:
 487         return dlg_default_callback (w, sender, msg, parm, data);
 488     }
 489 }
 490 
 491 /* --------------------------------------------------------------------------------------------- */
 492 
 493 static WDialog *
 494 layout_dlg_create (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 495 {
 496     WDialog *layout_dlg;
 497     WGroup *g;
 498     int l1 = 0, width;
 499     int b1, b2, b;
 500     size_t i;
 501 
 502     const char *title1 = N_("Panel split");
 503     const char *title2 = N_("Console output");
 504     const char *title3 = N_("Other options");
 505 
 506     const char *s_split_direction[2] = {
 507         N_("&Vertical"),
 508         N_("&Horizontal")
 509     };
 510 
 511     const char *ok_button = N_("&OK");
 512     const char *cancel_button = N_("&Cancel");
 513 
 514     output_lines_label = _("Output lines:");
 515 
 516 #ifdef ENABLE_NLS
 517     {
 518         static gboolean i18n = FALSE;
 519 
 520         title1 = _(title1);
 521         title2 = _(title2);
 522         title3 = _(title3);
 523 
 524         i = G_N_ELEMENTS (s_split_direction);
 525         while (i-- != 0)
 526             s_split_direction[i] = _(s_split_direction[i]);
 527 
 528         if (!i18n)
 529         {
 530             for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
 531                 check_options[i].text = _(check_options[i].text);
 532             i18n = TRUE;
 533         }
 534 
 535         ok_button = _(ok_button);
 536         cancel_button = _(cancel_button);
 537     }
 538 #endif
 539 
 540     /* radiobuttons */
 541     i = G_N_ELEMENTS (s_split_direction);
 542     while (i-- != 0)
 543         l1 = max (l1, str_term_width1 (s_split_direction[i]) + 7);
 544     /* checkboxes */
 545     for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
 546         l1 = max (l1, str_term_width1 (check_options[i].text) + 7);
 547     /* groupboxes */
 548     l1 = max (l1, str_term_width1 (title1) + 4);
 549     l1 = max (l1, str_term_width1 (title2) + 4);
 550     l1 = max (l1, str_term_width1 (title3) + 4);
 551     /* label + "+"/"-" buttons */
 552     output_lines_label_len = str_term_width1 (output_lines_label);
 553     l1 = max (l1, output_lines_label_len + 12);
 554     /* buttons */
 555     b1 = str_term_width1 (ok_button) + 5;       /* default button */
 556     b2 = str_term_width1 (cancel_button) + 3;
 557     b = b1 + b2 + 1;
 558     /* dialog width */
 559     width = max (l1 * 2 + 7, b);
 560 
 561     layout_dlg =
 562         dlg_create (TRUE, 0, 0, 15, width, WPOS_CENTER, FALSE, dialog_colors, layout_callback, NULL,
 563                     "[Layout]", _("Layout"));
 564     g = GROUP (layout_dlg);
 565 
 566     /* draw background */
 567     layout_dlg->bg->callback = layout_bg_callback;
 568 
 569 #define XTRACT(i) (*check_options[i].variable != 0), check_options[i].text
 570 
 571     /* "Panel split" groupbox */
 572     group_add_widget (g, groupbox_new (2, 3, 6, l1, title1));
 573 
 574     radio_widget = radio_new (3, 5, 2, s_split_direction);
 575     radio_widget->sel = panels_layout.horizontal_split ? 1 : 0;
 576     group_add_widget (g, radio_widget);
 577 
 578     check_options[0].widget = check_new (5, 5, XTRACT (0));
 579     group_add_widget (g, check_options[0].widget);
 580 
 581     equal_split = panels_layout.horizontal_split ?
 582         panels_layout.horizontal_equal : panels_layout.vertical_equal;
 583 
 584     bleft_widget = button_new (6, 8, B_2LEFT, NARROW_BUTTON, "&<", b_left_right_cback);
 585     widget_disable (WIDGET (bleft_widget), equal_split);
 586     group_add_widget (g, bleft_widget);
 587 
 588     bright_widget = button_new (6, 14, B_2RIGHT, NARROW_BUTTON, "&>", b_left_right_cback);
 589     widget_disable (WIDGET (bright_widget), equal_split);
 590     group_add_widget (g, bright_widget);
 591 
 592     /* "Console output" groupbox */
 593     {
 594         widget_state_t disabled;
 595         Widget *w;
 596 
 597         disabled = mc_global.tty.console_flag != '\0' ? 0 : WST_DISABLED;
 598 
 599         w = WIDGET (groupbox_new (8, 3, 3, l1, title2));
 600         w->state |= disabled;
 601         group_add_widget (g, w);
 602 
 603         w = WIDGET (button_new (9, output_lines_label_len + 5, B_PLUS,
 604                                 NARROW_BUTTON, "&+", bplus_cback));
 605         w->state |= disabled;
 606         group_add_widget (g, w);
 607 
 608         w = WIDGET (button_new (9, output_lines_label_len + 5 + 5, B_MINUS,
 609                                 NARROW_BUTTON, "&-", bminus_cback));
 610         w->state |= disabled;
 611         group_add_widget (g, w);
 612     }
 613 
 614     /* "Other options" groupbox */
 615     group_add_widget (g, groupbox_new (2, 4 + l1, 9, l1, title3));
 616 
 617     for (i = 1; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
 618     {
 619         check_options[i].widget = check_new (i + 2, 6 + l1, XTRACT (i));
 620         group_add_widget (g, check_options[i].widget);
 621     }
 622 
 623 #undef XTRACT
 624 
 625     group_add_widget (g, hline_new (11, -1, -1));
 626     /* buttons */
 627     group_add_widget (g, button_new (12, (width - b) / 2, B_ENTER, DEFPUSH_BUTTON, ok_button, 0));
 628     group_add_widget (g,
 629                       button_new (12, (width - b) / 2 + b1 + 1, B_CANCEL, NORMAL_BUTTON,
 630                                   cancel_button, 0));
 631 
 632     widget_select (WIDGET (radio_widget));
 633 
 634     return layout_dlg;
 635 }
 636 
 637 /* --------------------------------------------------------------------------------------------- */
 638 
 639 static void
 640 panel_do_cols (int idx)
     /* [previous][next][first][last][top][bottom][index][help]  */
 641 {
 642     if (get_panel_type (idx) == view_listing)
 643         set_panel_formats (PANEL (panels[idx].widget));
 644     else
 645         panel_update_cols (panels[idx].widget, frame_half);
 646 }
 647 
 648 /* --------------------------------------------------------------------------------------------- */
 649 /** Save current list_view widget directory into panel */
 650 
 651 static Widget *
 652 restore_into_right_dir_panel (int idx, gboolean last_was_panel, int y, int x, int lines, int cols)
     /* [previous][next][first][last][top][bottom][index][help]  */
 653 {
 654     WPanel *new_widget;
 655     const char *p_name;
 656 
 657     p_name = get_nth_panel_name (idx);
 658 
 659     if (last_was_panel)
 660     {
 661         vfs_path_t *saved_dir_vpath;
 662 
 663         saved_dir_vpath = vfs_path_from_str (panels[idx].last_saved_dir);
 664         new_widget = panel_sized_with_dir_new (p_name, y, x, lines, cols, saved_dir_vpath);
 665         vfs_path_free (saved_dir_vpath, TRUE);
 666     }
 667     else
 668         new_widget = panel_sized_new (p_name, y, x, lines, cols);
 669 
 670     return WIDGET (new_widget);
 671 }
 672 
 673 /* --------------------------------------------------------------------------------------------- */
 674 
 675 static void
 676 layout_save (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 677 {
 678     old_layout.menubar_visible = menubar_visible;
 679     old_layout.command_prompt = command_prompt;
 680     old_layout.keybar_visible = mc_global.keybar_visible;
 681     old_layout.message_visible = mc_global.message_visible;
 682     old_layout.xterm_title = xterm_title;
 683     old_layout.free_space = free_space;
 684     old_layout.output_lines = -1;
 685 
 686     _output_lines = output_lines;
 687 
 688     old_panels_layout = panels_layout;
 689 }
 690 
 691 /* --------------------------------------------------------------------------------------------- */
 692 
 693 static void
 694 layout_restore (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 695 {
 696     menubar_visible = old_layout.menubar_visible;
 697     command_prompt = old_layout.command_prompt;
 698     mc_global.keybar_visible = old_layout.keybar_visible;
 699     mc_global.message_visible = old_layout.message_visible;
 700     xterm_title = old_layout.xterm_title;
 701     free_space = old_layout.free_space;
 702     output_lines = old_layout.output_lines;
 703 
 704     panels_layout = old_panels_layout;
 705 }
 706 
 707 /* --------------------------------------------------------------------------------------------- */
 708 /*** public functions ****************************************************************************/
 709 /* --------------------------------------------------------------------------------------------- */
 710 
 711 void
 712 layout_change (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 713 {
 714     setup_panels ();
 715     /* update the main menu, because perhaps there was a change in the way
 716        how the panel are split (horizontal/vertical),
 717        and a change of menu visibility. */
 718     update_menu ();
 719     load_hint (TRUE);
 720 }
 721 
 722 /* --------------------------------------------------------------------------------------------- */
 723 
 724 void
 725 layout_box (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 726 {
 727     WDialog *layout_dlg;
 728 
 729     layout_save ();
 730 
 731     layout_dlg = layout_dlg_create ();
 732 
 733     if (dlg_run (layout_dlg) == B_ENTER)
 734     {
 735         size_t i;
 736 
 737         for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
 738             if (check_options[i].widget != NULL)
 739                 *check_options[i].variable = check_options[i].widget->state;
 740 
 741         output_lines = _output_lines;
 742     }
 743     else
 744         layout_restore ();
 745 
 746     widget_destroy (WIDGET (layout_dlg));
 747     layout_change ();
 748     do_refresh ();
 749 }
 750 
 751 /* --------------------------------------------------------------------------------------------- */
 752 
 753 void
 754 panel_update_cols (Widget * widget, panel_display_t frame_size)
     /* [previous][next][first][last][top][bottom][index][help]  */
 755 {
 756     const Widget *mw = CONST_WIDGET (filemanager);
 757     int cols, x;
 758 
 759     /* don't touch panel if it is not in dialog yet */
 760     /* if panel is not in dialog it is not in widgets list
 761        and cannot be compared with get_panel_widget() result */
 762     if (widget->owner == NULL)
 763         return;
 764 
 765     if (panels_layout.horizontal_split)
 766     {
 767         widget->rect.cols = mw->rect.cols;
 768         return;
 769     }
 770 
 771     if (frame_size == frame_full)
 772     {
 773         cols = mw->rect.cols;
 774         x = mw->rect.x;
 775     }
 776     else if (widget == get_panel_widget (0))
 777     {
 778         cols = panels_layout.left_panel_size;
 779         x = mw->rect.x;
 780     }
 781     else
 782     {
 783         cols = mw->rect.cols - panels_layout.left_panel_size;
 784         x = mw->rect.x + panels_layout.left_panel_size;
 785     }
 786 
 787     widget->rect.cols = cols;
 788     widget->rect.x = x;
 789 }
 790 
 791 /* --------------------------------------------------------------------------------------------- */
 792 
 793 void
 794 setup_panels (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 795 {
 796     /* File manager screen layout:
 797      *
 798      * +---------------------------------------------------------------+
 799      * | Menu bar                                                      |
 800      * +-------------------------------+-------------------------------+
 801      * |                               |                               |
 802      * |                               |                               |
 803      * |                               |                               |
 804      * |                               |                               |
 805      * |         Left panel            |         Right panel           |
 806      * |                               |                               |
 807      * |                               |                               |
 808      * |                               |                               |
 809      * |                               |                               |
 810      * +-------------------------------+-------------------------------+
 811      * | Hint (message) bar                                            |
 812      * +---------------------------------------------------------------+
 813      * |                                                               |
 814      * |                        Console content                        |
 815      * |                                                               |
 816      * +--------+------------------------------------------------------+
 817      * | Prompt | Command line                                         |
 818      * | Key (button) bar                                              |
 819      * +--------+------------------------------------------------------+
 820      */
 821 
 822     Widget *mw = WIDGET (filemanager);
 823     const WRect *r = &CONST_WIDGET (mw)->rect;
 824     int start_y;
 825     gboolean active;
 826     WRect rb;
 827 
 828     active = widget_get_state (mw, WST_ACTIVE);
 829 
 830     /* lock the group to avoid many redraws */
 831     if (active)
 832         widget_set_state (mw, WST_SUSPENDED, TRUE);
 833 
 834     /* initial height of panels */
 835     height =
 836         r->lines - (menubar_visible ? 1 : 0) - (mc_global.message_visible ? 1 : 0) -
 837         (command_prompt ? 1 : 0) - (mc_global.keybar_visible ? 1 : 0);
 838 
 839     if (mc_global.tty.console_flag != '\0')
 840     {
 841         int minimum;
 842 
 843         if (output_lines < 0)
 844             output_lines = 0;
 845         else
 846             height -= output_lines;
 847         minimum = MINHEIGHT * (1 + (panels_layout.horizontal_split ? 1 : 0));
 848         if (height < minimum)
 849         {
 850             output_lines -= minimum - height;
 851             height = minimum;
 852         }
 853     }
 854 
 855     rb = *r;
 856     rb.lines = 1;
 857     widget_set_size_rect (WIDGET (the_menubar), &rb);
 858     widget_set_visibility (WIDGET (the_menubar), menubar_visible);
 859 
 860     check_split (&panels_layout);
 861     start_y = r->y + (menubar_visible ? 1 : 0);
 862 
 863     /* update columns first... */
 864     panel_do_cols (0);
 865     panel_do_cols (1);
 866 
 867     /* ...then rows and origin */
 868     if (panels_layout.horizontal_split)
 869     {
 870         widget_set_size (panels[0].widget, start_y, r->x, panels_layout.top_panel_size,
 871                          panels[0].widget->rect.cols);
 872         widget_set_size (panels[1].widget, start_y + panels_layout.top_panel_size, r->x,
 873                          height - panels_layout.top_panel_size, panels[1].widget->rect.cols);
 874     }
 875     else
 876     {
 877         widget_set_size (panels[0].widget, start_y, r->x, height, panels[0].widget->rect.cols);
 878         widget_set_size (panels[1].widget, start_y, panels[1].widget->rect.x, height,
 879                          panels[1].widget->rect.cols);
 880     }
 881 
 882     widget_set_size (WIDGET (the_hint), height + start_y, r->x, 1, r->cols);
 883     widget_set_visibility (WIDGET (the_hint), mc_global.message_visible);
 884 
 885     /* Output window */
 886     if (mc_global.tty.console_flag != '\0' && output_lines != 0)
 887     {
 888         unsigned char end_line;
 889 
 890         end_line = r->lines - (mc_global.keybar_visible ? 1 : 0) - 1;
 891         output_start_y = end_line - (command_prompt ? 1 : 0) - output_lines + 1;
 892         show_console_contents (output_start_y, end_line - output_lines, end_line);
 893     }
 894 
 895     if (command_prompt)
 896     {
 897 #ifdef ENABLE_SUBSHELL
 898         if (!mc_global.tty.use_subshell || !do_load_prompt ())
 899 #endif
 900             setup_cmdline ();
 901     }
 902     else
 903     {
 904         /* make invisible */
 905         widget_hide (WIDGET (cmdline));
 906         widget_hide (WIDGET (the_prompt));
 907     }
 908 
 909     rb = *r;
 910     rb.y = r->lines - 1;
 911     rb.lines = 1;
 912     widget_set_size_rect (WIDGET (the_bar), &rb);
 913     widget_set_visibility (WIDGET (the_bar), mc_global.keybar_visible);
 914 
 915     update_xterm_title_path ();
 916     update_terminal_cwd ();
 917 
 918     /* unlock */
 919     if (active)
 920     {
 921         widget_set_state (mw, WST_ACTIVE, TRUE);
 922         widget_draw (mw);
 923     }
 924 }
 925 
 926 /* --------------------------------------------------------------------------------------------- */
 927 
 928 void
 929 panels_split_equal (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 930 {
 931     if (panels_layout.horizontal_split)
 932         panels_layout.horizontal_equal = TRUE;
 933     else
 934         panels_layout.vertical_equal = TRUE;
 935 
 936     layout_change ();
 937     do_refresh ();
 938 }
 939 
 940 /* --------------------------------------------------------------------------------------------- */
 941 
 942 void
 943 panels_split_more (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 944 {
 945     if (panels_layout.horizontal_split)
 946     {
 947         panels_layout.horizontal_equal = FALSE;
 948         panels_layout.top_panel_size++;
 949     }
 950     else
 951     {
 952         panels_layout.vertical_equal = FALSE;
 953         panels_layout.left_panel_size++;
 954     }
 955 
 956     layout_change ();
 957 }
 958 
 959 /* --------------------------------------------------------------------------------------------- */
 960 
 961 void
 962 panels_split_less (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 963 {
 964     if (panels_layout.horizontal_split)
 965     {
 966         panels_layout.horizontal_equal = FALSE;
 967         panels_layout.top_panel_size--;
 968     }
 969     else
 970     {
 971         panels_layout.vertical_equal = FALSE;
 972         panels_layout.left_panel_size--;
 973     }
 974 
 975     layout_change ();
 976 }
 977 
 978 /* --------------------------------------------------------------------------------------------- */
 979 
 980 void
 981 setup_cmdline (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 982 {
 983     const Widget *mw = CONST_WIDGET (filemanager);
 984     const WRect *r = &mw->rect;
 985     int prompt_width;
 986     int y;
 987     char *tmp_prompt = (char *) mc_prompt;
 988 
 989     if (!command_prompt)
 990         return;
 991 
 992 #ifdef ENABLE_SUBSHELL
 993     if (mc_global.tty.use_subshell)
 994     {
 995         /* Workaround: avoid crash on FreeBSD (see ticket #4213 for details)  */
 996         if (subshell_prompt != NULL)
 997             tmp_prompt = g_string_free (subshell_prompt, FALSE);
 998         else
 999             tmp_prompt = g_strdup (mc_prompt);
1000         (void) strip_ctrl_codes (tmp_prompt);
1001     }
1002 #endif
1003 
1004     prompt_width = str_term_width1 (tmp_prompt);
1005 
1006     /* Check for prompts too big */
1007     if (r->cols > 8 && prompt_width > r->cols - 8)
1008     {
1009         int prompt_len;
1010 
1011         prompt_width = r->cols - 8;
1012         prompt_len = str_offset_to_pos (tmp_prompt, prompt_width);
1013         tmp_prompt[prompt_len] = '\0';
1014     }
1015 
1016 #ifdef ENABLE_SUBSHELL
1017     if (mc_global.tty.use_subshell)
1018     {
1019         subshell_prompt = g_string_new_take (tmp_prompt);
1020         mc_prompt = subshell_prompt->str;
1021     }
1022 #endif
1023 
1024     y = r->lines - 1 - (mc_global.keybar_visible ? 1 : 0);
1025 
1026     widget_set_size (WIDGET (the_prompt), y, r->x, 1, prompt_width);
1027     label_set_text (the_prompt, mc_prompt);
1028     widget_set_size (WIDGET (cmdline), y, r->x + prompt_width, 1, r->cols - prompt_width);
1029 
1030     widget_show (WIDGET (the_prompt));
1031     widget_show (WIDGET (cmdline));
1032 }
1033 
1034 /* --------------------------------------------------------------------------------------------- */
1035 
1036 void
1037 use_dash (gboolean flag)
     /* [previous][next][first][last][top][bottom][index][help]  */
1038 {
1039     if (flag)
1040         ok_to_refresh++;
1041     else
1042         ok_to_refresh--;
1043 }
1044 
1045 /* --------------------------------------------------------------------------------------------- */
1046 
1047 void
1048 set_hintbar (const char *str)
     /* [previous][next][first][last][top][bottom][index][help]  */
1049 {
1050     label_set_text (the_hint, str);
1051     if (ok_to_refresh > 0)
1052         mc_refresh ();
1053 }
1054 
1055 /* --------------------------------------------------------------------------------------------- */
1056 
1057 void
1058 rotate_dash (gboolean show)
     /* [previous][next][first][last][top][bottom][index][help]  */
1059 {
1060     static gint64 timestamp = 0;
1061     /* update with 10 FPS rate */
1062     static const gint64 delay = G_USEC_PER_SEC / 10;
1063 
1064     const Widget *w = CONST_WIDGET (filemanager);
1065 
1066     if (!nice_rotating_dash || (ok_to_refresh <= 0))
1067         return;
1068 
1069     if (show && !mc_time_elapsed (&timestamp, delay))
1070         return;
1071 
1072     widget_gotoyx (w, menubar_visible ? 1 : 0, w->rect.cols - 1);
1073     tty_setcolor (NORMAL_COLOR);
1074 
1075     if (!show)
1076         tty_print_alt_char (ACS_URCORNER, FALSE);
1077     else
1078     {
1079         static const char rotating_dash[4] = "|/-\\";
1080         static size_t pos = 0;
1081 
1082         tty_print_char (rotating_dash[pos]);
1083         pos = (pos + 1) % sizeof (rotating_dash);
1084     }
1085 
1086     mc_refresh ();
1087 }
1088 
1089 /* --------------------------------------------------------------------------------------------- */
1090 
1091 const char *
1092 get_nth_panel_name (int num)
     /* [previous][next][first][last][top][bottom][index][help]  */
1093 {
1094     if (num == 0)
1095         return "New Left Panel";
1096 
1097     if (num == 1)
1098         return "New Right Panel";
1099 
1100     {
1101         static char buffer[BUF_SMALL];
1102 
1103         g_snprintf (buffer, sizeof (buffer), "%ith Panel", num);
1104         return buffer;
1105     }
1106 }
1107 
1108 /* --------------------------------------------------------------------------------------------- */
1109 /* I wonder if I should start to use the folding mode than Dugan uses */
1110 /*                                                                     */
1111 /* This is the centralized managing of the panel display types         */
1112 /* This routine takes care of destroying and creating new widgets      */
1113 /* Please note that it could manage MAX_VIEWS, not just left and right */
1114 /* Currently nothing in the code takes advantage of this and has hard- */
1115 /* coded values for two panels only                                    */
1116 
1117 /* Set the num-th panel to the view type: type */
1118 /* This routine also keeps at least one WPanel object in the screen */
1119 /* since a lot of routines depend on the current_panel variable */
1120 
1121 void
1122 create_panel (int num, panel_view_mode_t type)
     /* [previous][next][first][last][top][bottom][index][help]  */
1123 {
1124     WRect r = { 0, 0, 0, 0 };
1125     unsigned int the_other = 0; /* Index to the other panel */
1126     const char *file_name = NULL;       /* For Quick view */
1127     Widget *new_widget = NULL, *old_widget = NULL;
1128     panel_view_mode_t old_type = view_listing;
1129     WPanel *the_other_panel = NULL;
1130 
1131     if (num >= MAX_VIEWS)
1132     {
1133         fprintf (stderr, "Cannot allocate more that %d views\n", MAX_VIEWS);
1134         abort ();
1135     }
1136     /* Check that we will have a WPanel * at least */
1137     if (type != view_listing)
1138     {
1139         the_other = num == 0 ? 1 : 0;
1140 
1141         if (panels[the_other].type != view_listing)
1142             return;
1143     }
1144 
1145     /* Get rid of it */
1146     if (panels[num].widget != NULL)
1147     {
1148         Widget *w = panels[num].widget;
1149         WPanel *panel = PANEL (w);
1150 
1151         r = w->rect;
1152         old_widget = w;
1153         old_type = panels[num].type;
1154 
1155         if (old_type == view_listing && panel->frame_size == frame_full && type != view_listing)
1156         {
1157             int md_cols = CONST_WIDGET (filemanager)->rect.cols;
1158 
1159             if (panels_layout.horizontal_split)
1160             {
1161                 r.cols = md_cols;
1162                 r.x = 0;
1163             }
1164             else
1165             {
1166                 r.cols = md_cols - panels_layout.left_panel_size;
1167                 if (num == 1)
1168                     r.x = panels_layout.left_panel_size;
1169             }
1170         }
1171     }
1172 
1173     /* Restoring saved path from panels.ini for nonlist panel */
1174     /* when it's first creation (for example view_info) */
1175     if (old_widget == NULL && type != view_listing)
1176         panels[num].last_saved_dir = vfs_get_cwd ();
1177 
1178     switch (type)
1179     {
1180     case view_nothing:
1181     case view_listing:
1182         {
1183             gboolean last_was_panel;
1184 
1185             last_was_panel = old_widget != NULL && get_panel_type (num) != view_listing;
1186             new_widget =
1187                 restore_into_right_dir_panel (num, last_was_panel, r.y, r.x, r.lines, r.cols);
1188             break;
1189         }
1190 
1191     case view_info:
1192         new_widget = WIDGET (info_new (r.y, r.x, r.lines, r.cols));
1193         break;
1194 
1195     case view_tree:
1196         new_widget = WIDGET (tree_new (r.y, r.x, r.lines, r.cols, TRUE));
1197         break;
1198 
1199     case view_quick:
1200         new_widget = WIDGET (mcview_new (r.y, r.x, r.lines, r.cols, TRUE));
1201         the_other_panel = PANEL (panels[the_other].widget);
1202         if (the_other_panel != NULL)
1203             file_name = panel_current_entry (the_other_panel)->fname->str;
1204         else
1205             file_name = "";
1206 
1207         mcview_load ((WView *) new_widget, 0, file_name, 0, 0, 0);
1208         break;
1209 
1210     default:
1211         break;
1212     }
1213 
1214     if (type != view_listing)
1215         /* Must save dir, for restoring after change type to */
1216         /* view_listing */
1217         save_panel_dir (num);
1218 
1219     panels[num].type = type;
1220     panels[num].widget = new_widget;
1221 
1222     /* We use replace to keep the circular list of the dialog in the */
1223     /* same state.  Maybe we could just kill it and then replace it  */
1224     if (old_widget != NULL)
1225     {
1226         if (old_type == view_listing)
1227         {
1228             /* save and write directory history of panel
1229              * ... and other histories of filemanager  */
1230             dlg_save_history (filemanager);
1231         }
1232 
1233         widget_replace (old_widget, new_widget);
1234     }
1235 
1236     if (type == view_listing)
1237     {
1238         WPanel *panel = PANEL (new_widget);
1239 
1240         /* if existing panel changed type to view_listing, then load history */
1241         if (old_widget != NULL)
1242         {
1243             ev_history_load_save_t event_data = { NULL, new_widget };
1244 
1245             mc_event_raise (filemanager->event_group, MCEVENT_HISTORY_LOAD, &event_data);
1246         }
1247 
1248         if (num == 0)
1249             left_panel = panel;
1250         else
1251             right_panel = panel;
1252 
1253         /* forced update format after set new sizes */
1254         set_panel_formats (panel);
1255     }
1256 
1257     if (type == view_tree)
1258         the_tree = (WTree *) new_widget;
1259 
1260     /* Prevent current_panel's value from becoming invalid.
1261      * It's just a quick hack to prevent segfaults. Comment out and
1262      * try following:
1263      * - select left panel
1264      * - invoke menu left/tree
1265      * - as long as you stay in the left panel almost everything that uses
1266      *   current_panel causes segfault, e.g. C-Enter, C-x c, ...
1267      */
1268     if ((type != view_listing) && (current_panel == PANEL (old_widget)))
1269         current_panel = num == 0 ? right_panel : left_panel;
1270 
1271     g_free (old_widget);
1272 }
1273 
1274 /* --------------------------------------------------------------------------------------------- */
1275 /** This routine is deeply sticked to the two panels idea.
1276    What should it do in more panels. ANSWER - don't use it
1277    in any multiple panels environment. */
1278 
1279 void
1280 swap_panels (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
1281 {
1282     WPanel *panel1, *panel2;
1283     Widget *tmp_widget;
1284 
1285     panel1 = PANEL (panels[0].widget);
1286     panel2 = PANEL (panels[1].widget);
1287 
1288     if (panels[0].type == view_listing && panels[1].type == view_listing &&
1289         !mc_config_get_bool (mc_global.main_config, CONFIG_PANELS_SECTION, "simple_swap", FALSE))
1290     {
1291         WPanel panel;
1292 
1293 #define panelswap(x) panel.x = panel1->x; panel1->x = panel2->x; panel2->x = panel.x;
1294         /* Change content and related stuff */
1295         panelswap (dir);
1296         panelswap (active);
1297         panelswap (cwd_vpath);
1298         panelswap (lwd_vpath);
1299         panelswap (marked);
1300         panelswap (dirs_marked);
1301         panelswap (total);
1302         panelswap (top);
1303         panelswap (current);
1304         panelswap (is_panelized);
1305         panelswap (panelized_descr);
1306         panelswap (dir_stat);
1307 #undef panelswap
1308 
1309         panel1->quick_search.active = FALSE;
1310         panel2->quick_search.active = FALSE;
1311 
1312         if (current_panel == panel1)
1313             current_panel = panel2;
1314         else
1315             current_panel = panel1;
1316 
1317         /* if sort options are different -> resort panels */
1318         if (memcmp (&panel1->sort_info, &panel2->sort_info, sizeof (dir_sort_options_t)) != 0)
1319         {
1320             panel_re_sort (other_panel);
1321             panel_re_sort (current_panel);
1322         }
1323 
1324         if (widget_is_active (panels[0].widget))
1325             widget_select (panels[1].widget);
1326         else if (widget_is_active (panels[1].widget))
1327             widget_select (panels[0].widget);
1328     }
1329     else
1330     {
1331         WPanel *tmp_panel;
1332         WRect r;
1333         int tmp_type;
1334 
1335         tmp_panel = right_panel;
1336         right_panel = left_panel;
1337         left_panel = tmp_panel;
1338 
1339         if (panels[0].type == view_listing)
1340         {
1341             if (strcmp (panel1->name, get_nth_panel_name (0)) == 0)
1342             {
1343                 g_free (panel1->name);
1344                 panel1->name = g_strdup (get_nth_panel_name (1));
1345             }
1346         }
1347         if (panels[1].type == view_listing)
1348         {
1349             if (strcmp (panel2->name, get_nth_panel_name (1)) == 0)
1350             {
1351                 g_free (panel2->name);
1352                 panel2->name = g_strdup (get_nth_panel_name (0));
1353             }
1354         }
1355 
1356         r = panels[0].widget->rect;
1357         panels[0].widget->rect = panels[1].widget->rect;
1358         panels[1].widget->rect = r;
1359 
1360         tmp_widget = panels[0].widget;
1361         panels[0].widget = panels[1].widget;
1362         panels[1].widget = tmp_widget;
1363         tmp_type = panels[0].type;
1364         panels[0].type = panels[1].type;
1365         panels[1].type = tmp_type;
1366 
1367         /* force update formats because of possible changed sizes */
1368         if (panels[0].type == view_listing)
1369             set_panel_formats (PANEL (panels[0].widget));
1370         if (panels[1].type == view_listing)
1371             set_panel_formats (PANEL (panels[1].widget));
1372     }
1373 }
1374 
1375 /* --------------------------------------------------------------------------------------------- */
1376 
1377 panel_view_mode_t
1378 get_panel_type (int idx)
     /* [previous][next][first][last][top][bottom][index][help]  */
1379 {
1380     return panels[idx].type;
1381 }
1382 
1383 /* --------------------------------------------------------------------------------------------- */
1384 
1385 Widget *
1386 get_panel_widget (int idx)
     /* [previous][next][first][last][top][bottom][index][help]  */
1387 {
1388     return panels[idx].widget;
1389 }
1390 
1391 /* --------------------------------------------------------------------------------------------- */
1392 
1393 int
1394 get_current_index (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
1395 {
1396     return (panels[0].widget == WIDGET (current_panel) ? 0 : 1);
1397 }
1398 
1399 /* --------------------------------------------------------------------------------------------- */
1400 
1401 int
1402 get_other_index (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
1403 {
1404     return (get_current_index () == 0 ? 1 : 0);
1405 }
1406 
1407 /* --------------------------------------------------------------------------------------------- */
1408 
1409 WPanel *
1410 get_other_panel (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
1411 {
1412     return PANEL (get_panel_widget (get_other_index ()));
1413 }
1414 
1415 /* --------------------------------------------------------------------------------------------- */
1416 /** Returns the view type for the current panel/view */
1417 
1418 panel_view_mode_t
1419 get_current_type (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
1420 {
1421     return (panels[0].widget == WIDGET (current_panel) ? panels[0].type : panels[1].type);
1422 }
1423 
1424 /* --------------------------------------------------------------------------------------------- */
1425 /** Returns the view type of the unselected panel */
1426 
1427 panel_view_mode_t
1428 get_other_type (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
1429 {
1430     return (panels[0].widget == WIDGET (current_panel) ? panels[1].type : panels[0].type);
1431 }
1432 
1433 /* --------------------------------------------------------------------------------------------- */
1434 /** Save current list_view widget directory into panel */
1435 
1436 void
1437 save_panel_dir (int idx)
     /* [previous][next][first][last][top][bottom][index][help]  */
1438 {
1439     panel_view_mode_t type;
1440 
1441     type = get_panel_type (idx);
1442     if (type == view_listing)
1443     {
1444         WPanel *p;
1445 
1446         p = PANEL (get_panel_widget (idx));
1447         if (p != NULL)
1448         {
1449             g_free (panels[idx].last_saved_dir);        /* last path no needed */
1450             /* Because path can be nonlocal */
1451             panels[idx].last_saved_dir = g_strdup (vfs_path_as_str (p->cwd_vpath));
1452         }
1453     }
1454 }
1455 
1456 /* --------------------------------------------------------------------------------------------- */
1457 /** Return working dir, if it's view_listing - cwd,
1458    but for other types - last_saved_dir */
1459 
1460 char *
1461 get_panel_dir_for (const WPanel * widget)
     /* [previous][next][first][last][top][bottom][index][help]  */
1462 {
1463     int i;
1464 
1465     for (i = 0; i < MAX_VIEWS; i++)
1466         if (PANEL (get_panel_widget (i)) == widget)
1467             break;
1468 
1469     if (i >= MAX_VIEWS)
1470         return g_strdup (".");
1471 
1472     if (get_panel_type (i) == view_listing)
1473     {
1474         vfs_path_t *cwd_vpath;
1475 
1476         cwd_vpath = PANEL (get_panel_widget (i))->cwd_vpath;
1477         return g_strdup (vfs_path_as_str (cwd_vpath));
1478     }
1479 
1480     return g_strdup (panels[i].last_saved_dir);
1481 }
1482 
1483 /* --------------------------------------------------------------------------------------------- */
1484 
1485 #ifdef ENABLE_SUBSHELL
1486 gboolean
1487 do_load_prompt (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
1488 {
1489     gboolean ret = FALSE;
1490 
1491     if (!read_subshell_prompt ())
1492         return ret;
1493 
1494     /* Don't actually change the prompt if it's invisible */
1495     if (top_dlg != NULL && DIALOG (top_dlg->data) == filemanager && command_prompt)
1496     {
1497         setup_cmdline ();
1498 
1499         /* since the prompt has changed, and we are called from one of the
1500          * tty_get_event channels, the prompt updating does not take place
1501          * automatically: force a cursor update and a screen refresh
1502          */
1503         widget_update_cursor (WIDGET (filemanager));
1504         mc_refresh ();
1505         ret = TRUE;
1506     }
1507     update_subshell_prompt = TRUE;
1508     return ret;
1509 }
1510 
1511 /* --------------------------------------------------------------------------------------------- */
1512 
1513 int
1514 load_prompt (int fd, void *unused)
     /* [previous][next][first][last][top][bottom][index][help]  */
1515 {
1516     (void) fd;
1517     (void) unused;
1518 
1519     if (should_read_new_subshell_prompt)
1520         do_load_prompt ();
1521     else
1522         flush_subshell (0, QUIETLY);
1523 
1524     return 0;
1525 }
1526 #endif /* ENABLE_SUBSHELL */
1527 
1528 /* --------------------------------------------------------------------------------------------- */
1529 
1530 void
1531 title_path_prepare (char **path, char **login)
     /* [previous][next][first][last][top][bottom][index][help]  */
1532 {
1533     char host[BUF_TINY];
1534     struct passwd *pw = NULL;
1535     int res = 0;
1536 
1537     *path =
1538         vfs_path_to_str_flags (current_panel->cwd_vpath, 0, VPF_STRIP_HOME | VPF_STRIP_PASSWORD);
1539 
1540     res = gethostname (host, sizeof (host));
1541     if (res != 0)
1542         host[0] = '\0';
1543     else
1544         host[sizeof (host) - 1] = '\0';
1545 
1546     pw = getpwuid (getuid ());
1547     if (pw != NULL)
1548         *login = g_strdup_printf ("%s@%s", pw->pw_name, host);
1549     else
1550         *login = g_strdup (host);
1551 }
1552 
1553 /* --------------------------------------------------------------------------------------------- */
1554 
1555 /** Show current directory in the xterm title */
1556 void
1557 update_xterm_title_path (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
1558 {
1559     if (mc_global.tty.xterm_flag && xterm_title)
1560     {
1561         char *p;
1562         char *path;
1563         char *login;
1564 
1565         title_path_prepare (&path, &login);
1566 
1567         p = g_strdup_printf ("mc [%s]:%s", login, path);
1568         g_free (login);
1569         g_free (path);
1570 
1571         fprintf (stdout, ESC_STR "]0;%s" ESC_STR "\\", str_term_form (p));
1572         g_free (p);
1573 
1574         if (!mc_global.tty.alternate_plus_minus)
1575             numeric_keypad_mode ();
1576         (void) fflush (stdout);
1577     }
1578 }
1579 
1580 /* --------------------------------------------------------------------------------------------- */
1581 
1582 /** Tell the current directory to the terminal so it can open new tabs there */
1583 void
1584 update_terminal_cwd (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
1585 {
1586     if (mc_global.tty.xterm_flag && vfs_current_is_local ())
1587     {
1588         const gchar *host;
1589         char *path, *path_uri;
1590 
1591         host = g_get_host_name ();
1592         path = vfs_path_to_str_flags (current_panel->cwd_vpath, 0, VPF_NONE);
1593         path_uri = g_uri_escape_string (path, "/", FALSE);
1594 
1595         fprintf (stdout, ESC_STR "]7;file://%s%s" ESC_STR "\\", host, path_uri);
1596         (void) fflush (stdout);
1597 
1598         g_free (path_uri);
1599         g_free (path);
1600     }
1601 }
1602 
1603 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */