/*************************************************************************
 *
 *  $RCSfile: wrtgraf.cxx,v $
 *
 *  $Revision: 1.1.1.1 $
 *
 *  last change: $Author: hr $ $Date: 2000/09/18 17:14:57 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

#ifdef PRECOMPILED
#include "filt_pch.hxx"
#endif

#pragma hdrstop

#ifndef _FRMFMT_HXX
#include <frmfmt.hxx>
#endif
#ifndef _NDGRF_HXX
#include <ndgrf.hxx>
#endif
#ifndef _WRTW4W_HXX
#include <wrtw4w.hxx>
#endif

#if defined(VCL)

void SwW4WWriter::OutW4W_Grf( const SwGrfNode* )
{
// Anhand des alten WindowCodes muss das gesamte mal auf Stream-Interfaces
// umgestellt werden !!!
	return;
}

#else

// !!!!!!!!!!!!! der alte Windows Code !!!!!!!!!!
// Windows: wirkliche Funktion
#include <stdio.h>			// sprintf()

#include <dos.h>   // FP_SEG, ....
extern "C" USHORT SegIncr();
#define BUMP_PTR(p) (void*)MK_FP(FP_SEG(p) + SegIncr(), FP_OFF(p))
#define MY_FP_OFF(fp) (*((USHORT *)&(fp)))

#ifndef _MAPMOD_HXX //autogen
#include <vcl/mapmod.hxx>
#endif
#ifndef _VIRDEV_HXX //autogen
#include <vcl/virdev.hxx>
#endif

//#ifdef NIE
void SwW4WWriter::DumpIt( char* pBuf, long nSize )
{
	register long i;
	BYTE* pBI = (BYTE*)pBuf;
	char tmp[15];

	for (i=0; i<nSize; i++)
	{
		sprintf( tmp, "%02X", *pBI );	// nur so gibt's grosse Buchstaben
		Strm() << tmp;                  // ueber Stream gibt's nur Kleinbuchstaben

		pBI++;
		if ( MY_FP_OFF( pBI ) == 0 )     // 64K-Grenze
			pBI = (BYTE*)BUMP_PTR( pBI );

	}
}
//#endif

#define BUF_SIZE 8192	// muss 1..30000 sein

void SwW4WWriter::OutW4W_Grf( const SwGrfNode* pGrfNd )
{
	if ( GetFilter() != 44 && GetFilter() != 49 )
		return;		// nur WW2 und WW6 vertragen embedded WMF
	if ( GetIniFlags() & 0x20000 )
		return;		// Iniflags: kein Grafik-Export

	Graphic& rGrf = (Graphic&)(pGrfNd->GetGrf());
	BOOL bSwapped = rGrf.IsSwapOut();
	((SwGrfNode*)pGrfNd)->SwapIn();		// immer ueber den Node einswappen !!!

	GDIMetaFile aMeta;

	switch ( rGrf.GetType() )
	{
	case GRAPHIC_BITMAP:				// Bitmap -> in Metafile abspielen
	{
			VirtualDevice aVirt;
//			Size aS = rGrf.GetBitmap().GetSizePixel();
//			aVirt.SetOutputSizePixel( rGrf.GetBitmap().GetSizePixel() );
//			aMeta.SetPrefMapMode( MapMode( MAP_100TH_MM ) );
//			aMeta.SetPrefSize( aVirt.PixelToLogic( aS ) );
			MapMode aMap = rGrf.GetPrefMapMode();
//			Size aSiz = rGrf.GetPrefSize();


			aVirt.SetMapMode( aMap );
			aMeta.Record( &aVirt );
//			aVirt.DrawBitmap( Point( 0,0 ), rGrf.GetBitmap() );
//			aVirt.DrawBitmap( Point( 0,0 ), aSiz, rGrf.GetBitmap() );
			aVirt.DrawBitmap( Point( 0,0 ), Size( 1,1 ), rGrf.GetBitmap() );
			aMeta.Stop();
			Sysdepen::ConvertToSysMetaFile( aMeta );
		}
		break;
	case GRAPHIC_GDIMETAFILE :				// GDI ( =SV ) Metafile
		aMeta = rGrf.GetGDIMetaFile();	// Kopie machen
		Sysdepen::ConvertToSysMetaFile( aMeta );
		break;

	case GRAPHIC_WINMETAFILE :			// WinMetafile
		aMeta = rGrf.GetGDIMetaFile();
		break;

	default :
		return;
	}

	HMETAFILE hM = Sysdepen::GethMetaFile( aMeta );
	HMETAFILE hM2 = CopyMetaFile( hM, 0 );
	HGLOBAL hG = GetMetaFileBits( hM2 );
	DWORD nSiz = GlobalSize( hG );
	char* pBuf = (char*)GlobalLock( hG );
	if ( 0 == pBuf ) return;

	SwTwips nOrgWidth = pGrfNd->GetTwipSize().Width();
	SwTwips nOrgHeight = pGrfNd->GetTwipSize().Height();
	SwTwips nWidth = nOrgWidth;
	SwTwips nHeight = nOrgHeight;

	if ( nFlyWidth > 0 && nFlyHeight > 0 )
	{
		nWidth = nFlyWidth;					// hier steht die ware Groesse
		nHeight = nFlyHeight;				// naemlich im uebergeordneten Frame
		if ( nOrgWidth == 0 ) nOrgWidth = nFlyWidth;
		if ( nOrgHeight == 0 ) nOrgHeight = nFlyHeight;
	}

								// folgende Angaben sind z.T. gemogelt

	Strm() << sW4W_RECBEGIN << "PDS1" << cW4W_TXTERM 	// Win- Metafile
	 << '0' << cW4W_TXTERM                             // !Mogel! Float
	 << '0' << cW4W_TXTERM                             // !Mogel! x-Pos
	 << '0' << cW4W_TXTERM;                             // !Mogel! y-Pos
	OutLong( nWidth ) << cW4W_TXTERM;                        // Breite
	OutLong( nHeight ) << cW4W_TXTERM;                       // Hoehe
	OutLong( nOrgWidth ) << cW4W_TXTERM;                     // Original Breite
	OutLong( nOrgHeight ) << sW4W_TERMEND;                    // Original Hoehe

	Strm() << sW4W_RECBEGIN << "PCT";
	OutLong( nSiz ) << cW4W_TXTERM;

	DumpIt( pBuf, nSiz );

	Strm() << sW4W_TERMEND;

	GlobalUnlock( hG );
	GlobalFree( hG );
	if( bSwapped ) rGrf.SwapOut();
}

#endif
				// WIN
/*************************************************************************

	  Source Code Control System - Header

	  $Header: /cvs/oo/sw/sw/source/filter/w4w/wrtgraf.cxx,v 1.1.1.1 2000/09/18 17:14:57 hr Exp $

	  Source Code Control System - Update

	  $Log: wrtgraf.cxx,v $
	  Revision 1.1.1.1  2000/09/18 17:14:57  hr
	  initial import
	
	  Revision 1.26  2000/09/18 16:04:55  willem.vandorp
	  OpenOffice header added.
	
	  Revision 1.25  2000/05/11 19:47:11  jp
	  Changes for Unicode
	
	  Revision 1.24  2000/02/11 14:38:35  hr
	  #70473# changes for unicode ( patched by automated patchtool )

	  Revision 1.23  1997/11/26 13:48:56  MA
	  headerfiles


	  Rev 1.22   26 Nov 1997 14:48:56   MA
   headerfiles

	  Rev 1.21   03 Nov 1997 14:12:14   MA
   precomp entfernt

	  Rev 1.20   29 Aug 1997 18:57:52   JP
   VCL Anpassung

	  Rev 1.19   18 Aug 1997 11:45:18   OS
   includes

	  Rev 1.18   15 Aug 1997 12:54:34   OS
   charatr/frmatr/txtatr aufgeteilt

	  Rev 1.17   11 Aug 1997 09:45:28   MH
   chg: header

	  Rev 1.16   28 Jun 1996 22:32:14   MA
   includes

	  Rev 1.15   25 Mar 1996 16:42:28   MA
   Umstellung soref

	  Rev 1.14   24 Nov 1995 17:24:30   OM
   PCH->PRECOMPILED

	  Rev 1.13   08 Nov 1995 12:26:16   JP
   Umstellung 301: Change -> Set

	  Rev 1.12   31 Oct 1995 15:15:50   JP
   S-Size/-Point/-Rectangle entfernt

	  Rev 1.11   17 Oct 1995 00:34:40   JP
   SwappIn von Grafik immer ueber den Node

	  Rev 1.10   26 Oct 1994 08:02:42   SWG
   Include nachgeruestet.

	  Rev 1.9   25 Oct 1994 15:53:50   MA
   PreHdr.

	  Rev 1.8   19 Oct 1994 14:27:36   MA
   SV-ChangeXXX -> SetXXX.

	  Rev 1.7   26 Sep 1994 12:03:52   JP
   Umstellung auf Sfx-Items

	  Rev 1.6   21 Sep 1994 17:59:08   PK
   einbau sv-streams

	  Rev 1.5   04 Aug 1994 13:45:02   SWG
   swg32: SED Size to SSize, LSize to Size etc.

	  Rev 1.4   24 May 1994 09:05:00   OK
   Von Sven aus Berlin am 24.05.94

	  Rev 1.3   20 May 1994 08:41:12   OK
   Grafikexport in richtiger Groesse und Orginagroesse

	  Rev 1.00   17 Apr 1994 21:10:44   SH

*************************************************************************/


