]> git.saurik.com Git - wxWidgets.git/blob - src/msw/tbar95.cpp
Patch from Tim Kosse to add supoprt for wxListCtrl::OnGetItemColumnImage
[wxWidgets.git] / src / msw / tbar95.cpp
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
97 IMPLEMENT_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
118 BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
119 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent)
120 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged)
121 EVT_ERASE_BACKGROUND(wxToolBar::OnEraseBackground)
122 END_EVENT_TABLE()
123
124 // ----------------------------------------------------------------------------
125 // private classes
126 // ----------------------------------------------------------------------------
127
128 class wxToolBarTool : public wxToolBarToolBase
129 {
130 public:
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
170 private:
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
185 wxToolBarToolBase *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
198 wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
199 {
200 return new wxToolBarTool(this, control);
201 }
202
203 // ----------------------------------------------------------------------------
204 // wxToolBar construction
205 // ----------------------------------------------------------------------------
206
207 void 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
220 bool 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
256 bool 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
267 void 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
320 wxToolBar::~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
334 wxSize 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
366 WXDWORD 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
411 bool 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
421 bool 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
497 void 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
532 bool 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 SetBestFittingSize();
1053 UpdateSize();
1054
1055 return true;
1056 }
1057
1058 // ----------------------------------------------------------------------------
1059 // message handlers
1060 // ----------------------------------------------------------------------------
1061
1062 bool wxToolBar::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD id)
1063 {
1064 wxToolBarToolBase *tool = FindById((int)id);
1065 if ( !tool )
1066 return false;
1067
1068 bool toggled = false; // just to suppress warnings
1069
1070 if ( tool->CanBeToggled() )
1071 {
1072 LRESULT state = ::SendMessage(GetHwnd(), TB_GETSTATE, id, 0);
1073 toggled = (state & TBSTATE_CHECKED) != 0;
1074
1075 // ignore the event when a radio button is released, as this doesn't
1076 // seem to happen at all, and is handled otherwise
1077 if ( tool->GetKind() == wxITEM_RADIO && !toggled )
1078 return true;
1079
1080 tool->Toggle(toggled);
1081 UnToggleRadioGroup(tool);
1082 }
1083
1084 // OnLeftClick() can veto the button state change - for buttons which
1085 // may be toggled only, of couse
1086 if ( !OnLeftClick((int)id, toggled) && tool->CanBeToggled() )
1087 {
1088 // revert back
1089 tool->Toggle(!toggled);
1090
1091 ::SendMessage(GetHwnd(), TB_CHECKBUTTON, id, MAKELONG(!toggled, 0));
1092 }
1093
1094 return true;
1095 }
1096
1097 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl),
1098 WXLPARAM lParam,
1099 WXLPARAM *WXUNUSED(result))
1100 {
1101 #if wxUSE_TOOLTIPS
1102 // First check if this applies to us
1103 NMHDR *hdr = (NMHDR *)lParam;
1104
1105 // the tooltips control created by the toolbar is sometimes Unicode, even
1106 // in an ANSI application - this seems to be a bug in comctl32.dll v5
1107 UINT code = hdr->code;
1108 if ( (code != (UINT) TTN_NEEDTEXTA) && (code != (UINT) TTN_NEEDTEXTW) )
1109 return false;
1110
1111 HWND toolTipWnd = (HWND)::SendMessage((HWND)GetHWND(), TB_GETTOOLTIPS, 0, 0);
1112 if ( toolTipWnd != hdr->hwndFrom )
1113 return false;
1114
1115 LPTOOLTIPTEXT ttText = (LPTOOLTIPTEXT)lParam;
1116 int id = (int)ttText->hdr.idFrom;
1117
1118 wxToolBarToolBase *tool = FindById(id);
1119 if ( !tool )
1120 return false;
1121
1122 return HandleTooltipNotify(code, lParam, tool->GetShortHelp());
1123 #else
1124 wxUnusedVar(lParam);
1125
1126 return false;
1127 #endif
1128 }
1129
1130 // ----------------------------------------------------------------------------
1131 // toolbar geometry
1132 // ----------------------------------------------------------------------------
1133
1134 void wxToolBar::SetToolBitmapSize(const wxSize& size)
1135 {
1136 wxToolBarBase::SetToolBitmapSize(size);
1137
1138 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE, 0, MAKELONG(size.x, size.y));
1139 }
1140
1141 void wxToolBar::SetRows(int nRows)
1142 {
1143 if ( nRows == m_maxRows )
1144 {
1145 // avoid resizing the frame uselessly
1146 return;
1147 }
1148
1149 // TRUE in wParam means to create at least as many rows, FALSE -
1150 // at most as many
1151 RECT rect;
1152 ::SendMessage(GetHwnd(), TB_SETROWS,
1153 MAKEWPARAM(nRows, !(GetWindowStyle() & wxTB_VERTICAL)),
1154 (LPARAM) &rect);
1155
1156 m_maxRows = nRows;
1157
1158 UpdateSize();
1159 }
1160
1161 // The button size is bigger than the bitmap size
1162 wxSize wxToolBar::GetToolSize() const
1163 {
1164 // TB_GETBUTTONSIZE is supported from version 4.70
1165 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
1166 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \
1167 && !defined (__DIGITALMARS__)
1168 if ( wxApp::GetComCtl32Version() >= 470 )
1169 {
1170 DWORD dw = ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE, 0, 0);
1171
1172 return wxSize(LOWORD(dw), HIWORD(dw));
1173 }
1174 else
1175 #endif // comctl32.dll 4.70+
1176 {
1177 // defaults
1178 return wxSize(m_defaultWidth + 8, m_defaultHeight + 7);
1179 }
1180 }
1181
1182 static
1183 wxToolBarToolBase *GetItemSkippingDummySpacers(const wxToolBarToolsList& tools,
1184 size_t index )
1185 {
1186 wxToolBarToolsList::compatibility_iterator current = tools.GetFirst();
1187
1188 for ( ; current ; current = current->GetNext() )
1189 {
1190 if ( index == 0 )
1191 return current->GetData();
1192
1193 wxToolBarTool *tool = (wxToolBarTool *)current->GetData();
1194 size_t separators = tool->GetSeparatorsCount();
1195
1196 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1197 // otherwise, skip as many items as the separator count, plus the
1198 // control itself
1199 index -= separators ? separators + 1 : 1;
1200 }
1201
1202 return 0;
1203 }
1204
1205 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
1206 {
1207 POINT pt;
1208 pt.x = x;
1209 pt.y = y;
1210 int index = (int)::SendMessage(GetHwnd(), TB_HITTEST, 0, (LPARAM)&pt);
1211
1212 // MBN: when the point ( x, y ) is close to the toolbar border
1213 // TB_HITTEST returns m_nButtons ( not -1 )
1214 if ( index < 0 || (size_t)index >= m_nButtons )
1215 // it's a separator or there is no tool at all there
1216 return (wxToolBarToolBase *)NULL;
1217
1218 // when TB_SETBUTTONINFO is available (both during compile- and run-time),
1219 // we don't use the dummy separators hack
1220 #ifdef TB_SETBUTTONINFO
1221 if ( wxApp::GetComCtl32Version() >= 471 )
1222 {
1223 return m_tools.Item((size_t)index)->GetData();
1224 }
1225 else
1226 #endif // TB_SETBUTTONINFO
1227 {
1228 return GetItemSkippingDummySpacers( m_tools, (size_t) index );
1229 }
1230 }
1231
1232 void wxToolBar::UpdateSize()
1233 {
1234 // In case Realize is called after the initial display (IOW the programmer
1235 // may have rebuilt the toolbar) give the frame the option of resizing the
1236 // toolbar to full width again, but only if the parent is a frame and the
1237 // toolbar is managed by the frame. Otherwise assume that some other
1238 // layout mechanism is controlling the toolbar size and leave it alone.
1239
1240 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
1241 if ( frame && frame->GetToolBar() == this )
1242 {
1243 // the toolbar size changed
1244 ::SendMessage(GetHwnd(), TB_AUTOSIZE, 0, 0);
1245 frame->SendSizeEvent();
1246 }
1247 }
1248
1249 // ----------------------------------------------------------------------------
1250 // toolbar styles
1251 // ---------------------------------------------------------------------------
1252
1253 void wxToolBar::SetWindowStyleFlag(long style)
1254 {
1255 // the style bits whose changes force us to recreate the toolbar
1256 static const long MASK_NEEDS_RECREATE = wxTB_TEXT | wxTB_NOICONS;
1257
1258 const long styleOld = GetWindowStyle();
1259
1260 wxToolBarBase::SetWindowStyleFlag(style);
1261
1262 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1263 // also fatal as we'd then try to recreate the toolbar when it's just being
1264 // created
1265 if ( GetToolsCount() &&
1266 (style & MASK_NEEDS_RECREATE) != (styleOld & MASK_NEEDS_RECREATE) )
1267 {
1268 // to remove the text labels, simply re-realizing the toolbar is enough
1269 // but I don't know of any way to add the text to an existing toolbar
1270 // other than by recreating it entirely
1271 Recreate();
1272 }
1273 }
1274
1275 // ----------------------------------------------------------------------------
1276 // tool state
1277 // ----------------------------------------------------------------------------
1278
1279 void wxToolBar::DoEnableTool(wxToolBarToolBase *tool, bool enable)
1280 {
1281 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON,
1282 (WPARAM)tool->GetId(), (LPARAM)MAKELONG(enable, 0));
1283 }
1284
1285 void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool toggle)
1286 {
1287 ::SendMessage(GetHwnd(), TB_CHECKBUTTON,
1288 (WPARAM)tool->GetId(), (LPARAM)MAKELONG(toggle, 0));
1289 }
1290
1291 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
1292 {
1293 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1294 // without, so we really need to delete the button and recreate it here
1295 wxFAIL_MSG( _T("not implemented") );
1296 }
1297
1298 // ----------------------------------------------------------------------------
1299 // event handlers
1300 // ----------------------------------------------------------------------------
1301
1302 // Responds to colour changes, and passes event on to children.
1303 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent& event)
1304 {
1305 wxRGBToColour(m_backgroundColour, ::GetSysColor(COLOR_BTNFACE));
1306
1307 // Remap the buttons
1308 Realize();
1309
1310 // Relayout the toolbar
1311 int nrows = m_maxRows;
1312 m_maxRows = 0; // otherwise SetRows() wouldn't do anything
1313 SetRows(nrows);
1314
1315 Refresh();
1316
1317 // let the event propagate further
1318 event.Skip();
1319 }
1320
1321 void wxToolBar::OnMouseEvent(wxMouseEvent& event)
1322 {
1323 if (event.Leaving() && m_pInTool)
1324 {
1325 OnMouseEnter( -1 );
1326 event.Skip();
1327 return;
1328 }
1329
1330 if ( event.RightDown() )
1331 {
1332 // find the tool under the mouse
1333 wxCoord x,y;
1334 event.GetPosition(&x, &y);
1335
1336 wxToolBarToolBase *tool = FindToolForPosition(x, y);
1337 OnRightClick(tool ? tool->GetId() : -1, x, y);
1338 }
1339 else
1340 {
1341 event.Skip();
1342 }
1343 }
1344
1345 // This handler is required to allow the toolbar to be set to a non-default
1346 // colour: for example, when it must blend in with a notebook page.
1347 void wxToolBar::OnEraseBackground(wxEraseEvent& event)
1348 {
1349 wxColour bgCol = GetBackgroundColour();
1350 if (!bgCol.Ok())
1351 {
1352 event.Skip();
1353 return;
1354 }
1355
1356 // notice that this 'dumb' implementation may cause flicker for some of the
1357 // controls in which case they should intercept wxEraseEvent and process it
1358 // themselves somehow
1359
1360 RECT rect;
1361 ::GetClientRect(GetHwnd(), &rect);
1362
1363 HBRUSH hBrush = ::CreateSolidBrush(wxColourToRGB(bgCol));
1364
1365 HDC hdc = GetHdcOf((*event.GetDC()));
1366
1367 #ifndef __WXWINCE__
1368 int mode = ::SetMapMode(hdc, MM_TEXT);
1369 #endif
1370
1371 ::FillRect(hdc, &rect, hBrush);
1372 ::DeleteObject(hBrush);
1373
1374 #ifndef __WXWINCE__
1375 ::SetMapMode(hdc, mode);
1376 #endif
1377 }
1378
1379 bool wxToolBar::HandleSize(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam)
1380 {
1381 // calculate our minor dimension ourselves - we're confusing the standard
1382 // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks
1383 RECT r;
1384 if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT, 0, (LPARAM)&r) )
1385 {
1386 int w, h;
1387
1388 if ( GetWindowStyle() & wxTB_VERTICAL )
1389 {
1390 w = r.right - r.left;
1391 if ( m_maxRows )
1392 {
1393 w *= (m_nButtons + m_maxRows - 1)/m_maxRows;
1394 }
1395 h = HIWORD(lParam);
1396 }
1397 else
1398 {
1399 w = LOWORD(lParam);
1400 if (HasFlag( wxTB_FLAT ))
1401 h = r.bottom - r.top - 3;
1402 else
1403 h = r.bottom - r.top;
1404 if ( m_maxRows )
1405 {
1406 // FIXME: hardcoded separator line height...
1407 h += HasFlag(wxTB_NODIVIDER) ? 4 : 6;
1408 h *= m_maxRows;
1409 }
1410 }
1411
1412 if ( MAKELPARAM(w, h) != lParam )
1413 {
1414 // size really changed
1415 SetSize(w, h);
1416 }
1417
1418 // message processed
1419 return true;
1420 }
1421
1422 return false;
1423 }
1424
1425 bool wxToolBar::HandlePaint(WXWPARAM wParam, WXLPARAM lParam)
1426 {
1427 // erase any dummy separators which were used
1428 // for aligning the controls if any here
1429
1430 // first of all, are there any controls at all?
1431 wxToolBarToolsList::compatibility_iterator node;
1432 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1433 {
1434 if ( node->GetData()->IsControl() )
1435 break;
1436 }
1437
1438 if ( !node )
1439 // no controls, nothing to erase
1440 return false;
1441
1442 // prepare the DC on which we'll be drawing
1443 wxClientDC dc(this);
1444 dc.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID));
1445 dc.SetPen(*wxTRANSPARENT_PEN);
1446
1447 RECT r;
1448 if ( !::GetUpdateRect(GetHwnd(), &r, FALSE) )
1449 // nothing to redraw anyhow
1450 return false;
1451
1452 wxRect rectUpdate;
1453 wxCopyRECTToRect(r, rectUpdate);
1454
1455 dc.SetClippingRegion(rectUpdate);
1456
1457 // draw the toolbar tools, separators &c normally
1458 wxControl::MSWWindowProc(WM_PAINT, wParam, lParam);
1459
1460 // for each control in the toolbar find all the separators intersecting it
1461 // and erase them
1462 //
1463 // NB: this is really the only way to do it as we don't know if a separator
1464 // corresponds to a control (i.e. is a dummy one) or a real one
1465 // otherwise
1466 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1467 {
1468 wxToolBarToolBase *tool = node->GetData();
1469 if ( tool->IsControl() )
1470 {
1471 // get the control rect in our client coords
1472 wxControl *control = tool->GetControl();
1473 wxRect rectCtrl = control->GetRect();
1474
1475 // iterate over all buttons
1476 TBBUTTON tbb;
1477 int count = ::SendMessage(GetHwnd(), TB_BUTTONCOUNT, 0, 0);
1478 for ( int n = 0; n < count; n++ )
1479 {
1480 // is it a separator?
1481 if ( !::SendMessage(GetHwnd(), TB_GETBUTTON,
1482 n, (LPARAM)&tbb) )
1483 {
1484 wxLogDebug(_T("TB_GETBUTTON failed?"));
1485
1486 continue;
1487 }
1488
1489 if ( tbb.fsStyle != TBSTYLE_SEP )
1490 continue;
1491
1492 // get the bounding rect of the separator
1493 RECT r;
1494 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT,
1495 n, (LPARAM)&r) )
1496 {
1497 wxLogDebug(_T("TB_GETITEMRECT failed?"));
1498
1499 continue;
1500 }
1501
1502 // does it intersect the control?
1503 wxRect rectItem;
1504 wxCopyRECTToRect(r, rectItem);
1505 if ( rectCtrl.Intersects(rectItem) )
1506 {
1507 // yes, do erase it!
1508 dc.DrawRectangle(rectItem);
1509
1510 // Necessary in case we use a no-paint-on-size
1511 // style in the parent: the controls can disappear
1512 control->Refresh(false);
1513 }
1514 }
1515 }
1516 }
1517
1518 return true;
1519 }
1520
1521 void wxToolBar::HandleMouseMove(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam)
1522 {
1523 wxCoord x = GET_X_LPARAM(lParam),
1524 y = GET_Y_LPARAM(lParam);
1525 wxToolBarToolBase* tool = FindToolForPosition( x, y );
1526
1527 // cursor left current tool
1528 if ( tool != m_pInTool && !tool )
1529 {
1530 m_pInTool = 0;
1531 OnMouseEnter( -1 );
1532 }
1533
1534 // cursor entered a tool
1535 if ( tool != m_pInTool && tool )
1536 {
1537 m_pInTool = tool;
1538 OnMouseEnter( tool->GetId() );
1539 }
1540 }
1541
1542 WXLRESULT wxToolBar::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
1543 {
1544 switch ( nMsg )
1545 {
1546 case WM_MOUSEMOVE:
1547 // we don't handle mouse moves, so always pass the message to
1548 // wxControl::MSWWindowProc (HandleMouseMove just calls OnMouseEnter)
1549 HandleMouseMove(wParam, lParam);
1550 break;
1551
1552 case WM_SIZE:
1553 if ( HandleSize(wParam, lParam) )
1554 return 0;
1555 break;
1556
1557 #ifndef __WXWINCE__
1558 case WM_PAINT:
1559 if ( HandlePaint(wParam, lParam) )
1560 return 0;
1561 #endif
1562
1563 default:
1564 break;
1565 }
1566
1567 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
1568 }
1569
1570 // ----------------------------------------------------------------------------
1571 // private functions
1572 // ----------------------------------------------------------------------------
1573
1574 WXHBITMAP wxToolBar::MapBitmap(WXHBITMAP bitmap, int width, int height)
1575 {
1576 MemoryHDC hdcMem;
1577
1578 if ( !hdcMem )
1579 {
1580 wxLogLastError(_T("CreateCompatibleDC"));
1581
1582 return bitmap;
1583 }
1584
1585 SelectInHDC bmpInHDC(hdcMem, (HBITMAP)bitmap);
1586
1587 if ( !bmpInHDC )
1588 {
1589 wxLogLastError(_T("SelectObject"));
1590
1591 return bitmap;
1592 }
1593
1594 wxCOLORMAP *cmap = wxGetStdColourMap();
1595
1596 for ( int i = 0; i < width; i++ )
1597 {
1598 for ( int j = 0; j < height; j++ )
1599 {
1600 COLORREF pixel = ::GetPixel(hdcMem, i, j);
1601
1602 for ( size_t k = 0; k < wxSTD_COL_MAX; k++ )
1603 {
1604 COLORREF col = cmap[k].from;
1605 if ( abs(GetRValue(pixel) - GetRValue(col)) < 10 &&
1606 abs(GetGValue(pixel) - GetGValue(col)) < 10 &&
1607 abs(GetBValue(pixel) - GetBValue(col)) < 10 )
1608 {
1609 ::SetPixel(hdcMem, i, j, cmap[k].to);
1610 break;
1611 }
1612 }
1613 }
1614 }
1615
1616 return bitmap;
1617
1618 // VZ: I leave here my attempts to map the bitmap to the system colours
1619 // faster by using BitBlt() even though it's broken currently - but
1620 // maybe someone else can finish it? It should be faster than iterating
1621 // over all pixels...
1622 #if 0
1623 MemoryHDC hdcMask, hdcDst;
1624 if ( !hdcMask || !hdcDst )
1625 {
1626 wxLogLastError(_T("CreateCompatibleDC"));
1627
1628 return bitmap;
1629 }
1630
1631 // create the target bitmap
1632 HBITMAP hbmpDst = ::CreateCompatibleBitmap(hdcDst, width, height);
1633 if ( !hbmpDst )
1634 {
1635 wxLogLastError(_T("CreateCompatibleBitmap"));
1636
1637 return bitmap;
1638 }
1639
1640 // create the monochrome mask bitmap
1641 HBITMAP hbmpMask = ::CreateBitmap(width, height, 1, 1, 0);
1642 if ( !hbmpMask )
1643 {
1644 wxLogLastError(_T("CreateBitmap(mono)"));
1645
1646 ::DeleteObject(hbmpDst);
1647
1648 return bitmap;
1649 }
1650
1651 SelectInHDC bmpInDst(hdcDst, hbmpDst),
1652 bmpInMask(hdcMask, hbmpMask);
1653
1654 // for each colour:
1655 for ( n = 0; n < NUM_OF_MAPPED_COLOURS; n++ )
1656 {
1657 // create the mask for this colour
1658 ::SetBkColor(hdcMem, ColorMap[n].from);
1659 ::BitBlt(hdcMask, 0, 0, width, height, hdcMem, 0, 0, SRCCOPY);
1660
1661 // replace this colour with the target one in the dst bitmap
1662 HBRUSH hbr = ::CreateSolidBrush(ColorMap[n].to);
1663 HGDIOBJ hbrOld = ::SelectObject(hdcDst, hbr);
1664
1665 ::MaskBlt(hdcDst, 0, 0, width, height,
1666 hdcMem, 0, 0,
1667 hbmpMask, 0, 0,
1668 MAKEROP4(PATCOPY, SRCCOPY));
1669
1670 (void)::SelectObject(hdcDst, hbrOld);
1671 ::DeleteObject(hbr);
1672 }
1673
1674 ::DeleteObject((HBITMAP)bitmap);
1675
1676 return (WXHBITMAP)hbmpDst;
1677 #endif // 0
1678 }
1679
1680 #endif // wxUSE_TOOLBAR
1681