Chris@2: /* Chris@2: Copyright (C) 2001, 2006 by Simon Dixon Chris@2: Chris@2: This program is free software; you can redistribute it and/or modify Chris@2: it under the terms of the GNU General Public License as published by Chris@2: the Free Software Foundation; either version 2 of the License, or Chris@2: (at your option) any later version. Chris@2: Chris@2: This program is distributed in the hope that it will be useful, Chris@2: but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@2: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@2: GNU General Public License for more details. Chris@2: Chris@2: You should have received a copy of the GNU General Public License along Chris@2: with this program (the file gpl.txt); if not, download it from Chris@2: http://www.gnu.org/licenses/gpl.txt or write to the Chris@2: Free Software Foundation, Inc., Chris@2: 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Chris@2: */ Chris@2: Chris@2: package at.ofai.music.util; Chris@2: Chris@2: import java.awt.*; Chris@2: import javax.swing.*; Chris@2: Chris@2: /** Java has problems communicating with window managers like fvwm2. Chris@2: * This class is a workaround to find the size of borders of JFrames in Chris@2: * the current environment, so that programs can size their JFrames correctly. Chris@2: * Since we don't know how big borders are until after the window is created, Chris@2: * we create a dummy JFrame, query the size of its borders, and destroy it. Chris@2: * The values are saved for later calls to this class, so that the dummy JFrame Chris@2: * is only created once. Chris@2: */ Chris@2: public class FrameMargins { Chris@2: Chris@2: protected static Insets i = null; Chris@2: protected static Dimension insetsWithMenu = null; Chris@2: protected static Dimension insetsWithoutMenu = null; Chris@2: protected static Dimension topLeftWithMenu = null; Chris@2: protected static Dimension topLeftWithoutMenu = null; Chris@2: Chris@2: /** Returns the total size of the insets of a JFrame, that is the size of Chris@2: * the title bar, menu bar (if requested) and the borders of the JFrame. Chris@2: * In other words, the return value is the difference in size between the Chris@2: * JFrame itself and its content pane. Chris@2: * @param withMenuFlag indicates whether a menu bar should be included in Chris@2: * the calculations Chris@2: * @return the height and width of the insets of a JFrame, unless Chris@2: * getInsets() returns a ridiculously large value, in which Chris@2: * case we gracefully return a guess of (30,20). Chris@2: */ Chris@2: public static Dimension get(boolean withMenuFlag) { Chris@2: if (i == null) { Chris@2: JFrame f = new JFrame("Get size of window borders"); Chris@2: JMenuBar mb = new JMenuBar(); Chris@2: f.setJMenuBar(mb); Chris@2: mb.add(new JMenu("OK")); Chris@2: f.setVisible(true); Chris@2: i = f.getInsets(); Chris@2: f.dispose(); Chris@2: if ((i.left>100) || (i.right>100) || (i.top>100) || (i.bottom>100)){ Chris@2: i.left = 10; // Code around a bug in getInsets() Chris@2: i.right = 10; // - don't believe ridiculously high values Chris@2: i.top = 20; Chris@2: i.bottom = 10; Chris@2: } Chris@2: insetsWithMenu = new Dimension(i.left + i.right, Chris@2: i.top + i.bottom + mb.getHeight()); Chris@2: insetsWithoutMenu = new Dimension(i.left + i.right, i.top+i.bottom); Chris@2: topLeftWithoutMenu = new Dimension(i.left, i.top); Chris@2: topLeftWithMenu = new Dimension(i.left, i.top + mb.getHeight()); Chris@2: } Chris@2: return withMenuFlag? insetsWithMenu: insetsWithoutMenu; Chris@2: } // get() Chris@2: Chris@2: /** Returns the location of the content pane with respect to its JFrame. Chris@2: * @param withMenuFlag indicates whether a menu bar should be included in Chris@2: * the calculations Chris@2: * @return the x and y offsets of the top left corner of the content pane Chris@2: * from the top left corner of the JFrame Chris@2: */ Chris@2: public static Dimension getOrigin(boolean withMenuFlag) { Chris@2: if (i == null) Chris@2: get(withMenuFlag); Chris@2: return withMenuFlag? topLeftWithMenu: topLeftWithoutMenu; Chris@2: } // getOrigin() Chris@2: Chris@2: /** Returns the Insets object for a JFrame. Chris@2: * @return the Insets object measuring the size of the borders of a JFrame Chris@2: */ Chris@2: public static Insets getFrameInsets() { Chris@2: if (i == null) Chris@2: get(false); Chris@2: return i; Chris@2: } // getFrameInsets() Chris@2: Chris@2: } // class FrameMargins