]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/demo_menus.c
ncurses 5.7 - patch 20101023
[ncurses.git] / test / demo_menus.c
index d572b2d17c99b43ab2786ca725ccbc066926189a..4625ef672fdbe78ed8d21b3cf704c17198771bfd 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
 /****************************************************************************
- * Copyright (c) 2005-2007,2008 Free Software Foundation, Inc.              *
+ * Copyright (c) 2005-2009,2010 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            *
  *                                                                          *
  * 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.                                                           *
  ****************************************************************************/
 /*
  * authorization.                                                           *
  ****************************************************************************/
 /*
- * $Id: demo_menus.c,v 1.23 2008/02/23 23:06:49 tom Exp $
+ * $Id: demo_menus.c,v 1.30 2010/05/01 19:12:26 tom Exp $
  *
  * Demonstrate a variety of functions from the menu library.
  * Thomas Dickey - 2005/4/9
  *
  * Demonstrate a variety of functions from the menu library.
  * Thomas Dickey - 2005/4/9
@@ -107,12 +107,14 @@ static MENU *mpBanner;
 static MENU *mpFile;
 static MENU *mpSelect;
 
 static MENU *mpFile;
 static MENU *mpSelect;
 
+static bool loaded_file = FALSE;
+
 #if !HAVE_STRDUP
 #define strdup my_strdup
 static char *
 strdup(char *s)
 {
 #if !HAVE_STRDUP
 #define strdup my_strdup
 static char *
 strdup(char *s)
 {
-    char *p = (char *) malloc(strlen(s) + 1);
+    char *p = typeMalloc(char, strlen(s) + 1);
     if (p)
        strcpy(p, s);
     return (p);
     if (p)
        strcpy(p, s);
     return (p);
@@ -256,23 +258,35 @@ menu_create(ITEM ** items, int count, int ncols, MenuNo number)
 static void
 menu_destroy(MENU * m)
 {
 static void
 menu_destroy(MENU * m)
 {
-    ITEM **ip;
     int count;
 
     int count;
 
+    Trace(("menu_destroy %p", (void *) m));
     if (m != 0) {
     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);
        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);
        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
     }
        }
 #endif
     }
@@ -340,6 +354,7 @@ build_select_menu(MenuNo number, char *filename)
 
     ITEM **ip;
     CONST_MENUS char **ap = 0;
 
     ITEM **ip;
     CONST_MENUS char **ap = 0;
+    CONST_MENUS char **myList = 0;
     unsigned count = 0;
 
     if (filename != 0) {
     unsigned count = 0;
 
     if (filename != 0) {
@@ -347,13 +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) {
        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;
            unsigned j, k;
-           char *blob = malloc(size + 1);
-           CONST_MENUS char **list =
-           (CONST_MENUS char **) calloc(sizeof(*list), size + 1);
+           char *blob = typeMalloc(char, size + 1);
+           CONST_MENUS char **list = typeCalloc(CONST_MENUS char *, size + 1);
 
 
-           items = (ITEM **) calloc(sizeof(ITEM *), 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) {
            if (blob != 0 && list != 0) {
                FILE *fp = fopen(filename, "r");
                if (fp != 0) {
@@ -375,16 +392,17 @@ build_select_menu(MenuNo number, char *filename)
                        }
                        list[k] = 0;
                        count = k;
                        }
                        list[k] = 0;
                        count = k;
-                       ap = list;
+                       ap = myList = list;
                    }
                    fclose(fp);
                }
                    }
                    fclose(fp);
                }
+               loaded_file = TRUE;
            }
        }
     }
     if (ap == 0) {
        count = SIZEOF(labels) - 1;
            }
        }
     }
     if (ap == 0) {
        count = SIZEOF(labels) - 1;
-       items = (ITEM **) calloc(count + 1, sizeof(*items));
+       items = typeCalloc(ITEM *, count + 1);
        ap = labels;
     }
 
        ap = labels;
     }
 
@@ -394,6 +412,8 @@ build_select_menu(MenuNo number, char *filename)
     *ip = 0;
 
     mpSelect = menu_create(items, (int) count, 1, number);
     *ip = 0;
 
     mpSelect = menu_create(items, (int) count, 1, number);
+    if (myList != 0)
+       free(myList);
 }
 
 static int
 }
 
 static int
@@ -457,7 +477,7 @@ tracetrace(unsigned tlevel)
        size_t need = 12;
        for (n = 0; t_tbl[n].name != 0; n++)
            need += strlen(t_tbl[n].name) + 2;
        size_t need = 12;
        for (n = 0; t_tbl[n].name != 0; n++)
            need += strlen(t_tbl[n].name) + 2;
-       buf = (char *) malloc(need);
+       buf = typeMalloc(char, need);
     }
     sprintf(buf, "0x%02x = {", tlevel);
     if (tlevel == 0) {
     }
     sprintf(buf, "0x%02x = {", tlevel);
     if (tlevel == 0) {
@@ -526,7 +546,7 @@ perform_trace_menu(int cmd)
            trace(newtrace);
            Trace(("trace level interactively set to %s", tracetrace(_nc_tracing)));
 
            trace(newtrace);
            Trace(("trace level interactively set to %s", tracetrace(_nc_tracing)));
 
-           (void) mvprintw(LINES - 2, 0,
+           MvPrintw(LINES - 2, 0,
                            "Trace level is %s\n", tracetrace(_nc_tracing));
            refresh();
        }
                            "Trace level is %s\n", tracetrace(_nc_tracing));
            refresh();
        }