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

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