2011-10-15 14:56:47 +04:00
|
|
|
/*
|
|
|
|
Widgets for the Midnight Commander
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2020-01-18 19:54:37 +03:00
|
|
|
Copyright (C) 1994-2020
|
2014-02-12 10:33:10 +04:00
|
|
|
Free Software Foundation, Inc.
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
Authors:
|
|
|
|
Radek Doulik, 1994, 1995
|
|
|
|
Miguel de Icaza, 1994, 1995
|
|
|
|
Jakub Jelinek, 1995
|
|
|
|
Andrej Borsenkow, 1996
|
|
|
|
Norbert Warmuth, 1997
|
2019-07-28 15:24:00 +03:00
|
|
|
Andrew Borodin <aborodin@vmail.ru>, 2009-2019
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
This file is part of the Midnight Commander.
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
The Midnight Commander is free software: you can redistribute it
|
|
|
|
and/or modify it under the terms of the GNU General Public License as
|
|
|
|
published by the Free Software Foundation, either version 3 of the License,
|
|
|
|
or (at your option) any later version.
|
|
|
|
|
|
|
|
The Midnight Commander is distributed in the hope that it will be useful,
|
2010-11-12 11:03:57 +03:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2011-10-15 14:56:47 +04:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2010-11-12 11:03:57 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/** \file history.c
|
2019-07-28 15:24:00 +03:00
|
|
|
* \brief Source: show history
|
2010-11-12 11:03:57 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include "lib/global.h"
|
|
|
|
|
|
|
|
#include "lib/tty/tty.h" /* LINES, COLS */
|
|
|
|
#include "lib/strutil.h"
|
|
|
|
#include "lib/widget.h"
|
2019-07-14 11:18:03 +03:00
|
|
|
#include "lib/keybind.h" /* CK_* */
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
2012-03-28 22:29:02 +04:00
|
|
|
#define B_VIEW (B_USER + 1)
|
|
|
|
#define B_EDIT (B_USER + 2)
|
|
|
|
|
2010-11-12 11:03:57 +03:00
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2019-07-27 11:44:00 +03:00
|
|
|
int y;
|
|
|
|
int x;
|
2010-11-12 11:03:57 +03:00
|
|
|
size_t count;
|
2019-07-20 20:24:30 +03:00
|
|
|
size_t max_width;
|
2010-11-12 11:03:57 +03:00
|
|
|
} history_dlg_data;
|
|
|
|
|
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
|
|
|
|
static cb_ret_t
|
2012-09-28 11:18:45 +04:00
|
|
|
history_dlg_reposition (WDialog * dlg_head)
|
2010-11-12 11:03:57 +03:00
|
|
|
{
|
|
|
|
history_dlg_data *data;
|
|
|
|
int x = 0, y, he, wi;
|
2016-10-14 15:28:00 +03:00
|
|
|
WRect r;
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
/* guard checks */
|
|
|
|
if ((dlg_head == NULL) || (dlg_head->data == NULL))
|
|
|
|
return MSG_NOT_HANDLED;
|
|
|
|
|
|
|
|
data = (history_dlg_data *) dlg_head->data;
|
|
|
|
|
2019-07-27 11:44:00 +03:00
|
|
|
y = data->y;
|
2010-11-12 11:03:57 +03:00
|
|
|
he = data->count + 2;
|
|
|
|
|
|
|
|
if (he <= y || y > (LINES - 6))
|
|
|
|
{
|
2016-04-07 10:52:04 +03:00
|
|
|
he = MIN (he, y - 1);
|
2010-11-12 11:03:57 +03:00
|
|
|
y -= he;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
y++;
|
2016-04-07 10:52:04 +03:00
|
|
|
he = MIN (he, LINES - y);
|
2010-11-12 11:03:57 +03:00
|
|
|
}
|
|
|
|
|
2019-07-27 11:44:00 +03:00
|
|
|
if (data->x > 2)
|
|
|
|
x = data->x - 2;
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2019-07-20 20:24:30 +03:00
|
|
|
wi = data->max_width + 4;
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
if ((wi + x) > COLS)
|
|
|
|
{
|
2016-04-07 10:52:04 +03:00
|
|
|
wi = MIN (wi, COLS);
|
2010-11-12 11:03:57 +03:00
|
|
|
x = COLS - wi;
|
|
|
|
}
|
|
|
|
|
2016-10-14 15:28:00 +03:00
|
|
|
rect_init (&r, y, x, he, wi);
|
|
|
|
dlg_set_position (dlg_head, &r);
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
return MSG_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static cb_ret_t
|
2012-09-28 15:05:43 +04:00
|
|
|
history_dlg_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
|
2010-11-12 11:03:57 +03:00
|
|
|
{
|
|
|
|
switch (msg)
|
|
|
|
{
|
2012-09-28 15:05:43 +04:00
|
|
|
case MSG_RESIZE:
|
|
|
|
return history_dlg_reposition (DIALOG (w));
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2019-07-14 11:18:03 +03:00
|
|
|
case MSG_NOTIFY:
|
|
|
|
{
|
|
|
|
/* message from listbox */
|
|
|
|
WDialog *d = DIALOG (w);
|
|
|
|
|
|
|
|
switch (parm)
|
|
|
|
{
|
|
|
|
case CK_View:
|
|
|
|
d->ret_value = B_VIEW;
|
|
|
|
break;
|
|
|
|
case CK_Edit:
|
|
|
|
d->ret_value = B_EDIT;
|
|
|
|
break;
|
|
|
|
case CK_Enter:
|
|
|
|
d->ret_value = B_ENTER;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return MSG_NOT_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
dlg_stop (d);
|
|
|
|
return MSG_HANDLED;
|
|
|
|
}
|
2012-03-28 22:29:02 +04:00
|
|
|
|
2010-11-12 11:03:57 +03:00
|
|
|
default:
|
2012-09-28 15:05:43 +04:00
|
|
|
return dlg_default_callback (w, sender, msg, parm, data);
|
2010-11-12 11:03:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-27 11:44:00 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static void
|
|
|
|
history_create_item (history_descriptor_t * hd, void *data)
|
|
|
|
{
|
|
|
|
char *text = (char *) data;
|
|
|
|
size_t width;
|
|
|
|
|
|
|
|
width = str_term_width1 (text);
|
|
|
|
hd->max_width = MAX (width, hd->max_width);
|
|
|
|
|
|
|
|
listbox_add_item (hd->listbox, LISTBOX_APPEND_AT_END, 0, text, NULL, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static void *
|
|
|
|
history_release_item (history_descriptor_t * hd, WLEntry * le)
|
|
|
|
{
|
|
|
|
void *text;
|
|
|
|
|
|
|
|
(void) hd;
|
|
|
|
|
|
|
|
text = le->text;
|
|
|
|
le->text = NULL;
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2010-11-12 11:03:57 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2019-07-27 11:44:00 +03:00
|
|
|
void
|
|
|
|
history_descriptor_init (history_descriptor_t * hd, int y, int x, GList * history, int current)
|
|
|
|
{
|
|
|
|
hd->list = history;
|
|
|
|
hd->y = y;
|
|
|
|
hd->x = x;
|
|
|
|
hd->current = current;
|
|
|
|
hd->action = CK_IgnoreKey;
|
|
|
|
hd->text = NULL;
|
|
|
|
hd->max_width = 0;
|
|
|
|
hd->listbox = listbox_new (1, 1, 2, 2, TRUE, NULL);
|
|
|
|
/* in most cases history list contains string only and no any other data */
|
|
|
|
hd->create = history_create_item;
|
|
|
|
hd->release = history_release_item;
|
|
|
|
hd->free = g_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
void
|
|
|
|
history_show (history_descriptor_t * hd)
|
2010-11-12 11:03:57 +03:00
|
|
|
{
|
2019-07-20 20:15:43 +03:00
|
|
|
GList *z, *hi;
|
2019-07-27 11:44:00 +03:00
|
|
|
size_t count;
|
2012-09-28 11:18:45 +04:00
|
|
|
WDialog *query_dlg;
|
2010-11-12 11:03:57 +03:00
|
|
|
history_dlg_data hist_data;
|
2012-03-28 22:29:02 +04:00
|
|
|
int dlg_ret;
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2019-07-27 11:44:00 +03:00
|
|
|
if (hd == NULL || hd->list == NULL)
|
|
|
|
return;
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2019-07-27 11:44:00 +03:00
|
|
|
hd->max_width = str_term_width1 (_("History")) + 2;
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2019-07-27 11:44:00 +03:00
|
|
|
for (z = hd->list; z != NULL; z = g_list_previous (z))
|
|
|
|
hd->create (hd, z->data);
|
|
|
|
/* after this, the order of history items is following: recent at begin, oldest at end */
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2019-07-27 11:44:00 +03:00
|
|
|
count = listbox_get_length (hd->listbox);
|
2019-07-20 20:15:43 +03:00
|
|
|
|
2019-07-27 11:44:00 +03:00
|
|
|
hist_data.y = hd->y;
|
|
|
|
hist_data.x = hd->x;
|
2010-11-12 11:03:57 +03:00
|
|
|
hist_data.count = count;
|
2019-07-27 11:44:00 +03:00
|
|
|
hist_data.max_width = hd->max_width;
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
query_dlg =
|
2016-05-08 21:10:05 +03:00
|
|
|
dlg_create (TRUE, 0, 0, 4, 4, WPOS_KEEP_DEFAULT, TRUE, dialog_colors, history_dlg_callback,
|
|
|
|
NULL, "[History-query]", _("History"));
|
2010-11-12 11:03:57 +03:00
|
|
|
query_dlg->data = &hist_data;
|
|
|
|
|
|
|
|
/* this call makes list stick to all sides of dialog, effectively make
|
|
|
|
it be resized with dialog */
|
2016-09-28 11:01:30 +03:00
|
|
|
group_add_widget_autopos (GROUP (query_dlg), hd->listbox, WPOS_KEEP_ALL, NULL);
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
/* to avoid diplicating of (calculating sizes in two places)
|
2019-07-27 11:44:00 +03:00
|
|
|
code, call history_dlg_callback function here, to set dialog and
|
2010-11-12 11:03:57 +03:00
|
|
|
controls positions.
|
|
|
|
The main idea - create 4x4 dialog and add 2x2 list in
|
2019-07-27 11:44:00 +03:00
|
|
|
center of it, and let dialog function resize it to needed size. */
|
2012-09-28 15:05:43 +04:00
|
|
|
send_message (query_dlg, NULL, MSG_RESIZE, 0, NULL);
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2019-07-27 11:44:00 +03:00
|
|
|
if (WIDGET (query_dlg)->y < hd->y)
|
2010-11-12 11:03:57 +03:00
|
|
|
{
|
2019-09-04 11:23:02 +03:00
|
|
|
/* history is above base widget -- revert order to place recent item at bottom */
|
2019-07-27 11:44:00 +03:00
|
|
|
/* revert history direction */
|
|
|
|
g_queue_reverse (hd->listbox->list);
|
|
|
|
if (hd->current < 0 || (size_t) hd->current >= count)
|
|
|
|
listbox_select_last (hd->listbox);
|
2012-04-27 21:18:21 +04:00
|
|
|
else
|
2019-07-27 11:44:00 +03:00
|
|
|
listbox_select_entry (hd->listbox, count - 1 - (size_t) hd->current);
|
2010-11-12 11:03:57 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-09-04 11:23:02 +03:00
|
|
|
/* history is below base widget -- keep order to place recent item on top */
|
2019-07-27 11:44:00 +03:00
|
|
|
if (hd->current > 0)
|
|
|
|
listbox_select_entry (hd->listbox, hd->current);
|
2010-11-12 11:03:57 +03:00
|
|
|
}
|
|
|
|
|
2012-03-28 22:29:02 +04:00
|
|
|
dlg_ret = dlg_run (query_dlg);
|
|
|
|
if (dlg_ret != B_CANCEL)
|
2010-11-12 11:03:57 +03:00
|
|
|
{
|
|
|
|
char *q;
|
|
|
|
|
2019-07-27 11:44:00 +03:00
|
|
|
switch (dlg_ret)
|
2012-03-28 22:29:02 +04:00
|
|
|
{
|
2019-07-27 11:44:00 +03:00
|
|
|
case B_EDIT:
|
|
|
|
hd->action = CK_Edit;
|
|
|
|
break;
|
|
|
|
case B_VIEW:
|
|
|
|
hd->action = CK_View;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
hd->action = CK_Enter;
|
2012-03-28 22:29:02 +04:00
|
|
|
}
|
|
|
|
|
2019-07-27 11:44:00 +03:00
|
|
|
listbox_get_current (hd->listbox, &q, NULL);
|
|
|
|
hd->text = g_strdup (q);
|
2010-11-12 11:03:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get modified history from dialog */
|
|
|
|
z = NULL;
|
2019-07-27 11:44:00 +03:00
|
|
|
for (hi = listbox_get_first_link (hd->listbox); hi != NULL; hi = g_list_next (hi))
|
2010-11-12 11:03:57 +03:00
|
|
|
/* history is being reverted here again */
|
2019-07-27 11:44:00 +03:00
|
|
|
z = g_list_prepend (z, hd->release (hd, LENTRY (hi->data)));
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
/* restore history direction */
|
2019-09-04 11:23:02 +03:00
|
|
|
if (WIDGET (query_dlg)->y < hd->y)
|
2010-11-12 11:03:57 +03:00
|
|
|
z = g_list_reverse (z);
|
|
|
|
|
2013-06-24 11:40:53 +04:00
|
|
|
dlg_destroy (query_dlg);
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2019-07-27 11:44:00 +03:00
|
|
|
hd->list = g_list_first (hd->list);
|
|
|
|
g_list_free_full (hd->list, hd->free);
|
|
|
|
hd->list = g_list_last (z);
|
2010-11-12 11:03:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|