xref: /aoo42x/main/sfx2/source/sidebar/TabBar.cxx (revision b9e67834)
122de8995SAndre Fischer /**************************************************************
222de8995SAndre Fischer  *
322de8995SAndre Fischer  * Licensed to the Apache Software Foundation (ASF) under one
422de8995SAndre Fischer  * or more contributor license agreements.  See the NOTICE file
522de8995SAndre Fischer  * distributed with this work for additional information
622de8995SAndre Fischer  * regarding copyright ownership.  The ASF licenses this file
722de8995SAndre Fischer  * to you under the Apache License, Version 2.0 (the
822de8995SAndre Fischer  * "License"); you may not use this file except in compliance
922de8995SAndre Fischer  * with the License.  You may obtain a copy of the License at
1022de8995SAndre Fischer  *
1122de8995SAndre Fischer  *   http://www.apache.org/licenses/LICENSE-2.0
1222de8995SAndre Fischer  *
1322de8995SAndre Fischer  * Unless required by applicable law or agreed to in writing,
1422de8995SAndre Fischer  * software distributed under the License is distributed on an
1522de8995SAndre Fischer  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1622de8995SAndre Fischer  * KIND, either express or implied.  See the License for the
1722de8995SAndre Fischer  * specific language governing permissions and limitations
1822de8995SAndre Fischer  * under the License.
1922de8995SAndre Fischer  *
2022de8995SAndre Fischer  *************************************************************/
2122de8995SAndre Fischer 
2222de8995SAndre Fischer #include "precompiled_sfx2.hxx"
2322de8995SAndre Fischer 
2422de8995SAndre Fischer #include "TabBar.hxx"
25ff12d537SAndre Fischer #include "TabItem.hxx"
26*b9e67834SAndre Fischer #include "sfx2/sidebar/ControlFactory.hxx"
27ff12d537SAndre Fischer #include "DeckDescriptor.hxx"
28ff12d537SAndre Fischer #include "Paint.hxx"
29*b9e67834SAndre Fischer #include "sfx2/sidebar/Theme.hxx"
30*b9e67834SAndre Fischer #include "Tools.hxx"
31ff12d537SAndre Fischer 
3222de8995SAndre Fischer #include <vcl/gradient.hxx>
33ff12d537SAndre Fischer #include <vcl/image.hxx>
34ff12d537SAndre Fischer #include <comphelper/processfactory.hxx>
35ff12d537SAndre Fischer #include <comphelper/componentcontext.hxx>
36ff12d537SAndre Fischer #include <tools/SvBorder.hxx>
37ff12d537SAndre Fischer 
38ff12d537SAndre Fischer #include <com/sun/star/graphic/XGraphicProvider.hpp>
39ff12d537SAndre Fischer 
40ff12d537SAndre Fischer 
41ff12d537SAndre Fischer using namespace ::com::sun::star;
42ff12d537SAndre Fischer using namespace ::com::sun::star::uno;
43ff12d537SAndre Fischer 
44ff12d537SAndre Fischer 
45ff12d537SAndre Fischer 
4622de8995SAndre Fischer 
47ff12d537SAndre Fischer namespace sfx2 { namespace sidebar {
4822de8995SAndre Fischer 
49ff12d537SAndre Fischer TabBar::TabBar (
50ff12d537SAndre Fischer     Window* pParentWindow,
51ff12d537SAndre Fischer     const Reference<frame::XFrame>& rxFrame,
52ff12d537SAndre Fischer     const ::boost::function<void(const DeckDescriptor&)>& rDeckActivationFunctor,
53ff12d537SAndre Fischer     const ::boost::function<void(const Rectangle&)>& rPopupMenuProvider)
54ff12d537SAndre Fischer     : Window(pParentWindow),
55ff12d537SAndre Fischer       mxFrame(rxFrame),
56ff12d537SAndre Fischer       mpMenuButton(ControlFactory::CreateMenuButton(this)),
57ff12d537SAndre Fischer       maItems(),
58ff12d537SAndre Fischer       maDeckActivationFunctor(rDeckActivationFunctor),
59ff12d537SAndre Fischer       maPopupMenuProvider(rPopupMenuProvider)
6022de8995SAndre Fischer {
61*b9e67834SAndre Fischer     SetBackground(Theme::GetPaint(Theme::Paint_TabBarBackground).GetWallpaper());
62ff12d537SAndre Fischer 
63ff12d537SAndre Fischer     mpMenuButton->SetHelpText(A2S("This is the menu button"));
64ff12d537SAndre Fischer     mpMenuButton->SetQuickHelpText(A2S("This is the menu button"));
65ff12d537SAndre Fischer     mpMenuButton->SetModeImage(
66*b9e67834SAndre Fischer         Theme::GetImage(Theme::Image_Menu),
67ff12d537SAndre Fischer         Theme::IsHighContrastMode()
68ff12d537SAndre Fischer             ? BMP_COLOR_HIGHCONTRAST
69ff12d537SAndre Fischer             : BMP_COLOR_NORMAL);
70ff12d537SAndre Fischer     mpMenuButton->SetClickHdl(LINK(this, TabBar, OnToolboxClicked));
71*b9e67834SAndre Fischer 
72*b9e67834SAndre Fischer     EnableClipSiblings();
7322de8995SAndre Fischer }
7422de8995SAndre Fischer 
7522de8995SAndre Fischer 
7622de8995SAndre Fischer 
7722de8995SAndre Fischer 
7822de8995SAndre Fischer TabBar::~TabBar (void)
7922de8995SAndre Fischer {
8022de8995SAndre Fischer }
8122de8995SAndre Fischer 
8222de8995SAndre Fischer 
8322de8995SAndre Fischer 
8422de8995SAndre Fischer 
8522de8995SAndre Fischer void TabBar::Paint (const Rectangle& rUpdateArea)
8622de8995SAndre Fischer {
87ff12d537SAndre Fischer     Window::Paint(rUpdateArea);
88ff12d537SAndre Fischer 
89ff12d537SAndre Fischer     const sal_Int32 nVerticalPadding (Theme::GetTabMenuPadding());
90*b9e67834SAndre Fischer     const sal_Int32 nHorizontalPadding (Theme::GetInteger(Theme::Int_TabMenuSeparatorPadding));
91*b9e67834SAndre Fischer     SetLineColor(Theme::GetColor(Theme::Color_TabMenuSeparator));
92ff12d537SAndre Fischer     DrawLine(
93ff12d537SAndre Fischer         Point(nHorizontalPadding, mnMenuSeparatorY),
94ff12d537SAndre Fischer         Point(GetSizePixel().Width()-nHorizontalPadding, mnMenuSeparatorY));
9522de8995SAndre Fischer }
9622de8995SAndre Fischer 
9722de8995SAndre Fischer 
9822de8995SAndre Fischer 
9922de8995SAndre Fischer 
10022de8995SAndre Fischer sal_Int32 TabBar::GetDefaultWidth (void)
10122de8995SAndre Fischer {
102*b9e67834SAndre Fischer     return Theme::GetInteger(Theme::Int_TabItemWidth)
103*b9e67834SAndre Fischer         + Theme::GetInteger(Theme::Int_TabBarLeftPadding)
104*b9e67834SAndre Fischer         + Theme::GetInteger(Theme::Int_TabBarRightPadding);
105ff12d537SAndre Fischer }
106ff12d537SAndre Fischer 
107ff12d537SAndre Fischer 
108ff12d537SAndre Fischer 
109ff12d537SAndre Fischer 
110ff12d537SAndre Fischer void TabBar::SetDecks (
111ff12d537SAndre Fischer     const ResourceManager::DeckContainer& rDeckDescriptors)
112ff12d537SAndre Fischer {
113ff12d537SAndre Fischer     // Remove the current buttons.
114ff12d537SAndre Fischer     {
115ff12d537SAndre Fischer         Window aTemporaryParent (NULL,0);
116ff12d537SAndre Fischer         for(ItemContainer::const_iterator
117ff12d537SAndre Fischer                 iItem(maItems.begin()), iEnd(maItems.end());
118ff12d537SAndre Fischer             iItem!=iEnd;
119ff12d537SAndre Fischer             ++iItem)
120ff12d537SAndre Fischer         {
121ff12d537SAndre Fischer             removeWindow(iItem->mpButton, &aTemporaryParent);
122ff12d537SAndre Fischer         }
123ff12d537SAndre Fischer         maItems.clear();
124ff12d537SAndre Fischer     }
125ff12d537SAndre Fischer 
126ff12d537SAndre Fischer     maItems.resize(rDeckDescriptors.size());
127ff12d537SAndre Fischer     sal_Int32 nIndex (0);
128ff12d537SAndre Fischer     for (ResourceManager::DeckContainer::const_iterator
129ff12d537SAndre Fischer              iDeck(rDeckDescriptors.begin()),
130ff12d537SAndre Fischer              iEnd(rDeckDescriptors.end());
131ff12d537SAndre Fischer          iDeck!=iEnd;
132ff12d537SAndre Fischer          ++iDeck)
133ff12d537SAndre Fischer     {
134ff12d537SAndre Fischer         Item& rItem (maItems[nIndex++]);
135ff12d537SAndre Fischer         rItem.maDeckDescriptor = *iDeck;
136ff12d537SAndre Fischer         rItem.mpButton = CreateTabItem(*iDeck);
137ff12d537SAndre Fischer         rItem.mpButton->SetClickHdl(LINK(&rItem, TabBar::Item, HandleClick));
138ff12d537SAndre Fischer         rItem.maDeckActivationFunctor = maDeckActivationFunctor;
139ff12d537SAndre Fischer         rItem.mbIsHiddenByDefault = false;
140ff12d537SAndre Fischer         rItem.mbIsHidden = rItem.mbIsHiddenByDefault;
141ff12d537SAndre Fischer     }
142ff12d537SAndre Fischer 
143ff12d537SAndre Fischer     UpdateButtonIcons();
144ff12d537SAndre Fischer     Layout();
145ff12d537SAndre Fischer }
146ff12d537SAndre Fischer 
147ff12d537SAndre Fischer 
148ff12d537SAndre Fischer 
149ff12d537SAndre Fischer 
150ff12d537SAndre Fischer void TabBar::UpdateButtonIcons (void)
151ff12d537SAndre Fischer {
152ff12d537SAndre Fischer     const BmpColorMode eColorMode (
153ff12d537SAndre Fischer         Theme::IsHighContrastMode()
154ff12d537SAndre Fischer             ? BMP_COLOR_HIGHCONTRAST
155ff12d537SAndre Fischer             : BMP_COLOR_NORMAL);
156ff12d537SAndre Fischer 
157*b9e67834SAndre Fischer     mpMenuButton->SetModeImage(Theme::GetImage(Theme::Image_Menu), eColorMode);
158ff12d537SAndre Fischer 
159ff12d537SAndre Fischer     for(ItemContainer::const_iterator
160ff12d537SAndre Fischer             iItem(maItems.begin()), iEnd(maItems.end());
161ff12d537SAndre Fischer         iItem!=iEnd;
162ff12d537SAndre Fischer         ++iItem)
163ff12d537SAndre Fischer     {
164ff12d537SAndre Fischer         iItem->mpButton->SetModeImage(
165ff12d537SAndre Fischer             GetItemImage(iItem->maDeckDescriptor),
166ff12d537SAndre Fischer             eColorMode);
167ff12d537SAndre Fischer     }
168ff12d537SAndre Fischer }
169ff12d537SAndre Fischer 
170ff12d537SAndre Fischer 
171ff12d537SAndre Fischer 
172ff12d537SAndre Fischer 
173ff12d537SAndre Fischer void TabBar::Layout (void)
174ff12d537SAndre Fischer {
175*b9e67834SAndre Fischer     const SvBorder aPadding (
176*b9e67834SAndre Fischer         Theme::GetInteger(Theme::Int_TabBarLeftPadding),
177*b9e67834SAndre Fischer         Theme::GetInteger(Theme::Int_TabBarTopPadding),
178*b9e67834SAndre Fischer         Theme::GetInteger(Theme::Int_TabBarRightPadding),
179*b9e67834SAndre Fischer         Theme::GetInteger(Theme::Int_TabBarBottomPadding));
180ff12d537SAndre Fischer     sal_Int32 nX (aPadding.Top());
181ff12d537SAndre Fischer     sal_Int32 nY (aPadding.Left());
182*b9e67834SAndre Fischer     const Size aTabItemSize (
183*b9e67834SAndre Fischer         Theme::GetInteger(Theme::Int_TabItemWidth),
184*b9e67834SAndre Fischer         Theme::GetInteger(Theme::Int_TabItemHeight));
185ff12d537SAndre Fischer 
186ff12d537SAndre Fischer     if (mpMenuButton != NULL)
187ff12d537SAndre Fischer     {
188ff12d537SAndre Fischer         mpMenuButton->SetPosSizePixel(
189ff12d537SAndre Fischer             Point(nX,nY),
190*b9e67834SAndre Fischer             aTabItemSize);
191ff12d537SAndre Fischer         mpMenuButton->Show();
192*b9e67834SAndre Fischer         nY += mpMenuButton->GetSizePixel().Height() + 1 + Theme::GetInteger(Theme::Int_TabMenuPadding);
193*b9e67834SAndre Fischer         mnMenuSeparatorY = nY - Theme::GetInteger(Theme::Int_TabMenuPadding)/2 - 1;
194ff12d537SAndre Fischer     }
195ff12d537SAndre Fischer 
196ff12d537SAndre Fischer     for(ItemContainer::const_iterator
197ff12d537SAndre Fischer             iItem(maItems.begin()), iEnd(maItems.end());
198ff12d537SAndre Fischer         iItem!=iEnd;
199ff12d537SAndre Fischer         ++iItem)
200ff12d537SAndre Fischer     {
201ff12d537SAndre Fischer         if (iItem->mbIsHidden)
202ff12d537SAndre Fischer             continue;
203ff12d537SAndre Fischer 
204ff12d537SAndre Fischer         Button& rButton (*iItem->mpButton);
205ff12d537SAndre Fischer 
206ff12d537SAndre Fischer         // Place and size the icon.
207ff12d537SAndre Fischer         rButton.SetPosSizePixel(
208ff12d537SAndre Fischer             Point(nX,nY),
209*b9e67834SAndre Fischer             aTabItemSize);
210ff12d537SAndre Fischer         rButton.Show();
211ff12d537SAndre Fischer 
212ff12d537SAndre Fischer         nY += rButton.GetSizePixel().Height() + 1 + aPadding.Bottom();
213ff12d537SAndre Fischer     }
214ff12d537SAndre Fischer     Invalidate();
215ff12d537SAndre Fischer }
216ff12d537SAndre Fischer 
217ff12d537SAndre Fischer 
218ff12d537SAndre Fischer 
219ff12d537SAndre Fischer 
220ff12d537SAndre Fischer void TabBar::HighlightDeck (const ::rtl::OUString& rsDeckId)
221ff12d537SAndre Fischer {
222ff12d537SAndre Fischer     for (ItemContainer::const_iterator iItem(maItems.begin()),iEnd(maItems.end());
223ff12d537SAndre Fischer          iItem!=iEnd;
224ff12d537SAndre Fischer          ++iItem)
225ff12d537SAndre Fischer     {
226ff12d537SAndre Fischer         if (iItem->maDeckDescriptor.msId.equals(rsDeckId))
227ff12d537SAndre Fischer         {
228ff12d537SAndre Fischer             iItem->mpButton->Check();
229ff12d537SAndre Fischer             break;
230ff12d537SAndre Fischer         }
231ff12d537SAndre Fischer     }
232ff12d537SAndre Fischer }
233ff12d537SAndre Fischer 
234ff12d537SAndre Fischer 
235ff12d537SAndre Fischer 
236ff12d537SAndre Fischer 
237ff12d537SAndre Fischer void TabBar::DataChanged (const DataChangedEvent& rDataChangedEvent)
238ff12d537SAndre Fischer {
239ff12d537SAndre Fischer     if  (rDataChangedEvent.GetType() == DATACHANGED_SETTINGS
240ff12d537SAndre Fischer         &&  (rDataChangedEvent.GetFlags() & SETTINGS_STYLE)!= 0)
241ff12d537SAndre Fischer     {
242ff12d537SAndre Fischer         UpdateButtonIcons();
243ff12d537SAndre Fischer         Invalidate();
244ff12d537SAndre Fischer     }
245ff12d537SAndre Fischer     else
246ff12d537SAndre Fischer         Window::DataChanged(rDataChangedEvent);
247ff12d537SAndre Fischer }
248ff12d537SAndre Fischer 
249ff12d537SAndre Fischer 
250ff12d537SAndre Fischer 
251ff12d537SAndre Fischer 
252ff12d537SAndre Fischer RadioButton* TabBar::CreateTabItem (const DeckDescriptor& rDeckDescriptor)
253ff12d537SAndre Fischer {
254ff12d537SAndre Fischer     RadioButton* pItem = ControlFactory::CreateTabItem(this);
255ff12d537SAndre Fischer     pItem->SetHelpText(rDeckDescriptor.msHelpText);
256ff12d537SAndre Fischer     pItem->SetQuickHelpText(rDeckDescriptor.msHelpText);
257ff12d537SAndre Fischer 
258ff12d537SAndre Fischer     return pItem;
25922de8995SAndre Fischer }
26022de8995SAndre Fischer 
26122de8995SAndre Fischer 
262ff12d537SAndre Fischer 
263ff12d537SAndre Fischer 
264ff12d537SAndre Fischer Image TabBar::GetItemImage (const DeckDescriptor& rDeckDescriptor) const
265ff12d537SAndre Fischer {
266*b9e67834SAndre Fischer     return Tools::GetImage(
267*b9e67834SAndre Fischer         rDeckDescriptor.msIconURL,
268*b9e67834SAndre Fischer         rDeckDescriptor.msHighContrastIconURL,
269*b9e67834SAndre Fischer         mxFrame);
270ff12d537SAndre Fischer }
271ff12d537SAndre Fischer 
272ff12d537SAndre Fischer 
273ff12d537SAndre Fischer 
274ff12d537SAndre Fischer 
275ff12d537SAndre Fischer IMPL_LINK(TabBar::Item, HandleClick, Button*, pButton)
276ff12d537SAndre Fischer {
277ff12d537SAndre Fischer     maDeckActivationFunctor(maDeckDescriptor);
278ff12d537SAndre Fischer     return 1;
279ff12d537SAndre Fischer }
280ff12d537SAndre Fischer 
281ff12d537SAndre Fischer 
282ff12d537SAndre Fischer 
283ff12d537SAndre Fischer 
284ff12d537SAndre Fischer void TabBar::AddPopupMenuEntries (
285ff12d537SAndre Fischer     PopupMenu& rMenu,
286ff12d537SAndre Fischer     const sal_Int32 nFirstIndex)
287ff12d537SAndre Fischer {
288ff12d537SAndre Fischer     sal_Int32 nIndex (nFirstIndex);
289ff12d537SAndre Fischer     for(ItemContainer::const_iterator iItem(maItems.begin()),iEnd(maItems.end());
290ff12d537SAndre Fischer         iItem!=iEnd;
291ff12d537SAndre Fischer         ++iItem)
292ff12d537SAndre Fischer     {
293ff12d537SAndre Fischer         rMenu.InsertItem(nIndex, iItem->maDeckDescriptor.msTitle, MIB_RADIOCHECK);
294ff12d537SAndre Fischer         rMenu.CheckItem(nIndex, iItem->mpButton->IsChecked());
295ff12d537SAndre Fischer         ++nIndex;
296ff12d537SAndre Fischer     }
297ff12d537SAndre Fischer }
298ff12d537SAndre Fischer 
299ff12d537SAndre Fischer 
300ff12d537SAndre Fischer 
301ff12d537SAndre Fischer 
302ff12d537SAndre Fischer void TabBar::AddCustomizationMenuEntries (
303ff12d537SAndre Fischer     PopupMenu& rMenu,
304ff12d537SAndre Fischer     const sal_Int32 nFirstIndex)
305ff12d537SAndre Fischer {
306ff12d537SAndre Fischer     sal_Int32 nIndex (nFirstIndex);
307ff12d537SAndre Fischer     for(ItemContainer::const_iterator iItem(maItems.begin()),iEnd(maItems.end());
308ff12d537SAndre Fischer         iItem!=iEnd;
309ff12d537SAndre Fischer         ++iItem)
310ff12d537SAndre Fischer     {
311ff12d537SAndre Fischer         rMenu.InsertItem(nIndex, iItem->maDeckDescriptor.msTitle, MIB_CHECKABLE);
312ff12d537SAndre Fischer         rMenu.CheckItem(nIndex, !iItem->mbIsHidden);
313ff12d537SAndre Fischer         ++nIndex;
314ff12d537SAndre Fischer     }
315ff12d537SAndre Fischer }
316ff12d537SAndre Fischer 
317ff12d537SAndre Fischer 
318ff12d537SAndre Fischer 
319ff12d537SAndre Fischer 
320ff12d537SAndre Fischer const DeckDescriptor& TabBar::GetDeckDescriptorForIndex (const sal_Int32 nIndex) const
321ff12d537SAndre Fischer {
322ff12d537SAndre Fischer     if (nIndex<0 || nIndex>=maItems.size())
323ff12d537SAndre Fischer         throw RuntimeException();
324ff12d537SAndre Fischer     else
325ff12d537SAndre Fischer         return maItems[nIndex].maDeckDescriptor;
326ff12d537SAndre Fischer }
327ff12d537SAndre Fischer 
328ff12d537SAndre Fischer 
329ff12d537SAndre Fischer 
330ff12d537SAndre Fischer 
331ff12d537SAndre Fischer void TabBar::ToggleHideFlag (const sal_Int32 nIndex)
332ff12d537SAndre Fischer {
333ff12d537SAndre Fischer     if (nIndex<0 || nIndex>=maItems.size())
334ff12d537SAndre Fischer         throw RuntimeException();
335ff12d537SAndre Fischer     else
336ff12d537SAndre Fischer     {
337ff12d537SAndre Fischer         maItems[nIndex].mbIsHidden = ! maItems[nIndex].mbIsHidden;
338ff12d537SAndre Fischer         Layout();
339ff12d537SAndre Fischer     }
340ff12d537SAndre Fischer }
341ff12d537SAndre Fischer 
342ff12d537SAndre Fischer 
343ff12d537SAndre Fischer 
344ff12d537SAndre Fischer 
345ff12d537SAndre Fischer void TabBar::RestoreHideFlags (void)
346ff12d537SAndre Fischer {
347ff12d537SAndre Fischer     bool bNeedsLayout (false);
348ff12d537SAndre Fischer     for(ItemContainer::iterator iItem(maItems.begin()),iEnd(maItems.end());
349ff12d537SAndre Fischer         iItem!=iEnd;
350ff12d537SAndre Fischer         ++iItem)
351ff12d537SAndre Fischer     {
352ff12d537SAndre Fischer         if (iItem->mbIsHidden != iItem->mbIsHiddenByDefault)
353ff12d537SAndre Fischer         {
354ff12d537SAndre Fischer             iItem->mbIsHidden = iItem->mbIsHiddenByDefault;
355ff12d537SAndre Fischer             bNeedsLayout = true;
356ff12d537SAndre Fischer         }
357ff12d537SAndre Fischer     }
358ff12d537SAndre Fischer     if (bNeedsLayout)
359ff12d537SAndre Fischer         Layout();
360ff12d537SAndre Fischer }
361ff12d537SAndre Fischer 
362ff12d537SAndre Fischer 
363ff12d537SAndre Fischer 
364ff12d537SAndre Fischer 
365ff12d537SAndre Fischer IMPL_LINK(TabBar, OnToolboxClicked, void*, pToolBox)
366ff12d537SAndre Fischer {
367ff12d537SAndre Fischer     //    mpMenuButton->EndSelection();
368ff12d537SAndre Fischer     maPopupMenuProvider(
369ff12d537SAndre Fischer         Rectangle(
370ff12d537SAndre Fischer             mpMenuButton->GetPosPixel(),
371ff12d537SAndre Fischer             mpMenuButton->GetSizePixel()));
372ff12d537SAndre Fischer 
373ff12d537SAndre Fischer     return 0;
374ff12d537SAndre Fischer }
375ff12d537SAndre Fischer 
376ff12d537SAndre Fischer 
377ff12d537SAndre Fischer 
378ff12d537SAndre Fischer } } // end of namespace sfx2::sidebar
379