1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/tbar95.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "tbar95.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
35 #include "wx/dynarray.h"
36 #include "wx/settings.h"
37 #include "wx/bitmap.h"
38 #include "wx/dcmemory.h"
39 #include "wx/control.h"
42 #if wxUSE_TOOLBAR && wxUSE_TOOLBAR_NATIVE && (!defined(_WIN32_WCE) || (_WIN32_WCE >= 400 && !defined(__POCKETPC__) && !defined(__SMARTPHONE__)))
44 #include "wx/toolbar.h"
45 #include "wx/sysopt.h"
47 #include "wx/msw/private.h"
49 // include <commctrl.h> "properly"
50 #include "wx/msw/wrapcctl.h"
52 #include "wx/app.h" // for GetComCtl32Version
54 // ----------------------------------------------------------------------------
55 // conditional compilation
56 // ----------------------------------------------------------------------------
58 // wxWidgets previously always considered that toolbar buttons have light grey
59 // (0xc0c0c0) background and so ignored any bitmap masks - however, this
60 // doesn't work with XPMs which then appear to have black background. To make
61 // this work, we must respect the bitmap masks - which we do now. This should
62 // be ok in any case, but to restore 100% compatible with the old version
63 // behaviour, you can set this to 0.
64 #define USE_BITMAP_MASKS 1
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 // these standard constants are not always defined in compilers headers
74 #define TBSTYLE_LIST 0x1000
75 #define TBSTYLE_FLAT 0x0800
78 #ifndef TBSTYLE_TRANSPARENT
79 #define TBSTYLE_TRANSPARENT 0x8000
82 #ifndef TBSTYLE_TOOLTIPS
83 #define TBSTYLE_TOOLTIPS 0x0100
88 #define TB_SETSTYLE (WM_USER + 56)
89 #define TB_GETSTYLE (WM_USER + 57)
93 #define TB_HITTEST (WM_USER + 69)
96 // these values correspond to those used by comctl32.dll
97 #define DEFAULTBITMAPX 16
98 #define DEFAULTBITMAPY 15
99 #define DEFAULTBUTTONX 24
100 #define DEFAULTBUTTONY 24
101 #define DEFAULTBARHEIGHT 27
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
119 style ( wxNO_BORDER | wxTB_HORIZONTAL)
128 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
129 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent
)
130 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged
)
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 class wxToolBarTool
: public wxToolBarToolBase
140 wxToolBarTool(wxToolBar
*tbar
,
142 const wxString
& label
,
143 const wxBitmap
& bmpNormal
,
144 const wxBitmap
& bmpDisabled
,
146 wxObject
*clientData
,
147 const wxString
& shortHelp
,
148 const wxString
& longHelp
)
149 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
150 clientData
, shortHelp
, longHelp
)
155 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
156 : wxToolBarToolBase(tbar
, control
)
161 virtual void SetLabel(const wxString
& label
)
163 if ( label
== m_label
)
166 wxToolBarToolBase::SetLabel(label
);
168 // we need to update the label shown in the toolbar because it has a
169 // pointer to the internal buffer of the old label
171 // TODO: use TB_SETBUTTONINFO
174 // set/get the number of separators which we use to cover the space used by
175 // a control in the toolbar
176 void SetSeparatorsCount(size_t count
) { m_nSepCount
= count
; }
177 size_t GetSeparatorsCount() const { return m_nSepCount
; }
182 DECLARE_NO_COPY_CLASS(wxToolBarTool
)
186 // ============================================================================
188 // ============================================================================
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
194 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
195 const wxString
& label
,
196 const wxBitmap
& bmpNormal
,
197 const wxBitmap
& bmpDisabled
,
199 wxObject
*clientData
,
200 const wxString
& shortHelp
,
201 const wxString
& longHelp
)
203 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
204 clientData
, shortHelp
, longHelp
);
207 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
209 return new wxToolBarTool(this, control
);
212 // ----------------------------------------------------------------------------
213 // wxToolBar construction
214 // ----------------------------------------------------------------------------
216 void wxToolBar::Init()
222 m_defaultWidth
= DEFAULTBITMAPX
;
223 m_defaultHeight
= DEFAULTBITMAPY
;
228 bool wxToolBar::Create(wxWindow
*parent
,
233 const wxString
& name
)
235 // common initialisation
236 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
239 // MSW-specific initialisation
240 if ( !MSWCreateToolbar(pos
, size
) )
243 wxSetCCUnicodeFormat(GetHwnd());
245 // set up the colors and fonts
246 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR
));
247 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
252 bool wxToolBar::MSWCreateToolbar(const wxPoint
& pos
, const wxSize
& size
)
254 if ( !MSWCreateControl(TOOLBARCLASSNAME
, wxEmptyString
, pos
, size
) )
257 // toolbar-specific post initialisation
258 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
263 void wxToolBar::Recreate()
265 const HWND hwndOld
= GetHwnd();
268 // we haven't been created yet, no need to recreate
272 // get the position and size before unsubclassing the old toolbar
273 const wxPoint pos
= GetPosition();
274 const wxSize size
= GetSize();
278 if ( !MSWCreateToolbar(pos
, size
) )
281 wxFAIL_MSG( _T("recreating the toolbar failed") );
286 // reparent all our children under the new toolbar
287 for ( wxWindowList::compatibility_iterator node
= m_children
.GetFirst();
289 node
= node
->GetNext() )
291 wxWindow
*win
= node
->GetData();
292 if ( !win
->IsTopLevel() )
293 ::SetParent(GetHwndOf(win
), GetHwnd());
296 // only destroy the old toolbar now -- after all the children had been
298 ::DestroyWindow(hwndOld
);
300 // it is for the old bitmap control and can't be used with the new one
303 ::DeleteObject((HBITMAP
) m_hBitmap
);
311 wxToolBar::~wxToolBar()
313 // we must refresh the frame size when the toolbar is deleted but the frame
314 // is not - otherwise toolbar leaves a hole in the place it used to occupy
315 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
316 if ( frame
&& !frame
->IsBeingDeleted() )
318 frame
->SendSizeEvent();
323 ::DeleteObject((HBITMAP
) m_hBitmap
);
327 wxSize
wxToolBar::DoGetBestSize() const
329 wxSize sizeBest
= GetToolSize();
330 sizeBest
.x
*= GetToolsCount();
332 // reverse horz and vertical components if necessary
333 return HasFlag(wxTB_VERTICAL
) ? wxSize(sizeBest
.y
, sizeBest
.x
) : sizeBest
;
336 WXDWORD
wxToolBar::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
338 // toolbars never have border, giving one to them results in broken
340 WXDWORD msStyle
= wxControl::MSWGetStyle
342 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
345 // always include this one, it never hurts and setting it later only if we
346 // do have tooltips wouldn't work
347 msStyle
|= TBSTYLE_TOOLTIPS
;
349 if ( style
& (wxTB_FLAT
| wxTB_HORZ_LAYOUT
) )
351 // static as it doesn't change during the program lifetime
352 static int s_verComCtl
= wxTheApp
->GetComCtl32Version();
354 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
355 // style with 6.00 (part of Windows XP) leads to the toolbar with
356 // incorrect background colour - and not using it still results in the
357 // correct (flat) toolbar, so don't use it there
358 if ( s_verComCtl
> 400 && s_verComCtl
< 600 )
360 msStyle
|= TBSTYLE_FLAT
| TBSTYLE_TRANSPARENT
;
363 if ( s_verComCtl
>= 470 && style
& wxTB_HORZ_LAYOUT
)
365 msStyle
|= TBSTYLE_LIST
;
369 if ( style
& wxTB_NODIVIDER
)
370 msStyle
|= CCS_NODIVIDER
;
372 if ( style
& wxTB_NOALIGN
)
373 msStyle
|= CCS_NOPARENTALIGN
;
375 if ( style
& wxTB_VERTICAL
)
381 // ----------------------------------------------------------------------------
382 // adding/removing tools
383 // ----------------------------------------------------------------------------
385 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
387 // nothing special to do here - we really create the toolbar buttons in
391 InvalidateBestSize();
395 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
397 // the main difficulty we have here is with the controls in the toolbars:
398 // as we (sometimes) use several separators to cover up the space used by
399 // them, the indices are not the same for us and the toolbar
401 // first determine the position of the first button to delete: it may be
402 // different from pos if we use several separators to cover the space used
404 wxToolBarToolsList::compatibility_iterator node
;
405 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
407 wxToolBarToolBase
*tool2
= node
->GetData();
410 // let node point to the next node in the list
411 node
= node
->GetNext();
416 if ( tool2
->IsControl() )
418 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount() - 1;
422 // now determine the number of buttons to delete and the area taken by them
423 size_t nButtonsToDelete
= 1;
425 // get the size of the button we're going to delete
427 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
, pos
, (LPARAM
)&r
) )
429 wxLogLastError(_T("TB_GETITEMRECT"));
432 int width
= r
.right
- r
.left
;
434 if ( tool
->IsControl() )
436 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
438 width
*= nButtonsToDelete
;
441 // do delete all buttons
442 m_nButtons
-= nButtonsToDelete
;
443 while ( nButtonsToDelete
-- > 0 )
445 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
447 wxLogLastError(wxT("TB_DELETEBUTTON"));
455 // and finally reposition all the controls after this button (the toolbar
456 // takes care of all normal items)
457 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
459 wxToolBarToolBase
*tool2
= node
->GetData();
460 if ( tool2
->IsControl() )
463 wxControl
*control
= tool2
->GetControl();
464 control
->GetPosition(&x
, NULL
);
465 control
->Move(x
- width
, -1);
469 InvalidateBestSize();
473 bool wxToolBar::Realize()
475 const size_t nTools
= GetToolsCount();
482 const bool isVertical
= HasFlag(wxTB_VERTICAL
);
484 // delete all old buttons, if any
485 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
487 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
489 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
493 // First, add the bitmap: we use one bitmap for all toolbar buttons
494 // ----------------------------------------------------------------
496 wxToolBarToolsList::compatibility_iterator node
;
500 if ( HasFlag(wxTB_NOICONS
) )
502 // no icons, don't leave space for them
506 else // do show icons
508 // if we already have a bitmap, we'll replace the existing one --
509 // otherwise we'll install a new one
510 HBITMAP oldToolBarBitmap
= (HBITMAP
)m_hBitmap
;
512 sizeBmp
.x
= m_defaultWidth
;
513 sizeBmp
.y
= m_defaultHeight
;
515 const wxCoord totalBitmapWidth
= m_defaultWidth
* nTools
,
516 totalBitmapHeight
= m_defaultHeight
;
518 // Create a bitmap and copy all the tool bitmaps to it
520 wxMemoryDC dcAllButtons
;
521 wxBitmap
bitmap(totalBitmapWidth
, totalBitmapHeight
);
522 dcAllButtons
.SelectObject(bitmap
);
523 dcAllButtons
.SetBackground(*wxLIGHT_GREY_BRUSH
);
524 dcAllButtons
.Clear();
526 m_hBitmap
= bitmap
.GetHBITMAP();
527 HBITMAP hBitmap
= (HBITMAP
)m_hBitmap
;
528 #else // !USE_BITMAP_MASKS
529 HBITMAP hBitmap
= ::CreateCompatibleBitmap(ScreenHDC(),
534 wxLogLastError(_T("CreateCompatibleBitmap"));
539 m_hBitmap
= (WXHBITMAP
)hBitmap
;
542 SelectInHDC
hdcSelector(memoryDC
, hBitmap
);
545 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
547 if (wxSystemOptions::HasOption(wxT("msw.remap")) && wxSystemOptions::GetOptionInt(wxT("msw.remap")) == 0)
550 dcAllButtons
.SelectObject(wxNullBitmap
);
553 // Even if we're not remapping the bitmap
554 // content, we still have to remap the background.
555 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
556 totalBitmapWidth
, totalBitmapHeight
);
559 dcAllButtons
.SelectObject(bitmap
);
563 // the button position
566 // the number of buttons (not separators)
569 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
571 wxToolBarToolBase
*tool
= node
->GetData();
572 if ( tool
->IsButton() )
574 const wxBitmap
& bmp
= tool
->GetNormalBitmap();
577 int xOffset
= wxMax(0, (m_defaultWidth
- bmp
.GetWidth())/2);
578 int yOffset
= wxMax(0, (m_defaultHeight
- bmp
.GetHeight())/2);
580 // notice the last parameter: do use mask
581 dcAllButtons
.DrawBitmap(bmp
, x
+xOffset
, yOffset
, TRUE
);
582 #else // !USE_BITMAP_MASKS
583 SelectInHDC
hdcSelector2(memoryDC2
, GetHbitmapOf(bmp
));
584 if ( !BitBlt(memoryDC
,
585 x
+xOffset
, yOffset
, m_defaultWidth
, m_defaultHeight
,
589 wxLogLastError(wxT("BitBlt"));
591 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
595 wxFAIL_MSG( _T("invalid tool button bitmap") );
598 // still inc width and number of buttons because otherwise the
599 // subsequent buttons will all be shifted which is rather confusing
600 // (and like this you'd see immediately which bitmap was bad)
607 dcAllButtons
.SelectObject(wxNullBitmap
);
609 // don't delete this HBITMAP!
610 bitmap
.SetHBITMAP(0);
611 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
613 if (!wxSystemOptions::HasOption(wxT("msw.remap")) || wxSystemOptions::GetOptionInt(wxT("msw.remap")) == 1)
615 // Map to system colours
616 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
617 totalBitmapWidth
, totalBitmapHeight
);
620 bool addBitmap
= TRUE
;
622 if ( oldToolBarBitmap
)
624 #ifdef TB_REPLACEBITMAP
625 if ( wxTheApp
->GetComCtl32Version() >= 400 )
627 TBREPLACEBITMAP replaceBitmap
;
628 replaceBitmap
.hInstOld
= NULL
;
629 replaceBitmap
.hInstNew
= NULL
;
630 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
631 replaceBitmap
.nIDNew
= (UINT
) hBitmap
;
632 replaceBitmap
.nButtons
= nButtons
;
633 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
634 0, (LPARAM
) &replaceBitmap
) )
636 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
639 ::DeleteObject(oldToolBarBitmap
);
645 #endif // TB_REPLACEBITMAP
647 // we can't replace the old bitmap, so we will add another one
648 // (awfully inefficient, but what else to do?) and shift the bitmap
649 // indices accordingly
652 bitmapId
= m_nButtons
;
656 if ( addBitmap
) // no old bitmap or we can't replace it
658 TBADDBITMAP addBitmap
;
660 addBitmap
.nID
= (UINT
) hBitmap
;
661 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
662 (WPARAM
) nButtons
, (LPARAM
)&addBitmap
) == -1 )
664 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
669 // don't call SetToolBitmapSize() as we don't want to change the values of
670 // m_defaultWidth/Height
671 if ( !::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0,
672 MAKELONG(sizeBmp
.x
, sizeBmp
.y
)) )
674 wxLogLastError(_T("TB_SETBITMAPSIZE"));
677 // Next add the buttons and separators
678 // -----------------------------------
680 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
682 // this array will hold the indices of all controls in the toolbar
683 wxArrayInt controlIds
;
685 bool lastWasRadio
= FALSE
;
687 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
689 wxToolBarToolBase
*tool
= node
->GetData();
691 // don't add separators to the vertical toolbar with old comctl32.dll
692 // versions as they didn't handle this properly
693 if ( isVertical
&& tool
->IsSeparator() &&
694 wxTheApp
->GetComCtl32Version() <= 472 )
700 TBBUTTON
& button
= buttons
[i
];
702 wxZeroMemory(button
);
704 bool isRadio
= FALSE
;
705 switch ( tool
->GetStyle() )
707 case wxTOOL_STYLE_CONTROL
:
708 button
.idCommand
= tool
->GetId();
709 // fall through: create just a separator too
711 case wxTOOL_STYLE_SEPARATOR
:
712 button
.fsState
= TBSTATE_ENABLED
;
713 button
.fsStyle
= TBSTYLE_SEP
;
716 case wxTOOL_STYLE_BUTTON
:
717 if ( !HasFlag(wxTB_NOICONS
) )
718 button
.iBitmap
= bitmapId
;
720 if ( HasFlag(wxTB_TEXT
) )
722 const wxString
& label
= tool
->GetLabel();
723 if ( !label
.empty() )
725 button
.iString
= (int)label
.c_str();
729 button
.idCommand
= tool
->GetId();
731 if ( tool
->IsEnabled() )
732 button
.fsState
|= TBSTATE_ENABLED
;
733 if ( tool
->IsToggled() )
734 button
.fsState
|= TBSTATE_CHECKED
;
736 switch ( tool
->GetKind() )
739 button
.fsStyle
= TBSTYLE_CHECKGROUP
;
743 // the first item in the radio group is checked by
744 // default to be consistent with wxGTK and the menu
746 button
.fsState
|= TBSTATE_CHECKED
;
755 button
.fsStyle
= TBSTYLE_CHECK
;
759 wxFAIL_MSG( _T("unexpected toolbar button kind") );
763 button
.fsStyle
= TBSTYLE_BUTTON
;
770 lastWasRadio
= isRadio
;
775 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
, (WPARAM
)i
, (LPARAM
)buttons
) )
777 wxLogLastError(wxT("TB_ADDBUTTONS"));
782 // Deal with the controls finally
783 // ------------------------------
785 // adjust the controls size to fit nicely in the toolbar
788 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
790 wxToolBarToolBase
*tool
= node
->GetData();
792 // we calculate the running y coord for vertical toolbars so we need to
793 // get the items size for all items but for the horizontal ones we
794 // don't need to deal with the non controls
795 bool isControl
= tool
->IsControl();
796 if ( !isControl
&& !isVertical
)
799 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
800 // latter only appeared in v4.70 of comctl32.dll
802 if ( !SendMessage(GetHwnd(), TB_GETITEMRECT
,
803 index
, (LPARAM
)(LPRECT
)&r
) )
805 wxLogLastError(wxT("TB_GETITEMRECT"));
810 // can only be control if isVertical
811 y
+= r
.bottom
- r
.top
;
816 wxControl
*control
= tool
->GetControl();
818 wxSize size
= control
->GetSize();
820 // the position of the leftmost controls corner
823 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
824 #ifdef TB_SETBUTTONINFO
825 // available in headers, now check whether it is available now
827 if ( wxTheApp
->GetComCtl32Version() >= 471 )
829 // set the (underlying) separators width to be that of the
832 tbbi
.cbSize
= sizeof(tbbi
);
833 tbbi
.dwMask
= TBIF_SIZE
;
835 if ( !SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
836 tool
->GetId(), (LPARAM
)&tbbi
) )
838 // the id is probably invalid?
839 wxLogLastError(wxT("TB_SETBUTTONINFO"));
843 #endif // comctl32.dll 4.71
844 // TB_SETBUTTONINFO unavailable
846 // try adding several separators to fit the controls width
847 int widthSep
= r
.right
- r
.left
;
853 tbb
.fsState
= TBSTATE_ENABLED
;
854 tbb
.fsStyle
= TBSTYLE_SEP
;
856 size_t nSeparators
= size
.x
/ widthSep
;
857 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
859 if ( !SendMessage(GetHwnd(), TB_INSERTBUTTON
,
860 index
, (LPARAM
)&tbb
) )
862 wxLogLastError(wxT("TB_INSERTBUTTON"));
868 // remember the number of separators we used - we'd have to
869 // delete all of them later
870 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
872 // adjust the controls width to exactly cover the separators
873 control
->SetSize((nSeparators
+ 1)*widthSep
, -1);
876 // position the control itself correctly vertically
877 int height
= r
.bottom
- r
.top
;
878 int diff
= height
- size
.y
;
881 // the control is too high, resize to fit
882 control
->SetSize(-1, height
- 2);
893 y
+= height
+ 2*GetMargins().y
;
895 else // horizontal toolbar
903 control
->Move(left
, top
+ (diff
+ 1) / 2);
906 // the max index is the "real" number of buttons - i.e. counting even the
907 // separators which we added just for aligning the controls
912 if ( m_maxRows
== 0 )
914 // if not set yet, only one row
918 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
920 if ( m_maxRows
== 0 )
922 // if not set yet, have one column
927 InvalidateBestSize();
931 // ----------------------------------------------------------------------------
933 // ----------------------------------------------------------------------------
935 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id
)
937 wxToolBarToolBase
*tool
= FindById((int)id
);
941 bool toggled
= false; // just to suppress warnings
943 if ( tool
->CanBeToggled() )
945 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
946 toggled
= (state
& TBSTATE_CHECKED
) != 0;
948 // ignore the event when a radio button is released, as this doesn't seem to
949 // happen at all, and is handled otherwise
950 if ( tool
->GetKind() == wxITEM_RADIO
&& !toggled
)
953 tool
->Toggle(toggled
);
954 UnToggleRadioGroup(tool
);
957 // OnLeftClick() can veto the button state change - for buttons which
958 // may be toggled only, of couse
959 if ( !OnLeftClick((int)id
, toggled
) && tool
->CanBeToggled() )
962 tool
->Toggle(!toggled
);
964 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(toggled
, 0));
970 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
972 WXLPARAM
*WXUNUSED(result
))
975 // First check if this applies to us
976 NMHDR
*hdr
= (NMHDR
*)lParam
;
978 // the tooltips control created by the toolbar is sometimes Unicode, even
979 // in an ANSI application - this seems to be a bug in comctl32.dll v5
980 UINT code
= hdr
->code
;
981 if ( (code
!= (UINT
) TTN_NEEDTEXTA
) && (code
!= (UINT
) TTN_NEEDTEXTW
) )
984 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
985 if ( toolTipWnd
!= hdr
->hwndFrom
)
988 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
989 int id
= (int)ttText
->hdr
.idFrom
;
991 wxToolBarToolBase
*tool
= FindById(id
);
995 return HandleTooltipNotify(code
, lParam
, tool
->GetShortHelp());
1001 // ----------------------------------------------------------------------------
1003 // ----------------------------------------------------------------------------
1005 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1007 wxToolBarBase::SetToolBitmapSize(size
);
1009 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
1012 void wxToolBar::SetRows(int nRows
)
1014 if ( nRows
== m_maxRows
)
1016 // avoid resizing the frame uselessly
1020 // TRUE in wParam means to create at least as many rows, FALSE -
1023 ::SendMessage(GetHwnd(), TB_SETROWS
,
1024 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
1032 // The button size is bigger than the bitmap size
1033 wxSize
wxToolBar::GetToolSize() const
1035 // TB_GETBUTTONSIZE is supported from version 4.70
1036 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
1037 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \
1038 && !defined (__DIGITALMARS__)
1039 if ( wxTheApp
->GetComCtl32Version() >= 470 )
1041 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
1043 return wxSize(LOWORD(dw
), HIWORD(dw
));
1046 #endif // comctl32.dll 4.70+
1049 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
1054 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
1057 wxToolBarToolsList::compatibility_iterator current
= tools
.GetFirst();
1059 for ( ; current
!= 0; current
= current
->GetNext() )
1062 return current
->GetData();
1064 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
1065 size_t separators
= tool
->GetSeparatorsCount();
1067 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1068 // otherwise, skip as many items as the separator count, plus the
1070 index
-= separators
? separators
+ 1 : 1;
1076 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1081 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
1082 // MBN: when the point ( x, y ) is close to the toolbar border
1083 // TB_HITTEST returns m_nButtons ( not -1 )
1084 if ( index
< 0 || (size_t)index
>= m_nButtons
)
1086 // it's a separator or there is no tool at all there
1087 return (wxToolBarToolBase
*)NULL
;
1090 // if comctl32 version < 4.71 wxToolBar95 adds dummy spacers
1091 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
1092 if ( wxTheApp
->GetComCtl32Version() >= 471 )
1094 return m_tools
.Item((size_t)index
)->GetData();
1099 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
1103 void wxToolBar::UpdateSize()
1105 // the toolbar size changed
1106 SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
1108 // we must also refresh the frame after the toolbar size (possibly) changed
1109 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
1112 frame
->SendSizeEvent();
1116 // ----------------------------------------------------------------------------
1118 // ---------------------------------------------------------------------------
1120 void wxToolBar::SetWindowStyleFlag(long style
)
1122 // the style bits whose changes force us to recreate the toolbar
1123 static const long MASK_NEEDS_RECREATE
= wxTB_TEXT
| wxTB_NOICONS
;
1125 const long styleOld
= GetWindowStyle();
1127 wxToolBarBase::SetWindowStyleFlag(style
);
1129 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1130 // also fatal as we'd then try to recreate the toolbar when it's just being
1132 if ( GetToolsCount() &&
1133 (style
& MASK_NEEDS_RECREATE
) != (styleOld
& MASK_NEEDS_RECREATE
) )
1135 // to remove the text labels, simply re-realizing the toolbar is enough
1136 // but I don't know of any way to add the text to an existing toolbar
1137 // other than by recreating it entirely
1142 // ----------------------------------------------------------------------------
1144 // ----------------------------------------------------------------------------
1146 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
1148 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
1149 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
1152 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
1154 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
1155 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
1158 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1160 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1161 // without, so we really need to delete the button and recreate it here
1162 wxFAIL_MSG( _T("not implemented") );
1165 // ----------------------------------------------------------------------------
1167 // ----------------------------------------------------------------------------
1169 // Responds to colour changes, and passes event on to children.
1170 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1172 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
1174 // Remap the buttons
1177 // Relayout the toolbar
1178 int nrows
= m_maxRows
;
1179 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
1184 // let the event propagate further
1188 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
1190 if (event
.Leaving() && m_pInTool
)
1197 if (event
.RightDown())
1199 // For now, we don't have an id. Later we could
1200 // try finding the tool.
1201 OnRightClick((int)-1, event
.GetX(), event
.GetY());
1209 bool wxToolBar::HandleSize(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1211 // calculate our minor dimension ourselves - we're confusing the standard
1212 // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks
1214 if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT
, 0, (LPARAM
)&r
) )
1218 if ( GetWindowStyle() & wxTB_VERTICAL
)
1220 w
= r
.right
- r
.left
;
1223 w
*= (m_nButtons
+ m_maxRows
- 1)/m_maxRows
;
1230 if (HasFlag( wxTB_FLAT
))
1231 h
= r
.bottom
- r
.top
- 3;
1233 h
= r
.bottom
- r
.top
;
1236 // FIXME: hardcoded separator line height...
1237 h
+= HasFlag(wxTB_NODIVIDER
) ? 4 : 6;
1242 if ( MAKELPARAM(w
, h
) != lParam
)
1244 // size really changed
1248 // message processed
1255 bool wxToolBar::HandlePaint(WXWPARAM wParam
, WXLPARAM lParam
)
1257 // erase any dummy separators which we used for aligning the controls if
1260 // first of all, do we have any controls at all?
1261 wxToolBarToolsList::compatibility_iterator node
;
1262 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1264 if ( node
->GetData()->IsControl() )
1270 // no controls, nothing to erase
1274 // prepare the DC on which we'll be drawing
1275 wxClientDC
dc(this);
1276 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
1277 dc
.SetPen(*wxTRANSPARENT_PEN
);
1280 if ( !GetUpdateRect(GetHwnd(), &r
, FALSE
) )
1282 // nothing to redraw anyhow
1287 wxCopyRECTToRect(r
, rectUpdate
);
1289 dc
.SetClippingRegion(rectUpdate
);
1291 // draw the toolbar tools, separators &c normally
1292 wxControl::MSWWindowProc(WM_PAINT
, wParam
, lParam
);
1294 // for each control in the toolbar find all the separators intersecting it
1297 // NB: this is really the only way to do it as we don't know if a separator
1298 // corresponds to a control (i.e. is a dummy one) or a real one
1300 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1302 wxToolBarToolBase
*tool
= node
->GetData();
1303 if ( tool
->IsControl() )
1305 // get the control rect in our client coords
1306 wxControl
*control
= tool
->GetControl();
1307 wxRect rectCtrl
= control
->GetRect();
1309 // iterate over all buttons
1311 int count
= ::SendMessage(GetHwnd(), TB_BUTTONCOUNT
, 0, 0);
1312 for ( int n
= 0; n
< count
; n
++ )
1314 // is it a separator?
1315 if ( !::SendMessage(GetHwnd(), TB_GETBUTTON
,
1318 wxLogDebug(_T("TB_GETBUTTON failed?"));
1323 if ( tbb
.fsStyle
!= TBSTYLE_SEP
)
1326 // get the bounding rect of the separator
1328 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
,
1331 wxLogDebug(_T("TB_GETITEMRECT failed?"));
1336 // does it intersect the control?
1338 wxCopyRECTToRect(r
, rectItem
);
1339 if ( rectCtrl
.Intersects(rectItem
) )
1341 // yes, do erase it!
1342 dc
.DrawRectangle(rectItem
);
1344 // Necessary in case we use a no-paint-on-size
1345 // style in the parent: the controls can disappear
1346 control
->Refresh(FALSE
);
1355 void wxToolBar::HandleMouseMove(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1357 wxCoord x
= GET_X_LPARAM(lParam
),
1358 y
= GET_Y_LPARAM(lParam
);
1359 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1361 // cursor left current tool
1362 if( tool
!= m_pInTool
&& !tool
)
1368 // cursor entered a tool
1369 if( tool
!= m_pInTool
&& tool
)
1372 OnMouseEnter( tool
->GetId() );
1376 WXLRESULT
wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1381 if ( HandleSize(wParam
, lParam
) )
1386 // we don't handle mouse moves, so always pass the message to
1387 // wxControl::MSWWindowProc
1388 HandleMouseMove(wParam
, lParam
);
1392 if ( HandlePaint(wParam
, lParam
) )
1396 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1399 // ----------------------------------------------------------------------------
1400 // private functions
1401 // ----------------------------------------------------------------------------
1403 WXHBITMAP
wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1409 wxLogLastError(_T("CreateCompatibleDC"));
1414 SelectInHDC
bmpInHDC(hdcMem
, (HBITMAP
)bitmap
);
1418 wxLogLastError(_T("SelectObject"));
1423 wxCOLORMAP
*cmap
= wxGetStdColourMap();
1425 for ( int i
= 0; i
< width
; i
++ )
1427 for ( int j
= 0; j
< height
; j
++ )
1429 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1431 for ( size_t k
= 0; k
< wxSTD_COL_MAX
; k
++ )
1433 COLORREF col
= cmap
[k
].from
;
1434 if ( abs(GetRValue(pixel
) - GetRValue(col
)) < 10 &&
1435 abs(GetGValue(pixel
) - GetGValue(col
)) < 10 &&
1436 abs(GetBValue(pixel
) - GetBValue(col
)) < 10 )
1438 ::SetPixel(hdcMem
, i
, j
, cmap
[k
].to
);
1447 // VZ: I leave here my attempts to map the bitmap to the system colours
1448 // faster by using BitBlt() even though it's broken currently - but
1449 // maybe someone else can finish it? It should be faster than iterating
1450 // over all pixels...
1452 MemoryHDC hdcMask
, hdcDst
;
1453 if ( !hdcMask
|| !hdcDst
)
1455 wxLogLastError(_T("CreateCompatibleDC"));
1460 // create the target bitmap
1461 HBITMAP hbmpDst
= ::CreateCompatibleBitmap(hdcDst
, width
, height
);
1464 wxLogLastError(_T("CreateCompatibleBitmap"));
1469 // create the monochrome mask bitmap
1470 HBITMAP hbmpMask
= ::CreateBitmap(width
, height
, 1, 1, 0);
1473 wxLogLastError(_T("CreateBitmap(mono)"));
1475 ::DeleteObject(hbmpDst
);
1480 SelectInHDC
bmpInDst(hdcDst
, hbmpDst
),
1481 bmpInMask(hdcMask
, hbmpMask
);
1484 for ( n
= 0; n
< NUM_OF_MAPPED_COLOURS
; n
++ )
1486 // create the mask for this colour
1487 ::SetBkColor(hdcMem
, ColorMap
[n
].from
);
1488 ::BitBlt(hdcMask
, 0, 0, width
, height
, hdcMem
, 0, 0, SRCCOPY
);
1490 // replace this colour with the target one in the dst bitmap
1491 HBRUSH hbr
= ::CreateSolidBrush(ColorMap
[n
].to
);
1492 HGDIOBJ hbrOld
= ::SelectObject(hdcDst
, hbr
);
1494 ::MaskBlt(hdcDst
, 0, 0, width
, height
,
1497 MAKEROP4(PATCOPY
, SRCCOPY
));
1499 (void)::SelectObject(hdcDst
, hbrOld
);
1500 ::DeleteObject(hbr
);
1503 ::DeleteObject((HBITMAP
)bitmap
);
1505 return (WXHBITMAP
)hbmpDst
;
1509 #endif // wxUSE_TOOLBAR && Win95