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 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20 #include <sfx2/sidebar/TitleBar.hxx>
21 #include <sfx2/sidebar/Paint.hxx>
22 #include <sfx2/sidebar/Accessible.hxx>
23 #include <sfx2/sidebar/AccessibleTitleBar.hxx>
24
25 #include <tools/svborder.hxx>
26 #include <vcl/gradient.hxx>
27 #include <vcl/lineinfo.hxx>
28
29 #include <com/sun/star/accessibility/AccessibleRole.hpp>
30
31 namespace
32 {
33 const static sal_Int32 gnLeftIconSpace (3);
34 const static sal_Int32 gnRightIconSpace (3);
35 }
36
37 namespace sfx2 { namespace sidebar {
38
TitleBar(const OUString & rsTitle,vcl::Window * pParentWindow,const sidebar::Paint & rInitialBackgroundPaint)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)
47 {
48 maToolBox->SetSelectHdl(LINK(this, TitleBar, SelectionHandler));
49 }
50
~TitleBar()51 TitleBar::~TitleBar()
52 {
53 disposeOnce();
54 }
55
dispose()56 void TitleBar::dispose()
57 {
58 maToolBox.disposeAndClear();
59 vcl::Window::dispose();
60 }
61
SetTitle(const OUString & rsTitle)62 void TitleBar::SetTitle(const OUString& rsTitle)
63 {
64 msTitle = rsTitle;
65 Invalidate();
66 }
67
SetIcon(const Image & rIcon)68 void TitleBar::SetIcon(const Image& rIcon)
69 {
70 maIcon = rIcon;
71 Invalidate();
72 }
73
ApplySettings(vcl::RenderContext & rRenderContext)74 void TitleBar::ApplySettings(vcl::RenderContext& rRenderContext)
75 {
76 rRenderContext.SetBackground(maBackgroundPaint.GetWallpaper());
77 }
78
Paint(vcl::RenderContext & rRenderContext,const tools::Rectangle &)79 void TitleBar::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& /*rUpdateArea*/)
80 {
81 // Paint title bar background.
82 Size aWindowSize (GetSizePixel());
83 tools::Rectangle aTitleBarBox(0,0, aWindowSize.Width(), aWindowSize.Height());
84
85 PaintDecoration(rRenderContext);
86 const tools::Rectangle aTitleBox(GetTitleArea(aTitleBarBox));
87 PaintTitle(rRenderContext, aTitleBox);
88 PaintFocus(rRenderContext, aTitleBox);
89 }
90
DataChanged(const DataChangedEvent &)91 void TitleBar::DataChanged (const DataChangedEvent& /*rEvent*/)
92 {
93 maBackgroundPaint = GetBackgroundPaint();
94 Invalidate();
95 }
96
setPosSizePixel(long nX,long nY,long nWidth,long nHeight,PosSizeFlags nFlags)97 void TitleBar::setPosSizePixel (long nX, long nY, long nWidth, long nHeight, PosSizeFlags nFlags)
98 {
99 Window::setPosSizePixel(nX, nY, nWidth, nHeight, nFlags);
100
101 // Place the toolbox.
102 const sal_Int32 nToolBoxWidth (maToolBox->GetItemPosRect(0).GetWidth());
103 maToolBox->setPosSizePixel(nWidth - nToolBoxWidth,0, nToolBoxWidth, nHeight);
104 maToolBox->Show();
105 }
106
HandleToolBoxItemClick(const sal_uInt16)107 void TitleBar::HandleToolBoxItemClick(const sal_uInt16 /*nItemIndex*/)
108 {
109 // Any real processing has to be done in derived class.
110 }
111
CreateAccessible()112 css::uno::Reference<css::accessibility::XAccessible> TitleBar::CreateAccessible()
113 {
114 SetAccessibleRole(css::accessibility::AccessibleRole::PANEL);
115 return AccessibleTitleBar::Create(*this);
116 }
117
PaintTitle(vcl::RenderContext & rRenderContext,const tools::Rectangle & rTitleBox)118 void TitleBar::PaintTitle(vcl::RenderContext& rRenderContext, const tools::Rectangle& rTitleBox)
119 {
120 rRenderContext.Push(PushFlags::FONT | PushFlags::TEXTCOLOR);
121
122 tools::Rectangle aTitleBox(rTitleBox);
123
124 // When there is an icon then paint it at the left of the given
125 // box.
126 if (!!maIcon)
127 {
128 rRenderContext.DrawImage(Point(aTitleBox.Left() + gnLeftIconSpace,
129 aTitleBox.Top() + (aTitleBox.GetHeight() - maIcon.GetSizePixel().Height()) / 2),
130 maIcon);
131 aTitleBox.AdjustLeft(gnLeftIconSpace + maIcon.GetSizePixel().Width() + gnRightIconSpace );
132 }
133
134 vcl::Font aFont(rRenderContext.GetFont());
135 aFont.SetWeight(WEIGHT_BOLD);
136 rRenderContext.SetFont(aFont);
137
138 // Paint title bar text.
139 rRenderContext.SetTextColor(rRenderContext.GetTextColor());
140 rRenderContext.DrawText(aTitleBox, msTitle, DrawTextFlags::Left | DrawTextFlags::VCenter);
141 rRenderContext.Pop();
142 }
143
PaintFocus(vcl::RenderContext & rRenderContext,const tools::Rectangle & rFocusBox)144 void TitleBar::PaintFocus(vcl::RenderContext& rRenderContext, const tools::Rectangle& rFocusBox)
145 {
146 rRenderContext.Push(PushFlags::FONT | PushFlags::TEXTCOLOR);
147
148 vcl::Font aFont(rRenderContext.GetFont());
149 aFont.SetWeight(WEIGHT_BOLD);
150 rRenderContext.SetFont(aFont);
151
152 const tools::Rectangle aTextBox(rRenderContext.GetTextRect(rFocusBox, msTitle, DrawTextFlags::Left | DrawTextFlags::VCenter));
153
154 const tools::Rectangle aLargerTextBox(aTextBox.Left() - 2,
155 aTextBox.Top() - 2,
156 aTextBox.Right() + 2,
157 aTextBox.Bottom() + 2);
158
159 if (HasFocus())
160 Window::ShowFocus(aLargerTextBox);
161 else
162 Window::HideFocus();
163
164 rRenderContext.Pop();
165 }
166
IMPL_LINK(TitleBar,SelectionHandler,ToolBox *,pToolBox,void)167 IMPL_LINK(TitleBar, SelectionHandler, ToolBox*, pToolBox, void)
168 {
169 OSL_ASSERT(maToolBox.get()==pToolBox);
170 const sal_uInt16 nItemId (maToolBox->GetHighlightItemId());
171
172 HandleToolBoxItemClick(nItemId);
173 }
174
175 } } // end of namespace sfx2::sidebar
176
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
178