]> git.saurik.com Git - wxWidgets.git/blame - contrib/src/fl/newbmpbtn.cpp
Applied patch [ 1174270 ] small fixes to wxGenericDirCtrl
[wxWidgets.git] / contrib / src / fl / newbmpbtn.cpp
CommitLineData
8e08b761 1/////////////////////////////////////////////////////////////////////////////
4cbc57f0
JS
2// Name: newbmpbtn.cpp
3// Purpose: wxNewBitmapButton enhanced bitmap button class.
8e08b761
JS
4// Author: Aleksandras Gluchovas
5// Modified by:
6// Created: ??/09/98
7// RCS-ID: $Id$
8// Copyright: (c) Aleksandras Gluchovas
4cbc57f0 9// Licence: wxWindows licence
8e08b761
JS
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
65e50848
JS
30#ifdef __WXMSW__
31#include "wx/msw/private.h"
32#endif
33
8e08b761
JS
34///////////// button-label rendering helpers //////////////////
35
36static int* create_array( int width, int height, int fill = 0 )
37{
5515f252 38 int* array = new int[width*height];
8e08b761 39
5515f252
GT
40 int len = width*height;
41 int i;
42 for ( i = 0; i != len; ++i )
43 array[i] = fill;
8e08b761 44
5515f252 45 return array;
8e08b761
JS
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
18c45cd6
WS
54#define GET_RED(col) col & 0xFF
55#define GET_GREEN(col) (col >> 8) & 0xFF
56#define GET_BLUE(col) (col >> 16) & 0xFF
8e08b761
JS
57
58#define MAKE_INT_COLOR(red,green,blue) ( (red) | \
59 ( ( (green) << 8 ) & 0xFF00 ) | \
5515f252 60 ( ( (blue) << 16) & 0xFF0000) \
18c45cd6 61 )
8e08b761
JS
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 ) && \
5515f252
GT
65 ( (GET_BLUE(col1) ) > (GET_BLUE(col2) ) + MIN_COLOR_DIFF ) \
66 )
8e08b761 67
5515f252 68#define MASK_BG 0
8e08b761
JS
69#define MASK_DARK 1
70#define MASK_LIGHT 2
71
72// helper function, used internally
73
74static void gray_out_pixmap( int* src, int* dest, int width, int height )
75{
5515f252
GT
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 )
18c45cd6 104
5515f252
GT
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
18c45cd6 117 if ( IS_IN_ARRAY(x+1,y-1) )
5515f252
GT
118 {
119 ++x;
120 --y;
121 }
122 else
123 {
18c45cd6 124 while ( IS_IN_ARRAY(x-1,y+1) )
5515f252
GT
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);
8e08b761
JS
147}
148
4cbc57f0 149// algorithm for making the image look "grayed" (e.g. disabled button)
8e08b761
JS
150// NOTE:: used GetPixel(), which is Windows-Only!
151
152void gray_out_image_on_dc( wxDC& dc, int width, int height )
153{
5515f252
GT
154 // assuming the pixels along the edges are of the background color
155 wxColour bgCol;
156 dc.GetPixel( 0, 0, &bgCol );
157
e1c6c6ae
VS
158 wxPen darkPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW),1, wxSOLID );
159 wxPen lightPen( wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT),1, wxSOLID );
5515f252
GT
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 {
18c45cd6 186 case MASK_BG : { dc.SetPen( bgPen );
5515f252
GT
187 dc.DrawPoint( x,y ); break;
188 }
18c45cd6 189 case MASK_DARK : { dc.SetPen( darkPen );
5515f252
GT
190 dc.DrawPoint( x,y ); break;
191 }
18c45cd6 192 case MASK_LIGHT : { dc.SetPen( lightPen );
5515f252
GT
193 dc.DrawPoint( x,y ); break;
194 }
195 default : break;
196 }
197 }
198 }
199 delete [] src;
200 delete [] dest;
8e08b761
JS
201}
202
203///////////////////////////////
204
4cbc57f0 205/***** Implementation for class wxNewBitmapButton *****/
8e08b761
JS
206
207IMPLEMENT_DYNAMIC_CLASS(wxNewBitmapButton, wxPanel)
208
209BEGIN_EVENT_TABLE( wxNewBitmapButton, wxPanel )
210
45da7759
JS
211 EVT_LEFT_DOWN ( wxNewBitmapButton::OnLButtonDown )
212 EVT_LEFT_UP ( wxNewBitmapButton::OnLButtonUp )
45d6fd80
JS
213// EVT_LEFT_DCLICK ( wxNewBitmapButton::OnLButtonDClick )
214 EVT_LEFT_DCLICK ( wxNewBitmapButton::OnLButtonDown )
45da7759
JS
215 EVT_ENTER_WINDOW( wxNewBitmapButton::OnMouseEnter )
216 EVT_LEAVE_WINDOW( wxNewBitmapButton::OnMouseLeave )
8e08b761 217
5515f252
GT
218 EVT_SIZE ( wxNewBitmapButton::OnSize )
219 EVT_PAINT( wxNewBitmapButton::OnPaint )
8e08b761 220
5515f252 221 //EVT_KILL_FOCUS( wxNewBitmapButton::OnKillFocus )
8e08b761 222
5515f252 223 EVT_ERASE_BACKGROUND( wxNewBitmapButton::OnEraseBackground )
8e08b761 224
ab839dff
JS
225 EVT_IDLE(wxNewBitmapButton::OnIdle)
226
8e08b761
JS
227END_EVENT_TABLE()
228
ab839dff 229wxNewBitmapButton::wxNewBitmapButton( const wxBitmap& labelBitmap,
5515f252
GT
230 const wxString& labelText,
231 int alignText,
232 bool isFlat,
ab839dff 233 int firedEventType,
5515f252
GT
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
c82c42d4
WS
254 mDragStarted ( false ),
255 mIsPressed ( false ),
256 mIsInFocus ( false ),
257 mIsToggled ( false ),
258 mHasFocusedBmp( false ),
5515f252
GT
259 mFiredEventType( firedEventType ),
260
261 mBlackPen( wxColour( 0, 0, 0), 1, wxSOLID ),
e1c6c6ae
VS
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 ),
5515f252 265
c82c42d4
WS
266 mIsCreated( false ),
267 mSizeIsSet( false )
8e08b761
JS
268
269{
270}
271
272wxNewBitmapButton::wxNewBitmapButton( const wxString& bitmapFileName,
52b5ab7e
GD
273 const wxBitmapType bitmapFileType,
274 const wxString& labelText,
275 int alignText,
276 bool isFlat,
18c45cd6 277 int WXUNUSED(firedEventType),
196be0f1
JS
278 int WXUNUSED(marginX),
279 int WXUNUSED(marginY),
280 int WXUNUSED(textToLabelGap),
281 bool WXUNUSED(isSticky))
8e08b761 282
5515f252
GT
283 : mTextToLabelGap ( 2 ),
284 mMarginX( 2 ),
285 mMarginY( 2 ),
286 mTextAlignment( alignText ),
c82c42d4 287 mIsSticky( false ),
5515f252
GT
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
c82c42d4
WS
298 mDragStarted ( false ),
299 mIsPressed ( false ),
300 mIsInFocus ( false ),
301 mIsToggled ( false ),
302 mHasFocusedBmp( false ),
5515f252
GT
303 mFiredEventType( wxEVT_COMMAND_MENU_SELECTED ),
304
305 mBlackPen( wxColour( 0, 0, 0), 1, wxSOLID ),
e1c6c6ae
VS
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 ),
5515f252 309
c82c42d4
WS
310 mIsCreated( false ),
311 mSizeIsSet( false )
8e08b761
JS
312
313{
314}
315
18c45cd6 316wxNewBitmapButton::~wxNewBitmapButton(void)
8e08b761 317{
5515f252 318 DestroyLabels();
8e08b761
JS
319}
320
321void wxNewBitmapButton::DrawShade( int outerLevel,
5515f252
GT
322 wxDC& dc,
323 wxPen& upperLeftSidePen,
324 wxPen& lowerRightSidePen )
8e08b761 325{
5515f252 326 wxBitmap* pBmp = GetStateLabel();
8e08b761 327
ab839dff
JS
328 int x = mMarginX - (outerLevel + 2);
329 int y = mMarginY - (outerLevel + 2);
8e08b761 330
ab839dff
JS
331 int height = pBmp->GetHeight() + (outerLevel + 2)*2 - 1;
332 int width = pBmp->GetWidth() + (outerLevel + 2)*2 - 1;
8e08b761 333
5515f252
GT
334 dc.SetPen( upperLeftSidePen );
335 dc.DrawLine( x,y, x + width, y );
336 dc.DrawLine( x,y, x, y + height );
8e08b761 337
5515f252
GT
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 );
8e08b761
JS
341}
342
343void wxNewBitmapButton::DestroyLabels()
344{
5515f252
GT
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;
8e08b761
JS
354}
355
356wxBitmap* wxNewBitmapButton::GetStateLabel()
357{
5515f252
GT
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;
8e08b761
JS
380}
381
18c45cd6
WS
382#ifndef __WXMSW__
383
8e08b761 384static const unsigned char _gDisableImage[] = { 0x55,0xAA,0x55,0xAA,
5515f252
GT
385 0x55,0xAA,0x55,0xAA,
386 0x55,0xAA,0x55,0xAA,
387 0x55,0xAA,0x55,0xAA
388 };
18c45cd6
WS
389
390#endif
391
8e08b761 392void wxNewBitmapButton::RenderLabelImage( wxBitmap*& destBmp, wxBitmap* srcBmp,
5515f252 393 bool isEnabled, bool isPressed )
8e08b761 394{
5515f252
GT
395 if ( destBmp != 0 ) return;
396
4cbc57f0 397 // render labels on-demand
5515f252
GT
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
e1c6c6ae 415 srcDc.SetFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT) );
5515f252
GT
416 srcDc.GetTextExtent( mLabelText, &txtWidth, &txtHeight );
417
418 if ( mTextAlignment == NB_ALIGN_TEXT_RIGHT )
419 {
420 destDim.x = srcBmp->GetWidth() + 2*mTextToLabelGap + txtWidth;
421
ab839dff 422 destDim.y =
5515f252
GT
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 {
ab839dff 433 destDim.x =
5515f252
GT
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 }
ab839dff 443 else
5515f252 444 {
873a543b 445 wxFAIL_MSG(wxT("Unsupported FL alignment type detected in wxNewBitmapButton::RenderLabelImage()"));
5515f252
GT
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
e1c6c6ae 461 wxBrush grayBrush( wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE), wxSOLID );
5515f252 462
5515f252 463 destDc.SetBrush( grayBrush );
18c45cd6 464 destDc.SetPen( *wxTRANSPARENT_PEN );
5515f252 465 destDc.DrawRectangle( 0,0, destDim.x+1, destDim.y+1 );
8e08b761 466
5515f252
GT
467 if ( isPressed )
468 {
469 ++imgPos.x; ++imgPos.y;
470 ++txtPos.x; ++txtPos.y;
471 }
8e08b761 472
5515f252
GT
473 if ( hasImage )
474 {
8e08b761 475
ab839dff 476 destDc.Blit( imgPos.x, imgPos.y,
1480c61a
VZ
477 srcBmp->GetWidth(),
478 srcBmp->GetHeight(),
c82c42d4 479 &srcDc, 0,0, wxCOPY,true );
5515f252 480 }
8e08b761 481
5515f252
GT
482 if ( hasText )
483 {
484 wxWindow* pTopWnd = this;
8e08b761 485
5515f252
GT
486 do
487 {
488 wxWindow* pParent = pTopWnd->GetParent();
8e08b761 489
5515f252 490 if ( pParent == 0 )
8e08b761
JS
491 break;
492
5515f252
GT
493 pTopWnd = pParent;
494 } while (1);
8e08b761 495
e1c6c6ae 496 destDc.SetFont( wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT) );
8e08b761 497
5515f252 498 if ( isEnabled )
8e08b761 499 {
e1c6c6ae 500 destDc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT) );
5515f252 501 }
8e08b761
JS
502 else
503 {
e1c6c6ae 504 destDc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW) );
5515f252 505 }
e1c6c6ae 506 destDc.SetTextBackground( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE) );
8e08b761 507
5515f252
GT
508 destDc.DrawText( mLabelText, txtPos.x, txtPos.y );
509 }
8e08b761 510
5515f252 511 if ( !isEnabled ){
ab839dff 512
8e08b761 513#ifdef __WXMSW__ // This is currently MSW specific
5515f252 514 gray_out_image_on_dc( destDc, destDim.x, destDim.y );
8e08b761 515#else
5515f252 516 wxBrush checkerBrush( wxBitmap( (const char*)_gDisableImage,8,8) );
e1c6c6ae 517 checkerBrush.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
5515f252
GT
518 destDc.SetBrush( checkerBrush );
519 destDc.DrawRectangle( imgPos.x, imgPos.y, srcBmp->GetWidth()+1, srcBmp->GetHeight()+1);
8e08b761 520#endif
5515f252
GT
521 }
522 // adjust button size to fit the new dimensions of the label
523 if ( !mSizeIsSet && 0 )
524 {
c82c42d4 525 mSizeIsSet = true;
422d0ff0 526 SetSize( wxDefaultCoord, wxDefaultCoord,
5515f252 527 destBmp->GetWidth() + mMarginX*2,
ab839dff 528 destBmp->GetHeight() + mMarginY*2, 0
5515f252
GT
529 );
530 }
65e50848 531 destDc.SelectObject( wxNullBitmap );
ab839dff 532
45747ae3 533#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
65e50848 534 // Map to system colours
2b5f62a0 535 (void) wxToolBar::MapBitmap(destBmp->GetHBITMAP(), destBmp->GetWidth(), destBmp->GetHeight());
ab839dff 536#endif
8e08b761 537}
65e50848 538
8e08b761
JS
539void wxNewBitmapButton::RenderAllLabelImages()
540{
5515f252 541 if ( !mIsCreated )
8e08b761 542 return;
c82c42d4
WS
543 RenderLabelImage( mpDisabledImg, &mDepressedBmp, false );
544 RenderLabelImage( mpPressedImg, &mDepressedBmp, true, true );
545 RenderLabelImage( mpDepressedImg, &mDepressedBmp, true, false );
5515f252 546 if ( mHasFocusedBmp )
8e08b761 547 {
c82c42d4 548 RenderLabelImage( mpFocusedImg, &mFocusedBmp, true, false );
5515f252 549 }
8e08b761 550}
ab839dff 551
8e08b761
JS
552
553void wxNewBitmapButton::RenderLabelImages()
554{
5515f252 555 if ( !mIsCreated )
8e08b761
JS
556 return;
557
5515f252
GT
558 if ( !IsEnabled() )
559 {
c82c42d4 560 RenderLabelImage( mpDisabledImg, &mDepressedBmp, false );
5515f252
GT
561 }
562 else
563
ab839dff 564 if ( mIsPressed )
5515f252 565
c82c42d4 566 RenderLabelImage( mpPressedImg, &mDepressedBmp, true, true );
5515f252
GT
567 else
568 {
569 if ( mIsInFocus )
570 {
571 if ( mHasFocusedBmp )
c82c42d4 572 RenderLabelImage( mpFocusedImg, &mFocusedBmp, true, false );
5515f252 573 else
c82c42d4 574 RenderLabelImage( mpDepressedImg, &mDepressedBmp, true, false );
5515f252
GT
575 }
576 else
c82c42d4 577 RenderLabelImage( mpDepressedImg, &mDepressedBmp, true, false );
5515f252 578 }
8e08b761
JS
579}
580
ab839dff
JS
581bool wxNewBitmapButton::Toggle(bool enable)
582{
583 if ( mIsToggled == enable )
584 {
c82c42d4 585 return false;
ab839dff
JS
586 }
587
588 mIsToggled = enable;
589 Refresh();
590
c82c42d4 591 return true;
ab839dff
JS
592}
593
45da7759
JS
594bool wxNewBitmapButton::Enable(bool enable)
595{
596 if ( enable != m_isEnabled )
597 {
598 if ( mIsInFocus )
599 {
c82c42d4 600 mIsInFocus = false;
45da7759
JS
601 }
602
603 if ( mIsPressed )
604 {
c82c42d4 605 mIsPressed = false;
45da7759
JS
606 }
607
608 Refresh();
609 }
610
611 return wxPanel::Enable( enable );
612}
613
8e08b761
JS
614void wxNewBitmapButton::DrawDecorations( wxDC& dc )
615{
5515f252
GT
616 if ( mIsFlat )
617 {
618 DrawShade( 1, dc, mGrayPen, mGrayPen );
ab839dff
JS
619 if ( mIsToggled )
620 {
621 DrawShade( 0, dc, mDarkPen, mLightPen );
622 }
623 else if ( mIsInFocus )
5515f252
GT
624 {
625 if ( mIsPressed )
626 DrawShade( 0, dc, mDarkPen, mLightPen );
627 else
628 DrawShade( 0, dc, mLightPen, mDarkPen );
629 }
630 else
631 DrawShade( 0, dc, mGrayPen, mGrayPen );
632 }
633 else
634 {
ab839dff 635 if ( mIsPressed || mIsToggled )
5515f252
GT
636 {
637 DrawShade( 0, dc, mDarkPen, mGrayPen );
638 DrawShade( 1, dc, mBlackPen, mLightPen );
639 }
640 else
641 {
642 DrawShade( 0, dc, mGrayPen, mDarkPen );
643 DrawShade( 1, dc, mLightPen, mBlackPen );
644 }
645 }
8e08b761
JS
646}
647
ab839dff 648void wxNewBitmapButton::SetLabel(const wxBitmap& labelBitmap,
5515f252 649 const wxString& labelText )
8e08b761 650{
5515f252 651 DestroyLabels();
8e08b761 652
5515f252
GT
653 mLabelText = labelText;
654 mDepressedBmp = labelBitmap;
8e08b761 655
5515f252
GT
656 //RenderLabelImages();
657 RenderAllLabelImages();
8e08b761
JS
658}
659
660void wxNewBitmapButton::SetAlignments( int alignText,
5515f252
GT
661 int marginX,
662 int marginY,
663 int textToLabelGap)
8e08b761 664{
5515f252 665 DestroyLabels();
8e08b761 666
5515f252
GT
667 mMarginX = marginX;
668 mMarginY = marginY;
669 mTextAlignment = alignText;
670 mTextToLabelGap = textToLabelGap;
8e08b761 671
5515f252
GT
672 //RenderLabelImages();
673 RenderAllLabelImages();
8e08b761
JS
674}
675
676// event handlers
677
196be0f1 678void wxNewBitmapButton::OnLButtonDown( wxMouseEvent& WXUNUSED(event) )
8e08b761 679{
c82c42d4
WS
680 mDragStarted = true;
681 mIsPressed = true;
5515f252 682 Refresh();
8e08b761
JS
683}
684
685void wxNewBitmapButton::OnLButtonUp( wxMouseEvent& event )
686{
5515f252 687 if ( !mDragStarted )
8e08b761
JS
688 return;
689
c82c42d4
WS
690 mDragStarted = false;
691 mIsPressed = false;
5515f252 692 Refresh();
8e08b761 693
18c45cd6 694 if ( IsInWindow( event.m_x, event.m_y ) )
45da7759
JS
695 {
696 // fire event, if mouse was released
697 // within the bounds of button
698 wxCommandEvent cmd( mFiredEventType, GetId() );
699 GetParent()->ProcessEvent( cmd );
700 }
701}
8e08b761 702
8e08b761
JS
703bool wxNewBitmapButton::IsInWindow( int x, int y )
704{
5515f252
GT
705 int width, height;
706 GetSize( &width, &height );
8e08b761 707
5515f252
GT
708 return ( x >= 0 && y >= 0 &&
709 x < width &&
710 y < height );
8e08b761
JS
711}
712
196be0f1 713void wxNewBitmapButton::OnMouseEnter( wxMouseEvent& WXUNUSED(event) )
8e08b761 714{
45da7759 715 bool prevIsInFocus = mIsInFocus;
5515f252 716
45da7759
JS
717 if ( !mIsInFocus )
718 {
c82c42d4 719 mIsInFocus = true;
5515f252 720 }
45da7759 721 if ( prevIsInFocus != mIsInFocus )
5515f252 722 {
45da7759 723 Refresh();
5515f252 724 }
45da7759 725}
5515f252 726
196be0f1 727void wxNewBitmapButton::OnMouseLeave( wxMouseEvent& WXUNUSED(event) )
45da7759
JS
728{
729 bool prevIsInFocus = mIsInFocus;
730 bool prevIsPressed = mIsPressed;
731 if ( mIsInFocus )
5515f252 732 {
c82c42d4
WS
733 mIsInFocus = false;
734 mIsPressed = false;
5515f252 735 }
45da7759 736 if ( prevIsInFocus != mIsInFocus || prevIsPressed != mIsPressed )
8e08b761 737 {
5515f252
GT
738 Refresh();
739 }
8e08b761
JS
740}
741
196be0f1 742void wxNewBitmapButton::OnSize( wxSizeEvent& WXUNUSED(event) )
8e08b761 743{
5515f252 744 //Reshape();
8e08b761
JS
745}
746
747void wxNewBitmapButton::Reshape( )
18c45cd6 748{
5515f252 749 bool wasCreated = mIsCreated;
c82c42d4 750 mIsCreated = true;
8e08b761 751
5515f252
GT
752 if ( !wasCreated )
753 {
754 // in the case of loading button from stream, check if we
18c45cd6 755 // have non-empty image-file name, load if possible
8e08b761 756
18c45cd6 757 if (!mImageFileName.empty())
5515f252
GT
758 {
759 mDepressedBmp.LoadFile( mImageFileName, mImageFileType );
8e08b761 760
5515f252
GT
761 //wxMessageBox("Image Loaded!!!");
762 }
8e08b761 763
5515f252
GT
764 //RenderLabelImages();
765 RenderAllLabelImages();
8e08b761 766
5515f252 767 wxBitmap* pCurImg = GetStateLabel();
8e08b761 768
5515f252
GT
769 int w = pCurImg->GetWidth(),
770 h = pCurImg->GetHeight();
8e08b761 771
5515f252
GT
772 SetSize( 0,0, w + mMarginX*2, h + mMarginY*2 , 0 );
773 }
8e08b761
JS
774}
775
776void wxNewBitmapButton::DrawLabel( wxDC& dc )
777{
5515f252 778 wxBitmap* pCurBmp = GetStateLabel();
8e08b761 779
5515f252
GT
780 if ( pCurBmp == NULL )
781 {
782 wxSizeEvent evt;
783 OnSize( evt ); // fake it up!
8e08b761 784
5515f252
GT
785 //RenderLabelImages();
786 pCurBmp = GetStateLabel();
787 }
8e08b761 788
5515f252
GT
789 wxMemoryDC mdc;
790 mdc.SelectObject( *pCurBmp );
8e08b761 791
18c45cd6 792 dc.Blit( mMarginX, mMarginY,
5515f252
GT
793 pCurBmp->GetWidth(),
794 pCurBmp->GetHeight(),
18c45cd6 795 &mdc, 0,0, wxCOPY
5515f252 796 );
8e08b761 797
5515f252 798 mdc.SelectObject( wxNullBitmap );
8e08b761
JS
799}
800
196be0f1 801void wxNewBitmapButton::OnPaint( wxPaintEvent& WXUNUSED(event) )
8e08b761 802{
5515f252 803 wxPaintDC dc(this);
8e08b761 804
5515f252
GT
805 // first, make sure images for current state are prepared
806 //RenderLabelImages();
8e08b761 807
5515f252 808 DrawLabel( dc );
8e08b761 809
5515f252 810 DrawDecorations( dc );
8e08b761
JS
811}
812
196be0f1 813void wxNewBitmapButton::OnEraseBackground( wxEraseEvent& WXUNUSED(event) )
8e08b761 814{
5515f252 815 // do nothing
8e08b761
JS
816}
817
196be0f1 818void wxNewBitmapButton::OnKillFocus( wxFocusEvent& WXUNUSED(event) )
8e08b761 819{
5515f252 820 // useless
8e08b761 821
873a543b 822 wxMessageBox(wxT("kill-focus for button!"));
8e08b761
JS
823}
824
ab839dff
JS
825// ----------------------------------------------------------------------------
826// UI updates
827// ----------------------------------------------------------------------------
828
829void wxNewBitmapButton::OnIdle(wxIdleEvent& event)
830{
831 DoButtonUpdate();
832
833 event.Skip();
834}
835
836// Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
837void wxNewBitmapButton::DoButtonUpdate()
838{
839 wxUpdateUIEvent event(GetId());
840 event.SetEventObject(this);
841
842 if ( GetParent()->ProcessEvent(event) )
843 {
844 if ( event.GetSetEnabled() )
845 {
846 bool enabled = event.GetEnabled();
847 if ( enabled != IsEnabled() )
848 Enable( enabled );
849 }
850 if ( event.GetSetChecked() )
851 Toggle( event.GetChecked() );
852 }
853}