home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / wxos2240.zip / wxWindows-2.4.0 / contrib / src / fl / newbmpbtn.cpp < prev    next >
C/C++ Source or Header  |  2002-12-22  |  22KB  |  802 lines

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