home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / screen / m / modesel / !ModeSel_c_CreateMenu < prev    next >
Encoding:
Text File  |  1994-06-20  |  4.1 KB  |  150 lines

  1. #include "stdlib.h"
  2. #include "string.h"
  3. #include "wimp.h"
  4. #include "types.h"
  5. #include "CreateMenu.h"
  6.  
  7. /*
  8.     Conversion From DeskLib by Andrew Campbell
  9.  
  10.     File:    Menu.NewMenu.c
  11.     Author:  Copyright © 1993 Shaun Blackmore and Jason Williams
  12.     Version: 1.00 (30 Apr 1993)
  13.     Purpose: Creates a new menu given a textual description string
  14. */
  15.  
  16.  
  17. /* Description format:
  18.  *      opt   :- " "  no special options (i.e. leading spaces are skipped)
  19.  *               "!"  ticked
  20.  *               "~"  shaded
  21.  *               ">"  has submenu/subwindow
  22.  *               "|"  dotted
  23.  *      name  :- any character except "," and "|"
  24.  *      entry :- {opt}* name
  25.  *      sep   :- ","  to separate normal items
  26.  *               "|"  to separate items and place a dotted line between them
  27.  *      descr :- entry {sep entry}*
  28.  *
  29.  *      EXAMPLE:
  30.  *        ">Info, Create, Quit"
  31.  *        "!Ticked,~Disabled|>Submenu"
  32.  */
  33.  
  34. extern bool Menu__Create(wimp_menu_entry *item, char *description, int numitems);
  35.  
  36. extern bool Menu__Create(wimp_menu_entry *item, char *description, int numitems)
  37. {
  38.   wimp_menu_entry *it;
  39.   char *s, *t;
  40.   int  i, index;
  41.   bool foundtext;
  42.  
  43.   s = description;
  44.   for (i = 0; i < numitems; i++)
  45.   {
  46.     it = &item[i];
  47.     it->menu_flags = 0x0u;
  48.     it->sub_menu = NULL;
  49.     it->icon_flags = wimp_ICON_TEXT | wimp_ICON_FILLED;
  50.     it->icon_flags |= wimp_ICON_FG_COLOUR &
  51.                       (wimp_COLOUR_BLACK << wimp_ICON_FG_COLOUR_SHIFT );
  52.     it->icon_flags |= wimp_ICON_BG_COLOUR &
  53.                       (wimp_COLOUR_WHITE << wimp_ICON_BG_COLOUR_SHIFT );
  54.     foundtext = FALSE;
  55.     do
  56.     {
  57.       switch(*s++)
  58.       {
  59.         case '!':
  60.           it->menu_flags |= wimp_MENU_TICKED;
  61.           break;
  62.  
  63.         case '~':
  64.           it->icon_flags |= wimp_ICON_SHADED;
  65.           break;
  66.  
  67.         case '>':
  68.           /*it->menu_flags |= wimp_MENU_GIVE_WARNING;*/  /* Ask for sublink warnings */
  69.           it->sub_menu = wimp_DEFER_SUB_MENU;        /* This is sublink item '1' */
  70.           break;
  71.  
  72.         case ' ':  /* No option */
  73.           break;
  74.  
  75.         default:   /* Any other == start of menu item text, so don't lose it */
  76.           s--;
  77.           foundtext = TRUE;
  78.           break;
  79.       }
  80.     } while (!foundtext);
  81.  
  82.     t = s;
  83.     for (index = 0; ; index++)
  84.       if (t[index] == 0 || t[index] == '|' || t[index] == ',')
  85.         break;
  86.  
  87.     if (index <= wimp_MENU_NAME_LIMIT)
  88.       t = it->data.text;            /* Copy text directly into menu item */
  89.     else
  90.     {
  91.       /* Copy text into indirected menu item */
  92.       it->icon_flags |= wimp_ICON_INDIRECTED;
  93.       it->data.indirected_text.text = malloc(index + 1);
  94.  
  95.       it->data.indirected_text.size = index + 1;
  96.       it->data.indirected_text.validation = (char *) -1;
  97.  
  98.       t = it->data.indirected_text.text;
  99.     }
  100.  
  101.     while (*s != 0 && *s != ',' && *s != '|')
  102.       *t++ = *s++;
  103.  
  104.     *t = 0;
  105.     if (*s++ == '|')                        /* Step over separator...        */
  106.       it->menu_flags |= wimp_MENU_SEPARATE; /* ...setting 'dotted' if needed */
  107.   }
  108.  
  109.   return(TRUE);
  110. }
  111.  
  112.  
  113. extern wimp_menu *Menu_New(char *title, char *description, int numitems, int maxwidth)
  114. {
  115.   wimp_menu        *menu;
  116.   wimp_menu_entry  *item;
  117.  
  118.   maxwidth+=2;
  119.  
  120.   /*Menu__CountItems(description, &numitems, &maxwidth);*/
  121.  
  122.   if (strlen(title) > maxwidth)    /* Make sure it is wide enough for title  */
  123.     maxwidth = strlen(title);
  124.  
  125.   menu = (wimp_menu*) malloc(sizeof(wimp_menu) + (numitems * sizeof(wimp_menu_entry)));
  126.  
  127.   /*item = (wimp_menu_entry *) &(menu[1]);*/  /* pointer after menu block */
  128.  
  129.   item = menu->entries;
  130.  
  131.   /*  Copy the string. If it's less than 12 characters we need it to be
  132.    *  zero-terminated. We can let it go to 12 chars without any terminator
  133.    *  though, so we don't need to bother terminating after the strncpy.
  134.    */
  135.   strncpy(menu->title_data.text, title, 12);
  136.  
  137.   menu->title_fg = wimp_COLOUR_BLACK ;
  138.   menu->title_bg = wimp_COLOUR_LIGHT_GREY ;
  139.   menu->work_fg  = wimp_COLOUR_BLACK ;
  140.   menu->work_bg  = wimp_COLOUR_WHITE ;
  141.   menu->width     = maxwidth * 16;
  142.   menu->height    = 44;
  143.   menu->gap       = 0;
  144.  
  145.   Menu__Create(item, description, numitems);
  146.   item[numitems - 1].menu_flags |= wimp_MENU_LAST ;
  147.  
  148.   return(menu);
  149. }
  150.