/* support.c
 * Copyright (C) 2004, 2005, 2006 Sylvain Cresto <scresto@gmail.com>
 * 
 * This file is part of graveman!
 *
 * graveman! 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, or
 * (at your option) any later version.
 * 
 * graveman! 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 program; see the file COPYING. If not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA. 
 * 
 * URL: http://www.nongnu.org/graveman/
 *
 */

#include "graveman.h"

static GdkCursor *_cursor = NULL;

/* glade-xml.c */
static const gchar *glade_xml_tooltips_key = "GladeXML::tooltips";
static GQuark       glade_xml_tooltips_id  = 0;
/* glade-xml.c */


GtkWidget *glade_to_hash(GladeXML *Axml, GHashTable *Ahash, gchar *Akey)
{
  GtkWidget *Lobj = glade_xml_get_widget(Axml, Akey);

  if (! GTK_IS_WIDGET(Lobj)) {
    g_warning("glade_to_hash: '%s' is not a widget !\n", Akey);
    return NULL;
  }
  _DEB("on insere le widget '%s'\n", Akey);
  g_hash_table_insert(Ahash, g_strdup(Akey), Lobj);

  return Lobj;
}

void glade_to_hash_list(GladeXML *Axml, GHashTable *Ahash, const gchar *Afirst, ...)
{
  va_list Largs;
  gchar *Lbuf = (gchar *) Afirst;
  gchar *Lname;
  GtkWidget *Lobject;
  if ((Lobject = glade_xml_get_widget(Axml, Afirst)))
    g_hash_table_insert(Ahash, Lbuf, Lobject);
  
  va_start(Largs, Afirst);
  while (*Lbuf++) {
    if (!(Lname = va_arg(Largs, gchar *))) break;
_DEB("unwid %s !!!\n", Lname);
    if ((Lobject = glade_xml_get_widget(Axml, Lname))) {
      g_hash_table_insert(Ahash, Lname, Lobject);
#ifdef DEBUG
    } else {
      _DEB("object non trouve => %s\n", Lname);
#endif        
    }
  }

  va_end(Largs);
}

GtkWidget *glade_to_hashv(GladeXML *Axml, GHashTable *Ahash, const gchar *Akeyformat, ...) 
{
  va_list Largs;
  gchar *Lkey;
  GtkWidget *Lreturn;

  va_start(Largs, Akeyformat);
  Lkey = g_strdup_vprintf(Akeyformat, Largs);
  va_end(Largs);
_DEB("ON ENREGISTRE => '%s'", Lkey);
 
  Lreturn = glade_to_hash(Axml, Ahash, Lkey);
  g_free(Lkey);
  
  return Lreturn;
}

/* gestion du curseur de la souris */
void set_cursor(GtkWidget *Awindow, GdkCursorType Acursortype)
{
  _cursor = gdk_cursor_new(Acursortype);
  gdk_window_set_cursor(GDK_WINDOW(Awindow->window), _cursor);
  gdk_flush ();
}

void unset_cursor(GtkWidget *Awindow)
{
  gdk_window_set_cursor(GDK_WINDOW(Awindow->window), NULL);
  gdk_cursor_unref(_cursor);
}

/* mise a jour d'une barre d'avancement */
void maj_progress(GtkProgressBar *AprogressBar, gdouble Apourcent)
{
  gchar Lsbuf[40];

  gtk_progress_bar_set_fraction(AprogressBar, Apourcent);
  g_snprintf(Lsbuf, sizeof(Lsbuf)-1, "%.0f%%", Apourcent*100);
  gtk_progress_bar_set_text(AprogressBar, Lsbuf);
}

/* traitement Twidget */
Tgrave *sc_grave_newempty()
{
  Tgrave *Agrave = g_malloc0(sizeof(Tgrave));

  Agrave->data = g_hash_table_new(g_str_hash, g_str_equal);
  
  return Agrave;
}

Tgrave *sc_grave_load(gchar *Aname)
{
  Tgrave *Agrave = g_malloc0(sizeof(Tgrave));
  gchar Lfullname[MAXPATHLEN];

  g_snprintf(Lfullname, sizeof(Lfullname)-1, "%s/%s/%s", DATA_DIR, PACKAGE, Aname);
  
  Agrave->xml = glade_xml_new(Lfullname, NULL, NULL);


  if (!Agrave->xml) {
    g_free(Agrave);
    g_critical(_("error cannot load '%s': %s"), Lfullname, g_strerror(errno));
    return FALSE;
  }

  Agrave->data = g_hash_table_new(g_str_hash, g_str_equal);
  
  return Agrave;
}

void sc_init()
{
  glade_xml_tooltips_id  = g_quark_from_static_string(glade_xml_tooltips_key);
}

/* activer / desactiver les tooltips */
void sc_tooltips(GtkWidget *Awin, gboolean Aetat)
{
  GtkTooltips *Ltooltips = NULL;

  Ltooltips = GTK_TOOLTIPS(g_object_get_qdata(G_OBJECT(Awin), glade_xml_tooltips_id));

  if (GTK_IS_TOOLTIPS(Ltooltips)) {
    if (Aetat) {
      gtk_tooltips_enable(Ltooltips);
    } else {
      gtk_tooltips_disable(Ltooltips);
    }
  }
}

Tgrave *sc_grave_destroy(Tgrave *Agrave)
{
  g_return_val_if_fail(Agrave != NULL, NULL);

  if (Agrave->xml != NULL) {
    g_object_unref(Agrave->xml);
  }
  if (Agrave->data) g_hash_table_destroy(Agrave->data);

  g_free(Agrave);

  return NULL;
}

GtkWidget *sc_grave_get_widget(Tgrave *Agrave, gchar *Aname)
{
  if (Agrave == NULL || Agrave->xml == NULL) return NULL;

  return glade_xml_get_widget(Agrave->xml, Aname);
}

gpointer sc_grave_get_widgetordata(Tgrave *Agrave, gchar *Aname)
{
  gpointer Ldata = NULL;
  if (Agrave == NULL || Agrave->xml == NULL) return NULL;

  if ((Ldata = glade_xml_get_widget(Agrave->xml, Aname))) return Ldata;

  if ((Ldata = g_hash_table_lookup(Agrave->data, Aname))) return Ldata;

  return NULL;
  
}

GtkWidget *sc_grave_get_widgetv(Tgrave *Agrave, gchar *Aformat, ...)
{
  va_list Largs;
  gchar *Lfname;
  GtkWidget *Lptr;

  va_start(Largs, Aformat);
  Lfname = g_strdup_vprintf(Aformat, Largs);
  va_end(Largs);
  Lptr = sc_grave_get_widget(Agrave, Lfname); 
  g_free(Lfname);

  return Lptr;
}

gpointer sc_grave_get_data(Tgrave *Agrave, gchar *Aname)
{
  g_return_val_if_fail(Agrave != NULL && Agrave->data != NULL, NULL);

  return g_hash_table_lookup(Agrave->data, Aname);
}

gpointer sc_grave_get_datav(Tgrave *Agrave, gchar *Aformat, ...)
{
  va_list Largs;
  gchar *Lfname;
  gpointer Lptr;

  va_start(Largs, Aformat);
  Lfname = g_strdup_vprintf(Aformat, Largs);
  va_end(Largs);
  Lptr = sc_grave_get_data(Agrave, Lfname); 
  g_free(Lfname);

  return Lptr;
}

gpointer sc_grave_set_data(Tgrave *Agrave, gpointer Aptr, gchar *Aname)
{
  g_return_val_if_fail(Agrave != NULL && Agrave->data != NULL, NULL);

  g_hash_table_insert(Agrave->data, Aname, Aptr);

  return Aptr;
}

gpointer sc_grave_set_datav(Tgrave *Agrave, gpointer Aptr, gchar *Aformat, ...)
{
  va_list Largs;
  gchar *Lfname;

  va_start(Largs, Aformat);
  Lfname = g_strdup_vprintf(Aformat, Largs);
  va_end(Largs);
  sc_grave_set_data(Agrave, Aptr, Lfname); 
  g_free(Lfname);

  return Aptr;
}

gpointer sc_grave_replace_data(Tgrave *Agrave, gpointer Aptr, gchar *Aname)
{
  g_return_val_if_fail(Agrave != NULL && Agrave->data != NULL, NULL);

  g_hash_table_replace(Agrave->data, Aname, Aptr);

  return Aptr;
}

gpointer sc_grave_replace_datav(Tgrave *Agrave, gpointer Aptr, gchar *Aformat, ...)
{
  va_list Largs;
  gchar *Lfname;

  va_start(Largs, Aformat);
  Lfname = g_strdup_vprintf(Aformat, Largs);
  va_end(Largs);
  sc_grave_replace_data(Agrave, Aptr, Lfname); 
  g_free(Lfname);

  return Aptr;
}

gpointer sc_grave_del_data(Tgrave *Agrave, gchar *Aname)
{
  g_return_val_if_fail(Agrave != NULL && Agrave->data != NULL, NULL);

  g_hash_table_remove(Agrave->data, Aname);

  return NULL;
}

gpointer sc_grave_del_datav(Tgrave *Agrave, gchar *Aformat, ...)
{
  va_list Largs;
  gchar *Lfname;

  va_start(Largs, Aformat);
  Lfname = g_strdup_vprintf(Aformat, Largs);
  va_end(Largs);
  sc_grave_del_data(Agrave, Lfname); 
  g_free(Lfname);

  return NULL;
}

gpointer sc_grave_ref_wtd(Tgrave *Agrave, gchar *Awidgetsrc, gchar *Adatadst)
{
  GtkWidget *Awidg;
  g_return_val_if_fail(Agrave && Agrave->data && Agrave->xml, NULL);

  if (!(Awidg = glade_xml_get_widget(Agrave->xml, Awidgetsrc))) return NULL;

  g_hash_table_insert(Agrave->data, Adatadst, Awidg);

  return Awidg;
}

/*
 * vim:et:ts=8:sts=2:sw=2
 */
