Update wxTreeCtrl and its sample to work with wxBitmapBundle
Override OnImagesChanged() to call UpdateImageListIfNecessary() even in wxGenericTreeCtrl for now, although in the future it would really make sense to stop using wxImageList in its implementation and just use wxBitmapBundle directly instead. wxMSW is the only one which really needs an image list, as it's required by the native control. Also update the sample, even though it doesn't look very nice because its icons are only available in a single size, so we have to always scale them.
This commit is contained in:
@@ -688,7 +688,7 @@ void MyFrame::OnSetImageSize(wxCommandEvent& WXUNUSED(event))
|
||||
if ( size == -1 )
|
||||
return;
|
||||
|
||||
m_treeCtrl->CreateImageList(size);
|
||||
m_treeCtrl->CreateImages(size);
|
||||
wxGetApp().SetShowImages(true);
|
||||
}
|
||||
|
||||
@@ -696,12 +696,12 @@ void MyFrame::OnToggleImages(wxCommandEvent& WXUNUSED(event))
|
||||
{
|
||||
if ( wxGetApp().ShowImages() )
|
||||
{
|
||||
m_treeCtrl->CreateImageList(-1);
|
||||
m_treeCtrl->CreateImages(-1);
|
||||
wxGetApp().SetShowImages(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_treeCtrl->CreateImageList(0);
|
||||
m_treeCtrl->CreateImages(0);
|
||||
wxGetApp().SetShowImages(true);
|
||||
}
|
||||
}
|
||||
@@ -730,7 +730,7 @@ void MyFrame::OnToggleAlternateImages(wxCommandEvent& WXUNUSED(event))
|
||||
bool alternateImages = m_treeCtrl->AlternateImages();
|
||||
|
||||
m_treeCtrl->SetAlternateImages(!alternateImages);
|
||||
m_treeCtrl->CreateImageList(0);
|
||||
m_treeCtrl->CreateImages(0);
|
||||
}
|
||||
|
||||
void MyFrame::OnToggleAlternateStates(wxCommandEvent& WXUNUSED(event))
|
||||
@@ -955,14 +955,14 @@ MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id,
|
||||
{
|
||||
m_reverseSort = false;
|
||||
|
||||
CreateImageList();
|
||||
CreateImages(16);
|
||||
CreateStateImageList();
|
||||
|
||||
// Add some items to the tree
|
||||
AddTestItemsToTree(NUM_CHILDREN_PER_LEVEL, NUM_LEVELS);
|
||||
}
|
||||
|
||||
void MyTreeCtrl::CreateImageList(int size)
|
||||
void MyTreeCtrl::CreateImages(int size)
|
||||
{
|
||||
if ( size == -1 )
|
||||
{
|
||||
@@ -974,8 +974,7 @@ void MyTreeCtrl::CreateImageList(int size)
|
||||
else
|
||||
m_imageSize = size;
|
||||
|
||||
// Make an image list containing small icons
|
||||
wxImageList *images = new wxImageList(size, size, true);
|
||||
const wxSize iconSize(size, size);
|
||||
|
||||
// should correspond to TreeCtrlIcon_xxx enum
|
||||
wxIcon icons[5];
|
||||
@@ -990,8 +989,6 @@ void MyTreeCtrl::CreateImageList(int size)
|
||||
}
|
||||
else
|
||||
{
|
||||
wxSize iconSize(size, size);
|
||||
|
||||
icons[TreeCtrlIcon_File] =
|
||||
icons[TreeCtrlIcon_FileSelected] = wxArtProvider::GetIcon(wxART_NORMAL_FILE, wxART_LIST, iconSize);
|
||||
icons[TreeCtrlIcon_Folder] =
|
||||
@@ -999,20 +996,50 @@ void MyTreeCtrl::CreateImageList(int size)
|
||||
icons[TreeCtrlIcon_FolderOpened] = wxArtProvider::GetIcon(wxART_FOLDER, wxART_LIST, iconSize);
|
||||
}
|
||||
|
||||
// Make a vector of bundles corresponding to the icons. We use a custom
|
||||
// bundle implementation here as we always scale the icons, even at 100%
|
||||
// DPI, to ensure they are of the desired size.
|
||||
wxVector<wxBitmapBundle> images;
|
||||
|
||||
class FixedSizeImpl : public wxBitmapBundleImpl
|
||||
{
|
||||
public:
|
||||
FixedSizeImpl(const wxSize& sizeDef, const wxIcon& icon)
|
||||
: m_sizeDef(sizeDef),
|
||||
m_icon(icon)
|
||||
{
|
||||
}
|
||||
|
||||
wxSize GetDefaultSize() const wxOVERRIDE
|
||||
{
|
||||
return m_sizeDef;
|
||||
}
|
||||
|
||||
wxSize GetPreferredSizeAtScale(double scale) const wxOVERRIDE
|
||||
{
|
||||
return m_sizeDef*scale;
|
||||
}
|
||||
|
||||
wxBitmap GetBitmap(const wxSize& size) wxOVERRIDE
|
||||
{
|
||||
wxBitmap bmp(m_icon);
|
||||
if ( size != bmp.GetSize() )
|
||||
wxBitmap::Rescale(bmp, size);
|
||||
|
||||
return bmp;
|
||||
}
|
||||
|
||||
private:
|
||||
const wxSize m_sizeDef;
|
||||
const wxIcon m_icon;
|
||||
};
|
||||
|
||||
for ( size_t i = 0; i < WXSIZEOF(icons); i++ )
|
||||
{
|
||||
int sizeOrig = icons[0].GetWidth();
|
||||
if ( size == sizeOrig )
|
||||
{
|
||||
images->Add(icons[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
images->Add(wxBitmap(wxBitmap(icons[i]).ConvertToImage().Rescale(size, size)));
|
||||
}
|
||||
images.push_back(wxBitmapBundle::FromImpl(new FixedSizeImpl(iconSize, icons[i])));
|
||||
}
|
||||
|
||||
AssignImageList(images);
|
||||
SetImages(images);
|
||||
}
|
||||
|
||||
void MyTreeCtrl::CreateStateImageList(bool del)
|
||||
|
||||
Reference in New Issue
Block a user