X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/5515f252cb0a46c2a200a14f0692e6db7c3aea47..d1a6e2b7a103d761fbc15e0e9c1c20ab53a849d9:/contrib/src/fl/newbmpbtn.cpp?ds=inline diff --git a/contrib/src/fl/newbmpbtn.cpp b/contrib/src/fl/newbmpbtn.cpp index 5cfdd6d637..dbec38e40d 100644 --- a/contrib/src/fl/newbmpbtn.cpp +++ b/contrib/src/fl/newbmpbtn.cpp @@ -1,12 +1,12 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: No names yet. -// Purpose: Contrib. demo +// Name: newbmpbtn.cpp +// Purpose: wxNewBitmapButton enhanced bitmap button class. // Author: Aleksandras Gluchovas // Modified by: // Created: ??/09/98 // RCS-ID: $Id$ // Copyright: (c) Aleksandras Gluchovas -// Licence: wxWindows license +// Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// #ifdef __GNUG__ @@ -27,6 +27,10 @@ #include "wx/fl/newbmpbtn.h" #include "wx/utils.h" // import wxMin,wxMax macros +#ifdef __WXMSW__ +#include "wx/msw/private.h" +#endif + ///////////// button-label rendering helpers ////////////////// static int* create_array( int width, int height, int fill = 0 ) @@ -142,7 +146,7 @@ static void gray_out_pixmap( int* src, int* dest, int width, int height ) } while (1); } -// alg. for making the image look "grayed" (e.g. disabled button) +// algorithm for making the image look "grayed" (e.g. disabled button) // NOTE:: used GetPixel(), which is Windows-Only! void gray_out_image_on_dc( wxDC& dc, int width, int height ) @@ -151,8 +155,8 @@ void gray_out_image_on_dc( wxDC& dc, int width, int height ) wxColour bgCol; dc.GetPixel( 0, 0, &bgCol ); - wxPen darkPen ( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW),1, wxSOLID ); - wxPen lightPen( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHIGHLIGHT),1, wxSOLID ); + wxPen darkPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW),1, wxSOLID ); + wxPen lightPen( wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT),1, wxSOLID ); wxPen bgPen ( bgCol, 1, wxSOLID ); int* src = create_array( width, height, MASK_BG ); @@ -198,15 +202,18 @@ void gray_out_image_on_dc( wxDC& dc, int width, int height ) /////////////////////////////// -/***** Impelementation for class wxNewBitmapButton *****/ +/***** Implementation for class wxNewBitmapButton *****/ IMPLEMENT_DYNAMIC_CLASS(wxNewBitmapButton, wxPanel) BEGIN_EVENT_TABLE( wxNewBitmapButton, wxPanel ) - EVT_LEFT_DOWN( wxNewBitmapButton::OnLButtonDown ) - EVT_LEFT_UP ( wxNewBitmapButton::OnLButtonUp ) - EVT_MOTION ( wxNewBitmapButton::OnMouseMove ) + EVT_LEFT_DOWN ( wxNewBitmapButton::OnLButtonDown ) + EVT_LEFT_UP ( wxNewBitmapButton::OnLButtonUp ) +// EVT_LEFT_DCLICK ( wxNewBitmapButton::OnLButtonDClick ) + EVT_LEFT_DCLICK ( wxNewBitmapButton::OnLButtonDown ) + EVT_ENTER_WINDOW( wxNewBitmapButton::OnMouseEnter ) + EVT_LEAVE_WINDOW( wxNewBitmapButton::OnMouseLeave ) EVT_SIZE ( wxNewBitmapButton::OnSize ) EVT_PAINT( wxNewBitmapButton::OnPaint ) @@ -215,13 +222,15 @@ BEGIN_EVENT_TABLE( wxNewBitmapButton, wxPanel ) EVT_ERASE_BACKGROUND( wxNewBitmapButton::OnEraseBackground ) + EVT_IDLE(wxNewBitmapButton::OnIdle) + END_EVENT_TABLE() -wxNewBitmapButton::wxNewBitmapButton( const wxBitmap& labelBitmap, +wxNewBitmapButton::wxNewBitmapButton( const wxBitmap& labelBitmap, const wxString& labelText, int alignText, bool isFlat, - int firedEventType, + int firedEventType, int marginX, int marginY, int textToLabelGap, @@ -242,18 +251,17 @@ wxNewBitmapButton::wxNewBitmapButton( const wxBitmap& labelBitmap, mpFocusedImg ( NULL ), - mDragStarted ( FALSE ), - mIsPressed ( FALSE ), - mIsInFocus( FALSE ), - mPrevPressedState( FALSE ), - mPrevInFocusState( FALSE ), + mDragStarted ( FALSE ), + mIsPressed ( FALSE ), + mIsInFocus ( FALSE ), + mIsToggled ( FALSE ), mHasFocusedBmp( FALSE ), mFiredEventType( firedEventType ), mBlackPen( wxColour( 0, 0, 0), 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 ), + mDarkPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxSOLID ), + mGrayPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE), 1, wxSOLID ), + mLightPen( wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT), 1, wxSOLID ), mIsCreated( FALSE ), mSizeIsSet( FALSE ) @@ -266,11 +274,11 @@ wxNewBitmapButton::wxNewBitmapButton( const wxString& bitmapFileName, const wxString& labelText, int alignText, bool isFlat, - int firedEventType, - int marginX, - int marginY, - int textToLabelGap, - bool isSticky) + int WXUNUSED(firedEventType), + int WXUNUSED(marginX), + int WXUNUSED(marginY), + int WXUNUSED(textToLabelGap), + bool WXUNUSED(isSticky)) : mTextToLabelGap ( 2 ), mMarginX( 2 ), @@ -287,18 +295,17 @@ wxNewBitmapButton::wxNewBitmapButton( const wxString& bitmapFileName, mpDisabledImg ( NULL ), mpFocusedImg ( NULL ), - mDragStarted ( FALSE ), - mIsPressed ( FALSE ), - mIsInFocus ( FALSE ), - mPrevPressedState( FALSE ), - mPrevInFocusState( FALSE ), + mDragStarted ( FALSE ), + mIsPressed ( FALSE ), + mIsInFocus ( FALSE ), + mIsToggled ( FALSE ), mHasFocusedBmp( FALSE ), mFiredEventType( wxEVT_COMMAND_MENU_SELECTED ), mBlackPen( wxColour( 0, 0, 0), 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 ), + mDarkPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxSOLID ), + mGrayPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE), 1, wxSOLID ), + mLightPen( wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT), 1, wxSOLID ), mIsCreated( FALSE ), mSizeIsSet( FALSE ) @@ -318,11 +325,11 @@ void wxNewBitmapButton::DrawShade( int outerLevel, { wxBitmap* pBmp = GetStateLabel(); - int x = mMarginX - (outerLevel + 1); - int y = mMarginY - (outerLevel + 1); + int x = mMarginX - (outerLevel + 2); + int y = mMarginY - (outerLevel + 2); - int height = pBmp->GetHeight() + (outerLevel + 1)*2 - 1; - int width = pBmp->GetWidth() + (outerLevel + 1)*2 - 1; + int height = pBmp->GetHeight() + (outerLevel + 2)*2 - 1; + int width = pBmp->GetWidth() + (outerLevel + 2)*2 - 1; dc.SetPen( upperLeftSidePen ); dc.DrawLine( x,y, x + width, y ); @@ -382,7 +389,7 @@ void wxNewBitmapButton::RenderLabelImage( wxBitmap*& destBmp, wxBitmap* srcBmp, { if ( destBmp != 0 ) return; - // render lables on-demand + // render labels on-demand wxMemoryDC srcDc; srcDc.SelectObject( *srcBmp ); @@ -400,14 +407,14 @@ void wxNewBitmapButton::RenderLabelImage( wxBitmap*& destBmp, wxBitmap* srcBmp, { long txtWidth, txtHeight; - srcDc.SetFont( wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT) ); + srcDc.SetFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT) ); srcDc.GetTextExtent( mLabelText, &txtWidth, &txtHeight ); if ( mTextAlignment == NB_ALIGN_TEXT_RIGHT ) { destDim.x = srcBmp->GetWidth() + 2*mTextToLabelGap + txtWidth; - destDim.y = + destDim.y = wxMax( srcBmp->GetHeight(), txtHeight ); txtPos.x = srcBmp->GetWidth() + mTextToLabelGap; @@ -418,7 +425,7 @@ void wxNewBitmapButton::RenderLabelImage( wxBitmap*& destBmp, wxBitmap* srcBmp, else if ( mTextAlignment == NB_ALIGN_TEXT_BOTTOM ) { - destDim.x = + destDim.x = wxMax( srcBmp->GetWidth(), txtWidth ); destDim.y = srcBmp->GetHeight() + mTextToLabelGap + txtHeight; @@ -428,9 +435,9 @@ void wxNewBitmapButton::RenderLabelImage( wxBitmap*& destBmp, wxBitmap* srcBmp, imgPos.x = (destDim.x - srcBmp->GetWidth())/2; imgPos.y = 0; } - else + else { - wxFAIL_MSG("Unsupported FL alignment type detected in wxNewBitmapButton::RenderLabelImage()"); + wxFAIL_MSG(wxT("Unsupported FL alignment type detected in wxNewBitmapButton::RenderLabelImage()")); } } else @@ -446,7 +453,7 @@ void wxNewBitmapButton::RenderLabelImage( wxBitmap*& destBmp, wxBitmap* srcBmp, wxMemoryDC destDc; destDc.SelectObject( *destBmp ); - wxBrush grayBrush( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_3DFACE), wxSOLID ); + wxBrush grayBrush( wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE), wxSOLID ); wxPen nullPen( wxColour(0,0,0), 1, wxTRANSPARENT ); destDc.SetBrush( grayBrush ); @@ -463,57 +470,7 @@ void wxNewBitmapButton::RenderLabelImage( wxBitmap*& destBmp, wxBitmap* srcBmp, 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 ); - } - - - 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, + destDc.Blit( imgPos.x, imgPos.y, srcBmp->GetWidth()+1, srcBmp->GetHeight()+1, &srcDc, 0,0, wxCOPY,TRUE ); @@ -533,28 +490,28 @@ void wxNewBitmapButton::RenderLabelImage( wxBitmap*& destBmp, wxBitmap* srcBmp, pTopWnd = pParent; } while (1); - destDc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT) ); + destDc.SetFont( wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT) ); if ( isEnabled ) { - destDc.SetTextForeground( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNTEXT) ); + destDc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT) ); } else { - destDc.SetTextForeground( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW) ); + destDc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW) ); } - destDc.SetTextBackground( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE) ); + destDc.SetTextBackground( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE) ); destDc.DrawText( mLabelText, txtPos.x, txtPos.y ); } if ( !isEnabled ){ - + #ifdef __WXMSW__ // This is currently MSW specific gray_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 ) ); + checkerBrush.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) ); destDc.SetBrush( checkerBrush ); destDc.DrawRectangle( imgPos.x, imgPos.y, srcBmp->GetWidth()+1, srcBmp->GetHeight()+1); #endif @@ -562,13 +519,20 @@ void wxNewBitmapButton::RenderLabelImage( wxBitmap*& destBmp, wxBitmap* srcBmp, // adjust button size to fit the new dimensions of the label if ( !mSizeIsSet && 0 ) { - mSizeIsSet = TRUE; - SetSize( -1,-1, + mSizeIsSet = TRUE; + SetSize( -1,-1, destBmp->GetWidth() + mMarginX*2, - destBmp->GetHeight() + mMarginY*2, 0 + destBmp->GetHeight() + mMarginY*2, 0 ); } + destDc.SelectObject( wxNullBitmap ); + +#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) + // Map to system colours + (void) wxToolBar::MapBitmap(destBmp->GetHBITMAP(), destBmp->GetWidth(), destBmp->GetHeight()); +#endif } + void wxNewBitmapButton::RenderAllLabelImages() { if ( !mIsCreated ) @@ -581,7 +545,7 @@ void wxNewBitmapButton::RenderAllLabelImages() RenderLabelImage( mpFocusedImg, &mFocusedBmp, TRUE, FALSE ); } } - + void wxNewBitmapButton::RenderLabelImages() { @@ -594,7 +558,7 @@ void wxNewBitmapButton::RenderLabelImages() } else - if ( mIsPressed ) + if ( mIsPressed ) RenderLabelImage( mpPressedImg, &mDepressedBmp, TRUE, TRUE ); else @@ -611,13 +575,49 @@ void wxNewBitmapButton::RenderLabelImages() } } +bool wxNewBitmapButton::Toggle(bool enable) +{ + if ( mIsToggled == enable ) + { + return FALSE; + } + + mIsToggled = enable; + Refresh(); + + return TRUE; +} + +bool wxNewBitmapButton::Enable(bool enable) +{ + if ( enable != m_isEnabled ) + { + if ( mIsInFocus ) + { + mIsInFocus = FALSE; + } + + if ( mIsPressed ) + { + mIsPressed = FALSE; + } + + Refresh(); + } + + return wxPanel::Enable( enable ); +} + void wxNewBitmapButton::DrawDecorations( wxDC& dc ) { if ( mIsFlat ) { DrawShade( 1, dc, mGrayPen, mGrayPen ); - - if ( mIsInFocus ) + if ( mIsToggled ) + { + DrawShade( 0, dc, mDarkPen, mLightPen ); + } + else if ( mIsInFocus ) { if ( mIsPressed ) DrawShade( 0, dc, mDarkPen, mLightPen ); @@ -629,7 +629,7 @@ void wxNewBitmapButton::DrawDecorations( wxDC& dc ) } else { - if ( mIsPressed ) + if ( mIsPressed || mIsToggled ) { DrawShade( 0, dc, mDarkPen, mGrayPen ); DrawShade( 1, dc, mBlackPen, mLightPen ); @@ -642,7 +642,7 @@ void wxNewBitmapButton::DrawDecorations( wxDC& dc ) } } -void wxNewBitmapButton::SetLabel(const wxBitmap& labelBitmap, +void wxNewBitmapButton::SetLabel(const wxBitmap& labelBitmap, const wxString& labelText ) { DestroyLabels(); @@ -672,15 +672,11 @@ void wxNewBitmapButton::SetAlignments( int alignText, // event handlers -void wxNewBitmapButton::OnLButtonDown( wxMouseEvent& event ) +void wxNewBitmapButton::OnLButtonDown( wxMouseEvent& WXUNUSED(event) ) { - mPrevPressedState = FALSE; mDragStarted = TRUE; mIsPressed = TRUE; Refresh(); - - if ( !mIsInFocus ) - CaptureMouse(); } void wxNewBitmapButton::OnLButtonUp( wxMouseEvent& event ) @@ -690,11 +686,8 @@ void wxNewBitmapButton::OnLButtonUp( wxMouseEvent& event ) mDragStarted = FALSE; mIsPressed = FALSE; - mIsInFocus = FALSE; Refresh(); - ReleaseMouse(); - if ( IsInWindow( event.m_x, event.m_y ) ) { // fire event, if mouse was released @@ -714,50 +707,42 @@ bool wxNewBitmapButton::IsInWindow( int x, int y ) y < height ); } -void wxNewBitmapButton::OnMouseMove( wxMouseEvent& event ) +void wxNewBitmapButton::OnMouseEnter( wxMouseEvent& WXUNUSED(event) ) { - mPrevPressedState=mIsPressed; - mPrevInFocusState=mIsInFocus; - if ( !mIsInFocus && IsInWindow( event.m_x, event.m_y ) ) - { - if ( !mDragStarted ) - CaptureMouse(); + bool prevIsInFocus = mIsInFocus; + if ( !mIsInFocus ) + { mIsInFocus = TRUE; } - else - if ( mIsInFocus && !IsInWindow( event.m_x, event.m_y ) ) + if ( prevIsInFocus != mIsInFocus ) { - mIsInFocus = FALSE; - - if ( !mDragStarted ) - ReleaseMouse(); + Refresh(); } +} - if ( mDragStarted ) +void wxNewBitmapButton::OnMouseLeave( wxMouseEvent& WXUNUSED(event) ) +{ + bool prevIsInFocus = mIsInFocus; + bool prevIsPressed = mIsPressed; + if ( mIsInFocus ) { - if ( IsInWindow( event.m_x, event.m_y ) ) - - mIsPressed = TRUE; - else - mIsPressed = FALSE; + mIsInFocus = FALSE; + mIsPressed = FALSE; } - - if ((mIsPressed != mPrevPressedState) || - (mIsInFocus!=mPrevInFocusState)) + if ( prevIsInFocus != mIsInFocus || prevIsPressed != mIsPressed ) { Refresh(); } } -void wxNewBitmapButton::OnSize( wxSizeEvent& event ) +void wxNewBitmapButton::OnSize( wxSizeEvent& WXUNUSED(event) ) { //Reshape(); } void wxNewBitmapButton::Reshape( ) -{ - +{ bool wasCreated = mIsCreated; mIsCreated = TRUE; @@ -766,7 +751,7 @@ void wxNewBitmapButton::Reshape( ) // in the case of loading button from stream, check if we // have non-empty image-file name, load if possible - if ( mImageFileName != "" ) + if ( mImageFileName != wxT("") ) { mDepressedBmp.LoadFile( mImageFileName, mImageFileType ); @@ -810,7 +795,7 @@ void wxNewBitmapButton::DrawLabel( wxDC& dc ) mdc.SelectObject( wxNullBitmap ); } -void wxNewBitmapButton::OnPaint( wxPaintEvent& event ) +void wxNewBitmapButton::OnPaint( wxPaintEvent& WXUNUSED(event) ) { wxPaintDC dc(this); @@ -822,15 +807,44 @@ void wxNewBitmapButton::OnPaint( wxPaintEvent& event ) DrawDecorations( dc ); } -void wxNewBitmapButton::OnEraseBackground( wxEraseEvent& event ) +void wxNewBitmapButton::OnEraseBackground( wxEraseEvent& WXUNUSED(event) ) { // do nothing } -void wxNewBitmapButton::OnKillFocus( wxFocusEvent& event ) +void wxNewBitmapButton::OnKillFocus( wxFocusEvent& WXUNUSED(event) ) { // useless - wxMessageBox("kill-focus for button!"); + wxMessageBox(wxT("kill-focus for button!")); +} + +// ---------------------------------------------------------------------------- +// UI updates +// ---------------------------------------------------------------------------- + +void wxNewBitmapButton::OnIdle(wxIdleEvent& event) +{ + DoButtonUpdate(); + + event.Skip(); } +// Do the toolbar button updates (check for EVT_UPDATE_UI handlers) +void wxNewBitmapButton::DoButtonUpdate() +{ + wxUpdateUIEvent event(GetId()); + event.SetEventObject(this); + + if ( GetParent()->ProcessEvent(event) ) + { + if ( event.GetSetEnabled() ) + { + bool enabled = event.GetEnabled(); + if ( enabled != IsEnabled() ) + Enable( enabled ); + } + if ( event.GetSetChecked() ) + Toggle( event.GetChecked() ); + } +}