1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.java.swing.plaf.windows;
  27 
  28 import java.awt.Color;
  29 import java.awt.Graphics;
  30 import java.awt.Rectangle;
  31 import java.awt.Shape;
  32 import javax.swing.plaf.basic.*;
  33 import javax.swing.*;
  34 import javax.swing.plaf.TextUI;
  35 import javax.swing.plaf.UIResource;
  36 import javax.swing.text.*;
  37 
  38 /**
  39  * Windows text rendering.
  40  * <p>
  41  * <strong>Warning:</strong>
  42  * Serialized objects of this class will not be compatible with
  43  * future Swing releases.  The current serialization support is appropriate
  44  * for short term storage or RMI between applications running the same
  45  * version of Swing.  A future release of Swing will provide support for
  46  * long term persistence.
  47  */
  48 public abstract class WindowsTextUI extends BasicTextUI {
  49     /**
  50      * Creates the object to use for a caret.  By default an
  51      * instance of WindowsCaret is created.  This method
  52      * can be redefined to provide something else that implements
  53      * the InputPosition interface or a subclass of DefaultCaret.
  54      *
  55      * @return the caret object
  56      */
  57     protected Caret createCaret() {
  58         return new WindowsCaret();
  59     }
  60 
  61     /* public */
  62     static LayeredHighlighter.LayerPainter WindowsPainter = new WindowsHighlightPainter(null);
  63 
  64     /* public */
  65     static class WindowsCaret extends DefaultCaret
  66                      implements UIResource {
  67         /**
  68          * Gets the painter for the Highlighter.
  69          *
  70          * @return the painter
  71          */
  72         protected Highlighter.HighlightPainter getSelectionPainter() {
  73             return WindowsTextUI.WindowsPainter;
  74         }
  75     }
  76 
  77     /* public */
  78     static class WindowsHighlightPainter extends
  79                      DefaultHighlighter.DefaultHighlightPainter {
  80         WindowsHighlightPainter(Color c) {
  81             super(c);
  82         }
  83 
  84         // --- HighlightPainter methods ---------------------------------------
  85 
  86         /**
  87          * Paints a highlight.
  88          *
  89          * @param g the graphics context
  90          * @param offs0 the starting model offset >= 0
  91          * @param offs1 the ending model offset >= offs1
  92          * @param bounds the bounding box for the highlight
  93          * @param c the editor
  94          */
  95         public void paint(Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c) {
  96             Rectangle alloc = bounds.getBounds();
  97             try {
  98                 // --- determine locations ---
  99                 TextUI mapper = c.getUI();
 100                 Rectangle p0 = mapper.modelToView(c, offs0);
 101                 Rectangle p1 = mapper.modelToView(c, offs1);
 102 
 103                 // --- render ---
 104                 Color color = getColor();
 105 
 106                 if (color == null) {
 107                     g.setColor(c.getSelectionColor());
 108                 }
 109                 else {
 110                     g.setColor(color);
 111                 }
 112                 boolean firstIsDot = false;
 113                 boolean secondIsDot = false;
 114                 if (c.isEditable()) {
 115                     int dot = c.getCaretPosition();
 116                     firstIsDot = (offs0 == dot);
 117                     secondIsDot = (offs1 == dot);
 118                 }
 119                 if (p0.y == p1.y) {
 120                     // same line, render a rectangle
 121                     Rectangle r = p0.union(p1);
 122                     if (r.width > 0) {
 123                         if (firstIsDot) {
 124                             r.x++;
 125                             r.width--;
 126                         }
 127                         else if (secondIsDot) {
 128                             r.width--;
 129                         }
 130                     }
 131                     g.fillRect(r.x, r.y, r.width, r.height);
 132                 } else {
 133                     // different lines
 134                     int p0ToMarginWidth = alloc.x + alloc.width - p0.x;
 135                     if (firstIsDot && p0ToMarginWidth > 0) {
 136                         p0.x++;
 137                         p0ToMarginWidth--;
 138                     }
 139                     g.fillRect(p0.x, p0.y, p0ToMarginWidth, p0.height);
 140                     if ((p0.y + p0.height) != p1.y) {
 141                         g.fillRect(alloc.x, p0.y + p0.height, alloc.width,
 142                                    p1.y - (p0.y + p0.height));
 143                     }
 144                     if (secondIsDot && p1.x > alloc.x) {
 145                         p1.x--;
 146                     }
 147                     g.fillRect(alloc.x, p1.y, (p1.x - alloc.x), p1.height);
 148                 }
 149             } catch (BadLocationException e) {
 150                 // can't render
 151             }
 152         }
 153 
 154         // --- LayerPainter methods ----------------------------
 155         /**
 156          * Paints a portion of a highlight.
 157          *
 158          * @param g the graphics context
 159          * @param offs0 the starting model offset >= 0
 160          * @param offs1 the ending model offset >= offs1
 161          * @param bounds the bounding box of the view, which is not
 162          *        necessarily the region to paint.
 163          * @param c the editor
 164          * @param view View painting for
 165          * @return region drawing occurred in
 166          */
 167         public Shape paintLayer(Graphics g, int offs0, int offs1,
 168                                 Shape bounds, JTextComponent c, View view) {
 169             Color color = getColor();
 170 
 171             if (color == null) {
 172                 g.setColor(c.getSelectionColor());
 173             }
 174             else {
 175                 g.setColor(color);
 176             }
 177             boolean firstIsDot = false;
 178             boolean secondIsDot = false;
 179             if (c.isEditable()) {
 180                 int dot = c.getCaretPosition();
 181                 firstIsDot = (offs0 == dot);
 182                 secondIsDot = (offs1 == dot);
 183             }
 184             if (offs0 == view.getStartOffset() &&
 185                 offs1 == view.getEndOffset()) {
 186                 // Contained in view, can just use bounds.
 187                 Rectangle alloc;
 188                 if (bounds instanceof Rectangle) {
 189                     alloc = (Rectangle)bounds;
 190                 }
 191                 else {
 192                     alloc = bounds.getBounds();
 193                 }
 194                 if (firstIsDot && alloc.width > 0) {
 195                     g.fillRect(alloc.x + 1, alloc.y, alloc.width - 1,
 196                                alloc.height);
 197                 }
 198                 else if (secondIsDot && alloc.width > 0) {
 199                     g.fillRect(alloc.x, alloc.y, alloc.width - 1,
 200                                alloc.height);
 201                 }
 202                 else {
 203                     g.fillRect(alloc.x, alloc.y, alloc.width, alloc.height);
 204                 }
 205                 return alloc;
 206             }
 207             else {
 208                 // Should only render part of View.
 209                 try {
 210                     // --- determine locations ---
 211                     Shape shape = view.modelToView(offs0, Position.Bias.Forward,
 212                                                    offs1,Position.Bias.Backward,
 213                                                    bounds);
 214                     Rectangle r = (shape instanceof Rectangle) ?
 215                                   (Rectangle)shape : shape.getBounds();
 216                     if (firstIsDot && r.width > 0) {
 217                         g.fillRect(r.x + 1, r.y, r.width - 1, r.height);
 218                     }
 219                     else if (secondIsDot && r.width > 0) {
 220                         g.fillRect(r.x, r.y, r.width - 1, r.height);
 221                     }
 222                     else {
 223                         g.fillRect(r.x, r.y, r.width, r.height);
 224                     }
 225                     return r;
 226                 } catch (BadLocationException e) {
 227                     // can't render
 228                 }
 229             }
 230             // Only if exception
 231             return null;
 232         }
 233 
 234     }
 235 
 236 }