#51 wip
[fanfix.git] / examples / JexerTilingWindowManager2.java
index cc23aa76f0840309361e879fa40d7916eecb5f69..accbf739317bf0212c1c7aa07c6fb2e934b84b11 100644 (file)
@@ -1,12 +1,12 @@
 import jexer.TAction;
 import jexer.TApplication;
 import jexer.TDesktop;
+import jexer.TPanel;
 import jexer.TTerminalWidget;
+import jexer.TSplitPane;
 import jexer.TWidget;
 import jexer.event.TKeypressEvent;
 import jexer.event.TMenuEvent;
-import jexer.event.TMouseEvent;
-import jexer.event.TResizeEvent;
 import jexer.menu.TMenu;
 
 /**
@@ -34,7 +34,7 @@ public class JexerTilingWindowManager2 extends TApplication {
      * Handle to the root widget.
      */
     private TWidget root = null;
-    
+
     /**
      * Main entry point.
      */
@@ -78,17 +78,7 @@ public class JexerTilingWindowManager2 extends TApplication {
         tileMenu.addDefaultItem(TMenu.MID_EXIT);
 
         // Spin up the root terminal
-        root = new TTerminalWidget(getDesktop(), 0, 0,
-            getDesktop().getWidth(), getDesktop().getHeight(),
-            new TAction() {
-                public void DO() {
-                    // TODO: if root's parent is TSplitPane, call
-                    // TSplitPane.removeSplit(TWidget).
-                    if (root != null) {
-                        root.remove();
-                    }
-                }
-            });
+        createRootTerminal();
     }
 
     /**
@@ -97,11 +87,59 @@ public class JexerTilingWindowManager2 extends TApplication {
     @Override
     protected boolean onMenu(TMenuEvent event) {
         if (event.getId() == MENU_SPLIT_VERTICAL) {
-            splitVertical();
+            if (root == null) {
+                assert (getDesktop().getActiveChild() == null);
+                createRootTerminal();
+                return true;
+            }
+            TWidget active = getDesktop().getActiveChild();
+            TSplitPane split = active.splitVertical(false,
+                new TTerminalWidget(active, active.getX(),
+                    active.getY(), active.getWidth(), active.getHeight(),
+                    new TAction() {
+                        public void DO() {
+                            if (source.getParent() instanceof TSplitPane) {
+                                ((TSplitPane) source.getParent()).removeSplit(source, true);
+                            } else if (source == root) {
+                                assert (root != null);
+                                root.remove();
+                                root = null;
+                            }
+                        }
+                    }));
+            if (active == root) {
+                root = split;
+            }
+            System.err.println("\nAfter vertical split:");
+            System.err.println(getDesktop().toPrettyString());
             return true;
         }
         if (event.getId() == MENU_SPLIT_HORIZONTAL) {
-            splitHorizontal();
+            if (root == null) {
+                assert (getDesktop().getActiveChild() == null);
+                createRootTerminal();
+                return true;
+            }
+            TWidget active = getDesktop().getActiveChild();
+            TSplitPane split = active.splitHorizontal(false,
+                new TTerminalWidget(active, active.getX(),
+                    active.getY(), active.getWidth(), active.getHeight(),
+                    new TAction() {
+                        public void DO() {
+                            if (source.getParent() instanceof TSplitPane) {
+                                ((TSplitPane) source.getParent()).removeSplit(source, true);
+                            } else if (source == root) {
+                                assert (root != null);
+                                root.remove();
+                                root = null;
+                            }
+                        }
+                    }));
+            if (active == root) {
+                root = split;
+            }
+            System.err.println("\nAfter horizontal split:");
+            System.err.println(getDesktop().toPrettyString());
             return true;
         }
 
@@ -109,17 +147,23 @@ public class JexerTilingWindowManager2 extends TApplication {
     }
 
     /**
-     * Perform the vertical split.
+     * Create the root terminal.
      */
-    private void splitVertical() {
-        // TODO
-    }
-
-    /**
-     * Perform the horizontal split.
-     */
-    private void splitHorizontal() {
-        // TODO
+    private void createRootTerminal() {
+        assert (root == null);
+        root = new TTerminalWidget(getDesktop(), 0, 0,
+            getDesktop().getWidth(), getDesktop().getHeight(),
+            new TAction() {
+                public void DO() {
+                    if (source.getParent() instanceof TSplitPane) {
+                        ((TSplitPane) source.getParent()).removeSplit(source, true);
+                    } else if (source == root) {
+                        assert (root != null);
+                        root.remove();
+                        root = null;
+                    }
+                }
+            });
     }
 
 }