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

    File:    Menu.ReviseWdth.c
    Author:  Copyright  1994 Lenny
    Version: 0.02 (20 Nov 1994)
    Purpose: Revises a menu's width, according to the longest menu item.
    History: 0.01 (20 Nov 94) : Added 'Desk_Menu_ReviseWidth()'
    Mods:    22 Jul 1995 - JPS - Set maxwidth to 0 initially

*/


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

#include <string.h>


extern void Desk_Menu_ReviseWidth(Desk_menu_ptr menu)
/*
 *  Revises a menu's width, by scanning through all the items.
 *  The title width is also taken into consideration.
 *  It is assumed that there is AT LEAST ONE item in the menu.
 *
 *  Use it after using Desk_Menu_SetText(), or when updating the
 *  contents of an indirected text menu item, ie when having
 *  used Desk_Menu_MakeIndirected().
 */
{
  int                   width, maxwidth=0;
  Desk_bool                  last = Desk_bool_FALSE;
  Desk_menu_item   *item = (Desk_menu_item *) (((int) menu) + sizeof(Desk_menu_block));

  do {
    if (item->iconflags.data.indirected) {
      width = strlen(item->icondata.indirecttext.buffer);
    } 
    else {
      width = strlen(item->icondata.text);
      if (width > 12)  width = 12;  /* It's a full length non-terminated entry */
    }
    if (width > maxwidth)  maxwidth = width;
    if (item->menuflags.data.last)  last = Desk_bool_TRUE;
    item++;
  } while (last == Desk_bool_FALSE);
  maxwidth += 1;

  width = strlen(menu->title);
  if (width > 12)  width = 12;  /* It's a full length non-terminated title */
  if (width > maxwidth)  maxwidth = width;

  menu->width = maxwidth * 16;

}

