/***************************************************************************
                          DIA_crop.cpp  -  description
                             -------------------

			    GUI for cropping including autocrop
			    +Revisted the Gtk2 way
			     +Autocrop now in RGB space (more accurate)

    begin                : Fri May 3 2002
    copyright            : (C) 2002/2007 by mean
    email                : fixounet@free.fr
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "ADM_toolkitGtk.h"
#include "ADM_image.h"
#include "DIA_flyDialog.h"
#include "DIA_flyCrop.h"
#include "../crop.h"

#if 1
static GtkWidget	*create_dialog1 (void);

/* Link between UI and core */
static gboolean 	ui_draw(GtkWidget*, cairo_t*, gpointer);
static void 		ui_autocrop (void );
static void 		ui_reset( void );
static void 		ui_upload(void);
static void  		ui_read ( void);
static void  		ui_update ( void);
static void 		ui_frame_changed( void );

static GtkWidget *dialog=NULL;
static GtkWidget *drawingarea1;
static GtkWidget *buttonAutocrop;
static GtkWidget *buttonReset;
static GtkWidget *scale;
static GtkWidget *spinbuttonTop;
static GtkWidget *spinbuttonLeft;
static GtkWidget *spinbuttonRight;
static GtkWidget *spinbuttonBottom;

/* Prevent loop when updating value */

static int lock=0;
static flyCrop *myCrop=NULL;
/**
    \fn DIA_getCropParams

*/
int DIA_getCropParams(const char *name, crop *param, ADM_coreVideoFilter *in)
{
	uint32_t width, height;

	// Allocate space for green-ised video
	width = in->getInfo()->width;
	height = in->getInfo()->height;

	uint8_t ret=0;

	dialog=create_dialog1();
	gtk_dialog_set_alternative_button_order(GTK_DIALOG(dialog),
										GTK_RESPONSE_OK,
										GTK_RESPONSE_CANCEL,
										GTK_RESPONSE_APPLY,
										-1);
	gtk_register_dialog(dialog);
	gtk_window_set_title(GTK_WINDOW(dialog), name);
	gtk_widget_show(dialog);
	
#define CONNECT(x,y,z) 	g_signal_connect(x, #y, G_CALLBACK(z), NULL);

	CONNECT(drawingarea1,draw,ui_draw);
	CONNECT(buttonAutocrop,clicked,ui_autocrop);
	CONNECT(buttonReset,clicked,ui_reset);
	CONNECT(scale,value_changed,ui_frame_changed);

#define CONNECT_SPIN(x) CONNECT(spinbutton##x, value_changed,ui_update)
          
	CONNECT_SPIN(Top);
	CONNECT_SPIN(Left);
	CONNECT_SPIN(Right);
	CONNECT_SPIN(Bottom);

	myCrop=new flyCrop(width, height,in,drawingarea1,scale);
	myCrop->left=param->left;
	myCrop->right=param->right;
	myCrop->top=param->top;
	myCrop->bottom=param->bottom;
	myCrop->upload();	
	myCrop->sliderChanged();	

	int response;

	while((response = gtk_dialog_run(GTK_DIALOG(dialog))) == GTK_RESPONSE_APPLY)
	{
	  ui_update();
	}

	if(response==GTK_RESPONSE_OK)
	{
		ui_read( );
		param->left=myCrop->left;
		param->right=myCrop->right;
		param->top=myCrop->top;
		param->bottom=myCrop->bottom;
		ret=1;
	}

	gtk_unregister_dialog(dialog);
	gtk_widget_destroy(dialog);
	delete myCrop;
	return ret;
}

/*
      Link GTK dialog and core
*/
void ui_frame_changed( void )
{
  myCrop->sliderChanged();
}
void ui_update(void)
{
  if(lock) return;
  myCrop->download();
  myCrop->sameImage();
  //myCrop->display();
}
/*---------------------------------------------------------------------------
	Actually draw the working frame on screen
*/
gboolean ui_draw(GtkWidget*, cairo_t*, gpointer)
{
        myCrop->sameImage();
}
void ui_read (void )
{
        myCrop->download();
}
void ui_upload(void)
{
    myCrop->upload();
}
void ui_autocrop( void )
{
  myCrop->autocrop();
 
}
void ui_reset( void )
{
        myCrop->left=0;
        myCrop->right=0;
        myCrop->bottom=0;
        myCrop->top=0;
        
        myCrop->upload();
        myCrop->sameImage();
        //myCrop->display();
}

/*---------------------------------------------------------------------------
	Read entried from dialog box
*/

#define SPIN_GET(x,y) {x= gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spinbutton##y)) ;}
#define SPIN_SET(x,y)  {gtk_spin_button_set_value(GTK_SPIN_BUTTON(spinbutton##y),(gfloat)x) ;}


//____________________________________
uint8_t flyCrop::upload(void)
{
        lock++;
        SPIN_SET(left,Left);
        SPIN_SET(right,Right);
        SPIN_SET(top,Top);
        SPIN_SET(bottom,Bottom);
        lock--;
        return 1;
}
uint8_t flyCrop::download(void)
{
        int reject=0;
        
                        SPIN_GET(left,Left);
                        SPIN_GET(right,Right);
                        SPIN_GET(top,Top);
                        SPIN_GET(bottom,Bottom);
                        
                        printf("%d %d %d %d\n",left,right,top,bottom);
                        
                        left&=0xffffe;
                        right&=0xffffe;
                        top&=0xffffe;
                        bottom&=0xffffe;
                        
                        if((top+bottom)>_h)
                                {
                                        top=bottom=0;
                                        reject=1;
                                }
                        if((left+right)>_w)
                                {
                                        left=right=0;
                                        reject=1;
                                }
                        if(reject)
                                upload();
}
// End common part
//--------------------------------------------
GtkWidget*
create_dialog1 (void)
{
  GtkWidget *dialog1;
  GtkWidget *dialog_vbox1;
  GtkWidget *vbox1;
  GtkWidget *table1;
  GtkWidget *label1;
  GtkWidget *label2;
  GtkWidget *label3;
  GtkWidget *label4;
  GtkAdjustment *spinbuttonRight_adj;
  GtkAdjustment *spinbuttonLeft_adj;
  GtkAdjustment *spinbuttonBottom_adj;
  GtkAdjustment *spinbuttonTop_adj;
  GtkWidget *hbox1;
  GtkWidget *hbox2;
  GtkWidget *dialog_action_area1;
  GtkWidget *applybutton1;
  GtkWidget *cancelbutton1;
  GtkWidget *okbutton1;

  dialog1 = gtk_dialog_new ();
  gtk_window_set_title (GTK_WINDOW (dialog1), QT_TR_NOOP("Crop"));
  gtk_window_set_type_hint (GTK_WINDOW (dialog1), GDK_WINDOW_TYPE_HINT_DIALOG);

  dialog_vbox1 = gtk_dialog_get_content_area (GTK_DIALOG (dialog1));
  gtk_widget_show (dialog_vbox1);

  vbox1 = gtk_vbox_new (FALSE, 6);
  gtk_widget_show (vbox1);
  gtk_box_pack_start (GTK_BOX (dialog_vbox1), vbox1, TRUE, TRUE, 0);
  gtk_container_set_border_width (GTK_CONTAINER (vbox1), 6);

  drawingarea1 = gtk_drawing_area_new ();
  gtk_widget_show (drawingarea1);
  gtk_box_pack_start (GTK_BOX (vbox1), drawingarea1, TRUE, TRUE, 0);

  scale = gtk_hscale_new (gtk_adjustment_new (0, 0, 0, 0, 0, 0));
  gtk_widget_show (scale);
  gtk_box_pack_start (GTK_BOX (vbox1), scale, FALSE, TRUE, 0);

  table1 = gtk_table_new (2, 4, FALSE);
  gtk_widget_show (table1);
  gtk_box_pack_start (GTK_BOX (vbox1), table1, FALSE, FALSE, 10);
  gtk_table_set_row_spacings (GTK_TABLE (table1), 4);
  gtk_table_set_col_spacings (GTK_TABLE (table1), 15);

  label1 = gtk_label_new (QT_TR_NOOP("Crop left:"));
  gtk_widget_show (label1);
  gtk_table_attach (GTK_TABLE (table1), label1, 0, 1, 0, 1,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_misc_set_alignment (GTK_MISC (label1), 0, 0.5);

  label2 = gtk_label_new (QT_TR_NOOP("Crop right:"));
  gtk_widget_show (label2);
  gtk_table_attach (GTK_TABLE (table1), label2, 0, 1, 1, 2,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_misc_set_alignment (GTK_MISC (label2), 0, 0.5);

  label3 = gtk_label_new (QT_TR_NOOP("Crop top:"));
  gtk_widget_show (label3);
  gtk_table_attach (GTK_TABLE (table1), label3, 2, 3, 0, 1,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_misc_set_alignment (GTK_MISC (label3), 0, 0.5);

  label4 = gtk_label_new (QT_TR_NOOP("Crop bottom:"));
  gtk_widget_show (label4);
  gtk_table_attach (GTK_TABLE (table1), label4, 2, 3, 1, 2,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_misc_set_alignment (GTK_MISC (label4), 0, 0.5);

  spinbuttonRight_adj = gtk_adjustment_new (1, 0, 1000, 1, 10, 10);
  spinbuttonRight = gtk_spin_button_new (spinbuttonRight_adj, 1, 0);
  gtk_widget_show (spinbuttonRight);
  gtk_table_attach (GTK_TABLE (table1), spinbuttonRight, 1, 2, 1, 2,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);

  spinbuttonLeft_adj = gtk_adjustment_new (1, 0, 1000, 1, 10, 10);
  spinbuttonLeft = gtk_spin_button_new (spinbuttonLeft_adj, 1, 0);
  gtk_widget_show (spinbuttonLeft);
  gtk_table_attach (GTK_TABLE (table1), spinbuttonLeft, 1, 2, 0, 1,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);

  spinbuttonBottom_adj = gtk_adjustment_new (1, 0, 1000, 1, 10, 10);
  spinbuttonBottom = gtk_spin_button_new (spinbuttonBottom_adj, 1, 0);
  gtk_widget_show (spinbuttonBottom);
  gtk_table_attach (GTK_TABLE (table1), spinbuttonBottom, 3, 4, 1, 2,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);

  spinbuttonTop_adj = gtk_adjustment_new (1, 0, 1000, 1, 10, 10);
  spinbuttonTop = gtk_spin_button_new (spinbuttonTop_adj, 1, 0);
  gtk_widget_show (spinbuttonTop);
  gtk_table_attach (GTK_TABLE (table1), spinbuttonTop, 3, 4, 0, 1,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);

  hbox1 = gtk_hbox_new (FALSE, 0);
  gtk_widget_show (hbox1);
  gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, TRUE, 0);

  hbox2 = gtk_hbox_new (TRUE, 6);
  gtk_widget_show (hbox2);
  gtk_box_pack_start (GTK_BOX (hbox1), hbox2, FALSE, TRUE, 0);

  buttonAutocrop = gtk_button_new_with_mnemonic (QT_TR_NOOP("Auto Crop"));
  gtk_widget_show (buttonAutocrop);
  gtk_box_pack_start (GTK_BOX (hbox2), buttonAutocrop, FALSE, TRUE, 0);

  buttonReset = gtk_button_new_from_stock ("gtk-clear");
  gtk_widget_show (buttonReset);
  gtk_box_pack_start (GTK_BOX (hbox2), buttonReset, FALSE, TRUE, 0);

  dialog_action_area1 = gtk_dialog_get_action_area (GTK_DIALOG (dialog1));
  gtk_widget_show (dialog_action_area1);
  gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog_action_area1), GTK_BUTTONBOX_END);

  applybutton1 = gtk_button_new_from_stock ("gtk-apply");
  gtk_widget_show (applybutton1);
  gtk_dialog_add_action_widget (GTK_DIALOG (dialog1), applybutton1, GTK_RESPONSE_APPLY);

  cancelbutton1 = gtk_button_new_from_stock ("gtk-cancel");
  gtk_widget_show (cancelbutton1);
  gtk_dialog_add_action_widget (GTK_DIALOG (dialog1), cancelbutton1, GTK_RESPONSE_CANCEL);

  okbutton1 = gtk_button_new_from_stock ("gtk-ok");
  gtk_widget_show (okbutton1);
  gtk_dialog_add_action_widget (GTK_DIALOG (dialog1), okbutton1, GTK_RESPONSE_OK);

  return dialog1;
}

#endif
