📄 m_global.c
字号:
/**************************************************************************** * Copyright (c) 1998-2004,2005 Free Software Foundation, Inc. * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * * "Software"), to deal in the Software without restriction, including * * without limitation the rights to use, copy, modify, merge, publish, * * distribute, distribute with modifications, sublicense, and/or sell * * copies of the Software, and to permit persons to whom the Software is * * furnished to do so, subject to the following conditions: * * * * The above copyright notice and this permission notice shall be included * * in all copies or substantial portions of the Software. * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * * * * Except as contained in this notice, the name(s) of the above copyright * * holders shall not be used in advertising or otherwise to promote the * * sale, use or other dealings in this Software without prior written * * authorization. * ****************************************************************************//**************************************************************************** * Author: Juergen Pfeifer, 1995,1997 * ****************************************************************************//**************************************************************************** Module m_global ** Globally used internal routines and the default menu and item structures ****************************************************************************/#include "menu.priv.h"MODULE_ID("$Id: m_global.c,v 1.20 2005/04/16 17:30:57 tom Exp $")static char mark[] = "-";/* *INDENT-OFF* */NCURSES_EXPORT_VAR(MENU) _nc_Default_Menu = { 16, /* Nr. of chars high */ 1, /* Nr. of chars wide */ 16, /* Nr. of items high */ 1, /* Nr. of items wide */ 16, /* Nr. of formatted items high */ 1, /* Nr. of formatted items wide */ 16, /* Nr. of items high (actual) */ 0, /* length of widest name */ 0, /* length of widest description */ 1, /* length of mark */ 1, /* length of one item */ 1, /* Spacing for descriptor */ 1, /* Spacing for columns */ 1, /* Spacing for rows */ (char *)0, /* buffer used to store match chars */ 0, /* Index into pattern buffer */ (WINDOW *)0, /* Window containing entire menu */ (WINDOW *)0, /* Portion of menu displayed */ (WINDOW *)0, /* User's window */ (WINDOW *)0, /* User's subwindow */ (ITEM **)0, /* List of items */ 0, /* Total Nr. of items in menu */ (ITEM *)0, /* Current item */ 0, /* Top row of menu */ (chtype)A_REVERSE, /* Attribute for selection */ (chtype)A_NORMAL, /* Attribute for nonselection */ (chtype)A_UNDERLINE, /* Attribute for inactive */ ' ', /* Pad character */ (Menu_Hook)0, /* Menu init */ (Menu_Hook)0, /* Menu term */ (Menu_Hook)0, /* Item init */ (Menu_Hook)0, /* Item term */ (void *)0, /* userptr */ mark, /* mark */ ALL_MENU_OPTS, /* options */ 0 /* status */ };NCURSES_EXPORT_VAR(ITEM) _nc_Default_Item = { { (char *)0, 0 }, /* name */ { (char *)0, 0 }, /* description */ (MENU *)0, /* Pointer to parent menu */ (char *)0, /* Userpointer */ ALL_ITEM_OPTS, /* options */ 0, /* Item Nr. */ 0, /* y */ 0, /* x */ FALSE, /* value */ (ITEM *)0, /* left */ (ITEM *)0, /* right */ (ITEM *)0, /* up */ (ITEM *)0 /* down */ };/* *INDENT-ON* *//*---------------------------------------------------------------------------| Facility : libnmenu | Function : static void ComputeMaximum_NameDesc_Lenths(MENU *menu)| | Description : Calculates the maximum name and description lengths| of the items connected to the menu|| Return Values : -+--------------------------------------------------------------------------*/INLINE static voidComputeMaximum_NameDesc_Lengths(MENU * menu){ unsigned MaximumNameLength = 0; unsigned MaximumDescriptionLength = 0; ITEM **items; assert(menu && menu->items); for (items = menu->items; *items; items++) { if (items[0]->name.length > MaximumNameLength) MaximumNameLength = items[0]->name.length; if (items[0]->description.length > MaximumDescriptionLength) MaximumDescriptionLength = items[0]->description.length; } menu->namelen = MaximumNameLength; menu->desclen = MaximumDescriptionLength; T(("ComputeMaximum_NameDesc_Lengths %d,%d", menu->namelen, menu->desclen));}/*---------------------------------------------------------------------------| Facility : libnmenu | Function : static void ResetConnectionInfo(MENU *, ITEM **)| | Description : Reset all informations in the menu and the items in| the item array that indicates a connection|| Return Values : -+--------------------------------------------------------------------------*/INLINE static voidResetConnectionInfo(MENU * menu, ITEM ** items){ ITEM **item; assert(menu && items); for (item = items; *item; item++) { (*item)->index = 0; (*item)->imenu = (MENU *) 0; } if (menu->pattern) free(menu->pattern); menu->pattern = (char *)0; menu->pindex = 0; menu->items = (ITEM **) 0; menu->nitems = 0;}/*---------------------------------------------------------------------------| Facility : libnmenu | Function : bool _nc_Connect_Items(MENU *menu, ITEM **items)|| Description : Connect the items in the item array to the menu.| Decorate all the items with a number and a backward| pointer to the menu.|| Return Values : TRUE - successful connection| FALSE - connection failed+--------------------------------------------------------------------------*/NCURSES_EXPORT(bool)_nc_Connect_Items(MENU * menu, ITEM ** items){ ITEM **item; unsigned int ItemCount = 0; if (menu && items) { for (item = items; *item; item++) { if ((*item)->imenu) { /* if a item is already connected, reject connection */ break; } } if (!(*item)) /* we reached the end, so there was no connected item */ { for (item = items; *item; item++) { if (menu->opt & O_ONEVALUE) { (*item)->value = FALSE; } (*item)->index = ItemCount++; (*item)->imenu = menu; } } } else return (FALSE); if (ItemCount != 0) { menu->items = items; menu->nitems = ItemCount; ComputeMaximum_NameDesc_Lengths(menu); if ((menu->pattern = typeMalloc(char, (unsigned)(1 + menu->namelen)))) { Reset_Pattern(menu); set_menu_format(menu, menu->frows, menu->fcols); menu->curitem = *items; menu->toprow = 0; return (TRUE); } } /* If we fall through to this point, we have to reset all items connection and inform about a reject connection */ ResetConnectionInfo(menu, items); return (FALSE);}/*---------------------------------------------------------------------------| Facility : libnmenu | Function : void _nc_Disconnect_Items(MENU *menu)| | Description : Disconnect the menus item array from the menu|| Return Values : -+--------------------------------------------------------------------------*/NCURSES_EXPORT(void)_nc_Disconnect_Items(MENU * menu){ if (menu && menu->items) ResetConnectionInfo(menu, menu->items);}/*---------------------------------------------------------------------------| Facility : libnmenu | Function : int _nc_Calculate_Text_Width(const TEXT * item)| | Description : Calculate the number of columns for a TEXT.|| Return Values : the width+--------------------------------------------------------------------------*/NCURSES_EXPORT(int)_nc_Calculate_Text_Width(const TEXT * item /*FIXME: limit length */ ){#if USE_WIDEC_SUPPORT int result = item->length; int count = mbstowcs(0, item->str, 0); wchar_t *temp = 0; T((T_CALLED("_nc_menu_text_width(%p)"), item)); if (count > 0 && (temp = typeMalloc(wchar_t, 2 + count)) != 0) { int n; result = 0; mbstowcs(temp, item->str, (unsigned)count); for (n = 0; n < count; ++n) { int test = wcwidth(temp[n]); if (test <= 0) test = 1; result += test; } free(temp); } returnCode(result);#else return item->length;#endif}/* FIXME: this is experimental, should cache the results but don't want to * modify the MENU struct to do this until it's complete. */#if 0 /* USE_WIDEC_SUPPORT */static intcalculate_actual_width(MENU * menu, bool name){ int width = 0; int check = 0; ITEM **items; assert(menu && menu->items); for (items = menu->items; *items; items++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -