]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/drawing/drawing.cpp
M_PI is not ANSI
[wxWidgets.git] / samples / drawing / drawing.cpp
index 0fe06ad96e1fb7b4583c36db4152772859ac5cc9..c44bef0949a05c28194baa7c87e0e9aecfe03d82 100644 (file)
@@ -36,6 +36,7 @@
 #endif
 
 #include "wx/colordlg.h"
+#include "wx/image.h"
 
 // ----------------------------------------------------------------------------
 // ressources
@@ -58,7 +59,8 @@ enum ScreenToShow
     Show_Text,
     Show_Lines,
     Show_Polygons,
-    Show_Mask
+    Show_Mask,
+    Show_Ops
 };
 
 // ----------------------------------------------------------------------------
@@ -144,6 +146,7 @@ protected:
     void DrawTestLines( int x, int y, int width, wxDC &dc );
     void DrawText(wxDC& dc);
     void DrawImages(wxDC& dc);
+    void DrawWithLogicalOps(wxDC& dc);
     void DrawDefault(wxDC& dc);
 
 private:
@@ -171,7 +174,8 @@ enum
     File_ShowLines,
     File_ShowPolygons,
     File_ShowMask,
-    MenuShow_Last = File_ShowMask,
+    File_ShowOps,
+    MenuShow_Last = File_ShowOps,
 
     MenuOption_First,
 
@@ -254,9 +258,12 @@ bool MyApp::LoadImages()
     if ( !path )
         return FALSE;
     gs_bmpMask.LoadFile(path, wxBITMAP_TYPE_BMP);
-    gs_bmpMask.SetDepth(1);
 
-    wxMask *mask = new wxMask(gs_bmpMask);
+//    This is so wrong, it hurts.
+//    gs_bmpMask.SetDepth(1);
+//    wxMask *mask = new wxMask(gs_bmpMask);
+
+    wxMask *mask = new wxMask(gs_bmpMask, *wxBLACK);
     gs_bmpWithMask.SetMask(mask);
 
     mask = new wxMask(gs_bmpWithColMask, *wxWHITE);
@@ -552,7 +559,6 @@ void MyCanvas::DrawTestLines( int x, int y, int width, wxDC &dc )
     dash1[0] = 0xFF;
     ud.SetDashes( 1, dash1 );
     dc.DrawLine( x+20, y+170, 100, y+170 );
-
 }
 
 void MyCanvas::DrawDefault(wxDC& dc)
@@ -564,15 +570,130 @@ void MyCanvas::DrawDefault(wxDC& dc)
     dc.FloodFill(0, 0, wxColour(255, 0, 0));
 #endif //
 
-    dc.DrawIcon( wxICON(mondrian), 410, 40 );
+    dc.DrawIcon( wxICON(mondrian), 40, 40 );
 
-    // test the rectangle drawing - there should be no pixels between the rect
-    // and the lines
+    // this is the test for "blitting bitmap into DC damages selected brush"
+    // bug
     dc.SetPen(*wxTRANSPARENT_PEN);
+    dc.SetBrush( *wxGREEN_BRUSH );
+    dc.DrawRectangle(100, 10, 40, 40);
+    dc.DrawBitmap(wxTheApp->GetStdIcon(wxICON_INFORMATION), 102, 12, TRUE);
+    dc.DrawRectangle(150, 10, 40, 40);
+    dc.DrawIcon(wxTheApp->GetStdIcon(wxICON_INFORMATION), 152, 12);
+    dc.DrawRectangle(200, 10, 40, 40);
+
+    // test for "transparent" bitmap drawing (it intersects with the last
+    // rectangle above)
+    //dc.SetBrush( *wxTRANSPARENT_BRUSH );
+    #include "../image/smile.xpm"
+    wxBitmap bmp(smile_xpm);
+    dc.DrawBitmap(bmp, 210, 30, TRUE);
+
+    dc.SetBrush( *wxBLACK_BRUSH );
+    dc.DrawRectangle( 0, 160, 1000, 300 );
+
+    // draw lines
+    wxBitmap bitmap(20,70);
+    wxMemoryDC memdc;
+    memdc.SelectObject( bitmap );
+    memdc.SetBrush( *wxBLACK_BRUSH );
+    memdc.SetPen( *wxWHITE_PEN );
+    memdc.DrawRectangle(0,0,20,70);
+    memdc.DrawLine( 10,0,10,70 );
+
+    // to the right
+    wxPen pen = *wxRED_PEN;
+    pen.SetWidth(2);
+    memdc.SetPen(pen);
+    memdc.DrawLine( 10, 5,10, 5 );
+    memdc.DrawLine( 10,10,11,10 );
+    memdc.DrawLine( 10,15,12,15 );
+    memdc.DrawLine( 10,20,13,20 );
+
+/*
+    memdc.SetPen(*wxRED_PEN);
+    memdc.DrawLine( 12, 5,12, 5 );
+    memdc.DrawLine( 12,10,13,10 );
+    memdc.DrawLine( 12,15,14,15 );
+    memdc.DrawLine( 12,20,15,20 );
+*/
+
+    // same to the left
+    memdc.DrawLine( 10,25,10,25 );
+    memdc.DrawLine( 10,30, 9,30 );
+    memdc.DrawLine( 10,35, 8,35 );
+    memdc.DrawLine( 10,40, 7,40 );
+
+    // XOR draw lines
+    dc.SetPen(*wxWHITE_PEN);
+    memdc.SetLogicalFunction( wxINVERT );
+    memdc.SetPen( *wxWHITE_PEN );
+    memdc.DrawLine( 10,50,10,50 );
+    memdc.DrawLine( 10,55,11,55 );
+    memdc.DrawLine( 10,60,12,60 );
+    memdc.DrawLine( 10,65,13,65 );
+
+    memdc.DrawLine( 12,50,12,50 );
+    memdc.DrawLine( 12,55,13,55 );
+    memdc.DrawLine( 12,60,14,60 );
+    memdc.DrawLine( 12,65,15,65 );
+
+    memdc.SelectObject( wxNullBitmap );
+    dc.DrawBitmap( bitmap, 10, 170 );
+    wxImage image( bitmap );
+    image.Rescale( 60,210 );
+    bitmap = image.ConvertToBitmap();
+    dc.DrawBitmap( bitmap, 50, 170 );
+
+    // test the rectangle outline drawing - there should be one pixel between
+    // the rect and the lines
+    dc.SetPen(*wxWHITE_PEN);
+    dc.SetBrush( *wxTRANSPARENT_BRUSH );
+    dc.DrawRectangle(150, 170, 49, 29);
+    dc.DrawRectangle(200, 170, 49, 29);
+    dc.SetPen(*wxWHITE_PEN);
+    dc.DrawLine(250, 210, 250, 170);
+    dc.DrawLine(260, 200, 150, 200);
+
+    // test the rectangle filled drawing - there should be one pixel between
+    // the rect and the lines
+    dc.SetPen(*wxTRANSPARENT_PEN);
+    dc.SetBrush( *wxWHITE_BRUSH );
+    dc.DrawRectangle(300, 170, 49, 29);
     dc.DrawRectangle(350, 170, 49, 29);
+    dc.SetPen(*wxWHITE_PEN);
+    dc.DrawLine(400, 170, 400, 210);
+    dc.DrawLine(300, 200, 410, 200);
+
+    // and now for filled rect with outline
     dc.SetPen(*wxRED_PEN);
-    dc.DrawLine(400, 160, 400, 210); 
-    dc.DrawLine(340, 200, 410, 200); 
+    dc.SetBrush( *wxWHITE_BRUSH );
+    dc.DrawRectangle(500, 170, 49, 29);
+    dc.DrawRectangle(550, 170, 49, 29);
+    dc.SetPen(*wxWHITE_PEN);
+    dc.DrawLine(600, 170, 600, 210);
+    dc.DrawLine(500, 200, 610, 200);
+
+    // test the rectangle outline drawing - there should be one pixel between
+    // the rect and the lines
+    dc.SetPen(*wxWHITE_PEN);
+    dc.SetBrush( *wxTRANSPARENT_BRUSH );
+    dc.DrawRoundedRectangle(150, 270, 49, 29, 6);
+    dc.DrawRoundedRectangle(200, 270, 49, 29, 6);
+    dc.SetPen(*wxWHITE_PEN);
+    dc.DrawLine(250, 270, 250, 310);
+    dc.DrawLine(150, 300, 260, 300);
+
+    // test the rectangle filled drawing - there should be one pixel between
+    // the rect and the lines
+    dc.SetPen(*wxTRANSPARENT_PEN);
+    dc.SetBrush( *wxWHITE_BRUSH );
+    dc.DrawRoundedRectangle(300, 270, 49, 29, 6);
+    dc.DrawRoundedRectangle(350, 270, 49, 29, 6);
+    dc.SetPen(*wxWHITE_PEN);
+    dc.DrawLine(400, 270, 400, 310);
+    dc.DrawLine(300, 300, 410, 300);
+
 }
 
 void MyCanvas::DrawText(wxDC& dc)
@@ -610,31 +731,31 @@ void MyCanvas::DrawText(wxDC& dc)
     dc.DrawRectangle( 100, 40, 4, height );
 }
 
-void MyCanvas::DrawImages(wxDC& dc)
+static const struct
 {
-    static const struct
-    {
-        const wxChar *name;
-        int           rop;
-    } rasterOperations[] =
-    {
-        { "wxAND",          wxAND           },
-        { "wxAND_INVERT",   wxAND_INVERT    },
-        { "wxAND_REVERSE",  wxAND_REVERSE   },
-        { "wxCLEAR",        wxCLEAR         },
-        { "wxCOPY",         wxCOPY          },
-        { "wxEQUIV",        wxEQUIV         },
-        { "wxINVERT",       wxINVERT        },
-        { "wxNAND",         wxNAND          },
-        { "wxNO_OP",        wxNO_OP         },
-        { "wxOR",           wxOR            },
-        { "wxOR_INVERT",    wxOR_INVERT     },
-        { "wxOR_REVERSE",   wxOR_REVERSE    },
-        { "wxSET",          wxSET           },
-        { "wxSRC_INVERT",   wxSRC_INVERT    },
-        { "wxXOR",          wxXOR           },
-    };
+    const wxChar *name;
+    int           rop;
+} rasterOperations[] =
+{
+    { "wxAND",          wxAND           },
+    { "wxAND_INVERT",   wxAND_INVERT    },
+    { "wxAND_REVERSE",  wxAND_REVERSE   },
+    { "wxCLEAR",        wxCLEAR         },
+    { "wxCOPY",         wxCOPY          },
+    { "wxEQUIV",        wxEQUIV         },
+    { "wxINVERT",       wxINVERT        },
+    { "wxNAND",         wxNAND          },
+    { "wxNO_OP",        wxNO_OP         },
+    { "wxOR",           wxOR            },
+    { "wxOR_INVERT",    wxOR_INVERT     },
+    { "wxOR_REVERSE",   wxOR_REVERSE    },
+    { "wxSET",          wxSET           },
+    { "wxSRC_INVERT",   wxSRC_INVERT    },
+    { "wxXOR",          wxXOR           },
+};
 
+void MyCanvas::DrawImages(wxDC& dc)
+{
     dc.DrawText("original image", 0, 0);
     dc.DrawBitmap(gs_bmpNoMask, 0, 20, 0);
     dc.DrawText("with colour mask", 0, 100);
@@ -659,6 +780,28 @@ void MyCanvas::DrawImages(wxDC& dc)
     }
 }
 
+void MyCanvas::DrawWithLogicalOps(wxDC& dc)
+{
+    static const wxCoord w = 60;
+    static const wxCoord h = 60;
+
+    // reuse the text colour here
+    dc.SetPen(wxPen(m_owner->m_colourForeground, 1, wxSOLID));
+    dc.SetBrush(*wxTRANSPARENT_BRUSH);
+
+    for ( size_t n = 0; n < WXSIZEOF(rasterOperations); n++ )
+    {
+        wxCoord x = 20 + 150*(n%4),
+                y = 20 + 100*(n/4);
+
+        dc.DrawText(rasterOperations[n].name, x, y - 20);
+        dc.SetLogicalFunction(rasterOperations[n].rop);
+        dc.DrawRectangle(x, y, w, h);
+        dc.DrawLine(x, y, x + w, y + h);
+        dc.DrawLine(x + w, y, x, y + h);
+    }
+}
+
 void MyCanvas::OnPaint(wxPaintEvent &WXUNUSED(event))
 {
     wxPaintDC dc(this);
@@ -699,6 +842,10 @@ void MyCanvas::OnPaint(wxPaintEvent &WXUNUSED(event))
         case Show_Mask:
             DrawImages(dc);
             break;
+
+        case Show_Ops:
+            DrawWithLogicalOps(dc);
+            break;
     }
 }
 
@@ -745,6 +892,7 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
     menuFile->Append(File_ShowLines, "&Lines screen\tF3");
     menuFile->Append(File_ShowPolygons, "&Polygons screen\tF4");
     menuFile->Append(File_ShowMask, "wx&Mask screen\tF5");
+    menuFile->Append(File_ShowOps, "&ROP screen\tF6");
     menuFile->AppendSeparator();
     menuFile->Append(File_About, "&About...\tCtrl-A", "Show about dialog");
     menuFile->AppendSeparator();