X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=test%2Fdemo_menus.c;h=7a3db9cf3863091bd28400e6743cea18807dd0d8;hp=34d582ed4a20afa2e7536eca7772ab5e703a9a5b;hb=5bfda8791637989e00be64748603cf4be2efbce5;hpb=396a05943b7da5039dd15d79c4385c7d2a75d6d4 diff --git a/test/demo_menus.c b/test/demo_menus.c index 34d582ed..7a3db9cf 100644 --- a/test/demo_menus.c +++ b/test/demo_menus.c @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright (c) 2005-2007,2008 Free Software Foundation, Inc. * + * Copyright (c) 2005-2011,2012 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 * @@ -26,7 +26,7 @@ * authorization. * ****************************************************************************/ /* - * $Id: demo_menus.c,v 1.24 2008/04/12 22:03:33 tom Exp $ + * $Id: demo_menus.c,v 1.34 2012/06/09 20:30:33 tom Exp $ * * Demonstrate a variety of functions from the menu library. * Thomas Dickey - 2005/4/9 @@ -107,6 +107,8 @@ static MENU *mpBanner; static MENU *mpFile; static MENU *mpSelect; +static bool loaded_file = FALSE; + #if !HAVE_STRDUP #define strdup my_strdup static char * @@ -200,7 +202,7 @@ menu_offset(MenuNo number) #endif /* FIXME: MENU.itemlen seems the only way to get actual width of items */ - result = (number - (eBanner + 1)) * (mpBanner->itemlen + spc_rows); + result = (number - (eBanner + 1)) * (menu_itemwidth(mpBanner) + spc_rows); } return result; } @@ -223,8 +225,8 @@ menu_create(ITEM ** items, int count, int ncols, MenuNo number) result = new_menu(items); if (has_colors()) { - set_menu_fore(result, COLOR_PAIR(1)); - set_menu_back(result, COLOR_PAIR(2)); + set_menu_fore(result, (chtype) COLOR_PAIR(1)); + set_menu_back(result, (chtype) COLOR_PAIR(2)); } set_menu_format(result, maxrow, maxcol); @@ -256,23 +258,35 @@ menu_create(ITEM ** items, int count, int ncols, MenuNo number) static void menu_destroy(MENU * m) { - ITEM **ip; int count; + Trace(("menu_destroy %p", (void *) m)); if (m != 0) { - delwin(menu_sub(m)); - delwin(menu_win(m)); + ITEM **items = menu_items(m); + const char *blob = 0; - ip = menu_items(m); count = item_count(m); + Trace(("menu_destroy %p count %d", (void *) m, count)); + if ((count > 0) && (m == mpSelect)) { + blob = item_name(*items); + } + unpost_menu(m); free_menu(m); -#if 0 - if (count > 0) { - while (*ip) { - Trace(("freeing item %d:%d", ip - menu_items(m), count)); - free_item(*ip++); + + /* free the extra data allocated in build_select_menu() */ + if ((count > 0) && (m == mpSelect)) { + if (blob && loaded_file) { + Trace(("freeing blob %p", blob)); + free((char *) blob); } + free(items); + } +#ifdef TRACE + if ((count > 0) && (m == mpTrace)) { + ITEM **ip = items; + while (*ip) + free(*ip++); } #endif } @@ -340,6 +354,7 @@ build_select_menu(MenuNo number, char *filename) ITEM **ip; CONST_MENUS char **ap = 0; + CONST_MENUS char **myList = 0; unsigned count = 0; if (filename != 0) { @@ -347,12 +362,15 @@ build_select_menu(MenuNo number, char *filename) if (stat(filename, &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFREG && sb.st_size != 0) { - unsigned size = sb.st_size; + size_t size = (size_t) sb.st_size; unsigned j, k; char *blob = typeMalloc(char, size + 1); CONST_MENUS char **list = typeCalloc(CONST_MENUS char *, size + 1); items = typeCalloc(ITEM *, size + 1); + Trace(("build_select_menu blob=%p, items=%p", + (void *) blob, + (void *) items)); if (blob != 0 && list != 0) { FILE *fp = fopen(filename, "r"); if (fp != 0) { @@ -374,10 +392,11 @@ build_select_menu(MenuNo number, char *filename) } list[k] = 0; count = k; - ap = list; + ap = myList = list; } fclose(fp); } + loaded_file = TRUE; } } } @@ -393,6 +412,8 @@ build_select_menu(MenuNo number, char *filename) *ip = 0; mpSelect = menu_create(items, (int) count, 1, number); + if (myList != 0) + free(myList); } static int @@ -525,8 +546,8 @@ perform_trace_menu(int cmd) trace(newtrace); Trace(("trace level interactively set to %s", tracetrace(_nc_tracing))); - (void) mvprintw(LINES - 2, 0, - "Trace level is %s\n", tracetrace(_nc_tracing)); + MvPrintw(LINES - 2, 0, + "Trace level is %s\n", tracetrace(_nc_tracing)); refresh(); } } @@ -810,7 +831,7 @@ usage(void) { static const char *const tbl[] = { - "Usage: demo_menus [options]" + "Usage: demo_menus [options] [menu-file]" ,"" ,"Options:" #if HAVE_RIPOFFLINE