1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sd.hxx"
30 
31 #include <sfx2/request.hxx>
32 #include <sfx2/bindings.hxx>
33 #include <sfx2/dispatch.hxx>
34 
35 #include <svl/itemiter.hxx>
36 
37 #include <svx/globl3d.hxx>
38 #include <svx/svxids.hrc>
39 #include <svx/svdotable.hxx>
40 #include <editeng/outliner.hxx>
41 #include <editeng/eeitem.hxx>
42 #include <editeng/editeng.hxx>
43 
44 #define _SD_DLL // fuer SD_MOD()
45 #include "sdmod.hxx"
46 
47 #include "fuformatpaintbrush.hxx"
48 #include "drawview.hxx"
49 #include "DrawDocShell.hxx"
50 #include "DrawViewShell.hxx"
51 #include "FrameView.hxx"
52 #include "drawdoc.hxx"
53 #include "Outliner.hxx"
54 #include "ViewShellBase.hxx"
55 
56 #ifndef SD_WINDOW_SHELL_HXX
57 #include "Window.hxx"
58 #endif
59 
60 namespace sd {
61 
62 TYPEINIT1( FuFormatPaintBrush, FuText );
63 
64 FuFormatPaintBrush::FuFormatPaintBrush( ViewShell* pViewSh, ::sd::Window* pWin, ::sd::View* pView, SdDrawDocument* pDoc, SfxRequest& rReq )
65 : FuText(pViewSh, pWin, pView, pDoc, rReq)
66 , mbPermanent( false )
67 , mbOldIsQuickTextEditMode( true )
68 {
69 }
70 
71 FunctionReference FuFormatPaintBrush::Create( ViewShell* pViewSh, ::sd::Window* pWin, ::sd::View* pView, SdDrawDocument* pDoc, SfxRequest& rReq )
72 {
73 	FunctionReference xFunc( new FuFormatPaintBrush( pViewSh, pWin, pView, pDoc, rReq ) );
74 	xFunc->DoExecute( rReq );
75 	return xFunc;
76 }
77 
78 void FuFormatPaintBrush::DoExecute( SfxRequest& rReq )
79 {
80     const SfxItemSet *pArgs = rReq.GetArgs();
81     if( pArgs && pArgs->Count() >= 1 )
82     {
83         mbPermanent = static_cast<bool>(((SfxBoolItem &)pArgs->Get(SID_FORMATPAINTBRUSH)).GetValue());
84     }
85 
86     if( mpView )
87     {
88         mpView->TakeFormatPaintBrush( mpItemSet );
89 	}
90 }
91 
92 void FuFormatPaintBrush::implcancel()
93 {
94 	if( mpViewShell && mpViewShell->GetViewFrame() )
95 	{
96 		SfxViewFrame* pViewFrame = mpViewShell->GetViewFrame();
97 		pViewFrame->GetBindings().Invalidate(SID_FORMATPAINTBRUSH);
98 		pViewFrame->GetDispatcher()->Execute(SID_OBJECT_SELECT, SFX_CALLMODE_ASYNCHRON);
99 	}
100 }
101 
102 static void unmarkimpl( SdrView* pView )
103 {
104 	pView->SdrEndTextEdit();
105 	pView->UnMarkAll();
106 }
107 
108 sal_Bool FuFormatPaintBrush::MouseButtonDown(const MouseEvent& rMEvt)
109 {
110     if(mpView&&mpWindow)
111     {
112         SdrViewEvent aVEvt;
113 		SdrHitKind eHit = mpView->PickAnything(rMEvt, SDRMOUSEBUTTONDOWN, aVEvt);
114 
115 		if( (eHit == SDRHIT_TEXTEDIT) || (eHit == SDRHIT_TEXTEDITOBJ && ( mpViewShell->GetFrameView()->IsQuickEdit() || dynamic_cast< sdr::table::SdrTableObj* >( aVEvt.pObj ) != NULL ) ))
116 		{
117 			SdrObject* pPickObj=0;
118 			SdrPageView* pPV=0;
119 			sal_uInt16 nHitLog = sal_uInt16 ( mpWindow->PixelToLogic(Size(HITPIX,0)).Width() );
120 			mpView->PickObj( mpWindow->PixelToLogic( rMEvt.GetPosPixel() ),nHitLog, pPickObj, pPV, SDRSEARCH_PICKMARKABLE);
121 
122 			if( (pPickObj != 0) && !pPickObj->IsEmptyPresObj() )
123 			{
124 				// if we text hit another shape than the one currently selected, unselect the old one now
125 				const SdrMarkList& rMarkList = mpView->GetMarkedObjectList();
126 				if( rMarkList.GetMarkCount() >= 1 )
127 				{
128 					if( rMarkList.GetMarkCount() == 1 )
129 					{
130 						if( rMarkList.GetMark(0)->GetMarkedSdrObj() != pPickObj )
131 						{
132 
133 							// if current selected shape is not that of the hit text edit, deselect it
134 							unmarkimpl( mpView );
135 						}
136 					}
137 					else
138 					{
139 						// more than one shape selected, deselect all of them
140 						unmarkimpl( mpView );
141 					}
142 				}
143 				MouseEvent aMEvt( rMEvt.GetPosPixel(), rMEvt.GetClicks(), rMEvt.GetMode(), rMEvt.GetButtons(), 0 );
144 				return FuText::MouseButtonDown(aMEvt);
145 			}
146 
147 			if( aVEvt.pObj == 0 )
148 				aVEvt.pObj = pPickObj;
149 		}
150 
151 		unmarkimpl( mpView );
152 
153 		if( aVEvt.pObj )
154 		{
155 			sal_uInt16 nHitLog = sal_uInt16 ( mpWindow->PixelToLogic(Size(HITPIX,0)).Width() );
156 			sal_Bool bToggle = sal_False;
157 			mpView->MarkObj(mpWindow->PixelToLogic( rMEvt.GetPosPixel() ), nHitLog, bToggle, sal_False);
158 			return sal_True;
159 		}
160     }
161 	return sal_False;
162 }
163 
164 sal_Bool FuFormatPaintBrush::MouseMove(const MouseEvent& rMEvt)
165 {
166 	sal_Bool bReturn = sal_False;
167 	if( mpWindow && mpView )
168 	{
169 		if ( mpView->IsTextEdit() )
170 		{
171 			bReturn = FuText::MouseMove( rMEvt );
172 			mpWindow->SetPointer(Pointer(POINTER_FILL));
173 		}
174 		else
175 		{
176 			sal_uInt16 nHitLog = sal_uInt16 ( mpWindow->PixelToLogic(Size(HITPIX,0)).Width() );
177 			SdrObject* pObj=0;
178 			SdrPageView* pPV=0;
179 			sal_Bool bOverMarkableObject = mpView->PickObj( mpWindow->PixelToLogic( rMEvt.GetPosPixel() ),nHitLog, pObj, pPV, SDRSEARCH_PICKMARKABLE);
180 
181 			if(bOverMarkableObject && HasContentForThisType(pObj->GetObjInventor(),pObj->GetObjIdentifier()) )
182 				mpWindow->SetPointer(Pointer(POINTER_FILL));
183 			else
184 				mpWindow->SetPointer(Pointer(POINTER_ARROW));
185 		}
186 	}
187 	return bReturn;
188 }
189 
190 sal_Bool FuFormatPaintBrush::MouseButtonUp(const MouseEvent& rMEvt)
191 {
192 	if( mpItemSet.get() && mpView && mpView->AreObjectsMarked() )
193     {
194         bool bNoCharacterFormats = false;
195         bool bNoParagraphFormats = false;
196         {
197             if( (rMEvt.GetModifier()&KEY_MOD1) && (rMEvt.GetModifier()&KEY_SHIFT) )
198                 bNoCharacterFormats = true;
199             else if( rMEvt.GetModifier() & KEY_MOD1 )
200                 bNoParagraphFormats = true;
201         }
202 
203 		OutlinerView* pOLV = mpView->GetTextEditOutlinerView();
204 		if( pOLV )
205 			pOLV->MouseButtonUp(rMEvt);
206 
207         Paste( bNoCharacterFormats, bNoParagraphFormats );
208         if(mpViewShell)
209             mpViewShell->GetViewFrame()->GetBindings().Invalidate(SID_FORMATPAINTBRUSH);
210 
211 		if( mbPermanent )
212 			return sal_True;
213     }
214 
215 	implcancel();
216     return sal_True;
217 }
218 
219 sal_Bool FuFormatPaintBrush::KeyInput(const KeyEvent& rKEvt)
220 {
221     if( (rKEvt.GetKeyCode().GetCode() == KEY_ESCAPE) && mpViewShell )
222     {
223 		implcancel();
224         return sal_True;
225     }
226     return FuPoor::KeyInput(rKEvt);
227 }
228 
229 void FuFormatPaintBrush::Activate()
230 {
231     mbOldIsQuickTextEditMode = mpViewShell->GetFrameView()->IsQuickEdit();
232     if( !mbOldIsQuickTextEditMode  )
233     {
234         mpViewShell->GetFrameView()->SetQuickEdit(sal_True);
235 	    mpView->SetQuickTextEditMode(sal_True);
236 	}
237 }
238 
239 void FuFormatPaintBrush::Deactivate()
240 {
241     if( !mbOldIsQuickTextEditMode  )
242     {
243         mpViewShell->GetFrameView()->SetQuickEdit(sal_False);
244 	    mpView->SetQuickTextEditMode(sal_False);
245 	}
246 }
247 
248 bool FuFormatPaintBrush::HasContentForThisType( sal_uInt32 nObjectInventor, sal_uInt16 nObjectIdentifier ) const
249 {
250     if( mpItemSet.get() == 0 )
251         return false;
252     if( !mpView || (!mpView->SupportsFormatPaintbrush( nObjectInventor, nObjectIdentifier) ) )
253         return false;
254     return true;
255 }
256 
257 void FuFormatPaintBrush::Paste( bool bNoCharacterFormats, bool bNoParagraphFormats )
258 {
259     const SdrMarkList& rMarkList = mpView->GetMarkedObjectList();
260     if(mpItemSet.get() && (rMarkList.GetMarkCount() == 1) )
261     {
262         SdrObject* pObj = rMarkList.GetMark(0)->GetMarkedSdrObj();
263 
264 		if( mpDoc->IsUndoEnabled() )
265 		{
266 			String sLabel( mpViewShell->GetViewShellBase().RetrieveLabelFromCommand( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:FormatPaintbrush" ) ) ) );
267 			mpDoc->BegUndo( sLabel );
268 			mpDoc->AddUndo(mpDoc->GetSdrUndoFactory().CreateUndoAttrObject(*pObj,sal_False,sal_True));
269 		}
270 
271         mpView->ApplyFormatPaintBrush( *mpItemSet.get(), bNoCharacterFormats, bNoParagraphFormats );
272 
273 		if( mpDoc->IsUndoEnabled() )
274 		{
275 			mpDoc->EndUndo();
276 		}
277     }
278 }
279 
280 /* static */ void FuFormatPaintBrush::GetMenuState( DrawViewShell& rDrawViewShell, SfxItemSet &rSet )
281 {
282 	const SdrMarkList& rMarkList = rDrawViewShell.GetDrawView()->GetMarkedObjectList();
283 	const sal_uLong nMarkCount = rMarkList.GetMarkCount();
284 
285     if( nMarkCount == 1 )
286     {
287         SdrObject* pObj = rMarkList.GetMark(0)->GetMarkedSdrObj();
288         if( pObj && rDrawViewShell.GetDrawView()->SupportsFormatPaintbrush(pObj->GetObjInventor(),pObj->GetObjIdentifier()) )
289             return;
290     }
291     rSet.DisableItem( SID_FORMATPAINTBRUSH );
292 }
293 
294 
295 } // end of namespace sd
296