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 "imivctl.hxx"
21 #include <sal/log.hxx>
22 
IcnCursor_Impl(SvxIconChoiceCtrl_Impl * pOwner)23 IcnCursor_Impl::IcnCursor_Impl( SvxIconChoiceCtrl_Impl* pOwner )
24 {
25     pView       = pOwner;
26     pCurEntry   = nullptr;
27     nDeltaWidth = 0;
28     nDeltaHeight= 0;
29     nCols       = 0;
30     nRows       = 0;
31 }
32 
~IcnCursor_Impl()33 IcnCursor_Impl::~IcnCursor_Impl()
34 {
35 }
36 
GetSortListPos(SvxIconChoiceCtrlEntryPtrVec & rList,long nValue,bool bVertical)37 sal_uInt16 IcnCursor_Impl::GetSortListPos( SvxIconChoiceCtrlEntryPtrVec& rList, long nValue,
38     bool bVertical )
39 {
40     sal_uInt16 nCount = rList.size();
41     if( !nCount )
42         return 0;
43 
44     sal_uInt16 nCurPos = 0;
45     long nPrevValue = LONG_MIN;
46     while( nCount )
47     {
48         const tools::Rectangle& rRect = pView->GetEntryBoundRect( rList[nCurPos] );
49         long nCurValue;
50         if( bVertical )
51             nCurValue = rRect.Top();
52         else
53             nCurValue = rRect.Left();
54         if( nValue >= nPrevValue && nValue <= nCurValue )
55             return nCurPos;
56         nPrevValue = nCurValue;
57         nCount--;
58         nCurPos++;
59     }
60     return rList.size();
61 }
62 
ImplCreate()63 void IcnCursor_Impl::ImplCreate()
64 {
65     pView->CheckBoundingRects();
66     DBG_ASSERT(xColumns==nullptr&&xRows==nullptr,"ImplCreate: Not cleared");
67 
68     SetDeltas();
69 
70     xColumns.reset(new IconChoiceMap);
71     xRows.reset(new IconChoiceMap);
72 
73     size_t nCount = pView->maEntries.size();
74     for( size_t nCur = 0; nCur < nCount; nCur++ )
75     {
76         SvxIconChoiceCtrlEntry* pEntry = pView->maEntries[ nCur ].get();
77         // const Rectangle& rRect = pView->GetEntryBoundRect( pEntry );
78         tools::Rectangle rRect( pView->CalcBmpRect( pEntry ) );
79         short nY = static_cast<short>( ((rRect.Top()+rRect.Bottom())/2) / nDeltaHeight );
80         short nX = static_cast<short>( ((rRect.Left()+rRect.Right())/2) / nDeltaWidth );
81 
82         // capture rounding errors
83         if( nY >= nRows )
84             nY = sal::static_int_cast< short >(nRows - 1);
85         if( nX >= nCols )
86             nX = sal::static_int_cast< short >(nCols - 1);
87 
88         SvxIconChoiceCtrlEntryPtrVec& rColEntry = (*xColumns)[nX];
89         sal_uInt16 nIns = GetSortListPos( rColEntry, rRect.Top(), true );
90         rColEntry.insert( rColEntry.begin() + nIns, pEntry );
91 
92         SvxIconChoiceCtrlEntryPtrVec& rRowEntry = (*xRows)[nY];
93         nIns = GetSortListPos( rRowEntry, rRect.Left(), false );
94         rRowEntry.insert( rRowEntry.begin() + nIns, pEntry );
95 
96         pEntry->nX = nX;
97         pEntry->nY = nY;
98     }
99 }
100 
101 
Clear()102 void IcnCursor_Impl::Clear()
103 {
104     if( xColumns )
105     {
106         xColumns.reset();
107         xRows.reset();
108         pCurEntry = nullptr;
109         nDeltaWidth = 0;
110         nDeltaHeight = 0;
111     }
112 }
113 
SearchCol(sal_uInt16 nCol,sal_uInt16 nTop,sal_uInt16 nBottom,bool bDown,bool bSimple)114 SvxIconChoiceCtrlEntry* IcnCursor_Impl::SearchCol(sal_uInt16 nCol, sal_uInt16 nTop, sal_uInt16 nBottom,
115     bool bDown, bool bSimple )
116 {
117     DBG_ASSERT(pCurEntry, "SearchCol: No reference entry");
118     IconChoiceMap::iterator mapIt = xColumns->find( nCol );
119     if ( mapIt == xColumns->end() )
120         return nullptr;
121     SvxIconChoiceCtrlEntryPtrVec const & rList = mapIt->second;
122     const sal_uInt16 nCount = rList.size();
123     if( !nCount )
124         return nullptr;
125 
126     const tools::Rectangle& rRefRect = pView->GetEntryBoundRect(pCurEntry);
127 
128     if( bSimple )
129     {
130         SvxIconChoiceCtrlEntryPtrVec::const_iterator it = std::find( rList.begin(), rList.end(), pCurEntry );
131 
132         assert(it != rList.end()); //Entry not in Col-List
133         if (it == rList.end())
134             return nullptr;
135 
136         if( bDown )
137         {
138             while( ++it != rList.end() )
139             {
140                 SvxIconChoiceCtrlEntry* pEntry = *it;
141                 const tools::Rectangle& rRect = pView->GetEntryBoundRect( pEntry );
142                 if( rRect.Top() > rRefRect.Top() )
143                     return pEntry;
144             }
145             return nullptr;
146         }
147         else
148         {
149             SvxIconChoiceCtrlEntryPtrVec::const_reverse_iterator it2(it);
150             while (it2 != rList.rend())
151             {
152                 SvxIconChoiceCtrlEntry* pEntry = *it2;
153                 const tools::Rectangle& rRect = pView->GetEntryBoundRect( pEntry );
154                 if( rRect.Top() < rRefRect.Top() )
155                     return pEntry;
156                 ++it2;
157             }
158             return nullptr;
159         }
160     }
161 
162     if( nTop > nBottom )
163         std::swap(nTop, nBottom);
164 
165     long nMinDistance = LONG_MAX;
166     SvxIconChoiceCtrlEntry* pResult = nullptr;
167     for( sal_uInt16 nCur = 0; nCur < nCount; nCur++ )
168     {
169         SvxIconChoiceCtrlEntry* pEntry = rList[ nCur ];
170         if( pEntry != pCurEntry )
171         {
172             sal_uInt16 nY = pEntry->nY;
173             if( nY >= nTop && nY <= nBottom )
174             {
175                 const tools::Rectangle& rRect = pView->GetEntryBoundRect( pEntry );
176                 long nDistance = rRect.Top() - rRefRect.Top();
177                 if( nDistance < 0 )
178                     nDistance *= -1;
179                 if( nDistance && nDistance < nMinDistance )
180                 {
181                     nMinDistance = nDistance;
182                     pResult = pEntry;
183                 }
184             }
185         }
186     }
187     return pResult;
188 }
189 
SearchRow(sal_uInt16 nRow,sal_uInt16 nLeft,sal_uInt16 nRight,bool bRight,bool bSimple)190 SvxIconChoiceCtrlEntry* IcnCursor_Impl::SearchRow(sal_uInt16 nRow, sal_uInt16 nLeft, sal_uInt16 nRight,
191     bool bRight, bool bSimple )
192 {
193     DBG_ASSERT(pCurEntry,"SearchRow: No reference entry");
194     IconChoiceMap::iterator mapIt = xRows->find( nRow );
195     if ( mapIt == xRows->end() )
196         return nullptr;
197     SvxIconChoiceCtrlEntryPtrVec const & rList = mapIt->second;
198     const sal_uInt16 nCount = rList.size();
199     if( !nCount )
200         return nullptr;
201 
202     const tools::Rectangle& rRefRect = pView->GetEntryBoundRect(pCurEntry);
203 
204     if( bSimple )
205     {
206         SvxIconChoiceCtrlEntryPtrVec::const_iterator it = std::find( rList.begin(), rList.end(), pCurEntry );
207 
208         assert(it != rList.end()); //Entry not in Row-List
209         if (it == rList.end())
210             return nullptr;
211 
212         if( bRight )
213         {
214             while( ++it != rList.end() )
215             {
216                 SvxIconChoiceCtrlEntry* pEntry = *it;
217                 const tools::Rectangle& rRect = pView->GetEntryBoundRect( pEntry );
218                 if( rRect.Left() > rRefRect.Left() )
219                     return pEntry;
220             }
221             return nullptr;
222         }
223         else
224         {
225             SvxIconChoiceCtrlEntryPtrVec::const_reverse_iterator it2(it);
226             while (it2 != rList.rend())
227             {
228                 SvxIconChoiceCtrlEntry* pEntry = *it2;
229                 const tools::Rectangle& rRect = pView->GetEntryBoundRect( pEntry );
230                 if( rRect.Left() < rRefRect.Left() )
231                     return pEntry;
232                 ++it2;
233             }
234             return nullptr;
235         }
236 
237     }
238     if( nRight < nLeft )
239         std::swap(nRight, nLeft);
240 
241     long nMinDistance = LONG_MAX;
242     SvxIconChoiceCtrlEntry* pResult = nullptr;
243     for( sal_uInt16 nCur = 0; nCur < nCount; nCur++ )
244     {
245         SvxIconChoiceCtrlEntry* pEntry = rList[ nCur ];
246         if( pEntry != pCurEntry )
247         {
248             sal_uInt16 nX = pEntry->nX;
249             if( nX >= nLeft && nX <= nRight )
250             {
251                 const tools::Rectangle& rRect = pView->GetEntryBoundRect( pEntry );
252                 long nDistance = rRect.Left() - rRefRect.Left();
253                 if( nDistance < 0 )
254                     nDistance *= -1;
255                 if( nDistance && nDistance < nMinDistance )
256                 {
257                     nMinDistance = nDistance;
258                     pResult = pEntry;
259                 }
260             }
261         }
262     }
263     return pResult;
264 }
265 
266 
267 /*
268     Searches, starting from the passed value, the next entry to the left/to the
269     right. Example for bRight = sal_True:
270 
271                   c
272                 b c
273               a b c
274             S 1 1 1      ====> search direction
275               a b c
276                 b c
277                   c
278 
279     S : starting position
280     1 : first searched rectangle
281     a,b,c : 2nd, 3rd, 4th searched rectangle
282 */
283 
GoLeftRight(SvxIconChoiceCtrlEntry * pCtrlEntry,bool bRight)284 SvxIconChoiceCtrlEntry* IcnCursor_Impl::GoLeftRight( SvxIconChoiceCtrlEntry* pCtrlEntry, bool bRight )
285 {
286     SvxIconChoiceCtrlEntry* pResult;
287     pCurEntry = pCtrlEntry;
288     Create();
289     sal_uInt16 nY = pCtrlEntry->nY;
290     sal_uInt16 nX = pCtrlEntry->nX;
291     DBG_ASSERT(nY< nRows,"GoLeftRight:Bad column");
292     DBG_ASSERT(nX< nCols,"GoLeftRight:Bad row");
293     // neighbor in same row?
294     if( bRight )
295         pResult = SearchRow(
296             nY, nX, sal::static_int_cast< sal_uInt16 >(nCols-1), true, true );
297     else
298         pResult = SearchRow( nY, 0, nX, false, true );
299     if( pResult )
300         return pResult;
301 
302     long nCurCol = nX;
303 
304     long nColOffs, nLastCol;
305     if( bRight )
306     {
307         nColOffs = 1;
308         nLastCol = nCols;
309     }
310     else
311     {
312         nColOffs = -1;
313         nLastCol = -1;   // 0-1
314     }
315 
316     sal_uInt16 nRowMin = nY;
317     sal_uInt16 nRowMax = nY;
318     do
319     {
320         SvxIconChoiceCtrlEntry* pEntry = SearchCol(static_cast<sal_uInt16>(nCurCol), nRowMin, nRowMax, true, false);
321         if( pEntry )
322             return pEntry;
323         if( nRowMin )
324             nRowMin--;
325         if( nRowMax < (nRows-1))
326             nRowMax++;
327         nCurCol += nColOffs;
328     } while( nCurCol != nLastCol );
329     return nullptr;
330 }
331 
GoPageUpDown(SvxIconChoiceCtrlEntry * pStart,bool bDown)332 SvxIconChoiceCtrlEntry* IcnCursor_Impl::GoPageUpDown( SvxIconChoiceCtrlEntry* pStart, bool bDown)
333 {
334     if( pView->IsAutoArrange() && !(pView->nWinBits & WB_ALIGN_TOP) )
335     {
336         const long nPos = static_cast<long>(pView->GetEntryListPos( pStart ));
337         long nEntriesInView = pView->aOutputSize.Height() / pView->nGridDY;
338         nEntriesInView *=
339             ((pView->aOutputSize.Width()+(pView->nGridDX/2)) / pView->nGridDX );
340         long nNewPos = nPos;
341         if( bDown )
342         {
343             nNewPos += nEntriesInView;
344             if( nNewPos >= static_cast<long>(pView->maEntries.size()) )
345                 nNewPos = pView->maEntries.size() - 1;
346         }
347         else
348         {
349             nNewPos -= nEntriesInView;
350             if( nNewPos < 0 )
351                 nNewPos = 0;
352         }
353         if( nPos != nNewPos )
354             return pView->maEntries[ static_cast<size_t>(nNewPos) ].get();
355         return nullptr;
356     }
357     long nOpt = pView->GetEntryBoundRect( pStart ).Top();
358     if( bDown )
359     {
360         nOpt += pView->aOutputSize.Height();
361         nOpt -= pView->nGridDY;
362     }
363     else
364     {
365         nOpt -= pView->aOutputSize.Height();
366         nOpt += pView->nGridDY;
367     }
368     if( nOpt < 0 )
369         nOpt = 0;
370 
371     long nPrevErr = LONG_MAX;
372 
373     SvxIconChoiceCtrlEntry* pPrev = pStart;
374     SvxIconChoiceCtrlEntry* pNext = GoUpDown( pStart, bDown );
375     while( pNext )
376     {
377         long nCur = pView->GetEntryBoundRect( pNext ).Top();
378         long nErr = nOpt - nCur;
379         if( nErr < 0 )
380             nErr *= -1;
381         if( nErr > nPrevErr )
382             return pPrev;
383         nPrevErr = nErr;
384         pPrev = pNext;
385         pNext = GoUpDown( pNext, bDown );
386     }
387     if( pPrev != pStart )
388         return pPrev;
389     return nullptr;
390 }
391 
GoUpDown(SvxIconChoiceCtrlEntry * pCtrlEntry,bool bDown)392 SvxIconChoiceCtrlEntry* IcnCursor_Impl::GoUpDown( SvxIconChoiceCtrlEntry* pCtrlEntry, bool bDown)
393 {
394     if( pView->IsAutoArrange() && !(pView->nWinBits & WB_ALIGN_TOP) )
395     {
396         sal_uLong nPos = pView->GetEntryListPos( pCtrlEntry );
397         if( bDown && nPos < (pView->maEntries.size() - 1) )
398             return pView->maEntries[ nPos + 1 ].get();
399         else if( !bDown && nPos > 0 )
400             return pView->maEntries[ nPos - 1 ].get();
401         return nullptr;
402     }
403 
404     SvxIconChoiceCtrlEntry* pResult;
405     pCurEntry = pCtrlEntry;
406     Create();
407     sal_uInt16 nY = pCtrlEntry->nY;
408     sal_uInt16 nX = pCtrlEntry->nX;
409     DBG_ASSERT(nY<nRows,"GoUpDown:Bad column");
410     DBG_ASSERT(nX<nCols,"GoUpDown:Bad row");
411 
412     // neighbor in same column?
413     if( bDown )
414         pResult = SearchCol(
415             nX, nY, sal::static_int_cast< sal_uInt16 >(nRows-1), true, true );
416     else
417         pResult = SearchCol( nX, 0, nY, false, true );
418     if( pResult )
419         return pResult;
420 
421     long nCurRow = nY;
422 
423     long nRowOffs, nLastRow;
424     if( bDown )
425     {
426         nRowOffs = 1;
427         nLastRow = nRows;
428     }
429     else
430     {
431         nRowOffs = -1;
432         nLastRow = -1;   // 0-1
433     }
434 
435     sal_uInt16 nColMin = nX;
436     sal_uInt16 nColMax = nX;
437     do
438     {
439         SvxIconChoiceCtrlEntry* pEntry = SearchRow(static_cast<sal_uInt16>(nCurRow), nColMin, nColMax, true, false);
440         if( pEntry )
441             return pEntry;
442         if( nColMin )
443             nColMin--;
444         if( nColMax < (nCols-1))
445             nColMax++;
446         nCurRow += nRowOffs;
447     } while( nCurRow != nLastRow );
448     return nullptr;
449 }
450 
SetDeltas()451 void IcnCursor_Impl::SetDeltas()
452 {
453     const Size& rSize = pView->aVirtOutputSize;
454     nCols = rSize.Width() / pView->nGridDX;
455     if( !nCols )
456         nCols = 1;
457     nRows = rSize.Height() / pView->nGridDY;
458     if( (nRows * pView->nGridDY) < rSize.Height() )
459         nRows++;
460     if( !nRows )
461         nRows = 1;
462 
463     nDeltaWidth = static_cast<short>(rSize.Width() / nCols);
464     nDeltaHeight = static_cast<short>(rSize.Height() / nRows);
465     if( !nDeltaHeight )
466     {
467         nDeltaHeight = 1;
468         SAL_INFO("vcl", "SetDeltas:Bad height");
469     }
470     if( !nDeltaWidth )
471     {
472         nDeltaWidth = 1;
473         SAL_INFO("vcl", "SetDeltas:Bad width");
474     }
475 }
476 
IcnGridMap_Impl(SvxIconChoiceCtrl_Impl * pView)477 IcnGridMap_Impl::IcnGridMap_Impl(SvxIconChoiceCtrl_Impl* pView)
478 {
479     _pView = pView;
480     _nGridCols = 0;
481     _nGridRows = 0;
482 }
483 
~IcnGridMap_Impl()484 IcnGridMap_Impl::~IcnGridMap_Impl()
485 {
486 }
487 
Expand()488 void IcnGridMap_Impl::Expand()
489 {
490     if( !_pGridMap )
491         Create_Impl();
492     else
493     {
494         sal_uInt16 nNewGridRows = _nGridRows;
495         sal_uInt16 nNewGridCols = _nGridCols;
496         if( _pView->nWinBits & WB_ALIGN_TOP )
497             nNewGridRows += 50;
498         else
499             nNewGridCols += 50;
500 
501         size_t nNewCellCount = static_cast<size_t>(nNewGridRows) * nNewGridCols;
502         bool* pNewGridMap = new bool[nNewCellCount];
503         size_t nOldCellCount = static_cast<size_t>(_nGridRows) * _nGridCols;
504         memcpy(pNewGridMap, _pGridMap.get(), nOldCellCount * sizeof(bool));
505         memset(pNewGridMap + nOldCellCount, 0, (nNewCellCount-nOldCellCount) * sizeof(bool));
506         _pGridMap.reset( pNewGridMap );
507         _nGridRows = nNewGridRows;
508         _nGridCols = nNewGridCols;
509     }
510 }
511 
Create_Impl()512 void IcnGridMap_Impl::Create_Impl()
513 {
514     DBG_ASSERT(!_pGridMap,"Unnecessary call to IcnGridMap_Impl::Create_Impl()");
515     if( _pGridMap )
516         return;
517     GetMinMapSize( _nGridCols, _nGridRows );
518     if( _pView->nWinBits & WB_ALIGN_TOP )
519         _nGridRows += 50;  // avoid resize of gridmap too often
520     else
521         _nGridCols += 50;
522 
523     size_t nCellCount = static_cast<size_t>(_nGridRows) * _nGridCols;
524     _pGridMap.reset( new bool[nCellCount] );
525     memset(_pGridMap.get(), 0, nCellCount * sizeof(bool));
526 
527     const size_t nCount = _pView->maEntries.size();
528     for( size_t nCur=0; nCur < nCount; nCur++ )
529         OccupyGrids( _pView->maEntries[ nCur ].get() );
530 }
531 
GetMinMapSize(sal_uInt16 & rDX,sal_uInt16 & rDY) const532 void IcnGridMap_Impl::GetMinMapSize( sal_uInt16& rDX, sal_uInt16& rDY ) const
533 {
534     long nX, nY;
535     if( _pView->nWinBits & WB_ALIGN_TOP )
536     {
537         // The view grows in vertical direction. Its max. width is _pView->nMaxVirtWidth
538         nX = _pView->nMaxVirtWidth;
539         if( !nX )
540             nX = _pView->pView->GetOutputSizePixel().Width();
541         if( !(_pView->nFlags & IconChoiceFlags::Arranging) )
542             nX -= _pView->nVerSBarWidth;
543 
544         nY = _pView->aVirtOutputSize.Height();
545     }
546     else
547     {
548         // The view grows in horizontal direction. Its max. height is _pView->nMaxVirtHeight
549         nY = _pView->nMaxVirtHeight;
550         if( !nY )
551             nY = _pView->pView->GetOutputSizePixel().Height();
552         if( !(_pView->nFlags & IconChoiceFlags::Arranging) )
553             nY -= _pView->nHorSBarHeight;
554         nX = _pView->aVirtOutputSize.Width();
555     }
556 
557     if( !nX )
558         nX = DEFAULT_MAX_VIRT_WIDTH;
559     if( !nY )
560         nY = DEFAULT_MAX_VIRT_HEIGHT;
561 
562     long nDX = nX / _pView->nGridDX;
563     long nDY = nY / _pView->nGridDY;
564 
565     if( !nDX )
566         nDX++;
567     if( !nDY )
568         nDY++;
569 
570     rDX = static_cast<sal_uInt16>(nDX);
571     rDY = static_cast<sal_uInt16>(nDY);
572 }
573 
GetGrid(sal_uInt16 nGridX,sal_uInt16 nGridY)574 GridId IcnGridMap_Impl::GetGrid( sal_uInt16 nGridX, sal_uInt16 nGridY )
575 {
576     Create();
577     if( _pView->nWinBits & WB_ALIGN_TOP )
578         return nGridX + ( static_cast<GridId>(nGridY) * _nGridCols );
579     else
580         return nGridY + ( static_cast<GridId>(nGridX) * _nGridRows );
581 }
582 
GetGrid(const Point & rDocPos)583 GridId IcnGridMap_Impl::GetGrid( const Point& rDocPos )
584 {
585     Create();
586 
587     long nX = rDocPos.X();
588     long nY = rDocPos.Y();
589     nX -= LROFFS_WINBORDER;
590     nY -= TBOFFS_WINBORDER;
591     nX /= _pView->nGridDX;
592     nY /= _pView->nGridDY;
593     if( nX >= _nGridCols )
594     {
595         nX = _nGridCols - 1;
596     }
597     if( nY >= _nGridRows )
598     {
599         nY = _nGridRows - 1;
600     }
601     GridId nId = GetGrid( static_cast<sal_uInt16>(nX), static_cast<sal_uInt16>(nY) );
602     DBG_ASSERT(nId <static_cast<sal_uLong>(_nGridCols*_nGridRows),"GetGrid failed");
603     return nId;
604 }
605 
GetGridRect(GridId nId)606 tools::Rectangle IcnGridMap_Impl::GetGridRect( GridId nId )
607 {
608     Create();
609     sal_uInt16 nGridX, nGridY;
610     GetGridCoord( nId, nGridX, nGridY );
611     const long nLeft = nGridX * _pView->nGridDX+ LROFFS_WINBORDER;
612     const long nTop = nGridY * _pView->nGridDY + TBOFFS_WINBORDER;
613     return tools::Rectangle(
614         nLeft, nTop,
615         nLeft + _pView->nGridDX,
616         nTop + _pView->nGridDY );
617 }
618 
GetUnoccupiedGrid()619 GridId IcnGridMap_Impl::GetUnoccupiedGrid()
620 {
621     Create();
622     sal_uLong nStart = 0;
623     bool bExpanded = false;
624 
625     while( true )
626     {
627         const sal_uLong nCount = static_cast<sal_uInt16>(_nGridCols * _nGridRows);
628         for( sal_uLong nCur = nStart; nCur < nCount; nCur++ )
629         {
630             if( !_pGridMap[ nCur ] )
631             {
632                 _pGridMap[ nCur ] = true;
633                 return static_cast<GridId>(nCur);
634             }
635         }
636         DBG_ASSERT(!bExpanded,"ExpandGrid failed");
637         if( bExpanded )
638             return 0; // prevent never ending loop
639         bExpanded = true;
640         Expand();
641         nStart = nCount;
642     }
643 }
644 
645 // An entry only means that there's a GridRect lying under its center. This
646 // variant is much faster than allocating via the bounding rectangle but can
647 // lead to small overlaps.
OccupyGrids(const SvxIconChoiceCtrlEntry * pEntry)648 void IcnGridMap_Impl::OccupyGrids( const SvxIconChoiceCtrlEntry* pEntry )
649 {
650     if( !_pGridMap || !SvxIconChoiceCtrl_Impl::IsBoundingRectValid( pEntry->aRect ))
651         return;
652     OccupyGrid( GetGrid( pEntry->aRect.Center()) );
653 }
654 
Clear()655 void IcnGridMap_Impl::Clear()
656 {
657     if( _pGridMap )
658     {
659         _pGridMap.reset();
660         _nGridRows = 0;
661         _nGridCols = 0;
662         _aLastOccupiedGrid.SetEmpty();
663     }
664 }
665 
GetGridCount(const Size & rSizePixel,sal_uInt16 nDX,sal_uInt16 nDY)666 sal_uLong IcnGridMap_Impl::GetGridCount( const Size& rSizePixel, sal_uInt16 nDX, sal_uInt16 nDY)
667 {
668     long ndx = (rSizePixel.Width() - LROFFS_WINBORDER) / nDX;
669     if( ndx < 0 ) ndx *= -1;
670     long ndy = (rSizePixel.Height() - TBOFFS_WINBORDER) / nDY;
671     if( ndy < 0 ) ndy *= -1;
672     return static_cast<sal_uLong>(ndx * ndy);
673 }
674 
OutputSizeChanged()675 void IcnGridMap_Impl::OutputSizeChanged()
676 {
677     if( !_pGridMap )
678         return;
679 
680     sal_uInt16 nCols, nRows;
681     GetMinMapSize( nCols, nRows );
682     if( _pView->nWinBits & WB_ALIGN_TOP )
683     {
684         if( nCols != _nGridCols )
685             Clear();
686         else if( nRows >= _nGridRows )
687             Expand();
688     }
689     else
690     {
691         if( nRows != _nGridRows )
692             Clear();
693         else if( nCols >= _nGridCols )
694             Expand();
695     }
696 }
697 
698 // Independently of the view's alignment (TOP or LEFT), the gridmap
699 // should contain the data in a continuous region, to make it possible
700 // to copy the whole block if the gridmap needs to be expanded.
GetGridCoord(GridId nId,sal_uInt16 & rGridX,sal_uInt16 & rGridY)701 void IcnGridMap_Impl::GetGridCoord( GridId nId, sal_uInt16& rGridX, sal_uInt16& rGridY )
702 {
703     Create();
704     if( _pView->nWinBits & WB_ALIGN_TOP )
705     {
706         rGridX = static_cast<sal_uInt16>(nId % _nGridCols);
707         rGridY = static_cast<sal_uInt16>(nId / _nGridCols);
708     }
709     else
710     {
711         rGridX = static_cast<sal_uInt16>(nId / _nGridRows);
712         rGridY = static_cast<sal_uInt16>(nId % _nGridRows);
713     }
714 }
715 
716 
717 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
718