1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/tbar95.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
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"
41 #if wxUSE_TOOLBAR && defined(__WIN95__) && wxUSE_TOOLBAR_NATIVE
43 #include "wx/toolbar.h"
45 #if !defined(__GNUWIN32__) && !defined(__SALFORDC__)
49 #include "wx/msw/private.h"
53 #if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
56 #include "wx/msw/gnuwin32/extra.h"
61 #include "wx/msw/dib.h"
62 #include "wx/app.h" // for GetComCtl32Version
64 #if defined(__MWERKS__) && defined(__WXMSW__)
65 // including <windef.h> for max definition doesn't seem
66 // to work using CodeWarrior 6 Windows. So we define it
67 // here. (Otherwise we get a undefined identifier 'max'
68 // later on in this file.) (Added by dimitri@shortcut.nl)
70 # define max(a,b) (((a) > (b)) ? (a) : (b))
75 // ----------------------------------------------------------------------------
76 // conditional compilation
77 // ----------------------------------------------------------------------------
79 // wxWindows previously always considered that toolbar buttons have light grey
80 // (0xc0c0c0) background and so ignored any bitmap masks - however, this
81 // doesn't work with XPMs which then appear to have black background. To make
82 // this work, we must respect the bitmap masks - which we do now. This should
83 // be ok in any case, but to restore 100% compatible with the old version
84 // behaviour, you can set this to 0.
85 #define USE_BITMAP_MASKS 1
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
91 // these standard constants are not always defined in compilers headers
95 #define TBSTYLE_LIST 0x1000
96 #define TBSTYLE_FLAT 0x0800
97 #define TBSTYLE_TRANSPARENT 0x8000
99 // use TBSTYLE_TRANSPARENT if you use TBSTYLE_FLAT
103 #define TB_SETSTYLE (WM_USER + 56)
104 #define TB_GETSTYLE (WM_USER + 57)
108 #define TB_HITTEST (WM_USER + 69)
111 // these values correspond to those used by comctl32.dll
112 #define DEFAULTBITMAPX 16
113 #define DEFAULTBITMAPY 15
114 #define DEFAULTBUTTONX 24
115 #define DEFAULTBUTTONY 24
116 #define DEFAULTBARHEIGHT 27
118 // ----------------------------------------------------------------------------
119 // private function prototypes
120 // ----------------------------------------------------------------------------
122 // adjust toolbar bitmap colours
123 // static void wxMapBitmap(HBITMAP hBitmap, int width, int height);
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxToolBarBase
)
131 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
132 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent
)
133 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged
)
136 // ----------------------------------------------------------------------------
138 // ----------------------------------------------------------------------------
140 class wxToolBarTool
: public wxToolBarToolBase
143 wxToolBarTool(wxToolBar
*tbar
,
145 const wxBitmap
& bitmap1
,
146 const wxBitmap
& bitmap2
,
148 wxObject
*clientData
,
149 const wxString
& shortHelpString
,
150 const wxString
& longHelpString
)
151 : wxToolBarToolBase(tbar
, id
, bitmap1
, bitmap2
, toggle
,
152 clientData
, shortHelpString
, longHelpString
)
157 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
158 : wxToolBarToolBase(tbar
, control
)
163 // set/get the number of separators which we use to cover the space used by
164 // a control in the toolbar
165 void SetSeparatorsCount(size_t count
) { m_nSepCount
= count
; }
166 size_t GetSeparatorsCount() const { return m_nSepCount
; }
173 // ============================================================================
175 // ============================================================================
177 // ----------------------------------------------------------------------------
179 // ----------------------------------------------------------------------------
181 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
182 const wxBitmap
& bitmap1
,
183 const wxBitmap
& bitmap2
,
185 wxObject
*clientData
,
186 const wxString
& shortHelpString
,
187 const wxString
& longHelpString
)
189 return new wxToolBarTool(this, id
, bitmap1
, bitmap2
, toggle
,
190 clientData
, shortHelpString
, longHelpString
);
193 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
195 return new wxToolBarTool(this, control
);
198 // ----------------------------------------------------------------------------
199 // wxToolBar construction
200 // ----------------------------------------------------------------------------
202 void wxToolBar::Init()
208 m_defaultWidth
= DEFAULTBITMAPX
;
209 m_defaultHeight
= DEFAULTBITMAPY
;
214 bool wxToolBar::Create(wxWindow
*parent
,
219 const wxString
& name
)
221 // toolbars never have border, giving one to them results in broken
223 style
&= ~wxBORDER_MASK
;
224 style
|= wxBORDER_NONE
;
226 // common initialisation
227 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
231 DWORD msflags
= 0; // WS_VISIBLE | WS_CHILD always included
233 if ( style
& wxCLIP_SIBLINGS
)
234 msflags
|= WS_CLIPSIBLINGS
;
236 #ifdef TBSTYLE_TOOLTIPS
237 msflags
|= TBSTYLE_TOOLTIPS
;
240 if (style
& wxTB_FLAT
)
242 if (wxTheApp
->GetComCtl32Version() > 400)
243 msflags
|= TBSTYLE_FLAT
;
246 // MSW-specific initialisation
247 if ( !wxControl::MSWCreateControl(TOOLBARCLASSNAME
, msflags
) )
250 // toolbar-specific post initialisation
251 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
253 // set up the colors and fonts
254 wxRGBToColour(m_backgroundColour
, GetSysColor(COLOR_BTNFACE
));
255 m_foregroundColour
= *wxBLACK
;
257 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
268 height
= m_defaultHeight
;
274 SetSize(x
, y
, width
, height
);
279 wxToolBar::~wxToolBar()
281 // we must refresh the frame size when the toolbar is deleted but the frame
282 // is not - otherwise toolbar leaves a hole in the place it used to occupy
283 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
284 if ( frame
&& !frame
->IsBeingDeleted() )
286 frame
->SendSizeEvent();
291 ::DeleteObject((HBITMAP
) m_hBitmap
);
295 // ----------------------------------------------------------------------------
296 // adding/removing tools
297 // ----------------------------------------------------------------------------
299 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
300 wxToolBarToolBase
*tool
)
302 // nothing special to do here - we really create the toolbar buttons in
309 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
311 // the main difficulty we have here is with the controls in the toolbars:
312 // as we (sometimes) use several separators to cover up the space used by
313 // them, the indices are not the same for us and the toolbar
315 // first determine the position of the first button to delete: it may be
316 // different from pos if we use several separators to cover the space used
318 wxToolBarToolsList::Node
*node
;
319 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
321 wxToolBarToolBase
*tool2
= node
->GetData();
324 // let node point to the next node in the list
325 node
= node
->GetNext();
330 if ( tool2
->IsControl() )
332 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount() - 1;
336 // now determine the number of buttons to delete and the area taken by them
337 size_t nButtonsToDelete
= 1;
339 // get the size of the button we're going to delete
341 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
, pos
, (LPARAM
)&r
) )
343 wxLogLastError(_T("TB_GETITEMRECT"));
346 int width
= r
.right
- r
.left
;
348 if ( tool
->IsControl() )
350 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
352 width
*= nButtonsToDelete
;
355 // do delete all buttons
356 m_nButtons
-= nButtonsToDelete
;
357 while ( nButtonsToDelete
-- > 0 )
359 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
361 wxLogLastError(wxT("TB_DELETEBUTTON"));
369 // and finally reposition all the controls after this button (the toolbar
370 // takes care of all normal items)
371 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
373 wxToolBarToolBase
*tool2
= node
->GetData();
374 if ( tool2
->IsControl() )
377 wxControl
*control
= tool2
->GetControl();
378 control
->GetPosition(&x
, NULL
);
379 control
->Move(x
- width
, -1);
386 bool wxToolBar::Realize()
388 size_t nTools
= GetToolsCount();
395 bool isVertical
= (GetWindowStyle() & wxTB_VERTICAL
) != 0;
397 // First, add the bitmap: we use one bitmap for all toolbar buttons
398 // ----------------------------------------------------------------
400 // if we already have a bitmap, we'll replace the existing one - otherwise
401 // we'll install a new one
402 HBITMAP oldToolBarBitmap
= (HBITMAP
)m_hBitmap
;
404 int totalBitmapWidth
= (int)(m_defaultWidth
* nTools
);
405 int totalBitmapHeight
= (int)m_defaultHeight
;
407 // Create a bitmap and copy all the tool bitmaps to it
409 wxMemoryDC dcAllButtons
;
410 wxBitmap
bitmap(totalBitmapWidth
, totalBitmapHeight
);
411 dcAllButtons
.SelectObject(bitmap
);
412 dcAllButtons
.SetBackground(*wxLIGHT_GREY_BRUSH
);
413 dcAllButtons
.Clear();
415 m_hBitmap
= bitmap
.GetHBITMAP();
416 HBITMAP hBitmap
= (HBITMAP
)m_hBitmap
;
417 #else // !USE_BITMAP_MASKS
418 HBITMAP hBitmap
= ::CreateCompatibleBitmap(ScreenHDC(),
423 wxLogLastError(_T("CreateCompatibleBitmap"));
428 m_hBitmap
= (WXHBITMAP
)hBitmap
;
430 HDC memoryDC
= ::CreateCompatibleDC(NULL
);
431 HBITMAP oldBitmap
= (HBITMAP
) ::SelectObject(memoryDC
, hBitmap
);
433 HDC memoryDC2
= ::CreateCompatibleDC(NULL
);
434 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
436 // the button position
439 // the number of buttons (not separators)
442 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
445 wxToolBarToolBase
*tool
= node
->GetData();
446 if ( tool
->IsButton() )
448 const wxBitmap
& bmp
= tool
->GetBitmap1();
452 // notice the last parameter: do use mask
453 dcAllButtons
.DrawBitmap(tool
->GetBitmap1(), x
, 0, TRUE
);
454 #else // !USE_BITMAP_MASKS
455 HBITMAP hbmp
= GetHbitmapOf(bmp
);
456 HBITMAP oldBitmap2
= (HBITMAP
)::SelectObject(memoryDC2
, hbmp
);
457 if ( !BitBlt(memoryDC
, x
, 0, m_defaultWidth
, m_defaultHeight
,
458 memoryDC2
, 0, 0, SRCCOPY
) )
460 wxLogLastError(wxT("BitBlt"));
463 ::SelectObject(memoryDC2
, oldBitmap2
);
464 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
468 wxFAIL_MSG( _T("invalid tool button bitmap") );
471 // still inc width and number of buttons because otherwise the
472 // subsequent buttons will all be shifted which is rather confusing
473 // (and like this you'd see immediately which bitmap was bad)
478 node
= node
->GetNext();
482 dcAllButtons
.SelectObject(wxNullBitmap
);
484 // don't delete this HBITMAP!
485 bitmap
.SetHBITMAP(0);
486 #else // !USE_BITMAP_MASKS
487 ::SelectObject(memoryDC
, oldBitmap
);
488 ::DeleteDC(memoryDC
);
489 ::DeleteDC(memoryDC2
);
490 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
492 // Map to system colours
493 MapBitmap((WXHBITMAP
) hBitmap
, totalBitmapWidth
, totalBitmapHeight
);
497 bool addBitmap
= TRUE
;
499 if ( oldToolBarBitmap
)
501 #ifdef TB_REPLACEBITMAP
502 if ( wxTheApp
->GetComCtl32Version() >= 400 )
504 TBREPLACEBITMAP replaceBitmap
;
505 replaceBitmap
.hInstOld
= NULL
;
506 replaceBitmap
.hInstNew
= NULL
;
507 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
508 replaceBitmap
.nIDNew
= (UINT
) hBitmap
;
509 replaceBitmap
.nButtons
= nButtons
;
510 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
511 0, (LPARAM
) &replaceBitmap
) )
513 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
516 ::DeleteObject(oldToolBarBitmap
);
522 #endif // TB_REPLACEBITMAP
524 // we can't replace the old bitmap, so we will add another one
525 // (awfully inefficient, but what else to do?) and shift the bitmap
526 // indices accordingly
529 bitmapId
= m_nButtons
;
532 // Now delete all the buttons
533 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
535 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
537 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
543 if ( addBitmap
) // no old bitmap or we can't replace it
545 TBADDBITMAP addBitmap
;
547 addBitmap
.nID
= (UINT
) hBitmap
;
548 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
549 (WPARAM
) nButtons
, (LPARAM
)&addBitmap
) == -1 )
551 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
555 // Next add the buttons and separators
556 // -----------------------------------
558 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
560 // this array will hold the indices of all controls in the toolbar
561 wxArrayInt controlIds
;
564 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
566 wxToolBarToolBase
*tool
= node
->GetData();
568 // don't add separators to the vertical toolbar - looks ugly
569 if ( isVertical
&& tool
->IsSeparator() )
572 TBBUTTON
& button
= buttons
[i
];
574 wxZeroMemory(button
);
576 switch ( tool
->GetStyle() )
578 case wxTOOL_STYLE_CONTROL
:
579 button
.idCommand
= tool
->GetId();
580 // fall through: create just a separator too
582 case wxTOOL_STYLE_SEPARATOR
:
583 button
.fsState
= TBSTATE_ENABLED
;
584 button
.fsStyle
= TBSTYLE_SEP
;
587 case wxTOOL_STYLE_BUTTON
:
588 button
.iBitmap
= bitmapId
;
589 button
.idCommand
= tool
->GetId();
591 if ( tool
->IsEnabled() )
592 button
.fsState
|= TBSTATE_ENABLED
;
593 if ( tool
->IsToggled() )
594 button
.fsState
|= TBSTATE_CHECKED
;
596 button
.fsStyle
= tool
->CanBeToggled() ? TBSTYLE_CHECK
606 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
,
607 (WPARAM
)i
, (LPARAM
)buttons
) )
609 wxLogLastError(wxT("TB_ADDBUTTONS"));
614 // Deal with the controls finally
615 // ------------------------------
617 // adjust the controls size to fit nicely in the toolbar
620 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
622 wxToolBarToolBase
*tool
= node
->GetData();
624 // we calculate the running y coord for vertical toolbars so we need to
625 // get the items size for all items but for the horizontal ones we
626 // don't need to deal with the non controls
627 bool isControl
= tool
->IsControl();
628 if ( !isControl
&& !isVertical
)
631 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
632 // latter only appeared in v4.70 of comctl32.dll
634 if ( !SendMessage(GetHwnd(), TB_GETITEMRECT
,
635 index
, (LPARAM
)(LPRECT
)&r
) )
637 wxLogLastError(wxT("TB_GETITEMRECT"));
642 // can only be control if isVertical
643 y
+= r
.bottom
- r
.top
;
648 wxControl
*control
= tool
->GetControl();
650 wxSize size
= control
->GetSize();
652 // the position of the leftmost controls corner
655 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
656 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
657 // available in headers, now check whether it is available now
659 if ( wxTheApp
->GetComCtl32Version() >= 471 )
661 // set the (underlying) separators width to be that of the
664 tbbi
.cbSize
= sizeof(tbbi
);
665 tbbi
.dwMask
= TBIF_SIZE
;
667 if ( !SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
668 tool
->GetId(), (LPARAM
)&tbbi
) )
670 // the id is probably invalid?
671 wxLogLastError(wxT("TB_SETBUTTONINFO"));
675 #endif // comctl32.dll 4.71
676 // TB_SETBUTTONINFO unavailable
678 // try adding several separators to fit the controls width
679 int widthSep
= r
.right
- r
.left
;
685 tbb
.fsState
= TBSTATE_ENABLED
;
686 tbb
.fsStyle
= TBSTYLE_SEP
;
688 size_t nSeparators
= size
.x
/ widthSep
;
689 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
691 if ( !SendMessage(GetHwnd(), TB_INSERTBUTTON
,
692 index
, (LPARAM
)&tbb
) )
694 wxLogLastError(wxT("TB_INSERTBUTTON"));
700 // remember the number of separators we used - we'd have to
701 // delete all of them later
702 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
704 // adjust the controls width to exactly cover the separators
705 control
->SetSize((nSeparators
+ 1)*widthSep
, -1);
708 // position the control itself correctly vertically
709 int height
= r
.bottom
- r
.top
;
710 int diff
= height
- size
.y
;
713 // the control is too high, resize to fit
714 control
->SetSize(-1, height
- 2);
725 y
+= height
+ 2*GetMargins().y
;
727 else // horizontal toolbar
735 control
->Move(left
, top
+ (diff
+ 1) / 2);
738 // the max index is the "real" number of buttons - i.e. counting even the
739 // separators which we added just for aligning the controls
744 if ( m_maxRows
== 0 )
746 // if not set yet, only one row
750 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
752 if ( m_maxRows
== 0 )
754 // if not set yet, have one column
762 // ----------------------------------------------------------------------------
764 // ----------------------------------------------------------------------------
766 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id
)
768 wxToolBarToolBase
*tool
= FindById((int)id
);
772 if ( tool
->CanBeToggled() )
774 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
775 tool
->Toggle((state
& TBSTATE_CHECKED
) != 0);
778 bool toggled
= tool
->IsToggled();
780 // OnLeftClick() can veto the button state change - for buttons which may
781 // be toggled only, of couse
782 if ( !OnLeftClick((int)id
, toggled
) && tool
->CanBeToggled() )
786 tool
->SetToggle(toggled
);
788 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(toggled
, 0));
794 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
796 WXLPARAM
*WXUNUSED(result
))
798 // First check if this applies to us
799 NMHDR
*hdr
= (NMHDR
*)lParam
;
801 // the tooltips control created by the toolbar is sometimes Unicode, even
802 // in an ANSI application - this seems to be a bug in comctl32.dll v5
803 int code
= (int)hdr
->code
;
804 if ( (code
!= TTN_NEEDTEXTA
) && (code
!= TTN_NEEDTEXTW
) )
807 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
808 if ( toolTipWnd
!= hdr
->hwndFrom
)
811 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
812 int id
= (int)ttText
->hdr
.idFrom
;
814 wxToolBarToolBase
*tool
= FindById(id
);
818 const wxString
& help
= tool
->GetShortHelp();
820 if ( !help
.IsEmpty() )
822 if ( code
== TTN_NEEDTEXTA
)
824 ttText
->lpszText
= (wxChar
*)help
.c_str();
829 ttText
->lpszText
= (wxChar
*)help
.c_str();
831 // VZ: I don't know why it happens, but the versions of
832 // comctl32.dll starting from 4.70 sometimes send TTN_NEEDTEXTW
833 // even to ANSI programs (normally, this message is supposed
834 // to be sent to Unicode programs only) - hence we need to
835 // handle it as well, otherwise no tooltips will be shown in
838 size_t lenAnsi
= help
.Len();
839 #if defined( __MWERKS__ ) || defined( __CYGWIN__ )
840 // MetroWerks doesn't like calling mbstowcs with NULL argument
841 // neither Cygwin does
842 size_t lenUnicode
= 2*lenAnsi
;
844 size_t lenUnicode
= mbstowcs(NULL
, help
, lenAnsi
);
847 // using the pointer of right type avoids us doing all sorts of
848 // pointer arithmetics ourselves
849 wchar_t *dst
= (wchar_t *)ttText
->szText
,
850 *pwz
= new wchar_t[lenUnicode
+ 1];
851 mbstowcs(pwz
, help
, lenAnsi
+ 1);
852 memcpy(dst
, pwz
, lenUnicode
*sizeof(wchar_t));
854 // put the terminating _wide_ NUL
865 // ----------------------------------------------------------------------------
867 // ----------------------------------------------------------------------------
869 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
871 wxToolBarBase::SetToolBitmapSize(size
);
873 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
876 void wxToolBar::SetRows(int nRows
)
878 if ( nRows
== m_maxRows
)
880 // avoid resizing the frame uselessly
884 // TRUE in wParam means to create at least as many rows, FALSE -
887 ::SendMessage(GetHwnd(), TB_SETROWS
,
888 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
896 // The button size is bigger than the bitmap size
897 wxSize
wxToolBar::GetToolSize() const
899 // TB_GETBUTTONSIZE is supported from version 4.70
900 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
901 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
902 if ( wxTheApp
->GetComCtl32Version() >= 470 )
904 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
906 return wxSize(LOWORD(dw
), HIWORD(dw
));
909 #endif // comctl32.dll 4.70+
912 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
917 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
920 wxToolBarToolsList::Node
* current
= tools
.GetFirst();
922 for ( ; current
!= 0; current
= current
->GetNext() )
925 return current
->GetData();
927 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
928 size_t separators
= tool
->GetSeparatorsCount();
930 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
931 // otherwise, skip as many items as the separator count, plus the
933 index
-= separators
? separators
+ 1 : 1;
939 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
944 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
945 // MBN: when the point ( x, y ) is close to the toolbar border
946 // TB_HITTEST returns m_nButtons ( not -1 )
947 if ( index
< 0 || (size_t)index
>= m_nButtons
)
949 // it's a separator or there is no tool at all there
950 return (wxToolBarToolBase
*)NULL
;
953 // if comctl32 version < 4.71 wxToolBar95 adds dummy spacers
954 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
955 if ( wxTheApp
->GetComCtl32Version() >= 471 )
957 return m_tools
.Item((size_t)index
)->GetData();
962 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
966 void wxToolBar::UpdateSize()
968 // the toolbar size changed
969 SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
971 // we must also refresh the frame after the toolbar size (possibly) changed
972 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
975 frame
->SendSizeEvent();
979 // ----------------------------------------------------------------------------
981 // ----------------------------------------------------------------------------
983 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
985 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
986 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
989 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
991 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
992 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
995 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
997 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
998 // without, so we really need to delete the button and recreate it here
999 wxFAIL_MSG( _T("not implemented") );
1002 // ----------------------------------------------------------------------------
1004 // ----------------------------------------------------------------------------
1006 // Responds to colour changes, and passes event on to children.
1007 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1009 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
1011 // Remap the buttons
1014 // Relayout the toolbar
1015 int nrows
= m_maxRows
;
1016 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
1021 // let the event propagate further
1025 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
1027 if (event
.RightDown())
1029 // For now, we don't have an id. Later we could
1030 // try finding the tool.
1031 OnRightClick((int)-1, event
.GetX(), event
.GetY());
1039 long wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1041 if ( nMsg
== WM_SIZE
)
1043 // calculate our minor dimenstion ourselves - we're confusing the
1044 // standard logic (TB_AUTOSIZE) with our horizontal toolbars and other
1047 if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT
, 0, (LPARAM
)&r
) )
1051 if ( GetWindowStyle() & wxTB_VERTICAL
)
1053 w
= r
.right
- r
.left
;
1056 w
*= (m_nButtons
+ m_maxRows
- 1)/m_maxRows
;
1063 h
= r
.bottom
- r
.top
;
1066 h
+= 6; // FIXME: this is the separator line height...
1071 if ( MAKELPARAM(w
, h
) != lParam
)
1073 // size really changed
1077 // message processed
1081 else if ( nMsg
== WM_MOUSEMOVE
)
1083 wxCoord x
= GET_X_LPARAM(lParam
), y
= GET_Y_LPARAM(lParam
);
1084 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1086 // cursor left current tool
1087 if( tool
!= m_pInTool
&& !tool
)
1093 // cursor entered a tool
1094 if( tool
!= m_pInTool
&& tool
)
1097 OnMouseEnter( tool
->GetId() );
1100 // we don't handle mouse moves, so fall through
1101 // to wxControl::MSWWindowProc
1104 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1107 // ----------------------------------------------------------------------------
1108 // private functions
1109 // ----------------------------------------------------------------------------
1111 bool wxToolBar::sm_coloursInit
= FALSE
;
1112 long wxToolBar::sm_stdColours
[6];
1114 void wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1116 if (!sm_coloursInit
)
1118 // When a bitmap is loaded, the RGB values can change. So we need to have a
1119 // reference bitmap which can tell us what the RGB values change to.
1120 wxBitmap
stdColourBitmap("wxBITMAP_STD_COLOURS", wxBITMAP_TYPE_RESOURCE
);
1121 if (stdColourBitmap
.Ok())
1124 memDC
.SelectObject(stdColourBitmap
);
1128 for (i
= 0; i
< 6; i
++)
1130 memDC
.GetPixel(i
, 0, & colour
);
1131 sm_stdColours
[i
] = RGB(colour
.Red(), colour
.Green(), colour
.Blue());
1133 sm_coloursInit
= TRUE
;
1134 memDC
.SelectObject(wxNullBitmap
);
1138 sm_stdColours
[0] = RGB(000,000,000) ;
1139 sm_stdColours
[1] = RGB(128,128,128) ;
1140 sm_stdColours
[2] = RGB(192,192,192) ;
1141 sm_stdColours
[3] = RGB(255,255,255) ;
1142 sm_stdColours
[4] = RGB(000,000,255) ;
1143 sm_stdColours
[5] = RGB(255,000,255) ;
1144 sm_coloursInit
= TRUE
;
1148 HBITMAP hBitmap
= (HBITMAP
) bitmap
;
1150 COLORMAP ColorMap
[5];
1152 ColorMap
[0].from
= sm_stdColours
[0]; ColorMap
[0].to
= COLOR_BTNTEXT
; // black (0, 0 0)
1153 ColorMap
[1].from
= sm_stdColours
[1]; ColorMap
[1].to
= COLOR_BTNSHADOW
; // dark grey (128, 128, 128)
1154 ColorMap
[2].from
= sm_stdColours
[2]; ColorMap
[2].to
= COLOR_BTNFACE
; // bright grey (192, 192, 192)
1155 ColorMap
[3].from
= sm_stdColours
[3]; ColorMap
[3].to
= COLOR_BTNHIGHLIGHT
; // white (255, 255, 255)
1156 // ColorMap[4].from = sm_stdColours[4]; ColorMap[4].to = COLOR_HIGHLIGHT; // blue (0, 0, 255)
1157 ColorMap
[4].from
= sm_stdColours
[5]; ColorMap
[4].to
= COLOR_WINDOW
; // magenta (255, 0, 255)
1159 for ( size_t n
= 0; n
< WXSIZEOF(ColorMap
); n
++)
1161 ColorMap
[n
].to
= ::GetSysColor(ColorMap
[n
].to
);
1165 HDC hdcMem
= CreateCompatibleDC(NULL
);
1169 hbmOld
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
1171 for ( int i
= 0; i
< width
; i
++ )
1173 for ( int j
= 0; j
< height
; j
++ )
1175 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1177 for ( size_t k
= 0; k
< WXSIZEOF(ColorMap
); k
++ )
1179 int distance
= abs( GetRValue( pixel
) - GetRValue( ColorMap
[k
].from
)) ;
1180 distance
= max( distance
, abs(GetGValue(pixel
) - GetGValue( ColorMap
[k
].from
))) ;
1181 distance
= max( distance
, abs(GetBValue(pixel
) - GetBValue( ColorMap
[k
].from
))) ;
1182 if ( distance
< 0x10 )
1184 ::SetPixel(hdcMem
, i
, j
, ColorMap
[k
].to
);
1192 SelectObject(hdcMem
, hbmOld
);
1193 DeleteObject(hdcMem
);
1197 // Some experiments...
1199 // What we want to do is create another bitmap which has a depth of 4,
1200 // and set the bits. So probably we want to convert this HBITMAP into a
1201 // DIB, then call SetDIBits.
1202 // AAAGH. The stupid thing is that if newBitmap has a depth of 4 (less than that of
1203 // the screen), then SetDIBits fails.
1204 HBITMAP newBitmap
= ::CreateBitmap(totalBitmapWidth
, totalBitmapHeight
, 1, 4, NULL
);
1205 HANDLE newDIB
= ::BitmapToDIB((HBITMAP
) m_hBitmap
, NULL
);
1206 LPBITMAPINFOHEADER lpbmi
= (LPBITMAPINFOHEADER
) GlobalLock(newDIB
);
1209 // LPBITMAPINFOHEADER lpbmi = (LPBITMAPINFOHEADER) newDIB;
1211 int result
= ::SetDIBits(dc
, newBitmap
, 0, lpbmi
->biHeight
, FindDIBBits((LPSTR
)lpbmi
), (LPBITMAPINFO
)lpbmi
,
1213 DWORD err
= GetLastError();
1215 ::ReleaseDC(NULL
, dc
);
1218 GlobalUnlock (newDIB
);
1219 GlobalFree (newDIB
);
1221 // WXHBITMAP hBitmap2 = wxCreateMappedBitmap((WXHINSTANCE) wxGetInstance(), (WXHBITMAP) m_hBitmap);
1222 // Substitute our new bitmap for the old one
1223 ::DeleteObject((HBITMAP
) m_hBitmap
);
1224 m_hBitmap
= (WXHBITMAP
) newBitmap
;
1228 #endif // wxUSE_TOOLBAR && Win95