1 /*
   2  * Copyright (c) 2002, 2011, 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 package sun.awt.X11;
  26 
  27 import java.awt.*;
  28 import java.awt.peer.*;
  29 
  30 import sun.awt.SunGraphicsCallback;
  31 
  32 public class XPanelPeer extends XCanvasPeer implements PanelPeer {
  33 
  34     XEmbeddingContainer embedder = null; //new XEmbeddingContainer();
  35     /**
  36      * Embeds the given window into container using XEmbed protocol
  37      */
  38     public void xembed(long window) {
  39         if (embedder != null) {
  40             embedder.add(window);
  41         }
  42     }
  43     XPanelPeer() {}
  44 
  45     XPanelPeer(XCreateWindowParams params) {
  46         super(params);
  47     }
  48 
  49     XPanelPeer(Component target) {
  50         super(target);
  51     }
  52 
  53     void postInit(XCreateWindowParams params) {
  54         super.postInit(params);
  55         if (embedder != null) {
  56             embedder.install(this);
  57         }
  58     }
  59 
  60     public Insets getInsets() {
  61         return new Insets(0, 0, 0, 0);
  62     }
  63     public void paint(Graphics g) {
  64         super.paint(g);
  65         SunGraphicsCallback.PaintHeavyweightComponentsCallback.getInstance().
  66             runComponents(((Container)target).getComponents(), g,
  67                           SunGraphicsCallback.LIGHTWEIGHTS |
  68                           SunGraphicsCallback.HEAVYWEIGHTS);
  69     }
  70     public void print(Graphics g) {
  71         super.print(g);
  72         SunGraphicsCallback.PrintHeavyweightComponentsCallback.getInstance().
  73             runComponents(((Container)target).getComponents(), g,
  74                           SunGraphicsCallback.LIGHTWEIGHTS |
  75                           SunGraphicsCallback.HEAVYWEIGHTS);
  76 
  77     }
  78 
  79     public void setBackground(Color c) {
  80         Component comp;
  81         int i;
  82 
  83         Container cont = (Container) target;
  84         synchronized(target.getTreeLock()) {
  85             int n = cont.getComponentCount();
  86             for(i=0; i < n; i++) {
  87                 comp = cont.getComponent(i);
  88                 ComponentPeer peer = comp.getPeer();
  89                 if (peer != null) {
  90                     Color color = comp.getBackground();
  91                     if (color == null || color.equals(c)) {
  92                         peer.setBackground(c);
  93                     }
  94                 }
  95             }
  96         }
  97         super.setBackground(c);
  98     }
  99 
 100     public void setForeground(Color c) {
 101         setForegroundForHierarchy((Container) target, c);
 102     }
 103 
 104     private void setForegroundForHierarchy(Container cont, Color c) {
 105         synchronized(target.getTreeLock()) {
 106             int n = cont.getComponentCount();
 107             for(int i=0; i < n; i++) {
 108                 Component comp = cont.getComponent(i);
 109                 Color color = comp.getForeground();
 110                 if (color == null || color.equals(c)) {
 111                     ComponentPeer cpeer = comp.getPeer();
 112                     if (cpeer != null) {
 113                         cpeer.setForeground(c);
 114                     }
 115                     if (cpeer instanceof LightweightPeer
 116                         && comp instanceof Container)
 117                     {
 118                         setForegroundForHierarchy((Container) comp, c);
 119                     }
 120                 }
 121             }
 122         }
 123     }
 124 
 125     /**
 126      * DEPRECATED:  Replaced by getInsets().
 127      */
 128     public Insets insets() {
 129         return getInsets();
 130     }
 131 
 132     public void dispose() {
 133         if (embedder != null) {
 134             embedder.deinstall();
 135         }
 136         super.dispose();
 137     }
 138 
 139     protected boolean shouldFocusOnClick() {
 140         // Return false if this container has children so in that case it won't
 141         // be focused. Return true otherwise.
 142         return ((Container)target).getComponentCount() == 0;
 143     }
 144 }