1 /*
2 Widget based utility functions.
3
4 Copyright (C) 1994-2026
5 Free Software Foundation, Inc.
6
7 Authors:
8 Miguel de Icaza, 1994, 1995, 1996
9 Radek Doulik, 1994, 1995
10 Jakub Jelinek, 1995
11 Andrej Borsenkow, 1995
12 Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2013
13
14 This file is part of the Midnight Commander.
15
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.
20
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.
25
26 You should have received a copy of the GNU General Public License
27 along with this program. If not, see <https://www.gnu.org/licenses/>.
28 */
29
30 /** \file listbox-window.c
31 * \brief Source: Listbox widget, a listbox within dialog window
32 */
33
34 #include <config.h>
35
36 #include <stdlib.h>
37
38 #include "lib/global.h"
39 #include "lib/tty/tty.h" // COLS
40 #include "lib/skin.h"
41 #include "lib/strutil.h" // str_term_width1()
42 #include "lib/widget.h"
43
44 /*** global variables ****************************************************************************/
45
46 /*** file scope macro definitions ****************************************************************/
47
48 /*** file scope type declarations ****************************************************************/
49
50 /*** file scope variables ************************************************************************/
51
52 static const dlg_colors_t listbox_colors = {
53 [DLG_COLOR_NORMAL] = PMENU_ENTRY_COLOR,
54 [DLG_COLOR_FOCUS] = PMENU_SELECTED_COLOR,
55 [DLG_COLOR_HOT_NORMAL] = PMENU_ENTRY_COLOR,
56 [DLG_COLOR_HOT_FOCUS] = PMENU_SELECTED_COLOR,
57 [DLG_COLOR_SELECTED_NORMAL] = PMENU_SELECTED_COLOR, // unused
58 [DLG_COLOR_SELECTED_FOCUS] = PMENU_SELECTED_COLOR, // unused
59 [DLG_COLOR_TITLE] = PMENU_TITLE_COLOR,
60 [DLG_COLOR_FRAME] = PMENU_FRAME_COLOR,
61 };
62
63 /*** file scope functions ************************************************************************/
64
65 /* --------------------------------------------------------------------------------------------- */
66 /*** public functions ****************************************************************************/
67 /* --------------------------------------------------------------------------------------------- */
68
69 Listbox *
70 listbox_window_centered_new (int center_y, int center_x, int lines, int cols, const char *title,
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
71 const char *help)
72 {
73 const int space = 4;
74
75 int xpos = 0, ypos = 0;
76 Listbox *listbox;
77 widget_pos_flags_t pos_flags = WPOS_TRYUP;
78
79 // Adjust sizes
80 lines = MIN (lines, LINES - 6);
81
82 if (title != NULL)
83 {
84 int len;
85
86 len = str_term_width1 (title) + 4;
87 cols = MAX (cols, len);
88 }
89
90 cols = MIN (cols, COLS - 6);
91
92 // adjust position
93 if ((center_y < 0) || (center_x < 0))
94 pos_flags |= WPOS_CENTER;
95 else
96 {
97 // Actually, this this is not used in MC.
98
99 ypos = center_y;
100 xpos = center_x;
101
102 ypos -= lines / 2;
103 xpos -= cols / 2;
104
105 if (ypos + lines >= LINES)
106 ypos = LINES - lines - space;
107 if (ypos < 0)
108 ypos = 0;
109
110 if (xpos + cols >= COLS)
111 xpos = COLS - cols - space;
112 if (xpos < 0)
113 xpos = 0;
114 }
115
116 listbox = g_new (Listbox, 1);
117
118 listbox->dlg = dlg_create (TRUE, ypos, xpos, lines + space, cols + space, pos_flags, FALSE,
119 listbox_colors, NULL, NULL, help, title);
120
121 listbox->list = listbox_new (2, 2, lines, cols, FALSE, NULL);
122 group_add_widget (GROUP (listbox->dlg), listbox->list);
123
124 return listbox;
125 }
126
127 /* --------------------------------------------------------------------------------------------- */
128
129 Listbox *
130 listbox_window_new (int lines, int cols, const char *title, const char *help)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
131 {
132 return listbox_window_centered_new (-1, -1, lines, cols, title, help);
133 }
134
135 /* --------------------------------------------------------------------------------------------- */
136
137 /** Returns the number of the item selected */
138 int
139 listbox_run (Listbox *l)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
140 {
141 int val = -1;
142
143 if (dlg_run (l->dlg) != B_CANCEL)
144 val = l->list->current;
145 widget_destroy (WIDGET (l->dlg));
146 g_free (l);
147 return val;
148 }
149
150 /* --------------------------------------------------------------------------------------------- */
151
152 /**
153 * A variant of listbox_run() which is more convenient to use when we
154 * need to select arbitrary 'data'.
155 *
156 * @param select the item to select initially, by its 'data'. Optional.
157 * @return the 'data' of the item selected, or NULL if none selected.
158 */
159 void *
160 listbox_run_with_data (Listbox *l, const void *select)
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
161 {
162 void *val = NULL;
163
164 if (select != NULL)
165 listbox_set_current (l->list, listbox_search_data (l->list, select));
166
167 if (dlg_run (l->dlg) != B_CANCEL)
168 {
169 WLEntry *e;
170
171 e = listbox_get_nth_entry (l->list, l->list->current);
172 if (e != NULL)
173 {
174 /* The assert guards against returning a soon-to-be deallocated
175 * pointer (as in listbox_add_item(..., TRUE)). */
176 g_assert (!e->free_data);
177 val = e->data;
178 }
179 }
180
181 widget_destroy (WIDGET (l->dlg));
182 g_free (l);
183 return val;
184 }
185
186 /* --------------------------------------------------------------------------------------------- */