]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/image/image.cpp
fix vertical mouse wheel event rotation value, sign was reversed in r74805
[wxWidgets.git] / samples / image / image.cpp
index 0fac64b19e91085aa29c7a611fa4ca06969b8bc6..3c614c322930476fc0d86852b3f1a715dbdbdbf8 100644 (file)
@@ -4,10 +4,9 @@
 // Author:      Robert Roebling
 // Modified by: Francesco Montorsi
 // Created:     1998
-// RCS-ID:      $Id$
 // Copyright:   (c) 1998-2005 Robert Roebling
 //              (c) 2005-2009 Vadim Zeitlin
-// License:     wxWindows licence
+// Licence:     wxWindows licence
 ///////////////////////////////////////////////////////////////////////////////
 
 // For compilers that support precompilation, includes "wx/wx.h".
 #include "wx/image.h"
 #include "wx/file.h"
 #include "wx/filename.h"
+#include "wx/graphics.h"
 #include "wx/mstream.h"
 #include "wx/wfstream.h"
 #include "wx/quantize.h"
+#include "wx/scopedptr.h"
 #include "wx/stopwatch.h"
+#include "wx/versioninfo.h"
 
 #if wxUSE_CLIPBOARD
     #include "wx/dataobj.h"
@@ -47,7 +49,7 @@
 
 #include "canvas.h"
 
-#ifndef __WXMSW__
+#ifndef wxHAS_IMAGES_IN_RESOURCES
     #include "../sample.xpm"
 #endif
 
@@ -82,6 +84,9 @@ public:
 #ifdef wxHAVE_RAW_BITMAP
     void OnTestRawBitmap( wxCommandEvent &event );
 #endif // wxHAVE_RAW_BITMAP
+#if wxUSE_GRAPHICS_CONTEXT
+    void OnTestGraphics(wxCommandEvent& event);
+#endif // wxUSE_GRAPHICS_CONTEXT
     void OnQuit( wxCommandEvent &event );
 
 #if wxUSE_CLIPBOARD
@@ -144,7 +149,7 @@ private:
         m_zoom = 1.;
 
         wxMenu *menu = new wxMenu;
-        menu->Append(wxID_SAVE);
+        menu->Append(wxID_SAVEAS);
         menu->AppendSeparator();
         menu->AppendCheckItem(ID_PAINT_BG, wxT("&Paint background"),
                               "Uncheck this for transparent images");
@@ -186,7 +191,7 @@ private:
         wxPaintDC dc(this);
 
         if ( GetMenuBar()->IsChecked(ID_PAINT_BG) )
-            ClearBackground();
+            dc.Clear();
 
         dc.SetUserScale(m_zoom, m_zoom);
 
@@ -208,7 +213,7 @@ private:
         wxString savefilename = wxFileSelector( wxT("Save Image"),
                                                 wxEmptyString,
                                                 wxEmptyString,
-                                                (const wxChar *)NULL,
+                                                wxEmptyString,
                                                 wxT("BMP files (*.bmp)|*.bmp|")
 #if wxUSE_LIBPNG
                                                 wxT("PNG files (*.png)|*.png|")
@@ -224,10 +229,13 @@ private:
 #endif
 #if wxUSE_PCX
                                                 wxT("PCX files (*.pcx)|*.pcx|")
+#endif
+#if wxUSE_XPM
+                                                wxT("X PixMap files (*.xpm)|*.xpm|")
 #endif
                                                 wxT("ICO files (*.ico)|*.ico|")
                                                 wxT("CUR files (*.cur)|*.cur"),
-                                                wxFD_SAVE,
+                                                wxFD_SAVE | wxFD_OVERWRITE_PROMPT,
                                                 this);
 
         if ( savefilename.empty() )
@@ -406,7 +414,7 @@ private:
 
         wxImage img(m_bitmap.ConvertToImage());
         img = img.Rotate(angle, wxPoint(img.GetWidth() / 2, img.GetHeight() / 2));
-        if ( !img.Ok() )
+        if ( !img.IsOk() )
         {
             wxLogWarning(wxT("Rotation failed"));
             return;
@@ -584,7 +592,7 @@ BEGIN_EVENT_TABLE(MyImageFrame, wxFrame)
     EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground)
     EVT_PAINT(MyImageFrame::OnPaint)
 
-    EVT_MENU(wxID_SAVE, MyImageFrame::OnSave)
+    EVT_MENU(wxID_SAVEAS, MyImageFrame::OnSave)
     EVT_MENU_RANGE(ID_ROTATE_LEFT, ID_ROTATE_RIGHT, MyImageFrame::OnRotate)
     EVT_MENU(ID_RESIZE, MyImageFrame::OnResize)
 
@@ -616,6 +624,7 @@ enum
     ID_NEW = 100,
     ID_INFO,
     ID_SHOWRAW,
+    ID_GRAPHICS,
     ID_SHOWTHUMBNAIL
 };
 
@@ -629,6 +638,9 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame)
 #ifdef wxHAVE_RAW_BITMAP
     EVT_MENU    (ID_SHOWRAW, MyFrame::OnTestRawBitmap)
 #endif
+#if wxUSE_GRAPHICS_CONTEXT
+    EVT_MENU    (ID_GRAPHICS, MyFrame::OnTestGraphics)
+#endif // wxUSE_GRAPHICS_CONTEXT
 #if wxUSE_CLIPBOARD
     EVT_MENU(wxID_COPY, MyFrame::OnCopy)
     EVT_MENU(wxID_PASTE, MyFrame::OnPaste)
@@ -650,11 +662,15 @@ MyFrame::MyFrame()
     menuImage->AppendSeparator();
     menuImage->Append( ID_SHOWRAW, wxT("Test &raw bitmap...\tCtrl-R"));
 #endif
+#if wxUSE_GRAPHICS_CONTEXT
+    menuImage->AppendSeparator();
+    menuImage->Append(ID_GRAPHICS, "Test &graphics context...\tCtrl-G");
+#endif // wxUSE_GRAPHICS_CONTEXT
     menuImage->AppendSeparator();
     menuImage->Append( ID_SHOWTHUMBNAIL, wxT("Test &thumbnail...\tCtrl-T"),
                         "Test scaling the image during load (try with JPEG)");
     menuImage->AppendSeparator();
-    menuImage->Append( ID_ABOUT, wxT("&About..."));
+    menuImage->Append( ID_ABOUT, wxT("&About\tF1"));
     menuImage->AppendSeparator();
     menuImage->Append( ID_QUIT, wxT("E&xit\tCtrl-Q"));
     menu_bar->Append(menuImage, wxT("&Image"));
@@ -678,6 +694,7 @@ MyFrame::MyFrame()
 
     // 500 width * 2750 height
     m_canvas->SetScrollbars( 10, 10, 50, 275 );
+    m_canvas->SetCursor(wxImage("cursor.png"));
 }
 
 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
@@ -685,11 +702,35 @@ void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
     Close( true );
 }
 
+#if wxUSE_ZLIB && wxUSE_STREAMS
+#include "wx/zstream.h"
+#endif
+
 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
 {
-    (void)wxMessageBox( "wxImage demo\n"
-                        "(c) Robert Roebling 1998-2005"
-                        "(c) Vadim Zeitlin 2005-2009",
+    wxArrayString array;
+
+    array.Add("wxImage demo");
+    array.Add("(c) Robert Roebling 1998-2005");
+    array.Add("(c) Vadim Zeitlin 2005-2009");
+
+    array.Add(wxEmptyString);
+    array.Add("Version of the libraries used:");
+
+#if wxUSE_LIBPNG
+    array.Add(wxPNGHandler::GetLibraryVersionInfo().ToString());
+#endif
+#if wxUSE_LIBJPEG
+    array.Add(wxJPEGHandler::GetLibraryVersionInfo().ToString());
+#endif
+#if wxUSE_LIBTIFF
+    array.Add(wxTIFFHandler::GetLibraryVersionInfo().ToString());
+#endif
+#if wxUSE_ZLIB && wxUSE_STREAMS
+    // zlib is used by libpng
+    array.Add(wxGetZlibVersionInfo().ToString());
+#endif
+    (void)wxMessageBox( wxJoin(array, '\n'),
                         "About wxImage Demo",
                         wxICON_INFORMATION | wxOK );
 }
@@ -770,6 +811,85 @@ void MyFrame::OnTestRawBitmap( wxCommandEvent &WXUNUSED(event) )
 
 #endif // wxHAVE_RAW_BITMAP
 
+#if wxUSE_GRAPHICS_CONTEXT
+
+class MyGraphicsFrame : public wxFrame
+{
+public:
+    enum
+    {
+        WIDTH = 256,
+        HEIGHT = 90
+    };
+
+    MyGraphicsFrame(wxWindow* parent) :
+        wxFrame(parent, wxID_ANY, "Graphics context test"),
+        m_image(WIDTH, HEIGHT, false)
+    {
+        // Create a test image: it has 3 horizontal primary colour bands with
+        // alpha increasing from left to right.
+        m_image.SetAlpha();
+        unsigned char* alpha = m_image.GetAlpha();
+        unsigned char* data = m_image.GetData();
+
+        for ( int y = 0; y < HEIGHT; y++ )
+        {
+            unsigned char r = 0,
+                          g = 0,
+                          b = 0;
+            if ( y < HEIGHT/3 )
+                r = 0xff;
+            else if ( y < (2*HEIGHT)/3 )
+                g = 0xff;
+            else
+                b = 0xff;
+
+            for ( int x = 0; x < WIDTH; x++ )
+            {
+                *alpha++ = x;
+                *data++ = r;
+                *data++ = g;
+                *data++ = b;
+            }
+        }
+
+        m_bitmap = wxBitmap(m_image);
+
+        Connect(wxEVT_PAINT, wxPaintEventHandler(MyGraphicsFrame::OnPaint));
+
+        Show();
+    }
+
+private:
+    void OnPaint(wxPaintEvent& WXUNUSED(event))
+    {
+        wxPaintDC dc(this);
+        wxScopedPtr<wxGraphicsContext> gc(wxGraphicsContext::Create(dc));
+        wxGraphicsBitmap gb(gc->CreateBitmapFromImage(m_image));
+
+        gc->SetFont(*wxNORMAL_FONT, *wxBLACK);
+        gc->DrawText("Bitmap", 0, HEIGHT/2);
+        gc->DrawBitmap(m_bitmap, 0, 0, WIDTH, HEIGHT);
+
+        wxGraphicsFont gf = gc->CreateFont(wxNORMAL_FONT->GetPixelSize().y, "");
+        gc->SetFont(gf);
+        gc->DrawText("Graphics bitmap", 0, (3*HEIGHT)/2);
+        gc->DrawBitmap(gb, 0, HEIGHT, WIDTH, HEIGHT);
+    }
+
+    wxImage m_image;
+    wxBitmap m_bitmap;
+
+    wxDECLARE_NO_COPY_CLASS(MyGraphicsFrame);
+};
+
+void MyFrame::OnTestGraphics(wxCommandEvent& WXUNUSED(event))
+{
+    new MyGraphicsFrame(this);
+}
+
+#endif // wxUSE_GRAPHICS_CONTEXT
+
 #if wxUSE_CLIPBOARD
 
 void MyFrame::OnCopy(wxCommandEvent& WXUNUSED(event))
@@ -826,10 +946,14 @@ void MyFrame::OnThumbnail( wxCommandEvent &WXUNUSED(event) )
         return;
     }
 
+    int origWidth = image.GetOptionInt( wxIMAGE_OPTION_ORIGINAL_WIDTH );
+    int origHeight = image.GetOptionInt( wxIMAGE_OPTION_ORIGINAL_HEIGHT );
+
     const long loadTime = sw.Time();
 
     MyImageFrame * const frame = new MyImageFrame(this, filename, image);
-    wxLogStatus(frame, "Loaded \"%s\" in %ldms", filename, loadTime);
+    wxLogStatus(frame, "Loaded \"%s\" in %ldms; original size was (%d, %d)",
+                filename, loadTime, origWidth, origHeight);
 #else
     wxLogError( wxT("Couldn't create file selector dialog") );
     return;