Bump version to 24.04.3.4
[LibreOffice.git] / basctl / source / basicide / layout.cxx
blobe6b6676ed6c686626bf4ecdb5c13d29b194dfb63
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <layout.hxx>
22 #include <bastypes.hxx>
23 #include <vcl/settings.hxx>
24 #include <vcl/event.hxx>
26 namespace basctl
29 namespace
31 // the thickness of the splitting lines
32 tools::Long const nSplitThickness = 3;
33 } // namespace
35 // ctor for derived classes
36 // pParent: the parent window (Shell)
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)
44 SetBackground(GetSettings().GetStyleSettings().GetWindowColor());
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);
55 Layout::~Layout()
57 disposeOnce();
60 void Layout::dispose()
62 aLeftSide.dispose();
63 aBottomSide.dispose();
64 pChild.clear();
65 Window::dispose();
68 // removes a docking window
69 void Layout::Remove (DockingWindow* pWin)
71 aLeftSide.Remove(pWin);
72 aBottomSide.Remove(pWin);
75 // called by Window when resized
76 void Layout::Resize()
78 if (IsVisible())
79 ArrangeWindows();
82 // ArrangeWindows() -- arranges the child windows
83 void Layout::ArrangeWindows ()
85 // prevent recursion via OnFirstSize() -> Add() -> ArrangeWindows()
86 static bool bInArrangeWindows = false;
87 if (bInArrangeWindows)
88 return;
89 bInArrangeWindows = true;
91 Size const aSize = GetOutputSizePixel();
92 tools::Long const nWidth = aSize.Width(), nHeight = aSize.Height();
93 if (nWidth && nHeight) // non-empty size
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)
100 bFirstSize = false;
101 OnFirstSize(nWidth, nHeight); // virtual
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())
114 bInArrangeWindows = false;
117 void Layout::Activating (BaseWindow& rWindow)
119 // first activation
120 pChild = &rWindow;
121 ArrangeWindows();
122 Show();
123 pChild->Activating();
126 void Layout::Deactivating ()
128 if (pChild)
129 pChild->Deactivating();
130 Hide();
131 pChild = nullptr;
134 // virtual
135 void Layout::DataChanged (DataChangedEvent const& rDCEvt)
137 Window::DataChanged(rDCEvt);
138 if (!(rDCEvt.GetType() == DataChangedEventType::SETTINGS && (rDCEvt.GetFlags() & AllSettingsFlags::STYLE)))
139 return;
141 bool bInvalidate = false;
142 Color aColor = GetSettings().GetStyleSettings().GetWindowColor();
143 const AllSettings* pOldSettings = rDCEvt.GetOldSettings();
144 if (!pOldSettings || aColor != pOldSettings->GetStyleSettings().GetWindowColor())
146 SetBackground(Wallpaper(aColor));
147 bInvalidate = true;
149 aColor = GetSettings().GetStyleSettings().GetWindowTextColor();
150 if (!pOldSettings || aColor != pOldSettings->GetStyleSettings().GetWindowTextColor())
152 vcl::Font aFont(GetFont());
153 aFont.SetColor(aColor);
154 SetFont(aFont);
155 bInvalidate = true;
157 if (bInvalidate)
158 Invalidate();
162 // SplittedSide
165 // ctor
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))
173 InitSplitter(*aSplitter);
176 void Layout::SplittedSide::dispose()
178 aSplitter.disposeAndClear();
179 for (auto & item : vItems)
181 item.pSplit.disposeAndClear();
182 item.pWin.clear();
186 // Add() -- adds a new window to the side (after construction)
187 void Layout::SplittedSide::Add (DockingWindow* pWin, Size const& rSize)
189 tools::Long const nSize1 = (bVertical ? rSize.Width() : rSize.Height()) + nSplitThickness;
190 tools::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())
202 aItem.pSplit = VclPtr<Splitter>::Create(&rLayout, bVertical ? WB_VSCROLL : WB_HSCROLL);
203 aItem.pSplit->SetSplitPosPixel(aItem.nStartPos - nSplitThickness);
204 InitSplitter(*aItem.pSplit);
206 vItems.push_back(aItem);
207 // refresh
208 rLayout.ArrangeWindows();
211 // Remove() -- removes a window from the side (if contains)
212 void Layout::SplittedSide::Remove (DockingWindow* pWin)
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();
230 // creating a Point or a Size object
231 // The coordinate order depends on bVertical (reversed if true).
232 inline Size Layout::SplittedSide::MakeSize (tools::Long A, tools::Long B) const
234 return bVertical ? Size(B, A) : Size(A, B);
236 inline Point Layout::SplittedSide::MakePoint (tools::Long A, tools::Long B) const
238 return bVertical ? Point(B, A) : Point(A, B);
241 // IsDocking() -- is this window currently docking in the strip?
242 bool Layout::SplittedSide::IsDocking (DockingWindow const& rWin)
244 return rWin.IsVisible() && !rWin.IsFloatingMode();
247 // IsEmpty() -- are there no windows docked in this strip?
248 bool Layout::SplittedSide::IsEmpty () const
250 for (auto const & i: vItems)
251 if (IsDocking(*i.pWin))
252 return false;
253 return true;
256 // GetSize() -- returns the width or height of the strip (depending on the direction)
257 tools::Long Layout::SplittedSide::GetSize () const
259 return IsEmpty() ? 0 : nSize;
262 // Arrange() -- arranges the docking windows
263 // rRect: the available space
264 void Layout::SplittedSide::ArrangeIn (tools::Rectangle const& rRect)
266 // saving the rectangle
267 aRect = rRect;
269 // the length of the side
270 tools::Long const nLength = bVertical ? aRect.GetSize().Height() : aRect.GetSize().Width();
271 tools::Long const nOtherSize = bVertical ? aRect.GetSize().Width() : aRect.GetSize().Height();
272 // bVertical ? horizontal position : vertical position
273 tools::Long const nPos1 = (bVertical ? aRect.Left() : aRect.Top()) +
274 (bLower ? 0 : nOtherSize - (nSize - nSplitThickness));
275 // bVertical ? vertical position : horizontal position
276 tools::Long const nPos2 = bVertical ? aRect.Top() : aRect.Left();
278 // main line
279 bool const bEmpty = IsEmpty();
280 // shown if any of the windows is docked
281 if (!bEmpty)
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)
291 // dragging rectangle
292 aSplitter->SetDragRectPixel(aRect);
294 else
295 aSplitter->Hide();
297 // positioning separator lines and windows
298 bool bPrevDocking = false; // is the previous window docked?
299 tools::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
302 for (std::vector<Item>::size_type i = 0; i != vItems.size(); ++i)
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)
314 // splitting line before the window
315 if (i > 0)
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)
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)
331 // the dragging rectangle
332 rSplit.SetDragRectPixel(tools::Rectangle(
333 MakePoint(nPos2, nPos1),
334 MakeSize(nLength, nSize - nSplitThickness)
337 else
338 rSplit.Hide();
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.
348 // filling the remaining space with the last docking window
349 if (bEmpty || vItems[iLastWin].nEndPos == nLength)
350 return;
352 Item& rItem = vItems[iLastWin];
353 Size aSize = rItem.pWin->GetDockingSize();
354 if (bVertical)
355 aSize.AdjustHeight( nLength - rItem.nEndPos );
356 else
357 aSize.AdjustWidth( nLength - rItem.nEndPos );
358 rItem.pWin->ResizeIfDocking(aSize);
359 // and hiding the split line after the window
360 if (iLastWin < vItems.size() - 1)
361 vItems[iLastWin + 1].pSplit->Hide();
364 IMPL_LINK(Layout::SplittedSide, SplitHdl, Splitter*, pSplitter, void)
366 // checking margins
367 CheckMarginsFor(pSplitter);
368 // changing stored sizes
369 if (pSplitter == aSplitter.get())
371 // nSize
372 if (bLower)
373 nSize = pSplitter->GetSplitPosPixel();
374 else
375 nSize = (bVertical ? aRect.Right() : aRect.Bottom()) + 1 - pSplitter->GetSplitPosPixel();
377 else
379 // Item::nStartPos, Item::nLength
380 for (size_t i = 1; i < vItems.size(); ++i)
382 if (vItems[i].pSplit.get() == pSplitter)
384 // before the line
385 vItems[i - 1].nEndPos = pSplitter->GetSplitPosPixel();
386 // after the line
387 vItems[i].nStartPos = pSplitter->GetSplitPosPixel() + nSplitThickness;
391 // arranging windows
392 rLayout.ArrangeWindows();
395 void Layout::SplittedSide::CheckMarginsFor (Splitter* pSplitter)
397 // The splitter line cannot be closer to the edges than nMargin pixels.
398 static tools::Long const nMargin = 16;
399 // Checking margins:
400 tools::Long const nLength = pSplitter->IsHorizontal() ?
401 aRect.GetWidth() : aRect.GetHeight();
402 if (!nLength)
403 return;
405 // bounds
406 tools::Long const nLower = (pSplitter->IsHorizontal() ? aRect.Left() : aRect.Top()) + nMargin;
407 tools::Long const nUpper = nLower + nLength - 2*nMargin;
408 // split position
409 tools::Long const nPos = pSplitter->GetSplitPosPixel();
410 // checking bounds
411 if (nPos < nLower)
412 pSplitter->SetSplitPosPixel(nLower);
413 if (nPos > nUpper)
414 pSplitter->SetSplitPosPixel(nUpper);
417 void Layout::SplittedSide::InitSplitter (Splitter& rSplitter)
419 // link
420 rSplitter.SetSplitHdl(LINK(this, SplittedSide, SplitHdl));
421 // color
422 Color aColor = rLayout.GetSettings().GetStyleSettings().GetShadowColor();
423 rSplitter.GetOutDev()->SetLineColor(aColor);
424 rSplitter.GetOutDev()->SetFillColor(aColor);
428 } // namespace basctl
430 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */