]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/shaped/shaped.cpp
Change version to 3.0.0.
[wxWidgets.git] / samples / shaped / shaped.cpp
index 7a371fbdda3bede4f88b6cae12b3a82f596f6197..cc357408ed10a2148182831fcc3890cdcf810350 100644 (file)
@@ -4,7 +4,6 @@
 // Author:      Robin Dunn
 // Modified by:
 // Created:     28-Mar-2003
-// RCS-ID:      $Id$
 // Copyright:   (c) Robin Dunn
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 #endif
 
 #include "wx/dcclient.h"
+#include "wx/graphics.h"
 #include "wx/image.h"
 
+#ifndef wxHAS_IMAGES_IN_RESOURCES
+    #include "../sample.xpm"
+#endif
+
 // ----------------------------------------------------------------------------
 // constants
 // ----------------------------------------------------------------------------
@@ -87,6 +91,7 @@ private:
     void OnShowShaped(wxCommandEvent& event);
     void OnShowTransparent(wxCommandEvent& event);
     void OnShowEffect(wxCommandEvent& event);
+    void OnExit(wxCommandEvent& event);
 
     DECLARE_EVENT_TABLE()
 };
@@ -109,7 +114,16 @@ public:
     void OnPaint(wxPaintEvent& evt);
 
 private:
-    bool     m_hasShape;
+    enum ShapeKind
+    {
+        Shape_None,
+        Shape_Star,
+#if wxUSE_GRAPHICS_CONTEXT
+        Shape_Circle,
+#endif // wxUSE_GRAPHICS_CONTEXT
+        Shape_Max
+    } m_shapeKind;
+
     wxBitmap m_bmp;
     wxPoint  m_delta;
 
@@ -152,15 +166,13 @@ public:
                 wxShowEffect effect,
                 // TODO: add menu command to the main frame to allow changing
                 //       these parameters
-                unsigned timeout = 1000,
-                wxDirection dir = wxBOTTOM)
+                unsigned timeout = 1000)
         : wxFrame(parent, wxID_ANY,
                   wxString::Format("Frame shown with %s effect",
                                    GetEffectName(effect)),
                   wxDefaultPosition, wxSize(450, 300)),
           m_effect(effect),
-          m_timeout(timeout),
-          m_dir(dir)
+          m_timeout(timeout)
     {
         new wxStaticText(this, wxID_ANY,
                          wxString::Format("Effect: %s", GetEffectName(effect)),
@@ -169,7 +181,7 @@ public:
                          wxString::Format("Timeout: %ums", m_timeout),
                          wxPoint(20, 60));
 
-        ShowWithEffect(m_effect, m_timeout, m_dir);
+        ShowWithEffect(m_effect, m_timeout);
 
         Connect(wxEVT_CLOSE_WINDOW, wxCloseEventHandler(EffectFrame::OnClose));
     }
@@ -179,7 +191,17 @@ private:
     {
         static const char *names[] =
         {
-            "roll", "slide", "fade", "expand",
+            "none",
+            "roll to left",
+            "roll to right",
+            "roll to top",
+            "roll to bottom",
+            "slide to left",
+            "slide to right",
+            "slide to top",
+            "slide to bottom",
+            "fade",
+            "expand",
         };
         wxCOMPILE_TIME_ASSERT( WXSIZEOF(names) == wxSHOW_EFFECT_MAX,
                                 EffectNamesMismatch );
@@ -189,14 +211,13 @@ private:
 
     void OnClose(wxCloseEvent& event)
     {
-        HideWithEffect(m_effect, m_timeout, m_dir);
+        HideWithEffect(m_effect, m_timeout);
 
         event.Skip();
     }
 
     wxShowEffect m_effect;
     unsigned m_timeout;
-    wxDirection m_dir;
 };
 
 // ============================================================================
@@ -233,12 +254,15 @@ BEGIN_EVENT_TABLE(MainFrame, wxFrame)
     EVT_MENU(Show_Shaped, MainFrame::OnShowShaped)
     EVT_MENU(Show_Transparent, MainFrame::OnShowTransparent)
     EVT_MENU_RANGE(Show_Effect_First, Show_Effect_Last, MainFrame::OnShowEffect)
+    EVT_MENU(wxID_EXIT, MainFrame::OnExit)
 END_EVENT_TABLE()
 
 MainFrame::MainFrame()
          : wxFrame(NULL, wxID_ANY, "wxWidgets Shaped Sample",
                    wxDefaultPosition, wxSize(200, 100))
 {
+    SetIcon(wxICON(sample));
+
     wxMenuBar * const mbar = new wxMenuBar;
     wxMenu * const menuFrames = new wxMenu;
     menuFrames->Append(Show_Shaped, "Show &shaped window\tCtrl-S");
@@ -271,12 +295,75 @@ void MainFrame::OnShowTransparent(wxCommandEvent& WXUNUSED(event))
 
 void MainFrame::OnShowEffect(wxCommandEvent& event)
 {
-    int effect = wxSHOW_EFFECT_ROLL + event.GetId() - Show_Effect_Roll;
+    int effect = event.GetId();
     static wxDirection direction = wxLEFT;
     direction = (wxDirection)(((int)direction)<< 1);
     if ( direction > wxDOWN )
         direction = wxLEFT ;
-    new EffectFrame(this, wx_static_cast(wxShowEffect, effect),1000,direction);
+
+    wxShowEffect eff;
+    switch ( effect )
+    {
+        case Show_Effect_Roll:
+            switch ( direction )
+            {
+                case wxLEFT:
+                    eff = wxSHOW_EFFECT_ROLL_TO_LEFT;
+                    break;
+                case wxRIGHT:
+                    eff = wxSHOW_EFFECT_ROLL_TO_RIGHT;
+                    break;
+                case wxTOP:
+                    eff = wxSHOW_EFFECT_ROLL_TO_TOP;
+                    break;
+                case wxBOTTOM:
+                    eff = wxSHOW_EFFECT_ROLL_TO_BOTTOM;
+                    break;
+                default:
+                    wxFAIL_MSG( "invalid direction" );
+                    return;
+            }
+            break;
+        case Show_Effect_Slide:
+            switch ( direction )
+            {
+                case wxLEFT:
+                    eff = wxSHOW_EFFECT_SLIDE_TO_LEFT;
+                    break;
+                case wxRIGHT:
+                    eff = wxSHOW_EFFECT_SLIDE_TO_RIGHT;
+                    break;
+                case wxTOP:
+                    eff = wxSHOW_EFFECT_SLIDE_TO_TOP;
+                    break;
+                case wxBOTTOM:
+                    eff = wxSHOW_EFFECT_SLIDE_TO_BOTTOM;
+                    break;
+                default:
+                    wxFAIL_MSG( "invalid direction" );
+                    return;
+            }
+            break;
+
+        case Show_Effect_Blend:
+            eff = wxSHOW_EFFECT_BLEND;
+            break;
+
+        case Show_Effect_Expand:
+            eff = wxSHOW_EFFECT_EXPAND;
+            break;
+
+        default:
+            wxFAIL_MSG( "invalid effect" );
+            return;
+    }
+
+    new EffectFrame(this,  eff, 1000);
+}
+
+void MainFrame::OnExit(wxCommandEvent& WXUNUSED(event))
+{
+    Close();
 }
 
 // ----------------------------------------------------------------------------
@@ -304,29 +391,46 @@ ShapedFrame::ShapedFrame(wxFrame *parent)
                   | wxSTAY_ON_TOP
             )
 {
-    m_hasShape = false;
-    m_bmp = wxBitmap(_T("star.png"), wxBITMAP_TYPE_PNG);
+    m_shapeKind = Shape_None;
+    m_bmp = wxBitmap(wxT("star.png"), wxBITMAP_TYPE_PNG);
     SetSize(wxSize(m_bmp.GetWidth(), m_bmp.GetHeight()));
-    SetToolTip(wxT("Right-click to close"));
+    SetToolTip(wxT("Right-click to close, double click to cycle shape"));
     SetWindowShape();
 }
 
 void ShapedFrame::SetWindowShape()
 {
-    wxRegion region(m_bmp, *wxWHITE);
-    m_hasShape = SetShape(region);
+    switch ( m_shapeKind )
+    {
+        case Shape_None:
+            SetShape(wxRegion());
+            break;
+
+        case Shape_Star:
+            SetShape(wxRegion(m_bmp, *wxWHITE));
+            break;
+
+#if wxUSE_GRAPHICS_CONTEXT
+        case Shape_Circle:
+            {
+                wxGraphicsPath
+                    path = wxGraphicsRenderer::GetDefaultRenderer()->CreatePath();
+                path.AddCircle(m_bmp.GetWidth()/2, m_bmp.GetHeight()/2, 30);
+                SetShape(path);
+            }
+            break;
+#endif // wxUSE_GRAPHICS_CONTEXT
+
+        case Shape_Max:
+            wxFAIL_MSG( "invalid shape kind" );
+            break;
+    }
 }
 
 void ShapedFrame::OnDoubleClick(wxMouseEvent& WXUNUSED(evt))
 {
-    if (m_hasShape)
-    {
-        wxRegion region;
-        SetShape(region);
-        m_hasShape = false;
-    }
-    else
-        SetWindowShape();
+    m_shapeKind = static_cast<ShapeKind>((m_shapeKind + 1) % Shape_Max);
+    SetWindowShape();
 }
 
 void ShapedFrame::OnLeftDown(wxMouseEvent& evt)
@@ -385,7 +489,7 @@ SeeThroughFrame::SeeThroughFrame()
                   wxSTAY_ON_TOP),
          m_currentState(STATE_SEETHROUGH)
 {
-    SetBackgroundColour(wxColour(255, 255, 255, 255));
+    SetBackgroundColour(*wxWHITE);
     SetBackgroundStyle(wxBG_STYLE_TRANSPARENT);
 }