]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/fl/newbmpbtn.cpp
added the keyboard sample
[wxWidgets.git] / contrib / src / fl / newbmpbtn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: newbmpbtn.cpp
3 // Purpose: wxNewBitmapButton enhanced bitmap button class.
4 // Author: Aleksandras Gluchovas
5 // Modified by:
6 // Created: ??/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Aleksandras Gluchovas
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "newbmpbtn.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/wx.h"
25 #endif
26
27 #include "wx/fl/newbmpbtn.h"
28 #include "wx/utils.h" // import wxMin,wxMax macros
29
30 #ifdef __WXMSW__
31 #include "wx/msw/private.h"
32 #endif
33
34 ///////////// button-label rendering helpers //////////////////
35
36 static int* create_array( int width, int height, int fill = 0 )
37 {
38 int* array = new int[width*height];
39
40 int len = width*height;
41 int i;
42 for ( i = 0; i != len; ++i )
43 array[i] = fill;
44
45 return array;
46 }
47
48 #define GET_ELEM(array,x,y) (array[width*(y)+(x)])
49
50 #define MIN_COLOR_DIFF 10
51
52 #define IS_IN_ARRAY(x,y) ( (x) < width && (y) < height && (x) >= 0 && (y) >= 0 )
53
54 #define GET_RED(col) col & 0xFF
55 #define GET_GREEN(col) (col >> 8) & 0xFF
56 #define GET_BLUE(col) (col >> 16) & 0xFF
57
58 #define MAKE_INT_COLOR(red,green,blue) ( (red) | \
59 ( ( (green) << 8 ) & 0xFF00 ) | \
60 ( ( (blue) << 16) & 0xFF0000) \
61 )
62
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 ) \
66 )
67
68 #define MASK_BG 0
69 #define MASK_DARK 1
70 #define MASK_LIGHT 2
71
72 // helper function, used internally
73
74 static void gray_out_pixmap( int* src, int* dest, int width, int height )
75 {
76 // assuming the pixels along the edges are of the background color
77
78 int x = 0;
79 int y = 1;
80
81 do
82 {
83 int cur = GET_ELEM(src,x,y);
84
85
86 if ( IS_IN_ARRAY(x-1,y-1) )
87 {
88 int upperElem = GET_ELEM(src,x-1,y-1);
89
90 // if the upper element is lighter than current
91 if ( IS_GREATER(upperElem,cur) )
92 {
93 GET_ELEM(dest,x,y) = MASK_DARK;
94 }
95 else
96 // if the current element is ligher than the upper
97 if ( IS_GREATER(cur,upperElem) )
98 {
99 GET_ELEM(dest,x,y) = MASK_LIGHT;
100 }
101 else
102 {
103 if ( GET_ELEM(dest,x-1,y-1) == MASK_LIGHT )
104
105 GET_ELEM(dest,x,y) = MASK_BG;
106
107 if ( GET_ELEM(dest,x-1,y-1 ) == MASK_DARK )
108
109 GET_ELEM(dest,x,y) = MASK_DARK;
110 else
111 GET_ELEM(dest,x,y) = MASK_BG;
112 }
113 }
114
115 // go zig-zag
116
117 if ( IS_IN_ARRAY(x+1,y-1) )
118 {
119 ++x;
120 --y;
121 }
122 else
123 {
124 while ( IS_IN_ARRAY(x-1,y+1) )
125 {
126 --x;
127 ++y;
128 }
129
130 if ( IS_IN_ARRAY(x,y+1) )
131 {
132 ++y;
133 continue;
134 }
135 else
136 {
137 if ( IS_IN_ARRAY(x+1,y) )
138 {
139 ++x;
140 continue;
141 }
142 else break;
143 }
144 }
145
146 } while (1);
147 }
148
149 // algorithm for making the image look "grayed" (e.g. disabled button)
150 // NOTE:: used GetPixel(), which is Windows-Only!
151
152 void gray_out_image_on_dc( wxDC& dc, int width, int height )
153 {
154 // assuming the pixels along the edges are of the background color
155 wxColour bgCol;
156 dc.GetPixel( 0, 0, &bgCol );
157
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 );
161
162 int* src = create_array( width, height, MASK_BG );
163 int* dest = create_array( width, height, MASK_BG );
164
165 int x, y;
166 for ( y = 0; y != height; ++y )
167 {
168 for ( x = 0; x != width; ++x )
169 {
170 wxColour col;
171 dc.GetPixel( x,y, &col );
172
173 GET_ELEM(src,x,y) = MAKE_INT_COLOR( col.Red(), col.Green(), col.Blue() );
174 }
175 }
176 gray_out_pixmap( src, dest, width, height );
177
178 for ( y = 0; y != height; ++y )
179 {
180 for ( x = 0; x != width; ++x )
181 {
182 int mask = GET_ELEM(dest,x,y);
183
184 switch (mask)
185 {
186 case MASK_BG : { dc.SetPen( bgPen );
187 dc.DrawPoint( x,y ); break;
188 }
189 case MASK_DARK : { dc.SetPen( darkPen );
190 dc.DrawPoint( x,y ); break;
191 }
192 case MASK_LIGHT : { dc.SetPen( lightPen );
193 dc.DrawPoint( x,y ); break;
194 }
195 default : break;
196 }
197 }
198 }
199 delete [] src;
200 delete [] dest;
201 }
202
203 ///////////////////////////////
204
205 /***** Implementation for class wxNewBitmapButton *****/
206
207 IMPLEMENT_DYNAMIC_CLASS(wxNewBitmapButton, wxPanel)
208
209 BEGIN_EVENT_TABLE( wxNewBitmapButton, wxPanel )
210
211 EVT_LEFT_DOWN ( wxNewBitmapButton::OnLButtonDown )
212 EVT_LEFT_UP ( wxNewBitmapButton::OnLButtonUp )
213 EVT_LEFT_DCLICK ( wxNewBitmapButton::OnLButtonDClick )
214 EVT_ENTER_WINDOW( wxNewBitmapButton::OnMouseEnter )
215 EVT_LEAVE_WINDOW( wxNewBitmapButton::OnMouseLeave )
216
217 EVT_SIZE ( wxNewBitmapButton::OnSize )
218 EVT_PAINT( wxNewBitmapButton::OnPaint )
219
220 //EVT_KILL_FOCUS( wxNewBitmapButton::OnKillFocus )
221
222 EVT_ERASE_BACKGROUND( wxNewBitmapButton::OnEraseBackground )
223
224 END_EVENT_TABLE()
225
226 wxNewBitmapButton::wxNewBitmapButton( const wxBitmap& labelBitmap,
227 const wxString& labelText,
228 int alignText,
229 bool isFlat,
230 int firedEventType,
231 int marginX,
232 int marginY,
233 int textToLabelGap,
234 bool isSticky)
235 : mTextToLabelGap ( textToLabelGap ),
236 mMarginX( marginX ),
237 mMarginY( marginY ),
238 mTextAlignment( alignText ),
239 mIsSticky( isSticky ),
240 mIsFlat( isFlat ),
241 mLabelText( labelText ),
242 mImageFileType( wxBITMAP_TYPE_INVALID ),
243 mDepressedBmp( labelBitmap ),
244
245 mpDepressedImg( NULL ),
246 mpPressedImg ( NULL ),
247 mpDisabledImg ( NULL ),
248 mpFocusedImg ( NULL ),
249
250
251 mDragStarted ( FALSE ),
252 mIsPressed ( FALSE ),
253 mIsInFocus( FALSE ),
254 mHasFocusedBmp( FALSE ),
255 mFiredEventType( firedEventType ),
256
257 mBlackPen( wxColour( 0, 0, 0), 1, wxSOLID ),
258 mDarkPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxSOLID ),
259 mGrayPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE), 1, wxSOLID ),
260 mLightPen( wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT), 1, wxSOLID ),
261
262 mIsCreated( FALSE ),
263 mSizeIsSet( FALSE )
264
265 {
266 }
267
268 wxNewBitmapButton::wxNewBitmapButton( const wxString& bitmapFileName,
269 const wxBitmapType bitmapFileType,
270 const wxString& labelText,
271 int alignText,
272 bool isFlat,
273 int firedEventType,
274 int marginX,
275 int marginY,
276 int textToLabelGap,
277 bool isSticky)
278
279 : mTextToLabelGap ( 2 ),
280 mMarginX( 2 ),
281 mMarginY( 2 ),
282 mTextAlignment( alignText ),
283 mIsSticky( FALSE ),
284 mIsFlat( isFlat ),
285 mLabelText( labelText ),
286 mImageFileName( bitmapFileName ),
287 mImageFileType( bitmapFileType ),
288
289 mpDepressedImg( NULL ),
290 mpPressedImg ( NULL ),
291 mpDisabledImg ( NULL ),
292 mpFocusedImg ( NULL ),
293
294 mDragStarted ( FALSE ),
295 mIsPressed ( FALSE ),
296 mIsInFocus ( FALSE ),
297 mHasFocusedBmp( FALSE ),
298 mFiredEventType( wxEVT_COMMAND_MENU_SELECTED ),
299
300 mBlackPen( wxColour( 0, 0, 0), 1, wxSOLID ),
301 mDarkPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxSOLID ),
302 mGrayPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE), 1, wxSOLID ),
303 mLightPen( wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT), 1, wxSOLID ),
304
305 mIsCreated( FALSE ),
306 mSizeIsSet( FALSE )
307
308 {
309 }
310
311 wxNewBitmapButton::~wxNewBitmapButton(void)
312 {
313 DestroyLabels();
314 }
315
316 void wxNewBitmapButton::DrawShade( int outerLevel,
317 wxDC& dc,
318 wxPen& upperLeftSidePen,
319 wxPen& lowerRightSidePen )
320 {
321 wxBitmap* pBmp = GetStateLabel();
322
323 int x = mMarginX - (outerLevel + 1);
324 int y = mMarginY - (outerLevel + 1);
325
326 int height = pBmp->GetHeight() + (outerLevel + 1)*2 - 1;
327 int width = pBmp->GetWidth() + (outerLevel + 1)*2 - 1;
328
329 dc.SetPen( upperLeftSidePen );
330 dc.DrawLine( x,y, x + width, y );
331 dc.DrawLine( x,y, x, y + height );
332
333 dc.SetPen( lowerRightSidePen );
334 dc.DrawLine( x + width, y, x + width, y + height + 1 );
335 dc.DrawLine( x, y + height, x + width, y + height );
336 }
337
338 void wxNewBitmapButton::DestroyLabels()
339 {
340 if ( mpDepressedImg ) delete mpDepressedImg;
341 if ( mpPressedImg ) delete mpPressedImg;
342 if ( mpDisabledImg ) delete mpDisabledImg;
343 if ( mpFocusedImg ) delete mpFocusedImg;
344
345 mpDepressedImg = NULL;
346 mpPressedImg = NULL;
347 mpDisabledImg = NULL;
348 mpFocusedImg = NULL;
349 }
350
351 wxBitmap* wxNewBitmapButton::GetStateLabel()
352 {
353 if ( IsEnabled() )
354 {
355 if ( mIsPressed )
356 {
357 return mpPressedImg;
358 }
359 else
360 {
361 if ( mIsInFocus )
362 {
363 if ( mHasFocusedBmp )
364
365 return mpFocusedImg;
366 else
367 return mpDepressedImg;
368 }
369 else
370 return mpDepressedImg;
371 }
372 }
373 else
374 return mpDisabledImg;
375 }
376
377 static const unsigned char _gDisableImage[] = { 0x55,0xAA,0x55,0xAA,
378 0x55,0xAA,0x55,0xAA,
379 0x55,0xAA,0x55,0xAA,
380 0x55,0xAA,0x55,0xAA
381 };
382 void wxNewBitmapButton::RenderLabelImage( wxBitmap*& destBmp, wxBitmap* srcBmp,
383 bool isEnabled, bool isPressed )
384 {
385 if ( destBmp != 0 ) return;
386
387 // render labels on-demand
388
389 wxMemoryDC srcDc;
390 srcDc.SelectObject( *srcBmp );
391
392 bool hasText = ( mTextAlignment != NB_NO_TEXT ) &&
393 ( mLabelText.length() != 0 );
394
395 bool hasImage = (mTextAlignment != NB_NO_IMAGE);
396
397 wxSize destDim;
398 wxPoint txtPos;
399 wxPoint imgPos;
400
401 if ( hasText )
402 {
403 long txtWidth, txtHeight;
404
405 srcDc.SetFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT) );
406 srcDc.GetTextExtent( mLabelText, &txtWidth, &txtHeight );
407
408 if ( mTextAlignment == NB_ALIGN_TEXT_RIGHT )
409 {
410 destDim.x = srcBmp->GetWidth() + 2*mTextToLabelGap + txtWidth;
411
412 destDim.y =
413 wxMax( srcBmp->GetHeight(), txtHeight );
414
415 txtPos.x = srcBmp->GetWidth() + mTextToLabelGap;
416 txtPos.y = (destDim.y - txtHeight)/2;
417 imgPos.x = 0;
418 imgPos.y = (destDim.y - srcBmp->GetHeight())/2;
419 }
420 else
421 if ( mTextAlignment == NB_ALIGN_TEXT_BOTTOM )
422 {
423 destDim.x =
424 wxMax( srcBmp->GetWidth(), txtWidth );
425
426 destDim.y = srcBmp->GetHeight() + mTextToLabelGap + txtHeight;
427
428 txtPos.x = (destDim.x - txtWidth)/2;
429 txtPos.y = srcBmp->GetHeight() + mTextToLabelGap;
430 imgPos.x = (destDim.x - srcBmp->GetWidth())/2;
431 imgPos.y = 0;
432 }
433 else
434 {
435 wxFAIL_MSG("Unsupported FL alignment type detected in wxNewBitmapButton::RenderLabelImage()");
436 }
437 }
438 else
439 {
440 imgPos.x = 0;
441 imgPos.y = 0;
442 destDim.x = srcBmp->GetWidth();
443 destDim.y = srcBmp->GetHeight();
444 }
445
446 destBmp = new wxBitmap( int(destDim.x), int(destDim.y) );
447
448 wxMemoryDC destDc;
449 destDc.SelectObject( *destBmp );
450
451 wxBrush grayBrush( wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE), wxSOLID );
452 wxPen nullPen( wxColour(0,0,0), 1, wxTRANSPARENT );
453
454 destDc.SetBrush( grayBrush );
455 destDc.SetPen( nullPen );
456
457 destDc.DrawRectangle( 0,0, destDim.x+1, destDim.y+1 );
458
459 if ( isPressed )
460 {
461 ++imgPos.x; ++imgPos.y;
462 ++txtPos.x; ++txtPos.y;
463 }
464
465 if ( hasImage )
466 {
467
468 destDc.Blit( imgPos.x, imgPos.y,
469 srcBmp->GetWidth()+1,
470 srcBmp->GetHeight()+1,
471 &srcDc, 0,0, wxCOPY,TRUE );
472 }
473
474 if ( hasText )
475 {
476 wxWindow* pTopWnd = this;
477
478 do
479 {
480 wxWindow* pParent = pTopWnd->GetParent();
481
482 if ( pParent == 0 )
483 break;
484
485 pTopWnd = pParent;
486 } while (1);
487
488 destDc.SetFont( wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT) );
489
490 if ( isEnabled )
491 {
492 destDc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT) );
493 }
494 else
495 {
496 destDc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW) );
497 }
498 destDc.SetTextBackground( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE) );
499
500 destDc.DrawText( mLabelText, txtPos.x, txtPos.y );
501 }
502
503 if ( !isEnabled ){
504
505 #ifdef __WXMSW__ // This is currently MSW specific
506 gray_out_image_on_dc( destDc, destDim.x, destDim.y );
507 #else
508 wxBrush checkerBrush( wxBitmap( (const char*)_gDisableImage,8,8) );
509 checkerBrush.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
510 destDc.SetBrush( checkerBrush );
511 destDc.DrawRectangle( imgPos.x, imgPos.y, srcBmp->GetWidth()+1, srcBmp->GetHeight()+1);
512 #endif
513 }
514 // adjust button size to fit the new dimensions of the label
515 if ( !mSizeIsSet && 0 )
516 {
517 mSizeIsSet = TRUE;
518 SetSize( -1,-1,
519 destBmp->GetWidth() + mMarginX*2,
520 destBmp->GetHeight() + mMarginY*2, 0
521 );
522 }
523 destDc.SelectObject( wxNullBitmap );
524
525 #ifdef __WXMSW__
526 // Map to system colours
527 (void) MapBitmap(destBmp->GetHBITMAP(), destBmp->GetWidth(), destBmp->GetHeight());
528 #endif
529 }
530
531 void wxNewBitmapButton::RenderAllLabelImages()
532 {
533 if ( !mIsCreated )
534 return;
535 RenderLabelImage( mpDisabledImg, &mDepressedBmp, FALSE );
536 RenderLabelImage( mpPressedImg, &mDepressedBmp, TRUE, TRUE );
537 RenderLabelImage( mpDepressedImg, &mDepressedBmp, TRUE, FALSE );
538 if ( mHasFocusedBmp )
539 {
540 RenderLabelImage( mpFocusedImg, &mFocusedBmp, TRUE, FALSE );
541 }
542 }
543
544
545 void wxNewBitmapButton::RenderLabelImages()
546 {
547 if ( !mIsCreated )
548 return;
549
550 if ( !IsEnabled() )
551 {
552 RenderLabelImage( mpDisabledImg, &mDepressedBmp, FALSE );
553 }
554 else
555
556 if ( mIsPressed )
557
558 RenderLabelImage( mpPressedImg, &mDepressedBmp, TRUE, TRUE );
559 else
560 {
561 if ( mIsInFocus )
562 {
563 if ( mHasFocusedBmp )
564 RenderLabelImage( mpFocusedImg, &mFocusedBmp, TRUE, FALSE );
565 else
566 RenderLabelImage( mpDepressedImg, &mDepressedBmp, TRUE, FALSE );
567 }
568 else
569 RenderLabelImage( mpDepressedImg, &mDepressedBmp, TRUE, FALSE );
570 }
571 }
572
573 bool wxNewBitmapButton::Enable(bool enable)
574 {
575 if ( enable != m_isEnabled )
576 {
577 if ( mIsInFocus )
578 {
579 mIsInFocus = FALSE;
580 }
581
582 if ( mIsPressed )
583 {
584 mIsPressed = FALSE;
585 }
586
587 Refresh();
588 }
589
590 return wxPanel::Enable( enable );
591 }
592
593 void wxNewBitmapButton::DrawDecorations( wxDC& dc )
594 {
595 if ( mIsFlat )
596 {
597 DrawShade( 1, dc, mGrayPen, mGrayPen );
598
599 if ( mIsInFocus )
600 {
601 if ( mIsPressed )
602 DrawShade( 0, dc, mDarkPen, mLightPen );
603 else
604 DrawShade( 0, dc, mLightPen, mDarkPen );
605 }
606 else
607 DrawShade( 0, dc, mGrayPen, mGrayPen );
608 }
609 else
610 {
611 if ( mIsPressed )
612 {
613 DrawShade( 0, dc, mDarkPen, mGrayPen );
614 DrawShade( 1, dc, mBlackPen, mLightPen );
615 }
616 else
617 {
618 DrawShade( 0, dc, mGrayPen, mDarkPen );
619 DrawShade( 1, dc, mLightPen, mBlackPen );
620 }
621 }
622 }
623
624 void wxNewBitmapButton::SetLabel(const wxBitmap& labelBitmap,
625 const wxString& labelText )
626 {
627 DestroyLabels();
628
629 mLabelText = labelText;
630 mDepressedBmp = labelBitmap;
631
632 //RenderLabelImages();
633 RenderAllLabelImages();
634 }
635
636 void wxNewBitmapButton::SetAlignments( int alignText,
637 int marginX,
638 int marginY,
639 int textToLabelGap)
640 {
641 DestroyLabels();
642
643 mMarginX = marginX;
644 mMarginY = marginY;
645 mTextAlignment = alignText;
646 mTextToLabelGap = textToLabelGap;
647
648 //RenderLabelImages();
649 RenderAllLabelImages();
650 }
651
652 // event handlers
653
654 void wxNewBitmapButton::OnLButtonDown( wxMouseEvent& event )
655 {
656 mDragStarted = TRUE;
657 mIsPressed = TRUE;
658 Refresh();
659 }
660
661 void wxNewBitmapButton::OnLButtonUp( wxMouseEvent& event )
662 {
663 if ( !mDragStarted )
664 return;
665
666 mDragStarted = FALSE;
667 mIsPressed = FALSE;
668 Refresh();
669
670 if ( IsInWindow( event.m_x, event.m_y ) )
671 {
672 // fire event, if mouse was released
673 // within the bounds of button
674 wxCommandEvent cmd( mFiredEventType, GetId() );
675 GetParent()->ProcessEvent( cmd );
676 }
677 }
678
679 void wxNewBitmapButton::OnLButtonDClick( wxMouseEvent& event )
680 {
681 if ( IsInWindow( event.m_x, event.m_y ) )
682 {
683 // fire event, if mouse was released
684 // within the bounds of button
685 wxCommandEvent cmd( mFiredEventType, GetId() );
686 GetParent()->ProcessEvent( cmd );
687
688 mDragStarted = FALSE;
689 mIsPressed = FALSE;
690 Refresh();
691 }
692 }
693
694 bool wxNewBitmapButton::IsInWindow( int x, int y )
695 {
696 int width, height;
697 GetSize( &width, &height );
698
699 return ( x >= 0 && y >= 0 &&
700 x < width &&
701 y < height );
702 }
703
704 void wxNewBitmapButton::OnMouseEnter( wxMouseEvent& event )
705 {
706 bool prevIsInFocus = mIsInFocus;
707
708 if ( !mIsInFocus )
709 {
710 mIsInFocus = TRUE;
711 }
712 if ( prevIsInFocus != mIsInFocus )
713 {
714 Refresh();
715 }
716 }
717
718 void wxNewBitmapButton::OnMouseLeave( wxMouseEvent& event )
719 {
720 bool prevIsInFocus = mIsInFocus;
721 bool prevIsPressed = mIsPressed;
722 if ( mIsInFocus )
723 {
724 mIsInFocus = FALSE;
725 mIsPressed = FALSE;
726 }
727 if ( prevIsInFocus != mIsInFocus || prevIsPressed != mIsPressed )
728 {
729 Refresh();
730 }
731 }
732
733 void wxNewBitmapButton::OnSize( wxSizeEvent& event )
734 {
735 //Reshape();
736 }
737
738 void wxNewBitmapButton::Reshape( )
739 {
740 bool wasCreated = mIsCreated;
741 mIsCreated = TRUE;
742
743 if ( !wasCreated )
744 {
745 // in the case of loading button from stream, check if we
746 // have non-empty image-file name, load if possible
747
748 if ( mImageFileName != "" )
749 {
750 mDepressedBmp.LoadFile( mImageFileName, mImageFileType );
751
752 //wxMessageBox("Image Loaded!!!");
753 }
754
755 //RenderLabelImages();
756 RenderAllLabelImages();
757
758 wxBitmap* pCurImg = GetStateLabel();
759
760 int w = pCurImg->GetWidth(),
761 h = pCurImg->GetHeight();
762
763 SetSize( 0,0, w + mMarginX*2, h + mMarginY*2 , 0 );
764 }
765 }
766
767 void wxNewBitmapButton::DrawLabel( wxDC& dc )
768 {
769 wxBitmap* pCurBmp = GetStateLabel();
770
771 if ( pCurBmp == NULL )
772 {
773 wxSizeEvent evt;
774 OnSize( evt ); // fake it up!
775
776 //RenderLabelImages();
777 pCurBmp = GetStateLabel();
778 }
779
780 wxMemoryDC mdc;
781 mdc.SelectObject( *pCurBmp );
782
783 dc.Blit( mMarginX, mMarginY,
784 pCurBmp->GetWidth(),
785 pCurBmp->GetHeight(),
786 &mdc, 0,0, wxCOPY
787 );
788
789 mdc.SelectObject( wxNullBitmap );
790 }
791
792 void wxNewBitmapButton::OnPaint( wxPaintEvent& event )
793 {
794 wxPaintDC dc(this);
795
796 // first, make sure images for current state are prepared
797 //RenderLabelImages();
798
799 DrawLabel( dc );
800
801 DrawDecorations( dc );
802 }
803
804 void wxNewBitmapButton::OnEraseBackground( wxEraseEvent& event )
805 {
806 // do nothing
807 }
808
809 void wxNewBitmapButton::OnKillFocus( wxFocusEvent& event )
810 {
811 // useless
812
813 wxMessageBox("kill-focus for button!");
814 }
815
816 #ifdef __WXMSW__
817 WXHBITMAP wxNewBitmapButton::MapBitmap(WXHBITMAP bitmap, int width, int height)
818 {
819 MemoryHDC hdcMem;
820
821 if ( !hdcMem )
822 {
823 wxLogLastError(_T("CreateCompatibleDC"));
824
825 return bitmap;
826 }
827
828 SelectInHDC bmpInHDC(hdcMem, (HBITMAP)bitmap);
829
830 if ( !bmpInHDC )
831 {
832 wxLogLastError(_T("SelectObject"));
833
834 return bitmap;
835 }
836
837 wxCOLORMAP *cmap = wxGetStdColourMap();
838
839 for ( int i = 0; i < width; i++ )
840 {
841 for ( int j = 0; j < height; j++ )
842 {
843 COLORREF pixel = ::GetPixel(hdcMem, i, j);
844
845 for ( size_t k = 0; k < wxSTD_COL_MAX; k++ )
846 {
847 COLORREF col = cmap[k].from;
848 if ( abs(GetRValue(pixel) - GetRValue(col)) < 10 &&
849 abs(GetGValue(pixel) - GetGValue(col)) < 10 &&
850 abs(GetBValue(pixel) - GetBValue(col)) < 10 )
851 {
852 ::SetPixel(hdcMem, i, j, cmap[k].to);
853 break;
854 }
855 }
856 }
857 }
858
859 return bitmap;
860 }
861 #endif