#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>
#include <dialog/user.h>
#include <dialog/menu.h>

#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/dlg.h>

extern struct USER_DATA StUser;

UBYTE menu(struct ShortMenu *menu, char size, char level)

{char  *buffer      = NULL;
 char  *breakbuffer = NULL;
 char  *index;

 long   counter;
 long   pos;
 long   breakindex  = 0;
 USHORT npos        = 0;
 UBYTE  retval      = 0;

 if (level==2)  return(0);

 counter = StUser.Screen_Width / 20;
 if (counter < 1)  counter = 1;
 if (counter > 4)  counter = 4;

 buffer      = malloc(size * 31 + 23);
 breakbuffer = buffer + (size * 30) + 22;
 index       = buffer;

 index += ASPrintf(&StUser, index, "%ao%a2");
 if (level == 1)  index += ASPrintf(&StUser, index, "(%a7");

 for(pos = counter; size; size--, menu++)
    {if (menu->status || level == 3)
        {if (level == 0)
            {if (menu->command)
                  index += ASPrintf(&StUser, index, "[%a7%c%a2] %-14.14s",  menu->command, menu->description);
                else
                  index += ASPrintf(&StUser, index, "[%a7RET%a2] %-12.12s", menu->description);

             breakbuffer[breakindex] = menu->command;
             breakindex++;   
            }
          else
            {if (menu->command)
                {*index = menu->command;
                 index++;
                }
              else
               {strcpy(index, "RET");
                index += 3;
               }

             if (size > 1)
                {*index = ',';
                 index++;
                }
            }

         if (level != 1)
            {pos--;

             if (!pos  ||  size == 1)
                {*index = '\n';
                 index++;
                 pos = counter;
                 continue;
                }

             *index++ = ' ';
             *index++ = ' ';
            }
        }
    }

 if (level == 1)  index += ASPrintf(&StUser,index,"%a2)%a7");

*index                   = 0;
 breakbuffer[breakindex] = 0;
 retval = DisplayBuffer(Output(),buffer,StUser.Screen_Width,StUser.Screen_Len,StUser.More_Flag,StUser.Ansi_Flag,&npos,0,NULL,0,breakbuffer);

 free(buffer);
 return(retval);
}
