1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <svx/svxids.hrc>
21 #include <sfx2/request.hxx>
22 #include <sfx2/dispatch.hxx>
23 #include <sfx2/viewfrm.hxx>
24 #include <svx/svdview.hxx>
25 #include <svx/drawitem.hxx>
26 
27 #include <svx/xtable.hxx>
28 #include <view.hxx>
29 #include <wrtsh.hxx>
30 #include <docsh.hxx>
31 #include <cmdid.h>
32 
33 #include <drawsh.hxx>
34 #include <svx/svxdlg.hxx>
35 #include <svx/dialogs.hrc>
36 #include <memory>
37 #include <svl/stritem.hxx>
38 #include <svx/xlnclit.hxx>
39 #include <svx/xflclit.hxx>
40 #include <svx/chrtitem.hxx>
41 #include <svx/xlnwtit.hxx>
42 
ExecDrawDlg(SfxRequest & rReq)43 void SwDrawShell::ExecDrawDlg(SfxRequest& rReq)
44 {
45     SwWrtShell*     pSh     = &GetShell();
46     SdrView*        pView   = pSh->GetDrawView();
47     SdrModel*       pDoc    = pView->GetModel();
48     bool            bChanged = pDoc->IsChanged();
49     pDoc->SetChanged(false);
50 
51     SfxItemSet aNewAttr( pDoc->GetItemPool() );
52     pView->GetAttributes( aNewAttr );
53 
54     GetView().NoRotate();
55 
56     switch (rReq.GetSlot())
57     {
58         case FN_DRAWTEXT_ATTR_DLG:
59         {
60             SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
61             ScopedVclPtr<SfxAbstractTabDialog> pDlg(pFact->CreateTextTabDialog(rReq.GetFrameWeld(), &aNewAttr, pView));
62             sal_uInt16 nResult = pDlg->Execute();
63 
64             if (nResult == RET_OK)
65             {
66                 if (pView->AreObjectsMarked())
67                 {
68                     pSh->StartAction();
69                     pView->SetAttributes(*pDlg->GetOutputItemSet());
70                     rReq.Done(*(pDlg->GetOutputItemSet()));
71                     pSh->EndAction();
72                 }
73             }
74         }
75         break;
76 
77         case SID_MEASURE_DLG:
78         {
79             bool bHasMarked = pView->AreObjectsMarked();
80 
81             SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
82             ScopedVclPtr<SfxAbstractDialog> pDlg(pFact->CreateSfxDialog(rReq.GetFrameWeld(),
83                                                  aNewAttr, pView, RID_SVXPAGE_MEASURE));
84             if (pDlg->Execute() == RET_OK)
85             {
86                 pSh->StartAction();
87                 if (bHasMarked)
88                     pView->SetAttrToMarked(*pDlg->GetOutputItemSet(), false);
89                 else
90                     pView->SetDefaultAttr(*pDlg->GetOutputItemSet(), false);
91                 pSh->EndAction();
92             }
93         }
94         break;
95 
96         case SID_ATTRIBUTES_AREA:
97         {
98             bool bHasMarked = pView->AreObjectsMarked();
99 
100             SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
101             VclPtr<AbstractSvxAreaTabDialog> pDlg(pFact->CreateSvxAreaTabDialog(rReq.GetFrameWeld(),
102                                                                             &aNewAttr,
103                                                                             pDoc,
104                                                                             true));
105 
106             pDlg->StartExecuteAsync([bChanged, bHasMarked, pDoc, pDlg, pSh, pView, this](
107                                         sal_Int32 nResult){
108                 if (nResult == RET_OK)
109                 {
110                     pSh->StartAction();
111                     if (bHasMarked)
112                         pView->SetAttributes(*pDlg->GetOutputItemSet());
113                     else
114                         pView->SetDefaultAttr(*pDlg->GetOutputItemSet(), false);
115                     pSh->EndAction();
116 
117                     static sal_uInt16 aInval[] =
118                     {
119                         SID_ATTR_FILL_STYLE,
120                         SID_ATTR_FILL_COLOR,
121                         SID_ATTR_FILL_TRANSPARENCE,
122                         SID_ATTR_FILL_FLOATTRANSPARENCE,
123                         0
124                     };
125                     SfxBindings &rBnd = GetView().GetViewFrame()->GetBindings();
126                     rBnd.Invalidate(aInval);
127                     rBnd.Update(SID_ATTR_FILL_STYLE);
128                     rBnd.Update(SID_ATTR_FILL_COLOR);
129                     rBnd.Update(SID_ATTR_FILL_TRANSPARENCE);
130                     rBnd.Update(SID_ATTR_FILL_FLOATTRANSPARENCE);
131                 }
132 
133                 if (pDoc->IsChanged())
134                     GetShell().SetModified();
135                 else if (bChanged)
136                     pDoc->SetChanged();
137 
138                 pDlg->disposeOnce();
139             });
140         }
141         break;
142 
143         case SID_ATTRIBUTES_LINE:
144         {
145             bool bHasMarked = pView->AreObjectsMarked();
146 
147             const SdrObject* pObj = nullptr;
148             const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
149             if( rMarkList.GetMarkCount() == 1 )
150                 pObj = rMarkList.GetMark(0)->GetMarkedSdrObj();
151 
152             SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
153             VclPtr<SfxAbstractTabDialog> pDlg(pFact->CreateSvxLineTabDialog(rReq.GetFrameWeld(),
154                     &aNewAttr,
155                 pDoc,
156                 pObj,
157                 bHasMarked));
158 
159             pDlg->StartExecuteAsync([bChanged, bHasMarked, pDoc, pDlg, pSh, pView, this](
160                                         sal_Int32 nResult){
161                 if (nResult == RET_OK)
162                 {
163                     pSh->StartAction();
164                     if(bHasMarked)
165                         pView->SetAttrToMarked(*pDlg->GetOutputItemSet(), false);
166                     else
167                         pView->SetDefaultAttr(*pDlg->GetOutputItemSet(), false);
168                     pSh->EndAction();
169 
170                     static sal_uInt16 aInval[] =
171                     {
172                         SID_ATTR_LINE_STYLE,                // ( SID_SVX_START + 169 )
173                         SID_ATTR_LINE_DASH,                 // ( SID_SVX_START + 170 )
174                         SID_ATTR_LINE_WIDTH,                // ( SID_SVX_START + 171 )
175                         SID_ATTR_LINE_COLOR,                // ( SID_SVX_START + 172 )
176                         SID_ATTR_LINE_START,                // ( SID_SVX_START + 173 )
177                         SID_ATTR_LINE_END,                  // ( SID_SVX_START + 174 )
178                         SID_ATTR_LINE_TRANSPARENCE,         // (SID_SVX_START+1107)
179                         SID_ATTR_LINE_JOINT,                // (SID_SVX_START+1110)
180                         SID_ATTR_LINE_CAP,                  // (SID_SVX_START+1111)
181                         0
182                     };
183 
184                     GetView().GetViewFrame()->GetBindings().Invalidate(aInval);
185                 }
186 
187                 if (pDoc->IsChanged())
188                     GetShell().SetModified();
189                 else if (bChanged)
190                     pDoc->SetChanged();
191 
192                 pDlg->disposeOnce();
193             });
194         }
195         break;
196 
197         default:
198             break;
199     }
200 
201     if (pDoc->IsChanged())
202         GetShell().SetModified();
203     else
204         if (bChanged)
205             pDoc->SetChanged();
206 }
207 
208 namespace
209 {
lcl_convertStringArguments(sal_uInt16 nSlot,std::unique_ptr<SfxItemSet> & pArgs)210     void lcl_convertStringArguments(sal_uInt16 nSlot, std::unique_ptr<SfxItemSet>& pArgs)
211     {
212         Color aColor;
213         OUString sColor;
214         const SfxPoolItem* pItem = nullptr;
215 
216         if (SfxItemState::SET == pArgs->GetItemState(SID_ATTR_COLOR_STR, false, &pItem))
217         {
218             sColor = static_cast<const SfxStringItem*>(pItem)->GetValue();
219 
220             if (sColor == "transparent")
221                 aColor = COL_TRANSPARENT;
222             else
223                 aColor = Color(sColor.toInt32(16));
224 
225             switch (nSlot)
226             {
227                 case SID_ATTR_LINE_COLOR:
228                 {
229                     XLineColorItem aLineColorItem(OUString(), aColor);
230                     pArgs->Put(aLineColorItem);
231                     break;
232                 }
233 
234                 case SID_ATTR_FILL_COLOR:
235                 {
236                     XFillColorItem aFillColorItem(OUString(), aColor);
237                     pArgs->Put(aFillColorItem);
238                     break;
239                 }
240             }
241         }
242         else if (SfxItemState::SET == pArgs->GetItemState(SID_ATTR_LINE_WIDTH_ARG, false, &pItem))
243         {
244             double fValue = static_cast<const SvxDoubleItem*>(pItem)->GetValue();
245             // FIXME: different units...
246             int nPow = 100;
247             int nValue = fValue * nPow;
248 
249             XLineWidthItem aItem(nValue);
250             pArgs->Put(aItem);
251         }
252     }
253 }
254 
ExecDrawAttrArgs(SfxRequest const & rReq)255 void SwDrawShell::ExecDrawAttrArgs(SfxRequest const & rReq)
256 {
257     SwWrtShell* pSh   = &GetShell();
258     SdrView*    pView = pSh->GetDrawView();
259     const SfxItemSet* pArgs = rReq.GetArgs();
260     bool        bChanged = pView->GetModel()->IsChanged();
261     pView->GetModel()->SetChanged(false);
262 
263     GetView().NoRotate();
264 
265     if (pArgs)
266     {
267         if(pView->AreObjectsMarked())
268         {
269             std::unique_ptr<SfxItemSet> pNewArgs = pArgs->Clone();
270             lcl_convertStringArguments(rReq.GetSlot(), pNewArgs);
271             pView->SetAttrToMarked(*pNewArgs, false);
272         }
273         else
274             pView->SetDefaultAttr(*rReq.GetArgs(), false);
275     }
276     else
277     {
278         SfxDispatcher* pDis = pSh->GetView().GetViewFrame()->GetDispatcher();
279         switch (rReq.GetSlot())
280         {
281             case SID_ATTR_FILL_STYLE:
282             case SID_ATTR_FILL_COLOR:
283             case SID_ATTR_FILL_GRADIENT:
284             case SID_ATTR_FILL_HATCH:
285             case SID_ATTR_FILL_BITMAP:
286             case SID_ATTR_FILL_TRANSPARENCE:
287             case SID_ATTR_FILL_FLOATTRANSPARENCE:
288                 pDis->Execute(SID_ATTRIBUTES_AREA);
289                 break;
290             case SID_ATTR_LINE_STYLE:
291             case SID_ATTR_LINE_DASH:
292             case SID_ATTR_LINE_WIDTH:
293             case SID_ATTR_LINE_COLOR:
294             case SID_ATTR_LINE_TRANSPARENCE:
295             case SID_ATTR_LINE_JOINT:
296             case SID_ATTR_LINE_CAP:
297                 pDis->Execute(SID_ATTRIBUTES_LINE);
298                 break;
299         }
300     }
301     if (pView->GetModel()->IsChanged())
302         GetShell().SetModified();
303     else
304         if (bChanged)
305             pView->GetModel()->SetChanged();
306 }
307 
GetDrawAttrState(SfxItemSet & rSet)308 void SwDrawShell::GetDrawAttrState(SfxItemSet& rSet)
309 {
310     SdrView* pSdrView = GetShell().GetDrawView();
311 
312     if (pSdrView->AreObjectsMarked())
313     {
314         bool bDisable = Disable( rSet );
315 
316         if( !bDisable )
317             pSdrView->GetAttributes( rSet );
318     }
319     else
320         rSet.Put(pSdrView->GetDefaultAttr());
321 }
322 
323 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
324