update dev300-m58
[ooovba.git] / sd / source / ui / toolpanel / TitledControl.cxx
blobccd85fc72127e430c63a8a978351e4f6f895a233
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
353 GetParentNode()->GetControlContainer().SetExpansionState (
354 this,
355 ControlContainer::ES_EXPAND);
357 Enable();
360 GetTitleBar()->SetEnabledState(bFlag);
365 void TitledControl::SetUserData (void* pUserData)
367 mpUserData = pUserData;
373 void* TitledControl::GetUserData (void) const
375 return mpUserData;
381 bool TitledControl::IsShowing (void) const
383 return mbVisible;
389 void TitledControl::Show (bool bVisible)
391 if (mbVisible != bVisible)
393 mbVisible = bVisible;
394 UpdateStates ();
401 void TitledControl::UpdateStates (void)
403 if (mbVisible)
404 GetWindow()->Show();
405 else
406 GetWindow()->Hide();
408 TreeNode* pControl = GetControl(false);
409 if (pControl!=NULL && pControl->GetWindow() != NULL)
411 if (IsVisible() && IsExpanded())
412 pControl->GetWindow()->Show();
413 else
414 pControl->GetWindow()->Hide();
421 IMPL_LINK(TitledControl, WindowEventListener,
422 VclSimpleEvent*, pEvent)
424 if (pEvent!=NULL && pEvent->ISA(VclWindowEvent))
426 VclWindowEvent* pWindowEvent = static_cast<VclWindowEvent*>(pEvent);
427 switch (pWindowEvent->GetId())
429 case VCLEVENT_WINDOW_MOUSEBUTTONUP:
430 if (IsEnabled())
431 (*mpClickHandler)(*this);
432 break;
435 return 0;
441 TreeNode* TitledControl::GetControl (bool bCreate)
443 TreeNode* pNode = mpControlContainer->GetControl(1);
444 if (pNode==NULL && mpControlFactory.get()!=NULL && bCreate)
446 // We have to create the control with the factory object.
447 ::std::auto_ptr<TreeNode> pControl (mpControlFactory->CreateControl(this));//GetParentNode()));
448 if (pControl.get() != NULL)
450 pControl->SetParentNode(this);
451 mpControlContainer->AddControl(pControl);
453 pNode = mpControlContainer->GetControl(1);
454 FocusManager::Instance().RegisterDownLink(this, pNode->GetWindow());
455 FocusManager::Instance().RegisterUpLink(pNode->GetWindow(), this);
459 return pNode;
465 const TreeNode* TitledControl::GetConstControl (bool bCreate) const
467 return const_cast<TitledControl*>(this)->GetControl(bCreate);
473 TitleBar* TitledControl::GetTitleBar (void)
475 return static_cast<TitleBar*>(mpControlContainer->GetControl(0));
481 ::com::sun::star::uno::Reference<
482 ::com::sun::star::accessibility::XAccessible> TitledControl::CreateAccessibleObject (
483 const ::com::sun::star::uno::Reference<
484 ::com::sun::star::accessibility::XAccessible>& )
486 return new ::accessibility::AccessibleTreeNode(
487 *this,
488 GetTitle(),
489 GetTitle(),
490 ::com::sun::star::accessibility::AccessibleRole::LIST_ITEM);
496 //===== TitledControlStandardClickHandler =====================================
498 TitledControlStandardClickHandler::TitledControlStandardClickHandler (
499 ControlContainer& rControlContainer,
500 ControlContainer::ExpansionState eExpansionState)
501 : mrControlContainer(rControlContainer),
502 meExpansionState(eExpansionState)
509 void TitledControlStandardClickHandler::operator () (TitledControl& rTitledControl)
511 // Toggle expansion.
512 mrControlContainer.SetExpansionState (&rTitledControl, meExpansionState);
515 } } // end of namespace ::sd::toolpanel