1 /* GtkFramePeer.java -- Implements FramePeer with GTK
2 Copyright (C) 1999, 2002, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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., 59 Temple Place, Suite 330, Boston, MA
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
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. */
39 package gnu
.java
.awt
.peer
.gtk
;
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
.PaintEvent
;
48 import java
.awt
.image
.ColorModel
;
49 import java
.awt
.peer
.FramePeer
;
50 import java
.awt
.peer
.MenuBarPeer
;
52 public class GtkFramePeer
extends GtkWindowPeer
55 private int menuBarHeight
;
56 private MenuBarPeer menuBar
;
57 native int getMenuBarHeight (MenuBarPeer bar
);
58 native void setMenuBarWidth (MenuBarPeer bar
, int width
);
59 native void setMenuBarPeer (MenuBarPeer bar
);
60 native void removeMenuBarPeer ();
61 native void gtkFixedSetVisible (boolean visible
);
63 int getMenuBarHeight ()
65 return menuBar
== null ?
0 : getMenuBarHeight (menuBar
);
68 public void setMenuBar (MenuBar bar
)
70 if (bar
== null && menuBar
!= null)
72 // We're removing the menubar.
73 gtkFixedSetVisible (false);
76 insets
.top
-= menuBarHeight
;
78 awtComponent
.validate ();
79 gtkFixedSetVisible (true);
81 else if (bar
!= null && menuBar
== null)
83 // We're adding a menubar where there was no menubar before.
84 gtkFixedSetVisible (false);
85 menuBar
= (MenuBarPeer
) ((MenuBar
) bar
).getPeer();
86 setMenuBarPeer (menuBar
);
88 awtComponent
.getWidth () - insets
.left
- insets
.right
;
90 setMenuBarWidth (menuBar
, menuBarWidth
);
91 menuBarHeight
= getMenuBarHeight ();
92 insets
.top
+= menuBarHeight
;
93 awtComponent
.validate ();
94 gtkFixedSetVisible (true);
96 else if (bar
!= null && menuBar
!= null)
98 // We're swapping the menubar.
99 gtkFixedSetVisible (false);
101 int oldHeight
= menuBarHeight
;
103 awtComponent
.getWidth () - insets
.left
- insets
.right
;
104 menuBar
= (MenuBarPeer
) ((MenuBar
) bar
).getPeer ();
105 setMenuBarPeer (menuBar
);
106 if (menuBarWidth
> 0)
107 setMenuBarWidth (menuBar
, menuBarWidth
);
108 menuBarHeight
= getMenuBarHeight ();
109 if (oldHeight
!= menuBarHeight
)
111 insets
.top
+= (menuBarHeight
- oldHeight
);
112 awtComponent
.validate ();
114 gtkFixedSetVisible (true);
118 public void setBounds (int x
, int y
, int width
, int height
)
120 int menuBarWidth
= width
- insets
.left
- insets
.right
;
121 if (menuBar
!= null && menuBarWidth
> 0)
122 setMenuBarWidth (menuBar
, menuBarWidth
);
124 nativeSetBounds (x
, y
,
125 width
- insets
.left
- insets
.right
,
126 height
- insets
.top
- insets
.bottom
130 public void setResizable (boolean resizable
)
132 // Call setSize; otherwise when resizable is changed from true to
133 // false the frame will shrink to the dimensions it had before it
135 setSize (awtComponent
.getWidth() - insets
.left
- insets
.right
,
136 awtComponent
.getHeight() - insets
.top
- insets
.bottom
138 gtkWindowSetResizable (resizable
);
141 protected void postInsetsChangedEvent (int top
, int left
,
142 int bottom
, int right
)
144 insets
.top
= top
+ menuBarHeight
;
146 insets
.bottom
= bottom
;
147 insets
.right
= right
;
150 public GtkFramePeer (Frame frame
)
157 // Create a normal decorated window.
158 create (GDK_WINDOW_TYPE_HINT_NORMAL
, true);
160 Frame frame
= (Frame
) awtComponent
;
162 setMenuBar (frame
.getMenuBar ());
164 setTitle (frame
.getTitle ());
165 gtkWindowSetResizable (frame
.isResizable ());
166 setIconImage(frame
.getIconImage());
169 native void nativeSetIconImageFromDecoder (GdkPixbufDecoder decoder
);
170 native void nativeSetIconImageFromData (int[] pixels
, int width
, int height
);
171 public void setIconImage (Image image
)
173 if (image
!= null && image
instanceof GtkImage
)
175 GtkImage img
= (GtkImage
) image
;
176 // FIXME: Image should be loaded, but if not, do image loading here.
179 if (img
.getSource() instanceof GdkPixbufDecoder
)
181 nativeSetIconImageFromDecoder((GdkPixbufDecoder
) img
.getSource());
185 int[] pixels
= img
.getPixelCache();
186 ColorModel model
= img
.getColorModel();
187 int[] data
= new int[pixels
.length
* 4];
188 for (int i
= 0; i
< pixels
.length
; i
++)
190 data
[i
* 4] = model
.getRed(pixels
[i
]);
191 data
[i
* 4 + 1] = model
.getGreen(pixels
[i
]);
192 data
[i
* 4 + 2] = model
.getBlue(pixels
[i
]);
193 data
[i
* 4 + 3] = model
.getAlpha(pixels
[i
]);
195 nativeSetIconImageFromData(data
, img
.getWidth(null), img
.getHeight(null));
201 public Graphics
getGraphics ()
204 if (GtkToolkit
.useGraphics2D ())
205 g
= new GdkGraphics2D (this);
207 g
= new GdkGraphics (this);
208 g
.translate (-insets
.left
, -insets
.top
);
212 protected void postConfigureEvent (int x
, int y
, int width
, int height
)
214 int frame_x
= x
- insets
.left
;
215 // Since insets.top includes the MenuBar height, we need to add back
216 // the MenuBar height to the frame's y position.
217 // If no MenuBar exists in this frame, the MenuBar height will be 0.
218 int frame_y
= y
- insets
.top
+ menuBarHeight
;
219 int frame_width
= width
+ insets
.left
+ insets
.right
;
220 // Ditto as above. Since insets.top already includes the MenuBar's height,
221 // we need to subtract the MenuBar's height from the top inset.
222 int frame_height
= height
+ insets
.top
+ insets
.bottom
- menuBarHeight
;
223 if (frame_x
!= awtComponent
.getX()
224 || frame_y
!= awtComponent
.getY()
225 || frame_width
!= awtComponent
.getWidth()
226 || frame_height
!= awtComponent
.getHeight())
228 if (frame_width
!= awtComponent
.getWidth() && menuBar
!= null
230 setMenuBarWidth (menuBar
, width
);
232 setBoundsCallback ((Window
) awtComponent
,
238 awtComponent
.validate();
242 protected void postMouseEvent(int id
, long when
, int mods
, int x
, int y
,
243 int clickCount
, boolean popupTrigger
)
245 super.postMouseEvent (id
, when
, mods
,
246 x
+ insets
.left
, y
+ insets
.top
,
247 clickCount
, popupTrigger
);
250 protected void postExposeEvent (int x
, int y
, int width
, int height
)
253 q().postEvent (new PaintEvent (awtComponent
, PaintEvent
.PAINT
,
254 new Rectangle (x
+ insets
.left
,
259 public int getState ()
264 public void setState (int state
)
269 public void setMaximizedBounds (Rectangle r
)