/*
 * eventBox.c --
 *
 * This file implements the eventBox widget
 *
 * Copyright (c) 2002 P.G. Baum
 *
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 */

/*
   History:
   2002-01: Begin of developement
 */
/*
   TODO? handle invisible for user? Or only for gnocl::box?
*/

#include "gnocl.h"
#include <string.h>
#include <assert.h>

typedef struct
{
   Gnocl_StringBool child;
} EventBoxOptions;

static const GnoclWidgetOptions widgetOptions[] = 
{
   { "-child", GNOCL_STRING, GNOCL_OFFSET( EventBoxOptions, child ) },
   { NULL, 0, 0 }
};

/*
   This struct contains only the necessary states to remember.
   Option values can be inquired via gtk
*/
typedef struct
{
   GtkEventBox  *eventBox;
   char       *name;
   Tcl_Interp *interp;
} EventBoxParams;

static void freeOpts( EventBoxOptions *p )
{
   GNOCL_FREE_MEM_OPT( p->child );
}

static void eventBoxDestroyFunc( GtkWidget *widget, gpointer data )
{
   EventBoxParams *p = (EventBoxParams *)data;

   gnoclForgetWidgetFromName( p->name );
   Tcl_DeleteCommand( p->interp, p->name );

   g_free( p->name );
   g_free( p );
}



/*
   test, if params are valid
   set params and show eventBox with new settings
*/
static int eventBoxConfigure( Tcl_Interp *interp, EventBoxParams *para,
      EventBoxOptions *popt )
{
   /*
      FIXME: delete old child
      if( gtk_container_children( GTK_CONTAINER( parent ) ) != NULL )
   */
   if( popt->child.changed )
   {
      GtkWidget *childWidget = gnoclGetWidgetFromName( popt->child.val, 
            interp );
      if( childWidget == NULL )
         return TCL_ERROR;

      gtk_container_add( GTK_CONTAINER( para->eventBox ), childWidget );
   }

   /* gtk_widget_show_all( GTK_WIDGET( para->eventBox ) ); */

   return TCL_OK;
}

static int eventBoxFunc( ClientData data, Tcl_Interp *interp,
      int objc, Tcl_Obj * const objv[] )
{

   static char *cmds[] = { GNOCL_STD_CMD_NAMES,
         "configure", NULL };
   enum cmdIdx { GNOCL_STD_CMD_ENUMS, ConfigureIdx };
   EventBoxParams *para = (EventBoxParams *)data;
   GtkWidget *widget = GTK_WIDGET( para->eventBox );
   int idx;

   if( Tcl_GetIndexFromObj( interp, objv[1], cmds, "command", 
         TCL_EXACT, &idx ) != TCL_OK )
      return TCL_ERROR;

   switch( idx )
   {
      GNCOL_STD_CMD_CASE( interp, objc, objv, widget );
            break;
      case ConfigureIdx:
            {
               EventBoxOptions opt;
               int ret = TCL_ERROR;

               memset( &opt, 0, sizeof( opt ) );
               if( gnoclParseAllWidgetOpts( interp, objc, objv, 1,
                     widgetOptions, (char *)&opt ) == TCL_OK )
                  ret = eventBoxConfigure( interp, para, &opt );

               freeOpts( &opt );
               if( ret != TCL_OK )
                  return TCL_ERROR;
               /* gtk_widget_show_all( widget ); */
            }
            break;
   }

   return TCL_OK;
}


int gnoclEventBoxCmd( ClientData data, Tcl_Interp *interp,
      int objc, Tcl_Obj * const objv[] )
{
   EventBoxOptions opt;
   EventBoxParams *para;
   int ret;
  
   memset( &opt, 0, sizeof( opt ) );
   if( gnoclParseAllWidgetOpts( interp, objc, objv, 0,
         widgetOptions, (char *)&opt ) != TCL_OK )
      return TCL_ERROR;

   para = g_new( EventBoxParams, 1 );
   para->name = gnoclGetAutoWidgetId();
   para->interp = interp;

   para->eventBox = GTK_EVENT_BOX( gtk_event_box_new( ) );
   ret = eventBoxConfigure( interp, para, &opt );
   freeOpts( &opt );
   if( ret != TCL_OK )
   {
      /* FIXME: g_free pp->name ... */
      gtk_widget_destroy( GTK_WIDGET( para->eventBox ) );
      return TCL_ERROR;
   }

   gtk_signal_connect( GTK_OBJECT( para->eventBox ), "destroy", 
      GTK_SIGNAL_FUNC( eventBoxDestroyFunc ), para );

   gnoclMemNameAndWidget( para->name, GTK_WIDGET( para->eventBox ) );
   gtk_widget_show( GTK_WIDGET( para->eventBox ) );

   Tcl_CreateObjCommand( interp, para->name, eventBoxFunc, para, NULL );
   Tcl_SetObjResult( interp, Tcl_NewStringObj( para->name, -1 ) );

   return TCL_OK;
}

