jdk/src/solaris/classes/sun/awt/X11/XButtonPeer.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2002-2003 Sun Microsystems, Inc.  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.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 package sun.awt.X11;
       
    26 
       
    27 import java.awt.*;
       
    28 import java.awt.peer.*;
       
    29 import java.awt.event.MouseEvent;
       
    30 import java.awt.event.FocusEvent;
       
    31 import java.awt.event.KeyEvent;
       
    32 import java.awt.event.ActionEvent;
       
    33 import javax.swing.plaf.basic.*;
       
    34 import javax.swing.SwingUtilities;
       
    35 import javax.swing.SwingConstants;
       
    36 
       
    37 public class XButtonPeer extends XComponentPeer implements ButtonPeer {
       
    38 
       
    39     boolean pressed;
       
    40     boolean armed;
       
    41 
       
    42     private Insets focusInsets;
       
    43     private Insets borderInsets;
       
    44     private Insets contentAreaInsets;
       
    45 
       
    46     private final static String propertyPrefix = "Button" + ".";
       
    47     protected Color focusColor =  SystemColor.windowText;
       
    48 
       
    49     private boolean disposed = false;
       
    50 
       
    51     String label;
       
    52 
       
    53     protected String getPropertyPrefix() {
       
    54         return propertyPrefix;
       
    55     }
       
    56 
       
    57     void preInit(XCreateWindowParams params) {
       
    58         super.preInit(params);
       
    59         borderInsets = new Insets(2,2,2,2);
       
    60         focusInsets = new Insets(0,0,0,0);
       
    61         contentAreaInsets = new Insets(3,3,3,3);
       
    62     }
       
    63 
       
    64 
       
    65     public  XButtonPeer(Button target) {
       
    66         super(target);
       
    67         pressed = false;
       
    68         armed = false;
       
    69         label = target.getLabel();
       
    70         updateMotifColors(getPeerBackground());
       
    71     }
       
    72 
       
    73     public  void dispose() {
       
    74         synchronized (target)
       
    75         {
       
    76             disposed = true;
       
    77         }
       
    78         super.dispose();
       
    79     }
       
    80 
       
    81     public boolean isFocusable() {
       
    82         return true;
       
    83     }
       
    84 
       
    85     public void  setLabel(java.lang.String label) {
       
    86         this.label = label;
       
    87         repaint();
       
    88     }
       
    89 
       
    90     public void paint(Graphics g) {
       
    91         paint(g,target);
       
    92     }
       
    93 
       
    94     public void setBackground(Color c) {
       
    95         updateMotifColors(c);
       
    96         super.setBackground(c);
       
    97     }
       
    98 
       
    99     void handleJavaMouseEvent(MouseEvent e) {
       
   100         super.handleJavaMouseEvent(e);
       
   101         int id = e.getID();
       
   102         switch (id) {
       
   103           case MouseEvent.MOUSE_PRESSED:
       
   104               if (XToolkit.isLeftMouseButton(e) ) {
       
   105                   Button b = (Button) e.getSource();
       
   106 
       
   107                   if(b.contains(e.getX(), e.getY())) {
       
   108                       if (!isEnabled()) {
       
   109                           // Disabled buttons ignore all input...
       
   110                           return;
       
   111                       }
       
   112                       pressed = true;
       
   113                       armed = true;
       
   114                       repaint();
       
   115                   }
       
   116               }
       
   117 
       
   118               break;
       
   119 
       
   120           case MouseEvent.MOUSE_RELEASED:
       
   121               if (XToolkit.isLeftMouseButton(e)) {
       
   122                   if (armed)
       
   123                   {
       
   124                       action(e.getWhen(),e.getModifiers());
       
   125                   }
       
   126                   pressed = false;
       
   127                   armed = false;
       
   128                   repaint();
       
   129               }
       
   130 
       
   131               break;
       
   132 
       
   133           case  MouseEvent.MOUSE_ENTERED:
       
   134               if (pressed)
       
   135                   armed = true;
       
   136 //                 repaint();
       
   137 
       
   138               break;
       
   139 
       
   140           case MouseEvent.MOUSE_EXITED:
       
   141               armed = false;
       
   142 //                 repaint();
       
   143 
       
   144               break;
       
   145 
       
   146         }
       
   147     }
       
   148 
       
   149 
       
   150     // NOTE: This method is called by privileged threads.
       
   151     //       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
       
   152     public void action(final long when, final int modifiers) {
       
   153         postEvent(new ActionEvent(target, ActionEvent.ACTION_PERFORMED,
       
   154                                   ((Button)target).getActionCommand(),
       
   155                                   when, modifiers));
       
   156     }
       
   157 
       
   158 
       
   159     public void focusGained(FocusEvent e) {
       
   160         super.focusGained(e);
       
   161         repaint();
       
   162     }
       
   163 
       
   164     public void focusLost(FocusEvent e) {
       
   165         super.focusLost(e);
       
   166         repaint();
       
   167     }
       
   168 
       
   169     void handleJavaKeyEvent(KeyEvent e) {
       
   170         int id = e.getID();
       
   171         switch (id) {
       
   172           case KeyEvent.KEY_PRESSED:
       
   173               if (e.getKeyCode() == KeyEvent.VK_SPACE)
       
   174               {
       
   175                   pressed=true;
       
   176                   armed=true;
       
   177                   repaint();
       
   178                   action(e.getWhen(),e.getModifiers());
       
   179               }
       
   180 
       
   181               break;
       
   182 
       
   183           case KeyEvent.KEY_RELEASED:
       
   184               if (e.getKeyCode() == KeyEvent.VK_SPACE)
       
   185               {
       
   186                   pressed = false;
       
   187                   armed = false;
       
   188                   repaint();
       
   189               }
       
   190 
       
   191               break;
       
   192 
       
   193 
       
   194         }
       
   195     }
       
   196 
       
   197     public Dimension getMinimumSize() {
       
   198         FontMetrics fm = getFontMetrics(getPeerFont());
       
   199         if ( label == null ) {
       
   200             label = "";
       
   201         }
       
   202         return new Dimension(fm.stringWidth(label) + 14,
       
   203                              fm.getHeight() + 8);
       
   204     }
       
   205 
       
   206     /**
       
   207      * DEPRECATED
       
   208      */
       
   209     public Dimension minimumSize() {
       
   210         return getMinimumSize();
       
   211     }
       
   212 
       
   213 
       
   214     /*
       
   215        This method is called from Toolkit Thread and so it should not call any client code
       
   216 
       
   217     */
       
   218     public void paint(Graphics g, Component c)
       
   219     {
       
   220         if (!disposed && (g != null))
       
   221         {
       
   222             Dimension size = getPeerSize();
       
   223 
       
   224             g.setColor( getPeerBackground() );   /* erase the existing button remains */
       
   225             g.fillRect(0,0, size.width , size.height);
       
   226             paintBorder(g,borderInsets.left,
       
   227                         borderInsets.top,
       
   228                         size.width-(borderInsets.left+borderInsets.right),
       
   229                         size.height-(borderInsets.top+borderInsets.bottom));
       
   230 
       
   231             FontMetrics fm = g.getFontMetrics();
       
   232 
       
   233             Rectangle textRect,iconRect,viewRect;
       
   234 
       
   235             textRect = new Rectangle();
       
   236             viewRect = new Rectangle();
       
   237             iconRect = new Rectangle();
       
   238 
       
   239 
       
   240             viewRect.width = size.width - (contentAreaInsets.left+contentAreaInsets.right);
       
   241             viewRect.height = size.height - (contentAreaInsets.top+contentAreaInsets.bottom);
       
   242 
       
   243             viewRect.x = contentAreaInsets.left;
       
   244             viewRect.y = contentAreaInsets.right;
       
   245             String llabel = (label != null) ? label : "";
       
   246 
       
   247             // layout the text and icon
       
   248             String text = SwingUtilities.layoutCompoundLabel(
       
   249                                                              fm, llabel, null,
       
   250                                                              SwingConstants.CENTER, SwingConstants.CENTER,
       
   251                                                              SwingConstants.CENTER, SwingConstants.CENTER,
       
   252                                                              viewRect, iconRect, textRect, 0);
       
   253 
       
   254             Font f = getPeerFont();
       
   255 
       
   256             g.setFont(f);
       
   257 
       
   258             // perform UI specific press action, e.g. Windows L&F shifts text
       
   259             if (pressed && armed) {
       
   260                 paintButtonPressed(g,target);
       
   261             }
       
   262 
       
   263             paintText(g, target, textRect, text);
       
   264 
       
   265             if (hasFocus()) {
       
   266                 // paint UI specific focus
       
   267                 paintFocus(g,focusInsets.left,
       
   268                            focusInsets.top,
       
   269                            size.width-(focusInsets.left+focusInsets.right)-1,
       
   270                            size.height-(focusInsets.top+focusInsets.bottom)-1);
       
   271             }
       
   272         }
       
   273         flush();
       
   274     }
       
   275 
       
   276     public void paintBorder(Graphics g, int x, int y, int w, int h) {
       
   277         drawMotif3DRect(g, x, y, w-1, h-1, pressed);
       
   278     }
       
   279 
       
   280     public void setFont(Font f) {
       
   281         super.setFont(f);
       
   282         target.repaint();
       
   283     }
       
   284     protected void paintFocus(Graphics g, int x, int y, int w, int h){
       
   285         g.setColor(focusColor);
       
   286         g.drawRect(x,y,w,h);
       
   287     }
       
   288 
       
   289     protected void paintButtonPressed(Graphics g, Component b) {
       
   290         Dimension size = getPeerSize();
       
   291         g.setColor(selectColor);
       
   292         g.fillRect(contentAreaInsets.left,
       
   293                    contentAreaInsets.top,
       
   294                    size.width-(contentAreaInsets.left+contentAreaInsets.right),
       
   295                    size.height-(contentAreaInsets.top+contentAreaInsets.bottom));
       
   296 
       
   297     }
       
   298     protected void paintText(Graphics g, Component c, Rectangle textRect, String text) {
       
   299         FontMetrics fm = g.getFontMetrics();
       
   300 
       
   301         int mnemonicIndex = -1;
       
   302 
       
   303         /* Draw the Text */
       
   304         if(isEnabled()) {
       
   305             /*** paint the text normally */
       
   306             g.setColor(getPeerForeground());
       
   307             BasicGraphicsUtils.drawStringUnderlineCharAt(g,text,mnemonicIndex , textRect.x , textRect.y + fm.getAscent() );
       
   308         }
       
   309         else {
       
   310             /*** paint the text disabled ***/
       
   311             g.setColor(getPeerBackground().brighter());
       
   312 
       
   313             BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,
       
   314                                                          textRect.x, textRect.y + fm.getAscent());
       
   315             g.setColor(c.getBackground().darker());
       
   316             BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,
       
   317                                                          textRect.x - 1, textRect.y + fm.getAscent() - 1);
       
   318         }
       
   319     }
       
   320 }