[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [15254] branches/soc-2008-quorn/source/ blender: Added functions to the BPy Text object for positioning the cursor and inserting text .

Ian Thompson quornian at googlemail.com
Tue Jun 17 21:26:48 CEST 2008


Revision: 15254
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=15254
Author:   quorn
Date:     2008-06-17 21:26:26 +0200 (Tue, 17 Jun 2008)

Log Message:
-----------
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.

Modified Paths:
--------------
    branches/soc-2008-quorn/source/blender/blenkernel/BKE_text.h
    branches/soc-2008-quorn/source/blender/blenkernel/intern/text.c
    branches/soc-2008-quorn/source/blender/python/api2_2x/Text.c
    branches/soc-2008-quorn/source/blender/python/api2_2x/doc/Text.py

Modified: branches/soc-2008-quorn/source/blender/blenkernel/BKE_text.h
===================================================================
--- branches/soc-2008-quorn/source/blender/blenkernel/BKE_text.h	2008-06-17 19:00:21 UTC (rev 15253)
+++ branches/soc-2008-quorn/source/blender/blenkernel/BKE_text.h	2008-06-17 19:26:26 UTC (rev 15254)
@@ -66,6 +66,7 @@
 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);

Modified: branches/soc-2008-quorn/source/blender/blenkernel/intern/text.c
===================================================================
--- branches/soc-2008-quorn/source/blender/blenkernel/intern/text.c	2008-06-17 19:00:21 UTC (rev 15253)
+++ branches/soc-2008-quorn/source/blender/blenkernel/intern/text.c	2008-06-17 19:26:26 UTC (rev 15254)
@@ -829,6 +829,11 @@
 
 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 @@
 		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);
 }
 
 /****************************/

Modified: branches/soc-2008-quorn/source/blender/python/api2_2x/Text.c
===================================================================
--- branches/soc-2008-quorn/source/blender/python/api2_2x/Text.c	2008-06-17 19:00:21 UTC (rev 15253)
+++ branches/soc-2008-quorn/source/blender/python/api2_2x/Text.c	2008-06-17 19:26:26 UTC (rev 15254)
@@ -91,8 +91,11 @@
 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 @@
 	 "() - 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 @@
 	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 @@
 	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        */

Modified: branches/soc-2008-quorn/source/blender/python/api2_2x/doc/Text.py
===================================================================
--- branches/soc-2008-quorn/source/blender/python/api2_2x/doc/Text.py	2008-06-17 19:00:21 UTC (rev 15253)
+++ branches/soc-2008-quorn/source/blender/python/api2_2x/doc/Text.py	2008-06-17 19:26:26 UTC (rev 15254)
@@ -118,6 +118,13 @@
 		@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 @@
 		@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





More information about the Bf-blender-cvs mailing list