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
74 int bgCol
= GET_ELEM(src
,0,0);
81 int cur
= GET_ELEM(src
,x
,y
);
84 int g
= GET_GREEN(cur
);
85 int b
= GET_BLUE(cur
);
87 if ( IS_IN_ARRAY(x
-1,y
-1) )
89 int upperElem
= GET_ELEM(src
,x
-1,y
-1);
91 // if the upper element is lighter than current
92 if ( IS_GREATER(upperElem
,cur
) )
94 GET_ELEM(dest
,x
,y
) = MASK_DARK
;
97 // if the current element is ligher than the upper
98 if ( IS_GREATER(cur
,upperElem
) )
100 GET_ELEM(dest
,x
,y
) = MASK_LIGHT
;
104 if ( GET_ELEM(dest
,x
-1,y
-1) == MASK_LIGHT
)
106 GET_ELEM(dest
,x
,y
) = MASK_BG
;
108 if ( GET_ELEM(dest
,x
-1,y
-1 ) == MASK_DARK
)
110 GET_ELEM(dest
,x
,y
) = MASK_DARK
;
112 GET_ELEM(dest
,x
,y
) = MASK_BG
;
118 if ( IS_IN_ARRAY(x
+1,y
-1) )
124 while( IS_IN_ARRAY(x
-1,y
+1) )
129 if ( IS_IN_ARRAY(x
,y
+1) )
135 if ( IS_IN_ARRAY(x
+1,y
) )
146 // alg. for making the image look "grayed" (e.g. disabled button)
147 // NOTE:: used GetPixel(), which is Windows-Only!
149 void greay_out_image_on_dc( wxDC
& dc
, int width
, int height
)
151 // assuming the pixels along the edges are of the background color
153 dc
.GetPixel( 0, 0, &bgCol
);
155 wxPen
darkPen ( wxColour(128,128,128),1, wxSOLID
);
156 wxPen
lightPen( wxColour(255,255,255),1, wxSOLID
);
157 wxPen
bgPen ( bgCol
, 1, wxSOLID
);
159 int* src
= create_array( width
, height
, MASK_BG
);
160 int* dest
= create_array( width
, height
, MASK_BG
);
163 for( y
= 0; y
!= height
; ++y
)
165 for( int x
= 0; x
!= width
; ++x
)
168 dc
.GetPixel( x
,y
, &col
);
174 int o
= MAKE_INT_COLOR( r
,g
,b
);
176 GET_ELEM(src
,x
,y
) = MAKE_INT_COLOR( col
.Red(), col
.Green(), col
.Blue() );
179 gray_out_pixmap( src
, dest
, width
, height
);
181 for( y
= 0; y
!= height
; ++y
)
183 for( int x
= 0; x
!= width
; ++x
)
185 int mask
= GET_ELEM(dest
,x
,y
);
189 case MASK_BG
: { dc
.SetPen( bgPen
);
190 dc
.DrawPoint( x
,y
); break;
192 case MASK_DARK
: { dc
.SetPen( darkPen
);
193 dc
.DrawPoint( x
,y
); break;
195 case MASK_LIGHT
: { dc
.SetPen( lightPen
);
196 dc
.DrawPoint( x
,y
); break;
206 ///////////////////////////////
208 /***** Impelementation for class wxNewBitmapButton *****/
210 IMPLEMENT_DYNAMIC_CLASS(wxNewBitmapButton
, wxPanel
)
212 BEGIN_EVENT_TABLE( wxNewBitmapButton
, wxPanel
)
214 EVT_LEFT_DOWN( wxNewBitmapButton::OnLButtonDown
)
215 EVT_LEFT_UP ( wxNewBitmapButton::OnLButtonUp
)
216 EVT_MOTION ( wxNewBitmapButton::OnMouseMove
)
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 : mpDepressedImg( NULL
),
237 mpPressedImg ( NULL
),
238 mpDisabledImg ( NULL
),
239 mpFocusedImg ( NULL
),
243 mTextAlignment( alignText
),
246 mIsPressed ( FALSE
),
247 mDragStarted ( FALSE
),
248 mPrevPressedState( FALSE
),
249 mTextToLabelGap ( textToLabelGap
),
251 mBlackPen( wxColour( 0, 0, 0), 1, wxSOLID
),
252 mDarkPen ( wxColour(128,128,128), 1, wxSOLID
),
253 mGrayPen ( wxColour(192,192,192),
255 mLightPen( wxColour(255,255,255), 1, wxSOLID
),
257 mFiredEventType( firedEventType
),
258 mIsSticky( isSticky
),
262 mHasFocusedBmp( FALSE
),
265 mDepressedBmp( labelBitmap
),
266 mLabelText( labelText
),
271 wxNewBitmapButton::wxNewBitmapButton( const wxString
& bitmapFileName
,
272 const int bitmapFileType
,
273 const wxString
& labelText
,
282 : mpDepressedImg( NULL
),
283 mpPressedImg ( NULL
),
284 mpDisabledImg ( NULL
),
285 mpFocusedImg ( NULL
),
289 mTextAlignment( alignText
),
292 mIsPressed ( FALSE
),
293 mDragStarted ( FALSE
),
294 mPrevPressedState( FALSE
),
295 mTextToLabelGap ( 2 ),
297 mBlackPen( wxColour( 0, 0, 0), 1, wxSOLID
),
298 mDarkPen ( wxColour(128,128,128), 1, wxSOLID
),
299 mGrayPen ( wxColour(192,192,192),
301 mLightPen( wxColour(255,255,255), 1, wxSOLID
),
303 mFiredEventType( wxEVT_COMMAND_MENU_SELECTED
),
308 mHasFocusedBmp( FALSE
),
311 mLabelText( labelText
),
312 mImageFileName( bitmapFileName
),
313 mImageFileType( bitmapFileType
)
315 //mDepressedBmp.LoadFile( bitmapFileName, bitmapFileType );
318 wxNewBitmapButton::~wxNewBitmapButton(void)
323 void wxNewBitmapButton::DrawShade( int outerLevel
,
325 wxPen
& upperLeftSidePen
,
326 wxPen
& lowerRightSidePen
)
328 wxBitmap
* pBmp
= GetStateLabel();
330 int x
= mMarginX
- (outerLevel
+ 1);
331 int y
= mMarginY
- (outerLevel
+ 1);
333 int height
= pBmp
->GetHeight() + (outerLevel
+ 1)*2 - 1;
334 int width
= pBmp
->GetWidth() + (outerLevel
+ 1)*2 - 1;
336 dc
.SetPen( upperLeftSidePen
);
337 dc
.DrawLine( x
,y
, x
+ width
, y
);
338 dc
.DrawLine( x
,y
, x
, y
+ height
);
340 dc
.SetPen( lowerRightSidePen
);
341 dc
.DrawLine( x
+ width
, y
, x
+ width
, y
+ height
+ 1 );
342 dc
.DrawLine( x
, y
+ height
, x
+ width
, y
+ height
);
345 void wxNewBitmapButton::DestroyLabels()
347 if ( mpDepressedImg
) delete mpDepressedImg
;
348 if ( mpPressedImg
) delete mpPressedImg
;
349 if ( mpDisabledImg
) delete mpDisabledImg
;
350 if ( mpFocusedImg
) delete mpFocusedImg
;
352 mpDepressedImg
= NULL
;
354 mpDisabledImg
= NULL
;
358 wxBitmap
* wxNewBitmapButton::GetStateLabel()
370 if ( mHasFocusedBmp
)
374 return mpDepressedImg
;
377 return mpDepressedImg
;
381 return mpDisabledImg
;
384 void wxNewBitmapButton::RenderLabelImage( wxBitmap
*& destBmp
, wxBitmap
* srcBmp
,
385 bool isEnabled
, bool isPressed
)
387 if ( destBmp
!= 0 ) return;
389 // render lables on-demand
392 srcDc
.SelectObject( *srcBmp
);
393 wxFont
fnt( 9, wxDECORATIVE
, wxNORMAL
, wxNORMAL
);
395 bool hasText
= ( mTextAlignment
!= NB_NO_TEXT
) &&
396 ( mLabelText
.length() != 0 );
398 bool hasImage
= (mTextAlignment
!= NB_NO_IMAGE
);
406 long txtWidth
, txtHeight
;
408 srcDc
.SetFont( fnt
);
409 srcDc
.GetTextExtent( mLabelText
, &txtWidth
, &txtHeight
);
411 if ( mTextAlignment
== NB_ALIGN_TEXT_RIGHT
)
413 destDim
.x
= srcBmp
->GetWidth() + 2*mTextToLabelGap
+ txtWidth
;
416 wxMax( srcBmp
->GetHeight(), txtHeight
);
418 txtPos
.x
= srcBmp
->GetWidth() + mTextToLabelGap
;
419 txtPos
.y
= (destDim
.y
- txtHeight
)/2;
421 imgPos
.y
= (destDim
.y
- srcBmp
->GetHeight())/2;
424 if ( mTextAlignment
== NB_ALIGN_TEXT_BOTTOM
)
427 wxMax( srcBmp
->GetWidth(), txtWidth
);
429 destDim
.y
= srcBmp
->GetHeight() + mTextToLabelGap
+ txtHeight
;
431 txtPos
.x
= (destDim
.x
- txtWidth
)/2;
432 txtPos
.y
= srcBmp
->GetHeight() + mTextToLabelGap
;
433 imgPos
.x
= (destDim
.x
- srcBmp
->GetWidth())/2;
436 else wxASSERT(0);// unsupported alignment type
442 destDim
.x
= srcBmp
->GetWidth();
443 destDim
.y
= srcBmp
->GetHeight();
446 destBmp
= new wxBitmap( int(destDim
.x
), int(destDim
.y
) );
449 destDc
.SelectObject( *destBmp
);
451 // FOR NOW:: hard-coded label background
452 wxBrush
grayBrush( wxColour(192,192,192), 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
;
468 destDc
.Blit( imgPos
.x
, imgPos
.y
,
469 srcBmp
->GetWidth()+1,
470 srcBmp
->GetHeight()+1,
471 &srcDc
, 0,0, wxCOPY
);
476 wxWindow
* pTopWnd
= this;
480 wxWindow
* pParent
= pTopWnd
->GetParent();
482 if ( pParent
== 0 ) break;
487 destDc
.SetFont( fnt
);
489 // FOR NOW:: hard-coded text colors
490 destDc
.SetTextForeground( wxColour( 0, 0, 0) );
491 destDc
.SetTextBackground( wxColour(192,192,192) );
493 destDc
.DrawText( mLabelText
, txtPos
.x
, txtPos
.y
);
498 greay_out_image_on_dc( destDc
, destDim
.x
, destDim
.y
);
500 // adjust button size to fit the new dimensions of the label
501 if ( !mSizeIsSet
&& 0 )
505 destBmp
->GetWidth() + mMarginX
*2,
506 destBmp
->GetHeight() + mMarginY
*2, 0
511 void wxNewBitmapButton::RenderLabelImages()
513 if ( !mIsCreated
) return;
517 RenderLabelImage( mpDisabledImg
, &mDepressedBmp
, FALSE
);
523 RenderLabelImage( mpPressedImg
, &mDepressedBmp
, TRUE
, TRUE
);
528 if ( mHasFocusedBmp
)
530 RenderLabelImage( mpFocusedImg
, &mFocusedBmp
, TRUE
, FALSE
);
532 RenderLabelImage( mpDepressedImg
, &mDepressedBmp
, TRUE
, FALSE
);
535 RenderLabelImage( mpDepressedImg
, &mDepressedBmp
, TRUE
, FALSE
);
539 void wxNewBitmapButton::DrawDecorations( wxDC
& dc
)
543 DrawShade( 1, dc
, mGrayPen
, mGrayPen
);
549 DrawShade( 0, dc
, mDarkPen
, mLightPen
);
551 DrawShade( 0, dc
, mLightPen
, mDarkPen
);
554 DrawShade( 0, dc
, mGrayPen
, mGrayPen
);
560 DrawShade( 0, dc
, mDarkPen
, mGrayPen
);
561 DrawShade( 1, dc
, mBlackPen
, mLightPen
);
565 DrawShade( 0, dc
, mGrayPen
, mDarkPen
);
566 DrawShade( 1, dc
, mLightPen
, mBlackPen
);
571 void wxNewBitmapButton::SetLabel(const wxBitmap
& labelBitmap
,
572 const wxString
& labelText
)
576 mLabelText
= labelText
;
577 mDepressedBmp
= labelBitmap
;
582 void wxNewBitmapButton::SetAlignments( int alignText
,
591 mTextAlignment
= alignText
;
592 mTextToLabelGap
= textToLabelGap
;
599 void wxNewBitmapButton::OnLButtonDown( wxMouseEvent
& event
)
601 mPrevPressedState
= FALSE
;
611 void wxNewBitmapButton::OnLButtonUp( wxMouseEvent
& event
)
613 if ( !mDragStarted
) return;
615 mDragStarted
= FALSE
;
622 if ( IsInWindow( event
.m_x
, event
.m_y
) )
624 // fire event, if mouse was released
625 // within the bounds of button
626 wxCommandEvent
cmd( mFiredEventType
, GetId() );
627 GetParent()->ProcessEvent( cmd
);
631 bool wxNewBitmapButton::IsInWindow( int x
, int y
)
634 GetSize( &width
, &height
);
636 return ( x
>= 0 && y
>= 0 &&
641 void wxNewBitmapButton::OnMouseMove( wxMouseEvent
& event
)
643 if ( !mIsInFocus
&& IsInWindow( event
.m_x
, event
.m_y
) )
651 if ( mIsInFocus
&& !IsInWindow( event
.m_x
, event
.m_y
) )
661 if ( IsInWindow( event
.m_x
, event
.m_y
) )
667 if ( mIsPressed
!= mPrevPressedState
)
671 mPrevPressedState
= mIsPressed
;
678 void wxNewBitmapButton::OnSize( wxSizeEvent
& event
)
683 void wxNewBitmapButton::Reshape( )
685 bool wasCreated
= mIsCreated
;
690 // in the case of loading button from stream, check if we
691 // have non-empty image-file name, load if possible
693 if ( mImageFileName
!= "" )
695 mDepressedBmp
.LoadFile( mImageFileName
, mImageFileType
);
697 //wxMessageBox("Image Loaded!!!");
702 wxBitmap
* pCurImg
= GetStateLabel();
704 int w
= pCurImg
->GetWidth(),
705 h
= pCurImg
->GetHeight();
707 SetSize( 0,0, w
+ mMarginX
*2, h
+ mMarginY
*2 , 0 );
711 void wxNewBitmapButton::DrawLabel( wxDC
& dc
)
713 wxBitmap
* pCurBmp
= GetStateLabel();
715 if ( pCurBmp
== NULL
)
718 OnSize( evt
); // fake it up!
721 pCurBmp
= GetStateLabel();
725 mdc
.SelectObject( *pCurBmp
);
727 dc
.Blit( mMarginX
, mMarginY
,
729 pCurBmp
->GetHeight(),
733 mdc
.SelectObject( wxNullBitmap
);
736 void wxNewBitmapButton::OnPaint( wxPaintEvent
& event
)
740 // first, make sure images for current state are prepared
745 DrawDecorations( dc
);
748 void wxNewBitmapButton::OnEraseBackground( wxEraseEvent
& event
)
753 void wxNewBitmapButton::OnKillFocus( wxFocusEvent
& event
)
757 wxMessageBox("kill-focus for button!");