diff options
-rw-r--r-- | source/blender/blenkernel/BKE_text.h | 1 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/text.c | 11 | ||||
-rw-r--r-- | source/blender/python/api2_2x/Text.c | 69 | ||||
-rw-r--r-- | source/blender/python/api2_2x/doc/Text.py | 25 |
4 files changed, 104 insertions, 2 deletions
diff --git a/source/blender/blenkernel/BKE_text.h b/source/blender/blenkernel/BKE_text.h index 45b4034549f..10223589859 100644 --- a/source/blender/blenkernel/BKE_text.h +++ b/source/blender/blenkernel/BKE_text.h @@ -66,6 +66,7 @@ void txt_move_eof (struct Text *text, short sel); void txt_move_bol (struct Text *text, short sel); void txt_move_eol (struct Text *text, short sel); void txt_move_toline (struct Text *text, unsigned int line, short sel); +void txt_move_to (struct Text *text, unsigned int line, unsigned int ch, short sel); void txt_pop_sel (struct Text *text); void txt_delete_char (struct Text *text); void txt_delete_word (struct Text *text); diff --git a/source/blender/blenkernel/intern/text.c b/source/blender/blenkernel/intern/text.c index 1c7b505752f..fbb94289166 100644 --- a/source/blender/blenkernel/intern/text.c +++ b/source/blender/blenkernel/intern/text.c @@ -829,6 +829,11 @@ void txt_move_eof (Text *text, short sel) void txt_move_toline (Text *text, unsigned int line, short sel) { + txt_move_to(text, line, 0, sel); +} + +void txt_move_to (Text *text, unsigned int line, unsigned int ch, short sel) +{ TextLine **linep, *oldl; int *charp, oldc; unsigned int i; @@ -845,10 +850,12 @@ void txt_move_toline (Text *text, unsigned int line, short sel) if ((*linep)->next) *linep= (*linep)->next; else break; } - *charp= 0; + if (ch>(*linep)->len) + ch= (*linep)->len; + *charp= ch; if(!sel) txt_pop_sel(text); - if(!undoing) txt_undo_add_toop(text, sel?UNDO_STO:UNDO_CTO, txt_get_span(text->lines.first, oldl), oldc, txt_get_span(text->lines.first, *linep), (unsigned short)*charp); + if(!undoing) txt_undo_add_toop(text, sel?UNDO_STO:UNDO_CTO, txt_get_span(text->lines.first, oldl), oldc, txt_get_span(text->lines.first, *linep), (unsigned short)*charp); } /****************************/ diff --git a/source/blender/python/api2_2x/Text.c b/source/blender/python/api2_2x/Text.c index 308ad094c7b..603deb768ad 100644 --- a/source/blender/python/api2_2x/Text.c +++ b/source/blender/python/api2_2x/Text.c @@ -91,8 +91,11 @@ static PyObject *Text_getFilename( BPy_Text * self ); static PyObject *Text_getNLines( BPy_Text * self ); static PyObject *Text_clear( BPy_Text * self ); static PyObject *Text_write( BPy_Text * self, PyObject * value ); +static PyObject *Text_insert( BPy_Text * self, PyObject * value ); static PyObject *Text_set( BPy_Text * self, PyObject * args ); static PyObject *Text_asLines( BPy_Text * self ); +static PyObject *Text_getCursorPos( BPy_Text * self ); +static PyObject *Text_setCursorPos( BPy_Text * self, PyObject * args ); /*****************************************************************************/ /* Python BPy_Text methods table: */ @@ -111,10 +114,16 @@ static PyMethodDef BPy_Text_methods[] = { "() - Clear Text buffer"}, {"write", ( PyCFunction ) Text_write, METH_O, "(line) - Append string 'str' to Text buffer"}, + {"insert", ( PyCFunction ) Text_insert, METH_O, + "(line) - Insert string 'str' to Text buffer at cursor location"}, {"set", ( PyCFunction ) Text_set, METH_VARARGS, "(name, val) - Set attribute 'name' to value 'val'"}, {"asLines", ( PyCFunction ) Text_asLines, METH_NOARGS, "() - Return text buffer as a list of lines"}, + {"getCursorPos", ( PyCFunction ) Text_getCursorPos, METH_NOARGS, + "() - Return cursor position as (row, col) tuple"}, + {"setCursorPos", ( PyCFunction ) Text_setCursorPos, METH_VARARGS, + "(row, col) - Set the cursor position to (row, col)"}, {NULL, NULL, 0, NULL} }; @@ -416,6 +425,26 @@ static PyObject *Text_write( BPy_Text * self, PyObject * value ) Py_RETURN_NONE; } +static PyObject *Text_insert( BPy_Text * self, PyObject * value ) +{ + char *str = PyString_AsString(value); + int oldstate; + + if( !self->text ) + return EXPP_ReturnPyObjError( PyExc_RuntimeError, + "This object isn't linked to a Blender Text Object" ); + + if( !str ) + return EXPP_ReturnPyObjError( PyExc_TypeError, + "expected string argument" ); + + oldstate = txt_get_undostate( ); + txt_insert_buf( self->text, str ); + txt_set_undostate( oldstate ); + + Py_RETURN_NONE; +} + static PyObject *Text_asLines( BPy_Text * self ) { TextLine *line; @@ -442,6 +471,46 @@ static PyObject *Text_asLines( BPy_Text * self ) return list; } +static PyObject *Text_getCursorPos( BPy_Text * self ) +{ + Text *text; + TextLine *linep; + int row, col; + + text = self->text; + if( !text ) + return EXPP_ReturnPyObjError( PyExc_RuntimeError, + "This object isn't linked to a Blender Text Object" ); + + for (row=0,linep=text->lines.first; linep!=text->curl; linep=linep->next) + row++; + col= text->curc; + + return Py_BuildValue( "ii", row, col ); +} + +static PyObject *Text_setCursorPos( BPy_Text * self, PyObject * args ) +{ + int row, col; + int oldstate; + + if(!self->text) + return EXPP_ReturnPyObjError(PyExc_RuntimeError, + "This object isn't linked to a Blender Text Object"); + + if (!PyArg_ParseTuple(args, "ii", &row, &col)) + return EXPP_ReturnPyObjError(PyExc_TypeError, + "expected two ints as arguments."); + if (col<0) col=0; + if (col>self->text->curl->len) col=self->text->curl->len; + + oldstate = txt_get_undostate(); + txt_move_to(self->text, row, col, 0); + txt_set_undostate(oldstate); + + Py_RETURN_NONE; +} + /*****************************************************************************/ /* Function: Text_compare */ /* Description: This is a callback function for the BPy_Text type. It */ diff --git a/source/blender/python/api2_2x/doc/Text.py b/source/blender/python/api2_2x/doc/Text.py index 98ecb664b71..4099b13828d 100644 --- a/source/blender/python/api2_2x/doc/Text.py +++ b/source/blender/python/api2_2x/doc/Text.py @@ -118,6 +118,13 @@ class Text: @param data: The string to append to the text buffer. """ + def insert(data): + """ + Inserts a string into this Text buffer at the cursor. + @type data: string + @param data: The string to insert into the text buffer. + """ + def asLines(): """ Retrieve the contents of this Text buffer as a list of strings. @@ -125,5 +132,23 @@ class Text: @return: A list of strings, one for each line in the buffer """ + def getCursorPos(): + """ + Retrieve the position of the cursor in this Text buffer. + @rtype: (int, int) + @return: A pair (row, col) indexing the line and character of the + cursor. + """ + + def setCursorPos(row, col): + """ + Set the position of the cursor in this Text buffer. + @type row: int + @param row: The index of the line in which to position the cursor. + @type col: int + @param col: The index of the character within the line to position the + cursor. + """ + import id_generics Text.__doc__ += id_generics.attributes
\ No newline at end of file |