2011-10-15 14:56:47 +04:00
|
|
|
/*
|
|
|
|
Widget based utility functions.
|
|
|
|
|
2014-01-05 12:37:43 +04:00
|
|
|
Copyright (C) 1994-2014
|
2011-10-15 14:56:47 +04:00
|
|
|
The Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
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 *
|
|
|
|
create_listbox_window_centered (int center_y, int center_x, int lines, int cols,
|
|
|
|
const char *title, const char *help)
|
|
|
|
{
|
|
|
|
const int space = 4;
|
|
|
|
|
2013-10-15 10:34:04 +04:00
|
|
|
int xpos, ypos;
|
2010-11-12 11:03:57 +03:00
|
|
|
Listbox *listbox;
|
|
|
|
|
|
|
|
/* Adjust sizes */
|
|
|
|
lines = min (lines, LINES - 6);
|
|
|
|
|
|
|
|
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;
|
|
|
|
cols = max (cols, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
cols = min (cols, COLS - 6);
|
|
|
|
|
|
|
|
/* adjust position */
|
|
|
|
if ((center_y < 0) || (center_x < 0))
|
|
|
|
{
|
|
|
|
ypos = LINES / 2;
|
|
|
|
xpos = COLS / 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ypos = center_y;
|
|
|
|
xpos = center_x;
|
|
|
|
}
|
|
|
|
|
|
|
|
ypos -= lines / 2;
|
|
|
|
xpos -= cols / 2;
|
|
|
|
|
|
|
|
if (ypos + lines >= LINES)
|
|
|
|
ypos = LINES - lines - space;
|
|
|
|
if (ypos < 0)
|
|
|
|
ypos = 0;
|
|
|
|
|
|
|
|
if (xpos + cols >= COLS)
|
|
|
|
xpos = COLS - cols - space;
|
|
|
|
if (xpos < 0)
|
|
|
|
xpos = 0;
|
|
|
|
|
|
|
|
listbox = g_new (Listbox, 1);
|
|
|
|
|
|
|
|
listbox->dlg =
|
2013-06-24 11:40:53 +04:00
|
|
|
dlg_create (TRUE, ypos, xpos, lines + space, cols + space,
|
2012-09-17 15:09:13 +04:00
|
|
|
listbox_colors, NULL, NULL, help, title, DLG_TRYUP);
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
listbox->list = listbox_new (2, 2, lines, cols, FALSE, NULL);
|
|
|
|
add_widget (listbox->dlg, listbox->list);
|
|
|
|
|
|
|
|
return listbox;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
Listbox *
|
|
|
|
create_listbox_window (int lines, int cols, const char *title, const char *help)
|
|
|
|
{
|
|
|
|
return create_listbox_window_centered (-1, -1, lines, cols, title, help);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/** Returns the number of the item selected */
|
|
|
|
int
|
|
|
|
run_listbox (Listbox * l)
|
|
|
|
{
|
|
|
|
int val = -1;
|
|
|
|
|
2013-06-24 11:40:53 +04:00
|
|
|
if (dlg_run (l->dlg) != B_CANCEL)
|
2010-11-12 11:03:57 +03:00
|
|
|
val = l->list->pos;
|
2013-06-24 11:40:53 +04:00
|
|
|
dlg_destroy (l->dlg);
|
2010-11-12 11:03:57 +03:00
|
|
|
g_free (l);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|