/*
    ####             #    #     # #
    #   #            #    #       #          The FreeWare C library for 
    #   #  ##   ###  #  # #     # ###             RISC OS machines
    #   # #  # #     # #  #     # #  #   ___________________________________
    #   # ####  ###  ##   #     # #  #                                      
    #   # #        # # #  #     # #  #    Please refer to the accompanying
    ####   ### ####  #  # ##### # ###    documentation for conditions of use
    ________________________________________________________________________

    File:    Menu.AddSubMenu.c
    Author:  Copyright  1993 Shaun Blackmore and Jason Williams
    Version: 1.00 (30 Apr 1993)
    Purpose: Adds one menu to another as a submenu
*/

#include "Desk.Wimp.h"
#include "Desk.WimpSWIs.h"
#include "Desk.Menu.h"


Desk_wimp_point Desk_menu_currentpos;
Desk_menu_ptr   Desk_menu_currentopen = NULL;


#ifdef _DLL
Desk_wimp_point	*Desk_Menu__Ref_currentpos( void)	{ return &Desk_menu_currentpos;	}
Desk_menu_ptr	*Desk_Menu__Ref_currentopen( void)	{ return &Desk_menu_currentopen;	}
#endif


int Desk_Menu_CalcHeight(Desk_menu_ptr menu)
{
  int       itemheight, count = 0;
  Desk_menu_item *item;

  item = (Desk_menu_item *) (((int) menu) + sizeof(Desk_menu_block));
  itemheight = menu->height + menu->gap;

  while(Desk_bool_TRUE)
  {
    if (item->menuflags.data.dotted)         /* Count height of dotted line  */
      count += 24;

    count += itemheight;                     /* Plus the height of each item */

    if (item->menuflags.data.last)
      break;

    item++;
  }

  return(count);
}


void Desk_Menu_Show(Desk_menu_ptr menu, int x, int y)
{                    
  x -= 64;
  if (y < 0)
    y = 96 + Desk_Menu_CalcHeight(menu);            /* Calculate iconbar position */

  Desk_menu_currentopen  = menu;
  Desk_menu_currentpos.x = x;
  Desk_menu_currentpos.y = y;

  Desk_Wimp_eCreateMenu(menu, x, y);
}
 

void Desk_Menu_ShowLast(void)
{
  Desk_Wimp_eCreateMenu(Desk_menu_currentopen, Desk_menu_currentpos.x, Desk_menu_currentpos.y);
}
