1 /*
2  * Copyright (c) 2002, 2014, 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.util.*;
28 import java.awt.*;
29 import java.awt.peer.*;
30 import java.awt.event.*;
31 import sun.awt.AWTAccessor;
32 
33 import sun.awt.*;
34 
35 class XDialogPeer extends XDecoratedPeer implements DialogPeer {
36 
37     private Boolean undecorated;
38 
XDialogPeer(Dialog target)39     XDialogPeer(Dialog target) {
40         super(target);
41     }
42 
preInit(XCreateWindowParams params)43     public void preInit(XCreateWindowParams params) {
44         super.preInit(params);
45 
46         Dialog target = (Dialog)(this.target);
47         undecorated = Boolean.valueOf(target.isUndecorated());
48         winAttr.nativeDecor = !target.isUndecorated();
49         if (winAttr.nativeDecor) {
50             winAttr.decorations = XWindowAttributesData.AWT_DECOR_ALL;
51         } else {
52             winAttr.decorations = XWindowAttributesData.AWT_DECOR_NONE;
53         }
54         winAttr.functions = MWMConstants.MWM_FUNC_ALL;
55         winAttr.isResizable =  true; //target.isResizable();
56         winAttr.initialResizability =  target.isResizable();
57         winAttr.title = target.getTitle();
58         winAttr.initialState = XWindowAttributesData.NORMAL;
59     }
60 
setVisible(boolean vis)61     public void setVisible(boolean vis) {
62         XToolkit.awtLock();
63         try {
64             Dialog target = (Dialog)this.target;
65             if (vis) {
66                 if (target.getModalityType() != Dialog.ModalityType.MODELESS) {
67                     if (!isModalBlocked()) {
68                         XBaseWindow.ungrabInput();
69                     }
70                 }
71             } else {
72                 restoreTransientFor(this);
73                 prevTransientFor = null;
74                 nextTransientFor = null;
75             }
76         } finally {
77             XToolkit.awtUnlock();
78         }
79 
80         super.setVisible(vis);
81     }
82 
83     @Override
isTargetUndecorated()84     boolean isTargetUndecorated() {
85         if (undecorated != null) {
86             return undecorated.booleanValue();
87         } else {
88             return ((Dialog)target).isUndecorated();
89         }
90     }
91 
getDecorations()92     int getDecorations() {
93         int d = super.getDecorations();
94         // remove minimize and maximize buttons for dialogs
95         if ((d & MWMConstants.MWM_DECOR_ALL) != 0) {
96             d |= (MWMConstants.MWM_DECOR_MINIMIZE | MWMConstants.MWM_DECOR_MAXIMIZE);
97         } else {
98             d &= ~(MWMConstants.MWM_DECOR_MINIMIZE | MWMConstants.MWM_DECOR_MAXIMIZE);
99         }
100         return d;
101     }
102 
getFunctions()103     int getFunctions() {
104         int f = super.getFunctions();
105         // remove minimize and maximize functions for dialogs
106         if ((f & MWMConstants.MWM_FUNC_ALL) != 0) {
107             f |= (MWMConstants.MWM_FUNC_MINIMIZE | MWMConstants.MWM_FUNC_MAXIMIZE);
108         } else {
109             f &= ~(MWMConstants.MWM_FUNC_MINIMIZE | MWMConstants.MWM_FUNC_MAXIMIZE);
110         }
111         return f;
112     }
113 
blockWindows(java.util.List<Window> toBlock)114     public void blockWindows(java.util.List<Window> toBlock) {
115         Vector<XWindowPeer> javaToplevels = null;
116         XToolkit.awtLock();
117         try {
118             javaToplevels = XWindowPeer.collectJavaToplevels();
119             for (Window w : toBlock) {
120                 XWindowPeer wp = AWTAccessor.getComponentAccessor().getPeer(w);
121                 if (wp != null) {
122                     wp.setModalBlocked((Dialog)target, true, javaToplevels);
123                 }
124             }
125         } finally {
126             XToolkit.awtUnlock();
127         }
128     }
129 
130     /*
131      * WARNING: don't call client code in this method!
132      *
133      * The check is performed before the dialog is shown.
134      * The focused window can't be blocked at the time it's focused.
135      * Thus we don't have to perform any transitive (a blocker of a blocker) checks.
136      */
isFocusedWindowModalBlocker()137     boolean isFocusedWindowModalBlocker() {
138         Window focusedWindow = XKeyboardFocusManagerPeer.getInstance().getCurrentFocusedWindow();
139         XWindowPeer focusedWindowPeer = null;
140 
141         if (focusedWindow != null) {
142             focusedWindowPeer = AWTAccessor.getComponentAccessor().getPeer(focusedWindow);
143         } else {
144             /*
145              * For the case when a potential blocked window is not yet focused
146              * on the Java level (e.g. it's just been mapped) we're asking for the
147              * focused window on the native level.
148              */
149             focusedWindowPeer = getNativeFocusedWindowPeer();
150         }
151         synchronized (getStateLock()) {
152             if (focusedWindowPeer != null && focusedWindowPeer.modalBlocker == target) {
153                 return true;
154             }
155         }
156         return super.isFocusedWindowModalBlocker();
157     }
158 }
159