Get the style color and number just once
[LibreOffice.git] / sd / source / ui / func / smarttag.cxx
blobf9aeec99eb1b876d14c0b549f254a2876c755891
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <utility>
21 #include <vcl/commandevent.hxx>
23 #include <ViewShell.hxx>
24 #include <smarttag.hxx>
25 #include <Window.hxx>
26 #include <View.hxx>
28 namespace sd
31 SmartTag::SmartTag( ::sd::View& rView )
32 : mrView( rView )
33 , mbSelected( false )
35 SmartTagReference xThis( this );
36 mrView.getSmartTags().add( xThis );
39 SmartTag::~SmartTag()
43 bool SmartTag::MouseButtonDown( const MouseEvent&, SmartHdl& )
45 return false;
48 /** returns true if the SmartTag consumes this event. */
49 bool SmartTag::KeyInput( const KeyEvent& /*rKEvt*/ )
51 return false;
54 /** returns true if the SmartTag consumes this event. */
55 bool SmartTag::Command( const CommandEvent& /*rCEvt*/ )
57 return false;
60 void SmartTag::addCustomHandles( SdrHdlList& /*rHandlerList*/ )
64 void SmartTag::select()
66 mbSelected = true;
69 void SmartTag::deselect()
71 mbSelected = false;
74 void SmartTag::disposing()
76 SmartTagReference xThis( this );
77 mrView.getSmartTags().remove( xThis );
80 bool SmartTag::getContext( SdrViewContext& /*rContext*/ )
82 return false;
85 sal_Int32 SmartTag::GetMarkablePointCount() const
87 return 0;
90 sal_Int32 SmartTag::GetMarkedPointCount() const
92 return 0;
95 bool SmartTag::MarkPoint(SdrHdl& /*rHdl*/, bool /*bUnmark*/ )
97 return false;
100 bool SmartTag::MarkPoints(const ::tools::Rectangle* /*pRect*/, bool /*bUnmark*/ )
102 return false;
105 void SmartTag::CheckPossibilities()
109 SmartTagSet::SmartTagSet( View& rView )
110 : mrView( rView )
114 SmartTagSet::~SmartTagSet()
118 void SmartTagSet::add( const SmartTagReference& xTag )
120 maSet.insert( xTag );
121 mrView.InvalidateAllWin();
123 if( xTag == mxMouseOverTag )
124 mxMouseOverTag.clear();
126 if( xTag == mxSelectedTag )
127 mxSelectedTag.clear();
130 void SmartTagSet::remove( const SmartTagReference& xTag )
132 std::set< SmartTagReference >::iterator aIter( maSet.find( xTag ) );
133 if( aIter != maSet.end() )
134 maSet.erase( aIter );
135 mrView.InvalidateAllWin();
137 if( xTag == mxMouseOverTag )
138 mxMouseOverTag.clear();
140 if( xTag == mxSelectedTag )
141 mxSelectedTag.clear();
144 void SmartTagSet::Dispose()
146 std::set< SmartTagReference > aSet;
147 aSet.swap( maSet );
148 for( auto& rxItem : aSet )
149 rxItem->Dispose();
150 mrView.InvalidateAllWin();
151 mxMouseOverTag.clear();
152 mxSelectedTag.clear();
155 void SmartTagSet::select( const SmartTagReference& xTag )
157 if( mxSelectedTag == xTag )
158 return;
160 if( mxSelectedTag.is() )
161 mxSelectedTag->deselect();
163 mxSelectedTag = xTag;
164 mxSelectedTag->select();
165 mrView.SetPossibilitiesDirty();
166 const SdrMarkList& rMarkList = mrView.GetMarkedObjectList();
167 if( rMarkList.GetMarkCount() > 0 )
168 mrView.UnmarkAllObj();
169 else
170 mrView.updateHandles();
173 void SmartTagSet::deselect()
175 if( mxSelectedTag.is() )
177 mxSelectedTag->deselect();
178 mxSelectedTag.clear();
179 mrView.SetPossibilitiesDirty();
180 mrView.updateHandles();
184 bool SmartTagSet::MouseButtonDown( const MouseEvent& rMEvt )
186 Point aMDPos( mrView.GetViewShell()->GetActiveWindow()->PixelToLogic( rMEvt.GetPosPixel() ) );
187 SdrHdl* pHdl = mrView.PickHandle(aMDPos);
189 // check if a smart tag is selected and no handle is hit
190 if( mxSelectedTag.is() && !pHdl )
192 // deselect smart tag
193 deselect();
194 return false;
197 // if a smart tag handle is hit, forward event to its smart tag
198 SmartHdl* pSmartHdl = dynamic_cast< SmartHdl* >( pHdl );
199 if(pSmartHdl && pSmartHdl->getTag().is() )
201 SmartTagReference xTag( pSmartHdl->getTag() );
202 return xTag->MouseButtonDown( rMEvt, *pSmartHdl );
205 return false;
208 bool SmartTagSet::KeyInput( const KeyEvent& rKEvt )
210 if( mxSelectedTag.is() )
211 return mxSelectedTag->KeyInput( rKEvt );
212 else if( rKEvt.GetKeyCode().GetCode() == KEY_SPACE )
214 SmartHdl* pSmartHdl = dynamic_cast< SmartHdl* >( mrView.GetHdlList().GetFocusHdl() );
215 if( pSmartHdl )
217 const_cast< SdrHdlList& >( mrView.GetHdlList() ).ResetFocusHdl();
218 const SmartTagReference& xTag( pSmartHdl->getTag() );
219 select( xTag );
220 return true;
224 return false;
227 /** returns true if the SmartTag consumes this event. */
228 bool SmartTagSet::Command( const CommandEvent& rCEvt )
230 if( rCEvt.IsMouseEvent() )
232 Point aMDPos( mrView.GetViewShell()->GetActiveWindow()->PixelToLogic( rCEvt.GetMousePosPixel() ) );
233 SdrHdl* pHdl = mrView.PickHandle(aMDPos);
235 if( pHdl )
237 // if a smart tag handle is hit, forward event to its smart tag
238 SmartHdl* pSmartHdl = dynamic_cast< SmartHdl* >( pHdl );
239 if(pSmartHdl && pSmartHdl->getTag().is() )
241 const SmartTagReference& xTag( pSmartHdl->getTag() );
242 return xTag->Command( rCEvt );
246 else
248 if( mxSelectedTag.is() )
249 return mxSelectedTag->Command( rCEvt );
253 return false;
256 void SmartTagSet::addCustomHandles( SdrHdlList& rHandlerList )
258 for( auto& rxItem : maSet )
259 rxItem->addCustomHandles( rHandlerList );
262 /** returns true if the currently selected smart tag has
263 a special context, returned in rContext. */
264 bool SmartTagSet::getContext( SdrViewContext& rContext ) const
266 if( mxSelectedTag.is() )
267 return mxSelectedTag->getContext( rContext );
268 else
269 return false;
272 // support point editing
274 bool SmartTagSet::HasMarkablePoints() const
276 return GetMarkablePointCount() != 0;
279 sal_uLong SmartTagSet::GetMarkablePointCount() const
281 if( mxSelectedTag.is() )
282 return mxSelectedTag->GetMarkablePointCount();
283 return 0;
286 bool SmartTagSet::HasMarkedPoints() const
288 return GetMarkedPointCount() != 0;
291 sal_uLong SmartTagSet::GetMarkedPointCount() const
293 if( mxSelectedTag.is() )
294 return mxSelectedTag->GetMarkedPointCount();
295 else
296 return 0;
299 bool SmartTagSet::MarkPoint(SdrHdl& rHdl, bool bUnmark )
301 if( mxSelectedTag.is() )
302 return mxSelectedTag->MarkPoint( rHdl, bUnmark );
304 return false;
307 bool SmartTagSet::MarkPoints(const ::tools::Rectangle* pRect, bool bUnmark)
309 if( mxSelectedTag.is() )
310 return mxSelectedTag->MarkPoints( pRect, bUnmark );
311 return false;
314 void SmartTagSet::CheckPossibilities()
316 if( mxSelectedTag.is() )
317 mxSelectedTag->CheckPossibilities();
320 SmartHdl::SmartHdl( SmartTagReference xTag, SdrObject* pObject, const Point& rPnt, SdrHdlKind eNewKind /*=SdrHdlKind::Move*/ )
321 : SdrHdl( rPnt, eNewKind )
322 , mxSmartTag(std::move( xTag ))
324 SetObj( pObject );
327 SmartHdl::SmartHdl( SmartTagReference xTag, const Point& rPnt, SdrHdlKind eNewKind /*=SdrHdlKind::Move*/ )
328 : SdrHdl( rPnt, eNewKind )
329 , mxSmartTag(std::move( xTag ))
333 } // end of namespace sd
335 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */