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