/[pcsx2_0.9.7]/branch/r3113_0.9.7_beta/3rdparty/wxWidgets/src/msw/tbar95.cpp
ViewVC logotype

Contents of /branch/r3113_0.9.7_beta/3rdparty/wxWidgets/src/msw/tbar95.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 32 - (show annotations) (download)
Tue Sep 7 03:29:01 2010 UTC (9 years, 11 months ago) by william
File size: 58053 byte(s)
branching from upstream revision (http://pcsx2.googlecode.com/svn/trunk
): r3113 to
https://svn.netsolutions.dnsalias.com/websvn/ps2/pcsx2/pcsx2_0.9.7/branch/r3113_0.9.7_beta
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/tbar95.cpp
3 // Purpose: wxToolBar
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id: tbar95.cpp 58446 2009-01-26 23:32:16Z VS $
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_TOOLBAR && wxUSE_TOOLBAR_NATIVE && !defined(__SMARTPHONE__)
28
29 #include "wx/toolbar.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #include "wx/dynarray.h"
34 #include "wx/frame.h"
35 #include "wx/log.h"
36 #include "wx/intl.h"
37 #include "wx/settings.h"
38 #include "wx/bitmap.h"
39 #include "wx/dcmemory.h"
40 #include "wx/control.h"
41 #include "wx/app.h" // for GetComCtl32Version
42 #include "wx/image.h"
43 #endif
44
45 #include "wx/sysopt.h"
46
47 #include "wx/msw/private.h"
48
49 #if wxUSE_UXTHEME
50 #include "wx/msw/uxtheme.h"
51 #endif
52
53 // this define controls whether the code for button colours remapping (only
54 // useful for 16 or 256 colour images) is active at all, it's always turned off
55 // for CE where it doesn't compile (and is probably not needed anyhow) and may
56 // also be turned off for other systems if you always use 24bpp images and so
57 // never need it
58 #ifndef __WXWINCE__
59 #define wxREMAP_BUTTON_COLOURS
60 #endif // !__WXWINCE__
61
62 // ----------------------------------------------------------------------------
63 // constants
64 // ----------------------------------------------------------------------------
65
66 // these standard constants are not always defined in compilers headers
67
68 // Styles
69 #ifndef TBSTYLE_FLAT
70 #define TBSTYLE_LIST 0x1000
71 #define TBSTYLE_FLAT 0x0800
72 #endif
73
74 #ifndef TBSTYLE_TRANSPARENT
75 #define TBSTYLE_TRANSPARENT 0x8000
76 #endif
77
78 #ifndef TBSTYLE_TOOLTIPS
79 #define TBSTYLE_TOOLTIPS 0x0100
80 #endif
81
82 // Messages
83 #ifndef TB_GETSTYLE
84 #define TB_SETSTYLE (WM_USER + 56)
85 #define TB_GETSTYLE (WM_USER + 57)
86 #endif
87
88 #ifndef TB_HITTEST
89 #define TB_HITTEST (WM_USER + 69)
90 #endif
91
92 #ifndef TB_GETMAXSIZE
93 #define TB_GETMAXSIZE (WM_USER + 83)
94 #endif
95
96 // these values correspond to those used by comctl32.dll
97 #define DEFAULTBITMAPX 16
98 #define DEFAULTBITMAPY 15
99
100 // ----------------------------------------------------------------------------
101 // wxWin macros
102 // ----------------------------------------------------------------------------
103
104 IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl)
105
106 /*
107 TOOLBAR PROPERTIES
108 tool
109 bitmap
110 bitmap2
111 tooltip
112 longhelp
113 radio (bool)
114 toggle (bool)
115 separator
116 style ( wxNO_BORDER | wxTB_HORIZONTAL)
117 bitmapsize
118 margins
119 packing
120 separation
121
122 dontattachtoframe
123 */
124
125 BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
126 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent)
127 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged)
128 EVT_ERASE_BACKGROUND(wxToolBar::OnEraseBackground)
129 END_EVENT_TABLE()
130
131 // ----------------------------------------------------------------------------
132 // private classes
133 // ----------------------------------------------------------------------------
134
135 class wxToolBarTool : public wxToolBarToolBase
136 {
137 public:
138 wxToolBarTool(wxToolBar *tbar,
139 int id,
140 const wxString& label,
141 const wxBitmap& bmpNormal,
142 const wxBitmap& bmpDisabled,
143 wxItemKind kind,
144 wxObject *clientData,
145 const wxString& shortHelp,
146 const wxString& longHelp)
147 : wxToolBarToolBase(tbar, id, label, bmpNormal, bmpDisabled, kind,
148 clientData, shortHelp, longHelp)
149 {
150 m_nSepCount = 0;
151 }
152
153 wxToolBarTool(wxToolBar *tbar, wxControl *control)
154 : wxToolBarToolBase(tbar, control)
155 {
156 m_nSepCount = 1;
157 }
158
159 virtual void SetLabel(const wxString& label)
160 {
161 if ( label == m_label )
162 return;
163
164 wxToolBarToolBase::SetLabel(label);
165
166 // we need to update the label shown in the toolbar because it has a
167 // pointer to the internal buffer of the old label
168 //
169 // TODO: use TB_SETBUTTONINFO
170 }
171
172 // set/get the number of separators which we use to cover the space used by
173 // a control in the toolbar
174 void SetSeparatorsCount(size_t count) { m_nSepCount = count; }
175 size_t GetSeparatorsCount() const { return m_nSepCount; }
176
177 private:
178 size_t m_nSepCount;
179
180 DECLARE_NO_COPY_CLASS(wxToolBarTool)
181 };
182
183 // ============================================================================
184 // implementation
185 // ============================================================================
186
187 // ----------------------------------------------------------------------------
188 // wxToolBarTool
189 // ----------------------------------------------------------------------------
190
191 wxToolBarToolBase *wxToolBar::CreateTool(int id,
192 const wxString& label,
193 const wxBitmap& bmpNormal,
194 const wxBitmap& bmpDisabled,
195 wxItemKind kind,
196 wxObject *clientData,
197 const wxString& shortHelp,
198 const wxString& longHelp)
199 {
200 return new wxToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind,
201 clientData, shortHelp, longHelp);
202 }
203
204 wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
205 {
206 return new wxToolBarTool(this, control);
207 }
208
209 // ----------------------------------------------------------------------------
210 // wxToolBar construction
211 // ----------------------------------------------------------------------------
212
213 void wxToolBar::Init()
214 {
215 m_hBitmap = 0;
216 m_disabledImgList = NULL;
217
218 m_nButtons = 0;
219
220 m_defaultWidth = DEFAULTBITMAPX;
221 m_defaultHeight = DEFAULTBITMAPY;
222
223 m_pInTool = 0;
224 }
225
226 bool wxToolBar::Create(wxWindow *parent,
227 wxWindowID id,
228 const wxPoint& pos,
229 const wxSize& size,
230 long style,
231 const wxString& name)
232 {
233 // common initialisation
234 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
235 return false;
236
237 FixupStyle();
238
239 // MSW-specific initialisation
240 if ( !MSWCreateToolbar(pos, size) )
241 return false;
242
243 wxSetCCUnicodeFormat(GetHwnd());
244
245 // workaround for flat toolbar on Windows XP classic style: we have to set
246 // the style after creating the control; doing it at creation time doesn't work
247 #if wxUSE_UXTHEME
248 if ( style & wxTB_FLAT )
249 {
250 LRESULT style = GetMSWToolbarStyle();
251
252 if ( !(style & TBSTYLE_FLAT) )
253 ::SendMessage(GetHwnd(), TB_SETSTYLE, 0, style | TBSTYLE_FLAT);
254 }
255 #endif // wxUSE_UXTHEME
256
257 return true;
258 }
259
260 bool wxToolBar::MSWCreateToolbar(const wxPoint& pos, const wxSize& size)
261 {
262 if ( !MSWCreateControl(TOOLBARCLASSNAME, wxEmptyString, pos, size) )
263 return false;
264
265 // toolbar-specific post initialisation
266 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
267
268 // Fix a bug on e.g. the Silver theme on WinXP where control backgrounds
269 // are incorrectly drawn, by forcing the background to a specific colour.
270 int majorVersion, minorVersion;
271 wxGetOsVersion(& majorVersion, & minorVersion);
272 if (majorVersion < 6)
273 SetBackgroundColour(GetBackgroundColour());
274
275 return true;
276 }
277
278 void wxToolBar::Recreate()
279 {
280 const HWND hwndOld = GetHwnd();
281 if ( !hwndOld )
282 {
283 // we haven't been created yet, no need to recreate
284 return;
285 }
286
287 // get the position and size before unsubclassing the old toolbar
288 const wxPoint pos = GetPosition();
289 const wxSize size = GetSize();
290
291 UnsubclassWin();
292
293 if ( !MSWCreateToolbar(pos, size) )
294 {
295 // what can we do?
296 wxFAIL_MSG( _T("recreating the toolbar failed") );
297
298 return;
299 }
300
301 // reparent all our children under the new toolbar
302 for ( wxWindowList::compatibility_iterator node = m_children.GetFirst();
303 node;
304 node = node->GetNext() )
305 {
306 wxWindow *win = node->GetData();
307 if ( !win->IsTopLevel() )
308 ::SetParent(GetHwndOf(win), GetHwnd());
309 }
310
311 // only destroy the old toolbar now --
312 // after all the children had been reparented
313 ::DestroyWindow(hwndOld);
314
315 // it is for the old bitmap control and can't be used with the new one
316 if ( m_hBitmap )
317 {
318 ::DeleteObject((HBITMAP) m_hBitmap);
319 m_hBitmap = 0;
320 }
321
322 if ( m_disabledImgList )
323 {
324 delete m_disabledImgList;
325 m_disabledImgList = NULL;
326 }
327
328 Realize();
329 }
330
331 wxToolBar::~wxToolBar()
332 {
333 // we must refresh the frame size when the toolbar is deleted but the frame
334 // is not - otherwise toolbar leaves a hole in the place it used to occupy
335 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
336 if ( frame && !frame->IsBeingDeleted() )
337 frame->SendSizeEvent();
338
339 if ( m_hBitmap )
340 ::DeleteObject((HBITMAP) m_hBitmap);
341
342 delete m_disabledImgList;
343 }
344
345 wxSize wxToolBar::DoGetBestSize() const
346 {
347 wxSize sizeBest;
348
349 SIZE size;
350 if ( !::SendMessage(GetHwnd(), TB_GETMAXSIZE, 0, (LPARAM)&size) )
351 {
352 // maybe an old (< 0x400) Windows version? try to approximate the
353 // toolbar size ourselves
354 sizeBest = GetToolSize();
355 sizeBest.y += 2 * ::GetSystemMetrics(SM_CYBORDER); // Add borders
356 sizeBest.x *= GetToolsCount();
357
358 // reverse horz and vertical components if necessary
359 if ( IsVertical() )
360 {
361 int t = sizeBest.x;
362 sizeBest.x = sizeBest.y;
363 sizeBest.y = t;
364 }
365 }
366 else // TB_GETMAXSIZE succeeded
367 {
368 // but it could still return an incorrect result due to what appears to
369 // be a bug in old comctl32.dll versions which don't handle controls in
370 // the toolbar correctly, so work around it (see SF patch 1902358)
371 if ( !IsVertical() && wxApp::GetComCtl32Version() < 600 )
372 {
373 // calculate the toolbar width in alternative way
374 RECT rcFirst, rcLast;
375 if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT, 0, (LPARAM)&rcFirst)
376 && ::SendMessage(GetHwnd(), TB_GETITEMRECT,
377 GetToolsCount() - 1, (LPARAM)&rcLast) )
378 {
379 const int widthAlt = rcLast.right - rcFirst.left;
380 if ( widthAlt > size.cx )
381 size.cx = widthAlt;
382 }
383 }
384
385 sizeBest.x = size.cx;
386 sizeBest.y = size.cy;
387 }
388
389 if (!IsVertical())
390 {
391 // Without the extra height, DoGetBestSize can report a size that's
392 // smaller than the actual window, causing windows to overlap slightly
393 // in some circumstances, leading to missing borders (especially noticeable
394 // in AUI layouts).
395 if (!(GetWindowStyle() & wxTB_NODIVIDER))
396 sizeBest.y += 2;
397 sizeBest.y ++;
398 }
399
400 CacheBestSize(sizeBest);
401
402 return sizeBest;
403 }
404
405 WXDWORD wxToolBar::MSWGetStyle(long style, WXDWORD *exstyle) const
406 {
407 // toolbars never have border, giving one to them results in broken
408 // appearance
409 WXDWORD msStyle = wxControl::MSWGetStyle
410 (
411 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle
412 );
413
414 if ( !(style & wxTB_NO_TOOLTIPS) )
415 msStyle |= TBSTYLE_TOOLTIPS;
416
417 if ( style & (wxTB_FLAT | wxTB_HORZ_LAYOUT) )
418 {
419 // static as it doesn't change during the program lifetime
420 static const int s_verComCtl = wxApp::GetComCtl32Version();
421
422 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
423 // style with 6.00 (part of Windows XP) leads to the toolbar with
424 // incorrect background colour - and not using it still results in the
425 // correct (flat) toolbar, so don't use it there
426 if ( s_verComCtl > 400 && s_verComCtl < 600 )
427 msStyle |= TBSTYLE_FLAT | TBSTYLE_TRANSPARENT;
428
429 if ( s_verComCtl >= 470 && style & wxTB_HORZ_LAYOUT )
430 msStyle |= TBSTYLE_LIST;
431 }
432
433 if ( style & wxTB_NODIVIDER )
434 msStyle |= CCS_NODIVIDER;
435
436 if ( style & wxTB_NOALIGN )
437 msStyle |= CCS_NOPARENTALIGN;
438
439 if ( style & wxTB_VERTICAL )
440 msStyle |= CCS_VERT;
441
442 if( style & wxTB_BOTTOM )
443 msStyle |= CCS_BOTTOM;
444
445 if ( style & wxTB_RIGHT )
446 msStyle |= CCS_RIGHT;
447
448 return msStyle;
449 }
450
451 // ----------------------------------------------------------------------------
452 // adding/removing tools
453 // ----------------------------------------------------------------------------
454
455 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool)
456 {
457 // nothing special to do here - we really create the toolbar buttons in
458 // Realize() later
459 tool->Attach(this);
460
461 InvalidateBestSize();
462 return true;
463 }
464
465 bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool)
466 {
467 // the main difficulty we have here is with the controls in the toolbars:
468 // as we (sometimes) use several separators to cover up the space used by
469 // them, the indices are not the same for us and the toolbar
470
471 // first determine the position of the first button to delete: it may be
472 // different from pos if we use several separators to cover the space used
473 // by a control
474 wxToolBarToolsList::compatibility_iterator node;
475 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
476 {
477 wxToolBarToolBase *tool2 = node->GetData();
478 if ( tool2 == tool )
479 {
480 // let node point to the next node in the list
481 node = node->GetNext();
482
483 break;
484 }
485
486 if ( tool2->IsControl() )
487 pos += ((wxToolBarTool *)tool2)->GetSeparatorsCount() - 1;
488 }
489
490 // now determine the number of buttons to delete and the area taken by them
491 size_t nButtonsToDelete = 1;
492
493 // get the size of the button we're going to delete
494 RECT r;
495 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT, pos, (LPARAM)&r) )
496 {
497 wxLogLastError(_T("TB_GETITEMRECT"));
498 }
499
500 int width = r.right - r.left;
501
502 if ( tool->IsControl() )
503 {
504 nButtonsToDelete = ((wxToolBarTool *)tool)->GetSeparatorsCount();
505 width *= nButtonsToDelete;
506 tool->GetControl()->Destroy();
507 }
508
509 // do delete all buttons
510 m_nButtons -= nButtonsToDelete;
511 while ( nButtonsToDelete-- > 0 )
512 {
513 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, pos, 0) )
514 {
515 wxLogLastError(wxT("TB_DELETEBUTTON"));
516
517 return false;
518 }
519 }
520
521 tool->Detach();
522
523 // and finally reposition all the controls after this button (the toolbar
524 // takes care of all normal items)
525 for ( /* node -> first after deleted */ ; node; node = node->GetNext() )
526 {
527 wxToolBarToolBase *tool2 = node->GetData();
528 if ( tool2->IsControl() )
529 {
530 int x;
531 wxControl *control = tool2->GetControl();
532 control->GetPosition(&x, NULL);
533 control->Move(x - width, wxDefaultCoord);
534 }
535 }
536
537 InvalidateBestSize();
538
539 return true;
540 }
541
542 void wxToolBar::CreateDisabledImageList()
543 {
544 if (m_disabledImgList != NULL)
545 {
546 delete m_disabledImgList;
547 m_disabledImgList = NULL;
548 }
549
550 // as we can't use disabled image list with older versions of comctl32.dll,
551 // don't even bother creating it
552 if ( wxApp::GetComCtl32Version() >= 470 )
553 {
554 // search for the first disabled button img in the toolbar, if any
555 for ( wxToolBarToolsList::compatibility_iterator
556 node = m_tools.GetFirst(); node; node = node->GetNext() )
557 {
558 wxToolBarToolBase *tool = node->GetData();
559 wxBitmap bmpDisabled = tool->GetDisabledBitmap();
560 if ( bmpDisabled.Ok() )
561 {
562 m_disabledImgList = new wxImageList
563 (
564 m_defaultWidth,
565 m_defaultHeight,
566 bmpDisabled.GetMask() != NULL,
567 GetToolsCount()
568 );
569 break;
570 }
571 }
572
573 // we don't have any disabled bitmaps
574 }
575 }
576
577 void wxToolBar::AdjustToolBitmapSize()
578 {
579 wxSize s(m_defaultWidth, m_defaultHeight);
580 const wxSize orig_s(s);
581
582 for ( wxToolBarToolsList::const_iterator i = m_tools.begin();
583 i != m_tools.end();
584 ++i )
585 {
586 const wxBitmap& bmp = (*i)->GetNormalBitmap();
587 s.IncTo(wxSize(bmp.GetWidth(), bmp.GetHeight()));
588 }
589
590 if ( s != orig_s )
591 SetToolBitmapSize(s);
592 }
593
594 bool wxToolBar::Realize()
595 {
596 const size_t nTools = GetToolsCount();
597 if ( nTools == 0 )
598 // nothing to do
599 return true;
600
601 // make sure tool size is larger enough for all all bitmaps to fit in
602 // (this is consistent with what other ports do):
603 AdjustToolBitmapSize();
604
605 #ifdef wxREMAP_BUTTON_COLOURS
606 // don't change the values of these constants, they can be set from the
607 // user code via wxSystemOptions
608 enum
609 {
610 Remap_None = -1,
611 Remap_Bg,
612 Remap_Buttons,
613 Remap_TransparentBg
614 };
615
616 // the user-specified option overrides anything, but if it wasn't set, only
617 // remap the buttons on 8bpp displays as otherwise the bitmaps usually look
618 // much worse after remapping
619 static const wxChar *remapOption = wxT("msw.remap");
620 const int remapValue = wxSystemOptions::HasOption(remapOption)
621 ? wxSystemOptions::GetOptionInt(remapOption)
622 : wxDisplayDepth() <= 8 ? Remap_Buttons
623 : Remap_None;
624
625 #endif // wxREMAP_BUTTON_COLOURS
626
627 // delete all old buttons, if any
628 for ( size_t pos = 0; pos < m_nButtons; pos++ )
629 {
630 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, 0, 0) )
631 {
632 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
633 }
634 }
635
636 // First, add the bitmap: we use one bitmap for all toolbar buttons
637 // ----------------------------------------------------------------
638
639 wxToolBarToolsList::compatibility_iterator node;
640 int bitmapId = 0;
641
642 wxSize sizeBmp;
643 if ( HasFlag(wxTB_NOICONS) )
644 {
645 // no icons, don't leave space for them
646 sizeBmp.x =
647 sizeBmp.y = 0;
648 }
649 else // do show icons
650 {
651 // if we already have a bitmap, we'll replace the existing one --
652 // otherwise we'll install a new one
653 HBITMAP oldToolBarBitmap = (HBITMAP)m_hBitmap;
654
655 sizeBmp.x = m_defaultWidth;
656 sizeBmp.y = m_defaultHeight;
657
658 const wxCoord totalBitmapWidth = m_defaultWidth *
659 wx_truncate_cast(wxCoord, nTools),
660 totalBitmapHeight = m_defaultHeight;
661
662 // Create a bitmap and copy all the tool bitmaps into it
663 wxMemoryDC dcAllButtons;
664 wxBitmap bitmap(totalBitmapWidth, totalBitmapHeight);
665 dcAllButtons.SelectObject(bitmap);
666
667 #ifdef wxREMAP_BUTTON_COLOURS
668 if ( remapValue != Remap_TransparentBg )
669 #endif // wxREMAP_BUTTON_COLOURS
670 {
671 // VZ: why do we hardcode grey colour for CE?
672 dcAllButtons.SetBackground(wxBrush(
673 #ifdef __WXWINCE__
674 wxColour(0xc0, 0xc0, 0xc0)
675 #else // !__WXWINCE__
676 GetBackgroundColour()
677 #endif // __WXWINCE__/!__WXWINCE__
678 ));
679 dcAllButtons.Clear();
680 }
681
682 m_hBitmap = bitmap.GetHBITMAP();
683 HBITMAP hBitmap = (HBITMAP)m_hBitmap;
684
685 #ifdef wxREMAP_BUTTON_COLOURS
686 if ( remapValue == Remap_Bg )
687 {
688 dcAllButtons.SelectObject(wxNullBitmap);
689
690 // Even if we're not remapping the bitmap
691 // content, we still have to remap the background.
692 hBitmap = (HBITMAP)MapBitmap((WXHBITMAP) hBitmap,
693 totalBitmapWidth, totalBitmapHeight);
694
695 dcAllButtons.SelectObject(bitmap);
696 }
697 #endif // wxREMAP_BUTTON_COLOURS
698
699 // the button position
700 wxCoord x = 0;
701
702 // the number of buttons (not separators)
703 int nButtons = 0;
704
705 CreateDisabledImageList();
706 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
707 {
708 wxToolBarToolBase *tool = node->GetData();
709 if ( tool->IsButton() )
710 {
711 const wxBitmap& bmp = tool->GetNormalBitmap();
712
713 const int w = bmp.GetWidth();
714 const int h = bmp.GetHeight();
715
716 if ( bmp.Ok() )
717 {
718 int xOffset = wxMax(0, (m_defaultWidth - w)/2);
719 int yOffset = wxMax(0, (m_defaultHeight - h)/2);
720
721 // notice the last parameter: do use mask
722 dcAllButtons.DrawBitmap(bmp, x + xOffset, yOffset, true);
723 }
724 else
725 {
726 wxFAIL_MSG( _T("invalid tool button bitmap") );
727 }
728
729 // also deal with disabled bitmap if we want to use them
730 if ( m_disabledImgList )
731 {
732 wxBitmap bmpDisabled = tool->GetDisabledBitmap();
733 #if wxUSE_IMAGE && wxUSE_WXDIB
734 if ( !bmpDisabled.Ok() )
735 {
736 // no disabled bitmap specified but we still need to
737 // fill the space in the image list with something, so
738 // we grey out the normal bitmap
739 wxImage imgGreyed;
740 wxCreateGreyedImage(bmp.ConvertToImage(), imgGreyed);
741
742 #ifdef wxREMAP_BUTTON_COLOURS
743 if ( remapValue == Remap_Buttons )
744 {
745 // we need to have light grey background colour for
746 // MapBitmap() to work correctly
747 for ( int y = 0; y < h; y++ )
748 {
749 for ( int x = 0; x < w; x++ )
750 {
751 if ( imgGreyed.IsTransparent(x, y) )
752 imgGreyed.SetRGB(x, y,
753 wxLIGHT_GREY->Red(),
754 wxLIGHT_GREY->Green(),
755 wxLIGHT_GREY->Blue());
756 }
757 }
758 }
759 #endif // wxREMAP_BUTTON_COLOURS
760
761 bmpDisabled = wxBitmap(imgGreyed);
762 }
763 #endif // wxUSE_IMAGE
764
765 #ifdef wxREMAP_BUTTON_COLOURS
766 if ( remapValue == Remap_Buttons )
767 MapBitmap(bmpDisabled.GetHBITMAP(), w, h);
768 #endif // wxREMAP_BUTTON_COLOURS
769
770 m_disabledImgList->Add(bmpDisabled);
771 }
772
773 // still inc width and number of buttons because otherwise the
774 // subsequent buttons will all be shifted which is rather confusing
775 // (and like this you'd see immediately which bitmap was bad)
776 x += m_defaultWidth;
777 nButtons++;
778 }
779 }
780
781 dcAllButtons.SelectObject(wxNullBitmap);
782
783 // don't delete this HBITMAP!
784 bitmap.SetHBITMAP(0);
785
786 #ifdef wxREMAP_BUTTON_COLOURS
787 if ( remapValue == Remap_Buttons )
788 {
789 // Map to system colours
790 hBitmap = (HBITMAP)MapBitmap((WXHBITMAP) hBitmap,
791 totalBitmapWidth, totalBitmapHeight);
792 }
793 #endif // wxREMAP_BUTTON_COLOURS
794
795 bool addBitmap = true;
796
797 if ( oldToolBarBitmap )
798 {
799 #ifdef TB_REPLACEBITMAP
800 if ( wxApp::GetComCtl32Version() >= 400 )
801 {
802 TBREPLACEBITMAP replaceBitmap;
803 replaceBitmap.hInstOld = NULL;
804 replaceBitmap.hInstNew = NULL;
805 replaceBitmap.nIDOld = (UINT) oldToolBarBitmap;
806 replaceBitmap.nIDNew = (UINT) hBitmap;
807 replaceBitmap.nButtons = nButtons;
808 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP,
809 0, (LPARAM) &replaceBitmap) )
810 {
811 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
812 }
813
814 ::DeleteObject(oldToolBarBitmap);
815
816 // already done
817 addBitmap = false;
818 }
819 else
820 #endif // TB_REPLACEBITMAP
821 {
822 // we can't replace the old bitmap, so we will add another one
823 // (awfully inefficient, but what else to do?) and shift the bitmap
824 // indices accordingly
825 addBitmap = true;
826
827 bitmapId = m_nButtons;
828 }
829 }
830
831 if ( addBitmap ) // no old bitmap or we can't replace it
832 {
833 TBADDBITMAP addBitmap;
834 addBitmap.hInst = 0;
835 addBitmap.nID = (UINT) hBitmap;
836 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP,
837 (WPARAM) nButtons, (LPARAM)&addBitmap) == -1 )
838 {
839 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
840 }
841 }
842
843 // disable image lists are only supported in comctl32.dll 4.70+
844 if ( wxApp::GetComCtl32Version() >= 470 )
845 {
846 HIMAGELIST hil = m_disabledImgList
847 ? GetHimagelistOf(m_disabledImgList)
848 : 0;
849
850 // notice that we set the image list even if don't have one right
851 // now as we could have it before and need to reset it in this case
852 HIMAGELIST oldImageList = (HIMAGELIST)
853 ::SendMessage(GetHwnd(), TB_SETDISABLEDIMAGELIST, 0, (LPARAM)hil);
854
855 // delete previous image list if any
856 if ( oldImageList )
857 ::DeleteObject(oldImageList);
858 }
859 }
860
861 // don't call SetToolBitmapSize() as we don't want to change the values of
862 // m_defaultWidth/Height
863 if ( !::SendMessage(GetHwnd(), TB_SETBITMAPSIZE, 0,
864 MAKELONG(sizeBmp.x, sizeBmp.y)) )
865 {
866 wxLogLastError(_T("TB_SETBITMAPSIZE"));
867 }
868
869 // Next add the buttons and separators
870 // -----------------------------------
871
872 TBBUTTON *buttons = new TBBUTTON[nTools];
873
874 // this array will hold the indices of all controls in the toolbar
875 wxArrayInt controlIds;
876
877 bool lastWasRadio = false;
878 int i = 0;
879 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
880 {
881 wxToolBarToolBase *tool = node->GetData();
882
883 // don't add separators to the vertical toolbar with old comctl32.dll
884 // versions as they didn't handle this properly
885 if ( IsVertical() && tool->IsSeparator() &&
886 wxApp::GetComCtl32Version() <= 472 )
887 {
888 continue;
889 }
890
891 TBBUTTON& button = buttons[i];
892
893 wxZeroMemory(button);
894
895 bool isRadio = false;
896 switch ( tool->GetStyle() )
897 {
898 case wxTOOL_STYLE_CONTROL:
899 button.idCommand = tool->GetId();
900 // fall through: create just a separator too
901
902 case wxTOOL_STYLE_SEPARATOR:
903 button.fsState = TBSTATE_ENABLED;
904 button.fsStyle = TBSTYLE_SEP;
905 break;
906
907 case wxTOOL_STYLE_BUTTON:
908 if ( !HasFlag(wxTB_NOICONS) )
909 button.iBitmap = bitmapId;
910
911 if ( HasFlag(wxTB_TEXT) )
912 {
913 const wxString& label = tool->GetLabel();
914 if ( !label.empty() )
915 button.iString = (int)label.c_str();
916 }
917
918 button.idCommand = tool->GetId();
919
920 if ( tool->IsEnabled() )
921 button.fsState |= TBSTATE_ENABLED;
922 if ( tool->IsToggled() )
923 button.fsState |= TBSTATE_CHECKED;
924
925 switch ( tool->GetKind() )
926 {
927 case wxITEM_RADIO:
928 button.fsStyle = TBSTYLE_CHECKGROUP;
929
930 if ( !lastWasRadio )
931 {
932 // the first item in the radio group is checked by
933 // default to be consistent with wxGTK and the menu
934 // radio items
935 button.fsState |= TBSTATE_CHECKED;
936
937 if (tool->Toggle(true))
938 {
939 DoToggleTool(tool, true);
940 }
941 }
942 else if ( tool->IsToggled() )
943 {
944 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
945 int prevIndex = i - 1;
946 while ( nodePrev )
947 {
948 TBBUTTON& prevButton = buttons[prevIndex];
949 wxToolBarToolBase *tool = nodePrev->GetData();
950 if ( !tool->IsButton() || tool->GetKind() != wxITEM_RADIO )
951 break;
952
953 if ( tool->Toggle(false) )
954 DoToggleTool(tool, false);
955
956 prevButton.fsState &= ~TBSTATE_CHECKED;
957 nodePrev = nodePrev->GetPrevious();
958 prevIndex--;
959 }
960 }
961
962 isRadio = true;
963 break;
964
965 case wxITEM_CHECK:
966 button.fsStyle = TBSTYLE_CHECK;
967 break;
968
969 case wxITEM_NORMAL:
970 button.fsStyle = TBSTYLE_BUTTON;
971 break;
972
973 default:
974 wxFAIL_MSG( _T("unexpected toolbar button kind") );
975 button.fsStyle = TBSTYLE_BUTTON;
976 break;
977 }
978
979 bitmapId++;
980 break;
981 }
982
983 lastWasRadio = isRadio;
984
985 i++;
986 }
987
988 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS, (WPARAM)i, (LPARAM)buttons) )
989 {
990 wxLogLastError(wxT("TB_ADDBUTTONS"));
991 }
992
993 delete [] buttons;
994
995 // Deal with the controls finally
996 // ------------------------------
997
998 // adjust the controls size to fit nicely in the toolbar
999 int y = 0;
1000 size_t index = 0;
1001 for ( node = m_tools.GetFirst(); node; node = node->GetNext(), index++ )
1002 {
1003 wxToolBarToolBase *tool = node->GetData();
1004
1005 // we calculate the running y coord for vertical toolbars so we need to
1006 // get the items size for all items but for the horizontal ones we
1007 // don't need to deal with the non controls
1008 bool isControl = tool->IsControl();
1009 if ( !isControl && !IsVertical() )
1010 continue;
1011
1012 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
1013 // latter only appeared in v4.70 of comctl32.dll
1014 RECT r;
1015 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT,
1016 index, (LPARAM)(LPRECT)&r) )
1017 {
1018 wxLogLastError(wxT("TB_GETITEMRECT"));
1019 }
1020
1021 if ( !isControl )
1022 {
1023 // can only be control if isVertical
1024 y += r.bottom - r.top;
1025
1026 continue;
1027 }
1028
1029 wxControl *control = tool->GetControl();
1030 wxSize size = control->GetSize();
1031
1032 // the position of the leftmost controls corner
1033 int left = wxDefaultCoord;
1034
1035 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
1036 #ifdef TB_SETBUTTONINFO
1037 // available in headers, now check whether it is available now
1038 // (during run-time)
1039 if ( wxApp::GetComCtl32Version() >= 471 )
1040 {
1041 // set the (underlying) separators width to be that of the
1042 // control
1043 TBBUTTONINFO tbbi;
1044 tbbi.cbSize = sizeof(tbbi);
1045 tbbi.dwMask = TBIF_SIZE;
1046 tbbi.cx = (WORD)size.x;
1047 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO,
1048 tool->GetId(), (LPARAM)&tbbi) )
1049 {
1050 // the id is probably invalid?
1051 wxLogLastError(wxT("TB_SETBUTTONINFO"));
1052 }
1053 }
1054 else
1055 #endif // comctl32.dll 4.71
1056 // TB_SETBUTTONINFO unavailable
1057 {
1058 // try adding several separators to fit the controls width
1059 int widthSep = r.right - r.left;
1060 left = r.left;
1061
1062 TBBUTTON tbb;
1063 wxZeroMemory(tbb);
1064 tbb.idCommand = 0;
1065 tbb.fsState = TBSTATE_ENABLED;
1066 tbb.fsStyle = TBSTYLE_SEP;
1067
1068 size_t nSeparators = size.x / widthSep;
1069 for ( size_t nSep = 0; nSep < nSeparators; nSep++ )
1070 {
1071 if ( !::SendMessage(GetHwnd(), TB_INSERTBUTTON,
1072 index, (LPARAM)&tbb) )
1073 {
1074 wxLogLastError(wxT("TB_INSERTBUTTON"));
1075 }
1076
1077 index++;
1078 }
1079
1080 // remember the number of separators we used - we'd have to
1081 // delete all of them later
1082 ((wxToolBarTool *)tool)->SetSeparatorsCount(nSeparators);
1083
1084 // adjust the controls width to exactly cover the separators
1085 control->SetSize((nSeparators + 1)*widthSep, wxDefaultCoord);
1086 }
1087
1088 // position the control itself correctly vertically
1089 int height = r.bottom - r.top;
1090 int diff = height - size.y;
1091 if ( diff < 0 )
1092 {
1093 // the control is too high, resize to fit
1094 control->SetSize(wxDefaultCoord, height - 2);
1095
1096 diff = 2;
1097 }
1098
1099 int top;
1100 if ( IsVertical() )
1101 {
1102 left = 0;
1103 top = y;
1104
1105 y += height + 2 * GetMargins().y;
1106 }
1107 else // horizontal toolbar
1108 {
1109 if ( left == wxDefaultCoord )
1110 left = r.left;
1111
1112 top = r.top;
1113 }
1114
1115 control->Move(left, top + (diff + 1) / 2);
1116 }
1117
1118 // the max index is the "real" number of buttons - i.e. counting even the
1119 // separators which we added just for aligning the controls
1120 m_nButtons = index;
1121
1122 if ( !IsVertical() )
1123 {
1124 if ( m_maxRows == 0 )
1125 // if not set yet, only one row
1126 SetRows(1);
1127 }
1128 else if ( m_nButtons > 0 ) // vertical non empty toolbar
1129 {
1130 // if not set yet, have one column
1131 m_maxRows = 1;
1132 SetRows(m_nButtons);
1133 }
1134
1135 InvalidateBestSize();
1136 UpdateSize();
1137
1138 return true;
1139 }
1140
1141 // ----------------------------------------------------------------------------
1142 // message handlers
1143 // ----------------------------------------------------------------------------
1144
1145 bool wxToolBar::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD id)
1146 {
1147 wxToolBarToolBase *tool = FindById((int)id);
1148 if ( !tool )
1149 return false;
1150
1151 bool toggled = false; // just to suppress warnings
1152
1153 LRESULT state = ::SendMessage(GetHwnd(), TB_GETSTATE, id, 0);
1154
1155 if ( tool->CanBeToggled() )
1156 {
1157 toggled = (state & TBSTATE_CHECKED) != 0;
1158
1159 // ignore the event when a radio button is released, as this doesn't
1160 // seem to happen at all, and is handled otherwise
1161 if ( tool->GetKind() == wxITEM_RADIO && !toggled )
1162 return true;
1163
1164 tool->Toggle(toggled);
1165 UnToggleRadioGroup(tool);
1166 }
1167
1168 // Without the two lines of code below, if the toolbar was repainted during
1169 // OnLeftClick(), then it could end up without the tool bitmap temporarily
1170 // (see http://lists.nongnu.org/archive/html/lmi/2008-10/msg00014.html).
1171 // The Update() call bellow ensures that this won't happen, by repainting
1172 // invalidated areas of the toolbar immediately.
1173 //
1174 // To complicate matters, the tool would be drawn in depressed state (this
1175 // code is called when mouse button is released, not pressed). That's not
1176 // ideal, having the tool pressed for the duration of OnLeftClick()
1177 // provides the user with useful visual clue that the app is busy reacting
1178 // to the event. So we manually put the tool into pressed state, handle the
1179 // event and then finally restore tool's original state.
1180 ::SendMessage(GetHwnd(), TB_SETSTATE, id, MAKELONG(state | TBSTATE_PRESSED, 0));
1181 Update();
1182
1183 bool allowLeftClick = OnLeftClick((int)id, toggled);
1184
1185 // Restore the unpressed state. Enabled/toggled state might have been
1186 // changed since so take care of it.
1187 if (tool->IsEnabled())
1188 state |= TBSTATE_ENABLED;
1189 else
1190 state &= ~TBSTATE_ENABLED;
1191 if (tool->IsToggled())
1192 state |= TBSTATE_CHECKED;
1193 else
1194 state &= ~TBSTATE_CHECKED;
1195 ::SendMessage(GetHwnd(), TB_SETSTATE, id, MAKELONG(state, 0));
1196
1197 // OnLeftClick() can veto the button state change - for buttons which
1198 // may be toggled only, of couse
1199 if ( !allowLeftClick && tool->CanBeToggled() )
1200 {
1201 // revert back
1202 tool->Toggle(!toggled);
1203
1204 ::SendMessage(GetHwnd(), TB_CHECKBUTTON, id, MAKELONG(!toggled, 0));
1205 }
1206
1207 return true;
1208 }
1209
1210 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl),
1211 WXLPARAM lParam,
1212 WXLPARAM *WXUNUSED(result))
1213 {
1214 if( !HasFlag(wxTB_NO_TOOLTIPS) )
1215 {
1216 #if wxUSE_TOOLTIPS
1217 // First check if this applies to us
1218 NMHDR *hdr = (NMHDR *)lParam;
1219
1220 // the tooltips control created by the toolbar is sometimes Unicode, even
1221 // in an ANSI application - this seems to be a bug in comctl32.dll v5
1222 UINT code = hdr->code;
1223 if ( (code != (UINT) TTN_NEEDTEXTA) && (code != (UINT) TTN_NEEDTEXTW) )
1224 return false;
1225
1226 HWND toolTipWnd = (HWND)::SendMessage(GetHwnd(), TB_GETTOOLTIPS, 0, 0);
1227 if ( toolTipWnd != hdr->hwndFrom )
1228 return false;
1229
1230 LPTOOLTIPTEXT ttText = (LPTOOLTIPTEXT)lParam;
1231 int id = (int)ttText->hdr.idFrom;
1232
1233 wxToolBarToolBase *tool = FindById(id);
1234 if ( tool )
1235 return HandleTooltipNotify(code, lParam, tool->GetShortHelp());
1236 #else
1237 wxUnusedVar(lParam);
1238 #endif
1239 }
1240
1241 return false;
1242 }
1243
1244 // ----------------------------------------------------------------------------
1245 // toolbar geometry
1246 // ----------------------------------------------------------------------------
1247
1248 void wxToolBar::SetToolBitmapSize(const wxSize& size)
1249 {
1250 wxToolBarBase::SetToolBitmapSize(size);
1251
1252 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE, 0, MAKELONG(size.x, size.y));
1253 }
1254
1255 void wxToolBar::SetRows(int nRows)
1256 {
1257 if ( nRows == m_maxRows )
1258 {
1259 // avoid resizing the frame uselessly
1260 return;
1261 }
1262
1263 // TRUE in wParam means to create at least as many rows, FALSE -
1264 // at most as many
1265 RECT rect;
1266 ::SendMessage(GetHwnd(), TB_SETROWS,
1267 MAKEWPARAM(nRows, !(GetWindowStyle() & wxTB_VERTICAL)),
1268 (LPARAM) &rect);
1269
1270 m_maxRows = nRows;
1271
1272 UpdateSize();
1273 }
1274
1275 // The button size is bigger than the bitmap size
1276 wxSize wxToolBar::GetToolSize() const
1277 {
1278 // TB_GETBUTTONSIZE is supported from version 4.70
1279 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
1280 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \
1281 && !defined (__DIGITALMARS__)
1282 if ( wxApp::GetComCtl32Version() >= 470 )
1283 {
1284 DWORD dw = ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE, 0, 0);
1285
1286 return wxSize(LOWORD(dw), HIWORD(dw));
1287 }
1288 else
1289 #endif // comctl32.dll 4.70+
1290 {
1291 // defaults
1292 return wxSize(m_defaultWidth + 8, m_defaultHeight + 7);
1293 }
1294 }
1295
1296 static
1297 wxToolBarToolBase *GetItemSkippingDummySpacers(const wxToolBarToolsList& tools,
1298 size_t index )
1299 {
1300 wxToolBarToolsList::compatibility_iterator current = tools.GetFirst();
1301
1302 for ( ; current ; current = current->GetNext() )
1303 {
1304 if ( index == 0 )
1305 return current->GetData();
1306
1307 wxToolBarTool *tool = (wxToolBarTool *)current->GetData();
1308 size_t separators = tool->GetSeparatorsCount();
1309
1310 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1311 // otherwise, skip as many items as the separator count, plus the
1312 // control itself
1313 index -= separators ? separators + 1 : 1;
1314 }
1315
1316 return 0;
1317 }
1318
1319 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
1320 {
1321 POINT pt;
1322 pt.x = x;
1323 pt.y = y;
1324 int index = (int)::SendMessage(GetHwnd(), TB_HITTEST, 0, (LPARAM)&pt);
1325
1326 // MBN: when the point ( x, y ) is close to the toolbar border
1327 // TB_HITTEST returns m_nButtons ( not -1 )
1328 if ( index < 0 || (size_t)index >= m_nButtons )
1329 // it's a separator or there is no tool at all there
1330 return (wxToolBarToolBase *)NULL;
1331
1332 // when TB_SETBUTTONINFO is available (both during compile- and run-time),
1333 // we don't use the dummy separators hack
1334 #ifdef TB_SETBUTTONINFO
1335 if ( wxApp::GetComCtl32Version() >= 471 )
1336 {
1337 return m_tools.Item((size_t)index)->GetData();
1338 }
1339 else
1340 #endif // TB_SETBUTTONINFO
1341 {
1342 return GetItemSkippingDummySpacers( m_tools, (size_t) index );
1343 }
1344 }
1345
1346 void wxToolBar::UpdateSize()
1347 {
1348 wxPoint pos = GetPosition();
1349 ::SendMessage(GetHwnd(), TB_AUTOSIZE, 0, 0);
1350 if (pos != GetPosition())
1351 Move(pos);
1352
1353 // In case Realize is called after the initial display (IOW the programmer
1354 // may have rebuilt the toolbar) give the frame the option of resizing the
1355 // toolbar to full width again, but only if the parent is a frame and the
1356 // toolbar is managed by the frame. Otherwise assume that some other
1357 // layout mechanism is controlling the toolbar size and leave it alone.
1358 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
1359 if ( frame && frame->GetToolBar() == this )
1360 {
1361 frame->SendSizeEvent();
1362 }
1363 }
1364
1365 // ----------------------------------------------------------------------------
1366 // toolbar styles
1367 // ---------------------------------------------------------------------------
1368
1369 // get the TBSTYLE of the given toolbar window
1370 long wxToolBar::GetMSWToolbarStyle() const
1371 {
1372 return ::SendMessage(GetHwnd(), TB_GETSTYLE, 0, 0L);
1373 }
1374
1375 void wxToolBar::SetWindowStyleFlag(long style)
1376 {
1377 // the style bits whose changes force us to recreate the toolbar
1378 static const long MASK_NEEDS_RECREATE = wxTB_TEXT | wxTB_NOICONS;
1379
1380 const long styleOld = GetWindowStyle();
1381
1382 wxToolBarBase::SetWindowStyleFlag(style);
1383
1384 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1385 // also fatal as we'd then try to recreate the toolbar when it's just being
1386 // created
1387 if ( GetToolsCount() &&
1388 (style & MASK_NEEDS_RECREATE) != (styleOld & MASK_NEEDS_RECREATE) )
1389 {
1390 // to remove the text labels, simply re-realizing the toolbar is enough
1391 // but I don't know of any way to add the text to an existing toolbar
1392 // other than by recreating it entirely
1393 Recreate();
1394 }
1395 }
1396
1397 // ----------------------------------------------------------------------------
1398 // tool state
1399 // ----------------------------------------------------------------------------
1400
1401 void wxToolBar::DoEnableTool(wxToolBarToolBase *tool, bool enable)
1402 {
1403 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON,
1404 (WPARAM)tool->GetId(), (LPARAM)MAKELONG(enable, 0));
1405 }
1406
1407 void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool toggle)
1408 {
1409 ::SendMessage(GetHwnd(), TB_CHECKBUTTON,
1410 (WPARAM)tool->GetId(), (LPARAM)MAKELONG(toggle, 0));
1411 }
1412
1413 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
1414 {
1415 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1416 // without, so we really need to delete the button and recreate it here
1417 wxFAIL_MSG( _T("not implemented") );
1418 }
1419
1420 void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
1421 {
1422 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id));
1423 if ( tool )
1424 {
1425 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1426
1427 tool->SetNormalBitmap(bitmap);
1428 Realize();
1429 }
1430 }
1431
1432 void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
1433 {
1434 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id));
1435 if ( tool )
1436 {
1437 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1438
1439 tool->SetDisabledBitmap(bitmap);
1440 Realize();
1441 }
1442 }
1443
1444 // ----------------------------------------------------------------------------
1445 // event handlers
1446 // ----------------------------------------------------------------------------
1447
1448 // Responds to colour changes, and passes event on to children.
1449 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent& event)
1450 {
1451 wxRGBToColour(m_backgroundColour, ::GetSysColor(COLOR_BTNFACE));
1452
1453 // Remap the buttons
1454 Realize();
1455
1456 // Relayout the toolbar
1457 int nrows = m_maxRows;
1458 m_maxRows = 0; // otherwise SetRows() wouldn't do anything
1459 SetRows(nrows);
1460
1461 Refresh();
1462
1463 // let the event propagate further
1464 event.Skip();
1465 }
1466
1467 void wxToolBar::OnMouseEvent(wxMouseEvent& event)
1468 {
1469 if (event.Leaving() && m_pInTool)
1470 {
1471 OnMouseEnter( -1 );
1472 event.Skip();
1473 return;
1474 }
1475
1476 if ( event.RightDown() )
1477 {
1478 // find the tool under the mouse
1479 wxCoord x = 0, y = 0;
1480 event.GetPosition(&x, &y);
1481
1482 wxToolBarToolBase *tool = FindToolForPosition(x, y);
1483 OnRightClick(tool ? tool->GetId() : -1, x, y);
1484 }
1485 else
1486 {
1487 event.Skip();
1488 }
1489 }
1490
1491 // This handler is required to allow the toolbar to be set to a non-default
1492 // colour: for example, when it must blend in with a notebook page.
1493 void wxToolBar::OnEraseBackground(wxEraseEvent& event)
1494 {
1495 RECT rect = wxGetClientRect(GetHwnd());
1496 HDC hdc = GetHdcOf((*event.GetDC()));
1497
1498 int majorVersion, minorVersion;
1499 wxGetOsVersion(& majorVersion, & minorVersion);
1500
1501 #if wxUSE_UXTHEME
1502 // we may need to draw themed colour so that we appear correctly on
1503 // e.g. notebook page under XP with themes but only do it if the parent
1504 // draws themed background itself
1505 if ( !UseBgCol() && !GetParent()->UseBgCol() )
1506 {
1507 wxUxThemeEngine *theme = wxUxThemeEngine::GetIfActive();
1508 if ( theme )
1509 {
1510 HRESULT
1511 hr = theme->DrawThemeParentBackground(GetHwnd(), hdc, &rect);
1512 if ( hr == S_OK )
1513 return;
1514
1515 // it can also return S_FALSE which seems to simply say that it
1516 // didn't draw anything but no error really occurred
1517 if ( FAILED(hr) )
1518 wxLogApiError(_T("DrawThemeParentBackground(toolbar)"), hr);
1519 }
1520 }
1521
1522 // Only draw a rebar theme on Vista, since it doesn't jive so well with XP
1523 if ( !UseBgCol() && majorVersion >= 6 )
1524 {
1525 wxUxThemeEngine *theme = wxUxThemeEngine::GetIfActive();
1526 if ( theme )
1527 {
1528 wxUxThemeHandle hTheme(this, L"REBAR");
1529
1530 RECT r;
1531 wxRect rect = GetClientRect();
1532 wxCopyRectToRECT(rect, r);
1533
1534 HRESULT hr = theme->DrawThemeBackground(hTheme, hdc, 0, 0, & r, NULL);
1535 if ( hr == S_OK )
1536 return;
1537
1538 // it can also return S_FALSE which seems to simply say that it
1539 // didn't draw anything but no error really occurred
1540 if ( FAILED(hr) )
1541 wxLogApiError(_T("DrawThemeBackground(toolbar)"), hr);
1542 }
1543 }
1544
1545 #endif // wxUSE_UXTHEME
1546
1547 if ( UseBgCol() || (GetMSWToolbarStyle() & TBSTYLE_TRANSPARENT) )
1548 {
1549 // do draw our background
1550 //
1551 // notice that this 'dumb' implementation may cause flicker for some of
1552 // the controls in which case they should intercept wxEraseEvent and
1553 // process it themselves somehow
1554 AutoHBRUSH hBrush(wxColourToRGB(GetBackgroundColour()));
1555
1556 wxCHANGE_HDC_MAP_MODE(hdc, MM_TEXT);
1557 ::FillRect(hdc, &rect, hBrush);
1558 }
1559 else // we have no non default background colour
1560 {
1561 // let the system do it for us
1562 event.Skip();
1563 }
1564 }
1565
1566 bool wxToolBar::HandleSize(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam)
1567 {
1568 // calculate our minor dimension ourselves - we're confusing the standard
1569 // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks
1570 RECT r;
1571 if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT, 0, (LPARAM)&r) )
1572 {
1573 int w, h;
1574
1575 if ( IsVertical() )
1576 {
1577 w = r.right - r.left;
1578 if ( m_maxRows )
1579 {
1580 w *= (m_nButtons + m_maxRows - 1)/m_maxRows;
1581 }
1582 h = HIWORD(lParam);
1583 }
1584 else
1585 {
1586 w = LOWORD(lParam);
1587 if (HasFlag( wxTB_FLAT ))
1588 h = r.bottom - r.top - 3;
1589 else
1590 h = r.bottom - r.top;
1591 if ( m_maxRows )
1592 {
1593 // FIXME: hardcoded separator line height...
1594 h += HasFlag(wxTB_NODIVIDER) ? 4 : 6;
1595 h *= m_maxRows;
1596 }
1597 }
1598
1599 if ( MAKELPARAM(w, h) != lParam )
1600 {
1601 // size really changed
1602 SetSize(w, h);
1603 }
1604
1605 // message processed
1606 return true;
1607 }
1608
1609 return false;
1610 }
1611
1612 bool wxToolBar::HandlePaint(WXWPARAM wParam, WXLPARAM lParam)
1613 {
1614 // erase any dummy separators which were used
1615 // for aligning the controls if any here
1616
1617 // first of all, are there any controls at all?
1618 wxToolBarToolsList::compatibility_iterator node;
1619 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1620 {
1621 if ( node->GetData()->IsControl() )
1622 break;
1623 }
1624
1625 if ( !node )
1626 // no controls, nothing to erase
1627 return false;
1628
1629 wxSize clientSize = GetClientSize();
1630 int majorVersion, minorVersion;
1631 wxGetOsVersion(& majorVersion, & minorVersion);
1632
1633 // prepare the DC on which we'll be drawing
1634 // prepare the DC on which we'll be drawing
1635 wxClientDC dc(this);
1636 dc.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID));
1637 dc.SetPen(*wxTRANSPARENT_PEN);
1638
1639 RECT r;
1640 if ( !::GetUpdateRect(GetHwnd(), &r, FALSE) )
1641 // nothing to redraw anyhow
1642 return false;
1643
1644 wxRect rectUpdate;
1645 wxCopyRECTToRect(r, rectUpdate);
1646
1647 dc.SetClippingRegion(rectUpdate);
1648
1649 // draw the toolbar tools, separators &c normally
1650 wxControl::MSWWindowProc(WM_PAINT, wParam, lParam);
1651
1652 // for each control in the toolbar find all the separators intersecting it
1653 // and erase them
1654 //
1655 // NB: this is really the only way to do it as we don't know if a separator
1656 // corresponds to a control (i.e. is a dummy one) or a real one
1657 // otherwise
1658 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1659 {
1660 wxToolBarToolBase *tool = node->GetData();
1661 if ( tool->IsControl() )
1662 {
1663 // get the control rect in our client coords
1664 wxControl *control = tool->GetControl();
1665 wxRect rectCtrl = control->GetRect();
1666
1667 // iterate over all buttons
1668 TBBUTTON tbb;
1669 int count = ::SendMessage(GetHwnd(), TB_BUTTONCOUNT, 0, 0);
1670 for ( int n = 0; n < count; n++ )
1671 {
1672 // is it a separator?
1673 if ( !::SendMessage(GetHwnd(), TB_GETBUTTON,
1674 n, (LPARAM)&tbb) )
1675 {
1676 wxLogDebug(_T("TB_GETBUTTON failed?"));
1677
1678 continue;
1679 }
1680
1681 if ( tbb.fsStyle != TBSTYLE_SEP )
1682 continue;
1683
1684 // get the bounding rect of the separator
1685 RECT r;
1686 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT,
1687 n, (LPARAM)&r) )
1688 {
1689 wxLogDebug(_T("TB_GETITEMRECT failed?"));
1690
1691 continue;
1692 }
1693
1694 // does it intersect the control?
1695 wxRect rectItem;
1696 wxCopyRECTToRect(r, rectItem);
1697 if ( rectCtrl.Intersects(rectItem) )
1698 {
1699 // yes, do erase it!
1700
1701 bool haveRefreshed = false;
1702
1703 #if wxUSE_UXTHEME
1704 if ( !UseBgCol() && !GetParent()->UseBgCol() )
1705 {
1706 // Don't use DrawThemeBackground
1707 }
1708 else if (!UseBgCol() && majorVersion >= 6 )
1709 {
1710 wxUxThemeEngine *theme = wxUxThemeEngine::GetIfActive();
1711 if ( theme )
1712 {
1713 wxUxThemeHandle hTheme(this, L"REBAR");
1714
1715 RECT clipRect = r;
1716
1717 // Draw the whole background since the pattern may be position sensitive;
1718 // but clip it to the area of interest.
1719 r.left = 0;
1720 r.right = clientSize.x;
1721 r.top = 0;
1722 r.bottom = clientSize.y;
1723
1724 HRESULT hr = theme->DrawThemeBackground(hTheme, (HDC) dc.GetHDC(), 0, 0, & r, & clipRect);
1725 if ( hr == S_OK )
1726 haveRefreshed = true;
1727 }
1728 }
1729 #endif
1730
1731 if (!haveRefreshed)
1732 dc.DrawRectangle(rectItem);
1733
1734 // Necessary in case we use a no-paint-on-size
1735 // style in the parent: the controls can disappear
1736 control->Refresh(false);
1737 }
1738 }
1739 }
1740 }
1741
1742 return true;
1743 }
1744
1745 void wxToolBar::HandleMouseMove(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam)
1746 {
1747 wxCoord x = GET_X_LPARAM(lParam),
1748 y = GET_Y_LPARAM(lParam);
1749 wxToolBarToolBase* tool = FindToolForPosition( x, y );
1750
1751 // cursor left current tool
1752 if ( tool != m_pInTool && !tool )
1753 {
1754 m_pInTool = 0;
1755 OnMouseEnter( -1 );
1756 }
1757
1758 // cursor entered a tool
1759 if ( tool != m_pInTool && tool )
1760 {
1761 m_pInTool = tool;
1762 OnMouseEnter( tool->GetId() );
1763 }
1764 }
1765
1766 WXLRESULT wxToolBar::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
1767 {
1768 switch ( nMsg )
1769 {
1770 case WM_MOUSEMOVE:
1771 // we don't handle mouse moves, so always pass the message to
1772 // wxControl::MSWWindowProc (HandleMouseMove just calls OnMouseEnter)
1773 HandleMouseMove(wParam, lParam);
1774 break;
1775
1776 case WM_SIZE:
1777 if ( HandleSize(wParam, lParam) )
1778 return 0;
1779 break;
1780
1781 #ifndef __WXWINCE__
1782 case WM_PAINT:
1783 if ( HandlePaint(wParam, lParam) )
1784 return 0;
1785 #endif
1786
1787 default:
1788 break;
1789 }
1790
1791 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
1792 }
1793
1794 // ----------------------------------------------------------------------------
1795 // private functions
1796 // ----------------------------------------------------------------------------
1797
1798 #ifdef wxREMAP_BUTTON_COLOURS
1799
1800 WXHBITMAP wxToolBar::MapBitmap(WXHBITMAP bitmap, int width, int height)
1801 {
1802 MemoryHDC hdcMem;
1803
1804 if ( !hdcMem )
1805 {
1806 wxLogLastError(_T("CreateCompatibleDC"));
1807
1808 return bitmap;
1809 }
1810
1811 SelectInHDC bmpInHDC(hdcMem, (HBITMAP)bitmap);
1812
1813 if ( !bmpInHDC )
1814 {
1815 wxLogLastError(_T("SelectObject"));
1816
1817 return bitmap;
1818 }
1819
1820 wxCOLORMAP *cmap = wxGetStdColourMap();
1821
1822 for ( int i = 0; i < width; i++ )
1823 {
1824 for ( int j = 0; j < height; j++ )
1825 {
1826 COLORREF pixel = ::GetPixel(hdcMem, i, j);
1827
1828 for ( size_t k = 0; k < wxSTD_COL_MAX; k++ )
1829 {
1830 COLORREF col = cmap[k].from;
1831 if ( abs(GetRValue(pixel) - GetRValue(col)) < 10 &&
1832 abs(GetGValue(pixel) - GetGValue(col)) < 10 &&
1833 abs(GetBValue(pixel) - GetBValue(col)) < 10 )
1834 {
1835 if ( cmap[k].to != pixel )
1836 ::SetPixel(hdcMem, i, j, cmap[k].to);
1837 break;
1838 }
1839 }
1840 }
1841 }
1842
1843 return bitmap;
1844 }
1845
1846 #endif // wxREMAP_BUTTON_COLOURS
1847
1848 #endif // wxUSE_TOOLBAR

  ViewVC Help
Powered by ViewVC 1.1.22