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