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_MOTION ( wxNewBitmapButton::OnMouseMove
)
211 EVT_SIZE ( wxNewBitmapButton::OnSize
)
212 EVT_PAINT( wxNewBitmapButton::OnPaint
)
214 //EVT_KILL_FOCUS( wxNewBitmapButton::OnKillFocus )
216 EVT_ERASE_BACKGROUND( wxNewBitmapButton::OnEraseBackground
)
220 wxNewBitmapButton::wxNewBitmapButton( const wxBitmap
& labelBitmap
,
221 const wxString
& labelText
,
229 : mTextToLabelGap ( textToLabelGap
),
232 mTextAlignment( alignText
),
233 mIsSticky( isSticky
),
235 mLabelText( labelText
),
236 mImageFileType( wxBITMAP_TYPE_INVALID
),
237 mDepressedBmp( labelBitmap
),
239 mpDepressedImg( NULL
),
240 mpPressedImg ( NULL
),
241 mpDisabledImg ( NULL
),
242 mpFocusedImg ( NULL
),
245 mDragStarted ( FALSE
),
246 mIsPressed ( FALSE
),
248 mPrevPressedState( FALSE
),
249 mPrevInFocusState( 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 mPrevPressedState( FALSE
),
294 mPrevInFocusState( FALSE
),
295 mHasFocusedBmp( FALSE
),
296 mFiredEventType( wxEVT_COMMAND_MENU_SELECTED
),
298 mBlackPen( wxColour( 0, 0, 0), 1, wxSOLID
),
299 mDarkPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
),
300 mGrayPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
), 1, wxSOLID
),
301 mLightPen( wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
), 1, wxSOLID
),
309 wxNewBitmapButton::~wxNewBitmapButton(void)
314 void wxNewBitmapButton::DrawShade( int outerLevel
,
316 wxPen
& upperLeftSidePen
,
317 wxPen
& lowerRightSidePen
)
319 wxBitmap
* pBmp
= GetStateLabel();
321 int x
= mMarginX
- (outerLevel
+ 1);
322 int y
= mMarginY
- (outerLevel
+ 1);
324 int height
= pBmp
->GetHeight() + (outerLevel
+ 1)*2 - 1;
325 int width
= pBmp
->GetWidth() + (outerLevel
+ 1)*2 - 1;
327 dc
.SetPen( upperLeftSidePen
);
328 dc
.DrawLine( x
,y
, x
+ width
, y
);
329 dc
.DrawLine( x
,y
, x
, y
+ height
);
331 dc
.SetPen( lowerRightSidePen
);
332 dc
.DrawLine( x
+ width
, y
, x
+ width
, y
+ height
+ 1 );
333 dc
.DrawLine( x
, y
+ height
, x
+ width
, y
+ height
);
336 void wxNewBitmapButton::DestroyLabels()
338 if ( mpDepressedImg
) delete mpDepressedImg
;
339 if ( mpPressedImg
) delete mpPressedImg
;
340 if ( mpDisabledImg
) delete mpDisabledImg
;
341 if ( mpFocusedImg
) delete mpFocusedImg
;
343 mpDepressedImg
= NULL
;
345 mpDisabledImg
= NULL
;
349 wxBitmap
* wxNewBitmapButton::GetStateLabel()
361 if ( mHasFocusedBmp
)
365 return mpDepressedImg
;
368 return mpDepressedImg
;
372 return mpDisabledImg
;
375 static const unsigned char _gDisableImage
[] = { 0x55,0xAA,0x55,0xAA,
380 void wxNewBitmapButton::RenderLabelImage( wxBitmap
*& destBmp
, wxBitmap
* srcBmp
,
381 bool isEnabled
, bool isPressed
)
383 if ( destBmp
!= 0 ) return;
385 // render labels on-demand
388 srcDc
.SelectObject( *srcBmp
);
390 bool hasText
= ( mTextAlignment
!= NB_NO_TEXT
) &&
391 ( mLabelText
.length() != 0 );
393 bool hasImage
= (mTextAlignment
!= NB_NO_IMAGE
);
401 long txtWidth
, txtHeight
;
403 srcDc
.SetFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
) );
404 srcDc
.GetTextExtent( mLabelText
, &txtWidth
, &txtHeight
);
406 if ( mTextAlignment
== NB_ALIGN_TEXT_RIGHT
)
408 destDim
.x
= srcBmp
->GetWidth() + 2*mTextToLabelGap
+ txtWidth
;
411 wxMax( srcBmp
->GetHeight(), txtHeight
);
413 txtPos
.x
= srcBmp
->GetWidth() + mTextToLabelGap
;
414 txtPos
.y
= (destDim
.y
- txtHeight
)/2;
416 imgPos
.y
= (destDim
.y
- srcBmp
->GetHeight())/2;
419 if ( mTextAlignment
== NB_ALIGN_TEXT_BOTTOM
)
422 wxMax( srcBmp
->GetWidth(), txtWidth
);
424 destDim
.y
= srcBmp
->GetHeight() + mTextToLabelGap
+ txtHeight
;
426 txtPos
.x
= (destDim
.x
- txtWidth
)/2;
427 txtPos
.y
= srcBmp
->GetHeight() + mTextToLabelGap
;
428 imgPos
.x
= (destDim
.x
- srcBmp
->GetWidth())/2;
433 wxFAIL_MSG("Unsupported FL alignment type detected in wxNewBitmapButton::RenderLabelImage()");
440 destDim
.x
= srcBmp
->GetWidth();
441 destDim
.y
= srcBmp
->GetHeight();
444 destBmp
= new wxBitmap( int(destDim
.x
), int(destDim
.y
) );
447 destDc
.SelectObject( *destBmp
);
449 wxBrush
grayBrush( wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE
), wxSOLID
);
450 wxPen
nullPen( wxColour(0,0,0), 1, wxTRANSPARENT
);
452 destDc
.SetBrush( grayBrush
);
453 destDc
.SetPen( nullPen
);
455 destDc
.DrawRectangle( 0,0, destDim
.x
+1, destDim
.y
+1 );
459 ++imgPos
.x
; ++imgPos
.y
;
460 ++txtPos
.x
; ++txtPos
.y
;
466 destDc
.Blit( imgPos
.x
, imgPos
.y
,
467 srcBmp
->GetWidth()+1,
468 srcBmp
->GetHeight()+1,
469 &srcDc
, 0,0, wxCOPY
,TRUE
);
474 wxWindow
* pTopWnd
= this;
478 wxWindow
* pParent
= pTopWnd
->GetParent();
486 destDc
.SetFont( wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
) );
490 destDc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT
) );
494 destDc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
) );
496 destDc
.SetTextBackground( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
) );
498 destDc
.DrawText( mLabelText
, txtPos
.x
, txtPos
.y
);
502 destDc
.SetBrush( grayBrush
);
503 destDc
.SetPen( nullPen
);
505 destDc
.DrawRectangle( 0,0, destDim
.x
+1, destDim
.y
+1 );
509 ++imgPos
.x
; ++imgPos
.y
;
510 ++txtPos
.x
; ++txtPos
.y
;
516 destDc
.Blit( imgPos
.x
, imgPos
.y
,
517 srcBmp
->GetWidth()+1,
518 srcBmp
->GetHeight()+1,
519 &srcDc
, 0,0, wxCOPY
,TRUE
);
524 wxWindow
* pTopWnd
= this;
528 wxWindow
* pParent
= pTopWnd
->GetParent();
536 destDc
.SetFont( wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
) );
540 destDc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT
) );
544 destDc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
) );
546 destDc
.SetTextBackground( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
) );
548 destDc
.DrawText( mLabelText
, txtPos
.x
, txtPos
.y
);
553 #ifdef __WXMSW__ // This is currently MSW specific
554 gray_out_image_on_dc( destDc
, destDim
.x
, destDim
.y
);
556 wxBrush
checkerBrush( wxBitmap( (const char*)_gDisableImage
,8,8) );
557 checkerBrush
.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
) );
558 destDc
.SetBrush( checkerBrush
);
559 destDc
.DrawRectangle( imgPos
.x
, imgPos
.y
, srcBmp
->GetWidth()+1, srcBmp
->GetHeight()+1);
562 // adjust button size to fit the new dimensions of the label
563 if ( !mSizeIsSet
&& 0 )
567 destBmp
->GetWidth() + mMarginX
*2,
568 destBmp
->GetHeight() + mMarginY
*2, 0
572 void wxNewBitmapButton::RenderAllLabelImages()
576 RenderLabelImage( mpDisabledImg
, &mDepressedBmp
, FALSE
);
577 RenderLabelImage( mpPressedImg
, &mDepressedBmp
, TRUE
, TRUE
);
578 RenderLabelImage( mpDepressedImg
, &mDepressedBmp
, TRUE
, FALSE
);
579 if ( mHasFocusedBmp
)
581 RenderLabelImage( mpFocusedImg
, &mFocusedBmp
, TRUE
, FALSE
);
586 void wxNewBitmapButton::RenderLabelImages()
593 RenderLabelImage( mpDisabledImg
, &mDepressedBmp
, FALSE
);
599 RenderLabelImage( mpPressedImg
, &mDepressedBmp
, TRUE
, TRUE
);
604 if ( mHasFocusedBmp
)
605 RenderLabelImage( mpFocusedImg
, &mFocusedBmp
, TRUE
, FALSE
);
607 RenderLabelImage( mpDepressedImg
, &mDepressedBmp
, TRUE
, FALSE
);
610 RenderLabelImage( mpDepressedImg
, &mDepressedBmp
, TRUE
, FALSE
);
614 void wxNewBitmapButton::DrawDecorations( wxDC
& dc
)
618 DrawShade( 1, dc
, mGrayPen
, mGrayPen
);
623 DrawShade( 0, dc
, mDarkPen
, mLightPen
);
625 DrawShade( 0, dc
, mLightPen
, mDarkPen
);
628 DrawShade( 0, dc
, mGrayPen
, mGrayPen
);
634 DrawShade( 0, dc
, mDarkPen
, mGrayPen
);
635 DrawShade( 1, dc
, mBlackPen
, mLightPen
);
639 DrawShade( 0, dc
, mGrayPen
, mDarkPen
);
640 DrawShade( 1, dc
, mLightPen
, mBlackPen
);
645 void wxNewBitmapButton::SetLabel(const wxBitmap
& labelBitmap
,
646 const wxString
& labelText
)
650 mLabelText
= labelText
;
651 mDepressedBmp
= labelBitmap
;
653 //RenderLabelImages();
654 RenderAllLabelImages();
657 void wxNewBitmapButton::SetAlignments( int alignText
,
666 mTextAlignment
= alignText
;
667 mTextToLabelGap
= textToLabelGap
;
669 //RenderLabelImages();
670 RenderAllLabelImages();
675 void wxNewBitmapButton::OnLButtonDown( wxMouseEvent
& event
)
677 mPrevPressedState
= FALSE
;
686 void wxNewBitmapButton::OnLButtonUp( wxMouseEvent
& event
)
691 mDragStarted
= FALSE
;
698 if ( IsInWindow( event
.m_x
, event
.m_y
) )
700 // fire event, if mouse was released
701 // within the bounds of button
702 wxCommandEvent
cmd( mFiredEventType
, GetId() );
703 GetParent()->ProcessEvent( cmd
);
707 bool wxNewBitmapButton::IsInWindow( int x
, int y
)
710 GetSize( &width
, &height
);
712 return ( x
>= 0 && y
>= 0 &&
717 void wxNewBitmapButton::OnMouseMove( wxMouseEvent
& event
)
719 mPrevPressedState
=mIsPressed
;
720 mPrevInFocusState
=mIsInFocus
;
721 if ( !mIsInFocus
&& IsInWindow( event
.m_x
, event
.m_y
) )
729 if ( mIsInFocus
&& !IsInWindow( event
.m_x
, event
.m_y
) )
739 if ( IsInWindow( event
.m_x
, event
.m_y
) )
746 if ((mIsPressed
!= mPrevPressedState
) ||
747 (mIsInFocus
!=mPrevInFocusState
))
753 void wxNewBitmapButton::OnSize( wxSizeEvent
& event
)
758 void wxNewBitmapButton::Reshape( )
760 bool wasCreated
= mIsCreated
;
765 // in the case of loading button from stream, check if we
766 // have non-empty image-file name, load if possible
768 if ( mImageFileName
!= "" )
770 mDepressedBmp
.LoadFile( mImageFileName
, mImageFileType
);
772 //wxMessageBox("Image Loaded!!!");
775 //RenderLabelImages();
776 RenderAllLabelImages();
778 wxBitmap
* pCurImg
= GetStateLabel();
780 int w
= pCurImg
->GetWidth(),
781 h
= pCurImg
->GetHeight();
783 SetSize( 0,0, w
+ mMarginX
*2, h
+ mMarginY
*2 , 0 );
787 void wxNewBitmapButton::DrawLabel( wxDC
& dc
)
789 wxBitmap
* pCurBmp
= GetStateLabel();
791 if ( pCurBmp
== NULL
)
794 OnSize( evt
); // fake it up!
796 //RenderLabelImages();
797 pCurBmp
= GetStateLabel();
801 mdc
.SelectObject( *pCurBmp
);
803 dc
.Blit( mMarginX
, mMarginY
,
805 pCurBmp
->GetHeight(),
809 mdc
.SelectObject( wxNullBitmap
);
812 void wxNewBitmapButton::OnPaint( wxPaintEvent
& event
)
816 // first, make sure images for current state are prepared
817 //RenderLabelImages();
821 DrawDecorations( dc
);
824 void wxNewBitmapButton::OnEraseBackground( wxEraseEvent
& event
)
829 void wxNewBitmapButton::OnKillFocus( wxFocusEvent
& event
)
833 wxMessageBox("kill-focus for button!");