javauis/lcdui_qt/src/javax/microedition/lcdui/Canvas.java
changeset 48 e0d6e9bd3ca7
parent 47 f40128debb5d
child 61 bf7ee68962da
equal deleted inserted replaced
47:f40128debb5d 48:e0d6e9bd3ca7
     1 /*
     1 /*
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2009, 2010 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
    28 import org.eclipse.swt.widgets.*;
    28 import org.eclipse.swt.widgets.*;
    29 import org.eclipse.swt.widgets.Display;
    29 import org.eclipse.swt.widgets.Display;
    30 import org.eclipse.swt.graphics.*;
    30 import org.eclipse.swt.graphics.*;
    31 import org.eclipse.swt.internal.extension.CompositeExtension;
    31 import org.eclipse.swt.internal.extension.CompositeExtension;
    32 import org.eclipse.swt.internal.qt.graphics.WindowSurface;
    32 import org.eclipse.swt.internal.qt.graphics.WindowSurface;
       
    33 import org.eclipse.swt.internal.extension.MobileShellExtension;
       
    34 import org.eclipse.swt.internal.qt.SymbianWindowVisibilityListener;
    33 
    35 
    34 
    36 
    35 /**
    37 /**
    36  * The abstract <code>Canvas</code> class is designed to handle low-level
    38  * The abstract <code>Canvas</code> class is designed to handle low-level
    37  * graphical operations as well as low-level key and pointer events. Canvas is
    39  * graphical operations as well as low-level key and pointer events. Canvas is
   170         new CanvasShellPaintListener();
   172         new CanvasShellPaintListener();
   171 
   173 
   172     private CanvasShellMouseListener mouseListener =
   174     private CanvasShellMouseListener mouseListener =
   173         new CanvasShellMouseListener();
   175         new CanvasShellMouseListener();
   174 
   176 
       
   177     private CanvasShellVisibilityListener shellVisibilityListener =
       
   178         new CanvasShellVisibilityListener();
       
   179 
   175     // Canvas Graphics object passed to paint(Graphics g)
   180     // Canvas Graphics object passed to paint(Graphics g)
   176     private Graphics canvasGraphics;
   181     private Graphics canvasGraphics;
   177 
   182 
   178     // Graphics object for transferring return values
   183     // Graphics object for transferring return values
   179     // from UI thread
   184     // from UI thread
   180     private Graphics tempGraphics;
   185     private Graphics tempGraphics;
   181     
   186 
   182     // Graphics command buffer for this instance
   187     // Graphics command buffer for this instance
   183     Buffer graphicsBuffer;
   188     Buffer graphicsBuffer;
   184     
   189 
   185     //On Screen Keypad
   190     //On Screen Keypad
   186     //private Composite keypadComposite;
   191     //private Composite keypadComposite;
   187     private CanvasKeypad onScreenkeypad;
   192     private CanvasKeypad onScreenkeypad;
   188     private static CanvasKeypad sharedKeypad;
   193     private static CanvasKeypad sharedKeypad;
   189     private int oskHeight;
   194     private int oskHeight;
   241         setMode(GAME_CANVAS, this instanceof GameCanvas);
   246         setMode(GAME_CANVAS, this instanceof GameCanvas);
   242         construct();
   247         construct();
   243         keysPressed = new Vector();
   248         keysPressed = new Vector();
   244     }
   249     }
   245 
   250 
   246 
       
   247     /**
   251     /**
   248      * Disposes this instance
   252      * Disposes this instance
   249      * Called when finalizer is destroying this instance.
   253      * Called when finalizer is destroying this instance.
   250      */
   254      */
   251     void dispose()
   255     void dispose()
   262                 }
   266                 }
   263 
   267 
   264                 synchronized(this)
   268                 synchronized(this)
   265                 {
   269                 {
   266                     objectCount--;
   270                     objectCount--;
   267                     
   271 
   268                     if((objectCount == 0) || isMode(GAME_CANVAS))
   272                     if((objectCount == 0) || isMode(GAME_CANVAS))
   269                     {
   273                     {
   270                         mShell.dispose();
   274                         mShell.dispose();
   271                         sharedShell = null;
   275                         sharedShell = null;
   272                         sharedKeypad = null;
   276                         sharedKeypad = null;
   288                 }
   292                 }
   289             }
   293             }
   290         });
   294         });
   291     }
   295     }
   292 
   296 
   293 
       
   294     /* (non-Javadoc)
   297     /* (non-Javadoc)
   295      * @see Displayable#eswtConstructShell(int)
   298      * @see Displayable#eswtConstructShell(int)
   296      */
   299      */
   297     Shell eswtConstructShell(int style)
   300     Shell eswtConstructShell(int style)
   298     {
   301     {
   315 
   318 
   316         // Make the Shell maximized. On Symbian it's automatically maximized
   319         // Make the Shell maximized. On Symbian it's automatically maximized
   317         // so this has no effect but on other platforms explicit maximizing
   320         // so this has no effect but on other platforms explicit maximizing
   318         // might be needed.
   321         // might be needed.
   319         mShell.setMaximized(true);
   322         mShell.setMaximized(true);
   320         
   323 
   321         return mShell;
   324         return mShell;
   322     }
   325     }
   323 
   326 
   324     /**
   327     /**
   325      * Sets ticker. If ticker is added already to other canvas(es),
   328      * Sets ticker. If ticker is added already to other canvas(es),
   427             setDefaultTapValues();
   430             setDefaultTapValues();
   428         }
   431         }
   429 
   432 
   430         canvasComp = super.eswtConstructContent(style);
   433         canvasComp = super.eswtConstructContent(style);
   431         canvasComp.setVisible(false);
   434         canvasComp.setVisible(false);
   432         
   435 
   433         createOnScreenKeypad();
   436         createOnScreenKeypad();
   434 
   437 
   435         return canvasComp;
       
   436     }
       
   437 
       
   438     void eswtInitGraphics() {
       
   439         // create graphics buffer
   438         // create graphics buffer
   440         graphicsBuffer = Buffer.createInstance(this, canvasComp);
   439         graphicsBuffer = Buffer.createInstance(this, canvasComp);
   441     }
   440 
   442 	
   441         return canvasComp;
   443     /**
   442     }
   444      * Creates OSK(OnScreenKeypad), shared Keypad will be created for Canvas, 
   443 
       
   444     /**
       
   445      * Creates OSK(OnScreenKeypad), shared Keypad will be created for Canvas,
   445      * seperate OSK will be created for each GameCanvas.
   446      * seperate OSK will be created for each GameCanvas.
   446      */
   447      */
   447     CanvasKeypad createOnScreenKeypad()
   448     CanvasKeypad createOnScreenKeypad()
   448     {
   449     {
   449         // Read the on screen keypad settings from the jad attribute
   450         // Read the on screen keypad settings from the jad attribute
   473         }
   474         }
   474 
   475 
   475         return null;
   476         return null;
   476     }
   477     }
   477 
   478 
   478 
       
   479     Rectangle eswtLayoutShellContent()
   479     Rectangle eswtLayoutShellContent()
   480     {
   480     {
   481         Rectangle shellArea = mShell.getClientArea();
   481         Rectangle shellArea = mShell.getClientArea();
   482         int oskHeight = (onScreenkeypad != null ? onScreenkeypad.getHeight() : 0);
   482         int oskHeight = (onScreenkeypad != null ? onScreenkeypad.getHeight() : 0);
   483         int tickerHeight = (tickerLabel != null ? tickerLabel.getBounds().height : 0);
   483         int tickerHeight = (tickerLabel != null ? tickerLabel.getBounds().height : 0);
   486                               shellArea.width, shellArea.height - tickerHeight - oskHeight);
   486                               shellArea.width, shellArea.height - tickerHeight - oskHeight);
   487 
   487 
   488         canvasComp.setFocus();
   488         canvasComp.setFocus();
   489         return canvasComp.getClientArea();
   489         return canvasComp.getClientArea();
   490     }
   490     }
   491 
       
   492 
   491 
   493     /* (non-Javadoc)
   492     /* (non-Javadoc)
   494      * @see Displayable#eswtHandleShowCurrentEvent()
   493      * @see Displayable#eswtHandleShowCurrentEvent()
   495      */
   494      */
   496     void eswtHandleShowCurrentEvent()
   495     void eswtHandleShowCurrentEvent()
   514         addCommands();
   513         addCommands();
   515         super.eswtHandleShowCurrentEvent();
   514         super.eswtHandleShowCurrentEvent();
   516         getContentComp().addPaintListener(paintListener);
   515         getContentComp().addPaintListener(paintListener);
   517         getContentComp().addMouseListener(mouseListener);
   516         getContentComp().addMouseListener(mouseListener);
   518         getContentComp().addMouseMoveListener(mouseListener);
   517         getContentComp().addMouseMoveListener(mouseListener);
       
   518         ((MobileShellExtension)getShell()).addSymbianWindowVisibilityListener(shellVisibilityListener);
   519     }
   519     }
   520 
   520 
   521     /* (non-Javadoc)
   521     /* (non-Javadoc)
   522      * @see Displayable#eswtHandleHideCurrentEvent()
   522      * @see Displayable#eswtHandleHideCurrentEvent()
   523      */
   523      */
   532         removeCommands();
   532         removeCommands();
   533         super.eswtHandleHideCurrentEvent();
   533         super.eswtHandleHideCurrentEvent();
   534         getContentComp().removePaintListener(paintListener);
   534         getContentComp().removePaintListener(paintListener);
   535         getContentComp().removeMouseListener(mouseListener);
   535         getContentComp().removeMouseListener(mouseListener);
   536         getContentComp().removeMouseMoveListener(mouseListener);
   536         getContentComp().removeMouseMoveListener(mouseListener);
       
   537         ((MobileShellExtension)getShell()).removeSymbianWindowVisibilityListener(shellVisibilityListener);
   537     }
   538     }
   538 
   539 
   539     /**
   540     /**
   540      * eSWT callback to add a Command.
   541      * eSWT callback to add a Command.
   541      */
   542      */
   649                     {
   650                     {
   650                         tickerLabel.pack();
   651                         tickerLabel.pack();
   651                         tickerLabel.setLocation(Integer.MIN_VALUE, 0);
   652                         tickerLabel.setLocation(Integer.MIN_VALUE, 0);
   652                     }
   653                     }
   653 
   654 
   654 					if(isMode(CURRENTLY_VISIBLE))
   655                     if(isMode(CURRENTLY_VISIBLE))
   655 					{
   656                     {
   656 	                    tickerLabel.setVisible(!isMode(FULLSCREEN_MODE));
   657                         tickerLabel.setVisible(!isMode(FULLSCREEN_MODE));
   657 					}
   658                     }
   658                 }
   659                 }
   659 
   660 
   660                 if(isMode(CURRENTLY_VISIBLE))
   661                 if(isMode(CURRENTLY_VISIBLE))
   661                 {
   662                 {
   662                     ((MobileShell)mShell).setFullScreenMode(isMode(FULLSCREEN_MODE));
   663                     ((MobileShell)mShell).setFullScreenMode(isMode(FULLSCREEN_MODE));
   770             }
   771             }
   771         });
   772         });
   772         return (super.getHeight() - oskHeight);
   773         return (super.getHeight() - oskHeight);
   773 
   774 
   774     }
   775     }
   775 
       
   776 
   776 
   777     /**
   777     /**
   778      * Callback to be implemented by the application to render the
   778      * Callback to be implemented by the application to render the
   779      * <code>Canvas</code>. The clip region of <code>Graphics</code> object
   779      * <code>Canvas</code>. The clip region of <code>Graphics</code> object
   780      * specifies the area that needs to be updated. All the region has to be
   780      * specifies the area that needs to be updated. All the region has to be
   887     final void initGameCanvas(boolean suppressKeys)
   887     final void initGameCanvas(boolean suppressKeys)
   888     {
   888     {
   889         setMode(SUPPRESS_GAMEKEYS, suppressKeys);
   889         setMode(SUPPRESS_GAMEKEYS, suppressKeys);
   890     }
   890     }
   891 
   891 
   892 
       
   893     /**
   892     /**
   894      * Gets composite that contains Canvas content.
   893      * Gets composite that contains Canvas content.
   895      *
   894      *
   896      * @return Composite.
   895      * @return Composite.
   897      */
   896      */
   898     Composite getContentComp()
   897     Composite getContentComp()
   899     {
   898     {
   900         return canvasComp;
   899         return canvasComp;
   901     }
   900     }
   902 
   901 
   903 
       
   904     /**
   902     /**
   905      * Get game canvas frame buffer graphics.
   903      * Get game canvas frame buffer graphics.
   906      */
   904      */
   907     final Graphics getGameBufferGraphics()
   905     final Graphics getGameBufferGraphics()
   908     {
   906     {
   909         tempGraphics = null;
   907         tempGraphics = null;
   910         ESWTUIThreadRunner.safeSyncExec(new Runnable() 
   908         ESWTUIThreadRunner.safeSyncExec(new Runnable()
   911         {
   909         {
   912             public void run()
   910             public void run()
   913             {
   911             {
   914                 tempGraphics =  graphicsBuffer.getGraphics();
   912                 tempGraphics =  graphicsBuffer.getGraphics();
   915             }
   913             }
   956      * @param height
   954      * @param height
   957      */
   955      */
   958     void flushGameBuffer(final int x, final int y, final int width,
   956     void flushGameBuffer(final int x, final int y, final int width,
   959                          final int height)
   957                          final int height)
   960     {
   958     {
   961         // This is serialized with the 
   959         // This is serialized with the
   962         // paint callback processing
   960         // paint callback processing
   963         synchronized(flushLock)
   961         synchronized(flushLock)
   964         {
   962         {
   965             synchronized(graphicsBuffer)
   963             synchronized(graphicsBuffer)
   966             {
   964             {
   967                  ESWTUIThreadRunner.safeSyncExec(new Runnable() 
   965                  ESWTUIThreadRunner.safeSyncExec(new Runnable()
   968                 {
   966                 {
   969                     public void run()
   967                     public void run()
   970                     {
   968                     {
   971                         graphicsBuffer.sync();
   969                         graphicsBuffer.sync();
   972                         graphicsBuffer.blitToDisplay(null, getContentComp());
   970                         graphicsBuffer.blitToDisplay(null, getContentComp());
   973                     }
   971                     }
   974                 });
   972                 });
   975             }
   973             }
   976         }
   974         }
   977     }    
   975     }
   978 
   976 
   979     /**
   977     /**
   980      * Called by ShellListener when shell gets activated.
   978      * Called by ShellListener when shell gets activated.
   981      */
   979      */
   982     void handleShellActivatedEvent()
   980     void handleShellActivatedEvent()
  1010      */
  1008      */
  1011     void eswtHandleResizeEvent(int width, int height)
  1009     void eswtHandleResizeEvent(int width, int height)
  1012     {
  1010     {
  1013         super.eswtHandleResizeEvent(width, height);
  1011         super.eswtHandleResizeEvent(width, height);
  1014         // update new bounds to graphicsBuffer
  1012         // update new bounds to graphicsBuffer
  1015         // this call must not be synchronized as we 
  1013         // this call must not be synchronized as we
  1016         // cannot use locking in UI thread
  1014         // cannot use locking in UI thread
  1017         graphicsBuffer.setControlBounds(getContentComp());
  1015         graphicsBuffer.setControlBounds(getContentComp());
  1018         synchronized(cleanupLock)
  1016         synchronized(cleanupLock)
  1019         {
  1017         {
  1020             setMode(CLEANUP_NEEDED, true);
  1018             setMode(CLEANUP_NEEDED, true);
  1063     class CanvasShellPaintListener implements PaintListener
  1061     class CanvasShellPaintListener implements PaintListener
  1064     {
  1062     {
  1065         public void paintControl(PaintEvent pe)
  1063         public void paintControl(PaintEvent pe)
  1066         {
  1064         {
  1067             // Check if we got here from buffer flush
  1065             // Check if we got here from buffer flush
  1068             if(graphicsBuffer.isPaintingActive()) 
  1066             if(graphicsBuffer.isPaintingActive())
  1069             {
  1067             {
  1070                 graphicsBuffer.blitToDisplay(pe.gc.getGCData().internalGc, null);
  1068                 graphicsBuffer.blitToDisplay(pe.gc.getGCData().internalGc, null);
  1071             }
  1069             }
  1072             else
  1070             else
  1073             {
  1071             {
  1140     {
  1138     {
  1141         synchronized(flushLock)
  1139         synchronized(flushLock)
  1142         {
  1140         {
  1143             // It's possible that this Canvas is sent to background
  1141             // It's possible that this Canvas is sent to background
  1144             // right after the visibility is checked here, however
  1142             // right after the visibility is checked here, however
  1145             // it is okay as in such case we just do one extra paint 
  1143             // it is okay as in such case we just do one extra paint
  1146             // callback. The visibility change cannot be synchronized with
  1144             // callback. The visibility change cannot be synchronized with
  1147             // this method, since it would expose implementation to deadlock
  1145             // this method, since it would expose implementation to deadlock
  1148             if(!isMode(CURRENTLY_VISIBLE))
  1146             if(!isMode(CURRENTLY_VISIBLE))
  1149             {
  1147             {
  1150                 return;
  1148                 return;
  1151             }
  1149             }
  1152             
  1150 
  1153             // Decide the area going to be painted by the callback.
  1151             // Decide the area going to be painted by the callback.
  1154             final int redrawNowX;
  1152             final int redrawNowX;
  1155             final int redrawNowY;
  1153             final int redrawNowY;
  1156             final int redrawNowW;
  1154             final int redrawNowW;
  1157             final int redrawNowH;
  1155             final int redrawNowH;
  1219 
  1217 
  1220             // The callback
  1218             // The callback
  1221             paint(canvasGraphics);
  1219             paint(canvasGraphics);
  1222 
  1220 
  1223             // Blit frame to display
  1221             // Blit frame to display
  1224             synchronized(graphicsBuffer) 
  1222             synchronized(graphicsBuffer)
  1225             {
  1223             {
  1226                 ESWTUIThreadRunner.safeSyncExec(new Runnable() 
  1224                 ESWTUIThreadRunner.safeSyncExec(new Runnable()
  1227                 {
  1225                 {
  1228                      public void run()
  1226                      public void run()
  1229                     {
  1227                     {
  1230                         if(event.widget.isDisposed())
  1228                         if(event.widget.isDisposed())
  1231                         {
  1229                         {
  1305             EventDispatcher.instance().postEvent(event);
  1303             EventDispatcher.instance().postEvent(event);
  1306 
  1304 
  1307         }
  1305         }
  1308     }
  1306     }
  1309 
  1307 
  1310 
       
  1311     /*
  1308     /*
  1312      * UI thread calls.
  1309      * UI thread calls.
  1313      */
  1310      */
  1314     void doKeyReleased(int keyCode)
  1311     void doKeyReleased(int keyCode)
  1315     {
  1312     {
  1560         public void run()
  1557         public void run()
  1561         {
  1558         {
  1562             setMode(SUPPRESS_DRAGEVENT, false);
  1559             setMode(SUPPRESS_DRAGEVENT, false);
  1563         }
  1560         }
  1564     }
  1561     }
       
  1562 
       
  1563     class CanvasShellVisibilityListener implements SymbianWindowVisibilityListener
       
  1564     {
       
  1565         public void handleSymbianWindowVisibilityChange(Widget widget, boolean visible) {
       
  1566             if (javax.microedition.lcdui.Canvas.this.getShell() == widget)
       
  1567             {
       
  1568                 graphicsBuffer.getWindowSurface().handleSymbianWindowVisibilityChange(visible);
       
  1569             }
       
  1570         }
       
  1571     }
  1565 }
  1572 }