]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/fl/newbmpbtn.cpp
Applied patch [ 1284335 ] doc update for wxString::operator[]
[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_LEFT_DCLICK ( wxNewBitmapButton::OnLButtonDown )
215 EVT_ENTER_WINDOW( wxNewBitmapButton::OnMouseEnter )
216 EVT_LEAVE_WINDOW( wxNewBitmapButton::OnMouseLeave )
217
218 EVT_SIZE ( wxNewBitmapButton::OnSize )
219 EVT_PAINT( wxNewBitmapButton::OnPaint )
220
221 //EVT_KILL_FOCUS( wxNewBitmapButton::OnKillFocus )
222
223 EVT_ERASE_BACKGROUND( wxNewBitmapButton::OnEraseBackground )
224
225 EVT_IDLE(wxNewBitmapButton::OnIdle)
226
227 END_EVENT_TABLE()
228
229 wxNewBitmapButton::wxNewBitmapButton( const wxBitmap& labelBitmap,
230 const wxString& labelText,
231 int alignText,
232 bool isFlat,
233 int firedEventType,
234 int marginX,
235 int marginY,
236 int textToLabelGap,
237 bool isSticky)
238 : mTextToLabelGap ( textToLabelGap ),
239 mMarginX( marginX ),
240 mMarginY( marginY ),
241 mTextAlignment( alignText ),
242 mIsSticky( isSticky ),
243 mIsFlat( isFlat ),
244 mLabelText( labelText ),
245 mImageFileType( wxBITMAP_TYPE_INVALID ),
246 mDepressedBmp( labelBitmap ),
247
248 mpDepressedImg( NULL ),
249 mpPressedImg ( NULL ),
250 mpDisabledImg ( NULL ),
251 mpFocusedImg ( NULL ),
252
253
254 mDragStarted ( false ),
255 mIsPressed ( false ),
256 mIsInFocus ( false ),
257 mIsToggled ( false ),
258 mHasFocusedBmp( false ),
259 mFiredEventType( firedEventType ),
260
261 mBlackPen( wxColour( 0, 0, 0), 1, wxSOLID ),
262 mDarkPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxSOLID ),
263 mGrayPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE), 1, wxSOLID ),
264 mLightPen( wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT), 1, wxSOLID ),
265
266 mIsCreated( false ),
267 mSizeIsSet( false )
268
269 {
270 }
271
272 wxNewBitmapButton::wxNewBitmapButton( const wxString& bitmapFileName,
273 const wxBitmapType bitmapFileType,
274 const wxString& labelText,
275 int alignText,
276 bool isFlat,
277 int WXUNUSED(firedEventType),
278 int WXUNUSED(marginX),
279 int WXUNUSED(marginY),
280 int WXUNUSED(textToLabelGap),
281 bool WXUNUSED(isSticky))
282
283 : mTextToLabelGap ( 2 ),
284 mMarginX( 2 ),
285 mMarginY( 2 ),
286 mTextAlignment( alignText ),
287 mIsSticky( false ),
288 mIsFlat( isFlat ),
289 mLabelText( labelText ),
290 mImageFileName( bitmapFileName ),
291 mImageFileType( bitmapFileType ),
292
293 mpDepressedImg( NULL ),
294 mpPressedImg ( NULL ),
295 mpDisabledImg ( NULL ),
296 mpFocusedImg ( NULL ),
297
298 mDragStarted ( false ),
299 mIsPressed ( false ),
300 mIsInFocus ( false ),
301 mIsToggled ( false ),
302 mHasFocusedBmp( false ),
303 mFiredEventType( wxEVT_COMMAND_MENU_SELECTED ),
304
305 mBlackPen( wxColour( 0, 0, 0), 1, wxSOLID ),
306 mDarkPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxSOLID ),
307 mGrayPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE), 1, wxSOLID ),
308 mLightPen( wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT), 1, wxSOLID ),
309
310 mIsCreated( false ),
311 mSizeIsSet( false )
312
313 {
314 }
315
316 wxNewBitmapButton::~wxNewBitmapButton(void)
317 {
318 DestroyLabels();
319 }
320
321 void wxNewBitmapButton::DrawShade( int outerLevel,
322 wxDC& dc,
323 wxPen& upperLeftSidePen,
324 wxPen& lowerRightSidePen )
325 {
326 wxBitmap* pBmp = GetStateLabel();
327
328 int x = mMarginX - (outerLevel + 2);
329 int y = mMarginY - (outerLevel + 2);
330
331 int height = pBmp->GetHeight() + (outerLevel + 2)*2 - 1;
332 int width = pBmp->GetWidth() + (outerLevel + 2)*2 - 1;
333
334 dc.SetPen( upperLeftSidePen );
335 dc.DrawLine( x,y, x + width, y );
336 dc.DrawLine( x,y, x, y + height );
337
338 dc.SetPen( lowerRightSidePen );
339 dc.DrawLine( x + width, y, x + width, y + height + 1 );
340 dc.DrawLine( x, y + height, x + width, y + height );
341 }
342
343 void wxNewBitmapButton::DestroyLabels()
344 {
345 if ( mpDepressedImg ) delete mpDepressedImg;
346 if ( mpPressedImg ) delete mpPressedImg;
347 if ( mpDisabledImg ) delete mpDisabledImg;
348 if ( mpFocusedImg ) delete mpFocusedImg;
349
350 mpDepressedImg = NULL;
351 mpPressedImg = NULL;
352 mpDisabledImg = NULL;
353 mpFocusedImg = NULL;
354 }
355
356 wxBitmap* wxNewBitmapButton::GetStateLabel()
357 {
358 if ( IsEnabled() )
359 {
360 if ( mIsPressed )
361 {
362 return mpPressedImg;
363 }
364 else
365 {
366 if ( mIsInFocus )
367 {
368 if ( mHasFocusedBmp )
369
370 return mpFocusedImg;
371 else
372 return mpDepressedImg;
373 }
374 else
375 return mpDepressedImg;
376 }
377 }
378 else
379 return mpDisabledImg;
380 }
381
382 #ifndef __WXMSW__
383
384 static const unsigned char _gDisableImage[] = { 0x55,0xAA,0x55,0xAA,
385 0x55,0xAA,0x55,0xAA,
386 0x55,0xAA,0x55,0xAA,
387 0x55,0xAA,0x55,0xAA
388 };
389
390 #endif
391
392 void wxNewBitmapButton::RenderLabelImage( wxBitmap*& destBmp, wxBitmap* srcBmp,
393 bool isEnabled, bool isPressed )
394 {
395 if ( destBmp != 0 ) return;
396
397 // render labels on-demand
398
399 wxMemoryDC srcDc;
400 srcDc.SelectObject( *srcBmp );
401
402 bool hasText = ( mTextAlignment != NB_NO_TEXT ) &&
403 ( mLabelText.length() != 0 );
404
405 bool hasImage = (mTextAlignment != NB_NO_IMAGE);
406
407 wxSize destDim;
408 wxPoint txtPos;
409 wxPoint imgPos;
410
411 if ( hasText )
412 {
413 long txtWidth, txtHeight;
414
415 srcDc.SetFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT) );
416 srcDc.GetTextExtent( mLabelText, &txtWidth, &txtHeight );
417
418 if ( mTextAlignment == NB_ALIGN_TEXT_RIGHT )
419 {
420 destDim.x = srcBmp->GetWidth() + 2*mTextToLabelGap + txtWidth;
421
422 destDim.y =
423 wxMax( srcBmp->GetHeight(), txtHeight );
424
425 txtPos.x = srcBmp->GetWidth() + mTextToLabelGap;
426 txtPos.y = (destDim.y - txtHeight)/2;
427 imgPos.x = 0;
428 imgPos.y = (destDim.y - srcBmp->GetHeight())/2;
429 }
430 else
431 if ( mTextAlignment == NB_ALIGN_TEXT_BOTTOM )
432 {
433 destDim.x =
434 wxMax( srcBmp->GetWidth(), txtWidth );
435
436 destDim.y = srcBmp->GetHeight() + mTextToLabelGap + txtHeight;
437
438 txtPos.x = (destDim.x - txtWidth)/2;
439 txtPos.y = srcBmp->GetHeight() + mTextToLabelGap;
440 imgPos.x = (destDim.x - srcBmp->GetWidth())/2;
441 imgPos.y = 0;
442 }
443 else
444 {
445 wxFAIL_MSG(wxT("Unsupported FL alignment type detected in wxNewBitmapButton::RenderLabelImage()"));
446 }
447 }
448 else
449 {
450 imgPos.x = 0;
451 imgPos.y = 0;
452 destDim.x = srcBmp->GetWidth();
453 destDim.y = srcBmp->GetHeight();
454 }
455
456 destBmp = new wxBitmap( int(destDim.x), int(destDim.y) );
457
458 wxMemoryDC destDc;
459 destDc.SelectObject( *destBmp );
460
461 wxBrush grayBrush( wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE), wxSOLID );
462
463 destDc.SetBrush( grayBrush );
464 destDc.SetPen( *wxTRANSPARENT_PEN );
465 destDc.DrawRectangle( 0,0, destDim.x+1, destDim.y+1 );
466
467 if ( isPressed )
468 {
469 ++imgPos.x; ++imgPos.y;
470 ++txtPos.x; ++txtPos.y;
471 }
472
473 if ( hasImage )
474 {
475
476 destDc.Blit( imgPos.x, imgPos.y,
477 srcBmp->GetWidth(),
478 srcBmp->GetHeight(),
479 &srcDc, 0,0, wxCOPY,true );
480 }
481
482 if ( hasText )
483 {
484 wxWindow* pTopWnd = this;
485
486 do
487 {
488 wxWindow* pParent = pTopWnd->GetParent();
489
490 if ( pParent == 0 )
491 break;
492
493 pTopWnd = pParent;
494 } while (1);
495
496 destDc.SetFont( wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT) );
497
498 if ( isEnabled )
499 {
500 destDc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT) );
501 }
502 else
503 {
504 destDc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW) );
505 }
506 destDc.SetTextBackground( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE) );
507
508 destDc.DrawText( mLabelText, txtPos.x, txtPos.y );
509 }
510
511 if ( !isEnabled ){
512
513 #ifdef __WXMSW__ // This is currently MSW specific
514 gray_out_image_on_dc( destDc, destDim.x, destDim.y );
515 #else
516 wxBitmap bmp( (const char*)_gDisableImage,8,8);
517 wxBrush checkerBrush(bmp);
518 checkerBrush.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
519 destDc.SetBrush( checkerBrush );
520 destDc.DrawRectangle( imgPos.x, imgPos.y, srcBmp->GetWidth()+1, srcBmp->GetHeight()+1);
521 #endif
522 }
523 // adjust button size to fit the new dimensions of the label
524 if ( !mSizeIsSet && 0 )
525 {
526 mSizeIsSet = true;
527 SetSize( wxDefaultCoord, wxDefaultCoord,
528 destBmp->GetWidth() + mMarginX*2,
529 destBmp->GetHeight() + mMarginY*2, 0
530 );
531 }
532 destDc.SelectObject( wxNullBitmap );
533
534 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
535 // Map to system colours
536 (void) wxToolBar::MapBitmap(destBmp->GetHBITMAP(), destBmp->GetWidth(), destBmp->GetHeight());
537 #endif
538 }
539
540 void wxNewBitmapButton::RenderAllLabelImages()
541 {
542 if ( !mIsCreated )
543 return;
544 RenderLabelImage( mpDisabledImg, &mDepressedBmp, false );
545 RenderLabelImage( mpPressedImg, &mDepressedBmp, true, true );
546 RenderLabelImage( mpDepressedImg, &mDepressedBmp, true, false );
547 if ( mHasFocusedBmp )
548 {
549 RenderLabelImage( mpFocusedImg, &mFocusedBmp, true, false );
550 }
551 }
552
553
554 void wxNewBitmapButton::RenderLabelImages()
555 {
556 if ( !mIsCreated )
557 return;
558
559 if ( !IsEnabled() )
560 {
561 RenderLabelImage( mpDisabledImg, &mDepressedBmp, false );
562 }
563 else
564
565 if ( mIsPressed )
566
567 RenderLabelImage( mpPressedImg, &mDepressedBmp, true, true );
568 else
569 {
570 if ( mIsInFocus )
571 {
572 if ( mHasFocusedBmp )
573 RenderLabelImage( mpFocusedImg, &mFocusedBmp, true, false );
574 else
575 RenderLabelImage( mpDepressedImg, &mDepressedBmp, true, false );
576 }
577 else
578 RenderLabelImage( mpDepressedImg, &mDepressedBmp, true, false );
579 }
580 }
581
582 bool wxNewBitmapButton::Toggle(bool enable)
583 {
584 if ( mIsToggled == enable )
585 {
586 return false;
587 }
588
589 mIsToggled = enable;
590 Refresh();
591
592 return true;
593 }
594
595 bool wxNewBitmapButton::Enable(bool enable)
596 {
597 if ( enable != m_isEnabled )
598 {
599 if ( mIsInFocus )
600 {
601 mIsInFocus = false;
602 }
603
604 if ( mIsPressed )
605 {
606 mIsPressed = false;
607 }
608
609 Refresh();
610 }
611
612 return wxPanel::Enable( enable );
613 }
614
615 void wxNewBitmapButton::DrawDecorations( wxDC& dc )
616 {
617 if ( mIsFlat )
618 {
619 DrawShade( 1, dc, mGrayPen, mGrayPen );
620 if ( mIsToggled )
621 {
622 DrawShade( 0, dc, mDarkPen, mLightPen );
623 }
624 else if ( mIsInFocus )
625 {
626 if ( mIsPressed )
627 DrawShade( 0, dc, mDarkPen, mLightPen );
628 else
629 DrawShade( 0, dc, mLightPen, mDarkPen );
630 }
631 else
632 DrawShade( 0, dc, mGrayPen, mGrayPen );
633 }
634 else
635 {
636 if ( mIsPressed || mIsToggled )
637 {
638 DrawShade( 0, dc, mDarkPen, mGrayPen );
639 DrawShade( 1, dc, mBlackPen, mLightPen );
640 }
641 else
642 {
643 DrawShade( 0, dc, mGrayPen, mDarkPen );
644 DrawShade( 1, dc, mLightPen, mBlackPen );
645 }
646 }
647 }
648
649 void wxNewBitmapButton::SetLabel(const wxBitmap& labelBitmap,
650 const wxString& labelText )
651 {
652 DestroyLabels();
653
654 mLabelText = labelText;
655 mDepressedBmp = labelBitmap;
656
657 //RenderLabelImages();
658 RenderAllLabelImages();
659 }
660
661 void wxNewBitmapButton::SetAlignments( int alignText,
662 int marginX,
663 int marginY,
664 int textToLabelGap)
665 {
666 DestroyLabels();
667
668 mMarginX = marginX;
669 mMarginY = marginY;
670 mTextAlignment = alignText;
671 mTextToLabelGap = textToLabelGap;
672
673 //RenderLabelImages();
674 RenderAllLabelImages();
675 }
676
677 // event handlers
678
679 void wxNewBitmapButton::OnLButtonDown( wxMouseEvent& WXUNUSED(event) )
680 {
681 mDragStarted = true;
682 mIsPressed = true;
683 Refresh();
684 }
685
686 void wxNewBitmapButton::OnLButtonUp( wxMouseEvent& event )
687 {
688 if ( !mDragStarted )
689 return;
690
691 mDragStarted = false;
692 mIsPressed = false;
693 Refresh();
694
695 if ( IsInWindow( event.m_x, event.m_y ) )
696 {
697 // fire event, if mouse was released
698 // within the bounds of button
699 wxCommandEvent cmd( mFiredEventType, GetId() );
700 GetParent()->ProcessEvent( cmd );
701 }
702 }
703
704 bool wxNewBitmapButton::IsInWindow( int x, int y )
705 {
706 int width, height;
707 GetSize( &width, &height );
708
709 return ( x >= 0 && y >= 0 &&
710 x < width &&
711 y < height );
712 }
713
714 void wxNewBitmapButton::OnMouseEnter( wxMouseEvent& WXUNUSED(event) )
715 {
716 bool prevIsInFocus = mIsInFocus;
717
718 if ( !mIsInFocus )
719 {
720 mIsInFocus = true;
721 }
722 if ( prevIsInFocus != mIsInFocus )
723 {
724 Refresh();
725 }
726 }
727
728 void wxNewBitmapButton::OnMouseLeave( wxMouseEvent& WXUNUSED(event) )
729 {
730 bool prevIsInFocus = mIsInFocus;
731 bool prevIsPressed = mIsPressed;
732 if ( mIsInFocus )
733 {
734 mIsInFocus = false;
735 mIsPressed = false;
736 }
737 if ( prevIsInFocus != mIsInFocus || prevIsPressed != mIsPressed )
738 {
739 Refresh();
740 }
741 }
742
743 void wxNewBitmapButton::OnSize( wxSizeEvent& WXUNUSED(event) )
744 {
745 //Reshape();
746 }
747
748 void wxNewBitmapButton::Reshape( )
749 {
750 bool wasCreated = mIsCreated;
751 mIsCreated = true;
752
753 if ( !wasCreated )
754 {
755 // in the case of loading button from stream, check if we
756 // have non-empty image-file name, load if possible
757
758 if (!mImageFileName.empty())
759 {
760 mDepressedBmp.LoadFile( mImageFileName, mImageFileType );
761
762 //wxMessageBox("Image Loaded!!!");
763 }
764
765 //RenderLabelImages();
766 RenderAllLabelImages();
767
768 wxBitmap* pCurImg = GetStateLabel();
769
770 int w = pCurImg->GetWidth(),
771 h = pCurImg->GetHeight();
772
773 SetSize( 0,0, w + mMarginX*2, h + mMarginY*2 , 0 );
774 }
775 }
776
777 void wxNewBitmapButton::DrawLabel( wxDC& dc )
778 {
779 wxBitmap* pCurBmp = GetStateLabel();
780
781 if ( pCurBmp == NULL )
782 {
783 wxSizeEvent evt;
784 OnSize( evt ); // fake it up!
785
786 //RenderLabelImages();
787 pCurBmp = GetStateLabel();
788 }
789
790 wxMemoryDC mdc;
791 mdc.SelectObject( *pCurBmp );
792
793 dc.Blit( mMarginX, mMarginY,
794 pCurBmp->GetWidth(),
795 pCurBmp->GetHeight(),
796 &mdc, 0,0, wxCOPY
797 );
798
799 mdc.SelectObject( wxNullBitmap );
800 }
801
802 void wxNewBitmapButton::OnPaint( wxPaintEvent& WXUNUSED(event) )
803 {
804 wxPaintDC dc(this);
805
806 // first, make sure images for current state are prepared
807 //RenderLabelImages();
808
809 DrawLabel( dc );
810
811 DrawDecorations( dc );
812 }
813
814 void wxNewBitmapButton::OnEraseBackground( wxEraseEvent& WXUNUSED(event) )
815 {
816 // do nothing
817 }
818
819 void wxNewBitmapButton::OnKillFocus( wxFocusEvent& WXUNUSED(event) )
820 {
821 // useless
822
823 wxMessageBox(wxT("kill-focus for button!"));
824 }
825
826 // ----------------------------------------------------------------------------
827 // UI updates
828 // ----------------------------------------------------------------------------
829
830 void wxNewBitmapButton::OnIdle(wxIdleEvent& event)
831 {
832 DoButtonUpdate();
833
834 event.Skip();
835 }
836
837 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
838 void wxNewBitmapButton::DoButtonUpdate()
839 {
840 wxUpdateUIEvent event(GetId());
841 event.SetEventObject(this);
842
843 if ( GetParent()->ProcessEvent(event) )
844 {
845 if ( event.GetSetEnabled() )
846 {
847 bool enabled = event.GetEnabled();
848 if ( enabled != IsEnabled() )
849 Enable( enabled );
850 }
851 if ( event.GetSetChecked() )
852 Toggle( event.GetChecked() );
853 }
854 }