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