tdf#164627 docx export: consolidate getWordCompatibilityMode()
[LibreOffice.git] / svtools / source / control / toolbarmenu.cxx
blob5d5fc8d375a1e08ff6158f968589ffbb8301a611
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 <memory>
21 #include <comphelper/lok.hxx>
22 #include <comphelper/processfactory.hxx>
24 #include <utility>
25 #include <vcl/taskpanelist.hxx>
26 #include <vcl/svapp.hxx>
28 #include <framestatuslistener.hxx>
29 #include <svtools/toolbarmenu.hxx>
31 using namespace ::com::sun::star::uno;
32 using namespace ::com::sun::star::lang;
33 using namespace ::com::sun::star::frame;
35 namespace {
37 SystemWindow* GetTopMostParentSystemWindow(const vcl::Window& rWindow)
39 // ->manually search topmost system window
40 // required because their might be another system window between this and the top window
41 vcl::Window* pWindow = rWindow.GetParent();
42 SystemWindow* pTopMostSysWin = nullptr;
43 while ( pWindow )
45 if ( pWindow->IsSystemWindow() )
46 pTopMostSysWin = static_cast<SystemWindow*>(pWindow);
47 pWindow = pWindow->GetParent();
49 return pTopMostSysWin;
52 class ToolbarPopupStatusListener : public svt::FrameStatusListener
54 public:
55 ToolbarPopupStatusListener( const css::uno::Reference< css::frame::XFrame >& xFrame,
56 WeldToolbarPopup& rToolbarPopup );
58 virtual void SAL_CALL dispose() override;
59 virtual void SAL_CALL statusChanged( const css::frame::FeatureStateEvent& Event ) override;
61 WeldToolbarPopup* mpPopup;
65 ToolbarPopupStatusListener::ToolbarPopupStatusListener(
66 const css::uno::Reference< css::frame::XFrame >& xFrame,
67 WeldToolbarPopup& rToolbarPopup )
68 : svt::FrameStatusListener( ::comphelper::getProcessComponentContext(), xFrame )
69 , mpPopup( &rToolbarPopup )
74 void SAL_CALL ToolbarPopupStatusListener::dispose()
76 mpPopup = nullptr;
77 svt::FrameStatusListener::dispose();
81 void SAL_CALL ToolbarPopupStatusListener::statusChanged( const css::frame::FeatureStateEvent& Event )
83 if( mpPopup )
84 mpPopup->statusChanged( Event );
89 void WeldToolbarPopup::AddStatusListener(const OUString& rCommandURL)
91 if (!m_xStatusListener.is())
92 m_xStatusListener.set(new ToolbarPopupStatusListener(m_xFrame, *this));
94 m_xStatusListener->addStatusListener(rCommandURL);
97 void WeldToolbarPopup::statusChanged(const css::frame::FeatureStateEvent& /*Event*/)
101 void InterimToolbarPopup::EndPopupMode()
103 GetDockingManager()->EndPopupMode(this);
106 WeldToolbarPopup::WeldToolbarPopup(css::uno::Reference<css::frame::XFrame> xFrame,
107 weld::Widget* pParent, const OUString& rUIFile,
108 const OUString& rId)
109 : m_xBuilder(Application::CreateBuilder(pParent, rUIFile))
110 , m_xTopLevel(m_xBuilder->weld_popover(rId))
111 , m_xContainer(m_xBuilder->weld_container(u"container"_ustr))
112 , m_xFrame(std::move(xFrame))
114 m_xTopLevel->connect_focus_in(LINK(this, WeldToolbarPopup, FocusHdl));
117 WeldToolbarPopup::~WeldToolbarPopup()
119 if (m_xStatusListener.is())
120 m_xStatusListener->dispose();
123 IMPL_LINK_NOARG(WeldToolbarPopup, FocusHdl, weld::Widget&, void)
125 GrabFocus();
128 ToolbarPopupContainer::ToolbarPopupContainer(weld::Widget* pParent)
129 : m_xBuilder(Application::CreateBuilder(pParent, u"svx/ui/toolbarpopover.ui"_ustr))
130 , m_xTopLevel(m_xBuilder->weld_container(u"ToolbarPopover"_ustr))
131 , m_xContainer(m_xBuilder->weld_container(u"container"_ustr))
133 m_xTopLevel->connect_focus_in(LINK(this, ToolbarPopupContainer, FocusHdl));
136 void ToolbarPopupContainer::setPopover(std::unique_ptr<WeldToolbarPopup> xPopup)
138 m_xPopup = std::move(xPopup);
139 // move the WeldToolbarPopup contents into this toolbar so on-demand contents can appear inside a preexisting gtk popover
140 // because the arrow for the popover is only enabled if there's a popover set
141 m_xPopup->getTopLevel()->move(m_xPopup->getContainer(), m_xContainer.get());
143 // in online LoseFocus event is fired due to this line and popup is closed
144 // when first time opened any popup from not focused sidebar
145 if (!comphelper::LibreOfficeKit::isActive())
146 m_xPopup->GrabFocus();
149 void ToolbarPopupContainer::unsetPopover()
151 if (!m_xPopup)
152 return;
153 m_xContainer->move(m_xPopup->getContainer(), m_xPopup->getTopLevel());
154 m_xPopup.reset();
157 ToolbarPopupContainer::~ToolbarPopupContainer()
159 unsetPopover();
162 IMPL_LINK_NOARG(ToolbarPopupContainer, FocusHdl, weld::Widget&, void)
164 if (m_xPopup)
165 m_xPopup->GrabFocus();
168 InterimToolbarPopup::InterimToolbarPopup(const css::uno::Reference<css::frame::XFrame>& rFrame, vcl::Window* pParent,
169 std::unique_ptr<WeldToolbarPopup> xPopup, bool bTearable)
170 : DropdownDockingWindow(pParent, rFrame, bTearable)
171 , m_xFrame(rFrame)
172 , m_xBuilder(Application::CreateInterimBuilder(m_xBox.get(), u"svt/ui/interimparent.ui"_ustr, false))
173 , m_xContainer(m_xBuilder->weld_container(u"container"_ustr))
174 , m_xPopup(std::move(xPopup))
176 if (SystemWindow* pWindow = GetTopMostParentSystemWindow(*this))
177 pWindow->GetTaskPaneList()->AddWindow(this);
179 // move the WeldToolbarPopup contents into this interim toolbar so welded contents can appear as a dropdown in an unwelded toolbar
180 m_xPopup->getTopLevel()->move(m_xPopup->getContainer(), m_xContainer.get());
183 void InterimToolbarPopup::GetFocus()
185 DropdownDockingWindow::GetFocus();
186 if (!m_xPopup)
187 return;
188 m_xPopup->GrabFocus();
191 void InterimToolbarPopup::dispose()
193 if (SystemWindow* pWindow = GetTopMostParentSystemWindow(*this))
194 pWindow->GetTaskPaneList()->RemoveWindow(this);
196 // if we have focus when disposed, pick the document window as destination
197 // for focus rather than let it go to an arbitrary windows
198 if (HasFocus())
200 if (auto xWindow = m_xFrame->getContainerWindow())
201 xWindow->setFocus();
203 // move the contents back where it belongs
204 m_xContainer->move(m_xPopup->getContainer(), m_xPopup->getTopLevel());
205 m_xPopup.reset();
206 m_xContainer.reset();
207 m_xBuilder.reset();
208 m_xFrame.clear();
209 DropdownDockingWindow::dispose();
212 InterimToolbarPopup::~InterimToolbarPopup()
214 disposeOnce();
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */