]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: newbmpbtn.cpp | |
3 | // Purpose: wxNewBitmapButton enhanced bitmap button class. | |
4 | // Author: Aleksandras Gluchovas | |
5 | // Modified by: | |
6 | // Created: ??/09/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Aleksandras Gluchovas | |
9 | // Licence: wxWindows licence | |
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 | ||
32 | static int* create_array( int width, int height, int fill = 0 ) | |
33 | { | |
34 | int* array = new int[width*height]; | |
35 | ||
36 | int len = width*height; | |
37 | int i; | |
38 | for ( i = 0; i != len; ++i ) | |
39 | array[i] = fill; | |
40 | ||
41 | return array; | |
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 | |
51 | #define GET_GREEN(col) (col >> 8) & 0xFF | |
52 | #define GET_BLUE(col) (col >> 16) & 0xFF | |
53 | ||
54 | #define MAKE_INT_COLOR(red,green,blue) ( (red) | \ | |
55 | ( ( (green) << 8 ) & 0xFF00 ) | \ | |
56 | ( ( (blue) << 16) & 0xFF0000) \ | |
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 ) && \ | |
61 | ( (GET_BLUE(col1) ) > (GET_BLUE(col2) ) + MIN_COLOR_DIFF ) \ | |
62 | ) | |
63 | ||
64 | #define MASK_BG 0 | |
65 | #define MASK_DARK 1 | |
66 | #define MASK_LIGHT 2 | |
67 | ||
68 | // helper function, used internally | |
69 | ||
70 | static void gray_out_pixmap( int* src, int* dest, int width, int height ) | |
71 | { | |
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); | |
143 | } | |
144 | ||
145 | // algorithm for making the image look "grayed" (e.g. disabled button) | |
146 | // NOTE:: used GetPixel(), which is Windows-Only! | |
147 | ||
148 | void gray_out_image_on_dc( wxDC& dc, int width, int height ) | |
149 | { | |
150 | // assuming the pixels along the edges are of the background color | |
151 | wxColour bgCol; | |
152 | dc.GetPixel( 0, 0, &bgCol ); | |
153 | ||
154 | wxPen darkPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW),1, wxSOLID ); | |
155 | wxPen lightPen( wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT),1, wxSOLID ); | |
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; | |
197 | } | |
198 | ||
199 | /////////////////////////////// | |
200 | ||
201 | /***** Implementation for class wxNewBitmapButton *****/ | |
202 | ||
203 | IMPLEMENT_DYNAMIC_CLASS(wxNewBitmapButton, wxPanel) | |
204 | ||
205 | BEGIN_EVENT_TABLE( wxNewBitmapButton, wxPanel ) | |
206 | ||
207 | EVT_LEFT_DOWN( wxNewBitmapButton::OnLButtonDown ) | |
208 | EVT_LEFT_UP ( wxNewBitmapButton::OnLButtonUp ) | |
209 | EVT_MOTION ( wxNewBitmapButton::OnMouseMove ) | |
210 | ||
211 | EVT_SIZE ( wxNewBitmapButton::OnSize ) | |
212 | EVT_PAINT( wxNewBitmapButton::OnPaint ) | |
213 | ||
214 | //EVT_KILL_FOCUS( wxNewBitmapButton::OnKillFocus ) | |
215 | ||
216 | EVT_ERASE_BACKGROUND( wxNewBitmapButton::OnEraseBackground ) | |
217 | ||
218 | END_EVENT_TABLE() | |
219 | ||
220 | wxNewBitmapButton::wxNewBitmapButton( const wxBitmap& labelBitmap, | |
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 ), | |
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 ), | |
257 | ||
258 | mIsCreated( FALSE ), | |
259 | mSizeIsSet( FALSE ) | |
260 | ||
261 | { | |
262 | } | |
263 | ||
264 | wxNewBitmapButton::wxNewBitmapButton( const wxString& bitmapFileName, | |
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) | |
274 | ||
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 ), | |
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 ), | |
302 | ||
303 | mIsCreated( FALSE ), | |
304 | mSizeIsSet( FALSE ) | |
305 | ||
306 | { | |
307 | } | |
308 | ||
309 | wxNewBitmapButton::~wxNewBitmapButton(void) | |
310 | { | |
311 | DestroyLabels(); | |
312 | } | |
313 | ||
314 | void wxNewBitmapButton::DrawShade( int outerLevel, | |
315 | wxDC& dc, | |
316 | wxPen& upperLeftSidePen, | |
317 | wxPen& lowerRightSidePen ) | |
318 | { | |
319 | wxBitmap* pBmp = GetStateLabel(); | |
320 | ||
321 | int x = mMarginX - (outerLevel + 1); | |
322 | int y = mMarginY - (outerLevel + 1); | |
323 | ||
324 | int height = pBmp->GetHeight() + (outerLevel + 1)*2 - 1; | |
325 | int width = pBmp->GetWidth() + (outerLevel + 1)*2 - 1; | |
326 | ||
327 | dc.SetPen( upperLeftSidePen ); | |
328 | dc.DrawLine( x,y, x + width, y ); | |
329 | dc.DrawLine( x,y, x, y + height ); | |
330 | ||
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 ); | |
334 | } | |
335 | ||
336 | void wxNewBitmapButton::DestroyLabels() | |
337 | { | |
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; | |
347 | } | |
348 | ||
349 | wxBitmap* wxNewBitmapButton::GetStateLabel() | |
350 | { | |
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; | |
373 | } | |
374 | ||
375 | static const unsigned char _gDisableImage[] = { 0x55,0xAA,0x55,0xAA, | |
376 | 0x55,0xAA,0x55,0xAA, | |
377 | 0x55,0xAA,0x55,0xAA, | |
378 | 0x55,0xAA,0x55,0xAA | |
379 | }; | |
380 | void wxNewBitmapButton::RenderLabelImage( wxBitmap*& destBmp, wxBitmap* srcBmp, | |
381 | bool isEnabled, bool isPressed ) | |
382 | { | |
383 | if ( destBmp != 0 ) return; | |
384 | ||
385 | // render labels on-demand | |
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 | ||
403 | srcDc.SetFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT) ); | |
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 | ||
449 | wxBrush grayBrush( wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE), wxSOLID ); | |
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 ) | |
481 | break; | |
482 | ||
483 | pTopWnd = pParent; | |
484 | } while (1); | |
485 | ||
486 | destDc.SetFont( wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT) ); | |
487 | ||
488 | if ( isEnabled ) | |
489 | { | |
490 | destDc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT) ); | |
491 | } | |
492 | else | |
493 | { | |
494 | destDc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW) ); | |
495 | } | |
496 | destDc.SetTextBackground( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE) ); | |
497 | ||
498 | destDc.DrawText( mLabelText, txtPos.x, txtPos.y ); | |
499 | } | |
500 | ||
501 | ||
502 | destDc.SetBrush( grayBrush ); | |
503 | destDc.SetPen( nullPen ); | |
504 | ||
505 | destDc.DrawRectangle( 0,0, destDim.x+1, destDim.y+1 ); | |
506 | ||
507 | if ( isPressed ) | |
508 | { | |
509 | ++imgPos.x; ++imgPos.y; | |
510 | ++txtPos.x; ++txtPos.y; | |
511 | } | |
512 | ||
513 | if ( hasImage ) | |
514 | { | |
515 | ||
516 | destDc.Blit( imgPos.x, imgPos.y, | |
517 | srcBmp->GetWidth()+1, | |
518 | srcBmp->GetHeight()+1, | |
519 | &srcDc, 0,0, wxCOPY,TRUE ); | |
520 | } | |
521 | ||
522 | if ( hasText ) | |
523 | { | |
524 | wxWindow* pTopWnd = this; | |
525 | ||
526 | do | |
527 | { | |
528 | wxWindow* pParent = pTopWnd->GetParent(); | |
529 | ||
530 | if ( pParent == 0 ) | |
531 | break; | |
532 | ||
533 | pTopWnd = pParent; | |
534 | } while (1); | |
535 | ||
536 | destDc.SetFont( wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT) ); | |
537 | ||
538 | if ( isEnabled ) | |
539 | { | |
540 | destDc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT) ); | |
541 | } | |
542 | else | |
543 | { | |
544 | destDc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW) ); | |
545 | } | |
546 | destDc.SetTextBackground( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE) ); | |
547 | ||
548 | destDc.DrawText( mLabelText, txtPos.x, txtPos.y ); | |
549 | } | |
550 | ||
551 | if ( !isEnabled ){ | |
552 | ||
553 | #ifdef __WXMSW__ // This is currently MSW specific | |
554 | gray_out_image_on_dc( destDc, destDim.x, destDim.y ); | |
555 | #else | |
556 | wxBrush checkerBrush( wxBitmap( (const char*)_gDisableImage,8,8) ); | |
557 | checkerBrush.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) ); | |
558 | destDc.SetBrush( checkerBrush ); | |
559 | destDc.DrawRectangle( imgPos.x, imgPos.y, srcBmp->GetWidth()+1, srcBmp->GetHeight()+1); | |
560 | #endif | |
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 | } | |
571 | } | |
572 | void wxNewBitmapButton::RenderAllLabelImages() | |
573 | { | |
574 | if ( !mIsCreated ) | |
575 | return; | |
576 | RenderLabelImage( mpDisabledImg, &mDepressedBmp, FALSE ); | |
577 | RenderLabelImage( mpPressedImg, &mDepressedBmp, TRUE, TRUE ); | |
578 | RenderLabelImage( mpDepressedImg, &mDepressedBmp, TRUE, FALSE ); | |
579 | if ( mHasFocusedBmp ) | |
580 | { | |
581 | RenderLabelImage( mpFocusedImg, &mFocusedBmp, TRUE, FALSE ); | |
582 | } | |
583 | } | |
584 | ||
585 | ||
586 | void wxNewBitmapButton::RenderLabelImages() | |
587 | { | |
588 | if ( !mIsCreated ) | |
589 | return; | |
590 | ||
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 | } | |
612 | } | |
613 | ||
614 | void wxNewBitmapButton::DrawDecorations( wxDC& dc ) | |
615 | { | |
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 | } | |
643 | } | |
644 | ||
645 | void wxNewBitmapButton::SetLabel(const wxBitmap& labelBitmap, | |
646 | const wxString& labelText ) | |
647 | { | |
648 | DestroyLabels(); | |
649 | ||
650 | mLabelText = labelText; | |
651 | mDepressedBmp = labelBitmap; | |
652 | ||
653 | //RenderLabelImages(); | |
654 | RenderAllLabelImages(); | |
655 | } | |
656 | ||
657 | void wxNewBitmapButton::SetAlignments( int alignText, | |
658 | int marginX, | |
659 | int marginY, | |
660 | int textToLabelGap) | |
661 | { | |
662 | DestroyLabels(); | |
663 | ||
664 | mMarginX = marginX; | |
665 | mMarginY = marginY; | |
666 | mTextAlignment = alignText; | |
667 | mTextToLabelGap = textToLabelGap; | |
668 | ||
669 | //RenderLabelImages(); | |
670 | RenderAllLabelImages(); | |
671 | } | |
672 | ||
673 | // event handlers | |
674 | ||
675 | void wxNewBitmapButton::OnLButtonDown( wxMouseEvent& event ) | |
676 | { | |
677 | mPrevPressedState = FALSE; | |
678 | mDragStarted = TRUE; | |
679 | mIsPressed = TRUE; | |
680 | Refresh(); | |
681 | ||
682 | if ( !mIsInFocus ) | |
683 | CaptureMouse(); | |
684 | } | |
685 | ||
686 | void wxNewBitmapButton::OnLButtonUp( wxMouseEvent& event ) | |
687 | { | |
688 | if ( !mDragStarted ) | |
689 | return; | |
690 | ||
691 | mDragStarted = FALSE; | |
692 | mIsPressed = FALSE; | |
693 | mIsInFocus = FALSE; | |
694 | Refresh(); | |
695 | ||
696 | ReleaseMouse(); | |
697 | ||
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 | } | |
705 | } | |
706 | ||
707 | bool wxNewBitmapButton::IsInWindow( int x, int y ) | |
708 | { | |
709 | int width, height; | |
710 | GetSize( &width, &height ); | |
711 | ||
712 | return ( x >= 0 && y >= 0 && | |
713 | x < width && | |
714 | y < height ); | |
715 | } | |
716 | ||
717 | void wxNewBitmapButton::OnMouseMove( wxMouseEvent& event ) | |
718 | { | |
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) || | |
747 | (mIsInFocus!=mPrevInFocusState)) | |
748 | { | |
749 | Refresh(); | |
750 | } | |
751 | } | |
752 | ||
753 | void wxNewBitmapButton::OnSize( wxSizeEvent& event ) | |
754 | { | |
755 | //Reshape(); | |
756 | } | |
757 | ||
758 | void wxNewBitmapButton::Reshape( ) | |
759 | { | |
760 | bool wasCreated = mIsCreated; | |
761 | mIsCreated = TRUE; | |
762 | ||
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 | |
767 | ||
768 | if ( mImageFileName != "" ) | |
769 | { | |
770 | mDepressedBmp.LoadFile( mImageFileName, mImageFileType ); | |
771 | ||
772 | //wxMessageBox("Image Loaded!!!"); | |
773 | } | |
774 | ||
775 | //RenderLabelImages(); | |
776 | RenderAllLabelImages(); | |
777 | ||
778 | wxBitmap* pCurImg = GetStateLabel(); | |
779 | ||
780 | int w = pCurImg->GetWidth(), | |
781 | h = pCurImg->GetHeight(); | |
782 | ||
783 | SetSize( 0,0, w + mMarginX*2, h + mMarginY*2 , 0 ); | |
784 | } | |
785 | } | |
786 | ||
787 | void wxNewBitmapButton::DrawLabel( wxDC& dc ) | |
788 | { | |
789 | wxBitmap* pCurBmp = GetStateLabel(); | |
790 | ||
791 | if ( pCurBmp == NULL ) | |
792 | { | |
793 | wxSizeEvent evt; | |
794 | OnSize( evt ); // fake it up! | |
795 | ||
796 | //RenderLabelImages(); | |
797 | pCurBmp = GetStateLabel(); | |
798 | } | |
799 | ||
800 | wxMemoryDC mdc; | |
801 | mdc.SelectObject( *pCurBmp ); | |
802 | ||
803 | dc.Blit( mMarginX, mMarginY, | |
804 | pCurBmp->GetWidth(), | |
805 | pCurBmp->GetHeight(), | |
806 | &mdc, 0,0, wxCOPY | |
807 | ); | |
808 | ||
809 | mdc.SelectObject( wxNullBitmap ); | |
810 | } | |
811 | ||
812 | void wxNewBitmapButton::OnPaint( wxPaintEvent& event ) | |
813 | { | |
814 | wxPaintDC dc(this); | |
815 | ||
816 | // first, make sure images for current state are prepared | |
817 | //RenderLabelImages(); | |
818 | ||
819 | DrawLabel( dc ); | |
820 | ||
821 | DrawDecorations( dc ); | |
822 | } | |
823 | ||
824 | void wxNewBitmapButton::OnEraseBackground( wxEraseEvent& event ) | |
825 | { | |
826 | // do nothing | |
827 | } | |
828 | ||
829 | void wxNewBitmapButton::OnKillFocus( wxFocusEvent& event ) | |
830 | { | |
831 | // useless | |
832 | ||
833 | wxMessageBox("kill-focus for button!"); | |
834 | } | |
835 |