]>
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 | #ifdef __WXMSW__ | |
31 | #include "wx/msw/private.h" | |
32 | #endif | |
33 | ||
34 | ///////////// button-label rendering helpers ////////////////// | |
35 | ||
36 | static int* create_array( int width, int height, int fill = 0 ) | |
37 | { | |
38 | int* array = new int[width*height]; | |
39 | ||
40 | int len = width*height; | |
41 | int i; | |
42 | for ( i = 0; i != len; ++i ) | |
43 | array[i] = fill; | |
44 | ||
45 | return array; | |
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 | |
55 | #define GET_GREEN(col) (col >> 8) & 0xFF | |
56 | #define GET_BLUE(col) (col >> 16) & 0xFF | |
57 | ||
58 | #define MAKE_INT_COLOR(red,green,blue) ( (red) | \ | |
59 | ( ( (green) << 8 ) & 0xFF00 ) | \ | |
60 | ( ( (blue) << 16) & 0xFF0000) \ | |
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 ) && \ | |
65 | ( (GET_BLUE(col1) ) > (GET_BLUE(col2) ) + MIN_COLOR_DIFF ) \ | |
66 | ) | |
67 | ||
68 | #define MASK_BG 0 | |
69 | #define MASK_DARK 1 | |
70 | #define MASK_LIGHT 2 | |
71 | ||
72 | // helper function, used internally | |
73 | ||
74 | static void gray_out_pixmap( int* src, int* dest, int width, int height ) | |
75 | { | |
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); | |
147 | } | |
148 | ||
149 | // algorithm for making the image look "grayed" (e.g. disabled button) | |
150 | // NOTE:: used GetPixel(), which is Windows-Only! | |
151 | ||
152 | void gray_out_image_on_dc( wxDC& dc, int width, int height ) | |
153 | { | |
154 | // assuming the pixels along the edges are of the background color | |
155 | wxColour bgCol; | |
156 | dc.GetPixel( 0, 0, &bgCol ); | |
157 | ||
158 | wxPen darkPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW),1, wxSOLID ); | |
159 | wxPen lightPen( wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT),1, wxSOLID ); | |
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; | |
201 | } | |
202 | ||
203 | /////////////////////////////// | |
204 | ||
205 | /***** Implementation for class wxNewBitmapButton *****/ | |
206 | ||
207 | IMPLEMENT_DYNAMIC_CLASS(wxNewBitmapButton, wxPanel) | |
208 | ||
209 | BEGIN_EVENT_TABLE( wxNewBitmapButton, wxPanel ) | |
210 | ||
211 | EVT_LEFT_DOWN ( wxNewBitmapButton::OnLButtonDown ) | |
212 | EVT_LEFT_UP ( wxNewBitmapButton::OnLButtonUp ) | |
213 | // EVT_LEFT_DCLICK ( wxNewBitmapButton::OnLButtonDClick ) | |
214 | EVT_LEFT_DCLICK ( wxNewBitmapButton::OnLButtonDown ) | |
215 | EVT_ENTER_WINDOW( wxNewBitmapButton::OnMouseEnter ) | |
216 | EVT_LEAVE_WINDOW( wxNewBitmapButton::OnMouseLeave ) | |
217 | ||
218 | EVT_SIZE ( wxNewBitmapButton::OnSize ) | |
219 | EVT_PAINT( wxNewBitmapButton::OnPaint ) | |
220 | ||
221 | //EVT_KILL_FOCUS( wxNewBitmapButton::OnKillFocus ) | |
222 | ||
223 | EVT_ERASE_BACKGROUND( wxNewBitmapButton::OnEraseBackground ) | |
224 | ||
225 | EVT_IDLE(wxNewBitmapButton::OnIdle) | |
226 | ||
227 | END_EVENT_TABLE() | |
228 | ||
229 | wxNewBitmapButton::wxNewBitmapButton( const wxBitmap& labelBitmap, | |
230 | const wxString& labelText, | |
231 | int alignText, | |
232 | bool isFlat, | |
233 | int firedEventType, | |
234 | int marginX, | |
235 | int marginY, | |
236 | int textToLabelGap, | |
237 | bool isSticky) | |
238 | : mTextToLabelGap ( textToLabelGap ), | |
239 | mMarginX( marginX ), | |
240 | mMarginY( marginY ), | |
241 | mTextAlignment( alignText ), | |
242 | mIsSticky( isSticky ), | |
243 | mIsFlat( isFlat ), | |
244 | mLabelText( labelText ), | |
245 | mImageFileType( wxBITMAP_TYPE_INVALID ), | |
246 | mDepressedBmp( labelBitmap ), | |
247 | ||
248 | mpDepressedImg( NULL ), | |
249 | mpPressedImg ( NULL ), | |
250 | mpDisabledImg ( NULL ), | |
251 | mpFocusedImg ( NULL ), | |
252 | ||
253 | ||
254 | mDragStarted ( false ), | |
255 | mIsPressed ( false ), | |
256 | mIsInFocus ( false ), | |
257 | mIsToggled ( false ), | |
258 | mHasFocusedBmp( false ), | |
259 | mFiredEventType( firedEventType ), | |
260 | ||
261 | mBlackPen( wxColour( 0, 0, 0), 1, wxSOLID ), | |
262 | mDarkPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxSOLID ), | |
263 | mGrayPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE), 1, wxSOLID ), | |
264 | mLightPen( wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT), 1, wxSOLID ), | |
265 | ||
266 | mIsCreated( false ), | |
267 | mSizeIsSet( false ) | |
268 | ||
269 | { | |
270 | } | |
271 | ||
272 | wxNewBitmapButton::wxNewBitmapButton( const wxString& bitmapFileName, | |
273 | const wxBitmapType bitmapFileType, | |
274 | const wxString& labelText, | |
275 | int alignText, | |
276 | bool isFlat, | |
277 | int WXUNUSED(firedEventType), | |
278 | int WXUNUSED(marginX), | |
279 | int WXUNUSED(marginY), | |
280 | int WXUNUSED(textToLabelGap), | |
281 | bool WXUNUSED(isSticky)) | |
282 | ||
283 | : mTextToLabelGap ( 2 ), | |
284 | mMarginX( 2 ), | |
285 | mMarginY( 2 ), | |
286 | mTextAlignment( alignText ), | |
287 | mIsSticky( false ), | |
288 | mIsFlat( isFlat ), | |
289 | mLabelText( labelText ), | |
290 | mImageFileName( bitmapFileName ), | |
291 | mImageFileType( bitmapFileType ), | |
292 | ||
293 | mpDepressedImg( NULL ), | |
294 | mpPressedImg ( NULL ), | |
295 | mpDisabledImg ( NULL ), | |
296 | mpFocusedImg ( NULL ), | |
297 | ||
298 | mDragStarted ( false ), | |
299 | mIsPressed ( false ), | |
300 | mIsInFocus ( false ), | |
301 | mIsToggled ( false ), | |
302 | mHasFocusedBmp( false ), | |
303 | mFiredEventType( wxEVT_COMMAND_MENU_SELECTED ), | |
304 | ||
305 | mBlackPen( wxColour( 0, 0, 0), 1, wxSOLID ), | |
306 | mDarkPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxSOLID ), | |
307 | mGrayPen ( wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE), 1, wxSOLID ), | |
308 | mLightPen( wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT), 1, wxSOLID ), | |
309 | ||
310 | mIsCreated( false ), | |
311 | mSizeIsSet( false ) | |
312 | ||
313 | { | |
314 | } | |
315 | ||
316 | wxNewBitmapButton::~wxNewBitmapButton(void) | |
317 | { | |
318 | DestroyLabels(); | |
319 | } | |
320 | ||
321 | void wxNewBitmapButton::DrawShade( int outerLevel, | |
322 | wxDC& dc, | |
323 | wxPen& upperLeftSidePen, | |
324 | wxPen& lowerRightSidePen ) | |
325 | { | |
326 | wxBitmap* pBmp = GetStateLabel(); | |
327 | ||
328 | int x = mMarginX - (outerLevel + 2); | |
329 | int y = mMarginY - (outerLevel + 2); | |
330 | ||
331 | int height = pBmp->GetHeight() + (outerLevel + 2)*2 - 1; | |
332 | int width = pBmp->GetWidth() + (outerLevel + 2)*2 - 1; | |
333 | ||
334 | dc.SetPen( upperLeftSidePen ); | |
335 | dc.DrawLine( x,y, x + width, y ); | |
336 | dc.DrawLine( x,y, x, y + height ); | |
337 | ||
338 | dc.SetPen( lowerRightSidePen ); | |
339 | dc.DrawLine( x + width, y, x + width, y + height + 1 ); | |
340 | dc.DrawLine( x, y + height, x + width, y + height ); | |
341 | } | |
342 | ||
343 | void wxNewBitmapButton::DestroyLabels() | |
344 | { | |
345 | if ( mpDepressedImg ) delete mpDepressedImg; | |
346 | if ( mpPressedImg ) delete mpPressedImg; | |
347 | if ( mpDisabledImg ) delete mpDisabledImg; | |
348 | if ( mpFocusedImg ) delete mpFocusedImg; | |
349 | ||
350 | mpDepressedImg = NULL; | |
351 | mpPressedImg = NULL; | |
352 | mpDisabledImg = NULL; | |
353 | mpFocusedImg = NULL; | |
354 | } | |
355 | ||
356 | wxBitmap* wxNewBitmapButton::GetStateLabel() | |
357 | { | |
358 | if ( IsEnabled() ) | |
359 | { | |
360 | if ( mIsPressed ) | |
361 | { | |
362 | return mpPressedImg; | |
363 | } | |
364 | else | |
365 | { | |
366 | if ( mIsInFocus ) | |
367 | { | |
368 | if ( mHasFocusedBmp ) | |
369 | ||
370 | return mpFocusedImg; | |
371 | else | |
372 | return mpDepressedImg; | |
373 | } | |
374 | else | |
375 | return mpDepressedImg; | |
376 | } | |
377 | } | |
378 | else | |
379 | return mpDisabledImg; | |
380 | } | |
381 | ||
382 | static const unsigned char _gDisableImage[] = { 0x55,0xAA,0x55,0xAA, | |
383 | 0x55,0xAA,0x55,0xAA, | |
384 | 0x55,0xAA,0x55,0xAA, | |
385 | 0x55,0xAA,0x55,0xAA | |
386 | }; | |
387 | void wxNewBitmapButton::RenderLabelImage( wxBitmap*& destBmp, wxBitmap* srcBmp, | |
388 | bool isEnabled, bool isPressed ) | |
389 | { | |
390 | if ( destBmp != 0 ) return; | |
391 | ||
392 | // render labels on-demand | |
393 | ||
394 | wxMemoryDC srcDc; | |
395 | srcDc.SelectObject( *srcBmp ); | |
396 | ||
397 | bool hasText = ( mTextAlignment != NB_NO_TEXT ) && | |
398 | ( mLabelText.length() != 0 ); | |
399 | ||
400 | bool hasImage = (mTextAlignment != NB_NO_IMAGE); | |
401 | ||
402 | wxSize destDim; | |
403 | wxPoint txtPos; | |
404 | wxPoint imgPos; | |
405 | ||
406 | if ( hasText ) | |
407 | { | |
408 | long txtWidth, txtHeight; | |
409 | ||
410 | srcDc.SetFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT) ); | |
411 | srcDc.GetTextExtent( mLabelText, &txtWidth, &txtHeight ); | |
412 | ||
413 | if ( mTextAlignment == NB_ALIGN_TEXT_RIGHT ) | |
414 | { | |
415 | destDim.x = srcBmp->GetWidth() + 2*mTextToLabelGap + txtWidth; | |
416 | ||
417 | destDim.y = | |
418 | wxMax( srcBmp->GetHeight(), txtHeight ); | |
419 | ||
420 | txtPos.x = srcBmp->GetWidth() + mTextToLabelGap; | |
421 | txtPos.y = (destDim.y - txtHeight)/2; | |
422 | imgPos.x = 0; | |
423 | imgPos.y = (destDim.y - srcBmp->GetHeight())/2; | |
424 | } | |
425 | else | |
426 | if ( mTextAlignment == NB_ALIGN_TEXT_BOTTOM ) | |
427 | { | |
428 | destDim.x = | |
429 | wxMax( srcBmp->GetWidth(), txtWidth ); | |
430 | ||
431 | destDim.y = srcBmp->GetHeight() + mTextToLabelGap + txtHeight; | |
432 | ||
433 | txtPos.x = (destDim.x - txtWidth)/2; | |
434 | txtPos.y = srcBmp->GetHeight() + mTextToLabelGap; | |
435 | imgPos.x = (destDim.x - srcBmp->GetWidth())/2; | |
436 | imgPos.y = 0; | |
437 | } | |
438 | else | |
439 | { | |
440 | wxFAIL_MSG(wxT("Unsupported FL alignment type detected in wxNewBitmapButton::RenderLabelImage()")); | |
441 | } | |
442 | } | |
443 | else | |
444 | { | |
445 | imgPos.x = 0; | |
446 | imgPos.y = 0; | |
447 | destDim.x = srcBmp->GetWidth(); | |
448 | destDim.y = srcBmp->GetHeight(); | |
449 | } | |
450 | ||
451 | destBmp = new wxBitmap( int(destDim.x), int(destDim.y) ); | |
452 | ||
453 | wxMemoryDC destDc; | |
454 | destDc.SelectObject( *destBmp ); | |
455 | ||
456 | wxBrush grayBrush( wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE), wxSOLID ); | |
457 | wxPen nullPen( wxColour(0,0,0), 1, wxTRANSPARENT ); | |
458 | ||
459 | destDc.SetBrush( grayBrush ); | |
460 | destDc.SetPen( nullPen ); | |
461 | ||
462 | destDc.DrawRectangle( 0,0, destDim.x+1, destDim.y+1 ); | |
463 | ||
464 | if ( isPressed ) | |
465 | { | |
466 | ++imgPos.x; ++imgPos.y; | |
467 | ++txtPos.x; ++txtPos.y; | |
468 | } | |
469 | ||
470 | if ( hasImage ) | |
471 | { | |
472 | ||
473 | destDc.Blit( imgPos.x, imgPos.y, | |
474 | srcBmp->GetWidth()+1, | |
475 | srcBmp->GetHeight()+1, | |
476 | &srcDc, 0,0, wxCOPY,true ); | |
477 | } | |
478 | ||
479 | if ( hasText ) | |
480 | { | |
481 | wxWindow* pTopWnd = this; | |
482 | ||
483 | do | |
484 | { | |
485 | wxWindow* pParent = pTopWnd->GetParent(); | |
486 | ||
487 | if ( pParent == 0 ) | |
488 | break; | |
489 | ||
490 | pTopWnd = pParent; | |
491 | } while (1); | |
492 | ||
493 | destDc.SetFont( wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT) ); | |
494 | ||
495 | if ( isEnabled ) | |
496 | { | |
497 | destDc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT) ); | |
498 | } | |
499 | else | |
500 | { | |
501 | destDc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW) ); | |
502 | } | |
503 | destDc.SetTextBackground( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE) ); | |
504 | ||
505 | destDc.DrawText( mLabelText, txtPos.x, txtPos.y ); | |
506 | } | |
507 | ||
508 | if ( !isEnabled ){ | |
509 | ||
510 | #ifdef __WXMSW__ // This is currently MSW specific | |
511 | gray_out_image_on_dc( destDc, destDim.x, destDim.y ); | |
512 | #else | |
513 | wxBrush checkerBrush( wxBitmap( (const char*)_gDisableImage,8,8) ); | |
514 | checkerBrush.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) ); | |
515 | destDc.SetBrush( checkerBrush ); | |
516 | destDc.DrawRectangle( imgPos.x, imgPos.y, srcBmp->GetWidth()+1, srcBmp->GetHeight()+1); | |
517 | #endif | |
518 | } | |
519 | // adjust button size to fit the new dimensions of the label | |
520 | if ( !mSizeIsSet && 0 ) | |
521 | { | |
522 | mSizeIsSet = true; | |
523 | SetSize( wxDefaultPosition.x,wxDefaultPosition.y, | |
524 | destBmp->GetWidth() + mMarginX*2, | |
525 | destBmp->GetHeight() + mMarginY*2, 0 | |
526 | ); | |
527 | } | |
528 | destDc.SelectObject( wxNullBitmap ); | |
529 | ||
530 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) | |
531 | // Map to system colours | |
532 | (void) wxToolBar::MapBitmap(destBmp->GetHBITMAP(), destBmp->GetWidth(), destBmp->GetHeight()); | |
533 | #endif | |
534 | } | |
535 | ||
536 | void wxNewBitmapButton::RenderAllLabelImages() | |
537 | { | |
538 | if ( !mIsCreated ) | |
539 | return; | |
540 | RenderLabelImage( mpDisabledImg, &mDepressedBmp, false ); | |
541 | RenderLabelImage( mpPressedImg, &mDepressedBmp, true, true ); | |
542 | RenderLabelImage( mpDepressedImg, &mDepressedBmp, true, false ); | |
543 | if ( mHasFocusedBmp ) | |
544 | { | |
545 | RenderLabelImage( mpFocusedImg, &mFocusedBmp, true, false ); | |
546 | } | |
547 | } | |
548 | ||
549 | ||
550 | void wxNewBitmapButton::RenderLabelImages() | |
551 | { | |
552 | if ( !mIsCreated ) | |
553 | return; | |
554 | ||
555 | if ( !IsEnabled() ) | |
556 | { | |
557 | RenderLabelImage( mpDisabledImg, &mDepressedBmp, false ); | |
558 | } | |
559 | else | |
560 | ||
561 | if ( mIsPressed ) | |
562 | ||
563 | RenderLabelImage( mpPressedImg, &mDepressedBmp, true, true ); | |
564 | else | |
565 | { | |
566 | if ( mIsInFocus ) | |
567 | { | |
568 | if ( mHasFocusedBmp ) | |
569 | RenderLabelImage( mpFocusedImg, &mFocusedBmp, true, false ); | |
570 | else | |
571 | RenderLabelImage( mpDepressedImg, &mDepressedBmp, true, false ); | |
572 | } | |
573 | else | |
574 | RenderLabelImage( mpDepressedImg, &mDepressedBmp, true, false ); | |
575 | } | |
576 | } | |
577 | ||
578 | bool wxNewBitmapButton::Toggle(bool enable) | |
579 | { | |
580 | if ( mIsToggled == enable ) | |
581 | { | |
582 | return false; | |
583 | } | |
584 | ||
585 | mIsToggled = enable; | |
586 | Refresh(); | |
587 | ||
588 | return true; | |
589 | } | |
590 | ||
591 | bool wxNewBitmapButton::Enable(bool enable) | |
592 | { | |
593 | if ( enable != m_isEnabled ) | |
594 | { | |
595 | if ( mIsInFocus ) | |
596 | { | |
597 | mIsInFocus = false; | |
598 | } | |
599 | ||
600 | if ( mIsPressed ) | |
601 | { | |
602 | mIsPressed = false; | |
603 | } | |
604 | ||
605 | Refresh(); | |
606 | } | |
607 | ||
608 | return wxPanel::Enable( enable ); | |
609 | } | |
610 | ||
611 | void wxNewBitmapButton::DrawDecorations( wxDC& dc ) | |
612 | { | |
613 | if ( mIsFlat ) | |
614 | { | |
615 | DrawShade( 1, dc, mGrayPen, mGrayPen ); | |
616 | if ( mIsToggled ) | |
617 | { | |
618 | DrawShade( 0, dc, mDarkPen, mLightPen ); | |
619 | } | |
620 | else 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 || mIsToggled ) | |
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& WXUNUSED(event) ) | |
676 | { | |
677 | mDragStarted = true; | |
678 | mIsPressed = true; | |
679 | Refresh(); | |
680 | } | |
681 | ||
682 | void wxNewBitmapButton::OnLButtonUp( wxMouseEvent& event ) | |
683 | { | |
684 | if ( !mDragStarted ) | |
685 | return; | |
686 | ||
687 | mDragStarted = false; | |
688 | mIsPressed = false; | |
689 | Refresh(); | |
690 | ||
691 | if ( IsInWindow( event.m_x, event.m_y ) ) | |
692 | { | |
693 | // fire event, if mouse was released | |
694 | // within the bounds of button | |
695 | wxCommandEvent cmd( mFiredEventType, GetId() ); | |
696 | GetParent()->ProcessEvent( cmd ); | |
697 | } | |
698 | } | |
699 | ||
700 | bool wxNewBitmapButton::IsInWindow( int x, int y ) | |
701 | { | |
702 | int width, height; | |
703 | GetSize( &width, &height ); | |
704 | ||
705 | return ( x >= 0 && y >= 0 && | |
706 | x < width && | |
707 | y < height ); | |
708 | } | |
709 | ||
710 | void wxNewBitmapButton::OnMouseEnter( wxMouseEvent& WXUNUSED(event) ) | |
711 | { | |
712 | bool prevIsInFocus = mIsInFocus; | |
713 | ||
714 | if ( !mIsInFocus ) | |
715 | { | |
716 | mIsInFocus = true; | |
717 | } | |
718 | if ( prevIsInFocus != mIsInFocus ) | |
719 | { | |
720 | Refresh(); | |
721 | } | |
722 | } | |
723 | ||
724 | void wxNewBitmapButton::OnMouseLeave( wxMouseEvent& WXUNUSED(event) ) | |
725 | { | |
726 | bool prevIsInFocus = mIsInFocus; | |
727 | bool prevIsPressed = mIsPressed; | |
728 | if ( mIsInFocus ) | |
729 | { | |
730 | mIsInFocus = false; | |
731 | mIsPressed = false; | |
732 | } | |
733 | if ( prevIsInFocus != mIsInFocus || prevIsPressed != mIsPressed ) | |
734 | { | |
735 | Refresh(); | |
736 | } | |
737 | } | |
738 | ||
739 | void wxNewBitmapButton::OnSize( wxSizeEvent& WXUNUSED(event) ) | |
740 | { | |
741 | //Reshape(); | |
742 | } | |
743 | ||
744 | void wxNewBitmapButton::Reshape( ) | |
745 | { | |
746 | bool wasCreated = mIsCreated; | |
747 | mIsCreated = true; | |
748 | ||
749 | if ( !wasCreated ) | |
750 | { | |
751 | // in the case of loading button from stream, check if we | |
752 | // have non-empty image-file name, load if possible | |
753 | ||
754 | if ( mImageFileName != wxT("") ) | |
755 | { | |
756 | mDepressedBmp.LoadFile( mImageFileName, mImageFileType ); | |
757 | ||
758 | //wxMessageBox("Image Loaded!!!"); | |
759 | } | |
760 | ||
761 | //RenderLabelImages(); | |
762 | RenderAllLabelImages(); | |
763 | ||
764 | wxBitmap* pCurImg = GetStateLabel(); | |
765 | ||
766 | int w = pCurImg->GetWidth(), | |
767 | h = pCurImg->GetHeight(); | |
768 | ||
769 | SetSize( 0,0, w + mMarginX*2, h + mMarginY*2 , 0 ); | |
770 | } | |
771 | } | |
772 | ||
773 | void wxNewBitmapButton::DrawLabel( wxDC& dc ) | |
774 | { | |
775 | wxBitmap* pCurBmp = GetStateLabel(); | |
776 | ||
777 | if ( pCurBmp == NULL ) | |
778 | { | |
779 | wxSizeEvent evt; | |
780 | OnSize( evt ); // fake it up! | |
781 | ||
782 | //RenderLabelImages(); | |
783 | pCurBmp = GetStateLabel(); | |
784 | } | |
785 | ||
786 | wxMemoryDC mdc; | |
787 | mdc.SelectObject( *pCurBmp ); | |
788 | ||
789 | dc.Blit( mMarginX, mMarginY, | |
790 | pCurBmp->GetWidth(), | |
791 | pCurBmp->GetHeight(), | |
792 | &mdc, 0,0, wxCOPY | |
793 | ); | |
794 | ||
795 | mdc.SelectObject( wxNullBitmap ); | |
796 | } | |
797 | ||
798 | void wxNewBitmapButton::OnPaint( wxPaintEvent& WXUNUSED(event) ) | |
799 | { | |
800 | wxPaintDC dc(this); | |
801 | ||
802 | // first, make sure images for current state are prepared | |
803 | //RenderLabelImages(); | |
804 | ||
805 | DrawLabel( dc ); | |
806 | ||
807 | DrawDecorations( dc ); | |
808 | } | |
809 | ||
810 | void wxNewBitmapButton::OnEraseBackground( wxEraseEvent& WXUNUSED(event) ) | |
811 | { | |
812 | // do nothing | |
813 | } | |
814 | ||
815 | void wxNewBitmapButton::OnKillFocus( wxFocusEvent& WXUNUSED(event) ) | |
816 | { | |
817 | // useless | |
818 | ||
819 | wxMessageBox(wxT("kill-focus for button!")); | |
820 | } | |
821 | ||
822 | // ---------------------------------------------------------------------------- | |
823 | // UI updates | |
824 | // ---------------------------------------------------------------------------- | |
825 | ||
826 | void wxNewBitmapButton::OnIdle(wxIdleEvent& event) | |
827 | { | |
828 | DoButtonUpdate(); | |
829 | ||
830 | event.Skip(); | |
831 | } | |
832 | ||
833 | // Do the toolbar button updates (check for EVT_UPDATE_UI handlers) | |
834 | void wxNewBitmapButton::DoButtonUpdate() | |
835 | { | |
836 | wxUpdateUIEvent event(GetId()); | |
837 | event.SetEventObject(this); | |
838 | ||
839 | if ( GetParent()->ProcessEvent(event) ) | |
840 | { | |
841 | if ( event.GetSetEnabled() ) | |
842 | { | |
843 | bool enabled = event.GetEnabled(); | |
844 | if ( enabled != IsEnabled() ) | |
845 | Enable( enabled ); | |
846 | } | |
847 | if ( event.GetSetChecked() ) | |
848 | Toggle( event.GetChecked() ); | |
849 | } | |
850 | } |