tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / sw / source / filter / ww8 / writerhelper.hxx
blob632d1cb3a639999c3c927c5f98b0038e260586cd
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 #ifndef INCLUDED_SW_SOURCE_FILTER_WW8_WRITERHELPER_HXX
21 #define INCLUDED_SW_SOURCE_FILTER_WW8_WRITERHELPER_HXX
23 #include <vector>
24 #include <map>
25 #include <com/sun/star/embed/XEmbeddedObject.hpp>
27 #include <sfx2/objsh.hxx>
28 #include <svl/itempool.hxx>
29 #include <svl/itemset.hxx>
30 #include <svx/svdtypes.hxx>
31 #include <node.hxx>
32 #include <pam.hxx>
33 #include <tools/poly.hxx>
34 #include <doc.hxx>
35 #include <vcl/graph.hxx>
37 class SwTextFormatColl;
38 class SwCharFormat;
39 class SdrObject;
40 class SdrOle2Obj;
41 class OutlinerParaObject;
42 class SwNumFormat;
43 class SwTextNode;
44 class SwNoTextNode;
45 class SwFormatCharFormat;
46 class SwDoc;
47 class SwNumRule;
49 namespace sw
51 namespace util
53 class ItemSort
55 public:
56 bool operator()(sal_uInt16 nA, sal_uInt16 nB) const;
61 namespace ww8
63 /// STL container of Paragraph Styles (SwTextFormatColl)
64 typedef std::vector<SwTextFormatColl *> ParaStyles;
65 /// STL container of SfxPoolItems (Attributes)
66 typedef std::map<sal_uInt16, const SfxPoolItem *, sw::util::ItemSort> PoolItems;
68 /** Make exporting a Writer Frame easy
70 In word all frames are effectively anchored to character or as
71 character. This is nice and simple, writer is massively complex in this
72 area, so this ww8::Frame simplifies matters by providing a single unified
73 view of the multitude of elements in writer and their differing quirks.
75 A ww8::Frame wraps a writer frame and is guaranteed to have a suitable
76 anchor position available from it. It hides much of the needless
77 complexity of the multitude of floating/inline elements in writer, it...
79 Guarantees an anchor position for a frame.
80 Provides a readable way to see if we are anchored inline. (as character)
81 Provides a simple way to flag what type of entity this frame describes.
82 Provides the size of the element as drawn by writer.
84 class Frame
86 public:
87 enum WriterSource {eTextBox, eGraphic, eOle, eDrawing, eFormControl,eBulletGrf};
88 private:
89 const SwFrameFormat* mpFlyFrame;
90 SwPosition maPos;
91 Size maSize;
92 // #i43447# - Size of the frame in the layout.
93 // Especially needed for graphics, whose layout size can differ from its
94 // size, because it is scaled into its environment.
95 Size maLayoutSize;
97 WriterSource meWriterType;
98 const SwNode *mpStartFrameContent;
99 bool mbIsInline;
100 bool mbForBullet:1;
101 Graphic maGrf;
102 public:
103 Frame(const SwFrameFormat &rFlyFrame, SwPosition aPos);
104 Frame(const Graphic&, SwPosition );
106 /** Get the writer SwFrameFormat that this object describes
108 @return
109 The wrapped SwFrameFormat
111 const SwFrameFormat &GetFrameFormat() const { return *mpFlyFrame; }
113 /** Get the position this frame is anchored at
115 @return
116 The anchor position of this frame
118 const SwPosition &GetPosition() const { return maPos; }
119 void SetPosition(SwPosition const& rPos) { maPos = rPos; }
121 /** Get the node this frame is anchored into
123 @return
124 The SwTextNode this frame is anchored inside
126 const SwContentNode *GetContentNode() const
127 { return maPos.GetNode().GetContentNode(); }
129 /** Get the type of frame that this wraps
131 @return
132 a WriterSource which describes the source type of this wrapper
134 WriterSource GetWriterType() const { return meWriterType; }
136 /** Is this frame inline (as character)
138 @return
139 whether this is inline or not
141 bool IsInline() const { return mbIsInline; }
143 /** Even if the frame isn't an inline frame, force it to behave as one
145 There are a variety of circumstances where word cannot have
146 anything except inline elements, e.g. inside frames. So its easier
147 to force this ww8::Frame into behaving as one, instead of special
148 casing export code all over the place.
151 void ForceTreatAsInline();
153 /** Get the first node of content in the frame
155 @return
156 the first node of content in the frame, might not be any at all.
158 const SwNode *GetContent() const { return mpStartFrameContent; }
159 const Graphic &GetGraphic() const { return maGrf; }
160 bool HasGraphic() const { return mbForBullet; }
162 /** Does this ww8::Frame refer to the same writer content as another
164 @return
165 if the two ww8::Frames are handling the same writer frame
167 bool RefersToSameFrameAs(const Frame &rOther) const
169 if (mbForBullet && rOther.mbForBullet)
170 return (maGrf == rOther.maGrf);
171 else if ((!mbForBullet) && (!rOther.mbForBullet))
172 return (mpFlyFrame == rOther.mpFlyFrame);
174 return false;
177 /** The Size of the contained element
179 @return
180 the best size to use to export to word
182 const Size& GetSize() const { return maSize; }
184 /** The layout size of the contained element
186 #i43447# - Needed for graphics, which are scaled into its environment
188 @return layout size
190 const Size& GetLayoutSize() const
192 return maLayoutSize;
196 /// STL container of Frames
197 typedef std::vector<Frame> Frames;
198 /// STL iterator for Frames
199 typedef std::vector<Frame>::iterator FrameIter;
202 namespace sw
204 namespace util
206 /** Provide a dynamic_cast style cast for SfxPoolItems
208 A SfxPoolItem generally need to be cast back to its original type
209 to be useful, which is both tedious and error prone. So item_cast is
210 a helper template to aid the process and test if the cast is
211 correct.
213 @param rItem
214 The SfxPoolItem which is to be casted
216 @tplparam T
217 A SfxPoolItem derived class to cast rItem to
219 @return A rItem upcasted back to a T
221 @exception std::bad_cast Thrown if the rItem was not a T
223 template<class T> const T & item_cast(const SfxPoolItem &rItem)
225 assert(dynamic_cast<const T *>(&rItem) && "bad type cast");
226 return static_cast<const T &>(rItem);
229 /** Provide a dynamic_cast style cast for SfxPoolItems
231 A SfxPoolItem generally need to be cast back to its original type
232 to be useful, which is both tedious and error prone. So item_cast is
233 a helper template to aid the process and test if the cast is
234 correct.
236 @param pItem
237 The SfxPoolItem which is to be casted
239 @tplparam T
240 A SfxPoolItem derived class to cast pItem to
242 @return A pItem upcasted back to a T or 0 if pItem was not a T
244 template<class T> const T * item_cast(const SfxPoolItem *pItem)
246 return dynamic_cast<const T *>(pItem);
249 /** Get the Paragraph Styles of a SwDoc
251 Writer's styles are in one of those dreaded macro based pre-STL
252 containers. Give me an STL container of the paragraph styles
253 instead.
255 @param rDoc
256 The SwDoc document to get the styles from
258 @return A ParaStyles containing the SwDoc's Paragraph Styles
260 ww8::ParaStyles GetParaStyles(const SwDoc &rDoc);
262 /** Get a Paragraph Style which fits a given name
264 Its surprisingly tricky to get a style when all you have is a name,
265 but that's what this does
267 @param rDoc
268 The SwDoc document to search in
270 @param rName
271 The name of the style to search for
273 @return A Paragraph Style if one exists which matches the name
275 SwTextFormatColl* GetParaStyle(SwDoc &rDoc, const OUString& rName);
277 /** Get a Character Style which fits a given name
279 Its surprisingly tricky to get a style when all you have is a name,
280 but that's what this does
282 @param rDoc
283 The SwDoc document to search in
285 @param rName
286 The name of the style to search for
288 @return A Character Style if one exists which matches the name
290 SwCharFormat* GetCharStyle(SwDoc &rDoc, const OUString& rName);
292 /** Sort sequence of Paragraph Styles by assigned outline style list level
294 Sort ParaStyles in ascending order of assigned outline style list level,
295 e.g. given Normal/Heading1/Heading2/.../Heading10 at their default
296 assigned outline style list levels of body level/level 1/level 2/.../level 10
298 #i98791#
299 adjust the sorting algorithm due to introduced outline level attribute
301 @param rStyles
302 The ParaStyles to sort
304 void SortByAssignedOutlineStyleListLevel(ww8::ParaStyles &rStyles);
306 /** Get the SfxPoolItems of a SfxItemSet
308 Writer's SfxPoolItems (attributes) are in one of those dreaded
309 macro based pre-STL containers. Give me an STL container of the
310 items instead.
312 @param rSet
313 The SfxItemSet to get the items from
315 @param rItems
316 The sw::PoolItems to put the items into
318 void GetPoolItems(const SfxItemSet &rSet, ww8::PoolItems &rItems, bool bExportParentItemSet );
320 const SfxPoolItem *SearchPoolItems(const ww8::PoolItems &rItems,
321 sal_uInt16 eType);
323 template<class T> const T* HasItem(const ww8::PoolItems &rItems,
324 sal_uInt16 eType)
326 return item_cast<T>(SearchPoolItems(rItems, eType));
329 /** Remove properties from an SfxItemSet which a SwFormatCharFormat overrides
331 Given an SfxItemSet and a SwFormatCharFormat remove from the rSet all the
332 properties which the SwFormatCharFormat would override. An SfxItemSet
333 contains attributes, and a SwFormatCharFormat is a "Character Style",
334 so if the SfxItemSet contains bold and so does the character style
335 then delete bold from the SfxItemSet
337 @param
338 rFormat the SwFormatCharFormat which describes the Character Style
340 @param
341 rSet the SfxItemSet from which we want to remove any properties
342 which the rFormat would override
344 @see #i24291# for examples
346 void ClearOverridesFromSet(const SwFormatCharFormat &rFormat, SfxItemSet &rSet);
348 /** Get the Floating elements in a SwDoc
350 Writer's FrameFormats may or may not be anchored to some text content,
351 e.g. Page Anchored elements will not be. For the winword export we
352 need them to have something to be anchored to. So this method
353 returns all the floating elements in a document as a STL container
354 of ww8::Frames which are guaranteed to have an appropriate anchor.
356 @param rDoc
357 The SwDoc document to get the styles from
359 @param pPaM
360 The SwPam to describe the selection in the document to get the
361 elements from. 0 means the entire document.
363 @return A Frames containing the selections Floating elements
365 ww8::Frames GetFrames(const SwDoc &rDoc, SwPaM const *pPaM);
367 /** fix up frame positions, must be called after SetRedlineFlags */
368 void UpdateFramePositions(ww8::Frames & rFrames);
370 /** Get the Frames anchored to a given node
372 Given a container of frames, find the ones anchored to a given node
374 @param rFrames
375 The container of frames to search in
377 @param rNode
378 The SwNode to check for anchors to
380 @return the Frames in rFrames anchored to rNode
382 ww8::Frames GetFramesInNode(const ww8::Frames &rFrames, const SwNode &rNode);
384 /** Get the Numbering Format used on a paragraph
386 There are two differing types of numbering formats that may be on a
387 paragraph, normal and outline. The outline is that numbering you
388 see in tools->outline numbering. There's no difference in the
389 numbering itself, just how you get it from the SwTextNode. Needless
390 to say the filter generally couldn't care less what type of
391 numbering is in use.
393 @param rTextNode
394 The SwTextNode that is the paragraph
396 @return A SwNumFormat pointer that describes the numbering level
397 on this paragraph, or 0 if there is none.
399 const SwNumFormat* GetNumFormatFromTextNode(const SwTextNode &rTextNode);
401 /** Get the Numbering Format for a given level from a numbering rule
403 @param rRule
404 The numbering rule
406 @param nLevel
407 The numbering level
409 @return A SwNumFormat pointer that describes the numbering level
410 or 0 if the nLevel is out of range
412 const SwNumFormat* GetNumFormatFromSwNumRuleLevel(const SwNumRule &rRule,
413 int nLevel);
415 const SwNumRule* GetNumRuleFromTextNode(const SwTextNode &rTextNd);
416 const SwNumRule* GetNormalNumRuleFromTextNode(const SwTextNode &rTextNd);
418 /** Get the SwNoTextNode associated with a SwFrameFormat if here is one
420 There are two differing types of numbering formats that may be on a
421 paragraph, normal and outline. The outline is that numbering you
422 see in tools->outline numbering. There's no difference in the
423 numbering itself, just how you get it from the SwTextNode. Needless
424 to say the filter generally couldn't care less what type of
425 numbering is in use.
427 @param rFormat
428 The SwFrameFormat that may describe a graphic
430 @return A SwNoTextNode pointer that describes the graphic of this
431 frame if there is one, or 0 if there is none.
433 SwNoTextNode *GetNoTextNodeFromSwFrameFormat(const SwFrameFormat &rFormat);
435 /** Does a node have a "page break before" applied
437 Both text nodes and tables in writer can have "page break before"
438 This function gives a unified view to both entities
440 @param rNode
441 The SwNode to query the page break of
443 @return true if there is a page break, false otherwise
445 bool HasPageBreak(const SwNode &rNode);
447 /** Make a best fit Polygon from a PolyPolygon
449 For custom contours in writer we use a PolyPolygon, while word uses
450 a simple polygon, so we need to try and make the best polygon from
451 a PolyPolygon
453 @param rPolyPoly
454 The tools::PolyPolygon to try and turn into a Polygon
456 @return best fit Polygon from rPolyPoly
458 tools::Polygon PolygonFromPolyPolygon(const tools::PolyPolygon &rPolyPoly);
460 /// Undo all scaling / move tricks of the wrap polygon done during import.
461 tools::Polygon CorrectWordWrapPolygonForExport(const tools::PolyPolygon& rPolyPoly, const SwNoTextNode* pNd, bool bCorrectCrop);
463 /** Make setting a drawing object's layer in a Writer document easy
465 Word has the simple concept of a drawing object either in the
466 foreground and in the background. We have an additional complexity
467 that form components live in a separate layer, which seems
468 unnecessarily complicated. So in the winword filter we set the
469 object's layer through this class with either SendObjectToHell for
470 the bottom layer and SendObjectToHeaven for the top and we don't
471 worry about the odd form layer design wrinkle.
473 class SetLayer
475 private:
476 SdrLayerID mnHeavenLayer, mnHellLayer, mnFormLayer;
477 enum Layer {eHeaven, eHell};
478 void SetObjectLayer(SdrObject &rObject, Layer eLayer) const;
479 public:
481 /** Make Object live in the bottom drawing layer
483 @param rObject
484 The object to be set to the bottom layer
486 void SendObjectToHell(SdrObject &rObject) const;
488 /** Make Object lives in the top layer
490 @param rObject
491 The object to be set to the top layer
493 void SendObjectToHeaven(SdrObject &rObject) const;
495 /** Normal constructor
497 @param rDoc
498 The Writer document whose drawing layers we will be inserting
499 objects into
501 explicit SetLayer(const SwDoc &rDoc);
504 const SwCharFormat* GetSwCharFormat(const SwFormatINetFormat& rINet, SwDoc& rDoc);
507 namespace hack
509 /** Map an ID valid in one SfxItemPool to its equivalent in another
511 Given a WhichId (the id that identifies a property e.g. bold) which
512 is correct in a given SfxItemPool, get the equivalent whichId in
513 another SfxItemPool
515 This arises because the drawing layer uses the same properties as
516 writer e.g. SvxWeight, but for some reason uses different ids
517 for the same properties as writer.
519 @param rDestPool
520 The SfxItemPool in whose terms the Id is returned
522 @param rSrcPool
523 The SfxItemPool in whose terms the Id is passed in
525 @param nWhich
526 The Id to transform from source to dest
528 @return 0 on failure, the correct property Id on success
530 sal_uInt16 TransformWhichBetweenPools(const SfxItemPool &rDestPool,
531 const SfxItemPool &rSrcPool, sal_uInt16 nWhich);
533 /** Map a SwDoc WhichId to the equivalent Id for a given SfxItemSet
535 Given a WhichId (the id that identifies a property e.g. bold) which
536 is correct for a Writer document, get the equivalent whichId which
537 for a given SfxItemSet.
539 This arises because the drawing layer uses the same properties as
540 writer e.g. SvxWeight, but for some reason uses different ids
541 for the same properties as writer.
543 This is effectively the same as TransformWhichBetweenPools except
544 at a slightly different layer.
546 @param rSet
547 The SfxItemSet in whose terms the Id is returned
549 @param rDoc
550 The SwDoc in whose terms the Id is passed in
552 @param nWhich
553 The Id to transform from writer to the SfxItemSet's domain
555 @return 0 on failure, the correct SfxItemSet Id on success
557 sal_uInt16 GetSetWhichFromSwDocWhich(const SfxItemSet &rSet,
558 const SwDoc &rDoc, sal_uInt16 nWhich);
560 /** Make inserting an OLE object into a Writer document easy
562 The rest of Office uses SdrOle2Obj for their OLE objects, Writer
563 doesn't, which makes things a bit difficult as this is the type of
564 object that the escher import code shared by the MSOffice filters
565 produces when it imports an OLE object.
567 This utility class takes ownership of the OLE object away from a
568 SdrOle2Obj and can massage it into the condition best suited to
569 insertion into Writer.
571 If the object was not transferred into Writer then it is deleted
572 during destruction.
574 class DrawingOLEAdaptor
576 private:
577 css::uno::Reference < css::embed::XEmbeddedObject > mxIPRef;
578 SfxObjectShell& mrPers;
579 const Graphic* mpGraphic;
580 public:
581 /** Take ownership of a SdrOle2Objs OLE object
583 @param rObj
584 The SdrOle2Obj whose OLE object we want to take control of
586 @param rPers
587 The SvPersist of a SwDoc (SwDoc::GetPersist()) into which we
588 may want to move the object, or remove it from if unwanted.
590 DrawingOLEAdaptor(SdrOle2Obj &rObj, SfxObjectShell &rPers);
592 /// Destructor will destroy the owned OLE object if not transferred
593 ~DrawingOLEAdaptor();
595 /** Transfer ownership of the OLE object to a document's SvPersist
597 TransferToDoc moves the object into the persist under the name
598 passed in. This name is then suitable to be used as an argument
599 to SwDoc::InsertOLE.
601 The object is no longer owned by the adaptor after this call,
602 subsequent calls are an error and return false.
604 @param rName
605 The name to store the object under in the document.
607 @return On success true is returned, otherwise false. On
608 success rName is then suitable for user with SwDoc::InsertOLE
610 bool TransferToDoc(OUString &rName);
611 private:
612 DrawingOLEAdaptor& operator=(const DrawingOLEAdaptor&) = delete;
613 DrawingOLEAdaptor(const DrawingOLEAdaptor &rDoc) = delete;
618 #endif
620 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */