lok: Don't attempt to select the exact text after a failed search.
[LibreOffice.git] / basctl / source / basicide / layout.cxx
blob12ad6277055b88c1032cdc5b121f6e85bf11ad11
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>
25 #include <boost/make_shared.hpp>
27 namespace basctl
30 namespace
32 // the thickness of the splitting lines
33 static long const nSplitThickness = 3;
34 } // namespace
36 // ctor for derived classes
37 // pParent: the parent window (Shell)
38 Layout::Layout (vcl::Window* pParent) :
39 Window(pParent, WB_CLIPCHILDREN),
40 pChild(0),
41 bFirstSize(true),
42 aLeftSide(this, SplittedSide::Left),
43 aBottomSide(this, SplittedSide::Bottom)
45 SetBackground(GetSettings().GetStyleSettings().GetWindowColor());
47 vcl::Font aFont = GetFont();
48 Size aSz = aFont.GetSize();
49 aSz.Height() *= 1.5;
50 aFont.SetSize(aSz);
51 aFont.SetWeight(WEIGHT_BOLD);
52 aFont.SetColor(GetSettings().GetStyleSettings().GetWindowTextColor());
53 SetFont(aFont);
56 Layout::~Layout()
58 disposeOnce();
61 void Layout::dispose()
63 aLeftSide.dispose();
64 aBottomSide.dispose();
65 pChild.clear();
66 Window::dispose();
69 // removes a docking window
70 void Layout::Remove (DockingWindow* pWin)
72 aLeftSide.Remove(pWin);
73 aBottomSide.Remove(pWin);
76 // called by Window when resized
77 void Layout::Resize()
79 if (IsVisible())
80 ArrangeWindows();
83 // ArrangeWindows() -- arranges the child windows
84 void Layout::ArrangeWindows ()
86 // prevent recursion via OnFirstSize() -> Add() -> ArrangeWindows()
87 static bool bInArrangeWindows = false;
88 if (bInArrangeWindows)
89 return;
90 bInArrangeWindows = true;
92 Size const aSize = GetOutputSizePixel();
93 long const nWidth = aSize.Width(), nHeight = aSize.Height();
94 if (nWidth && nHeight) // non-empty size
96 // On first call the derived classes initializes the sizes of the
97 // docking windows. This cannot be done at construction because
98 // the Layout has empty size at that point.
99 if (bFirstSize)
101 bFirstSize = false;
102 this->OnFirstSize(nWidth, nHeight); // virtual
105 // sides
106 aBottomSide.ArrangeIn(Rectangle(Point(0, 0), aSize));
107 aLeftSide.ArrangeIn(Rectangle(Point(0, 0), Size(nWidth, nHeight - aBottomSide.GetSize())));
108 // child in the middle
109 pChild->SetPosSizePixel(
110 Point(aLeftSide.GetSize(), 0),
111 Size(nWidth - aLeftSide.GetSize(), nHeight - aBottomSide.GetSize())
115 bInArrangeWindows = false;
118 void Layout::DockaWindow (DockingWindow*)
120 ArrangeWindows();
123 void Layout::Activating (BaseWindow& rWindow)
125 // first activation
126 pChild = &rWindow;
127 ArrangeWindows();
128 Show();
129 pChild->Activating();
132 void Layout::Deactivating ()
134 if (pChild)
135 pChild->Deactivating();
136 Hide();
137 pChild = 0;
140 // virtual
141 void Layout::DataChanged (DataChangedEvent const& rDCEvt)
143 Window::DataChanged(rDCEvt);
144 if (rDCEvt.GetType() == DataChangedEventType::SETTINGS && (rDCEvt.GetFlags() & AllSettingsFlags::STYLE))
146 bool bInvalidate = false;
147 Color aColor = GetSettings().GetStyleSettings().GetWindowColor();
148 const AllSettings* pOldSettings = rDCEvt.GetOldSettings();
149 if (!pOldSettings || aColor != pOldSettings->GetStyleSettings().GetWindowColor())
151 SetBackground(Wallpaper(aColor));
152 bInvalidate = true;
154 aColor = GetSettings().GetStyleSettings().GetWindowTextColor();
155 if (!pOldSettings || aColor != pOldSettings->GetStyleSettings().GetWindowTextColor())
157 vcl::Font aFont(GetFont());
158 aFont.SetColor(aColor);
159 SetFont(aFont);
160 bInvalidate = true;
162 if (bInvalidate)
163 Invalidate();
168 // SplittedSide
172 // ctor
173 Layout::SplittedSide::SplittedSide (Layout* pParent, Side eSide) :
174 rLayout(*pParent),
175 bVertical(eSide == Left || eSide == Right),
176 bLower(eSide == Left || eSide == Top),
177 nSize(0),
178 aSplitter(VclPtr<Splitter>::Create(pParent, bVertical ? WB_HSCROLL : WB_VSCROLL))
180 InitSplitter(*aSplitter.get());
183 void Layout::SplittedSide::dispose()
185 aSplitter.disposeAndClear();
186 for (auto i = vItems.begin(); i != vItems.end(); ++i)
188 i->pSplit.disposeAndClear();
189 i->pWin.clear();
193 // Add() -- adds a new window to the side (after construction)
194 void Layout::SplittedSide::Add (DockingWindow* pWin, Size const& rSize)
196 long const nSize1 = (bVertical ? rSize.Width() : rSize.Height()) + nSplitThickness;
197 long const nSize2 = bVertical ? rSize.Height() : rSize.Width();
198 // nSize
199 if (nSize1 > nSize)
200 nSize = nSize1;
201 // window
202 Item aItem;
203 aItem.pWin = pWin;
204 aItem.nStartPos = vItems.empty() ? 0 : vItems.back().nEndPos + nSplitThickness;
205 aItem.nEndPos = aItem.nStartPos + nSize2;
206 // splitter
207 if (!vItems.empty())
209 aItem.pSplit = VclPtr<Splitter>::Create(&rLayout, bVertical ? WB_VSCROLL : WB_HSCROLL);
210 aItem.pSplit->SetSplitPosPixel(aItem.nStartPos - nSplitThickness);
211 InitSplitter(*aItem.pSplit);
213 vItems.push_back(aItem);
214 // refresh
215 rLayout.ArrangeWindows();
218 // Remove() -- removes a window from the side (if contains)
219 void Layout::SplittedSide::Remove (DockingWindow* pWin)
221 // contains?
222 unsigned iWin;
223 for (iWin = 0; iWin != vItems.size(); ++iWin)
224 if (vItems[iWin].pWin == pWin)
225 break;
226 if (iWin == vItems.size())
227 return;
228 // remove
229 vItems[iWin].pSplit.disposeAndClear();
230 vItems[iWin].pWin.clear();
231 vItems.erase(vItems.begin() + iWin);
232 // if that was the first one, remove the first splitter line
233 if (iWin == 0 && !vItems.empty())
234 vItems.front().pSplit.reset();
237 // creating a Point or a Size object
238 // The coordinate order depends on bVertical (reversed if true).
239 inline Size Layout::SplittedSide::MakeSize (long A, long B) const
241 return bVertical ? Size(B, A) : Size(A, B);
243 inline Point Layout::SplittedSide::MakePoint (long A, long B) const
245 return bVertical ? Point(B, A) : Point(A, B);
248 // IsDocking() -- is this window currently docking in the strip?
249 bool Layout::SplittedSide::IsDocking (DockingWindow const& rWin)
251 return rWin.IsVisible() && !rWin.IsFloatingMode();
254 // IsEmpty() -- are there no windows docked in this strip?
255 bool Layout::SplittedSide::IsEmpty () const
257 for (unsigned i = 0; i != vItems.size(); ++i)
258 if (IsDocking(*vItems[i].pWin))
259 return false;
260 return true;
263 // GetSize() -- returns the width or height of the strip (depending on the direction)
264 long Layout::SplittedSide::GetSize () const
266 return IsEmpty() ? 0 : nSize;
269 // Arrange() -- arranges the docking windows
270 // rRect: the available space
271 void Layout::SplittedSide::ArrangeIn (Rectangle const& rRect)
273 // saving the rectangle
274 aRect = rRect;
276 // the length of the side
277 long const nLength = bVertical ? aRect.GetSize().Height() : aRect.GetSize().Width();
278 long const nOtherSize = bVertical ? aRect.GetSize().Width() : aRect.GetSize().Height();
279 // bVertical ? horizontal pozition : vertical pozition
280 long const nPos1 = (bVertical ? aRect.Left() : aRect.Top()) +
281 (bLower ? 0 : nOtherSize - (nSize - nSplitThickness));
282 // bVertical ? vertical position : horizontal position
283 long const nPos2 = bVertical ? aRect.Top() : aRect.Left();
285 // main line
286 bool const bEmpty = IsEmpty();
287 // shown if any of the windows is docked
288 if (!bEmpty)
290 aSplitter->Show();
291 // split position
292 aSplitter->SetSplitPosPixel((bLower ? nSize : nPos1) - nSplitThickness);
293 // the actual position and size
294 aSplitter->SetPosSizePixel(
295 MakePoint(nPos2, aSplitter->GetSplitPosPixel()),
296 MakeSize(nLength, nSplitThickness)
298 // dragging rectangle
299 aSplitter->SetDragRectPixel(aRect);
301 else
302 aSplitter->Hide();
304 // positioning separator lines and windows
305 bool bPrevDocking = false; // is the previous window docked?
306 long nStartPos = 0; // window position in the strip
307 unsigned iLastWin = vItems.size(); // index of last docking window in the strip
309 for (unsigned i = 0; i != vItems.size(); ++i)
311 // window
312 DockingWindow& rWin = *vItems[i].pWin;
313 bool const bDocking = IsDocking(rWin);
314 if (bDocking)
315 iLastWin = i;
316 // sizing window
317 rWin.ResizeIfDocking(
318 MakePoint(nPos2 + nStartPos, nPos1),
319 MakeSize(vItems[i].nEndPos - nStartPos, nSize - nSplitThickness)
321 // splitting line before the window
322 if (i > 0)
324 Splitter& rSplit = *vItems[i].pSplit;
325 // If neither of two adjacent windows are docked,
326 // the splitting line is hidden.
327 // If this window is docking but the previous isn't,
328 // then the splitting line is also hidden, because this window
329 // will occupy the space of the previous.
330 if (bPrevDocking)
332 rSplit.Show();
333 // the actual position and size of the line
334 rSplit.SetPosSizePixel(
335 MakePoint(nPos2 + nStartPos - nSplitThickness, nPos1),
336 MakeSize(nSplitThickness, nSize - nSplitThickness)
338 // the dragging rectangle
339 rSplit.SetDragRectPixel(Rectangle(
340 MakePoint(nPos2, nPos1),
341 MakeSize(nLength, nSize - nSplitThickness)
344 else
345 rSplit.Hide();
347 // next
348 bPrevDocking = bDocking;
349 if (bDocking)
350 nStartPos = vItems[i].nEndPos + nSplitThickness;
351 // We only set nStartPos if this window is docking, because otherwise
352 // the next window will occupy also the space of this window.
355 // filling the remaining space with the last docking window
356 if (!bEmpty && vItems[iLastWin].nEndPos != nLength)
358 Item& rItem = vItems[iLastWin];
359 Size aSize = rItem.pWin->GetDockingSize();
360 (bVertical ? aSize.Height() : aSize.Width()) += nLength - rItem.nEndPos;
361 rItem.pWin->ResizeIfDocking(aSize);
362 // and hiding the split line after the window
363 if (iLastWin < vItems.size() - 1)
364 vItems[iLastWin + 1].pSplit->Hide();
368 IMPL_LINK(Layout::SplittedSide, SplitHdl, Splitter*, pSplitter)
370 // checking margins
371 CheckMarginsFor(pSplitter);
372 // changing stored sizes
373 if (pSplitter == aSplitter.get())
375 // nSize
376 if (bLower)
377 nSize = pSplitter->GetSplitPosPixel();
378 else
379 nSize = (bVertical ? aRect.Right() : aRect.Bottom()) + 1 - pSplitter->GetSplitPosPixel();
381 else
383 // Item::nStartPos, Item::nLength
384 for (unsigned i = 1; i < vItems.size(); ++i)
386 if (vItems[i].pSplit.get() == pSplitter)
388 // before the line
389 vItems[i - 1].nEndPos = pSplitter->GetSplitPosPixel();
390 // after the line
391 vItems[i].nStartPos = pSplitter->GetSplitPosPixel() + nSplitThickness;
395 // arranging windows
396 rLayout.ArrangeWindows();
398 return 0;
401 void Layout::SplittedSide::CheckMarginsFor (Splitter* pSplitter)
403 // The splitter line cannot be closer to the edges than nMargin pixels.
404 static long const nMargin = 16;
405 // Checking margins:
406 if (long const nLength = pSplitter->IsHorizontal() ?
407 aRect.GetWidth() : aRect.GetHeight()
409 // bounds
410 long const nLower = (pSplitter->IsHorizontal() ? aRect.Left() : aRect.Top()) + nMargin;
411 long const nUpper = nLower + nLength - 2*nMargin;
412 // split position
413 long const nPos = pSplitter->GetSplitPosPixel();
414 // checking bounds
415 if (nPos < nLower)
416 pSplitter->SetSplitPosPixel(nLower);
417 if (nPos > nUpper)
418 pSplitter->SetSplitPosPixel(nUpper);
422 void Layout::SplittedSide::InitSplitter (Splitter& rSplitter)
424 // link
425 rSplitter.SetSplitHdl(LINK(this, SplittedSide, SplitHdl));
426 // color
427 Color aColor = rLayout.GetSettings().GetStyleSettings().GetShadowColor();
428 rSplitter.SetLineColor(aColor);
429 rSplitter.SetFillColor(aColor);
433 } // namespace basctl
435 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */