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