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