Ticket #2862: fix syncronization with filelist and tree panels
[midnight-commander.git] / lib / widget / listbox-window.c
blob9aa77577a412caf0d66deb7f9363a064480d21bb
1 /*
2 Widget based utility functions.
4 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2008, 2009, 2010, 2011
6 The Free Software Foundation, Inc.
8 Authors:
9 Miguel de Icaza, 1994, 1995, 1996
10 Radek Doulik, 1994, 1995
11 Jakub Jelinek, 1995
12 Andrej Borsenkow, 1995
13 Andrew Borodin <aborodin@vmail.ru>, 2009, 2010
15 This file is part of the Midnight Commander.
17 The Midnight Commander is free software: you can redistribute it
18 and/or modify it under the terms of the GNU General Public License as
19 published by the Free Software Foundation, either version 3 of the License,
20 or (at your option) any later version.
22 The Midnight Commander is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 /** \file listbox-window.c
32 * \brief Source: Listbox widget, a listbox within dialog window
35 #include <config.h>
37 #include <stdlib.h>
39 #include "lib/global.h"
40 #include "lib/tty/tty.h" /* COLS */
41 #include "lib/skin.h"
42 #include "lib/strutil.h" /* str_term_width1() */
43 #include "lib/widget.h"
45 /*** global variables ****************************************************************************/
47 /*** file scope macro definitions ****************************************************************/
49 /*** file scope type declarations ****************************************************************/
51 /*** file scope variables ************************************************************************/
53 /*** file scope functions ************************************************************************/
55 /* --------------------------------------------------------------------------------------------- */
56 /*** public functions ****************************************************************************/
57 /* --------------------------------------------------------------------------------------------- */
59 Listbox *
60 create_listbox_window_centered (int center_y, int center_x, int lines, int cols,
61 const char *title, const char *help)
63 const dlg_colors_t listbox_colors = {
64 PMENU_ENTRY_COLOR,
65 PMENU_SELECTED_COLOR,
66 PMENU_ENTRY_COLOR,
67 PMENU_SELECTED_COLOR,
68 PMENU_TITLE_COLOR
71 const int space = 4;
73 int xpos, ypos, len;
74 Listbox *listbox;
76 /* Adjust sizes */
77 lines = min (lines, LINES - 6);
79 if (title != NULL)
81 len = str_term_width1 (title) + 4;
82 cols = max (cols, len);
85 cols = min (cols, COLS - 6);
87 /* adjust position */
88 if ((center_y < 0) || (center_x < 0))
90 ypos = LINES / 2;
91 xpos = COLS / 2;
93 else
95 ypos = center_y;
96 xpos = center_x;
99 ypos -= lines / 2;
100 xpos -= cols / 2;
102 if (ypos + lines >= LINES)
103 ypos = LINES - lines - space;
104 if (ypos < 0)
105 ypos = 0;
107 if (xpos + cols >= COLS)
108 xpos = COLS - cols - space;
109 if (xpos < 0)
110 xpos = 0;
112 listbox = g_new (Listbox, 1);
114 listbox->dlg =
115 create_dlg (TRUE, ypos, xpos, lines + space, cols + space,
116 listbox_colors, NULL, NULL, help, title, DLG_REVERSE | DLG_TRYUP);
118 listbox->list = listbox_new (2, 2, lines, cols, FALSE, NULL);
119 add_widget (listbox->dlg, listbox->list);
121 return listbox;
124 /* --------------------------------------------------------------------------------------------- */
126 Listbox *
127 create_listbox_window (int lines, int cols, const char *title, const char *help)
129 return create_listbox_window_centered (-1, -1, lines, cols, title, help);
132 /* --------------------------------------------------------------------------------------------- */
134 /** Returns the number of the item selected */
136 run_listbox (Listbox * l)
138 int val = -1;
140 if (run_dlg (l->dlg) != B_CANCEL)
141 val = l->list->pos;
142 destroy_dlg (l->dlg);
143 g_free (l);
144 return val;
147 /* --------------------------------------------------------------------------------------------- */