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