/*****************************************************************************/

/*
 *      keypadcb.c -- keypad GTK+ callbacks
 *
 *      Copyright (C) 2001 Sigma Designs
 *                    written by Pascal Cannenterre <pascal_cannenterre@sdesigns.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*****************************************************************************/
#include "define.h"
#include "gtk_callbacks.h"

KeypadSelection selectkey;

void initialize_keypad_data_struct(KeypadSelection *select)
{
  select->hits = 0; 
  select->hits_list = NULL;
  select->value = 0;
}

void init_keypad()
{
  initialize_keypad_data_struct(&selectkey);
}

gboolean keypad_digit_clicked_cb(GtkWidget *widget, gpointer data)
{
  gchar *msg = NULL;
  
  if(selectkey.hits < KEYPAD_NUMBER_OF_DIGITS)
    {
      selectkey.hits = selectkey.hits + 1;
      selectkey.hits_list = g_slist_prepend(selectkey.hits_list, data);
      // G_MESSAGE("Click before : %d", selectkey.value);
      selectkey.value = (10 * selectkey.value) + GPOINTER_TO_UINT(data);
      // G_MESSAGE("Click after : %d", selectkey.value);
      msg = g_strdup_printf("%02d",selectkey.value);
      gtk_statusbar_push( GTK_STATUSBAR(GUI.keypad_statusbar), GUI.keypad_context_id, 
			  msg );
      g_free(msg);
    }
  
  return TRUE;
}

gboolean keypad_del_clicked_cb(GtkWidget *widget, gpointer data)
{
  GSList * tmp;

  if(selectkey.hits_list != NULL)
    {
      tmp = selectkey.hits_list;
      selectkey.hits_list = g_slist_remove_link(selectkey.hits_list, selectkey.hits_list);
      selectkey.hits = selectkey.hits - 1;
      // G_MESSAGE("Del before : %d", selectkey.value);
      selectkey.value = ( selectkey.value - GPOINTER_TO_UINT(tmp->data) ) / 10;
      // G_MESSAGE("Del after : %d", selectkey.value);
      g_slist_free_1(tmp);
      gtk_statusbar_pop(GTK_STATUSBAR(GUI.keypad_statusbar), GUI.keypad_context_id );
    }
  else
    initialize_keypad_data_struct(&selectkey);

  return TRUE;
}

gboolean keypad_ent_clicked_cb(GtkWidget *widget, gpointer data)
{
  guint i;
  
  if(selectkey.hits_list != NULL)
    {
      G_PRINT("\n\n Value entered in keypad = %d \n\n", selectkey.value);
      handle_Direct_Play_disc(&Info, (WORD)selectkey.value);

      g_slist_free(selectkey.hits_list);
      initialize_keypad_data_struct(&selectkey);
      for(i=0;i<KEYPAD_NUMBER_OF_DIGITS;i++)
	gtk_statusbar_pop(GTK_STATUSBAR(GUI.keypad_statusbar), GUI.keypad_context_id );
    }
  
  return TRUE;
}

