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