From 49bf4e3e689c2131994aa2aab3c9eca6255a46eb Mon Sep 17 00:00:00 2001 From: Julian Smart Date: Sun, 27 Feb 2005 10:36:58 +0000 Subject: [PATCH] Added GetBitmap, GetIcon to wxImageList git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@32400 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/generic/imaglist.h | 5 ++- include/wx/msw/imaglist.h | 6 ++++ src/cocoa/notebook.mm | 4 +-- src/generic/imaglist.cpp | 26 +++++++++++++++- src/gtk/notebook.cpp | 4 +-- src/gtk/treegtk.cpp | 2 +- src/gtk1/notebook.cpp | 4 +-- src/gtk1/treegtk.cpp | 2 +- src/mac/carbon/notebmac.cpp | 2 +- src/mac/classic/notebmac.cpp | 2 +- src/msw/imaglist.cpp | 57 +++++++++++++++++++++++++++++++++++ src/univ/notebook.cpp | 2 +- 12 files changed, 103 insertions(+), 13 deletions(-) diff --git a/include/wx/generic/imaglist.h b/include/wx/generic/imaglist.h index 692302e433..11e51fbe9d 100644 --- a/include/wx/generic/imaglist.h +++ b/include/wx/generic/imaglist.h @@ -68,7 +68,8 @@ public: int Add( const wxBitmap& bitmap ); int Add( const wxBitmap& bitmap, const wxBitmap& mask ); int Add( const wxBitmap& bitmap, const wxColour& maskColour ); - const wxBitmap *GetBitmap(int index) const; + wxBitmap GetBitmap(int index) const; + wxIcon GetIcon(int index) const; bool Replace( int index, const wxBitmap &bitmap ); bool Remove( int index ); bool RemoveAll(); @@ -77,6 +78,8 @@ public: int flags = wxIMAGELIST_DRAW_NORMAL, bool solidBackground = false); + // Internal use only + const wxBitmap *GetBitmapPtr(int index) const; private: wxList m_images; diff --git a/include/wx/msw/imaglist.h b/include/wx/msw/imaglist.h index dce0618369..1f66f8c6ca 100644 --- a/include/wx/msw/imaglist.h +++ b/include/wx/msw/imaglist.h @@ -130,6 +130,12 @@ public: int flags = wxIMAGELIST_DRAW_NORMAL, bool solidBackground = false); + // Get a bitmap + wxBitmap GetBitmap(int index) const; + + // Get an icon + wxIcon GetIcon(int index) const; + // TODO: miscellaneous functionality /* wxIcon *MakeIcon(int index); diff --git a/src/cocoa/notebook.mm b/src/cocoa/notebook.mm index 12863c1b90..33687e9f0b 100644 --- a/src/cocoa/notebook.mm +++ b/src/cocoa/notebook.mm @@ -202,7 +202,7 @@ bool wxNotebook::InsertPage( size_t pos, m_pages.Insert(page,pos); NSTabViewItem *tvitem = [[WXCTabViewImageItem alloc] initWithIdentifier:nil]; [tvitem setLabel: wxNSStringWithWxString(title)]; - const wxBitmap *bmp = (imageId!=-1)?m_imageList->GetBitmap(imageId):NULL; + const wxBitmap *bmp = (imageId!=-1)?m_imageList->GetBitmapPtr(imageId):NULL; if(bmp) [(WXCTabViewImageItem*) tvitem setImage: bmp->GetNSImage(true)]; @@ -250,7 +250,7 @@ int wxNotebook::GetPageImage(size_t nPage) const bool wxNotebook::SetPageImage(size_t nPage, int nImage) { - const wxBitmap *bmp = nImage!=-1?m_imageList->GetBitmap(nImage):NULL; + const wxBitmap *bmp = nImage!=-1?m_imageList->GetBitmapPtr(nImage):NULL; if(!bmp) return false; NSTabViewItem *tvitem = [GetNSTabView() tabViewItemAtIndex: nPage]; diff --git a/src/generic/imaglist.cpp b/src/generic/imaglist.cpp index 54e53a6200..f1ab5376d5 100644 --- a/src/generic/imaglist.cpp +++ b/src/generic/imaglist.cpp @@ -107,7 +107,7 @@ int wxGenericImageList::Add( const wxBitmap& bitmap, const wxColour& maskColour return Add(wxBitmap(img)); } -const wxBitmap *wxGenericImageList::GetBitmap( int index ) const +const wxBitmap *wxGenericImageList::GetBitmapPtr( int index ) const { wxList::compatibility_iterator node = m_images.Item( index ); @@ -116,6 +116,30 @@ const wxBitmap *wxGenericImageList::GetBitmap( int index ) const return (wxBitmap*)node->GetData(); } +// Get the bitmap +wxBitmap wxImageList::GetBitmap(int index) const +{ + const wxBitmap* bmp = GetBitmapPtr(index); + if (bmp) + return *bmp; + else + return wxNullBitmap; +} + +// Get the icon +wxIcon wxImageList::GetIcon(int index) const +{ + const wxBitmap* bmp = GetBitmapPtr(index); + if (bmp) + { + wxIcon icon; + icon.CopyFromBitmap(*bmp); + return icon; + } + else + return wxNullIcon; +} + bool wxGenericImageList::Replace( int index, const wxBitmap &bitmap ) { wxList::compatibility_iterator node = m_images.Item( index ); diff --git a/src/gtk/notebook.cpp b/src/gtk/notebook.cpp index 75a64b0233..d58e2fe172 100644 --- a/src/gtk/notebook.cpp +++ b/src/gtk/notebook.cpp @@ -499,7 +499,7 @@ bool wxNotebook::SetPageImage( size_t page, int image ) wxASSERT( m_imageList != NULL ); /* Just in case */ /* Construct the new pixmap */ - const wxBitmap *bmp = m_imageList->GetBitmap(image); + const wxBitmap *bmp = m_imageList->GetBitmapPtr(image); GdkPixmap *pixmap = bmp->GetPixmap(); GdkBitmap *mask = (GdkBitmap*) NULL; if ( bmp->GetMask() ) @@ -673,7 +673,7 @@ bool wxNotebook::InsertPage( size_t position, { wxASSERT( m_imageList != NULL ); - const wxBitmap *bmp = m_imageList->GetBitmap(imageId); + const wxBitmap *bmp = m_imageList->GetBitmapPtr(imageId); GdkPixmap *pixmap = bmp->GetPixmap(); GdkBitmap *mask = (GdkBitmap*) NULL; if ( bmp->GetMask() ) diff --git a/src/gtk/treegtk.cpp b/src/gtk/treegtk.cpp index 8c2dd7b4a4..0e2ad4c3c8 100644 --- a/src/gtk/treegtk.cpp +++ b/src/gtk/treegtk.cpp @@ -496,7 +496,7 @@ printf("begin insert\n"); const wxBitmap *bmp; const wxImageList *list; if ((list = GetImageList(wxIMAGE_LIST_NORMAL)) != NULL) - if ((bmp = list->GetBitmap(image)) != NULL) + if ((bmp = list->GetBitmapPtr(image)) != NULL) if (bmp->Ok()) { GdkBitmap *mask = NULL; if (bmp->GetMask()) diff --git a/src/gtk1/notebook.cpp b/src/gtk1/notebook.cpp index 75a64b0233..d58e2fe172 100644 --- a/src/gtk1/notebook.cpp +++ b/src/gtk1/notebook.cpp @@ -499,7 +499,7 @@ bool wxNotebook::SetPageImage( size_t page, int image ) wxASSERT( m_imageList != NULL ); /* Just in case */ /* Construct the new pixmap */ - const wxBitmap *bmp = m_imageList->GetBitmap(image); + const wxBitmap *bmp = m_imageList->GetBitmapPtr(image); GdkPixmap *pixmap = bmp->GetPixmap(); GdkBitmap *mask = (GdkBitmap*) NULL; if ( bmp->GetMask() ) @@ -673,7 +673,7 @@ bool wxNotebook::InsertPage( size_t position, { wxASSERT( m_imageList != NULL ); - const wxBitmap *bmp = m_imageList->GetBitmap(imageId); + const wxBitmap *bmp = m_imageList->GetBitmapPtr(imageId); GdkPixmap *pixmap = bmp->GetPixmap(); GdkBitmap *mask = (GdkBitmap*) NULL; if ( bmp->GetMask() ) diff --git a/src/gtk1/treegtk.cpp b/src/gtk1/treegtk.cpp index 8c2dd7b4a4..0e2ad4c3c8 100644 --- a/src/gtk1/treegtk.cpp +++ b/src/gtk1/treegtk.cpp @@ -496,7 +496,7 @@ printf("begin insert\n"); const wxBitmap *bmp; const wxImageList *list; if ((list = GetImageList(wxIMAGE_LIST_NORMAL)) != NULL) - if ((bmp = list->GetBitmap(image)) != NULL) + if ((bmp = list->GetBitmapPtr(image)) != NULL) if (bmp->Ok()) { GdkBitmap *mask = NULL; if (bmp->GetMask()) diff --git a/src/mac/carbon/notebmac.cpp b/src/mac/carbon/notebmac.cpp index e382c62449..aedbc3ccf1 100644 --- a/src/mac/carbon/notebmac.cpp +++ b/src/mac/carbon/notebmac.cpp @@ -351,7 +351,7 @@ void wxNotebook::MacSetupTabs() if ( GetImageList() && GetPageImage(ii) >= 0 && UMAGetSystemVersion() >= 0x1020 ) { - const wxBitmap* bmap = GetImageList()->GetBitmap( GetPageImage(ii ) ) ; + const wxBitmap* bmap = GetImageList()->GetBitmapPtr( GetPageImage(ii ) ) ; if ( bmap ) { ControlButtonContentInfo info ; diff --git a/src/mac/classic/notebmac.cpp b/src/mac/classic/notebmac.cpp index a553ef66f1..b7664be30d 100644 --- a/src/mac/classic/notebmac.cpp +++ b/src/mac/classic/notebmac.cpp @@ -457,7 +457,7 @@ void wxNotebook::MacSetupTabs() // afterwards Unregister it (IconRef is ref counted, so it will stay on the tab even if we // unregister it) in case this will ever lead to having the same icon everywhere add some kind // of static counter - const wxBitmap* bmap = GetImageList()->GetBitmap( GetPageImage(ii ) ) ; + const wxBitmap* bmap = GetImageList()->GetBitmapPtr( GetPageImage(ii ) ) ; if ( bmap ) { wxBitmap scaledBitmap ; diff --git a/src/msw/imaglist.cpp b/src/msw/imaglist.cpp index ca19520082..785a18c637 100644 --- a/src/msw/imaglist.cpp +++ b/src/msw/imaglist.cpp @@ -42,6 +42,7 @@ #include "wx/log.h" #include "wx/intl.h" +#include "wx/image.h" #include "wx/msw/imaglist.h" #include "wx/msw/private.h" @@ -293,6 +294,62 @@ bool wxImageList::Draw(int index, return ok; } +// Get the bitmap +wxBitmap wxImageList::GetBitmap(int index) const +{ + int bmp_width = 0, bmp_height = 0; + GetSize(index, bmp_width, bmp_height); + + wxBitmap bitmap(bmp_width, bmp_height); + wxMemoryDC dc; + dc.SelectObject(bitmap); + + // draw it the first time to find a suitable mask colour + ((wxImageList*)this)->Draw(index, dc, 0, 0, wxIMAGELIST_DRAW_TRANSPARENT); + dc.SelectObject(wxNullBitmap); + + // find the suitable mask colour + wxImage image = bitmap.ConvertToImage(); + unsigned char r = 0, g = 0, b = 0; + image.FindFirstUnusedColour(&r, &g, &b); + + // redraw whole image and bitmap in the mask colour + image.Create(bmp_width, bmp_height); + image.Replace(0, 0, 0, r, g, b); + bitmap = wxBitmap(image); + + // redraw icon over the mask colour to actually draw it + dc.SelectObject(bitmap); + ((wxImageList*)this)->Draw(index, dc, 0, 0, wxIMAGELIST_DRAW_TRANSPARENT); + dc.SelectObject(wxNullBitmap); + + // get the image, set the mask colour and convert back to get transparent bitmap + image = bitmap.ConvertToImage(); + image.SetMaskColour(r, g, b); + bitmap = wxBitmap(image); + + return bitmap; +} + +// Get the icon +wxIcon wxImageList::GetIcon(int index) const +{ + HICON hIcon = ImageList_ExtractIcon(0, GetHImageList(), index); + if (hIcon) + { + wxIcon icon; + icon.SetHICON((WXHICON)hIcon); + + int iconW, iconH; + GetSize(index, iconW, iconH); + icon.SetSize(iconW, iconH); + + return icon; + } + else + return wxNullIcon; +} + // ---------------------------------------------------------------------------- // helpers // ---------------------------------------------------------------------------- diff --git a/src/univ/notebook.cpp b/src/univ/notebook.cpp index 2b9e19f9ff..8326d62016 100644 --- a/src/univ/notebook.cpp +++ b/src/univ/notebook.cpp @@ -473,7 +473,7 @@ void wxNotebook::DoDrawTab(wxDC& dc, const wxRect& rect, size_t n) m_imageList->Draw(image, dc, 0, 0, wxIMAGELIST_DRAW_NORMAL, true); dc.SelectObject(wxNullBitmap); #else - bmp = *m_imageList->GetBitmap(image); + bmp = m_imageList->GetBitmap(image); #endif } -- 2.45.2