2011-10-15 14:56:47 +04:00
|
|
|
/*
|
|
|
|
Widget based utility functions.
|
|
|
|
|
2023-01-03 11:30:57 +03:00
|
|
|
Copyright (C) 1994-2023
|
2014-02-12 10:33:10 +04:00
|
|
|
Free Software Foundation, Inc.
|
2011-10-15 14:56:47 +04:00
|
|
|
|
|
|
|
Authors:
|
|
|
|
Miguel de Icaza, 1994, 1995, 1996
|
|
|
|
Radek Doulik, 1994, 1995
|
|
|
|
Jakub Jelinek, 1995
|
|
|
|
Andrej Borsenkow, 1995
|
2013-06-24 11:40:53 +04:00
|
|
|
Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2013
|
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.
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
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 listbox-window.c
|
|
|
|
* \brief Source: Listbox widget, a listbox within dialog window
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "lib/global.h"
|
|
|
|
#include "lib/tty/tty.h" /* COLS */
|
|
|
|
#include "lib/skin.h"
|
|
|
|
#include "lib/strutil.h" /* str_term_width1() */
|
|
|
|
#include "lib/widget.h"
|
|
|
|
|
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
Listbox *
|
2023-03-18 09:45:24 +03:00
|
|
|
listbox_window_centered_new (int center_y, int center_x, int lines, int cols,
|
|
|
|
const char *title, const char *help)
|
2010-11-12 11:03:57 +03:00
|
|
|
{
|
|
|
|
const int space = 4;
|
|
|
|
|
2014-02-22 14:56:35 +04:00
|
|
|
int xpos = 0, ypos = 0;
|
2010-11-12 11:03:57 +03:00
|
|
|
Listbox *listbox;
|
2016-05-08 21:10:05 +03:00
|
|
|
widget_pos_flags_t pos_flags = WPOS_TRYUP;
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
/* Adjust sizes */
|
2016-04-07 10:52:04 +03:00
|
|
|
lines = MIN (lines, LINES - 6);
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
if (title != NULL)
|
|
|
|
{
|
2013-10-15 10:34:04 +04:00
|
|
|
int len;
|
|
|
|
|
2010-11-12 11:03:57 +03:00
|
|
|
len = str_term_width1 (title) + 4;
|
2016-04-07 10:52:04 +03:00
|
|
|
cols = MAX (cols, len);
|
2010-11-12 11:03:57 +03:00
|
|
|
}
|
|
|
|
|
2016-04-07 10:52:04 +03:00
|
|
|
cols = MIN (cols, COLS - 6);
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
/* adjust position */
|
|
|
|
if ((center_y < 0) || (center_x < 0))
|
2016-05-08 21:10:05 +03:00
|
|
|
pos_flags |= WPOS_CENTER;
|
2010-11-12 11:03:57 +03:00
|
|
|
else
|
|
|
|
{
|
2014-02-22 14:56:35 +04:00
|
|
|
/* Actually, this this is not used in MC. */
|
|
|
|
|
2010-11-12 11:03:57 +03:00
|
|
|
ypos = center_y;
|
|
|
|
xpos = center_x;
|
|
|
|
|
2014-02-22 14:56:35 +04:00
|
|
|
ypos -= lines / 2;
|
|
|
|
xpos -= cols / 2;
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2014-02-22 14:56:35 +04:00
|
|
|
if (ypos + lines >= LINES)
|
|
|
|
ypos = LINES - lines - space;
|
|
|
|
if (ypos < 0)
|
|
|
|
ypos = 0;
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2014-02-22 14:56:35 +04:00
|
|
|
if (xpos + cols >= COLS)
|
|
|
|
xpos = COLS - cols - space;
|
|
|
|
if (xpos < 0)
|
|
|
|
xpos = 0;
|
|
|
|
}
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
listbox = g_new (Listbox, 1);
|
|
|
|
|
|
|
|
listbox->dlg =
|
2016-05-08 21:10:05 +03:00
|
|
|
dlg_create (TRUE, ypos, xpos, lines + space, cols + space, pos_flags, FALSE, listbox_colors,
|
|
|
|
NULL, NULL, help, title);
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
listbox->list = listbox_new (2, 2, lines, cols, FALSE, NULL);
|
2016-09-28 11:01:30 +03:00
|
|
|
group_add_widget (GROUP (listbox->dlg), listbox->list);
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
return listbox;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
Listbox *
|
2023-03-18 09:45:24 +03:00
|
|
|
listbox_window_new (int lines, int cols, const char *title, const char *help)
|
2010-11-12 11:03:57 +03:00
|
|
|
{
|
2023-03-18 09:45:24 +03:00
|
|
|
return listbox_window_centered_new (-1, -1, lines, cols, title, help);
|
2010-11-12 11:03:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/** Returns the number of the item selected */
|
|
|
|
int
|
2023-03-18 09:45:24 +03:00
|
|
|
listbox_run (Listbox * l)
|
2010-11-12 11:03:57 +03:00
|
|
|
{
|
|
|
|
int val = -1;
|
|
|
|
|
2013-06-24 11:40:53 +04:00
|
|
|
if (dlg_run (l->dlg) != B_CANCEL)
|
2023-04-15 14:56:07 +03:00
|
|
|
val = l->list->current;
|
2020-10-31 19:32:29 +03:00
|
|
|
widget_destroy (WIDGET (l->dlg));
|
2010-11-12 11:03:57 +03:00
|
|
|
g_free (l);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2017-01-25 15:07:18 +03:00
|
|
|
|
|
|
|
/**
|
2023-03-18 09:45:24 +03:00
|
|
|
* A variant of listbox_run() which is more convenient to use when we
|
2017-01-25 15:07:18 +03:00
|
|
|
* need to select arbitrary 'data'.
|
|
|
|
*
|
|
|
|
* @param select the item to select initially, by its 'data'. Optional.
|
|
|
|
* @return the 'data' of the item selected, or NULL if none selected.
|
|
|
|
*/
|
|
|
|
void *
|
2023-03-18 09:45:24 +03:00
|
|
|
listbox_run_with_data (Listbox * l, const void *select)
|
2017-01-25 15:07:18 +03:00
|
|
|
{
|
|
|
|
void *val = NULL;
|
|
|
|
|
|
|
|
if (select != NULL)
|
2023-04-15 14:56:07 +03:00
|
|
|
listbox_set_current (l->list, listbox_search_data (l->list, select));
|
2017-01-25 15:07:18 +03:00
|
|
|
|
|
|
|
if (dlg_run (l->dlg) != B_CANCEL)
|
|
|
|
{
|
|
|
|
WLEntry *e;
|
2023-04-15 14:56:07 +03:00
|
|
|
|
|
|
|
e = listbox_get_nth_entry (l->list, l->list->current);
|
2017-01-25 15:07:18 +03:00
|
|
|
if (e != NULL)
|
|
|
|
{
|
|
|
|
/* The assert guards against returning a soon-to-be deallocated
|
|
|
|
* pointer (as in listbox_add_item(..., TRUE)). */
|
|
|
|
g_assert (!e->free_data);
|
|
|
|
val = e->data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-31 19:32:29 +03:00
|
|
|
widget_destroy (WIDGET (l->dlg));
|
2017-01-25 15:07:18 +03:00
|
|
|
g_free (l);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|