/*
 *  menu_riscos.c -- menu creation, RISC OS
 */

#include "gui.h"


void gui_menu_activate(GtkMenuItem *menu_item, gpointer data)
{
  struct gui_menu_data *menu = data;
  unsigned int item, i, j;
  signed int selection[17];

  for (item = 0; menu->item[item] != menu_item; item++)
    ;

  printf("gui_menu_activate: menu 0x%x, item %i, selection ", (unsigned int) data, item);

  selection[16] = -1;
  selection[15] = item;
  for (i = 14; menu->parent != NULL; menu = menu->parent, i--)
    selection[i] = menu->i;

  for (j = i + 1; j < 16; j++)
    printf("%i ", selection[j]);
  printf("\n");

  gui_window_list[menu_window_id].menu_fn(menu_window_id, &selection[i + 1]);
}


struct gui_menu_data *gui_menu(unsigned int base, unsigned int items)
{
  char *text;
  unsigned int line, i;
  struct gui_menu_data *menu;

  menu = malloc(sizeof(struct gui_menu_data));
  if (menu == NULL) gui_raise_error("out of memory");

  menu->item = malloc(sizeof(GtkWidget *) * items);
  if (menu->item == NULL) gui_raise_error("out of memory");

  menu->menu = gtk_menu_new();
  menu->size = items;
  menu->parent = NULL;

  for (i = 0; i < items; i++)
  {
    text = gui_message[base + 1 + i];

    line = (text[0] == '-'); if (line) text++;

    menu->item[i] = gtk_menu_item_new_with_label(text);
    gtk_menu_append(menu->menu, menu->item[i]);
    gtk_widget_show(menu->item[i]);
    gtk_signal_connect(menu->item[i], "activate", gui_menu_activate, menu);

    if (line)
    {
      GtkWidget *sep = gtk_menu_item_new();
      gtk_menu_append(menu->menu, sep);
      gtk_widget_show(sep);
    }
  }

  return menu;
}


void gui_menu_attach(struct gui_menu_data *menu, unsigned int item,
                     struct gui_menu_data *submenu)
{
  gtk_menu_item_set_submenu(menu->item[item], submenu->menu);
  submenu->parent = menu;
  submenu->i = item;
}
