]> git.saurik.com Git - wxWidgets.git/blame - contrib/src/fl/newbmpbtn.cpp
added test for env var expansion
[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
54#define GET_RED(col) col & 0xFF
5515f252 55#define GET_GREEN(col) (col >> 8) & 0xFF
8e08b761
JS
56#define GET_BLUE(col) (col >> 16) & 0xFF
57
58#define MAKE_INT_COLOR(red,green,blue) ( (red) | \
59 ( ( (green) << 8 ) & 0xFF00 ) | \
5515f252 60 ( ( (blue) << 16) & 0xFF0000) \
8e08b761
JS
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 ) && \
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 )
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);
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 {
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;
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
JS
224
225END_EVENT_TABLE()
226
227wxNewBitmapButton::wxNewBitmapButton( const wxBitmap& labelBitmap,
5515f252
GT
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 ),
5515f252
GT
255 mHasFocusedBmp( FALSE ),
256 mFiredEventType( firedEventType ),
257
258 mBlackPen( wxColour( 0, 0, 0), 1, wxSOLID ),
e1c6c6ae
VS
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 ),
5515f252
GT
262
263 mIsCreated( FALSE ),
264 mSizeIsSet( FALSE )
8e08b761
JS
265
266{
267}
268
269wxNewBitmapButton::wxNewBitmapButton( const wxString& bitmapFileName,
52b5ab7e
GD
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)
8e08b761 279
5515f252
GT
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 ),
5515f252
GT
298 mHasFocusedBmp( FALSE ),
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
GT
305
306 mIsCreated( FALSE ),
307 mSizeIsSet( FALSE )
8e08b761
JS
308
309{
310}
311
312wxNewBitmapButton::~wxNewBitmapButton(void)
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
5515f252
GT
324 int x = mMarginX - (outerLevel + 1);
325 int y = mMarginY - (outerLevel + 1);
8e08b761 326
5515f252
GT
327 int height = pBmp->GetHeight() + (outerLevel + 1)*2 - 1;
328 int width = pBmp->GetWidth() + (outerLevel + 1)*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
378static const unsigned char _gDisableImage[] = { 0x55,0xAA,0x55,0xAA,
5515f252
GT
379 0x55,0xAA,0x55,0xAA,
380 0x55,0xAA,0x55,0xAA,
381 0x55,0xAA,0x55,0xAA
382 };
8e08b761 383void wxNewBitmapButton::RenderLabelImage( wxBitmap*& destBmp, wxBitmap* srcBmp,
5515f252 384 bool isEnabled, bool isPressed )
8e08b761 385{
5515f252
GT
386 if ( destBmp != 0 ) return;
387
4cbc57f0 388 // render labels on-demand
5515f252
GT
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
e1c6c6ae 406 srcDc.SetFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT) );
5515f252
GT
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
e1c6c6ae 452 wxBrush grayBrush( wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE), wxSOLID );
5515f252
GT
453 wxPen nullPen( wxColour(0,0,0), 1, wxTRANSPARENT );
454
5515f252
GT
455 destDc.SetBrush( grayBrush );
456 destDc.SetPen( nullPen );
8e08b761 457
5515f252 458 destDc.DrawRectangle( 0,0, destDim.x+1, destDim.y+1 );
8e08b761 459
5515f252
GT
460 if ( isPressed )
461 {
462 ++imgPos.x; ++imgPos.y;
463 ++txtPos.x; ++txtPos.y;
464 }
8e08b761 465
5515f252
GT
466 if ( hasImage )
467 {
8e08b761 468
5515f252
GT
469 destDc.Blit( imgPos.x, imgPos.y,
470 srcBmp->GetWidth()+1,
471 srcBmp->GetHeight()+1,
472 &srcDc, 0,0, wxCOPY,TRUE );
473 }
8e08b761 474
5515f252
GT
475 if ( hasText )
476 {
477 wxWindow* pTopWnd = this;
8e08b761 478
5515f252
GT
479 do
480 {
481 wxWindow* pParent = pTopWnd->GetParent();
8e08b761 482
5515f252 483 if ( pParent == 0 )
8e08b761
JS
484 break;
485
5515f252
GT
486 pTopWnd = pParent;
487 } while (1);
8e08b761 488
e1c6c6ae 489 destDc.SetFont( wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT) );
8e08b761 490
5515f252 491 if ( isEnabled )
8e08b761 492 {
e1c6c6ae 493 destDc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT) );
5515f252 494 }
8e08b761
JS
495 else
496 {
e1c6c6ae 497 destDc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW) );
5515f252 498 }
e1c6c6ae 499 destDc.SetTextBackground( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE) );
8e08b761 500
5515f252
GT
501 destDc.DrawText( mLabelText, txtPos.x, txtPos.y );
502 }
8e08b761 503
5515f252
GT
504 if ( !isEnabled ){
505
8e08b761 506#ifdef __WXMSW__ // This is currently MSW specific
5515f252 507 gray_out_image_on_dc( destDc, destDim.x, destDim.y );
8e08b761 508#else
5515f252 509 wxBrush checkerBrush( wxBitmap( (const char*)_gDisableImage,8,8) );
e1c6c6ae 510 checkerBrush.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
5515f252
GT
511 destDc.SetBrush( checkerBrush );
512 destDc.DrawRectangle( imgPos.x, imgPos.y, srcBmp->GetWidth()+1, srcBmp->GetHeight()+1);
8e08b761 513#endif
5515f252
GT
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 }
65e50848
JS
524 destDc.SelectObject( wxNullBitmap );
525
526#ifdef __WXMSW__
527 // Map to system colours
528 (void) MapBitmap(destBmp->GetHBITMAP(), destBmp->GetWidth(), destBmp->GetHeight());
529#endif
8e08b761 530}
65e50848 531
8e08b761
JS
532void wxNewBitmapButton::RenderAllLabelImages()
533{
5515f252 534 if ( !mIsCreated )
8e08b761 535 return;
5515f252
GT
536 RenderLabelImage( mpDisabledImg, &mDepressedBmp, FALSE );
537 RenderLabelImage( mpPressedImg, &mDepressedBmp, TRUE, TRUE );
538 RenderLabelImage( mpDepressedImg, &mDepressedBmp, TRUE, FALSE );
539 if ( mHasFocusedBmp )
8e08b761 540 {
5515f252
GT
541 RenderLabelImage( mpFocusedImg, &mFocusedBmp, TRUE, FALSE );
542 }
8e08b761 543}
5515f252 544
8e08b761
JS
545
546void wxNewBitmapButton::RenderLabelImages()
547{
5515f252 548 if ( !mIsCreated )
8e08b761
JS
549 return;
550
5515f252
GT
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 }
8e08b761
JS
572}
573
45da7759
JS
574bool 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
8e08b761
JS
594void wxNewBitmapButton::DrawDecorations( wxDC& dc )
595{
5515f252
GT
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 }
8e08b761
JS
623}
624
625void wxNewBitmapButton::SetLabel(const wxBitmap& labelBitmap,
5515f252 626 const wxString& labelText )
8e08b761 627{
5515f252 628 DestroyLabels();
8e08b761 629
5515f252
GT
630 mLabelText = labelText;
631 mDepressedBmp = labelBitmap;
8e08b761 632
5515f252
GT
633 //RenderLabelImages();
634 RenderAllLabelImages();
8e08b761
JS
635}
636
637void wxNewBitmapButton::SetAlignments( int alignText,
5515f252
GT
638 int marginX,
639 int marginY,
640 int textToLabelGap)
8e08b761 641{
5515f252 642 DestroyLabels();
8e08b761 643
5515f252
GT
644 mMarginX = marginX;
645 mMarginY = marginY;
646 mTextAlignment = alignText;
647 mTextToLabelGap = textToLabelGap;
8e08b761 648
5515f252
GT
649 //RenderLabelImages();
650 RenderAllLabelImages();
8e08b761
JS
651}
652
653// event handlers
654
655void wxNewBitmapButton::OnLButtonDown( wxMouseEvent& event )
656{
5515f252
GT
657 mDragStarted = TRUE;
658 mIsPressed = TRUE;
659 Refresh();
8e08b761
JS
660}
661
662void wxNewBitmapButton::OnLButtonUp( wxMouseEvent& event )
663{
5515f252 664 if ( !mDragStarted )
8e08b761
JS
665 return;
666
5515f252
GT
667 mDragStarted = FALSE;
668 mIsPressed = FALSE;
5515f252 669 Refresh();
8e08b761 670
45da7759
JS
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}
8e08b761 679
8e08b761
JS
680bool wxNewBitmapButton::IsInWindow( int x, int y )
681{
5515f252
GT
682 int width, height;
683 GetSize( &width, &height );
8e08b761 684
5515f252
GT
685 return ( x >= 0 && y >= 0 &&
686 x < width &&
687 y < height );
8e08b761
JS
688}
689
45da7759 690void wxNewBitmapButton::OnMouseEnter( wxMouseEvent& event )
8e08b761 691{
45da7759 692 bool prevIsInFocus = mIsInFocus;
5515f252 693
45da7759
JS
694 if ( !mIsInFocus )
695 {
5515f252
GT
696 mIsInFocus = TRUE;
697 }
45da7759 698 if ( prevIsInFocus != mIsInFocus )
5515f252 699 {
45da7759 700 Refresh();
5515f252 701 }
45da7759 702}
5515f252 703
45da7759
JS
704void wxNewBitmapButton::OnMouseLeave( wxMouseEvent& event )
705{
706 bool prevIsInFocus = mIsInFocus;
707 bool prevIsPressed = mIsPressed;
708 if ( mIsInFocus )
5515f252 709 {
45da7759
JS
710 mIsInFocus = FALSE;
711 mIsPressed = FALSE;
5515f252 712 }
45da7759 713 if ( prevIsInFocus != mIsInFocus || prevIsPressed != mIsPressed )
8e08b761 714 {
5515f252
GT
715 Refresh();
716 }
8e08b761
JS
717}
718
719void wxNewBitmapButton::OnSize( wxSizeEvent& event )
720{
5515f252 721 //Reshape();
8e08b761
JS
722}
723
724void wxNewBitmapButton::Reshape( )
4cbc57f0 725{
5515f252
GT
726 bool wasCreated = mIsCreated;
727 mIsCreated = TRUE;
8e08b761 728
5515f252
GT
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
8e08b761 733
5515f252
GT
734 if ( mImageFileName != "" )
735 {
736 mDepressedBmp.LoadFile( mImageFileName, mImageFileType );
8e08b761 737
5515f252
GT
738 //wxMessageBox("Image Loaded!!!");
739 }
8e08b761 740
5515f252
GT
741 //RenderLabelImages();
742 RenderAllLabelImages();
8e08b761 743
5515f252 744 wxBitmap* pCurImg = GetStateLabel();
8e08b761 745
5515f252
GT
746 int w = pCurImg->GetWidth(),
747 h = pCurImg->GetHeight();
8e08b761 748
5515f252
GT
749 SetSize( 0,0, w + mMarginX*2, h + mMarginY*2 , 0 );
750 }
8e08b761
JS
751}
752
753void wxNewBitmapButton::DrawLabel( wxDC& dc )
754{
5515f252 755 wxBitmap* pCurBmp = GetStateLabel();
8e08b761 756
5515f252
GT
757 if ( pCurBmp == NULL )
758 {
759 wxSizeEvent evt;
760 OnSize( evt ); // fake it up!
8e08b761 761
5515f252
GT
762 //RenderLabelImages();
763 pCurBmp = GetStateLabel();
764 }
8e08b761 765
5515f252
GT
766 wxMemoryDC mdc;
767 mdc.SelectObject( *pCurBmp );
8e08b761 768
5515f252
GT
769 dc.Blit( mMarginX, mMarginY,
770 pCurBmp->GetWidth(),
771 pCurBmp->GetHeight(),
772 &mdc, 0,0, wxCOPY
773 );
8e08b761 774
5515f252 775 mdc.SelectObject( wxNullBitmap );
8e08b761
JS
776}
777
778void wxNewBitmapButton::OnPaint( wxPaintEvent& event )
779{
5515f252 780 wxPaintDC dc(this);
8e08b761 781
5515f252
GT
782 // first, make sure images for current state are prepared
783 //RenderLabelImages();
8e08b761 784
5515f252 785 DrawLabel( dc );
8e08b761 786
5515f252 787 DrawDecorations( dc );
8e08b761
JS
788}
789
790void wxNewBitmapButton::OnEraseBackground( wxEraseEvent& event )
791{
5515f252 792 // do nothing
8e08b761
JS
793}
794
795void wxNewBitmapButton::OnKillFocus( wxFocusEvent& event )
796{
5515f252 797 // useless
8e08b761 798
5515f252 799 wxMessageBox("kill-focus for button!");
8e08b761
JS
800}
801
65e50848
JS
802#ifdef __WXMSW__
803WXHBITMAP wxNewBitmapButton::MapBitmap(WXHBITMAP bitmap, int width, int height)
804{
805 MemoryHDC hdcMem;
806
807 if ( !hdcMem )
808 {
809 wxLogLastError(_T("CreateCompatibleDC"));
810
811 return bitmap;
812 }
813
814 SelectInHDC bmpInHDC(hdcMem, (HBITMAP)bitmap);
815
816 if ( !bmpInHDC )
817 {
818 wxLogLastError(_T("SelectObject"));
819
820 return bitmap;
821 }
822
823 wxCOLORMAP *cmap = wxGetStdColourMap();
824
825 for ( int i = 0; i < width; i++ )
826 {
827 for ( int j = 0; j < height; j++ )
828 {
829 COLORREF pixel = ::GetPixel(hdcMem, i, j);
830
831 for ( size_t k = 0; k < wxSTD_COL_MAX; k++ )
832 {
833 COLORREF col = cmap[k].from;
834 if ( abs(GetRValue(pixel) - GetRValue(col)) < 10 &&
835 abs(GetGValue(pixel) - GetGValue(col)) < 10 &&
836 abs(GetBValue(pixel) - GetBValue(col)) < 10 )
837 {
838 ::SetPixel(hdcMem, i, j, cmap[k].to);
839 break;
840 }
841 }
842 }
843 }
844
845 return bitmap;
846}
847#endif