/*
 * scrolledWindow.c --
 *
 * This file implements the scrolledWindow 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-02: Begin of developement
 */

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

typedef struct
{
   GNOCL_STD_OPTIONS_VARS;

   Gnocl_StringBool child;
   Gnocl_ObjBool    scrollbar;
} ScrolledWindowOptions;

static const GnoclWidgetOptions widgetOptions[] = 
{
   GNOCL_STD_OPTIONS_NAMES( ScrolledWindowOptions ),

   { "-child", GNOCL_STRING, GNOCL_OFFSET( ScrolledWindowOptions, child ) },
   { "-scrollbar", GNOCL_OBJ, GNOCL_OFFSET( ScrolledWindowOptions, child ) },
   { NULL, 0, 0 }
};

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

static void freeOpts( ScrolledWindowOptions *p )
{
   GNOCL_STD_OPTIONS_FREE( p );

   GNOCL_FREE_MEM_OPT( p->child );
   GNOCL_FREE_TCL_OPT( p->scrollbar );
}

static void destroyFunc( GtkWidget *widget, gpointer data )
{
   WindowParams *p = (WindowParams *)data;

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

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

static int scrolledWindowConfigure( Tcl_Interp *interp, WindowParams *para,
      ScrolledWindowOptions *popt )
{
   GNOCL_STD_OPTIONS_HANDLE( popt, GTK_WIDGET( para->scrolledWindow ) );


   if( popt->child.changed )
   {
      GtkWidget *childWidget = gnoclChildNotPacked( popt->child.val, 
            interp );
      if( childWidget == NULL )
         return TCL_ERROR;

      /* differentiate between widgets with and without 
         native scrolling capabilities */
      /* TODO: how to make this nicer / more portable? */
      if( GTK_WIDGET_CLASS( GTK_OBJECT( childWidget )->klass)
            ->set_scroll_adjustments_signal )
         gtk_container_add( GTK_CONTAINER( para->scrolledWindow ), 
               childWidget );
      else
         gtk_scrolled_window_add_with_viewport( para->scrolledWindow, 
               childWidget );
   }

   if( popt->scrollbar.changed )
   {
      GtkPolicyType hor, vert;
      if( gnoclGetScrollbarPolicy( interp, popt->scrollbar.val, 
            &hor, &vert ) != TCL_OK )
         return TCL_ERROR;

      gtk_scrolled_window_set_policy( para->scrolledWindow, hor, vert );
   }



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

   return TCL_OK;
}

static int scrolledWindowFunc( 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 };
   WindowParams *para = (WindowParams *)data;
   GtkWidget *widget = GTK_WIDGET( para->scrolledWindow );
   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:
            {
               ScrolledWindowOptions opt;
               int ret = TCL_ERROR;

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

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

   return TCL_OK;
}


int gnoclScrolledWindowCmd( ClientData data, Tcl_Interp *interp,
      int objc, Tcl_Obj * const objv[] )
{
   ScrolledWindowOptions opt;
   WindowParams *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( WindowParams, 1 );
   para->name = gnoclGetAutoWidgetId();
   para->interp = interp;
   para->scrolledWindow = GTK_SCROLLED_WINDOW( 
         gtk_scrolled_window_new( NULL, NULL ) );
   gtk_scrolled_window_set_policy( para->scrolledWindow, 
         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );

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

   gtk_signal_connect( GTK_OBJECT( para->scrolledWindow ), "destroy", 
      GTK_SIGNAL_FUNC( destroyFunc ), para );

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

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

   return TCL_OK;
}

