1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #include "precompiled_sfx2.hxx"
23 
24 #include "PanelTitleBar.hxx"
25 #include "sfx2/sfxresid.hxx"
26 #include "Sidebar.hrc"
27 
28 #include "Paint.hxx"
29 #include "Panel.hxx"
30 #include "sfx2/sidebar/Theme.hxx"
31 
32 #include <tools/svborder.hxx>
33 #include <vcl/gradient.hxx>
34 #include <vcl/image.hxx>
35 
36 #ifdef DEBUG
37 #include "sfx2/sidebar/Tools.hxx"
38 #endif
39 
40 
41 namespace sfx2 { namespace sidebar {
42 
43 
44 static const sal_Int32 gaLeftIconPadding (5);
45 static const sal_Int32 gaRightIconPadding (5);
46 
47 
48 PanelTitleBar::PanelTitleBar (
49     const ::rtl::OUString& rsTitle,
50     Window* pParentWindow,
51     Panel* pPanel )
52     : TitleBar(rsTitle, pParentWindow, GetBackgroundPaint()),
53       mbIsLeftButtonDown(false),
54       mpPanel(pPanel),
55       mnMenuItemIndex(1),
56       maMenuAction()
57 {
58     OSL_ASSERT(mpPanel != NULL);
59 
60 #ifdef DEBUG
61     SetText(A2S("PanelTitleBar"));
62 #endif
63 }
64 
65 
66 
67 
68 PanelTitleBar::~PanelTitleBar (void)
69 {
70 }
71 
72 
73 
74 
75 void PanelTitleBar::SetMenuAction ( const ::boost::function<void(void)>& rMenuAction )
76 {
77     if ( !maMenuAction && rMenuAction )
78     {
79         maToolBox.InsertItem(
80             mnMenuItemIndex,
81             Theme::GetImage(Theme::Image_PanelMenu));
82         maToolBox.SetOutStyle(TOOLBOX_STYLE_FLAT);
83         maToolBox.SetQuickHelpText(
84             mnMenuItemIndex,
85             String(SfxResId(SFX_STR_SIDEBAR_MORE_OPTIONS)));
86     }
87     else if ( maMenuAction && !rMenuAction )
88     {
89         maToolBox.RemoveItem( maToolBox.GetItemPos( mnMenuItemIndex ) );
90     }
91     maMenuAction = rMenuAction;
92 }
93 
94 
95 
96 
97 Rectangle PanelTitleBar::GetTitleArea (const Rectangle& rTitleBarBox)
98 {
99     if (mpPanel != NULL)
100     {
101         Image aImage (mpPanel->IsExpanded()
102             ? Theme::GetImage(Theme::Image_Expand)
103             : Theme::GetImage(Theme::Image_Collapse));
104         return Rectangle(
105             aImage.GetSizePixel().Width() + gaLeftIconPadding + gaRightIconPadding,
106             rTitleBarBox.Top(),
107             rTitleBarBox.Right(),
108             rTitleBarBox.Bottom());
109     }
110     else
111         return rTitleBarBox;
112 }
113 
114 
115 
116 
117 void PanelTitleBar::PaintDecoration (const Rectangle& rTitleBarBox)
118 {
119     (void)rTitleBarBox;
120 
121     if (mpPanel != NULL)
122     {
123         Image aImage (mpPanel->IsExpanded()
124             ? Theme::GetImage(Theme::Image_Collapse)
125             : Theme::GetImage(Theme::Image_Expand));
126         const Point aTopLeft (
127             gaLeftIconPadding,
128             (GetSizePixel().Height()-aImage.GetSizePixel().Height())/2);
129         DrawImage(aTopLeft, aImage);
130     }
131 }
132 
133 
134 
135 
136 Paint PanelTitleBar::GetBackgroundPaint (void)
137 {
138     return Theme::GetPaint(Theme::Paint_PanelTitleBarBackground);
139 }
140 
141 
142 
143 
144 Color PanelTitleBar::GetTextColor (void)
145 {
146     return Theme::GetColor(Theme::Color_PanelTitleFont);
147 }
148 
149 
150 
151 
152 void PanelTitleBar::HandleToolBoxItemClick (const sal_uInt16 nItemIndex)
153 {
154     if (nItemIndex == mnMenuItemIndex)
155         if (maMenuAction)
156             maMenuAction();
157 }
158 
159 
160 
161 
162 void PanelTitleBar::MouseButtonDown (const MouseEvent& rMouseEvent)
163 {
164     if (rMouseEvent.IsLeft())
165     {
166         mbIsLeftButtonDown = true;
167         CaptureMouse();
168     }
169 }
170 
171 
172 
173 
174 void PanelTitleBar::MouseButtonUp (const MouseEvent& rMouseEvent)
175 {
176     if (IsMouseCaptured())
177         ReleaseMouse();
178 
179     if (rMouseEvent.IsLeft())
180     {
181         if (mbIsLeftButtonDown)
182         {
183             if (mpPanel != NULL)
184             {
185                 mpPanel->SetExpanded( ! mpPanel->IsExpanded());
186                 Invalidate();
187             }
188         }
189     }
190     if (mbIsLeftButtonDown)
191         mbIsLeftButtonDown = false;
192 }
193 
194 
195 
196 
197 void PanelTitleBar::DataChanged (const DataChangedEvent& rEvent)
198 {
199     maToolBox.SetItemImage(
200         mnMenuItemIndex,
201         Theme::GetImage(Theme::Image_PanelMenu));
202     TitleBar::DataChanged(rEvent);
203 }
204 
205 } } // end of namespace sfx2::sidebar
206