
/*
 * gnoclColorPicker.c --
 *
 * This file implements the colorPicker widget
 *
 * Copyright (c) 2001 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:
   2001-07: Begin of developement
 */

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

typedef struct
{
   GNOCL_STD_OPTIONS_VARS;

   Gnocl_ObjBool    text;
   Gnocl_ObjBool    title;
   Gnocl_StringBool command;
   Gnocl_StringBool variable;
} ColorPickerOptions;

static const GnoclWidgetOptions widgetOptions[] =
{
   GNOCL_STD_OPTIONS_NAMES( ColorPickerOptions ),
   { "-text", GNOCL_OBJ, GNOCL_OFFSET( ColorPickerOptions, text ) },
   { "-title", GNOCL_OBJ, GNOCL_OFFSET( ColorPickerOptions, title ) },
   { "-command", GNOCL_STRING, GNOCL_OFFSET( ColorPickerOptions, command ) },
   { "-variable", GNOCL_STRING, GNOCL_OFFSET( ColorPickerOptions, variable ) },
   { NULL, 0, 0 }
};

/*
   This struct contains only the necessary states to remember.
   Option values can be inquired via gtk
*/
typedef struct
{
   char             *name;
   Tcl_Interp       *interp;
   GnomeColorPicker *colorPicker;
   char             *command;
   char             *variable;
   int              inSignal;
} ColorPickerParams;

static void freeOpts( ColorPickerOptions *p )
{
   GNOCL_STD_OPTIONS_FREE( p );
   GNOCL_FREE_MEM_OPT( p->command );
}

static Tcl_Obj *rgbaToObj( Tcl_Interp *interp, int r, int g, int b, int a )
{
   Tcl_Obj *obj = Tcl_NewListObj( 0, NULL );

   Tcl_ListObjAppendElement( interp, obj, Tcl_NewIntObj( r ) );
   Tcl_ListObjAppendElement( interp, obj, Tcl_NewIntObj( g ) );
   Tcl_ListObjAppendElement( interp, obj, Tcl_NewIntObj( b ) );
   Tcl_ListObjAppendElement( interp, obj, Tcl_NewIntObj( a ) );

   return obj;
}

static void colorSetFunc( GtkWidget *widget, 
      guint r, guint g, guint b, guint a, /* these are 16 bit values */
      gpointer data )
{
   ColorPickerParams *para = (ColorPickerParams *)data;

   para->inSignal = 1;
   #if 0
      /* we realy want only 8 Bit -- realy? */
      guint r, guint g, guint b, guint a;
      gnome_color_picker_get_i8( &r, &g, &b, &a );
   #endif
   if( para->variable )
   {
      Tcl_SetVar2Ex( para->interp, para->variable, NULL, 
            rgbaToObj( para->interp, r, g, b, a ), 0 );
   }
   if( para->command )
   {
      static Gnocl_PercSubst ps[] = {
         { 'w', GNOCL_STRING },     /* widget */
         { 'r', GNOCL_INT },        /* red */
         { 'g', GNOCL_INT },        /* green */
         { 'b', GNOCL_INT },        /* blue */
         { 'a', GNOCL_INT },        /* alpha */
         { 0 }
      };

      ps[0].val.str = para->name;
      ps[1].val.i = r;
      ps[2].val.i = g;
      ps[3].val.i = b;
      ps[4].val.i = a;

      gnoclPercentSubstAndEval( para->interp, ps, para->command );
   }
   para->inSignal = 0;
}

static char *traceFunc( ClientData data,
      Tcl_Interp *interp, char *name1, char *name2, int flags)
{
   ColorPickerParams *para = (ColorPickerParams *)data;

printf( "DEBUG: in colorPicker trace inSignal %d\n", para->inSignal );
   if( para->inSignal == 0 )
   {
      Tcl_Obj *obj = Tcl_GetVar2Ex( interp, name1, name2, flags ); 
      if( obj == NULL ) /* var does not yet exist */
      {
         gushort r, g, b, a;
         gnome_color_picker_get_i16( para->colorPicker, &r, &g, &b, &a );
         Tcl_SetVar2Ex( para->interp, para->variable, NULL, 
               rgbaToObj( para->interp, r, g, b, a ), 0 );
      }
      else
      {
         int r, g, b, a;
         if( gnoclGetRGBA( interp, obj, &r, &g, &b, &a ) != TCL_OK )
            return NULL;
   printf( "DEBUG: in colorPicker trace rgba: %d %d %d %d\n", r, g, b, a );

         gnome_color_picker_set_i16( para->colorPicker, r, g, b, a );
      }
   }

   return NULL;
}

static void colorPickerDestroyFunc( GtkWidget *widget, gpointer data )
{
   ColorPickerParams *para = (ColorPickerParams *)data;

   /* untrace and free command and variable */
   gnoclAttacheCmdAndVar( 
         1, &para->command, NULL,
         1, &para->variable, NULL,
         "color_set", GTK_OBJECT( para->colorPicker ), colorSetFunc,
         para->interp, traceFunc, para );

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

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


static int colorPickerConfigure( Tcl_Interp *interp, ColorPickerParams *para, 
      ColorPickerOptions *popt )
{
   GNOCL_STD_OPTIONS_HANDLE( popt, GTK_WIDGET( para->colorPicker ) );

   gnoclAttacheCmdAndVar( 
         popt->command.changed, &para->command, &popt->command.val,
         popt->variable.changed, &para->variable, &popt->variable.val,
         "color_set", GTK_OBJECT( para->colorPicker ), colorSetFunc,
         interp, traceFunc, para );

   if( popt->title.changed )
   {
      gnome_color_picker_set_title( para->colorPicker, 
         gnoclGetString( popt->title.val ) );
   }

   return TCL_OK;
}

static int colorPickerFunc( ClientData data, Tcl_Interp *interp,
      int objc, Tcl_Obj * const objv[] )
{
   static char *cmds[] = { GNOCL_STD_CMD_NAMES,
         "configure", 
         "getValue", "getColor",
         "setValue", "setColor",
         NULL };
   enum cmdIdx { GNOCL_STD_CMD_ENUMS, 
         ConfigureIdx, 
         GetValueIdx, GetColorIdx, 
         SetValueIdx, SetColorIdx };
   ColorPickerParams *para = (ColorPickerParams *)data;
   GtkWidget *widget = GTK_WIDGET( para->colorPicker );
   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:
            {
               ColorPickerOptions opt;
               int ret = TCL_ERROR;
               memset( &opt, 0, sizeof( opt ) );
               if( gnoclParseAllWidgetOpts( interp, objc, objv, 1,
                     widgetOptions, (char *)&opt ) == TCL_OK )
                  ret = colorPickerConfigure( interp, para, &opt );

               freeOpts( &opt );
               if( ret != TCL_OK )
                  return TCL_ERROR;
            }
            break;
      case GetColorIdx:
      case GetValueIdx:
            {
               gushort r, g, b, a;
               if( objc != 2 )
               {
                  Tcl_WrongNumArgs( interp, 2, objv, "" );
                  return TCL_ERROR;
               }
               gnome_color_picker_get_i16( para->colorPicker, &r, &g, &b, &a );
               Tcl_SetObjResult( interp, 
                     rgbaToObj( para->interp, r, g, b, a ) );
            }
            break;

      case SetColorIdx:
      case SetValueIdx:
            {
               int r, g, b, a;
               if( objc != 3 )
               {
                  Tcl_WrongNumArgs( interp, 2, objv, "value" );
                  return TCL_ERROR;
               }

               if( gnoclGetRGBA( interp, objv[2], &r, &g, &b, &a ) != TCL_OK )
                  return TCL_ERROR;

               gnome_color_picker_set_i16( para->colorPicker, r, g, b, a );
               /* TODO: howto prevent recursion? */
               colorSetFunc( GTK_WIDGET( para->colorPicker ),
                     r, g, b, a, para );
            }
            break;
   }

   return TCL_OK;
}

int gnoclColorPickerCmd( ClientData data, Tcl_Interp *interp,
      int objc, Tcl_Obj * const objv[] )
{
   ColorPickerOptions opt;
   ColorPickerParams *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( ColorPickerParams, 1 );
   para->interp = interp;
   para->command = NULL;
   para->variable = NULL;
   para->name = gnoclGetAutoWidgetId();
   para->inSignal = 0;

   para->colorPicker = GNOME_COLOR_PICKER( gnome_color_picker_new( ) );
   ret = colorPickerConfigure( interp, para, &opt );
   freeOpts( &opt );
   if( ret != TCL_OK )
   {
      /* FIXME: g_free pp->name ... */
      gtk_widget_destroy( GTK_WIDGET( para->colorPicker ) );
      return TCL_ERROR;
   }
   /*
   gtk_signal_connect( GTK_OBJECT( para->colorPicker ), "clicked", 
         GTK_SIGNAL_FUNC( commandFunc ), para );
   */
   gtk_signal_connect( GTK_OBJECT( para->colorPicker ), "color_set", 
         GTK_SIGNAL_FUNC( colorSetFunc ), para );
   gtk_signal_connect( GTK_OBJECT( para->colorPicker ), "destroy", 
         GTK_SIGNAL_FUNC( colorPickerDestroyFunc ), para );

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

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

   return TCL_OK;
}

