1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxNewBitmapButton enhanced bitmap button class.
4 // Author: Aleksandras Gluchovas
8 // Copyright: (c) Aleksandras Gluchovas
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "newbmpbtn.h"
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
27 #include "wx/fl/newbmpbtn.h"
28 #include "wx/utils.h" // import wxMin,wxMax macros
30 ///////////// button-label rendering helpers //////////////////
32 static int* create_array( int width
, int height
, int fill
= 0 )
34 int* array
= new int[width
*height
];
36 int len
= width
*height
;
38 for ( i
= 0; i
!= len
; ++i
)
44 #define GET_ELEM(array,x,y) (array[width*(y)+(x)])
46 #define MIN_COLOR_DIFF 10
48 #define IS_IN_ARRAY(x,y) ( (x) < width && (y) < height && (x) >= 0 && (y) >= 0 )
50 #define GET_RED(col) col & 0xFF
51 #define GET_GREEN(col) (col >> 8) & 0xFF
52 #define GET_BLUE(col) (col >> 16) & 0xFF
54 #define MAKE_INT_COLOR(red,green,blue) ( (red) | \
55 ( ( (green) << 8 ) & 0xFF00 ) | \
56 ( ( (blue) << 16) & 0xFF0000) \
59 #define IS_GREATER(col1,col2) ( ( (GET_RED(col1) ) > (GET_RED(col2) ) + MIN_COLOR_DIFF ) && \
60 ( (GET_GREEN(col1)) > (GET_GREEN(col2)) + MIN_COLOR_DIFF ) && \
61 ( (GET_BLUE(col1) ) > (GET_BLUE(col2) ) + MIN_COLOR_DIFF ) \
68 // helper function, used internally
70 static void gray_out_pixmap( int* src
, int* dest
, int width
, int height
)
72 // assuming the pixels along the edges are of the background color
79 int cur
= GET_ELEM(src
,x
,y
);
82 if ( IS_IN_ARRAY(x
-1,y
-1) )
84 int upperElem
= GET_ELEM(src
,x
-1,y
-1);
86 // if the upper element is lighter than current
87 if ( IS_GREATER(upperElem
,cur
) )
89 GET_ELEM(dest
,x
,y
) = MASK_DARK
;
92 // if the current element is ligher than the upper
93 if ( IS_GREATER(cur
,upperElem
) )
95 GET_ELEM(dest
,x
,y
) = MASK_LIGHT
;
99 if ( GET_ELEM(dest
,x
-1,y
-1) == MASK_LIGHT
)
101 GET_ELEM(dest
,x
,y
) = MASK_BG
;
103 if ( GET_ELEM(dest
,x
-1,y
-1 ) == MASK_DARK
)
105 GET_ELEM(dest
,x
,y
) = MASK_DARK
;
107 GET_ELEM(dest
,x
,y
) = MASK_BG
;
113 if ( IS_IN_ARRAY(x
+1,y
-1) )
120 while ( IS_IN_ARRAY(x
-1,y
+1) )
126 if ( IS_IN_ARRAY(x
,y
+1) )
133 if ( IS_IN_ARRAY(x
+1,y
) )
145 // algorithm for making the image look "grayed" (e.g. disabled button)
146 // NOTE:: used GetPixel(), which is Windows-Only!
148 void gray_out_image_on_dc( wxDC
& dc
, int width
, int height
)
150 // assuming the pixels along the edges are of the background color
152 dc
.GetPixel( 0, 0, &bgCol
);
154 wxPen
darkPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
),1, wxSOLID
);
155 wxPen
lightPen( wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
),1, wxSOLID
);
156 wxPen
bgPen ( bgCol
, 1, wxSOLID
);
158 int* src
= create_array( width
, height
, MASK_BG
);
159 int* dest
= create_array( width
, height
, MASK_BG
);
162 for ( y
= 0; y
!= height
; ++y
)
164 for ( x
= 0; x
!= width
; ++x
)
167 dc
.GetPixel( x
,y
, &col
);
169 GET_ELEM(src
,x
,y
) = MAKE_INT_COLOR( col
.Red(), col
.Green(), col
.Blue() );
172 gray_out_pixmap( src
, dest
, width
, height
);
174 for ( y
= 0; y
!= height
; ++y
)
176 for ( x
= 0; x
!= width
; ++x
)
178 int mask
= GET_ELEM(dest
,x
,y
);
182 case MASK_BG
: { dc
.SetPen( bgPen
);
183 dc
.DrawPoint( x
,y
); break;
185 case MASK_DARK
: { dc
.SetPen( darkPen
);
186 dc
.DrawPoint( x
,y
); break;
188 case MASK_LIGHT
: { dc
.SetPen( lightPen
);
189 dc
.DrawPoint( x
,y
); break;
199 ///////////////////////////////
201 /***** Implementation for class wxNewBitmapButton *****/
203 IMPLEMENT_DYNAMIC_CLASS(wxNewBitmapButton
, wxPanel
)
205 BEGIN_EVENT_TABLE( wxNewBitmapButton
, wxPanel
)
207 EVT_LEFT_DOWN ( wxNewBitmapButton::OnLButtonDown
)
208 EVT_LEFT_UP ( wxNewBitmapButton::OnLButtonUp
)
209 EVT_LEFT_DCLICK ( wxNewBitmapButton::OnLButtonDClick
)
210 EVT_ENTER_WINDOW( wxNewBitmapButton::OnMouseEnter
)
211 EVT_LEAVE_WINDOW( wxNewBitmapButton::OnMouseLeave
)
213 EVT_SIZE ( wxNewBitmapButton::OnSize
)
214 EVT_PAINT( wxNewBitmapButton::OnPaint
)
216 //EVT_KILL_FOCUS( wxNewBitmapButton::OnKillFocus )
218 EVT_ERASE_BACKGROUND( wxNewBitmapButton::OnEraseBackground
)
222 wxNewBitmapButton::wxNewBitmapButton( const wxBitmap
& labelBitmap
,
223 const wxString
& labelText
,
231 : mTextToLabelGap ( textToLabelGap
),
234 mTextAlignment( alignText
),
235 mIsSticky( isSticky
),
237 mLabelText( labelText
),
238 mImageFileType( wxBITMAP_TYPE_INVALID
),
239 mDepressedBmp( labelBitmap
),
241 mpDepressedImg( NULL
),
242 mpPressedImg ( NULL
),
243 mpDisabledImg ( NULL
),
244 mpFocusedImg ( NULL
),
247 mDragStarted ( FALSE
),
248 mIsPressed ( FALSE
),
250 mHasFocusedBmp( FALSE
),
251 mFiredEventType( firedEventType
),
253 mBlackPen( wxColour( 0, 0, 0), 1, wxSOLID
),
254 mDarkPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
),
255 mGrayPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
), 1, wxSOLID
),
256 mLightPen( wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
), 1, wxSOLID
),
264 wxNewBitmapButton::wxNewBitmapButton( const wxString
& bitmapFileName
,
265 const wxBitmapType bitmapFileType
,
266 const wxString
& labelText
,
275 : mTextToLabelGap ( 2 ),
278 mTextAlignment( alignText
),
281 mLabelText( labelText
),
282 mImageFileName( bitmapFileName
),
283 mImageFileType( bitmapFileType
),
285 mpDepressedImg( NULL
),
286 mpPressedImg ( NULL
),
287 mpDisabledImg ( NULL
),
288 mpFocusedImg ( NULL
),
290 mDragStarted ( FALSE
),
291 mIsPressed ( FALSE
),
292 mIsInFocus ( FALSE
),
293 mHasFocusedBmp( FALSE
),
294 mFiredEventType( wxEVT_COMMAND_MENU_SELECTED
),
296 mBlackPen( wxColour( 0, 0, 0), 1, wxSOLID
),
297 mDarkPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
),
298 mGrayPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
), 1, wxSOLID
),
299 mLightPen( wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
), 1, wxSOLID
),
307 wxNewBitmapButton::~wxNewBitmapButton(void)
312 void wxNewBitmapButton::DrawShade( int outerLevel
,
314 wxPen
& upperLeftSidePen
,
315 wxPen
& lowerRightSidePen
)
317 wxBitmap
* pBmp
= GetStateLabel();
319 int x
= mMarginX
- (outerLevel
+ 1);
320 int y
= mMarginY
- (outerLevel
+ 1);
322 int height
= pBmp
->GetHeight() + (outerLevel
+ 1)*2 - 1;
323 int width
= pBmp
->GetWidth() + (outerLevel
+ 1)*2 - 1;
325 dc
.SetPen( upperLeftSidePen
);
326 dc
.DrawLine( x
,y
, x
+ width
, y
);
327 dc
.DrawLine( x
,y
, x
, y
+ height
);
329 dc
.SetPen( lowerRightSidePen
);
330 dc
.DrawLine( x
+ width
, y
, x
+ width
, y
+ height
+ 1 );
331 dc
.DrawLine( x
, y
+ height
, x
+ width
, y
+ height
);
334 void wxNewBitmapButton::DestroyLabels()
336 if ( mpDepressedImg
) delete mpDepressedImg
;
337 if ( mpPressedImg
) delete mpPressedImg
;
338 if ( mpDisabledImg
) delete mpDisabledImg
;
339 if ( mpFocusedImg
) delete mpFocusedImg
;
341 mpDepressedImg
= NULL
;
343 mpDisabledImg
= NULL
;
347 wxBitmap
* wxNewBitmapButton::GetStateLabel()
359 if ( mHasFocusedBmp
)
363 return mpDepressedImg
;
366 return mpDepressedImg
;
370 return mpDisabledImg
;
373 static const unsigned char _gDisableImage
[] = { 0x55,0xAA,0x55,0xAA,
378 void wxNewBitmapButton::RenderLabelImage( wxBitmap
*& destBmp
, wxBitmap
* srcBmp
,
379 bool isEnabled
, bool isPressed
)
381 if ( destBmp
!= 0 ) return;
383 // render labels on-demand
386 srcDc
.SelectObject( *srcBmp
);
388 bool hasText
= ( mTextAlignment
!= NB_NO_TEXT
) &&
389 ( mLabelText
.length() != 0 );
391 bool hasImage
= (mTextAlignment
!= NB_NO_IMAGE
);
399 long txtWidth
, txtHeight
;
401 srcDc
.SetFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
) );
402 srcDc
.GetTextExtent( mLabelText
, &txtWidth
, &txtHeight
);
404 if ( mTextAlignment
== NB_ALIGN_TEXT_RIGHT
)
406 destDim
.x
= srcBmp
->GetWidth() + 2*mTextToLabelGap
+ txtWidth
;
409 wxMax( srcBmp
->GetHeight(), txtHeight
);
411 txtPos
.x
= srcBmp
->GetWidth() + mTextToLabelGap
;
412 txtPos
.y
= (destDim
.y
- txtHeight
)/2;
414 imgPos
.y
= (destDim
.y
- srcBmp
->GetHeight())/2;
417 if ( mTextAlignment
== NB_ALIGN_TEXT_BOTTOM
)
420 wxMax( srcBmp
->GetWidth(), txtWidth
);
422 destDim
.y
= srcBmp
->GetHeight() + mTextToLabelGap
+ txtHeight
;
424 txtPos
.x
= (destDim
.x
- txtWidth
)/2;
425 txtPos
.y
= srcBmp
->GetHeight() + mTextToLabelGap
;
426 imgPos
.x
= (destDim
.x
- srcBmp
->GetWidth())/2;
431 wxFAIL_MSG("Unsupported FL alignment type detected in wxNewBitmapButton::RenderLabelImage()");
438 destDim
.x
= srcBmp
->GetWidth();
439 destDim
.y
= srcBmp
->GetHeight();
442 destBmp
= new wxBitmap( int(destDim
.x
), int(destDim
.y
) );
445 destDc
.SelectObject( *destBmp
);
447 wxBrush
grayBrush( wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE
), wxSOLID
);
448 wxPen
nullPen( wxColour(0,0,0), 1, wxTRANSPARENT
);
450 destDc
.SetBrush( grayBrush
);
451 destDc
.SetPen( nullPen
);
453 destDc
.DrawRectangle( 0,0, destDim
.x
+1, destDim
.y
+1 );
457 ++imgPos
.x
; ++imgPos
.y
;
458 ++txtPos
.x
; ++txtPos
.y
;
464 destDc
.Blit( imgPos
.x
, imgPos
.y
,
465 srcBmp
->GetWidth()+1,
466 srcBmp
->GetHeight()+1,
467 &srcDc
, 0,0, wxCOPY
,TRUE
);
472 wxWindow
* pTopWnd
= this;
476 wxWindow
* pParent
= pTopWnd
->GetParent();
484 destDc
.SetFont( wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
) );
488 destDc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT
) );
492 destDc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
) );
494 destDc
.SetTextBackground( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
) );
496 destDc
.DrawText( mLabelText
, txtPos
.x
, txtPos
.y
);
501 #ifdef __WXMSW__ // This is currently MSW specific
502 gray_out_image_on_dc( destDc
, destDim
.x
, destDim
.y
);
504 wxBrush
checkerBrush( wxBitmap( (const char*)_gDisableImage
,8,8) );
505 checkerBrush
.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
) );
506 destDc
.SetBrush( checkerBrush
);
507 destDc
.DrawRectangle( imgPos
.x
, imgPos
.y
, srcBmp
->GetWidth()+1, srcBmp
->GetHeight()+1);
510 // adjust button size to fit the new dimensions of the label
511 if ( !mSizeIsSet
&& 0 )
515 destBmp
->GetWidth() + mMarginX
*2,
516 destBmp
->GetHeight() + mMarginY
*2, 0
520 void wxNewBitmapButton::RenderAllLabelImages()
524 RenderLabelImage( mpDisabledImg
, &mDepressedBmp
, FALSE
);
525 RenderLabelImage( mpPressedImg
, &mDepressedBmp
, TRUE
, TRUE
);
526 RenderLabelImage( mpDepressedImg
, &mDepressedBmp
, TRUE
, FALSE
);
527 if ( mHasFocusedBmp
)
529 RenderLabelImage( mpFocusedImg
, &mFocusedBmp
, TRUE
, FALSE
);
534 void wxNewBitmapButton::RenderLabelImages()
541 RenderLabelImage( mpDisabledImg
, &mDepressedBmp
, FALSE
);
547 RenderLabelImage( mpPressedImg
, &mDepressedBmp
, TRUE
, TRUE
);
552 if ( mHasFocusedBmp
)
553 RenderLabelImage( mpFocusedImg
, &mFocusedBmp
, TRUE
, FALSE
);
555 RenderLabelImage( mpDepressedImg
, &mDepressedBmp
, TRUE
, FALSE
);
558 RenderLabelImage( mpDepressedImg
, &mDepressedBmp
, TRUE
, FALSE
);
562 bool wxNewBitmapButton::Enable(bool enable
)
564 if ( enable
!= m_isEnabled
)
579 return wxPanel::Enable( enable
);
582 void wxNewBitmapButton::DrawDecorations( wxDC
& dc
)
586 DrawShade( 1, dc
, mGrayPen
, mGrayPen
);
591 DrawShade( 0, dc
, mDarkPen
, mLightPen
);
593 DrawShade( 0, dc
, mLightPen
, mDarkPen
);
596 DrawShade( 0, dc
, mGrayPen
, mGrayPen
);
602 DrawShade( 0, dc
, mDarkPen
, mGrayPen
);
603 DrawShade( 1, dc
, mBlackPen
, mLightPen
);
607 DrawShade( 0, dc
, mGrayPen
, mDarkPen
);
608 DrawShade( 1, dc
, mLightPen
, mBlackPen
);
613 void wxNewBitmapButton::SetLabel(const wxBitmap
& labelBitmap
,
614 const wxString
& labelText
)
618 mLabelText
= labelText
;
619 mDepressedBmp
= labelBitmap
;
621 //RenderLabelImages();
622 RenderAllLabelImages();
625 void wxNewBitmapButton::SetAlignments( int alignText
,
634 mTextAlignment
= alignText
;
635 mTextToLabelGap
= textToLabelGap
;
637 //RenderLabelImages();
638 RenderAllLabelImages();
643 void wxNewBitmapButton::OnLButtonDown( wxMouseEvent
& event
)
650 void wxNewBitmapButton::OnLButtonUp( wxMouseEvent
& event
)
655 mDragStarted
= FALSE
;
659 if ( IsInWindow( event
.m_x
, event
.m_y
) )
661 // fire event, if mouse was released
662 // within the bounds of button
663 wxCommandEvent
cmd( mFiredEventType
, GetId() );
664 GetParent()->ProcessEvent( cmd
);
668 void wxNewBitmapButton::OnLButtonDClick( wxMouseEvent
& event
)
670 if ( IsInWindow( event
.m_x
, event
.m_y
) )
672 // fire event, if mouse was released
673 // within the bounds of button
674 wxCommandEvent
cmd( mFiredEventType
, GetId() );
675 GetParent()->ProcessEvent( cmd
);
677 mDragStarted
= FALSE
;
683 bool wxNewBitmapButton::IsInWindow( int x
, int y
)
686 GetSize( &width
, &height
);
688 return ( x
>= 0 && y
>= 0 &&
693 void wxNewBitmapButton::OnMouseEnter( wxMouseEvent
& event
)
695 bool prevIsInFocus
= mIsInFocus
;
701 if ( prevIsInFocus
!= mIsInFocus
)
707 void wxNewBitmapButton::OnMouseLeave( wxMouseEvent
& event
)
709 bool prevIsInFocus
= mIsInFocus
;
710 bool prevIsPressed
= mIsPressed
;
716 if ( prevIsInFocus
!= mIsInFocus
|| prevIsPressed
!= mIsPressed
)
722 void wxNewBitmapButton::OnSize( wxSizeEvent
& event
)
727 void wxNewBitmapButton::Reshape( )
729 bool wasCreated
= mIsCreated
;
734 // in the case of loading button from stream, check if we
735 // have non-empty image-file name, load if possible
737 if ( mImageFileName
!= "" )
739 mDepressedBmp
.LoadFile( mImageFileName
, mImageFileType
);
741 //wxMessageBox("Image Loaded!!!");
744 //RenderLabelImages();
745 RenderAllLabelImages();
747 wxBitmap
* pCurImg
= GetStateLabel();
749 int w
= pCurImg
->GetWidth(),
750 h
= pCurImg
->GetHeight();
752 SetSize( 0,0, w
+ mMarginX
*2, h
+ mMarginY
*2 , 0 );
756 void wxNewBitmapButton::DrawLabel( wxDC
& dc
)
758 wxBitmap
* pCurBmp
= GetStateLabel();
760 if ( pCurBmp
== NULL
)
763 OnSize( evt
); // fake it up!
765 //RenderLabelImages();
766 pCurBmp
= GetStateLabel();
770 mdc
.SelectObject( *pCurBmp
);
772 dc
.Blit( mMarginX
, mMarginY
,
774 pCurBmp
->GetHeight(),
778 mdc
.SelectObject( wxNullBitmap
);
781 void wxNewBitmapButton::OnPaint( wxPaintEvent
& event
)
785 // first, make sure images for current state are prepared
786 //RenderLabelImages();
790 DrawDecorations( dc
);
793 void wxNewBitmapButton::OnEraseBackground( wxEraseEvent
& event
)
798 void wxNewBitmapButton::OnKillFocus( wxFocusEvent
& event
)
802 wxMessageBox("kill-focus for button!");