You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2009/11/02 17:35:49 UTC

svn commit: r831956 - in /incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin: TextAreaSkin.java terra/TerraTextInputSkin.java

Author: gbrown
Date: Mon Nov  2 16:35:49 2009
New Revision: 831956

URL: http://svn.apache.org/viewvc?rev=831956&view=rev
Log:
Ensure that selected and unselected text is only painted once in TextInput and TextArea.

Modified:
    incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkin.java
    incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTextInputSkin.java

Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkin.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkin.java?rev=831956&r1=831955&r2=831956&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkin.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkin.java Mon Nov  2 16:35:49 2009
@@ -1023,48 +1023,73 @@
                         RenderingHints.VALUE_FRACTIONALMETRICS_ON);
                 }
 
-                // Draw text
-                // TODO Draw selection using multiple clip rects
-                graphics.setFont(font);
-                graphics.setPaint(color);
-
                 LineMetrics lm = font.getLineMetrics("", FONT_RENDER_CONTEXT);
-                graphics.drawGlyphVector(glyphVector, 0, lm.getAscent());
+                float ascent = lm.getAscent();
 
-                // Draw selected characters using the selection color
+                graphics.setFont(font);
+
+                int selectionStart = textArea.getSelectionStart();
                 int selectionLength = textArea.getSelectionLength();
-                if (selectionLength > 0) {
-                    int selectionStart = textArea.getSelectionStart();
-                    Span selectionRange = new Span(selectionStart, selectionStart + selectionLength - 1);
+                Span selectionRange = new Span(selectionStart, selectionStart + selectionLength - 1);
 
-                    int documentOffset = getDocumentOffset();
-                    Span characterRange = new Span(documentOffset, documentOffset + getCharacterCount() - 1);
+                int documentOffset = getDocumentOffset();
+                Span characterRange = new Span(documentOffset, documentOffset + getCharacterCount() - 1);
 
-                    if (characterRange.intersects(selectionRange)) {
-                        int width = getWidth();
-                        int height = getHeight();
-
-                        int x0;
-                        if (selectionRange.start > characterRange.start) {
-                            Bounds leadingSelectionBounds = getCharacterBounds(selectionRange.start - documentOffset);
-                            x0 = leadingSelectionBounds.x;
-                        } else {
-                            x0 = 0;
-                        }
+                if (selectionLength > 0
+                    && characterRange.intersects(selectionRange)) {
+                    // Determine the selection bounds
+                    int width = getWidth();
+                    int height = getHeight();
 
-                        int x1;
-                        if (selectionRange.end < characterRange.end) {
-                            Bounds trailingSelectionBounds = getCharacterBounds(selectionRange.end - documentOffset);
-                            x1 = trailingSelectionBounds.x + trailingSelectionBounds.width;
-                        } else {
-                            x1 = width;
-                        }
+                    int x0;
+                    if (selectionRange.start > characterRange.start) {
+                        Bounds leadingSelectionBounds = getCharacterBounds(selectionRange.start - documentOffset);
+                        x0 = leadingSelectionBounds.x;
+                    } else {
+                        x0 = 0;
+                    }
 
-                        graphics.clipRect(x0, 0, x1 - x0, height);
-                        graphics.setColor(textArea.isFocused() &&
-                            textArea.isEditable() ? selectionColor : inactiveSelectionColor);
-                        graphics.drawGlyphVector(glyphVector, 0, lm.getAscent());
+                    int x1;
+                    if (selectionRange.end < characterRange.end) {
+                        Bounds trailingSelectionBounds = getCharacterBounds(selectionRange.end - documentOffset);
+                        x1 = trailingSelectionBounds.x + trailingSelectionBounds.width;
+                    } else {
+                        x1 = width;
                     }
+
+                    Rectangle selection = new Rectangle(x0, 0, x1 - x0, height);
+
+                    // Paint the unselected text
+                    Area unselectedArea = new Area();
+                    unselectedArea.add(new Area(new Rectangle(0, 0, width, height)));
+                    unselectedArea.subtract(new Area(selection));
+
+                    Graphics2D textGraphics = (Graphics2D)graphics.create();
+                    textGraphics.setColor(color);
+                    textGraphics.clip(unselectedArea);
+                    textGraphics.drawGlyphVector(glyphVector, 0, ascent);
+                    textGraphics.dispose();
+
+                    // Paint the selection
+                    Color selectionColor;
+                    if (textArea.isFocused()) {
+                        selectionColor = TextAreaSkin.this.selectionColor;
+                        selectionBackgroundColor = TextAreaSkin.this.selectionBackgroundColor;
+                    } else {
+                        selectionColor = inactiveSelectionColor;
+                        selectionBackgroundColor = inactiveSelectionBackgroundColor;
+                    }
+
+                    Graphics2D selectedTextGraphics = (Graphics2D)graphics.create();
+                    selectedTextGraphics.setColor(textArea.isFocused() &&
+                        textArea.isEditable() ? selectionColor : inactiveSelectionColor);
+                    selectedTextGraphics.clip(selection.getBounds());
+                    selectedTextGraphics.drawGlyphVector(glyphVector, 0, ascent);
+                    selectedTextGraphics.dispose();
+                } else {
+                    // Draw the text
+                    graphics.setColor(color);
+                    graphics.drawGlyphVector(glyphVector, 0, ascent);
                 }
             }
         }
@@ -1484,7 +1509,7 @@
             if (caret != null
                 && caretOn
                 && textArea.isFocused()) {
-                graphics.setPaint(textArea.isEditable() ? color : inactiveColor);
+                graphics.setColor(textArea.isEditable() ? color : inactiveColor);
                 graphics.fill(caret);
             }
         }

Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTextInputSkin.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTextInputSkin.java?rev=831956&r1=831955&r2=831956&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTextInputSkin.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTextInputSkin.java Mon Nov  2 16:35:49 2009
@@ -27,6 +27,7 @@
 import java.awt.font.FontRenderContext;
 import java.awt.font.GlyphVector;
 import java.awt.font.LineMetrics;
+import java.awt.geom.Area;
 import java.awt.geom.Rectangle2D;
 import java.text.CharacterIterator;
 import java.text.StringCharacterIterator;
@@ -351,20 +352,26 @@
                color = disabledColor;
             }
 
-            // TODO Paint the selection text using multiple, non-overlapping clip rects
-
             if (glyphVector != null) {
-                if (color != null) {
-                    graphics.setFont(font);
+                graphics.setFont(font);
+
+                if (selection == null) {
+                    // Paint the text
                     graphics.setColor(color);
                     graphics.drawGlyphVector(glyphVector, padding.left - scrollLeft + 1, padding.top + ascent + 1);
-                }
+                } else {
+                    // Paint the unselected text
+                    Area unselectedArea = new Area();
+                    unselectedArea.add(new Area(new Rectangle(0, 0, width, height)));
+                    unselectedArea.subtract(new Area(selection));
+
+                    Graphics2D textGraphics = (Graphics2D)graphics.create();
+                    textGraphics.setColor(color);
+                    textGraphics.clip(unselectedArea);
+                    textGraphics.drawGlyphVector(glyphVector, padding.left - scrollLeft + 1, padding.top + ascent + 1);
+                    textGraphics.dispose();
 
-                if (textInput.getSelectionLength() > 0) {
                     // Paint the selection
-                    Graphics2D selectionGraphics = (Graphics2D)graphics.create();
-                    selectionGraphics.clip(selection.getBounds());
-
                     Color selectionColor;
                     Color selectionBackgroundColor;
 
@@ -376,13 +383,14 @@
                         selectionBackgroundColor = inactiveSelectionBackgroundColor;
                     }
 
-                    selectionGraphics.setColor(selectionBackgroundColor);
-                    selectionGraphics.fill(selection);
-
-                    selectionGraphics.setColor(selectionColor);
-                    selectionGraphics.drawGlyphVector(glyphVector, padding.left - scrollLeft + 1, padding.top + ascent + 1);
+                    graphics.setColor(selectionBackgroundColor);
+                    graphics.fill(selection);
 
-                    selectionGraphics.dispose();
+                    Graphics2D selectedTextGraphics = (Graphics2D)graphics.create();
+                    selectedTextGraphics.setColor(selectionColor);
+                    selectedTextGraphics.clip(selection.getBounds());
+                    selectedTextGraphics.drawGlyphVector(glyphVector, padding.left - scrollLeft + 1, padding.top + ascent + 1);
+                    selectedTextGraphics.dispose();
                 }
             }