Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svx / source / sidebar / SelectionAnalyzer.cxx
blobdc8073ec6c41ba412af1f576b999ffd26080663d
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 <svx/sidebar/SelectionAnalyzer.hxx>
21 #include <svx/svdmark.hxx>
22 #include <svx/svdobj.hxx>
23 #include <svx/svdotext.hxx>
24 #include <svx/svdpage.hxx>
25 #include <svx/fontworkbar.hxx>
27 using vcl::EnumContext;
29 namespace svx::sidebar
31 EnumContext::Context SelectionAnalyzer::GetContextForSelection_SC(const SdrMarkList& rMarkList)
33 EnumContext::Context eContext = EnumContext::Context::Unknown;
35 switch (rMarkList.GetMarkCount())
37 case 0:
38 // Empty selection. Return Context::Unknown to let the caller
39 // substitute it with the default context.
40 break;
42 case 1:
44 SdrObject* pObj = rMarkList.GetMark(0)->GetMarkedSdrObj();
45 auto pTextObj = DynCastSdrTextObj(pObj);
46 if (pTextObj && pTextObj->IsInEditMode())
48 eContext = EnumContext::Context::DrawText;
50 else if (svx::checkForFontWork(pObj))
52 eContext = EnumContext::Context::DrawFontwork;
54 else
56 const SdrInventor nInv = pObj->GetObjInventor();
57 const SdrObjKind nObjId = pObj->GetObjIdentifier();
58 if (nInv == SdrInventor::Default)
59 eContext = GetContextForObjectId_SC(nObjId);
60 else if (nInv == SdrInventor::FmForm)
61 eContext = EnumContext::Context::Form;
63 break;
66 default:
68 // Multi selection.
69 switch (GetInventorTypeFromMark(rMarkList))
71 case SdrInventor::Default:
73 const SdrObjKind nObjId(GetObjectTypeFromMark(rMarkList));
74 if (nObjId == SdrObjKind::NONE)
75 eContext = EnumContext::Context::MultiObject;
76 else
77 eContext = GetContextForObjectId_SC(nObjId);
78 break;
81 case SdrInventor::FmForm:
82 eContext = EnumContext::Context::Form;
83 break;
85 case SdrInventor::Unknown:
86 eContext = EnumContext::Context::MultiObject;
87 break;
89 default:
90 break;
95 return eContext;
98 EnumContext::Context SelectionAnalyzer::GetContextForSelection_SD(const SdrMarkList& rMarkList,
99 const ViewType eViewType)
101 EnumContext::Context eContext = EnumContext::Context::Unknown;
103 // Note that some cases are handled by the caller. They rely on
104 // sd specific data.
105 switch (rMarkList.GetMarkCount())
107 case 0:
108 switch (eViewType)
110 case ViewType::Standard:
111 eContext = EnumContext::Context::DrawPage;
112 break;
113 case ViewType::Master:
114 eContext = EnumContext::Context::MasterPage;
115 break;
116 case ViewType::Handout:
117 eContext = EnumContext::Context::HandoutPage;
118 break;
119 case ViewType::Notes:
120 eContext = EnumContext::Context::NotesPage;
121 break;
123 break;
125 case 1:
127 SdrObject* pObj = rMarkList.GetMark(0)->GetMarkedSdrObj();
128 auto pTextObj = DynCastSdrTextObj(pObj);
129 if (pTextObj && pTextObj->IsInEditMode())
131 if (pObj->GetObjIdentifier() == SdrObjKind::Table)
133 // Let a table object take precedence over text
134 // edit mode. The panels for text editing are
135 // present for table context as well, anyway.
136 eContext = EnumContext::Context::Table;
138 else
139 eContext = EnumContext::Context::DrawText;
141 else if (svx::checkForFontWork(pObj))
143 eContext = EnumContext::Context::DrawFontwork;
145 else
147 const SdrInventor nInv = pObj->GetObjInventor();
148 SdrObjKind nObjId = pObj->GetObjIdentifier();
149 if (nInv == SdrInventor::Default)
151 if (nObjId == SdrObjKind::Group)
153 nObjId = GetObjectTypeFromGroup(pObj);
154 if (nObjId == SdrObjKind::NONE)
155 nObjId = SdrObjKind::Group;
157 eContext = GetContextForObjectId_SD(nObjId, eViewType);
159 else if (nInv == SdrInventor::E3d)
161 eContext = EnumContext::Context::ThreeDObject;
163 else if (nInv == SdrInventor::FmForm)
165 eContext = EnumContext::Context::Form;
168 break;
171 default:
173 switch (GetInventorTypeFromMark(rMarkList))
175 case SdrInventor::Default:
177 const SdrObjKind nObjId = GetObjectTypeFromMark(rMarkList);
178 if (nObjId == SdrObjKind::NONE)
179 eContext = EnumContext::Context::MultiObject;
180 else
181 eContext = GetContextForObjectId_SD(nObjId, eViewType);
182 break;
185 case SdrInventor::E3d:
186 eContext = EnumContext::Context::ThreeDObject;
187 break;
189 case SdrInventor::FmForm:
190 eContext = EnumContext::Context::Form;
191 break;
193 case SdrInventor::Unknown:
194 eContext = EnumContext::Context::MultiObject;
195 break;
197 default:
198 break;
200 break;
204 return eContext;
207 EnumContext::Context SelectionAnalyzer::GetContextForObjectId_SC(const SdrObjKind nObjectId)
209 switch (nObjectId)
211 case SdrObjKind::Caption:
212 case SdrObjKind::TitleText:
213 case SdrObjKind::OutlineText:
214 case SdrObjKind::Text:
215 case SdrObjKind::Measure:
216 case SdrObjKind::Rectangle:
217 case SdrObjKind::CircleOrEllipse:
218 case SdrObjKind::FreehandFill:
219 case SdrObjKind::PathFill:
220 case SdrObjKind::Polygon:
221 case SdrObjKind::CircleSection:
222 case SdrObjKind::CircleArc:
223 case SdrObjKind::CircleCut:
224 case SdrObjKind::CustomShape:
225 case SdrObjKind::Group:
226 return EnumContext::Context::Draw;
228 case SdrObjKind::PolyLine:
229 case SdrObjKind::PathLine:
230 case SdrObjKind::FreehandLine:
231 case SdrObjKind::Line:
232 case SdrObjKind::Edge:
233 return EnumContext::Context::DrawLine;
235 case SdrObjKind::Graphic:
236 return EnumContext::Context::Graphic;
238 case SdrObjKind::OLE2:
239 return EnumContext::Context::OLE;
241 case SdrObjKind::Media:
242 return EnumContext::Context::Media;
244 default:
245 return EnumContext::Context::Unknown;
249 EnumContext::Context SelectionAnalyzer::GetContextForObjectId_SD(const SdrObjKind nObjectId,
250 const ViewType eViewType)
252 switch (nObjectId)
254 case SdrObjKind::Caption:
255 case SdrObjKind::Measure:
256 case SdrObjKind::Rectangle:
257 case SdrObjKind::CircleOrEllipse:
258 case SdrObjKind::FreehandFill:
259 case SdrObjKind::PathFill:
260 case SdrObjKind::Polygon:
261 case SdrObjKind::CircleSection:
262 case SdrObjKind::CircleArc:
263 case SdrObjKind::CircleCut:
264 case SdrObjKind::CustomShape:
265 case SdrObjKind::Group:
266 return EnumContext::Context::Draw;
268 case SdrObjKind::Edge:
269 case SdrObjKind::PathLine:
270 case SdrObjKind::FreehandLine:
271 case SdrObjKind::PolyLine:
272 case SdrObjKind::Line:
273 return EnumContext::Context::DrawLine;
275 case SdrObjKind::TitleText:
276 case SdrObjKind::OutlineText:
277 case SdrObjKind::Text:
278 return EnumContext::Context::TextObject;
280 case SdrObjKind::Graphic:
281 return EnumContext::Context::Graphic;
283 case SdrObjKind::OLE2:
284 return EnumContext::Context::OLE;
286 case SdrObjKind::Media:
287 return EnumContext::Context::Media;
289 case SdrObjKind::Table:
290 return EnumContext::Context::Table;
292 case SdrObjKind::Page:
293 switch (eViewType)
295 case ViewType::Handout:
296 return EnumContext::Context::HandoutPage;
297 case ViewType::Notes:
298 return EnumContext::Context::NotesPage;
299 default:
300 return EnumContext::Context::Unknown;
303 default:
304 return EnumContext::Context::Unknown;
308 SdrInventor SelectionAnalyzer::GetInventorTypeFromMark(const SdrMarkList& rMarkList)
310 const size_t nMarkCount(rMarkList.GetMarkCount());
312 if (nMarkCount < 1)
313 return SdrInventor::Unknown;
315 SdrMark* pMark = rMarkList.GetMark(0);
316 SdrObject* pObj = pMark->GetMarkedSdrObj();
317 const SdrInventor nFirstInv = pObj->GetObjInventor();
319 for (size_t nIndex = 1; nIndex < nMarkCount; ++nIndex)
321 pMark = rMarkList.GetMark(nIndex);
322 pObj = pMark->GetMarkedSdrObj();
323 const SdrInventor nInv(pObj->GetObjInventor());
325 if (nInv != nFirstInv)
326 return SdrInventor::Unknown;
329 return nFirstInv;
332 SdrObjKind SelectionAnalyzer::GetObjectTypeFromGroup(const SdrObject* pObj)
334 SdrObjList* pObjList = pObj->GetSubList();
335 if (pObjList)
337 const size_t nSubObjCount(pObjList->GetObjCount());
339 if (nSubObjCount > 0)
341 SdrObject* pSubObj = pObjList->GetObj(0);
342 SdrObjKind nResultType = pSubObj->GetObjIdentifier();
344 if (nResultType == SdrObjKind::Group)
345 nResultType = GetObjectTypeFromGroup(pSubObj);
347 if (IsShapeType(nResultType))
348 nResultType = SdrObjKind::CustomShape;
350 if (IsTextObjType(nResultType))
351 nResultType = SdrObjKind::Text;
353 for (size_t nIndex = 1; nIndex < nSubObjCount; ++nIndex)
355 pSubObj = pObjList->GetObj(nIndex);
356 SdrObjKind nType(pSubObj->GetObjIdentifier());
358 if (nType == SdrObjKind::Group)
359 nType = GetObjectTypeFromGroup(pSubObj);
361 if (IsShapeType(nType))
362 nType = SdrObjKind::CustomShape;
364 if ((nType == SdrObjKind::CustomShape) && (nResultType == SdrObjKind::Text))
365 nType = SdrObjKind::Text;
367 if (IsTextObjType(nType))
368 nType = SdrObjKind::Text;
370 if ((nType == SdrObjKind::Text) && (nResultType == SdrObjKind::CustomShape))
371 nResultType = SdrObjKind::Text;
373 if (nType != nResultType)
374 return SdrObjKind::NONE;
377 return nResultType;
381 return SdrObjKind::NONE;
384 SdrObjKind SelectionAnalyzer::GetObjectTypeFromMark(const SdrMarkList& rMarkList)
386 const size_t nMarkCount(rMarkList.GetMarkCount());
388 if (nMarkCount < 1)
389 return SdrObjKind::NONE;
391 SdrMark* pMark = rMarkList.GetMark(0);
392 SdrObject* pObj = pMark->GetMarkedSdrObj();
393 SdrObjKind nResultType = pObj->GetObjIdentifier();
395 if (nResultType == SdrObjKind::Group)
396 nResultType = GetObjectTypeFromGroup(pObj);
398 if (IsShapeType(nResultType))
399 nResultType = SdrObjKind::CustomShape;
401 if (IsTextObjType(nResultType))
402 nResultType = SdrObjKind::Text;
404 for (size_t nIndex = 1; nIndex < nMarkCount; ++nIndex)
406 pMark = rMarkList.GetMark(nIndex);
407 pObj = pMark->GetMarkedSdrObj();
408 SdrObjKind nType = pObj->GetObjIdentifier();
410 if (nType == SdrObjKind::Group)
411 nType = GetObjectTypeFromGroup(pObj);
413 if (IsShapeType(nType))
414 nType = SdrObjKind::CustomShape;
416 if ((nType == SdrObjKind::CustomShape) && (nResultType == SdrObjKind::Text))
417 nType = SdrObjKind::Text;
419 if (IsTextObjType(nType))
420 nType = SdrObjKind::Text;
422 if ((nType == SdrObjKind::Text) && (nResultType == SdrObjKind::CustomShape))
423 nResultType = SdrObjKind::Text;
425 if (nType != nResultType)
426 return SdrObjKind::NONE;
429 return nResultType;
432 bool SelectionAnalyzer::IsShapeType(const SdrObjKind nType)
434 switch (nType)
436 case SdrObjKind::Line:
437 case SdrObjKind::CircleArc:
438 case SdrObjKind::PolyLine:
439 case SdrObjKind::PathLine:
440 case SdrObjKind::Rectangle:
441 case SdrObjKind::CircleOrEllipse:
442 case SdrObjKind::CircleSection:
443 case SdrObjKind::CircleCut:
444 case SdrObjKind::PathFill:
445 case SdrObjKind::CustomShape:
446 case SdrObjKind::Caption:
447 case SdrObjKind::Measure:
448 case SdrObjKind::Edge:
449 case SdrObjKind::Polygon:
450 case SdrObjKind::FreehandLine:
451 case SdrObjKind::FreehandFill:
453 // #122145# adding SdrObjKind::OLE2 since these also allow line/fill style and may
454 // be multiselected/grouped with normal draw objects, e.g. math OLE objects
455 case SdrObjKind::OLE2:
456 return true;
458 default:
459 return false;
463 bool SelectionAnalyzer::IsTextObjType(const SdrObjKind nType)
465 switch (nType)
467 case SdrObjKind::Text:
468 case SdrObjKind::TitleText:
469 case SdrObjKind::OutlineText:
470 return true;
472 default:
473 return false;
477 } // end of namespace svx::sidebar
479 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */