/* Dia -- a diagram creation/manipulation program -*- c -*-
 * Copyright (C) 1998 Alexander Larsson
 *
 * Property system for dia objects/shapes.
 * Copyright (C) 2000 James Henstridge
 * Copyright (C) 2001 Cyrille Chepelov
 * Major restructuration done in August 2001 by C. Chepelov
 *
 * Routines for the construction of a property dialog.
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>

#include <gtk/gtk.h>
#define WIDGET GtkWidget
#include "widgets.h"
#include "properties.h"
#include "propinternals.h"
#include "object.h"

const gchar *prop_dialogdata_key = "object-props:dialogdata";

static void prop_dialog_signal_destroy(GtkWidget *dialog_widget);
static void prop_dialog_fill(PropDialog *dialog, GList *objects, gboolean is_default);

PropDialog *
prop_dialog_new(GList *objects, gboolean is_default) 
{
  PropDialog *dialog = g_new0(PropDialog,1);
  dialog->props = NULL;
  dialog->widget = gtk_vbox_new(FALSE,1);
  dialog->prop_widgets = g_array_new(FALSE,TRUE,sizeof(PropWidgetAssoc));
  dialog->copies = NULL;
  dialog->curtable = NULL;
  dialog->containers = g_ptr_array_new();

  prop_dialog_container_push(dialog,dialog->widget);

  g_object_set_data (G_OBJECT (dialog->widget), prop_dialogdata_key, dialog);
  g_signal_connect (G_OBJECT (dialog->widget), "destroy",
                    G_CALLBACK (prop_dialog_signal_destroy), NULL);

  prop_dialog_fill(dialog,objects,is_default);

  return dialog;
}

void
prop_dialog_destroy(PropDialog *dialog)
{
  if (dialog->props) prop_list_free(dialog->props);
  g_array_free(dialog->prop_widgets,TRUE);
  g_ptr_array_free(dialog->containers,TRUE);
  if (dialog->copies) destroy_object_list(dialog->copies);
  g_free(dialog);
}

PropDialog *
prop_dialog_from_widget(GtkWidget *dialog_widget)
{
  return g_object_get_data (G_OBJECT (dialog_widget), prop_dialogdata_key);
}

static void 
prop_dialog_signal_destroy(GtkWidget *dialog_widget)
{
  prop_dialog_destroy(prop_dialog_from_widget(dialog_widget));
}

WIDGET *
prop_dialog_get_widget(const PropDialog *dialog)
{
  return dialog->widget;
}

void 
prop_dialog_add_raw(PropDialog *dialog, GtkWidget *widget)
{
  dialog->curtable = NULL;
  if (!widget) return;
  gtk_container_add(GTK_CONTAINER(dialog->lastcont),widget);
}

void 
prop_dialog_add_raw_with_flags(PropDialog *dialog, GtkWidget *widget,
			       gboolean expand, gboolean fill)
{
  g_return_if_fail(GTK_IS_BOX(dialog->lastcont));

  dialog->curtable = NULL;
  if (!widget) return;
  gtk_box_pack_start(GTK_BOX(dialog->lastcont),widget, expand, fill, 0);
}

static void
prop_dialog_make_curtable(PropDialog *dialog) 
{
  GtkWidget *table = gtk_table_new(1,2,FALSE);  
  gtk_table_set_row_spacings(GTK_TABLE(table), 2);
  gtk_table_set_col_spacings(GTK_TABLE(table), 5);
  gtk_widget_show(table);
  prop_dialog_add_raw(dialog,table);

  dialog->currow = 0;
  dialog->curtable = table;
}

static void
prop_dialog_add_widget(PropDialog *dialog, GtkWidget *label, GtkWidget *widget)
{
  gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);

  if (!dialog->curtable) prop_dialog_make_curtable(dialog);
  gtk_table_attach(GTK_TABLE(dialog->curtable),label, 
                   0,1,
                   dialog->currow, dialog->currow+1,
                   GTK_FILL, GTK_FILL|GTK_EXPAND,
                   0,0);
  gtk_table_attach(GTK_TABLE(dialog->curtable),widget, 
                   1,2,
                   dialog->currow, dialog->currow+1,
                   GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND,
                   0,0);
  gtk_widget_show(label);
  gtk_widget_show(widget);
  dialog->currow++;
}

/* Register a new container widget (which won't be automatically added) */
void 
prop_dialog_container_push(PropDialog *dialog, GtkWidget *container)
{
  g_ptr_array_add(dialog->containers,container);
  dialog->lastcont = container;
  dialog->curtable = NULL;
}

/* De-register the last container of the stack. */
GtkWidget *
prop_dialog_container_pop(PropDialog *dialog)
{
  GtkWidget *res = g_ptr_array_remove_index(dialog->containers,
                                            dialog->containers->len-1);
  dialog->lastcont = g_ptr_array_index(dialog->containers,
                                       dialog->containers->len-1);
  dialog->curtable = NULL;

  return res;
}

static void 
property_signal_handler(GtkObject *obj,
                        gpointer func_data)
{
  PropEventData *ped = (PropEventData *)func_data;
  if (ped) {
    PropDialog *dialog = ped->dialog;
    Property *prop = ped->self;
    GList *list, *tmp;
    guint j;

    list = dialog->copies;
    g_return_if_fail(list);

    prop->experience &= ~PXP_NOTSET;

    if (!prop->event_handler)
	return;

    /* g_message("Received a signal for object %s from property %s",
       obj->type->name,prop->name); */

    /* obj is a scratch object ; we can do what we want with it. */
    prop_get_data_from_widgets(dialog);

    for (tmp = list; tmp != NULL; tmp = tmp->next) {
      DiaObject *obj = (DiaObject*)tmp->data;
      obj->ops->set_props(obj,dialog->props);
      prop->event_handler(obj,prop);
      obj->ops->get_props(obj,dialog->props);
    }

    for (j = 0; j < dialog->prop_widgets->len; j++) {
      PropWidgetAssoc *pwa = 
        &g_array_index(dialog->prop_widgets,PropWidgetAssoc,j);
      pwa->prop->ops->reset_widget(pwa->prop,pwa->widget);
    }
  } else {
    g_assert_not_reached();
  }
}

void 
prophandler_connect(const Property *prop,
                    GObject *object,
                    const gchar *signal)
{
  if (0==strcmp(signal,"FIXME")) {
    g_warning("signal type unknown for this kind of property (name is %s), \n"
              "handler ignored.",prop->name);
    return;
  } 

  g_signal_connect (G_OBJECT (object),
                    signal,
                    G_CALLBACK (property_signal_handler),
                    (gpointer)(&prop->self));
}

void 
prop_dialog_add_property(PropDialog *dialog, Property *prop)
{
  GtkWidget *widget = NULL;
  PropWidgetAssoc pwa;
  GtkWidget *label;

  prop->self.dialog = dialog;
  prop->self.self = prop;
  prop->self.my_index = dialog->prop_widgets->len;

  if (prop->ops->get_widget) 
    widget = prop->ops->get_widget(prop,dialog);  
  if (!widget) 
    return; /* either property has no widget or it's a container */

  prop->self.widget = widget;
  if (prop->ops->reset_widget) prop->ops->reset_widget(prop,widget);
  prop->experience |= PXP_NOTSET;
  
  pwa.prop = prop;
  pwa.widget = widget;  
  g_array_append_val(dialog->prop_widgets,pwa);  

  label = gtk_label_new(_(prop->descr->description));
  gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);

  prop_dialog_add_widget(dialog, label, widget);
}

static void 
prop_dialog_add_properties(PropDialog *dialog, GPtrArray *props)
{
  guint i;
  gboolean scrollable = (props->len > 16);

  if (scrollable) {
    GtkWidget *swin = gtk_scrolled_window_new (NULL, NULL); 
    GtkWidget *vbox = gtk_vbox_new(FALSE,2);
    gtk_box_pack_start(GTK_BOX (dialog->widget), swin, TRUE, TRUE, 0);
    gtk_widget_show (swin);
    gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (swin), vbox);
    gtk_viewport_set_shadow_type(GTK_VIEWPORT(GTK_BIN(swin)->child), GTK_SHADOW_NONE);
    gtk_widget_show (vbox);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
    prop_dialog_container_push (dialog, swin);
    prop_dialog_container_push (dialog, vbox);
  }


  for (i = 0; i < props->len; i++) {
    Property *prop = (Property*)g_ptr_array_index(props,i);
    prop_dialog_add_property(dialog, prop);
  }

  if (scrollable) {
    GtkRequisition requisition;
    GtkWidget *vbox = prop_dialog_container_pop(dialog);
    GtkWidget *swin = prop_dialog_container_pop(dialog);
    GdkScreen *screen = gtk_widget_get_screen(swin);
    guint sheight = screen ? (2 * gdk_screen_get_height(screen)) / 3 : 400;

    gtk_widget_size_request (vbox, &requisition);
    /* I'd say default size calculation for scrollable is quite broken */
    gtk_widget_set_size_request (swin, -1, requisition.height > sheight ? sheight : requisition.height);    
  }
}

void 
prop_get_data_from_widgets(PropDialog *dialog)
{
  guint i;
  for (i = 0; i < dialog->prop_widgets->len; i++) {
    PropWidgetAssoc *pwa = &g_array_index(dialog->prop_widgets,
                                          PropWidgetAssoc,i);
    pwa->prop->ops->set_from_widget(pwa->prop,pwa->widget);
    
  }
}

static gboolean
pdtpp_is_visible_default (const PropDescription *pdesc)
{
  return pdtpp_defaults (pdesc) && pdtpp_is_visible_no_standard(pdesc);
}

static void 
prop_dialog_fill(PropDialog *dialog, GList *objects, gboolean is_default)
{
  const PropDescription *pdesc;
  GPtrArray *props;

  g_return_if_fail(objects_comply_with_stdprop(objects));

  dialog->objects = g_list_copy(objects);
  dialog->copies = object_copy_list(objects);

  pdesc = object_list_get_prop_descriptions(objects, PROP_UNION);
  if (!pdesc) return;

  if (is_default)
      props = prop_list_from_descs(pdesc,pdtpp_is_visible_default);
  else
      props = prop_list_from_descs(pdesc,pdtpp_is_visible);

  if (!props) return;
    
  dialog->props = props;
  object_list_get_props(objects, props);

  prop_dialog_add_properties(dialog, props);
}



