Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 1 | /**************************************************************************** |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 2 | * Copyright 2020,2021 Thomas E. Dickey * |
| 3 | * Copyright 1998-2010,2012 Free Software Foundation, Inc. * |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4 | * * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a * |
| 6 | * copy of this software and associated documentation files (the * |
| 7 | * "Software"), to deal in the Software without restriction, including * |
| 8 | * without limitation the rights to use, copy, modify, merge, publish, * |
| 9 | * distribute, distribute with modifications, sublicense, and/or sell * |
| 10 | * copies of the Software, and to permit persons to whom the Software is * |
| 11 | * furnished to do so, subject to the following conditions: * |
| 12 | * * |
| 13 | * The above copyright notice and this permission notice shall be included * |
| 14 | * in all copies or substantial portions of the Software. * |
| 15 | * * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * |
| 17 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * |
| 19 | * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * |
| 20 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * |
| 21 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * |
| 22 | * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * |
| 23 | * * |
| 24 | * Except as contained in this notice, the name(s) of the above copyright * |
| 25 | * holders shall not be used in advertising or otherwise to promote the * |
| 26 | * sale, use or other dealings in this Software without prior written * |
| 27 | * authorization. * |
| 28 | ****************************************************************************/ |
| 29 | |
| 30 | /**************************************************************************** |
| 31 | * Author: Juergen Pfeifer, 1995,1997 * |
| 32 | ****************************************************************************/ |
| 33 | |
| 34 | /*************************************************************************** |
| 35 | * Module m_format * |
| 36 | * Set and get maximum numbers of rows and columns in menus * |
| 37 | ***************************************************************************/ |
| 38 | |
| 39 | #include "menu.priv.h" |
| 40 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 41 | MODULE_ID("$Id: m_format.c,v 1.22 2021/03/27 23:46:29 tom Exp $") |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 42 | |
| 43 | #define minimum(a,b) ((a)<(b) ? (a): (b)) |
| 44 | |
| 45 | /*--------------------------------------------------------------------------- |
| 46 | | Facility : libnmenu |
| 47 | | Function : int set_menu_format(MENU *menu, int rows, int cols) |
| 48 | | |
| 49 | | Description : Sets the maximum number of rows and columns of items |
| 50 | | that may be displayed at one time on a menu. If the |
| 51 | | menu contains more items than can be displayed at |
| 52 | | once, the menu will be scrollable. |
| 53 | | |
| 54 | | Return Values : E_OK - success |
| 55 | | E_BAD_ARGUMENT - invalid values passed |
| 56 | | E_NOT_CONNECTED - there are no items connected |
| 57 | | E_POSTED - the menu is already posted |
| 58 | +--------------------------------------------------------------------------*/ |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 59 | MENU_EXPORT(int) |
| 60 | set_menu_format(MENU *menu, int rows, int cols) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 61 | { |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 62 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 63 | T((T_CALLED("set_menu_format(%p,%d,%d)"), (void *)menu, rows, cols)); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 64 | |
| 65 | if (rows < 0 || cols < 0) |
| 66 | RETURN(E_BAD_ARGUMENT); |
| 67 | |
| 68 | if (menu) |
| 69 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 70 | int total_rows, total_cols; |
| 71 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 72 | if (menu->status & _POSTED) |
| 73 | RETURN(E_POSTED); |
| 74 | |
| 75 | if (!(menu->items)) |
| 76 | RETURN(E_NOT_CONNECTED); |
| 77 | |
| 78 | if (rows == 0) |
| 79 | rows = menu->frows; |
| 80 | if (cols == 0) |
| 81 | cols = menu->fcols; |
| 82 | |
| 83 | if (menu->pattern) |
| 84 | Reset_Pattern(menu); |
| 85 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 86 | menu->frows = (short)rows; |
| 87 | menu->fcols = (short)cols; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 88 | |
| 89 | assert(rows > 0 && cols > 0); |
| 90 | total_rows = (menu->nitems - 1) / cols + 1; |
| 91 | total_cols = (menu->opt & O_ROWMAJOR) ? |
| 92 | minimum(menu->nitems, cols) : |
| 93 | (menu->nitems - 1) / total_rows + 1; |
| 94 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 95 | menu->rows = (short)total_rows; |
| 96 | menu->cols = (short)total_cols; |
| 97 | menu->arows = (short)minimum(total_rows, rows); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 98 | menu->toprow = 0; |
| 99 | menu->curitem = *(menu->items); |
| 100 | assert(menu->curitem); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 101 | SetStatus(menu, _LINK_NEEDED); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 102 | _nc_Calculate_Item_Length_and_Width(menu); |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | if (rows > 0) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 107 | _nc_Default_Menu.frows = (short)rows; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 108 | if (cols > 0) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 109 | _nc_Default_Menu.fcols = (short)cols; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | RETURN(E_OK); |
| 113 | } |
| 114 | |
| 115 | /*--------------------------------------------------------------------------- |
| 116 | | Facility : libnmenu |
| 117 | | Function : void menu_format(const MENU *menu, int *rows, int *cols) |
| 118 | | |
| 119 | | Description : Returns the maximum number of rows and columns that may |
| 120 | | be displayed at one time on menu. |
| 121 | | |
| 122 | | Return Values : - |
| 123 | +--------------------------------------------------------------------------*/ |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 124 | MENU_EXPORT(void) |
| 125 | menu_format(const MENU *menu, int *rows, int *cols) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 126 | { |
| 127 | if (rows) |
| 128 | *rows = Normalize_Menu(menu)->frows; |
| 129 | if (cols) |
| 130 | *cols = Normalize_Menu(menu)->fcols; |
| 131 | } |
| 132 | |
| 133 | /* m_format.c ends here */ |