/* -*- Mode: C; c-basic-offset: 4 -*- */
%%
headers
#include <Python.h>

#define NO_IMPORT_PYGOBJECT
#include "pygobject.h"
#include <libmateui/libmateui.h>
#include <libmateui/mate-ice.h>
/* #include <libmateui/mate-appbar.h> */
#include <matecomponent/matecomponent-ui-type-builtins.h>
#define MATE_TYPE_APP_BAR MATE_TYPE_APPBAR
#define MATE_APP_BAR MATE_APPBAR
#define NO_IMPORT_PYMATEVFS
#include "pymatevfs.h"
#include "config.h"

%%
modulename mate.ui
%%
import gobject.GObject as PyGObject_Type
import gtk.Object as PyGtkObject_Type
import gtk.Widget as PyGtkWidget_Type
import gtk.Bin as PyGtkBin_Type
import gtk.Container as PyGtkContainer_Type
import gtk.HBox as PyGtkHBox_Type
import gtk.VBox as PyGtkVBox_Type
import gtk.Window as PyGtkWindow_Type
import gtk.Dialog as PyGtkDialog_Type
import gtk.Button as PyGtkButton_Type
import gtk.Image as PyGtkImage_Type
import gtk.Menu as PyGtkMenu_Type
import gtk.MenuBar as PyGtkMenuBar_Type
import gtk.Toolbar as PyGtkToolbar_Type
import gtk.Combo as PyGtkCombo_Type
import gtk.Adjustment as PyGtkAdjustment_Type
import gtk.Editable as PyGtkEditable_Type
import gtk.gdk.Pixbuf as PyGdkPixbuf_Type
import matecanvas.Canvas as PyMateCanvas_Type
import matecanvas.CanvasItem as PyMateCanvasItem_Type
import matecomponent.ui.DockItem as PyMateComponentDockItem_Type
import gtk.gdk.Screen as PyGdkScreen_Type
import gtk.IconTheme as PyGtkIconTheme_Type
import gtk.HBox as PyGtkHBox_Type
%%
ignore-glob
  *_get_type
  *_construct

%%
ignore
  mate_window_icon_init
  mate_window_icon_set_from_file
  mate_window_icon_set_from_file_list
  mate_window_icon_set_default_from_file
  mate_window_icon_set_default_from_file_list
  mate_ok_dialog
  mate_message_box_new
  mate_icon_lookup_sync

%%
override mate_about_new kwargs
static int
_wrap_mate_about_new (PyGObject *self, PyObject *args, PyObject *kwargs)
{
    static char  *kwlist[] = { "name", "version", "copyright", "comments",
			       "authors", "documenters", "translator_credits",
			       "logo_pixbuf", NULL };
    
    char       *name, *version, *copyright, *comments;
    PyObject   *pyauthors;
    PyObject   *pydocumenters = Py_None;
    char       *translator_credits = NULL;
    PyGObject  *pypixbuf = NULL;
    int         n_authors;
    char      **authors;
    int         i;
    int         n_documenters;
    char      **documenters;
    GdkPixbuf  *pixbuf;
    
    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
                                     "ssssO|OzO!:MateAbout.__init__", kwlist,
                                     &name, &version, &copyright, &comments,
				     &pyauthors, &pydocumenters,
				     &translator_credits, &PyGdkPixbuf_Type, &pypixbuf)) {
	return -1;
    }

    if (!PyList_Check(pyauthors) && !PyTuple_Check(pyauthors)) {
	PyErr_SetString(PyExc_TypeError, "5th argument not a tuple or list");
	return -1;
    }

    if (pydocumenters != Py_None && !PyList_Check(pydocumenters) && !PyTuple_Check(pydocumenters)) {
	PyErr_SetString(PyExc_TypeError, "6th argument not a tuple or list");
	return -1;
    }
    
    n_authors = PySequence_Length(pyauthors);
    authors = g_new(gchar *, n_authors + 1);
    for (i = 0; i < n_authors; i++) {
        PyObject *item = PySequence_GetItem(pyauthors, i);
        Py_DECREF(item); 
        authors[i] = PyString_AsString(item);
    }
    authors[n_authors] = NULL;
    
    if (pydocumenters != Py_None) {
	    n_documenters = PySequence_Length(pydocumenters);
	    documenters = g_new(gchar *, n_documenters + 1);
	    for (i = 0; i < n_documenters; i++) {
		    PyObject *item = PySequence_GetItem(pydocumenters, i);
		    Py_DECREF(item); 
		    documenters[i] = PyString_AsString(item);
	    }
	    documenters[n_documenters] = NULL;
    } else {
	    documenters = NULL;
    }

    /* if translator_credits is "translator_credits", set it to NULL */
    if (translator_credits &&
	!strcmp(translator_credits, "translator_credits"))
	translator_credits = NULL;

    if (pypixbuf != NULL) {
	pixbuf = GDK_PIXBUF(pypixbuf->obj);
    } else {
	pixbuf = NULL;
    }
    
    self->obj = (GObject*)mate_about_new (name,
					   version,
					   copyright,
					   comments,
					   (const gchar**)authors,
					   (const gchar**)documenters,
					   translator_credits,
					   pixbuf);
    if (!self->obj) {
        PyErr_SetString(PyExc_RuntimeError,"could not create MateAbout object");
        return -1;
    }

    g_object_ref(self->obj); /* we don't own the first reference of windows */
    pygobject_register_wrapper((PyObject *)self);
    return 0;
}
%%
override mate_icon_list_get_icon_data kwargs
static PyObject *
_wrap_mate_icon_list_get_icon_data(PyGObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "idx", NULL };
    int idx;
    PyGObject *data;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
				     "i:MateIconList.get_icon_data",
				     kwlist, &idx))
        return NULL;
    
    data = mate_icon_list_get_icon_data(MATE_ICON_LIST(self->obj), idx);
    if (!data) {
	Py_INCREF(Py_None);
	return Py_None;
    }
    
    Py_INCREF(data);
    return (PyObject*)data;
}
%%
override mate_icon_list_set_icon_data kwargs
static PyObject *
_wrap_mate_icon_list_set_icon_data(PyGObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "idx", "data", NULL };
    int idx;
    PyGObject *data;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
				     "iO:MateIconList.set_icon_data",
				     kwlist, &idx, &data))
        return NULL;
    
    mate_icon_list_set_icon_data(MATE_ICON_LIST(self->obj),
				  idx, data);
    
    Py_INCREF(Py_None);
    return Py_None;
}
%%
override mate_icon_list_get_selection
static PyObject *
_wrap_mate_icon_list_get_selection(PyGObject *self)
{
    GList    *list, *l;
    PyObject *rv;
    int       len, i;

    list = mate_icon_list_get_selection(MATE_ICON_LIST(self->obj));
    len = g_list_length(list);
    rv = PyList_New(len);
    if (!rv) {
	return NULL;
    }
    for (i = 0, l = list; i < len; ++i, l = l->next)
	PyList_SET_ITEM(rv, i, PyInt_FromLong(GPOINTER_TO_INT(l->data)));
    g_list_free(list);
    return rv;
}
%%
override mate_client_set_restart_command kwargs

typedef void (*clientfunc) (MateClient *, int, gchar*[]);

static PyObject*
generic_mate_client_command (PyGObject *self, PyObject*args, PyObject* kwargs,
			      const char *name, clientfunc func)
{
    char **argv;
    int argc, i;
    PyObject *py_argv;
    char fmt[128];

    if (PySequence_Length(args) == 2) {
	static char *kwlist[] = { "argc", "argv", NULL };
	snprintf(fmt, 128, "iO:gome.ui.Client.%s", name);
	if (!PyArg_ParseTupleAndKeywords(args, kwargs,
					 fmt, kwlist, &argc, &py_argv))
	    return NULL;
	if (PyErr_Warn(PyExc_DeprecationWarning, "the 'argc' parameter"
		       " is optional and deprecated") < 0)
	    return NULL;
    } else {
	static char *kwlist[] = { "argv", NULL };
	snprintf(fmt, 128, "O:gome.ui.Client.%s", name);
	if (!PyArg_ParseTupleAndKeywords(args, kwargs,
					 fmt, kwlist, &py_argv))
	    return NULL;
    }

    if (!PySequence_Check(py_argv)) {
	PyErr_SetString(PyExc_TypeError, "argv argument is not a sequence");
	return NULL;
    }

    argc = PySequence_Length(py_argv);
    argv = g_new(gchar *, argc + 1);
    for (i = 0; i < argc; i++) {
	PyObject *item = PySequence_GetItem(py_argv, i);
	if (!PyString_Check(item)) {
	    PyErr_SetString(PyExc_TypeError, "argv element is not a string");
	    Py_DECREF(item);
	    g_free(argv);
	    return NULL;
	}
	argv[i] = PyString_AsString(item);
	Py_DECREF(item);
    }
    argv[i] = NULL;
    func(MATE_CLIENT(self->obj), argc, argv);
    g_free(argv);
    
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject*
_wrap_mate_client_set_restart_command (PyGObject *self, PyObject *args,
					PyObject *kwargs)
{
    return generic_mate_client_command(self, args, kwargs,
					"set_restart_command",
					mate_client_set_restart_command);
}
%%
override mate_client_set_clone_command kwargs
static PyObject*
_wrap_mate_client_set_clone_command (PyGObject *self, PyObject *args,
				      PyObject *kwargs)
{
    return generic_mate_client_command(self, args, kwargs,
					"set_clone_command",
					mate_client_set_clone_command);
}
%%
override mate_client_set_discard_command kwargs
static PyObject*
_wrap_mate_client_set_discard_command (PyGObject *self, PyObject *args,
					PyObject *kwargs)
{
    return generic_mate_client_command(self, args, kwargs,
					"set_discard_command",
					mate_client_set_discard_command);
}
%%
override mate_client_set_resign_command kwargs
static PyObject*
_wrap_mate_client_set_resign_command (PyGObject *self, PyObject *args,
				       PyObject *kwargs)
{
    return generic_mate_client_command(self, args, kwargs,
					"set_resign_command",
					mate_client_set_resign_command);
}
%%
override mate_client_set_shutdown_command kwargs
static PyObject*
_wrap_mate_client_set_shutdown_command (PyGObject *self, PyObject *args,
					 PyObject *kwargs)
{
    return generic_mate_client_command(self, args, kwargs,
					"set_shutdown_command",
					mate_client_set_shutdown_command);
}
%%
override mate_client_module_info_get noargs
static PyObject *
_wrap_mate_client_module_info_get(PyObject *self)
{
    MateModuleInfo *ret;

    ret = (MateModuleInfo *) mate_client_module_info_get();
    /* pyg_boxed_new handles NULL checking */
    return pyg_boxed_new(MATE_TYPE_MODULE_INFO, ret, TRUE, TRUE);
}
%%
override libmateui_module_info_get noargs
static PyObject *
_wrap_libmateui_module_info_get(PyObject *self)
{
    MateModuleInfo *ret;

    ret = (MateModuleInfo *) libmateui_module_info_get();
    /* pyg_boxed_new handles NULL checking */
    return pyg_boxed_new(MATE_TYPE_MODULE_INFO, ret, TRUE, TRUE);
}
%%
override mate_gtk_module_info_get noargs
static PyObject *
_wrap_mate_gtk_module_info_get(PyObject *self)
{
    MateModuleInfo *ret;

    ret = (MateModuleInfo *) mate_gtk_module_info_get();
    /* pyg_boxed_new handles NULL checking */
    return pyg_boxed_new(MATE_TYPE_MODULE_INFO, ret, TRUE, TRUE);
}
%%
override mate_icon_lookup kwargs
static PyObject *
_wrap_mate_icon_lookup(PyObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "icon_theme", "thumbnail_factory", "file_uri",
                              "custom_icon", "flags", "mime_type", "file_info", NULL };
    PyGObject *icon_theme;
    char *file_uri, *custom_icon = NULL, *mime_type = NULL;
    gchar *ret;
    PyObject *file_info = NULL;
    PyObject *py_flags = NULL;
    PyObject *py_thumb_fact;
    MateThumbnailFactory *thumbnail_factory = NULL;
    MateIconLookupFlags flags;
    MateIconLookupResultFlags result_flags = 0;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!Os|sOsO!:icon_lookup", kwlist,
                                     &PyGtkIconTheme_Type, &icon_theme,
                                     &py_thumb_fact,
                                     &file_uri, &custom_icon,
                                     &py_flags, &mime_type,
                                     &PyMateVFSFileInfo_Type, &file_info))
        return NULL;

    if ((file_info && !mime_type) || (!file_info && mime_type)) {
        PyErr_SetString(PyExc_TypeError, "file_info and mime_type mutually exclusive");
        return NULL;
    }

    if (py_flags) {
        if (pyg_flags_get_value(MATE_TYPE_ICON_LOOKUP_FLAGS, py_flags, (gint *)&flags))
            return NULL;
    } else
        flags = MATE_ICON_LOOKUP_FLAGS_NONE;
    
    if (pygobject_check(((PyGObject *) py_thumb_fact), &PyMateThumbnailFactory_Type))
        thumbnail_factory = MATE_THUMBNAIL_FACTORY(((PyGObject *) py_thumb_fact)->obj);
    else if (py_thumb_fact == Py_None)
        thumbnail_factory = NULL;
    else {
        PyErr_SetString(PyExc_TypeError,
                        "thumbnail_factory has to be a MateThumbnailFactory or None");
        return NULL;
    }

    if (file_info)
        ret = mate_icon_lookup(GTK_ICON_THEME(icon_theme->obj),
                                thumbnail_factory,
                                file_uri, custom_icon,
                                pymate_vfs_file_info_get(file_info),
                                mime_type, flags, &result_flags);
    else
        ret = mate_icon_lookup_sync(GTK_ICON_THEME(icon_theme->obj),
                                     thumbnail_factory,
                                     file_uri, custom_icon,
                                     flags, &result_flags);
    if (ret) {
        PyObject *py_ret = Py_BuildValue("si", ret, result_flags);
        g_free(ret);
        return py_ret;
    }
      /* The function should never return NULL; if it does, it's an internal bug */
    PyErr_SetString(PyExc_RuntimeError, "internal error");
    return NULL;
}
%%
override mate_appbar_new kwargs
static int
_wrap_mate_appbar_new(PyGObject *self, PyObject *args, PyObject *kwargs)
{
    GType obj_type = pyg_type_from_object((PyObject *) self);
    GParameter params[3];
    PyObject *parsed_args[3] = {NULL, };
    char *arg_names[] = {"has_progress", "has_status", "interactivity", NULL };
    char *prop_names[] = {"has_progress", "has_status", "interactivity", NULL };
    guint nparams, i;

      /* http://bugzilla.mate.org/show_bug.cgi?id=334836 */
    if ((args? PySequence_Size(args) : 0) + (kwargs? PyDict_Size(kwargs) : 0) < 3) {
        if (PyErr_Warn(PyExc_DeprecationWarning, "mate.ui.AppBar constructor takes exactly 3 arguments,"
                       " but calling old gtk.HBox constructor for compatibility reasons.") < 0)
            return -1;
        return PyGtkHBox_Type.tp_init((PyObject *) self, args, kwargs);
    }

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OOO:mate.ui.AppBar.__init__" , arg_names ,
                                     &parsed_args[0] , &parsed_args[1] , &parsed_args[2]))
        return -1;

    memset(params, 0, sizeof(GParameter)*3);
    if (!pyg_parse_constructor_args(obj_type, arg_names,
                                    prop_names, params, 
                                    &nparams, parsed_args))
        return -1;
    pygobject_constructv(self, nparams, params);
    for (i = 0; i < nparams; ++i)
        g_value_unset(&params[i].value);
    if (!self->obj) {
        PyErr_SetString(
            PyExc_RuntimeError, 
            "could not create mate.ui.AppBar object");
        return -1;
    }
    return 0;
}
