1 /*************************************************************************
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * Copyright 2000, 2010 Oracle and/or its affiliates.
5  *
6  * OpenOffice.org - a multi-platform office productivity suite
7  *
8  * This file is part of OpenOffice.org.
9  *
10  * OpenOffice.org is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License version 3
12  * only, as published by the Free Software Foundation.
13  *
14  * OpenOffice.org is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Lesser General Public License version 3 for more details
18  * (a copy is included in the LICENSE file that accompanied this code).
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * version 3 along with OpenOffice.org.  If not, see
22  * <http://www.openoffice.org/license.html>
23  * for a copy of the LGPLv3 License.
24  *
25  ************************************************************************/
26 
27 #include "precompiled_svtools.hxx"
28 
29 #include "toolpaneldrawer.hxx"
30 #include "toolpaneldrawerpeer.hxx"
31 #include "svtools/svtdata.hxx"
32 #include "svtools/svtools.hrc"
33 
34 #include <com/sun/star/accessibility/AccessibleRole.hpp>
35 
36 #include <vcl/lineinfo.hxx>
37 #include <vcl/image.hxx>
38 #include <vcl/svapp.hxx>
39 #include <vcl/vclevent.hxx>
40 
41 //......................................................................................................................
42 namespace svt
43 {
44 //......................................................................................................................
45 
46     using ::com::sun::star::uno::Reference;
47     using ::com::sun::star::awt::XWindowPeer;
48     namespace AccessibleRole = ::com::sun::star::accessibility::AccessibleRole;
49 
50     static const int s_nIndentationWidth = 16;
51 
52 	//==================================================================================================================
53 	//= DrawerVisualization
54 	//==================================================================================================================
55 	//------------------------------------------------------------------------------------------------------------------
56     DrawerVisualization::DrawerVisualization( ToolPanelDrawer& i_rParent )
57         :Window( &i_rParent )
58         ,m_rDrawer( i_rParent )
59     {
60         SetMouseTransparent( sal_True );
61         Show();
62         SetAccessibleRole( AccessibleRole::LABEL );
63     }
64 
65 	//------------------------------------------------------------------------------------------------------------------
66     DrawerVisualization::~DrawerVisualization()
67     {
68     }
69 
70 	//------------------------------------------------------------------------------------------------------------------
71     void DrawerVisualization::Paint( const Rectangle& i_rBoundingBox )
72     {
73         Window::Paint( i_rBoundingBox );
74         m_rDrawer.Paint();
75     }
76 
77 	//==================================================================================================================
78 	//= ToolPanelDrawer
79 	//==================================================================================================================
80 	//------------------------------------------------------------------------------------------------------------------
81     ToolPanelDrawer::ToolPanelDrawer( Window& i_rParent, const ::rtl::OUString& i_rTitle )
82         :Window( &i_rParent, WB_TABSTOP )
83         ,m_pPaintDevice( new VirtualDevice( *this ) )
84         ,m_aVisualization( *this )
85         ,m_bFocused( false )
86         ,m_bExpanded( false )
87     {
88         EnableMapMode( sal_False );
89         SetBackground( Wallpaper() );
90         SetPointer( POINTER_REFHAND );
91 
92         SetAccessibleRole( AccessibleRole::LIST_ITEM );
93 
94         SetText( i_rTitle );
95         SetAccessibleName( i_rTitle );
96         SetAccessibleDescription( i_rTitle );
97 
98         m_aVisualization.SetAccessibleName( i_rTitle );
99         m_aVisualization.SetAccessibleDescription( i_rTitle );
100     }
101 
102 	//------------------------------------------------------------------------------------------------------------------
103     ToolPanelDrawer::~ToolPanelDrawer()
104     {
105     }
106 
107 	//------------------------------------------------------------------------------------------------------------------
108     long ToolPanelDrawer::GetPreferredHeightPixel() const
109     {
110         Rectangle aTitleBarBox( impl_calcTitleBarBox( impl_calcTextBoundingBox() ) );
111         return aTitleBarBox.GetHeight();
112     }
113 
114 	//------------------------------------------------------------------------------------------------------------------
115     void ToolPanelDrawer::Paint()
116     {
117         m_pPaintDevice->SetMapMode( GetMapMode() );
118         m_pPaintDevice->SetOutputSize( GetOutputSizePixel() );
119         m_pPaintDevice->SetSettings( GetSettings() );
120         m_pPaintDevice->SetDrawMode( GetDrawMode() );
121 
122         const Rectangle aTextBox( impl_calcTextBoundingBox() );
123         impl_paintBackground( impl_calcTitleBarBox( aTextBox ) );
124 
125         Rectangle aFocusBox( impl_paintExpansionIndicator( aTextBox ) );
126 
127         m_pPaintDevice->DrawText( aTextBox, GetText(), impl_getTextStyle() );
128 
129         aFocusBox.Union( aTextBox );
130         aFocusBox.Left() += 2;
131         impl_paintFocusIndicator( aFocusBox );
132 
133         m_aVisualization.DrawOutDev(
134             Point(), GetOutputSizePixel(),
135             Point(), GetOutputSizePixel(),
136             *m_pPaintDevice
137         );
138     }
139 
140     //------------------------------------------------------------------------------------------------------------------
141     Rectangle ToolPanelDrawer::impl_paintExpansionIndicator( const Rectangle& i_rTextBox )
142     {
143         Rectangle aExpansionIndicatorArea;
144 
145         Image aImage( impl_getExpansionIndicator() );
146         const int nHeight( aImage.GetSizePixel().Height() );
147         if ( nHeight > 0 )
148         {
149             Point aPosition(
150                 0,
151                 i_rTextBox.Top() + ( GetTextHeight() - nHeight ) / 2
152             );
153             m_pPaintDevice->DrawImage( aPosition, aImage );
154 
155             aExpansionIndicatorArea = Rectangle( aPosition, aImage.GetSizePixel() );
156         }
157 
158         return aExpansionIndicatorArea;
159     }
160 
161     //------------------------------------------------------------------------------------------------------------------
162     Image ToolPanelDrawer::impl_getExpansionIndicator() const
163     {
164         const bool bHighContrastMode( GetSettings().GetStyleSettings().GetHighContrastMode() != 0 );
165         sal_uInt16 nResourceId = 0;
166         if ( m_bExpanded )
167             if ( bHighContrastMode )
168                 nResourceId = IMG_TRIANGLE_DOWN_HC;
169             else
170                 nResourceId = IMG_TRIANGLE_DOWN;
171         else
172             if ( bHighContrastMode )
173                 nResourceId = IMG_TRIANGLE_RIGHT_HC;
174             else
175                 nResourceId = IMG_TRIANGLE_RIGHT;
176         return Image( SvtResId( nResourceId ) );
177     }
178 
179     //------------------------------------------------------------------------------------------------------------------
180     sal_uInt16 ToolPanelDrawer::impl_getTextStyle() const
181     {
182         const sal_uInt16 nBasicStyle =  TEXT_DRAW_LEFT
183                                 |   TEXT_DRAW_TOP
184                                 |   TEXT_DRAW_WORDBREAK;
185 
186         if ( IsEnabled() )
187             return nBasicStyle;
188 
189         return nBasicStyle | TEXT_DRAW_DISABLE;
190     }
191 
192     //------------------------------------------------------------------------------------------------------------------
193     void ToolPanelDrawer::impl_paintBackground( const Rectangle& i_rTitleBarBox )
194     {
195         m_pPaintDevice->SetFillColor( GetSettings().GetStyleSettings().GetDialogColor() );
196         m_pPaintDevice->DrawRect( i_rTitleBarBox );
197 
198         m_pPaintDevice->SetFillColor();
199         m_pPaintDevice->SetLineColor( GetSettings().GetStyleSettings().GetLightColor() );
200         m_pPaintDevice->DrawLine( i_rTitleBarBox.TopLeft(), i_rTitleBarBox.TopRight() );
201         m_pPaintDevice->DrawLine( i_rTitleBarBox.TopLeft(), i_rTitleBarBox.BottomLeft() );
202 
203         m_pPaintDevice->SetLineColor( GetSettings().GetStyleSettings().GetShadowColor() );
204         m_pPaintDevice->DrawLine( i_rTitleBarBox.BottomLeft(), i_rTitleBarBox.BottomRight() );
205         m_pPaintDevice->DrawLine( i_rTitleBarBox.TopRight(), i_rTitleBarBox.BottomRight() );
206     }
207 
208     //------------------------------------------------------------------------------------------------------------------
209     void ToolPanelDrawer::impl_paintFocusIndicator( const Rectangle& i_rTextBox )
210     {
211         if ( m_bFocused )
212         {
213             const Rectangle aTextPixelBox( m_pPaintDevice->LogicToPixel( i_rTextBox ) );
214 
215             m_pPaintDevice->EnableMapMode( sal_False );
216             m_pPaintDevice->SetFillColor();
217 
218             Rectangle aBox( i_rTextBox );
219             aBox.Top() -= 1;
220             aBox.Bottom() += 1;
221 
222             m_pPaintDevice->DrawRect( aTextPixelBox );
223 
224             LineInfo aDottedStyle( LINE_DASH );
225             aDottedStyle.SetDashCount( 0 );
226             aDottedStyle.SetDotCount( 1 );
227             aDottedStyle.SetDotLen( 1 );
228             aDottedStyle.SetDistance( 1 );
229 
230             m_pPaintDevice->SetLineColor( COL_BLACK );
231             m_pPaintDevice->DrawPolyLine( Polygon( aTextPixelBox ), aDottedStyle );
232             m_pPaintDevice->EnableMapMode( sal_False );
233         }
234         else
235             HideFocus();
236     }
237 
238     //------------------------------------------------------------------------------------------------------------------
239     void ToolPanelDrawer::GetFocus()
240     {
241         m_bFocused = true;
242         Invalidate();
243     }
244 
245     //------------------------------------------------------------------------------------------------------------------
246     void ToolPanelDrawer::LoseFocus()
247     {
248         m_bFocused = false;
249         Invalidate();
250     }
251 
252     //------------------------------------------------------------------------------------------------------------------
253     void ToolPanelDrawer::Resize()
254     {
255         Window::Resize();
256         m_aVisualization.SetPosSizePixel( Point(), GetOutputSizePixel() );
257     }
258 
259     //------------------------------------------------------------------------------------------------------------------
260     void ToolPanelDrawer::MouseButtonDown( const MouseEvent& i_rMouseEvent )
261     {
262         // consume this event, and do not forward to the base class - it would sent a NotifyEvent, which in turn, when
263         // we live in a DockingWindow, would start undocking
264         (void)i_rMouseEvent;
265     }
266 
267     //------------------------------------------------------------------------------------------------------------------
268     void ToolPanelDrawer::DataChanged( const DataChangedEvent& i_rEvent )
269     {
270         Window::DataChanged( i_rEvent );
271 
272         switch ( i_rEvent.GetType() )
273         {
274             case DATACHANGED_SETTINGS:
275                 if ( ( i_rEvent.GetFlags() & SETTINGS_STYLE ) == 0 )
276                     break;
277                 SetSettings( Application::GetSettings() );
278                 m_pPaintDevice.reset( new VirtualDevice( *this ) );
279 
280                 // fall through.
281 
282             case DATACHANGED_FONTS:
283             case DATACHANGED_FONTSUBSTITUTION:
284             {
285                 const StyleSettings& rStyleSettings( GetSettings().GetStyleSettings() );
286 
287                 // Font.
288                 Font aFont = rStyleSettings.GetAppFont();
289                 if ( IsControlFont() )
290                     aFont.Merge( GetControlFont() );
291                 SetZoomedPointFont( aFont );
292 
293                 // Color.
294                 Color aColor;
295                 if ( IsControlForeground() )
296                     aColor = GetControlForeground();
297                 else
298                     aColor = rStyleSettings.GetButtonTextColor();
299                 SetTextColor( aColor );
300                 SetTextFillColor();
301 
302                 Invalidate();
303             }
304             break;
305         }
306     }
307 
308     //------------------------------------------------------------------------------------------------------------------
309     Reference< XWindowPeer > ToolPanelDrawer::GetComponentInterface( sal_Bool i_bCreate )
310     {
311         Reference< XWindowPeer > xWindowPeer( Window::GetComponentInterface( sal_False ) );
312         if ( !xWindowPeer.is() && i_bCreate )
313         {
314             xWindowPeer.set( new ToolPanelDrawerPeer() );
315             SetComponentInterface( xWindowPeer );
316         }
317         return xWindowPeer;
318     }
319 
320     //------------------------------------------------------------------------------------------------------------------
321     Rectangle ToolPanelDrawer::impl_calcTextBoundingBox() const
322     {
323         Font aFont( GetFont() );
324         if ( m_bExpanded )
325             aFont.SetWeight( m_bExpanded ? WEIGHT_BOLD : WEIGHT_NORMAL );
326         m_pPaintDevice->SetFont( aFont );
327 
328         int nAvailableWidth = m_pPaintDevice->GetTextWidth( GetText() );
329 
330         Rectangle aTextBox(
331             Point(),
332             Size(
333                 nAvailableWidth,
334                 GetSettings().GetStyleSettings().GetTitleHeight()
335             )
336         );
337         aTextBox.Top() += ( aTextBox.GetHeight() - GetTextHeight() ) / 2;
338         aTextBox.Left() += s_nIndentationWidth;
339         aTextBox.Right() -= 1;
340 
341         aTextBox = m_pPaintDevice->GetTextRect( aTextBox, GetText(), impl_getTextStyle() );
342         return aTextBox;
343     }
344 
345     //------------------------------------------------------------------------------------------------------------------
346     Rectangle ToolPanelDrawer::impl_calcTitleBarBox( const Rectangle& i_rTextBox ) const
347     {
348         Rectangle aTitleBarBox( i_rTextBox );
349         aTitleBarBox.Bottom() += aTitleBarBox.Top();
350         aTitleBarBox.Top() = 0;
351         aTitleBarBox.Left() = 0;
352 
353         const long nWidth = GetOutputSizePixel().Width();
354         if ( aTitleBarBox.GetWidth() < nWidth )
355             aTitleBarBox.Right() = nWidth - 1;
356 
357         return aTitleBarBox;
358     }
359 
360     //------------------------------------------------------------------------------------------------------------------
361     void ToolPanelDrawer::SetExpanded( const bool i_bExpanded )
362     {
363         if ( m_bExpanded != i_bExpanded )
364         {
365             m_bExpanded = i_bExpanded;
366             CallEventListeners( m_bExpanded ? VCLEVENT_ITEM_EXPANDED : VCLEVENT_ITEM_COLLAPSED );
367             Invalidate();
368         }
369     }
370 
371 //......................................................................................................................
372 } // namespace svt
373 //......................................................................................................................
374