Source for gnu.java.awt.peer.qt.QtToolkit

   1: /* QtToolkit.java --
   2:    Copyright (C)  2005, 2006, 2007  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: package gnu.java.awt.peer.qt;
  39: 
  40: import gnu.java.awt.ClasspathToolkit;
  41: import gnu.java.awt.EmbeddedWindow;
  42: import gnu.java.awt.peer.ClasspathFontPeer;
  43: import gnu.java.awt.peer.EmbeddedWindowPeer;
  44: 
  45: import java.awt.AWTException;
  46: import java.awt.Button;
  47: import java.awt.Canvas;
  48: import java.awt.Checkbox;
  49: import java.awt.CheckboxMenuItem;
  50: import java.awt.Choice;
  51: import java.awt.Dialog;
  52: import java.awt.Dimension;
  53: import java.awt.EventQueue;
  54: import java.awt.FileDialog;
  55: import java.awt.Font;
  56: import java.awt.FontMetrics;
  57: import java.awt.Frame;
  58: import java.awt.GraphicsDevice;
  59: import java.awt.GraphicsEnvironment;
  60: import java.awt.Image;
  61: import java.awt.Label;
  62: import java.awt.List;
  63: import java.awt.Menu;
  64: import java.awt.MenuBar;
  65: import java.awt.MenuItem;
  66: import java.awt.Panel;
  67: import java.awt.PopupMenu;
  68: import java.awt.PrintJob;
  69: import java.awt.ScrollPane;
  70: import java.awt.Scrollbar;
  71: import java.awt.TextArea;
  72: import java.awt.TextField;
  73: import java.awt.Window;
  74: import java.awt.datatransfer.Clipboard;
  75: import java.awt.dnd.DragGestureEvent;
  76: import java.awt.dnd.peer.DragSourceContextPeer;
  77: import java.awt.event.AWTEventListener;
  78: import java.awt.im.InputMethodHighlight;
  79: import java.awt.image.ColorModel;
  80: import java.awt.image.DirectColorModel;
  81: import java.awt.image.ImageObserver;
  82: import java.awt.image.ImageProducer;
  83: import java.awt.peer.ButtonPeer;
  84: import java.awt.peer.CanvasPeer;
  85: import java.awt.peer.CheckboxMenuItemPeer;
  86: import java.awt.peer.CheckboxPeer;
  87: import java.awt.peer.ChoicePeer;
  88: import java.awt.peer.DialogPeer;
  89: import java.awt.peer.FileDialogPeer;
  90: import java.awt.peer.FontPeer;
  91: import java.awt.peer.FramePeer;
  92: import java.awt.peer.LabelPeer;
  93: import java.awt.peer.ListPeer;
  94: import java.awt.peer.MenuBarPeer;
  95: import java.awt.peer.MenuItemPeer;
  96: import java.awt.peer.MenuPeer;
  97: import java.awt.peer.PanelPeer;
  98: import java.awt.peer.PopupMenuPeer;
  99: import java.awt.peer.RobotPeer;
 100: import java.awt.peer.ScrollPanePeer;
 101: import java.awt.peer.ScrollbarPeer;
 102: import java.awt.peer.TextAreaPeer;
 103: import java.awt.peer.TextFieldPeer;
 104: import java.awt.peer.WindowPeer;
 105: import java.io.InputStream;
 106: import java.net.URL;
 107: import java.util.HashMap;
 108: import java.util.Map;
 109: import java.util.Properties;
 110: 
 111: public class QtToolkit extends ClasspathToolkit
 112: {
 113:   public static EventQueue eventQueue = null; // the native event queue
 114:   public static QtRepaintThread repaintThread = null;
 115:   public static MainQtThread guiThread = null;
 116:   public static QtGraphicsEnvironment graphicsEnv = null;
 117: 
 118:   private static void initToolkit()
 119:   {
 120:     eventQueue = new EventQueue();
 121:     repaintThread = new QtRepaintThread();
 122:     System.loadLibrary("qtpeer");
 123: 
 124:     String theme = null;
 125:     try
 126:       {
 127:         String style = System.getProperty("qtoptions.style");
 128:         if(style != null)
 129:           theme = style;
 130:       }
 131:     catch(SecurityException e)
 132:       {
 133:       }
 134:     catch(IllegalArgumentException e)
 135:       {
 136:       }
 137: 
 138:     boolean doublebuffer = true;
 139:     try
 140:       {
 141:         String style = System.getProperty("qtoptions.nodoublebuffer");
 142:         if(style != null)
 143:           doublebuffer = false;
 144:       }
 145:     catch(SecurityException e)
 146:       {
 147:       }
 148:     catch(IllegalArgumentException e)
 149:       {
 150:       }
 151: 
 152:     guiThread = new MainQtThread( theme, doublebuffer );
 153:     guiThread.start();
 154:     repaintThread.start();
 155:   }
 156: 
 157:   /**
 158:    * Construct the toolkit!
 159:    */
 160:   public QtToolkit()
 161:   {
 162:     if( guiThread == null )
 163:       initToolkit();
 164: 
 165:     // make sure the GUI thread has started.
 166:     while (!guiThread.isRunning())
 167:       ;
 168: 
 169:     if( graphicsEnv == null )
 170:       graphicsEnv = new QtGraphicsEnvironment( this );
 171:   }
 172: 
 173:   native String[] nativeFontFamilies();
 174: 
 175:   native int numScreens();
 176: 
 177:   native int defaultScreen();
 178: 
 179:   // ************ Public methods *********************
 180: 
 181:   public synchronized native void beep();
 182: 
 183:   public int checkImage(Image image, int w, int h, ImageObserver observer)
 184:   {
 185:     if(image instanceof QtImage)
 186:       return ((QtImage)image).checkImage(observer);
 187: 
 188:     return ImageObserver.ERROR; // FIXME
 189:   }
 190: 
 191:   protected ButtonPeer createButton( Button target )
 192:   {
 193:     return new QtButtonPeer( this, target );
 194:   }
 195: 
 196:   protected CanvasPeer createCanvas(Canvas target)
 197:   {
 198:     return new QtCanvasPeer( this, target );
 199:   }
 200: 
 201:   protected CheckboxPeer createCheckbox(Checkbox target)
 202:   {
 203:     return new QtCheckboxPeer( this, target );
 204:   }
 205: 
 206:   protected  ChoicePeer createChoice(Choice target)
 207:   {
 208:     return new QtChoicePeer( this, target );
 209:   }
 210: 
 211:   protected CheckboxMenuItemPeer createCheckboxMenuItem(CheckboxMenuItem target)
 212:   {
 213:     return new QtMenuItemPeer( this, target );
 214:   }
 215: 
 216:   public DragSourceContextPeer createDragSourceContextPeer(DragGestureEvent dge)
 217:   {
 218:     throw new RuntimeException("Not implemented");
 219:   }
 220: 
 221:   protected FramePeer createFrame(Frame target)
 222:   {
 223:     return new QtFramePeer( this, target );
 224:   }
 225: 
 226:   protected FileDialogPeer createFileDialog(FileDialog target)
 227:   {
 228:     return new QtFileDialogPeer( this, target );
 229:   }
 230: 
 231:   public Image createImage(ImageProducer producer)
 232:   {
 233:     return new QtImage( producer );
 234:   }
 235: 
 236:   public Image createImage(byte[] imageData,
 237:                            int imageOffset,
 238:                            int imageLength)
 239:   {
 240:     byte[] dataCopy = new byte[imageLength];
 241:     System.arraycopy(imageData, imageOffset, dataCopy, 0, imageLength);
 242:     return new QtImage( dataCopy );
 243:   }
 244: 
 245:   public Image createImage(String filename)
 246:   {
 247:     return new QtImage( filename );
 248:   }
 249: 
 250:   public Image createImage(URL url)
 251:   {
 252:     return new QtImage( url );
 253:   }
 254: 
 255:   protected TextFieldPeer createTextField(TextField target)
 256:   {
 257:     return new QtTextFieldPeer(this,target);
 258:   }
 259: 
 260:   protected LabelPeer createLabel(Label target)
 261:   {
 262:     return new QtLabelPeer( this, target );
 263:   }
 264: 
 265:   protected ListPeer createList(List target)
 266:   {
 267:     return new QtListPeer( this, target );
 268:   }
 269: 
 270:   protected ScrollbarPeer createScrollbar(Scrollbar target)
 271:   {
 272:     return new QtScrollbarPeer( this, target );
 273:   }
 274: 
 275:   protected ScrollPanePeer createScrollPane(ScrollPane target)
 276:   {
 277:     return new QtScrollPanePeer( this, target );
 278:   }
 279: 
 280:   protected TextAreaPeer createTextArea(TextArea target)
 281:   {
 282:     return new QtTextAreaPeer( this, target );
 283:   }
 284: 
 285:   protected PanelPeer createPanel(Panel target)
 286:   {
 287:     return new QtPanelPeer( this, target);
 288:   }
 289: 
 290:   protected WindowPeer createWindow(Window target)
 291:   {
 292:     return new QtWindowPeer( this, target );
 293:   }
 294: 
 295:   protected DialogPeer createDialog(Dialog target)
 296:   {
 297:     return new QtDialogPeer( this, target );
 298:   }
 299: 
 300:   protected MenuBarPeer createMenuBar(MenuBar target)
 301:   {
 302:     return new QtMenuBarPeer( this, target );
 303:   }
 304: 
 305:   protected MenuPeer createMenu(Menu target)
 306:   {
 307:     return new QtMenuPeer( this, target );
 308:   }
 309: 
 310:   protected PopupMenuPeer createPopupMenu(PopupMenu target)
 311:   {
 312:     return new QtPopupMenuPeer( this, target );
 313:   }
 314: 
 315:   protected MenuItemPeer createMenuItem(MenuItem target)
 316:   {
 317:     return new QtMenuItemPeer( this, target );
 318:   }
 319: 
 320:   /**
 321:    * @since 1.4
 322:    */
 323:   public AWTEventListener[] getAWTEventListeners()
 324:   {
 325:     return null; // FIXME
 326:   }
 327: 
 328:   /**
 329:    * @since 1.4
 330:    */
 331:   public AWTEventListener[] getAWTEventListeners(long mask)
 332:   {
 333:     return null; // FIXME
 334:   }
 335: 
 336:   public ColorModel getColorModel()
 337:   {
 338:     return new DirectColorModel(32,
 339:                                 0x00FF0000,
 340:                                 0x0000FF00,
 341:                                 0x000000FF,
 342:                                 0xFF000000);
 343:   }
 344: 
 345:   /**
 346:    * Just return the defaults.
 347:    */
 348:   public String[] getFontList()
 349:   {
 350:     String[] builtIn = new String[] { "Dialog",
 351:                                       "DialogInput",
 352:                                       "Monospaced",
 353:                                       "Serif",
 354:                                       "SansSerif" };
 355:     String[] nat = nativeFontFamilies();
 356:     String[] allFonts = new String[ nat.length + 5 ];
 357:     System.arraycopy(builtIn, 0, allFonts, 0, 5);
 358:     System.arraycopy(nat, 0, allFonts, 5, nat.length);
 359:     return allFonts;
 360:   }
 361: 
 362:   public FontMetrics getFontMetrics(Font font)
 363:   {
 364:     return new QtFontMetrics(font);
 365:   }
 366: 
 367:   protected FontPeer getFontPeer(String name,
 368:                                  int style)
 369:   {
 370:     Map attrs = new HashMap ();
 371:     ClasspathFontPeer.copyStyleToAttrs(style, attrs);
 372:     ClasspathFontPeer.copySizeToAttrs(12, attrs); // Default size is 12.
 373:     return getClasspathFontPeer (name, attrs);
 374:   }
 375: 
 376:   public Image getImage(String filename)
 377:   {
 378:     return new QtImage(filename);
 379:   }
 380: 
 381:   public Image getImage(URL url)
 382:   {
 383:     return createImage( url );
 384:   }
 385: 
 386:   public PrintJob getPrintJob(Frame frame,
 387:                               String jobtitle,
 388:                               Properties props)
 389:   {
 390:     SecurityManager sm;
 391:     sm = System.getSecurityManager();
 392:     if (sm != null)
 393:       sm.checkPrintJobAccess();
 394: 
 395:     throw new RuntimeException("Not implemented");
 396:   }
 397: 
 398:   public Clipboard getSystemClipboard()
 399:   {
 400:     throw new RuntimeException("Not implemented");
 401:   }
 402: 
 403:   protected EventQueue getSystemEventQueueImpl()
 404:   {
 405:     return eventQueue;
 406:   }
 407: 
 408:   public native Dimension getScreenSize();
 409: 
 410:   public native int getScreenResolution();
 411: 
 412:   public Map mapInputMethodHighlight(InputMethodHighlight highlight)
 413:   {
 414:     return null; // FIXME
 415:   }
 416: 
 417:   public boolean prepareImage(Image image, int w, int h, ImageObserver observer)
 418:   {
 419:     if(image instanceof QtImage)
 420:       return true;
 421:     return false; // FIXME?
 422:   }
 423: 
 424:   public native void sync();
 425: 
 426:   // ********************** ClasspathToolkit methods
 427: 
 428:   public GraphicsEnvironment getLocalGraphicsEnvironment()
 429:   {
 430:     return graphicsEnv;
 431:   }
 432: 
 433:   public ClasspathFontPeer getClasspathFontPeer (String name, Map attrs)
 434:   {
 435:     return new QtFontPeer (name, attrs);
 436:   }
 437: 
 438:   // FIXME
 439:   public Font createFont(int format, InputStream stream)
 440:   {
 441:     throw new UnsupportedOperationException();
 442:   }
 443: 
 444:   // FIXME
 445:   public RobotPeer createRobot (GraphicsDevice screen) throws AWTException
 446:   {
 447:     throw new UnsupportedOperationException();
 448:   }
 449: 
 450:   public EmbeddedWindowPeer createEmbeddedWindow(EmbeddedWindow w)
 451:   {
 452:     //    return new QtEmbeddedWindowPeer( this, w );
 453:     return null;
 454:   }
 455: 
 456:   @Override
 457:   public boolean isModalExclusionTypeSupported
 458:   (Dialog.ModalExclusionType modalExclusionType)
 459:   {
 460:     return false;
 461:   }
 462: 
 463:   @Override
 464:   public boolean isModalityTypeSupported(Dialog.ModalityType modalityType)
 465:   {
 466:     return false;
 467:   }
 468: 
 469: 
 470: }