fix toolbar import
[ooovba.git] / tools / source / generic / gen.cxx
blob4be5471d84c9240845e415ffe8669c0323f0a486
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: gen.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_tools.hxx"
33 #include <tools/debug.hxx>
34 #include <tools/gen.hxx>
35 #include <tools/stream.hxx>
37 // =======================================================================
39 SvStream& operator>>( SvStream& rIStream, Pair& rPair )
41 DBG_ASSERTWARNING( rIStream.GetVersion(), "Pair::>> - Solar-Version not set on rIStream" );
43 if ( rIStream.GetCompressMode() == COMPRESSMODE_FULL )
45 unsigned char cId;
46 unsigned char cAry[8];
47 int i;
48 int i1;
49 int i2;
50 UINT32 nNum;
52 rIStream >> cId;
53 i1 = (cId & 0x70) >> 4;
54 i2 = cId & 0x07;
55 rIStream.Read( cAry, i1+i2 );
57 nNum = 0;
58 i = i1;
59 while ( i )
61 i--;
62 nNum <<= 8;
63 nNum |= cAry[i];
65 if ( cId & 0x80 )
66 nNum ^= 0xFFFFFFFF;
67 rPair.nA = (INT32)nNum;
69 nNum = 0;
70 i = i1+i2;
71 while ( i > i1 )
73 i--;
74 nNum <<= 8;
75 nNum |= cAry[i];
77 if ( cId & 0x08 )
78 nNum ^= 0xFFFFFFFF;
79 rPair.nB = (INT32)nNum;
81 else
83 rIStream >> rPair.nA >> rPair.nB;
86 return rIStream;
89 // -----------------------------------------------------------------------
91 SvStream& operator<<( SvStream& rOStream, const Pair& rPair )
93 DBG_ASSERTWARNING( rOStream.GetVersion(), "Pair::<< - Solar-Version not set on rOStream" );
95 if ( rOStream.GetCompressMode() == COMPRESSMODE_FULL )
97 unsigned char cAry[9];
98 int i = 1;
99 UINT32 nNum;
101 cAry[0] = 0;
103 nNum = (UINT32)(INT32)rPair.nA;
104 if ( rPair.nA < 0 )
106 cAry[0] |= 0x80;
107 nNum ^= 0xFFFFFFFF;
109 if ( nNum )
111 cAry[i] = (unsigned char)(nNum & 0xFF);
112 nNum >>= 8;
113 i++;
115 if ( nNum )
117 cAry[i] = (unsigned char)(nNum & 0xFF);
118 nNum >>= 8;
119 i++;
121 if ( nNum )
123 cAry[i] = (unsigned char)(nNum & 0xFF);
124 nNum >>= 8;
125 i++;
127 if ( nNum )
129 cAry[i] = (unsigned char)(nNum & 0xFF);
130 nNum >>= 8;
131 i++;
132 cAry[0] |= 0x40;
134 else
135 cAry[0] |= 0x30;
137 else
138 cAry[0] |= 0x20;
140 else
141 cAry[0] |= 0x10;
144 nNum = (UINT32)(INT32)rPair.nB;
145 if ( rPair.nB < 0 )
147 cAry[0] |= 0x08;
148 nNum ^= 0xFFFFFFFF;
150 if ( nNum )
152 cAry[i] = (unsigned char)(nNum & 0xFF);
153 nNum >>= 8;
154 i++;
156 if ( nNum )
158 cAry[i] = (unsigned char)(nNum & 0xFF);
159 nNum >>= 8;
160 i++;
162 if ( nNum )
164 cAry[i] = (unsigned char)(nNum & 0xFF);
165 nNum >>= 8;
166 i++;
168 if ( nNum )
170 cAry[i] = (unsigned char)(nNum & 0xFF);
171 nNum >>= 8;
172 i++;
173 cAry[0] |= 0x04;
175 else
176 cAry[0] |= 0x03;
178 else
179 cAry[0] |= 0x02;
181 else
182 cAry[0] |= 0x01;
185 rOStream.Write( cAry, i );
187 else
189 rOStream << rPair.nA << rPair.nB;
192 return rOStream;
195 /*************************************************************************
197 |* Rectangle::SetSize()
199 |* Beschreibung GEN.SDW
200 |* Ersterstellung DV 29.10.91
201 |* Letzte Aenderung MM 21.04.94
203 *************************************************************************/
205 void Rectangle::SetSize( const Size& rSize )
207 if ( rSize.Width() < 0 )
208 nRight = nLeft + rSize.Width() +1;
209 else if ( rSize.Width() > 0 )
210 nRight = nLeft + rSize.Width() -1;
211 else
212 nRight = RECT_EMPTY;
214 if ( rSize.Height() < 0 )
215 nBottom = nTop + rSize.Height() +1;
216 else if ( rSize.Height() > 0 )
217 nBottom = nTop + rSize.Height() -1;
218 else
219 nBottom = RECT_EMPTY;
222 /*************************************************************************
224 |* Rectangle::Union()
226 |* Beschreibung GEN.SDW
227 |* Ersterstellung TH 20.10.92
228 |* Letzte Aenderung MM 21.04.94
230 *************************************************************************/
232 Rectangle& Rectangle::Union( const Rectangle& rRect )
234 if ( rRect.IsEmpty() )
235 return *this;
237 if ( IsEmpty() )
238 *this = rRect;
239 else
241 nLeft = Min( Min( nLeft, rRect.nLeft ), Min( nRight, rRect.nRight ) );
242 nRight = Max( Max( nLeft, rRect.nLeft ), Max( nRight, rRect.nRight ) );
243 nTop = Min( Min( nTop, rRect.nTop ), Min( nBottom, rRect.nBottom ) );
244 nBottom = Max( Max( nTop, rRect.nTop ), Max( nBottom, rRect.nBottom ) );
247 return *this;
250 /*************************************************************************
252 |* Rectangle::Intersection()
254 |* Beschreibung GEN.SDW
255 |* Ersterstellung TH 20.10.92
256 |* Letzte Aenderung MM 21.04.94
258 *************************************************************************/
260 Rectangle& Rectangle::Intersection( const Rectangle& rRect )
262 if ( IsEmpty() )
263 return *this;
264 if ( rRect.IsEmpty() )
266 *this = Rectangle();
267 return *this;
270 // nicht mit umgedrehten Rechtecken arbeiten
271 Rectangle aTmpRect( rRect );
272 Justify();
273 aTmpRect.Justify();
275 // Schnitt bilden
276 nLeft = Max( nLeft, aTmpRect.nLeft );
277 nRight = Min( nRight, aTmpRect.nRight );
278 nTop = Max( nTop, aTmpRect.nTop );
279 nBottom= Min( nBottom, aTmpRect.nBottom );
281 // Feststellen ob Schnitt leer
282 if ( nRight < nLeft || nBottom < nTop )
283 *this = Rectangle();
285 return *this;
288 /*************************************************************************
290 |* Rectangle::Justify()
292 |* Beschreibung GEN.SDW
293 |* Ersterstellung DV 07.03.91
294 |* Letzte Aenderung DV 07.03.91
296 *************************************************************************/
298 void Rectangle::Justify()
300 long nHelp;
302 // Abfrage, ob Right kleiner Left
303 if ( (nRight < nLeft) && (nRight != RECT_EMPTY) )
305 nHelp = nLeft;
306 nLeft = nRight;
307 nRight = nHelp;
310 // Abfrage, ob Bottom kleiner Top
311 if ( (nBottom < nTop) && (nBottom != RECT_EMPTY) )
313 nHelp = nBottom;
314 nBottom = nTop;
315 nTop = nHelp;
319 /*************************************************************************
321 |* Rectangle::IsInside()
323 |* Beschreibung GEN.SDW
324 |* Ersterstellung TH 19.03.90
325 |* Letzte Aenderung MM 21.04.94
327 *************************************************************************/
329 BOOL Rectangle::IsInside( const Point& rPoint ) const
331 if ( IsEmpty() )
332 return FALSE;
334 BOOL bRet = TRUE;
335 if ( nLeft <= nRight )
337 if ( (rPoint.X() < nLeft) || (rPoint.X() > nRight) )
338 bRet = FALSE;
340 else
342 if ( (rPoint.X() > nLeft) || (rPoint.X() < nRight) )
343 bRet = FALSE;
345 if ( nTop <= nBottom )
347 if ( (rPoint.Y() < nTop) || (rPoint.Y() > nBottom) )
348 bRet = FALSE;
350 else
352 if ( (rPoint.Y() > nTop) || (rPoint.Y() < nBottom) )
353 bRet = FALSE;
355 return bRet;
358 /*************************************************************************
360 |* Rectangle::IsInside()
362 |* Beschreibung GEN.SDW
363 |* Ersterstellung TH 19.03.90
364 |* Letzte Aenderung MM 21.04.94
366 *************************************************************************/
368 BOOL Rectangle::IsInside( const Rectangle& rRect ) const
370 if ( IsInside( rRect.TopLeft() ) && IsInside( rRect.BottomRight() ) )
371 return TRUE;
372 else
373 return FALSE;
376 /*************************************************************************
378 |* Rectangle::IsOver()
380 |* Beschreibung GEN.SDW
381 |* Ersterstellung TH 19.03.90
382 |* Letzte Aenderung MM 21.04.94
384 *************************************************************************/
386 BOOL Rectangle::IsOver( const Rectangle& rRect ) const
388 // Wenn sie sich nicht schneiden, ueberlappen sie auch nicht
389 return !GetIntersection( rRect ).IsEmpty();
392 // =======================================================================
394 SvStream& operator>>( SvStream& rIStream, Rectangle& rRect )
396 DBG_ASSERTWARNING( rIStream.GetVersion(), "Rectangle::>> - Solar-Version not set on rIStream" );
398 if ( rIStream.GetCompressMode() == COMPRESSMODE_FULL )
400 unsigned char cIdAry[2];
401 unsigned char cAry[16];
402 int i;
403 int iLast;
404 int i1;
405 int i2;
406 int i3;
407 int i4;
408 UINT32 nNum;
410 rIStream.Read( cIdAry, 2 );
411 i1 = (cIdAry[0] & 0x70) >> 4;
412 i2 = cIdAry[0] & 0x07;
413 i3 = (cIdAry[1] & 0x70) >> 4;
414 i4 = cIdAry[1] & 0x07;
415 rIStream.Read( cAry, i1+i2+i3+i4 );
417 nNum = 0;
418 i = i1;
419 iLast = i;
420 while ( i )
422 i--;
423 nNum <<= 8;
424 nNum |= cAry[i];
426 iLast = i1;
427 if ( cIdAry[0] & 0x80 )
428 nNum ^= 0xFFFFFFFF;
429 rRect.nLeft = (INT32)nNum;
431 nNum = 0;
432 i = iLast+i2;
433 while ( i > iLast )
435 i--;
436 nNum <<= 8;
437 nNum |= cAry[i];
439 iLast += i2;
440 if ( cIdAry[0] & 0x08 )
441 nNum ^= 0xFFFFFFFF;
442 rRect.nTop = (INT32)nNum;
444 nNum = 0;
445 i = iLast+i3;
446 while ( i > iLast )
448 i--;
449 nNum <<= 8;
450 nNum |= cAry[i];
452 iLast += i3;
453 if ( cIdAry[1] & 0x80 )
454 nNum ^= 0xFFFFFFFF;
455 rRect.nRight = (INT32)nNum;
457 nNum = 0;
458 i = iLast+i4;
459 while ( i > iLast )
461 i--;
462 nNum <<= 8;
463 nNum |= cAry[i];
465 if ( cIdAry[1] & 0x08 )
466 nNum ^= 0xFFFFFFFF;
467 rRect.nBottom = (INT32)nNum;
469 else
471 rIStream >> rRect.nLeft >> rRect.nTop >> rRect.nRight >> rRect.nBottom;
474 return rIStream;
477 // -----------------------------------------------------------------------
479 SvStream& operator<<( SvStream& rOStream, const Rectangle& rRect )
481 DBG_ASSERTWARNING( rOStream.GetVersion(), "Rectangle::<< - Solar-Version not set on rOStream" );
483 if ( rOStream.GetCompressMode() == COMPRESSMODE_FULL )
485 unsigned char cAry[18];
486 int i = 2;
487 UINT32 nNum;
489 cAry[0] = 0;
490 cAry[1] = 0;
492 nNum = (UINT32)(INT32)rRect.nLeft;
493 if ( rRect.nLeft < 0 )
495 cAry[0] |= 0x80;
496 nNum ^= 0xFFFFFFFF;
498 if ( nNum )
500 cAry[i] = (unsigned char)(nNum & 0xFF);
501 nNum >>= 8;
502 i++;
504 if ( nNum )
506 cAry[i] = (unsigned char)(nNum & 0xFF);
507 nNum >>= 8;
508 i++;
510 if ( nNum )
512 cAry[i] = (unsigned char)(nNum & 0xFF);
513 nNum >>= 8;
514 i++;
516 if ( nNum )
518 cAry[i] = (unsigned char)(nNum & 0xFF);
519 nNum >>= 8;
520 i++;
521 cAry[0] |= 0x40;
523 else
524 cAry[0] |= 0x30;
526 else
527 cAry[0] |= 0x20;
529 else
530 cAry[0] |= 0x10;
533 nNum = (UINT32)(INT32)rRect.nTop;
534 if ( rRect.nTop < 0 )
536 cAry[0] |= 0x08;
537 nNum ^= 0xFFFFFFFF;
539 if ( nNum )
541 cAry[i] = (unsigned char)(nNum & 0xFF);
542 nNum >>= 8;
543 i++;
545 if ( nNum )
547 cAry[i] = (unsigned char)(nNum & 0xFF);
548 nNum >>= 8;
549 i++;
551 if ( nNum )
553 cAry[i] = (unsigned char)(nNum & 0xFF);
554 nNum >>= 8;
555 i++;
557 if ( nNum )
559 cAry[i] = (unsigned char)(nNum & 0xFF);
560 nNum >>= 8;
561 i++;
562 cAry[0] |= 0x04;
564 else
565 cAry[0] |= 0x03;
567 else
568 cAry[0] |= 0x02;
570 else
571 cAry[0] |= 0x01;
574 nNum = (UINT32)(INT32)rRect.nRight;
575 if ( rRect.nRight < 0 )
577 cAry[1] |= 0x80;
578 nNum ^= 0xFFFFFFFF;
580 if ( nNum )
582 cAry[i] = (unsigned char)(nNum & 0xFF);
583 nNum >>= 8;
584 i++;
586 if ( nNum )
588 cAry[i] = (unsigned char)(nNum & 0xFF);
589 nNum >>= 8;
590 i++;
592 if ( nNum )
594 cAry[i] = (unsigned char)(nNum & 0xFF);
595 nNum >>= 8;
596 i++;
598 if ( nNum )
600 cAry[i] = (unsigned char)(nNum & 0xFF);
601 nNum >>= 8;
602 i++;
603 cAry[1] |= 0x40;
605 else
606 cAry[1] |= 0x30;
608 else
609 cAry[1] |= 0x20;
611 else
612 cAry[1] |= 0x10;
615 nNum = (UINT32)(INT32)rRect.nBottom;
616 if ( rRect.nBottom < 0 )
618 cAry[1] |= 0x08;
619 nNum ^= 0xFFFFFFFF;
621 if ( nNum )
623 cAry[i] = (unsigned char)(nNum & 0xFF);
624 nNum >>= 8;
625 i++;
627 if ( nNum )
629 cAry[i] = (unsigned char)(nNum & 0xFF);
630 nNum >>= 8;
631 i++;
633 if ( nNum )
635 cAry[i] = (unsigned char)(nNum & 0xFF);
636 nNum >>= 8;
637 i++;
639 if ( nNum )
641 cAry[i] = (unsigned char)(nNum & 0xFF);
642 nNum >>= 8;
643 i++;
644 cAry[1] |= 0x04;
646 else
647 cAry[1] |= 0x03;
649 else
650 cAry[1] |= 0x02;
652 else
653 cAry[1] |= 0x01;
656 rOStream.Write( cAry, i );
658 else
660 rOStream << rRect.nLeft << rRect.nTop << rRect.nRight << rRect.nBottom;
663 return rOStream;