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