Ticket #2598: u7z: Improve handling of missing p7zip binaries.
[midnight-commander.git] / lib / widget / listbox-window.c
blob94ab3784440922714e07c94354d6066d45e7d08f
1 /* Widget based utility functions.
2 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 Authors: 1994, 1995, 1996 Miguel de Icaza
6 1994, 1995 Radek Doulik
7 1995 Jakub Jelinek
8 1995 Andrej Borsenkow
9 2009, 2010 Andrew Borodin
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 /** \file listbox-window.c
28 * \brief Source: Listbox widget, a listbox within dialog window
31 #include <config.h>
33 #include <stdlib.h>
35 #include "lib/global.h"
36 #include "lib/tty/tty.h" /* COLS */
37 #include "lib/skin.h"
38 #include "lib/strutil.h" /* str_term_width1() */
39 #include "lib/widget.h"
41 /*** global variables ****************************************************************************/
43 /*** file scope macro definitions ****************************************************************/
45 /*** file scope type declarations ****************************************************************/
47 /*** file scope variables ************************************************************************/
49 /*** file scope functions ************************************************************************/
51 /* --------------------------------------------------------------------------------------------- */
52 /*** public functions ****************************************************************************/
53 /* --------------------------------------------------------------------------------------------- */
55 Listbox *
56 create_listbox_window_centered (int center_y, int center_x, int lines, int cols,
57 const char *title, const char *help)
59 const dlg_colors_t listbox_colors = {
60 PMENU_ENTRY_COLOR,
61 PMENU_SELECTED_COLOR,
62 PMENU_ENTRY_COLOR,
63 PMENU_SELECTED_COLOR,
64 PMENU_TITLE_COLOR
67 const int space = 4;
69 int xpos, ypos, len;
70 Listbox *listbox;
72 /* Adjust sizes */
73 lines = min (lines, LINES - 6);
75 if (title != NULL)
77 len = str_term_width1 (title) + 4;
78 cols = max (cols, len);
81 cols = min (cols, COLS - 6);
83 /* adjust position */
84 if ((center_y < 0) || (center_x < 0))
86 ypos = LINES / 2;
87 xpos = COLS / 2;
89 else
91 ypos = center_y;
92 xpos = center_x;
95 ypos -= lines / 2;
96 xpos -= cols / 2;
98 if (ypos + lines >= LINES)
99 ypos = LINES - lines - space;
100 if (ypos < 0)
101 ypos = 0;
103 if (xpos + cols >= COLS)
104 xpos = COLS - cols - space;
105 if (xpos < 0)
106 xpos = 0;
108 listbox = g_new (Listbox, 1);
110 listbox->dlg =
111 create_dlg (TRUE, ypos, xpos, lines + space, cols + space,
112 listbox_colors, NULL, help, title, DLG_REVERSE | DLG_TRYUP);
114 listbox->list = listbox_new (2, 2, lines, cols, FALSE, NULL);
115 add_widget (listbox->dlg, listbox->list);
117 return listbox;
120 /* --------------------------------------------------------------------------------------------- */
122 Listbox *
123 create_listbox_window (int lines, int cols, const char *title, const char *help)
125 return create_listbox_window_centered (-1, -1, lines, cols, title, help);
128 /* --------------------------------------------------------------------------------------------- */
130 /** Returns the number of the item selected */
132 run_listbox (Listbox * l)
134 int val = -1;
136 if (run_dlg (l->dlg) != B_CANCEL)
137 val = l->list->pos;
138 destroy_dlg (l->dlg);
139 g_free (l);
140 return val;
143 /* --------------------------------------------------------------------------------------------- */