1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/log.hxx>
21 #include <tools/debug.hxx>
22 #include <vcl/image.hxx>
23 #include "ImageList.hxx"
25 ImageList::ImageList()
29 ImageList::ImageList(const std::vector
< OUString
>& rNameVector
,
30 const OUString
& rPrefix
)
32 SAL_INFO( "vcl", "vcl: ImageList::ImageList(const vector< OUString >& ..." );
34 maImages
.reserve( rNameVector
.size() );
37 for( size_t i
= 0; i
< rNameVector
.size(); ++i
)
38 ImplAddImage( rPrefix
, rNameVector
[ i
], static_cast< sal_uInt16
>( i
) + 1, Image() );
41 // FIXME: Rather a performance hazard
42 BitmapEx
ImageList::GetAsHorizontalStrip() const
44 sal_uInt16 nCount
= maImages
.size();
48 BitmapEx aTempl
= maImages
[ 0 ]->maImage
.GetBitmapEx();
49 Size
aImageSize(aTempl
.GetSizePixel());
50 Size
aSize(aImageSize
.Width() * nCount
, aImageSize
.Height());
51 BitmapEx
aResult( aTempl
, Point(), aSize
);
53 tools::Rectangle
aSrcRect( Point( 0, 0 ), aImageSize
);
54 for (sal_uInt16 nIdx
= 0; nIdx
< nCount
; nIdx
++)
56 tools::Rectangle
aDestRect( Point( nIdx
* aImageSize
.Width(), 0 ), aImageSize
);
57 ImageAryData
*pData
= maImages
[ nIdx
].get();
58 BitmapEx aTmp
= pData
->maImage
.GetBitmapEx();
59 aResult
.CopyPixel( aDestRect
, aSrcRect
, aTmp
);
65 void ImageList::InsertFromHorizontalStrip( const BitmapEx
&rBitmapEx
,
66 const std::vector
< OUString
> &rNameVector
)
68 sal_uInt16 nItems
= sal::static_int_cast
< sal_uInt16
>( rNameVector
.size() );
73 Size
aSize( rBitmapEx
.GetSizePixel() );
74 DBG_ASSERT (rBitmapEx
.GetSizePixel().Width() % nItems
== 0,
75 "ImageList::InsertFromHorizontalStrip - very odd size");
76 aSize
.setWidth( aSize
.Width() / nItems
);
79 maImages
.reserve( nItems
);
82 for (sal_uInt16 nIdx
= 0; nIdx
< nItems
; nIdx
++)
84 BitmapEx
aBitmap( rBitmapEx
, Point( nIdx
* aSize
.Width(), 0 ), aSize
);
85 ImplAddImage( maPrefix
, rNameVector
[ nIdx
], nIdx
+ 1, Image( aBitmap
) );
89 sal_uInt16
ImageList::ImplGetImageId( const OUString
& rImageName
) const
91 auto it
= maNameHash
.find( rImageName
);
92 if (it
== maNameHash
.end())
94 return it
->second
->mnId
;
97 void ImageList::AddImage( const OUString
& rImageName
, const Image
& rImage
)
99 SAL_WARN_IF( GetImagePos( rImageName
) != IMAGELIST_IMAGE_NOTFOUND
, "vcl", "ImageList::AddImage() - ImageName already exists" );
101 ImplAddImage( maPrefix
, rImageName
, GetImageCount() + 1, rImage
);
104 void ImageList::ReplaceImage( const OUString
& rImageName
, const Image
& rImage
)
106 const sal_uInt16 nId
= ImplGetImageId( rImageName
);
110 // Just replace the bitmap rather than doing RemoveImage / AddImage
111 // which breaks index-based iteration.
112 ImageAryData
*pImg
= maNameHash
[ rImageName
];
113 pImg
->maImage
= rImage
;
117 void ImageList::RemoveImage( sal_uInt16 nId
)
119 for( size_t i
= 0; i
< maImages
.size(); ++i
)
121 if( maImages
[ i
]->mnId
== nId
)
123 ImplRemoveImage( static_cast< sal_uInt16
>( i
) );
129 Image
ImageList::GetImage( const OUString
& rImageName
) const
131 auto it
= maNameHash
.find( rImageName
);
132 if (it
== maNameHash
.end())
134 return it
->second
->maImage
;
137 sal_uInt16
ImageList::GetImageCount() const
139 return static_cast< sal_uInt16
>( maImages
.size() );
142 sal_uInt16
ImageList::GetImagePos( std::u16string_view rImageName
) const
144 if( !rImageName
.empty() )
146 for( size_t i
= 0; i
< maImages
.size(); i
++ )
148 if (maImages
[i
]->maName
== rImageName
)
149 return static_cast< sal_uInt16
>( i
);
153 return IMAGELIST_IMAGE_NOTFOUND
;
156 sal_uInt16
ImageList::GetImageId( sal_uInt16 nPos
) const
158 return maImages
[ nPos
]->mnId
;
161 const OUString
& ImageList::GetImageName( sal_uInt16 nPos
) const
163 return maImages
[ nPos
]->maName
;
166 void ImageList::GetImageNames( std::vector
< OUString
>& rNames
) const
168 SAL_INFO( "vcl", "vcl: ImageList::GetImageNames" );
170 rNames
= std::vector
< OUString
>();
172 for(auto const & pImage
: maImages
)
174 const OUString
& rName( pImage
->maName
);
175 if( !rName
.isEmpty())
176 rNames
.push_back( rName
);
180 void ImageList::ImplAddImage( std::u16string_view aPrefix
, const OUString
&aName
,
181 sal_uInt16 nId
, const Image
&aImage
)
183 Image aInsert
= aImage
;
185 aInsert
= Image( OUString::Concat("private:graphicrepository/") + aPrefix
+ aName
);
187 ImageAryData
*pImg
= new ImageAryData
{ aName
, nId
, aInsert
};
188 maImages
.emplace_back( pImg
);
189 if( !aName
.isEmpty() )
190 maNameHash
[ aName
] = pImg
;
193 void ImageList::ImplRemoveImage( sal_uInt16 nPos
)
195 ImageAryData
*pImg
= maImages
[ nPos
].get();
196 if( !pImg
->maName
.isEmpty() )
197 maNameHash
.erase( pImg
->maName
);
198 maImages
.erase( maImages
.begin() + nPos
);
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */