]> git.neil.brown.name Git - edlib.git/commitdiff
python: bind python Pane more tightly to edlib pane
authorNeilBrown <neil@brown.name>
Mon, 25 Jan 2016 01:58:11 +0000 (12:58 +1100)
committerNeilBrown <neil@brown.name>
Mon, 25 Jan 2016 02:07:57 +0000 (13:07 +1100)
When python allocates a pane, the matching Pane is buond to it and
always used in python code (previously we would allocate various separate Panes).
This makes is possible to subclass Pane and when we get a pane back from
edlib, we get the whole subclassed Pane.

I should do a similar think with Marks....

Signed-off-by: NeilBrown <neil@brown.name>
lang-python.c
python/display-pygtk.py

index b6f95de6a02e28badf7c67693393fd390fcdca02..28d1eb539fb47b1c78f7e86b79eb8a1f5897cafd 100644 (file)
@@ -76,8 +76,14 @@ static int get_cmd_info(struct cmd_info *ci, PyObject *args, PyObject *kwds);
 
 static inline PyObject *Pane_Frompane(struct pane *p)
 {
-       Pane *pane = (Pane *)PyObject_CallObject((PyObject*)&PaneType, NULL);
-       pane->pane = p;
+       Pane *pane;
+       if (p && p->handle->func == python_call.func) {
+               pane = p->data;
+               Py_INCREF(pane);
+       } else {
+               pane = (Pane *)PyObject_CallObject((PyObject*)&PaneType, NULL);
+               pane->pane = p;
+       }
        return (PyObject*)pane;
 }
 
@@ -196,11 +202,10 @@ static int Pane_init(Pane *self, PyObject *args, PyObject *kwds)
 {
        Pane *parent;
        PyObject *py_handler;
-       PyObject *data;
        int z = 0;
        int ret;
        struct python_command *handler;
-       static char *keywords[] = {"parent", "handler", "data", "z", NULL};
+       static char *keywords[] = {"parent", "handler", "z", NULL};
 
        if (self->pane) {
                PyErr_SetString(PyExc_TypeError, "Pane already initialised");
@@ -211,8 +216,8 @@ static int Pane_init(Pane *self, PyObject *args, PyObject *kwds)
                /* Probably an internal Pane_Frompane call */
                return 1;
 
-       ret = PyArg_ParseTupleAndKeywords(args, kwds, "O!OO|i", keywords,
-                                         &PaneType, &parent, &py_handler, &data,
+       ret = PyArg_ParseTupleAndKeywords(args, kwds, "O!O|i", keywords,
+                                         &PaneType, &parent, &py_handler,
                                          &z);
        if (ret <= 0)
                return ret;
@@ -227,7 +232,7 @@ static int Pane_init(Pane *self, PyObject *args, PyObject *kwds)
        handler->callable = py_handler;
        handler->home_func = 1;
 
-       self->pane = pane_register(parent->pane, z, &handler->c, data, NULL);
+       self->pane = pane_register(parent->pane, z, &handler->c, self, NULL);
        return 0;
 }
 
@@ -420,7 +425,34 @@ static PyObject *Pane_rel(Pane *self, PyObject *args)
        return Py_BuildValue("ii", x, y);
 }
 
+static PyObject *Pane_close(Pane *self)
+{
+       struct pane *p = self->pane;
+       if (p) {
+               pane_close(p);
+               self->pane = NULL;
+       }
+       Py_INCREF(Py_None);
+       return Py_None;
+}
+
+static PyObject *Pane_release(Pane *self)
+{
+       struct pane *p = self->pane;
+       if (p && p->handle->func == python_call.func && p->data) {
+               p->data = NULL;
+               Py_DECREF(self);
+       }
+       self->pane = NULL;
+       Py_INCREF(Py_None);
+       return Py_None;
+}
+
 static PyMethodDef pane_methods[] = {
+       {"close", (PyCFunction)Pane_close, METH_NOARGS,
+        "close the pane"},
+       {"release", (PyCFunction)Pane_release, METH_NOARGS,
+        "pane is being closed, so release the Pane"},
        {"children", (PyCFunction)pane_children, METH_NOARGS,
         "provides and iterator which will iterate over all children"},
        {"take_focus", (PyCFunction)Pane_focus, METH_NOARGS,
index 4e718cd6570ff3c1c9370aad975042df65535e2f..b86b65450ced8df2400faeabee4d783be24fc958 100644 (file)
@@ -18,7 +18,7 @@ import glib
 class EdDisplay(gtk.Window):
     def __init__(self, home):
         gtk.Window.__init__(self)
-        self.pane = edlib.Pane(home, self.handle, None)
+        self.pane = edlib.Pane(home, self.handle)
         self.panes = {}
         self.set_title("EDLIB")
         self.connect('destroy', self.close_win)
@@ -29,6 +29,12 @@ class EdDisplay(gtk.Window):
         self.show()
 
     def handle(self, key, **a):
+
+        if key == "Close":
+            self.pane.close()
+            # FIXME close the window??
+            return True
+
         if key == "pane-clear":
             f = a["focus"]
             if "str2" in a: