Revision: 2327
          http://vexi.svn.sourceforge.net/vexi/?rev=2327&view=rev
Author:   clrg
Date:     2007-09-24 15:51:19 -0700 (Mon, 24 Sep 2007)

Log Message:
-----------
Refactor lib.widget.tree and fix a few bugs at the same time

Modified Paths:
--------------
    trunk/widgets/org.vexi.widgets/src/org/vexi/lib/widget/tree.t

Modified: trunk/widgets/org.vexi.widgets/src/org/vexi/lib/widget/tree.t
===================================================================
--- trunk/widgets/org.vexi.widgets/src/org/vexi/lib/widget/tree.t       
2007-09-24 22:39:09 UTC (rev 2326)
+++ trunk/widgets/org.vexi.widgets/src/org/vexi/lib/widget/tree.t       
2007-09-24 22:51:19 UTC (rev 2327)
@@ -17,80 +17,148 @@
         thisbox.v_isroot = true;
         thisbox.v_self ++= function() { return thisbox; }
         
-        var flipDisplayFunc = function(v) { th_expand.display = 
!th_expand.display; cascade = v; }
+        thisbox.expanded = true;
         
-        thisbox.th_content ++= function(v) {
-            cascade = v;
-            th_content.Children ++= function(c) {
-                if (c and c.v_istree) {
-                    c.v_isroot = false;
-                    if (groupref) {
-                        var prevtree = th_content[arguments.trapname];
-                        // easy case 1: insert into a group of nodes
-                        if (prevtree) prevtree = 
groupref.members.before(prevtree);
-                        // easy case 2: no nodes in this tree
-                        else if (!th_content.numchildren) prevtree = thisbox;
-                        // need to find last node (nodes may be trees)
-                        else {
-                            var node = thisbox;
-                            prevtree = thisbox;
-                            for (var i=thisbox.numchildren-1; i>=0; i--) {
-                               if (node[i].v_istree) {
-                                   prevtree = node[i].v_self;
-                                   if (node[i].numchildren) {
-                                       node = node[i];
-                                       // reset i, no -1 due to for loop i--
-                                       i = node.numchildren;
-                                   } else break;
-                               }
-                            }
+        var icontemplate = null;
+        
+        /** used for closing / expanding the tree */
+        thisbox.flipDisplay = function(v) { expanded = !th_expand.display; 
cascade = v; }
+        
+        /** select when we mouse press on the node */
+        thisbox.nodePress = function(v) { selected = true; cascade = v; }
+        
+        /** used for fancier icon implementations */
+        thisbox.setIconTemplate = function(i) { icontemplate = i; }
+        
+        thisbox.childrenWrite = function(c) {
+            if (c and c.v_istree) {
+                var cself = c.v_self;
+                // we have no group
+                if (!groupref) {
+                    // add to child group
+                    if (cself.groupref) {
+                        groupref = cself.groupref;
+                        groupref.members.unshift(thisbox);
+                    // start a new group
+                    } else c.group = group;
+                // we have a group - insert or merge into this
+                } else {
+                    var insafter = null;
+                    // no children, insert after thisbox
+                    if (!th_content.numchildren) insafter = thisbox;
+                    // try the box under the current placement
+                    else {
+                        insafter = th_content[trapname];
+                        if (insafter) insafter = 
groupref.members.before(insafter);
+                    }
+                    // need to find last node (nodes may be trees)
+                    if (!insafter) {
+                        var node = thisbox;
+                        insafter = thisbox;
+                        for (var i=thisbox.numchildren-1; i>=0; i--) {
+                           if (node[i].v_istree) {
+                               insafter = node[i].v_self;
+                               if (node[i].numchildren) {
+                                   node = node[i];
+                                   // reset i, no -1 due to for loop i--
+                                   i = node.numchildren;
+                               } else break;
+                           }
                         }
-                        // merge groups
-                        if (c.v_self.groupref)
-                            groupref.merge(c.v_self.groupref, prevtree);
-                        // c has no group - insert it
-                        else {
-                            c.v_self.groupref = groupref;
-                            groupref.members.insert(c.v_self, prevtree);
-                        }
-                    } else {
-                        if (c.v_self.groupref) {
-                            groupref = c.v_self.groupref;
-                            groupref.members.unshift(thisbox);
-                        } else c.group = group;
                     }
+                    // now position [insafter] is known
+                    
+                    // merge groups
+                    if (c.v_self.groupref)
+                        groupref.merge(c.v_self.groupref, insafter);
+                    // c has no group - insert it
+                    else {
+                        c.v_self.groupref = groupref;
+                        groupref.members.insert(c.v_self, insafter);
+                    }
                 }
-                var _c = trapee[arguments.trapname];
-                if (c == null and _c != null) {
-                    _c.v_isroot = true;
-                    if (_c.v_istree) _c.group = null;
+            }
+            var _c = trapee[trapname];
+            if (c == null and _c != null) {
+                // remove from this tree
+                if (_c.v_istree) {
+                    // FIXME: split groups
+                    _c.v_self.groupref = null;
+                    _c.v_self.nexttree --= nexttreeChildRead;
+                    _c.v_self.prevtree --= prevtreeChildRead;
                 }
-                cascade = c;
-                th_handle.display = (th_content.numchildren > 0);
             }
+            cascade = c;
+            th_handle.display = (th_content.numchildren > 0);
         }
         
-        thisbox.th_handle ++= function(v) { cascade = v; v.Press1 ++= 
flipDisplayFunc; }
+        thisbox.expanded   ++= static.expandRead;
+        thisbox.expanded   ++= static.expandWrite;
+        thisbox.nextselect ++= static.nextselectRead;
+        thisbox.prevselect ++= static.prevselectRead;
+        thisbox.th_content ++= static.contentWrite;
+        thisbox.th_handle  ++= static.handleWrite;
+        thisbox.th_node    ++= static.nodeWrite;
+        thisbox.th_title   ++= static.titleWrite;
+        thisbox.KeyPressed ++= static.keypressWrite;
         
-        thisbox.th_title ++= function(v) { cascade = v; v.DoubleClick1 ++= 
flipDisplayFunc; }
-        
-        thisbox.KeyPressed ++= function(v) {
-            if (v == "left") th_expand.display = false;
-            else if (v == "right") { if (th_content.numchildren) 
th_expand.display = true; }
-            else cascade = v;
+    </ui:box>
+    
+    static.expandRead = function() { return trapee.th_expand.display; }
+    static.expandWrite = function(v) {
+        var t = trapee;
+        t.th_expand.display = v;
+        if (!v) {
+            var g = t.groupref;
+            if (g and g.selected and !g.selected.visible)
+                t.selected = true;
         }
+        return;
+    }
+    
+    static.nextselectRead = function() {
+        var t = trapee;
+        var c = cascade;
+        if (t.groupref.members.first == c)
+            return t;
+        return c;
+    }
+    
+    static.prevselectRead = function() {
+        var t = trapee;
+        if (t.groupref.members.first == t)
+            return t;
+        return cascade;
+    }
+    
+    static.contentWrite = function(v) {
+        cascade = v;
+        v.Children ++= trapee.childrenWrite;
+    }
         
-        /** override action write trap in selectable */
-        thisbox.th_node ++= function(n) {
-            cascade = n;
-            n.Press1 ++= function(v) { selected = true; cascade = v; }
-        }
-        
-        /** prevents looping when selecting down the tree */
-        thisbox.nextselect ++= function() { var c = cascade; return c.v_isroot 
? thisbox : c; }
-        
-        /** prevents looping when selecting up the tree */
-        thisbox.prevselect ++= function() { if (v_isroot) return thisbox; else 
return cascade; }
-        
-    </ui:box>
+    static.handleWrite = function(v) {
+        cascade = v;
+        v.Press1 ++= trapee.flipDisplay;
+    }
+    
+    /** override action write trap in selectable */
+    static.nodeWrite = function(n) {
+        cascade = n;
+        n.Press1 ++= trapee.nodePress;
+    }
+    
+    static.titleWrite = function(v) {
+        cascade = v;
+        v.DoubleClick1 ++= trapee.flipDisplay;
+    }
+    
+    static.keypressWrite = function(v) {
+        var t = trapee;
+        if (v == "left") t.th_expand.display = false;
+        else if (v == "right") {
+            if (t.th_content.numchildren)
+                t.th_expand.display = true;
+        } else cascade = v;
+    }
+    
 </vexi>


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

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Vexi-svn mailing list
Vexi-svn@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/vexi-svn

Reply via email to