1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Contrib. demo
4 // Author: Aleksandras Gluchovas
8 // Copyright: (c) Aleksandras Gluchovas
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "newbmpbtn.cpp"
14 #pragma interface "newbmpbtn.cpp"
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
30 #include "newbmpbtn.h"
31 #include "wx/utils.h" // import wxMin,wxMax macros
33 ///////////// button-label rendering helpers //////////////////
35 static int* create_array( int width
, int height
, int fill
= 0 )
37 int* array
= new int[width
*height
];
39 int len
= width
*height
;
40 for( int i
= 0; i
!= len
; ++i
) array
[i
] = fill
;
45 #define GET_ELEM(array,x,y) (array[width*(y)+(x)])
47 #define MIN_COLOR_DIFF 10
49 #define IS_IN_ARRAY(x,y) ( (x) < width && (y) < height && (x) >= 0 && (y) >= 0 )
51 #define GET_RED(col) col & 0xFF
52 #define GET_GREEN(col) (col >> 8) & 0xFF
53 #define GET_BLUE(col) (col >> 16) & 0xFF
55 #define MAKE_INT_COLOR(red,green,blue) ( (red) | \
56 ( ( (green) << 8 ) & 0xFF00 ) | \
57 ( ( (blue) << 16) & 0xFF0000) \
60 #define IS_GREATER(col1,col2) ( ( (GET_RED(col1) ) > (GET_RED(col2) ) + MIN_COLOR_DIFF ) && \
61 ( (GET_GREEN(col1)) > (GET_GREEN(col2)) + MIN_COLOR_DIFF ) && \
62 ( (GET_BLUE(col1) ) > (GET_BLUE(col2) ) + MIN_COLOR_DIFF ) \
69 // helper function, used internally
71 static void gray_out_pixmap( int* src
, int* dest
, int width
, int height
)
73 // assuming the pixels along the edges are of the background color
80 int cur
= GET_ELEM(src
,x
,y
);
83 if ( IS_IN_ARRAY(x
-1,y
-1) )
85 int upperElem
= GET_ELEM(src
,x
-1,y
-1);
87 // if the upper element is lighter than current
88 if ( IS_GREATER(upperElem
,cur
) )
90 GET_ELEM(dest
,x
,y
) = MASK_DARK
;
93 // if the current element is ligher than the upper
94 if ( IS_GREATER(cur
,upperElem
) )
96 GET_ELEM(dest
,x
,y
) = MASK_LIGHT
;
100 if ( GET_ELEM(dest
,x
-1,y
-1) == MASK_LIGHT
)
102 GET_ELEM(dest
,x
,y
) = MASK_BG
;
104 if ( GET_ELEM(dest
,x
-1,y
-1 ) == MASK_DARK
)
106 GET_ELEM(dest
,x
,y
) = MASK_DARK
;
108 GET_ELEM(dest
,x
,y
) = MASK_BG
;
114 if ( IS_IN_ARRAY(x
+1,y
-1) )
120 while( IS_IN_ARRAY(x
-1,y
+1) )
125 if ( IS_IN_ARRAY(x
,y
+1) )
131 if ( IS_IN_ARRAY(x
+1,y
) )
142 // alg. for making the image look "grayed" (e.g. disabled button)
143 // NOTE:: used GetPixel(), which is Windows-Only!
145 void greay_out_image_on_dc( wxDC
& dc
, int width
, int height
)
147 // assuming the pixels along the edges are of the background color
149 dc
.GetPixel( 0, 0, &bgCol
);
151 wxPen
darkPen ( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW
),1, wxSOLID
);
152 wxPen
lightPen( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHIGHLIGHT
),1, wxSOLID
);
153 wxPen
bgPen ( bgCol
, 1, wxSOLID
);
155 int* src
= create_array( width
, height
, MASK_BG
);
156 int* dest
= create_array( width
, height
, MASK_BG
);
159 for( y
= 0; y
!= height
; ++y
)
161 for( int x
= 0; x
!= width
; ++x
)
164 dc
.GetPixel( x
,y
, &col
);
167 GET_ELEM(src
,x
,y
) = MAKE_INT_COLOR( col
.Red(), col
.Green(), col
.Blue() );
170 gray_out_pixmap( src
, dest
, width
, height
);
172 for( y
= 0; y
!= height
; ++y
)
174 for( int x
= 0; x
!= width
; ++x
)
176 int mask
= GET_ELEM(dest
,x
,y
);
180 case MASK_BG
: { dc
.SetPen( bgPen
);
181 dc
.DrawPoint( x
,y
); break;
183 case MASK_DARK
: { dc
.SetPen( darkPen
);
184 dc
.DrawPoint( x
,y
); break;
186 case MASK_LIGHT
: { dc
.SetPen( lightPen
);
187 dc
.DrawPoint( x
,y
); break;
197 ///////////////////////////////
199 /***** Impelementation for class wxNewBitmapButton *****/
201 IMPLEMENT_DYNAMIC_CLASS(wxNewBitmapButton
, wxPanel
)
203 BEGIN_EVENT_TABLE( wxNewBitmapButton
, wxPanel
)
205 EVT_LEFT_DOWN( wxNewBitmapButton::OnLButtonDown
)
206 EVT_LEFT_UP ( wxNewBitmapButton::OnLButtonUp
)
207 EVT_MOTION ( wxNewBitmapButton::OnMouseMove
)
209 EVT_SIZE ( wxNewBitmapButton::OnSize
)
210 EVT_PAINT( wxNewBitmapButton::OnPaint
)
212 //EVT_KILL_FOCUS( wxNewBitmapButton::OnKillFocus )
214 EVT_ERASE_BACKGROUND( wxNewBitmapButton::OnEraseBackground
)
218 wxNewBitmapButton::wxNewBitmapButton( const wxBitmap
& labelBitmap
,
219 const wxString
& labelText
,
227 : mTextToLabelGap ( textToLabelGap
),
230 mTextAlignment( alignText
),
231 mIsSticky( isSticky
),
233 mLabelText( labelText
),
234 mImageFileType( -1 ),
235 mDepressedBmp( labelBitmap
),
237 mpDepressedImg( NULL
),
238 mpPressedImg ( NULL
),
239 mpDisabledImg ( NULL
),
240 mpFocusedImg ( NULL
),
243 mDragStarted ( FALSE
),
244 mIsPressed ( FALSE
),
246 mPrevPressedState( FALSE
),
247 mPrevInFocusState( FALSE
),
248 mHasFocusedBmp( FALSE
),
249 mFiredEventType( firedEventType
),
251 mBlackPen( wxColour( 0, 0, 0), 1, wxSOLID
),
252 mDarkPen ( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
),
253 mGrayPen ( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
), 1, wxSOLID
),
254 mLightPen( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHIGHLIGHT
), 1, wxSOLID
),
262 wxNewBitmapButton::wxNewBitmapButton( const wxString
& bitmapFileName
,
263 const int bitmapFileType
,
264 const wxString
& labelText
,
273 : mTextToLabelGap ( 2 ),
276 mTextAlignment( alignText
),
279 mLabelText( labelText
),
280 mImageFileName( bitmapFileName
),
281 mImageFileType( bitmapFileType
),
283 mpDepressedImg( NULL
),
284 mpPressedImg ( NULL
),
285 mpDisabledImg ( NULL
),
286 mpFocusedImg ( NULL
),
288 mDragStarted ( FALSE
),
289 mIsPressed ( FALSE
),
290 mIsInFocus ( FALSE
),
291 mPrevPressedState( FALSE
),
292 mPrevInFocusState( FALSE
),
293 mHasFocusedBmp( FALSE
),
294 mFiredEventType( wxEVT_COMMAND_MENU_SELECTED
),
296 mBlackPen( wxColour( 0, 0, 0), 1, wxSOLID
),
297 mDarkPen ( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
),
298 mGrayPen ( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
), 1, wxSOLID
),
299 mLightPen( wxSystemSettings::GetSystemColour(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 lables 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::GetSystemFont(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;
429 else wxASSERT(0);// unsupported alignment type
435 destDim
.x
= srcBmp
->GetWidth();
436 destDim
.y
= srcBmp
->GetHeight();
439 destBmp
= new wxBitmap( int(destDim
.x
), int(destDim
.y
) );
442 destDc
.SelectObject( *destBmp
);
444 wxBrush
grayBrush( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_3DFACE
), wxSOLID
);
445 wxPen
nullPen( wxColour(0,0,0), 1, wxTRANSPARENT
);
447 destDc
.SetBrush( grayBrush
);
448 destDc
.SetPen( nullPen
);
450 destDc
.DrawRectangle( 0,0, destDim
.x
+1, destDim
.y
+1 );
454 ++imgPos
.x
; ++imgPos
.y
;
455 ++txtPos
.x
; ++txtPos
.y
;
461 destDc
.Blit( imgPos
.x
, imgPos
.y
,
462 srcBmp
->GetWidth()+1,
463 srcBmp
->GetHeight()+1,
464 &srcDc
, 0,0, wxCOPY
,TRUE
);
469 wxWindow
* pTopWnd
= this;
473 wxWindow
* pParent
= pTopWnd
->GetParent();
475 if ( pParent
== 0 ) break;
480 destDc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
483 destDc
.SetTextForeground( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNTEXT
) );
485 destDc
.SetTextForeground( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW
) );
487 destDc
.SetTextBackground( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
) );
489 destDc
.DrawText( mLabelText
, txtPos
.x
, txtPos
.y
);
493 destDc
.SetBrush( grayBrush
);
494 destDc
.SetPen( nullPen
);
496 destDc
.DrawRectangle( 0,0, destDim
.x
+1, destDim
.y
+1 );
500 ++imgPos
.x
; ++imgPos
.y
;
501 ++txtPos
.x
; ++txtPos
.y
;
507 destDc
.Blit( imgPos
.x
, imgPos
.y
,
508 srcBmp
->GetWidth()+1,
509 srcBmp
->GetHeight()+1,
510 &srcDc
, 0,0, wxCOPY
,TRUE
);
515 wxWindow
* pTopWnd
= this;
519 wxWindow
* pParent
= pTopWnd
->GetParent();
521 if ( pParent
== 0 ) break;
526 destDc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
529 destDc
.SetTextForeground( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNTEXT
) );
531 destDc
.SetTextForeground( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW
) );
533 destDc
.SetTextBackground( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
) );
535 destDc
.DrawText( mLabelText
, txtPos
.x
, txtPos
.y
);
540 #ifdef __WXMSW // This is currently MSW specific
541 greay_out_image_on_dc( destDc
, destDim
.x
, destDim
.y
);
543 wxBrush
checkerBrush( wxBitmap( (const char*)_gDisableImage
,8,8) );
544 checkerBrush
.SetColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE
) );
545 destDc
.SetBrush( checkerBrush
);
546 destDc
.DrawRectangle( imgPos
.x
, imgPos
.y
, srcBmp
->GetWidth()+1, srcBmp
->GetHeight()+1);
549 // adjust button size to fit the new dimensions of the label
550 if ( !mSizeIsSet
&& 0 )
554 destBmp
->GetWidth() + mMarginX
*2,
555 destBmp
->GetHeight() + mMarginY
*2, 0
559 void wxNewBitmapButton::RenderAllLabelImages()
561 if( !mIsCreated
) return;
562 RenderLabelImage( mpDisabledImg
, &mDepressedBmp
, FALSE
);
563 RenderLabelImage( mpPressedImg
, &mDepressedBmp
, TRUE
, TRUE
);
564 RenderLabelImage( mpDepressedImg
, &mDepressedBmp
, TRUE
, FALSE
);
565 if ( mHasFocusedBmp
){
566 RenderLabelImage( mpFocusedImg
, &mFocusedBmp
, TRUE
, FALSE
);
571 void wxNewBitmapButton::RenderLabelImages()
573 if ( !mIsCreated
) return;
577 RenderLabelImage( mpDisabledImg
, &mDepressedBmp
, FALSE
);
583 RenderLabelImage( mpPressedImg
, &mDepressedBmp
, TRUE
, TRUE
);
588 if ( mHasFocusedBmp
)
590 RenderLabelImage( mpFocusedImg
, &mFocusedBmp
, TRUE
, FALSE
);
592 RenderLabelImage( mpDepressedImg
, &mDepressedBmp
, TRUE
, FALSE
);
595 RenderLabelImage( mpDepressedImg
, &mDepressedBmp
, TRUE
, FALSE
);
599 void wxNewBitmapButton::DrawDecorations( wxDC
& dc
)
603 DrawShade( 1, dc
, mGrayPen
, mGrayPen
);
609 DrawShade( 0, dc
, mDarkPen
, mLightPen
);
611 DrawShade( 0, dc
, mLightPen
, mDarkPen
);
614 DrawShade( 0, dc
, mGrayPen
, mGrayPen
);
620 DrawShade( 0, dc
, mDarkPen
, mGrayPen
);
621 DrawShade( 1, dc
, mBlackPen
, mLightPen
);
625 DrawShade( 0, dc
, mGrayPen
, mDarkPen
);
626 DrawShade( 1, dc
, mLightPen
, mBlackPen
);
631 void wxNewBitmapButton::SetLabel(const wxBitmap
& labelBitmap
,
632 const wxString
& labelText
)
636 mLabelText
= labelText
;
637 mDepressedBmp
= labelBitmap
;
639 //RenderLabelImages();
640 RenderAllLabelImages();
643 void wxNewBitmapButton::SetAlignments( int alignText
,
652 mTextAlignment
= alignText
;
653 mTextToLabelGap
= textToLabelGap
;
655 //RenderLabelImages();
656 RenderAllLabelImages();
661 void wxNewBitmapButton::OnLButtonDown( wxMouseEvent
& event
)
663 mPrevPressedState
= FALSE
;
673 void wxNewBitmapButton::OnLButtonUp( wxMouseEvent
& event
)
675 if ( !mDragStarted
) return;
677 mDragStarted
= FALSE
;
684 if ( IsInWindow( event
.m_x
, event
.m_y
) )
686 // fire event, if mouse was released
687 // within the bounds of button
688 wxCommandEvent
cmd( mFiredEventType
, GetId() );
689 GetParent()->ProcessEvent( cmd
);
693 bool wxNewBitmapButton::IsInWindow( int x
, int y
)
696 GetSize( &width
, &height
);
698 return ( x
>= 0 && y
>= 0 &&
703 void wxNewBitmapButton::OnMouseMove( wxMouseEvent
& event
)
705 mPrevPressedState
=mIsPressed
;
706 mPrevInFocusState
=mIsInFocus
;
707 if ( !mIsInFocus
&& IsInWindow( event
.m_x
, event
.m_y
) )
715 if ( mIsInFocus
&& !IsInWindow( event
.m_x
, event
.m_y
) )
725 if ( IsInWindow( event
.m_x
, event
.m_y
) )
732 if((mIsPressed
!= mPrevPressedState
)||(mIsInFocus
!=mPrevInFocusState
)){
737 void wxNewBitmapButton::OnSize( wxSizeEvent
& event
)
742 void wxNewBitmapButton::Reshape( )
745 bool wasCreated
= mIsCreated
;
750 // in the case of loading button from stream, check if we
751 // have non-empty image-file name, load if possible
753 if ( mImageFileName
!= "" )
755 mDepressedBmp
.LoadFile( mImageFileName
, mImageFileType
);
757 //wxMessageBox("Image Loaded!!!");
760 //RenderLabelImages();
761 RenderAllLabelImages();
763 wxBitmap
* pCurImg
= GetStateLabel();
765 int w
= pCurImg
->GetWidth(),
766 h
= pCurImg
->GetHeight();
768 SetSize( 0,0, w
+ mMarginX
*2, h
+ mMarginY
*2 , 0 );
772 void wxNewBitmapButton::DrawLabel( wxDC
& dc
)
774 wxBitmap
* pCurBmp
= GetStateLabel();
776 if ( pCurBmp
== NULL
)
779 OnSize( evt
); // fake it up!
781 //RenderLabelImages();
782 pCurBmp
= GetStateLabel();
786 mdc
.SelectObject( *pCurBmp
);
788 dc
.Blit( mMarginX
, mMarginY
,
790 pCurBmp
->GetHeight(),
794 mdc
.SelectObject( wxNullBitmap
);
797 void wxNewBitmapButton::OnPaint( wxPaintEvent
& event
)
801 // first, make sure images for current state are prepared
802 //RenderLabelImages();
806 DrawDecorations( dc
);
809 void wxNewBitmapButton::OnEraseBackground( wxEraseEvent
& event
)
814 void wxNewBitmapButton::OnKillFocus( wxFocusEvent
& event
)
818 wxMessageBox("kill-focus for button!");