Applied patch for ArtProvider.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@32769 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robert Roebling
2005-03-12 10:33:00 +00:00
parent ab5ea030d8
commit e53a95bcb1
8 changed files with 100 additions and 48 deletions

View File

@@ -176,11 +176,12 @@ The bitmap if one of registered providers recognizes the ID or wxNullBitmap othe
Same as \helpref{wxArtProvider::GetBitmap}{wxartprovidergetbitmap}, but Same as \helpref{wxArtProvider::GetBitmap}{wxartprovidergetbitmap}, but
return a wxIcon object (or wxNullIcon on failure). return a wxIcon object (or wxNullIcon on failure).
\func{static wxSize}{GetSize}{\param{const wxArtClient\& }{client}, \param{bool }{platform_default = false}} \func{static wxSize}{GetSizeHint}{\param{const wxArtClient\& }{client}, \param{bool }{platform_default = false}}
Returns the default size for the given art {\it client} by either using the topmost Returns a suitable size hint for the given {\it wxArtClient}. If
wxArtProvider or if {\it platform_default} is \true then return a suitable default size for {\it platform_default} is \true, return a size based on the current platform,
{\it client} depending on the current platform. otherwise return the size from the topmost wxArtProvider. {\it wxDefaultSize} may be
returned if the client doesn't have a specified size, like wxART_OTHER for example.
\membersection{wxArtProvider::PopProvider}\label{wxartproviderctor} \membersection{wxArtProvider::PopProvider}\label{wxartproviderctor}

View File

@@ -137,9 +137,9 @@ public:
const wxArtClient& client = wxART_OTHER, const wxArtClient& client = wxART_OTHER,
const wxSize& size = wxDefaultSize); const wxSize& size = wxDefaultSize);
// Get the size of an icon from a specific wxArtClient, queries // Get the size hint of an icon from a specific wxArtClient, queries
// the topmost provider if platform_dependent = false // the topmost provider if platform_dependent = false
static wxSize GetSize(const wxArtClient& client, bool platform_dependent = false); static wxSize GetSizeHint(const wxArtClient& client, bool platform_dependent = false);
protected: protected:
friend class wxArtProviderModule; friend class wxArtProviderModule;
@@ -151,9 +151,9 @@ protected:
static void CleanUpProviders(); static void CleanUpProviders();
// Get the default size of an icon for a specific client // Get the default size of an icon for a specific client
virtual wxSize DoGetSize(const wxArtClient& client) virtual wxSize DoGetSizeHint(const wxArtClient& client)
{ {
return GetSize(client, true); return GetSizeHint(client, true);
} }
// Derived classes must override this method to create requested // Derived classes must override this method to create requested

View File

@@ -146,6 +146,9 @@ wxArtBrowserDialog::wxArtBrowserDialog(wxWindow *parent)
subsizer->Add(m_list, 1, wxEXPAND | wxRIGHT, 10); subsizer->Add(m_list, 1, wxEXPAND | wxRIGHT, 10);
wxSizer *subsub = new wxBoxSizer(wxVERTICAL); wxSizer *subsub = new wxBoxSizer(wxVERTICAL);
m_text = new wxStaticText(this, wxID_ANY, wxT("Size: 333x333"));
subsub->Add(m_text);
m_canvas = new wxStaticBitmap(this, wxID_ANY, wxBitmap(null_xpm)); m_canvas = new wxStaticBitmap(this, wxID_ANY, wxBitmap(null_xpm));
subsub->Add(m_canvas); subsub->Add(m_canvas);
subsub->Add(100, 100); subsub->Add(100, 100);
@@ -173,22 +176,24 @@ void wxArtBrowserDialog::SetArtClient(const wxArtClient& client)
img->Add(wxIcon(null_xpm)); img->Add(wxIcon(null_xpm));
int index = 0; int index = 0;
long sel = m_list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED);
if (sel < 0) sel = 0;
m_list->DeleteAllItems(); m_list->DeleteAllItems();
FillBitmaps(img, m_list, index, client, wxSize(16, 16)); FillBitmaps(img, m_list, index, client, wxSize(16, 16));
m_list->AssignImageList(img, wxIMAGE_LIST_SMALL); m_list->AssignImageList(img, wxIMAGE_LIST_SMALL);
m_list->SetColumnWidth(0, wxLIST_AUTOSIZE); m_list->SetColumnWidth(0, wxLIST_AUTOSIZE);
m_list->SetItemState(sel, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
m_client = client; m_client = client;
SetArtBitmap((const wxChar*)m_list->GetItemData(sel), m_client);
} }
void wxArtBrowserDialog::OnSelectItem(wxListEvent &event) void wxArtBrowserDialog::OnSelectItem(wxListEvent &event)
{ {
const wxChar *data = (const wxChar*)event.GetData(); const wxChar *data = (const wxChar*)event.GetData();
wxBitmap bmp = wxArtProvider::GetBitmap(data, m_client); SetArtBitmap(data, m_client, wxDefaultSize);
m_canvas->SetSize(bmp.GetWidth(), bmp.GetHeight());
m_canvas->SetBitmap(bmp);
Refresh();
} }
void wxArtBrowserDialog::OnChooseClient(wxCommandEvent &event) void wxArtBrowserDialog::OnChooseClient(wxCommandEvent &event)
@@ -196,3 +201,12 @@ void wxArtBrowserDialog::OnChooseClient(wxCommandEvent &event)
const wxChar *data = (const wxChar*)event.GetClientData(); const wxChar *data = (const wxChar*)event.GetClientData();
SetArtClient(data); SetArtClient(data);
} }
void wxArtBrowserDialog::SetArtBitmap(const wxArtID& id, const wxArtClient& client, const wxSize& size)
{
wxBitmap bmp = wxArtProvider::GetBitmap(id, client, size);
m_canvas->SetSize(bmp.GetWidth(), bmp.GetHeight());
m_canvas->SetBitmap(bmp);
m_text->SetLabel(wxString::Format(wxT("Size: %d x %d"), bmp.GetWidth(), bmp.GetHeight()));
Refresh();
}

View File

@@ -29,6 +29,7 @@ public:
wxArtBrowserDialog(wxWindow *parent); wxArtBrowserDialog(wxWindow *parent);
void SetArtClient(const wxArtClient& client); void SetArtClient(const wxArtClient& client);
void SetArtBitmap(const wxArtID& id, const wxArtClient& client, const wxSize& size = wxDefaultSize);
private: private:
void OnSelectItem(wxListEvent &event); void OnSelectItem(wxListEvent &event);
@@ -36,6 +37,7 @@ private:
wxListCtrl *m_list; wxListCtrl *m_list;
wxStaticBitmap *m_canvas; wxStaticBitmap *m_canvas;
wxStaticText *m_text;
wxString m_client; wxString m_client;
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()

View File

@@ -151,16 +151,14 @@ wxArtProviderCache *wxArtProvider::sm_cache = NULL;
/*static*/ wxBitmap wxArtProvider::GetBitmap(const wxArtID& id, /*static*/ wxBitmap wxArtProvider::GetBitmap(const wxArtID& id,
const wxArtClient& client, const wxArtClient& client,
const wxSize& reqSize) const wxSize& size)
{ {
// safety-check against writing client,id,size instead of id,client,size: // safety-check against writing client,id,size instead of id,client,size:
wxASSERT_MSG( client.Last() == _T('C'), _T("invalid 'client' parameter") ); wxASSERT_MSG( client.Last() == _T('C'), _T("invalid 'client' parameter") );
wxCHECK_MSG( sm_providers, wxNullBitmap, _T("no wxArtProvider exists") ); wxCHECK_MSG( sm_providers, wxNullBitmap, _T("no wxArtProvider exists") );
wxSize bestSize = (reqSize != wxDefaultSize) ? reqSize : GetSize(client); wxString hashId = wxArtProviderCache::ConstructHashID(id, client, size);
wxString hashId = wxArtProviderCache::ConstructHashID(id, client, bestSize);
wxBitmap bmp; wxBitmap bmp;
if ( !sm_cache->GetBitmap(hashId, &bmp) ) if ( !sm_cache->GetBitmap(hashId, &bmp) )
@@ -168,25 +166,15 @@ wxArtProviderCache *wxArtProvider::sm_cache = NULL;
for (wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst(); for (wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst();
node; node = node->GetNext()) node; node = node->GetNext())
{ {
bmp = node->GetData()->CreateBitmap(id, client, bestSize); bmp = node->GetData()->CreateBitmap(id, client, size);
if ( bmp.Ok() ) if ( bmp.Ok() )
{ {
#if wxUSE_IMAGE #if wxUSE_IMAGE
int bmp_w = bmp.GetWidth(); if ( size != wxDefaultSize &&
int bmp_h = bmp.GetHeight(); (bmp.GetWidth() != size.x || bmp.GetHeight() != size.y) )
// want default size but it's smaller, paste into transparent image
if ((reqSize == wxDefaultSize) &&
(bmp_h < bestSize.x) && (bmp_w < bestSize.y))
{
wxPoint offset((bestSize.x - bmp_w)/2, (bestSize.y - bmp_h)/2);
wxImage img = bmp.ConvertToImage();
img.Resize(bestSize, offset);
bmp = wxBitmap(img);
}
else if ( (bmp_w != bestSize.x) || (bmp_h != bestSize.y) )
{ {
wxImage img = bmp.ConvertToImage(); wxImage img = bmp.ConvertToImage();
img.Rescale(bestSize.x, bestSize.y); img.Rescale(size.x, size.y);
bmp = wxBitmap(img); bmp = wxBitmap(img);
} }
#endif #endif
@@ -218,36 +206,45 @@ wxArtProviderCache *wxArtProvider::sm_cache = NULL;
#if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__) #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
#include <gtk/gtk.h> #include <gtk/gtk.h>
extern GtkIconSize wxArtClientToIconSize(const wxArtClient& client); extern GtkIconSize wxArtClientToIconSize(const wxArtClient& client);
#endif // __WXGTK__ #endif // defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
/*static*/ wxSize wxArtProvider::GetSize(const wxArtClient& client, /*static*/ wxSize wxArtProvider::GetSizeHint(const wxArtClient& client,
bool platform_dependent) bool platform_dependent)
{ {
if (!platform_dependent) if (!platform_dependent)
{ {
wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst(); wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst();
if (node) if (node)
return node->GetData()->DoGetSize(client); return node->GetData()->DoGetSizeHint(client);
// else return platform dependent size
} }
// else return platform dependent size
#if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__) #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
// Gtk has specific sizes for each client, see artgtk.cpp
GtkIconSize gtk_size = wxArtClientToIconSize(client); GtkIconSize gtk_size = wxArtClientToIconSize(client);
// no size hints for this client
if (gtk_size == GTK_ICON_SIZE_INVALID)
return wxDefaultSize;
gint width, height; gint width, height;
gtk_icon_size_lookup( gtk_size, &width, &height); gtk_icon_size_lookup( gtk_size, &width, &height);
return wxSize(width, height); return wxSize(width, height);
#else // !GTK+ 2 #else // !GTK+ 2
// NB: These size hints may have to be adjusted per platform
if (client == wxART_TOOLBAR) if (client == wxART_TOOLBAR)
return wxSize(32, 32); return wxSize(16, 15);
else if (client == wxART_MENU) else if (client == wxART_MENU)
return wxSize(16, 15); return wxSize(16, 15);
else if (client == wxART_FRAME_ICON)
return wxSize(16, 15);
else if (client == wxART_CMN_DIALOG || client == wxART_MESSAGE_BOX) else if (client == wxART_CMN_DIALOG || client == wxART_MESSAGE_BOX)
return wxSize(32, 32); return wxSize(32, 32);
else if (client == wxART_HELP_BROWSER)
return wxSize(16, 15);
else if (client == wxART_BUTTON) else if (client == wxART_BUTTON)
return wxSize(16, 15); return wxSize(16, 15);
else else // wxART_OTHER or perhaps a user's client, no specified size
return wxSize(16, 15); // this is arbitrary return wxDefaultSize;
#endif // GTK+ 2/else #endif // GTK+ 2/else
} }

View File

@@ -27,6 +27,7 @@
#endif #endif
#include "wx/artprov.h" #include "wx/artprov.h"
#include "wx/image.h"
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxDefaultArtProvider // wxDefaultArtProvider
@@ -143,13 +144,7 @@ protected:
#undef static #undef static
// ---------------------------------------------------------------------------- wxBitmap wxDefaultArtProvider_CreateBitmap(const wxArtID& id)
// CreateBitmap routine
// ----------------------------------------------------------------------------
wxBitmap wxDefaultArtProvider::CreateBitmap(const wxArtID& id,
const wxArtClient& WXUNUSED(client),
const wxSize& WXUNUSED(size))
{ {
// wxMessageBox icons: // wxMessageBox icons:
ART_MSGBOX(wxART_ERROR, wxICON_ERROR, error) ART_MSGBOX(wxART_ERROR, wxICON_ERROR, error)
@@ -195,3 +190,38 @@ wxBitmap wxDefaultArtProvider::CreateBitmap(const wxArtID& id,
return wxNullBitmap; return wxNullBitmap;
} }
// ----------------------------------------------------------------------------
// CreateBitmap routine
// ----------------------------------------------------------------------------
wxBitmap wxDefaultArtProvider::CreateBitmap(const wxArtID& id,
const wxArtClient& client,
const wxSize& reqSize)
{
wxBitmap bmp = wxDefaultArtProvider_CreateBitmap(id);
if (bmp.Ok())
{
// fit into transparent image with desired size hint from the client
if (reqSize == wxDefaultSize)
{
// find out if there is a desired size for this client
wxSize bestSize = GetSizeHint(client);
if (bestSize != wxDefaultSize)
{
int bmp_w = bmp.GetWidth();
int bmp_h = bmp.GetHeight();
// want default size but it's smaller, paste into transparent image
if ((bmp_h < bestSize.x) && (bmp_w < bestSize.y))
{
wxPoint offset((bestSize.x - bmp_w)/2, (bestSize.y - bmp_h)/2);
wxImage img = bmp.ConvertToImage();
img.Resize(bestSize, offset);
bmp = wxBitmap(img);
}
}
}
}
return bmp;
}

View File

@@ -136,7 +136,7 @@ GtkIconSize wxArtClientToIconSize(const wxArtClient& client)
else if (client == wxART_BUTTON) else if (client == wxART_BUTTON)
return GTK_ICON_SIZE_BUTTON; return GTK_ICON_SIZE_BUTTON;
else else
return GTK_ICON_SIZE_BUTTON; // this is arbitrary return GTK_ICON_SIZE_INVALID; // this is arbitrary
} }
static GtkIconSize FindClosestIconSize(const wxSize& size) static GtkIconSize FindClosestIconSize(const wxSize& size)
@@ -245,6 +245,10 @@ wxBitmap wxGTK2ArtProvider::CreateBitmap(const wxArtID& id,
wxArtClientToIconSize(client) : wxArtClientToIconSize(client) :
FindClosestIconSize(size); FindClosestIconSize(size);
// we must have some size, this is arbitrary
if (stocksize == GTK_ICON_SIZE_INVALID)
stocksize = GTK_ICON_SIZE_BUTTON;
// allow passing GTK+ stock IDs to wxArtProvider: // allow passing GTK+ stock IDs to wxArtProvider:
if (!stockid) if (!stockid)
stockid = id.ToAscii(); stockid = id.ToAscii();

View File

@@ -136,7 +136,7 @@ GtkIconSize wxArtClientToIconSize(const wxArtClient& client)
else if (client == wxART_BUTTON) else if (client == wxART_BUTTON)
return GTK_ICON_SIZE_BUTTON; return GTK_ICON_SIZE_BUTTON;
else else
return GTK_ICON_SIZE_BUTTON; // this is arbitrary return GTK_ICON_SIZE_INVALID; // this is arbitrary
} }
static GtkIconSize FindClosestIconSize(const wxSize& size) static GtkIconSize FindClosestIconSize(const wxSize& size)
@@ -245,6 +245,10 @@ wxBitmap wxGTK2ArtProvider::CreateBitmap(const wxArtID& id,
wxArtClientToIconSize(client) : wxArtClientToIconSize(client) :
FindClosestIconSize(size); FindClosestIconSize(size);
// we must have some size, this is arbitrary
if (stocksize == GTK_ICON_SIZE_INVALID)
stocksize = GTK_ICON_SIZE_BUTTON;
// allow passing GTK+ stock IDs to wxArtProvider: // allow passing GTK+ stock IDs to wxArtProvider:
if (!stockid) if (!stockid)
stockid = id.ToAscii(); stockid = id.ToAscii();