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  if (bIsWelded)
80  {
81  m_bIsWelded = true;
84  // now access it using GetMainContainer and set dispose callback with SetDisposeCallback
85  }
86  else
87  {
88  m_pUIBuilder.reset(
89  new VclBuilder(this, sUIDir, rUIXMLDescription, rID, rFrame, true, &aNotebookBarAddonsItem));
90 
91  // In the Notebookbar's .ui file must exist control handling context
92  // - implementing NotebookbarContextControl interface with id "ContextContainer"
93  // or "ContextContainerX" where X is a number >= 1
94  NotebookbarContextControl* pContextContainer = nullptr;
95  int i = 0;
96  do
97  {
98  OUString aName = "ContextContainer";
99  if (i)
100  aName += OUString::number(i);
101 
102  pContextContainer = dynamic_cast<NotebookbarContextControl*>(m_pUIBuilder->get<Window>(OUStringToOString(aName, RTL_TEXTENCODING_UTF8)));
103  if (pContextContainer)
104  m_pContextContainers.push_back(pContextContainer);
105  i++;
106  }
107  while( pContextContainer != nullptr );
108  }
109 
111 }
112 
113 void NotebookBar::SetDisposeCallback(const Link<const SfxViewShell*, void> rDisposeCallback, const SfxViewShell* pViewShell)
114 {
115  m_rDisposeLink = rDisposeCallback;
116  m_pViewShell = pViewShell;
117 }
118 
120 {
121  disposeOnce();
122 }
123 
125 {
126  m_pContextContainers.clear();
130 
131  if (m_rDisposeLink.IsSet())
133 
134  if (m_bIsWelded)
136  else
137  disposeBuilder();
138 
140  m_pEventListener.clear();
141 
143 }
144 
146 {
147  // capture KeyEvents for taskpane cycling
148  if (rNEvt.GetType() == MouseNotifyEvent::KEYINPUT)
149  {
150  if (m_pSystemWindow)
151  return m_pSystemWindow->PreNotify(rNEvt);
152  }
153  return Window::PreNotify( rNEvt );
154 }
155 
157 {
158  if (isLayoutEnabled(this))
160 
161  return Control::GetOptimalSize();
162 }
163 
165 {
166  bool bCanHandleSmallerWidth = false;
167  bool bCanHandleSmallerHeight = false;
168 
169  bool bIsLayoutEnabled = isLayoutEnabled(this);
171 
172  if (bIsLayoutEnabled && pChild->GetType() == WindowType::SCROLLWINDOW)
173  {
174  WinBits nStyle = pChild->GetStyle();
175  if (nStyle & (WB_AUTOHSCROLL | WB_HSCROLL))
176  bCanHandleSmallerWidth = true;
177  if (nStyle & (WB_AUTOVSCROLL | WB_VSCROLL))
178  bCanHandleSmallerHeight = true;
179  }
180 
181  Size aSize(GetOptimalSize());
182  if (!bCanHandleSmallerWidth)
183  nWidth = std::max(nWidth, aSize.Width());
184  if (!bCanHandleSmallerHeight)
185  nHeight = std::max(nHeight, aSize.Height());
186 
187  Control::setPosSizePixel(nX, nY, nWidth, nHeight, nFlags);
188 
189  if (bIsLayoutEnabled && (nFlags & PosSizeFlags::Size))
190  VclContainer::setLayoutAllocation(*pChild, Point(0, 0), Size(nWidth, nHeight));
191 }
192 
194 {
195  if(m_pUIBuilder && m_pUIBuilder->get_widget_root())
196  {
197  vcl::Window* pWindow = m_pUIBuilder->get_widget_root()->GetChild(0);
198  if (pWindow)
199  {
200  Size aSize = pWindow->GetSizePixel();
201  aSize.setWidth( GetSizePixel().Width() );
202  pWindow->SetSizePixel(aSize);
203  }
204  }
205  if(m_bIsWelded)
206  {
208  assert(pChild);
210  Control::Resize();
211  }
212  Control::Resize();
213 }
214 
216 {
217  m_pSystemWindow = pSystemWindow;
220 }
221 
222 void SAL_CALL NotebookBarContextChangeEventListener::notifyContextChangeEvent(const css::ui::ContextChangeEventObject& rEvent)
223 {
224  if (mpParent)
225  {
227  pControl->SetContext(vcl::EnumContext::GetContextEnum(rEvent.ContextName));
228  }
229 }
230 
232 {
234  return;
235 
236  auto xController = mxFrame->getController();
237  if(bListen)
238  {
239  // add listeners
240  if (m_alisteningControllers.count(xController) == 0)
241  {
242  auto xMultiplexer(css::ui::ContextChangeEventMultiplexer::get(
244  xMultiplexer->addContextChangeEventListener(m_pEventListener, xController);
246  }
247  }
248  else
249  {
250  // remove listeners
252  {
253  auto xMultiplexer(css::ui::ContextChangeEventMultiplexer::get(
255  xMultiplexer->removeContextChangeEventListener(m_pEventListener, xController);
257  }
258  }
259 }
260 
262 {
264  return;
265 
266  auto xMultiplexer(
267  css::ui::ContextChangeEventMultiplexer::get(comphelper::getProcessComponentContext()));
268  xMultiplexer->removeAllContextChangeEventListeners(m_pEventListener);
269  m_alisteningControllers.clear();
270 }
271 
272 void SAL_CALL NotebookBarContextChangeEventListener::disposing(const ::css::lang::EventObject&)
273 {
274  mpParent.clear();
275 }
276 
278 {
280  Control::DataChanged(rDCEvt);
281 }
282 
284 {
286  Control::StateChanged(nStateChange);
287  Invalidate();
288 }
289 
291 {
292  const StyleSettings& rStyleSettings = GetSettings().GetStyleSettings();
293  const BitmapEx& aPersona = rStyleSettings.GetPersonaHeader();
294  Wallpaper aWallpaper(aPersona);
296  if (!aPersona.IsEmpty())
297  {
298  SetBackground(aWallpaper);
301  }
302  else
303  {
304  SetBackground(rStyleSettings.GetDialogColor());
307  }
308 
310 }
311 
313 {
314  AllSettings aAllSettings( GetSettings() );
315  StyleSettings aStyleSet( aAllSettings.GetStyleSettings() );
316 
317  ::Color aTextColor = aStyleSet.GetFieldTextColor();
318  aStyleSet.SetDialogTextColor( aTextColor );
319  aStyleSet.SetButtonTextColor( aTextColor );
320  aStyleSet.SetRadioCheckTextColor( aTextColor );
321  aStyleSet.SetGroupTextColor( aTextColor );
322  aStyleSet.SetLabelTextColor( aTextColor );
323  aStyleSet.SetWindowTextColor( aTextColor );
324  aStyleSet.SetTabTextColor(aTextColor);
325  aStyleSet.SetToolTextColor(aTextColor);
326 
327  aAllSettings.SetStyleSettings(aStyleSet);
328  DefaultSettings = aAllSettings;
329 }
330 
332 {
333  AllSettings aAllSettings( GetSettings() );
334  StyleSettings aStyleSet( aAllSettings.GetStyleSettings() );
335 
336  ::Color aTextColor = aStyleSet.GetPersonaMenuBarTextColor().value_or(COL_BLACK );
337  aStyleSet.SetDialogTextColor( aTextColor );
338  aStyleSet.SetButtonTextColor( aTextColor );
339  aStyleSet.SetRadioCheckTextColor( aTextColor );
340  aStyleSet.SetGroupTextColor( aTextColor );
341  aStyleSet.SetLabelTextColor( aTextColor );
342  aStyleSet.SetWindowTextColor( aTextColor );
343  aStyleSet.SetTabTextColor(aTextColor);
344  aStyleSet.SetToolTextColor(aTextColor);
345 
346  aAllSettings.SetStyleSettings(aStyleSet);
347  PersonaSettings = aAllSettings;
348 }
349 
350 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static void setLayoutAllocation(vcl::Window &rWindow, const Point &rPos, const Size &rSize)
Definition: layout.cxx:67
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:142
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:2400
virtual void SetSizePixel(const Size &rNewSize)
Definition: window2.cxx:1262
WinBits const WB_AUTOVSCROLL
PosSizeFlags
Definition: window.hxx:140
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:317
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:65
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:556
WinBits const WB_DIALOGCONTROL
bool IsEmpty() const
Definition: BitmapEx.cxx:198
#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:91
int i
virtual void Resize() override
void SetStyle(WallpaperStyle eStyle)
Definition: wall.cxx:202
virtual void Invalidate(InvalidateFlags nFlags=InvalidateFlags::NONE)
Definition: paint.cxx:1164
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
tools::Long Width() const
std::unique_ptr< VclBuilder > m_pUIBuilder
Definition: builder.hxx:487
bool isLayoutEnabled(const vcl::Window *pWindow)
Definition: layout.cxx:2865
MouseNotifyEvent GetType() const
Definition: event.hxx:302
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:1961
vcl::Window * GetWindow(GetWindowType nType) const
Definition: stacking.cxx:1039
void disposeBuilder()
Definition: dialog.cxx:550
virtual void setPosSizePixel(tools::Long nX, tools::Long nY, tools::Long nWidth, tools::Long nHeight, PosSizeFlags nFlags=PosSizeFlags::All)
Definition: window.cxx:2664
virtual Size GetOptimalSize() const override
Definition: ctrl.cxx:374
SAL_DLLPRIVATE bool ImplIsInTaskPaneList(vcl::Window *pWin)
Definition: syswin.cxx:1009
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
OUString aName
virtual ~NotebookBar() override
static VclPtr< reference_type > Create(Arg &&...arg)
A construction helper for VclPtr.
Definition: vclptr.hxx:127
tools::Long Height() const
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:953
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:34
void Show(bool bVisible=true, ShowFlags nFlags=ShowFlags::NONE)
Definition: window.cxx:2186