LibreOffice Module vcl (master)  1
notebookbar.cxx
Go to the documentation of this file.
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 
10 #include <sal/config.h>
11 
12 #include <string_view>
13 
14 #include <vcl/layout.hxx>
16 #include <vcl/syswin.hxx>
17 #include <vcl/taskpanelist.hxx>
19 #include <cppuhelper/implbase.hxx>
21 #include <rtl/bootstrap.hxx>
22 #include <osl/file.hxx>
23 #include <config_folders.h>
24 #include <com/sun/star/frame/XFrame.hpp>
25 #include <com/sun/star/ui/ContextChangeEventMultiplexer.hpp>
26 #include <comphelper/lok.hxx>
27 
28 static OUString getCustomizedUIRootDir()
29 {
30  OUString sShareLayer("${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE(
31  "bootstrap") ":UserInstallation}/user/config/soffice.cfg/");
32  rtl::Bootstrap::expandMacros(sShareLayer);
33  return sShareLayer;
34 }
35 
36 static bool doesFileExist(std::u16string_view sUIDir, std::u16string_view sUIFile)
37 {
38  OUString sUri = OUString::Concat(sUIDir) + sUIFile;
39  osl::File file(sUri);
40  return( file.open(0) == osl::FileBase::E_None );
41 }
42 
46 class NotebookBarContextChangeEventListener : public ::cppu::WeakImplHelper<css::ui::XContextChangeEventListener>
47 {
49 public:
50  explicit NotebookBarContextChangeEventListener(NotebookBar *p) : mpParent(p) {}
51 
52  // XContextChangeEventListener
53  virtual void SAL_CALL notifyContextChangeEvent(const css::ui::ContextChangeEventObject& rEvent) override;
54 
55  virtual void SAL_CALL disposing(const ::css::lang::EventObject&) override;
56 };
57 
58 NotebookBar::NotebookBar(Window* pParent, const OString& rID, const OUString& rUIXMLDescription,
59  const css::uno::Reference<css::frame::XFrame>& rFrame,
60  const NotebookBarAddonsItem& aNotebookBarAddonsItem)
61  : Control(pParent)
62  , m_pEventListener(new NotebookBarContextChangeEventListener(this))
63  , m_pViewShell(nullptr)
64  , m_bIsWelded(false)
65  , m_sUIXMLDescription(rUIXMLDescription)
66 {
67  mxFrame = rFrame;
68 
70  OUString sUIDir = AllSettings::GetUIRootDir();
71  bool doesCustomizedUIExist = doesFileExist(getCustomizedUIRootDir(), rUIXMLDescription);
72  if ( doesCustomizedUIExist )
73  sUIDir = getCustomizedUIRootDir();
74 
75  bool bIsWelded = comphelper::LibreOfficeKit::isActive()
76  && (rUIXMLDescription == "modules/swriter/ui/notebookbar.ui"
77  || rUIXMLDescription == "modules/scalc/ui/notebookbar.ui"
78  || rUIXMLDescription == "modules/simpress/ui/notebookbar.ui"
79  || rUIXMLDescription == "modules/sdraw/ui/notebookbar.ui");
80  if (bIsWelded)
81  {
82  m_bIsWelded = true;
85  // now access it using GetMainContainer and set dispose callback with SetDisposeCallback
86  }
87  else
88  {
89  m_pUIBuilder.reset(
90  new VclBuilder(this, sUIDir, rUIXMLDescription, rID, rFrame, true, &aNotebookBarAddonsItem));
91 
92  // In the Notebookbar's .ui file must exist control handling context
93  // - implementing NotebookbarContextControl interface with id "ContextContainer"
94  // or "ContextContainerX" where X is a number >= 1
95  NotebookbarContextControl* pContextContainer = nullptr;
96  int i = 0;
97  do
98  {
99  OUString aName = "ContextContainer";
100  if (i)
101  aName += OUString::number(i);
102 
103  pContextContainer = dynamic_cast<NotebookbarContextControl*>(m_pUIBuilder->get<Window>(OUStringToOString(aName, RTL_TEXTENCODING_UTF8)));
104  if (pContextContainer)
105  m_pContextContainers.push_back(pContextContainer);
106  i++;
107  }
108  while( pContextContainer != nullptr );
109  }
110 
112 }
113 
114 void NotebookBar::SetDisposeCallback(const Link<const SfxViewShell*, void> rDisposeCallback, const SfxViewShell* pViewShell)
115 {
116  m_rDisposeLink = rDisposeCallback;
117  m_pViewShell = pViewShell;
118 }
119 
121 {
122  disposeOnce();
123 }
124 
126 {
127  m_pContextContainers.clear();
131 
132  if (m_rDisposeLink.IsSet())
134 
135  if (m_bIsWelded)
137  else
138  disposeBuilder();
139 
141  m_pEventListener.clear();
142 
144 }
145 
147 {
148  // capture KeyEvents for taskpane cycling
149  if (rNEvt.GetType() == MouseNotifyEvent::KEYINPUT)
150  {
151  if (m_pSystemWindow)
152  return m_pSystemWindow->PreNotify(rNEvt);
153  }
154  return Window::PreNotify( rNEvt );
155 }
156 
158 {
159  if (isLayoutEnabled(this))
161 
162  return Control::GetOptimalSize();
163 }
164 
166 {
167  bool bCanHandleSmallerWidth = false;
168  bool bCanHandleSmallerHeight = false;
169 
170  bool bIsLayoutEnabled = isLayoutEnabled(this);
172 
173  if (bIsLayoutEnabled && pChild->GetType() == WindowType::SCROLLWINDOW)
174  {
175  WinBits nStyle = pChild->GetStyle();
176  if (nStyle & (WB_AUTOHSCROLL | WB_HSCROLL))
177  bCanHandleSmallerWidth = true;
178  if (nStyle & (WB_AUTOVSCROLL | WB_VSCROLL))
179  bCanHandleSmallerHeight = true;
180  }
181 
182  Size aSize(GetOptimalSize());
183  if (!bCanHandleSmallerWidth)
184  nWidth = std::max(nWidth, aSize.Width());
185  if (!bCanHandleSmallerHeight)
186  nHeight = std::max(nHeight, aSize.Height());
187 
188  Control::setPosSizePixel(nX, nY, nWidth, nHeight, nFlags);
189 
190  if (bIsLayoutEnabled && (nFlags & PosSizeFlags::Size))
191  VclContainer::setLayoutAllocation(*pChild, Point(0, 0), Size(nWidth, nHeight));
192 }
193 
195 {
196  if(m_pUIBuilder && m_pUIBuilder->get_widget_root())
197  {
198  vcl::Window* pWindow = m_pUIBuilder->get_widget_root()->GetChild(0);
199  if (pWindow)
200  {
201  Size aSize = pWindow->GetSizePixel();
202  aSize.setWidth( GetSizePixel().Width() );
203  pWindow->SetSizePixel(aSize);
204  }
205  }
206  if(m_bIsWelded)
207  {
209  assert(pChild);
211  Control::Resize();
212  }
213  Control::Resize();
214 }
215 
217 {
218  m_pSystemWindow = pSystemWindow;
221 }
222 
223 void SAL_CALL NotebookBarContextChangeEventListener::notifyContextChangeEvent(const css::ui::ContextChangeEventObject& rEvent)
224 {
225  if (mpParent)
226  {
228  pControl->SetContext(vcl::EnumContext::GetContextEnum(rEvent.ContextName));
229  }
230 }
231 
233 {
235  return;
236 
237  auto xController = mxFrame->getController();
238  if(bListen)
239  {
240  // add listeners
241  if (m_alisteningControllers.count(xController) == 0)
242  {
243  auto xMultiplexer(css::ui::ContextChangeEventMultiplexer::get(
245  xMultiplexer->addContextChangeEventListener(m_pEventListener, xController);
247  }
248  }
249  else
250  {
251  // remove listeners
253  {
254  auto xMultiplexer(css::ui::ContextChangeEventMultiplexer::get(
256  xMultiplexer->removeContextChangeEventListener(m_pEventListener, xController);
258  }
259  }
260 }
261 
263 {
265  return;
266 
267  auto xMultiplexer(
268  css::ui::ContextChangeEventMultiplexer::get(comphelper::getProcessComponentContext()));
269  xMultiplexer->removeAllContextChangeEventListeners(m_pEventListener);
270  m_alisteningControllers.clear();
271 }
272 
273 void SAL_CALL NotebookBarContextChangeEventListener::disposing(const ::css::lang::EventObject&)
274 {
275  mpParent.clear();
276 }
277 
279 {
281  Control::DataChanged(rDCEvt);
282 }
283 
285 {
287  Control::StateChanged(nStateChange);
288  Invalidate();
289 }
290 
292 {
293  const StyleSettings& rStyleSettings = GetSettings().GetStyleSettings();
294  const BitmapEx& aPersona = rStyleSettings.GetPersonaHeader();
295  Wallpaper aWallpaper(aPersona);
297  if (!aPersona.IsEmpty())
298  {
299  SetBackground(aWallpaper);
302  }
303  else
304  {
305  SetBackground(rStyleSettings.GetDialogColor());
308  }
309 
311 }
312 
314 {
315  AllSettings aAllSettings( GetSettings() );
316  StyleSettings aStyleSet( aAllSettings.GetStyleSettings() );
317 
318  ::Color aTextColor = aStyleSet.GetFieldTextColor();
319  aStyleSet.SetDialogTextColor( aTextColor );
320  aStyleSet.SetButtonTextColor( aTextColor );
321  aStyleSet.SetRadioCheckTextColor( aTextColor );
322  aStyleSet.SetGroupTextColor( aTextColor );
323  aStyleSet.SetLabelTextColor( aTextColor );
324  aStyleSet.SetWindowTextColor( aTextColor );
325  aStyleSet.SetTabTextColor(aTextColor);
326  aStyleSet.SetToolTextColor(aTextColor);
327 
328  aAllSettings.SetStyleSettings(aStyleSet);
329  DefaultSettings = aAllSettings;
330 }
331 
333 {
334  AllSettings aAllSettings( GetSettings() );
335  StyleSettings aStyleSet( aAllSettings.GetStyleSettings() );
336 
337  ::Color aTextColor = aStyleSet.GetPersonaMenuBarTextColor().value_or(COL_BLACK );
338  aStyleSet.SetDialogTextColor( aTextColor );
339  aStyleSet.SetButtonTextColor( aTextColor );
340  aStyleSet.SetRadioCheckTextColor( aTextColor );
341  aStyleSet.SetGroupTextColor( aTextColor );
342  aStyleSet.SetLabelTextColor( aTextColor );
343  aStyleSet.SetWindowTextColor( aTextColor );
344  aStyleSet.SetTabTextColor(aTextColor);
345  aStyleSet.SetToolTextColor(aTextColor);
346 
347  aAllSettings.SetStyleSettings(aStyleSet);
348  PersonaSettings = aAllSettings;
349 }
350 
351 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static void setLayoutAllocation(vcl::Window &rWindow, const Point &rPos, const Size &rSize)
Definition: layout.cxx:68
const Color & GetDialogColor() const
virtual void dispose() override
This is intended to be used to clear any locally held references to other Window-subclass objects...
void setWidth(tools::Long nWidth)
AllSettings PersonaSettings
Definition: notebookbar.hxx:69
void SetStyleSettings(const StyleSettings &rSet)
std::vector< NotebookbarContextControl * > m_pContextContainers
Definition: notebookbar.hxx:59
virtual Size GetOptimalSize() const override
static Context GetContextEnum(const OUString &rsContextName)
static Size getLayoutRequisition(const vcl::Window &rWindow)
Definition: layout.cxx:143
long Long
void disposeAndClear()
Definition: vclptr.hxx:200
const StyleSettings & GetStyleSettings() const
static OUString getCustomizedUIRootDir()
Definition: notebookbar.cxx:28
BitmapEx const & GetPersonaHeader() const
VclPtr< vcl::Window > m_xVclContentArea
Definition: notebookbar.hxx:63
virtual void StateChanged(StateChangedType nStateChange) override
Definition: ctrl.cxx:260
virtual Size GetSizePixel() const
Definition: window.cxx:2394
virtual void SetSizePixel(const Size &rNewSize)
Definition: window2.cxx:1261
WinBits const WB_AUTOVSCROLL
PosSizeFlags
Definition: window.hxx:125
WinBits const WB_VSCROLL
virtual void SetSettings(const AllSettings &rSettings) override
void UpdateDefaultSettings()
VclPtr< SystemWindow > m_pSystemWindow
Definition: notebookbar.hxx:56
StateChangedType
Definition: window.hxx:289
constexpr tools::Long Width() const
sal_Int64 WinBits
void SetBackground()
void UpdateBackground()
Reference< XController > xController
Creates a hierarchy of vcl::Windows (widgets) from a .ui file for dialogs, sidebar, etc.
Definition: builder.hxx:66
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
virtual bool PreNotify(NotifyEvent &rNEvt) override
WinBits const WB_HSCROLL
void SetSystemWindow(SystemWindow *pSystemWindow)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
std::set< css::uno::Reference< css::frame::XController > > m_alisteningControllers
Definition: notebookbar.hxx:58
NotebookBarContextChangeEventListener(NotebookBar *p)
Definition: notebookbar.cxx:50
virtual void setPosSizePixel(tools::Long nX, tools::Long nY, tools::Long nWidth, tools::Long nHeight, PosSizeFlags nFlags=PosSizeFlags::All) override
css::uno::Reference< css::ui::XContextChangeEventListener > m_pEventListener
Definition: notebookbar.hxx:57
static OUString GetUIRootDir()
Definition: dialog.cxx:558
WinBits const WB_DIALOGCONTROL
bool IsEmpty() const
Definition: BitmapEx.cxx:196
#define SAL_CONFIGFILE(name)
void clear()
Definition: vclptr.hxx:190
void DataChanged(const DataChangedEvent &rDCEvt) override
virtual bool PreNotify(NotifyEvent &rNEvt) override
Definition: syswin.cxx:202
Window(WindowType nType)
Definition: window.cxx:92
int i
virtual void Resize() override
void SetStyle(WallpaperStyle eStyle)
Definition: wall.cxx:204
virtual void Invalidate(InvalidateFlags nFlags=InvalidateFlags::NONE)
Definition: paint.cxx:1140
static bool doesFileExist(std::u16string_view sUIDir, std::u16string_view sUIFile)
Definition: notebookbar.cxx:36
void ControlListenerForCurrentController(bool bListen)
void StateChanged(const StateChangedType nStateChange) override
std::unique_ptr< VclBuilder > m_pUIBuilder
Definition: syswin.hxx:86
bool isLayoutEnabled(const vcl::Window *pWindow)
Definition: layout.cxx:2874
MouseNotifyEvent GetType() const
Definition: event.hxx:300
virtual void dispose() override
This is intended to be used to clear any locally held references to other Window-subclass objects...
Definition: ctrl.cxx:59
const AllSettings & GetSettings() const
Definition: outdev.hxx:418
NotebookBar(Window *pParent, const OString &rID, const OUString &rUIXMLDescription, const css::uno::Reference< css::frame::XFrame > &rFrame, const NotebookBarAddonsItem &aNotebookBarAddonsItem)
Definition: notebookbar.cxx:58
AllSettings DefaultSettings
Definition: notebookbar.hxx:68
void SetDisposeCallback(const Link< const SfxViewShell *, void > rDisposeCallback, const SfxViewShell *pViewShell)
void SetStyle(WinBits nStyle)
Definition: window.cxx:1955
vcl::Window * GetWindow(GetWindowType nType) const
Definition: stacking.cxx:1035
void disposeBuilder()
Definition: dialog.cxx:552
virtual void setPosSizePixel(tools::Long nX, tools::Long nY, tools::Long nWidth, tools::Long nHeight, PosSizeFlags nFlags=PosSizeFlags::All)
Definition: window.cxx:2658
virtual Size GetOptimalSize() const override
Definition: ctrl.cxx:374
SAL_DLLPRIVATE bool ImplIsInTaskPaneList(vcl::Window *pWin)
Definition: syswin.cxx:961
Link< const SfxViewShell *, void > m_rDisposeLink
Definition: notebookbar.hxx:66
virtual void Resize() override
Definition: ctrl.cxx:74
void UpdatePersonaSettings()
css::uno::Reference< css::frame::XFrame > mxFrame
Definition: notebookbar.hxx:60
Definition: ctrl.hxx:33
constexpr tools::Long Height() const
OUString aName
virtual ~NotebookBar() override
static VclPtr< reference_type > Create(Arg &&...arg)
A construction helper for VclPtr.
Definition: vclptr.hxx:127
Reference< XComponentContext > getProcessComponentContext()
const ::std::vector< Color > ImpSvNumberformatScan::StandardColor COL_BLACK
virtual void SAL_CALL notifyContextChangeEvent(const css::ui::ContextChangeEventObject &rEvent) override
void RemoveWindow(vcl::Window *pWindow)
WinBits const WB_AUTOHSCROLL
const SfxViewShell * m_pViewShell
Definition: notebookbar.hxx:61
virtual void SAL_CALL disposing(const ::css::lang::EventObject &) override
WinBits GetStyle() const
Definition: window2.cxx:952
void AddWindow(vcl::Window *pWindow)
TaskPaneList * GetTaskPaneList()
Definition: syswin.cxx:244
virtual void SetContext(vcl::EnumContext::Context eContext)=0
void StopListeningAllControllers()
bool m_bIsWelded
Definition: notebookbar.hxx:64
This implements Widget Layout-based notebook-like menu bar.
Definition: notebookbar.hxx:26
split from the main class since it needs different ref-counting mana
Definition: notebookbar.cxx:46
virtual void DataChanged(const DataChangedEvent &rDCEvt)
Definition: event.cxx:36
void Show(bool bVisible=true, ShowFlags nFlags=ShowFlags::NONE)
Definition: window.cxx:2180