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 #include "sfx2/sidebar/ControllerFactory.hxx"
32 #include "sfx2/sidebar/Tools.hxx"
33 #include <tools/svborder.hxx>
34 #include <vcl/gradient.hxx>
35 #include <vcl/image.hxx>
36 #include <toolkit/helper/vclunohelper.hxx>
37 
38 using namespace css;
39 using namespace cssu;
40 
41 namespace sfx2 { namespace sidebar {
42 
43 
44 static const sal_Int32 gaLeftIconPadding (5);
45 static const sal_Int32 gaRightIconPadding (5);
46 
47 
PanelTitleBar(const::rtl::OUString & rsTitle,Window * pParentWindow,Panel * pPanel)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       mxFrame(),
57       msMoreOptionsCommand(),
58       msAccessibleNamePrefix(String(SfxResId(SFX_STR_SIDEBAR_ACCESSIBILITY_PANEL_PREFIX)))
59 {
60     OSL_ASSERT(mpPanel != NULL);
61 
62 #ifdef DEBUG
63     SetText(A2S("PanelTitleBar"));
64 #endif
65 }
66 
67 
68 
69 
~PanelTitleBar(void)70 PanelTitleBar::~PanelTitleBar (void)
71 {
72 }
73 
74 
75 
76 
SetMoreOptionsCommand(const::rtl::OUString & rsCommandName,const::cssu::Reference<css::frame::XFrame> & rxFrame)77 void PanelTitleBar::SetMoreOptionsCommand (
78     const ::rtl::OUString& rsCommandName,
79     const ::cssu::Reference<css::frame::XFrame>& rxFrame)
80 {
81     if ( ! rsCommandName.equals(msMoreOptionsCommand))
82     {
83         if (msMoreOptionsCommand.getLength() > 0)
84             maToolBox.RemoveItem(maToolBox.GetItemPos(mnMenuItemIndex));
85 
86         msMoreOptionsCommand = rsCommandName;
87         mxFrame = rxFrame;
88 
89         if (msMoreOptionsCommand.getLength() > 0)
90         {
91             maToolBox.InsertItem(
92                 mnMenuItemIndex,
93                 Theme::GetImage(Theme::Image_PanelMenu));
94             Reference<frame::XToolbarController> xController (
95                 ControllerFactory::CreateToolBoxController(
96                     &maToolBox,
97                     mnMenuItemIndex,
98                     msMoreOptionsCommand,
99                     rxFrame,
100                     VCLUnoHelper::GetInterface(&maToolBox),
101                     0));
102             maToolBox.SetController(mnMenuItemIndex, xController, msMoreOptionsCommand);
103             maToolBox.SetOutStyle(TOOLBOX_STYLE_FLAT);
104             maToolBox.SetQuickHelpText(
105                 mnMenuItemIndex,
106                 String(SfxResId(SFX_STR_SIDEBAR_MORE_OPTIONS)));
107         }
108     }
109 }
110 
111 
112 
113 
GetTitleArea(const Rectangle & rTitleBarBox)114 Rectangle PanelTitleBar::GetTitleArea (const Rectangle& rTitleBarBox)
115 {
116     if (mpPanel != NULL)
117     {
118         Image aImage (mpPanel->IsExpanded()
119             ? Theme::GetImage(Theme::Image_Expand)
120             : Theme::GetImage(Theme::Image_Collapse));
121         return Rectangle(
122             aImage.GetSizePixel().Width() + gaLeftIconPadding + gaRightIconPadding,
123             rTitleBarBox.Top(),
124             rTitleBarBox.Right(),
125             rTitleBarBox.Bottom());
126     }
127     else
128         return rTitleBarBox;
129 }
130 
131 
132 
133 
PaintDecoration(const Rectangle & rTitleBarBox)134 void PanelTitleBar::PaintDecoration (const Rectangle& rTitleBarBox)
135 {
136     (void)rTitleBarBox;
137 
138     if (mpPanel != NULL)
139     {
140         Image aImage (mpPanel->IsExpanded()
141             ? Theme::GetImage(Theme::Image_Collapse)
142             : Theme::GetImage(Theme::Image_Expand));
143         const Point aTopLeft (
144             gaLeftIconPadding,
145             (GetSizePixel().Height()-aImage.GetSizePixel().Height())/2);
146         DrawImage(aTopLeft, aImage);
147     }
148 }
149 
150 
151 
152 
GetBackgroundPaint(void)153 Paint PanelTitleBar::GetBackgroundPaint (void)
154 {
155     return Theme::GetPaint(Theme::Paint_PanelTitleBarBackground);
156 }
157 
158 
159 
160 
GetTextColor(void)161 Color PanelTitleBar::GetTextColor (void)
162 {
163     return Theme::GetColor(Theme::Color_PanelTitleFont);
164 }
165 
166 
167 
168 
HandleToolBoxItemClick(const sal_uInt16 nItemIndex)169 void PanelTitleBar::HandleToolBoxItemClick (const sal_uInt16 nItemIndex)
170 {
171     if (nItemIndex == mnMenuItemIndex)
172         if (msMoreOptionsCommand.getLength() > 0)
173         {
174             try
175             {
176                 const util::URL aURL (Tools::GetURL(msMoreOptionsCommand));
177                 Reference<frame::XDispatch> xDispatch (Tools::GetDispatch(mxFrame, aURL));
178                 if (xDispatch.is())
179                     xDispatch->dispatch(aURL, Sequence<beans::PropertyValue>());
180             }
181             catch(Exception& rException)
182             {
183                 OSL_TRACE("caught exception: %s",
184                     OUStringToOString(rException.Message, RTL_TEXTENCODING_ASCII_US).getStr());
185             }
186         }
187 }
188 
189 
190 
191 
CreateAccessible(void)192 Reference<accessibility::XAccessible> PanelTitleBar::CreateAccessible (void)
193 {
194     const ::rtl::OUString sAccessibleName(msAccessibleNamePrefix + msTitle);
195     SetAccessibleName(sAccessibleName);
196     SetAccessibleDescription(sAccessibleName);
197     return TitleBar::CreateAccessible();
198 }
199 
200 
201 
202 
MouseButtonDown(const MouseEvent & rMouseEvent)203 void PanelTitleBar::MouseButtonDown (const MouseEvent& rMouseEvent)
204 {
205     if (rMouseEvent.IsLeft())
206     {
207         mbIsLeftButtonDown = true;
208         CaptureMouse();
209     }
210 }
211 
212 
213 
214 
MouseButtonUp(const MouseEvent & rMouseEvent)215 void PanelTitleBar::MouseButtonUp (const MouseEvent& rMouseEvent)
216 {
217     if (IsMouseCaptured())
218         ReleaseMouse();
219 
220     if (rMouseEvent.IsLeft())
221     {
222         if (mbIsLeftButtonDown)
223         {
224             if (mpPanel != NULL)
225             {
226                 mpPanel->SetExpanded( ! mpPanel->IsExpanded());
227                 Invalidate();
228             }
229         }
230     }
231     if (mbIsLeftButtonDown)
232         mbIsLeftButtonDown = false;
233 }
234 
235 
236 
237 
DataChanged(const DataChangedEvent & rEvent)238 void PanelTitleBar::DataChanged (const DataChangedEvent& rEvent)
239 {
240     maToolBox.SetItemImage(
241         mnMenuItemIndex,
242         Theme::GetImage(Theme::Image_PanelMenu));
243     TitleBar::DataChanged(rEvent);
244 }
245 
246 } } // end of namespace sfx2::sidebar
247