tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / tools / source / generic / gen.cxx
blob012318c0218d76b21359e8a3213f2f2ee9af2e37
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 <sal/config.h>
21 #include <rtl/string.hxx>
23 #include <algorithm>
24 #include <tuple>
25 #include <o3tl/hash_combine.hxx>
26 #include <o3tl/safeint.hxx>
27 #include <tools/gen.hxx>
29 OString Pair::toString() const
31 // Note that this is not just used for debugging output but the
32 // format is parsed by external code (passed in callbacks to
33 // LibreOfficeKit clients). So don't change.
34 return OString::number(A()) + ", " + OString::number(B());
37 size_t Pair::GetHashValue() const
39 size_t hash = 0;
40 o3tl::hash_combine( hash, mnA );
41 o3tl::hash_combine( hash, mnB );
42 return hash;
45 void RectangleTemplateBase::SaturatingSetSize(const SizeTemplateBase& rSize)
47 if (rSize.Width() < 0)
48 mnRight = o3tl::saturating_add(mnLeft, (rSize.Width() + 1));
49 else if ( rSize.Width() > 0 )
50 mnRight = o3tl::saturating_add(mnLeft, (rSize.Width() - 1));
51 else
52 SetWidthEmpty();
54 if ( rSize.Height() < 0 )
55 mnBottom = o3tl::saturating_add(mnTop, (rSize.Height() + 1));
56 else if ( rSize.Height() > 0 )
57 mnBottom = o3tl::saturating_add(mnTop, (rSize.Height() - 1));
58 else
59 SetHeightEmpty();
62 void RectangleTemplateBase::SaturatingSetPosX(tools::Long x)
64 if (!IsWidthEmpty())
65 mnRight = o3tl::saturating_add(mnRight, x - mnLeft);
66 mnLeft = x;
69 void RectangleTemplateBase::SaturatingSetPosY(tools::Long y)
71 if (!IsHeightEmpty())
72 mnBottom = o3tl::saturating_add(mnBottom, y - mnTop);
73 mnTop = y;
76 void RectangleTemplateBase::Union( const RectangleTemplateBase& rRect )
78 if ( rRect.IsEmpty() )
79 return;
81 if ( IsEmpty() )
82 *this = rRect;
83 else
85 std::tie(mnLeft, mnRight) = std::minmax({ mnLeft, rRect.mnLeft, mnRight, rRect.mnRight });
86 std::tie(mnTop, mnBottom) = std::minmax({ mnTop, rRect.mnTop, mnBottom, rRect.mnBottom });
90 void RectangleTemplateBase::Intersection( const RectangleTemplateBase& rRect )
92 if ( IsEmpty() )
93 return;
94 if ( rRect.IsEmpty() )
96 *this = tools::Rectangle();
97 return;
100 // Normalize rectangle
101 RectangleTemplateBase aTmpRect( rRect );
102 Normalize();
103 aTmpRect.Normalize();
105 // Perform intersection
106 mnLeft = std::max( mnLeft, aTmpRect.mnLeft );
107 mnRight = std::min( mnRight, aTmpRect.mnRight );
108 mnTop = std::max( mnTop, aTmpRect.mnTop );
109 mnBottom= std::min( mnBottom, aTmpRect.mnBottom );
111 // Determine if intersection is empty
112 if ( mnRight < mnLeft || mnBottom < mnTop )
113 *this = tools::Rectangle();
116 void RectangleTemplateBase::Normalize()
118 if ((mnRight < mnLeft) && (!IsWidthEmpty()))
120 std::swap(mnLeft, mnRight);
123 if ((mnBottom < mnTop) && (!IsHeightEmpty()))
125 std::swap(mnBottom, mnTop);
129 bool RectangleTemplateBase::Contains( const PointTemplateBase& rPoint ) const
131 if ( IsEmpty() )
132 return false;
134 if ( mnLeft <= mnRight )
136 if ( (rPoint.X() < mnLeft) || (rPoint.X() > mnRight) )
137 return false;
139 else
141 if ( (rPoint.X() > mnLeft) || (rPoint.X() < mnRight) )
142 return false;
144 if ( mnTop <= mnBottom )
146 if ( (rPoint.Y() < mnTop) || (rPoint.Y() > mnBottom) )
147 return false;
149 else
151 if ( (rPoint.Y() > mnTop) || (rPoint.Y() < mnBottom) )
152 return false;
154 return true;
157 bool RectangleTemplateBase::Contains( const RectangleTemplateBase& rRect ) const
159 return Contains( PointTemplateBase{ rRect.Left(), rRect.Top() } )
160 && Contains( PointTemplateBase{ rRect.Right(), rRect.Bottom() } );
163 bool RectangleTemplateBase::Overlaps( const RectangleTemplateBase& rRect ) const
165 // If there's no intersection, they don't overlap
166 RectangleTemplateBase aTmp(*this);
167 aTmp.Intersection(rRect);
168 return !aTmp.IsEmpty();
171 OString RectangleTemplateBase::toString() const
173 // Note that this is not just used for debugging output but the
174 // format is parsed by external code (passed in callbacks to
175 // LibreOfficeKit clients). So don't change.
176 return OString::number(Left()) + ", "
177 + OString::number(Top()) + ", "
178 + OString::number(getOpenWidth()) + ", "
179 + OString::number(getOpenHeight());
182 void RectangleTemplateBase::expand(tools::Long nExpandBy)
184 AdjustLeft(-nExpandBy);
185 AdjustTop(-nExpandBy);
186 AdjustRight(nExpandBy);
187 AdjustBottom(nExpandBy);
190 void RectangleTemplateBase::shrink(tools::Long nShrinkBy)
192 mnLeft += nShrinkBy;
193 mnTop += nShrinkBy;
194 if (!IsWidthEmpty())
195 mnRight -= nShrinkBy;
196 if (!IsHeightEmpty())
197 mnBottom -= nShrinkBy;
200 tools::Long RectangleTemplateBase::AdjustRight(tools::Long nHorzMoveDelta)
202 if (IsWidthEmpty())
203 mnRight = mnLeft + nHorzMoveDelta - 1;
204 else
205 mnRight += nHorzMoveDelta;
206 return mnRight;
209 tools::Long RectangleTemplateBase::AdjustBottom( tools::Long nVertMoveDelta )
211 if (IsHeightEmpty())
212 mnBottom = mnTop + nVertMoveDelta - 1;
213 else
214 mnBottom += nVertMoveDelta;
215 return mnBottom;
218 static_assert( std::is_trivially_copyable< Pair >::value );
219 static_assert( std::is_trivially_copyable< Point >::value );
220 static_assert( std::is_trivially_copyable< Size >::value );
221 static_assert( std::is_trivially_copyable< Range >::value );
222 static_assert( std::is_trivially_copyable< Selection >::value );
223 static_assert( std::is_trivially_copyable< tools::Rectangle >::value );
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */