Source code: gnu/java/awt/peer/gtk/GtkFramePeer.java
1 /* GtkFramePeer.java -- Implements FramePeer with GTK
2 Copyright (C) 1999, 2002, 2004 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
37
38
39 package gnu.java.awt.peer.gtk;
40
41 import java.awt.Frame;
42 import java.awt.Graphics;
43 import java.awt.Image;
44 import java.awt.MenuBar;
45 import java.awt.Rectangle;
46 import java.awt.Window;
47 import java.awt.event.ComponentEvent;
48 import java.awt.event.PaintEvent;
49 import java.awt.image.ColorModel;
50 import java.awt.peer.FramePeer;
51 import java.awt.peer.MenuBarPeer;
52
53 public class GtkFramePeer extends GtkWindowPeer
54 implements FramePeer
55 {
56 private int menuBarHeight;
57 private MenuBarPeer menuBar;
58 native int getMenuBarHeight (MenuBarPeer bar);
59 native void setMenuBarWidthUnlocked (MenuBarPeer bar, int width);
60 native void setMenuBarWidth (MenuBarPeer bar, int width);
61 native void setMenuBarPeer (MenuBarPeer bar);
62 native void removeMenuBarPeer ();
63 native void gtkFixedSetVisible (boolean visible);
64
65 int getMenuBarHeight ()
66 {
67 return menuBar == null ? 0 : getMenuBarHeight (menuBar);
68 }
69
70 public void setMenuBar (MenuBar bar)
71 {
72 if (bar == null && menuBar != null)
73 {
74 // We're removing the menubar.
75 gtkFixedSetVisible (false);
76 menuBar = null;
77 removeMenuBarPeer ();
78 insets.top -= menuBarHeight;
79 menuBarHeight = 0;
80 awtComponent.validate ();
81 gtkFixedSetVisible (true);
82 }
83 else if (bar != null && menuBar == null)
84 {
85 // We're adding a menubar where there was no menubar before.
86 gtkFixedSetVisible (false);
87 menuBar = (MenuBarPeer) ((MenuBar) bar).getPeer();
88 setMenuBarPeer (menuBar);
89 int menuBarWidth =
90 awtComponent.getWidth () - insets.left - insets.right;
91 if (menuBarWidth > 0)
92 setMenuBarWidth (menuBar, menuBarWidth);
93 menuBarHeight = getMenuBarHeight ();
94 insets.top += menuBarHeight;
95 awtComponent.validate ();
96 gtkFixedSetVisible (true);
97 }
98 else if (bar != null && menuBar != null)
99 {
100 // We're swapping the menubar.
101 gtkFixedSetVisible (false);
102 removeMenuBarPeer();
103 int oldHeight = menuBarHeight;
104 int menuBarWidth =
105 awtComponent.getWidth () - insets.left - insets.right;
106 menuBar = (MenuBarPeer) ((MenuBar) bar).getPeer ();
107 setMenuBarPeer (menuBar);
108 if (menuBarWidth > 0)
109 setMenuBarWidth (menuBar, menuBarWidth);
110 menuBarHeight = getMenuBarHeight ();
111 if (oldHeight != menuBarHeight)
112 {
113 insets.top += (menuBarHeight - oldHeight);
114 awtComponent.validate ();
115 }
116 gtkFixedSetVisible (true);
117 }
118 }
119
120 public void setBounds (int x, int y, int width, int height)
121 {
122 // prevent window_configure_cb -> awtComponent.setSize ->
123 // peer.setBounds -> nativeSetBounds self-deadlock on GDK lock.
124 if (Thread.currentThread() == GtkToolkit.mainThread)
125 {
126 int menuBarWidth = width - insets.left - insets.right;
127 if (menuBar != null && menuBarWidth > 0)
128 setMenuBarWidthUnlocked (menuBar, menuBarWidth);
129
130 return;
131 }
132
133 int menuBarWidth = width - insets.left - insets.right;
134 if (menuBar != null && menuBarWidth > 0)
135 setMenuBarWidth (menuBar, menuBarWidth);
136
137 nativeSetBounds (x, y,
138 width - insets.left - insets.right,
139 height - insets.top - insets.bottom
140 + menuBarHeight);
141 }
142
143 public void setResizable (boolean resizable)
144 {
145 // Call setSize; otherwise when resizable is changed from true to
146 // false the frame will shrink to the dimensions it had before it
147 // was resizable.
148 setSize (awtComponent.getWidth() - insets.left - insets.right,
149 awtComponent.getHeight() - insets.top - insets.bottom
150 + menuBarHeight);
151 gtkWindowSetResizable (resizable);
152 }
153
154 protected void postInsetsChangedEvent (int top, int left,
155 int bottom, int right)
156 {
157 insets.top = top + menuBarHeight;
158 insets.left = left;
159 insets.bottom = bottom;
160 insets.right = right;
161 }
162
163 public GtkFramePeer (Frame frame)
164 {
165 super (frame);
166 }
167
168 void create ()
169 {
170 // Create a normal decorated window.
171 create (GDK_WINDOW_TYPE_HINT_NORMAL, true);
172
173 Frame frame = (Frame) awtComponent;
174
175 setMenuBar (frame.getMenuBar ());
176
177 setTitle (frame.getTitle ());
178 gtkWindowSetResizable (frame.isResizable ());
179 setIconImage(frame.getIconImage());
180 }
181
182 native void nativeSetIconImage (GtkImage image);
183
184 public void setIconImage (Image image)
185 {
186 if (image != null)
187 {
188 if (image instanceof GtkImage)
189 nativeSetIconImage((GtkImage) image);
190 else
191 nativeSetIconImage(new GtkImage(image.getSource()));
192 }
193 }
194
195 public Graphics getGraphics ()
196 {
197 Graphics g;
198 if (GtkToolkit.useGraphics2D ())
199 g = new GdkGraphics2D (this);
200 else
201 g = new GdkGraphics (this);
202 g.translate (-insets.left, -insets.top);
203 return g;
204 }
205
206 protected void postConfigureEvent (int x, int y, int width, int height)
207 {
208 int frame_width = width + insets.left + insets.right;
209 // Since insets.top already includes the MenuBar's height, we need
210 // to subtract the MenuBar's height from the top inset.
211 int frame_height = height + insets.top + insets.bottom - menuBarHeight;
212
213 if (frame_width != awtComponent.getWidth()
214 || frame_height != awtComponent.getHeight())
215 awtComponent.setSize(frame_width, frame_height);
216
217 int frame_x = x - insets.left;
218 // Likewise, since insets.top includes the MenuBar height, we need
219 // to add back the MenuBar height to the frame's y position. If
220 // no MenuBar exists in this frame, the MenuBar height will be 0.
221 int frame_y = y - insets.top + menuBarHeight;
222
223 if (frame_x != awtComponent.getX()
224 || frame_y != awtComponent.getY())
225 {
226 // awtComponent.setLocation(frame_x, frame_y);
227 }
228 }
229
230 protected void postMouseEvent(int id, long when, int mods, int x, int y,
231 int clickCount, boolean popupTrigger)
232 {
233 super.postMouseEvent (id, when, mods,
234 x + insets.left, y + insets.top,
235 clickCount, popupTrigger);
236 }
237
238 protected void postExposeEvent (int x, int y, int width, int height)
239 {
240 if (!isInRepaint)
241 q().postEvent (new PaintEvent (awtComponent, PaintEvent.PAINT,
242 new Rectangle (x + insets.left,
243 y + insets.top,
244 width, height)));
245 }
246
247 public int getState ()
248 {
249 return 0;
250 }
251
252 public void setState (int state)
253 {
254
255 }
256
257 public void setMaximizedBounds (Rectangle r)
258 {
259
260 }
261 public void setBoundsPrivate(int x, int y, int width, int height)
262 {
263 // TODO Auto-generated method stub
264
265 }
266 public void updateAlwaysOnTop()
267 {
268 // TODO Auto-generated method stub
269
270 }
271 public boolean requestWindowFocus()
272 {
273 // TODO Auto-generated method stub
274 return false;
275 }
276 }
277
278