1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #include <hintids.hxx>
23 #include <svl/eitem.hxx>
24 #include <vcl/settings.hxx>
25 #include <vcl/transfer.hxx>
26 #include <sfx2/dispatch.hxx>
27 #include <sfx2/viewfrm.hxx>
28 #include <svx/gallery.hxx>
29 #include <svx/graphichelper.hxx>
30 #include <editeng/brushitem.hxx>
32 #include <fmtinfmt.hxx>
36 #include <viewopt.hxx>
37 #include <swmodule.hxx>
39 #include <pagedesc.hxx>
44 using namespace ::com::sun::star
;
46 SwReadOnlyPopup::~SwReadOnlyPopup()
48 m_xMenu
.disposeAndClear();
51 void SwReadOnlyPopup::Check( sal_uInt16 nMID
, sal_uInt16 nSID
, SfxDispatcher
const &rDis
)
53 std::unique_ptr
<SfxPoolItem
> _pItem
;
54 SfxItemState eState
= rDis
.GetBindings()->QueryState( nSID
, _pItem
);
55 if (eState
>= SfxItemState::DEFAULT
)
57 m_xMenu
->EnableItem(nMID
);
60 m_xMenu
->CheckItem(nMID
, !IsDisabledItem(_pItem
.get()) &&
61 dynamic_cast< const SfxBoolItem
*>( _pItem
.get() ) != nullptr &&
62 static_cast<SfxBoolItem
*>(_pItem
.get())->GetValue());
63 //remove full screen entry when not in full screen mode
64 if (SID_WIN_FULLSCREEN
== nSID
&& !m_xMenu
->IsItemChecked(m_nReadonlyFullscreen
))
65 m_xMenu
->EnableItem(nMID
, false);
69 m_xMenu
->EnableItem(nMID
, false);
72 #define MN_READONLY_GRAPHICTOGALLERY 1000
73 #define MN_READONLY_BACKGROUNDTOGALLERY 2000
75 SwReadOnlyPopup::SwReadOnlyPopup(const Point
&rDPos
, SwView
&rV
)
76 : m_aBuilder(nullptr, AllSettings::GetUIRootDir(), u
"modules/swriter/ui/readonlymenu.ui"_ustr
, u
""_ustr
)
77 , m_xMenu(m_aBuilder
.get_menu(u
"menu"))
78 , m_nReadonlyOpenurl(m_xMenu
->GetItemId(u
"openurl"))
79 , m_nReadonlyOpendoc(m_xMenu
->GetItemId(u
"opendoc"))
80 , m_nReadonlyEditdoc(m_xMenu
->GetItemId(u
"edit"))
81 , m_nReadonlySelectionMode(m_xMenu
->GetItemId(u
"selection"))
82 , m_nReadonlyReload(m_xMenu
->GetItemId(u
"reload"))
83 , m_nReadonlyReloadFrame(m_xMenu
->GetItemId(u
"reloadframe"))
84 , m_nReadonlySourceview(m_xMenu
->GetItemId(u
"html"))
85 , m_nReadonlyBrowseBackward(m_xMenu
->GetItemId(u
"backward"))
86 , m_nReadonlyBrowseForward(m_xMenu
->GetItemId(u
"forward"))
87 , m_nReadonlySaveGraphic(m_xMenu
->GetItemId(u
"savegraphic"))
88 , m_nReadonlyGraphictogallery(m_xMenu
->GetItemId(u
"graphictogallery"))
89 , m_nReadonlyTogallerylink(m_xMenu
->GetItemId(u
"graphicaslink"))
90 , m_nReadonlyTogallerycopy(m_xMenu
->GetItemId(u
"graphicascopy"))
91 , m_nReadonlySaveBackground(m_xMenu
->GetItemId(u
"savebackground"))
92 , m_nReadonlyBackgroundtogallery(m_xMenu
->GetItemId(u
"backgroundtogallery"))
93 , m_nReadonlyBackgroundTogallerylink(m_xMenu
->GetItemId(u
"backaslink"))
94 , m_nReadonlyBackgroundTogallerycopy(m_xMenu
->GetItemId(u
"backascopy"))
95 , m_nReadonlyCopylink(m_xMenu
->GetItemId(u
"copylink"))
96 , m_nReadonlyLoadGraphic(m_xMenu
->GetItemId(u
"loadgraphic"))
97 , m_nReadonlyGraphicoff(m_xMenu
->GetItemId(u
"imagesoff"))
98 , m_nReadonlyFullscreen(m_xMenu
->GetItemId(u
"fullscreen"))
99 , m_nReadonlyCopyField(m_xMenu
->GetItemId(u
"copyfield"))
100 , m_nReadonlyCopy(m_xMenu
->GetItemId(u
"copy"))
102 , m_xBrushItem(std::make_unique
<SvxBrushItem
>(RES_BACKGROUND
))
104 m_bGrfToGalleryAsLnk
= SwModule::get()->GetModuleConfig()->IsGrfToGalleryAsLnk();
105 SwWrtShell
&rSh
= m_rView
.GetWrtShell();
106 OUString sDescription
;
107 rSh
.IsURLGrfAtPos( rDPos
, &m_sURL
, &m_sTargetFrameName
, &sDescription
);
108 if ( m_sURL
.isEmpty() )
110 SwContentAtPos
aContentAtPos( IsAttrAtPos::InetAttr
);
111 if( rSh
.GetContentAtPos( rDPos
, aContentAtPos
))
113 const SwFormatINetFormat
&rIItem
= *static_cast<const SwFormatINetFormat
*>(aContentAtPos
.aFnd
.pAttr
);
114 m_sURL
= rIItem
.GetValue();
115 m_sTargetFrameName
= rIItem
.GetTargetFrame();
120 const Graphic
*pGrf
= rSh
.GetGrfAtPos( rDPos
, m_sGrfName
, bLink
);
121 if ( nullptr == pGrf
)
123 m_xMenu
->EnableItem(m_nReadonlySaveGraphic
, false);
130 bool bEnableGraphicToGallery
= bLink
;
131 if ( bEnableGraphicToGallery
)
133 if (GalleryExplorer::FillThemeList( m_aThemeList
))
135 PopupMenu
*pMenu
= m_xMenu
->GetPopupMenu(m_nReadonlyGraphictogallery
);
136 pMenu
->CheckItem(m_nReadonlyTogallerylink
, m_bGrfToGalleryAsLnk
);
137 pMenu
->CheckItem(m_nReadonlyTogallerycopy
, !m_bGrfToGalleryAsLnk
);
139 for ( size_t i
=0; i
< m_aThemeList
.size(); ++i
)
140 pMenu
->InsertItem(MN_READONLY_GRAPHICTOGALLERY
+ i
, m_aThemeList
[i
]);
143 bEnableGraphicToGallery
= false;
146 m_xMenu
->EnableItem(m_nReadonlyGraphictogallery
, bEnableGraphicToGallery
);
148 SfxViewFrame
& rVFrame
= rV
.GetViewFrame();
149 SfxDispatcher
&rDis
= *rVFrame
.GetDispatcher();
150 const SwPageDesc
&rDesc
= rSh
.GetPageDesc( rSh
.GetCurPageDesc() );
151 m_xBrushItem
= rDesc
.GetMaster().makeBackgroundBrushItem();
152 bool bEnableBackGallery
= false,
155 if ( m_xBrushItem
&& GPOS_NONE
!= m_xBrushItem
->GetGraphicPos() )
158 if ( !m_xBrushItem
->GetGraphicLink().isEmpty() )
160 if ( m_aThemeList
.empty() )
161 GalleryExplorer::FillThemeList( m_aThemeList
);
163 if ( !m_aThemeList
.empty() )
165 PopupMenu
*pMenu
= m_xMenu
->GetPopupMenu(m_nReadonlyBackgroundtogallery
);
166 pMenu
->CheckItem(m_nReadonlyBackgroundTogallerylink
, m_bGrfToGalleryAsLnk
);
167 pMenu
->CheckItem(m_nReadonlyBackgroundTogallerycopy
, !m_bGrfToGalleryAsLnk
);
168 bEnableBackGallery
= true;
170 for ( size_t i
=0; i
< m_aThemeList
.size(); ++i
)
171 pMenu
->InsertItem(MN_READONLY_BACKGROUNDTOGALLERY
+ i
, m_aThemeList
[i
]);
175 m_xMenu
->EnableItem(m_nReadonlySaveBackground
, bEnableBack
);
176 m_xMenu
->EnableItem(m_nReadonlyBackgroundtogallery
, bEnableBackGallery
);
178 if ( !rSh
.GetViewOptions()->IsGraphic() )
179 m_xMenu
->CheckItem(m_nReadonlyGraphicoff
);
181 m_xMenu
->EnableItem(m_nReadonlyLoadGraphic
, false);
183 m_xMenu
->EnableItem(m_nReadonlyReloadFrame
, false);
184 m_xMenu
->EnableItem(m_nReadonlyReload
);
186 Check(m_nReadonlyEditdoc
, SID_EDITDOC
, rDis
);
187 Check(m_nReadonlySelectionMode
, FN_READONLY_SELECTION_MODE
, rDis
);
188 Check(m_nReadonlySourceview
, SID_SOURCEVIEW
, rDis
);
189 Check(m_nReadonlyBrowseBackward
, SID_BROWSE_BACKWARD
, rDis
);
190 Check(m_nReadonlyBrowseForward
,SID_BROWSE_FORWARD
, rDis
);
191 Check(m_nReadonlyOpenurl
, SID_OPENDOC
, rDis
);
192 Check(m_nReadonlyOpendoc
, SID_OPENDOC
, rDis
);
194 std::unique_ptr
<SfxPoolItem
> pState
;
196 SfxItemState eState
= rVFrame
.GetBindings().QueryState( SID_COPY
, pState
);
197 Check(m_nReadonlyCopy
, SID_COPY
, rDis
);
198 if (eState
< SfxItemState::DEFAULT
)
199 m_xMenu
->EnableItem(m_nReadonlyCopy
, false);
201 eState
= rVFrame
.GetBindings().QueryState( SID_EDITDOC
, pState
);
203 eState
< SfxItemState::DEFAULT
||
204 (rSh
.IsGlobalDoc() && m_rView
.GetDocShell()->IsReadOnlyUI())
207 m_xMenu
->EnableItem(m_nReadonlyEditdoc
, false);
210 if ( m_sURL
.isEmpty() )
212 m_xMenu
->EnableItem(m_nReadonlyOpenurl
, false);
213 m_xMenu
->EnableItem(m_nReadonlyOpendoc
, false);
214 m_xMenu
->EnableItem(m_nReadonlyCopylink
, false);
216 Check(m_nReadonlyFullscreen
, SID_WIN_FULLSCREEN
, rDis
);
217 eState
= rVFrame
.GetBindings().QueryState(FN_COPY_FIELD
, pState
);
218 m_xMenu
->EnableItem(m_nReadonlyCopyField
, eState
> SfxItemState::DISABLED
);
219 m_xMenu
->RemoveDisabledEntries( true );
222 void SwReadOnlyPopup::Execute( vcl::Window
* pWin
, const Point
&rPixPos
)
224 sal_uInt16 nId
= m_xMenu
->Execute(pWin
, rPixPos
);
228 // execute the resulting ID only - necessary to support XContextMenuInterception
229 void SwReadOnlyPopup::Execute( vcl::Window
* pWin
, sal_uInt16 nId
)
231 SwWrtShell
&rSh
= m_rView
.GetWrtShell();
232 SfxDispatcher
&rDis
= *m_rView
.GetViewFrame().GetDispatcher();
233 if (nId
>= MN_READONLY_GRAPHICTOGALLERY
)
237 if (m_xBrushItem
&& nId
>= MN_READONLY_BACKGROUNDTOGALLERY
)
239 nId
-= MN_READONLY_BACKGROUNDTOGALLERY
;
240 nSaveId
= m_nReadonlySaveBackground
;
241 sTmp
= m_xBrushItem
->GetGraphicLink();
245 nId
-= MN_READONLY_GRAPHICTOGALLERY
;
246 nSaveId
= m_nReadonlySaveGraphic
;
249 if ( !m_bGrfToGalleryAsLnk
)
250 sTmp
= SaveGraphic(nSaveId
);
252 if ( !sTmp
.isEmpty() )
253 GalleryExplorer::InsertURL( m_aThemeList
[nId
], sTmp
);
258 rtl::Reference
<TransferDataContainer
> pClipCntnr
;
260 sal_uInt16 nExecId
= USHRT_MAX
;
261 bool bFilterSet
= false;
262 LoadUrlFlags nFilter
= LoadUrlFlags::NONE
;
263 if (nId
== m_nReadonlyFullscreen
)
264 nExecId
= SID_WIN_FULLSCREEN
;
265 else if (nId
== m_nReadonlyOpenurl
)
267 nFilter
= LoadUrlFlags::NONE
;
270 else if (nId
== m_nReadonlyOpendoc
)
272 nFilter
= LoadUrlFlags::NewView
;
275 else if (nId
== m_nReadonlyCopy
)
277 else if (nId
== m_nReadonlyEditdoc
)
278 nExecId
= SID_EDITDOC
;
279 else if (nId
== m_nReadonlySelectionMode
)
280 nExecId
= FN_READONLY_SELECTION_MODE
;
281 else if (nId
== m_nReadonlyReload
|| nId
== m_nReadonlyReloadFrame
)
282 rSh
.GetView().GetViewFrame().GetDispatcher()->Execute(SID_RELOAD
);
283 else if (nId
== m_nReadonlyBrowseBackward
)
284 nExecId
= SID_BROWSE_BACKWARD
;
285 else if (nId
== m_nReadonlyBrowseForward
)
286 nExecId
= SID_BROWSE_FORWARD
;
287 else if (nId
== m_nReadonlySourceview
)
288 nExecId
= SID_SOURCEVIEW
;
289 else if (nId
== m_nReadonlyCopyField
)
290 nExecId
= FN_COPY_FIELD
;
291 else if (nId
== m_nReadonlySaveGraphic
|| nId
== m_nReadonlySaveBackground
)
293 else if (nId
== m_nReadonlyCopylink
)
295 pClipCntnr
= new TransferDataContainer
;
296 pClipCntnr
->CopyString( m_sURL
);
298 else if (nId
== m_nReadonlyLoadGraphic
)
300 bool bModified
= rSh
.IsModified();
301 SwViewOption
aOpt( *rSh
.GetViewOptions() );
302 aOpt
.SetGraphic( true );
303 rSh
.ApplyViewOptions( aOpt
);
307 else if (nId
== m_nReadonlyGraphicoff
)
308 nExecId
= FN_VIEW_GRAPHIC
;
309 else if (nId
== m_nReadonlyTogallerylink
|| nId
== m_nReadonlyBackgroundTogallerylink
)
310 SwModule::get()->GetModuleConfig()->SetGrfToGalleryAsLnk(true);
311 else if (nId
== m_nReadonlyTogallerycopy
|| nId
== m_nReadonlyBackgroundTogallerycopy
)
312 SwModule::get()->GetModuleConfig()->SetGrfToGalleryAsLnk(false);
314 if( USHRT_MAX
!= nExecId
)
315 rDis
.GetBindings()->Execute( nExecId
);
317 ::LoadURL(rSh
, m_sURL
, nFilter
, m_sTargetFrameName
);
319 if( pClipCntnr
&& pClipCntnr
->HasAnyData() )
321 pClipCntnr
->CopyToClipboard( pWin
);
325 OUString
SwReadOnlyPopup::SaveGraphic(sal_uInt16 nId
)
327 // fish out the graphic's name
328 if (nId
== m_nReadonlySaveBackground
)
330 if ( m_xBrushItem
&& !m_xBrushItem
->GetGraphicLink().isEmpty() )
331 m_sGrfName
= m_xBrushItem
->GetGraphicLink();
332 const Graphic
*pGrf
= m_xBrushItem
? m_xBrushItem
->GetGraphic() : nullptr;
336 if ( !m_xBrushItem
->GetGraphicLink().isEmpty() )
337 m_sGrfName
= m_xBrushItem
->GetGraphicLink();
342 return GraphicHelper::ExportGraphic(m_rView
.GetFrameWeld(), m_aGraphic
, m_sGrfName
);
345 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */