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 <vcl/layout.hxx>
11 #include <vcl/notebookbar.hxx>
12 #include <vcl/syswin.hxx>
13 #include <vcl/taskpanelist.hxx>
15 #include <cppuhelper/implbase.hxx>
17 #include <rtl/bootstrap.hxx>
18 #include <osl/file.hxx>
19 #include <config_folders.h>
20 #include <com/sun/star/frame/XFrame.hpp>
21 #include <com/sun/star/ui/ContextChangeEventMultiplexer.hpp>
22 #include <comphelper/lok.hxx>
23 
24 static OUString getCustomizedUIRootDir()
25 {
26  OUString sShareLayer("${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE(
27  "bootstrap") ":UserInstallation}/user/config/soffice.cfg/");
28  rtl::Bootstrap::expandMacros(sShareLayer);
29  return sShareLayer;
30 }
31 
32 static bool doesFileExist(const OUString& sUIDir, const OUString& sUIFile)
33 {
34  OUString sUri = sUIDir + sUIFile;
35  osl::File file(sUri);
36  return( file.open(0) == osl::FileBase::E_None );
37 }
38 
42 class NotebookBarContextChangeEventListener : public ::cppu::WeakImplHelper<css::ui::XContextChangeEventListener>
43 {
45 public:
46  explicit NotebookBarContextChangeEventListener(NotebookBar *p) : mpParent(p) {}
47 
48  // XContextChangeEventListener
49  virtual void SAL_CALL notifyContextChangeEvent(const css::ui::ContextChangeEventObject& rEvent) override;
50 
51  virtual void SAL_CALL disposing(const ::css::lang::EventObject&) override;
52 };
53 
54 NotebookBar::NotebookBar(Window* pParent, const OString& rID, const OUString& rUIXMLDescription,
55  const css::uno::Reference<css::frame::XFrame>& rFrame,
56  const NotebookBarAddonsItem& aNotebookBarAddonsItem)
57  : Control(pParent)
58  , m_pEventListener(new NotebookBarContextChangeEventListener(this))
59  , m_pViewShell(nullptr)
60  , m_bIsWelded(false)
61  , m_sUIXMLDescription(rUIXMLDescription)
62 {
63  mxFrame = rFrame;
64 
66  OUString sUIDir = AllSettings::GetUIRootDir();
67  bool doesCustomizedUIExist = doesFileExist(getCustomizedUIRootDir(), rUIXMLDescription);
68  if ( doesCustomizedUIExist )
69  sUIDir = getCustomizedUIRootDir();
70 
71  bool bIsWelded = comphelper::LibreOfficeKit::isActive()
72  && (rUIXMLDescription == "modules/swriter/ui/notebookbar.ui"
73  || rUIXMLDescription == "modules/scalc/ui/notebookbar.ui"
74  || rUIXMLDescription == "modules/simpress/ui/notebookbar.ui");
75  if (bIsWelded)
76  {
77  m_bIsWelded = true;
80  // now access it using GetMainContainer and set dispose callback with SetDisposeCallback
81  }
82  else
83  {
84  m_pUIBuilder.reset(
85  new VclBuilder(this, sUIDir, rUIXMLDescription, rID, rFrame, true, &aNotebookBarAddonsItem));
86 
87  // In the Notebookbar's .ui file must exist control handling context
88  // - implementing NotebookbarContextControl interface with id "ContextContainer"
89  // or "ContextContainerX" where X is a number >= 1
90  NotebookbarContextControl* pContextContainer = nullptr;
91  int i = 0;
92  do
93  {
94  OUString aName = "ContextContainer";
95  if (i)
96  aName += OUString::number(i);
97 
98  pContextContainer = dynamic_cast<NotebookbarContextControl*>(m_pUIBuilder->get<Window>(OUStringToOString(aName, RTL_TEXTENCODING_UTF8)));
99  if (pContextContainer)
100  m_pContextContainers.push_back(pContextContainer);
101  i++;
102  }
103  while( pContextContainer != nullptr );
104  }
105 
107 }
108 
109 void NotebookBar::SetDisposeCallback(const Link<const SfxViewShell*, void> rDisposeCallback, const SfxViewShell* pViewShell)
110 {
111  m_rDisposeLink = rDisposeCallback;
112  m_pViewShell = pViewShell;
113 }
114 
116 {
117  disposeOnce();
118 }
119 
121 {
122  m_pContextContainers.clear();
126 
127  if (m_rDisposeLink.IsSet())
129 
130  if (m_bIsWelded)
132  else
133  disposeBuilder();
134 
136  m_pEventListener.clear();
137 
139 }
140 
142 {
143  // capture KeyEvents for taskpane cycling
144  if (rNEvt.GetType() == MouseNotifyEvent::KEYINPUT)
145  {
146  if (m_pSystemWindow)
147  return m_pSystemWindow->PreNotify(rNEvt);
148  }
149  return Window::PreNotify( rNEvt );
150 }
151 
153 {
154  if (isLayoutEnabled(this))
156 
157  return Control::GetOptimalSize();
158 }
159 
160 void NotebookBar::setPosSizePixel(long nX, long nY, long nWidth, long nHeight, PosSizeFlags nFlags)
161 {
162  bool bCanHandleSmallerWidth = false;
163  bool bCanHandleSmallerHeight = false;
164 
165  bool bIsLayoutEnabled = isLayoutEnabled(this);
167 
168  if (bIsLayoutEnabled && pChild->GetType() == WindowType::SCROLLWINDOW)
169  {
170  WinBits nStyle = pChild->GetStyle();
171  if (nStyle & (WB_AUTOHSCROLL | WB_HSCROLL))
172  bCanHandleSmallerWidth = true;
173  if (nStyle & (WB_AUTOVSCROLL | WB_VSCROLL))
174  bCanHandleSmallerHeight = true;
175  }
176 
177  Size aSize(GetOptimalSize());
178  if (!bCanHandleSmallerWidth)
179  nWidth = std::max(nWidth, aSize.Width());
180  if (!bCanHandleSmallerHeight)
181  nHeight = std::max(nHeight, aSize.Height());
182 
183  Control::setPosSizePixel(nX, nY, nWidth, nHeight, nFlags);
184 
185  if (bIsLayoutEnabled && (nFlags & PosSizeFlags::Size))
186  VclContainer::setLayoutAllocation(*pChild, Point(0, 0), Size(nWidth, nHeight));
187 }
188 
190 {
191  if(m_pUIBuilder && m_pUIBuilder->get_widget_root())
192  {
193  vcl::Window* pWindow = m_pUIBuilder->get_widget_root()->GetChild(0);
194  if (pWindow)
195  {
196  Size aSize = pWindow->GetSizePixel();
197  aSize.setWidth( GetSizePixel().Width() );
198  pWindow->SetSizePixel(aSize);
199  }
200  }
201  if(m_bIsWelded)
202  {
204  assert(pChild);
206  Control::Resize();
207  }
208  Control::Resize();
209 }
210 
212 {
213  m_pSystemWindow = pSystemWindow;
216 }
217 
218 void SAL_CALL NotebookBarContextChangeEventListener::notifyContextChangeEvent(const css::ui::ContextChangeEventObject& rEvent)
219 {
220  if (mpParent)
221  {
223  pControl->SetContext(vcl::EnumContext::GetContextEnum(rEvent.ContextName));
224  }
225 }
226 
228 {
230  return;
231 
232  auto xController = mxFrame->getController();
233  if(bListen)
234  {
235  // add listeners
236  if (m_alisteningControllers.count(xController) == 0)
237  {
238  auto xMultiplexer(css::ui::ContextChangeEventMultiplexer::get(
240  xMultiplexer->addContextChangeEventListener(m_pEventListener, xController);
242  }
243  }
244  else
245  {
246  // remove listeners
248  {
249  auto xMultiplexer(css::ui::ContextChangeEventMultiplexer::get(
251  xMultiplexer->removeContextChangeEventListener(m_pEventListener, xController);
253  }
254  }
255 }
256 
258 {
260  return;
261 
262  auto xMultiplexer(
263  css::ui::ContextChangeEventMultiplexer::get(comphelper::getProcessComponentContext()));
264  xMultiplexer->removeAllContextChangeEventListeners(m_pEventListener);
265  m_alisteningControllers.clear();
266 }
267 
268 void SAL_CALL NotebookBarContextChangeEventListener::disposing(const ::css::lang::EventObject&)
269 {
270  mpParent.clear();
271 }
272 
274 {
276  Control::DataChanged(rDCEvt);
277 }
278 
280 {
282  Control::StateChanged(nStateChange);
283  Invalidate();
284 }
285 
287 {
288  const StyleSettings& rStyleSettings = GetSettings().GetStyleSettings();
289  const BitmapEx& aPersona = rStyleSettings.GetPersonaHeader();
290  Wallpaper aWallpaper(aPersona);
292  if (!aPersona.IsEmpty())
293  {
294  SetBackground(aWallpaper);
297  }
298  else
299  {
300  SetBackground(rStyleSettings.GetDialogColor());
303  }
304 
306 }
307 
309 {
310  AllSettings aAllSettings( GetSettings() );
311  StyleSettings aStyleSet( aAllSettings.GetStyleSettings() );
312 
313  ::Color aTextColor = aStyleSet.GetFieldTextColor();
314  aStyleSet.SetDialogTextColor( aTextColor );
315  aStyleSet.SetButtonTextColor( aTextColor );
316  aStyleSet.SetRadioCheckTextColor( aTextColor );
317  aStyleSet.SetGroupTextColor( aTextColor );
318  aStyleSet.SetLabelTextColor( aTextColor );
319  aStyleSet.SetWindowTextColor( aTextColor );
320  aStyleSet.SetTabTextColor(aTextColor);
321  aStyleSet.SetToolTextColor(aTextColor);
322 
323  aAllSettings.SetStyleSettings(aStyleSet);
324  DefaultSettings = aAllSettings;
325 }
326 
328 {
329  AllSettings aAllSettings( GetSettings() );
330  StyleSettings aStyleSet( aAllSettings.GetStyleSettings() );
331 
332  ::Color aTextColor = aStyleSet.GetPersonaMenuBarTextColor().value_or(COL_BLACK );
333  aStyleSet.SetDialogTextColor( aTextColor );
334  aStyleSet.SetButtonTextColor( aTextColor );
335  aStyleSet.SetRadioCheckTextColor( aTextColor );
336  aStyleSet.SetGroupTextColor( aTextColor );
337  aStyleSet.SetLabelTextColor( aTextColor );
338  aStyleSet.SetWindowTextColor( aTextColor );
339  aStyleSet.SetTabTextColor(aTextColor);
340  aStyleSet.SetToolTextColor(aTextColor);
341 
342  aAllSettings.SetStyleSettings(aStyleSet);
343  PersonaSettings = aAllSettings;
344 }
345 
346 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
long Width() const
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...
AllSettings PersonaSettings
Definition: notebookbar.hxx:70
void SetStyleSettings(const StyleSettings &rSet)
long Height() const
std::vector< NotebookbarContextControl * > m_pContextContainers
Definition: notebookbar.hxx:60
virtual Size GetOptimalSize() const override
virtual void setPosSizePixel(long nX, long nY, long nWidth, long nHeight, PosSizeFlags nFlags=PosSizeFlags::All) override
static Context GetContextEnum(const OUString &rsContextName)
static Size getLayoutRequisition(const vcl::Window &rWindow)
Definition: layout.cxx:143
void disposeAndClear()
Definition: vclptr.hxx:200
const StyleSettings & GetStyleSettings() const
static OUString getCustomizedUIRootDir()
Definition: notebookbar.cxx:24
BitmapEx const & GetPersonaHeader() const
VclPtr< vcl::Window > m_xVclContentArea
Definition: notebookbar.hxx:64
virtual void StateChanged(StateChangedType nStateChange) override
Definition: ctrl.cxx:260
virtual Size GetSizePixel() const
Definition: window.cxx:2395
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:57
StateChangedType
Definition: window.hxx:317
virtual void setPosSizePixel(long nX, long nY, long nWidth, long nHeight, PosSizeFlags nFlags=PosSizeFlags::All)
Definition: window.cxx:2667
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:64
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
virtual bool PreNotify(NotifyEvent &rNEvt) override
WinBits const WB_HSCROLL
void SetSystemWindow(SystemWindow *pSystemWindow)
std::set< css::uno::Reference< css::frame::XController > > m_alisteningControllers
Definition: notebookbar.hxx:59
NotebookBarContextChangeEventListener(NotebookBar *p)
Definition: notebookbar.cxx:46
css::uno::Reference< css::ui::XContextChangeEventListener > m_pEventListener
Definition: notebookbar.hxx:58
static OUString GetUIRootDir()
Definition: dialog.cxx:546
WinBits const WB_DIALOGCONTROL
bool IsEmpty() const
Definition: bitmapex.cxx:199
#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:89
int i
virtual void Resize() override
void SetStyle(WallpaperStyle eStyle)
Definition: wall.cxx:209
virtual void Invalidate(InvalidateFlags nFlags=InvalidateFlags::NONE)
Definition: paint.cxx:1162
void ControlListenerForCurrentController(bool bListen)
void StateChanged(const StateChangedType nStateChange) override
std::unique_ptr< VclBuilder > m_pUIBuilder
Definition: builder.hxx:486
bool isLayoutEnabled(const vcl::Window *pWindow)
Definition: layout.cxx:2841
MouseNotifyEvent GetType() const
Definition: event.hxx:303
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:54
AllSettings DefaultSettings
Definition: notebookbar.hxx:69
void SetDisposeCallback(const Link< const SfxViewShell *, void > rDisposeCallback, const SfxViewShell *pViewShell)
void SetStyle(WinBits nStyle)
Definition: window.cxx:1956
vcl::Window * GetWindow(GetWindowType nType) const
Definition: stacking.cxx:1039
void disposeBuilder()
Definition: dialog.cxx:540
OString OUStringToOString(const OUString &str, ConnectionSettings const *settings)
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:67
virtual void Resize() override
Definition: ctrl.cxx:74
void UpdatePersonaSettings()
css::uno::Reference< css::frame::XFrame > mxFrame
Definition: notebookbar.hxx:61
Definition: ctrl.hxx:33
static bool doesFileExist(const OUString &sUIDir, const OUString &sUIFile)
Definition: notebookbar.cxx:32
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:62
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:65
void setWidth(long nWidth)
This implements Widget Layout-based notebook-like menu bar.
Definition: notebookbar.hxx:27
split from the main class since it needs different ref-counting mana
Definition: notebookbar.cxx:42
virtual void DataChanged(const DataChangedEvent &rDCEvt)
Definition: event.cxx:35
void Show(bool bVisible=true, ShowFlags nFlags=ShowFlags::NONE)
Definition: window.cxx:2181