Bump version to 5.0-14
[LibreOffice.git] / svtools / source / toolpanel / toolpaneldrawer.cxx
blobe94da73ef89f60329497c4e04a5f5ff64e5d509c
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 .
21 #include "toolpaneldrawer.hxx"
22 #include "toolpaneldrawerpeer.hxx"
23 #include <svtools/svtresid.hxx>
24 #include <svtools/svtools.hrc>
26 #include <com/sun/star/accessibility/AccessibleRole.hpp>
28 #include <vcl/lineinfo.hxx>
29 #include <vcl/image.hxx>
30 #include <vcl/svapp.hxx>
31 #include <vcl/vclevent.hxx>
32 #include <vcl/settings.hxx>
35 namespace svt
39 using ::com::sun::star::uno::Reference;
40 using ::com::sun::star::awt::XWindowPeer;
41 namespace AccessibleRole = ::com::sun::star::accessibility::AccessibleRole;
43 static const int s_nIndentationWidth = 16;
46 //= DrawerVisualization
49 DrawerVisualization::DrawerVisualization( ToolPanelDrawer& i_rParent )
50 :Window( &i_rParent )
51 ,m_rDrawer( i_rParent )
53 SetMouseTransparent( true );
54 Show();
55 SetAccessibleRole( AccessibleRole::LABEL );
59 void DrawerVisualization::Paint(vcl::RenderContext& rRenderContext, const Rectangle& i_rBoundingBox)
61 Window::Paint(rRenderContext, i_rBoundingBox);
62 m_rDrawer.Paint(rRenderContext);
66 //= ToolPanelDrawer
69 ToolPanelDrawer::ToolPanelDrawer( vcl::Window& i_rParent, const OUString& i_rTitle )
70 :Window( &i_rParent, WB_TABSTOP )
71 ,m_pPaintDevice( VclPtr<VirtualDevice>::Create( *this ) )
72 ,m_aVisualization( VclPtr<DrawerVisualization>::Create(*this) )
73 ,m_bFocused( false )
74 ,m_bExpanded( false )
76 EnableMapMode( false );
77 SetBackground( Wallpaper() );
78 SetPointer( PointerStyle::RefHand );
80 SetAccessibleRole( AccessibleRole::LIST_ITEM );
82 SetText( i_rTitle );
83 SetAccessibleName( i_rTitle );
84 SetAccessibleDescription( i_rTitle );
86 m_aVisualization->SetAccessibleName( i_rTitle );
87 m_aVisualization->SetAccessibleDescription( i_rTitle );
90 ToolPanelDrawer::~ToolPanelDrawer()
92 disposeOnce();
95 void ToolPanelDrawer::dispose()
97 m_aVisualization.disposeAndClear();
98 vcl::Window::dispose();
101 long ToolPanelDrawer::GetPreferredHeightPixel() const
103 Rectangle aTitleBarBox( impl_calcTitleBarBox( impl_calcTextBoundingBox() ) );
104 return aTitleBarBox.GetHeight();
108 void ToolPanelDrawer::Paint(vcl::RenderContext& rRenderContext)
110 m_pPaintDevice->SetMapMode(rRenderContext.GetMapMode());
111 m_pPaintDevice->SetOutputSize(GetOutputSizePixel());
112 m_pPaintDevice->SetSettings(rRenderContext.GetSettings());
113 m_pPaintDevice->SetDrawMode(rRenderContext.GetDrawMode());
115 const Rectangle aTextBox(impl_calcTextBoundingBox());
116 impl_paintBackground(impl_calcTitleBarBox(aTextBox));
118 Rectangle aFocusBox(impl_paintExpansionIndicator(aTextBox));
120 m_pPaintDevice->DrawText(aTextBox, GetText(), impl_getTextStyle());
122 aFocusBox.Union(aTextBox);
123 aFocusBox.Left() += 2;
124 impl_paintFocusIndicator(aFocusBox);
126 rRenderContext.DrawOutDev(Point(), GetOutputSizePixel(), Point(), GetOutputSizePixel(), *m_pPaintDevice);
130 Rectangle ToolPanelDrawer::impl_paintExpansionIndicator( const Rectangle& i_rTextBox )
132 Rectangle aExpansionIndicatorArea;
134 Image aImage( impl_getExpansionIndicator() );
135 const int nHeight( aImage.GetSizePixel().Height() );
136 if ( nHeight > 0 )
138 Point aPosition(
140 i_rTextBox.Top() + ( GetTextHeight() - nHeight ) / 2
142 m_pPaintDevice->DrawImage( aPosition, aImage );
144 aExpansionIndicatorArea = Rectangle( aPosition, aImage.GetSizePixel() );
147 return aExpansionIndicatorArea;
151 Image ToolPanelDrawer::impl_getExpansionIndicator() const
153 sal_uInt16 nResourceId = 0;
154 if ( m_bExpanded )
155 nResourceId = IMG_TRIANGLE_DOWN;
156 else
157 nResourceId = IMG_TRIANGLE_RIGHT;
158 return Image( SvtResId( nResourceId ) );
162 DrawTextFlags ToolPanelDrawer::impl_getTextStyle() const
164 const DrawTextFlags nBasicStyle = DrawTextFlags::Left
165 | DrawTextFlags::Top
166 | DrawTextFlags::WordBreak;
168 if ( IsEnabled() )
169 return nBasicStyle;
171 return nBasicStyle | DrawTextFlags::Disable;
175 void ToolPanelDrawer::impl_paintBackground( const Rectangle& i_rTitleBarBox )
177 m_pPaintDevice->SetFillColor( GetSettings().GetStyleSettings().GetDialogColor() );
178 m_pPaintDevice->DrawRect( i_rTitleBarBox );
180 m_pPaintDevice->SetFillColor();
181 m_pPaintDevice->SetLineColor( GetSettings().GetStyleSettings().GetLightColor() );
182 m_pPaintDevice->DrawLine( i_rTitleBarBox.TopLeft(), i_rTitleBarBox.TopRight() );
183 m_pPaintDevice->DrawLine( i_rTitleBarBox.TopLeft(), i_rTitleBarBox.BottomLeft() );
185 m_pPaintDevice->SetLineColor( GetSettings().GetStyleSettings().GetShadowColor() );
186 m_pPaintDevice->DrawLine( i_rTitleBarBox.BottomLeft(), i_rTitleBarBox.BottomRight() );
187 m_pPaintDevice->DrawLine( i_rTitleBarBox.TopRight(), i_rTitleBarBox.BottomRight() );
191 void ToolPanelDrawer::impl_paintFocusIndicator( const Rectangle& i_rTextBox )
193 if ( m_bFocused )
195 const Rectangle aTextPixelBox( m_pPaintDevice->LogicToPixel( i_rTextBox ) );
197 m_pPaintDevice->EnableMapMode( false );
198 m_pPaintDevice->SetFillColor();
200 Rectangle aBox( i_rTextBox );
201 aBox.Top() -= 1;
202 aBox.Bottom() += 1;
204 m_pPaintDevice->DrawRect( aTextPixelBox );
206 LineInfo aDottedStyle( LINE_DASH );
207 aDottedStyle.SetDashCount( 0 );
208 aDottedStyle.SetDotCount( 1 );
209 aDottedStyle.SetDotLen( 1 );
210 aDottedStyle.SetDistance( 1 );
212 m_pPaintDevice->SetLineColor( COL_BLACK );
213 m_pPaintDevice->DrawPolyLine( Polygon( aTextPixelBox ), aDottedStyle );
214 m_pPaintDevice->EnableMapMode( false );
216 else
217 HideFocus();
221 void ToolPanelDrawer::GetFocus()
223 m_bFocused = true;
224 Invalidate();
228 void ToolPanelDrawer::LoseFocus()
230 m_bFocused = false;
231 Invalidate();
235 void ToolPanelDrawer::Resize()
237 Window::Resize();
238 m_aVisualization->SetPosSizePixel( Point(), GetOutputSizePixel() );
242 void ToolPanelDrawer::MouseButtonDown( const MouseEvent& i_rMouseEvent )
244 // consume this event, and do not forward to the base class - it would sent a NotifyEvent, which in turn, when
245 // we live in a DockingWindow, would start undocking
246 (void)i_rMouseEvent;
249 void ToolPanelDrawer::ApplySettings(vcl::RenderContext& rRenderContext)
251 const StyleSettings& rStyleSettings(rRenderContext.GetSettings().GetStyleSettings());
252 ApplyControlFont(rRenderContext, rStyleSettings.GetAppFont());
253 ApplyControlForeground(rRenderContext, rStyleSettings.GetButtonTextColor());
254 rRenderContext.SetTextFillColor();
257 void ToolPanelDrawer::DataChanged( const DataChangedEvent& i_rEvent )
259 Window::DataChanged( i_rEvent );
261 switch ( i_rEvent.GetType() )
263 case DataChangedEventType::SETTINGS:
264 if ( !( i_rEvent.GetFlags() & AllSettingsFlags::STYLE ) )
265 break;
266 SetSettings( Application::GetSettings() );
267 m_pPaintDevice.reset( VclPtr<VirtualDevice>::Create( *this ) );
269 // fall through.
271 case DataChangedEventType::FONTS:
272 case DataChangedEventType::FONTSUBSTITUTION:
274 const StyleSettings& rStyleSettings( GetSettings().GetStyleSettings() );
275 ApplyControlFont(*this, rStyleSettings.GetAppFont());
276 ApplyControlForeground(*this, rStyleSettings.GetButtonTextColor());
277 SetTextFillColor();
278 Invalidate();
280 break;
281 default: break;
286 Reference< XWindowPeer > ToolPanelDrawer::GetComponentInterface( bool i_bCreate )
288 Reference< XWindowPeer > xWindowPeer( Window::GetComponentInterface( false ) );
289 if ( !xWindowPeer.is() && i_bCreate )
291 xWindowPeer.set( new ToolPanelDrawerPeer() );
292 SetComponentInterface( xWindowPeer );
294 return xWindowPeer;
298 Rectangle ToolPanelDrawer::impl_calcTextBoundingBox() const
300 vcl::Font aFont( GetFont() );
301 if ( m_bExpanded )
302 aFont.SetWeight( m_bExpanded ? WEIGHT_BOLD : WEIGHT_NORMAL );
303 m_pPaintDevice->SetFont( aFont );
305 int nAvailableWidth = m_pPaintDevice->GetTextWidth( GetText() );
307 Rectangle aTextBox(
308 Point(),
309 Size(
310 nAvailableWidth,
311 GetSettings().GetStyleSettings().GetTitleHeight()
314 aTextBox.Top() += ( aTextBox.GetHeight() - GetTextHeight() ) / 2;
315 aTextBox.Left() += s_nIndentationWidth;
316 aTextBox.Right() -= 1;
318 aTextBox = m_pPaintDevice->GetTextRect( aTextBox, GetText(), impl_getTextStyle() );
319 return aTextBox;
323 Rectangle ToolPanelDrawer::impl_calcTitleBarBox( const Rectangle& i_rTextBox ) const
325 Rectangle aTitleBarBox( i_rTextBox );
326 aTitleBarBox.Bottom() += aTitleBarBox.Top();
327 aTitleBarBox.Top() = 0;
328 aTitleBarBox.Left() = 0;
330 const long nWidth = GetOutputSizePixel().Width();
331 if ( aTitleBarBox.GetWidth() < nWidth )
332 aTitleBarBox.Right() = nWidth - 1;
334 return aTitleBarBox;
338 void ToolPanelDrawer::SetExpanded( const bool i_bExpanded )
340 if ( m_bExpanded != i_bExpanded )
342 m_bExpanded = i_bExpanded;
343 CallEventListeners( m_bExpanded ? VCLEVENT_ITEM_EXPANDED : VCLEVENT_ITEM_COLLAPSED );
344 Invalidate();
349 } // namespace svt
352 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */