From fd94e8aa4563b22e7c66c379625a8373d20720aa Mon Sep 17 00:00:00 2001 From: =?utf8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Sat, 16 Feb 2002 01:45:30 +0000 Subject: [PATCH] A couple of changes to wxImage: 1. changed wxBMP_foo, wxCUR_foo to wxIMAGE_OPTION_{BMP,CUR}_foo (with backward compatiblity macros, of course) 2. applied Chris' patch to update hotspot when scaling an image 3. applied Chris' patch to write a filename in XPM and generalized it to pass wxIMAGE_OPTION_FILENAME to all saving handlers git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@14247 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/latex/wx/image.tex | 12 ++++++------ include/wx/imagbmp.h | 20 +++++++++++++------- include/wx/image.h | 2 ++ samples/image/image.cpp | 12 ++++++------ src/common/imagbmp.cpp | 20 ++++++++++---------- src/common/image.cpp | 13 +++++++++++++ src/common/imagxpm.cpp | 21 +++++++++++++++++---- 7 files changed, 67 insertions(+), 33 deletions(-) diff --git a/docs/latex/wx/image.tex b/docs/latex/wx/image.tex index cb487fc78e..aa5589fcac 100644 --- a/docs/latex/wx/image.tex +++ b/docs/latex/wx/image.tex @@ -145,8 +145,8 @@ initialized with \helpref{wxImage::AddHandler}{wximageaddhandler} or Note: you can use \helpref{GetOptionInt}{wximagegetoptionint} to get the hotspot for loaded cursor file: \begin{verbatim} - int hotspot_x = image.GetOptionInt(wxCUR_HOTSPOT_X); - int hotspot_y = image.GetOptionInt(wxCUR_HOTSPOT_Y); + int hotspot_x = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X); + int hotspot_y = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y); \end{verbatim} @@ -581,8 +581,8 @@ Depending on how wxWindows has been configured, not all formats may be available Note: you can use \helpref{GetOptionInt}{wximagegetoptionint} to get the hotspot for loaded cursor file: \begin{verbatim} - int hotspot_x = image.GetOptionInt(wxCUR_HOTSPOT_X); - int hotspot_y = image.GetOptionInt(wxCUR_HOTSPOT_Y); + int hotspot_x = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X); + int hotspot_y = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y); \end{verbatim} @@ -684,8 +684,8 @@ Note: you can use \helpref{GetOptionInt}{wximagegetoptionint} to set the hotspot before saving an image into a cursor file (default hotspot is in the centre of the image): \begin{verbatim} - image.SetOption(wxCUR_HOTSPOT_X, hotspotX); - image.SetOption(wxCUR_HOTSPOT_Y, hotspotY); + image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, hotspotX); + image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, hotspotY); \end{verbatim} diff --git a/include/wx/imagbmp.h b/include/wx/imagbmp.h index d687121f67..0ba6c31e9f 100644 --- a/include/wx/imagbmp.h +++ b/include/wx/imagbmp.h @@ -17,8 +17,19 @@ #include "wx/image.h" // defines for saving the BMP file in different formats, Bits Per Pixel -// USE: wximage.SetOption( wxBMP_FORMAT, wxBMP_xBPP ); -#define wxBMP_FORMAT wxString(_T("wxBMP_FORMAT")) +// USE: wximage.SetOption( wxIMAGE_OPTION_BMP_FORMAT, wxBMP_xBPP ); +#define wxIMAGE_OPTION_BMP_FORMAT wxString(_T("wxBMP_FORMAT")) + +// These two options are filled in upon reading CUR file and can (should) be +// specified when saving a CUR file - they define the hotspot of the cursor: +#define wxIMAGE_OPTION_CUR_HOTSPOT_X wxT("HotSpotX") +#define wxIMAGE_OPTION_CUR_HOTSPOT_Y wxT("HotSpotY") + +// Do not use these macros, they are deprecated!! : +#define wxBMP_FORMAT wxIMAGE_OPTION_BMP_FORMAT +#define wxCUR_HOTSPOT_X wxIMAGE_OPTION_CUR_HOTSPOT_X +#define wxCUR_HOTSPOT_Y wxIMAGE_OPTION_CUR_HOTSPOT_Y + enum { @@ -100,11 +111,6 @@ private: // wxCURHandler // ---------------------------------------------------------------------------- -// These two options are filled in upon reading CUR file and can (should) be -// specified when saving a CUR file - they define the hotspot of the cursor: -#define wxCUR_HOTSPOT_X wxT("HotSpotX") -#define wxCUR_HOTSPOT_Y wxT("HotSpotY") - class WXDLLEXPORT wxCURHandler : public wxICOHandler { public: diff --git a/include/wx/image.h b/include/wx/image.h index 14530e58bc..cf69aa743a 100644 --- a/include/wx/image.h +++ b/include/wx/image.h @@ -27,6 +27,8 @@ #if wxUSE_IMAGE +#define wxIMAGE_OPTION_FILENAME wxString(_T("FileName")) + //----------------------------------------------------------------------------- // classes //----------------------------------------------------------------------------- diff --git a/samples/image/image.cpp b/samples/image/image.cpp index 57260e265d..c0668f2331 100644 --- a/samples/image/image.cpp +++ b/samples/image/image.cpp @@ -151,7 +151,7 @@ public: return; } - image.SetOption(wxBMP_FORMAT, bppvalues[bppselection]); + image.SetOption(wxIMAGE_OPTION_BMP_FORMAT, bppvalues[bppselection]); wxString deffilename = bppchoices[bppselection]; deffilename.Replace(wxT(" "), wxT("_")); @@ -173,7 +173,7 @@ public: if ( savefilename.empty() ) return; - if ( image.GetOptionInt(wxBMP_FORMAT) == wxBMP_8BPP_PALETTE ) + if ( image.GetOptionInt(wxIMAGE_OPTION_BMP_FORMAT) == wxBMP_8BPP_PALETTE ) { unsigned char *cmap = new unsigned char [256]; for ( int i = 0; i < 256; i++ ) @@ -204,8 +204,8 @@ public: else if (extension == "cur") { image.Rescale(32,32); - image.SetOption(wxCUR_HOTSPOT_X, 0); - image.SetOption(wxCUR_HOTSPOT_Y, 0); + image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, 0); + image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, 0); saved=image.SaveFile(savefilename, wxBITMAP_TYPE_CUR); } else @@ -415,8 +415,8 @@ MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id, else { my_horse_cur = new wxBitmap( image ); - xH = 30 + image.GetOptionInt(wxCUR_HOTSPOT_X) ; - yH = 2420 + image.GetOptionInt(wxCUR_HOTSPOT_Y) ; + xH = 30 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X) ; + yH = 2420 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ; } #endif diff --git a/src/common/imagbmp.cpp b/src/common/imagbmp.cpp index f50bdbc30e..3dcd8b1254 100644 --- a/src/common/imagbmp.cpp +++ b/src/common/imagbmp.cpp @@ -89,8 +89,8 @@ bool wxBMPHandler::SaveDib(wxImage *image, // get the format of the BMP file to save, else use 24bpp unsigned format = wxBMP_24BPP; - if ( image->HasOption(wxBMP_FORMAT) ) - format = image->GetOptionInt(wxBMP_FORMAT); + if ( image->HasOption(wxIMAGE_OPTION_BMP_FORMAT) ) + format = image->GetOptionInt(wxIMAGE_OPTION_BMP_FORMAT); wxUint16 bpp; // # of bits per pixel int palette_size; // # of color map entries, ie. 2^bpp colors @@ -1001,10 +1001,10 @@ bool wxICOHandler::SaveFile(wxImage *image, } // Set the formats for image and mask // (Windows never saves with more than 8 colors): - image->SetOption(wxBMP_FORMAT, wxBMP_8BPP); + image->SetOption(wxIMAGE_OPTION_BMP_FORMAT, wxBMP_8BPP); // monochome bitmap: - mask.SetOption(wxBMP_FORMAT, wxBMP_1BPP_BW); + mask.SetOption(wxIMAGE_OPTION_BMP_FORMAT, wxBMP_1BPP_BW); bool IsBmp = FALSE; bool IsMask = FALSE; @@ -1049,11 +1049,11 @@ bool wxICOHandler::SaveFile(wxImage *image, icondirentry.wBitCount = wxUINT16_SWAP_ON_BE(wxBMP_8BPP); if ( type == 2 /*CUR*/) { - int hx = image->HasOption(wxCUR_HOTSPOT_X) ? - image->GetOptionInt(wxCUR_HOTSPOT_X) : + int hx = image->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X) ? + image->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X) : image->GetWidth() / 2; - int hy = image->HasOption(wxCUR_HOTSPOT_Y) ? - image->GetOptionInt(wxCUR_HOTSPOT_Y) : + int hy = image->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ? + image->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y) : image->GetHeight() / 2; // actually write the values of the hot spot here: @@ -1175,8 +1175,8 @@ bool wxICOHandler::DoLoadFile(wxImage *image, wxInputStream& stream, if ( bResult && bIsCursorType && nType == 2 ) { // it is a cursor, so let's set the hotspot: - image->SetOption(wxCUR_HOTSPOT_X, wxUINT16_SWAP_ON_BE(pCurrentEntry->wPlanes)); - image->SetOption(wxCUR_HOTSPOT_Y, wxUINT16_SWAP_ON_BE(pCurrentEntry->wBitCount)); + image->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, wxUINT16_SWAP_ON_BE(pCurrentEntry->wPlanes)); + image->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, wxUINT16_SWAP_ON_BE(pCurrentEntry->wBitCount)); } } delete[] pIconDirEntry; diff --git a/src/common/image.cpp b/src/common/image.cpp index 48b39581f5..f533e00432 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -274,6 +274,13 @@ wxImage wxImage::Scale( int width, int height ) const } } #endif + // In case this is a cursor, make sure the hotspot is scalled accordingly: + if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X) ) + image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, + (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X)*width)/old_width); + if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ) + image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, + (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y)*height)/old_height); return image; } @@ -933,6 +940,9 @@ bool wxImage::LoadFile( const wxString& filename, const wxString& mimetype, int bool wxImage::SaveFile( const wxString& filename, int type ) const { #if wxUSE_STREAMS + if ( !HasOption(wxIMAGE_OPTION_FILENAME) ) + ((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename); + wxFileOutputStream stream(filename); if ( stream.LastError() == wxStream_NOERROR ) @@ -948,6 +958,9 @@ bool wxImage::SaveFile( const wxString& filename, int type ) const bool wxImage::SaveFile( const wxString& filename, const wxString& mimetype ) const { #if wxUSE_STREAMS + if ( !HasOption(wxIMAGE_OPTION_FILENAME) ) + ((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename); + wxFileOutputStream stream(filename); if ( stream.LastError() == wxStream_NOERROR ) diff --git a/src/common/imagxpm.cpp b/src/common/imagxpm.cpp index 7ebe0c9edf..c6ad6d6aa1 100644 --- a/src/common/imagxpm.cpp +++ b/src/common/imagxpm.cpp @@ -127,13 +127,26 @@ bool wxXPMHandler::SaveFile(wxImage * image, for ( k = MaxCixels; cols > k; k *= MaxCixels) chars_per_pixel++; - // 2. write the header: + // 2. write the header: + wxString sName; + if ( image->HasOption(wxIMAGE_OPTION_FILENAME) ) + { + wxSplitPath(image->GetOption(wxIMAGE_OPTION_FILENAME), + NULL, &sName, NULL); + sName << wxT("_xpm"); + } + + if ( !sName.IsEmpty() ) + sName = wxString(wxT("/* XPM */\nstatic char *")) + sName; + else + sName = wxT("/* XPM */\nstatic char *xpm_data"); + stream.Write(sName.c_str(), sName.Len()); + char tmpbuf[200]; // VS: 200b is safe upper bound for anything produced by sprintf below - // (101 bytes the string, neither %i can expand into more than 10 chars) + // (<101 bytes the string, neither %i can expand into more than 10 chars) sprintf(tmpbuf, - "/* XPM */\n" - "static char *xpm_data[] = {\n" + "[] = {\n" "/* columns rows colors chars-per-pixel */\n" "\"%i %i %i %i\",\n", image->GetWidth(), image->GetHeight(), cols, chars_per_pixel); -- 2.45.2