m_format.c 5.3 KB

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