2 Widget based utility functions.
4 Copyright (C) 1994-2024
5 Free Software Foundation, Inc.
8 Miguel de Icaza, 1994, 1995, 1996
9 Radek Doulik, 1994, 1995
11 Andrej Borsenkow, 1995
12 Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2013
14 This file is part of the Midnight Commander.
16 The Midnight Commander is free software: you can redistribute it
17 and/or modify it under the terms of the GNU General Public License as
18 published by the Free Software Foundation, either version 3 of the License,
19 or (at your option) any later version.
21 The Midnight Commander is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 /** \file listbox-window.c
31 * \brief Source: Listbox widget, a listbox within dialog window
38 #include "lib/global.h"
39 #include "lib/tty/tty.h" /* COLS */
41 #include "lib/strutil.h" /* str_term_width1() */
42 #include "lib/widget.h"
44 /*** global variables ****************************************************************************/
46 /*** file scope macro definitions ****************************************************************/
48 /*** file scope type declarations ****************************************************************/
50 /*** file scope variables ************************************************************************/
52 /*** file scope functions ************************************************************************/
54 /* --------------------------------------------------------------------------------------------- */
55 /*** public functions ****************************************************************************/
56 /* --------------------------------------------------------------------------------------------- */
59 listbox_window_centered_new (int center_y
, int center_x
, int lines
, int cols
,
60 const char *title
, const char *help
)
64 int xpos
= 0, ypos
= 0;
66 widget_pos_flags_t pos_flags
= WPOS_TRYUP
;
69 lines
= MIN (lines
, LINES
- 6);
75 len
= str_term_width1 (title
) + 4;
76 cols
= MAX (cols
, len
);
79 cols
= MIN (cols
, COLS
- 6);
82 if ((center_y
< 0) || (center_x
< 0))
83 pos_flags
|= WPOS_CENTER
;
86 /* Actually, this this is not used in MC. */
94 if (ypos
+ lines
>= LINES
)
95 ypos
= LINES
- lines
- space
;
99 if (xpos
+ cols
>= COLS
)
100 xpos
= COLS
- cols
- space
;
105 listbox
= g_new (Listbox
, 1);
108 dlg_create (TRUE
, ypos
, xpos
, lines
+ space
, cols
+ space
, pos_flags
, FALSE
, listbox_colors
,
109 NULL
, NULL
, help
, title
);
111 listbox
->list
= listbox_new (2, 2, lines
, cols
, FALSE
, NULL
);
112 group_add_widget (GROUP (listbox
->dlg
), listbox
->list
);
117 /* --------------------------------------------------------------------------------------------- */
120 listbox_window_new (int lines
, int cols
, const char *title
, const char *help
)
122 return listbox_window_centered_new (-1, -1, lines
, cols
, title
, help
);
125 /* --------------------------------------------------------------------------------------------- */
127 /** Returns the number of the item selected */
129 listbox_run (Listbox
*l
)
133 if (dlg_run (l
->dlg
) != B_CANCEL
)
134 val
= l
->list
->current
;
135 widget_destroy (WIDGET (l
->dlg
));
140 /* --------------------------------------------------------------------------------------------- */
143 * A variant of listbox_run() which is more convenient to use when we
144 * need to select arbitrary 'data'.
146 * @param select the item to select initially, by its 'data'. Optional.
147 * @return the 'data' of the item selected, or NULL if none selected.
150 listbox_run_with_data (Listbox
*l
, const void *select
)
155 listbox_set_current (l
->list
, listbox_search_data (l
->list
, select
));
157 if (dlg_run (l
->dlg
) != B_CANCEL
)
161 e
= listbox_get_nth_entry (l
->list
, l
->list
->current
);
164 /* The assert guards against returning a soon-to-be deallocated
165 * pointer (as in listbox_add_item(..., TRUE)). */
166 g_assert (!e
->free_data
);
171 widget_destroy (WIDGET (l
->dlg
));
176 /* --------------------------------------------------------------------------------------------- */