Back to index

lightning-sunbird  0.9+nobinonly
PrintingPort.java
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is mozilla.org code.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1998
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either of the GNU General Public License Version 2 or later (the "GPL"),
00025  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 /*
00038        PrintingPort.java
00039        
00040        Wraps a Quickdraw printing port and provides a way to image on it
00041        with a java.awt.Graphics object.  
00042        
00043        by Patrick C. Beard.
00044  */
00045 
00046 package com.apple.mrj.internal.awt;
00047 
00048 import com.apple.mrj.macos.generated.RectStruct;
00049 import com.apple.mrj.macos.generated.QuickdrawFunctions;
00050 import java.awt.*;
00051 
00052 public class PrintingPort implements GraphicsHost {
00053        private int mPrintingPort;
00054        private int mOriginX;
00055        private int mOriginY;
00056        private PortManager mPortManager;
00057        private QDPipeline mPipeline;
00058        private Rectangle mClipping;
00059 
00060        public PrintingPort(int printingPort, int originX, int originY) {
00061               mPrintingPort = printingPort;
00062               mOriginX = originX;
00063               mOriginY = originY;
00064               mPortManager = new PortManager(printingPort, 0, 0, PortManager.kPrinting, 0);
00065               mPipeline = new QDPipeline();
00066               mPortManager.setPipeline(mPipeline);
00067        }
00068        
00069        public void dispose() {
00070               // dispose of port manager flushes the pipeline.
00071               if (mPortManager != null) {
00072                      mPortManager.dispose();
00073                      mPortManager = null;
00074               }
00075               if (mPipeline != null) {
00076                      mPipeline.dispose();
00077                      mPipeline = null;
00078               }
00079        }
00080 
00081        private static final short MAXSHORT = 32767, MINSHORT = -32768;
00082        
00083        static short pinToShort(int value) {
00084               if (value > MAXSHORT)
00085                      return MAXSHORT;
00086               else if (value < MINSHORT)
00087                      return MINSHORT;
00088               return (short) value;
00089        }
00090 
00091        final class PrintingGraphics extends QDGraphics {
00093               public PrintingGraphics() {
00094                      super();
00095               }
00096 
00097            public synchronized Graphics create() {
00098                      if( !internalMarkHostInUse ( ) )
00099                             throw new AWTError("Using invalid Graphics object");
00100                      try {
00101                      return (new PrintingGraphics()).initialize(this,fXoff,fYoff,fClip);
00102               } finally {
00103                             internalDoneUsingHost ( );
00104                      }
00105            }
00106 
00107            public synchronized Graphics create(int x, int y, int width, int height) {
00108                      if( !internalMarkHostInUse() )
00109                             throw new AWTError("Using invalid Graphics object");
00110                      try {
00111                             VToolkit.intersect(sRectangle, fClip, fXoff+x,fYoff+y,width,height);
00112                             return (new PrintingGraphics()).initialize(this,fXoff+x,fYoff+y,sRectangle);
00113               } finally {
00114                             internalDoneUsingHost ( );
00115                      }
00116               }
00117 
00121               void restore() {
00122                      super.restore();
00123                      
00124                      int port = VAWTDirect.FastGetThePort();
00125                      if (port != mPrintingPort)
00126                             QuickdrawFunctions.SetPort(mPrintingPort);
00127                      
00128                      // restore default orgin.
00129                      QuickdrawFunctions.SetOrigin((short)0, (short)0);
00130                      
00131                      // set up correct clipping.
00132                      QDRectStruct clipRect = new QDRectStruct();
00133                      clipRect.Set(pinToShort(mClipping.x), pinToShort(mClipping.y),
00134                                           pinToShort(mClipping.x + mClipping.width), pinToShort(mClipping.y + mClipping.width));
00135                      QuickdrawFunctions.ClipRect(clipRect);
00136                      
00137                      if (port != mPrintingPort)
00138                             QuickdrawFunctions.SetPort(port);
00139               }
00140        }
00141        
00147        public Graphics getGraphics(Component component) {
00148               mClipping = component.getBounds();
00149               mClipping.x = mOriginX; mClipping.y = mOriginY;
00150               QDGraphics graphics = new PrintingGraphics();
00151               graphics.initialize(mPrintingPort, this, mPortManager,
00152                                                  mOriginX, mOriginY, mClipping, component.getForeground(),
00153                                                  component.getFont());
00154               return graphics;
00155        }
00156 
00158        public void graphicsCreated(QDGraphics g) throws OutOfMemoryError {}
00159        public void graphicsDisposed(QDGraphics g) {}
00160        public RGBColorValue getBackgroundRGB() { return new RGBColorValue(Color.white); }
00161        public void repaint(RectStruct r) {}
00162        public boolean markInUse() { return (mPortManager != null); }
00163        public void doneUsingIt() {}
00164 }