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