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 .
20 #include <sfx2/sidebar/TabBar.hxx>
21 #include <sidebar/DeckDescriptor.hxx>
22 #include <sfx2/sidebar/Theme.hxx>
23 #include <sidebar/Tools.hxx>
24 #include <sfx2/sidebar/FocusManager.hxx>
25 #include <sfx2/sidebar/SidebarController.hxx>
27 #include <comphelper/lok.hxx>
28 #include <comphelper/processfactory.hxx>
29 #include <o3tl/safeint.hxx>
31 #include <vcl/commandevent.hxx>
32 #include <vcl/commandinfoprovider.hxx>
33 #include <vcl/event.hxx>
34 #include <vcl/svapp.hxx>
35 #include <svtools/acceleratorexecute.hxx>
36 #include <osl/diagnose.h>
38 #include "uiobject.hxx"
41 using namespace css::uno
;
43 static int gDefaultWidth
;
45 namespace sfx2::sidebar
{
47 TabBar::TabBar(vcl::Window
* pParentWindow
,
48 const Reference
<frame::XFrame
>& rxFrame
,
49 std::function
<void (const OUString
&)> aDeckActivationFunctor
,
50 PopupMenuProvider aPopupMenuProvider
,
51 SidebarController
* rParentSidebarController
53 : InterimItemWindow(pParentWindow
, "sfx/ui/tabbar.ui", "TabBar")
55 , mxAuxBuilder(Application::CreateBuilder(m_xContainer
.get(), "sfx/ui/tabbarcontents.ui"))
56 , mxTempToplevel(mxAuxBuilder
->weld_box("toplevel"))
57 , mxContents(mxAuxBuilder
->weld_widget("TabBarContents"))
58 , mxMeasureBox(mxAuxBuilder
->weld_widget("measure"))
59 , maDeckActivationFunctor(std::move(aDeckActivationFunctor
))
60 , maPopupMenuProvider(std::move(aPopupMenuProvider
))
61 , pParentSidebarController(rParentSidebarController
)
63 set_id("TabBar"); // for uitest
65 InitControlBase(mxMenuButton
.get());
67 mxTempToplevel
->move(mxContents
.get(), m_xContainer
.get());
69 // For Gtk4 defer menu_button until after the contents have been
70 // transferred to its final home (where the old parent is a GtkWindow to
71 // support loading the accelerators in the menu for Gtk3)
72 mxMenuButton
= mxAuxBuilder
->weld_menu_button("menubutton");
73 mxMainMenu
= mxAuxBuilder
->weld_menu("mainmenu");
74 mxSubMenu
= mxAuxBuilder
->weld_menu("submenu");
76 gDefaultWidth
= m_xContainer
->get_preferred_size().Width();
78 // we have this widget just so we can measure best width for static TabBar::GetDefaultWidth
81 SetBackground(Wallpaper(Theme::GetColor(Theme::Color_TabBarBackground
)));
83 mxMenuButton
->connect_toggled(LINK(this, TabBar
, OnToolboxClicked
));
86 SetText(OUString("TabBar"));
95 void TabBar::dispose()
101 mxMenuButton
.reset();
102 m_xContainer
->move(mxContents
.get(), mxTempToplevel
.get());
104 mxTempToplevel
.reset();
105 mxAuxBuilder
.reset();
106 InterimItemWindow::dispose();
109 sal_Int32
TabBar::GetDefaultWidth()
113 std::unique_ptr
<weld::Builder
> xBuilder(Application::CreateBuilder(nullptr, "sfx/ui/tabbarcontents.ui"));
114 std::unique_ptr
<weld::Widget
> xContainer(xBuilder
->weld_widget("TabBarContents"));
115 gDefaultWidth
= xContainer
->get_preferred_size().Width();
117 return gDefaultWidth
;
120 void TabBar::SetDecks(const ResourceManager::DeckContextDescriptorContainer
& rDecks
)
122 // invisible with LOK, so keep empty to avoid invalidations
123 if (comphelper::LibreOfficeKit::isActive())
126 // Remove the current buttons.
128 for (auto const& deck
: rDecks
)
130 std::shared_ptr
<DeckDescriptor
> xDescriptor
= pParentSidebarController
->GetResourceManager()->GetDeckDescriptor(deck
.msId
);
131 if (xDescriptor
== nullptr)
133 OSL_ASSERT(xDescriptor
!=nullptr);
137 maItems
.emplace_back(std::make_unique
<Item
>(*this));
138 auto& xItem(maItems
.back());
139 xItem
->msDeckId
= xDescriptor
->msId
;
140 CreateTabItem(*xItem
->mxButton
, *xDescriptor
);
141 xItem
->mxButton
->connect_clicked(LINK(xItem
.get(), TabBar::Item
, HandleClick
));
142 xItem
->maDeckActivationFunctor
= maDeckActivationFunctor
;
143 xItem
->mbIsHidden
= !xDescriptor
->mbIsEnabled
;
144 xItem
->mbIsHiddenByDefault
= xItem
->mbIsHidden
; // the default is the state while creating
146 xItem
->mxButton
->set_sensitive(deck
.mbIsEnabled
);
152 void TabBar::UpdateButtonIcons()
154 for (auto const& item
: maItems
)
156 std::shared_ptr
<DeckDescriptor
> xDeckDescriptor
= pParentSidebarController
->GetResourceManager()->GetDeckDescriptor(item
->msDeckId
);
157 if (!xDeckDescriptor
)
159 item
->mxButton
->set_item_image("toggle", GetItemImage(*xDeckDescriptor
));
163 void TabBar::HighlightDeck(std::u16string_view rsDeckId
)
165 for (auto const& item
: maItems
)
166 item
->mxButton
->set_item_active("toggle", item
->msDeckId
== rsDeckId
);
169 void TabBar::RemoveDeckHighlight()
171 for (auto const& item
: maItems
)
172 item
->mxButton
->set_item_active("toggle", false);
175 void TabBar::DataChanged(const DataChangedEvent
& rDataChangedEvent
)
177 SetBackground(Theme::GetColor(Theme::Color_TabBarBackground
));
180 InterimItemWindow::DataChanged(rDataChangedEvent
);
183 bool TabBar::EventNotify(NotifyEvent
& rEvent
)
185 NotifyEventType nType
= rEvent
.GetType();
186 if(NotifyEventType::KEYINPUT
== nType
)
188 const vcl::KeyCode
& rKeyCode
= rEvent
.GetKeyEvent()->GetKeyCode();
191 mpAccel
= svt::AcceleratorExecute::createAcceleratorHelper();
192 mpAccel
->init(comphelper::getProcessComponentContext(), mxFrame
);
194 const OUString
aCommand(mpAccel
->findCommand(svt::AcceleratorExecute::st_VCLKey2AWTKey(rKeyCode
)));
195 if (".uno:Sidebar" == aCommand
||
196 (rKeyCode
.IsMod1() && rKeyCode
.IsShift() && rKeyCode
.GetCode() == KEY_F10
))
197 return InterimItemWindow::EventNotify(rEvent
);
200 else if(NotifyEventType::COMMAND
== nType
)
202 const CommandEvent
& rCommandEvent
= *rEvent
.GetCommandEvent();
203 if(rCommandEvent
.GetCommand() == CommandEventId::Wheel
)
205 const CommandWheelData
* pData
= rCommandEvent
.GetWheelData();
206 if(!pData
->GetModifier() && (pData
->GetMode() == CommandWheelMode::SCROLL
))
208 auto pItem
= std::find_if(maItems
.begin(), maItems
.end(),
209 [] (const auto& item
) { return item
->mxButton
->get_item_active("toggle"); });
210 if(pItem
== maItems
.end())
212 if(pData
->GetNotchDelta()<0)
214 if(pItem
+1 == maItems
.end())
220 if(pItem
== maItems
.begin())
226 (*pItem
)->maDeckActivationFunctor((*pItem
)->msDeckId
);
228 catch(const css::uno::Exception
&) {};
236 void TabBar::CreateTabItem(weld::Toolbar
& rItem
, const DeckDescriptor
& rDeckDescriptor
)
238 rItem
.set_accessible_name(rDeckDescriptor
.msTitle
);
239 rItem
.set_accessible_description(rDeckDescriptor
.msHelpText
);
240 rItem
.set_tooltip_text(rDeckDescriptor
.msHelpText
);
241 const OUString sCommand
= ".uno:SidebarDeck." + rDeckDescriptor
.msId
;
242 OUString sShortcut
= vcl::CommandInfoProvider::GetCommandShortcut(sCommand
, mxFrame
);
243 if (!sShortcut
.isEmpty())
244 sShortcut
= u
" (" + sShortcut
+ u
")";
245 rItem
.set_item_tooltip_text("toggle", rDeckDescriptor
.msHelpText
+ sShortcut
);
248 css::uno::Reference
<css::graphic::XGraphic
> TabBar::GetItemImage(const DeckDescriptor
& rDeckDescriptor
) const
250 return Tools::GetImage(
251 rDeckDescriptor
.msIconURL
,
252 rDeckDescriptor
.msHighContrastIconURL
,
256 TabBar::Item::Item(TabBar
& rTabBar
)
258 , mxBuilder(Application::CreateBuilder(rTabBar
.GetContainer(), "sfx/ui/tabbutton.ui"))
259 , mxButton(mxBuilder
->weld_toolbar("button"))
261 , mbIsHiddenByDefault(false)
265 TabBar::Item::~Item()
267 mrTabBar
.GetContainer()->move(mxButton
.get(), nullptr);
270 IMPL_LINK_NOARG(TabBar::Item
, HandleClick
, const OUString
&, void)
272 // tdf#143146 copy the functor and arg before calling
273 // GrabFocusToDocument which may destroy this object
274 auto aDeckActivationFunctor
= maDeckActivationFunctor
;
275 auto sDeckId
= msDeckId
;
277 mrTabBar
.GrabFocusToDocument();
280 aDeckActivationFunctor(sDeckId
);
282 catch(const css::uno::Exception
&)
283 {} // workaround for #i123198#
286 OUString
const & TabBar::GetDeckIdForIndex (const sal_Int32 nIndex
) const
288 if (nIndex
<0 || o3tl::make_unsigned(nIndex
)>=maItems
.size())
289 throw RuntimeException();
290 return maItems
[nIndex
]->msDeckId
;
293 void TabBar::ToggleHideFlag (const sal_Int32 nIndex
)
295 if (nIndex
<0 || o3tl::make_unsigned(nIndex
) >= maItems
.size())
296 throw RuntimeException();
298 maItems
[nIndex
]->mbIsHidden
= ! maItems
[nIndex
]->mbIsHidden
;
300 std::shared_ptr
<DeckDescriptor
> xDeckDescriptor
= pParentSidebarController
->GetResourceManager()->GetDeckDescriptor(maItems
[nIndex
]->msDeckId
);
303 xDeckDescriptor
->mbIsEnabled
= ! maItems
[nIndex
]->mbIsHidden
;
306 aContext
.msApplication
= pParentSidebarController
->GetCurrentContext().msApplication
;
307 // leave aContext.msContext on default 'any' ... this func is used only for decks
308 // and we don't have context-sensitive decks anyway
310 xDeckDescriptor
->maContextList
.ToggleVisibilityForContext(
311 aContext
, xDeckDescriptor
->mbIsEnabled
);
315 void TabBar::RestoreHideFlags()
317 for (auto & item
: maItems
)
319 if (item
->mbIsHidden
!= item
->mbIsHiddenByDefault
)
321 item
->mbIsHidden
= item
->mbIsHiddenByDefault
;
322 std::shared_ptr
<DeckDescriptor
> xDeckDescriptor
= pParentSidebarController
->GetResourceManager()->GetDeckDescriptor(item
->msDeckId
);
324 xDeckDescriptor
->mbIsEnabled
= !item
->mbIsHidden
;
330 void TabBar::UpdateFocusManager(FocusManager
& rFocusManager
)
332 std::vector
<weld::Widget
*> aButtons
;
333 aButtons
.reserve(maItems
.size()+1);
334 aButtons
.push_back(mxMenuButton
.get());
335 for (auto const& item
: maItems
)
337 aButtons
.push_back(item
->mxButton
.get());
339 rFocusManager
.SetButtons(aButtons
);
342 IMPL_LINK_NOARG(TabBar
, OnToolboxClicked
, weld::Toggleable
&, void)
344 if (!mxMenuButton
->get_active())
347 std::vector
<DeckMenuData
> aMenuData
;
349 for (auto const& item
: maItems
)
351 std::shared_ptr
<DeckDescriptor
> xDeckDescriptor
= pParentSidebarController
->GetResourceManager()->GetDeckDescriptor(item
->msDeckId
);
353 if (!xDeckDescriptor
)
357 aData
.msDisplayName
= xDeckDescriptor
->msTitle
;
358 aData
.mbIsCurrentDeck
= item
->mxButton
->get_item_active("toggle");
359 aData
.mbIsActive
= !item
->mbIsHidden
;
360 aData
.mbIsEnabled
= item
->mxButton
->get_sensitive();
361 aMenuData
.push_back(aData
);
364 for (int i
= mxMainMenu
->n_children() - 1; i
>= 0; --i
)
366 OUString sIdent
= mxMainMenu
->get_id(i
);
367 if (sIdent
.startsWith("select"))
368 mxMainMenu
->remove(sIdent
);
370 for (int i
= mxSubMenu
->n_children() - 1; i
>= 0; --i
)
372 OUString sIdent
= mxSubMenu
->get_id(i
);
373 if (sIdent
.indexOf("customize") != -1)
374 mxSubMenu
->remove(sIdent
);
377 maPopupMenuProvider(*mxMainMenu
, *mxSubMenu
, aMenuData
);
380 void TabBar::EnableMenuButton(const bool bEnable
)
382 mxMenuButton
->set_sensitive(bEnable
);
385 FactoryFunction
TabBar::GetUITestFactory() const
387 return TabBarUIObject::create
;
390 } // end of namespace sfx2::sidebar
392 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */