1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "taskpane/TitledControl.hxx"
23 #include "AccessibleTreeNode.hxx"
24 #include "taskpane/ControlContainer.hxx"
25 #include "TaskPaneFocusManager.hxx"
26 #include "taskpane/TaskPaneControlFactory.hxx"
27 #include <vcl/ctrl.hxx>
28 #include <vcl/svapp.hxx>
31 namespace sd
{ namespace toolpanel
{
34 TitledControl::TitledControl (
36 ::std::auto_ptr
<TreeNode
> pControl
,
38 const ClickHandler
& rClickHandler
,
39 TitleBar::TitleBarType eType
)
40 : ::Window (pParent
->GetWindow(), WB_TABSTOP
),
45 mpClickHandler(new ClickHandler(rClickHandler
))
47 mpControlContainer
->AddControl (::std::auto_ptr
<TreeNode
> (
48 new TitleBar (this, rTitle
, eType
, pControl
->IsExpandable())));
49 pControl
->SetParentNode (this);
50 mpControlContainer
->AddControl (pControl
);
52 FocusManager::Instance().RegisterDownLink( GetTitleBar()->GetWindow(), GetControl()->GetWindow() );
53 FocusManager::Instance().RegisterUpLink( GetControl()->GetWindow(), GetTitleBar()->GetWindow() );
55 SetBackground (Wallpaper());
57 GetTitleBar()->GetWindow()->Show ();
58 GetTitleBar()->GetWindow()->AddEventListener (
59 LINK(this,TitledControl
,WindowEventListener
));
67 TitledControl::~TitledControl (void)
69 GetTitleBar()->GetWindow()->RemoveEventListener (
70 LINK(this,TitledControl
,WindowEventListener
));
76 Size
TitledControl::GetPreferredSize (void)
79 if (GetControl() != NULL
)
81 aPreferredSize
= GetControl()->GetPreferredSize();
83 aPreferredSize
.Height() = 0;
86 aPreferredSize
= Size (GetSizePixel().Width(), 0);
87 if (aPreferredSize
.Width() == 0)
88 aPreferredSize
.Width() = 300;
89 aPreferredSize
.Height() += GetTitleBar()->GetPreferredHeight(
90 aPreferredSize
.Width());
92 return aPreferredSize
;
98 sal_Int32
TitledControl::GetPreferredWidth (sal_Int32 nHeight
)
100 int nPreferredWidth
= 0;
101 if (GetControl() != NULL
)
102 nPreferredWidth
= GetControl()->GetPreferredWidth(
103 nHeight
- GetTitleBar()->GetWindow()->GetSizePixel().Height());
105 nPreferredWidth
= GetSizePixel().Width();
106 if (nPreferredWidth
== 0)
107 nPreferredWidth
= 300;
109 return nPreferredWidth
;
115 sal_Int32
TitledControl::GetPreferredHeight (sal_Int32 nWidth
)
117 int nPreferredHeight
= 0;
118 if (IsExpanded() && GetControl()!=NULL
)
119 nPreferredHeight
= GetControl()->GetPreferredHeight(nWidth
);
120 nPreferredHeight
+= GetTitleBar()->GetPreferredHeight(nWidth
);
122 return nPreferredHeight
;
128 bool TitledControl::IsResizable (void)
131 && GetControl()->IsResizable();
137 ::Window
* TitledControl::GetWindow (void)
145 void TitledControl::Resize (void)
147 Size
aWindowSize (GetOutputSizePixel());
150 = GetTitleBar()->GetPreferredHeight(aWindowSize
.Width());
151 GetTitleBar()->GetWindow()->SetPosSizePixel (
153 Size (aWindowSize
.Width(), nTitleBarHeight
));
156 TreeNode
* pControl
= GetControl();
158 && pControl
->GetWindow() != NULL
159 && pControl
->GetWindow()->IsVisible())
161 pControl
->GetWindow()->SetPosSizePixel (
162 Point (0,nTitleBarHeight
),
163 Size (aWindowSize
.Width(), aWindowSize
.Height()-nTitleBarHeight
));
170 void TitledControl::GetFocus (void)
172 ::Window::GetFocus();
173 if (GetTitleBar() != NULL
)
174 GetTitleBar()->GrabFocus();
180 void TitledControl::KeyInput (const KeyEvent
& rEvent
)
182 KeyCode nCode
= rEvent
.GetKeyCode();
183 if (nCode
== KEY_SPACE
)
185 // Toggle the expansion state of the control (when toggling is
186 // supported.) The focus remains on this control.
187 GetParentNode()->GetControlContainer().SetExpansionState (
189 ControlContainer::ES_TOGGLE
);
191 else if (nCode
== KEY_RETURN
)
193 // Return, also called enter, enters the control and puts the
194 // focus to the first child. If the control is not yet
195 // expanded then do that first.
196 GetParentNode()->GetControlContainer().SetExpansionState (
198 ControlContainer::ES_EXPAND
);
200 if ( ! FocusManager::Instance().TransferFocus(this,nCode
))
202 // When already expanded then put focus on first child.
203 TreeNode
* pControl
= GetControl();
204 if (pControl
!=NULL
&& IsExpanded())
205 if (pControl
->GetWindow() != NULL
)
206 pControl
->GetWindow()->GrabFocus();
209 else if (nCode
== KEY_ESCAPE
)
211 if ( ! FocusManager::Instance().TransferFocus(this,nCode
))
212 // Put focus to parent.
213 GetParent()->GrabFocus();
216 Window::KeyInput (rEvent
);
222 const String
& TitledControl::GetTitle (void) const
230 bool TitledControl::Expand (bool bExpanded
)
232 bool bExpansionStateChanged (false);
234 if (IsExpandable() && IsEnabled())
236 if (GetTitleBar()->IsExpanded() != bExpanded
)
237 bExpansionStateChanged
|= GetTitleBar()->Expand (bExpanded
);
238 // Get the control. Use the bExpanded parameter as argument to
239 // indicate that a control is created via its factory only when it
240 // is to be expanded. When it is collapsed this is not necessary.
241 TreeNode
* pControl
= GetControl();
243 && GetControl()->IsExpanded() != bExpanded
)
245 bExpansionStateChanged
|= pControl
->Expand (bExpanded
);
247 if (bExpansionStateChanged
)
251 return bExpansionStateChanged
;
257 bool TitledControl::IsExpandable (void) const
259 const TreeNode
* pControl
= GetConstControl();
260 if (pControl
!= NULL
)
261 return pControl
->IsExpandable();
263 // When a control factory is given but the control has not yet been
264 // created we assume that the control is expandable.
271 bool TitledControl::IsExpanded (void) const
273 const TreeNode
* pControl
= GetConstControl();
274 if (pControl
!= NULL
)
275 return pControl
->IsExpanded();
280 void TitledControl::SetEnabledState(bool bFlag
)
284 GetParentNode()->GetControlContainer().SetExpansionState (
286 ControlContainer::ES_COLLAPSE
);
294 GetTitleBar()->SetEnabledState(bFlag
);
299 bool TitledControl::IsShowing (void) const
307 void TitledControl::Show (bool bVisible
)
309 if (mbVisible
!= bVisible
)
311 mbVisible
= bVisible
;
319 void TitledControl::UpdateStates (void)
326 TreeNode
* pControl
= GetControl();
327 if (pControl
!=NULL
&& pControl
->GetWindow() != NULL
)
329 if (IsVisible() && IsExpanded())
330 pControl
->GetWindow()->Show();
332 pControl
->GetWindow()->Hide();
339 IMPL_LINK(TitledControl
, WindowEventListener
,
340 VclSimpleEvent
*, pEvent
)
342 if (pEvent
!=NULL
&& pEvent
->ISA(VclWindowEvent
))
344 VclWindowEvent
* pWindowEvent
= static_cast<VclWindowEvent
*>(pEvent
);
345 switch (pWindowEvent
->GetId())
347 case VCLEVENT_WINDOW_MOUSEBUTTONUP
:
349 (*mpClickHandler
)(*this);
359 TreeNode
* TitledControl::GetControl (void)
361 return mpControlContainer
->GetControl(1);
367 const TreeNode
* TitledControl::GetConstControl () const
369 return const_cast<TitledControl
*>(this)->GetControl();
375 TitleBar
* TitledControl::GetTitleBar (void)
377 return static_cast<TitleBar
*>(mpControlContainer
->GetControl(0));
383 ::com::sun::star::uno::Reference
<
384 ::com::sun::star::accessibility::XAccessible
> TitledControl::CreateAccessibleObject (
385 const ::com::sun::star::uno::Reference
<
386 ::com::sun::star::accessibility::XAccessible
>& )
388 return new ::accessibility::AccessibleTreeNode(
392 ::com::sun::star::accessibility::AccessibleRole::LIST_ITEM
);
398 //===== TitledControlStandardClickHandler =====================================
400 TitledControlStandardClickHandler::TitledControlStandardClickHandler (
401 ControlContainer
& rControlContainer
,
402 ControlContainer::ExpansionState eExpansionState
)
403 : mrControlContainer(rControlContainer
),
404 meExpansionState(eExpansionState
)
411 void TitledControlStandardClickHandler::operator () (TitledControl
& rTitledControl
)
414 mrControlContainer
.SetExpansionState (&rTitledControl
, meExpansionState
);
417 } } // end of namespace ::sd::toolpanel
419 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */