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