Branch libreoffice-5-0-4
[LibreOffice.git] / sfx2 / source / sidebar / TitleBar.cxx
blob239cf04131313513517a09829cfabb3361f410cb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "TitleBar.hxx"
21 #include "Paint.hxx"
22 #include "Accessible.hxx"
23 #include "AccessibleTitleBar.hxx"
25 #include <tools/svborder.hxx>
26 #include <vcl/gradient.hxx>
27 #include <vcl/lineinfo.hxx>
29 #include <com/sun/star/accessibility/AccessibleRole.hpp>
31 namespace
33 const static sal_Int32 gnLeftIconSpace (3);
34 const static sal_Int32 gnRightIconSpace (3);
37 namespace sfx2 { namespace sidebar {
39 TitleBar::TitleBar(const OUString& rsTitle,
40 vcl::Window* pParentWindow,
41 const sidebar::Paint& rInitialBackgroundPaint)
42 : Window(pParentWindow)
43 , maToolBox(VclPtr<SidebarToolBox>::Create(this))
44 , msTitle(rsTitle)
45 , maIcon()
46 , maBackgroundPaint(rInitialBackgroundPaint)
48 maToolBox->SetSelectHdl(LINK(this, TitleBar, SelectionHandler));
51 TitleBar::~TitleBar()
53 disposeOnce();
56 void TitleBar::dispose()
58 maToolBox.disposeAndClear();
59 vcl::Window::dispose();
62 void TitleBar::SetTitle(const OUString& rsTitle)
64 msTitle = rsTitle;
65 Invalidate();
68 void TitleBar::SetIcon(const Image& rIcon)
70 maIcon = rIcon;
71 Invalidate();
74 void TitleBar::ApplySettings(vcl::RenderContext& rRenderContext)
76 rRenderContext.SetBackground(maBackgroundPaint.GetWallpaper());
79 void TitleBar::Paint(vcl::RenderContext& rRenderContext, const Rectangle& /*rUpdateArea*/)
81 // Paint title bar background.
82 Size aWindowSize (GetSizePixel());
83 Rectangle aTitleBarBox(0,0, aWindowSize.Width(), aWindowSize.Height());
85 PaintDecoration(rRenderContext, aTitleBarBox);
86 const Rectangle aTitleBox(GetTitleArea(aTitleBarBox));
87 PaintTitle(rRenderContext, aTitleBox);
88 PaintFocus(rRenderContext, aTitleBox);
91 void TitleBar::DataChanged (const DataChangedEvent& /*rEvent*/)
93 maBackgroundPaint = GetBackgroundPaint();
94 Invalidate();
97 void TitleBar::setPosSizePixel (long nX, long nY, long nWidth, long nHeight, PosSizeFlags nFlags)
99 Window::setPosSizePixel(nX, nY, nWidth, nHeight, nFlags);
101 // Place the toolbox.
102 const sal_Int32 nToolBoxWidth (maToolBox->GetItemPosRect(0).GetWidth());
103 maToolBox->setPosSizePixel(nWidth - nToolBoxWidth,0, nToolBoxWidth, nHeight, PosSizeFlags::PosSize);
104 maToolBox->Show();
107 void TitleBar::HandleToolBoxItemClick(const sal_uInt16 /*nItemIndex*/)
109 // Any real processing has to be done in derived class.
112 css::uno::Reference<css::accessibility::XAccessible> TitleBar::CreateAccessible()
114 SetAccessibleRole(css::accessibility::AccessibleRole::PANEL);
115 return AccessibleTitleBar::Create(*this);
118 void TitleBar::PaintTitle(vcl::RenderContext& rRenderContext, const Rectangle& rTitleBox)
120 rRenderContext.Push(PushFlags::FONT | PushFlags::TEXTCOLOR);
122 Rectangle aTitleBox(rTitleBox);
124 // When there is an icon then paint it at the left of the given
125 // box.
126 if (!!maIcon)
128 rRenderContext.DrawImage(Point(aTitleBox.Left() + gnLeftIconSpace,
129 aTitleBox.Top() + (aTitleBox.GetHeight() - maIcon.GetSizePixel().Height()) / 2),
130 maIcon);
131 aTitleBox.Left() += gnLeftIconSpace + maIcon.GetSizePixel().Width() + gnRightIconSpace;
134 vcl::Font aFont(rRenderContext.GetFont());
135 aFont.SetWeight(WEIGHT_BOLD);
136 rRenderContext.SetFont(aFont);
138 // Paint title bar text.
139 rRenderContext.SetTextColor(rRenderContext.GetTextColor());
140 rRenderContext.DrawText(aTitleBox, msTitle, DrawTextFlags::Left | DrawTextFlags::VCenter);
141 rRenderContext.Pop();
144 void TitleBar::PaintFocus(vcl::RenderContext& rRenderContext, const Rectangle& rFocusBox)
146 rRenderContext.Push(PushFlags::FONT | PushFlags::TEXTCOLOR);
148 vcl::Font aFont(rRenderContext.GetFont());
149 aFont.SetWeight(WEIGHT_BOLD);
150 rRenderContext.SetFont(aFont);
152 const Rectangle aTextBox(rRenderContext.GetTextRect(rFocusBox, msTitle, DrawTextFlags::Left | DrawTextFlags::VCenter));
154 const Rectangle aLargerTextBox(aTextBox.Left() - 2,
155 aTextBox.Top() - 2,
156 aTextBox.Right() + 2,
157 aTextBox.Bottom() + 2);
159 if (HasFocus())
160 Window::ShowFocus(aLargerTextBox);
161 else
162 Window::HideFocus();
164 rRenderContext.Pop();
167 IMPL_LINK_TYPED(TitleBar, SelectionHandler, ToolBox*, pToolBox, void)
169 (void)pToolBox;
170 OSL_ASSERT(maToolBox.get()==pToolBox);
171 const sal_uInt16 nItemId (maToolBox->GetHighlightItemId());
173 HandleToolBoxItemClick(nItemId);
176 } } // end of namespace sfx2::sidebar
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */