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