/* -*- Mode: C; c-basic-offset: 4 -*- */
%%
headers
#include <Python.h>
#define NO_IMPORT_PYGOBJECT
#include <pygobject.h>
#include <pyorbit.h>
#include <bonobo.h>
#include <bonobo/bonobo-ui-config-widget.h>
#include "config.h"

%%
modulename bonobo.ui
%%
init
    init_pyorbit();
%%
import gobject.GObject as PyGObject_Type
import gtk.AccelGroup as PyGtkAccelGroup_Type
import gtk.Bin as PyGtkBin_Type
import gtk.Box as PyGtkBox_Type
import gtk.Container as PyGtkContainer_Type
import gtk.Dialog as PyGtkDialog_Type
import gtk.Image as PyGtkImage_Type
import gtk.Menu as PyGtkMenu_Type
import gtk.MenuBar as PyGtkMenuBar_Type
import gtk.Plug as PyGtkPlug_Type
import gtk.Socket as PyGtkSocket_Type
import gtk.Tooltips as PyGtkTooltips_Type
import gtk.VBox as PyGtkVBox_Type
import gtk.Widget as PyGtkWidget_Type
import gtk.Window as PyGtkWindow_Type
import gtk.gdk.Pixbuf as PyGdkPixbuf_Type
import gnomecanvas.CanvasItem as PyGnomeCanvasItem_Type
import bonobo._bonobo.Object as PyBonoboObject_Type
import bonobo._bonobo.PropertyBag as PyBonoboPropertyBag_Type
%%
ignore-glob
  *_get_type
  *_construct
%%
ignore
  bonobo_ui_component_new_default
  bonobo_ui_node_*
%%
override bonobo_ui_component_add_verb kwargs
static PyObject *
_wrap_bonobo_ui_component_add_verb (PyGObject * self,
				    PyObject *  args,
				    PyObject *  kwargs)
{
    static char *kwlist[] = { "cname", "callback", "extra_data", NULL };
    PyObject *callback, *param=NULL;
    char *cname;

    if (!PyArg_ParseTupleAndKeywords (args, kwargs,
				      "sO|O:BonoboUIComponent.add_verb", kwlist,
				      &cname, &callback, &param)) {
	return NULL;
    }

    if (!PyCallable_Check (callback)) {
        PyErr_SetString (PyExc_TypeError, "second arg not callable");
	return NULL;
    }

    bonobo_ui_component_add_verb_full (BONOBO_UI_COMPONENT (self->obj),
				       cname,
				       pyg_closure_new (callback, param, NULL));
    Py_INCREF (Py_None);
    return Py_None;
}
%%
override bonobo_ui_component_add_verb_list kwargs
static PyObject *
_wrap_bonobo_ui_component_add_verb_list (PyGObject * self,
					 PyObject *  args,
					 PyObject *  kwargs)
{
    static char *kwlist[] = { "list", "extra_data", NULL };    
    PyObject *          list;
    PyObject *          params = NULL;
    BonoboUIComponent * uic;
    int                 len, i;
    PyObject *          item;
    char *              cname;
    PyObject *          callback;
    
    if (!PyArg_ParseTupleAndKeywords (args, kwargs,
				      "O|O:BonoboUIComponent.add_verb_list", 
				      kwlist, &list, &params)) {
	return NULL;
    }

    if (!PySequence_Check (list)) {
	PyErr_SetString (PyExc_TypeError, "entries must be a sequence");
	return NULL;
    }

    uic = BONOBO_UI_COMPONENT (self->obj);
    len = PySequence_Size (list);
    for (i = 0; i < len; i++) {
	item = PySequence_GetItem (list, i);
	Py_DECREF (item);
	
	if (!PyArg_ParseTuple (item, "sO", &cname, &callback)) {
	    return NULL;
	}

	bonobo_ui_component_add_verb_full (uic, cname,
					   pyg_closure_new (callback, params, NULL));
    }

    Py_INCREF(Py_None);
    return Py_None;
}
%%
override bonobo_ui_component_add_listener kwargs
static PyObject *
_wrap_bonobo_ui_component_add_listener (PyGObject * self,
				        PyObject *  args,
				        PyObject *  kwargs)
{
    static char *kwlist[] = { "id", "callback", "extra_data", NULL };
    PyObject *callback, *param=NULL;
    char *id;

    if (!PyArg_ParseTupleAndKeywords (args, kwargs,
				      "sO|O:BonoboUIComponent.add_listener",
				      kwlist, &id, &callback, &param))
	return NULL;

    if (!PyCallable_Check (callback)) {
        PyErr_SetString (PyExc_TypeError, "second arg not callable");
	return NULL;
    }

    bonobo_ui_component_add_listener_full (BONOBO_UI_COMPONENT (self->obj),
				           id,
				           pyg_closure_new (callback, param, NULL));
    Py_INCREF (Py_None);
    return Py_None;
}
%%
override bonobo_widget_new_control kwargs
static int
_wrap_bonobo_widget_new_control(PyGObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "object", "uic", NULL };
    PyObject *object;
    PyObject *pyuic = Py_None;
    CORBA_Environment ev;
    Bonobo_UIContainer uic = CORBA_OBJECT_NIL;
    
    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
				     "O|O:BonoboWidget.__init__",
				     kwlist, &object, &pyuic)) {
        return -1;
    }

    CORBA_exception_init (&ev);

    if (PyObject_TypeCheck(pyuic, &PyCORBA_Object_Type)) {
	gboolean is_uic = CORBA_Object_is_a(((PyCORBA_Object *)pyuic)->objref,
					    "IDL:Bonobo/UIContainer:1.0", &ev);
	if (pyorbit_check_ex(&ev))
	    return -1;
	if (!is_uic) {
	    PyErr_SetString (PyExc_TypeError, "uic must be a Bonobo.UIContainer");
	    return -1;
	}
	uic = (Bonobo_UIContainer)((PyCORBA_Object *)pyuic)->objref;
    } else if (pyuic != Py_None) {
	PyErr_SetString (PyExc_TypeError, "uic must be a Bonobo.UIContainer");
	return -1;
    }
    
	
    if (PyString_Check (object)) {
	self->obj = (GObject *)bonobo_widget_new_control(PyString_AsString (object), uic);
    } else if (PyObject_TypeCheck(object, &PyCORBA_Object_Type)) {
	self->obj = (GObject *)bonobo_widget_new_control_from_objref(((PyCORBA_Object *)object)->objref, uic);
    } else {
	PyErr_SetString(PyExc_TypeError, "object should be a string or a Bonobo.Control");	
	return -1;
    }
    
    if (!self->obj) {
        PyErr_SetString(PyExc_RuntimeError, "could not create BonoboControl object");
        return -1;
    }
    
    pygobject_register_wrapper((PyObject *)self);
    return 0;
}

%%
override bonobo_ui_component_new
static int
_wrap_bonobo_ui_component_new(PyGObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "name", NULL };
    char *name = NULL;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s:BonoboUIComponent.__init__", kwlist, &name))
        return -1;
    if (name)
	self->obj = (GObject *)bonobo_ui_component_new(name);
    else
	self->obj = (GObject *)bonobo_ui_component_new_default();

    if (!self->obj) {
        PyErr_SetString(PyExc_RuntimeError, "could not create BonoboUIComponent object");
        return -1;
    }
    g_object_ref (self->obj);
    pygobject_register_wrapper((PyObject *)self);
    return 0;
}
%%
override libbonobo_ui_module_info_get noargs
static PyObject *
_wrap_libbonobo_ui_module_info_get(PyObject *self)
{
    GnomeModuleInfo *ret;

    ret = (GnomeModuleInfo *) libbonobo_ui_module_info_get();
    /* pyg_boxed_new handles NULL checking */
    return pyg_boxed_new(GNOME_TYPE_MODULE_INFO, ret, TRUE, TRUE);
}
%%
override bonobo_ui_gtk_module_info_get noargs
static PyObject *
_wrap_bonobo_ui_gtk_module_info_get(PyObject *self)
{
    GnomeModuleInfo *ret;

    ret = (GnomeModuleInfo *) bonobo_ui_gtk_module_info_get();
    /* pyg_boxed_new handles NULL checking */
    return pyg_boxed_new(GNOME_TYPE_MODULE_INFO, ret, TRUE, TRUE);
}
