/**************************************************************
 * 
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * 
 *************************************************************/



// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_sd.hxx"

#ifdef SD_DLLIMPLEMENTATION
#undef SD_DLLIMPLEMENTATION
#endif


#include "eetext.hxx"

#include <svx/dialogs.hrc>
#include <editeng/flstitem.hxx>
#include <svx/drawitem.hxx>
#include <svl/style.hxx>
#include <svx/tabline.hxx>
#include <editeng/bulitem.hxx>
#include <editeng/eeitem.hxx>
#include <editeng/brshitem.hxx>
#include <vcl/graph.hxx>
#include <editeng/lrspitem.hxx>
#include <editeng/numitem.hxx>
#include <svl/cjkoptions.hxx>

#include "DrawDocShell.hxx"
#include "glob.hrc"
#include "sdresid.hxx"
#include "prltempl.hxx"
#include "prltempl.hrc"
#include "enumdlg.hrc"
//#include "enumdlg.hxx"
#include "bulmaper.hxx"
#include <svl/intitem.hxx>
#include <svx/svxgrahicitem.hxx>
#include <svx/flagsdef.hxx>
#include "drawdoc.hxx"
#define IS_OUTLINE(x) (x >= PO_OUTLINE_1 && x <= PO_OUTLINE_9)

/*************************************************************************
|*
|* Konstruktor des Tab-Dialogs: Fuegt die Seiten zum Dialog hinzu
|*
\************************************************************************/

SdPresLayoutTemplateDlg::SdPresLayoutTemplateDlg( SfxObjectShell* pDocSh,
                                Window* pParent,
								SdResId DlgId,
								SfxStyleSheetBase& rStyleBase,
								PresentationObjects _ePO,
								SfxStyleSheetBasePool* pSSPool ) :
		SfxTabDialog		( pParent, DlgId ),
		mpDocShell			( pDocSh ),
		ePO 				( _ePO ),
		aInputSet			( *rStyleBase.GetItemSet().GetPool(), SID_PARAM_NUM_PRESET, SID_PARAM_CUR_NUM_LEVEL ),
		pOutSet				( NULL ),
		pOrgSet				( &rStyleBase.GetItemSet() )
{
	if( IS_OUTLINE(ePO))
	{
		// Leider sind die Itemsets unserer Stylesheets nicht discret..
		const sal_uInt16* pPtr = pOrgSet->GetRanges();
		sal_uInt16 p1, p2;
		while( *pPtr )
		{
			p1 = pPtr[0];
			p2 = pPtr[1];

			// erstmal das ganze discret machen
			while(pPtr[2] && (pPtr[2] - p2 == 1))
			{
				p2 = pPtr[3];
				pPtr += 2;
			}
			aInputSet.MergeRange( p1, p2 );
			pPtr += 2;
		}

		aInputSet.Put( rStyleBase.GetItemSet() );

		// need parent-relationship
		const SfxItemSet* pParentItemSet = rStyleBase.GetItemSet().GetParent();;
		if( pParentItemSet )
			aInputSet.SetParent( pParentItemSet );

		pOutSet = new SfxItemSet( rStyleBase.GetItemSet() );
		pOutSet->ClearItem();

		const SfxPoolItem *pItem = NULL;

		// Fals in diesem Stylesheet kein Bullet Item ist, holen wir uns
		// das aus dem 'Outline 1' Stylesheet.
		if( SFX_ITEM_SET != aInputSet.GetItemState(EE_PARA_NUMBULLET, sal_False, &pItem ))
		{
			String aStyleName((SdResId(STR_PSEUDOSHEET_OUTLINE)));
			aStyleName.AppendAscii( RTL_CONSTASCII_STRINGPARAM( " 1" ) );
			SfxStyleSheetBase* pFirstStyleSheet = pSSPool->Find( aStyleName, SD_STYLE_FAMILY_PSEUDO);

			if(pFirstStyleSheet)
				if( SFX_ITEM_SET == pFirstStyleSheet->GetItemSet().GetItemState(EE_PARA_NUMBULLET, sal_False, &pItem) )
					aInputSet.Put( *pItem );
		}

		// gewaehlte Ebene im Dialog vorselektieren
		aInputSet.Put( SfxUInt16Item( SID_PARAM_CUR_NUM_LEVEL, 1<<GetOutlineLevel()));

		SetInputSet( &aInputSet );
	}
	else
		SetInputSet( pOrgSet );

	FreeResource();

	SvxColorTableItem aColorTableItem(*( (const SvxColorTableItem*)
		( mpDocShell->GetItem( SID_COLOR_TABLE ) ) ) );
	SvxGradientListItem aGradientListItem(*( (const SvxGradientListItem*)
		( mpDocShell->GetItem( SID_GRADIENT_LIST ) ) ) );
	SvxBitmapListItem aBitmapListItem(*( (const SvxBitmapListItem*)
		( mpDocShell->GetItem( SID_BITMAP_LIST ) ) ) );
	SvxHatchListItem aHatchListItem(*( (const SvxHatchListItem*)
		( mpDocShell->GetItem( SID_HATCH_LIST ) ) ) );
	SvxDashListItem aDashListItem(*( (const SvxDashListItem*)
		( mpDocShell->GetItem( SID_DASH_LIST ) ) ) );
	SvxLineEndListItem aLineEndListItem(*( (const SvxLineEndListItem*)
		( mpDocShell->GetItem( SID_LINEEND_LIST ) ) ) );

	maColorTab = aColorTableItem.GetColorTable();
	maDashList = aDashListItem.GetDashList();
	maLineEndList = aLineEndListItem.GetLineEndList();
	maGradientList = aGradientListItem.GetGradientList();
	maHatchingList = aHatchListItem.GetHatchList();
	maBitmapList = aBitmapListItem.GetBitmapList();

	switch( DlgId.GetId() )
	{
		case TAB_PRES_LAYOUT_TEMPLATE:
		{
			AddTabPage( RID_SVXPAGE_LINE);
			AddTabPage( RID_SVXPAGE_AREA);
			AddTabPage( RID_SVXPAGE_SHADOW);
			AddTabPage( RID_SVXPAGE_TRANSPARENCE);
			AddTabPage( RID_SVXPAGE_CHAR_NAME );
			AddTabPage( RID_SVXPAGE_CHAR_EFFECTS );
			AddTabPage( RID_SVXPAGE_STD_PARAGRAPH );
    		AddTabPage( RID_SVXPAGE_TEXTATTR );
            AddTabPage( RID_SVXPAGE_PICK_BULLET );
            AddTabPage( RID_SVXPAGE_PICK_SINGLE_NUM );
            AddTabPage( RID_SVXPAGE_PICK_BMP );
            AddTabPage( RID_SVXPAGE_NUM_OPTIONS );	
    		AddTabPage( RID_SVXPAGE_TABULATOR );
		}
		break;

		case TAB_PRES_LAYOUT_TEMPLATE_BACKGROUND:        // background
			AddTabPage( RID_SVXPAGE_AREA);
		break;
	}

	// #112490# the tabpages Alignment, Tabs and Asian Typography are very
	// usefull, except for the background style
	if( DlgId.GetId() != TAB_PRES_LAYOUT_TEMPLATE_BACKGROUND )
	{
	    SvtCJKOptions aCJKOptions;
		if( aCJKOptions.IsAsianTypographyEnabled() )
			AddTabPage( RID_SVXPAGE_PARA_ASIAN );
		else
			RemoveTabPage( RID_SVXPAGE_PARA_ASIAN );

		AddTabPage( RID_SVXPAGE_ALIGN_PARAGRAPH );
	}

	// Titel setzen und
	// entsprechende Seiten zum Dialog hinzufuegen
	String aTitle;

	switch( ePO )
	{
		case PO_TITLE:
			aTitle = String(SdResId( STR_PSEUDOSHEET_TITLE ));
		break;

		case PO_SUBTITLE:
			aTitle = String(SdResId( STR_PSEUDOSHEET_SUBTITLE ));
		break;

		case PO_BACKGROUND:
			aTitle = String(SdResId( STR_PSEUDOSHEET_BACKGROUND ));
		break;

		case PO_BACKGROUNDOBJECTS:
			aTitle = String(SdResId( STR_PSEUDOSHEET_BACKGROUNDOBJECTS ));
		break;

		case PO_OUTLINE_1:
		case PO_OUTLINE_2:
		case PO_OUTLINE_3:
		case PO_OUTLINE_4:
		case PO_OUTLINE_5:
		case PO_OUTLINE_6:
		case PO_OUTLINE_7:
		case PO_OUTLINE_8:
		case PO_OUTLINE_9:
			aTitle = String(SdResId( STR_PSEUDOSHEET_OUTLINE ));
			aTitle.Append( sal_Unicode(' ') );
			aTitle.Append( UniString::CreateFromInt32( ePO - PO_OUTLINE_1 + 1 ) );
		break;

		case PO_NOTES:
			aTitle = String(SdResId( STR_PSEUDOSHEET_NOTES ));
		break;
	}
	SetText( aTitle );

	nDlgType = 1; // Vorlagen-Dialog
	nPageType = 0;
	nPos = 0;

	nColorTableState = CT_NONE;
	nBitmapListState = CT_NONE;
	nGradientListState = CT_NONE;
	nHatchingListState = CT_NONE;
}

// -----------------------------------------------------------------------

SdPresLayoutTemplateDlg::~SdPresLayoutTemplateDlg()
{
	delete pOutSet;
}

// -----------------------------------------------------------------------

void SdPresLayoutTemplateDlg::PageCreated( sal_uInt16 nId, SfxTabPage &rPage )
{	SfxAllItemSet aSet(*(aInputSet.GetPool()));
	switch( nId )
	{
		case RID_SVXPAGE_LINE:
		{
			aSet.Put (SvxColorTableItem(maColorTab,SID_COLOR_TABLE));
			aSet.Put (SvxDashListItem(maDashList,SID_DASH_LIST));
			aSet.Put (SvxLineEndListItem(maLineEndList,SID_LINEEND_LIST));
			aSet.Put (SfxUInt16Item(SID_DLG_TYPE,nDlgType));

			rPage.PageCreated(aSet);
		}
		break;

		case RID_SVXPAGE_AREA:
		{
			aSet.Put (SvxColorTableItem(maColorTab,SID_COLOR_TABLE));
			aSet.Put (SvxGradientListItem(maGradientList,SID_GRADIENT_LIST));
			aSet.Put (SvxHatchListItem(maHatchingList,SID_HATCH_LIST));
			aSet.Put (SvxBitmapListItem(maBitmapList,SID_BITMAP_LIST));
			aSet.Put (SfxUInt16Item(SID_PAGE_TYPE,nPageType));
			aSet.Put (SfxUInt16Item(SID_DLG_TYPE,nDlgType));
			aSet.Put (SfxUInt16Item(SID_TABPAGE_POS,nPos));
			rPage.PageCreated(aSet);

		}
		break;

		case RID_SVXPAGE_SHADOW:
				aSet.Put (SvxColorTableItem(maColorTab,SID_COLOR_TABLE)); //add CHINA001
				aSet.Put (SfxUInt16Item(SID_PAGE_TYPE,nPageType));
				aSet.Put (SfxUInt16Item(SID_DLG_TYPE,nDlgType));
				rPage.PageCreated(aSet);
			break;

		case RID_SVXPAGE_TRANSPARENCE:
			aSet.Put (SfxUInt16Item(SID_PAGE_TYPE,nPageType));
			aSet.Put (SfxUInt16Item(SID_DLG_TYPE,nDlgType));
			rPage.PageCreated(aSet);
		break;

		case RID_SVXPAGE_CHAR_NAME:
		{
			SvxFontListItem aItem(*( (const SvxFontListItem*)
				( mpDocShell->GetItem( SID_ATTR_CHAR_FONTLIST) ) ) );

			aSet.Put (SvxFontListItem( aItem.GetFontList(), SID_ATTR_CHAR_FONTLIST));
			rPage.PageCreated(aSet);
		}
		break;

		case RID_SVXPAGE_CHAR_EFFECTS:
			aSet.Put (SfxUInt16Item(SID_DISABLE_CTL,DISABLE_CASEMAP));
			rPage.PageCreated(aSet);
		break;

		case RID_SVXPAGE_STD_PARAGRAPH:
		break;
	}
}

const SfxItemSet* SdPresLayoutTemplateDlg::GetOutputItemSet() const
{
	if( pOutSet )
	{
		pOutSet->Put( *SfxTabDialog::GetOutputItemSet() );

		const SvxNumBulletItem *pSvxNumBulletItem = NULL;
		if( SFX_ITEM_SET == pOutSet->GetItemState(EE_PARA_NUMBULLET, sal_False, (const SfxPoolItem**)&pSvxNumBulletItem ))
			SdBulletMapper::MapFontsInNumRule( *pSvxNumBulletItem->GetNumRule(), *pOutSet );
		return pOutSet;
	}
	else
		return SfxTabDialog::GetOutputItemSet();
}

// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
sal_uInt16 SdPresLayoutTemplateDlg::GetOutlineLevel() const
{
	switch( ePO )
	{
	case PO_OUTLINE_1: return 0;
	case PO_OUTLINE_2: return 1;
	case PO_OUTLINE_3: return 2;
	case PO_OUTLINE_4: return 3;
	case PO_OUTLINE_5: return 4;
	case PO_OUTLINE_6: return 5;
	case PO_OUTLINE_7: return 6;
	case PO_OUTLINE_8: return 7;
	case PO_OUTLINE_9: return 8;
	default:
		DBG_ASSERT( sal_False, "Falscher Po! [CL]");
	}
	return 0;
}