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 
23 static OUString getCustomizedUIRootDir()
24 {
25  OUString sShareLayer("${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE(
26  "bootstrap") ":UserInstallation}/user/config/soffice.cfg/");
27  rtl::Bootstrap::expandMacros(sShareLayer);
28  return sShareLayer;
29 }
30 
31 static bool doesFileExist(const OUString& sUIDir, const OUString& sUIFile)
32 {
33  OUString sUri = sUIDir + sUIFile;
34  osl::File file(sUri);
35  return( file.open(0) == osl::FileBase::E_None );
36 }
37 
41 class NotebookBarContextChangeEventListener : public ::cppu::WeakImplHelper<css::ui::XContextChangeEventListener>
42 {
44 public:
45  explicit NotebookBarContextChangeEventListener(NotebookBar *p) : mpParent(p) {}
46 
47  // XContextChangeEventListener
48  virtual void SAL_CALL notifyContextChangeEvent(const css::ui::ContextChangeEventObject& rEvent) override;
49 
50  virtual void SAL_CALL disposing(const ::css::lang::EventObject&) override;
51 };
52 
53 NotebookBar::NotebookBar(Window* pParent, const OString& rID, const OUString& rUIXMLDescription,
54  const css::uno::Reference<css::frame::XFrame>& rFrame,
55  const NotebookBarAddonsItem& aNotebookBarAddonsItem)
56  : Control(pParent)
57  , m_pEventListener(new NotebookBarContextChangeEventListener(this))
58 {
60  OUString sUIDir = getUIRootDir();
61  bool doesCustomizedUIExist = doesFileExist(getCustomizedUIRootDir(), rUIXMLDescription);
62  if ( doesCustomizedUIExist )
63  sUIDir = getCustomizedUIRootDir();
64  m_pUIBuilder.reset(
65  new VclBuilder(this, sUIDir, rUIXMLDescription, rID, rFrame, true, &aNotebookBarAddonsItem));
66  mxFrame = rFrame;
67  // In the Notebookbar's .ui file must exist control handling context
68  // - implementing NotebookbarContextControl interface with id "ContextContainer"
69  // or "ContextContainerX" where X is a number >= 1
70  NotebookbarContextControl* pContextContainer = nullptr;
71  int i = 0;
72  do
73  {
74  OUString aName = "ContextContainer";
75  if (i)
76  aName += OUString::number(i);
77 
78  pContextContainer = dynamic_cast<NotebookbarContextControl*>(m_pUIBuilder->get<Window>(OUStringToOString(aName, RTL_TEXTENCODING_UTF8)));
79  if (pContextContainer)
80  m_pContextContainers.push_back(pContextContainer);
81  i++;
82  }
83  while( pContextContainer != nullptr );
84 
86 }
87 
89 {
90  disposeOnce();
91 }
92 
94 {
95  m_pContextContainers.clear();
100  assert(m_alisteningControllers.empty());
101  m_pEventListener.clear();
103 }
104 
106 {
107  // capture KeyEvents for taskpane cycling
108  if (rNEvt.GetType() == MouseNotifyEvent::KEYINPUT)
109  {
110  if (m_pSystemWindow)
111  return m_pSystemWindow->PreNotify(rNEvt);
112  }
113  return Window::PreNotify( rNEvt );
114 }
115 
117 {
118  if (isLayoutEnabled(this))
120 
121  return Control::GetOptimalSize();
122 }
123 
124 void NotebookBar::setPosSizePixel(long nX, long nY, long nWidth, long nHeight, PosSizeFlags nFlags)
125 {
126  bool bCanHandleSmallerWidth = false;
127  bool bCanHandleSmallerHeight = false;
128 
129  bool bIsLayoutEnabled = isLayoutEnabled(this);
131 
132  if (bIsLayoutEnabled && pChild->GetType() == WindowType::SCROLLWINDOW)
133  {
134  WinBits nStyle = pChild->GetStyle();
135  if (nStyle & (WB_AUTOHSCROLL | WB_HSCROLL))
136  bCanHandleSmallerWidth = true;
137  if (nStyle & (WB_AUTOVSCROLL | WB_VSCROLL))
138  bCanHandleSmallerHeight = true;
139  }
140 
141  Size aSize(GetOptimalSize());
142  if (!bCanHandleSmallerWidth)
143  nWidth = std::max(nWidth, aSize.Width());
144  if (!bCanHandleSmallerHeight)
145  nHeight = std::max(nHeight, aSize.Height());
146 
147  Control::setPosSizePixel(nX, nY, nWidth, nHeight, nFlags);
148 
149  if (bIsLayoutEnabled && (nFlags & PosSizeFlags::Size))
150  VclContainer::setLayoutAllocation(*pChild, Point(0, 0), Size(nWidth, nHeight));
151 }
152 
154 {
155  if(m_pUIBuilder && m_pUIBuilder->get_widget_root())
156  {
157  vcl::Window* pWindow = m_pUIBuilder->get_widget_root()->GetChild(0);
158  if (pWindow)
159  {
160  Size aSize = pWindow->GetSizePixel();
161  aSize.setWidth( GetSizePixel().Width() );
162  pWindow->SetSizePixel(aSize);
163  }
164  }
165  Control::Resize();
166 }
167 
169 {
170  m_pSystemWindow = pSystemWindow;
173 }
174 
175 void SAL_CALL NotebookBarContextChangeEventListener::notifyContextChangeEvent(const css::ui::ContextChangeEventObject& rEvent)
176 {
177  if (mpParent)
178  {
180  pControl->SetContext(vcl::EnumContext::GetContextEnum(rEvent.ContextName));
181  }
182 }
183 
185 {
186  auto xController = mxFrame->getController();
187  if(bListen)
188  {
189  // add listeners
190  if (m_alisteningControllers.count(xController) == 0)
191  {
192  auto xMultiplexer(css::ui::ContextChangeEventMultiplexer::get(
194  xMultiplexer->addContextChangeEventListener(m_pEventListener, xController);
196  }
197  }
198  else
199  {
200  // remove listeners
202  {
203  auto xMultiplexer(css::ui::ContextChangeEventMultiplexer::get(
205  xMultiplexer->removeContextChangeEventListener(m_pEventListener, xController);
207  }
208  }
209 }
210 
212 {
213  auto xMultiplexer(
214  css::ui::ContextChangeEventMultiplexer::get(comphelper::getProcessComponentContext()));
215  xMultiplexer->removeAllContextChangeEventListeners(m_pEventListener);
216  m_alisteningControllers.clear();
217 }
218 
219 void SAL_CALL NotebookBarContextChangeEventListener::disposing(const ::css::lang::EventObject&)
220 {
221  mpParent.clear();
222 }
223 
225 {
227  Control::DataChanged(rDCEvt);
228 }
229 
231 {
233  Control::StateChanged(nStateChange);
234  Invalidate();
235 }
236 
238 {
239  const StyleSettings& rStyleSettings = GetSettings().GetStyleSettings();
240  const BitmapEx& aPersona = rStyleSettings.GetPersonaHeader();
241  Wallpaper aWallpaper(aPersona);
243  if (!aPersona.IsEmpty())
244  {
245  SetBackground(aWallpaper);
248  }
249  else
250  {
251  SetBackground(rStyleSettings.GetDialogColor());
254  }
255 
257 }
258 
260 {
261  AllSettings aAllSettings( GetSettings() );
262  StyleSettings aStyleSet( aAllSettings.GetStyleSettings() );
263 
264  ::Color aTextColor = aStyleSet.GetFieldTextColor();
265  aStyleSet.SetDialogTextColor( aTextColor );
266  aStyleSet.SetButtonTextColor( aTextColor );
267  aStyleSet.SetRadioCheckTextColor( aTextColor );
268  aStyleSet.SetGroupTextColor( aTextColor );
269  aStyleSet.SetLabelTextColor( aTextColor );
270  aStyleSet.SetWindowTextColor( aTextColor );
271  aStyleSet.SetTabTextColor(aTextColor);
272  aStyleSet.SetToolTextColor(aTextColor);
273 
274  aAllSettings.SetStyleSettings(aStyleSet);
275  DefaultSettings = aAllSettings;
276 }
277 
279 {
280  AllSettings aAllSettings( GetSettings() );
281  StyleSettings aStyleSet( aAllSettings.GetStyleSettings() );
282 
283  ::Color aTextColor = aStyleSet.GetPersonaMenuBarTextColor().value_or(COL_BLACK );
284  aStyleSet.SetDialogTextColor( aTextColor );
285  aStyleSet.SetButtonTextColor( aTextColor );
286  aStyleSet.SetRadioCheckTextColor( aTextColor );
287  aStyleSet.SetGroupTextColor( aTextColor );
288  aStyleSet.SetLabelTextColor( aTextColor );
289  aStyleSet.SetWindowTextColor( aTextColor );
290  aStyleSet.SetTabTextColor(aTextColor);
291  aStyleSet.SetToolTextColor(aTextColor);
292 
293  aAllSettings.SetStyleSettings(aStyleSet);
294  PersonaSettings = aAllSettings;
295 }
296 
297 /* 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:58
const Color & GetDialogColor() const
constexpr::Color COL_BLACK(0x00, 0x00, 0x00)
virtual void dispose() override
This is intended to be used to clear any locally held references to other Window-subclass objects...
Definition: notebookbar.cxx:93
AllSettings PersonaSettings
Definition: notebookbar.hxx:58
void SetStyleSettings(const StyleSettings &rSet)
long Height() const
std::vector< NotebookbarContextControl * > m_pContextContainers
Definition: notebookbar.hxx:54
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:133
const StyleSettings & GetStyleSettings() const
static OUString getCustomizedUIRootDir()
Definition: notebookbar.cxx:23
BitmapEx const & GetPersonaHeader() const
virtual void StateChanged(StateChangedType nStateChange) override
Definition: ctrl.cxx:260
virtual Size GetSizePixel() const
Definition: window.cxx:2365
virtual void SetSizePixel(const Size &rNewSize)
Definition: window2.cxx:1257
WinBits const WB_AUTOVSCROLL
PosSizeFlags
Definition: window.hxx:141
WinBits const WB_VSCROLL
virtual void SetSettings(const AllSettings &rSettings) override
void UpdateDefaultSettings()
VclPtr< SystemWindow > m_pSystemWindow
Definition: notebookbar.hxx:51
StateChangedType
Definition: window.hxx:311
virtual void setPosSizePixel(long nX, long nY, long nWidth, long nHeight, PosSizeFlags nFlags=PosSizeFlags::All)
Definition: window.cxx:2682
sal_Int64 WinBits
void SetBackground()
void UpdateBackground()
Reference< XController > xController
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:53
NotebookBarContextChangeEventListener(NotebookBar *p)
Definition: notebookbar.cxx:45
css::uno::Reference< css::ui::XContextChangeEventListener > m_pEventListener
Definition: notebookbar.hxx:52
WinBits const WB_DIALOGCONTROL
bool IsEmpty() const
Definition: bitmapex.cxx:203
#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:200
Window(WindowType nType)
Definition: window.cxx:85
virtual void Resize() override
void SetStyle(WallpaperStyle eStyle)
Definition: wall.cxx:209
int i
virtual void Invalidate(InvalidateFlags nFlags=InvalidateFlags::NONE)
Definition: paint.cxx:1160
void ControlListenerForCurrentController(bool bListen)
void StateChanged(const StateChangedType nStateChange) override
std::unique_ptr< VclBuilder > m_pUIBuilder
Definition: builder.hxx:499
bool isLayoutEnabled(const vcl::Window *pWindow)
Definition: layout.cxx:2690
MouseNotifyEvent GetType() const
Definition: event.hxx:294
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:420
NotebookBar(Window *pParent, const OString &rID, const OUString &rUIXMLDescription, const css::uno::Reference< css::frame::XFrame > &rFrame, const NotebookBarAddonsItem &aNotebookBarAddonsItem)
Definition: notebookbar.cxx:53
static OUString getUIRootDir()
Definition: dialog.cxx:544
AllSettings DefaultSettings
Definition: notebookbar.hxx:57
void SetStyle(WinBits nStyle)
Definition: window.cxx:1926
vcl::Window * GetWindow(GetWindowType nType) const
Definition: stacking.cxx:1046
void disposeBuilder()
Definition: dialog.cxx:538
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:1007
virtual void Resize() override
Definition: ctrl.cxx:74
void UpdatePersonaSettings()
css::uno::Reference< css::frame::XFrame > mxFrame
Definition: notebookbar.hxx:55
Definition: ctrl.hxx:33
static bool doesFileExist(const OUString &sUIDir, const OUString &sUIFile)
Definition: notebookbar.cxx:31
OUString aName
virtual ~NotebookBar() override
Definition: notebookbar.cxx:88
Reference< XComponentContext > getProcessComponentContext()
virtual void SAL_CALL notifyContextChangeEvent(const css::ui::ContextChangeEventObject &rEvent) override
void RemoveWindow(vcl::Window *pWindow)
WinBits const WB_AUTOHSCROLL
virtual void SAL_CALL disposing(const ::css::lang::EventObject &) override
WinBits GetStyle() const
Definition: window2.cxx:948
void AddWindow(vcl::Window *pWindow)
TaskPaneList * GetTaskPaneList()
Definition: syswin.cxx:242
virtual void SetContext(vcl::EnumContext::Context eContext)=0
void StopListeningAllControllers()
void setWidth(long nWidth)
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:41
virtual void DataChanged(const DataChangedEvent &rDCEvt)
Definition: event.cxx:35