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