]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/shaped/shaped.cpp
Mention graphics device classes
[wxWidgets.git] / samples / shaped / shaped.cpp
index 1a9efa81324d33c58f99ea31cb156191fac7b502..7a371fbdda3bede4f88b6cae12b3a82f596f6197 100644 (file)
 #include "wx/dcclient.h"
 #include "wx/image.h"
 
+// ----------------------------------------------------------------------------
+// constants
+// ----------------------------------------------------------------------------
+
+// menu ids
+enum
+{
+    Show_Shaped = 100,
+    Show_Transparent,
+
+    // must be consecutive and in the same order as wxShowEffect enum elements
+    Show_Effect_First,
+    Show_Effect_Roll = Show_Effect_First,
+    Show_Effect_Slide,
+    Show_Effect_Blend,
+    Show_Effect_Expand,
+    Show_Effect_Last = Show_Effect_Expand
+};
+
 // ----------------------------------------------------------------------------
 // private classes
 // ----------------------------------------------------------------------------
@@ -58,6 +77,20 @@ public:
 };
 
 
+// Main frame just contains the menu items invoking the other tests
+class MainFrame : public wxFrame
+{
+public:
+    MainFrame();
+
+private:
+    void OnShowShaped(wxCommandEvent& event);
+    void OnShowTransparent(wxCommandEvent& event);
+    void OnShowEffect(wxCommandEvent& event);
+
+    DECLARE_EVENT_TABLE()
+};
+
 // Define a new frame type: this is going to the frame showing the
 // effect of wxFRAME_SHAPED
 class ShapedFrame : public wxFrame
@@ -74,7 +107,6 @@ public:
     void OnMouseMove(wxMouseEvent& evt);
     void OnExit(wxMouseEvent& evt);
     void OnPaint(wxPaintEvent& evt);
-    void OnWindowCreate(wxWindowCreateEvent& evt);
 
 private:
     bool     m_hasShape;
@@ -97,7 +129,6 @@ public:
     // event handlers (these functions should _not_ be virtual)
     void OnDoubleClick(wxMouseEvent& evt);
     void OnPaint(wxPaintEvent& evt);
-    void OnSize(wxSizeEvent& evt);
 
 private:
     enum State
@@ -114,35 +145,59 @@ private:
     DECLARE_EVENT_TABLE()
 };
 
+class EffectFrame : public wxFrame
+{
+public:
+    EffectFrame(wxWindow *parent,
+                wxShowEffect effect,
+                // TODO: add menu command to the main frame to allow changing
+                //       these parameters
+                unsigned timeout = 1000,
+                wxDirection dir = wxBOTTOM)
+        : 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)
+    {
+        new wxStaticText(this, wxID_ANY,
+                         wxString::Format("Effect: %s", GetEffectName(effect)),
+                         wxPoint(20, 20));
+        new wxStaticText(this, wxID_ANY,
+                         wxString::Format("Timeout: %ums", m_timeout),
+                         wxPoint(20, 60));
 
-// ----------------------------------------------------------------------------
-// event tables and other macros for wxWidgets
-// ----------------------------------------------------------------------------
+        ShowWithEffect(m_effect, m_timeout, m_dir);
 
-// the event tables connect the wxWidgets events with the functions (event
-// handlers) which process them. It can be also done at run-time, but for the
-// simple menu events like this the static method is much simpler.
-BEGIN_EVENT_TABLE(ShapedFrame, wxFrame)
-    EVT_LEFT_DCLICK(ShapedFrame::OnDoubleClick)
-    EVT_LEFT_DOWN(ShapedFrame::OnLeftDown)
-    EVT_LEFT_UP(ShapedFrame::OnLeftUp)
-    EVT_MOTION(ShapedFrame::OnMouseMove)
-    EVT_RIGHT_UP(ShapedFrame::OnExit)
+        Connect(wxEVT_CLOSE_WINDOW, wxCloseEventHandler(EffectFrame::OnClose));
+    }
 
-    EVT_PAINT(ShapedFrame::OnPaint)
+private:
+    static const char *GetEffectName(wxShowEffect effect)
+    {
+        static const char *names[] =
+        {
+            "roll", "slide", "fade", "expand",
+        };
+        wxCOMPILE_TIME_ASSERT( WXSIZEOF(names) == wxSHOW_EFFECT_MAX,
+                                EffectNamesMismatch );
 
-#ifdef __WXGTK__
-    EVT_WINDOW_CREATE(ShapedFrame::OnWindowCreate)
-#endif
-END_EVENT_TABLE()
+        return names[effect];
+    }
 
+    void OnClose(wxCloseEvent& event)
+    {
+        HideWithEffect(m_effect, m_timeout, m_dir);
 
-// Create a new application object: this macro will allow wxWidgets to create
-// the application object during program execution (it's better than using a
-// static object for many reasons) and also declares the accessor function
-// wxGetApp() which will return the reference of the right type (i.e. MyApp and
-// not wxApp)
-IMPLEMENT_APP(MyApp)
+        event.Skip();
+    }
+
+    wxShowEffect m_effect;
+    unsigned m_timeout;
+    wxDirection m_dir;
+};
 
 // ============================================================================
 // implementation
@@ -152,6 +207,8 @@ IMPLEMENT_APP(MyApp)
 // the application class
 // ----------------------------------------------------------------------------
 
+IMPLEMENT_APP(MyApp)
+
 // `Main program' equivalent: the program execution "starts" here
 bool MyApp::OnInit()
 {
@@ -160,14 +217,7 @@ bool MyApp::OnInit()
 
     wxInitAllImageHandlers();
 
-    // Create the transparent window
-    SeeThroughFrame *seeThroughFrame = new SeeThroughFrame();
-    seeThroughFrame->Show(true);
-    SetTopWindow(seeThroughFrame);
-
-    // Create the shaped window
-    ShapedFrame *shapedFrame = new ShapedFrame(seeThroughFrame);
-    shapedFrame->Show(true);
+    new MainFrame;
 
     // success: wxApp::OnRun() will be called which will enter the main message
     // loop and the application will run. If we returned false here, the
@@ -175,14 +225,78 @@ bool MyApp::OnInit()
     return true;
 }
 
+// ----------------------------------------------------------------------------
+// main frame
+// ----------------------------------------------------------------------------
+
+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)
+END_EVENT_TABLE()
+
+MainFrame::MainFrame()
+         : wxFrame(NULL, wxID_ANY, "wxWidgets Shaped Sample",
+                   wxDefaultPosition, wxSize(200, 100))
+{
+    wxMenuBar * const mbar = new wxMenuBar;
+    wxMenu * const menuFrames = new wxMenu;
+    menuFrames->Append(Show_Shaped, "Show &shaped window\tCtrl-S");
+    menuFrames->Append(Show_Transparent, "Show &transparent window\tCtrl-T");
+    menuFrames->AppendSeparator();
+    menuFrames->Append(Show_Effect_Roll, "Show &rolled effect\tCtrl-R");
+    menuFrames->Append(Show_Effect_Slide, "Show s&lide effect\tCtrl-L");
+    menuFrames->Append(Show_Effect_Blend, "Show &fade effect\tCtrl-F");
+    menuFrames->Append(Show_Effect_Expand, "Show &expand effect\tCtrl-E");
+    menuFrames->AppendSeparator();
+    menuFrames->Append(wxID_EXIT, "E&xit");
+
+    mbar->Append(menuFrames, "&Show");
+    SetMenuBar(mbar);
+
+    Show();
+}
+
+void MainFrame::OnShowShaped(wxCommandEvent& WXUNUSED(event))
+{
+    ShapedFrame *shapedFrame = new ShapedFrame(this);
+    shapedFrame->Show(true);
+}
+
+void MainFrame::OnShowTransparent(wxCommandEvent& WXUNUSED(event))
+{
+    SeeThroughFrame *seeThroughFrame = new SeeThroughFrame();
+    seeThroughFrame->Show(true);
+}
+
+void MainFrame::OnShowEffect(wxCommandEvent& event)
+{
+    int effect = wxSHOW_EFFECT_ROLL + event.GetId() - Show_Effect_Roll;
+    static wxDirection direction = wxLEFT;
+    direction = (wxDirection)(((int)direction)<< 1);
+    if ( direction > wxDOWN )
+        direction = wxLEFT ;
+    new EffectFrame(this, wx_static_cast(wxShowEffect, effect),1000,direction);
+}
+
 // ----------------------------------------------------------------------------
 // shaped frame
 // ----------------------------------------------------------------------------
 
+BEGIN_EVENT_TABLE(ShapedFrame, wxFrame)
+    EVT_LEFT_DCLICK(ShapedFrame::OnDoubleClick)
+    EVT_LEFT_DOWN(ShapedFrame::OnLeftDown)
+    EVT_LEFT_UP(ShapedFrame::OnLeftUp)
+    EVT_MOTION(ShapedFrame::OnMouseMove)
+    EVT_RIGHT_UP(ShapedFrame::OnExit)
+    EVT_PAINT(ShapedFrame::OnPaint)
+END_EVENT_TABLE()
+
+
 // frame constructor
 ShapedFrame::ShapedFrame(wxFrame *parent)
        : wxFrame(parent, wxID_ANY, wxEmptyString,
-                  wxDefaultPosition, wxSize(100, 100), //wxDefaultSize,
+                  wxDefaultPosition, wxSize(100, 100),
                   0
                   | wxFRAME_SHAPED
                   | wxSIMPLE_BORDER
@@ -193,15 +307,8 @@ ShapedFrame::ShapedFrame(wxFrame *parent)
     m_hasShape = false;
     m_bmp = wxBitmap(_T("star.png"), wxBITMAP_TYPE_PNG);
     SetSize(wxSize(m_bmp.GetWidth(), m_bmp.GetHeight()));
-    SetToolTip(wxT("Right-click to exit"));
-
-#ifndef __WXGTK__
-    // On wxGTK we can't do this yet because the window hasn't been created
-    // yet so we wait until the EVT_WINDOW_CREATE event happens.  On wxMSW and
-    // wxMac the window has been created at this point so we go ahead and set
-    // the shape now.
+    SetToolTip(wxT("Right-click to close"));
     SetWindowShape();
-#endif
 }
 
 void ShapedFrame::SetWindowShape()
@@ -225,7 +332,6 @@ void ShapedFrame::OnDoubleClick(wxMouseEvent& WXUNUSED(evt))
 void ShapedFrame::OnLeftDown(wxMouseEvent& evt)
 {
     CaptureMouse();
-    //printf("Mouse captured\n");
     wxPoint pos = ClientToScreen(evt.GetPosition());
     wxPoint origin = GetPosition();
     int dx =  pos.x - origin.x;
@@ -238,14 +344,12 @@ void ShapedFrame::OnLeftUp(wxMouseEvent& WXUNUSED(evt))
     if (HasCapture())
     {
         ReleaseMouse();
-        //printf("Mouse released\n");
     }
 }
 
 void ShapedFrame::OnMouseMove(wxMouseEvent& evt)
 {
     wxPoint pt = evt.GetPosition();
-    //printf("x:%d   y:%d\n", pt.x, pt.y);
     if (evt.Dragging() && evt.LeftIsDown())
     {
         wxPoint pos = ClientToScreen(pt);
@@ -264,32 +368,27 @@ void ShapedFrame::OnPaint(wxPaintEvent& WXUNUSED(evt))
     dc.DrawBitmap(m_bmp, 0, 0, true);
 }
 
-void ShapedFrame::OnWindowCreate(wxWindowCreateEvent& WXUNUSED(evt))
-{
-    SetWindowShape();
-}
-
 // ----------------------------------------------------------------------------
 // see-through frame
 // ----------------------------------------------------------------------------
 
-// frame constructor
+BEGIN_EVENT_TABLE(SeeThroughFrame, wxFrame)
+    EVT_LEFT_DCLICK(SeeThroughFrame::OnDoubleClick)
+    EVT_PAINT(SeeThroughFrame::OnPaint)
+END_EVENT_TABLE()
+
 SeeThroughFrame::SeeThroughFrame()
        : wxFrame(NULL, wxID_ANY, "Transparency test: double click here",
                   wxPoint(100, 30), wxSize(300, 300),
-                  wxDEFAULT_FRAME_STYLE | wxSTAY_ON_TOP),
+                  wxDEFAULT_FRAME_STYLE |
+                  wxFULL_REPAINT_ON_RESIZE |
+                  wxSTAY_ON_TOP),
          m_currentState(STATE_SEETHROUGH)
 {
     SetBackgroundColour(wxColour(255, 255, 255, 255));
     SetBackgroundStyle(wxBG_STYLE_TRANSPARENT);
 }
 
-// Redraws the whole window on resize
-void SeeThroughFrame::OnSize(wxSizeEvent& WXUNUSED(evt))
-{
-    Refresh();
-}
-
 // Paints a grid of varying hue and alpha
 void SeeThroughFrame::OnPaint(wxPaintEvent& WXUNUSED(evt))
 {
@@ -353,9 +452,3 @@ void SeeThroughFrame::OnDoubleClick(wxMouseEvent& WXUNUSED(evt))
     Refresh();
 }
 
-BEGIN_EVENT_TABLE(SeeThroughFrame, wxFrame)
-    EVT_LEFT_DCLICK(SeeThroughFrame::OnDoubleClick)
-    EVT_PAINT(SeeThroughFrame::OnPaint)
-    EVT_SIZE(SeeThroughFrame::OnSize)
-END_EVENT_TABLE()
-