update ooo310-m15
[ooovba.git] / sd / source / ui / toolpanel / TitledControl.cxx
blob495a5d3bf7a754ec9b837b2b1b72fa515b532cde
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: TitledControl.cxx,v $
10 * $Revision: 1.14 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sd.hxx"
34 #include "taskpane/TitledControl.hxx"
36 #include "AccessibleTreeNode.hxx"
37 #include "taskpane/ControlContainer.hxx"
38 #include "TaskPaneFocusManager.hxx"
39 #include "taskpane/TaskPaneControlFactory.hxx"
40 #include <vcl/ctrl.hxx>
41 #include <vcl/svapp.hxx>
44 namespace sd { namespace toolpanel {
47 TitledControl::TitledControl (
48 TreeNode* pParent,
49 ::std::auto_ptr<TreeNode> pControl,
50 const String& rTitle,
51 const ClickHandler& rClickHandler,
52 TitleBar::TitleBarType eType)
53 : ::Window (pParent->GetWindow(), WB_TABSTOP),
54 TreeNode(pParent),
55 msTitle(rTitle),
56 mbVisible(true),
57 mpUserData(NULL),
58 mpControlFactory(NULL),
59 mpClickHandler(new ClickHandler(rClickHandler)),
60 mbExpansionModeIsToggle(eType!=TitleBar::TBT_CONTROL_TITLE)
62 if (pControl.get() != NULL)
64 mpControlContainer->AddControl (::std::auto_ptr<TreeNode> (
65 new TitleBar (this, rTitle, eType, pControl->IsExpandable())));
66 pControl->SetParentNode (this);
68 mpControlContainer->AddControl (pControl);
70 FocusManager::Instance().RegisterDownLink(this, GetControl()->GetWindow());
71 FocusManager::Instance().RegisterUpLink(GetControl()->GetWindow(), this);
73 SetBackground (Wallpaper());
75 GetTitleBar()->GetWindow()->Show ();
76 GetTitleBar()->GetWindow()->AddEventListener (
77 LINK(this,TitledControl,WindowEventListener));
79 UpdateStates ();
85 TitledControl::TitledControl (
86 TreeNode* pParent,
87 ::std::auto_ptr<ControlFactory> pControlFactory,
88 const String& rTitle,
89 const ClickHandler& rClickHandler,
90 TitleBar::TitleBarType eType)
91 : ::Window (pParent->GetWindow(), WB_TABSTOP),
92 TreeNode(pParent),
93 msTitle (rTitle),
94 mbVisible (true),
95 mpUserData (NULL),
96 mpControlFactory(pControlFactory),
97 mpClickHandler(new ClickHandler(rClickHandler)),
98 mbExpansionModeIsToggle(eType!=TitleBar::TBT_CONTROL_TITLE)
100 mpControlContainer->AddControl (::std::auto_ptr<TreeNode> (
101 new TitleBar (this, rTitle, eType, true)));
103 // The second control is created on demand, i.e. when GetControl(true)
104 // is called the first time.
106 SetBackground (Wallpaper());
108 GetTitleBar()->GetWindow()->Show ();
109 GetTitleBar()->GetWindow()->AddEventListener (
110 LINK(this,TitledControl,WindowEventListener));
112 UpdateStates ();
118 TitledControl::~TitledControl (void)
120 GetTitleBar()->GetWindow()->RemoveEventListener (
121 LINK(this,TitledControl,WindowEventListener));
127 Size TitledControl::GetPreferredSize (void)
129 Size aPreferredSize;
130 if (GetControl(false) != NULL)
132 aPreferredSize = GetControl()->GetPreferredSize();
133 if ( ! IsExpanded())
134 aPreferredSize.Height() = 0;
136 else
137 aPreferredSize = Size (GetSizePixel().Width(), 0);
138 if (aPreferredSize.Width() == 0)
139 aPreferredSize.Width() = 300;
140 aPreferredSize.Height() += GetTitleBar()->GetPreferredHeight(
141 aPreferredSize.Width());
143 return aPreferredSize;
149 sal_Int32 TitledControl::GetPreferredWidth (sal_Int32 nHeight)
151 int nPreferredWidth = 0;
152 if (GetControl(false) != NULL)
153 nPreferredWidth = GetControl()->GetPreferredWidth(
154 nHeight - GetTitleBar()->GetWindow()->GetSizePixel().Height());
155 else
156 nPreferredWidth = GetSizePixel().Width();
157 if (nPreferredWidth == 0)
158 nPreferredWidth = 300;
160 return nPreferredWidth;
166 sal_Int32 TitledControl::GetPreferredHeight (sal_Int32 nWidth)
168 int nPreferredHeight = 0;
169 if (IsExpanded() && GetControl(false)!=NULL)
170 nPreferredHeight = GetControl()->GetPreferredHeight(nWidth);
171 nPreferredHeight += GetTitleBar()->GetPreferredHeight(nWidth);
173 return nPreferredHeight;
179 bool TitledControl::IsResizable (void)
181 return IsExpanded()
182 && GetControl()->IsResizable();
188 ::Window* TitledControl::GetWindow (void)
190 return this;
196 void TitledControl::Resize (void)
198 Size aWindowSize (GetOutputSizePixel());
200 int nTitleBarHeight
201 = GetTitleBar()->GetPreferredHeight(aWindowSize.Width());
202 GetTitleBar()->GetWindow()->SetPosSizePixel (
203 Point (0,0),
204 Size (aWindowSize.Width(), nTitleBarHeight));
207 TreeNode* pControl = GetControl(false);
208 if (pControl != NULL
209 && pControl->GetWindow() != NULL
210 && pControl->GetWindow()->IsVisible())
212 pControl->GetWindow()->SetPosSizePixel (
213 Point (0,nTitleBarHeight),
214 Size (aWindowSize.Width(), aWindowSize.Height()-nTitleBarHeight));
221 void TitledControl::GetFocus (void)
223 ::Window::GetFocus();
224 if (GetTitleBar() != NULL)
225 GetTitleBar()->SetFocus (true);
231 void TitledControl::LoseFocus (void)
233 ::Window::LoseFocus();
234 if (GetTitleBar() != NULL)
235 GetTitleBar()->SetFocus (false);
241 void TitledControl::KeyInput (const KeyEvent& rEvent)
243 KeyCode nCode = rEvent.GetKeyCode();
244 if (nCode == KEY_SPACE)
246 // Toggle the expansion state of the control (when toggling is
247 // supported.) The focus remains on this control.
248 GetParentNode()->GetControlContainer().SetExpansionState (
249 this,
250 ControlContainer::ES_TOGGLE);
252 else if (nCode == KEY_RETURN)
254 // Return, also called enter, enters the control and puts the
255 // focus to the first child. If the control is not yet
256 // expanded then do that first.
257 GetParentNode()->GetControlContainer().SetExpansionState (
258 this,
259 ControlContainer::ES_EXPAND);
261 if ( ! FocusManager::Instance().TransferFocus(this,nCode))
263 // When already expanded then put focus on first child.
264 TreeNode* pControl = GetControl(false);
265 if (pControl!=NULL && IsExpanded())
266 if (pControl->GetWindow() != NULL)
267 pControl->GetWindow()->GrabFocus();
270 else if (nCode == KEY_ESCAPE)
272 if ( ! FocusManager::Instance().TransferFocus(this,nCode))
273 // Put focus to parent.
274 GetParent()->GrabFocus();
276 else
277 Window::KeyInput (rEvent);
283 const String& TitledControl::GetTitle (void) const
285 return msTitle;
291 bool TitledControl::Expand (bool bExpanded)
293 bool bExpansionStateChanged (false);
295 if (IsExpandable() && IsEnabled())
297 if (GetTitleBar()->IsExpanded() != bExpanded)
298 bExpansionStateChanged |= GetTitleBar()->Expand (bExpanded);
299 // Get the control. Use the bExpanded parameter as argument to
300 // indicate that a control is created via its factory only when it
301 // is to be expanded. When it is collapsed this is not necessary.
302 TreeNode* pControl = GetControl(bExpanded);
303 if (pControl != NULL
304 && GetControl()->IsExpanded() != bExpanded)
306 bExpansionStateChanged |= pControl->Expand (bExpanded);
308 if (bExpansionStateChanged)
309 UpdateStates();
312 return bExpansionStateChanged;
318 bool TitledControl::IsExpandable (void) const
320 const TreeNode* pControl = GetConstControl(false);
321 if (pControl != NULL)
322 return pControl->IsExpandable();
323 else
324 // When a control factory is given but the control has not yet been
325 // created we assume that the control is expandable.
326 return true;
332 bool TitledControl::IsExpanded (void) const
334 const TreeNode* pControl = GetConstControl(false);
335 if (pControl != NULL)
336 return pControl->IsExpanded();
337 else
338 return false;
341 void TitledControl::SetEnabledState(bool bFlag)
343 if (!bFlag)
345 GetParentNode()->GetControlContainer().SetExpansionState (
346 this,
347 ControlContainer::ES_COLLAPSE);
348 Disable();
350 else
352 GetParentNode()->GetControlContainer().SetExpansionState (
353 this,
354 ControlContainer::ES_EXPAND);
355 Enable();
358 GetTitleBar()->SetEnabledState(bFlag);
363 void TitledControl::SetUserData (void* pUserData)
365 mpUserData = pUserData;
371 void* TitledControl::GetUserData (void) const
373 return mpUserData;
379 bool TitledControl::IsShowing (void) const
381 return mbVisible;
387 void TitledControl::Show (bool bVisible)
389 if (mbVisible != bVisible)
391 mbVisible = bVisible;
392 UpdateStates ();
399 void TitledControl::UpdateStates (void)
401 if (mbVisible)
402 GetWindow()->Show();
403 else
404 GetWindow()->Hide();
406 TreeNode* pControl = GetControl(false);
407 if (pControl!=NULL && pControl->GetWindow() != NULL)
409 if (IsVisible() && IsExpanded())
410 pControl->GetWindow()->Show();
411 else
412 pControl->GetWindow()->Hide();
419 IMPL_LINK(TitledControl, WindowEventListener,
420 VclSimpleEvent*, pEvent)
422 if (pEvent!=NULL && pEvent->ISA(VclWindowEvent))
424 VclWindowEvent* pWindowEvent = static_cast<VclWindowEvent*>(pEvent);
425 switch (pWindowEvent->GetId())
427 case VCLEVENT_WINDOW_MOUSEBUTTONUP:
428 if (IsEnabled())
429 (*mpClickHandler)(*this);
430 break;
433 return 0;
439 TreeNode* TitledControl::GetControl (bool bCreate)
441 TreeNode* pNode = mpControlContainer->GetControl(1);
442 if (pNode==NULL && mpControlFactory.get()!=NULL && bCreate)
444 // We have to create the control with the factory object.
445 ::std::auto_ptr<TreeNode> pControl (mpControlFactory->CreateControl(this));//GetParentNode()));
446 if (pControl.get() != NULL)
448 pControl->SetParentNode(this);
449 mpControlContainer->AddControl(pControl);
451 pNode = mpControlContainer->GetControl(1);
452 FocusManager::Instance().RegisterDownLink(this, pNode->GetWindow());
453 FocusManager::Instance().RegisterUpLink(pNode->GetWindow(), this);
457 return pNode;
463 const TreeNode* TitledControl::GetConstControl (bool bCreate) const
465 return const_cast<TitledControl*>(this)->GetControl(bCreate);
471 TitleBar* TitledControl::GetTitleBar (void)
473 return static_cast<TitleBar*>(mpControlContainer->GetControl(0));
479 ::com::sun::star::uno::Reference<
480 ::com::sun::star::accessibility::XAccessible> TitledControl::CreateAccessibleObject (
481 const ::com::sun::star::uno::Reference<
482 ::com::sun::star::accessibility::XAccessible>& )
484 return new ::accessibility::AccessibleTreeNode(
485 *this,
486 GetTitle(),
487 GetTitle(),
488 ::com::sun::star::accessibility::AccessibleRole::LIST_ITEM);
494 //===== TitledControlStandardClickHandler =====================================
496 TitledControlStandardClickHandler::TitledControlStandardClickHandler (
497 ControlContainer& rControlContainer,
498 ControlContainer::ExpansionState eExpansionState)
499 : mrControlContainer(rControlContainer),
500 meExpansionState(eExpansionState)
507 void TitledControlStandardClickHandler::operator () (TitledControl& rTitledControl)
509 // Toggle expansion.
510 mrControlContainer.SetExpansionState (&rTitledControl, meExpansionState);
513 } } // end of namespace ::sd::toolpanel