/* 
   PMail - GNOME/GTK/Python email client
   Copyright (C) 2000 Scott Bender <sbender@harmony-ds.com>

   This file is part of PMail.

   PMail 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.

   PMail 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 PMail; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  
*/

#include <Python.h>
#include <gtk-xmhtml/gtk-xmhtml.h>
#include <pygtk.h>
#include <libgnomeui/libgnomeui.h>
#include <libgnome/gnome-mime-info.h>


static PyObject *_image_func = 0;

static XmImageInfo *imageProc(TWidget w, String url, TPointer tp)
{
  PyObject *ret, *tuple;
  char *res;
  
  tuple = PyTuple_New(1);
  PyTuple_SetItem(tuple, 0, PyString_FromString(url));

  ret = PyObject_CallObject(_image_func, tuple);

  if (ret == NULL) {
    if (PyGtk_FatalExceptions)
      gtk_main_quit();
    else {
      PyErr_Print();
      PyErr_Clear();
    }
    return;
  }

  res = PyString_AsString(ret);

  return XmHTMLImageDefaultProc(w, res, NULL, 0);
}

static PyObject *_wrap_gtk_xhtml_set_image_proc(PyObject *self, PyObject *args) {
    PyGtk_Object *obj;
    int signum;

    if (!PyArg_ParseTuple(args, "O!O:set_image_proc", &PyGtk_Type,
			  &obj, &_image_func))
        return NULL;
    if (!PyCallable_Check(_image_func)) {
        PyErr_SetString(PyExc_TypeError, "third argument must be callable");
        return NULL;
    }
    Py_INCREF(_image_func);

    gtk_xmhtml_set_image_procs(GTK_XMHTML(PyGtk_Get(obj)),
			       imageProc,
			       0, 0, 0);
    return Py_None;
}    

static PyObject *_wrap_gtk_xmhtml_set_anchor_underline_type(PyObject *self, PyObject *args) {
    int underline_type;
    PyObject *html;

    if (!PyArg_ParseTuple(args, "O!i:gtk_xmhtml_set_anchor_underline_type", &PyGtk_Type, &html, &underline_type))
        return NULL;
    gtk_xmhtml_set_anchor_underline_type(GTK_XMHTML(PyGtk_Get(html)), underline_type);
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *_wrap_gnome_mime_get_value(PyObject *self, PyObject *args) {
    char *mime_type, *key;
    const char *res;

    if (!PyArg_ParseTuple(args, "ss:gnome_mime_get_value", &mime_type, &key))
        return NULL;
    res = gnome_mime_get_value(mime_type, key);
    if (res == 0)
      {
	return Py_None;
      }
    return PyString_FromString(res);
}

static _my_spell_func(GnomeSpell *speller, gpointer arg1, gpointer user_data)
{
  printf("_my_spell_func\n");
}


static PyObject *_setup_gnome_spell(PyObject *self, PyObject *args) {
  PyObject *p_spell;
  GnomeSpell *spell;
  GtkWindow *win;
  PyObject *func;

  if (!PyArg_ParseTuple(args, "O!O:gnome_mime_get_value", &PyGtk_Type, 
			&p_spell, &func))
    return NULL;

  win = GTK_WINDOW(PyGtk_Get(p_spell));
  spell = GNOME_SPELL(win);
  
  gtk_signal_connect(GTK_OBJECT(spell), "found-word", _my_spell_func, func);
  return Py_None;
}


  


static PyObject *_window_set_parent(PyObject *self, PyObject *args)
{
  PyObject *p_dialog, *p_parent;
  GtkWindow *parent, *dialog;
  gint p_w = 0, p_h = 0;

  if (!PyArg_ParseTuple(args, "O!O!:window_set_parent", &PyGtk_Type, 
			&p_dialog, &PyGtk_Type, &p_parent))
    return NULL;

  dialog = GTK_WINDOW(PyGtk_Get(p_dialog));
  parent = GTK_WINDOW(PyGtk_Get(p_parent));

  if (dialog == NULL
      || !GTK_IS_WINDOW(dialog)
      || parent == NULL
      || !GTK_IS_WINDOW(parent)
      || parent == GTK_WINDOW(dialog))
    {
      PyErr_SetString(PyExc_TypeError, "invalid argument");
      return NULL;
    }

  gtk_window_set_transient_for (GTK_WINDOW(dialog), parent);

  if ( gnome_preferences_get_dialog_centered() ) {

    /* User wants us to center over parent */

    gint x, y, w, h, dialog_x, dialog_y, dialog_w = 0, dialog_h = 0;

    if ( ! GTK_WIDGET_VISIBLE(parent)) return; /* Can't get its
						  size/pos */


    /* Throw out other positioning */
    gtk_window_set_position(GTK_WINDOW(dialog),GTK_WIN_POS_NONE);

    gdk_window_get_origin(GTK_WIDGET(parent)->window, &x, &y);
    gdk_window_get_size(GTK_WIDGET(parent)->window, &w, &h);


    if (GTK_WIDGET_VISIBLE(dialog))
      {
	gdk_window_get_size(GTK_WIDGET(dialog)->window, &dialog_w, &dialog_h);
      }
    else if ( p_w && p_h )
      {
	dialog_w = p_w;
	dialog_h = p_h;
      }

    if ( dialog_w && dialog_h )
      {
	dialog_x = x + w/2 - dialog_w/2;
	dialog_y = y + h/2 - dialog_h/2;
      }	
    else
      {
	dialog_x = x + w/4;
	dialog_y = y + h/4;
      }	

    gtk_widget_set_uposition(GTK_WIDGET(dialog), dialog_x, dialog_y); 
  }
  return Py_None;
}


static PyObject *_spellinfo_get_original(PyObject *self, PyObject *args) 
{
  PyObject *pobj;
  GnomeSpellInfo *info;
  
  if (!PyArg_ParseTuple(args, "O:spellinfo_get_original", &pobj))
    return NULL;
  
  info = PyCObject_AsVoidPtr(pobj);
  printf("original = %s\n", info->original);
  return PyString_FromString(info->original);
}


static PyMethodDef pmailextensionMethods[] = {
    { "gtk_xmhtml_set_image_proc", _wrap_gtk_xhtml_set_image_proc, 1 },
    { "gtk_xmhtml_set_anchor_underline_type", _wrap_gtk_xmhtml_set_anchor_underline_type, 1 },
    { "gnome_mime_get_value", _wrap_gnome_mime_get_value, 1 },
    { "window_set_parent", _window_set_parent, 1 },
    { "setup_gnome_spell", _setup_gnome_spell, 1 },
//    { "spellinfo_get_replacement", _spellinfo_get_replacement, 1 },
//   { "spellinfo_get_word", _spellinfo_get_word, 1 },
//    { "spellinfo_get_offet", _spellinfo_get_offet, 1 },
    { NULL, NULL, 0 }
};


void init_pmailextensions() {
    PyObject *m, *d;

    m = Py_InitModule("_pmailextensions", pmailextensionMethods);

    init_pygtk();

    if (PyErr_Occurred())
        Py_FatalError("can't initialise module _pmailextensions");
}


syntax highlighted by Code2HTML, v. 0.9.1