Ignore:
Timestamp:
2012-02-22T08:42:29+01:00 (12 years ago)
Author:
stoecker
Message:

better handling of dialogs for multiple screens

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/WindowGeometry.java

    r4965 r5015  
    2727    /**
    2828     * Replies a window geometry object for a window with a specific size which is
    29      * centered on screen
     29     * centered on screen, where main window is
    3030     *
    3131     * @param extent  the size
     
    3333     */
    3434    static public WindowGeometry centerOnScreen(Dimension extent) {
     35        return centerOnScreen(extent, "gui.geometry");
     36    }
     37
     38    /**
     39     * Replies a window geometry object for a window with a specific size which is
     40     * centered on screen where the corresponding window is.
     41     *
     42     * @param extent  the size
     43     * @param preferenceKey the key to get window size and position from, null value format
     44     * for whole virtual screen
     45     * @return the geometry object
     46     */
     47    static public WindowGeometry centerOnScreen(Dimension extent, String preferenceKey) {
     48        Rectangle size = preferenceKey != null ? getScreenInfo(preferenceKey)
     49            : getFullScreenInfo();
    3550        Point topLeft = new Point(
    36                 Math.max(0, (Toolkit.getDefaultToolkit().getScreenSize().width - extent.width) /2),
    37                 Math.max(0, (Toolkit.getDefaultToolkit().getScreenSize().height - extent.height) /2)
     51                size.x + Math.max(0, (size.width - extent.width) /2),
     52                size.y + Math.max(0, (size.height - extent.height) /2)
    3853        );
    3954        return new WindowGeometry(topLeft, extent);
     
    92107        this.topLeft = topLeft;
    93108        this.extent = extent;
     109    }
     110
     111    /**
     112     *
     113     * @param rect the position
     114     */
     115    public WindowGeometry(Rectangle rect) {
     116        this.topLeft = rect.getLocation();
     117        this.extent = rect.getSize();
    94118    }
    95119
     
    139163
    140164    static public WindowGeometry mainWindow(String preferenceKey, String arg, boolean maximize) {
    141         Dimension screenDimension = getScreenSize(null);
     165        Rectangle screenDimension = getScreenInfo("gui.geometry");
    142166        if (arg != null) {
    143167            final Matcher m = Pattern.compile("(\\d+)x(\\d+)(([+-])(\\d+)([+-])(\\d+))?").matcher(arg);
     
    145169                int w = Integer.valueOf(m.group(1));
    146170                int h = Integer.valueOf(m.group(2));
    147                 int x = 0, y = 0;
     171                int x = screenDimension.x, y = screenDimension.y;
    148172                if (m.group(3) != null) {
    149173                    x = Integer.valueOf(m.group(5));
    150174                    y = Integer.valueOf(m.group(7));
    151175                    if (m.group(4).equals("-")) {
    152                         x = screenDimension.width - x - w;
     176                        x = screenDimension.x + screenDimension.width - x - w;
    153177                    }
    154178                    if (m.group(6).equals("-")) {
    155                         y = screenDimension.height - y - h;
     179                        y = screenDimension.y + screenDimension.height - y - h;
    156180                    }
    157181                }
     
    163187        WindowGeometry def;
    164188        if(maximize)
    165             def = new WindowGeometry(new Point(0,0), screenDimension);
     189            def = new WindowGeometry(screenDimension);
    166190        else
    167             def = new WindowGeometry(new Point(0,0), new Dimension(1000, 740));
     191            def = new WindowGeometry(screenDimension.getLocation(), new Dimension(1000, 740));
    168192        return new WindowGeometry(preferenceKey, def);
    169193    }
     
    230254    }
    231255
    232     /**
    233      * Center window on screen. When preferenceKey is given, the window is centered
    234      * on the screen where the corresponding window is.
    235      *
    236      * @param window the window
    237      * @param preferenceKey the key to get size and position from
    238      */
    239     public static void centerOnScreen(Window window, String preferenceKey) {
    240         Dimension dim = getScreenSize(preferenceKey);
    241         Dimension size = window.getSize();
    242         window.setLocation(new Point((dim.width-size.width)/2,(dim.height-size.height)/2));
     256    private Rectangle getRectangle() {
     257        return new Rectangle(topLeft, extent);
    243258    }
    244259
     
    281296
    282297    /**
    283      * Find the size of the screen for given coordinates. Use first screen,
     298     * Find the size and position of the screen for given coordinates. Use first screen,
    284299     * when no coordinates are stored or null is passed.
    285300     *
    286301     * @param preferenceKey the key to get size and position from
    287302     */
    288     public static Dimension getScreenSize(String preferenceKey) {
     303    public static Rectangle getScreenInfo(String preferenceKey) {
     304        Rectangle g = new WindowGeometry(preferenceKey,
     305            /* default: something on screen 1 */
     306            new WindowGeometry(new Point(0,0), new Dimension(10,10))).getRectangle();
    289307        GraphicsEnvironment ge = GraphicsEnvironment
    290308                .getLocalGraphicsEnvironment();
    291309        GraphicsDevice[] gs = ge.getScreenDevices();
     310        int intersect = 0;
     311        Rectangle bounds = null;
    292312        for (int j = 0; j < gs.length; j++) {
    293313            GraphicsDevice gd = gs[j];
    294314            GraphicsConfiguration[] gc = gd.getConfigurations();
    295315            for (int i = 0; i < gc.length; i++) {
    296 //System.out.println("-- " + j + " " + i + " " + gc[i].getBounds());
     316                Rectangle b = gc[i].getBounds();
     317                Rectangle is = b.intersection(g);
     318                int s = is.width*is.height;
     319                if(bounds == null || intersect < s) {
     320                    intersect = s;
     321                    bounds = b;
     322                }
    297323            }
    298324        }
    299         /* TODO: implement this function properly */
    300         return Toolkit.getDefaultToolkit().getScreenSize();
     325        return bounds;
     326    }
     327
     328    /**
     329     * Find the size of the full virtual screen.
     330     */
     331    public static Rectangle getFullScreenInfo() {
     332        return new Rectangle(new Point(0,0), Toolkit.getDefaultToolkit().getScreenSize());
    301333    }
    302334
Note: See TracChangeset for help on using the changeset viewer.