Home | History | Annotate | Download | only in widgets
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.sdkuilib.internal.widgets;
     18 
     19 import com.android.sdkuilib.ui.GridDialog;
     20 
     21 import org.eclipse.jface.dialogs.IDialogConstants;
     22 import org.eclipse.swt.SWT;
     23 import org.eclipse.swt.events.SelectionAdapter;
     24 import org.eclipse.swt.events.SelectionEvent;
     25 import org.eclipse.swt.graphics.Rectangle;
     26 import org.eclipse.swt.widgets.Button;
     27 import org.eclipse.swt.widgets.Combo;
     28 import org.eclipse.swt.widgets.Composite;
     29 import org.eclipse.swt.widgets.Control;
     30 import org.eclipse.swt.widgets.Display;
     31 import org.eclipse.swt.widgets.Label;
     32 import org.eclipse.swt.widgets.Monitor;
     33 import org.eclipse.swt.widgets.Shell;
     34 
     35 /**
     36  * Small dialog to let a user choose a screen size (from a fixed list) and a resolution
     37  * (as returned by {@link Display#getMonitors()}).
     38 
     39  * After the dialog as returned, one can query {@link #getDensity()} to get the chosen monitor
     40  * pixel density.
     41  */
     42 public class ResolutionChooserDialog extends GridDialog {
     43     public final static float[] MONITOR_SIZES = new float[] {
     44             13.3f, 14, 15.4f, 15.6f, 17, 19, 20, 21, 24, 30,
     45     };
     46 
     47     private Button mButton;
     48     private Combo mScreenSizeCombo;
     49     private Combo mMonitorCombo;
     50 
     51     private Monitor[] mMonitors;
     52     private int mScreenSizeIndex = -1;
     53     private int mMonitorIndex = 0;
     54 
     55     public ResolutionChooserDialog(Shell parentShell) {
     56         super(parentShell, 2, false);
     57     }
     58 
     59     /**
     60      * Returns the pixel density of the user-chosen monitor.
     61      */
     62     public int getDensity() {
     63         float size = MONITOR_SIZES[mScreenSizeIndex];
     64         Rectangle rect = mMonitors[mMonitorIndex].getBounds();
     65 
     66         // compute the density
     67         double d = Math.sqrt(rect.width * rect.width + rect.height * rect.height) / size;
     68         return (int)Math.round(d);
     69     }
     70 
     71     @Override
     72     protected void configureShell(Shell newShell) {
     73         newShell.setText("Monitor Density");
     74         super.configureShell(newShell);
     75     }
     76 
     77     @Override
     78     protected Control createContents(Composite parent) {
     79         Control control = super.createContents(parent);
     80         mButton = getButton(IDialogConstants.OK_ID);
     81         mButton.setEnabled(false);
     82         return control;
     83     }
     84 
     85     @Override
     86     public void createDialogContent(Composite parent) {
     87         Label l = new Label(parent, SWT.NONE);
     88         l.setText("Screen Size:");
     89 
     90         mScreenSizeCombo = new Combo(parent, SWT.DROP_DOWN | SWT.READ_ONLY);
     91         for (float size : MONITOR_SIZES) {
     92             if (Math.round(size) == size) {
     93                 mScreenSizeCombo.add(String.format("%.0f\"", size));
     94             } else {
     95                 mScreenSizeCombo.add(String.format("%.1f\"", size));
     96             }
     97         }
     98         mScreenSizeCombo.addSelectionListener(new SelectionAdapter() {
     99             @Override
    100             public void widgetSelected(SelectionEvent arg0) {
    101                 mScreenSizeIndex = mScreenSizeCombo.getSelectionIndex();
    102                 mButton.setEnabled(mScreenSizeIndex != -1);
    103             }
    104         });
    105 
    106         l = new Label(parent, SWT.NONE);
    107         l.setText("Resolution:");
    108 
    109         mMonitorCombo = new Combo(parent, SWT.DROP_DOWN | SWT.READ_ONLY);
    110         mMonitors = parent.getDisplay().getMonitors();
    111         for (Monitor m : mMonitors) {
    112             Rectangle r = m.getBounds();
    113             mMonitorCombo.add(String.format("%d x %d", r.width, r.height));
    114         }
    115         mMonitorCombo.select(mMonitorIndex);
    116         mMonitorCombo.addSelectionListener(new SelectionAdapter() {
    117             @Override
    118             public void widgetDefaultSelected(SelectionEvent arg0) {
    119                 mMonitorIndex = mMonitorCombo.getSelectionIndex();
    120             }
    121         });
    122     }
    123 }
    124