1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <layout.hxx>
21 
22 #include <bastypes.hxx>
23 #include <vcl/settings.hxx>
24 #include <vcl/event.hxx>
25 
26 namespace basctl
27 {
28 
29 namespace
30 {
31 // the thickness of the splitting lines
32 static long const nSplitThickness = 3;
33 } // namespace
34 
35 // ctor for derived classes
36 // pParent: the parent window (Shell)
Layout(vcl::Window * pParent)37 Layout::Layout (vcl::Window* pParent) :
38     Window(pParent, WB_CLIPCHILDREN),
39     pChild(nullptr),
40     bFirstSize(true),
41     aLeftSide(this, SplittedSide::Side::Left),
42     aBottomSide(this, SplittedSide::Side::Bottom)
43 {
44     SetBackground(GetSettings().GetStyleSettings().GetWindowColor());
45 
46     vcl::Font aFont = GetFont();
47     Size aSz = aFont.GetFontSize();
48     aSz.setHeight( aSz.Height() * 1.5 );
49     aFont.SetFontSize(aSz);
50     aFont.SetWeight(WEIGHT_BOLD);
51     aFont.SetColor(GetSettings().GetStyleSettings().GetWindowTextColor());
52     SetFont(aFont);
53 }
54 
~Layout()55 Layout::~Layout()
56 {
57     disposeOnce();
58 }
59 
dispose()60 void Layout::dispose()
61 {
62     aLeftSide.dispose();
63     aBottomSide.dispose();
64     pChild.clear();
65     Window::dispose();
66 }
67 
68 // removes a docking window
Remove(DockingWindow * pWin)69 void Layout::Remove (DockingWindow* pWin)
70 {
71     aLeftSide.Remove(pWin);
72     aBottomSide.Remove(pWin);
73 }
74 
75 // called by Window when resized
Resize()76 void Layout::Resize()
77 {
78     if (IsVisible())
79         ArrangeWindows();
80 }
81 
82 // ArrangeWindows() -- arranges the child windows
ArrangeWindows()83 void Layout::ArrangeWindows ()
84 {
85     // prevent recursion via OnFirstSize() -> Add() -> ArrangeWindows()
86     static bool bInArrangeWindows = false;
87     if (bInArrangeWindows)
88         return;
89     bInArrangeWindows = true;
90 
91     Size const aSize = GetOutputSizePixel();
92     long const nWidth = aSize.Width(), nHeight = aSize.Height();
93     if (nWidth && nHeight) // non-empty size
94     {
95         // On first call the derived classes initializes the sizes of the
96         // docking windows. This cannot be done at construction because
97         // the Layout has empty size at that point.
98         if (bFirstSize)
99         {
100             bFirstSize = false;
101             OnFirstSize(nWidth, nHeight); // virtual
102         }
103 
104         // sides
105         aBottomSide.ArrangeIn(tools::Rectangle(Point(0, 0), aSize));
106         aLeftSide.ArrangeIn(tools::Rectangle(Point(0, 0), Size(nWidth, nHeight - aBottomSide.GetSize())));
107         // child in the middle
108         pChild->SetPosSizePixel(
109             Point(aLeftSide.GetSize(), 0),
110             Size(nWidth - aLeftSide.GetSize(), nHeight - aBottomSide.GetSize())
111         );
112     }
113 
114     bInArrangeWindows = false;
115 }
116 
Activating(BaseWindow & rWindow)117 void Layout::Activating (BaseWindow& rWindow)
118 {
119     // first activation
120     pChild = &rWindow;
121     ArrangeWindows();
122     Show();
123     pChild->Activating();
124 }
125 
Deactivating()126 void Layout::Deactivating ()
127 {
128     if (pChild)
129         pChild->Deactivating();
130     Hide();
131     pChild = nullptr;
132 }
133 
134 // virtual
DataChanged(DataChangedEvent const & rDCEvt)135 void Layout::DataChanged (DataChangedEvent const& rDCEvt)
136 {
137     Window::DataChanged(rDCEvt);
138     if (rDCEvt.GetType() == DataChangedEventType::SETTINGS && (rDCEvt.GetFlags() & AllSettingsFlags::STYLE))
139     {
140         bool bInvalidate = false;
141         Color aColor = GetSettings().GetStyleSettings().GetWindowColor();
142         const AllSettings* pOldSettings = rDCEvt.GetOldSettings();
143         if (!pOldSettings || aColor != pOldSettings->GetStyleSettings().GetWindowColor())
144         {
145             SetBackground(Wallpaper(aColor));
146             bInvalidate = true;
147         }
148         aColor = GetSettings().GetStyleSettings().GetWindowTextColor();
149         if (!pOldSettings || aColor != pOldSettings->GetStyleSettings().GetWindowTextColor())
150         {
151             vcl::Font aFont(GetFont());
152             aFont.SetColor(aColor);
153             SetFont(aFont);
154             bInvalidate = true;
155         }
156         if (bInvalidate)
157             Invalidate();
158     }
159 }
160 
161 
162 // SplittedSide
163 
164 
165 // ctor
SplittedSide(Layout * pParent,Side eSide)166 Layout::SplittedSide::SplittedSide (Layout* pParent, Side eSide) :
167     rLayout(*pParent),
168     bVertical(eSide == Side::Left),
169     bLower(eSide == Side::Left),
170     nSize(0),
171     aSplitter(VclPtr<Splitter>::Create(pParent, bVertical ? WB_HSCROLL : WB_VSCROLL))
172 {
173     InitSplitter(*aSplitter);
174 }
175 
dispose()176 void Layout::SplittedSide::dispose()
177 {
178     aSplitter.disposeAndClear();
179     for (auto & item : vItems)
180     {
181         item.pSplit.disposeAndClear();
182         item.pWin.clear();
183     }
184 }
185 
186 // Add() -- adds a new window to the side (after construction)
Add(DockingWindow * pWin,Size const & rSize)187 void Layout::SplittedSide::Add (DockingWindow* pWin, Size const& rSize)
188 {
189     long const nSize1 = (bVertical ? rSize.Width() : rSize.Height()) + nSplitThickness;
190     long const nSize2 = bVertical ? rSize.Height() : rSize.Width();
191     // nSize
192     if (nSize1 > nSize)
193         nSize = nSize1;
194     // window
195     Item aItem;
196     aItem.pWin = pWin;
197     aItem.nStartPos = vItems.empty() ? 0 : vItems.back().nEndPos + nSplitThickness;
198     aItem.nEndPos = aItem.nStartPos + nSize2;
199     // splitter
200     if (!vItems.empty())
201     {
202         aItem.pSplit = VclPtr<Splitter>::Create(&rLayout, bVertical ? WB_VSCROLL : WB_HSCROLL);
203         aItem.pSplit->SetSplitPosPixel(aItem.nStartPos - nSplitThickness);
204         InitSplitter(*aItem.pSplit);
205     }
206     vItems.push_back(aItem);
207     // refresh
208     rLayout.ArrangeWindows();
209 }
210 
211 // Remove() -- removes a window from the side (if contains)
Remove(DockingWindow * pWin)212 void Layout::SplittedSide::Remove (DockingWindow* pWin)
213 {
214     // contains?
215     std::vector<Item>::size_type iWin;
216     for (iWin = 0; iWin != vItems.size(); ++iWin)
217         if (vItems[iWin].pWin == pWin)
218             break;
219     if (iWin == vItems.size())
220         return;
221     // remove
222     vItems[iWin].pSplit.disposeAndClear();
223     vItems[iWin].pWin.clear();
224     vItems.erase(vItems.begin() + iWin);
225     // if that was the first one, remove the first splitter line
226     if (iWin == 0 && !vItems.empty())
227         vItems.front().pSplit.reset();
228 }
229 
230 // creating a Point or a Size object
231 // The coordinate order depends on bVertical (reversed if true).
MakeSize(long A,long B) const232 inline Size Layout::SplittedSide::MakeSize (long A, long B) const
233 {
234     return bVertical ? Size(B, A) : Size(A, B);
235 }
MakePoint(long A,long B) const236 inline Point Layout::SplittedSide::MakePoint (long A, long B) const
237 {
238     return bVertical ? Point(B, A) : Point(A, B);
239 }
240 
241 // IsDocking() -- is this window currently docking in the strip?
IsDocking(DockingWindow const & rWin)242 bool Layout::SplittedSide::IsDocking (DockingWindow const& rWin)
243 {
244     return rWin.IsVisible() && !rWin.IsFloatingMode();
245 }
246 
247 // IsEmpty() -- are there no windows docked in this strip?
IsEmpty() const248 bool Layout::SplittedSide::IsEmpty () const
249 {
250     for (auto const & i: vItems)
251         if (IsDocking(*i.pWin))
252             return false;
253     return true;
254 }
255 
256 // GetSize() -- returns the width or height of the strip (depending on the direction)
GetSize() const257 long Layout::SplittedSide::GetSize () const
258 {
259     return IsEmpty() ? 0 : nSize;
260 }
261 
262 // Arrange() -- arranges the docking windows
263 // rRect: the available space
ArrangeIn(tools::Rectangle const & rRect)264 void Layout::SplittedSide::ArrangeIn (tools::Rectangle const& rRect)
265 {
266     // saving the rectangle
267     aRect = rRect;
268 
269     // the length of the side
270     long const nLength = bVertical ? aRect.GetSize().Height() : aRect.GetSize().Width();
271     long const nOtherSize = bVertical ? aRect.GetSize().Width() : aRect.GetSize().Height();
272     // bVertical ? horizontal position : vertical position
273     long const nPos1 = (bVertical ? aRect.Left() : aRect.Top()) +
274         (bLower ? 0 : nOtherSize - (nSize - nSplitThickness));
275     // bVertical ? vertical position : horizontal position
276     long const nPos2 = bVertical ? aRect.Top() : aRect.Left();
277 
278     // main line
279     bool const bEmpty = IsEmpty();
280     // shown if any of the windows is docked
281     if (!bEmpty)
282     {
283         aSplitter->Show();
284         // split position
285         aSplitter->SetSplitPosPixel((bLower ? nSize : nPos1) - nSplitThickness);
286         // the actual position and size
287         aSplitter->SetPosSizePixel(
288             MakePoint(nPos2, aSplitter->GetSplitPosPixel()),
289             MakeSize(nLength, nSplitThickness)
290         );
291         // dragging rectangle
292         aSplitter->SetDragRectPixel(aRect);
293     }
294     else
295         aSplitter->Hide();
296 
297     // positioning separator lines and windows
298     bool bPrevDocking = false; // is the previous window docked?
299     long nStartPos = 0; // window position in the strip
300     std::vector<Item>::size_type iLastWin = vItems.size(); // index of last docking window in the strip
301 
302     for (std::vector<Item>::size_type i = 0; i != vItems.size(); ++i)
303     {
304         // window
305         DockingWindow& rWin = *vItems[i].pWin;
306         bool const bDocking = IsDocking(rWin);
307         if (bDocking)
308             iLastWin = i;
309         // sizing window
310         rWin.ResizeIfDocking(
311             MakePoint(nPos2 + nStartPos, nPos1),
312             MakeSize(vItems[i].nEndPos - nStartPos, nSize - nSplitThickness)
313         );
314         // splitting line before the window
315         if (i > 0)
316         {
317             Splitter& rSplit = *vItems[i].pSplit;
318             // If neither of two adjacent windows are docked,
319             // the splitting line is hidden.
320             // If this window is docking but the previous isn't,
321             // then the splitting line is also hidden, because this window
322             // will occupy the space of the previous.
323             if (bPrevDocking)
324             {
325                 rSplit.Show();
326                 // the actual position and size of the line
327                 rSplit.SetPosSizePixel(
328                     MakePoint(nPos2 + nStartPos - nSplitThickness, nPos1),
329                     MakeSize(nSplitThickness, nSize - nSplitThickness)
330                 );
331                 // the dragging rectangle
332                 rSplit.SetDragRectPixel(tools::Rectangle(
333                     MakePoint(nPos2, nPos1),
334                     MakeSize(nLength, nSize - nSplitThickness)
335                 ));
336             }
337             else
338                 rSplit.Hide();
339         }
340         // next
341         bPrevDocking = bDocking;
342         if (bDocking)
343             nStartPos = vItems[i].nEndPos + nSplitThickness;
344         // We only set nStartPos if this window is docking, because otherwise
345         // the next window will occupy also the space of this window.
346     }
347 
348     // filling the remaining space with the last docking window
349     if (!bEmpty && vItems[iLastWin].nEndPos != nLength)
350     {
351         Item& rItem = vItems[iLastWin];
352         Size aSize = rItem.pWin->GetDockingSize();
353         if (bVertical)
354             aSize.AdjustHeight( nLength - rItem.nEndPos );
355         else
356             aSize.AdjustWidth( nLength - rItem.nEndPos );
357         rItem.pWin->ResizeIfDocking(aSize);
358         // and hiding the split line after the window
359         if (iLastWin < vItems.size() - 1)
360             vItems[iLastWin + 1].pSplit->Hide();
361     }
362 }
363 
IMPL_LINK(Layout::SplittedSide,SplitHdl,Splitter *,pSplitter,void)364 IMPL_LINK(Layout::SplittedSide, SplitHdl, Splitter*, pSplitter, void)
365 {
366     // checking margins
367     CheckMarginsFor(pSplitter);
368     // changing stored sizes
369     if (pSplitter == aSplitter.get())
370     {
371         // nSize
372         if (bLower)
373             nSize = pSplitter->GetSplitPosPixel();
374         else
375             nSize = (bVertical ? aRect.Right() : aRect.Bottom()) + 1 - pSplitter->GetSplitPosPixel();
376     }
377     else
378     {
379         // Item::nStartPos, Item::nLength
380         for (size_t i = 1; i < vItems.size(); ++i)
381         {
382             if (vItems[i].pSplit.get() == pSplitter)
383             {
384                 // before the line
385                 vItems[i - 1].nEndPos = pSplitter->GetSplitPosPixel();
386                 // after the line
387                 vItems[i].nStartPos = pSplitter->GetSplitPosPixel() + nSplitThickness;
388             }
389         }
390     }
391     // arranging windows
392     rLayout.ArrangeWindows();
393 }
394 
CheckMarginsFor(Splitter * pSplitter)395 void Layout::SplittedSide::CheckMarginsFor (Splitter* pSplitter)
396 {
397     // The splitter line cannot be closer to the edges than nMargin pixels.
398     static long const nMargin = 16;
399     // Checking margins:
400     if (long const nLength = pSplitter->IsHorizontal() ?
401         aRect.GetWidth() : aRect.GetHeight()
402     ) {
403         // bounds
404         long const nLower = (pSplitter->IsHorizontal() ? aRect.Left() : aRect.Top()) + nMargin;
405         long const nUpper = nLower + nLength - 2*nMargin;
406         // split position
407         long const nPos = pSplitter->GetSplitPosPixel();
408         // checking bounds
409         if (nPos < nLower)
410             pSplitter->SetSplitPosPixel(nLower);
411         if (nPos > nUpper)
412             pSplitter->SetSplitPosPixel(nUpper);
413     }
414 }
415 
InitSplitter(Splitter & rSplitter)416 void Layout::SplittedSide::InitSplitter (Splitter& rSplitter)
417 {
418     // link
419     rSplitter.SetSplitHdl(LINK(this, SplittedSide, SplitHdl));
420     // color
421     Color aColor = rLayout.GetSettings().GetStyleSettings().GetShadowColor();
422     rSplitter.SetLineColor(aColor);
423     rSplitter.SetFillColor(aColor);
424 }
425 
426 
427 } // namespace basctl
428 
429 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
430