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