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

/*
 *      adjustborder.c -- REALmagic calibration application 
 *
 *      Copyright (C) 2000 Sigma Designs
 *                    written by Nicolas Vignal <nicolas_vignal@sdesigns.com>
 *                    modified by Emmanuel Michon <emmanuel_michon@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 "adjustborder.h"
#include "adjustborderCB.h"

/* include pixmaps*/
#include"./pixmaps/down.xpm"
#include"./pixmaps/enlarge.xpm"
#include"./pixmaps/left.xpm"
#include"./pixmaps/pinkscreen.xpm"
#include"./pixmaps/reduce.xpm"
#include"./pixmaps/right.xpm"
#include"./pixmaps/up.xpm"
#include"./pixmaps/REALmagic.xpm"

GtkWidget*
create_BorderAdjust (void)
{
  GtkWidget *BorderAdjust;
  GtkWidget *vbox3;
  GtkWidget *hbox2;
  GtkWidget *pinkscreen;
  GtkWidget *vbox4;
  GtkWidget *vbox5;
  GtkWidget *vbuttonbox2;
  guint Ok_key;
  GtkWidget *Ok;
  guint Cancel_key;
  GtkWidget *Cancel;
  GtkWidget *vbuttonbox3;
  guint Default_key;
  GtkWidget *Default;
  GtkWidget *PositionSize;
  GtkWidget *position;
  GtkWidget *left;
  GtkWidget *pixmap13;
  GtkWidget *right;
  GtkWidget *pixmap14;
  GtkWidget *wideup;
  GtkWidget *pixmap15;
  GtkWidget *widedown;
  GtkWidget *pixmap16;
  GtkWidget *down;
  GtkWidget *pixmap12;
  GtkWidget *up;
  GtkWidget *pixmap11;
  GtkWidget *hbox3;
  GtkWidget *hbox4;
  guint StabilityLabel_key;
  GtkWidget *StabilityLabel;
  GtkObject *Stability_adj;
  GtkWidget *Stability;
  GtkWidget *vbox12;
  GtkWidget *label5;
  GtkWidget *label6;
  GtkAccelGroup *accel_group;

  accel_group = gtk_accel_group_new ();

  BorderAdjust = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_object_set_data (GTK_OBJECT (BorderAdjust), "BorderAdjust", BorderAdjust);
  gtk_widget_set_usize (BorderAdjust, 520, 365);
  gtk_container_set_border_width (GTK_CONTAINER (BorderAdjust), 10);
  gtk_window_set_title (GTK_WINDOW (BorderAdjust), _("Border adjust..."));
  gtk_window_set_position (GTK_WINDOW (BorderAdjust), GTK_WIN_POS_CENTER);
  gtk_window_set_policy (GTK_WINDOW (BorderAdjust), FALSE, FALSE, FALSE);

  vbox3 = gtk_vbox_new (FALSE, 0);
  gtk_widget_ref (vbox3);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "vbox3", vbox3,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (vbox3);
  gtk_container_add (GTK_CONTAINER (BorderAdjust), vbox3);

  hbox2 = gtk_hbox_new (FALSE, 0);
  gtk_widget_ref (hbox2);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "hbox2", hbox2,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (hbox2);
  gtk_box_pack_start (GTK_BOX (vbox3), hbox2, TRUE, TRUE, 0);

  pinkscreen = create_pixmap (BorderAdjust, pinkscreen_xpm);
  gtk_widget_ref (pinkscreen);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "pinkscreen", pinkscreen,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (pinkscreen);
  gtk_box_pack_start (GTK_BOX (hbox2), pinkscreen, TRUE, TRUE, 0);

  vbox4 = gtk_vbox_new (FALSE, 0);
  gtk_widget_ref (vbox4);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "vbox4", vbox4,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (vbox4);
  gtk_box_pack_start (GTK_BOX (hbox2), vbox4, TRUE, TRUE, 0);

  vbox5 = gtk_vbox_new (FALSE, 0);
  gtk_widget_ref (vbox5);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "vbox5", vbox5,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (vbox5);
  gtk_box_pack_start (GTK_BOX (vbox4), vbox5, TRUE, TRUE, 0);

  vbuttonbox2 = gtk_vbutton_box_new ();
  gtk_widget_ref (vbuttonbox2);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "vbuttonbox2", vbuttonbox2,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (vbuttonbox2);
  gtk_box_pack_start (GTK_BOX (vbox5), vbuttonbox2, TRUE, TRUE, 0);
  gtk_button_box_set_layout (GTK_BUTTON_BOX (vbuttonbox2), GTK_BUTTONBOX_START);

  Ok = gtk_button_new_with_label ("");
  Ok_key = gtk_label_parse_uline (GTK_LABEL (GTK_BIN (Ok)->child),
                                   _("_OK"));
  gtk_widget_add_accelerator (Ok, "clicked", accel_group,
                              Ok_key, GDK_MOD1_MASK, 0);
  gtk_widget_ref (Ok);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "Ok", Ok,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (Ok);
  gtk_container_add (GTK_CONTAINER (vbuttonbox2), Ok);
  gtk_widget_add_accelerator (Ok, "clicked", accel_group,
                              GDK_O, GDK_MOD1_MASK,
                              GTK_ACCEL_VISIBLE);

  Cancel = gtk_button_new_with_label ("");
  Cancel_key = gtk_label_parse_uline (GTK_LABEL (GTK_BIN (Cancel)->child),
                                   _("_Cancel"));
  gtk_widget_add_accelerator (Cancel, "clicked", accel_group,
                              Cancel_key, GDK_MOD1_MASK, 0);
  gtk_widget_ref (Cancel);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "Cancel", Cancel,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (Cancel);
  gtk_container_add (GTK_CONTAINER (vbuttonbox2), Cancel);
  gtk_widget_add_accelerator (Cancel, "clicked", accel_group,
                              GDK_C, GDK_MOD1_MASK,
                              GTK_ACCEL_VISIBLE);

  vbuttonbox3 = gtk_vbutton_box_new ();
  gtk_widget_ref (vbuttonbox3);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "vbuttonbox3", vbuttonbox3,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (vbuttonbox3);
  gtk_box_pack_start (GTK_BOX (vbox5), vbuttonbox3, TRUE, TRUE, 0);
  gtk_button_box_set_layout (GTK_BUTTON_BOX (vbuttonbox3), GTK_BUTTONBOX_END);

  Default = gtk_button_new_with_label ("");
  Default_key = gtk_label_parse_uline (GTK_LABEL (GTK_BIN (Default)->child),
                                   _("_Default"));
  gtk_widget_add_accelerator (Default, "clicked", accel_group,
                              Default_key, GDK_MOD1_MASK, 0);
  gtk_widget_ref (Default);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "Default", Default,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (Default);
  gtk_container_add (GTK_CONTAINER (vbuttonbox3), Default);
  gtk_widget_add_accelerator (Default, "clicked", accel_group,
                              GDK_D, GDK_MOD1_MASK,
                              GTK_ACCEL_VISIBLE);

  PositionSize = gtk_frame_new (_("Position / Size"));
  gtk_widget_ref (PositionSize);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "PositionSize", PositionSize,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (PositionSize);
  gtk_box_pack_start (GTK_BOX (vbox4), PositionSize, TRUE, FALSE, 0);
  gtk_frame_set_label_align (GTK_FRAME (PositionSize), 0.49, 0.5);

  position = gtk_table_new (3, 3, TRUE);
  gtk_widget_ref (position);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "position", position,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (position);
  gtk_container_add (GTK_CONTAINER (PositionSize), position);
  gtk_container_set_border_width (GTK_CONTAINER (position), 5);
  GTK_WIDGET_SET_FLAGS (position, GTK_CAN_FOCUS);
  GTK_WIDGET_SET_FLAGS (position, GTK_CAN_DEFAULT);
  gtk_table_set_row_spacings (GTK_TABLE (position), 6);
  gtk_table_set_col_spacings (GTK_TABLE (position), 4);

  left = gtk_button_new ();
  gtk_widget_ref (left);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "left", left,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (left);
  gtk_table_attach (GTK_TABLE (position), left, 0, 1, 1, 2,
                    (GtkAttachOptions) (0),
                    (GtkAttachOptions) (0), 0, 0);
  GTK_WIDGET_UNSET_FLAGS (left, GTK_CAN_FOCUS);
  gtk_widget_add_accelerator (left, "pressed", accel_group,
                              GDK_KP_4, 0,
                              GTK_ACCEL_VISIBLE);

  pixmap13 = create_pixmap (BorderAdjust, left_xpm);
  gtk_widget_ref (pixmap13);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "pixmap13", pixmap13,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (pixmap13);
  gtk_container_add (GTK_CONTAINER (left), pixmap13);

  right = gtk_button_new ();
  gtk_widget_ref (right);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "right", right,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (right);
  gtk_table_attach (GTK_TABLE (position), right, 2, 3, 1, 2,
                    (GtkAttachOptions) (0),
                    (GtkAttachOptions) (0), 0, 0);
  GTK_WIDGET_UNSET_FLAGS (right, GTK_CAN_FOCUS);
  gtk_widget_add_accelerator (right, "pressed", accel_group,
                              GDK_KP_6, 0,
                              GTK_ACCEL_VISIBLE);

  pixmap14 = create_pixmap (BorderAdjust, right_xpm);
  gtk_widget_ref (pixmap14);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "pixmap14", pixmap14,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (pixmap14);
  gtk_container_add (GTK_CONTAINER (right), pixmap14);

  wideup = gtk_button_new ();
  gtk_widget_ref (wideup);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "wideup", wideup,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (wideup);
  gtk_table_attach (GTK_TABLE (position), wideup, 0, 1, 2, 3,
                    (GtkAttachOptions) (0),
                    (GtkAttachOptions) (0), 0, 0);
  GTK_WIDGET_UNSET_FLAGS (wideup, GTK_CAN_FOCUS);
  gtk_widget_add_accelerator (wideup, "pressed", accel_group,
                              GDK_KP_Add, 0,
                              GTK_ACCEL_VISIBLE);

  pixmap15 = create_pixmap (BorderAdjust, enlarge_xpm);
  gtk_widget_ref (pixmap15);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "pixmap15", pixmap15,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (pixmap15);
  gtk_container_add (GTK_CONTAINER (wideup), pixmap15);

  widedown = gtk_button_new ();
  gtk_widget_ref (widedown);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "widedown", widedown,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (widedown);
  gtk_table_attach (GTK_TABLE (position), widedown, 2, 3, 2, 3,
                    (GtkAttachOptions) (0),
                    (GtkAttachOptions) (0), 0, 0);
  GTK_WIDGET_UNSET_FLAGS (widedown, GTK_CAN_FOCUS);
  gtk_widget_add_accelerator (widedown, "pressed", accel_group,
                              GDK_KP_Subtract, 0,
                              GTK_ACCEL_VISIBLE);

  pixmap16 = create_pixmap (BorderAdjust, reduce_xpm);
  gtk_widget_ref (pixmap16);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "pixmap16", pixmap16,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (pixmap16);
  gtk_container_add (GTK_CONTAINER (widedown), pixmap16);

  down = gtk_button_new ();
  gtk_widget_ref (down);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "down", down,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (down);
  gtk_table_attach (GTK_TABLE (position), down, 1, 2, 1, 2,
                    (GtkAttachOptions) (GTK_EXPAND),
                    (GtkAttachOptions) (0), 0, 0);
  GTK_WIDGET_UNSET_FLAGS (down, GTK_CAN_FOCUS);
  gtk_widget_add_accelerator (down, "pressed", accel_group,
                              GDK_KP_2, 0,
                              GTK_ACCEL_VISIBLE);

  pixmap12 = create_pixmap (BorderAdjust, down_xpm);
  gtk_widget_ref (pixmap12);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "pixmap12", pixmap12,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (pixmap12);
  gtk_container_add (GTK_CONTAINER (down), pixmap12);

  up = gtk_button_new ();
  gtk_widget_ref (up);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "up", up,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (up);
  gtk_table_attach (GTK_TABLE (position), up, 1, 2, 0, 1,
                    (GtkAttachOptions) (0),
                    (GtkAttachOptions) (0), 0, 0);
  GTK_WIDGET_UNSET_FLAGS (up, GTK_CAN_FOCUS);
  gtk_widget_add_accelerator (up, "pressed", accel_group,
                              GDK_KP_8, 0,
                              GTK_ACCEL_VISIBLE);

  pixmap11 = create_pixmap (BorderAdjust, up_xpm);
  gtk_widget_ref (pixmap11);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "pixmap11", pixmap11,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (pixmap11);
  gtk_container_add (GTK_CONTAINER (up), pixmap11);

  hbox3 = gtk_hbox_new (FALSE, 0);
  gtk_widget_ref (hbox3);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "hbox3", hbox3,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (hbox3);
  gtk_box_pack_start (GTK_BOX (vbox3), hbox3, TRUE, TRUE, 0);

  hbox4 = gtk_hbox_new (FALSE, 0);
  gtk_widget_ref (hbox4);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "hbox4", hbox4,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (hbox4);
  gtk_box_pack_start (GTK_BOX (hbox3), hbox4, FALSE, TRUE, 0);

  StabilityLabel = gtk_label_new ("");
  StabilityLabel_key = gtk_label_parse_uline (GTK_LABEL (StabilityLabel),
                                   _("_Stability  "));
  gtk_widget_ref (StabilityLabel);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "StabilityLabel", StabilityLabel,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (StabilityLabel);
  gtk_box_pack_start (GTK_BOX (hbox4), StabilityLabel, FALSE, FALSE, 0);

  Stability_adj = gtk_adjustment_new (0, 0, 16, 1, 10, 10);
  Stability = gtk_spin_button_new (GTK_ADJUSTMENT (Stability_adj), 1, 0);
  gtk_widget_ref (Stability);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "Stability", Stability,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (Stability);
  gtk_box_pack_start (GTK_BOX (hbox4), Stability, FALSE, FALSE, 0);
  gtk_widget_add_accelerator (Stability, "grab_focus", accel_group,
                              GDK_S, GDK_MOD1_MASK,
                              GTK_ACCEL_VISIBLE);
  gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (Stability), TRUE);
  gtk_spin_button_set_wrap (GTK_SPIN_BUTTON (Stability), TRUE);

  vbox12 = gtk_vbox_new (FALSE, 0);
  gtk_widget_ref (vbox12);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "vbox12", vbox12,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (vbox12);
  gtk_box_pack_start (GTK_BOX (hbox3), vbox12, TRUE, TRUE, 0);

  label5 = gtk_label_new (_("Use arrow (num pad) keys to move and \"+\" or \"-\" keys to resize"));
  gtk_widget_ref (label5);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "label5", label5,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (label5);
  gtk_box_pack_start (GTK_BOX (vbox12), label5, FALSE, FALSE, 0);
  gtk_label_set_justify (GTK_LABEL (label5), GTK_JUSTIFY_RIGHT);
  gtk_misc_set_alignment (GTK_MISC (label5), 0.1, 0.5);

  label6 = gtk_label_new (_(" rectangle width. Press \"OK\" to validate settings."));
  gtk_widget_ref (label6);
  gtk_object_set_data_full (GTK_OBJECT (BorderAdjust), "label6", label6,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (label6);
  gtk_box_pack_start (GTK_BOX (vbox12), label6, FALSE, FALSE, 0);
  gtk_label_set_justify (GTK_LABEL (label6), GTK_JUSTIFY_RIGHT);
  gtk_misc_set_alignment (GTK_MISC (label6), 0.02, 0.5);

  gtk_signal_connect (GTK_OBJECT (BorderAdjust), "configure_event",
                      GTK_SIGNAL_FUNC (WindowMove),
                      NULL);
  gtk_signal_connect (GTK_OBJECT (BorderAdjust), "delete_event",
                      GTK_SIGNAL_FUNC (BA_Cancel_clicked),
                      NULL);
  gtk_signal_connect (GTK_OBJECT (Ok), "clicked",
                      GTK_SIGNAL_FUNC (BA_Ok_clicked),
                      NULL);
  gtk_signal_connect (GTK_OBJECT (Cancel), "clicked",
                      GTK_SIGNAL_FUNC (BA_Cancel_clicked),
                      NULL);
  gtk_signal_connect (GTK_OBJECT (Default), "clicked",
                      GTK_SIGNAL_FUNC (BA_Default_clicked),
                      NULL);
  gtk_signal_connect_after (GTK_OBJECT (left), "pressed",
                      GTK_SIGNAL_FUNC (BA_left_pressed),
                      NULL);
  gtk_signal_connect (GTK_OBJECT (right), "pressed",
                      GTK_SIGNAL_FUNC (BA_right_pressed),
                      NULL);
  gtk_signal_connect (GTK_OBJECT (wideup), "pressed",
                      GTK_SIGNAL_FUNC (BA_wideup_pressed),
                      NULL);
  gtk_signal_connect (GTK_OBJECT (widedown), "pressed",
                      GTK_SIGNAL_FUNC (BA_widedown_pressed),
                      NULL);
  gtk_signal_connect (GTK_OBJECT (down), "pressed",
                      GTK_SIGNAL_FUNC (BA_down_pressed),
                      NULL);
  gtk_signal_connect (GTK_OBJECT (up), "pressed",
                      GTK_SIGNAL_FUNC (BA_up_pressed),
                      NULL);
  gtk_signal_connect (GTK_OBJECT (Stability), "changed",
                      GTK_SIGNAL_FUNC (BA_Stability_changed),
                      NULL);

  gtk_widget_add_accelerator (Stability, "grab_focus", accel_group,
                              StabilityLabel_key, GDK_MOD1_MASK, 0);

  gtk_widget_grab_focus (position);
  gtk_widget_grab_default (position);
  gtk_window_add_accel_group (GTK_WINDOW (BorderAdjust), accel_group);

	/* addon*/
 	/* set window icon*/
	set_window_icon(BorderAdjust,REALmagic_xpm);
	/* set the stability value */
	BA_init_stability(Stability);
	
// switch output to VGA
 FMPSet (FMPI_VIDEOOUT, FMPV_VIDEOOUT_VGA);


  return BorderAdjust;
}
