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
31 #include "wx/msw/private.h"
34 ///////////// button-label rendering helpers //////////////////
36 static int* create_array( int width
, int height
, int fill
= 0 )
38 int* array
= new int[width
*height
];
40 int len
= width
*height
;
42 for ( i
= 0; i
!= len
; ++i
)
48 #define GET_ELEM(array,x,y) (array[width*(y)+(x)])
50 #define MIN_COLOR_DIFF 10
52 #define IS_IN_ARRAY(x,y) ( (x) < width && (y) < height && (x) >= 0 && (y) >= 0 )
54 #define GET_RED(col) col & 0xFF
55 #define GET_GREEN(col) (col >> 8) & 0xFF
56 #define GET_BLUE(col) (col >> 16) & 0xFF
58 #define MAKE_INT_COLOR(red,green,blue) ( (red) | \
59 ( ( (green) << 8 ) & 0xFF00 ) | \
60 ( ( (blue) << 16) & 0xFF0000) \
63 #define IS_GREATER(col1,col2) ( ( (GET_RED(col1) ) > (GET_RED(col2) ) + MIN_COLOR_DIFF ) && \
64 ( (GET_GREEN(col1)) > (GET_GREEN(col2)) + MIN_COLOR_DIFF ) && \
65 ( (GET_BLUE(col1) ) > (GET_BLUE(col2) ) + MIN_COLOR_DIFF ) \
72 // helper function, used internally
74 static void gray_out_pixmap( int* src
, int* dest
, int width
, int height
)
76 // assuming the pixels along the edges are of the background color
83 int cur
= GET_ELEM(src
,x
,y
);
86 if ( IS_IN_ARRAY(x
-1,y
-1) )
88 int upperElem
= GET_ELEM(src
,x
-1,y
-1);
90 // if the upper element is lighter than current
91 if ( IS_GREATER(upperElem
,cur
) )
93 GET_ELEM(dest
,x
,y
) = MASK_DARK
;
96 // if the current element is ligher than the upper
97 if ( IS_GREATER(cur
,upperElem
) )
99 GET_ELEM(dest
,x
,y
) = MASK_LIGHT
;
103 if ( GET_ELEM(dest
,x
-1,y
-1) == MASK_LIGHT
)
105 GET_ELEM(dest
,x
,y
) = MASK_BG
;
107 if ( GET_ELEM(dest
,x
-1,y
-1 ) == MASK_DARK
)
109 GET_ELEM(dest
,x
,y
) = MASK_DARK
;
111 GET_ELEM(dest
,x
,y
) = MASK_BG
;
117 if ( IS_IN_ARRAY(x
+1,y
-1) )
124 while ( IS_IN_ARRAY(x
-1,y
+1) )
130 if ( IS_IN_ARRAY(x
,y
+1) )
137 if ( IS_IN_ARRAY(x
+1,y
) )
149 // algorithm for making the image look "grayed" (e.g. disabled button)
150 // NOTE:: used GetPixel(), which is Windows-Only!
152 void gray_out_image_on_dc( wxDC
& dc
, int width
, int height
)
154 // assuming the pixels along the edges are of the background color
156 dc
.GetPixel( 0, 0, &bgCol
);
158 wxPen
darkPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
),1, wxSOLID
);
159 wxPen
lightPen( wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
),1, wxSOLID
);
160 wxPen
bgPen ( bgCol
, 1, wxSOLID
);
162 int* src
= create_array( width
, height
, MASK_BG
);
163 int* dest
= create_array( width
, height
, MASK_BG
);
166 for ( y
= 0; y
!= height
; ++y
)
168 for ( x
= 0; x
!= width
; ++x
)
171 dc
.GetPixel( x
,y
, &col
);
173 GET_ELEM(src
,x
,y
) = MAKE_INT_COLOR( col
.Red(), col
.Green(), col
.Blue() );
176 gray_out_pixmap( src
, dest
, width
, height
);
178 for ( y
= 0; y
!= height
; ++y
)
180 for ( x
= 0; x
!= width
; ++x
)
182 int mask
= GET_ELEM(dest
,x
,y
);
186 case MASK_BG
: { dc
.SetPen( bgPen
);
187 dc
.DrawPoint( x
,y
); break;
189 case MASK_DARK
: { dc
.SetPen( darkPen
);
190 dc
.DrawPoint( x
,y
); break;
192 case MASK_LIGHT
: { dc
.SetPen( lightPen
);
193 dc
.DrawPoint( x
,y
); break;
203 ///////////////////////////////
205 /***** Implementation for class wxNewBitmapButton *****/
207 IMPLEMENT_DYNAMIC_CLASS(wxNewBitmapButton
, wxPanel
)
209 BEGIN_EVENT_TABLE( wxNewBitmapButton
, wxPanel
)
211 EVT_LEFT_DOWN ( wxNewBitmapButton::OnLButtonDown
)
212 EVT_LEFT_UP ( wxNewBitmapButton::OnLButtonUp
)
213 // EVT_LEFT_DCLICK ( wxNewBitmapButton::OnLButtonDClick )
214 EVT_LEFT_DCLICK ( wxNewBitmapButton::OnLButtonDown
)
215 EVT_ENTER_WINDOW( wxNewBitmapButton::OnMouseEnter
)
216 EVT_LEAVE_WINDOW( wxNewBitmapButton::OnMouseLeave
)
218 EVT_SIZE ( wxNewBitmapButton::OnSize
)
219 EVT_PAINT( wxNewBitmapButton::OnPaint
)
221 //EVT_KILL_FOCUS( wxNewBitmapButton::OnKillFocus )
223 EVT_ERASE_BACKGROUND( wxNewBitmapButton::OnEraseBackground
)
227 wxNewBitmapButton::wxNewBitmapButton( const wxBitmap
& labelBitmap
,
228 const wxString
& labelText
,
236 : mTextToLabelGap ( textToLabelGap
),
239 mTextAlignment( alignText
),
240 mIsSticky( isSticky
),
242 mLabelText( labelText
),
243 mImageFileType( wxBITMAP_TYPE_INVALID
),
244 mDepressedBmp( labelBitmap
),
246 mpDepressedImg( NULL
),
247 mpPressedImg ( NULL
),
248 mpDisabledImg ( NULL
),
249 mpFocusedImg ( NULL
),
252 mDragStarted ( FALSE
),
253 mIsPressed ( FALSE
),
255 mHasFocusedBmp( FALSE
),
256 mFiredEventType( firedEventType
),
258 mBlackPen( wxColour( 0, 0, 0), 1, wxSOLID
),
259 mDarkPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
),
260 mGrayPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
), 1, wxSOLID
),
261 mLightPen( wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
), 1, wxSOLID
),
269 wxNewBitmapButton::wxNewBitmapButton( const wxString
& bitmapFileName
,
270 const wxBitmapType bitmapFileType
,
271 const wxString
& labelText
,
280 : mTextToLabelGap ( 2 ),
283 mTextAlignment( alignText
),
286 mLabelText( labelText
),
287 mImageFileName( bitmapFileName
),
288 mImageFileType( bitmapFileType
),
290 mpDepressedImg( NULL
),
291 mpPressedImg ( NULL
),
292 mpDisabledImg ( NULL
),
293 mpFocusedImg ( NULL
),
295 mDragStarted ( FALSE
),
296 mIsPressed ( FALSE
),
297 mIsInFocus ( FALSE
),
298 mHasFocusedBmp( FALSE
),
299 mFiredEventType( wxEVT_COMMAND_MENU_SELECTED
),
301 mBlackPen( wxColour( 0, 0, 0), 1, wxSOLID
),
302 mDarkPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
),
303 mGrayPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
), 1, wxSOLID
),
304 mLightPen( wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
), 1, wxSOLID
),
312 wxNewBitmapButton::~wxNewBitmapButton(void)
317 void wxNewBitmapButton::DrawShade( int outerLevel
,
319 wxPen
& upperLeftSidePen
,
320 wxPen
& lowerRightSidePen
)
322 wxBitmap
* pBmp
= GetStateLabel();
324 int x
= mMarginX
- (outerLevel
+ 1);
325 int y
= mMarginY
- (outerLevel
+ 1);
327 int height
= pBmp
->GetHeight() + (outerLevel
+ 1)*2 - 1;
328 int width
= pBmp
->GetWidth() + (outerLevel
+ 1)*2 - 1;
330 dc
.SetPen( upperLeftSidePen
);
331 dc
.DrawLine( x
,y
, x
+ width
, y
);
332 dc
.DrawLine( x
,y
, x
, y
+ height
);
334 dc
.SetPen( lowerRightSidePen
);
335 dc
.DrawLine( x
+ width
, y
, x
+ width
, y
+ height
+ 1 );
336 dc
.DrawLine( x
, y
+ height
, x
+ width
, y
+ height
);
339 void wxNewBitmapButton::DestroyLabels()
341 if ( mpDepressedImg
) delete mpDepressedImg
;
342 if ( mpPressedImg
) delete mpPressedImg
;
343 if ( mpDisabledImg
) delete mpDisabledImg
;
344 if ( mpFocusedImg
) delete mpFocusedImg
;
346 mpDepressedImg
= NULL
;
348 mpDisabledImg
= NULL
;
352 wxBitmap
* wxNewBitmapButton::GetStateLabel()
364 if ( mHasFocusedBmp
)
368 return mpDepressedImg
;
371 return mpDepressedImg
;
375 return mpDisabledImg
;
378 static const unsigned char _gDisableImage
[] = { 0x55,0xAA,0x55,0xAA,
383 void wxNewBitmapButton::RenderLabelImage( wxBitmap
*& destBmp
, wxBitmap
* srcBmp
,
384 bool isEnabled
, bool isPressed
)
386 if ( destBmp
!= 0 ) return;
388 // render labels on-demand
391 srcDc
.SelectObject( *srcBmp
);
393 bool hasText
= ( mTextAlignment
!= NB_NO_TEXT
) &&
394 ( mLabelText
.length() != 0 );
396 bool hasImage
= (mTextAlignment
!= NB_NO_IMAGE
);
404 long txtWidth
, txtHeight
;
406 srcDc
.SetFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
) );
407 srcDc
.GetTextExtent( mLabelText
, &txtWidth
, &txtHeight
);
409 if ( mTextAlignment
== NB_ALIGN_TEXT_RIGHT
)
411 destDim
.x
= srcBmp
->GetWidth() + 2*mTextToLabelGap
+ txtWidth
;
414 wxMax( srcBmp
->GetHeight(), txtHeight
);
416 txtPos
.x
= srcBmp
->GetWidth() + mTextToLabelGap
;
417 txtPos
.y
= (destDim
.y
- txtHeight
)/2;
419 imgPos
.y
= (destDim
.y
- srcBmp
->GetHeight())/2;
422 if ( mTextAlignment
== NB_ALIGN_TEXT_BOTTOM
)
425 wxMax( srcBmp
->GetWidth(), txtWidth
);
427 destDim
.y
= srcBmp
->GetHeight() + mTextToLabelGap
+ txtHeight
;
429 txtPos
.x
= (destDim
.x
- txtWidth
)/2;
430 txtPos
.y
= srcBmp
->GetHeight() + mTextToLabelGap
;
431 imgPos
.x
= (destDim
.x
- srcBmp
->GetWidth())/2;
436 wxFAIL_MSG("Unsupported FL alignment type detected in wxNewBitmapButton::RenderLabelImage()");
443 destDim
.x
= srcBmp
->GetWidth();
444 destDim
.y
= srcBmp
->GetHeight();
447 destBmp
= new wxBitmap( int(destDim
.x
), int(destDim
.y
) );
450 destDc
.SelectObject( *destBmp
);
452 wxBrush
grayBrush( wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE
), wxSOLID
);
453 wxPen
nullPen( wxColour(0,0,0), 1, wxTRANSPARENT
);
455 destDc
.SetBrush( grayBrush
);
456 destDc
.SetPen( nullPen
);
458 destDc
.DrawRectangle( 0,0, destDim
.x
+1, destDim
.y
+1 );
462 ++imgPos
.x
; ++imgPos
.y
;
463 ++txtPos
.x
; ++txtPos
.y
;
469 destDc
.Blit( imgPos
.x
, imgPos
.y
,
470 srcBmp
->GetWidth()+1,
471 srcBmp
->GetHeight()+1,
472 &srcDc
, 0,0, wxCOPY
,TRUE
);
477 wxWindow
* pTopWnd
= this;
481 wxWindow
* pParent
= pTopWnd
->GetParent();
489 destDc
.SetFont( wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
) );
493 destDc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT
) );
497 destDc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
) );
499 destDc
.SetTextBackground( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
) );
501 destDc
.DrawText( mLabelText
, txtPos
.x
, txtPos
.y
);
506 #ifdef __WXMSW__ // This is currently MSW specific
507 gray_out_image_on_dc( destDc
, destDim
.x
, destDim
.y
);
509 wxBrush
checkerBrush( wxBitmap( (const char*)_gDisableImage
,8,8) );
510 checkerBrush
.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
) );
511 destDc
.SetBrush( checkerBrush
);
512 destDc
.DrawRectangle( imgPos
.x
, imgPos
.y
, srcBmp
->GetWidth()+1, srcBmp
->GetHeight()+1);
515 // adjust button size to fit the new dimensions of the label
516 if ( !mSizeIsSet
&& 0 )
520 destBmp
->GetWidth() + mMarginX
*2,
521 destBmp
->GetHeight() + mMarginY
*2, 0
524 destDc
.SelectObject( wxNullBitmap
);
526 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
527 // Map to system colours
528 (void) wxToolBar::MapBitmap(destBmp
->GetHBITMAP(), destBmp
->GetWidth(), destBmp
->GetHeight());
532 void wxNewBitmapButton::RenderAllLabelImages()
536 RenderLabelImage( mpDisabledImg
, &mDepressedBmp
, FALSE
);
537 RenderLabelImage( mpPressedImg
, &mDepressedBmp
, TRUE
, TRUE
);
538 RenderLabelImage( mpDepressedImg
, &mDepressedBmp
, TRUE
, FALSE
);
539 if ( mHasFocusedBmp
)
541 RenderLabelImage( mpFocusedImg
, &mFocusedBmp
, TRUE
, FALSE
);
546 void wxNewBitmapButton::RenderLabelImages()
553 RenderLabelImage( mpDisabledImg
, &mDepressedBmp
, FALSE
);
559 RenderLabelImage( mpPressedImg
, &mDepressedBmp
, TRUE
, TRUE
);
564 if ( mHasFocusedBmp
)
565 RenderLabelImage( mpFocusedImg
, &mFocusedBmp
, TRUE
, FALSE
);
567 RenderLabelImage( mpDepressedImg
, &mDepressedBmp
, TRUE
, FALSE
);
570 RenderLabelImage( mpDepressedImg
, &mDepressedBmp
, TRUE
, FALSE
);
574 bool wxNewBitmapButton::Enable(bool enable
)
576 if ( enable
!= m_isEnabled
)
591 return wxPanel::Enable( enable
);
594 void wxNewBitmapButton::DrawDecorations( wxDC
& dc
)
598 DrawShade( 1, dc
, mGrayPen
, mGrayPen
);
603 DrawShade( 0, dc
, mDarkPen
, mLightPen
);
605 DrawShade( 0, dc
, mLightPen
, mDarkPen
);
608 DrawShade( 0, dc
, mGrayPen
, mGrayPen
);
614 DrawShade( 0, dc
, mDarkPen
, mGrayPen
);
615 DrawShade( 1, dc
, mBlackPen
, mLightPen
);
619 DrawShade( 0, dc
, mGrayPen
, mDarkPen
);
620 DrawShade( 1, dc
, mLightPen
, mBlackPen
);
625 void wxNewBitmapButton::SetLabel(const wxBitmap
& labelBitmap
,
626 const wxString
& labelText
)
630 mLabelText
= labelText
;
631 mDepressedBmp
= labelBitmap
;
633 //RenderLabelImages();
634 RenderAllLabelImages();
637 void wxNewBitmapButton::SetAlignments( int alignText
,
646 mTextAlignment
= alignText
;
647 mTextToLabelGap
= textToLabelGap
;
649 //RenderLabelImages();
650 RenderAllLabelImages();
655 void wxNewBitmapButton::OnLButtonDown( wxMouseEvent
& event
)
662 void wxNewBitmapButton::OnLButtonUp( wxMouseEvent
& event
)
667 mDragStarted
= FALSE
;
671 if ( IsInWindow( event
.m_x
, event
.m_y
) )
673 // fire event, if mouse was released
674 // within the bounds of button
675 wxCommandEvent
cmd( mFiredEventType
, GetId() );
676 GetParent()->ProcessEvent( cmd
);
680 bool wxNewBitmapButton::IsInWindow( int x
, int y
)
683 GetSize( &width
, &height
);
685 return ( x
>= 0 && y
>= 0 &&
690 void wxNewBitmapButton::OnMouseEnter( wxMouseEvent
& event
)
692 bool prevIsInFocus
= mIsInFocus
;
698 if ( prevIsInFocus
!= mIsInFocus
)
704 void wxNewBitmapButton::OnMouseLeave( wxMouseEvent
& event
)
706 bool prevIsInFocus
= mIsInFocus
;
707 bool prevIsPressed
= mIsPressed
;
713 if ( prevIsInFocus
!= mIsInFocus
|| prevIsPressed
!= mIsPressed
)
719 void wxNewBitmapButton::OnSize( wxSizeEvent
& event
)
724 void wxNewBitmapButton::Reshape( )
726 bool wasCreated
= mIsCreated
;
731 // in the case of loading button from stream, check if we
732 // have non-empty image-file name, load if possible
734 if ( mImageFileName
!= "" )
736 mDepressedBmp
.LoadFile( mImageFileName
, mImageFileType
);
738 //wxMessageBox("Image Loaded!!!");
741 //RenderLabelImages();
742 RenderAllLabelImages();
744 wxBitmap
* pCurImg
= GetStateLabel();
746 int w
= pCurImg
->GetWidth(),
747 h
= pCurImg
->GetHeight();
749 SetSize( 0,0, w
+ mMarginX
*2, h
+ mMarginY
*2 , 0 );
753 void wxNewBitmapButton::DrawLabel( wxDC
& dc
)
755 wxBitmap
* pCurBmp
= GetStateLabel();
757 if ( pCurBmp
== NULL
)
760 OnSize( evt
); // fake it up!
762 //RenderLabelImages();
763 pCurBmp
= GetStateLabel();
767 mdc
.SelectObject( *pCurBmp
);
769 dc
.Blit( mMarginX
, mMarginY
,
771 pCurBmp
->GetHeight(),
775 mdc
.SelectObject( wxNullBitmap
);
778 void wxNewBitmapButton::OnPaint( wxPaintEvent
& event
)
782 // first, make sure images for current state are prepared
783 //RenderLabelImages();
787 DrawDecorations( dc
);
790 void wxNewBitmapButton::OnEraseBackground( wxEraseEvent
& event
)
795 void wxNewBitmapButton::OnKillFocus( wxFocusEvent
& event
)
799 wxMessageBox("kill-focus for button!");