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