Merge branch 'subtree'
[fanfix.git] / src / jexer / TImage.java
index d1707074f8951d5d2775b94ada20e650fe9a886e..b7bfbd00a2ff165becc701575023b42e50b6deed 100644 (file)
@@ -30,19 +30,18 @@ package jexer;
 
 import java.awt.image.BufferedImage;
 
-import jexer.backend.ECMA48Terminal;
-import jexer.backend.MultiScreen;
-import jexer.backend.SwingTerminal;
 import jexer.bits.Cell;
+import jexer.event.TCommandEvent;
 import jexer.event.TKeypressEvent;
 import jexer.event.TMouseEvent;
 import jexer.event.TResizeEvent;
+import static jexer.TCommand.*;
 import static jexer.TKeypress.*;
 
 /**
  * TImage renders a piece of a bitmap image on screen.
  */
-public class TImage extends TWidget {
+public class TImage extends TWidget implements EditMenuUser {
 
     // ------------------------------------------------------------------------
     // Constants --------------------------------------------------------------
@@ -202,24 +201,12 @@ public class TImage extends TWidget {
         this.clickAction = clickAction;
 
         sizeToImage(true);
-
-        getApplication().addImage(this);
     }
 
     // ------------------------------------------------------------------------
     // Event handlers ---------------------------------------------------------
     // ------------------------------------------------------------------------
 
-    /**
-     * Subclasses should override this method to cleanup resources.  This is
-     * called by TWindow.onClose().
-     */
-    @Override
-    protected void close() {
-        getApplication().removeImage(this);
-        super.close();
-    }
-
     /**
      * Handle mouse press events.
      *
@@ -228,7 +215,7 @@ public class TImage extends TWidget {
     @Override
     public void onMouseDown(final TMouseEvent mouse) {
         if (clickAction != null) {
-            clickAction.DO();
+            clickAction.DO(this);
             return;
         }
     }
@@ -337,6 +324,20 @@ public class TImage extends TWidget {
         resized = true;
     }
 
+    /**
+     * Handle posted command events.
+     *
+     * @param command command event
+     */
+    @Override
+    public void onCommand(final TCommandEvent command) {
+        if (command.equals(cmCopy)) {
+            // Copy image to clipboard.
+            getClipboard().copyImage(image);
+            return;
+        }
+    }
+
     // ------------------------------------------------------------------------
     // TWidget ----------------------------------------------------------------
     // ------------------------------------------------------------------------
@@ -378,25 +379,8 @@ public class TImage extends TWidget {
      * @param always if true, always resize the cells
      */
     private void sizeToImage(final boolean always) {
-        int textWidth = 16;
-        int textHeight = 20;
-
-        if (getScreen() instanceof SwingTerminal) {
-            SwingTerminal terminal = (SwingTerminal) getScreen();
-
-            textWidth = terminal.getTextWidth();
-            textHeight = terminal.getTextHeight();
-        } if (getScreen() instanceof MultiScreen) {
-            MultiScreen terminal = (MultiScreen) getScreen();
-
-            textWidth = terminal.getTextWidth();
-            textHeight = terminal.getTextHeight();
-        } else if (getScreen() instanceof ECMA48Terminal) {
-            ECMA48Terminal terminal = (ECMA48Terminal) getScreen();
-
-            textWidth = terminal.getTextWidth();
-            textHeight = terminal.getTextHeight();
-        }
+        int textWidth = getScreen().getTextWidth();
+        int textHeight = getScreen().getTextHeight();
 
         if (image == null) {
             image = rotateImage(originalImage, clockwise);
@@ -438,8 +422,21 @@ public class TImage extends TWidget {
                     }
 
                     Cell cell = new Cell();
-                    cell.setImage(image.getSubimage(x * textWidth,
-                            y * textHeight, width, height));
+                    if ((width != textWidth) || (height != textHeight)) {
+                        BufferedImage newImage;
+                        newImage = new BufferedImage(textWidth, textHeight,
+                            BufferedImage.TYPE_INT_ARGB);
+
+                        java.awt.Graphics gr = newImage.getGraphics();
+                        gr.drawImage(image.getSubimage(x * textWidth,
+                                y * textHeight, width, height),
+                            0, 0, null, null);
+                        gr.dispose();
+                        cell.setImage(newImage);
+                    } else {
+                        cell.setImage(image.getSubimage(x * textWidth,
+                                y * textHeight, width, height));
+                    }
 
                     cells[x][y] = cell;
                 }
@@ -791,4 +788,44 @@ public class TImage extends TWidget {
         return newImage;
     }
 
+    // ------------------------------------------------------------------------
+    // EditMenuUser -----------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * Check if the cut menu item should be enabled.
+     *
+     * @return true if the cut menu item should be enabled
+     */
+    public boolean isEditMenuCut() {
+        return false;
+    }
+
+    /**
+     * Check if the copy menu item should be enabled.
+     *
+     * @return true if the copy menu item should be enabled
+     */
+    public boolean isEditMenuCopy() {
+        return true;
+    }
+
+    /**
+     * Check if the paste menu item should be enabled.
+     *
+     * @return true if the paste menu item should be enabled
+     */
+    public boolean isEditMenuPaste() {
+        return false;
+    }
+
+    /**
+     * Check if the clear menu item should be enabled.
+     *
+     * @return true if the clear menu item should be enabled
+     */
+    public boolean isEditMenuClear() {
+        return false;
+    }
+
 }