/* PyGtkScintilla: Python wrapper for the GtkScintilla editing widget.
 *
 * Copyright (c) 2001  Michele Campeotto <micampe@f2s.com>
 *
 * 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 Library 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.
 */

#include <Python.h>

#include <gtk/gtk.h>
#include <gtkscintilla.h>

#include "pygtk.h"

static PyObject *_wrap_gtk_scintilla_add_text(PyObject *self, PyObject *args) {
    PyObject *sci;
    PyObject *string;
    char *text;
    int length;

    if (!PyArg_ParseTuple(args, "O!iS:gtk_scintilla_add_text", &PyGtk_Type, &sci, &length, &string))
        return NULL;
    text = PyString_AsString(string);
    length = PyString_Size(string);
    gtk_scintilla_add_text(GTK_SCINTILLA(PyGtk_Get(sci)), length, text);
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *_wrap_gtk_scintilla_set_text(PyObject *self, PyObject *args) {
    PyObject *sci;
    PyObject *string;
    char *text;
    int length;

    if (!PyArg_ParseTuple(args, "O!S:gtk_scintilla_set_text", &PyGtk_Type, &sci, &string))
        return NULL;
    text = PyString_AsString(string);
    length = PyString_Size(string);
    gtk_scintilla_clear_all(GTK_SCINTILLA(PyGtk_Get(sci)));
    gtk_scintilla_add_text(GTK_SCINTILLA(PyGtk_Get(sci)), length, text);
    gtk_scintilla_goto_line(GTK_SCINTILLA(PyGtk_Get(sci)), 0);
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *_wrap_gtk_scintilla_get_text(PyObject *self, PyObject *args)
{
    PyObject *sci;
    PyObject *txt;
    char *text_buffer = NULL;
    int length;

    if (!PyArg_ParseTuple(args, "O!:gtk_scintilla_get_text", &PyGtk_Type, &sci))
        return NULL;
    length = gtk_scintilla_get_length(GTK_SCINTILLA(PyGtk_Get(sci))) + 1;
    text_buffer = g_malloc0(length);
    if (text_buffer == NULL) {
        PyErr_NoMemory();
        Py_INCREF(Py_None);
        return Py_None;
    }
    gtk_scintilla_get_text(GTK_SCINTILLA(PyGtk_Get(sci)), length, text_buffer);
    txt = PyString_FromStringAndSize(text_buffer, length - 1);
    g_free(text_buffer);
    return txt;
}

static PyObject *_wrap_gtk_scintilla_get_line(PyObject *self, PyObject *args)
{
    PyObject *sci;
    PyObject *txt;
    char *text_buffer = NULL;
    int line, length;

    if (!PyArg_ParseTuple(args, "O!i:gtk_scintilla_get_line",
                          &PyGtk_Type, &sci, &line))
        return NULL;
    length = gtk_scintilla_line_length(GTK_SCINTILLA(PyGtk_Get(sci)), line) + 1;
    text_buffer = g_malloc0(length);
    if (text_buffer == NULL) {
        PyErr_NoMemory();
        Py_INCREF(Py_None);
        return Py_None;
    }
    gtk_scintilla_get_line(GTK_SCINTILLA(PyGtk_Get(sci)), line, text_buffer);
    txt = PyString_FromString(text_buffer);
    g_free(text_buffer);
    return txt;
}

static PyObject *_wrap_gtk_scintilla_get_sel_text(PyObject *self, PyObject *args)
{
    PyObject *sci;
    PyObject *txt;
    char *text_buffer = NULL;
    int length;

    if (!PyArg_ParseTuple(args, "O!:gtk_scintilla_get_sel_text", &PyGtk_Type, &sci))
        return NULL;
    length = gtk_scintilla_get_selection_end(GTK_SCINTILLA(PyGtk_Get(sci))) - gtk_scintilla_get_selection_start(GTK_SCINTILLA(PyGtk_Get(sci))) + 1;
    text_buffer = g_malloc0(length);
    if (text_buffer == NULL) {
        PyErr_NoMemory();
        Py_INCREF(Py_None);
        return Py_None;
    }
    gtk_scintilla_get_sel_text(GTK_SCINTILLA(PyGtk_Get(sci)), text_buffer);
    txt = PyString_FromString(text_buffer);
    g_free(text_buffer);
    return txt;
}

static PyObject *_wrap_gtk_scintilla_find_text(PyObject *self, PyObject *args)
{
    PyObject *sci;
    int flags;
    char *text = NULL;
    long chrg_min, chrg_max, text_min, text_max, find_pos;
    
    if (!PyArg_ParseTuple(args, "O!is(ll):gtk_scintilla_find_text",
        &PyGtk_Type, &sci, &flags, &text, &chrg_min, &chrg_max))
        return NULL;
    find_pos = gtk_scintilla_find_text(GTK_SCINTILLA(PyGtk_Get(sci)), flags, text, chrg_min, chrg_max, &text_min, &text_max);
    if (find_pos < 0) {
        Py_INCREF(Py_None);
        return Py_None;
    } else {
        return Py_BuildValue("(ll)", text_min, text_max);
    }
}

#include "gtkscintillamodule_impl.c"

/* These two functions are here only as a hack to make python-libglade
   understand i18n. They should be removed when the library will be fixed. */
static PyObject *_wrap_bindtextdomain(PyObject *self, PyObject *args)
{
    char *domain;
    char *localedir;
    
    if (!PyArg_ParseTuple(args, "ss", &domain, &localedir))
        return NULL;
    bindtextdomain(domain, localedir);
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *_wrap_textdomain(PyObject *self, PyObject *args)
{
    char *domain;
    
    if (!PyArg_ParseTuple(args, "s", &domain))
        return NULL;
    textdomain(domain);
    Py_INCREF(Py_None);
    return Py_None;
}

static PyMethodDef gtkscintillaMethods[] = {
    { "gtk_scintilla_add_text", _wrap_gtk_scintilla_add_text, 1 },
    { "gtk_scintilla_set_text", _wrap_gtk_scintilla_set_text, 1 },
    { "gtk_scintilla_get_text", _wrap_gtk_scintilla_get_text, 1 },
    { "gtk_scintilla_get_line", _wrap_gtk_scintilla_get_line, 1 },
    { "gtk_scintilla_get_sel_text", _wrap_gtk_scintilla_get_sel_text, 1 },
    { "gtk_scintilla_find_text", _wrap_gtk_scintilla_find_text, 1 },
#include "gtkscintillamodule_defs.c"
    { "bindtextdomain", _wrap_bindtextdomain, 1 },
    { "textdomain", _wrap_textdomain, 1 },
    { NULL, NULL, 0 }
};

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

    m = Py_InitModule("_gtkscintilla", gtkscintillaMethods);
    
    init_pygtk();

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