1 /* 2 Widget based utility functions. 3 4 Copyright (C) 1994-2025 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 /*** file scope functions ************************************************************************/ 53 54 /* --------------------------------------------------------------------------------------------- */ 55 /*** public functions ****************************************************************************/ 56 /* --------------------------------------------------------------------------------------------- */ 57 58 Listbox * 59 listbox_window_centered_new (int center_y, int center_x, int lines, int cols, const char *title, /**/ 60 const char *help) 61 { 62 const int space = 4; 63 64 int xpos = 0, ypos = 0; 65 Listbox *listbox; 66 widget_pos_flags_t pos_flags = WPOS_TRYUP; 67 68 // Adjust sizes 69 lines = MIN (lines, LINES - 6); 70 71 if (title != NULL) 72 { 73 int len; 74 75 len = str_term_width1 (title) + 4; 76 cols = MAX (cols, len); 77 } 78 79 cols = MIN (cols, COLS - 6); 80 81 // adjust position 82 if ((center_y < 0) || (center_x < 0)) 83 pos_flags |= WPOS_CENTER; 84 else 85 { 86 // Actually, this this is not used in MC. 87 88 ypos = center_y; 89 xpos = center_x; 90 91 ypos -= lines / 2; 92 xpos -= cols / 2; 93 94 if (ypos + lines >= LINES) 95 ypos = LINES - lines - space; 96 if (ypos < 0) 97 ypos = 0; 98 99 if (xpos + cols >= COLS) 100 xpos = COLS - cols - space; 101 if (xpos < 0) 102 xpos = 0; 103 } 104 105 listbox = g_new (Listbox, 1); 106 107 listbox->dlg = dlg_create (TRUE, ypos, xpos, lines + space, cols + space, pos_flags, FALSE, 108 listbox_colors, NULL, NULL, help, title); 109 110 listbox->list = listbox_new (2, 2, lines, cols, FALSE, NULL); 111 group_add_widget (GROUP (listbox->dlg), listbox->list); 112 113 return listbox; 114 } 115 116 /* --------------------------------------------------------------------------------------------- */ 117 118 Listbox * 119 listbox_window_new (int lines, int cols, const char *title, const char *help) /*
*/ 120 { 121 return listbox_window_centered_new (-1, -1, lines, cols, title, help); 122 } 123 124 /* --------------------------------------------------------------------------------------------- */ 125 126 /** Returns the number of the item selected */ 127 int 128 listbox_run (Listbox *l) /*
*/ 129 { 130 int val = -1; 131 132 if (dlg_run (l->dlg) != B_CANCEL) 133 val = l->list->current; 134 widget_destroy (WIDGET (l->dlg)); 135 g_free (l); 136 return val; 137 } 138 139 /* --------------------------------------------------------------------------------------------- */ 140 141 /** 142 * A variant of listbox_run() which is more convenient to use when we 143 * need to select arbitrary 'data'. 144 * 145 * @param select the item to select initially, by its 'data'. Optional. 146 * @return the 'data' of the item selected, or NULL if none selected. 147 */ 148 void * 149 listbox_run_with_data (Listbox *l, const void *select) /*
*/ 150 { 151 void *val = NULL; 152 153 if (select != NULL) 154 listbox_set_current (l->list, listbox_search_data (l->list, select)); 155 156 if (dlg_run (l->dlg) != B_CANCEL) 157 { 158 WLEntry *e; 159 160 e = listbox_get_nth_entry (l->list, l->list->current); 161 if (e != NULL) 162 { 163 /* The assert guards against returning a soon-to-be deallocated 164 * pointer (as in listbox_add_item(..., TRUE)). */ 165 g_assert (!e->free_data); 166 val = e->data; 167 } 168 } 169 170 widget_destroy (WIDGET (l->dlg)); 171 g_free (l); 172 return val; 173 } 174 175 /* --------------------------------------------------------------------------------------------- */