Revision: 4116
          http://vexi.svn.sourceforge.net/vexi/?rev=4116&view=rev
Author:   clrg
Date:     2011-05-09 20:13:23 +0000 (Mon, 09 May 2011)

Log Message:
-----------
Feature: setCursorCharIndex + test

Modified Paths:
--------------
    trunk/org.vexi-vexi.widgets/src_main/org/vexi/lib/text/edit.t

Added Paths:
-----------
    trunk/org.vexi-vexi.widgets/src_test/test/lib/
    trunk/org.vexi-vexi.widgets/src_test/test/lib/text/
    trunk/org.vexi-vexi.widgets/src_test/test/lib/text/edit.t

Modified: trunk/org.vexi-vexi.widgets/src_main/org/vexi/lib/text/edit.t
===================================================================
--- trunk/org.vexi-vexi.widgets/src_main/org/vexi/lib/text/edit.t       
2011-05-09 17:17:58 UTC (rev 4115)
+++ trunk/org.vexi-vexi.widgets/src_main/org/vexi/lib/text/edit.t       
2011-05-09 20:13:23 UTC (rev 4116)
@@ -165,7 +165,7 @@
         /** for maintaining the mBlock variable */
         var activeTrap = function(v) { cascade = v; mBlock = trapee; }
         
-        /** sync cursor for cBlock after reflow */
+        /** trap for synchronizing cursor position after cBlock reflow */
         var syncBlock = function(v) {
             cascade = v;
             if (trapee == cBlock) {
@@ -1712,6 +1712,15 @@
         thisbox.getCPos   = function() { return cPos; }
         thisbox.getSPos   = function() { return sPos; }
         
+        /*
+        thisbox.setCPos = function(v) { cPos = v; }
+        thisbox.setCWord = function(v) { cWord = v; }
+        thisbox.setCBlock = function(v) { cBlock = v; }
+        thisbox.setSPos = function(v) { sPos = v; }
+        thisbox.setSWord = function(v) { sWord = v; }
+        thisbox.setSBlock = function(v) { sBlock = v; }
+        */
+        
         thisbox.getCursorCharIndex = function() {
             var blockInd = thisbox.indexof(cBlock);
             var wordInd = cWord ? cBlock.indexof(cWord) : -1;
@@ -1732,14 +1741,40 @@
             return cWord ? cindex+cPos : cindex;
         }
         
-        /*
-        thisbox.setCPos = function(v) { cPos = v; }
-        thisbox.setCWord = function(v) { cWord = v; }
-        thisbox.setCBlock = function(v) { cBlock = v; }
-        thisbox.setSPos = function(v) { sPos = v; }
-        thisbox.setSWord = function(v) { sWord = v; }
-        thisbox.setSBlock = function(v) { sBlock = v; }
-        */
+        thisbox.setCusrorCharIndex = function(index) {
+            var curindex = 0;
+            var numblocks = numchildren;
+            for (var i=0; numblocks>i; i++) {
+                var b = thisbox[i];
+                var numwords = b.numchildren;
+                for (var j=0; numwords>j; j++) {
+                    var addindex = b[j].length;
+                    if (index > addindex + curindex) {
+                        curindex += b[j].length;
+                    } else {
+                        cBlock = b;
+                        cWord = b[j];
+                        cPos = index - curindex;
+                        syncCursorAndOffset(true);
+                        return;
+                    }
+                }
+                // don't forget the carriage return \n
+                if (index == curindex) {
+                    cBlock = b;
+                    cWord = b[numwords-1];
+                    cPos = cWord.text.length;
+                    syncCursorAndOffset(true);
+                    return;
+                }
+                // *kah-ching!*
+            }
+            // reached the end of valid text, place cursor there
+            cBlock = thisbox[numblocks-1];
+            cWord = cBlock.numchildren ? cBlock[cBlock.numchildren-1] : null;
+            cPos = cWord ? cWord.text.length : 0;
+            syncCursorAndOffset(true);
+        }
         
         ///////////////////////////////////////////////////////////////
         //////// PROPERTY TRAPS ///////////////////////////////////////

Added: trunk/org.vexi-vexi.widgets/src_test/test/lib/text/edit.t
===================================================================
--- trunk/org.vexi-vexi.widgets/src_test/test/lib/text/edit.t                   
        (rev 0)
+++ trunk/org.vexi-vexi.widgets/src_test/test/lib/text/edit.t   2011-05-09 
20:13:23 UTC (rev 4116)
@@ -0,0 +1,51 @@
+<vexi xmlns:meta="vexi://meta" xmlns:ui="vexi://ui" xmlns="org.vexi.lib.text">
+    <meta:doc>
+        <author>Charles Goodwin</author>
+    </meta:doc>
+
+    /// Quick Suite
+    var suite = {};
+
+    var vunit = vexi..vexi.test.vunit;
+    var assertEq = vunit..assertEq;
+
+    suite.text_read_write = function() {
+        var b = .edit(vexi.box);
+        b.text = "1.00";
+        assertEq("1.00", b.text);
+        b.text = "sausage";
+        assertEq("sausage", b.text);
+    };
+    
+    suite.getCursorPosition = function() {
+        var b1 = .edit(vexi.box);
+        b1.text = "sausage";
+        b1.width = 200;
+        assertEq(0, b1.getCursorPosition());
+        b1.KeyPressed = "a";
+        b1.KeyPressed = " ";
+        assertEq(2, b1.getCursorPosition());
+        b1.KeyPressed = "arrow_right";
+        b1.KeyPressed = "arrow_right";
+        assertEq(4, b1.getCursorPosition());
+        b1.KeyPressed = "end";
+        assertEq(9, b1.getCursorPosition());
+    };
+
+    suite.setCursorPosition = function() {
+        var b1 = .edit(vexi.box);
+        b1.text = "sausage and beans";
+        b1.setCusrorPosition(3);
+        assertEq(3, b1.getCursorPosition());
+        b1.setCusrorPosition(7);
+        assertEq(7, b1.getCursorPosition());
+        b1.setCusrorPosition(10);
+        assertEq(10, b1.getCursorPosition());
+        b1.setCusrorPosition(17);
+        assertEq(17, b1.getCursorPosition());
+    };    
+
+    static.test = suite;
+
+    <ui:box />
+</vexi>
\ No newline at end of file


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network 
management toolset available today.  Delivers lowest initial 
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd
_______________________________________________
Vexi-svn mailing list
Vexi-svn@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/vexi-svn

Reply via email to