Added functions to the BPy Text object for positioning the cursor and inserting text. It seems Text.write() actually inserts *then* moves to the end of the buffer, so it doesn't really append as it says in the docs. However, with these new functions both appending and inserting can be achieved.

This commit is contained in:
2008-06-17 19:26:26 +00:00
parent 48bf0ef2ed
commit 05ce388f35
4 changed files with 104 additions and 2 deletions

View File

@@ -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_bol (struct Text *text, short sel);
void txt_move_eol (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_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_pop_sel (struct Text *text);
void txt_delete_char (struct Text *text); void txt_delete_char (struct Text *text);
void txt_delete_word (struct Text *text); void txt_delete_word (struct Text *text);

View File

@@ -828,6 +828,11 @@ void txt_move_eof (Text *text, short sel)
} }
void txt_move_toline (Text *text, unsigned int line, 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; TextLine **linep, *oldl;
int *charp, oldc; int *charp, oldc;
@@ -845,10 +850,12 @@ void txt_move_toline (Text *text, unsigned int line, short sel)
if ((*linep)->next) *linep= (*linep)->next; if ((*linep)->next) *linep= (*linep)->next;
else break; else break;
} }
*charp= 0; if (ch>(*linep)->len)
ch= (*linep)->len;
*charp= ch;
if(!sel) txt_pop_sel(text); 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);
} }
/****************************/ /****************************/

View File

@@ -91,8 +91,11 @@ static PyObject *Text_getFilename( BPy_Text * self );
static PyObject *Text_getNLines( BPy_Text * self ); static PyObject *Text_getNLines( BPy_Text * self );
static PyObject *Text_clear( BPy_Text * self ); static PyObject *Text_clear( BPy_Text * self );
static PyObject *Text_write( BPy_Text * self, PyObject * value ); 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_set( BPy_Text * self, PyObject * args );
static PyObject *Text_asLines( BPy_Text * self ); 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: */ /* Python BPy_Text methods table: */
@@ -111,10 +114,16 @@ static PyMethodDef BPy_Text_methods[] = {
"() - Clear Text buffer"}, "() - Clear Text buffer"},
{"write", ( PyCFunction ) Text_write, METH_O, {"write", ( PyCFunction ) Text_write, METH_O,
"(line) - Append string 'str' to Text buffer"}, "(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, {"set", ( PyCFunction ) Text_set, METH_VARARGS,
"(name, val) - Set attribute 'name' to value 'val'"}, "(name, val) - Set attribute 'name' to value 'val'"},
{"asLines", ( PyCFunction ) Text_asLines, METH_NOARGS, {"asLines", ( PyCFunction ) Text_asLines, METH_NOARGS,
"() - Return text buffer as a list of lines"}, "() - 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} {NULL, NULL, 0, NULL}
}; };
@@ -416,6 +425,26 @@ static PyObject *Text_write( BPy_Text * self, PyObject * value )
Py_RETURN_NONE; 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 ) static PyObject *Text_asLines( BPy_Text * self )
{ {
TextLine *line; TextLine *line;
@@ -442,6 +471,46 @@ static PyObject *Text_asLines( BPy_Text * self )
return list; 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 */ /* Function: Text_compare */
/* Description: This is a callback function for the BPy_Text type. It */ /* Description: This is a callback function for the BPy_Text type. It */

View File

@@ -118,6 +118,13 @@ class Text:
@param data: The string to append to the text buffer. @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(): def asLines():
""" """
Retrieve the contents of this Text buffer as a list of strings. 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 @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 import id_generics
Text.__doc__ += id_generics.attributes Text.__doc__ += id_generics.attributes