]> git.saurik.com Git - wxWidgets.git/blobdiff - utils/framelayout/src/newbmpbtn.cpp
fixed a canonical example of Stupid Bug(tm)
[wxWidgets.git] / utils / framelayout / src / newbmpbtn.cpp
index 6ca0afb0999933a86cba83cb348a149615aeecdf..997820a60d879247f22fe00c26883c9bf4576e29 100644 (file)
@@ -71,7 +71,6 @@ static int* create_array( int width, int height, int fill = 0 )
 static void gray_out_pixmap( int* src, int* dest, int width, int height )
 {
        // assuming the pixels along the edges are of the background color
 static void gray_out_pixmap( int* src, int* dest, int width, int height )
 {
        // assuming the pixels along the edges are of the background color
-       int bgCol = GET_ELEM(src,0,0);
 
        int x = 0;
        int y = 1;
 
        int x = 0;
        int y = 1;
@@ -80,9 +79,6 @@ static void gray_out_pixmap( int* src, int* dest, int width, int height )
        {
                int cur       = GET_ELEM(src,x,y);
 
        {
                int cur       = GET_ELEM(src,x,y);
 
-               int r = GET_RED(cur);
-               int g = GET_GREEN(cur);
-               int b = GET_BLUE(cur);
 
                if ( IS_IN_ARRAY(x-1,y-1) )
                {
 
                if ( IS_IN_ARRAY(x-1,y-1) )
                {
@@ -152,8 +148,8 @@ void greay_out_image_on_dc( wxDC& dc, int width, int height )
        wxColour bgCol;
        dc.GetPixel( 0, 0, &bgCol );
 
        wxColour bgCol;
        dc.GetPixel( 0, 0, &bgCol );
 
-       wxPen darkPen ( wxColour(128,128,128),1, wxSOLID );
-       wxPen lightPen( wxColour(255,255,255),1, wxSOLID );
+       wxPen darkPen ( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW),1, wxSOLID );
+       wxPen lightPen( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHIGHLIGHT),1, wxSOLID );
        wxPen bgPen   ( bgCol,                1, wxSOLID );
 
        int* src  = create_array( width, height, MASK_BG );
        wxPen bgPen   ( bgCol,                1, wxSOLID );
 
        int* src  = create_array( width, height, MASK_BG );
@@ -167,11 +163,6 @@ void greay_out_image_on_dc( wxDC& dc, int width, int height )
                        wxColour col;
                        dc.GetPixel( x,y, &col );
 
                        wxColour col;
                        dc.GetPixel( x,y, &col );
 
-                       int r = col.Red(), 
-                               g = col.Green(), 
-                               b = col.Blue();
-
-                       int o = MAKE_INT_COLOR( r,g,b );
 
                        GET_ELEM(src,x,y) = MAKE_INT_COLOR( col.Red(), col.Green(), col.Blue() );
                }
 
                        GET_ELEM(src,x,y) = MAKE_INT_COLOR( col.Red(), col.Green(), col.Blue() );
                }
@@ -233,38 +224,38 @@ wxNewBitmapButton::wxNewBitmapButton( const wxBitmap& labelBitmap,
                                                                          int  marginY,
                                                                          int  textToLabelGap,
                                                                          bool isSticky)
                                                                          int  marginY,
                                                                          int  textToLabelGap,
                                                                          bool isSticky)
-       :       mpDepressedImg( NULL ),
-               mpPressedImg  ( NULL ),
-               mpDisabledImg ( NULL ),
-               mpFocusedImg  ( NULL ),
-
+       :       mTextToLabelGap  ( textToLabelGap ),
                mMarginX( marginX ),
                mMarginY( marginY ),
                mTextAlignment( alignText ),
                mMarginX( marginX ),
                mMarginY( marginY ),
                mTextAlignment( alignText ),
+               mIsSticky( isSticky ),
                mIsFlat( isFlat ),
                mIsFlat( isFlat ),
+               mLabelText( labelText ),
+               mImageFileType( -1 ),
+               mDepressedBmp( labelBitmap ),
+
+               mpDepressedImg( NULL ),
+               mpPressedImg  ( NULL ),
+               mpDisabledImg ( NULL ),
+               mpFocusedImg  ( NULL ),
+
 
 
-               mIsPressed       ( FALSE ),
                mDragStarted     ( FALSE ),
                mDragStarted     ( FALSE ),
+               mIsPressed       ( FALSE ),
+               mIsInFocus( FALSE ),
                mPrevPressedState( FALSE ),
                mPrevPressedState( FALSE ),
-               mTextToLabelGap  ( textToLabelGap ),
+               mPrevInFocusState( FALSE ),
+               mHasFocusedBmp( FALSE ),
+               mFiredEventType( firedEventType ),
 
                mBlackPen( wxColour(  0,  0,  0), 1, wxSOLID ),
 
                mBlackPen( wxColour(  0,  0,  0), 1, wxSOLID ),
-               mDarkPen ( wxColour(128,128,128), 1, wxSOLID ),
-               mGrayPen ( wxColour(192,192,192),
-                          1, wxSOLID ),
-               mLightPen( wxColour(255,255,255), 1, wxSOLID ),
+               mDarkPen ( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW), 1, wxSOLID ),
+               mGrayPen ( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE), 1, wxSOLID ),
+               mLightPen( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHIGHLIGHT), 1, wxSOLID ),
 
 
-               mFiredEventType( firedEventType ),
-               mIsSticky( isSticky ),
                mIsCreated( FALSE ),
                mIsCreated( FALSE ),
-               mSizeIsSet( FALSE ),
-
-               mHasFocusedBmp( FALSE ),
-               mIsInFocus( FALSE ),
+               mSizeIsSet( FALSE )
 
 
-               mDepressedBmp( labelBitmap ),
-               mLabelText( labelText ),
-               mImageFileType( -1 )
 {
 }
 
 {
 }
 
@@ -279,40 +270,38 @@ wxNewBitmapButton::wxNewBitmapButton( const wxString& bitmapFileName,
                                                                          int  textToLabelGap,
                                                                          bool isSticky)
 
                                                                          int  textToLabelGap,
                                                                          bool isSticky)
 
-       :       mpDepressedImg( NULL ),
-               mpPressedImg  ( NULL ),
-               mpDisabledImg ( NULL ),
-               mpFocusedImg  ( NULL ),
-
+       :       mTextToLabelGap  ( 2 ),
                mMarginX( 2 ),
                mMarginY( 2 ),
                mTextAlignment( alignText ),
                mMarginX( 2 ),
                mMarginY( 2 ),
                mTextAlignment( alignText ),
+               mIsSticky( FALSE ),
                mIsFlat( isFlat ),
                mIsFlat( isFlat ),
+               mLabelText( labelText ),
+               mImageFileName( bitmapFileName ),
+               mImageFileType( bitmapFileType ),
+
+               mpDepressedImg( NULL ),
+               mpPressedImg  ( NULL ),
+               mpDisabledImg ( NULL ),
+               mpFocusedImg  ( NULL ),
 
 
-               mIsPressed       ( FALSE ),
                mDragStarted     ( FALSE ),
                mDragStarted     ( FALSE ),
+               mIsPressed       ( FALSE ),
+               mIsInFocus       ( FALSE ),
                mPrevPressedState( FALSE ),
                mPrevPressedState( FALSE ),
-               mTextToLabelGap  ( 2 ),
+               mPrevInFocusState( FALSE ),
+               mHasFocusedBmp( FALSE ),
+               mFiredEventType( wxEVT_COMMAND_MENU_SELECTED ),
 
                mBlackPen( wxColour(  0,  0,  0), 1, wxSOLID ),
 
                mBlackPen( wxColour(  0,  0,  0), 1, wxSOLID ),
-               mDarkPen ( wxColour(128,128,128), 1, wxSOLID ),
-               mGrayPen ( wxColour(192,192,192),
-                          1, wxSOLID ),
-               mLightPen( wxColour(255,255,255), 1, wxSOLID ),
+               mDarkPen ( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW), 1, wxSOLID ),
+               mGrayPen ( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE), 1, wxSOLID ),
+               mLightPen( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHIGHLIGHT), 1, wxSOLID ),
 
 
-               mFiredEventType( wxEVT_COMMAND_MENU_SELECTED ),
-               mIsSticky( FALSE ),
                mIsCreated( FALSE ),
                mIsCreated( FALSE ),
-               mSizeIsSet( FALSE ),
-
-               mHasFocusedBmp( FALSE ),
-               mIsInFocus( FALSE ),
+               mSizeIsSet( FALSE )
 
 
-               mLabelText( labelText ),
-               mImageFileName( bitmapFileName ),
-               mImageFileType( bitmapFileType )
 {
 {
-       //mDepressedBmp.LoadFile( bitmapFileName, bitmapFileType );
 }
 
 wxNewBitmapButton::~wxNewBitmapButton(void) 
 }
 
 wxNewBitmapButton::~wxNewBitmapButton(void) 
@@ -381,6 +370,11 @@ wxBitmap* wxNewBitmapButton::GetStateLabel()
                return mpDisabledImg;
 }
 
                return mpDisabledImg;
 }
 
+static const unsigned char _gDisableImage[] = { 0x55,0xAA,0x55,0xAA,
+                                                                                     0x55,0xAA,0x55,0xAA,
+                                                                                     0x55,0xAA,0x55,0xAA,
+                                                                                     0x55,0xAA,0x55,0xAA
+                                                                                       };
 void wxNewBitmapButton::RenderLabelImage( wxBitmap*& destBmp, wxBitmap* srcBmp,
                                                                                  bool isEnabled, bool isPressed )
 {
 void wxNewBitmapButton::RenderLabelImage( wxBitmap*& destBmp, wxBitmap* srcBmp,
                                                                                  bool isEnabled, bool isPressed )
 {
@@ -390,7 +384,6 @@ void wxNewBitmapButton::RenderLabelImage( wxBitmap*& destBmp, wxBitmap* srcBmp,
 
        wxMemoryDC srcDc;
        srcDc.SelectObject( *srcBmp );
 
        wxMemoryDC srcDc;
        srcDc.SelectObject( *srcBmp );
-       wxFont fnt( 9, wxDECORATIVE , wxNORMAL, wxNORMAL );
 
        bool hasText = ( mTextAlignment != NB_NO_TEXT ) &&
                           ( mLabelText.length() != 0 );
 
        bool hasText = ( mTextAlignment != NB_NO_TEXT ) &&
                           ( mLabelText.length() != 0 );
@@ -405,7 +398,7 @@ void wxNewBitmapButton::RenderLabelImage( wxBitmap*& destBmp, wxBitmap* srcBmp,
        {
                long txtWidth, txtHeight;
 
        {
                long txtWidth, txtHeight;
 
-               srcDc.SetFont( fnt );
+               srcDc.SetFont( wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT) );
                srcDc.GetTextExtent( mLabelText, &txtWidth, &txtHeight );
 
                if ( mTextAlignment == NB_ALIGN_TEXT_RIGHT )
                srcDc.GetTextExtent( mLabelText, &txtWidth, &txtHeight );
 
                if ( mTextAlignment == NB_ALIGN_TEXT_RIGHT )
@@ -448,8 +441,7 @@ void wxNewBitmapButton::RenderLabelImage( wxBitmap*& destBmp, wxBitmap* srcBmp,
        wxMemoryDC destDc;
        destDc.SelectObject( *destBmp );
 
        wxMemoryDC destDc;
        destDc.SelectObject( *destBmp );
 
-       // FOR NOW:: hard-coded label background
-       wxBrush grayBrush( wxColour(192,192,192), wxSOLID );
+       wxBrush grayBrush( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_3DFACE), wxSOLID );
        wxPen   nullPen( wxColour(0,0,0), 1, wxTRANSPARENT );
 
        destDc.SetBrush( grayBrush );
        wxPen   nullPen( wxColour(0,0,0), 1, wxTRANSPARENT );
 
        destDc.SetBrush( grayBrush );
@@ -465,10 +457,11 @@ void wxNewBitmapButton::RenderLabelImage( wxBitmap*& destBmp, wxBitmap* srcBmp,
 
        if ( hasImage )
        {
 
        if ( hasImage )
        {
+
                destDc.Blit( imgPos.x, imgPos.y, 
                destDc.Blit( imgPos.x, imgPos.y, 
-                                        srcBmp->GetWidth()+1,
-                                        srcBmp->GetHeight()+1,
-                                        &srcDc, 0,0, wxCOPY );
+                                srcBmp->GetWidth()+1,
+                                srcBmp->GetHeight()+1,
+                                &srcDc, 0,0, wxCOPY,TRUE );
        }
 
        if ( hasText )
        }
 
        if ( hasText )
@@ -484,19 +477,75 @@ void wxNewBitmapButton::RenderLabelImage( wxBitmap*& destBmp, wxBitmap* srcBmp,
                        pTopWnd = pParent;
                } while(1);
 
                        pTopWnd = pParent;
                } while(1);
 
-               destDc.SetFont( fnt );
+               destDc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT) );
 
 
-               // FOR NOW:: hard-coded text colors
-               destDc.SetTextForeground( wxColour(  0,  0,  0) );
-               destDc.SetTextBackground( wxColour(192,192,192) );
+               if( isEnabled ){
+                       destDc.SetTextForeground( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNTEXT) );
+               }else{
+                       destDc.SetTextForeground( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW) );
+               }
+               destDc.SetTextBackground( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE) );
 
                destDc.DrawText( mLabelText, txtPos.x, txtPos.y );
        }
 
 
                destDc.DrawText( mLabelText, txtPos.x, txtPos.y );
        }
 
-       if ( !isEnabled )
+
+       destDc.SetBrush( grayBrush );
+       destDc.SetPen( nullPen );
+
+       destDc.DrawRectangle( 0,0, destDim.x+1, destDim.y+1 );
+
+       if ( isPressed )
+       {
+               ++imgPos.x; ++imgPos.y;
+               ++txtPos.x; ++txtPos.y;
+       }
+
+       if ( hasImage )
+       {
+
+               destDc.Blit( imgPos.x, imgPos.y, 
+                                srcBmp->GetWidth()+1,
+                                srcBmp->GetHeight()+1,
+                                &srcDc, 0,0, wxCOPY,TRUE );
+       }
+
+       if ( hasText )
+       {
+               wxWindow* pTopWnd = this;
+
+               do
+               {
+                       wxWindow* pParent = pTopWnd->GetParent();
+
+                       if ( pParent == 0 ) break;
+
+                       pTopWnd = pParent;
+               } while(1);
+
+               destDc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT) );
+
+               if( isEnabled ){
+                       destDc.SetTextForeground( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNTEXT) );
+               }else{
+                       destDc.SetTextForeground( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW) );
+               }
+               destDc.SetTextBackground( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE) );
+
+               destDc.DrawText( mLabelText, txtPos.x, txtPos.y );
+       }
+
+       if ( !isEnabled ){
        
        
+#ifdef __WXMSW__ // This is currently MSW specific
                greay_out_image_on_dc( destDc, destDim.x, destDim.y );
                greay_out_image_on_dc( destDc, destDim.x, destDim.y );
-
+#else
+               wxBrush checkerBrush( wxBitmap( (const char*)_gDisableImage,8,8) );
+               checkerBrush.SetColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE ) );
+               destDc.SetBrush( checkerBrush );
+               destDc.DrawRectangle( imgPos.x, imgPos.y, srcBmp->GetWidth()+1, srcBmp->GetHeight()+1);
+#endif
+       }
        // adjust button size to fit the new dimensions of the label
        if ( !mSizeIsSet && 0 )
        {
        // adjust button size to fit the new dimensions of the label
        if ( !mSizeIsSet && 0 )
        {
@@ -507,6 +556,17 @@ void wxNewBitmapButton::RenderLabelImage( wxBitmap*& destBmp, wxBitmap* srcBmp,
                        );
        }
 }
                        );
        }
 }
+void wxNewBitmapButton::RenderAllLabelImages()
+{
+       if( !mIsCreated ) return;
+       RenderLabelImage( mpDisabledImg, &mDepressedBmp, FALSE );
+       RenderLabelImage( mpPressedImg,   &mDepressedBmp, TRUE, TRUE );
+       RenderLabelImage( mpDepressedImg, &mDepressedBmp, TRUE, FALSE );
+       if ( mHasFocusedBmp ){
+               RenderLabelImage( mpFocusedImg, &mFocusedBmp, TRUE, FALSE );
+       }
+}
+       
 
 void wxNewBitmapButton::RenderLabelImages()
 {
 
 void wxNewBitmapButton::RenderLabelImages()
 {
@@ -576,7 +636,8 @@ void wxNewBitmapButton::SetLabel(const wxBitmap& labelBitmap,
        mLabelText    = labelText;
        mDepressedBmp = labelBitmap;
 
        mLabelText    = labelText;
        mDepressedBmp = labelBitmap;
 
-       RenderLabelImages();
+       //RenderLabelImages();
+       RenderAllLabelImages();
 }
 
 void wxNewBitmapButton::SetAlignments( int alignText,
 }
 
 void wxNewBitmapButton::SetAlignments( int alignText,
@@ -591,7 +652,8 @@ void wxNewBitmapButton::SetAlignments( int alignText,
        mTextAlignment  = alignText;
        mTextToLabelGap = textToLabelGap;
 
        mTextAlignment  = alignText;
        mTextToLabelGap = textToLabelGap;
 
-       RenderLabelImages();
+       //RenderLabelImages();
+       RenderAllLabelImages();
 }
 
 // event handlers
 }
 
 // event handlers
@@ -640,6 +702,8 @@ bool wxNewBitmapButton::IsInWindow( int x, int y )
 
 void wxNewBitmapButton::OnMouseMove( wxMouseEvent& event )
 {
 
 void wxNewBitmapButton::OnMouseMove( wxMouseEvent& event )
 {
+       mPrevPressedState=mIsPressed;
+       mPrevInFocusState=mIsInFocus;
        if ( !mIsInFocus && IsInWindow( event.m_x, event.m_y ) )
        {
                if ( !mDragStarted )
        if ( !mIsInFocus && IsInWindow( event.m_x, event.m_y ) )
        {
                if ( !mDragStarted )
@@ -663,16 +727,11 @@ void wxNewBitmapButton::OnMouseMove( wxMouseEvent& event )
                        mIsPressed = TRUE;
                else
                        mIsPressed = FALSE;
                        mIsPressed = TRUE;
                else
                        mIsPressed = FALSE;
-
-               if ( mIsPressed != mPrevPressedState )
-                       
-                       Refresh();
-
-               mPrevPressedState = mIsPressed;
        }
 
        }
 
-       // FOR NOW::
-       Refresh();
+       if((mIsPressed != mPrevPressedState)||(mIsInFocus!=mPrevInFocusState)){
+               Refresh();
+       }
 }
 
 void wxNewBitmapButton::OnSize( wxSizeEvent& event )
 }
 
 void wxNewBitmapButton::OnSize( wxSizeEvent& event )
@@ -682,6 +741,7 @@ void wxNewBitmapButton::OnSize( wxSizeEvent& event )
 
 void wxNewBitmapButton::Reshape( )
 {
 
 void wxNewBitmapButton::Reshape( )
 {
+       
        bool wasCreated = mIsCreated;
        mIsCreated = TRUE;
 
        bool wasCreated = mIsCreated;
        mIsCreated = TRUE;
 
@@ -697,7 +757,8 @@ void wxNewBitmapButton::Reshape( )
                        //wxMessageBox("Image Loaded!!!");
                }
 
                        //wxMessageBox("Image Loaded!!!");
                }
 
-               RenderLabelImages();
+               //RenderLabelImages();
+               RenderAllLabelImages();
 
                wxBitmap* pCurImg = GetStateLabel();
 
 
                wxBitmap* pCurImg = GetStateLabel();
 
@@ -717,7 +778,7 @@ void wxNewBitmapButton::DrawLabel( wxDC& dc )
                wxSizeEvent evt;
                OnSize( evt ); // fake it up!
 
                wxSizeEvent evt;
                OnSize( evt ); // fake it up!
 
-               RenderLabelImages();
+               //RenderLabelImages();
                pCurBmp = GetStateLabel();
        }
 
                pCurBmp = GetStateLabel();
        }
 
@@ -738,7 +799,7 @@ void wxNewBitmapButton::OnPaint( wxPaintEvent& event )
        wxPaintDC dc(this);
 
        // first, make sure images for current state are prepared
        wxPaintDC dc(this);
 
        // first, make sure images for current state are prepared
-       RenderLabelImages();
+       //RenderLabelImages();
 
        DrawLabel( dc );
 
 
        DrawLabel( dc );